Skip to contentSkip to conversation

RISC-V DT related fixes for reserved memory & UEFI

PATCH RESEND Superseded Atish Patra 3 participants 12 messages Apr 18, 2020 – Apr 19, 2020

Subsystems (3)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover RISC-V DT related fixes for reserved memory & UEFI

This series adds few DT related fixes required for Linux EFI stub to work on RISC-V.

Patch 1 adds the boot hartid property under /chosen node. The related discussion can be found here.

https://patchwork.ozlabs.org/patch/1233664/
https://lists.denx.de/pipermail/u-boot/2020-March/402085.html

Patch 2 fixes a generic issue in fdtdec related to reserved memory node.

Patch 3,4,5 provide one of the option to update reserved-memory node for next stage. It depends on master OpenSBI branch.

The other options are SBI extension and trap/emulate on PMP csr access. The detaild discussion can be found here.
https://github.com/riscv/riscv-sbi-doc/pull/37

Patch 1 & 2 can be applied independently from 3 and 4. I want to keep all the patches together to provide a holistic view of changes required for RISC-V UEFI.

Changes v4->v5:
1. Added comments for new functions.

Changes v3->v4:
1. Dropped generic efi fix patch as it is already merged.
2. Moved all the fdt fixups to a common file.
3. Addressed few nit comments.

Changes from v2->v3:
1. Update the DT meant for OS if it is different from the one used by U-Boot
2. Use different FDT api to obtain "reg" address & size to honor the cell count.

Changes from v1->v2:
1. Fix the issue if chosen node is not present.

Changes from previous version:
1. Renamed the DT node property to "boot-hartid" from "efi-boot-hartid".
2. Changed the property type to u32 instead of u64 for RV32 compatibility.

Atish Patra (6):
riscv: Add boot hartid to Device tree
fdtdec: Fix boundary check
riscv: Provide a mechanism to fix DT for reserved memory
riscv: Setup reserved-memory node for FU540
riscv: Copy the reserved-memory nodes to final DT
riscv: Move all fdt fixups together

arch/riscv/cpu/start.S                |   1 +
arch/riscv/include/asm/global_data.h  |   1 +
arch/riscv/include/asm/u-boot-riscv.h |   2 +
arch/riscv/lib/Makefile               |   1 +
arch/riscv/lib/asm-offsets.c          |   1 +
arch/riscv/lib/bootm.c                |   5 -
arch/riscv/lib/fdt_fixup.c            | 150 ++++++++++++++++++++++++++
configs/sifive_fu540_defconfig        |   1 +
lib/fdtdec.c                          |   3 +-
9 files changed, 159 insertions(+), 6 deletions(-)

create mode 100644 arch/riscv/lib/fdt_fixup.c

--
2.25.1

#1 lore raw

PATCH RESEND v5 1/6 riscv: Add boot hartid to Device tree

Linux booting protocol mandates that register "a0" contains the hartid. However, U-boot can not pass the hartid via a0 during via standard UEFI protocol. DT nodes are commonly used to pass such information to the OS.

Add a DT node under chosen node to indicate the boot hartid. EFI stub in Linux kernel will parse this node and pass it to the real kernel in "a0" before jumping to it.

---
 arch/riscv/lib/bootm.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
arch/riscv/lib/bootm.c +22 -0
index fad16901c5f2..87cadad5016d 100644
@@ -28,6 +28,28 @@ __weak void board_quiesce_devices(void)
 
 int arch_fixup_fdt(void *blob)
 {
+#ifdef CONFIG_EFI_LOADER
+	int err;
+	u32 size;
+	int chosen_offset;
+
+	size = fdt_totalsize(blob);
+	err  = fdt_open_into(blob, blob, size + 32);
+	if (err < 0) {
+		printf("Device Tree can't be expanded to accommodate new node");
+		return err;
+	}
+	chosen_offset = fdt_path_offset(blob, "/chosen");
+	if (chosen_offset < 0) {
+		err = fdt_add_subnode(blob, 0, "chosen");
+		if (err < 0) {
+			printf("chosen node can not be added\n");
+			return err;
+		}
+	}
+	/* Overwrite the boot-hartid as U-Boot is the last stage BL */
+	fdt_setprop_u32(blob, chosen_offset, "boot-hartid", gd->arch.boot_hart);
+#endif
 	return 0;
 }
 
