mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 00/18] Dynamic OP-TEE Loading
@ 2024-01-11 11:48 Marco Felsch
  2024-01-11 11:48 ` [PATCH 01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros Marco Felsch
                   ` (17 more replies)
  0 siblings, 18 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

Hi all,

by this series it is supported to load OP-TEE to random DRAM locations.
The location is parsed and configured if the builtin OP-TEE contains a
header-v1. For this to work we needed to cleanup the imx-scratch code
(patches1-4).

I've tested this series on i.MX8MM and i.MX8MP SoCs using the eMMC and
QSPI boot mode. The USB boot mode was tested on i.MX8MM only.

Regards,
  Marco

Marco Felsch (17):
  ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros
  ARM: i.MX8M: bundle imx-scratch code
  ARM: i.MX8M: scratch: make imx_scratch_space private
  ARM: i.MX8M: romapi: refactor saving the bootrom log
  ARM: i.MX8M: scratch: add optee_hdr area
  common: limit BOOTM_OPTEE to 32bit systems
  common: add OPTEE_SHM_SIZE to configure optee shared memory
  optee: add header version check
  optee: add helper functions to set/get the optee memory base
  optee: optee_verify_header: constify optee_header
  ARM64: mmu: add dynamic optee memory mapping support
  ARM: i.MX8M: add dynamic optee memory of-fixup support
  drivers: tee: optee: add support for dynamic optee memory base address
  ARM: i.MX8M: atf: add support for optee hdr parsing
  ARM: i.MX8M: allow board code to configure the bl33 loadaddr
  ARM: i.MX8M: cleanup MX8M*_ATF_BL33_BASE_ADDR defines
  ARM: i.MX8M: fix optee of-fixup logic

Rouven Czerwinski (1):
  optee: add support to verify 64-bit headers as well

 .../boards/imx/zii-imx8mq-dev/openocd.cfg     | 10 +-
 arch/arm/boards/phytec-som-imx8mq/lowlevel.c  |  2 +-
 arch/arm/boards/zii-imx8mq-dev/lowlevel.c     |  2 +-
 arch/arm/cpu/mmu_64.c                         | 14 ++-
 arch/arm/mach-imx/Makefile                    |  2 +-
 arch/arm/mach-imx/atf.c                       | 83 +++++++++++++----
 arch/arm/mach-imx/esdctl.c                    |  4 +
 arch/arm/mach-imx/imx8m.c                     |  8 +-
 arch/arm/mach-imx/romapi.c                    | 35 +++----
 arch/arm/mach-imx/scratch.c                   | 91 +++++++++++++++++++
 arch/arm/mach-imx/xload-common.c              |  8 --
 arch/arm/mach-imx/xload-qspi.c                | 18 ++--
 common/Kconfig                                | 11 ++-
 common/Makefile                               |  2 +-
 common/optee.c                                | 44 ++++++++-
 drivers/mci/imx-esdhc-pbl.c                   | 13 +--
 drivers/tee/optee/of.c                        | 26 +++++-
 include/mach/imx/atf.h                        |  2 -
 include/mach/imx/romapi.h                     | 13 +--
 include/mach/imx/scratch.h                    | 21 +++++
 include/mach/imx/xload.h                      | 28 +++---
 include/tee/optee.h                           |  7 +-
 22 files changed, 326 insertions(+), 118 deletions(-)
 create mode 100644 arch/arm/mach-imx/scratch.c
 create mode 100644 include/mach/imx/scratch.h

-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 12:06   ` Ahmad Fatoum
  2024-01-11 11:48 ` [PATCH 02/18] ARM: i.MX8M: bundle imx-scratch code Marco Felsch
                   ` (16 subsequent siblings)
  17 siblings, 1 reply; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

Make use of the already existing macros and fix the memory bus width for
the i.MX8MQ.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/mach-imx/atf.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/arch/arm/mach-imx/atf.c b/arch/arm/mach-imx/atf.c
index e1a89ef543cd..c6da5de13a1a 100644
--- a/arch/arm/mach-imx/atf.c
+++ b/arch/arm/mach-imx/atf.c
@@ -123,7 +123,7 @@ __noreturn void imx8mm_load_and_start_image_via_tfa(void)
 	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
 	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);
 
-	imx8m_save_bootrom_log((void *)arm_mem_scratch(endmem));
+	imx8mm_save_bootrom_log();
 	imx8mm_load_bl33(bl33);
 
 	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MM_OPTEE)) {
@@ -186,7 +186,7 @@ __noreturn void imx8mp_load_and_start_image_via_tfa(void)
 	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
 	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);
 
-	imx8m_save_bootrom_log((void *)arm_mem_scratch(endmem));
+	imx8mp_save_bootrom_log();
 	imx8mp_load_bl33(bl33);
 
 	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MP_OPTEE)) {
@@ -250,7 +250,7 @@ __noreturn void imx8mn_load_and_start_image_via_tfa(void)
 	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
 	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(16);
 
-	imx8m_save_bootrom_log((void *)arm_mem_scratch(endmem));
+	imx8mn_save_bootrom_log();
 	imx8mn_load_bl33(bl33);
 
 	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MN_OPTEE)) {
@@ -305,9 +305,9 @@ __noreturn void imx8mq_load_and_start_image_via_tfa(void)
 	const void *bl31;
 	size_t bl31_size;
 	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
-	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(16);
+	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);
 
-	imx8m_save_bootrom_log((void *)arm_mem_scratch(endmem));
+	imx8mq_save_bootrom_log();
 	imx8mq_load_bl33(bl33);
 
 	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MQ_OPTEE)) {
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 02/18] ARM: i.MX8M: bundle imx-scratch code
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
  2024-01-11 11:48 ` [PATCH 01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 03/18] ARM: i.MX8M: scratch: make imx_scratch_space private Marco Felsch
                   ` (15 subsequent siblings)
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

The scatch code spread over the code base which is ok as long as we use
it for the bootrom-log only.

This commit bundles the scratch code into a own module to make it easier
to adapt later on and to make the scratch space independent of
CONFIG_ARCH_IMX_ROMAPI.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/mach-imx/Makefile       |  2 +-
 arch/arm/mach-imx/romapi.c       |  8 --------
 arch/arm/mach-imx/scratch.c      | 25 +++++++++++++++++++++++++
 arch/arm/mach-imx/xload-common.c |  8 --------
 include/mach/imx/romapi.h        |  2 +-
 include/mach/imx/scratch.h       | 17 +++++++++++++++++
 include/mach/imx/xload.h         | 11 -----------
 7 files changed, 44 insertions(+), 29 deletions(-)
 create mode 100644 arch/arm/mach-imx/scratch.c
 create mode 100644 include/mach/imx/scratch.h

diff --git a/arch/arm/mach-imx/Makefile b/arch/arm/mach-imx/Makefile
index 3a51ae87813a..9505cffa8111 100644
--- a/arch/arm/mach-imx/Makefile
+++ b/arch/arm/mach-imx/Makefile
@@ -17,7 +17,7 @@ CFLAGS_imx6.o := -march=armv7-a
 lwl-$(CONFIG_ARCH_IMX6) += imx6-mmdc.o
 obj-$(CONFIG_ARCH_IMX7) += imx7.o
 obj-$(CONFIG_ARCH_VF610) += vf610.o
-obj-pbl-$(CONFIG_ARCH_IMX8M) += imx8m.o
+obj-pbl-$(CONFIG_ARCH_IMX8M) += imx8m.o scratch.o
 obj-$(CONFIG_ARCH_IMX9) += imx9.o
 lwl-$(CONFIG_ARCH_IMX_ATF) += atf.o
 obj-pbl-$(CONFIG_ARCH_IMX8M) += tzasc.o
diff --git a/arch/arm/mach-imx/romapi.c b/arch/arm/mach-imx/romapi.c
index 6d47f3c6d4af..e11f38202461 100644
--- a/arch/arm/mach-imx/romapi.c
+++ b/arch/arm/mach-imx/romapi.c
@@ -228,14 +228,6 @@ const u32 *imx8m_get_bootrom_log(void)
 	return NULL;
 }
 
-static int imx8m_reserve_scratch_area(void)
-{
-	return PTR_ERR_OR_ZERO(request_sdram_region("scratch area",
-				    (ulong)arm_mem_scratch_get(),
-				    sizeof(struct imx_scratch_space)));
-}
-device_initcall(imx8m_reserve_scratch_area);
-
 void imx8m_save_bootrom_log(void *dest)
 {
 	const u32 *rom_log;
diff --git a/arch/arm/mach-imx/scratch.c b/arch/arm/mach-imx/scratch.c
new file mode 100644
index 000000000000..0828b55ff8e5
--- /dev/null
+++ b/arch/arm/mach-imx/scratch.c
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <asm/barebox-arm.h>
+#include <init.h>
+#include <linux/err.h>
+#include <mach/imx/imx8m-regs.h>
+#include <mach/imx/esdctl.h>
+#include <mach/imx/scratch.h>
+#include <memory.h>
+
+struct imx_scratch_space *__imx8m_scratch_space(int ddr_buswidth)
+{
+	ulong endmem = MX8M_DDR_CSD1_BASE_ADDR +
+		imx8m_barebox_earlymem_size(ddr_buswidth);
+
+	return (void *)arm_mem_scratch(endmem);
+}
+
+static int imx8m_reserve_scratch_area(void)
+{
+	return PTR_ERR_OR_ZERO(request_sdram_region("scratch area",
+				    (ulong)arm_mem_scratch_get(),
+				    sizeof(struct imx_scratch_space)));
+}
+device_initcall(imx8m_reserve_scratch_area);
diff --git a/arch/arm/mach-imx/xload-common.c b/arch/arm/mach-imx/xload-common.c
index 03eb2ef109e3..0c02e664a84b 100644
--- a/arch/arm/mach-imx/xload-common.c
+++ b/arch/arm/mach-imx/xload-common.c
@@ -21,14 +21,6 @@ int piggydata_size(void)
 	return input_data_end - input_data;
 }
 
-struct imx_scratch_space *__imx8m_scratch_space(int ddr_buswidth)
-{
-	ulong endmem = MX8M_DDR_CSD1_BASE_ADDR +
-		imx8m_barebox_earlymem_size(ddr_buswidth);
-
-	return (void *)arm_mem_scratch(endmem);
-}
-
 #define HDR_SIZE	512
 
 static int
diff --git a/include/mach/imx/romapi.h b/include/mach/imx/romapi.h
index 959d165a3323..977095a5a2f5 100644
--- a/include/mach/imx/romapi.h
+++ b/include/mach/imx/romapi.h
@@ -2,7 +2,7 @@
 #ifndef __MACH_IMX_ROMAPI_H
 #define __MACH_IMX_ROMAPI_H
 
-#include <mach/imx/xload.h>
+#include <mach/imx/scratch.h>
 #include <linux/types.h>
 
 struct rom_api {
diff --git a/include/mach/imx/scratch.h b/include/mach/imx/scratch.h
new file mode 100644
index 000000000000..6dcf35750caf
--- /dev/null
+++ b/include/mach/imx/scratch.h
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __MACH_IMX_SCRATCH_H
+#define __MACH_IMX_SCRATCH_H
+
+struct imx_scratch_space {
+	u32 bootrom_log[128];
+};
+
+struct imx_scratch_space *__imx8m_scratch_space(int ddr_buswidth);
+
+#define imx8mq_scratch_space() __imx8m_scratch_space(32)
+#define imx8mm_scratch_space() __imx8m_scratch_space(32)
+#define imx8mn_scratch_space() __imx8m_scratch_space(16)
+#define imx8mp_scratch_space() __imx8m_scratch_space(32)
+
+#endif /* __MACH_IMX_SCRATCH_H */
diff --git a/include/mach/imx/xload.h b/include/mach/imx/xload.h
index 2c2d2fa3c52d..965e73772ceb 100644
--- a/include/mach/imx/xload.h
+++ b/include/mach/imx/xload.h
@@ -43,15 +43,4 @@ int piggydata_size(void);
 extern unsigned char input_data[];
 extern unsigned char input_data_end[];
 
-struct imx_scratch_space {
-	u32 bootrom_log[128];
-};
-
-struct imx_scratch_space *__imx8m_scratch_space(int ddr_buswidth);
-
-#define imx8mq_scratch_space() __imx8m_scratch_space(32)
-#define imx8mm_scratch_space() __imx8m_scratch_space(32)
-#define imx8mn_scratch_space() __imx8m_scratch_space(16)
-#define imx8mp_scratch_space() __imx8m_scratch_space(32)
-
 #endif /* __MACH_IMX_XLOAD_H */
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 03/18] ARM: i.MX8M: scratch: make imx_scratch_space private
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
  2024-01-11 11:48 ` [PATCH 01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros Marco Felsch
  2024-01-11 11:48 ` [PATCH 02/18] ARM: i.MX8M: bundle imx-scratch code Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 04/18] ARM: i.MX8M: romapi: refactor saving the bootrom log Marco Felsch
                   ` (14 subsequent siblings)
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

Make the scratch layout opaque and provide required accessor functions
to make it easier to extent it later on.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/mach-imx/romapi.c  |  6 ++----
 arch/arm/mach-imx/scratch.c | 12 +++++++++++-
 include/mach/imx/scratch.h  |  6 ++----
 3 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/arch/arm/mach-imx/romapi.c b/arch/arm/mach-imx/romapi.c
index e11f38202461..51e35c9d2aa2 100644
--- a/arch/arm/mach-imx/romapi.c
+++ b/arch/arm/mach-imx/romapi.c
@@ -220,10 +220,8 @@ const u32 *imx8m_get_bootrom_log(void)
 		return (u32 *)rom_log_addr;
 	}
 
