mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2 00/18] Dynamic OP-TEE Loading
@ 2024-01-16 17:07 Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros Marco Felsch
                   ` (18 more replies)
  0 siblings, 19 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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.

This series is based on origin/next
(c324e9ea7f8808ae069cd8bd0aa53021c1239bd2).

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                     |  9 +-
 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                | 14 +--
 common/Kconfig                                | 11 ++-
 common/Makefile                               |  2 +-
 common/optee.c                                | 44 ++++++++-
 drivers/mci/imx-esdhc-pbl.c                   | 12 ++-
 drivers/tee/optee/of_fixup.c                  | 26 +++++-
 include/asm-generic/memory_layout.h           |  6 ++
 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                           | 23 ++++-
 23 files changed, 347 insertions(+), 115 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] 32+ messages in thread

* [PATCH v2 01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 02/18] ARM: i.MX8M: bundle imx-scratch code Marco Felsch
                   ` (17 subsequent siblings)
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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>
Reviewed-by: Ahmad Fatoum <a.fatoum@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 f9baad862dc7..5dfa7eeb50b9 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] 32+ messages in thread

* [PATCH v2 02/18] ARM: i.MX8M: bundle imx-scratch code
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-17 14:36   ` Sascha Hauer
  2024-01-16 17:07 ` [PATCH v2 03/18] ARM: i.MX8M: scratch: make imx_scratch_space private Marco Felsch
                   ` (16 subsequent siblings)
  18 siblings, 1 reply; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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 32f12cd7f574..3bbb7119b1b0 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 05932fac88ce..710068a8d41d 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 66219f9fef30..99f526d38be9 100644
--- a/include/mach/imx/xload.h
+++ b/include/mach/imx/xload.h
@@ -45,15 +45,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] 32+ messages in thread

* [PATCH v2 03/18] ARM: i.MX8M: scratch: make imx_scratch_space private
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 02/18] ARM: i.MX8M: bundle imx-scratch code Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 04/18] ARM: i.MX8M: romapi: refactor saving the bootrom log Marco Felsch
                   ` (15 subsequent siblings)
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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] 32+ messages in thread

* [PATCH v2 04/18] ARM: i.MX8M: romapi: refactor saving the bootrom log
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (2 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 03/18] ARM: i.MX8M: scratch: make imx_scratch_space private Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 05/18] ARM: i.MX8M: scratch: add optee_hdr area Marco Felsch
                   ` (14 subsequent siblings)
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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 5dfa7eeb50b9..3f796249061d 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 710068a8d41d..ae4e49cd7d25 100644
--- a/include/mach/imx/romapi.h
+++ b/include/mach/imx/romapi.h
@@ -41,12 +41,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] 32+ messages in thread

* [PATCH v2 05/18] ARM: i.MX8M: scratch: add optee_hdr area
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (3 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 04/18] ARM: i.MX8M: romapi: refactor saving the bootrom log Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-17 15:00   ` Sascha Hauer
  2024-01-16 17:07 ` [PATCH v2 06/18] common: limit BOOTM_OPTEE to 32bit systems Marco Felsch
                   ` (13 subsequent siblings)
  18 siblings, 1 reply; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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] 32+ messages in thread

* [PATCH v2 06/18] common: limit BOOTM_OPTEE to 32bit systems
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (4 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 05/18] ARM: i.MX8M: scratch: add optee_hdr area Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 07/18] common: add OPTEE_SHM_SIZE to configure optee shared memory Marco Felsch
                   ` (12 subsequent siblings)
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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 02f376c59757..c38cfaed0e9f 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -1143,7 +1143,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] 32+ messages in thread

* [PATCH v2 07/18] common: add OPTEE_SHM_SIZE to configure optee shared memory
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (5 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 06/18] common: limit BOOTM_OPTEE to 32bit systems Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 08/18] optee: add support to verify 64-bit headers as well Marco Felsch
                   ` (11 subsequent siblings)
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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           | 3 ++-
 common/Kconfig                      | 9 +++++++++
 include/asm-generic/memory_layout.h | 6 ++++++
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/arch/arm/mach-imx/imx8m.c b/arch/arm/mach-imx/imx8m.c
index c7c799c64bc4..ca50eec63622 100644
--- a/arch/arm/mach-imx/imx8m.c
+++ b/arch/arm/mach-imx/imx8m.c
@@ -8,6 +8,7 @@
 #include <pm_domain.h>
 #include <asm/syscounter.h>
 #include <asm/system.h>
+#include <asm-generic/memory_layout.h>
 #include <mach/imx/generic.h>
 #include <mach/imx/revision.h>
 #include <mach/imx/imx8mq.h>
