Repository navigation
arm: apple: turn the caches on before relocation - #58
RyanTheTide wants to merge 1 commit into
Conversation
The boot loader hands over with the MMU off and U-Boot turned it on only in initr_caches(), after relocation. Before that every data access goes to DRAM uncached, and board_init_f() is mostly device tree walks: the driver model scan, dram_init() and the console lookup make about six million data accesses with a T6000 device tree and four million with a T8112 one. board_init_f() took 2.4 to 2.9 s on an M1 Pro, 1 to 2 s on the M4 Pro and A18 Pro, and 0.4 s on an M2, on every boot. Earlier fixes removed single slow walks (dram_init(), the CTXR reserved-memory scan); the walks that are left are generic code. Turn the MMU and the data cache on in arch_cpu_init(), before the driver model scan, as i.MX8M does. Finding the DRAM ranges takes a walk of the whole tree, so the first tables map only the SoC's I/O regions and what U-Boot already uses: its image with the early stack, gd and malloc() area, and its device tree. dram_init() then maps the first DRAM range, which the reservations and the relocation destination are in, and enable_caches() turns the caches off and builds the full map after relocation, as before. If U-Boot or its device tree is not in the first DRAM range the caches go off again and board_init_f() carries on as it did. The SoC table lookup moves out of build_mem_map() so arch_cpu_init() can take the I/O regions from it. The early tables live in .data, which is usable before relocation, and are as large as the final ones. Signed-off-by: Ryan Murray <[email protected]>
|
Blockers: 1.
What I checked and found sound:
Nit: the 256 KiB table array in #54 rewrites the same tables and HW row: on the fixed head, the same aura-try trial (3 boots, Assisted-by: LLM (Claude:Opus 5.5) |
Problem
U-Boot runs
board_init_f()with the MMU off: the boot loader hands over with it off andinitr_caches()turns it on only after relocation. Every data access before that goes to DRAM uncached, andboard_init_f()is mostly device tree walks (the driver model scan,dram_init(), the console lookup). That costs, on every boot (bootstage marksboard_init_f->board_init_r):board_init_fdram_init()walk fix)Single slow walks were fixed before (
dram_init(), the CTXR reserved-memory scan). What is left is generic code, so fixing walks one at a time does not end it.Change
Turn the MMU and the data cache on in
arch_cpu_init(), before the driver model scan, the way i.MX8M does (early_enable_caches()):malloc()area above it, and its device tree. Finding the DRAM ranges first would need a walk of the whole tree with the caches still off.dram_init()then maps the first DRAM range, whichsetup_dest_addr(), the reservations andctxr_exec_relocate_uboot()all work in.enable_caches()turns the caches off and builds the full map after relocation, as before.board_init_f()carries on as it does today.The SoC table lookup moves out of
build_mem_map()intoapple_soc_mem_map(). The early tables are in.data(.bssis not usable before relocation) and as large asget_page_table_size(), so the image grows by 256 KiB (mostly zeros in the compressed payload).Testing
apple_m1_defconfig, GCC 16.1 aarch64: builds without warnings; checkpatch: one#if CONFIG_IS_ENABLED(SYS_MALLOC_F), needed because the gd fields only exist then (same asboot/ctxr_exec_window.c).QEMU (
virt, EL2,-cpu max) with the real T6000/T6002/T6022/T8112/T8132/T8140/T6040 device trees (memory node pointed at QEMU RAM): this branch and the base reach the same point after relocation (the first PMGR power-up), with no exceptions.SCTLR_EL2is0x1005(M, C, I) atdm_init_and_scan(), and the early tables use at most 32 KiB.Counting data accesses before relocation in QEMU: the base does 6.0 M with the J316s tree (4.2 M J413, 4.6 M J700, 5.1 M J616s). With this branch, about 15 k of them still run uncached (before and in
arch_cpu_init()). The rest run cached.M1 Pro (T6000, J316s), hardware, 3 + 3 boots. aura-try one-shot of the live Stage 2 with only the U-Boot payload swapped (Stage 1 and the committed Stage 2 untouched, trial guard and dead-man on), production kernel 7.3.0-aurora-fleet74:
board_init_f(s)efi_install_fdt, s since reset)That is 2.55 s off every boot. On every boot: SELinux enforcing, no failed units, no AVC denials, no kernel errors; one s2idle cycle (RTC wake) on a trial boot suspended and resumed; the Stage 2 hash matched after every restore.
board_init_ris unchanged (0.62 s).J616s (T6040), hardware, 3 + 3 boots, same procedure, production kernel 7.3.0-aurora-fleet75:
board_init_f1.529 / 1.529 / 1.494 s on the base, 0.123 / 0.123 / 0.123 s with this branch (1.40 s off every boot). SELinux enforcing, no failed units, no AVC denials, no kernel errors on every boot; s2idle (RTC wake) 1/1 on a trial boot; the Stage 2 hash matched after every restore.M2 (T8112, J413), hardware, 3 + 3 boots, same procedure, production kernel 7.3.0-aurora-fleet74:
board_init_f0.463 / 0.463 / 0.463 s on the base, 0.046 / 0.046 / 0.046 s with this branch. Same health checks on every boot, s2idle 1/1, Stage 2 hash matched after every restore.Still to run
The same trial on the Neo:
board_init_f->board_init_rfrom/proc/device-tree/bootstage, before and after, 3 boots each. The rest of the boot is unchanged (NVMe, keyboard, GRUB, Linux, s2idle). Expected:board_init_funder 0.1 s on all four.Where the uncached accesses were (QEMU, base, J316s tree)
Per initcall, data accesses between U-Boot entry and
relocate_code():initf_dm()(pre-relocation DM scan + its event)dram_init()serial_init()ctxr_exec_relocate_uboot()By function, 73% are
fdt_offset_ptr()/fdt_next_tag(): tag-by-tag walks. Divided by the measured times, one uncached access costs about 100 ns on the T8112, 200-260 ns on the T8140/T6040 and 400-475 ns on the T6000. That is why the M1 Pro is the slowest, although its tree is only 1.4x the M2's work.The counts come from a TCG plugin that counts executed instructions and memory accesses per PC between two PCs, with phases switched at the entry of each
board_init_f()initcall.Why the caches go off and on again
dram_init()andenable_caches()rebuild the tables in place, so they turn the caches off first (dcache_disable()cleans by set/way). This is the same sequenceenable_caches()on i.MX8M uses after its early tables. Set/way clean is also what the boot loader does at the handoff to U-Boot, and what U-Boot does beforebooti. While the caches are on before relocation,relocate_code()cleans the new copy to the point of coherency by VA and invalidates the I-cache (its existing D-cache-on path).