-	if (!IN_PBL) {
-		const struct imx_scratch_space *scratch = arm_mem_scratch_get();
-		return scratch->bootrom_log;
-	}
+	if (!IN_PBL)
+		return imx8m_scratch_get_bootrom_log();
 
 	return NULL;
 }
diff --git a/arch/arm/mach-imx/scratch.c b/arch/arm/mach-imx/scratch.c
index 0828b55ff8e5..fba00955c095 100644
--- a/arch/arm/mach-imx/scratch.c
+++ b/arch/arm/mach-imx/scratch.c
@@ -8,7 +8,11 @@
 #include <mach/imx/scratch.h>
 #include <memory.h>
 
-struct imx_scratch_space *__imx8m_scratch_space(int ddr_buswidth)
+struct imx_scratch_space {
+	u32 bootrom_log[128];
+};
+
+void *__imx8m_scratch_space(int ddr_buswidth)
 {
 	ulong endmem = MX8M_DDR_CSD1_BASE_ADDR +
 		imx8m_barebox_earlymem_size(ddr_buswidth);
@@ -16,6 +20,12 @@ struct imx_scratch_space *__imx8m_scratch_space(int ddr_buswidth)
 	return (void *)arm_mem_scratch(endmem);
 }
 
+const u32 *imx8m_scratch_get_bootrom_log(void)
+{
+	const struct imx_scratch_space *scratch = arm_mem_scratch_get();
+	return scratch->bootrom_log;
+}
+
 static int imx8m_reserve_scratch_area(void)
 {
 	return PTR_ERR_OR_ZERO(request_sdram_region("scratch area",
diff --git a/include/mach/imx/scratch.h b/include/mach/imx/scratch.h
index 6dcf35750caf..9b01cf315622 100644
--- a/include/mach/imx/scratch.h
+++ b/include/mach/imx/scratch.h
@@ -3,11 +3,9 @@
 #ifndef __MACH_IMX_SCRATCH_H
 #define __MACH_IMX_SCRATCH_H
 
-struct imx_scratch_space {
-	u32 bootrom_log[128];
-};
+void *__imx8m_scratch_space(int ddr_buswidth);
 
-struct imx_scratch_space *__imx8m_scratch_space(int ddr_buswidth);
+const u32 *imx8m_scratch_get_bootrom_log(void);
 
 #define imx8mq_scratch_space() __imx8m_scratch_space(32)
 #define imx8mm_scratch_space() __imx8m_scratch_space(32)
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 04/18] ARM: i.MX8M: romapi: refactor saving the bootrom log
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (2 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 03/18] ARM: i.MX8M: scratch: make imx_scratch_space private Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 05/18] ARM: i.MX8M: scratch: add optee_hdr area Marco Felsch
                   ` (13 subsequent siblings)
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

Saving the bootrom log to the scratch area was the last open coded part.
Move the code to the scratch module to make the scratch handling
completely opaque.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/mach-imx/atf.c     | 12 ++++++++----
 arch/arm/mach-imx/romapi.c  |  6 ++----
 arch/arm/mach-imx/scratch.c | 33 ++++++++++++++++++++++++++++++---
 include/mach/imx/romapi.h   |  7 +------
 include/mach/imx/scratch.h  | 11 ++++++-----
 5 files changed, 47 insertions(+), 22 deletions(-)

diff --git a/arch/arm/mach-imx/atf.c b/arch/arm/mach-imx/atf.c
index c6da5de13a1a..327e160777df 100644
--- a/arch/arm/mach-imx/atf.c
+++ b/arch/arm/mach-imx/atf.c
@@ -123,7 +123,8 @@ __noreturn void imx8mm_load_and_start_image_via_tfa(void)
 	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
 	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);
 
-	imx8mm_save_bootrom_log();
+	imx8mm_init_scratch_space();
+	imx8m_save_bootrom_log();
 	imx8mm_load_bl33(bl33);
 
 	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MM_OPTEE)) {
@@ -186,7 +187,8 @@ __noreturn void imx8mp_load_and_start_image_via_tfa(void)
 	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
 	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);
 
-	imx8mp_save_bootrom_log();
+	imx8mp_init_scratch_space();
+	imx8m_save_bootrom_log();
 	imx8mp_load_bl33(bl33);
 
 	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MP_OPTEE)) {
@@ -250,7 +252,8 @@ __noreturn void imx8mn_load_and_start_image_via_tfa(void)
 	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
 	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(16);
 
-	imx8mn_save_bootrom_log();
+	imx8mn_init_scratch_space();
+	imx8m_save_bootrom_log();
 	imx8mn_load_bl33(bl33);
 
 	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MN_OPTEE)) {
@@ -307,7 +310,8 @@ __noreturn void imx8mq_load_and_start_image_via_tfa(void)
 	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
 	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);
 
-	imx8mq_save_bootrom_log();
+	imx8mq_init_scratch_space();
+	imx8m_save_bootrom_log();
 	imx8mq_load_bl33(bl33);
 
 	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MQ_OPTEE)) {
diff --git a/arch/arm/mach-imx/romapi.c b/arch/arm/mach-imx/romapi.c
index 51e35c9d2aa2..a245357fdf5d 100644
--- a/arch/arm/mach-imx/romapi.c
+++ b/arch/arm/mach-imx/romapi.c
@@ -226,7 +226,7 @@ const u32 *imx8m_get_bootrom_log(void)
 	return NULL;
 }
 
-void imx8m_save_bootrom_log(void *dest)
+void imx8m_save_bootrom_log(void)
 {
 	const u32 *rom_log;
 
@@ -241,7 +241,5 @@ void imx8m_save_bootrom_log(void *dest)
 		return;
 	}
 
-	pr_debug("Saving bootrom log to 0x%p\n", dest);
-
-	memcpy(dest, rom_log, 128 * sizeof(u32));
+	imx8m_scratch_save_bootrom_log(rom_log);
 }
diff --git a/arch/arm/mach-imx/scratch.c b/arch/arm/mach-imx/scratch.c
index fba00955c095..f4faff835c8c 100644
--- a/arch/arm/mach-imx/scratch.c
+++ b/arch/arm/mach-imx/scratch.c
@@ -3,26 +3,53 @@
 #include <asm/barebox-arm.h>
 #include <init.h>
 #include <linux/err.h>
+#include <linux/printk.h>
 #include <mach/imx/imx8m-regs.h>
 #include <mach/imx/esdctl.h>
 #include <mach/imx/scratch.h>
 #include <memory.h>
+#include <pbl.h>
 
 struct imx_scratch_space {
 	u32 bootrom_log[128];
 };
 
-void *__imx8m_scratch_space(int ddr_buswidth)
+static struct imx_scratch_space *scratch;
+
+void imx8m_init_scratch_space(int ddr_buswidth, bool zero_init)
 {
 	ulong endmem = MX8M_DDR_CSD1_BASE_ADDR +
 		imx8m_barebox_earlymem_size(ddr_buswidth);
 
-	return (void *)arm_mem_scratch(endmem);
+	scratch = (void *)arm_mem_scratch(endmem);
+
+	if (zero_init)
+		memset(scratch, 0, sizeof(*scratch));
+}
+
+void imx8m_scratch_save_bootrom_log(const u32 *rom_log)
+{
+	size_t sz = sizeof(scratch->bootrom_log);
+
+	if (!scratch) {
+		pr_err("No scratch area initialized, skip saving bootrom log");
+		return;
+	}
+
+	pr_debug("Saving bootrom log to scratch area 0x%p\n", &scratch->bootrom_log);
+
+	memcpy(scratch->bootrom_log, rom_log, sz);
 }
 
 const u32 *imx8m_scratch_get_bootrom_log(void)
 {
-	const struct imx_scratch_space *scratch = arm_mem_scratch_get();
+	if (!scratch) {
+		if (IN_PBL)
+			return ERR_PTR(-EINVAL);
+		else
+			scratch = (void *)arm_mem_scratch_get();
+	}
+
 	return scratch->bootrom_log;
 }
 
diff --git a/include/mach/imx/romapi.h b/include/mach/imx/romapi.h
index 977095a5a2f5..b1a990085088 100644
--- a/include/mach/imx/romapi.h
+++ b/include/mach/imx/romapi.h
@@ -40,12 +40,7 @@ int imx8mn_romapi_load_image(void);
 int imx93_romapi_load_image(void);
 
 /* only call after DRAM has been configured */
-void imx8m_save_bootrom_log(void *dst);
+void imx8m_save_bootrom_log(void);
 const u32 *imx8m_get_bootrom_log(void);
 
-#define imx8mq_save_bootrom_log() imx8m_save_bootrom_log(imx8mq_scratch_space())
-#define imx8mm_save_bootrom_log() imx8m_save_bootrom_log(imx8mm_scratch_space())
-#define imx8mn_save_bootrom_log() imx8m_save_bootrom_log(imx8mn_scratch_space())
-#define imx8mp_save_bootrom_log() imx8m_save_bootrom_log(imx8mp_scratch_space())
-
 #endif /* __MACH_IMX_ROMAPI_H */
diff --git a/include/mach/imx/scratch.h b/include/mach/imx/scratch.h
index 9b01cf315622..487171fbee97 100644
--- a/include/mach/imx/scratch.h
+++ b/include/mach/imx/scratch.h
@@ -3,13 +3,14 @@
 #ifndef __MACH_IMX_SCRATCH_H
 #define __MACH_IMX_SCRATCH_H
 
-void *__imx8m_scratch_space(int ddr_buswidth);
+void imx8m_init_scratch_space(int ddr_buswidth, bool zero_init);
 
 const u32 *imx8m_scratch_get_bootrom_log(void);
+void imx8m_scratch_save_bootrom_log(const u32 *rom_log);
 
-#define imx8mq_scratch_space() __imx8m_scratch_space(32)
-#define imx8mm_scratch_space() __imx8m_scratch_space(32)
-#define imx8mn_scratch_space() __imx8m_scratch_space(16)
-#define imx8mp_scratch_space() __imx8m_scratch_space(32)
+#define imx8mq_init_scratch_space() imx8m_init_scratch_space(32, true)
+#define imx8mm_init_scratch_space() imx8m_init_scratch_space(32, true)
+#define imx8mn_init_scratch_space() imx8m_init_scratch_space(16, true)
+#define imx8mp_init_scratch_space() imx8m_init_scratch_space(32, true)
 
 #endif /* __MACH_IMX_SCRATCH_H */
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 05/18] ARM: i.MX8M: scratch: add optee_hdr area
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (3 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 04/18] ARM: i.MX8M: romapi: refactor saving the bootrom log Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 06/18] common: limit BOOTM_OPTEE to 32bit systems Marco Felsch
                   ` (12 subsequent siblings)
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

Add a new scratch storage slot for possible optee-hdr information which
have to passed between barebox-pbl and barebox-proper. Reserve 512 bytes
between the bootrom-log and the optee-hdr in case the bootrom-log need
more space in the future.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/mach-imx/scratch.c | 29 +++++++++++++++++++++++++++++
 include/mach/imx/scratch.h  |  5 +++++
 2 files changed, 34 insertions(+)

diff --git a/arch/arm/mach-imx/scratch.c b/arch/arm/mach-imx/scratch.c
index f4faff835c8c..6ba9b9d55b7d 100644
--- a/arch/arm/mach-imx/scratch.c
+++ b/arch/arm/mach-imx/scratch.c
@@ -8,10 +8,13 @@
 #include <mach/imx/esdctl.h>
 #include <mach/imx/scratch.h>
 #include <memory.h>
+#include <tee/optee.h>
 #include <pbl.h>
 
 struct imx_scratch_space {
 	u32 bootrom_log[128];
+	u32 reserved[128];		/* reserve for bootrom log */
+	struct optee_header optee_hdr;
 };
 
 static struct imx_scratch_space *scratch;
@@ -53,6 +56,32 @@ const u32 *imx8m_scratch_get_bootrom_log(void)
 	return scratch->bootrom_log;
 }
 