@@ -68,7 +69,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 = OPTEE_SHM_SIZE,
 			.method = "smc",
 		};
 
diff --git a/common/Kconfig b/common/Kconfig
index c38cfaed0e9f..ddca1e913b18 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -1140,6 +1140,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"
diff --git a/include/asm-generic/memory_layout.h b/include/asm-generic/memory_layout.h
index 7593e18da151..6af1db8113f2 100644
--- a/include/asm-generic/memory_layout.h
+++ b/include/asm-generic/memory_layout.h
@@ -19,6 +19,12 @@
 #define OPTEE_SIZE 0
 #endif
 
+#ifdef CONFIG_OPTEE_SHM_SIZE
+#define OPTEE_SHM_SIZE CONFIG_OPTEE_SHM_SIZE
+#else
+#define OPTEE_SHM_SIZE 0
+#endif
+
 #define HEAD_TEXT_BASE MALLOC_BASE
 #define MALLOC_SIZE CONFIG_MALLOC_SIZE
 #define STACK_SIZE  CONFIG_STACK_SIZE
-- 
2.39.2




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

* [PATCH v2 08/18] optee: add support to verify 64-bit headers as well
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (6 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 07/18] common: add OPTEE_SHM_SIZE to configure optee shared memory Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 09/18] optee: add header version check Marco Felsch
                   ` (10 subsequent siblings)
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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] 32+ messages in thread

* [PATCH v2 09/18] optee: add header version check
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (7 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 08/18] optee: add support to verify 64-bit headers as well Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 10/18] optee: add helper functions to set/get the optee memory base Marco Felsch
                   ` (9 subsequent siblings)
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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] 32+ messages in thread

* [PATCH v2 10/18] optee: add helper functions to set/get the optee memory base
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (8 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 09/18] optee: add header version check Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 11/18] optee: optee_verify_header: constify optee_header Marco Felsch
                   ` (8 subsequent siblings)
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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 | 18 ++++++++++++++++++
 2 files changed, 45 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..0cdf828a6d3a 100644
--- a/include/tee/optee.h
+++ b/include/tee/optee.h
@@ -33,6 +33,24 @@ struct optee_header {
 
 int optee_verify_header (struct optee_header *hdr);
 
+#ifdef CONFIG_HAVE_OPTEE
+
+void optee_set_membase(const struct optee_header *hdr);
+int optee_get_membase(u64 *membase);
+
+#else
+
+static inline void optee_set_membase(const struct optee_header *hdr)
+{
+}
+
+static inline int optee_get_membase(u64 *membase)
+{
+	return -ENOSYS;
+}
+
+#endif /* CONFIG_HAVE_OPTEE */
+
 #ifdef __PBL__
 
 int start_optee_early(void* fdt, void* tee);
-- 
2.39.2




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

* [PATCH v2 11/18] optee: optee_verify_header: constify optee_header
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (9 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 10/18] optee: add helper functions to set/get the optee memory base Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 12/18] ARM64: mmu: add dynamic optee memory mapping support Marco Felsch
                   ` (7 subsequent siblings)
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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 0cdf828a6d3a..f52775dab5b4 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);
 
 #ifdef CONFIG_HAVE_OPTEE
 
-- 
2.39.2




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

* [PATCH v2 12/18] ARM64: mmu: add dynamic optee memory mapping support
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (10 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 11/18] optee: optee_verify_header: constify optee_header Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-17 14:33   ` Sascha Hauer
  2024-01-16 17:07 ` [PATCH v2 13/18] ARM: i.MX8M: add dynamic optee memory of-fixup support Marco Felsch
                   ` (6 subsequent siblings)
  18 siblings, 1 reply; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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 84f45bc2c3c1..27fd15ea1233 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"
 
@@ -310,6 +311,7 @@ static void init_range(size_t total_level0_tables)
 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);
@@ -326,8 +328,16 @@ void mmu_early_enable(unsigned long membase, unsigned long memsize)
 	 * Set 1:1 mapping of VA->PA. So to cover the full 1TB range we need 2 tables.
 	 */
 	init_range(2);
-	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] 32+ messages in thread

* [PATCH v2 13/18] ARM: i.MX8M: add dynamic optee memory of-fixup support
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (11 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 12/18] ARM64: mmu: add dynamic optee memory mapping support Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 14/18] drivers: tee: optee: add support for dynamic optee memory base address Marco Felsch
                   ` (5 subsequent siblings)
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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 ca50eec63622..6db16f024148 100644
--- a/arch/arm/mach-imx/imx8m.c
+++ b/arch/arm/mach-imx/imx8m.c
@@ -17,8 +17,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>
@@ -73,6 +75,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] 32+ messages in thread

