Skip to contentSkip to conversation

imx8m: Automatically add the optee firmware node to the FDT

PATCH In review Alban Bedel 3 participants 8 messages May 16, 2022 – May 18, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover imx8m: Automatically add the optee firmware node to the FDT

Hi everyone,

this short series is about letting u-boot automatically add the optee firmware node to the DTB on imx8m. The imx8 already does that using its own function and the optee lib has a very similar function which copy the DT node from u-boot DT.

So this series start by adding a new helper to the optee lib to add the optee firmware node with the provided parameters. This function is then used to add this functionality to the imx8m boards and to replace the custom code used on the imx8 boards.

Alban Bedel (3):
optee: Add an helper to add the optee firmware node in the FDT
imx8m: Automatically add the optee firmware node to the FDT
imx8: Use the optee lib to add the optee firmware node to the FDT

 arch/arm/mach-imx/imx8/fdt.c           | 59 ++++---------------------
 arch/arm/mach-imx/imx8m/soc.c          |  9 ++++
 configs/apalis-imx8_defconfig          |  1 +
 configs/cgtqmx8_defconfig              |  1 +
 configs/colibri-imx8x_defconfig        |  1 +
 configs/deneb_defconfig                |  1 +
 configs/giedi_defconfig                |  1 +
 configs/imx8qm_mek_defconfig           |  1 +
 configs/imx8qm_rom7720_a1_4G_defconfig |  1 +
 configs/imx8qxp_mek_defconfig          |  1 +
 include/tee/optee.h                    | 11 +++++
 lib/optee/optee.c                      | 60 +++++++++++++++++++-------
 12 files changed, 80 insertions(+), 67 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 optee: Add an helper to add the optee firmware node in the FDT

Some platforms can detect if an optee firmware is running and then manually add the firmware node to the FDT. Provide a common helper to avoid code duplication in the board code.

---
 include/tee/optee.h | 11 +++++++++
 lib/optee/optee.c   | 60 +++++++++++++++++++++++++++++++++------------
 2 files changed, 55 insertions(+), 16 deletions(-)
include/tee/optee.h +11 -0
index 5412bc7386ec..d9a316150522 100644
@@ -58,11 +58,22 @@ static inline int optee_verify_bootm_image(unsigned long image_addr,
 
 #if defined(CONFIG_OPTEE_LIB) && defined(CONFIG_OF_LIBFDT)
 int optee_copy_fdt_nodes(void *new_blob);
+
+int optee_add_firmware_node(void *fdt_blob,
+			    const char *compatible,
+			    const char *method);
 #else
 static inline int optee_copy_fdt_nodes(void *new_blob)
 {
 	return 0;
 }
+
+static inline int optee_add_firmware_node(void *fdt_blob,
+					  const char *compatible,
+					  const char *method)
+{
+	return 0;
+}
 #endif
 
 #endif /* _OPTEE_H */
lib/optee/optee.c +44 -16

87 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/3 imx8m: Automatically add the optee firmware node to the FDT

In reply to Alban Bedel

If optee is running add the firmware node to the FDT to allow the kernel to use a more generic device tree.

---
 arch/arm/mach-imx/imx8m/soc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/arm/mach-imx/imx8m/soc.c +9 -0
index 8e23e6da326f..2a78cb6a0952 100644
@@ -30,6 +30,7 @@
 #include <fsl_wdog.h>
 #include <imx_sip.h>
 #include <linux/bitops.h>
+#include <tee/optee.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -1347,6 +1348,14 @@ usb_modify_speed:
 #endif
 
 	cleanup_nodes_for_efi(blob);
+
+	if (rom_pointer[1]) {
+		int err = optee_add_firmware_node(blob, "linaro,optee-tz",
+						  "smc");
+		if (err)
+			return err;
+	}
+
 	return 0;
 }
 #endif
Signature · 1 line
2.34.1
#3 lore raw

Hi Alban

Quoted 27 lines · Alban Bedel

If optee is running add the firmware node to the FDT to allow the kernel to use a more generic device tree.

---
 arch/arm/mach-imx/imx8m/soc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/arm/mach-imx/imx8m/soc.c +3 -0
index 8e23e6da326f..2a78cb6a0952 100644
@@ -30,6 +30,7 @@
 #include <fsl_wdog.h>
 #include <imx_sip.h>
 #include <linux/bitops.h>
+#include <tee/optee.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -1347,6 +1348,14 @@ usb_modify_speed:
 #endif
 
        cleanup_nodes_for_efi(blob);
+
+       if (rom_pointer[1]) {

arch/arm/mach-imx/imx8m/soc.c usb_modify_speed:

Quoted 10 lines
arch/arm/mach-imx/imx8m/soc.c +6 -1
+               int err = optee_add_firmware_node(blob, "linaro,optee-tz",
+                                                 "smc");
+               if (err)
+                       return err;
+       }
+
        return 0;
 }
 #endif
--

Adding the node is not sufficient. I think that we need reserved memory node on top of it. The nice scenario will be
only to define the size of such area and use always the last part of the memory to reserve it. Do you know if we have some api to pass information to the atf and optee?

Michael

Quoted 1 line

2.34.1

Signature · 11 lines
Michael Nazzareno Trimarchi
Co-Founder & Chief Executive Officer
M. +39 347 913 2170
[email protected]
__________________________________

Amarula Solutions BV
Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
T. +31 (0)85 111 9172
[email protected]
www.amarulasolutions.com
#4 lore raw

Hi,

Quoted 39 lines · Michael Nazzareno Trimarchi

Hi Alban

Quoted 35 lines · Alban Bedel

If optee is running add the firmware node to the FDT to allow the kernel to use a more generic device tree.

---
 arch/arm/mach-imx/imx8m/soc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/arm/mach-imx/imx8m/soc.c +9 -0
index 8e23e6da326f..2a78cb6a0952 100644
@@ -30,6 +30,7 @@
 #include <fsl_wdog.h>
 #include <imx_sip.h>
 #include <linux/bitops.h>
+#include <tee/optee.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -1347,6 +1348,14 @@ usb_modify_speed:
 #endif
 
        cleanup_nodes_for_efi(blob);
+
+       if (rom_pointer[1]) {
+               int err = optee_add_firmware_node(blob, "linaro,optee-tz",
+                                                 "smc");
+               if (err)
+                       return err;
+       }
+
        return 0;
 }
Quoted 8 lines

arch/arm/mach-imx/imx8m/soc.c usb_modify_speed:

Quoted 2 lines
arch/arm/mach-imx/imx8m/soc.c +0 -1
 #endif
--

Adding the node is not sufficient. I think that we need reserved memory node on top of it. The nice scenario will be
only to define the size of such area and use always the last part of the memory to reserve it. Do you know if we have some api to pass information to the atf and optee?

We have an ABI in OP-TEE for this, OPTEE_SMC_GET_SHM_CONFIG. This function wasn't intended for this, but I can't see that that should be a problem.

Cheers,
Jens

Quoted 18 lines

Michael

Quoted 1 line

2.34.1

--
Michael Nazzareno Trimarchi
Co-Founder & Chief Executive Officer
M. +39 347 913 2170
[email protected]
__________________________________

Amarula Solutions BV
Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
T. +31 (0)85 111 9172
[email protected]
www.amarulasolutions.com

#5 lore raw

Hi Jens

Quoted 53 lines · Jens Wiklander

Hi,

Quoted 39 lines · Michael Nazzareno Trimarchi

Hi Alban

Quoted 35 lines · Alban Bedel

If optee is running add the firmware node to the FDT to allow the kernel to use a more generic device tree.

---
 arch/arm/mach-imx/imx8m/soc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/arm/mach-imx/imx8m/soc.c +9 -0
index 8e23e6da326f..2a78cb6a0952 100644
@@ -30,6 +30,7 @@
 #include <fsl_wdog.h>
 #include <imx_sip.h>
 #include <linux/bitops.h>
+#include <tee/optee.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -1347,6 +1348,14 @@ usb_modify_speed:
 #endif
 
        cleanup_nodes_for_efi(blob);
+
+       if (rom_pointer[1]) {
+               int err = optee_add_firmware_node(blob, "linaro,optee-tz",
+                                                 "smc");
+               if (err)
+                       return err;
+       }
+
        return 0;
 }
Quoted 8 lines

arch/arm/mach-imx/imx8m/soc.c usb_modify_speed:

Quoted 2 lines
arch/arm/mach-imx/imx8m/soc.c +0 -1
 #endif
--

Adding the node is not sufficient. I think that we need reserved memory node on top of it. The nice scenario will be
only to define the size of such area and use always the last part of the memory to reserve it. Do you know if we have some api to pass information to the atf and optee?

Quoted 3 lines

We have an ABI in OP-TEE for this, OPTEE_SMC_GET_SHM_CONFIG. This function wasn't intended for this, but I can't see that that should be a problem.

Ok, the call resolves to know the size of SHM_CONFIG but does not give information about the memory to be reserved.

So SPL loads ATF that has hardcoded BL32_ADDR and BL32_SIZE. SPL is running in OCRAM as ATF, so then I can call
the OPTEE_SMC_GET to know what reservation OPTEE needs. This avoid us to create dts entry for optee only

Michael

Quoted 22 lines

Cheers,
Jens

Quoted 18 lines

Michael

Quoted 1 line

2.34.1

--
Michael Nazzareno Trimarchi
Co-Founder & Chief Executive Officer
M. +39 347 913 2170
[email protected]
__________________________________

Amarula Solutions BV
Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
T. +31 (0)85 111 9172
[email protected]
www.amarulasolutions.com

--
Michael Nazzareno Trimarchi
Co-Founder & Chief Executive Officer
M. +39 347 913 2170
[email protected]
__________________________________

Amarula Solutions BV
Joop Geesinkweg 125, 1114 AB, Amsterdam, NL
T. +31 (0)85 111 9172
[email protected]
www.amarulasolutions.com

#6 lore raw
In reply to Michael Nazzareno Trimarchi
Quoted 38 lines · Michael Nazzareno Trimarchi

Hi Alban

Quoted 34 lines · Alban Bedel

If optee is running add the firmware node to the FDT to allow the kernel to use a more generic device tree.

---
 arch/arm/mach-imx/imx8m/soc.c | 9 +++++++++

 1 file changed, 9 insertions(+)
arch/arm/mach- renamed from arch/arm/mach-imx/imx8m/soc.c +0 -0

imx/imx8m/soc.c
index 8e23e6da326f..2a78cb6a0952 100644

arch/arm/mach-imx/imx8m/soc.c +8 -0
@@ -30,6 +30,7 @@
  #include <fsl_wdog.h>
  #include <imx_sip.h>
  #include <linux/bitops.h>
+#include <tee/optee.h>
 
  DECLARE_GLOBAL_DATA_PTR;
 
@@ -1347,6 +1348,14 @@ usb_modify_speed:
  #endif
 
         cleanup_nodes_for_efi(blob);
+
+       if (rom_pointer[1]) {
+               int err = optee_add_firmware_node(blob,
 "linaro,optee-tz",
+                                                 "smc");
+               if (err)
+                       return err;
+       }
Quoted 8 lines
Quoted 5 lines
arch/arm/mach-imx/imx8m/soc.c +1 -0
+
         return 0;
  }

 #endif
--

Adding the node is not sufficient. I think that we need reserved memory node on top of it.

My board use an imx8m and there the SoC code already carve out the optee memory (arch/arm/mach-imx/imx8m/soc.c dram_init_banksize()). In that case it is enough to just add the firmware node as the memory node created in the DTB already exclude the optee area. I expect it was done like this to also prevent using this memory area in u-boot.

This is a first step we can later add another function to also create the reserved memory nodes when needed.

Quoted 2 lines

The nice scenario will be only to define the size of such area and use always the last part of the memory to reserve it.

The optee area might be anywhere, the size won't be enough.

Alban

#7 lore raw

PATCH 3/3 imx8: Use the optee lib to add the optee firmware node to the FDT

In reply to Alban Bedel

Use the function provided by the optee lib to add the firmware node to the FDT. Enable the optee lib on all imx8 defconfig to keep the same behavior.

---
 arch/arm/mach-imx/imx8/fdt.c           | 59 ++++----------------------
 configs/apalis-imx8_defconfig          |  1 +
 configs/cgtqmx8_defconfig              |  1 +
 configs/colibri-imx8x_defconfig        |  1 +
 configs/deneb_defconfig                |  1 +
 configs/giedi_defconfig                |  1 +
 configs/imx8qm_mek_defconfig           |  1 +
 configs/imx8qm_rom7720_a1_4G_defconfig |  1 +
 configs/imx8qxp_mek_defconfig          |  1 +
 9 files changed, 16 insertions(+), 51 deletions(-)
arch/arm/mach-imx/imx8/fdt.c +8 -51
index a132ce2e6a3a..60d6a6bb85b8 100644
@@ -11,6 +11,7 @@
 #include <dm/ofnode.h>
 #include <fdt_support.h>
 #include <linux/libfdt.h>
+#include <tee/optee.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -229,56 +230,6 @@ static int config_smmu_fdt(void *blob)
 	return 0;
 }
 
-static int ft_add_optee_node(void *fdt, struct bd_info *bd)
-{
-	const char *path, *subpath;
-	int offs;
-
-	/*
-	 * No TEE space allocated indicating no TEE running, so no
-	 * need to add optee node in dts
-	 */
-	if (!boot_pointer[1])
-		return 0;
-
-	offs = fdt_increase_size(fdt, 512);
-	if (offs) {
-		printf("No Space for dtb\n");
-		return 1;
-	}
-
-	path = "/firmware";
-	offs = fdt_path_offset(fdt, path);
-	if (offs < 0) {
-		path = "/";
-		offs = fdt_path_offset(fdt, path);
-
-		if (offs < 0) {
-			printf("Could not find root node.\n");
-			return offs;
-		}
-
-		subpath = "firmware";
-		offs = fdt_add_subnode(fdt, offs, subpath);
-		if (offs < 0) {
-			printf("Could not create %s node.\n", subpath);
-			return offs;
-		}
-	}
-
-	subpath = "optee";
-	offs = fdt_add_subnode(fdt, offs, subpath);
-	if (offs < 0) {
-		printf("Could not create %s node.\n", subpath);
-		return offs;
-	}
-
-	fdt_setprop_string(fdt, offs, "compatible", "linaro,optee-tz");
-	fdt_setprop_string(fdt, offs, "method", "smc");
-
-	return 0;
-}
-
 int ft_system_setup(void *blob, struct bd_info *bd)
 {
 	int ret;
@@ -300,5 +251,11 @@ int ft_system_setup(void *blob, struct bd_info *bd)
 			return ret;
 	}
 
-	return ft_add_optee_node(blob, bd);
+	if (boot_pointer[1]) {
+		ret = optee_add_firmware_node(blob, "linaro,optee-tz", "smc");
+		if (ret)
+			return ret;
+	}
+
+	return 0;
 }
configs/apalis-imx8_defconfig +1 -0
index beb20f6e1c01..f3dbb4bd9c19 100644
@@ -72,3 +72,4 @@ CONFIG_FSL_LPUART=y
 CONFIG_DM_THERMAL=y
 CONFIG_IMX_SCU_THERMAL=y
 # CONFIG_EFI_LOADER is not set
+CONFIG_OPTEE_LIB=y
configs/cgtqmx8_defconfig +1 -0
index 2cf882f826ab..ac251f37fed5 100644
@@ -90,3 +90,4 @@ CONFIG_DM_SERIAL=y
 CONFIG_FSL_LPUART=y
 CONFIG_SPL_TINY_MEMSET=y
 # CONFIG_EFI_LOADER is not set
+CONFIG_OPTEE_LIB=y
configs/colibri-imx8x_defconfig +1 -0
index 0c9d6b64c1b6..f37da2411b04 100644
@@ -70,3 +70,4 @@ CONFIG_FSL_LPUART=y
 CONFIG_DM_THERMAL=y
 CONFIG_IMX_SCU_THERMAL=y
 # CONFIG_EFI_LOADER is not set
+CONFIG_OPTEE_LIB=y
configs/deneb_defconfig +1 -0
index 425fff6c70a6..e8f8cd18cacf 100644
@@ -114,3 +114,4 @@ CONFIG_IMX_SCU_THERMAL=y
 # CONFIG_SPL_WDT is not set
 CONFIG_SPL_TINY_MEMSET=y
 # CONFIG_EFI_LOADER is not set
+CONFIG_OPTEE_LIB=y
configs/giedi_defconfig +1 -0
index 4fbf7ebdcd95..7ee50e1f45c5 100644
@@ -114,3 +114,4 @@ CONFIG_IMX_SCU_THERMAL=y
 # CONFIG_SPL_WDT is not set
 CONFIG_SPL_TINY_MEMSET=y
 # CONFIG_EFI_LOADER is not set
+CONFIG_OPTEE_LIB=y
configs/imx8qm_mek_defconfig +1 -0
index 2e42872f843d..10b61d62a821 100644
@@ -91,3 +91,4 @@ CONFIG_DM_SERIAL=y
 CONFIG_FSL_LPUART=y
 CONFIG_SPL_TINY_MEMSET=y
 # CONFIG_EFI_LOADER is not set
+CONFIG_OPTEE_LIB=y
configs/imx8qm_rom7720_a1_4G_defconfig +1 -0
index d9997cfa8280..dc94fc67261c 100644
@@ -86,3 +86,4 @@ CONFIG_DM_SERIAL=y
 CONFIG_FSL_LPUART=y
 CONFIG_SPL_TINY_MEMSET=y
 # CONFIG_EFI_LOADER is not set
+CONFIG_OPTEE_LIB=y
configs/imx8qxp_mek_defconfig +1 -0
index 43f42f7a58af..250ce43758cf 100644
@@ -94,3 +94,4 @@ CONFIG_DM_THERMAL=y
 CONFIG_IMX_SCU_THERMAL=y
 CONFIG_SPL_TINY_MEMSET=y
 # CONFIG_EFI_LOADER is not set
+CONFIG_OPTEE_LIB=y
Signature · 1 line
2.34.1