+void imx8m_scratch_save_optee_hdr(const struct optee_header *hdr)
+{
+	size_t sz = sizeof(*hdr);
+
+	if (!scratch) {
+		pr_err("No scratch area initialized, skip saving optee-hdr");
+		return;
+	}
+
+	pr_debug("Saving optee-hdr to scratch area 0x%p\n", &scratch->optee_hdr);
+
+	memcpy(&scratch->optee_hdr, hdr, sz);
+}
+
+const struct optee_header *imx8m_scratch_get_optee_hdr(void)
+{
+	if (!scratch) {
+		if (IN_PBL)
+			return ERR_PTR(-EINVAL);
+		else
+			scratch = (void *)arm_mem_scratch_get();
+	}
+
+	return &scratch->optee_hdr;
+}
+
 static int imx8m_reserve_scratch_area(void)
 {
 	return PTR_ERR_OR_ZERO(request_sdram_region("scratch area",
diff --git a/include/mach/imx/scratch.h b/include/mach/imx/scratch.h
index 487171fbee97..59681145e76f 100644
--- a/include/mach/imx/scratch.h
+++ b/include/mach/imx/scratch.h
@@ -8,6 +8,11 @@ void imx8m_init_scratch_space(int ddr_buswidth, bool zero_init);
 const u32 *imx8m_scratch_get_bootrom_log(void);
 void imx8m_scratch_save_bootrom_log(const u32 *rom_log);
 
+struct optee_header;
+
+const struct optee_header *imx8m_scratch_get_optee_hdr(void);
+void imx8m_scratch_save_optee_hdr(const struct optee_header *hdr);
+
 #define imx8mq_init_scratch_space() imx8m_init_scratch_space(32, true)
 #define imx8mm_init_scratch_space() imx8m_init_scratch_space(32, true)
 #define imx8mn_init_scratch_space() imx8m_init_scratch_space(16, true)
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 06/18] common: limit BOOTM_OPTEE to 32bit systems
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (4 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 05/18] ARM: i.MX8M: scratch: add optee_hdr area Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 07/18] common: add OPTEE_SHM_SIZE to configure optee shared memory Marco Felsch
                   ` (11 subsequent siblings)
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

On ARM64 OP-TEE loading requires to be in EL3 so we can't load OP-TEE
via bootm on these systems.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 common/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/common/Kconfig b/common/Kconfig
index 76b28ceaafe0..1de76651fddf 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -1139,7 +1139,7 @@ config OPTEE_SIZE
 config BOOTM_OPTEE
 	bool
 	prompt "support booting OP-TEE"
-	depends on BOOTM && ARM
+	depends on BOOTM && ARM && 32BIT
 	select HAVE_OPTEE
 	help
 	  OP-TEE is a trusted execution environment (TEE). With this option
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 07/18] common: add OPTEE_SHM_SIZE to configure optee shared memory
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (5 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 06/18] common: limit BOOTM_OPTEE to 32bit systems Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 13:24   ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 08/18] optee: add support to verify 64-bit headers as well Marco Felsch
                   ` (10 subsequent siblings)
  17 siblings, 1 reply; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

An size of 4M seems quite large and upstream OP-TEE code mostly uses 2M
for all i.MX platforms. However keep the 4M as default but make it
configurable for integrators which want to use the autom. OF fixup
logic.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/mach-imx/imx8m.c | 2 +-
 common/Kconfig            | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/arch/arm/mach-imx/imx8m.c b/arch/arm/mach-imx/imx8m.c
index c7c799c64bc4..90dc12ca18cc 100644
--- a/arch/arm/mach-imx/imx8m.c
+++ b/arch/arm/mach-imx/imx8m.c
@@ -68,7 +68,7 @@ static int imx8m_init(const char *cputypestr)
 	if (IS_ENABLED(CONFIG_PBL_OPTEE) && tzc380_is_enabled() &&
 	    !of_find_node_by_path_from(NULL, "/firmware/optee")) {
 		static struct of_optee_fixup_data optee_fixup_data = {
-			.shm_size = SZ_4M,
+			.shm_size = CONFIG_OPTEE_SHM_SIZE,
 			.method = "smc",
 		};
 
diff --git a/common/Kconfig b/common/Kconfig
index 1de76651fddf..5b5da6224752 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -1136,6 +1136,15 @@ config OPTEE_SIZE
 	  Can be smaller than the actual size used by OP-TEE, this is used to prevent
 	  barebox from allocating memory in this area.
 
+config OPTEE_SHM_SIZE
+	hex
+	default 0x400000
+	prompt "OP-TEE Shared Memory Size"
+	depends on HAVE_OPTEE
+	help
+	  Size to reserve in main memory for OP-TEE shared memory communication.
+	  Can be used for fixing up the OP-TEE OF node.
+
 config BOOTM_OPTEE
 	bool
 	prompt "support booting OP-TEE"
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 08/18] optee: add support to verify 64-bit headers as well
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (6 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 07/18] common: add OPTEE_SHM_SIZE to configure optee shared memory Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 09/18] optee: add header version check Marco Felsch
                   ` (9 subsequent siblings)
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

From: Rouven Czerwinski <r.czerwinski@pengutronix.de>

Guard the check by IS_ENABLED() to make it possible to verify 32-bit and
64-bit optee headers.

Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 common/optee.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/common/optee.c b/common/optee.c
index 32a6c0a15b94..fd2b03d06d2d 100644
--- a/common/optee.c
+++ b/common/optee.c
@@ -14,8 +14,14 @@ int optee_verify_header(struct optee_header *hdr)
 		return -EINVAL;
 	}
 
-	if (hdr->arch != OPTEE_ARCH_ARM32 || hdr->init_load_addr_hi) {
-		pr_err("Only 32bit supported\n");
+	if (IS_ENABLED(CPU_V7) &&
+	    (hdr->arch != OPTEE_ARCH_ARM32 || hdr->init_load_addr_hi)) {
+		pr_err("Wrong OP-TEE Arch for ARM v7 CPU\n");
+		return -EINVAL;
+	}
+
+	if (IS_ENABLED(CPU_V8) && hdr->arch != OPTEE_ARCH_ARM64) {
+		pr_err("Wrong OP-TEE Arch for ARM v8 CPU\n");
 		return -EINVAL;
 	}
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 09/18] optee: add header version check
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (7 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 08/18] optee: add support to verify 64-bit headers as well Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 10/18] optee: add helper functions to set/get the optee memory base Marco Felsch
                   ` (8 subsequent siblings)
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

OPTEE supports two header versions right now we do only support V1.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 common/optee.c      | 5 +++++
 include/tee/optee.h | 3 ++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/common/optee.c b/common/optee.c
index fd2b03d06d2d..6a8084839619 100644
--- a/common/optee.c
+++ b/common/optee.c
@@ -14,6 +14,11 @@ int optee_verify_header(struct optee_header *hdr)
 		return -EINVAL;
 	}
 
+	if (hdr->version != OPTEE_VERSION_V1) {
+		pr_err("Only V1 headers are supported right now\n");
+		return -EINVAL;
+	}
+
 	if (IS_ENABLED(CPU_V7) &&
 	    (hdr->arch != OPTEE_ARCH_ARM32 || hdr->init_load_addr_hi)) {
 		pr_err("Wrong OP-TEE Arch for ARM v7 CPU\n");
diff --git a/include/tee/optee.h b/include/tee/optee.h
index 607f5072d385..b5ba0a71d7c9 100644
--- a/include/tee/optee.h
+++ b/include/tee/optee.h
@@ -14,7 +14,8 @@
 #include <linux/errno.h>
 
 #define OPTEE_MAGIC             0x4554504f
-#define OPTEE_VERSION           1
+#define OPTEE_VERSION_V1        1
+#define OPTEE_VERSION_V2        2
 #define OPTEE_ARCH_ARM32        0
 #define OPTEE_ARCH_ARM64        1
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 10/18] optee: add helper functions to set/get the optee memory base
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (8 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 09/18] optee: add header version check Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 11/18] optee: optee_verify_header: constify optee_header Marco Felsch
                   ` (7 subsequent siblings)
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

Provide helper functions to set/get the optee memory base address
gathered previously from the builtin optee binary header.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 common/optee.c      | 27 +++++++++++++++++++++++++++
 include/tee/optee.h |  2 ++
 2 files changed, 29 insertions(+)

diff --git a/common/optee.c b/common/optee.c
index 6a8084839619..2ae28b172bbb 100644
--- a/common/optee.c
+++ b/common/optee.c
@@ -6,8 +6,13 @@
 #include <linux/printk.h>
 #include <linux/errno.h>
 
+static u64 optee_membase = U64_MAX;
+
 int optee_verify_header(struct optee_header *hdr)
 {
+	if (!hdr)
+		return -EINVAL;
+
 	if (hdr->magic != OPTEE_MAGIC) {
 		pr_err("Invalid header magic 0x%08x, expected 0x%08x\n",
 			   hdr->magic, OPTEE_MAGIC);
@@ -32,3 +37,25 @@ int optee_verify_header(struct optee_header *hdr)
 
 	return 0;
 }
+
+int optee_get_membase(u64 *membase)
+{
+	if (optee_membase == U64_MAX)
+		return -EINVAL;
+
+	*membase = optee_membase;
+
+	return 0;
+}
+
+void optee_set_membase(const struct optee_header *hdr)
+{
+	int ret;
+
+	ret = optee_verify_header(hdr);
+	if (ret)
+		return;
+
+	optee_membase = (u64)hdr->init_load_addr_hi << 32;
+	optee_membase |= hdr->init_load_addr_lo;
+}
diff --git a/include/tee/optee.h b/include/tee/optee.h
index b5ba0a71d7c9..7bd6638f8559 100644
--- a/include/tee/optee.h
+++ b/include/tee/optee.h
@@ -32,6 +32,8 @@ struct optee_header {
 };
 
 int optee_verify_header (struct optee_header *hdr);
+void optee_set_membase(const struct optee_header *hdr);
+int optee_get_membase(u64 *membase);
 
 #ifdef __PBL__
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 11/18] optee: optee_verify_header: constify optee_header
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (9 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 10/18] optee: add helper functions to set/get the optee memory base Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 12/18] ARM64: mmu: add dynamic optee memory mapping support Marco Felsch
                   ` (6 subsequent siblings)
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 common/optee.c      | 2 +-
 include/tee/optee.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/common/optee.c b/common/optee.c
index 2ae28b172bbb..34667f1f51e0 100644
--- a/common/optee.c
+++ b/common/optee.c
@@ -8,7 +8,7 @@
 
 static u64 optee_membase = U64_MAX;
 
