Skip to contentSkip to conversation

powerpc: Enable LTO on mpc85xx

PATCH In review Josef Schlehofer 4 participants 24 messages Nov 19 – Nov 20

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

v3 cover powerpc: Enable LTO on mpc85xx

This series allows LTO on mpc85xx and enables it for the CZ.NIC Turris 1.x. The Turris NOR image is limited to 768 KiB. LTO, together with dropping NVMe and SCSI from the NOR build, makes room for the ubi command and the BCH ECC code required by the NAND.

v3 is based on next, which has "Makefile: Set LTO_ENABLE before including config.mk" [1], needed by patch 7. On u-boot-ppc next, patch 2 conflicts in board/nxp/t104xrdb/spl.c, which that branch removes. Dropping that hunk resolves it.

The series consists of:

1-2. PowerPC global data:
- Drop an unused SPL copy of gd.
- Make the global data pointer LTO-safe.

3-5. PowerPC private libgcc:
- Add GCC's out-of-line register save and restore routines from Linux.
- Add 64-bit division routines.
- Select the private libgcc for mpc85xx LTO builds, as the LTO link puts it inside --whole-archive, where a toolchain libgcc does not link.

6-9. mpc85xx LTO support:
- Discard the LTO keep-syms section and its fixups.
- Drop -ffunction-sections and -fdata-sections with LTO, as 32-bit ARM does.
- Allow CONFIG_LTO=y on mpc85xx.
- Pass the make jobserver to xPL links, as GCC 12's lto-wrapper requires.

10-14. Turris 1.x:
- Enable LTO.
- Select the BCH ECC code required by the NAND.
- Make the NVMe and SCSI boot targets conditional.
- Add the ubi command to the NOR build.
- Put the OTP MAC addresses and serial number into the kernel device tree also after the environment has been reset.

On next, the turris_1x_sdcard u-boot.bin is 1052227 bytes, 3651 bytes more than the 1 MiB that its SPL loads from the card. The build does not catch this. Patch 10 brings the image back below the limit. Patch 11 fixes a separate problem: without it, NAND probing hits BUG().

With Debian's GCC 14.2, turris_1x_nor's u-boot-nodtb.bin shrinks from 759880 to 752752 bytes while gaining LTO, BCH ECC and the ubi command and dropping NVMe and SCSI. The turris_1x_sdcard u-boot.bin shrinks from 1052227 to 998051 bytes.

All 83 PowerPC defconfigs build, and all 71 mpc85xx defconfigs link with CONFIG_LTO=y. No .discard* section or zero fixup entry remains. test.py on qemu-ppce500 gives the same results as on next, with and without LTO. With the kernel.org GCC 14.2 used by CI, both Turris LTO builds fit. With Debian's GCC 12.2 and make 4.3 in a Debian bookworm container, the Turris SD card SPL links at -j8 only with patch 9.

v2 was boot-tested on a Turris 1.1 from NOR and SD card. Its NOR build also booted OpenWrt from a kernel in a UBI volume on NAND, and Linux received the OTP MAC addresses and serial number even after "env default -a". With the SD card build, ethaddr and serial# values set in the environment were preserved. Against v2, the code of v3 changes only in two comments of patch 4. v3 was rebased onto next and build-tested only, as were the other boards.

Changes in v3:
- Based on next, which has the generic LTO fixes v2 depended on.
- Patch 4: name lib/div64.c as the source more clearly and explain why the high word is only divided first when the divisor fits into 32 bits, as Bradley Morgan asked.
- Patch 7: compare with 32-bit ARM, as ARM64 keeps both flags with LTO.
- Collect Bradley's Reviewed-by tags.
- Measure the sizes again on next.

Changes in v2:
- Based on main.
- Replace "Makefile: Link a toolchain libgcc outside --whole-archive with LTO" with patches 3-5, which make the private libgcc work for mpc85xx LTO builds.
- Add patch 7 to drop -ffunction-sections and -fdata-sections with LTO.
- Send "Makefile: Skip undefined symbols in keep-syms-lto.c" and "Makefile: Pass -static to the LTO link" separately.
- Explain in patch 10 that the SD card image already exceeds 1 MiB without BCH ECC.
- Shorten the commit messages of patches 6 and 9.
- Keep Marek Mojík's Reviewed-by from v1. His Tested-by is not carried over because the images have changed.

[1] commit 5f3bcac3f7f9 ("Makefile: Set LTO_ENABLE before including config.mk")
v2: https://lore.kernel.org/u-boot/[email protected]/
v1: https://lore.kernel.org/u-boot/[email protected]/

Josef Schlehofer (14):
board: nxp: p1_p2_rdb_pc, p1010rdb: Drop unused SPL gd copy
powerpc: Make the global data pointer LTO safe
powerpc: lib: Add GPR save and restore routines to the private libgcc
powerpc: lib: Add 64-bit division to the private libgcc
powerpc: mpc85xx: Use the private libgcc with LTO
powerpc: mpc85xx: Discard LTO keep-syms sections
powerpc: Drop -ffunction-sections and -fdata-sections with LTO
powerpc: mpc85xx: Allow LTO
xpl: Pass the make jobserver to the xPL link
configs: turris_1x: Enable LTO
mtd: rawnand: fsl_elbc: Select NAND_ECC_BCH
board: CZ.NIC: turris_1x: Make NVMe and SCSI boot targets conditional
configs: turris_1x_nor: Enable CMD_UBI, drop NVMe and SCSI
board: CZ.NIC: turris_1x: Fix up MACs and serial number from OTP

 arch/powerpc/Kconfig                      |   2 +
 arch/powerpc/config.mk                    |   8 +-
 arch/powerpc/cpu/mpc83xx/cpu_init.c       |   3 +-
 arch/powerpc/cpu/mpc83xx/spl_minimal.c    |   3 +-
 arch/powerpc/cpu/mpc85xx/cpu_init_early.c |   3 +-
 arch/powerpc/cpu/mpc85xx/u-boot-spl.lds   |  11 +-
 arch/powerpc/cpu/mpc85xx/u-boot.lds       |  11 +-
 arch/powerpc/include/asm/global_data.h    |  30 +++
 arch/powerpc/lib/Makefile                 |   5 +-
 arch/powerpc/lib/crtsavres.S              | 213 ++++++++++++++++++++++
 arch/powerpc/lib/gd.c                     |  14 ++
 arch/powerpc/lib/udivdi3.c                |  85 +++++++++
 board/CZ.NIC/turris_1x/turris_1x.c        |  20 ++
 board/nxp/p1010rdb/spl.c                  |   9 +-
 board/nxp/p1010rdb/spl_minimal.c          |   2 +-
 board/nxp/p1_p2_rdb_pc/p1_p2_rdb_pc.c     |   3 +
 board/nxp/p1_p2_rdb_pc/spl.c              |   9 +-
 board/nxp/p1_p2_rdb_pc/spl_minimal.c      |   2 +-
 board/nxp/t102xrdb/spl.c                  |   4 +-
 board/nxp/t104xrdb/spl.c                  |   4 +-
 board/nxp/t208xqds/spl.c                  |   4 +-
 board/nxp/t208xrdb/spl.c                  |   4 +-
 board/nxp/t4rdb/spl.c                     |   4 +-
 common/board_r.c                          |   2 +-
 common/init/board_init.c                  |   9 +-
 common/spl/spl.c                          |   4 +-
 configs/kmcoge5ne_defconfig               |   1 -
 configs/turris_1x_nor_defconfig           |  10 +-
 configs/turris_1x_sdcard_defconfig        |   2 +-
 doc/build/gcc.rst                         |   6 +-
 drivers/mtd/nand/raw/Kconfig              |   2 +
 include/configs/turris_1x.h               |  20 +-
 scripts/Makefile.xpl                      |   2 +-
 33 files changed, 449 insertions(+), 62 deletions(-)
 create mode 100644 arch/powerpc/lib/crtsavres.S
 create mode 100644 arch/powerpc/lib/gd.c
 create mode 100644 arch/powerpc/lib/udivdi3.c

base-commit: e6fa1282975af50acf44be7aa678b89e187f0860

Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw

PATCH v3 1/14 board: nxp: p1_p2_rdb_pc, p1010rdb: Drop unused SPL gd copy

The SPL board_init_r() of these boards points its gd parameter, which shadows the global gd, at CONFIG_VAL(GD_ADDR), clears that area and stores bd, the full malloc flag and, in the NAND TPLs, the location of the loaded environment there. The SPL keeps running on the global data that cpu_init_early_f() set up in the initial RAM, so nothing reads this copy.

The next patch makes gd LTO safe, and gd can then no longer be the name of a parameter. Drop the copy and the stores to it instead of moving the global data to CONFIG_VAL(GD_ADDR): on the P1/P2 RDB SD card SPLs that address is 4 KiB below the stack top, and the 8 KiB environment buffer of env_mmc_load_singular() would overwrite it.

No functional change: the SPLs and TPLs lose only the stores to the copy.

Tested on CZ.NIC Turris 1.1 (SD boot, with the LTO build of this series).

Cc Priyanka Jain <[email protected]>
Cc Qiang Zhao <[email protected]>
Cc Peng Fan <[email protected]>
Cc Marek Behún <[email protected]>
Cc Marek Mojík <[email protected]>

---
(no changes since v1)

---
 board/nxp/p1010rdb/spl.c     | 9 +--------
 board/nxp/p1_p2_rdb_pc/spl.c | 9 +--------
 2 files changed, 2 insertions(+), 16 deletions(-)
board/nxp/p1010rdb/spl.c +1 -8
index fc26cef2cc8..7c36026a55a 100644
@@ -60,22 +60,17 @@ void board_init_f(ulong bootflag)
 	relocate_code(CONFIG_VAL(RELOC_STACK), 0, CONFIG_SPL_RELOC_TEXT_BASE);
 }
 