* [PATCH v2 14/18] drivers: tee: optee: add support for dynamic optee memory base address
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (12 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 13/18] ARM: i.MX8M: add dynamic optee memory of-fixup support Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 15/18] ARM: i.MX8M: atf: add support for optee hdr parsing Marco Felsch
                   ` (4 subsequent siblings)
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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_fixup.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/drivers/tee/optee/of_fixup.c b/drivers/tee/optee/of_fixup.c
index 8295a1751c52..cdf650592e90 100644
--- a/drivers/tee/optee/of_fixup.c
+++ b/drivers/tee/optee/of_fixup.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] 32+ messages in thread

* [PATCH v2 15/18] ARM: i.MX8M: atf: add support for optee hdr parsing
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (13 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 14/18] drivers: tee: optee: add support for dynamic optee memory base address Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 16/18] ARM: i.MX8M: allow board code to configure the bl33 loadaddr Marco Felsch
                   ` (3 subsequent siblings)
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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 3f796249061d..d7698b397f47 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] 32+ messages in thread

* [PATCH v2 16/18] ARM: i.MX8M: allow board code to configure the bl33 loadaddr
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (14 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 15/18] ARM: i.MX8M: atf: add support for optee hdr parsing Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 17/18] ARM: i.MX8M: cleanup MX8M*_ATF_BL33_BASE_ADDR defines Marco Felsch
                   ` (2 subsequent siblings)
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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 | 14 ++++++------
 drivers/mci/imx-esdhc-pbl.c    | 12 ++++++----
 include/mach/imx/romapi.h      |  4 ++--
 include/mach/imx/xload.h       | 17 +++++++++-----
 6 files changed, 63 insertions(+), 41 deletions(-)

diff --git a/arch/arm/mach-imx/atf.c b/arch/arm/mach-imx/atf.c
index d7698b397f47..f5b62d24ebeb 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);
+		imx8m_esdhc_load_image(instance, 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);
+		imx8mm_qspi_load_image(instance, 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);
+		imx8mp_esdhc_load_image(instance, bl33);
 		break;
 	case BOOTSOURCE_SERIAL:
-		imx8mp_romapi_load_image();
+		imx8mp_romapi_load_image(bl33);
 		break;
 	case BOOTSOURCE_SPI:
-		imx8mp_qspi_load_image(instance);
+		imx8mp_qspi_load_image(instance, 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);
+		imx8mn_esdhc_load_image(instance, bl33);
 		break;
 	case BOOTSOURCE_SERIAL:
-		imx8mn_romapi_load_image();
+		imx8mn_romapi_load_image(bl33);
 		break;
 	case BOOTSOURCE_SPI:
-		imx8mn_qspi_load_image(instance);
+		imx8mn_qspi_load_image(instance, 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);
+		imx8m_esdhc_load_image(instance, 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 5089f20d627c..327a560f8b70 100644
--- a/arch/arm/mach-imx/xload-qspi.c
+++ b/arch/arm/mach-imx/xload-qspi.c
@@ -32,24 +32,24 @@ int imx8m_qspi_read(void *dest, size_t len, void *priv)
  * A negative error code is returned when this function fails.
  */
 static
-int imx8m_qspi_load_image(int instance, off_t offset, off_t ivt_offset)
+int imx8m_qspi_load_image(int instance, 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,
+	return imx_load_image(MX8M_DDR_CSD1_BASE_ADDR, (ptrdiff_t)bl33,
 			      offset, ivt_offset, false, 0,
 			      imx8m_qspi_read, qspi_ahb);
 }
 
-int imx8mm_qspi_load_image(int instance)
+int imx8mm_qspi_load_image(int instance, void *bl33)
 {
-	return imx8m_qspi_load_image(instance, 0, SZ_4K);
+	return imx8m_qspi_load_image(instance, 0, SZ_4K, bl33);
 }
 
-int imx8mn_qspi_load_image(int instance)
+int imx8mn_qspi_load_image(int instance, void *bl33)
 {
-	return imx8m_qspi_load_image(instance, SZ_4K, 0);
+	return imx8m_qspi_load_image(instance, SZ_4K, 0, bl33);
 }
 
-int imx8mp_qspi_load_image(int instance)
+int imx8mp_qspi_load_image(int instance, void *bl33)
 	__alias(imx8mn_qspi_load_image);
diff --git a/drivers/mci/imx-esdhc-pbl.c b/drivers/mci/imx-esdhc-pbl.c
index 2cb703a0c552..5b1d9a3cf41e 100644
--- a/drivers/mci/imx-esdhc-pbl.c
+++ b/drivers/mci/imx-esdhc-pbl.c
@@ -242,6 +242,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)
+ * @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
@@ -251,7 +252,7 @@ int imx7_esdhc_start_image(int instance)
  * Return: If image successfully loaded, returns 0.
  * A negative error code is returned when this function fails.
  */
-int imx8m_esdhc_load_image(int instance)
+int imx8m_esdhc_load_image(int instance, void *bl33)
 {
 	struct esdhc_soc_data data;
 	struct fsl_esdhc_host host = { 0 };
@@ -262,13 +263,14 @@ int imx8m_esdhc_load_image(int instance)
 		return ret;
 
 	return esdhc_load_image(&host, MX8M_DDR_CSD1_BASE_ADDR,
-				MX8MQ_ATF_BL33_BASE_ADDR, SZ_32K, SZ_1K,
+				(ptrdiff_t)bl33, SZ_32K, SZ_1K,
 				false);
 }
 
 /**
  * imx8mp_esdhc_load_image - Load and optionally start an image from USDHC controller
  * @instance: The USDHC controller instance (0..2)
+ * @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
@@ -278,7 +280,7 @@ int imx8m_esdhc_load_image(int instance)
  * Return: If image successfully loaded, returns 0.
  * A negative error code is returned when this function fails.
  */
-int imx8mp_esdhc_load_image(int instance)
+int imx8mp_esdhc_load_image(int instance, void *bl33)
 {
 	struct esdhc_soc_data data;
 	struct fsl_esdhc_host host = { 0 };
@@ -292,10 +294,10 @@ int imx8mp_esdhc_load_image(int instance)
 	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, false);
+				(ptrdiff_t)bl33, offset, 0, false);
 }
 
-int imx8mn_esdhc_load_image(int instance)
+int imx8mn_esdhc_load_image(int instance, void *bl33)
 	__alias(imx8mp_esdhc_load_image);
 #endif
 
diff --git a/include/mach/imx/romapi.h b/include/mach/imx/romapi.h
index ae4e49cd7d25..e26b98097d37 100644
--- a/include/mach/imx/romapi.h
+++ b/include/mach/imx/romapi.h
@@ -36,8 +36,8 @@ enum boot_dev_type_e {
 #define ROM_API_OKAY		0xF0
 
 /* Below functions only load and don't start the image */
-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 99f526d38be9..3a396ac4535c 100644
--- a/include/mach/imx/xload.h
+++ b/include/mach/imx/xload.h
@@ -16,12 +16,12 @@ int imx7_esdhc_start_image(int instance);
 int imx7_nand_start_image(void);
 
 /* Below functions only load and don't start the image */
-int imx8m_esdhc_load_image(int instance);
-int imx8mn_esdhc_load_image(int instance);
-int imx8mp_esdhc_load_image(int instance);
-int imx8mm_qspi_load_image(int instance);
-int imx8mn_qspi_load_image(int instance);
-int imx8mp_qspi_load_image(int instance);
+int imx8m_esdhc_load_image(int instance, void *bl33);
+int imx8mn_esdhc_load_image(int instance, void *bl33);
+int imx8mp_esdhc_load_image(int instance, void *bl33);
+int imx8mm_qspi_load_image(int instance, void *bl33);
+int imx8mn_qspi_load_image(int instance, void *bl33);
+int imx8mp_qspi_load_image(int instance, void *bl33);
 
 void imx8mm_load_bl33(void *bl33);
 void imx8mn_load_bl33(void *bl33);
@@ -32,6 +32,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] 32+ messages in thread

* [PATCH v2 17/18] ARM: i.MX8M: cleanup MX8M*_ATF_BL33_BASE_ADDR defines
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (15 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 16/18] ARM: i.MX8M: allow board code to configure the bl33 loadaddr Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-16 17:07 ` [PATCH v2 18/18] ARM: i.MX8M: fix optee of-fixup logic Marco Felsch
  2024-01-19  8:02 ` [PATCH v2 00/18] Dynamic OP-TEE Loading Sascha Hauer
  18 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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] 32+ messages in thread

* [PATCH v2 18/18] ARM: i.MX8M: fix optee of-fixup logic
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (16 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 17/18] ARM: i.MX8M: cleanup MX8M*_ATF_BL33_BASE_ADDR defines Marco Felsch
@ 2024-01-16 17:07 ` Marco Felsch
  2024-01-18 15:30   ` Sascha Hauer
  2024-01-19  8:02 ` [PATCH v2 00/18] Dynamic OP-TEE Loading Sascha Hauer
  18 siblings, 1 reply; 32+ messages in thread
From: Marco Felsch @ 2024-01-16 17:07 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_fixup.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 6db16f024148..73b420b38697 100644
--- a/arch/arm/mach-imx/imx8m.c
+++ b/arch/arm/mach-imx/imx8m.c
@@ -68,8 +68,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 = OPTEE_SHM_SIZE,
 			.method = "smc",
diff --git a/drivers/tee/optee/of_fixup.c b/drivers/tee/optee/of_fixup.c
index cdf650592e90..2ff1f01964c8 100644
--- a/drivers/tee/optee/of_fixup.c
+++ b/drivers/tee/optee/of_fixup.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] 32+ messages in thread

* Re: [PATCH v2 12/18] ARM64: mmu: add dynamic optee memory mapping support
  2024-01-16 17:07 ` [PATCH v2 12/18] ARM64: mmu: add dynamic optee memory mapping support Marco Felsch
@ 2024-01-17 14:33   ` Sascha Hauer
  2024-01-18  9:13     ` Marco Felsch
  0 siblings, 1 reply; 32+ messages in thread
From: Sascha Hauer @ 2024-01-17 14:33 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On Tue, Jan 16, 2024 at 06:07:32PM +0100, 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>
> ---
>  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 84f45bc2c3c1..27fd15ea1233 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"
>  
> @@ -310,6 +311,7 @@ static void init_range(size_t total_level0_tables)
>  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);
> @@ -326,8 +328,16 @@ void mmu_early_enable(unsigned long membase, unsigned long memsize)
>  	 * Set 1:1 mapping of VA->PA. So to cover the full 1TB range we need 2 tables.
>  	 */
>  	init_range(2);
> -	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);
> +	}

You could replace the above with:

	early_remap_range(membase, memsize, MAP_CACHED);

	optee_membase = membase + memsize - OPTEE_SIZE;
	optee_get_membase(&optee_membase);
	early_remap_range(optee_membase, OPTEE_SIZE, MAP_FAULT);

Sascha

-- 
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] 32+ messages in thread

* Re: [PATCH v2 02/18] ARM: i.MX8M: bundle imx-scratch code
  2024-01-16 17:07 ` [PATCH v2 02/18] ARM: i.MX8M: bundle imx-scratch code Marco Felsch
@ 2024-01-17 14:36   ` Sascha Hauer
  2024-01-18  9:11     ` Marco Felsch
  0 siblings, 1 reply; 32+ messages in thread
From: Sascha Hauer @ 2024-01-17 14:36 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On Tue, Jan 16, 2024 at 06:07:22PM +0100, Marco Felsch wrote:
> 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);

This reservation doesn't seem to be i.MX specific. You could move this
to a more generic place.

Sascha

-- 
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] 32+ messages in thread

* Re: [PATCH v2 05/18] ARM: i.MX8M: scratch: add optee_hdr area
  2024-01-16 17:07 ` [PATCH v2 05/18] ARM: i.MX8M: scratch: add optee_hdr area Marco Felsch
@ 2024-01-17 15:00   ` Sascha Hauer
  2024-01-18  9:07     ` Marco Felsch
  0 siblings, 1 reply; 32+ messages in thread
From: Sascha Hauer @ 2024-01-17 15:00 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On Tue, Jan 16, 2024 at 06:07:25PM +0100, Marco Felsch wrote:
> 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;
> +}

These two function do not seem to be i.MX8 specific, I could reuse them
on i.MX9, so better with imx_ prefix?

Sascha

-- 
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] 32+ messages in thread

* Re: [PATCH v2 05/18] ARM: i.MX8M: scratch: add optee_hdr area
  2024-01-17 15:00   ` Sascha Hauer
@ 2024-01-18  9:07     ` Marco Felsch
  0 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-18  9:07 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 24-01-17, Sascha Hauer wrote:
> On Tue, Jan 16, 2024 at 06:07:25PM +0100, Marco Felsch wrote:
> > 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;
> > +}
> 
> These two function do not seem to be i.MX8 specific, I could reuse them
> on i.MX9, so better with imx_ prefix?

Yes, I can do that.

Regards,
  Marco

> 
> Sascha
> 
> -- 
> 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] 32+ messages in thread

* Re: [PATCH v2 02/18] ARM: i.MX8M: bundle imx-scratch code
  2024-01-17 14:36   ` Sascha Hauer
@ 2024-01-18  9:11     ` Marco Felsch
  2024-01-18 10:16       ` Sascha Hauer
  0 siblings, 1 reply; 32+ messages in thread
From: Marco Felsch @ 2024-01-18  9:11 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 24-01-17, Sascha Hauer wrote:
> On Tue, Jan 16, 2024 at 06:07:22PM +0100, Marco Felsch wrote:
> > 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);
> 
> This reservation doesn't seem to be i.MX specific. You could move this
> to a more generic place.

You mean to use it for Rockchip,... as well or just for all i.MX SoCs? I
wanted to keep this commit as simple as possible by just shifting the
code from placeA to placeB without changing the logic nor the naming.

Regards,
  Marco

> Sascha
> 
> -- 
> 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] 32+ messages in thread

* Re: [PATCH v2 12/18] ARM64: mmu: add dynamic optee memory mapping support
  2024-01-17 14:33   ` Sascha Hauer
@ 2024-01-18  9:13     ` Marco Felsch
  2024-01-18 10:17       ` Sascha Hauer
  0 siblings, 1 reply; 32+ messages in thread
From: Marco Felsch @ 2024-01-18  9:13 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 24-01-17, Sascha Hauer wrote:
> On Tue, Jan 16, 2024 at 06:07:32PM +0100, 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>
> > ---
> >  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 84f45bc2c3c1..27fd15ea1233 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"
> >  
> > @@ -310,6 +311,7 @@ static void init_range(size_t total_level0_tables)
> >  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);
> > @@ -326,8 +328,16 @@ void mmu_early_enable(unsigned long membase, unsigned long memsize)
> >  	 * Set 1:1 mapping of VA->PA. So to cover the full 1TB range we need 2 tables.
> >  	 */
> >  	init_range(2);
> > -	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);
> > +	}
> 
> You could replace the above with:
> 
> 	early_remap_range(membase, memsize, MAP_CACHED);
> 
> 	optee_membase = membase + memsize - OPTEE_SIZE;
> 	optee_get_membase(&optee_membase);
> 	early_remap_range(optee_membase, OPTEE_SIZE, MAP_FAULT);

Nice catch, thanks a lot. How about:

  	early_remap_range(membase, memsize, MAP_CACHED);
  
  	if (optee_get_membase(&optee_membase))
		optee_membase = membase + memsize - OPTEE_SIZE;
  	early_remap_range(optee_membase, OPTEE_SIZE, MAP_FAULT);

Regards,
  Marco

> 
> Sascha
> 
> -- 
> 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] 32+ messages in thread

* Re: [PATCH v2 02/18] ARM: i.MX8M: bundle imx-scratch code
  2024-01-18  9:11     ` Marco Felsch
@ 2024-01-18 10:16       ` Sascha Hauer
  0 siblings, 0 replies; 32+ messages in thread
From: Sascha Hauer @ 2024-01-18 10:16 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On Thu, Jan 18, 2024 at 10:11:18AM +0100, Marco Felsch wrote:
> On 24-01-17, Sascha Hauer wrote:
> > On Tue, Jan 16, 2024 at 06:07:22PM +0100, Marco Felsch wrote:
> > > 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);
> > 
> > This reservation doesn't seem to be i.MX specific. You could move this
> > to a more generic place.
> 
> You mean to use it for Rockchip,... as well or just for all i.MX SoCs? I
> wanted to keep this commit as simple as possible by just shifting the
> code from placeA to placeB without changing the logic nor the naming.

I didn't notice that this code was just moved, so it's ok for this
commit.

What I really meant is that this could be ARM generic as the scratch
area exists on all ARM machines. It's size is 32KiB, I am not sure why
sizeof(struct imx_scratch_space) is used here. It could be used to
request some other offset in the scratch area for somehing else, but do
we really want to go that path?
If this reservation is only meant to see something in the iomem output,
then we could make this reservation ARM generic.

Sascha

-- 
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] 32+ messages in thread

* Re: [PATCH v2 12/18] ARM64: mmu: add dynamic optee memory mapping support
  2024-01-18  9:13     ` Marco Felsch
@ 2024-01-18 10:17       ` Sascha Hauer
  0 siblings, 0 replies; 32+ messages in thread
From: Sascha Hauer @ 2024-01-18 10:17 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On Thu, Jan 18, 2024 at 10:13:43AM +0100, Marco Felsch wrote:
> On 24-01-17, Sascha Hauer wrote:
> > On Tue, Jan 16, 2024 at 06:07:32PM +0100, 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>
> > > ---
> > >  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 84f45bc2c3c1..27fd15ea1233 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"
> > >  
> > > @@ -310,6 +311,7 @@ static void init_range(size_t total_level0_tables)
> > >  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);
> > > @@ -326,8 +328,16 @@ void mmu_early_enable(unsigned long membase, unsigned long memsize)
> > >  	 * Set 1:1 mapping of VA->PA. So to cover the full 1TB range we need 2 tables.
> > >  	 */
> > >  	init_range(2);
> > > -	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);
> > > +	}
> > 
> > You could replace the above with:
> > 
> > 	early_remap_range(membase, memsize, MAP_CACHED);
> > 
> > 	optee_membase = membase + memsize - OPTEE_SIZE;
> > 	optee_get_membase(&optee_membase);
> > 	early_remap_range(optee_membase, OPTEE_SIZE, MAP_FAULT);
> 
> Nice catch, thanks a lot. How about:
> 
>   	early_remap_range(membase, memsize, MAP_CACHED);
>   
>   	if (optee_get_membase(&optee_membase))
> 		optee_membase = membase + memsize - OPTEE_SIZE;
>   	early_remap_range(optee_membase, OPTEE_SIZE, MAP_FAULT);

Also fine.

Sascha


-- 
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] 32+ messages in thread

* Re: [PATCH v2 18/18] ARM: i.MX8M: fix optee of-fixup logic
  2024-01-16 17:07 ` [PATCH v2 18/18] ARM: i.MX8M: fix optee of-fixup logic Marco Felsch
@ 2024-01-18 15:30   ` Sascha Hauer
  2024-01-18 16:52     ` Marco Felsch
  0 siblings, 1 reply; 32+ messages in thread
From: Sascha Hauer @ 2024-01-18 15:30 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox

On Tue, Jan 16, 2024 at 06:07:38PM +0100, Marco Felsch wrote:
> 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_fixup.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 6db16f024148..73b420b38697 100644
> --- a/arch/arm/mach-imx/imx8m.c
> +++ b/arch/arm/mach-imx/imx8m.c
> @@ -68,8 +68,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 = OPTEE_SHM_SIZE,
>  			.method = "smc",
> diff --git a/drivers/tee/optee/of_fixup.c b/drivers/tee/optee/of_fixup.c
> index cdf650592e90..2ff1f01964c8 100644
> --- a/drivers/tee/optee/of_fixup.c
> +++ b/drivers/tee/optee/of_fixup.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;

This implements a "when the internal dts does have a OP-TEE node, then
do not fixup the external dts". This is surely not what you meant. Should
this be

	if (of_find_node_by_path_from(root, optee_of_path))
		return 0;

or something else?

Sascha

-- 
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] 32+ messages in thread

* Re: [PATCH v2 18/18] ARM: i.MX8M: fix optee of-fixup logic
  2024-01-18 15:30   ` Sascha Hauer
@ 2024-01-18 16:52     ` Marco Felsch
  0 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-18 16:52 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 24-01-18, Sascha Hauer wrote:
> On Tue, Jan 16, 2024 at 06:07:38PM +0100, Marco Felsch wrote:
> > 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_fixup.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 6db16f024148..73b420b38697 100644
> > --- a/arch/arm/mach-imx/imx8m.c
> > +++ b/arch/arm/mach-imx/imx8m.c
> > @@ -68,8 +68,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 = OPTEE_SHM_SIZE,
> >  			.method = "smc",
> > diff --git a/drivers/tee/optee/of_fixup.c b/drivers/tee/optee/of_fixup.c
> > index cdf650592e90..2ff1f01964c8 100644
> > --- a/drivers/tee/optee/of_fixup.c
> > +++ b/drivers/tee/optee/of_fixup.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;
> 
> This implements a "when the internal dts does have a OP-TEE node, then
> do not fixup the external dts". This is surely not what you meant. 

Arg..

> Should this be
> 
> 	if (of_find_node_by_path_from(root, optee_of_path))
> 		return 0;

exactly, thanks.

Regards,
  Marco

> or something else?
> 
> Sascha
> 
> -- 
> 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] 32+ messages in thread

* Re: [PATCH v2 00/18] Dynamic OP-TEE Loading
  2024-01-16 17:07 [PATCH v2 00/18] Dynamic OP-TEE Loading Marco Felsch
                   ` (17 preceding siblings ...)
  2024-01-16 17:07 ` [PATCH v2 18/18] ARM: i.MX8M: fix optee of-fixup logic Marco Felsch
@ 2024-01-19  8:02 ` Sascha Hauer
  2024-01-19  8:09   ` Sascha Hauer
  18 siblings, 1 reply; 32+ messages in thread
From: Sascha Hauer @ 2024-01-19  8:02 UTC (permalink / raw)
  To: barebox, Marco Felsch


On Tue, 16 Jan 2024 18:07:20 +0100, Marco Felsch wrote:
> 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.
> 
> [...]

Applied, thanks!

[01/18] ARM: i.MX8M: atf: make use of imx8m*_save_bootrom_log macros
        https://git.pengutronix.de/cgit/barebox/commit/?id=264b718bb748 (link may not be stable)
[02/18] ARM: i.MX8M: bundle imx-scratch code
        https://git.pengutronix.de/cgit/barebox/commit/?id=b064b6bef70e (link may not be stable)