-int optee_verify_header(struct optee_header *hdr)
+int optee_verify_header(const struct optee_header *hdr)
 {
 	if (!hdr)
 		return -EINVAL;
diff --git a/include/tee/optee.h b/include/tee/optee.h
index 7bd6638f8559..393a6978f258 100644
--- a/include/tee/optee.h
+++ b/include/tee/optee.h
@@ -31,7 +31,7 @@ struct optee_header {
 	uint32_t paged_size;
 };
 
-int optee_verify_header (struct optee_header *hdr);
+int optee_verify_header (const struct optee_header *hdr);
 void optee_set_membase(const struct optee_header *hdr);
 int optee_get_membase(u64 *membase);
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 12/18] ARM64: mmu: add dynamic optee memory mapping support
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (10 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 11/18] optee: optee_verify_header: constify optee_header Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 12:07   ` Ahmad Fatoum
  2024-01-11 11:48 ` [PATCH 13/18] ARM: i.MX8M: add dynamic optee memory of-fixup support Marco Felsch
                   ` (5 subsequent siblings)
  17 siblings, 1 reply; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

Use the dynamic optee memory base address for the early mapping if
possible and fallback to the static mapping if the query failed.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/cpu/mmu_64.c      | 14 ++++++++++++--
 arch/arm/mach-imx/esdctl.c |  4 ++++
 common/Makefile            |  2 +-
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/arch/arm/cpu/mmu_64.c b/arch/arm/cpu/mmu_64.c
index c6ea63e655ad..69fd2218c1fc 100644
--- a/arch/arm/cpu/mmu_64.c
+++ b/arch/arm/cpu/mmu_64.c
@@ -20,6 +20,7 @@
 #include <memory.h>
 #include <asm/system_info.h>
 #include <linux/pagemap.h>
+#include <tee/optee.h>
 
 #include "mmu_64.h"
 
@@ -297,6 +298,7 @@ void dma_flush_range(void *ptr, size_t size)
 void mmu_early_enable(unsigned long membase, unsigned long memsize)
 {
 	int el;
+	u64 optee_membase;
 	unsigned long ttb = arm_mem_ttb(membase + memsize);
 
 	pr_debug("enabling MMU, ttb @ 0x%08lx\n", ttb);
@@ -309,8 +311,16 @@ void mmu_early_enable(unsigned long membase, unsigned long memsize)
 	memset((void *)ttb, 0, GRANULE_SIZE);
 
 	early_remap_range(0, 1UL << (BITS_PER_VA - 1), MAP_UNCACHED);
-	early_remap_range(membase, memsize - OPTEE_SIZE, MAP_CACHED);
-	early_remap_range(membase + memsize - OPTEE_SIZE, OPTEE_SIZE, MAP_FAULT);
+
+	if (optee_get_membase(&optee_membase)) {
+		/* Fallback and place OP-TEE at the memory end region */
+		early_remap_range(membase, memsize - OPTEE_SIZE, MAP_CACHED);
+		early_remap_range(membase + memsize - OPTEE_SIZE, OPTEE_SIZE, MAP_FAULT);
+	} else {
+		early_remap_range(membase, memsize, MAP_CACHED);
+		early_remap_range(optee_membase, OPTEE_SIZE, MAP_FAULT);
+	}
+
 	early_remap_range(PAGE_ALIGN_DOWN((uintptr_t)_stext), PAGE_ALIGN(_etext - _stext), MAP_CACHED);
 
 	mmu_enable();
diff --git a/arch/arm/mach-imx/esdctl.c b/arch/arm/mach-imx/esdctl.c
index ac35a2de66f1..47c43d75ec75 100644
--- a/arch/arm/mach-imx/esdctl.c
+++ b/arch/arm/mach-imx/esdctl.c
@@ -29,6 +29,8 @@
 #include <mach/imx/imx8m-regs.h>
 #include <mach/imx/imx7-regs.h>
 #include <mach/imx/imx9-regs.h>
+#include <mach/imx/scratch.h>
+#include <tee/optee.h>
 
 struct imx_esdctl_data {
 	unsigned long base0;
@@ -1004,6 +1006,8 @@ resource_size_t imx8m_barebox_earlymem_size(unsigned buswidth)
 
 static void __noreturn imx8m_barebox_entry(void *boarddata, unsigned buswidth)
 {
+	imx8m_init_scratch_space(buswidth, false);
+	optee_set_membase(imx8m_scratch_get_optee_hdr());
 	barebox_arm_entry(MX8M_DDR_CSD1_BASE_ADDR,
 			  imx8m_barebox_earlymem_size(buswidth), boarddata);
 }
diff --git a/common/Makefile b/common/Makefile
index 7fb864f61480..c31cbab9e48f 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -80,7 +80,7 @@ obj-$(CONFIG_BAREBOX_UPDATE_IMX_NAND_FCB) += imx-bbu-nand-fcb.o
 obj-$(CONFIG_BOOT)		+= boot.o
 obj-$(CONFIG_SERIAL_DEV_BUS)	+= serdev.o
 obj-$(CONFIG_USBGADGET_START)	+= usbgadget.o
-pbl-$(CONFIG_PBL_OPTEE)		+= optee.o
+obj-pbl-$(CONFIG_PBL_OPTEE)	+= optee.o
 obj-$(CONFIG_BOOTM_OPTEE)	+= optee.o
 obj-$(CONFIG_FASTBOOT_BASE)	+= fastboot.o
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 13/18] ARM: i.MX8M: add dynamic optee memory of-fixup support
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (11 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 12/18] ARM64: mmu: add dynamic optee memory mapping support Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 14/18] drivers: tee: optee: add support for dynamic optee memory base address Marco Felsch
                   ` (4 subsequent siblings)
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

Try to set the dynamic optee memory base address if the builtin optee
binary had an header.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/mach-imx/imx8m.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/arm/mach-imx/imx8m.c b/arch/arm/mach-imx/imx8m.c
index 90dc12ca18cc..6f8574199113 100644
--- a/arch/arm/mach-imx/imx8m.c
+++ b/arch/arm/mach-imx/imx8m.c
@@ -16,8 +16,10 @@
 #include <mach/imx/ocotp.h>
 #include <mach/imx/imx8mp-regs.h>
 #include <mach/imx/imx8mq-regs.h>
+#include <mach/imx/scratch.h>
 #include <mach/imx/tzasc.h>
 #include <soc/imx8m/clk-early.h>
+#include <tee/optee.h>
 
 #include <linux/iopoll.h>
 #include <linux/arm-smccc.h>
@@ -72,6 +74,7 @@ static int imx8m_init(const char *cputypestr)
 			.method = "smc",
 		};
 
+		optee_set_membase(imx8m_scratch_get_optee_hdr());
 		of_optee_fixup(of_get_root_node(), &optee_fixup_data);
 		of_register_fixup(of_optee_fixup, &optee_fixup_data);
 	}
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 14/18] drivers: tee: optee: add support for dynamic optee memory base address
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (12 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 13/18] ARM: i.MX8M: add dynamic optee memory of-fixup support Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 15/18] ARM: i.MX8M: atf: add support for optee hdr parsing Marco Felsch
                   ` (3 subsequent siblings)
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

Make use of the platform provided optee memory base address. Fallback to
the current behaviour if the platform doesn't provide the optee memory
base.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 drivers/tee/optee/of.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/drivers/tee/optee/of.c b/drivers/tee/optee/of.c
index 8295a1751c52..cdf650592e90 100644
--- a/drivers/tee/optee/of.c
+++ b/drivers/tee/optee/of.c
@@ -4,12 +4,14 @@
 #include <linux/ioport.h>
 #include <asm/barebox-arm.h>
 #include <asm/optee.h>
+#include <tee/optee.h>
 
 int of_optee_fixup(struct device_node *root, void *_data)
 {
 	struct of_optee_fixup_data *fixup_data = _data;
 	struct resource res = {};
 	struct device_node *node;
+	u64 optee_membase;
 	int ret;
 
 	node = of_create_node(root, "/firmware/optee");
@@ -24,8 +26,13 @@ int of_optee_fixup(struct device_node *root, void *_data)
 	if (ret)
 		return ret;
 
-	res.start = arm_mem_endmem_get() - OPTEE_SIZE;
-	res.end = arm_mem_endmem_get() - fixup_data->shm_size -1;
+	if (!optee_get_membase(&optee_membase)) {
+		res.start = optee_membase;
+		res.end = optee_membase + OPTEE_SIZE - fixup_data->shm_size - 1;
+	} else {
+		res.start = arm_mem_endmem_get() - OPTEE_SIZE;
+		res.end = arm_mem_endmem_get() - fixup_data->shm_size - 1;
+	}
 	res.flags = IORESOURCE_BUSY;
 	res.name = "optee_core";
 
@@ -33,8 +40,13 @@ int of_optee_fixup(struct device_node *root, void *_data)
 	if (ret)
 		return ret;
 
-	res.start = arm_mem_endmem_get() - fixup_data->shm_size;
-	res.end = arm_mem_endmem_get() - 1;
+	if (!optee_get_membase(&optee_membase)) {
+		res.start = optee_membase + OPTEE_SIZE - fixup_data->shm_size;
+		res.end = optee_membase + OPTEE_SIZE - 1;
+	} else {
+		res.start = arm_mem_endmem_get() - fixup_data->shm_size;
+		res.end = arm_mem_endmem_get() - 1;
+	}
 	res.flags &= ~IORESOURCE_BUSY;
 	res.name = "optee_shm";
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 15/18] ARM: i.MX8M: atf: add support for optee hdr parsing
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (13 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 14/18] drivers: tee: optee: add support for dynamic optee memory base address Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 16/18] ARM: i.MX8M: allow board code to configure the bl33 loadaddr Marco Felsch
                   ` (2 subsequent siblings)
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

The OP-TEE binary can contain a header with useful information like
loadaddr and size. This commit adds the support to take the header
loadaddr into account. If the header is not found the common builtin
loadaddr is used.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/mach-imx/atf.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/arch/arm/mach-imx/atf.c b/arch/arm/mach-imx/atf.c
index 327e160777df..eef3235b84f9 100644
--- a/arch/arm/mach-imx/atf.c
+++ b/arch/arm/mach-imx/atf.c
@@ -13,6 +13,7 @@
 #include <mach/imx/imx8m-regs.h>
 #include <soc/fsl/fsl_udc.h>
 #include <soc/fsl/caam.h>
+#include <tee/optee.h>
 
 /**
  * imx8m_atf_load_bl31 - Load ATF BL31 blob and transfer control to it
@@ -116,6 +117,24 @@ void imx8mm_load_bl33(void *bl33)
 	memcpy(bl33, __image_start, barebox_pbl_size);
 }
 
+static void imx_adjust_optee_memory(void **bl32, void **bl32_image, size_t *bl32_size)
+{
+	struct optee_header *hdr = *bl32_image;
+	u64 membase;
+
+	if (optee_verify_header(hdr))
+		return;
+
+	imx8m_scratch_save_optee_hdr(hdr);
+
+	membase = (u64)hdr->init_load_addr_hi << 32;
+	membase |= hdr->init_load_addr_lo;
+
+	*bl32 = (void *)membase;
+	*bl32_size -= sizeof(*hdr);
+	*bl32_image += sizeof(*hdr);
+}
+
 __noreturn void imx8mm_load_and_start_image_via_tfa(void)
 {
 	const void *bl31;
@@ -137,6 +156,8 @@ __noreturn void imx8mm_load_and_start_image_via_tfa(void)
 				bl33, &bl32_image,
 				&bl32_size);
 
+		imx_adjust_optee_memory(&bl32, &bl32_image, &bl32_size);
+
 		memcpy(bl32, bl32_image, bl32_size);
 
 		get_builtin_firmware(imx8mm_bl31_bin_optee, &bl31, &bl31_size);
@@ -201,6 +222,8 @@ __noreturn void imx8mp_load_and_start_image_via_tfa(void)
 				bl33, &bl32_image,
 				&bl32_size);
 
+		imx_adjust_optee_memory(&bl32, &bl32_image, &bl32_size);
+
 		memcpy(bl32, bl32_image, bl32_size);
 
 		get_builtin_firmware(imx8mp_bl31_bin_optee, &bl31, &bl31_size);
@@ -266,6 +289,8 @@ __noreturn void imx8mn_load_and_start_image_via_tfa(void)
 				bl33, &bl32_image,
 				&bl32_size);
 
+		imx_adjust_optee_memory(&bl32, &bl32_image, &bl32_size);
+
 		memcpy(bl32, bl32_image, bl32_size);
 
 		get_builtin_firmware(imx8mn_bl31_bin_optee, &bl31, &bl31_size);
@@ -324,6 +349,8 @@ __noreturn void imx8mq_load_and_start_image_via_tfa(void)
 				bl33, &bl32_image,
 				&bl32_size);
 
+		imx_adjust_optee_memory(&bl32, &bl32_image, &bl32_size);
+
 		memcpy(bl32, bl32_image, bl32_size);
 
 		get_builtin_firmware(imx8mq_bl31_bin_optee, &bl31, &bl31_size);
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 16/18] ARM: i.MX8M: allow board code to configure the bl33 loadaddr
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (14 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 15/18] ARM: i.MX8M: atf: add support for optee hdr parsing Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 17/18] ARM: i.MX8M: cleanup MX8M*_ATF_BL33_BASE_ADDR defines Marco Felsch
  2024-01-11 11:48 ` [PATCH 18/18] ARM: i.MX8M: fix optee of-fixup logic Marco Felsch
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

At an helper to load_and_start the image on arbitrary load addresses.
This should be taken with caution since it requires to configure the
TF-A correctly as well, therefore the functions are prefixt with '__'.

One use case of the new helper is to place OP-TEE at the very beginning
of the DRAM and barebox afterwards e.g. if multiple DRAM setups are
required for the platform/som-family.

A nice side effect of this change is to bundle the usage of
MX8M*_ATF_BL33_BASE_ADDR at a single place.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/mach-imx/atf.c        | 42 +++++++++++++++++++++++-----------
 arch/arm/mach-imx/romapi.c     | 15 ++++++------
 arch/arm/mach-imx/xload-qspi.c | 18 +++++++--------
 drivers/mci/imx-esdhc-pbl.c    | 13 ++++++-----
 include/mach/imx/romapi.h      |  4 ++--
 include/mach/imx/xload.h       | 17 +++++++++-----
 6 files changed, 65 insertions(+), 44 deletions(-)

diff --git a/arch/arm/mach-imx/atf.c b/arch/arm/mach-imx/atf.c
index eef3235b84f9..a976459aee56 100644
--- a/arch/arm/mach-imx/atf.c
+++ b/arch/arm/mach-imx/atf.c
@@ -66,7 +66,7 @@ void imx8mm_load_bl33(void *bl33)
 	imx8mm_get_boot_source(&src, &instance);
 	switch (src) {
 	case BOOTSOURCE_MMC:
-		imx8m_esdhc_load_image(instance, false);
+		imx8m_esdhc_load_image(instance, false, bl33);
 		break;
 	case BOOTSOURCE_SERIAL:
 		if (!IS_ENABLED(CONFIG_USB_GADGET_DRIVER_ARC_PBL)) {
@@ -99,7 +99,7 @@ void imx8mm_load_bl33(void *bl33)
 
 		break;
 	case BOOTSOURCE_SPI:
-		imx8mm_qspi_load_image(instance, false);
+		imx8mm_qspi_load_image(instance, false, bl33);
 		break;
 	default:
 		printf("Unsupported bootsource BOOTSOURCE_%d\n", src);
@@ -136,10 +136,14 @@ static void imx_adjust_optee_memory(void **bl32, void **bl32_image, size_t *bl32
 }
 
 __noreturn void imx8mm_load_and_start_image_via_tfa(void)
+{
+	__imx8mm_load_and_start_image_via_tfa((void *)MX8M_ATF_BL33_BASE_ADDR);
+}
+
+__noreturn void __imx8mm_load_and_start_image_via_tfa(void *bl33)
 {
 	const void *bl31;
 	size_t bl31_size;
-	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
 	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);
 
 	imx8mm_init_scratch_space();
@@ -176,13 +180,13 @@ void imx8mp_load_bl33(void *bl33)
 	imx8mp_get_boot_source(&src, &instance);
 	switch (src) {
 	case BOOTSOURCE_MMC:
-		imx8mp_esdhc_load_image(instance, false);
+		imx8mp_esdhc_load_image(instance, false, bl33);
 		break;
 	case BOOTSOURCE_SERIAL:
-		imx8mp_romapi_load_image();
+		imx8mp_romapi_load_image(bl33);
 		break;
 	case BOOTSOURCE_SPI:
-		imx8mp_qspi_load_image(instance, false);
+		imx8mp_qspi_load_image(instance, false, bl33);
 		break;
 	default:
 		printf("Unhandled bootsource BOOTSOURCE_%d\n", src);
@@ -202,10 +206,14 @@ void imx8mp_load_bl33(void *bl33)
 }
 
 __noreturn void imx8mp_load_and_start_image_via_tfa(void)
+{
+	__imx8mp_load_and_start_image_via_tfa((void *)MX8M_ATF_BL33_BASE_ADDR);
+}
+
+__noreturn void __imx8mp_load_and_start_image_via_tfa(void *bl33)
 {
 	const void *bl31;
 	size_t bl31_size;
-	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
 	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);
 
 	imx8mp_init_scratch_space();
@@ -243,13 +251,13 @@ void imx8mn_load_bl33(void *bl33)
 	imx8mn_get_boot_source(&src, &instance);
 	switch (src) {
 	case BOOTSOURCE_MMC:
-		imx8mn_esdhc_load_image(instance, false);
+		imx8mn_esdhc_load_image(instance, false, bl33);
 		break;
 	case BOOTSOURCE_SERIAL:
-		imx8mn_romapi_load_image();
+		imx8mn_romapi_load_image(bl33);
 		break;
 	case BOOTSOURCE_SPI:
-		imx8mn_qspi_load_image(instance, false);
+		imx8mn_qspi_load_image(instance, false, bl33);
 		break;
 	default:
 		printf("Unhandled bootsource BOOTSOURCE_%d\n", src);
@@ -269,10 +277,14 @@ void imx8mn_load_bl33(void *bl33)
 }
 
 __noreturn void imx8mn_load_and_start_image_via_tfa(void)
+{
+	__imx8mn_load_and_start_image_via_tfa((void *)MX8M_ATF_BL33_BASE_ADDR);
+}
+
+__noreturn void __imx8mn_load_and_start_image_via_tfa(void *bl33)
 {
 	const void *bl31;
 	size_t bl31_size;
-	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
 	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(16);
 
 	imx8mn_init_scratch_space();
@@ -309,7 +321,7 @@ void imx8mq_load_bl33(void *bl33)
 	imx8mn_get_boot_source(&src, &instance);
 	switch (src) {
 	case BOOTSOURCE_MMC:
-		imx8m_esdhc_load_image(instance, false);
+		imx8m_esdhc_load_image(instance, false, bl33);
 		break;
 	default:
 		printf("Unhandled bootsource BOOTSOURCE_%d\n", src);
@@ -329,10 +341,14 @@ void imx8mq_load_bl33(void *bl33)
 }
 
 __noreturn void imx8mq_load_and_start_image_via_tfa(void)
+{
+	__imx8mq_load_and_start_image_via_tfa((void *)MX8M_ATF_BL33_BASE_ADDR);
+}
+
+__noreturn void __imx8mq_load_and_start_image_via_tfa(void *bl33)
 {
 	const void *bl31;
 	size_t bl31_size;
-	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
 	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);
 
 	imx8mq_init_scratch_space();
diff --git a/arch/arm/mach-imx/romapi.c b/arch/arm/mach-imx/romapi.c
index a245357fdf5d..797b479c3e91 100644
--- a/arch/arm/mach-imx/romapi.c
+++ b/arch/arm/mach-imx/romapi.c
@@ -79,25 +79,24 @@ static int imx_romapi_load_seekable(struct rom_api *rom_api, void *adr, uint32_t
 }
 
 /* read piggydata via a bootrom callback and place it behind our copy in SDRAM */
-static int imx_romapi_load_image(struct rom_api *rom_api)
+static int imx_romapi_load_image(struct rom_api *rom_api, void *bl33)
 {
-	return imx_romapi_load_stream(rom_api,
-			(void *)MX8M_ATF_BL33_BASE_ADDR + barebox_pbl_size,
-			__image_end - __piggydata_start);
+	return imx_romapi_load_stream(rom_api, bl33 + barebox_pbl_size,
+				      __image_end - __piggydata_start);
 }
 
-int imx8mp_romapi_load_image(void)
+int imx8mp_romapi_load_image(void *bl33)
 {
 	struct rom_api *rom_api = (void *)0x980;
 
 	OPTIMIZER_HIDE_VAR(rom_api);
 
-	return imx_romapi_load_image(rom_api);
+	return imx_romapi_load_image(rom_api, bl33);
 }
 
-int imx8mn_romapi_load_image(void)
+int imx8mn_romapi_load_image(void *bl33)
 {
-	return imx8mp_romapi_load_image();
+	return imx8mp_romapi_load_image(bl33);
 }
 
 static int imx_romapi_boot_device_seekable(struct rom_api *rom_api)
diff --git a/arch/arm/mach-imx/xload-qspi.c b/arch/arm/mach-imx/xload-qspi.c
index 6bf5bba5e69d..fe7aa9c7bb46 100644
--- a/arch/arm/mach-imx/xload-qspi.c
+++ b/arch/arm/mach-imx/xload-qspi.c
@@ -34,24 +34,24 @@ int imx8m_qspi_read(void *dest, size_t len, void *priv)
  * or 0. A negative error code is returned when this function fails.
  */
 static
-int imx8m_qspi_load_image(int instance, bool start, off_t offset, off_t ivt_offset)
+int imx8m_qspi_load_image(int instance, bool start, off_t offset, off_t ivt_offset,
+			  void *bl33)
 {
 	void __iomem *qspi_ahb = IOMEM(IMX8M_QSPI_MMAP);
 
-	return imx_load_image(MX8M_DDR_CSD1_BASE_ADDR, MX8M_ATF_BL33_BASE_ADDR,
-			      offset, ivt_offset, start, 0,
-			      imx8m_qspi_read, qspi_ahb);
+	return imx_load_image(MX8M_DDR_CSD1_BASE_ADDR, (ptrdiff_t)bl33, offset,
+			      ivt_offset, start, 0, imx8m_qspi_read, qspi_ahb);
 }
 
-int imx8mm_qspi_load_image(int instance, bool start)
+int imx8mm_qspi_load_image(int instance, bool start, void *bl33)
 {
-	return imx8m_qspi_load_image(instance, start, 0, SZ_4K);
+	return imx8m_qspi_load_image(instance, start, 0, SZ_4K, bl33);
 }
 
-int imx8mn_qspi_load_image(int instance, bool start)
+int imx8mn_qspi_load_image(int instance, bool start, void *bl33)
 {
-	return imx8m_qspi_load_image(instance, start, SZ_4K, 0);
+	return imx8m_qspi_load_image(instance, start, SZ_4K, 0, bl33);
 }
 
-int imx8mp_qspi_load_image(int instance, bool start)
+int imx8mp_qspi_load_image(int instance, bool start, void *bl33)
 	__alias(imx8mn_qspi_load_image);
diff --git a/drivers/mci/imx-esdhc-pbl.c b/drivers/mci/imx-esdhc-pbl.c
index 892329de512a..5cf81eeffb1a 100644
--- a/drivers/mci/imx-esdhc-pbl.c
+++ b/drivers/mci/imx-esdhc-pbl.c
@@ -240,6 +240,7 @@ int imx7_esdhc_start_image(int instance)
  * imx8m_esdhc_load_image - Load and optionally start an image from USDHC controller
  * @instance: The USDHC controller instance (0..2)
  * @start: Whether to directly start the loaded image
+ * @bl33: Where to load the bl33 barebox image
  *
  * This uses esdhc_start_image() to load an image from SD/MMC.  It is
  * assumed that the image is the currently running barebox image (This
@@ -249,7 +250,7 @@ int imx7_esdhc_start_image(int instance)
  * Return: If successful, this function does not return (if directly started)
  * or 0. A negative error code is returned when this function fails.
  */
-int imx8m_esdhc_load_image(int instance, bool start)
+int imx8m_esdhc_load_image(int instance, bool start, void *bl33)
 {
 	struct esdhc_soc_data data;
 	struct fsl_esdhc_host host = { 0 };
@@ -260,14 +261,14 @@ int imx8m_esdhc_load_image(int instance, bool start)
 		return ret;
 
 	return esdhc_load_image(&host, MX8M_DDR_CSD1_BASE_ADDR,
-				MX8MQ_ATF_BL33_BASE_ADDR, SZ_32K, SZ_1K,
-				start);
+				(ptrdiff_t)bl33, SZ_32K, SZ_1K, start);
 }
 
 /**
  * imx8mp_esdhc_load_image - Load and optionally start an image from USDHC controller
  * @instance: The USDHC controller instance (0..2)
  * @start: Whether to directly start the loaded image
+ * @bl33: Where to load the bl33 barebox image
  *
  * This uses esdhc_start_image() to load an image from SD/MMC.  It is
  * assumed that the image is the currently running barebox image (This
@@ -277,7 +278,7 @@ int imx8m_esdhc_load_image(int instance, bool start)
  * Return: If successful, this function does not return (if directly started)
  * or 0. A negative error code is returned when this function fails.
  */
-int imx8mp_esdhc_load_image(int instance, bool start)
+int imx8mp_esdhc_load_image(int instance, bool start, void *bl33)
 {
 	struct esdhc_soc_data data;
 	struct fsl_esdhc_host host = { 0 };
@@ -291,10 +292,10 @@ int imx8mp_esdhc_load_image(int instance, bool start)
 	offset = esdhc_bootpart_active(&host)? 0 : SZ_32K;
 
 	return esdhc_load_image(&host, MX8M_DDR_CSD1_BASE_ADDR,
-				MX8MQ_ATF_BL33_BASE_ADDR, offset, 0, start);
+				(ptrdiff_t)bl33, offset, 0, start);
 }
 