-void board_init_r(gd_t *gd, ulong dest_addr)
+void board_init_r(gd_t *dummy, ulong dest_addr)
 {
-	/* Pointer is writable since we allocated a register for it */
-	gd = (gd_t *)CONFIG_VAL(GD_ADDR);
 	struct bd_info *bd;
 
-	memset(gd, 0, sizeof(gd_t));
 	bd = (struct bd_info *)(CONFIG_VAL(GD_ADDR) + sizeof(gd_t));
 	memset(bd, 0, sizeof(struct bd_info));
-	gd->bd = bd;
 
 	arch_cpu_init();
 	get_clocks();
 	mem_malloc_init(CONFIG_VAL(RELOC_MALLOC_ADDR),
 			CONFIG_VAL(RELOC_MALLOC_SIZE));
-	gd->flags |= GD_FLG_FULL_MALLOC_INIT;
 
 #ifndef CONFIG_SPL_NAND_BOOT
 	env_init();
@@ -88,8 +83,6 @@ void board_init_r(gd_t *gd, ulong dest_addr)
 #ifdef CONFIG_SPL_NAND_BOOT
 	nand_spl_load_image(CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
 			    (uchar *)SPL_ENV_ADDR);
-			    gd->env_addr  = (ulong)(SPL_ENV_ADDR);
-	gd->env_valid = ENV_VALID;
 #else
 	env_relocate();
 #endif
board/nxp/p1_p2_rdb_pc/spl.c +1 -8
index b07f481fbf9..6db77682e3c 100644
@@ -72,22 +72,17 @@ void board_init_f(ulong bootflag)
 	relocate_code(CONFIG_VAL(RELOC_STACK), 0, CONFIG_SPL_RELOC_TEXT_BASE);
 }
 
