Skip to contentSkip to conversation

ARM: i.MX8M: add optional CAAM init in PBL

PATCH Applied Ahmad Fatoum 2 participants 9 messages Jan 11, 2023

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

cover ARM: i.MX8M: add optional CAAM init in PBL

While the CAAM is TrustZone aware, Linux and OP-TEE drivers are not necessarily so:

- Linux running in normal world will attempt to set up RNG via DECO, which can be restricted to secure world

- (Some versions of) OP-TEE may depend on RNG being set up by BL2

While the proper solution would be to teach their drivers how to instantiate the RNG via SHs, we'll want to support existing firmware, so take the easy way out and just set up RNG4 SH0 and SH1 in barebox.

We already do that for the i.MX6, but the setup there happens in barebox proper. For security reasons, we want to install OP-TEE as early as possible while running the prebootloader, so we replicate the setup for PBL. This has been tested with the i.MX8MM and i.MX8MN. On the i.MX8MN in particular imx-optee used to hang for me while doing its crypto init, because it assumed a setup RNG. This is resolved now by this series.

Note that barebox itself does not yet benefit from this setup and that the barebox proper driver for CAAM is unaffected by this change. I verified it continues to work on an i.MX6Q as this series had some changes to the CAAM MMIO accessors.

Ahmad Fatoum (7):
ARM: i.MX8M: bootrom: access OCRAM directly if running in EL3
crypto: caam - sync 64-bit accessors with Linux
crypto: caam - add job ring accessors from Linux
crypto: caam - make command constants unsigned
crypto: caam - implement early PBL init
common: add new CONFIG_HAVE_OPTEE symbol
ARM: i.MX8M: init CAAM when CONFIG_FSL_CAAM_RNG_PBL_INIT

 arch/arm/mach-imx/Kconfig                   |   1 +
 arch/arm/mach-imx/Makefile                  |   4 +-
 arch/arm/mach-imx/atf.c                     |  10 +
 arch/arm/mach-imx/bootrom-cmd.c             |  14 +-
 arch/arm/mach-imx/include/mach/imx8m-regs.h |   3 +
 arch/arm/mach-imx/include/mach/romapi.h     |   4 +
 arch/arm/mach-imx/romapi.c                  |  24 +-
 common/Kconfig                              |   7 +-
 drivers/crypto/Makefile                     |   2 +-
 drivers/crypto/caam/Kconfig                 |   3 +
 drivers/crypto/caam/Makefile                |   1 +
 drivers/crypto/caam/ctrl.c                  |   6 +
 drivers/crypto/caam/desc.h                  |  43 +-
 drivers/crypto/caam/detect.h                |  19 +
 drivers/crypto/caam/pbl-init.c              | 491 ++++++++++++++++++++
 drivers/crypto/caam/regs.h                  | 272 ++++++++---
 include/soc/fsl/caam.h                      |  17 +
 include/zero_page.h                         |   2 +-
 18 files changed, 834 insertions(+), 89 deletions(-)
 create mode 100644 drivers/crypto/caam/detect.h
 create mode 100644 drivers/crypto/caam/pbl-init.c
 create mode 100644 include/soc/fsl/caam.h
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/7 ARM: i.MX8M: bootrom: access OCRAM directly if running in EL3

It's straight-forward to hack barebox PBL to skip ATF installation and to start barebox proper in EL3. This can be useful for debugging and may in future be just a Kconfig option like we now have with Rockchip.

In such a configuration, we don't need to copy the ROM log out of OCRAM, because there's no ATF installed there that might overwrite it. Therefore, just directly access the event log pointer in BootROM if running in EL3. As 0x9e0 is in the first zero page, we use the function in zero_page.h to temporarily disable traps in the zero page. We don't need to do that in PBL though as even early MMU will be enabled later.

---
 arch/arm/mach-imx/Makefile              |  4 ++--
 arch/arm/mach-imx/bootrom-cmd.c         | 14 ++++++++++++--
 arch/arm/mach-imx/include/mach/romapi.h |  4 ++++
 arch/arm/mach-imx/romapi.c              | 24 ++++++++++++++++--------
 include/zero_page.h                     |  2 +-
 5 files changed, 35 insertions(+), 13 deletions(-)