-int imx8mn_esdhc_load_image(int instance, bool start)
+int imx8mn_esdhc_load_image(int instance, bool start, void *bl33)
 	__alias(imx8mp_esdhc_load_image);
 #endif
 
diff --git a/include/mach/imx/romapi.h b/include/mach/imx/romapi.h
index b1a990085088..de5881001c02 100644
--- a/include/mach/imx/romapi.h
+++ b/include/mach/imx/romapi.h
@@ -35,8 +35,8 @@ enum boot_dev_type_e {
 
 #define ROM_API_OKAY		0xF0
 
-int imx8mp_romapi_load_image(void);
-int imx8mn_romapi_load_image(void);
+int imx8mp_romapi_load_image(void *bl33);
+int imx8mn_romapi_load_image(void *bl33);
 int imx93_romapi_load_image(void);
 
 /* only call after DRAM has been configured */
diff --git a/include/mach/imx/xload.h b/include/mach/imx/xload.h
index 965e73772ceb..49e38f8c1dd3 100644
--- a/include/mach/imx/xload.h
+++ b/include/mach/imx/xload.h
@@ -14,12 +14,12 @@ int imx6_esdhc_start_image(int instance);
 int imx6_nand_start_image(void);
 int imx7_esdhc_start_image(int instance);
 int imx7_nand_start_image(void);
-int imx8m_esdhc_load_image(int instance, bool start);
-int imx8mn_esdhc_load_image(int instance, bool start);
-int imx8mp_esdhc_load_image(int instance, bool start);
-int imx8mm_qspi_load_image(int instance, bool start);
-int imx8mn_qspi_load_image(int instance, bool start);
-int imx8mp_qspi_load_image(int instance, bool start);
+int imx8m_esdhc_load_image(int instance, bool start, void *bl33);
+int imx8mn_esdhc_load_image(int instance, bool start, void *bl33);
+int imx8mp_esdhc_load_image(int instance, bool start, void *bl33);
+int imx8mm_qspi_load_image(int instance, bool start, void *bl33);
+int imx8mn_qspi_load_image(int instance, bool start, void *bl33);
+int imx8mp_qspi_load_image(int instance, bool start, void *bl33);
 
 void imx8mm_load_bl33(void *bl33);
 void imx8mn_load_bl33(void *bl33);
@@ -30,6 +30,11 @@ void __noreturn imx8mm_load_and_start_image_via_tfa(void);
 void __noreturn imx8mn_load_and_start_image_via_tfa(void);
 void __noreturn imx8mp_load_and_start_image_via_tfa(void);
 void __noreturn imx8mq_load_and_start_image_via_tfa(void);
+void __noreturn __imx8mm_load_and_start_image_via_tfa(void *bl33);
+void __noreturn __imx8mn_load_and_start_image_via_tfa(void *bl33);
+void __noreturn __imx8mp_load_and_start_image_via_tfa(void *bl33);
+void __noreturn __imx8mq_load_and_start_image_via_tfa(void *bl33);
+
 void __noreturn imx93_load_and_start_image_via_tfa(void);
 
 int imx_load_image(ptrdiff_t address, ptrdiff_t entry, u32 offset,
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 17/18] ARM: i.MX8M: cleanup MX8M*_ATF_BL33_BASE_ADDR defines
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (15 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 16/18] ARM: i.MX8M: allow board code to configure the bl33 loadaddr Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  2024-01-11 11:48 ` [PATCH 18/18] ARM: i.MX8M: fix optee of-fixup logic Marco Felsch
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

Instead of having three defines for the same just use the base
definition which is MX8M_ATF_BL33_BASE_ADDR and drop the others.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 Documentation/boards/imx/zii-imx8mq-dev/openocd.cfg | 10 +++++-----
 arch/arm/boards/phytec-som-imx8mq/lowlevel.c        |  2 +-
 arch/arm/boards/zii-imx8mq-dev/lowlevel.c           |  2 +-
 include/mach/imx/atf.h                              |  2 --
 4 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/Documentation/boards/imx/zii-imx8mq-dev/openocd.cfg b/Documentation/boards/imx/zii-imx8mq-dev/openocd.cfg
index cc0bec6b74b9..fa25348757af 100644
--- a/Documentation/boards/imx/zii-imx8mq-dev/openocd.cfg
+++ b/Documentation/boards/imx/zii-imx8mq-dev/openocd.cfg
@@ -71,15 +71,15 @@ proc ddr_init { } {
 
 proc start_barebox {} {
 	#
-	# We have to place our image at MX8MQ_ATF_BL33_BASE_ADDR in order
+	# We have to place our image at MX8M_ATF_BL33_BASE_ADDR in order
 	# to be able to initialize ATF firmware since that's where it
 	# expects entry point to BL33 would be
 	#
-	set MX8MQ_ATF_BL33_BASE_ADDR	0x40200000
+	set MX8M_ATF_BL33_BASE_ADDR	0x40200000
 	echo "Bootstrap: Loading Barebox"
-	load_image images/start_zii_imx8mq_dev.pblb $MX8MQ_ATF_BL33_BASE_ADDR bin
-	echo [format "Bootstrap: Jumping to 0x%08x" $MX8MQ_ATF_BL33_BASE_ADDR]
-	resume $MX8MQ_ATF_BL33_BASE_ADDR
+	load_image images/start_zii_imx8mq_dev.pblb $MX8M_ATF_BL33_BASE_ADDR bin
+	echo [format "Bootstrap: Jumping to 0x%08x" $MX8M_ATF_BL33_BASE_ADDR]
+	resume $MX8M_ATF_BL33_BASE_ADDR
 }
 
 proc board_init { } {
diff --git a/arch/arm/boards/phytec-som-imx8mq/lowlevel.c b/arch/arm/boards/phytec-som-imx8mq/lowlevel.c
index 5708c8d75484..362b3ed823c1 100644
--- a/arch/arm/boards/phytec-som-imx8mq/lowlevel.c
+++ b/arch/arm/boards/phytec-som-imx8mq/lowlevel.c
@@ -88,7 +88,7 @@ static __noreturn noinline void phytec_phycore_imx8mq_start(void)
  *
  * 4. BL31 blob is uploaded to OCRAM and the control is transfer to it
  *
- * 5. BL31 exits EL3 into EL2 at address MX8MQ_ATF_BL33_BASE_ADDR,
+ * 5. BL31 exits EL3 into EL2 at address MX8M_ATF_BL33_BASE_ADDR,
  *    executing start_phytec_phycore_imx8mq() the third time
  *
  * 6. Standard barebox boot flow continues
diff --git a/arch/arm/boards/zii-imx8mq-dev/lowlevel.c b/arch/arm/boards/zii-imx8mq-dev/lowlevel.c
index 42cd05d3f17c..4184748cd858 100644
--- a/arch/arm/boards/zii-imx8mq-dev/lowlevel.c
+++ b/arch/arm/boards/zii-imx8mq-dev/lowlevel.c
@@ -174,7 +174,7 @@ static __noreturn noinline void zii_imx8mq_dev_start(void)
  *
  * 4. BL31 blob is uploaded to OCRAM and the control is transfer to it
  *
- * 5. BL31 exits EL3 into EL2 at address MX8MQ_ATF_BL33_BASE_ADDR,
+ * 5. BL31 exits EL3 into EL2 at address MX8M_ATF_BL33_BASE_ADDR,
  *    executing start_nxp_imx8mq_evk() the third time
  *
  * 6. Standard barebox boot flow continues
diff --git a/include/mach/imx/atf.h b/include/mach/imx/atf.h
index fb367d6a7052..15bd13eb27cc 100644
--- a/include/mach/imx/atf.h
+++ b/include/mach/imx/atf.h
@@ -15,8 +15,6 @@
 #define MX8MP_ATF_BL31_BASE_ADDR	0x00970000
 #define MX8MQ_ATF_BL31_BASE_ADDR	0x00910000
 #define MX8M_ATF_BL33_BASE_ADDR		0x40200000
-#define MX8MM_ATF_BL33_BASE_ADDR	MX8M_ATF_BL33_BASE_ADDR
-#define MX8MQ_ATF_BL33_BASE_ADDR	MX8M_ATF_BL33_BASE_ADDR
 #define MX93_ATF_BL31_BASE_ADDR		0x204e0000
 #define MX93_ATF_BL33_BASE_ADDR		0x80200000
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 18/18] ARM: i.MX8M: fix optee of-fixup logic
  2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (16 preceding siblings ...)
  2024-01-11 11:48 ` [PATCH 17/18] ARM: i.MX8M: cleanup MX8M*_ATF_BL33_BASE_ADDR defines Marco Felsch
@ 2024-01-11 11:48 ` Marco Felsch
  17 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 11:48 UTC (permalink / raw)
  To: barebox

The current code checks only if "/firmware/optee" exist on the builtin
dtb and applys the fixup if not found and if found nothing is done. If a
builtin dts contains the node but an external don't the fixup won't be
applied. Also if the external dts does have a node + the reserved memory
region nodes but the barebox builtin dts don't we do add additional
reserved memory nodes which may conflict due to different name scheme:
<name> vs. <name>@<addr>.

Move the "/firmware/optee" check into the of_optee_fixup() so the check
is done on the correct dtb root nodes.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 arch/arm/mach-imx/imx8m.c | 3 +--
 drivers/tee/optee/of.c    | 6 +++++-
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/arch/arm/mach-imx/imx8m.c b/arch/arm/mach-imx/imx8m.c
index 6f8574199113..03d6e7f64cde 100644
--- a/arch/arm/mach-imx/imx8m.c
+++ b/arch/arm/mach-imx/imx8m.c
@@ -67,8 +67,7 @@ static int imx8m_init(const char *cputypestr)
 	imx_set_reset_reason(src + IMX7_SRC_SRSR, imx7_reset_reasons);
 	pr_info("%s unique ID: %llx\n", cputypestr, imx8m_uid());
 
-	if (IS_ENABLED(CONFIG_PBL_OPTEE) && tzc380_is_enabled() &&
-	    !of_find_node_by_path_from(NULL, "/firmware/optee")) {
+	if (IS_ENABLED(CONFIG_PBL_OPTEE) && tzc380_is_enabled()) {
 		static struct of_optee_fixup_data optee_fixup_data = {
 			.shm_size = CONFIG_OPTEE_SHM_SIZE,
 			.method = "smc",
diff --git a/drivers/tee/optee/of.c b/drivers/tee/optee/of.c
index cdf650592e90..2ff1f01964c8 100644
--- a/drivers/tee/optee/of.c
+++ b/drivers/tee/optee/of.c
@@ -9,12 +9,16 @@
 int of_optee_fixup(struct device_node *root, void *_data)
 {
 	struct of_optee_fixup_data *fixup_data = _data;
+	const char *optee_of_path = "/firmware/optee";
 	struct resource res = {};
 	struct device_node *node;
 	u64 optee_membase;
 	int ret;
 
-	node = of_create_node(root, "/firmware/optee");
+	if (of_find_node_by_path_from(NULL, optee_of_path))
+		return 0;
+
+	node = of_create_node(root, optee_of_path);
 	if (!node)
 		return -ENOMEM;
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros
  2024-01-11 11:48 ` [PATCH 01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros Marco Felsch
@ 2024-01-11 12:06   ` Ahmad Fatoum
  2024-01-11 12:16     ` Marco Felsch
  0 siblings, 1 reply; 26+ messages in thread
From: Ahmad Fatoum @ 2024-01-11 12:06 UTC (permalink / raw)
  To: Marco Felsch, barebox

On 11.01.24 12:48, Marco Felsch wrote:
>  	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MN_OPTEE)) {
> @@ -305,9 +305,9 @@ __noreturn void imx8mq_load_and_start_image_via_tfa(void)
>  	const void *bl31;
>  	size_t bl31_size;
>  	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
> -	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(16);
> +	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);

Why this change?

>  
> -	imx8m_save_bootrom_log((void *)arm_mem_scratch(endmem));
> +	imx8mq_save_bootrom_log();
>  	imx8mq_load_bl33(bl33);
>  
>  	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MQ_OPTEE)) {

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 12/18] ARM64: mmu: add dynamic optee memory mapping support
  2024-01-11 11:48 ` [PATCH 12/18] ARM64: mmu: add dynamic optee memory mapping support Marco Felsch
@ 2024-01-11 12:07   ` Ahmad Fatoum
  2024-01-11 12:14     ` Marco Felsch
  0 siblings, 1 reply; 26+ messages in thread
From: Ahmad Fatoum @ 2024-01-11 12:07 UTC (permalink / raw)
  To: Marco Felsch, barebox

On 11.01.24 12:48, Marco Felsch wrote:
> Use the dynamic optee memory base address for the early mapping if
> possible and fallback to the static mapping if the query failed.
> 
> Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>

This patch fails to apply on next.

> ---
>  arch/arm/cpu/mmu_64.c      | 14 ++++++++++++--
>  arch/arm/mach-imx/esdctl.c |  4 ++++
>  common/Makefile            |  2 +-
>  3 files changed, 17 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/arm/cpu/mmu_64.c b/arch/arm/cpu/mmu_64.c
> index c6ea63e655ad..69fd2218c1fc 100644
> --- a/arch/arm/cpu/mmu_64.c
> +++ b/arch/arm/cpu/mmu_64.c
> @@ -20,6 +20,7 @@
>  #include <memory.h>
>  #include <asm/system_info.h>
>  #include <linux/pagemap.h>
> +#include <tee/optee.h>
>  
>  #include "mmu_64.h"
>  
> @@ -297,6 +298,7 @@ void dma_flush_range(void *ptr, size_t size)
>  void mmu_early_enable(unsigned long membase, unsigned long memsize)
>  {
>  	int el;
> +	u64 optee_membase;
>  	unsigned long ttb = arm_mem_ttb(membase + memsize);
>  
>  	pr_debug("enabling MMU, ttb @ 0x%08lx\n", ttb);
> @@ -309,8 +311,16 @@ void mmu_early_enable(unsigned long membase, unsigned long memsize)
>  	memset((void *)ttb, 0, GRANULE_SIZE);
>  
>  	early_remap_range(0, 1UL << (BITS_PER_VA - 1), MAP_UNCACHED);
> -	early_remap_range(membase, memsize - OPTEE_SIZE, MAP_CACHED);
> -	early_remap_range(membase + memsize - OPTEE_SIZE, OPTEE_SIZE, MAP_FAULT);
> +
> +	if (optee_get_membase(&optee_membase)) {
> +		/* Fallback and place OP-TEE at the memory end region */
> +		early_remap_range(membase, memsize - OPTEE_SIZE, MAP_CACHED);
> +		early_remap_range(membase + memsize - OPTEE_SIZE, OPTEE_SIZE, MAP_FAULT);
> +	} else {
> +		early_remap_range(membase, memsize, MAP_CACHED);
> +		early_remap_range(optee_membase, OPTEE_SIZE, MAP_FAULT);
> +	}
> +
>  	early_remap_range(PAGE_ALIGN_DOWN((uintptr_t)_stext), PAGE_ALIGN(_etext - _stext), MAP_CACHED);
>  
>  	mmu_enable();
> diff --git a/arch/arm/mach-imx/esdctl.c b/arch/arm/mach-imx/esdctl.c
> index ac35a2de66f1..47c43d75ec75 100644
> --- a/arch/arm/mach-imx/esdctl.c
> +++ b/arch/arm/mach-imx/esdctl.c
> @@ -29,6 +29,8 @@
>  #include <mach/imx/imx8m-regs.h>
>  #include <mach/imx/imx7-regs.h>
>  #include <mach/imx/imx9-regs.h>
> +#include <mach/imx/scratch.h>
> +#include <tee/optee.h>
>  
>  struct imx_esdctl_data {
>  	unsigned long base0;
> @@ -1004,6 +1006,8 @@ resource_size_t imx8m_barebox_earlymem_size(unsigned buswidth)
>  
>  static void __noreturn imx8m_barebox_entry(void *boarddata, unsigned buswidth)
>  {
> +	imx8m_init_scratch_space(buswidth, false);
> +	optee_set_membase(imx8m_scratch_get_optee_hdr());
>  	barebox_arm_entry(MX8M_DDR_CSD1_BASE_ADDR,
>  			  imx8m_barebox_earlymem_size(buswidth), boarddata);
>  }
> diff --git a/common/Makefile b/common/Makefile
> index 7fb864f61480..c31cbab9e48f 100644
> --- a/common/Makefile
> +++ b/common/Makefile
> @@ -80,7 +80,7 @@ obj-$(CONFIG_BAREBOX_UPDATE_IMX_NAND_FCB) += imx-bbu-nand-fcb.o
>  obj-$(CONFIG_BOOT)		+= boot.o
>  obj-$(CONFIG_SERIAL_DEV_BUS)	+= serdev.o
>  obj-$(CONFIG_USBGADGET_START)	+= usbgadget.o
> -pbl-$(CONFIG_PBL_OPTEE)		+= optee.o
> +obj-pbl-$(CONFIG_PBL_OPTEE)	+= optee.o
>  obj-$(CONFIG_BOOTM_OPTEE)	+= optee.o
>  obj-$(CONFIG_FASTBOOT_BASE)	+= fastboot.o
>  

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 12/18] ARM64: mmu: add dynamic optee memory mapping support
  2024-01-11 12:07   ` Ahmad Fatoum
@ 2024-01-11 12:14     ` Marco Felsch
  2024-01-11 15:25       ` Ahmad Fatoum
  0 siblings, 1 reply; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 12:14 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On 24-01-11, Ahmad Fatoum wrote:
> On 11.01.24 12:48, Marco Felsch wrote:
> > Use the dynamic optee memory base address for the early mapping if
> > possible and fallback to the static mapping if the query failed.
> > 
> > Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> 
> This patch fails to apply on next.

Argh.. I used the most recent version which is 2023.12.0 for this
development. Shall I rebase it on next or on master?

Regards,
  Marco