Signature · 1 line
2.25.1
#2 lore raw

PATCH RESEND v5 1/6 riscv: Add boot hartid to Device tree

Quoted 1 line · Atish Patra

nits: Device => device in the commit title

Quoted 2 lines

Linux booting protocol mandates that register "a0" contains the hartid. However, U-boot can not pass the hartid via a0 during via standard UEFI

nits: U-boot => U-Boot

Quoted 6 lines

protocol. DT nodes are commonly used to pass such information to the OS.

Add a DT node under chosen node to indicate the boot hartid. EFI stub in Linux kernel will parse this node and pass it to the real kernel in "a0" before jumping to it.

Quoted 7 lines
---
 arch/riscv/lib/bootm.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
#3 lore raw

PATCH RESEND v5 1/6 riscv: Add boot hartid to Device tree

In reply to Atish Patra
Quoted 2 lines · Atish Patra

Linux booting protocol mandates that register "a0" contains the hartid. However, U-boot can not pass the hartid via a0 during via standard UEFI

s/during via/during/

Andreas.

Signature · 3 lines
Andreas Schwab, schwab at linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
#4 lore raw

PATCH RESEND v5 2/6 fdtdec: Fix boundary check

In reply to Atish Patra

In U-Boot, the reserved memory end address is considered as a inclusive address. This notion is followed while adding a reserved memory node to the DT.

For example:
end_address = start_address + size - 1

Follow the same notion and fix the end address computation while checking for existing nodes.

---
 lib/fdtdec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/fdtdec.c +2 -1
index eb11fc898e30..07ba9f5c97e9 100644
@@ -1311,7 +1311,8 @@ int fdtdec_add_reserved_memory(void *blob, const char *basename,
 			continue;
 		}
 
-		if (addr == carveout->start && (addr + size) == carveout->end) {
+		if (addr == carveout->start && (addr + size - 1) ==
+						carveout->end) {
 			if (phandlep)
 				*phandlep = fdt_get_phandle(blob, node);
 			return 0;
Signature · 1 line
2.25.1
#5 lore raw

PATCH RESEND v5 2/6 fdtdec: Fix boundary check

Quoted 8 lines · Atish Patra

In U-Boot, the reserved memory end address is considered as a inclusive address. This notion is followed while adding a reserved memory node to the DT.

For example:
end_address = start_address + size - 1

Quoted 8 lines

Follow the same notion and fix the end address computation while checking for existing nodes.

---
 lib/fdtdec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
#6 lore raw

PATCH RESEND v5 3/6 riscv: Provide a mechanism to fix DT for reserved memory

In reply to Atish Patra

In RISC-V, M-mode software can reserve physical memory regions by setting appropriate physical memory protection (PMP) csr. As the PMP csr are accessible only in M-mode, S-mode U-Boot can not read this configuration directly. However, M-mode software can pass this information via reserved-memory node in device tree so that S-mode software can access this information.

This patch provides a framework to copy to the reserved-memory node from one DT to another. This will be used to update the DT used by U-Boot and the DT passed to the next stage OS.

---
 arch/riscv/cpu/start.S                |   1 +
 arch/riscv/include/asm/global_data.h  |   1 +
 arch/riscv/include/asm/u-boot-riscv.h |   2 +
 arch/riscv/lib/Makefile               |   1 +
 arch/riscv/lib/asm-offsets.c          |   1 +
 arch/riscv/lib/fdt_fixup.c            | 102 ++++++++++++++++++++++++++
 6 files changed, 108 insertions(+)
 create mode 100644 arch/riscv/lib/fdt_fixup.c
arch/riscv/cpu/start.S +1 -0
index 6b3ff99c3882..0282685c2906 100644
@@ -121,6 +121,7 @@ call_board_init_f_0:
 
 	jal	board_init_f_init_reserve
 
+	SREG	s1, GD_FIRMWARE_FDT_ADDR(gp)
 	/* save the boot hart id to global_data */
 	SREG	tp, GD_BOOT_HART(gp)
 
arch/riscv/include/asm/global_data.h +1 -0
index b74bd7e738bb..51ac8d1c98e2 100644
@@ -15,6 +15,7 @@
 /* Architecture-specific global data */
 struct arch_global_data {
 	long boot_hart;		/* boot hart id */
+	phys_addr_t firmware_fdt_addr;
 #ifdef CONFIG_SIFIVE_CLINT
 	void __iomem *clint;	/* clint base address */
 #endif
arch/riscv/include/asm/u-boot-riscv.h +2 -0
index 49febd588102..543a1688db8f 100644
@@ -17,5 +17,7 @@ int cleanup_before_linux(void);
 /* board/.../... */
 int board_init(void);
 void board_quiesce_devices(void);
+int riscv_board_reserved_mem_fixup(void *fdt);
+int riscv_fdt_copy_resv_mem_node(const void *src_fdt, void *dest_fdt);
 
 #endif	/* _U_BOOT_RISCV_H_ */
arch/riscv/lib/Makefile +1 -0
index adadbf4bcbef..d132b59ce32c 100644
@@ -24,6 +24,7 @@ obj-y	+= reset.o
 obj-y   += setjmp.o
 obj-$(CONFIG_SMP) += smp.o
 obj-$(CONFIG_SPL_BUILD)	+= spl.o
+obj-y   += fdt_fixup.o
 
 # For building EFI apps
 CFLAGS_$(EFI_CRT0) := $(CFLAGS_EFI)
arch/riscv/lib/asm-offsets.c +1 -0
index 4fa4fd371473..7301c1b98e23 100644
@@ -14,6 +14,7 @@
 int main(void)
 {
 	DEFINE(GD_BOOT_HART, offsetof(gd_t, arch.boot_hart));
+	DEFINE(GD_FIRMWARE_FDT_ADDR, offsetof(gd_t, arch.firmware_fdt_addr));
 #ifndef CONFIG_XIP
 	DEFINE(GD_AVAILABLE_HARTS, offsetof(gd_t, arch.available_harts));
 #endif
arch/riscv/lib/fdt_fixup.c new file +102 -0

102 lines not on this page · raw message

Signature · 1 line
2.25.1
#7 lore raw

PATCH RESEND v5 3/6 riscv: Provide a mechanism to fix DT for reserved memory

Quoted 13 lines · Atish Patra

In RISC-V, M-mode software can reserve physical memory regions by setting appropriate physical memory protection (PMP) csr. As the PMP csr are accessible only in M-mode, S-mode U-Boot can not read this configuration directly. However, M-mode software can pass this information via reserved-memory node in device tree so that S-mode software can access this information.

This patch provides a framework to copy to the reserved-memory node from one DT to another. This will be used to update the DT used by U-Boot and the DT passed to the next stage OS.

Quoted 10 lines
---
 arch/riscv/cpu/start.S                |   1 +
 arch/riscv/include/asm/global_data.h  |   1 +
 arch/riscv/include/asm/u-boot-riscv.h |   2 +
 arch/riscv/lib/Makefile               |   1 +
 arch/riscv/lib/asm-offsets.c          |   1 +
 arch/riscv/lib/fdt_fixup.c            | 102 ++++++++++++++++++++++++++
 6 files changed, 108 insertions(+)
 create mode 100644 arch/riscv/lib/fdt_fixup.c
#8 lore raw

PATCH RESEND v5 4/6 riscv: Setup reserved-memory node for FU540

In reply to Atish Patra

FU540 uses OF_SEPARATE instead of OF_PRIOR.

Enable OF_BOARD_FIXUP to update the DT with reserved-memory node.

---
 arch/riscv/lib/fdt_fixup.c     | 15 +++++++++++++++
 configs/sifive_fu540_defconfig |  1 +
 2 files changed, 16 insertions(+)
arch/riscv/lib/fdt_fixup.c +15 -0
index 1fce41490973..af12e484db9b 100644
@@ -100,3 +100,18 @@ int riscv_board_reserved_mem_fixup(void *fdt)
 
 	return 0;
 }
+
+#ifdef CONFIG_OF_BOARD_FIXUP
+int board_fix_fdt(void *fdt)
+{
+	int err;
+
+	err = riscv_board_reserved_mem_fixup(fdt);
+	if (err < 0) {
+		printf("failed to fixup DT for reserved memory: %d\n", err);
+		return err;
+	}
+
+	return 0;
+}
+#endif
configs/sifive_fu540_defconfig +1 -0
index 6d61e6c960ee..8fb3794cd578 100644
@@ -12,3 +12,4 @@ CONFIG_DISPLAY_BOARDINFO=y
 CONFIG_DEFAULT_DEVICE_TREE="hifive-unleashed-a00"
 CONFIG_SYS_RELOC_GD_ENV_ADDR=y
 CONFIG_DM_MTD=y
+CONFIG_OF_BOARD_FIXUP=y
Signature · 1 line
2.25.1
#9 lore raw

PATCH RESEND v5 4/6 riscv: Setup reserved-memory node for FU540

Quoted 2 lines · Atish Patra

FU540 uses OF_SEPARATE instead of OF_PRIOR.

Should be OF_PRIOR_STAGE

Quoted 37 lines

Enable OF_BOARD_FIXUP to update the DT with reserved-memory node.

---
 arch/riscv/lib/fdt_fixup.c     | 15 +++++++++++++++
 configs/sifive_fu540_defconfig |  1 +
 2 files changed, 16 insertions(+)
arch/riscv/lib/fdt_fixup.c +15 -0
index 1fce41490973..af12e484db9b 100644
@@ -100,3 +100,18 @@ int riscv_board_reserved_mem_fixup(void *fdt)
 
        return 0;
 }
+
+#ifdef CONFIG_OF_BOARD_FIXUP
+int board_fix_fdt(void *fdt)
+{
+       int err;
+
+       err = riscv_board_reserved_mem_fixup(fdt);
+       if (err < 0) {
+               printf("failed to fixup DT for reserved memory: %d\n", err);
+               return err;
+       }
+
+       return 0;
+}
+#endif
configs/sifive_fu540_defconfig +0 -0
index 6d61e6c960ee..8fb3794cd578 100644

configs/sifive_fu540_defconfig CONFIG_DISPLAY_BOARDINFO=y · line 15

Quoted 4 lines
configs/sifive_fu540_defconfig +1 -0
@@ -12,3 +12,4 @@ CONFIG_DISPLAY_BOARDINFO=y
 CONFIG_DEFAULT_DEVICE_TREE="hifive-unleashed-a00"
 CONFIG_SYS_RELOC_GD_ENV_ADDR=y
 CONFIG_DM_MTD=y
+CONFIG_OF_BOARD_FIXUP=y

This should be inserted before CONFIG_DEFAULT_DEVICE_TREE

Quoted 1 line

--

Other than above:

Regards,
Bin

#10 lore raw

PATCH RESEND v5 5/6 riscv: Copy the reserved-memory nodes to final DT

In reply to Atish Patra

The DT used by U-Boot may be different from the DT being passed to the OS if the DT is loaded from external media such as network or mmc. In that case, the reserved-memory node needs to be copied to the DT passed to the OS.

---
 arch/riscv/lib/bootm.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
arch/riscv/lib/bootm.c +7 -1
index 87cadad5016d..8ff8db6bf533 100644
@@ -28,8 +28,8 @@ __weak void board_quiesce_devices(void)
 
 int arch_fixup_fdt(void *blob)
 {
-#ifdef CONFIG_EFI_LOADER
 	int err;
+#ifdef CONFIG_EFI_LOADER
 	u32 size;
 	int chosen_offset;
 
@@ -50,6 +50,12 @@ int arch_fixup_fdt(void *blob)
 	/* Overwrite the boot-hartid as U-Boot is the last stage BL */
 	fdt_setprop_u32(blob, chosen_offset, "boot-hartid", gd->arch.boot_hart);
 #endif
+
+	/* Copy the reserved-memory node to the DT used by OS */
+	err = riscv_fdt_copy_resv_mem_node(gd->fdt_blob, blob);
+	if (err < 0)
+		return err;
+
 	return 0;
 }
 
Signature · 1 line
2.25.1
#11 lore raw

PATCH RESEND v5 6/6 riscv: Move all fdt fixups together

In reply to Atish Patra

Keep all the fdt fixups together for better code management.

---
 arch/riscv/lib/bootm.c     | 33 ---------------------------------
 arch/riscv/lib/fdt_fixup.c | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 33 deletions(-)
arch/riscv/lib/bootm.c +0 -33
index 8ff8db6bf533..0d06095da11a 100644
@@ -26,39 +26,6 @@ __weak void board_quiesce_devices(void)
 {
 }
 
-int arch_fixup_fdt(void *blob)
-{
-	int err;
-#ifdef CONFIG_EFI_LOADER
-	u32 size;
-	int chosen_offset;
-
-	size = fdt_totalsize(blob);
-	err  = fdt_open_into(blob, blob, size + 32);
-	if (err < 0) {
-		printf("Device Tree can't be expanded to accommodate new node");
-		return err;
-	}
-	chosen_offset = fdt_path_offset(blob, "/chosen");
-	if (chosen_offset < 0) {
-		err = fdt_add_subnode(blob, 0, "chosen");
-		if (err < 0) {
-			printf("chosen node can not be added\n");
-			return err;
-		}
-	}
-	/* Overwrite the boot-hartid as U-Boot is the last stage BL */
-	fdt_setprop_u32(blob, chosen_offset, "boot-hartid", gd->arch.boot_hart);
-#endif
-
-	/* Copy the reserved-memory node to the DT used by OS */
-	err = riscv_fdt_copy_resv_mem_node(gd->fdt_blob, blob);
-	if (err < 0)
-		return err;
-
-	return 0;
-}
-
 /**
  * announce_and_cleanup() - Print message and prepare for kernel boot
  *
arch/riscv/lib/fdt_fixup.c +33 -0
index af12e484db9b..20e0759f135b 100644
@@ -115,3 +115,36 @@ int board_fix_fdt(void *fdt)
 	return 0;
 }
 #endif
+
+int arch_fixup_fdt(void *blob)
+{
+	int err;
+#ifdef CONFIG_EFI_LOADER
+	u32 size;
+	int chosen_offset;
+
+	size = fdt_totalsize(blob);
+	err  = fdt_open_into(blob, blob, size + 32);
+	if (err < 0) {
+		printf("Device Tree can't be expanded to accommodate new node");
+		return err;
+	}
+	chosen_offset = fdt_path_offset(blob, "/chosen");
+	if (chosen_offset < 0) {
+		err = fdt_add_subnode(blob, 0, "chosen");
+		if (err < 0) {
+			printf("chosen node can not be added\n");
+			return err;
+		}
+	}
+	/* Overwrite the boot-hartid as U-Boot is the last stage BL */
+	fdt_setprop_u32(blob, chosen_offset, "boot-hartid", gd->arch.boot_hart);
+#endif
+
+	/* Copy the reserved-memory node to the DT used by OS */
+	err = riscv_fdt_copy_resv_mem_node(gd->fdt_blob, blob);
+	if (err < 0)
+		return err;
+
+	return 0;
+}
Signature · 1 line
2.25.1