arch/arm/mach-imx/Makefile +2 -2
index cc834fed7be7..5d70e79b57f1 100644
@@ -18,8 +18,8 @@ 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
-lwl-$(CONFIG_ARCH_IMX8M) += atf.o romapi.o
-obj-pbl-$(CONFIG_ARCH_IMX8M) += tzasc.o
+lwl-$(CONFIG_ARCH_IMX8M) += atf.o
+obj-pbl-$(CONFIG_ARCH_IMX8M) += romapi.o tzasc.o
 obj-$(CONFIG_IMX_IIM)	+= iim.o
 obj-$(CONFIG_NAND_IMX) += nand.o
 lwl-$(CONFIG_ARCH_IMX_EXTERNAL_BOOT_NAND) += external-nand-boot.o
arch/arm/mach-imx/bootrom-cmd.c +12 -2
index 0238d09b169f..c891fd2e0640 100644
@@ -8,6 +8,7 @@
 #include <linux/bitfield.h>
 #include <mach/imx8m-regs.h>
 #include <mach/xload.h>
+#include <mach/romapi.h>
 #include <asm/barebox-arm.h>
 
 /* i.MX7 and later ID field is swapped compared to i.MX6 */
@@ -51,6 +52,9 @@ static int imx8m_bootrom_decode_log(const u32 *rom_log)
 {
 	int i;
 
+	if (!rom_log)
+		return -ENODATA;
+
 	for (i = 0; i < 128; i++) {
 		u8 event_id = FIELD_GET(ROM_EVENT_FORMAT_V1_ID, rom_log[i]);
 		u8 event_id_idx = FIELD_GET(ROM_EVENT_FORMAT_V1_ID_IDX, rom_log[i]);
@@ -178,11 +182,17 @@ static int imx8m_bootrom_decode_log(const u32 *rom_log)
 
 static int do_bootrom(int argc, char *argv[])
 {
-	const struct imx_scratch_space *scratch = arm_mem_scratch_get();
-	const u32 *rom_log_addr = scratch->bootrom_log;
+	const u32 *rom_log_addr;
 	bool log = false;
 	int ret, opt;
 
+	if (current_el() == 3) {
+		rom_log_addr = __imx8m_get_bootrom_log();
+	} else {
+		const struct imx_scratch_space *scratch = arm_mem_scratch_get();
+		rom_log_addr = scratch->bootrom_log;
+	}
+
 	while((opt = getopt(argc, argv, "la:")) > 0) {
 		switch(opt) {
 		case 'a':
arch/arm/mach-imx/include/mach/romapi.h +4 -0
index d22ba7259dd0..0aecfa7f5c15 100644
@@ -3,6 +3,7 @@
 #define __MACH_IMX_ROMAPI_H
 
 #include <mach/xload.h>
+#include <linux/types.h>
 
 struct rom_api {
 	u16 ver;
@@ -40,6 +41,9 @@ int imx8mn_bootrom_load_image(void);
 /* only call after DRAM has been configured */
 void imx8m_save_bootrom_log(void *dst);
 
+/* only usable in EL3 */
+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())
arch/arm/mach-imx/romapi.c +16 -8
index 5885fb698ecd..74cc0119afdb 100644
@@ -7,6 +7,7 @@
 #include <mach/romapi.h>
 #include <mach/atf.h>
 #include <mach/imx8m-regs.h>
+#include <zero_page.h>
 
 static int imx8m_bootrom_load(struct rom_api *rom_api, void *adr, size_t size)
 {
@@ -50,23 +51,30 @@ int imx8mn_bootrom_load_image(void)
 	return imx8mp_bootrom_load_image();
 }
 
-void imx8m_save_bootrom_log(void *dest)
+const u32 *__imx8m_get_bootrom_log(void)
 {
 	ulong rom_log_addr;
 
-	if (!IS_ENABLED(CONFIG_IMX_SAVE_BOOTROM_LOG)) {
-		pr_debug("skipping bootrom log saving\n");
-		return;
-	}
-
-	rom_log_addr = *(u32 *)0x9e0;
+	zero_page_access();
+	rom_log_addr = readl(IOMEM(0x9e0));
+	zero_page_faulting();
 
 	if (rom_log_addr < MX8M_OCRAM_BASE_ADDR ||
 	    rom_log_addr >= MX8M_OCRAM_BASE_ADDR + MX8M_OCRAM_MAX_SIZE ||
 	    rom_log_addr & 0x3) {
 		pr_warn("No BootROM log found at address 0x%08lx\n", rom_log_addr);
+		return NULL;
+	}
+
+	return (u32 *)rom_log_addr;
+}
+
+void imx8m_save_bootrom_log(void *dest)
+{
+	if (!IS_ENABLED(CONFIG_IMX_SAVE_BOOTROM_LOG)) {
+		pr_debug("skipping bootrom log saving\n");
 		return;
 	}
 
-	memcpy(dest, (u32 *)rom_log_addr, 128 * sizeof(u32));
+	memcpy(dest, __imx8m_get_bootrom_log(), 128 * sizeof(u32));
 }
include/zero_page.h +1 -1
index 519e65be7628..26a12246f155 100644
@@ -4,7 +4,7 @@
 
 #include <common.h>
 
-#if defined CONFIG_ARCH_HAS_ZERO_PAGE && defined CONFIG_MMU
+#if defined CONFIG_ARCH_HAS_ZERO_PAGE && defined CONFIG_MMU && !defined __PBL__
 
 /*
  * zero_page_faulting - fault when accessing the zero page
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/7 crypto: caam - sync 64-bit accessors with Linux

In reply to Ahmad Fatoum

The barebox CAAM driver only ever ran on i.MX6 (32-bit little-endian i.MX). There are also CAAM units on Layerscape and i.MX8. Prepare for their support by syncing the current 64-bit accessors from Linux.

In addition to the existing caam_little_end, these can also consult the caam_imx and caam_ptr_sz globals. They are for now initialized to maintain i.MX6 compatibility, but may be initialized dynamically in future.

---
 drivers/crypto/caam/ctrl.c |   6 ++
 drivers/crypto/caam/regs.h | 192 +++++++++++++++++++++++++++----------
 2 files changed, 146 insertions(+), 52 deletions(-)
drivers/crypto/caam/ctrl.c +6 -0
index f78940e1ac91..2e44f60c7f86 100644
@@ -26,6 +26,12 @@
 bool caam_little_end;
 EXPORT_SYMBOL(caam_little_end);
 
+bool caam_imx = true;
+EXPORT_SYMBOL(caam_imx);
+
+size_t caam_ptr_sz = 4;
+EXPORT_SYMBOL(caam_ptr_sz);
+
 /*
  * Descriptor to instantiate RNG State Handle 0 in normal mode and
  * load the JDKEK, TDKEK and TDSK registers
drivers/crypto/caam/regs.h +140 -52

240 lines not on this page · raw message

Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/7 crypto: caam - add job ring accessors from Linux

In reply to Ahmad Fatoum

Job ring entries differ in size depending on caam_ptr_sz. Fortunately, an entry is at most two elements big, so we can just provide accessors.

Import them from Linux.

---
 drivers/crypto/caam/regs.h | 60 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
drivers/crypto/caam/regs.h +60 -0
index 5c4b783e80fe..76ccb3006a2c 100644
@@ -221,6 +221,66 @@ struct jr_outentry {
 	u32 jrstatus;	/* Status for completed descriptor */
 } __packed;
 
+static inline void jr_outentry_get(void *outring, int hw_idx, dma_addr_t *desc,
+				   u32 *jrstatus)
+{
+
+	if (caam_ptr_sz == sizeof(u32)) {
+		struct {
+			u32 desc;
+			u32 jrstatus;
+		} __packed *outentry = outring;
+
+		*desc = outentry[hw_idx].desc;
+		*jrstatus = outentry[hw_idx].jrstatus;
+	} else {
+		struct {
+			dma_addr_t desc;/* Pointer to completed descriptor */
+			u32 jrstatus;	/* Status for completed descriptor */
+		} __packed *outentry = outring;
+
+		*desc = outentry[hw_idx].desc;
+		*jrstatus = outentry[hw_idx].jrstatus;
+	}
+}
+
+#define SIZEOF_JR_OUTENTRY	(caam_ptr_sz + sizeof(u32))
+
+static inline dma_addr_t jr_outentry_desc(void *outring, int hw_idx)
+{
+	dma_addr_t desc;
+	u32 unused;
+
+	jr_outentry_get(outring, hw_idx, &desc, &unused);
+
+	return desc;
+}
+
+static inline u32 jr_outentry_jrstatus(void *outring, int hw_idx)
+{
+	dma_addr_t unused;
+	u32 jrstatus;
+
+	jr_outentry_get(outring, hw_idx, &unused, &jrstatus);
+
+	return jrstatus;
+}
+
+static inline void jr_inpentry_set(void *inpring, int hw_idx, dma_addr_t val)
+{
+	if (caam_ptr_sz == sizeof(u32)) {
+		u32 *inpentry = inpring;
+
+		inpentry[hw_idx] = val;
+	} else {
+		dma_addr_t *inpentry = inpring;
+
+		inpentry[hw_idx] = val;
+	}
+}
+
+#define SIZEOF_JR_INPENTRY	caam_ptr_sz
+
 /*
  * CHA version ID / instantiation bitfields
  * Defined for use within cha_id in perfmon
Signature · 1 line
2.30.2
#4 lore raw

PATCH 4/7 crypto: caam - make command constants unsigned

In reply to Ahmad Fatoum

CMD_OPERATION is 0x10 << 27, which shifts a one into the sign bit. Make the constants unsigned to fix this.

---
 drivers/crypto/caam/desc.h | 40 +++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 20 deletions(-)
drivers/crypto/caam/desc.h +20 -20
index a7966a878178..523f2d15f0bd 100644
@@ -36,26 +36,26 @@
 #define CMD_SHIFT		27
 #define CMD_MASK		0xf8000000
 
-#define CMD_KEY			(0x00 << CMD_SHIFT)
-#define CMD_SEQ_KEY		(0x01 << CMD_SHIFT)
-#define CMD_LOAD		(0x02 << CMD_SHIFT)
-#define CMD_SEQ_LOAD		(0x03 << CMD_SHIFT)
-#define CMD_FIFO_LOAD		(0x04 << CMD_SHIFT)
-#define CMD_SEQ_FIFO_LOAD	(0x05 << CMD_SHIFT)
-#define CMD_STORE		(0x0a << CMD_SHIFT)
-#define CMD_SEQ_STORE		(0x0b << CMD_SHIFT)
-#define CMD_FIFO_STORE		(0x0c << CMD_SHIFT)
-#define CMD_SEQ_FIFO_STORE	(0x0d << CMD_SHIFT)
-#define CMD_MOVE_LEN		(0x0e << CMD_SHIFT)
-#define CMD_MOVE		(0x0f << CMD_SHIFT)
-#define CMD_OPERATION		(0x10 << CMD_SHIFT)
-#define CMD_SIGNATURE		(0x12 << CMD_SHIFT)
-#define CMD_JUMP		(0x14 << CMD_SHIFT)
-#define CMD_MATH		(0x15 << CMD_SHIFT)
-#define CMD_DESC_HDR		(0x16 << CMD_SHIFT)
-#define CMD_SHARED_DESC_HDR	(0x17 << CMD_SHIFT)
-#define CMD_SEQ_IN_PTR		(0x1e << CMD_SHIFT)
-#define CMD_SEQ_OUT_PTR		(0x1f << CMD_SHIFT)
+#define CMD_KEY			(0x00u << CMD_SHIFT)
+#define CMD_SEQ_KEY		(0x01u << CMD_SHIFT)
+#define CMD_LOAD		(0x02u << CMD_SHIFT)
+#define CMD_SEQ_LOAD		(0x03u << CMD_SHIFT)
+#define CMD_FIFO_LOAD		(0x04u << CMD_SHIFT)
+#define CMD_SEQ_FIFO_LOAD	(0x05u << CMD_SHIFT)
+#define CMD_STORE		(0x0au << CMD_SHIFT)
+#define CMD_SEQ_STORE		(0x0bu << CMD_SHIFT)
+#define CMD_FIFO_STORE		(0x0cu << CMD_SHIFT)
+#define CMD_SEQ_FIFO_STORE	(0x0du << CMD_SHIFT)
+#define CMD_MOVE_LEN		(0x0eu << CMD_SHIFT)
+#define CMD_MOVE		(0x0fu << CMD_SHIFT)
+#define CMD_OPERATION		(0x10u << CMD_SHIFT)
+#define CMD_SIGNATURE		(0x12u << CMD_SHIFT)
+#define CMD_JUMP		(0x14u << CMD_SHIFT)
+#define CMD_MATH		(0x15u << CMD_SHIFT)
+#define CMD_DESC_HDR		(0x16u << CMD_SHIFT)
+#define CMD_SHARED_DESC_HDR	(0x17u << CMD_SHIFT)
+#define CMD_SEQ_IN_PTR		(0x1eu << CMD_SHIFT)
+#define CMD_SEQ_OUT_PTR		(0x1fu << CMD_SHIFT)
 
 /* General-purpose class selector for all commands */
 #define CLASS_SHIFT		25
Signature · 1 line
2.30.2
#5 lore raw

PATCH 5/7 crypto: caam - implement early PBL init

In reply to Ahmad Fatoum

While the CAAM is TrustZone aware, Linux and OP-TEE drivers are not necessarily so:

- Linux running in normal world will attempt to set up RNG via DECO, which can be restricted to secure world

- OP-TEE may depend on RNG being set up by BL2

While the proper solution would be to teach their drivers how to instantiate the RNG via SHs, we'll want to support existing firmware, so take the easy way out and just set up RNG4 SH0 and SH1 in barebox.

We already do that for the i.MX6, but the setup there happens in barebox proper. For security reasons, we want to install OP-TEE as early as possible while running the prebootloader, so we replicate the setup for PBL. This has been tested with the i.MX8MM and i.MX8MN.

Note that barebox itself does not yet benefit from this setup and that the barebox proper driver for CAAM is unaffected by this change.

---
 drivers/crypto/Makefile        |   2 +-
 drivers/crypto/caam/Kconfig    |   2 +
 drivers/crypto/caam/Makefile   |   1 +
 drivers/crypto/caam/desc.h     |   3 +
 drivers/crypto/caam/detect.h   |  19 ++
 drivers/crypto/caam/pbl-init.c | 491 +++++++++++++++++++++++++++++++++
 drivers/crypto/caam/regs.h     |  20 +-
 include/soc/fsl/caam.h         |  17 ++
 8 files changed, 552 insertions(+), 3 deletions(-)
 create mode 100644 drivers/crypto/caam/detect.h
 create mode 100644 drivers/crypto/caam/pbl-init.c
 create mode 100644 include/soc/fsl/caam.h
drivers/crypto/Makefile +1 -1
index 475dcf2defcb..8b600b8d40f7 100644
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
-obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM) += caam/
+obj-y += caam/
 obj-$(CONFIG_CRYPTO_DEV_MXC_SCC) += imx-scc/
drivers/crypto/caam/Kconfig +2 -0
index 0d2554e58679..33d7b0fb8329 100644
@@ -35,3 +35,5 @@ config CRYPTO_DEV_FSL_CAAM_RNG
 	help
 	  Selecting this will register the SEC4 hardware rng.
 
+config FSL_CAAM_RNG_PBL_INIT
+	bool
drivers/crypto/caam/Makefile +1 -0
index 2d5079b4a52b..5ab7892d953d 100644
@@ -6,3 +6,4 @@ obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM) += ctrl.o error.o jr.o
 obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM_RNG) += caamrng.o
 obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM) += rng_self_test.o
 obj-$(CONFIG_BLOBGEN) += caam-blobgen.o
+pbl-$(CONFIG_FSL_CAAM_RNG_PBL_INIT) += pbl-init.o
drivers/crypto/caam/desc.h +3 -0
index 523f2d15f0bd..1e68bc4f0b54 100644
@@ -1182,6 +1182,7 @@
 /* RNG4 AAI set */
 #define OP_ALG_AAI_RNG4_SH_0	(0x00 << OP_ALG_AAI_SHIFT)
 #define OP_ALG_AAI_RNG4_SH_1	(0x01 << OP_ALG_AAI_SHIFT)
+#define OP_ALG_AAI_RNG4_SH_MASK	(0x03 << OP_ALG_AAI_SHIFT)
 #define OP_ALG_AAI_RNG4_PS	(0x40 << OP_ALG_AAI_SHIFT)
 #define OP_ALG_AAI_RNG4_AI	(0x80 << OP_ALG_AAI_SHIFT)
 #define OP_ALG_AAI_RNG4_SK	(0x100 << OP_ALG_AAI_SHIFT)
@@ -1218,6 +1219,8 @@
 #define OP_ALG_ICV_OFF		(0 << OP_ALG_ICV_SHIFT)
 #define OP_ALG_ICV_ON		(1 << OP_ALG_ICV_SHIFT)
 
+#define OP_ALG_PR_ON		BIT(1)
+
 #define OP_ALG_DIR_SHIFT	0
 #define OP_ALG_DIR_MASK		1
 #define OP_ALG_DECRYPT		0
drivers/crypto/caam/detect.h new file +19 -0
new file mode 100644
index 000000000000..f621ce91e9c3
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+//
+#ifndef __CAAM_DETECT_H__
+#define __CAAM_DETECT_H__
+
+#include "regs.h"
+
+static inline int caam_is_64bit(struct caam_ctrl __iomem *ctrl)
+{
+	return	(rd_reg32(&ctrl->perfmon.comp_parms_ms) & CTPR_MS_PS) &&
+		(rd_reg32(&ctrl->mcr) & MCFGR_LONG_PTR);
+}
+
+static inline bool caam_is_big_endian(struct caam_ctrl *ctrl)
+{
+	return rd_reg32(&ctrl->perfmon.status) & (CSTA_PLEND | CSTA_ALT_PLEND);
+}
+
+#endif
drivers/crypto/caam/pbl-init.c new file +491 -0

491 lines not on this page · raw message

drivers/crypto/caam/regs.h +18 -2
index 76ccb3006a2c..c2eea8d1a587 100644
@@ -431,6 +431,7 @@ struct caam_perfmon {
 #define CRNR_LS_RNGRN_SHIFT	16
 #define CRNR_LS_RNGRN_MASK	(0xfull << CRNR_LS_RNGRN_SHIFT)
 	u32 cha_rev_ls;		/* CRNR - CHA Rev No. Least significant half*/
+#define CTPR_MS_PS		BIT(17)
 #define CTPR_MS_QI_SHIFT	25
 #define CTPR_MS_QI_MASK		(0x1ull << CTPR_MS_QI_SHIFT)
 #define CTPR_MS_VIRT_EN_INCL	0x00000001
@@ -582,7 +583,10 @@ struct rngtst {
 
 /* RNG4 TRNG test registers */
 struct rng4tst {
-#define RTMCTL_PRGM	0x00010000	/* 1 -> program mode, 0 -> run mode */
+#define RTMCTL_ACC		BIT(5)  /* TRNG access mode */
+#define RTMCTL_FCT_FAIL		BIT(8)
+#define RTMCTL_ERR		BIT(12)
+#define RTMCTL_PRGM		BIT(16) /* 1 -> program mode, 0 -> run mode */
 #define RTMCTL_SAMP_MODE_VON_NEUMANN_ES_SC	0 /* use von Neumann data in
 						     both entropy shifter and
 						     statistical checker */
@@ -593,6 +597,7 @@ struct rng4tst {
 						     entropy shifter, raw data
 						     in statistical checker */
 #define RTMCTL_SAMP_MODE_INVALID		3 /* invalid combination */
+#define RTMCTL_SAMP_MODE_MASK			3
 	u32 rtmctl;		/* misc. control register */
 	u32 rtscmisc;		/* statistical check misc. register */
 	u32 rtpkrrng;		/* poker range register */
@@ -615,12 +620,23 @@ struct rng4tst {
 		u32 rtfrqmax;	/* PRGM=1: freq. count max. limit register */
 		u32 rtfrqcnt;	/* PRGM=0: freq. count register */
 	};
-	u32 rsvd1[40];
+	u32 rtscml;
+	u32 rtscr1l;
+	u32 rtscr2l;
+	u32 rtscr3l;
+	u32 rtscr4l;
+	u32 rtscr5l;
+	u32 rtscr6pl;
+	u32 rtstatus;
+	u32 rsvd1[32];
 #define RDSTA_SKVT 0x80000000
 #define RDSTA_SKVN 0x40000000
+#define RDSTA_PR0 BIT(4)
+#define RDSTA_PR1 BIT(5)
 #define RDSTA_IF0 0x00000001
 #define RDSTA_IF1 0x00000002
 #define RDSTA_IFMASK (RDSTA_IF1 | RDSTA_IF0)
+#define RDSTA_MASK (RDSTA_PR1 | RDSTA_PR0 | RDSTA_IF1 | RDSTA_IF0)
 	u32 rdsta;
 	u32 rsvd2[15];
 };
include/soc/fsl/caam.h new file +17 -0
new file mode 100644
index 000000000000..a919a114e86f
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: BSD-3-Clause
+#ifndef __SOC_FSL_CAAM_H_
+#define __SOC_FSL_CAAM_H_
+
+#include <linux/compiler.h>
+#include <linux/types.h>
+
+struct caam_ctrl;
+
+int early_caam_init(struct caam_ctrl __iomem *caam, bool is_imx);
+
+static inline int imx_early_caam_init(struct caam_ctrl __iomem *caam)
+{
+	return early_caam_init(caam, true);
+}
+
+#endif
Signature · 1 line
2.30.2
#6 lore raw

PATCH 6/7 common: add new CONFIG_HAVE_OPTEE symbol

In reply to Ahmad Fatoum

We have two methods of loading a trusted OS: The legacy, but generic, way of doing it in barebox proper during bootm and the new way of doing it in prebootloader to reduce attack surface as much as possible by running barebox proper completely in normal world.

Add a new CONFIG_HAVE_OPTEE symbol that is selected in both cases, so code can use just that instead of checking for both.

---
 common/Kconfig | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
common/Kconfig +6 -1
index 46675dfe2ea7..9e175d36c43e 100644
@@ -1119,11 +1119,14 @@ config EXTERNAL_DTS_FRAGMENTS
 
 menu "OP-TEE loading"
 
+config HAVE_OPTEE
+	bool
+
 config OPTEE_SIZE
 	hex
 	default 0x02000000
 	prompt "OP-TEE Memory Size"
-	depends on BOOTM_OPTEE || PBL_OPTEE
+	depends on HAVE_OPTEE
 	help
 	  Size to reserve in main memory for OP-TEE.
 	  Can be smaller than the actual size used by OP-TEE, this is used to prevent
@@ -1133,6 +1136,7 @@ config BOOTM_OPTEE
 	bool
 	prompt "support booting OP-TEE"
 	depends on BOOTM && ARM
+	select HAVE_OPTEE
 	help
 	  OP-TEE is a trusted execution environment (TEE). With this option
 	  enabled barebox supports starting optee_os as part of the bootm command.
@@ -1144,6 +1148,7 @@ config PBL_OPTEE
 	bool "Enable OP-TEE early start"
 	depends on ARM
 	depends on !THUMB2_BAREBOX
+	select HAVE_OPTEE
 	help
 	  Allows starting OP-TEE during lowlevel initialization of the PBL.
 	  Requires explicit support in the board's lowlevel file.
Signature · 1 line
2.30.2
#7 lore raw

PATCH 7/7 ARM: i.MX8M: init CAAM when CONFIG_FSL_CAAM_RNG_PBL_INIT

In reply to Ahmad Fatoum
---
 arch/arm/mach-imx/Kconfig                   |  1 +
 arch/arm/mach-imx/atf.c                     | 10 ++++++++++
 arch/arm/mach-imx/include/mach/imx8m-regs.h |  3 +++
 drivers/crypto/caam/Kconfig                 |  3 ++-
 4 files changed, 16 insertions(+), 1 deletion(-)
arch/arm/mach-imx/Kconfig +1 -0
index 84b763f83fb1..774c4cacb7f7 100644
@@ -184,6 +184,7 @@ config ARCH_IMX8M
 	select IMX8M_DRAM
 	select PBL_VERIFY_PIGGY if HABV4
 	select ARM_USE_COMPRESSED_DTB
+	imply FSL_CAAM_RNG_PBL_INIT if HAVE_OPTEE
 
 config ARCH_IMX8MM
 	select ARCH_IMX8M
arch/arm/mach-imx/atf.c +10 -0
index 61b602816623..2a3e3f53b885 100644
@@ -8,6 +8,7 @@
 #include <mach/xload.h>
 #include <mach/romapi.h>
 #include <soc/fsl/fsl_udc.h>
+#include <soc/fsl/caam.h>
 
 /**
  * imx8m_atf_load_bl31 - Load ATF BL31 blob and transfer control to it
@@ -38,9 +39,18 @@
 static __noreturn void imx8m_atf_start_bl31(const void *fw, size_t fw_size, void *atf_dest)
 {
 	void __noreturn (*bl31)(void) = atf_dest;
+	int ret;
 
 	BUG_ON(fw_size > MX8M_ATF_BL31_SIZE_LIMIT);
 
+	if (IS_ENABLED(CONFIG_FSL_CAAM_RNG_PBL_INIT)) {
+		ret = imx_early_caam_init(MX8M_CAAM_BASE_ADDR);
+		if (ret)
+			pr_debug("CAAM early init failed: %d\n", ret);
+		else
+			pr_debug("CAAM early init successful\n");
+	}
+
 	memcpy(bl31, fw, fw_size);
 
 	asm volatile("msr sp_el2, %0" : :
arch/arm/mach-imx/include/mach/imx8m-regs.h +3 -0
index 794e1bdd88a4..d101b88cc4a6 100644
@@ -3,6 +3,8 @@
 #ifndef __MACH_IMX8M_REGS_H
 #define __MACH_IMX8M_REGS_H
 
+#include <linux/compiler.h>
+
 /*
  * Actual addressable OCRAM size may differ from SoC to SoC, but all of
  * i.MX8MQ/M/N/P have this region of MMIO address space set aside for
@@ -31,6 +33,7 @@
 #define MX8M_UART1_BASE_ADDR		0x30860000
 #define MX8M_UART3_BASE_ADDR		0x30880000
 #define MX8M_UART2_BASE_ADDR		0x30890000
+#define MX8M_CAAM_BASE_ADDR		IOMEM(0x30900000)
 #define MX8M_I2C1_BASE_ADDR		0x30A20000
 #define MX8M_I2C2_BASE_ADDR		0x30A30000
 #define MX8M_I2C3_BASE_ADDR		0x30A40000
drivers/crypto/caam/Kconfig +2 -1
index 33d7b0fb8329..e7f57708f3ea 100644
@@ -36,4 +36,5 @@ config CRYPTO_DEV_FSL_CAAM_RNG
 	  Selecting this will register the SEC4 hardware rng.
 
 config FSL_CAAM_RNG_PBL_INIT
-	bool
+	bool "Setup CAAM in EL3"
+	depends on ARCH_IMX8M
Signature · 1 line
2.30.2
Applied
#8 lore raw
In reply to Ahmad Fatoum
Quoted 24 lines · Ahmad Fatoum

While the CAAM is TrustZone aware, Linux and OP-TEE drivers are not necessarily so:

- Linux running in normal world will attempt to set up RNG via DECO, which can be restricted to secure world

- (Some versions of) OP-TEE may depend on RNG being set up by BL2

While the proper solution would be to teach their drivers how to instantiate the RNG via SHs, we'll want to support existing firmware, so take the easy way out and just set up RNG4 SH0 and SH1 in barebox.

We already do that for the i.MX6, but the setup there happens in barebox proper. For security reasons, we want to install OP-TEE as early as possible while running the prebootloader, so we replicate the setup for PBL. This has been tested with the i.MX8MM and i.MX8MN. On the i.MX8MN in particular imx-optee used to hang for me while doing its crypto init, because it assumed a setup RNG. This is resolved now by this series.

Note that barebox itself does not yet benefit from this setup and that the barebox proper driver for CAAM is unaffected by this change. I verified it continues to work on an i.MX6Q as this series had some changes to the CAAM MMIO accessors.

Quoted 8 lines

Ahmad Fatoum (7):
ARM: i.MX8M: bootrom: access OCRAM directly if running in EL3
crypto: caam - sync 64-bit accessors with Linux
crypto: caam - add job ring accessors from Linux
crypto: caam - make command constants unsigned
crypto: caam - implement early PBL init
common: add new CONFIG_HAVE_OPTEE symbol
ARM: i.MX8M: init CAAM when CONFIG_FSL_CAAM_RNG_PBL_INIT

Applied, thanks

Sascha

Quoted 29 lines
 arch/arm/mach-imx/Kconfig                   |   1 +
 arch/arm/mach-imx/Makefile                  |   4 +-
 arch/arm/mach-imx/atf.c                     |  10 +
 arch/arm/mach-imx/bootrom-cmd.c             |  14 +-
 arch/arm/mach-imx/include/mach/imx8m-regs.h |   3 +
 arch/arm/mach-imx/include/mach/romapi.h     |   4 +
 arch/arm/mach-imx/romapi.c                  |  24 +-
 common/Kconfig                              |   7 +-
 drivers/crypto/Makefile                     |   2 +-
 drivers/crypto/caam/Kconfig                 |   3 +
 drivers/crypto/caam/Makefile                |   1 +
 drivers/crypto/caam/ctrl.c                  |   6 +
 drivers/crypto/caam/desc.h                  |  43 +-
 drivers/crypto/caam/detect.h                |  19 +
 drivers/crypto/caam/pbl-init.c              | 491 ++++++++++++++++++++
 drivers/crypto/caam/regs.h                  | 272 ++++++++---
 include/soc/fsl/caam.h                      |  17 +
 include/zero_page.h                         |   2 +-
 18 files changed, 834 insertions(+), 89 deletions(-)
 create mode 100644 drivers/crypto/caam/detect.h
 create mode 100644 drivers/crypto/caam/pbl-init.c
 create mode 100644 include/soc/fsl/caam.h
Signature · 1 line
2.30.2
Signature · 4 lines
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 |