> 
> > ---
> >  arch/arm/cpu/mmu_64.c      | 14 ++++++++++++--
> >  arch/arm/mach-imx/esdctl.c |  4 ++++
> >  common/Makefile            |  2 +-
> >  3 files changed, 17 insertions(+), 3 deletions(-)
> > 
> > diff --git a/arch/arm/cpu/mmu_64.c b/arch/arm/cpu/mmu_64.c
> > index c6ea63e655ad..69fd2218c1fc 100644
> > --- a/arch/arm/cpu/mmu_64.c
> > +++ b/arch/arm/cpu/mmu_64.c
> > @@ -20,6 +20,7 @@
> >  #include <memory.h>
> >  #include <asm/system_info.h>
> >  #include <linux/pagemap.h>
> > +#include <tee/optee.h>
> >  
> >  #include "mmu_64.h"
> >  
> > @@ -297,6 +298,7 @@ void dma_flush_range(void *ptr, size_t size)
> >  void mmu_early_enable(unsigned long membase, unsigned long memsize)
> >  {
> >  	int el;
> > +	u64 optee_membase;
> >  	unsigned long ttb = arm_mem_ttb(membase + memsize);
> >  
> >  	pr_debug("enabling MMU, ttb @ 0x%08lx\n", ttb);
> > @@ -309,8 +311,16 @@ void mmu_early_enable(unsigned long membase, unsigned long memsize)
> >  	memset((void *)ttb, 0, GRANULE_SIZE);
> >  
> >  	early_remap_range(0, 1UL << (BITS_PER_VA - 1), MAP_UNCACHED);
> > -	early_remap_range(membase, memsize - OPTEE_SIZE, MAP_CACHED);
> > -	early_remap_range(membase + memsize - OPTEE_SIZE, OPTEE_SIZE, MAP_FAULT);
> > +
> > +	if (optee_get_membase(&optee_membase)) {
> > +		/* Fallback and place OP-TEE at the memory end region */
> > +		early_remap_range(membase, memsize - OPTEE_SIZE, MAP_CACHED);
> > +		early_remap_range(membase + memsize - OPTEE_SIZE, OPTEE_SIZE, MAP_FAULT);
> > +	} else {
> > +		early_remap_range(membase, memsize, MAP_CACHED);
> > +		early_remap_range(optee_membase, OPTEE_SIZE, MAP_FAULT);
> > +	}
> > +
> >  	early_remap_range(PAGE_ALIGN_DOWN((uintptr_t)_stext), PAGE_ALIGN(_etext - _stext), MAP_CACHED);
> >  
> >  	mmu_enable();
> > diff --git a/arch/arm/mach-imx/esdctl.c b/arch/arm/mach-imx/esdctl.c
> > index ac35a2de66f1..47c43d75ec75 100644
> > --- a/arch/arm/mach-imx/esdctl.c
> > +++ b/arch/arm/mach-imx/esdctl.c
> > @@ -29,6 +29,8 @@
> >  #include <mach/imx/imx8m-regs.h>
> >  #include <mach/imx/imx7-regs.h>
> >  #include <mach/imx/imx9-regs.h>
> > +#include <mach/imx/scratch.h>
> > +#include <tee/optee.h>
> >  
> >  struct imx_esdctl_data {
> >  	unsigned long base0;
> > @@ -1004,6 +1006,8 @@ resource_size_t imx8m_barebox_earlymem_size(unsigned buswidth)
> >  
> >  static void __noreturn imx8m_barebox_entry(void *boarddata, unsigned buswidth)
> >  {
> > +	imx8m_init_scratch_space(buswidth, false);
> > +	optee_set_membase(imx8m_scratch_get_optee_hdr());
> >  	barebox_arm_entry(MX8M_DDR_CSD1_BASE_ADDR,
> >  			  imx8m_barebox_earlymem_size(buswidth), boarddata);
> >  }
> > diff --git a/common/Makefile b/common/Makefile
> > index 7fb864f61480..c31cbab9e48f 100644
> > --- a/common/Makefile
> > +++ b/common/Makefile
> > @@ -80,7 +80,7 @@ obj-$(CONFIG_BAREBOX_UPDATE_IMX_NAND_FCB) += imx-bbu-nand-fcb.o
> >  obj-$(CONFIG_BOOT)		+= boot.o
> >  obj-$(CONFIG_SERIAL_DEV_BUS)	+= serdev.o
> >  obj-$(CONFIG_USBGADGET_START)	+= usbgadget.o
> > -pbl-$(CONFIG_PBL_OPTEE)		+= optee.o
> > +obj-pbl-$(CONFIG_PBL_OPTEE)	+= optee.o
> >  obj-$(CONFIG_BOOTM_OPTEE)	+= optee.o
> >  obj-$(CONFIG_FASTBOOT_BASE)	+= fastboot.o
> >  
> 
> -- 
> Pengutronix e.K.                           |                             |
> Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
> 
> 



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros
  2024-01-11 12:06   ` Ahmad Fatoum
@ 2024-01-11 12:16     ` Marco Felsch
  2024-01-11 15:28       ` Ahmad Fatoum
  0 siblings, 1 reply; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 12:16 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On 24-01-11, Ahmad Fatoum wrote:
> On 11.01.24 12:48, Marco Felsch wrote:
> >  	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MN_OPTEE)) {
> > @@ -305,9 +305,9 @@ __noreturn void imx8mq_load_and_start_image_via_tfa(void)
> >  	const void *bl31;
> >  	size_t bl31_size;
> >  	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
> > -	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(16);
> > +	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);
> 
> Why this change?

Because the ddr-bandwidth for i.MX8MQ is 32-bit and the usage of
imx8mq_save_bootrom_log() will fix this as well.

Regards,
  Marco

> 
> >  
> > -	imx8m_save_bootrom_log((void *)arm_mem_scratch(endmem));
> > +	imx8mq_save_bootrom_log();
> >  	imx8mq_load_bl33(bl33);
> >  
> >  	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MQ_OPTEE)) {
> 
> -- 
> Pengutronix e.K.                           |                             |
> Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
> 
> 



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 07/18] common: add OPTEE_SHM_SIZE to configure optee shared memory
  2024-01-11 11:48 ` [PATCH 07/18] common: add OPTEE_SHM_SIZE to configure optee shared memory Marco Felsch
@ 2024-01-11 13:24   ` Marco Felsch
  0 siblings, 0 replies; 26+ messages in thread
From: Marco Felsch @ 2024-01-11 13:24 UTC (permalink / raw)
  To: barebox

On 24-01-11, Marco Felsch wrote:
> An size of 4M seems quite large and upstream OP-TEE code mostly uses 2M
> for all i.MX platforms. However keep the 4M as default but make it
> configurable for integrators which want to use the autom. OF fixup
> logic.
> 
> Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> ---
>  arch/arm/mach-imx/imx8m.c | 2 +-
>  common/Kconfig            | 9 +++++++++
>  2 files changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/arm/mach-imx/imx8m.c b/arch/arm/mach-imx/imx8m.c
> index c7c799c64bc4..90dc12ca18cc 100644
> --- a/arch/arm/mach-imx/imx8m.c
> +++ b/arch/arm/mach-imx/imx8m.c
> @@ -68,7 +68,7 @@ static int imx8m_init(const char *cputypestr)
>  	if (IS_ENABLED(CONFIG_PBL_OPTEE) && tzc380_is_enabled() &&
>  	    !of_find_node_by_path_from(NULL, "/firmware/optee")) {
>  		static struct of_optee_fixup_data optee_fixup_data = {
> -			.shm_size = SZ_4M,
> +			.shm_size = CONFIG_OPTEE_SHM_SIZE,

This fails if not defined via Kconfig. I prepare a v2 which uses the
same mechanism as we use for OPTEE_SIZE.

Regards,
  Marco

>  			.method = "smc",
>  		};
>  
> diff --git a/common/Kconfig b/common/Kconfig
> index 1de76651fddf..5b5da6224752 100644
> --- a/common/Kconfig
> +++ b/common/Kconfig
> @@ -1136,6 +1136,15 @@ config OPTEE_SIZE
>  	  Can be smaller than the actual size used by OP-TEE, this is used to prevent
>  	  barebox from allocating memory in this area.
>  
> +config OPTEE_SHM_SIZE
> +	hex
> +	default 0x400000
> +	prompt "OP-TEE Shared Memory Size"
> +	depends on HAVE_OPTEE
> +	help
> +	  Size to reserve in main memory for OP-TEE shared memory communication.
> +	  Can be used for fixing up the OP-TEE OF node.
> +
>  config BOOTM_OPTEE
>  	bool
>  	prompt "support booting OP-TEE"
> -- 
> 2.39.2
> 
> 
> 



^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 12/18] ARM64: mmu: add dynamic optee memory mapping support
  2024-01-11 12:14     ` Marco Felsch
@ 2024-01-11 15:25       ` Ahmad Fatoum
  0 siblings, 0 replies; 26+ messages in thread
From: Ahmad Fatoum @ 2024-01-11 15:25 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On 11.01.24 13:14, Marco Felsch wrote:
> On 24-01-11, Ahmad Fatoum wrote:
>> On 11.01.24 12:48, Marco Felsch wrote:
>>> Use the dynamic optee memory base address for the early mapping if
>>> possible and fallback to the static mapping if the query failed.
>>>
>>> Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
>>
>> This patch fails to apply on next.
> 
> Argh.. I used the most recent version which is 2023.12.0 for this
> development. Shall I rebase it on next or on master?

As the series should go into next as it's not a fix, please rebase
on next. Usually, it doesn't matter, but there was recent rework
for OP-TEE for SCMI and thus it conflicts.

> 
> Regards,
>   Marco
> 
> 
>>
>>> ---
>>>  arch/arm/cpu/mmu_64.c      | 14 ++++++++++++--
>>>  arch/arm/mach-imx/esdctl.c |  4 ++++
>>>  common/Makefile            |  2 +-
>>>  3 files changed, 17 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/arch/arm/cpu/mmu_64.c b/arch/arm/cpu/mmu_64.c
>>> index c6ea63e655ad..69fd2218c1fc 100644
>>> --- a/arch/arm/cpu/mmu_64.c
>>> +++ b/arch/arm/cpu/mmu_64.c
>>> @@ -20,6 +20,7 @@
>>>  #include <memory.h>
>>>  #include <asm/system_info.h>
>>>  #include <linux/pagemap.h>
>>> +#include <tee/optee.h>
>>>  
>>>  #include "mmu_64.h"
>>>  
>>> @@ -297,6 +298,7 @@ void dma_flush_range(void *ptr, size_t size)
>>>  void mmu_early_enable(unsigned long membase, unsigned long memsize)
>>>  {
>>>  	int el;
>>> +	u64 optee_membase;
>>>  	unsigned long ttb = arm_mem_ttb(membase + memsize);
>>>  
>>>  	pr_debug("enabling MMU, ttb @ 0x%08lx\n", ttb);
>>> @@ -309,8 +311,16 @@ void mmu_early_enable(unsigned long membase, unsigned long memsize)
>>>  	memset((void *)ttb, 0, GRANULE_SIZE);
>>>  
>>>  	early_remap_range(0, 1UL << (BITS_PER_VA - 1), MAP_UNCACHED);
>>> -	early_remap_range(membase, memsize - OPTEE_SIZE, MAP_CACHED);
>>> -	early_remap_range(membase + memsize - OPTEE_SIZE, OPTEE_SIZE, MAP_FAULT);
>>> +
>>> +	if (optee_get_membase(&optee_membase)) {
>>> +		/* Fallback and place OP-TEE at the memory end region */
>>> +		early_remap_range(membase, memsize - OPTEE_SIZE, MAP_CACHED);
>>> +		early_remap_range(membase + memsize - OPTEE_SIZE, OPTEE_SIZE, MAP_FAULT);
>>> +	} else {
>>> +		early_remap_range(membase, memsize, MAP_CACHED);
>>> +		early_remap_range(optee_membase, OPTEE_SIZE, MAP_FAULT);
>>> +	}
>>> +
>>>  	early_remap_range(PAGE_ALIGN_DOWN((uintptr_t)_stext), PAGE_ALIGN(_etext - _stext), MAP_CACHED);
>>>  
>>>  	mmu_enable();
>>> diff --git a/arch/arm/mach-imx/esdctl.c b/arch/arm/mach-imx/esdctl.c
>>> index ac35a2de66f1..47c43d75ec75 100644
>>> --- a/arch/arm/mach-imx/esdctl.c
>>> +++ b/arch/arm/mach-imx/esdctl.c
>>> @@ -29,6 +29,8 @@
>>>  #include <mach/imx/imx8m-regs.h>
>>>  #include <mach/imx/imx7-regs.h>
>>>  #include <mach/imx/imx9-regs.h>
>>> +#include <mach/imx/scratch.h>
>>> +#include <tee/optee.h>
>>>  
>>>  struct imx_esdctl_data {
>>>  	unsigned long base0;
>>> @@ -1004,6 +1006,8 @@ resource_size_t imx8m_barebox_earlymem_size(unsigned buswidth)
>>>  
>>>  static void __noreturn imx8m_barebox_entry(void *boarddata, unsigned buswidth)
>>>  {
>>> +	imx8m_init_scratch_space(buswidth, false);
>>> +	optee_set_membase(imx8m_scratch_get_optee_hdr());
>>>  	barebox_arm_entry(MX8M_DDR_CSD1_BASE_ADDR,
>>>  			  imx8m_barebox_earlymem_size(buswidth), boarddata);
>>>  }
>>> diff --git a/common/Makefile b/common/Makefile
>>> index 7fb864f61480..c31cbab9e48f 100644
>>> --- a/common/Makefile
>>> +++ b/common/Makefile
>>> @@ -80,7 +80,7 @@ obj-$(CONFIG_BAREBOX_UPDATE_IMX_NAND_FCB) += imx-bbu-nand-fcb.o
>>>  obj-$(CONFIG_BOOT)		+= boot.o
>>>  obj-$(CONFIG_SERIAL_DEV_BUS)	+= serdev.o
>>>  obj-$(CONFIG_USBGADGET_START)	+= usbgadget.o
>>> -pbl-$(CONFIG_PBL_OPTEE)		+= optee.o
>>> +obj-pbl-$(CONFIG_PBL_OPTEE)	+= optee.o
>>>  obj-$(CONFIG_BOOTM_OPTEE)	+= optee.o
>>>  obj-$(CONFIG_FASTBOOT_BASE)	+= fastboot.o
>>>  
>>
>> -- 
>> Pengutronix e.K.                           |                             |
>> Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
>> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
>> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
>>
>>
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros
  2024-01-11 12:16     ` Marco Felsch
@ 2024-01-11 15:28       ` Ahmad Fatoum
  0 siblings, 0 replies; 26+ messages in thread
From: Ahmad Fatoum @ 2024-01-11 15:28 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On 11.01.24 13:16, Marco Felsch wrote:
> On 24-01-11, Ahmad Fatoum wrote:
>> On 11.01.24 12:48, Marco Felsch wrote:
>>>  	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MN_OPTEE)) {
>>> @@ -305,9 +305,9 @@ __noreturn void imx8mq_load_and_start_image_via_tfa(void)
>>>  	const void *bl31;
>>>  	size_t bl31_size;
>>>  	void *bl33 = (void *)MX8M_ATF_BL33_BASE_ADDR;
>>> -	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(16);
>>> +	unsigned long endmem = MX8M_DDR_CSD1_BASE_ADDR + imx8m_barebox_earlymem_size(32);
>>
>> Why this change?
> 
> Because the ddr-bandwidth for i.MX8MQ is 32-bit and the usage of
> imx8mq_save_bootrom_log() will fix this as well.

Ah, you even mention it in the commit message even.

Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

Thanks,
Ahmad

> 
> Regards,
>   Marco
> 
>>
>>>  
>>> -	imx8m_save_bootrom_log((void *)arm_mem_scratch(endmem));
>>> +	imx8mq_save_bootrom_log();
>>>  	imx8mq_load_bl33(bl33);
>>>  
>>>  	if (IS_ENABLED(CONFIG_FIRMWARE_IMX8MQ_OPTEE)) {
>>
>> -- 
>> Pengutronix e.K.                           |                             |
>> Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
>> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
>> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
>>
>>
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2024-01-11 15:29 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-11 11:48 [PATCH 00/18] Dynamic OP-TEE Loading Marco Felsch
2024-01-11 11:48 ` [PATCH 01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros Marco Felsch
2024-01-11 12:06   ` Ahmad Fatoum
2024-01-11 12:16     ` Marco Felsch
2024-01-11 15:28       ` Ahmad Fatoum
2024-01-11 11:48 ` [PATCH 02/18] ARM: i.MX8M: bundle imx-scratch code Marco Felsch
2024-01-11 11:48 ` [PATCH 03/18] ARM: i.MX8M: scratch: make imx_scratch_space private Marco Felsch
2024-01-11 11:48 ` [PATCH 04/18] ARM: i.MX8M: romapi: refactor saving the bootrom log Marco Felsch
2024-01-11 11:48 ` [PATCH 05/18] ARM: i.MX8M: scratch: add optee_hdr area Marco Felsch
2024-01-11 11:48 ` [PATCH 06/18] common: limit BOOTM_OPTEE to 32bit systems Marco Felsch
2024-01-11 11:48 ` [PATCH 07/18] common: add OPTEE_SHM_SIZE to configure optee shared memory Marco Felsch
2024-01-11 13:24   ` Marco Felsch
2024-01-11 11:48 ` [PATCH 08/18] optee: add support to verify 64-bit headers as well Marco Felsch
2024-01-11 11:48 ` [PATCH 09/18] optee: add header version check Marco Felsch
2024-01-11 11:48 ` [PATCH 10/18] optee: add helper functions to set/get the optee memory base Marco Felsch
2024-01-11 11:48 ` [PATCH 11/18] optee: optee_verify_header: constify optee_header Marco Felsch
2024-01-11 11:48 ` [PATCH 12/18] ARM64: mmu: add dynamic optee memory mapping support Marco Felsch
2024-01-11 12:07   ` Ahmad Fatoum
2024-01-11 12:14     ` Marco Felsch
2024-01-11 15:25       ` Ahmad Fatoum
2024-01-11 11:48 ` [PATCH 13/18] ARM: i.MX8M: add dynamic optee memory of-fixup support Marco Felsch
2024-01-11 11:48 ` [PATCH 14/18] drivers: tee: optee: add support for dynamic optee memory base address Marco Felsch
2024-01-11 11:48 ` [PATCH 15/18] ARM: i.MX8M: atf: add support for optee hdr parsing Marco Felsch
2024-01-11 11:48 ` [PATCH 16/18] ARM: i.MX8M: allow board code to configure the bl33 loadaddr Marco Felsch
2024-01-11 11:48 ` [PATCH 17/18] ARM: i.MX8M: cleanup MX8M*_ATF_BL33_BASE_ADDR defines Marco Felsch
2024-01-11 11:48 ` [PATCH 18/18] ARM: i.MX8M: fix optee of-fixup logic Marco Felsch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox