fix(raspi): the Pi 5's native path turns the MMU and caches on before any C; bounded mailbox; framebuffer address from dma-ranges

boards/BRINGUP.md R2/R3, FABRIC-3 SXXXIX.  The entry stub cleans the cache
over the image, builds an identity map (the image's GiB Normal, the rest
Device) and enables MMU and caches at EL2 (or EL1); rpi5_mmu.c makes the
devicetree's RAM Normal and the framebuffer uncached.  Mailbox waits give
up after one second; the buffer is cleaned around the exchange; the
framebuffer address goes through dma-ranges, not a Pi 4 mask.

New rpi5-rehearsal target runs the native image on QEMU virt at EL2.  It
found three more that would each have stopped the board: the PL011 shift
(mine, c9519173; the host test asserted it), the GIC address not carried
through /soc's ranges (new fdt_reg_address(), checked against the real
Pi 5 DTB), and no timer IRQ at EL2 (HCR_EL2.IMO/FMO/AMO).

Rehearsal: POST 1052 passed, same parity hash as UEFI.  Standard acceptance
on three ISAs: POST 1052, hash 0x82f4ae7cb2e2d424.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rajames
2026-10-14 12:58:35 -04:00
co-authored by Claude Opus 5.5
parent 4da370f492
commit 0e2c60dac3
23 changed files with 36835 additions and 85 deletions
+151 -22
View File
@@ -57,6 +57,7 @@
#include "starkernel/rpi5_mailbox.h"
#include "starkernel/rpi5_dtb.h"
#include "starkernel/fdt.h"
#define MBOX_OFF_READ 0x00u
#define MBOX_OFF_STATUS0 0x18u
@@ -108,6 +109,128 @@ static inline uint32_t mmio_read32(uint64_t addr)
return *(volatile uint32_t*) addr;
}
/* Every wait on the mailbox is bounded (2026-10-14): this runs before any
* console exists, so a VideoCore that never answers must cost a missing
* framebuffer, not a silent hang. One second, by the architectural
* counter -- there at any EL with no setup. */
static uint64_t counter_now(void)
{
uint64_t v;
__asm__ volatile("isb\n\tmrs %0, cntpct_el0" : "=r"(v));
return v;
}
static uint64_t counter_freq(void)
{
uint64_t v;
__asm__ volatile("mrs %0, cntfrq_el0" : "=r"(v));
return v ? v : 54000000u; /* the Pi 5's 54 MHz, if the firmware left it unset */
}
/* The VideoCore reads and writes RAM directly, not through the ARM's
* caches (the MMU and caches are on by now -- native_rpi5_entry.S): the
* request goes out to RAM before it is sent, and the cached copy is
* dropped before the answer is read. */
static void buffer_to_ram(void)
{
uint64_t a;
for (a = (uint64_t)(uintptr_t)g_mbox_buf; a < (uint64_t)(uintptr_t)(g_mbox_buf + 40); a += 16)
__asm__ volatile("dc civac, %0" :: "r"(a) : "memory");
__asm__ volatile("dsb sy" ::: "memory");
}
/* fdt helpers for the bus-address translation below. */
static uint32_t mb_be32(const unsigned char *p)
{
return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | p[3];
}
static uint64_t mb_cells(const unsigned char *p, uint32_t n)
{
return n == 2 ? ((uint64_t)mb_be32(p) << 32) | mb_be32(p + 4) : mb_be32(p);
}
static uint32_t mb_cell_prop(const void *dtb, const void *node, const char *name, uint32_t otherwise)
{
uint32_t len;
const void *v = fdt_find_prop_in_node(dtb, node, name, &len);
return (v && len == 4) ? mb_be32((const unsigned char *)v) : otherwise;
}
static int mb_compatible(const void *dtb, const void *node, const char *want)
{
uint32_t len, i = 0, wl = 0;
const char *c = (const char *)fdt_find_prop_in_node(dtb, node, "compatible", &len);
while (want[wl]) wl++;
while (c && i < len) {
uint32_t l = 0, k;
while (i + l < len && c[i + l]) l++;
if (l == wl) {
for (k = 0; k < l && c[i + k] == want[k]; k++) { }
if (k == l) return 1;
}
i += l + 1;
}
return 0;
}
/* The path from the root to the first node compatible with `want`:
* path[0] is the root, path[depth-1] the node. Depth-first. */
static int mb_find_path(const void *dtb, const void *node, const char *want,
const void **path, int depth, int max)
{
const void *child = (const void *)0;
if (depth >= max) return 0;
path[depth] = node;
if (mb_compatible(dtb, node, want)) return depth + 1;
while ((child = fdt_next_child_node(dtb, node, child)) != (const void *)0) {
int d = mb_find_path(dtb, child, want, path, depth + 1, max);
if (d) return d;
}
return 0;
}
/* The firmware's framebuffer address is an address on the firmware's own
* bus. It becomes an ARM physical address through the devicetree's
* dma-ranges, from the firmware node up to the root (2026-10-14): a node
* with none, or an empty one, maps one to one; otherwise the entry that
* holds the address moves it into the parent's space. On the Pi 5 the
* firmware node's is empty and /soc has none -- the address is already
* physical. (The 0x3FFFFFFF mask this replaced was the Pi 4's habit:
* there /soc's dma-ranges maps bus 0xC0000000 onto physical 0, which
* this handles as well.) Returns 0, or -1 if no entry holds it. */
static int bus_to_phys(const void *dtb, uint64_t bus, uint64_t *phys)
{
const void *path[12];
int depth, i;
const void *root = fdt_root_node(dtb);
if (!root) return -1;
depth = mb_find_path(dtb, root, "raspberrypi,bcm2835-firmware", path, 0, 12);
if (depth < 2) return -1;
for (i = depth - 1; i >= 1; i--) {
uint32_t len, ca, pa, sz, entry, k;
const unsigned char *r =
(const unsigned char *)fdt_find_prop_in_node(dtb, path[i], "dma-ranges", &len);
if (!r || len == 0) continue; /* one to one */
ca = mb_cell_prop(dtb, path[i], "#address-cells", 2);
sz = mb_cell_prop(dtb, path[i], "#size-cells", 1);
pa = mb_cell_prop(dtb, path[i - 1], "#address-cells", 2);
if (ca < 1 || ca > 2 || pa < 1 || pa > 2 || sz < 1 || sz > 2) return -1;
entry = (ca + pa + sz) * 4u;
for (k = 0; k + entry <= len; k += entry) {
uint64_t cb = mb_cells(r + k, ca);
uint64_t pb = mb_cells(r + k + ca * 4u, pa);
uint64_t n = mb_cells(r + k + (ca + pa) * 4u, sz);
if (bus >= cb && bus - cb < n) { bus = pb + (bus - cb); break; }
}
if (k + entry > len) return -1; /* no entry holds it */
}
*phys = bus;
return 0;
}
/* Appends one tag (id, value-buffer size in bytes, request size in
* bytes, then `nvalues` u32 value words) to g_mbox_buf starting at
* word index *idx, advancing *idx past it (including alignment
@@ -176,21 +299,31 @@ int rpi5_mailbox_get_framebuffer(const void* dtb, uint32_t width, uint32_t heigh
send_val = ((uint32_t) (uintptr_t) g_mbox_buf & ~0xFu) | MBOX_CH_PROPERTY;
while ((mmio_read32(base + MBOX_OFF_STATUS1) & MBOX_FULL) != 0)
{ /* spin until MBOX1 (send) has room */
}
mmio_write32(base + MBOX_OFF_WRITE, send_val);
for (;;)
buffer_to_ram();
{
while ((mmio_read32(base + MBOX_OFF_STATUS0) & MBOX_EMPTY) != 0)
{ /* spin until MBOX0 (receive) has a message */
uint64_t limit = counter_freq(); /* one second, for the whole exchange */
uint64_t t0 = counter_now();
while ((mmio_read32(base + MBOX_OFF_STATUS1) & MBOX_FULL) != 0)
{ /* MBOX1 (send) has no room yet */
if (counter_now() - t0 > limit) return -5;
}
mmio_write32(base + MBOX_OFF_WRITE, send_val);
for (;;)
{
while ((mmio_read32(base + MBOX_OFF_STATUS0) & MBOX_EMPTY) != 0)
{ /* MBOX0 (receive) has nothing yet */
if (counter_now() - t0 > limit) return -5;
}
if (mmio_read32(base + MBOX_OFF_READ) == send_val) break;
/* A message arrived, but not the one we sent (another channel's
* traffic) -- discard and keep waiting, per the wiki's own
* documented channel-matching convention. */
if (counter_now() - t0 > limit) return -5;
}
if (mmio_read32(base + MBOX_OFF_READ) == send_val) break;
/* A message arrived, but not the one we sent (another channel's
* traffic) -- discard and keep waiting, per the wiki's own
* documented channel-matching convention. */
}
buffer_to_ram(); /* drops the cached copy: what follows reads the answer */
if (g_mbox_buf[1] != MBOX_CODE_RESPONSE_OK) return -3; /* VC rejected the request */
@@ -215,16 +348,12 @@ int rpi5_mailbox_get_framebuffer(const void* dtb, uint32_t width, uint32_t heigh
if (fb_addr == 0 || fb_size == 0 || pitch_bytes == 0) return -4;
/* Allocate-buffer's own response address is a VC bus address,
* not necessarily the plain ARM-physical one -- masking the
* top bits to recover the ARM-physical alias is the documented
* convention on earlier Pi chips; kept here defensively even
* though the Pi-5-specific reference this file cites for the
* *send* side found no bus-alias bit in play there. Genuinely
* unverified which of these is correct for Pi 5's own
* allocate-buffer response specifically -- flagged, not
* guessed past this comment. */
out->base = (void*) (uintptr_t) (fb_addr & 0x3FFFFFFFu);
/* Allocate-buffer answers with an address on the firmware's bus:
* translated by the devicetree (bus_to_phys(), above), not masked
* by a rule from another chip. */
uint64_t fb_phys;
if (bus_to_phys(dtb, fb_addr, &fb_phys) != 0) return -6;
out->base = (void*) (uintptr_t) fb_phys;
out->size = fb_size;
out->width = width;
out->height = height;