SPM: Split S-EL1 shim from S-EL0 application

The shim prepares the S-EL1/0 environment (stack, VBAR, MMU, I-cache,
BSS clear, PIE fixup) for usage by the S-EL0 application. Split ivy
and shim parition code into separate folders.

Signed-off-by: Ruari Phipps <ruari.phipps@arm.com>
Signed-off-by: Olivier Deprez <olivier.deprez@arm.com>
Signed-off-by: Daniel Boulby <daniel.boulby@arm.com>
Change-Id: I203bbc0d379b12bd5cf1991b95e4f3a7d78d63d4
diff --git a/spm/ivy/app/aarch64/ivy_entrypoint.S b/spm/ivy/app/aarch64/ivy_entrypoint.S
new file mode 100644
index 0000000..d981d6a
--- /dev/null
+++ b/spm/ivy/app/aarch64/ivy_entrypoint.S
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <asm_macros.S>
+#include <ivy_def.h>
+#include <platform_def.h>
+
+	.globl	ivy_entrypoint
+
+.section .bss.stacks
+	.balign CACHE_WRITEBACK_GRANULE
+	.fill	IVY_STACKS_SIZE
+stacks_end:
+
+func ivy_entrypoint
+
+	/* Setup the stack pointer. */
+	adr	x0, stacks_end
+	mov	sp, x0
+
+	/* And jump to the C entrypoint. */
+	b	ivy_main
+
+endfunc ivy_entrypoint
diff --git a/spm/ivy/app/ivy.dts b/spm/ivy/app/ivy.dts
new file mode 100644
index 0000000..49a84bd
--- /dev/null
+++ b/spm/ivy/app/ivy.dts
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * This file is a Partition Manifest (PM) for a minimal Secure Partition (SP)
+ * that has additional optional properties defined.
+ */
+
+
+/dts-v1/;
+
+/ {
+	compatible = "arm,ffa-manifest-1.0";
+
+	/* Properties */
+	description = "ivy-1";
+	ffa-version = <0x00010000>; /* 31:16 - Major, 15:0 - Minor */
+	uuid = <0xeaba83d8 0xbaaf4eaf 0x8144f7fd 0xcbe544a7>;
+	execution-ctx-count = <1>;
+	exception-level = <2>; /* S-EL1 */
+	execution-state = <0>; /* AARCH64 */
+	load-address = <0x7600000>;
+	entrypoint-offset = <0x00001000>;
+	xlat-granule = <0>; /* 4KiB */
+	boot-order = <0>;
+	messaging-method = <0>; /* Direct messaging only */
+	run-time-model = <1>; /* SP pre-emptible */
+
+	/* Boot protocol */
+	gp-register-num = <0x0>;
+};
diff --git a/spm/ivy/app/ivy.h b/spm/ivy/app/ivy.h
new file mode 100644
index 0000000..a40f7e1
--- /dev/null
+++ b/spm/ivy/app/ivy.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef IVY_H
+#define IVY_H
+
+#include <stdint.h>
+
+/* Linker symbols used to figure out the memory layout of the S-EL1 shim. */
+extern uintptr_t __SHIM_TEXT_START__, __SHIM_TEXT_END__;
+#define SHIM_TEXT_START		((uintptr_t)&__SHIM_TEXT_START__)
+#define SHIM_TEXT_END		((uintptr_t)&__SHIM_TEXT_END__)
+
+extern uintptr_t __SHIM_RODATA_START__, __SHIM_RODATA_END__;
+#define SHIM_RODATA_START	((uintptr_t)&__SHIM_RODATA_START__)
+#define SHIM_RODATA_END		((uintptr_t)&__SHIM_RODATA_END__)
+
+extern uintptr_t __SHIM_DATA_START__, __SHIM_DATA_END__;
+#define SHIM_DATA_START		((uintptr_t)&__SHIM_DATA_START__)
+#define SHIM_DATA_END		((uintptr_t)&__SHIM_DATA_END__)
+
+extern uintptr_t __SHIM_BSS_START__, __SHIM_BSS_END__;
+#define SHIM_BSS_START		((uintptr_t)&__SHIM_BSS_START__)
+#define SHIM_BSS_END		((uintptr_t)&__SHIM_BSS_END__)
+
+/* Linker symbols used to figure out the memory layout of Ivy (S-EL0). */
+extern uintptr_t __TEXT_START__, __TEXT_END__;
+#define IVY_TEXT_START		((uintptr_t)&__TEXT_START__)
+#define IVY_TEXT_END		((uintptr_t)&__TEXT_END__)
+
+extern uintptr_t __RODATA_START__, __RODATA_END__;
+#define IVY_RODATA_START	((uintptr_t)&__RODATA_START__)
+#define IVY_RODATA_END		((uintptr_t)&__RODATA_END__)
+
+extern uintptr_t __DATA_START__, __DATA_END__;
+#define IVY_DATA_START		((uintptr_t)&__DATA_START__)
+#define IVY_DATA_END		((uintptr_t)&__DATA_END__)
+
+extern uintptr_t __BSS_START__, __BSS_END__;
+#define IVY_BSS_START		((uintptr_t)&__BSS_START__)
+#define IVY_BSS_END		((uintptr_t)&__BSS_END__)
+
+#endif /* __IVY_H__ */
diff --git a/spm/ivy/app/ivy_def.h b/spm/ivy/app/ivy_def.h
new file mode 100644
index 0000000..815a59e
--- /dev/null
+++ b/spm/ivy/app/ivy_def.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef IVY_DEF_H
+#define IVY_DEF_H
+
+/*
+ * Layout of the Secure Partition image.
+ */
+
+/* Up to 2 MiB at an arbitrary address that doesn't overlap the devices. */
+#define IVY_IMAGE_BASE			ULL(0x1000)
+#define IVY_IMAGE_SIZE			ULL(0x200000)
+
+/* Memory reserved for stacks */
+#define IVY_STACKS_SIZE			ULL(0x1000)
+
+/* Memory shared between EL3 and S-EL0 (64 KiB). */
+#define IVY_SPM_BUF_BASE		(IVY_IMAGE_BASE + IVY_IMAGE_SIZE)
+#define IVY_SPM_BUF_SIZE		ULL(0x10000)
+
+/* Memory shared between Normal world and S-EL0 (64 KiB). */
+#define IVY_NS_BUF_BASE			(IVY_SPM_BUF_BASE + IVY_SPM_BUF_SIZE)
+#define IVY_NS_BUF_SIZE			ULL(0x10000)
+
+/*
+ * UUIDs of Secure Services provided by Cactus
+ */
+
+#define IVY_SERVICE1_UUID	U(0x76543210), U(0x89ABCDEF), U(0x76543210), U(0xFEDCBA98)
+
+#define IVY_SERVICE1_UUID_RD	U(0x76543210) U(0x89ABCDEF) U(0x76543210) U(0xFEDCBA98)
+
+/*
+ * Service IDs
+ */
+/* Print a magic number unique to IVY and return */
+#define IVY_PRINT_MAGIC			U(1001)
+/* Return a magic number unique to IVY */
+#define IVY_GET_MAGIC			U(1002)
+/* Sleep for a number of milliseconds */
+#define IVY_SLEEP_MS			U(1003)
+
+#define IVY_MAGIC_NUMBER		U(0x97531842)
+
+#endif /* IVY_DEF_H */
diff --git a/spm/ivy/app/ivy_main.c b/spm/ivy/app/ivy_main.c
new file mode 100644
index 0000000..f51fcb5
--- /dev/null
+++ b/spm/ivy/app/ivy_main.c
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <assert.h>
+#include <debug.h>
+#include <errno.h>
+#include <ffa_helpers.h>
+#include <ivy_def.h>
+#include <platform_def.h>
+#include <sp_helpers.h>
+
+#include "ivy.h"
+
+/* Host machine information injected by the build system in the ELF file. */
+extern const char build_message[];
+extern const char version_string[];
+
+void __dead2 ivy_main(void)
+{
+	u_register_t ret;
+	svc_args args;
+
+	NOTICE("Entering S-EL0 Secure Partition\n");
+	NOTICE("%s\n", build_message);
+	NOTICE("%s\n", version_string);
+
+init:
+	args = (svc_args){.fid = FFA_MSG_WAIT};
+	ret = sp_svc(&args);
+	while (1) {
+		if (ret != FFA_MSG_SEND_DIRECT_REQ_SMC32) {
+			ERROR("unknown FF-A request %lx\n", ret);
+			goto init;
+		}
+		VERBOSE("Received request: %lx\n", args.arg3);
+		args.fid = FFA_MSG_SEND_DIRECT_RESP_SMC32;
+		args.arg1 = 0x80020000;
+		args.arg2 = 0;
+		args.arg3 = 0;
+		ret = sp_svc(&args);
+	}
+}