-void board_init_r(gd_t *gd, ulong dest_addr)
+void board_init_r(gd_t *dummy, ulong dest_addr)
 {
-	/* Pointer is writable since we allocated a register for it */
-	gd = (gd_t *)CONFIG_VAL(GD_ADDR);
 	struct bd_info *bd;
 
-	memset(gd, 0, sizeof(gd_t));
 	bd = (struct bd_info *)(CONFIG_VAL(GD_ADDR) + sizeof(gd_t));
 	memset(bd, 0, sizeof(struct bd_info));
-	gd->bd = bd;
 
 	arch_cpu_init();
 	get_clocks();
 	mem_malloc_init(CONFIG_VAL(RELOC_MALLOC_ADDR),
 			CONFIG_VAL(RELOC_MALLOC_SIZE));
-	gd->flags |= GD_FLG_FULL_MALLOC_INIT;
 
 #ifdef CONFIG_SPL_ENV_SUPPORT
 #ifndef CONFIG_SPL_NAND_BOOT
@@ -102,8 +97,6 @@ void board_init_r(gd_t *gd, ulong dest_addr)
 #ifdef CONFIG_SPL_NAND_BOOT
 	nand_spl_load_image(CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
 			    (uchar *)SPL_ENV_ADDR);
-	gd->env_addr  = (ulong)(SPL_ENV_ADDR);
-	gd->env_valid = ENV_VALID;
 #else
 	env_relocate();
 #endif
Signature · 1 line
2.54.0 (Apple Git-157)
#2 lore raw

PATCH v3 2/14 powerpc: Make the global data pointer LTO safe

In reply to Josef Schlehofer

The register variable form of gd cannot be used with LTO (GCC bug 68384). The LTO link fails with "multiple definition of `r2'". In LTO builds define gd through inline assembly accessors that read and write r2, following ARM and RISC-V. Other builds keep the register variable, and set_gd() assigns it.

Every assignment to gd has to go through set_gd() for that to work, so convert all PowerPC ones, including those in common code, and give PowerPC its own arch_setup_gd() in the new arch/powerpc/lib/gd.c instead of the weak one, which assigns gd. Rename the gd parameter of the SPL board_init_r() functions, which the gd macro would otherwise replace.

Cc Bradley Morgan <[email protected]>
Cc Marek Behún <[email protected]>
Cc Christophe Leroy (CS GROUP) <[email protected]>
Cc Mario Six <[email protected]>
Cc Priyanka Jain <[email protected]>
Cc Qiang Zhao <[email protected]>
Cc Peng Fan <[email protected]>
Cc Shengzhou Liu <[email protected]>
Cc Marek Mojík <[email protected]>

---
(no changes since v1)

The mpc83xx and common/spl/spl.c hunks change no build: mpc83xx cannot enable LTO and no PowerPC board builds common/spl/spl.c. They are converted so that no PowerPC code assigns gd directly.

Unlike on ARM and RISC-V, set_gd() assigns the register variable in builds without LTO. GCC requires inline assembly that accesses a global register variable to declare that access through constraints.

In the T-series SPLs, the board_init_r() bodies now use the global gd instead of the parameter. relocate_code() passes the CONFIG_SPL_GD_ADDR that board_init_f() has already set as gd and leaves r2 alone, so the pointer is the same.

checkpatch asks for IS_ENABLED() on the #if lines in common/board_r.c, common/init/board_init.c and common/spl/spl.c. They choose how gd is set per architecture: set_gd() exists only on ARM, RISC-V and PowerPC, so IS_ENABLED() cannot replace them. The new arch/powerpc/lib/gd.c is covered by the POWERPC entry in MAINTAINERS.

---
 arch/powerpc/cpu/mpc83xx/cpu_init.c       |  3 +--
 arch/powerpc/cpu/mpc83xx/spl_minimal.c    |  3 +--
 arch/powerpc/cpu/mpc85xx/cpu_init_early.c |  3 +--
 arch/powerpc/include/asm/global_data.h    | 30 +++++++++++++++++++++++
 arch/powerpc/lib/Makefile                 |  1 +
 arch/powerpc/lib/gd.c                     | 14 +++++++++++
 board/nxp/p1010rdb/spl_minimal.c          |  2 +-
 board/nxp/p1_p2_rdb_pc/spl_minimal.c      |  2 +-
 board/nxp/t102xrdb/spl.c                  |  4 +--
 board/nxp/t104xrdb/spl.c                  |  4 +--
 board/nxp/t208xqds/spl.c                  |  4 +--
 board/nxp/t208xrdb/spl.c                  |  4 +--
 board/nxp/t4rdb/spl.c                     |  4 +--
 common/board_r.c                          |  2 +-
 common/init/board_init.c                  |  9 ++++---
 common/spl/spl.c                          |  4 ++-
 16 files changed, 69 insertions(+), 24 deletions(-)
 create mode 100644 arch/powerpc/lib/gd.c
arch/powerpc/cpu/mpc83xx/cpu_init.c +1 -2
index 340f9a0da56..5618b79a78b 100644
@@ -139,8 +139,7 @@ void cpu_init_f (volatile immap_t * im)
 #endif
 		0;
 
-	/* Pointer is writable since we allocated a register for it */
-	gd = (gd_t *)SYS_INIT_SP_ADDR;
+	set_gd((gd_t *)SYS_INIT_SP_ADDR);
 
 	/* global data region was cleared in start.S */
 
arch/powerpc/cpu/mpc83xx/spl_minimal.c +1 -2
index 7036e3fae0c..1d48428573d 100644
@@ -25,8 +25,7 @@ DECLARE_GLOBAL_DATA_PTR;
  */
 void cpu_init_f (volatile immap_t * im)
 {
-	/* Pointer is writable since we allocated a register for it */
-	gd = (gd_t *)SYS_INIT_SP_ADDR;
+	set_gd((gd_t *)SYS_INIT_SP_ADDR);
 
 	/* global data region was cleared in start.S */
 
arch/powerpc/cpu/mpc85xx/cpu_init_early.c +1 -2
index 75bfc07fe97..fb3d662c9d3 100644
@@ -95,8 +95,7 @@ void cpu_init_early_f(void *fdt)
 	int i;
 #endif
 
-	/* Pointer is writable since we allocated a register for it */
-	gd = (gd_t *)SYS_INIT_SP_ADDR;
+	set_gd((gd_t *)SYS_INIT_SP_ADDR);
 
 	/* gd area was zeroed during startup */
 
arch/powerpc/include/asm/global_data.h +30 -0
index 26cbc7854d9..d8aefd2ca0a 100644
@@ -93,8 +93,38 @@ struct arch_global_data {
 
 #include <asm-generic/global_data.h>
 
+#ifdef LTO_ENABLE
+
+#define DECLARE_GLOBAL_DATA_PTR
+#define gd	get_gd()
+
+static inline gd_t *get_gd(void)
+{
+	gd_t *gd_ptr;
+
+	__asm__ volatile("mr %0, 2\n" : "=r" (gd_ptr));
+
+	return gd_ptr;
+}
+
+static inline void set_gd(gd_t *gd_ptr)
+{
+	__asm__ volatile("mr 2, %0\n" : : "r" (gd_ptr));
+}
+
+#else
+
 #define DECLARE_GLOBAL_DATA_PTR     register gd_t *gd asm ("r2")
 
+/*
+ * GCC does not see an asm statement write r2 and assumes that the
+ * register variable is unchanged, so it may move gd accesses across the
+ * write. Assign the variable itself.
+ */
+#define set_gd(gd_ptr)	(gd = (gd_ptr))
+
+#endif
+
 #include <asm/u-boot.h>
 
 #endif /* __ASM_GBL_DATA_H */
arch/powerpc/lib/Makefile +1 -0
index c0caa703caa..a3048975ae7 100644
@@ -18,6 +18,7 @@ endif
 endif
 
 obj-y	+= bdinfo.o
+obj-y	+= gd.o
 
 ifdef MINIMAL
 obj-y += cache.o time.o
arch/powerpc/lib/gd.c new file +14 -0
new file mode 100644
index 00000000000..d36f784fbf7
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2026 Josef Schlehofer <[email protected]>
+ */
+
+#include <init.h>
+#include <asm/global_data.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+void arch_setup_gd(gd_t *new_gd)
+{
+	set_gd(new_gd);
+}
board/nxp/p1010rdb/spl_minimal.c +1 -1
index 8cd79c6fb5f..33930fef8d9 100644
@@ -45,7 +45,7 @@ void board_init_f(ulong bootflag)
 	relocate_code(CONFIG_SPL_RELOC_STACK, 0, CONFIG_SPL_RELOC_TEXT_BASE);
 }
 
-void board_init_r(gd_t *gd, ulong dest_addr)
+void board_init_r(gd_t *dummy, ulong dest_addr)
 {
 	puts("\nSecond program loader running in sram...");
 	nand_boot();
board/nxp/p1_p2_rdb_pc/spl_minimal.c +1 -1
index 511bcf5506b..960b2c41457 100644
@@ -43,7 +43,7 @@ void board_init_f(ulong bootflag)
 	relocate_code(CONFIG_SPL_RELOC_STACK, 0, CONFIG_SPL_RELOC_TEXT_BASE);
 }
 
-void board_init_r(gd_t *gd, ulong dest_addr)
+void board_init_r(gd_t *dummy, ulong dest_addr)
 {
 	puts("\nSecond program loader running in sram...");
 	nand_boot();
board/nxp/t102xrdb/spl.c +2 -2
index de6cdda194e..f527f62a5dd 100644
@@ -58,7 +58,7 @@ void board_init_f(ulong bootflag)
 	memcpy((void *)CONFIG_SPL_GD_ADDR, (void *)gd, sizeof(gd_t));
 
 	/* Update GD pointer */
-	gd = (gd_t *)(CONFIG_SPL_GD_ADDR);
+	set_gd((gd_t *)(CONFIG_SPL_GD_ADDR));
 
 	console_init_f();
 
@@ -87,7 +87,7 @@ void board_init_f(ulong bootflag)
 	relocate_code(CONFIG_SPL_RELOC_STACK, (gd_t *)CONFIG_SPL_GD_ADDR, 0x0);
 }
 
-void board_init_r(gd_t *gd, ulong dest_addr)
+void board_init_r(gd_t *dummy, ulong dest_addr)
 {
 	struct bd_info *bd;
 
board/nxp/t104xrdb/spl.c +2 -2
index e02a1f95d4c..0310baa381c 100644
@@ -55,7 +55,7 @@ void board_init_f(ulong bootflag)
 	memcpy((void *)CONFIG_SPL_GD_ADDR, (void *)gd, sizeof(gd_t));
 
 	/* Update GD pointer */
-	gd = (gd_t *)(CONFIG_SPL_GD_ADDR);
+	set_gd((gd_t *)(CONFIG_SPL_GD_ADDR));
 
 #ifdef CONFIG_DEEP_SLEEP
 	/* disable the console if boot from deep sleep */
@@ -78,7 +78,7 @@ void board_init_f(ulong bootflag)
 	relocate_code(CONFIG_SPL_RELOC_STACK, (gd_t *)CONFIG_SPL_GD_ADDR, 0x0);
 }
 
-void board_init_r(gd_t *gd, ulong dest_addr)
+void board_init_r(gd_t *dummy, ulong dest_addr)
 {
 	struct bd_info *bd;
 
board/nxp/t208xqds/spl.c +2 -2
index 44ad4e68d9f..f8c641a9c64 100644
@@ -73,7 +73,7 @@ void board_init_f(ulong bootflag)
 	memcpy((void *)CONFIG_SPL_GD_ADDR, (void *)gd, sizeof(gd_t));
 
 	/* Update GD pointer */
-	gd = (gd_t *)(CONFIG_SPL_GD_ADDR);
+	set_gd((gd_t *)(CONFIG_SPL_GD_ADDR));
 
 	console_init_f();
 
@@ -96,7 +96,7 @@ void board_init_f(ulong bootflag)
 	relocate_code(CONFIG_SPL_RELOC_STACK, (gd_t *)CONFIG_SPL_GD_ADDR, 0x0);
 }
 
-void board_init_r(gd_t *gd, ulong dest_addr)
+void board_init_r(gd_t *dummy, ulong dest_addr)
 {
 	struct bd_info *bd;
 
board/nxp/t208xrdb/spl.c +2 -2
index df3b9c6fe40..4b1ab3618c3 100644
@@ -33,7 +33,7 @@ void board_init_f(ulong bootflag)
 	memcpy((void *)CONFIG_SPL_GD_ADDR, (void *)gd, sizeof(gd_t));
 
 	/* Update GD pointer */
-	gd = (gd_t *)(CONFIG_SPL_GD_ADDR);
+	set_gd((gd_t *)(CONFIG_SPL_GD_ADDR));
 
 	console_init_f();
 
@@ -56,7 +56,7 @@ void board_init_f(ulong bootflag)
 	relocate_code(CONFIG_SPL_RELOC_STACK, (gd_t *)CONFIG_SPL_GD_ADDR, 0x0);
 }
 
-void board_init_r(gd_t *gd, ulong dest_addr)
+void board_init_r(gd_t *dummy, ulong dest_addr)
 {
 	struct bd_info *bd;
 
board/nxp/t4rdb/spl.c +2 -2
index 9d2472dec25..441015bf5a4 100644
@@ -39,7 +39,7 @@ void board_init_f(ulong bootflag)
 	memcpy((void *)CONFIG_SPL_GD_ADDR, (void *)gd, sizeof(gd_t));
 
 	/* Update GD pointer */
-	gd = (gd_t *)(CONFIG_SPL_GD_ADDR);
+	set_gd((gd_t *)(CONFIG_SPL_GD_ADDR));
 
 	/* compiler optimization barrier needed for GCC >= 3.4 */
 	__asm__ __volatile__("" : : : "memory");
@@ -59,7 +59,7 @@ void board_init_f(ulong bootflag)
 	relocate_code(CONFIG_SPL_RELOC_STACK, (gd_t *)CONFIG_SPL_GD_ADDR, 0x0);
 }
 
-void board_init_r(gd_t *gd, ulong dest_addr)
+void board_init_r(gd_t *dummy, ulong dest_addr)
 {
 	struct bd_info *bd;
 
common/board_r.c +1 -1
index a397e20959d..7a9de6ba5f3 100644
@@ -796,7 +796,7 @@ void board_init_r(gd_t *new_gd, ulong dest_addr)
 	if (CONFIG_IS_ENABLED(X86_64) && !IS_ENABLED(CONFIG_EFI_APP))
 		arch_setup_gd(new_gd);
 
-#if defined(CONFIG_RISCV)
+#if defined(CONFIG_RISCV) || defined(CONFIG_PPC)
 	set_gd(new_gd);
 #elif !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
 	gd = new_gd;
common/init/board_init.c +5 -4
index 2a6f39f51ad..2faea5432c0 100644
@@ -14,15 +14,16 @@
 DECLARE_GLOBAL_DATA_PTR;
 
 /*
- * Unfortunately x86, ARM and RISC-V can't compile this code as gd is defined
- * as macro and cannot be assigned.
+ * x86, RISC-V and PowerPC provide their own arch_setup_gd(), and ARM does
+ * not call it.
  */
-#if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
+#if !defined(CONFIG_X86) && !defined(CONFIG_ARM) && !defined(CONFIG_RISCV) && \
+	!defined(CONFIG_PPC)
 __weak void arch_setup_gd(struct global_data *gd_ptr)
 {
 	gd = gd_ptr;
 }
-#endif /* !CONFIG_X86 && !CONFIG_ARM */
+#endif /* !CONFIG_X86 && !CONFIG_ARM && !CONFIG_RISCV && !CONFIG_PPC */
 
 /**
  * This function is called from board_init_f_init_reserve to set up
common/spl/spl.c +3 -1
index 6c9a1a1cbcc..81aec2b1108 100644
@@ -956,7 +956,9 @@ ulong spl_relocate_stack_gd(void)
 #if CONFIG_IS_ENABLED(LOG)
 	log_fixup_for_gd_move(new_gd);
 #endif
-#if !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
+#if defined(CONFIG_PPC)
+	set_gd(new_gd);
+#elif !defined(CONFIG_ARM) && !defined(CONFIG_RISCV)
 	gd = new_gd;
 #endif
 	return ptr;
Signature · 1 line
2.54.0 (Apple Git-157)
#3 lore raw

PATCH v3 3/14 powerpc: lib: Add GPR save and restore routines to the private libgcc

In reply to Josef Schlehofer

When GCC optimizes for size, it branches to libgcc's _restgpr_N_x routines to restore non-volatile registers. U-Boot's private libgcc lacks these routines, so builds with CONFIG_USE_PRIVATE_LIBGCC=y fail to link with Debian's GCC 14.2, for example qemu-ppce500:

undefined reference to `_restgpr_31_x'

Copy the 32-bit integer register save and restore routines from Linux's arch/powerpc/lib/crtsavres.S (v7.3-rc5), following the private libgcc's existing approach for shift routines. Put each group of routines in its own section so --gc-sections can discard unused groups. Linux's CONFIG_CC_OPTIMIZE_FOR_SIZE condition is then not needed.

Cc Bradley Morgan <[email protected]>
Cc Tom Rini <[email protected]>
Cc Christophe Leroy (CS GROUP) <[email protected]>
Cc Marek Behún <[email protected]>

---
(no changes since v2)

Changes in v2:
- new patch

The routines assemble to the same code as crtsavgpr.o, crtresgpr.o and crtresxgpr.o in Debian's libgcc 14.2.

---
 arch/powerpc/lib/Makefile    |   3 +-
 arch/powerpc/lib/crtsavres.S | 213 +++++++++++++++++++++++++++++++++++
 2 files changed, 215 insertions(+), 1 deletion(-)
 create mode 100644 arch/powerpc/lib/crtsavres.S
arch/powerpc/lib/Makefile +2 -1
index a3048975ae7..90f0e1e5fa9 100644
@@ -5,7 +5,8 @@
 
 ## Build a couple of necessary functions into a private libgcc
 ## if the user asked for it
-lib-$(CONFIG_USE_PRIVATE_LIBGCC) += _ashldi3.o _ashrdi3.o _lshrdi3.o
+lib-$(CONFIG_USE_PRIVATE_LIBGCC) += _ashldi3.o _ashrdi3.o _lshrdi3.o \
+				    crtsavres.o
 
 MINIMAL=
 
arch/powerpc/lib/crtsavres.S new file +213 -0

213 lines not on this page · raw message

Signature · 1 line
2.54.0 (Apple Git-157)
#4 lore raw

PATCH v3 4/14 powerpc: lib: Add 64-bit division to the private libgcc

In reply to Josef Schlehofer

On 32-bit PowerPC, GCC lowers '/' and '%' on unsigned 64-bit operands to calls to __udivdi3() and __umoddi3(). U-Boot's private libgcc lacks these routines, so turris_1x_sdcard fails to link with CONFIG_USE_PRIVATE_LIBGCC=y:

undefined reference to `__udivdi3'

Implement both with a copy of __div64_32() from lib/div64.c whose divisor is widened to 64 bits. The copy keeps the private libgcc independent of lib/, which not every xPL phase links.

Build the file without LTO, as ARM does for eabi_compat.o: LTO can drop these functions before GCC generates the calls that need them.

---
Changes in v3:
- name lib/div64.c as the source of udivmod64() more clearly
- explain in the comment why the high word is only divided first when the divisor fits into 32 bits

Changes in v2:
- new patch

At -Os the two take 300 bytes, against 1036 and 992 bytes of _udivdi3.o and _umoddi3.o in Debian's libgcc 14.2.

---
 arch/powerpc/lib/Makefile  |  3 +-
 arch/powerpc/lib/udivdi3.c | 85 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+), 1 deletion(-)
 create mode 100644 arch/powerpc/lib/udivdi3.c
arch/powerpc/lib/Makefile +2 -1
index 90f0e1e5fa9..23e62996701 100644
@@ -6,7 +6,8 @@
 ## Build a couple of necessary functions into a private libgcc
 ## if the user asked for it
 lib-$(CONFIG_USE_PRIVATE_LIBGCC) += _ashldi3.o _ashrdi3.o _lshrdi3.o \
-				    crtsavres.o
+				    crtsavres.o udivdi3.o
+CFLAGS_REMOVE_udivdi3.o := $(LTO_CFLAGS)
 
 MINIMAL=
 
arch/powerpc/lib/udivdi3.c new file +85 -0

85 lines not on this page · raw message

Signature · 1 line
2.54.0 (Apple Git-157)
#5 lore raw

Re: [PATCH v3 04/14] powerpc: lib: Add 64-bit division to the private libgcc

Le 19/11/2026 à 14:33, Josef Schlehofer a écrit :

Quoted 6 lines

On 32-bit PowerPC, GCC lowers '/' and '%' on unsigned 64-bit operands to calls to __udivdi3() and __umoddi3(). U-Boot's private libgcc lacks these routines, so turris_1x_sdcard fails to link with CONFIG_USE_PRIVATE_LIBGCC=y:

undefined reference to `__udivdi3'

Why do we need __udivdi3() at all ? Can't we just avoid / and % on 64-bit operands and use __div64_32() or equivalent instead ?

Christophe

Quoted 134 lines

Implement both with a copy of __div64_32() from lib/div64.c whose divisor is widened to 64 bits. The copy keeps the private libgcc independent of lib/, which not every xPL phase links.

Build the file without LTO, as ARM does for eabi_compat.o: LTO can drop these functions before GCC generates the calls that need them.

---
Changes in v3:
- name lib/div64.c as the source of udivmod64() more clearly
- explain in the comment why the high word is only divided first when the divisor fits into 32 bits

Changes in v2:
- new patch

At -Os the two take 300 bytes, against 1036 and 992 bytes of _udivdi3.o and _umoddi3.o in Debian's libgcc 14.2.

---
  arch/powerpc/lib/Makefile  |  3 +-
  arch/powerpc/lib/udivdi3.c | 85 ++++++++++++++++++++++++++++++++++++++
  2 files changed, 87 insertions(+), 1 deletion(-)

create mode 100644 arch/powerpc/lib/udivdi3.c

arch/powerpc/lib/Makefile +2 -1
index 90f0e1e5fa9..23e62996701 100644
@@ -6,7 +6,8 @@
  ## Build a couple of necessary functions into a private libgcc
  ## if the user asked for it
  lib-$(CONFIG_USE_PRIVATE_LIBGCC) += _ashldi3.o _ashrdi3.o _lshrdi3.o \
-				    crtsavres.o
+				    crtsavres.o udivdi3.o
+CFLAGS_REMOVE_udivdi3.o := $(LTO_CFLAGS)
 
  MINIMAL=
 
arch/powerpc/lib/udivdi3.c new file +85 -0
new file mode 100644
index 00000000000..4b02c61bcba
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * 64-bit unsigned division and modulo for the private libgcc
+ *
+ * GCC calls __udivdi3() and __umoddi3() for '/' and '%' on unsigned 64-bit
+ * operands on 32-bit PowerPC.
+ *
+ * udivmod64() is a copy of __div64_32() from lib/div64.c, changed to take a
+ * 64-bit divisor. Its shift-and-subtract loop already works with one. Like
+ * __div64_32(), the functions are not instrumented, as tracing code may
+ * divide. The copyright notices below are those of lib/div64.c.
+ *
+ *   Copyright (C) 2003 Bernardo Innocenti <[email protected]>
+ *
+ *   Based on former do_div() implementation from asm-parisc/div64.h:
+ *	Copyright (C) 1999 Hewlett-Packard Co
+ *	Copyright (C) 1999 David Mosberger-Tang <[email protected]>
+ */
+
+#include <linux/types.h>
+
+static notrace u64 udivmod64(u64 n, u64 base, u64 *rem)
+{
+	u64 b = base;
+	u64 res, d = 1;
+	u32 high = n >> 32;
+
+	/*
+	 * Divide the high word first, as __div64_32() does. This is a 32-bit
+	 * division, so it is only valid when the divisor fits into 32 bits.
+	 */
+	res = 0;
+	if (!(base >> 32) && high >= base) {
+		high /= (u32)base;
+		res = (u64)high << 32;
+		n -= (u64)(high * (u32)base) << 32;
+	}
+
+	while ((s64)b > 0 && b < n) {
+		b = b + b;
+		d = d + d;
+	}
+
+	do {
+		if (n >= b) {
+			n -= b;
+			res += d;
+		}
+		b >>= 1;
+		d >>= 1;
+	} while (d);
+
+	*rem = n;
+	return res;
+}
+
+/**
+ * __udivdi3() - divide two 64-bit unsigned integers
+ *
+ * @n:		dividend
+ * @d:		divisor
+ * Return:	quotient
+ */
+notrace u64 __udivdi3(u64 n, u64 d)
+{
+	u64 rem;
+
+	return udivmod64(n, d, &rem);
+}
+
+/**
+ * __umoddi3() - remainder of the division of two 64-bit unsigned integers
+ *
+ * @n:		dividend
+ * @d:		divisor
+ * Return:	remainder
+ */
+notrace u64 __umoddi3(u64 n, u64 d)
+{
+	u64 rem;
+
+	udivmod64(n, d, &rem);
+
+	return rem;
+}
#6 lore raw

Re: [PATCH v3 04/14] powerpc: lib: Add 64-bit division to the private libgcc

Quoted 8 lines · Christophe Leroy (CS GROUP)

Le 19/11/2026 à 14:33, Josef Schlehofer a écrit :

Quoted 5 lines

On 32-bit PowerPC, GCC lowers '/' and '%' on unsigned 64-bit operands to calls to __udivdi3() and __umoddi3(). U-Boot's private libgcc lacks these routines, so turris_1x_sdcard fails to link with CONFIG_USE_PRIVATE_LIBGCC=y:

Quoted 4 lines
Quoted 1 line

undefined reference to `__udivdi3'

Why do we need __udivdi3() at all ? Can't we just avoid / and % on 64-bit operands and use __div64_32() or equivalent instead ?

Oh yes. That's right. What's the point OF using __udivdi3()..

Also, a potential user of this file is the arch/powerpc/lib/time.c file.

It's a pain to do the dividing maths, and we need a "fast(er)" non overflowy way to do usec2ticks(), see the comment.

/*
* This function is intended for SHORT delays only.
* It will overflow at around 10 seconds @ 400MHz,
* or 20 seconds @ 200MHz.
*/

A patch I self-naked did 64 bit div math but it was slowww..

From a look of this. This looks slightly feasibler as a option, well, this just came into my head.

Quoted 128 lines

Christophe

Quoted 47 lines

Implement both with a copy of __div64_32() from lib/div64.c whose divisor is widened to 64 bits. The copy keeps the private libgcc independent of lib/, which not every xPL phase links.

Build the file without LTO, as ARM does for eabi_compat.o: LTO can drop these functions before GCC generates the calls that need them.

---
Changes in v3:
- name lib/div64.c as the source of udivmod64() more clearly
- explain in the comment why the high word is only divided first when the divisor fits into 32 bits

Changes in v2:
- new patch

At -Os the two take 300 bytes, against 1036 and 992 bytes of _udivdi3.o and _umoddi3.o in Debian's libgcc 14.2.

---
  arch/powerpc/lib/Makefile  |  3 +-
  arch/powerpc/lib/udivdi3.c | 85 ++++++++++++++++++++++++++++++++++++++
  2 files changed, 87 insertions(+), 1 deletion(-)

create mode 100644 arch/powerpc/lib/udivdi3.c

arch/powerpc/lib/Makefile +2 -1
index 90f0e1e5fa9..23e62996701 100644
@@ -6,7 +6,8 @@
  ## Build a couple of necessary functions into a private libgcc
  ## if the user asked for it
  lib-$(CONFIG_USE_PRIVATE_LIBGCC) += _ashldi3.o _ashrdi3.o _lshrdi3.o \
-				    crtsavres.o
+				    crtsavres.o udivdi3.o
+CFLAGS_REMOVE_udivdi3.o := $(LTO_CFLAGS)
    MINIMAL=
  diff --git a/arch/powerpc/lib/udivdi3.c b/arch/powerpc/lib/udivdi3.c

new file mode 100644
index 00000000000..4b02c61bcba

arch/powerpc/lib/udivdi3.c line 5

Quoted 5 lines
arch/powerpc/lib/udivdi3.c new file +5 -0
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * 64-bit unsigned division and modulo for the private libgcc
+ *
+ * GCC calls __udivdi3() and __umoddi3() for '/' and '%' on unsigned

64-bit

arch/powerpc/lib/udivdi3.c

Quoted 3 lines
arch/powerpc/lib/udivdi3.c new file +3 -0
+ * operands on 32-bit PowerPC.
+ *
+ * udivmod64() is a copy of __div64_32() from lib/div64.c, changed to

take a

arch/powerpc/lib/udivdi3.c line 9

Quoted 1 line
arch/powerpc/lib/udivdi3.c new file +1 -0
+ * 64-bit divisor. Its shift-and-subtract loop already works with one.

Like

arch/powerpc/lib/udivdi3.c line 10

Quoted 1 line
arch/powerpc/lib/udivdi3.c new file +1 -0
+ * __div64_32(), the functions are not instrumented, as tracing code

may

Quoted 52 lines
arch/powerpc/lib/udivdi3.c new file +52 -0
+ * divide. The copyright notices below are those of lib/div64.c.
+ *
+ *   Copyright (C) 2003 Bernardo Innocenti <[email protected]>
+ *
+ *   Based on former do_div() implementation from asm-parisc/div64.h:
+ *	Copyright (C) 1999 Hewlett-Packard Co
+ *	Copyright (C) 1999 David Mosberger-Tang <[email protected]>
+ */
+
+#include <linux/types.h>
+
+static notrace u64 udivmod64(u64 n, u64 base, u64 *rem)
+{
+	u64 b = base;
+	u64 res, d = 1;
+	u32 high = n >> 32;
+
+	/*
+	 * Divide the high word first, as __div64_32() does. This is a 32-bit
+	 * division, so it is only valid when the divisor fits into 32 bits.
+	 */
+	res = 0;
+	if (!(base >> 32) && high >= base) {
+		high /= (u32)base;
+		res = (u64)high << 32;
+		n -= (u64)(high * (u32)base) << 32;
+	}
+
+	while ((s64)b > 0 && b < n) {
+		b = b + b;
+		d = d + d;
+	}
+
+	do {
+		if (n >= b) {
+			n -= b;
+			res += d;
+		}
+		b >>= 1;
+		d >>= 1;
+	} while (d);
+
+	*rem = n;
+	return res;
+}
+
+/**
+ * __udivdi3() - divide two 64-bit unsigned integers
+ *
+ * @n:		dividend
+ * @d:		divisor
+ * Return:	quotient

arch/powerpc/lib/udivdi3.c

Quoted 10 lines
arch/powerpc/lib/udivdi3.c new file +10 -0
+ */
+notrace u64 __udivdi3(u64 n, u64 d)
+{
+	u64 rem;
+
+	return udivmod64(n, d, &rem);
+}
+
+/**
+ * __umoddi3() - remainder of the division of two 64-bit unsigned

integers

Quoted 13 lines

arch/powerpc/lib/udivdi3.c line 85

Quoted 13 lines
arch/powerpc/lib/udivdi3.c new file +13 -0
+ *
+ * @n:		dividend
+ * @d:		divisor
+ * Return:	remainder
+ */
+notrace u64 __umoddi3(u64 n, u64 d)
+{
+	u64 rem;
+
+	udivmod64(n, d, &rem);
+
+	return rem;
+}

Other than that, this file really looks good!

Quoted 2 lines

--- Thanks!

#7 lore raw

Re: [PATCH v3 04/14] powerpc: lib: Add 64-bit division to the private libgcc

Hi Christophe, Bradley,

Quoted 6 lines · Bradley Morgan
Quoted 2 lines · Christophe Leroy (CS GROUP)

Why do we need __udivdi3() at all ? Can't we just avoid / and % on 64-bit operands and use __div64_32() or equivalent instead ?

Oh yes. That's right. What's the point OF using __udivdi3()..

The division operations originate in common code, for example in btrfs, squashfs, and mmc_berase() in turris_1x_sdcard. All the instances I found use 32-bit divisors, so do_div() should work for them.

I can drop this patch and convert those divisions in v4 instead, if you both prefer. The downside is that another such division added to common code would break the mpc85xx LTO builds again, because patch 5 replaces the toolchain-provided libgcc for those builds.

Josef

#8 lore raw

Re: [PATCH v3 04/14] powerpc: lib: Add 64-bit division to the private libgcc

Quoted 5 lines · Josef Schlehofer

Hi Christophe, Bradley,

Quoted 2 lines · Bradley Morgan

On 19 November 2026 14:21:14 GMT, "Christophe Leroy (CS GROUP)" <[email protected]> wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Why do we need __udivdi3() at all ? Can't we just avoid / and % on 64-bit operands and use __div64_32() or equivalent instead ?

Oh yes. That's right. What's the point OF using __udivdi3()..

The division operations originate in common code, for example in btrfs, squashfs, and mmc_berase() in turris_1x_sdcard. All the instances I found use 32-bit divisors, so do_div() should work for them.

Listening...

Quoted 5 lines

I can drop this patch and convert those divisions in v4 instead, if you both prefer. The downside is that another such division added to common code would break the mpc85xx LTO builds again, because patch 5 replaces the toolchain-provided libgcc for those builds.

Well, Christophe has the concern, he can assess, also mind answering my question about the user?

Quoted 3 lines

Josef

--- Thanks!

#9 lore raw

Re: [PATCH v3 04/14] powerpc: lib: Add 64-bit division to the private libgcc

Hi Bradley,

Quoted 2 lines · Bradley Morgan

Well, Christophe has the concern, he can assess, also mind answering my question about the user?

Tom asked to update the common code, so v4 will convert the callers and drop this patch.

As for usec2ticks() in arch/powerpc/lib/time.c, a 64-bit implementation would divide by the constant 1000000. With a constant divisor, do_div() uses reciprocal multiplication, and lldiv() relies on the same implementation, as does usec_to_tick() in lib/time.c. GCC 14 at -Os compiles this without any call to a division helper.

However, the overflow described in the comment is in the 32-bit tick count itself, not in an intermediate calculation. Fixing it would therefore require usec2ticks() to return 64-bit tick counts and wait_ticks() to accept them as well.

Josef

#10 lore raw

Re: [PATCH v3 04/14] powerpc: lib: Add 64-bit division to the private libgcc

Quoted 8 lines · Josef Schlehofer

Hi Bradley,

Quoted 2 lines · Bradley Morgan

Well, Christophe has the concern, he can assess, also mind answering my question about the user?

Tom asked to update the common code, so v4 will convert the callers and drop this patch.

Oof.. (also please don't keep doing new threads, it's a pain)

Quoted 11 lines

As for usec2ticks() in arch/powerpc/lib/time.c, a 64-bit implementation would divide by the constant 1000000. With a constant divisor, do_div() uses reciprocal multiplication, and lldiv() relies on the same implementation, as does usec_to_tick() in lib/time.c. GCC 14 at -Os compiles this without any call to a division helper.

However, the overflow described in the comment is in the 32-bit tick count itself, not in an intermediate calculation. Fixing it would therefore require usec2ticks() to return 64-bit tick counts and wait_ticks() to accept them as well.

hmm. any way overflow could be stopped?

Quoted 3 lines

Josef

--- Thanks!

#11 lore raw

Re: [PATCH v3 04/14] powerpc: lib: Add 64-bit division to the private libgcc

Quoted 20 lines · Bradley Morgan
Quoted 8 lines · Josef Schlehofer

Hi Bradley,

Quoted 2 lines · Bradley Morgan

Well, Christophe has the concern, he can assess, also mind answering my question about the user?

Tom asked to update the common code, so v4 will convert the callers and drop this patch.

Oof.. (also please don't keep doing new threads, it's a pain)

Quoted 7 lines

As for usec2ticks() in arch/powerpc/lib/time.c, a 64-bit implementation would divide by the constant 1000000. With a constant divisor, do_div() uses reciprocal multiplication, and lldiv() relies on the same implementation, as does usec_to_tick() in lib/time.c. GCC 14 at -Os compiles this without any call to a division helper.

Quoted 6 lines
Quoted 4 lines

However, the overflow described in the comment is in the 32-bit tick count itself, not in an intermediate calculation. Fixing it would therefore require usec2ticks() to return 64-bit tick counts and wait_ticks() to accept them as well.

hmm. any way overflow could be stopped?

The question is, is it an overflow we might ever encounter?

Signature · 1 line
Tom
#12 lore raw

Re: [PATCH v3 04/14] powerpc: lib: Add 64-bit division to the private libgcc

Quoted 22 lines · Tom Rini
Quoted 6 lines · Bradley Morgan
Quoted 4 lines · Josef Schlehofer

Hi Bradley,

Quoted 1 line · Bradley Morgan

Well, Christophe has the concern, he can assess, also mind answering

my

Quoted 14 lines
Quoted 4 lines
Quoted 1 line

question about the user?

Tom asked to update the common code, so v4 will convert the callers and drop this patch.

Oof.. (also please don't keep doing new threads, it's a pain)

Quoted 7 lines

As for usec2ticks() in arch/powerpc/lib/time.c, a 64-bit implementation would divide by the constant 1000000. With a constant divisor, do_div() uses reciprocal multiplication, and lldiv() relies on the same implementation, as does usec_to_tick() in lib/time.c. GCC 14 at -Os compiles this without any call to a division helper.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

However, the overflow described in the comment is in the 32-bit tick count itself, not in an intermediate calculation. Fixing it would therefore require usec2ticks() to return 64-bit tick counts and wait_ticks() to accept them as well.

hmm. any way overflow could be stopped?

The question is, is it an overflow we might ever encounter?

I had a quick peek, it's not something you'd just reach from booting and praying.

But, my opinion is, it should be fixed in case. Just in case.

If it "can't" be fixed without a sacrifice, id just leave it.

Quoted 2 lines

--- Thanks!

#13 lore raw

Re: [PATCH v3 04/14] powerpc: lib: Add 64-bit division to the private libgcc

In reply to Josef Schlehofer
Quoted 10 lines · Josef Schlehofer

Hi Christophe, Bradley,

Quoted 6 lines · Bradley Morgan
Quoted 2 lines · Christophe Leroy (CS GROUP)

Why do we need __udivdi3() at all ? Can't we just avoid / and % on 64-bit operands and use __div64_32() or equivalent instead ?

Oh yes. That's right. What's the point OF using __udivdi3()..

Quoted 8 lines

The division operations originate in common code, for example in btrfs, squashfs, and mmc_berase() in turris_1x_sdcard. All the instances I found use 32-bit divisors, so do_div() should work for them.

I can drop this patch and convert those divisions in v4 instead, if you both prefer. The downside is that another such division added to common code would break the mpc85xx LTO builds again, because patch 5 replaces the toolchain-provided libgcc for those builds.

Please update the common code. New breakage would trip CI and so that new code would need to be corrected prior to merge.

Signature · 1 line
Tom
#14 lore raw

PATCH v3 5/14 powerpc: mpc85xx: Use the private libgcc with LTO

In reply to Josef Schlehofer

The LTO link rules put $(PLATFORM_LIBS) inside --whole-archive. This works with U-Boot's private libgcc, but pulls every member of a toolchain libgcc into the link. With Debian's GCC 14.2, turris_1x_nor fails to link with CONFIG_LTO=y: ld rejects the hard-float members in this soft-float build, while other members reference __morestack_fail, __tls_get_addr or __SDATA_START__.

Select USE_PRIVATE_LIBGCC for mpc85xx LTO builds. The previous two patches add the routines it was missing. This has no effect until a later patch allows LTO on mpc85xx.

---
(no changes since v2)

Changes in v2:
- new patch, instead of "Makefile: Link a toolchain libgcc outside --whole-archive with LTO"

With the whole series, all 71 mpc85xx defconfigs link with CONFIG_LTO=y.

---
 arch/powerpc/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/powerpc/Kconfig +1 -0
index f20d58b4de6..b4883e00458 100644
@@ -21,6 +21,7 @@ config MPC85xx
 	select SYS_FSL_DDR
 	select SYS_FSL_DDR_BE
 	select SYS_FSL_IFC_BE
+	select USE_PRIVATE_LIBGCC if LTO
 	select BINMAN if MPC85XX_HAVE_RESET_VECTOR && OF_SEPARATE
 	imply CMD_HASH
 	imply CMD_IRQ
Signature · 1 line
2.54.0 (Apple Git-157)
#15 lore raw

PATCH v3 6/14 powerpc: mpc85xx: Discard LTO keep-syms sections

In reply to Josef Schlehofer

With LTO, keep-syms-lto.o puts pointers to linker list entries in .discard.addressable to prevent the entries from being removed. On mpc85xx, -mrelocatable creates .fixup entries for these pointers, and the linker scripts keep all .fixup input sections, leaving .discard.addressable in the image.

Discard .discard* sections and the .fixup entries of keep-syms-lto.o, which point into them. Otherwise, ld leaves zero words in the fixup table, which start.S treats as addresses to relocate. With them, the Turris NOR build with LTO hung after the DRAM message.

---
(no changes since v2)

Changes in v2:
- shorter commit message

---
 arch/powerpc/cpu/mpc85xx/u-boot-spl.lds | 11 ++++++++++-
 arch/powerpc/cpu/mpc85xx/u-boot.lds     | 11 ++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)
arch/powerpc/cpu/mpc85xx/u-boot-spl.lds +10 -1
index d918b4395bf..dd4d18bc24b 100644
@@ -35,7 +35,7 @@ SECTIONS
 		KEEP(*(.got2))
 		KEEP(*(.got))
 		_FIXUP_TABLE_ = .;
-		KEEP(*(.fixup))
+		KEEP(*(EXCLUDE_FILE(*keep-syms-lto.o) .fixup))
 	}
 	__got2_entries = ((_GLOBAL_OFFSET_TABLE_ - _GOT2_TABLE_) >> 2) - 1;
 	__fixup_entries = (. - _FIXUP_TABLE_) >> 2;
@@ -63,6 +63,15 @@ SECTIONS
 	__init_end = .;
 	_end = .;
 
+	/*
+	 * .discard.addressable of keep-syms-lto.o only keeps the linker lists
+	 * alive during an LTO link. Drop it, and its fixups with it.
+	 */
+	/DISCARD/ : {
+		*(.discard*)
+		*keep-syms-lto.o(.fixup)
+	}
+
 #if CONFIG_IS_ENABLED(SYS_MPC85XX_NO_RESETVEC)
 #if defined(CONFIG_SDCARD) && !defined(CFG_SYS_MMC_U_BOOT_OFFS)
 	mmc_u_boot_offs = .;
arch/powerpc/cpu/mpc85xx/u-boot.lds +10 -1
index bedd2d47472..4bb9b4ecfb2 100644
@@ -49,7 +49,7 @@ SECTIONS
     KEEP(*(.got2))
     KEEP(*(.got))
     _FIXUP_TABLE_ = .;
-    KEEP(*(.fixup))
+    KEEP(*(EXCLUDE_FILE(*keep-syms-lto.o) .fixup))
   }
   __got2_entries = ((_GLOBAL_OFFSET_TABLE_ - _GOT2_TABLE_) >> 2) - 1;
   __fixup_entries = (. - _FIXUP_TABLE_) >> 2;
@@ -107,6 +107,15 @@ SECTIONS
 #endif
 #endif
 
+  /*
+   * .discard.addressable of keep-syms-lto.o only keeps the linker lists
+   * alive during an LTO link. Drop it, and its fixups with it.
+   */
+  /DISCARD/ : {
+    *(.discard*)
+    *keep-syms-lto.o(.fixup)
+  }
+
   __bss_start = .;
   .bss (NOLOAD)       :
   {
Signature · 1 line
2.54.0 (Apple Git-157)
#16 lore raw

PATCH v3 7/14 powerpc: Drop -ffunction-sections and -fdata-sections with LTO

In reply to Josef Schlehofer

When LTO is enabled, omit -ffunction-sections and -fdata-sections, as 32-bit ARM does. This affects U-Boot proper only, as scripts/Makefile.xpl adds both flags for xPL builds anyway.

Keep --gc-sections, as ARM does. It drops the .eh_frame sections, which the linker scripts do not place.

Cc Bradley Morgan <[email protected]>
Cc Tom Rini <[email protected]>
Cc Marek Behún <[email protected]>

---
Changes in v3:
- compare with 32-bit ARM, as ARM64 keeps both flags with LTO
- drop the sentence and the note on "Makefile: Set LTO_ENABLE before including config.mk", which is in next now

Changes in v2:
- new patch

With Debian's GCC 14.2, U-Boot proper of the 71 mpc85xx LTO builds changes by -2211 to +5628 bytes of text and data, and 70 of them shrink. SPL/TPL sizes and builds without LTO are unchanged.

---
 arch/powerpc/config.mk | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/powerpc/config.mk +6 -2
index 32e86d84a12..557d9360e3b 100644
@@ -3,10 +3,14 @@
 # (C) Copyright 2000-2010
 # Wolfgang Denk, DENX Software Engineering, [email protected].
 
+# Keep --gc-sections with LTO: it drops the .eh_frame sections, which the
+# linker scripts do not place.
 LDFLAGS_FINAL += --gc-sections
 LDFLAGS_FINAL += --bss-plt
-PLATFORM_RELFLAGS += -fpic -mrelocatable -ffunction-sections \
--fdata-sections -mcall-linux
+PLATFORM_RELFLAGS += -fpic -mrelocatable -mcall-linux
+ifneq ($(LTO_ENABLE),y)
+PLATFORM_RELFLAGS += -ffunction-sections -fdata-sections
+endif
 
 PF_CPPFLAGS_POWERPC	:= $(call cc-option,-fno-ira-hoist-pressure,) $(call cc-option,-Xassembler --fatal-warnings,)
 PLATFORM_CPPFLAGS += -D__powerpc__ -ffixed-r2 -m32 $(PF_CPPFLAGS_POWERPC)
Signature · 1 line
2.54.0 (Apple Git-157)
#17 lore raw

PATCH v3 8/14 powerpc: mpc85xx: Allow LTO

In reply to Josef Schlehofer

Select ARCH_SUPPORTS_LTO from MPC85xx, so that boards can set CONFIG_LTO=y, and name mpc85xx next to ARM in doc/build/gcc.rst. This does not turn LTO on for any board by itself.

Only mpc85xx is covered: it is the PowerPC family whose linker scripts discard the LTO keep-syms sections, and the one that was built and booted with LTO.

Cc Bradley Morgan <[email protected]>
Cc Marek Behún <[email protected]>
Cc Priyanka Jain <[email protected]>

---
(no changes since v1)

---
 arch/powerpc/Kconfig | 1 +
 doc/build/gcc.rst    | 6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)
arch/powerpc/Kconfig +1 -0
index b4883e00458..20716135cd5 100644
@@ -17,6 +17,7 @@ config MPC83xx
 
 config MPC85xx
 	bool "MPC85xx"
+	select ARCH_SUPPORTS_LTO
 	select CREATE_ARCH_SYMLINK
 	select SYS_FSL_DDR
 	select SYS_FSL_DDR_BE
doc/build/gcc.rst +3 -3
index 16afe292899..5e81b2e6661 100644
@@ -196,9 +196,9 @@ Link-time optimisation (LTO)
 U-Boot supports link-time optimisation which can reduce the size of the final
 U-Boot binaries, particularly with SPL.
 
-At present this can be enabled by ARM boards by adding `CONFIG_LTO=y` into the
-defconfig file. Other architectures are not supported. LTO is enabled by default
-for sandbox.
+At present this can be enabled by ARM boards and by PowerPC mpc85xx boards by
+adding `CONFIG_LTO=y` into the defconfig file. Other architectures are not
+supported. LTO is enabled by default for sandbox.
 
 This does incur a link-time penalty of several seconds. For faster incremental
 builds during development, you can disable it by setting `NO_LTO` to `1`.
Signature · 1 line
2.54.0 (Apple Git-157)
#18 lore raw

PATCH v3 9/14 xpl: Pass the make jobserver to the xPL link

In reply to Josef Schlehofer

With LTO, SPL, TPL and VPL are linked through the compiler driver, and GCC's lto-wrapper runs code generation through make. The link recipe is not marked recursive, so make does not pass its jobserver to that command.

With Debian's GCC 12.2 and make 4.3 in a Debian bookworm container, the SPL link of turris_1x_sdcard, which the next patch switches to LTO, therefore fails at -j8:

make[3]: *** write jobserver: Bad file descriptor. Stop.
lto-wrapper: fatal error: make returned 2 exit status

Prefix the recipe with '+', as the top-level U-Boot link rule does.

Tested with Debian's GCC 12.2, where the link now succeeds, and GCC 14.2, where '+' does not change the images of three mpc85xx LTO builds.

---
Changes in v3:
- name the container in which the link fails

Changes in v2:
- shorter commit message

With Debian's GCC 14.2.0-19, the link also succeeds without '+' at -j8. The LTO images of turris_1x_sdcard, P2020RDB-PC_NAND and T2080RDB_NAND are identical with and without it.

The GCC 12.2 failure was seen with the bookworm container emulated on an arm64 host, for v2 and again for v3. On a native amd64 GitHub Actions runner, the same build linked without the '+'.

---
 scripts/Makefile.xpl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/Makefile.xpl +1 -1
index 77746ec07cd..cfc1ad17822 100644
@@ -544,7 +544,7 @@ endif
 $(obj)/$(SPL_BIN): $(u-boot-spl-platdata) $(u-boot-spl-init) \
 		$(u-boot-spl-main) $(u-boot-spl-keep-syms-lto) \
 		$(obj)/u-boot-spl.lds FORCE
-	$(call if_changed,u-boot-spl)
+	+$(call if_changed,u-boot-spl)
 
 $(sort $(u-boot-spl-init) $(u-boot-spl-main)): $(u-boot-spl-dirs) ;
 
Signature · 1 line
2.54.0 (Apple Git-157)
#19 lore raw

PATCH v3 10/14 configs: turris_1x: Enable LTO

In reply to Josef Schlehofer

Enable LTO in both Turris 1.x builds. The NOR build needs the space for the ubi command next to the BCH code. The SD card build needs it even without the BCH code: with Debian's GCC 14.2, its u-boot.bin, device tree included, is 1052227 bytes, more than the 1 MiB (CFG_SYS_MMC_U_BOOT_SIZE) that its SPL loads from the card. With LTO, it is 989355 bytes.

---
Changes in v3:
- sizes measured again on next

Changes in v2:
- give the size of the SD card build, which is too large already without the BCH code

The build does not catch an SD card image that is too large: here BOARD_SIZE_CHECK covers only u-boot-nodtb.bin, while the SPL loads u-boot.bin, device tree included. That is left for a follow-up.

---
 configs/turris_1x_nor_defconfig    | 1 +
 configs/turris_1x_sdcard_defconfig | 1 +
 2 files changed, 2 insertions(+)
configs/turris_1x_nor_defconfig +1 -0
index 070428ae25e..d8ce4aed4a7 100644
@@ -19,6 +19,7 @@ CONFIG_PCIE1=y
 CONFIG_PCIE2=y
 CONFIG_PCIE3=y
 CONFIG_OPTIMIZE_INLINING=y
+CONFIG_LTO=y
 CONFIG_ENV_VARS_UBOOT_CONFIG=y
 CONFIG_HAS_BOARD_SIZE_LIMIT=y
 CONFIG_BOARD_SIZE_LIMIT=786432
configs/turris_1x_sdcard_defconfig +1 -0
index dbeb34dacfb..982a6cb0703 100644
@@ -29,6 +29,7 @@ CONFIG_OF_BOARD_FIXUP=y
 CONFIG_PCIE1=y
 CONFIG_PCIE2=y
 CONFIG_PCIE3=y
+CONFIG_LTO=y
 CONFIG_HAS_BOARD_SIZE_LIMIT=y
 CONFIG_BOARD_SIZE_LIMIT=1048576
 CONFIG_MP=y
Signature · 1 line
2.54.0 (Apple Git-157)
#20 lore raw

PATCH v3 11/14 mtd: rawnand: fsl_elbc: Select NAND_ECC_BCH

In reply to Josef Schlehofer

The CZ.NIC Turris 1.x device tree asks the eLBC FCM NAND driver for software BCH. Without CONFIG_NAND_ECC_BCH, nand_scan_tail() reaches BUG() while probing the NAND chip and the board resets:

BUG at drivers/mtd/nand/raw/nand_base.c:5074/nand_scan_tail()!

This broke with commit b182816c1fb4 ("turris_1x: Normalize Kconfig usage"), which moved the board Kconfig defaults to the defconfigs. NAND_ECC_BCH has no prompt, so a defconfig cannot set it.

Make NAND_FSL_ELBC_DT select NAND_ECC_BCH and NAND_ECC_BCH select BCH, and drop the now redundant CONFIG_BCH=y from the turris_1x and kmcoge5ne defconfigs. This comes after enabling LTO on the Turris 1.x, without which the turris_1x_sdcard u-boot.bin does not fit into the 1 MiB its SPL loads from the card.

Tested on CZ.NIC Turris 1.1.

Fixes b182816c1fb4 ("turris_1x: Normalize Kconfig usage")
Cc Tom Rini <[email protected]>
Cc Heiko Schocher <[email protected]>
Cc Michael Trimarchi <[email protected]>
Cc Dario Binacchi <[email protected]>
Cc Holger Brunck <[email protected]>
Cc Priyanka Jain <[email protected]>
Cc Marek Mojík <[email protected]>

---
(no changes since v2)

Changes in v2:
- reword why this comes after enabling LTO
- move the note on BOARD_SIZE_CHECK to "configs: turris_1x: Enable LTO"

Only the two Turris 1.x defconfigs enable NAND_FSL_ELBC_DT, so the ECC of no other board changes.

---
 configs/kmcoge5ne_defconfig        | 1 -
 configs/turris_1x_nor_defconfig    | 1 -
 configs/turris_1x_sdcard_defconfig | 1 -
 drivers/mtd/nand/raw/Kconfig       | 2 ++
 4 files changed, 2 insertions(+), 3 deletions(-)
configs/kmcoge5ne_defconfig +0 -1
index 69ef6cf3b9b..ef03f02d1e5 100644
@@ -200,5 +200,4 @@ CONFIG_QE_UEC=y
 # CONFIG_PINCTRL_FULL is not set
 CONFIG_QE=y
 CONFIG_SYS_NS16550=y
-CONFIG_BCH=y
 CONFIG_POST=y
configs/turris_1x_nor_defconfig +0 -1
index d8ce4aed4a7..35df37fbc70 100644
@@ -119,5 +119,4 @@ CONFIG_USB_EHCI_FSL=y
 CONFIG_USB_STORAGE=y
 CONFIG_WDT=y
 CONFIG_WDT_MAX6370=y
-CONFIG_BCH=y
 # CONFIG_BINMAN_FDT is not set
configs/turris_1x_sdcard_defconfig +0 -1
index 982a6cb0703..5e99d895b2f 100644
@@ -151,4 +151,3 @@ CONFIG_USB_EHCI_FSL=y
 CONFIG_WDT=y
 CONFIG_WDT_MAX6370=y
 CONFIG_FAT_WRITE=y
-CONFIG_BCH=y
drivers/mtd/nand/raw/Kconfig +2 -0
index 6084b614358..b0ae441900b 100644
@@ -240,6 +240,7 @@ config NAND_FSL_ELBC
 config NAND_FSL_ELBC_DT
 	bool "Support Freescale Enhanced Local Bus Controller FCM NAND driver (DT mode)"
 	depends on NAND_FSL_ELBC
+	select NAND_ECC_BCH
 
 config NAND_FSL_IFC
 	bool "Support Freescale Integrated Flash Controller NAND driver"
@@ -259,6 +260,7 @@ config NAND_KIRKWOOD
 
 config NAND_ECC_BCH
 	bool
+	select BCH
 
 config NAND_KMETER1
 	bool "Support KMETER1 NAND controller"
Signature · 1 line
2.54.0 (Apple Git-157)
#21 lore raw

PATCH v3 12/14 board: CZ.NIC: turris_1x: Make NVMe and SCSI boot targets conditional

In reply to Josef Schlehofer

A Turris 1.x configuration without CONFIG_NVME or CONFIG_SCSI does not build. The NVMe and SCSI distroboot targets are listed unconditionally, and config_distro_bootcmd.h turns a target whose support is disabled into a build error such as:

error: expected '}' before 'BOOT_TARGET_DEVICES_references_NVME_without_CONFIG_NVME'

List the NVMe target only when CONFIG_NVME is enabled and the SCSI target only when CONFIG_SCSI is enabled, so that the NOR build can drop NVMe and SCSI to make room for the ubi command. Both defconfigs enable NVMe and SCSI, so nothing changes yet.

---
(no changes since v1)

u-boot.bin of turris_1x_nor and turris_1x_sdcard has the same size and md5 before and after this patch (Debian GCC 14.2).

---
 include/configs/turris_1x.h | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
include/configs/turris_1x.h +16 -4
index c05614950c7..3a2818d1904 100644
@@ -71,6 +71,18 @@
  *         define CONFIG_SPL_MAX_SIZE = CONFIG_SYS_L2_SIZE
  */
 
+#ifdef CONFIG_NVME
+#define BOOT_TARGET_NVME(func) func(NVME, nvme, 0)
+#else
+#define BOOT_TARGET_NVME(func)
+#endif
+
+#ifdef CONFIG_SCSI
+#define BOOT_TARGET_SCSI(func) func(SCSI, scsi, 0)
+#else
+#define BOOT_TARGET_SCSI(func)
+#endif
+
 #ifdef CONFIG_SDCARD
 #define CFG_SYS_MMC_U_BOOT_SIZE	CONFIG_BOARD_SIZE_LIMIT
 #define CFG_SYS_MMC_U_BOOT_DST	CONFIG_TEXT_BASE
@@ -266,8 +278,8 @@
 #ifdef CONFIG_SDCARD
 #define BOOT_TARGET_DEVICES(func) \
 	func(MMC, mmc, 0) \
-	func(NVME, nvme, 0) \
-	func(SCSI, scsi, 0) \
+	BOOT_TARGET_NVME(func) \
+	BOOT_TARGET_SCSI(func) \
 	func(USB, usb, 0) \
 	func(USB, usb, 1) \
 	func(USB, usb, 2) \
@@ -279,8 +291,8 @@
 #else
 #define BOOT_TARGET_DEVICES(func) \
 	func(MMC, mmc, 0) \
-	func(NVME, nvme, 0) \
-	func(SCSI, scsi, 0) \
+	BOOT_TARGET_NVME(func) \
+	BOOT_TARGET_SCSI(func) \
 	func(USB, usb, 0) \
 	func(USB, usb, 1) \
 	func(USB, usb, 2) \
Signature · 1 line
2.54.0 (Apple Git-157)
#22 lore raw

PATCH v3 13/14 configs: turris_1x_nor: Enable CMD_UBI, drop NVMe and SCSI

In reply to Josef Schlehofer

Enable CMD_UBI so that the NOR build can boot a kernel from a UBI volume on the NAND. CMD_UBIFS, which defaults to y with it, stays off. A kernel inside a UBIFS volume would need it.

Even with LTO, the ubi command does not fit into the 768 KiB NOR image next to NVMe and SCSI, so drop those from the NOR build. The SD card build keeps both boot targets.

Tested on CZ.NIC Turris 1.1: with the MAX6370 watchdog stopped first, the NOR build read the OpenWrt kernel from its UBI volume on the NAND and booted OpenWrt.

---
(no changes since v1)

The default environment does not call ubi. The boot above used:

setenv bootargs ubi.mtd=nand_ubi,2048 rootwait console=ttyS0,115200 wdt dev watchdog@2; wdt stop
ubi part rootfs 2048; ubi read $kernel_addr_r kernel bootm $kernel_addr_r

---
 configs/turris_1x_nor_defconfig | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
configs/turris_1x_nor_defconfig +2 -6
index 35df37fbc70..c5aaf55a75c 100644
@@ -55,6 +55,8 @@ CONFIG_CMD_EXT4=y
 CONFIG_CMD_FAT=y
 CONFIG_CMD_FS_GENERIC=y
 CONFIG_CMD_FS_UUID=y
+CONFIG_CMD_UBI=y
+# CONFIG_CMD_UBIFS is not set
 CONFIG_OF_CONTROL=y
 CONFIG_OF_INITIAL_DTB_READONLY=y
 CONFIG_ENV_OVERWRITE=y
@@ -63,9 +65,6 @@ CONFIG_ENV_RELOC_GD_ENV_ADDR=y
 CONFIG_USE_ETHPRIME=y
 CONFIG_ETHPRIME="eth2"
 # CONFIG_DM_DEVICE_REMOVE is not set
-CONFIG_AHCI=y
-CONFIG_SCSI_AHCI=y
-CONFIG_AHCI_PCI=y
 CONFIG_LBA48=y
 CONFIG_SYS_64BIT_LBA=y
 CONFIG_DDR_CLK_FREQ=66666666
@@ -101,14 +100,11 @@ CONFIG_SYS_MAX_FLASH_SECT=128
 CONFIG_MTD_RAW_NAND=y
 CONFIG_NAND_FSL_ELBC=y
 CONFIG_NAND_FSL_ELBC_DT=y
-CONFIG_MTD_UBI=y
 CONFIG_DM_MDIO=y
 CONFIG_MII=y
 CONFIG_TSEC_ENET=y
 CONFIG_PHY_FIXED=y
-CONFIG_NVME_PCI=y
 CONFIG_PCIE_FSL=y
-CONFIG_SCSI=y
 CONFIG_SPECIFY_CONSOLE_INDEX=y
 CONFIG_DM_SERIAL=y
 CONFIG_SYS_NS16550=y
Signature · 1 line
2.54.0 (Apple Git-157)
#23 lore raw

PATCH v3 14/14 board: CZ.NIC: turris_1x: Fix up MACs and serial number from OTP

In reply to Josef Schlehofer

When the environment is reset with "env default -a" before booting, the kernel device tree gets no /serial-number and keeps its all-zero MAC addresses, so Linux reports "Using random MAC address". misc_init_r() reads both values from the ATSHA204A into the environment at startup, and the device tree fixups take them from there, so the reset loses them.

Read the OTP again in ft_board_setup() and repeat these fixups, through a new weak hook in the shared P1/P2 RDB code next to p1_p2_rdb_pc_fix_fdt_model(). As in misc_init_r(), a valid MAC address or a 16-character serial# already in the environment is kept. An EVT_FT_FIXUP spy would not be called: without OFNODE_MULTI_TREE, which Turris 1.x does not enable, image_setup_libfdt() sends that event only for the control device tree.

Tested on CZ.NIC Turris 1.1 with the NOR and the SD card build after "env default -a".

Fixes f41582035cf7 ("powerpc: Add support for CZ.NIC Turris 1.x routers")
Cc Marek Mojík <[email protected]>
Cc Marek Behún <[email protected]>
Cc Priyanka Jain <[email protected]>

---
(no changes since v1)

---
 board/CZ.NIC/turris_1x/turris_1x.c    | 20 ++++++++++++++++++++
 board/nxp/p1_p2_rdb_pc/p1_p2_rdb_pc.c |  3 +++
 2 files changed, 23 insertions(+)
board/CZ.NIC/turris_1x/turris_1x.c +20 -0
index 32535ed6ee0..8173e021f11 100644
@@ -315,6 +315,26 @@ int misc_init_r(void)
 	return 0;
 }
 
+/*
+ * The generic fixups copy the MAC addresses and the serial number from the
+ * environment into the kernel device tree. misc_init_r() puts them there, but
+ * if the environment is reset before booting, e.g. by "env default -a", they
+ * are gone. Read them from the OTP again and repeat these fixups. Valid MAC
+ * addresses and a 16-character serial# set in the environment are kept.
+ */
+void p1_p2_rdb_pc_fix_fdt_env(void *blob)
+{
+	int ret;
+
+	ret = turris_atsha_otp_init_mac_addresses(0);
+	if (ret)
+		printf("Cannot read MAC addresses from OTP: %d\n", ret);
+	turris_atsha_otp_init_serial_number();
+
+	fdt_fixup_ethernet(blob);
+	fdt_root(blob);
+}
+
 /* This comes from ../../nxp/p1_p2_rdb_pc/p1_p2_rdb_pc.c */
 extern int checkboard_p1_p2(void);
 
board/nxp/p1_p2_rdb_pc/p1_p2_rdb_pc.c +3 -0
index a19a98dd0d1..07c18117cbe 100644
@@ -373,6 +373,8 @@ int board_early_init_r(void)
 
 __weak void p1_p2_rdb_pc_fix_fdt_model(void *blob) {}
 
+__weak void p1_p2_rdb_pc_fix_fdt_env(void *blob) {}
+
 #if defined(CONFIG_OF_BOARD_SETUP) || defined(CONFIG_OF_BOARD_FIXUP)
 static void fix_max6370_watchdog(void *blob)
 {
@@ -418,6 +420,7 @@ int ft_board_setup(void *blob, struct bd_info *bd)
 
 	p1_p2_rdb_pc_fix_fdt_model(blob);
 	fix_max6370_watchdog(blob);
+	p1_p2_rdb_pc_fix_fdt_env(blob);
 
 #if defined(CONFIG_HAS_FSL_DR_USB)
 	fsl_fdt_fixup_dr_usb(blob, bd);
Signature · 1 line
2.54.0 (Apple Git-157)