[03/18] ARM: i.MX8M: scratch: make imx_scratch_space private
        https://git.pengutronix.de/cgit/barebox/commit/?id=071141f15e6e (link may not be stable)
[04/18] ARM: i.MX8M: romapi: refactor saving the bootrom log
        https://git.pengutronix.de/cgit/barebox/commit/?id=2073164ffc28 (link may not be stable)
[05/18] ARM: i.MX8M: scratch: add optee_hdr area
        https://git.pengutronix.de/cgit/barebox/commit/?id=62d1c1433600 (link may not be stable)
[06/18] common: limit BOOTM_OPTEE to 32bit systems
        https://git.pengutronix.de/cgit/barebox/commit/?id=52f5df29ee34 (link may not be stable)
[07/18] common: add OPTEE_SHM_SIZE to configure optee shared memory
        https://git.pengutronix.de/cgit/barebox/commit/?id=cf6bad3c3566 (link may not be stable)
[08/18] optee: add support to verify 64-bit headers as well
        https://git.pengutronix.de/cgit/barebox/commit/?id=5f2f1f5fc1a9 (link may not be stable)
[09/18] optee: add header version check
        https://git.pengutronix.de/cgit/barebox/commit/?id=19f07229bc19 (link may not be stable)
[10/18] optee: add helper functions to set/get the optee memory base
        https://git.pengutronix.de/cgit/barebox/commit/?id=99c0794d820a (link may not be stable)
[11/18] optee: optee_verify_header: constify optee_header
        https://git.pengutronix.de/cgit/barebox/commit/?id=5e9680111464 (link may not be stable)
[12/18] ARM64: mmu: add dynamic optee memory mapping support
        https://git.pengutronix.de/cgit/barebox/commit/?id=cd0d14fff1c9 (link may not be stable)
[13/18] ARM: i.MX8M: add dynamic optee memory of-fixup support
        https://git.pengutronix.de/cgit/barebox/commit/?id=b9d90943b720 (link may not be stable)
[14/18] drivers: tee: optee: add support for dynamic optee memory base address
        https://git.pengutronix.de/cgit/barebox/commit/?id=dee1d73921bf (link may not be stable)
[15/18] ARM: i.MX8M: atf: add support for optee hdr parsing
        https://git.pengutronix.de/cgit/barebox/commit/?id=532cdae92630 (link may not be stable)
[16/18] ARM: i.MX8M: allow board code to configure the bl33 loadaddr
        https://git.pengutronix.de/cgit/barebox/commit/?id=e10ea01ec023 (link may not be stable)
[17/18] ARM: i.MX8M: cleanup MX8M*_ATF_BL33_BASE_ADDR defines
        https://git.pengutronix.de/cgit/barebox/commit/?id=f58f1d227c15 (link may not be stable)
[18/18] ARM: i.MX8M: fix optee of-fixup logic
        https://git.pengutronix.de/cgit/barebox/commit/?id=fab23c06660b (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




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

* Re: [PATCH v2 00/18] Dynamic OP-TEE Loading
  2024-01-19  8:02 ` [PATCH v2 00/18] Dynamic OP-TEE Loading Sascha Hauer
@ 2024-01-19  8:09   ` Sascha Hauer
  2024-01-19  8:20     ` Marco Felsch
  0 siblings, 1 reply; 32+ messages in thread
From: Sascha Hauer @ 2024-01-19  8:09 UTC (permalink / raw)
  To: barebox, Marco Felsch

On Fri, Jan 19, 2024 at 09:02:15AM +0100, Sascha Hauer wrote:
> 
> On Tue, 16 Jan 2024 18:07:20 +0100, Marco Felsch wrote:
> > 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.
> > 
> > [...]
> 
> Applied, thanks!

I Applied it with the changes as discussed.

Sascha

-- 
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] 32+ messages in thread

* Re: [PATCH v2 00/18] Dynamic OP-TEE Loading
  2024-01-19  8:09   ` Sascha Hauer
@ 2024-01-19  8:20     ` Marco Felsch
  0 siblings, 0 replies; 32+ messages in thread
From: Marco Felsch @ 2024-01-19  8:20 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 24-01-19, Sascha Hauer wrote:
> On Fri, Jan 19, 2024 at 09:02:15AM +0100, Sascha Hauer wrote:
> > 
> > On Tue, 16 Jan 2024 18:07:20 +0100, Marco Felsch wrote:
> > > 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.
> > > 
> > > [...]
> > 
> > Applied, thanks!
> 
> I Applied it with the changes as discussed.

Thanks a lot.

Regards,
  Marco

> 
> Sascha
> 
> -- 
> 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] 32+ messages in thread

end of thread, other threads:[~2024-01-19  8:21 UTC | newest]

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

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