Skip to content

arm: apple: turn the caches on before relocation - #58

Draft
RyanTheTide wants to merge 1 commit into
aurora-wipfrom
bootrace/apple-early-caches
Draft

RyanTheTide wants to merge 1 commit into
aurora-wipfrom
bootrace/apple-early-caches

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

U-Boot runs board_init_f() with the MMU off: the boot loader hands over with it off and initr_caches() turns it on only after relocation. Every data access before that goes to DRAM uncached, and board_init_f() is mostly device tree walks (the driver model scan, dram_init(), the console lookup). That costs, on every boot (bootstage marks board_init_f -> board_init_r):

Mac SoC board_init_f
MacBook Pro 16" M1 Pro T6000 2.39-2.85 s
MacBook Pro 16" M4 Pro T6040 1.31 s
MacBook Neo T8140 0.97 s (1.97 s before the dram_init() walk fix)
MacBook Air M2 T8112 0.43 s

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()):

  • The first tables map the SoC's I/O regions (taken from the SoC's own map) and only what U-Boot already uses: its image with the early stack, gd and the 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, which setup_dest_addr(), the reservations and ctxr_exec_relocate_uboot() all work in.
  • 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 does today.

The SoC table lookup moves out of build_mem_map() into apple_soc_mem_map(). The early tables are in .data (.bss is not usable before relocation) and as large as get_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 as boot/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_EL2 is 0x1005 (M, C, I) at dm_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) U-Boot hands off to GRUB (efi_install_fdt, s since reset)
    base (aurora-wip 25cbb0f) 2.634 / 2.873 / 2.634 20.307 / 20.517 / 20.294
    this branch 0.085 / 0.085 / 0.085 17.755 / 17.760 / 17.775

    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_r is unchanged (0.62 s).

  • J616s (T6040), hardware, 3 + 3 boots, same procedure, production kernel 7.3.0-aurora-fleet75: board_init_f 1.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_f 0.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_r from /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_f under 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():

Phase Accesses
initf_dm() (pre-relocation DM scan + its event) 4.45 M
dram_init() 0.89 M
serial_init() 0.37 M
ctxr_exec_relocate_uboot() 0.25 M
everything else < 0.05 M

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() and enable_caches() rebuild the tables in place, so they turn the caches off first (dcache_disable() cleans by set/way). This is the same sequence enable_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 before booti. 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).

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]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Blockers: 1.

  1. arch/arm/mach-apple/board.c:1427-1431: the fallback that the commit says "carries on as it does today" hangs at initr_caches(). apple_early_remap() left gd->arch.tlb_fillptr pointing into apple_early_tlb. The fallback calls dcache_disable() and returns, without clearing it. Then:

    • arm_reserve_mmu() (arch/arm/lib/cache.c:145-151) moves tlb_addr to the new, unwritten reservation but does not touch tlb_fillptr.
    • After relocation, enable_caches() (:1437-1443) clears tlb_fillptr only when dcache_status() is true, and it isn't on this path.
    • dcache_enable() -> mmu_setup() then skips setup_all_pgtables() because tlb_fillptr is non-zero (arch/arm/cpu/armv8/cache_v8.c:807, :890). It loads TTBR with the empty reservation and turns the MMU on.

    Any boot where U-Boot or its FDT is outside the first /memory range would stop there. Today that boot is just slow. Fix: set gd->arch.tlb_fillptr = 0 in the fallback, or clear it unconditionally in enable_caches(). Add a test that runs the fallback, e.g. QEMU with the FDT placed above the first bank.

What I checked and found sound:

  • arch_cpu_init() runs after initf_malloc(), so apple_early_top() covers gd and the pre-relocation malloc area.
  • The CTXR relocation target is bounded to [ram_base, start_addr_sp) (boot/ctxr_exec_window.c:152), so it lies in the mapped first range.
  • The I/O entries are the ones the post-relocation map already uses.
  • An early map that doesn't fit apple_early_map[32] just leaves the caches off.
  • The commit carries only a human Signed-off-by.

Nit: the 256 KiB table array in .data (:1315) grows the uncompressed payload and the relocation copy on every boot. Taking the space above the malloc area at run time would avoid both.

#54 rewrites the same tables and build_mem_map(), and #56 edits the T8152 selection. Whichever lands second needs a rebase.

HW row: on the fixed head, the same aura-try trial (3 boots, board_init_f from bootstage) on the Neo J700 and on one of J615 (T8122) or J713 (T8132), whose SoC maps this has not run with. Plus one fallback boot, or the QEMU fallback test. Each boot with 0 AVC, 0 failed units and no new kernel errors.

Assisted-by: LLM (Claude:Opus 5.5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant