Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

Wi-Fi: ath11k's own files run on the phone; the firmware's first message over a copy engine

vcvkk39b83c4d2a60
diff --git a/prebuilt/SHA256SUMS b/prebuilt/SHA256SUMS
index 17beffa..3d0df26 100644
--- a/prebuilt/SHA256SUMS
+++ b/prebuilt/SHA256SUMS
@@ -1,6 +1,6 @@
-Built 2026-10-13 18:03 from this tree.
+Built 2026-10-13 18:51 from this tree.
-da06e84cf381e3b8f9a8b8d4e06ff56bf99fb6d9b2b52d679448d9bc8377e3ab prebuilt/mach.development.bcm2837
+fdf7275d96b6b717c33d8532fa61cc70a617a8e98e10c6508d64be6105c850b5 prebuilt/mach.development.bcm2837
1332a598d2dec00a1d8e930044e4f00a12ad44c6e5e50eecf4b7fe7fae803bf2 prebuilt/loader.efi
d2a35a4a1d6658ffb3696914f727661b52c9333de8a3af8a31642b8023ce03ac prebuilt/userland/child
1a413befd511384052e1962c1394adce20f718e1f2657ce63ab09828ebc4bd1b prebuilt/userland/cpu_test
diff --git a/prebuilt/mach.development.bcm2837 b/prebuilt/mach.development.bcm2837
index 1717236..2801b8e 100755
Binary files a/prebuilt/mach.development.bcm2837 and b/prebuilt/mach.development.bcm2837 differ
diff --git a/xnu/files/bsd/dev/redwood_lx.c b/xnu/files/bsd/dev/redwood_lx.c
new file mode 100644
index 0000000..7d069f1
--- /dev/null
+++ b/xnu/files/bsd/dev/redwood_lx.c
@@ -0,0 +1,237 @@
+/*
+ * Darwin-on-redwood: the kernel's side of the Wi-Fi driver's object.
+ *
+ * The driver of the phone's radio is Linux's ath11k, compiled unmodified outside this tree on a
+ * layer that gives it the kernel interfaces it expects, and tied into one object that the
+ * kernel links when it is there (tools/mk-ath11k.sh and wifi/lx in the repository; the kernel
+ * is built with that object when xnu-build.sh finds it). The object shows one name, lx_start(),
+ * and asks for the calls of sys/redwood_lx.h, which are here: memory, time, threads, one lock
+ * to sleep under, the kernel's options. What it asks of the radio itself -- its firmware's
+ * files, its view of memory, the processor its firmware runs on -- is redwood_wifi.c's to
+ * answer, which knows the phone.
+ *
+ * A kernel built without the object has the lx_start() at the end of this file, which says so.
+ */
+#include <kern/clock.h>
+#include <kern/kalloc.h>
+#include <kern/locks.h>
+#include <kern/sched_prim.h>
+#include <kern/thread.h>
+#include <libkern/libkern.h>
+#include <mach/mach_types.h>
+#include <mach/vm_param.h>
+#include <pexpert/pexpert.h>
+#include <sys/errno.h>
+#include <sys/redwood_input.h>
+#include <sys/redwood_lx.h>
+#include <sys/systm.h>
+#include <vm/memory_types.h>
+#include <vm/vm_kern.h>
+
+extern vm_offset_t ml_io_map_unmappable(vm_offset_t phys_addr, vm_size_t size, unsigned int flags);
+extern uint64_t kvtophys(vm_offset_t va); /* pmap_paddr_t */
+extern kern_return_t thread_terminate(thread_t thread);
+extern void thread_set_thread_name(thread_t thread, const char *name);
+
+#define say redwood_wifi_say
+
+static LCK_GRP_DECLARE(lx_lock_group, "redwood-lx");
+static LCK_MTX_DECLARE(lx_lock, &lx_lock_group);
+
+/* ---- memory ------------------------------------------------------------------------------------------ */
+
+void *
+lx_os_alloc(unsigned long size)
+{
+ return kalloc_data(size, Z_WAITOK | Z_ZERO);
+}
+
+void
+lx_os_free(void *memory, unsigned long size)
+{
+ kfree_data(memory, size);
+}
+
+/*
+ * In one physical piece, so that it can be mapped a second time without this processor's cache
+ * in between (as redwood_adreno.c does with what it shares with the GPU); the first, cached
+ * view is written out and never used again.
+ */
+void *
+lx_os_radio_memory(unsigned long size, unsigned long long *address)
+{
+ vm_offset_t cached = 0, uncached;
+ uint64_t physical;
+
+ size = round_page(size);
+ if (kmem_alloc_contig(kernel_map, &cached, size, 0, 0, 0, KMA_KOBJECT | KMA_ZERO | KMA_DATA, VM_KERN_MEMORY_IOKIT) !=
+ KERN_SUCCESS) {
+ say("lx: no %lu bytes of memory in one piece\n", size);
+ return NULL;
+ }
+ physical = kvtophys(cached);
+ bzero((void *)cached, size);
+ for (vm_offset_t line = cached; line < cached + size; line += 64) {
+ __asm__ volatile ("dc civac, %0" : : "r" (line));
+ }
+ __asm__ volatile ("dsb sy");
+ uncached = ml_io_map_unmappable(physical, size, VM_WIMG_WCOMB);
+ if (uncached == 0 || !redwood_wifi_radio_map(physical, size, address)) {
+ return NULL;
+ }
+ return (void *)uncached;
+}
+
+void *
+lx_os_map_registers(unsigned long long physical, unsigned long size)
+{
+ unsigned long long page = trunc_page(physical);
+ vm_offset_t mapped = ml_io_map_unmappable(page, round_page(physical + size) - page, VM_WIMG_IO);
+
+ return mapped == 0 ? NULL : (void *)(mapped + (physical - page));
+}
+
+/* ---- saying things ----------------------------------------------------------------------------------- */
+
+void
+lx_os_print(const char *text)
+{
+ say("%s", text);
+}
+
+void
+lx_os_panic(const char *text)
+{
+ panic("%s", text);
+}
+
+/* ---- time, threads, sleeping --------------------------------------------------------------------------- */
+
+unsigned long long
+lx_os_nanoseconds(void)
+{
+ uint64_t nanoseconds;
+
+ absolutetime_to_nanoseconds(mach_absolute_time(), &nanoseconds);
+ return nanoseconds;
+}
+
+void *
+lx_os_thread_self(void)
+{
+ return current_thread();
+}
+
+struct lx_thread {
+ void (*function)(void *);
+ void *argument;
+};
+
+static void
+lx_thread_runs(void *parameter, __unused wait_result_t result)
+{
+ struct lx_thread start = *(struct lx_thread *)parameter;
+
+ kfree_type(struct lx_thread, parameter);
+ start.function(start.argument);
+ thread_terminate(current_thread());
+}
+
+int
+lx_os_thread(const char *name, void (*function)(void *), void *argument)
+{
+ struct lx_thread *start = kalloc_type(struct lx_thread, Z_WAITOK | Z_ZERO);
+ thread_t thread;
+
+ if (start == NULL) {
+ return ENOMEM;
+ }
+ start->function = function;
+ start->argument = argument;
+ if (kernel_thread_start(lx_thread_runs, start, &thread) != KERN_SUCCESS) {
+ kfree_type(struct lx_thread, start);
+ return EAGAIN;
+ }
+ thread_set_thread_name(thread, name);
+ thread_deallocate(thread);
+ return 0;
+}
+
+void
+lx_os_lock(void)
+{
+ lck_mtx_lock(&lx_lock);
+}
+
+void
+lx_os_unlock(void)
+{
+ lck_mtx_unlock(&lx_lock);
+}
+
+int
+lx_os_sleep(const void *address, unsigned long long nanoseconds)
+{
+ uint64_t deadline;
+
+ if (nanoseconds == 0) {
+ return lck_mtx_sleep(&lx_lock, LCK_SLEEP_DEFAULT, (event_t)address, THREAD_UNINT) == THREAD_TIMED_OUT;
+ }
+ nanoseconds_to_absolutetime(nanoseconds, &deadline);
+ deadline += mach_absolute_time();
+ return lck_mtx_sleep_deadline(&lx_lock, LCK_SLEEP_DEFAULT, (event_t)address, THREAD_UNINT, deadline) == THREAD_TIMED_OUT;
+}
+
+void
+lx_os_wakeup(const void *address)
+{
+ thread_wakeup((event_t)address);
+}
+
+/* ---- the rest of the machine ------------------------------------------------------------------------- */
+
+unsigned
+lx_os_option(const char *name)
+{
+ uint32_t value = 0;
+
+ PE_parse_boot_argn(name, &value, sizeof(value));
+ return value;
+}
+
+int
+lx_os_firmware(const char *name, const void **data, unsigned long *size)
+{
+ uint64_t length = 0;
+ const uint8_t *file = redwood_wifi_driver_file(name, &length);
+
+ if (file == NULL) {
+ return ENOENT;
+ }
+ *data = file;
+ *size = (unsigned long)length;
+ return 0;
+}
+
+int
+lx_os_rproc_boot(void)
+{
+ return redwood_wifi_dsp_is_up() ? 0 : ENXIO;
+}
+
+/* ---- the driver ---------------------------------------------------------------------------------------- */
+
+/* The object has its own; this is the one of a kernel built without it. */
+__attribute__((weak)) int
+lx_start(void)
+{
+ say("lx: THE DRIVER IS NOT BUILT IN: its object was not there when this kernel was built (tools/mk-ath11k.sh, then "
+ "build the kernel again)\n");
+ return ENXIO;
+}
+
+int
+redwood_lx_start(void)
+{
+ return lx_start();
+}
diff --git a/xnu/files/bsd/dev/redwood_wifi.c b/xnu/files/bsd/dev/redwood_wifi.c
index 45fc9a9..43719ad 100644
--- a/xnu/files/bsd/dev/redwood_wifi.c
+++ b/xnu/files/bsd/dev/redwood_wifi.c
@@ -102,6 +102,12 @@
* wifi_return=1 when all is over, go back to the loader instead of starting the system
* wifi_smmu=1 before any step: read what the boot chain has set up in the SMMU for the
* radio's stream (smmu_look() below; nothing is written)
+ * wifi_smmu=2 ... and give that stream a context of the SMMU with page tables of ours,
+ * so that the radio can be shown memory (radio_space_on() below: THE
+ * SMMU IS WRITTEN); taken back when the DSP is stopped
+ * wifi_lx=1 step 15 is ath11k's own instead of the stand-in (redwood_lx.c), with
+ * the radio's registers neither read nor written; 2: for real (which
+ * wants wifi_smmu=2: the radio is given addresses of its rings)
* The system does not go on starting until all of this is over, so that the end of the log is
* ours.
*
@@ -1626,6 +1632,8 @@ scm(const char *what, uint64_t function, uint64_t arguments, uint64_t first, uin
* (it takes the image's memory back, cleared), and the always-on processor to know that the
* image is gone: it was told of one by us, or by the kernel that left the DSP running.
*/
+static void radio_space_off(void);
+
static bool
dsp_shutdown(const char *why)
{
@@ -1648,6 +1656,7 @@ dsp_shutdown(const char *why)
doorbells();
dsp_started = false;
mailbox_send("{class: image, res: load_state, name: wpss, val: off}");
+ radio_space_off(); /* (nobody is left to use the radio's stream) */
return true;
}
@@ -3102,12 +3111,53 @@ redwood_wifi_file(const char *name, uint64_t *size)
return image_file(name, size);
}
+static void radio_space_faults(void);
+
+/*
+ * With wifi_lx: ath11k itself in place of the stand-in. Its qmi.c has the exchange, core.c asks
+ * for mission mode with the configuration ce.c and hal.c work out (the shadow registers), has
+ * them set the copy engines' rings up in the radio's registers and waits for the firmware's
+ * first message over a copy engine; how far the parts built so far go from there, and what is
+ * in their place meanwhile, is wifi/lx/src/lx_bus.c's to say. wifi_lx=2 IS WHERE THE RADIO'S
+ * REGISTERS ARE WRITTEN.
+ *
+ * What the phone said (13 October, ath11k of Linux 7.0.14: core.c, hal.c, qmi.c, ce.c, hw.c,
+ * fw.c): with wifi_lx=1 the driver's own exchange runs to the end in 9 s -- capabilities, the
+ * regulatory database in 4 pieces and the board's file in 10, cold-boot calibration (7 s),
+ * "ready", the configuration with the 30 shadow registers the driver works out, mission mode
+ * -- and the mode "off" when the driver winds down. With wifi_lx=2 and wifi_smmu=2, 100 words
+ * of ring set-up go into the registers of copy engines 0, 1, 2, 3, 4, 5 and 7 without an
+ * abort, 510 buffers are posted on each of the three receiving rings, and 9 ms later the
+ * firmware's first message is in a buffer of copy engine 2: HTC's "ready" (2 credits of 2176
+ * bytes, 5 endpoints). The SMMU records no fault. No interrupt is involved: the rings are
+ * looked at every millisecond.
+ */
+static bool
+step_driver(uint32_t how)
+{
+ int error;
+
+ announce(how >= 2 ? "hand the radio to ath11k itself: its exchange with the firmware up to mission mode, then THE RADIO'S "
+ "REGISTERS ARE WRITTEN, by ce.c and hal.c: the copy engines' rings (every write is said as it is made)" :
+ "hand the firmware to ath11k itself: its own exchange up to mission mode; the radio's registers are neither read "
+ "nor written (wifi_lx=1), and nothing is written but datagrams");
+ error = redwood_lx_start();
+ radio_space_faults();
+ printf("wifi: %u: the driver's start returns %d\n", step, error);
+ return error == 0;
+}
+
static bool
step_exchange(void)
{
+ uint32_t driver = 0;
+
if (!glink.open) {
return false;
}
+ if (PE_parse_boot_argn("wifi_lx", &driver, sizeof(driver)) && driver) {
+ return step_driver(driver);
+ }
announce("have the exchange a WCN6750's driver has with the Wi-Fi firmware's service before it touches the radio, by "
"hand in ath11k's order (redwood_wlfw.c): indications, capabilities both ways, the regulatory database and the "
"board's data; nothing is written but datagrams");
@@ -3280,7 +3330,8 @@ step_qrtr(void)
* its stream match registers cover that stream. Nothing is written.
*/
static struct block apps_smmu = { "apps smmu", 0x15000000, 0x2000 };
-#define SMMU_SCR0 0x000 /* bit 0: the whole SMMU off; bit 10: a stream nobody matches faults */
+#define SMMU_SCR0 0x000 /* bit 0: the whole SMMU off; bit 10: a stream nobody matches faults; bit 3:
+ * streams are numbered with 16 bits */
#define SMMU_IDR0 0x020 /* [7:0] stream match registers */
#define SMMU_IDR1 0x024 /* bit 31: 64K pages, [30:28] log2(pages)-1, [7:0] translation contexts */
#define SMMU_SMR(n) (0x800 + 4 * (n)) /* bit 31 valid, [30:16] mask, [14:0] stream */
@@ -3306,8 +3357,8 @@ smmu_look(void)
matches, idr1 & 0xff, scr0 & 1u << 10 ? "faults" : "passes untranslated");
for (uint32_t n = 0; n < matches && 0x800 + 4 * n < 0xc00; n++) {
uint32_t smr = rd(apps_smmu.base + SMMU_SMR(n)), s2cr = rd(apps_smmu.base + SMMU_S2CR(n));
- /* with 16-bit stream numbers switched on (scr0 bit 15) "valid" moves from bit 31 of the one to bit 10 of the other */
- uint32_t wide = scr0 & 1u << 15, stream = smr & (wide ? 0xffff : 0x7fff), mask = (smr >> 16) & (wide ? 0xffff : 0x7fff);
+ /* with 16-bit stream numbers switched on "valid" moves from bit 31 of the one to bit 10 of the other */
+ uint32_t wide = scr0 & 1u << 3, stream = smr & (wide ? 0xffff : 0x7fff), mask = (smr >> 16) & (wide ? 0xffff : 0x7fff);
if (!(wide ? s2cr & 1u << 10 : smr >> 31)) {
continue;
@@ -3324,6 +3375,373 @@ smmu_look(void)
covering ? "" : ": THE BOOT CHAIN HAS SET NOTHING UP FOR THE RADIO'S STREAM");
}
+/* ---- beside the steps: the radio's view of memory (boot-arg wifi_smmu=2) ----------------------------- */
+
+/*
+ * The boot chain sets nothing up for the radio's stream, and a stream nobody matches faults
+ * (smmu_look()): before the radio can be given the address of a ring, the stream needs a match
+ * register, a translation context and page tables, as redwood_adreno.c makes them in the GPU's
+ * own SMMU -- there are the formats and the order of programming, and what the hypervisor does
+ * with the global registers. Here they are found rather than known: the first match register
+ * that is not valid, and the lowest context that no valid match register sends a stream
+ * through and that is not switched on (Linux, too, takes the lowest free ones, on top of what
+ * the boot chain left: dt "qcom,skip-init").
+ *
+ * The radio's addresses are where Android's tree puts them (dt: qcom,iommu-dma-addr-pool of
+ * qcom,wcn6750: 256 MB from 0xa0000000), handed out upwards and never taken back; its faults
+ * are recorded and do not stop it (dt: qcom,iommu-faults "stall-disable", "HUPCF"), and nobody
+ * takes their interrupt: radio_space_faults() looks.
+ *
+ * Everything written is put back when the DSP has been stopped, and on the way back to the
+ * loader after a panic (radio_space_off()).
+ *
+ * What the phone said (13 October): the hypervisor grants it. The boot chain uses match
+ * registers 0 to 7 and contexts 0 to 6; register 8 and context 7 (at 0x15087000) were taken,
+ * everything read back as written (but two bits that the SMMU does not keep), and the radio
+ * then read and wrote the memory it was shown without a fault.
+ */
+#define RADIO_ADDRESSES 0xa0000000ull
+#define RADIO_ADDRESSES_SIZE 0x10000000u
+#define RADIO_TABLES 16 /* of 4K: the root, one of the second level, and 2 MB of addresses each other */
+#define RADIO_PTE (3ull | 1ull << 6 | 2ull << 8 | 1ull << 10 | 1ull << 11) /* a page; for requests of either
+ * privilege; shared, accessed, not
+ * global: Linux's for a device */
+#define PTE_ADDRESS 0x0000fffffffff000ull
+#define SMMU_SGFSR 0x048 /* what the SMMU as a whole has refused ... */
+#define SMMU_SGFSYNR0 0x050
+#define SMMU_SGFSYNR1 0x054 /* ... and of which stream */
+#define SMMU_CBAR(n) (4 * (n)) /* in the second page; [17:16] kind of context */
+#define SMMU_CBAR_STAGE_1 (1u << 16 | 3u << 8 | 0xfu << 12) /* our tables alone decide; weakest defaults */
+#define SMMU_CBA2R(n) (0x800 + 4 * (n))
+#define SMMU_CBA2R_64_BIT 1
+#define SMMU_CB_SCTLR 0x00 /* a context's own page: bit 0 = translation on */
+#define SMMU_CB_TCR2 0x10
+#define SMMU_CB_TTBR0 0x20
+#define SMMU_CB_TTBR1 0x28
+#define SMMU_CB_TCR 0x30
+#define SMMU_CB_MAIR0 0x38
+#define SMMU_CB_MAIR1 0x3c
+#define SMMU_CB_FSR 0x58
+#define SMMU_CB_FAR 0x60
+#define SMMU_CB_FSYNR0 0x68
+#define SMMU_CB_TLBIASID 0x610
+#define SMMU_CB_TLBSYNC 0x7f0
+#define SMMU_CB_TLBSTATUS 0x7f4
+/* on; faults recorded, no interrupt, and a fault does not hold up the requests after it */
+#define SMMU_SCTLR_ON (1u << 0 | 1u << 1 | 1u << 2 | 1u << 5 | 1u << 8 | 1u << 12)
+#define SMMU_TCR_39_BITS (25u | 1u << 8 | 1u << 10 | 3u << 12 | 1u << 23) /* one table of 4K pages, cached walks */
+#define SMMU_TCR2 (1u | 1u << 4 | 7u << 15) /* 36 bits of physical address, 16 of context tag */
+#define SMMU_MAIR0 0xf404ff44 /* Linux's: 0 uncached, 1 cached, 2 device */
+#define SMMU_FSR_ALL 0xc00001fe /* every kind of fault, and "several" */
+
+static struct {
+ uint32_t wanted; /* wifi_smmu */
+ bool found, on;
+ uint32_t page, pages; /* of the SMMU's registers: a page's size, and how many before the contexts' */
+ uint32_t match, context; /* the stream match register and the context taken for the radio */
+ uint32_t old_smr, old_s2cr, old_sctlr;
+ vm_offset_t tables; /* RADIO_TABLES of them, the root first */
+ uint32_t table_count;
+ uint64_t next; /* of the radio's addresses, the first not handed out (from RADIO_ADDRESSES) */
+} radio_space;
+static struct block apps_smmu_second = { "apps smmu", 0, 0x1000 }; /* the second page: a word per context */
+static struct block apps_smmu_context = { "apps smmu, the radio's context", 0, 0x1000 };
+
+static uint64_t *
+radio_table(uint32_t n)
+{
+ return (uint64_t *)(radio_space.tables + 4096 * n);
+}
+
+/* The table an entry of a higher table leads to; made if the entry is empty. */
+static uint64_t *
+radio_next(uint64_t *entry)
+{
+ uint64_t *table;
+
+ if (*entry & 1) {
+ for (uint32_t i = 0; i < radio_space.table_count; i++) {
+ if (kvtophys((vm_offset_t)radio_table(i)) == (*entry & PTE_ADDRESS)) {
+ return radio_table(i);
+ }
+ }
+ return NULL;
+ }
+ if (radio_space.table_count == RADIO_TABLES) {
+ return NULL;
+ }
+ table = radio_table(radio_space.table_count++);
+ *entry = kvtophys((vm_offset_t)table) | 3;
+ return table;
+}
+
+/* Our memory at `physical` behind the radio's addresses from `address` on; the SMMU reads the tables from memory. */
+static bool
+radio_enter(uint64_t address, uint64_t physical, uint64_t size)
+{
+ if (radio_space.tables == 0) {
+ if (kmem_alloc(kernel_map, &radio_space.tables, round_page(4096 * RADIO_TABLES), KMA_KOBJECT | KMA_ZERO | KMA_DATA,
+ VM_KERN_MEMORY_IOKIT) != KERN_SUCCESS) {
+ return false;
+ }
+ radio_space.table_count = 1;
+ }
+ for (uint64_t offset = 0; offset < size; offset += 4096) {
+ uint64_t at = address + offset, *level2 = radio_next(&radio_table(0)[at >> 30 & 0x1ff]);
+ uint64_t *level3 = level2 == NULL ? NULL : radio_next(&level2[at >> 21 & 0x1ff]);
+
+ if (level3 == NULL) {
+ printf("wifi: %u: no page table left for the radio's %#llx\n", step, at);
+ return false;
+ }
+ level3[at >> 12 & 0x1ff] = (physical + offset) | RADIO_PTE;
+ }
+ for (vm_offset_t line = radio_space.tables; line < radio_space.tables + 4096 * RADIO_TABLES; line += 64) {
+ __asm__ volatile ("dc civac, %0" : : "r" (line));
+ }
+ settle();
+ return true;
+}
+
+/* Which match register and which context are free; nothing is written. */
+static bool
+radio_space_find(void)
+{
+ bool used[256] = { false };
+ uint32_t scr0 = get(&apps_smmu, "scr0", SMMU_SCR0), idr0 = get(&apps_smmu, "idr0", SMMU_IDR0);
+ uint32_t idr1 = get(&apps_smmu, "idr1", SMMU_IDR1), matches = idr0 & 0xff, contexts = idr1 & 0xff;
+
+ if (scr0 & 1u << 3) {
+ printf("wifi: %u: this SMMU numbers its streams with 16 bits, which nothing here is written for\n", step);
+ return false;
+ }
+ radio_space.page = idr1 >> 31 ? 0x10000 : 0x1000;
+ radio_space.pages = 2u << (idr1 >> 28 & 7);
+ if (2 * radio_space.pages * radio_space.page != 0x100000) { /* dt: "reg" of apps-smmu */
+ printf("wifi: %u: the SMMU says it has 2 x %u pages of %#x bytes, which is not the megabyte the device tree gives "
+ "it: its contexts are not looked for\n", step, radio_space.pages, radio_space.page);
+ return false;
+ }
+ radio_space.match = ~0u;
+ for (uint32_t n = 0; n < matches && SMMU_SMR(n) < 0xc00; n++) {
+ uint32_t smr = rd(apps_smmu.base + SMMU_SMR(n)), s2cr = rd(apps_smmu.base + SMMU_S2CR(n));
+
+ if (smr >> 31) {
+ printf("wifi: %u: match register %u is in use: stream %#x mask %#x, s2cr %#x\n", step, n, smr & 0x7fff,
+ smr >> 16 & 0x7fff, s2cr);
+ used[s2cr & 0xff] = true;
+ } else if (radio_space.match == ~0u) {
+ radio_space.match = n;
+ radio_space.old_smr = smr;
+ radio_space.old_s2cr = s2cr;
+ }
+ }
+ for (radio_space.context = 0; radio_space.context < contexts && used[radio_space.context]; radio_space.context++) {
+ }
+ if (radio_space.match == ~0u || radio_space.context >= contexts) {
+ printf("wifi: %u: the SMMU has no match register or no context left\n", step);
+ return false;
+ }
+ apps_smmu_second.phys = apps_smmu.phys + radio_space.page;
+ apps_smmu_context.phys = apps_smmu.phys + (radio_space.pages + radio_space.context) * radio_space.page;
+ printf("wifi: %u: free: match register %u (smr %#x, s2cr %#x) and context %u, whose registers are at %#x; the SMMU's "
+ "pages are of %#x bytes, %u before the contexts'\n", step, radio_space.match, radio_space.old_smr,
+ radio_space.old_s2cr, radio_space.context, apps_smmu_context.phys, radio_space.page, radio_space.pages);
+ get(&apps_smmu_second, "cbar", SMMU_CBAR(radio_space.context));
+ get(&apps_smmu_second, "cba2r", SMMU_CBA2R(radio_space.context));
+ radio_space.old_sctlr = get(&apps_smmu_context, "sctlr", SMMU_CB_SCTLR);
+ get(&apps_smmu_context, "ttbr0, low", SMMU_CB_TTBR0);
+ get(&apps_smmu_context, "fsr", SMMU_CB_FSR);
+ if (radio_space.old_sctlr & 1) {
+ printf("wifi: %u: CONTEXT %u IS SWITCHED ON although no stream is sent through it: it is somebody's, and is left "
+ "alone\n", step, radio_space.context);
+ return false;
+ }
+ radio_space.found = true;
+ return true;
+}
+
+/* What the SMMU has recorded of requests of the radio's that it could not serve. */
+static void
+radio_space_faults(void)
+{
+ uint32_t status, global;
+
+ if (!radio_space.on) {
+ return;
+ }
+ status = rd(apps_smmu_context.base + SMMU_CB_FSR);
+ global = rd(apps_smmu.base + SMMU_SGFSR);
+ if ((status & SMMU_FSR_ALL) == 0 && global == 0) {
+ printf("wifi: %u: the SMMU has refused the radio nothing (context %u: fsr %#x; sgfsr 0)\n", step, radio_space.context,
+ status);
+ return;
+ }
+ printf("wifi: %u: THE SMMU HAS REFUSED A REQUEST: context %u: status %#x, address %#llx, syndrome %#x; the SMMU as a whole: "
+ "status %#x, syndromes %#x %#x (the second names the stream)\n", step, radio_space.context, status,
+ (uint64_t)rd(apps_smmu_context.base + SMMU_CB_FAR + 4) << 32 | rd(apps_smmu_context.base + SMMU_CB_FAR),
+ rd(apps_smmu_context.base + SMMU_CB_FSYNR0), global, rd(apps_smmu.base + SMMU_SGFSYNR0),
+ rd(apps_smmu.base + SMMU_SGFSYNR1));
+}
+
+/* The radio's stream through a context with our tables. */
+static bool
+radio_space_on(void)
+{
+ uint32_t smr = SMMU_SMR(radio_space.match), s2cr = SMMU_S2CR(radio_space.match), sees = 1u << 31 | RADIO_STREAM_MASK << 16 |
+ RADIO_STREAM;
+ uint64_t root = kvtophys(radio_space.tables) | (uint64_t)radio_space.context << 48; /* tagged with the context's number */
+
+ printf("wifi: %u: THE SMMU IS ABOUT TO BE WRITTEN: context %u gets page tables of ours (root %#llx) and is switched on, and "
+ "match register %u sends stream %#x mask %#x through it; before: smr %#x, s2cr %#x, the context's sctlr %#x\n", step,
+ radio_space.context, root & PTE_ADDRESS, radio_space.match, RADIO_STREAM, RADIO_STREAM_MASK, radio_space.old_smr,
+ radio_space.old_s2cr, radio_space.old_sctlr);
+ sleep_ms(pause_ms);
+ put(&apps_smmu_context, "sctlr", SMMU_CB_SCTLR, 0);
+ put(&apps_smmu_second, "cba2r", SMMU_CBA2R(radio_space.context), SMMU_CBA2R_64_BIT);
+ put(&apps_smmu_second, "cbar", SMMU_CBAR(radio_space.context), SMMU_CBAR_STAGE_1);
+ put(&apps_smmu_context, "tcr2", SMMU_CB_TCR2, SMMU_TCR2);
+ put(&apps_smmu_context, "tcr", SMMU_CB_TCR, SMMU_TCR_39_BITS);
+ put(&apps_smmu_context, "ttbr0, low", SMMU_CB_TTBR0, (uint32_t)root);
+ put(&apps_smmu_context, "ttbr0, high", SMMU_CB_TTBR0 + 4, (uint32_t)(root >> 32));
+ put(&apps_smmu_context, "ttbr1, low", SMMU_CB_TTBR1, 0);
+ put(&apps_smmu_context, "ttbr1, high", SMMU_CB_TTBR1 + 4, 0);
+ put(&apps_smmu_context, "mair0", SMMU_CB_MAIR0, SMMU_MAIR0);
+ put(&apps_smmu_context, "mair1", SMMU_CB_MAIR1, 0);
+ put(&apps_smmu_context, "fsr", SMMU_CB_FSR, SMMU_FSR_ALL);
+ put(&apps_smmu_context, "tlbiasid", SMMU_CB_TLBIASID, radio_space.context);
+ put(&apps_smmu_context, "tlbsync", SMMU_CB_TLBSYNC, 1);
+ for (unsigned ms = 0; rd(apps_smmu_context.base + SMMU_CB_TLBSTATUS) & 1; ms++) {
+ if (ms == 100) {
+ printf("wifi: %u: the SMMU does not finish forgetting old translations\n", step);
+ return false;
+ }
+ sleep_ms(1);
+ }
+ put(&apps_smmu_context, "sctlr", SMMU_CB_SCTLR, SMMU_SCTLR_ON);
+ radio_space.on = true; /* (from here on there is something to take back) */
+ put(&apps_smmu, "s2cr", s2cr, radio_space.context);
+ put(&apps_smmu, "smr", smr, sees);
+
+ /* the global ones are the hypervisor's to grant: see what it made of them */
+ if (get(&apps_smmu, "smr", smr) != sees || (get(&apps_smmu, "s2cr", s2cr) & 0x300ff) != radio_space.context ||
+ (get(&apps_smmu_second, "cbar", SMMU_CBAR(radio_space.context)) & 0x30000) != (SMMU_CBAR_STAGE_1 & 0x30000) ||
+ !(get(&apps_smmu_context, "sctlr", SMMU_CB_SCTLR) & 1) ||
+ get(&apps_smmu_context, "ttbr0, low", SMMU_CB_TTBR0) != (uint32_t)root ||
+ get(&apps_smmu_context, "ttbr0, high", SMMU_CB_TTBR0 + 4) != (uint32_t)(root >> 32)) {
+ printf("wifi: %u: THE SMMU DID NOT TAKE WHAT WAS WRITTEN: stream %#x is not going through context %u\n", step,
+ RADIO_STREAM, radio_space.context);
+ return false;
+ }
+ get(&apps_smmu_context, "tcr", SMMU_CB_TCR);
+ get(&apps_smmu_context, "tcr2", SMMU_CB_TCR2);
+ get(&apps_smmu_second, "cba2r", SMMU_CBA2R(radio_space.context));
+ get(&apps_smmu, "sgfsr", SMMU_SGFSR);
+ printf("wifi: %u: THE RADIO'S STREAM GOES THROUGH CONTEXT %u of the SMMU, with our page tables\n", step, radio_space.context);
+ radio_space_faults();
+ return true;
+}
+
+/* The stream goes nowhere again, and the context translates nothing: as they were found. */
+static void
+radio_space_off(void)
+{
+ if (!radio_space.on) {
+ return;
+ }
+ radio_space_faults();
+ printf("wifi: %u: the radio's stream and context %u of the SMMU are put back as they were found\n", step, radio_space.context);
+ put(&apps_smmu, "smr", SMMU_SMR(radio_space.match), radio_space.old_smr);
+ put(&apps_smmu, "s2cr", SMMU_S2CR(radio_space.match), radio_space.old_s2cr);
+ put(&apps_smmu_context, "sctlr", SMMU_CB_SCTLR, radio_space.old_sctlr);
+ get(&apps_smmu, "smr", SMMU_SMR(radio_space.match));
+ get(&apps_smmu, "s2cr", SMMU_S2CR(radio_space.match));
+ get(&apps_smmu_context, "sctlr", SMMU_CB_SCTLR);
+ radio_space.on = false;
+}
+
+/*
+ * For the driver (redwood_lx.c): memory of ours that the radio is to see, at the address
+ * returned. Without wifi_smmu=2 it has an address and the radio is shown nothing.
+ */
+bool
+redwood_wifi_radio_map(uint64_t physical, uint64_t size, unsigned long long *address)
+{
+ size = (size + 4095) & ~4095ull;
+ if (radio_space.next + size > RADIO_ADDRESSES_SIZE) {
+ return false;
+ }
+ *address = RADIO_ADDRESSES + radio_space.next;
+ radio_space.next += size;
+ if (radio_space.wanted != 2) {
+ printf("wifi: %u: THE RADIO IS NOT SHOWN the %llu KB of ours at %#llx that the driver will know as %#llx: that takes "
+ "a context of the SMMU (wifi_smmu=2)\n", step, size >> 10, physical, *address);
+ return true;
+ }
+ if ((!radio_space.found && !radio_space_find()) || !radio_enter(*address, physical, size)) {
+ return false;
+ }
+ printf("wifi: %u: the radio is to see %llu KB of ours (%#llx) at %#llx\n", step, size >> 10, physical, *address);
+ if (!radio_space.on) {
+ return radio_space_on();
+ }
+ put(&apps_smmu_context, "tlbiasid", SMMU_CB_TLBIASID, radio_space.context);
+ put(&apps_smmu_context, "tlbsync", SMMU_CB_TLBSYNC, 1);
+ return true;
+}
+
+/*
+ * wifi_smmu=2 in a run that does not get as far as the driver: the context is set up with empty
+ * tables, so that whatever the stream asked for would be refused as it is now, and taken back.
+ * This is the first write to this SMMU, and to see whether the hypervisor grants it.
+ */
+static void
+radio_space_try(void)
+{
+ bool as_found;
+
+ if (!radio_space_find() || !radio_enter(RADIO_ADDRESSES, 0, 0)) {
+ return;
+ }
+ as_found = radio_space_on();
+ sleep_ms(pause_ms);
+ radio_space_off();
+ printf("wifi: %u: %s\n", step, as_found ? "THE SMMU TOOK A CONTEXT FOR THE RADIO'S STREAM, and it has been taken back" :
+ "THE SMMU DID NOT TAKE A CONTEXT FOR THE RADIO'S STREAM");
+}
+
+/* ---- for the driver (redwood_lx.c): the firmware's processor, and files ------------------------------- */
+
+bool
+redwood_wifi_dsp_is_up(void)
+{
+ printf("wifi: %u: the driver asks for the firmware's processor: it %s\n", step, dsp_started && glink.open ? "has been up "
+ "since step 11, with the link of step 13" : "IS NOT UP");
+ return dsp_started && glink.open;
+}
+
+/*
+ * ath11k asks for files by the names Linux's firmware tree has ("ath11k/WCN6750/hw1.0/board.bin");
+ * this phone has what Android's driver loads (fw-wifi/, see userland/build.sh): its board's own
+ * data file and regulatory database, which are what the two names below stand for. The others
+ * it asks for first and can do without (a container of several boards' data, a table of the
+ * firmware's features) are not there.
+ */
+const uint8_t *
+redwood_wifi_driver_file(const char *name, uint64_t *size)
+{
+ static const char *const files[][2] = { { "board.bin", "bd_m20gl.elf" }, { "regdb.bin", "regdb_xiaomi.bin" } };
+ const char *last = strrchr(name, '/');
+
+ last = last == NULL ? name : last + 1;
+ for (unsigned i = 0; i < sizeof(files) / sizeof(files[0]); i++) {
+ if (strcmp(last, files[i][0]) == 0) {
+ return image_file(files[i][1], size);
+ }
+ }
+ return NULL;
+}
+
/* ---- all of it together ---------------------------------------------------------------------------- */
static bool(*const steps[])(void) = {
@@ -3382,7 +3800,14 @@ redwood_wifi_init(void)
return;
}
if (pe_arm_get_soc_base_phys() != REDWOOD_SOC_BASE) {
- printf("wifi: wifi=N is for the phone, and this is not it\n");
+ if (PE_parse_boot_argn("wifi_lx", &back, sizeof(back)) && back) {
+ /* a check of what is under the driver: it gets as far as finding no firmware to talk to */
+ printf("wifi: this is not the phone; with wifi_lx the driver is started all the same, for what it does before "
+ "it needs a radio\n");
+ printf("wifi: the driver's start returns %d\n", redwood_lx_start());
+ } else {
+ printf("wifi: wifi=N is for the phone, and this is not it\n");
+ }
return;
}
if (last_step > STEPS) {
@@ -3398,10 +3823,16 @@ redwood_wifi_init(void)
printf("wifi: steps up to %u; those from %u on are new and wait %u ms %s\n", last_step, new_from, pause_ms,
fine ? "before every write and call" : "after each announcement");
- if (PE_parse_boot_argn("wifi_smmu", &back, sizeof(back)) && back) {
+ if (PE_parse_boot_argn("wifi_smmu", &radio_space.wanted, sizeof(radio_space.wanted)) && radio_space.wanted) {
smmu_look();
+ if (radio_space.wanted == 2 && last_step < 15) {
+ uint32_t known_from = new_from;
+
+ new_from = 0; /* (this is new: it waits as a new step does) */
+ radio_space_try();
+ new_from = known_from;
+ }
}
- back = 0;
for (step = 1; step <= last_step; step++) {
if (!steps[step - 1]()) {
printf("wifi: step %u: FAILED; the steps after it are not attempted\n", step);
@@ -3421,6 +3852,9 @@ redwood_wifi_init(void)
dsp_shutdown("the steps are over");
}
back = 0;
+ if (!dsp_started) {
+ radio_space_off(); /* (if the DSP could not be shut down in order: all the same) */
+ }
lck_mtx_unlock(&dsp_lock);
printf("wifi: over after step %u; %s\n", done, last_write);
if (PE_parse_boot_argn("wifi_return", &back, sizeof(back)) && back) {
diff --git a/xnu/files/bsd/sys/redwood_input.h b/xnu/files/bsd/sys/redwood_input.h
index ed224a6..4f176ca 100644
--- a/xnu/files/bsd/sys/redwood_input.h
+++ b/xnu/files/bsd/sys/redwood_input.h
@@ -56,6 +56,11 @@ bool redwood_wlfw_exchange(uint32_t step); /* step
bool redwood_wlfw_registers(uint64_t *address, uint32_t *size, bool *mission); /* ... and what it learnt for step 16 */
void redwood_wlfw_stop(uint32_t step); /* ... and its end, before the DSP is stopped */
void redwood_wifi_quiesce(void); /* on the way back to the loader: the Wi-Fi DSP, if it still runs, shut down */
+/* ath11k's object and the kernel's side of it (bsd/dev/redwood_lx.c), and what that asks of the phone's radio */
+int redwood_lx_start(void); /* the driver brought up on the radio as far as it is built: 0 or an errno */
+bool redwood_wifi_dsp_is_up(void); /* the DSP that runs the radio's firmware has been started and has a link */
+const uint8_t *redwood_wifi_driver_file(const char *name, uint64_t *size); /* a file by the name Linux has for it */
+bool redwood_wifi_radio_map(uint64_t physical, uint64_t size, unsigned long long *address); /* memory shown to the radio */
/*
* The GPU kept on (boot-arg adreno_keep=1), for iokit/Kernel/RedwoodGPU.cpp: see "the GPU kept on"
diff --git a/xnu/files/bsd/sys/redwood_lx.h b/xnu/files/bsd/sys/redwood_lx.h
new file mode 100644
index 0000000..4a700c4
--- /dev/null
+++ b/xnu/files/bsd/sys/redwood_lx.h
@@ -0,0 +1,61 @@
+/*
+ * The whole of what the Wi-Fi driver's object asks of the kernel it is linked into. That object
+ * (tools/mk-ath11k.sh in the repository: Linux's ath11k on FreeBSD's LinuxKPI headers and our
+ * wifi/lx) is compiled as FreeBSD kernel code, bsd/dev/redwood_lx.c on this side as XNU's;
+ * neither sees the other's headers, so nothing but C's own types crosses. Everything the
+ * driver's side knows of locks, timers, work queues and buffers is built in wifi/lx/src on the
+ * few calls here.
+ *
+ * Threads: the driver's code runs on threads made by lx_os_thread() and on the kernel's own
+ * threads that call into it; never in an interrupt.
+ */
+#ifndef _SYS_REDWOOD_LX_H_
+#define _SYS_REDWOOD_LX_H_
+
+/* ---- memory ---- */
+void *lx_os_alloc(unsigned long size); /* zeroed; NULL when there is none */
+void lx_os_free(void *memory, unsigned long size);
+/*
+ * Memory the radio itself reads and writes: whole pages, zeroed, which the radio knows from
+ * *address on (the machine says where: the radio sees memory through an SMMU). What either
+ * side writes the other sees at once: the processor's cache is not in between, which makes
+ * reading it slow. NULL when there is none; never given back.
+ */
+void *lx_os_radio_memory(unsigned long size, unsigned long long *address);
+void *lx_os_map_registers(unsigned long long physical, unsigned long size); /* never unmapped */
+
+/* ---- saying things ---- */
+void lx_os_print(const char *text);
+void lx_os_panic(const char *text) __attribute__((noreturn));
+
+/* ---- time, threads, sleeping ---- */
+unsigned long long lx_os_nanoseconds(void); /* since some moment; never goes back */
+void *lx_os_thread_self(void); /* a value that tells the kernel's threads apart */
+/* a thread that runs function(argument) and ends when that returns: 0, or an errno */
+int lx_os_thread(const char *name, void (*function)(void *), void *argument);
+/*
+ * One lock, and sleeping under it as with a condition variable: lx_os_sleep() is called with the
+ * lock held, gives it up while asleep and has it again when it returns -- 0 when woken by
+ * lx_os_wakeup() on the same address, 1 when the time (nanoseconds; 0: no limit) ran out.
+ * The lock is held for a few instructions at a time and never across a call out of the layer.
+ */
+void lx_os_lock(void);
+void lx_os_unlock(void);
+int lx_os_sleep(const void *address, unsigned long long nanoseconds);
+void lx_os_wakeup(const void *address); /* everyone asleep on it */
+
+/* ---- the rest of the machine ---- */
+/* a number given to the kernel when it was started ("wifi_lx=2"); 0 if it was not */
+unsigned lx_os_option(const char *name);
+/* a file the driver asks for by the name Linux has for it, in place: 0, or an errno (ENOENT) */
+int lx_os_firmware(const char *name, const void **data, unsigned long *size);
+/* the processor that runs the radio's firmware: started, and up when this returns; 0 or an errno */
+int lx_os_rproc_boot(void);
+
+/*
+ * ---- the one name the object shows the kernel: bring the driver up on the radio and go as far
+ * as what is built goes. 0, or an errno. Returns when that is over, or has failed.
+ */
+int lx_start(void);
+
+#endif
diff --git a/xnu/redwood.patch b/xnu/redwood.patch
index 4211b0c..f120b1e 100644
--- a/xnu/redwood.patch
+++ b/xnu/redwood.patch
@@ -46,10 +46,10 @@ index 222158a93918..68bb0f3e56bf 100755
######################################################################
# HISTORY
diff --git a/bsd/conf/files b/bsd/conf/files
-index 44274016a4cf..12254044a069 100644
+index 44274016a4cf..9666e7866eed 100644
--- a/bsd/conf/files
+++ b/bsd/conf/files
-@@ -109,6 +109,17 @@ bsd/dev/munge.c optional config_requires_u32_munging
+@@ -109,6 +109,18 @@ bsd/dev/munge.c optional config_requires_u32_munging
bsd/dev/unix_startup.c standard
bsd/dev/monotonic.c optional monotonic
@@ -64,10 +64,11 @@ index 44274016a4cf..12254044a069 100644
+bsd/dev/redwood_wifi.c standard
+bsd/dev/redwood_qmi.c standard
+bsd/dev/redwood_wlfw.c standard
++bsd/dev/redwood_lx.c standard
bsd/dev/dev_perfmon.c optional config_perfmon
bsd/libkern/crc32.c standard
-@@ -155,6 +166,10 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
+@@ -155,6 +167,10 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs
bsd/miscfs/mockfs/mockfs_fsnode.c optional mockfs
bsd/miscfs/mockfs/mockfs_vfsops.c optional mockfs
bsd/miscfs/mockfs/mockfs_vnops.c optional mockfs
@@ -78,7 +79,7 @@ index 44274016a4cf..12254044a069 100644
bsd/miscfs/routefs/routefs_ops.c optional routefs
-@@ -361,6 +376,7 @@ bsd/security/audit/audit_syscalls.c standard
+@@ -361,6 +377,7 @@ bsd/security/audit/audit_syscalls.c standard
bsd/security/audit/audit_worker.c optional config_audit
bsd/pthread/pthread_shims.c standard
@@ -86,7 +87,7 @@ index 44274016a4cf..12254044a069 100644
bsd/pthread/pthread_priority.c standard
bsd/pthread/pthread_workqueue.c standard
-@@ -383,6 +399,7 @@ bsd/kern/kern_debug.c standard
+@@ -383,6 +400,7 @@ bsd/kern/kern_debug.c standard
bsd/kern/kern_symfile.c standard
bsd/kern/kern_descrip.c standard
bsd/kern/kern_guarded.c standard
@@ -1512,7 +1513,7 @@ index bc39e19b3b8e..0a72a2d43908 100644
ifeq ($(origin HOST_CODESIGN_ALLOCATE),undefined)
export HOST_CODESIGN_ALLOCATE := $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find codesign_allocate)
diff --git a/makedefs/MakeInc.def b/makedefs/MakeInc.def
-index 5bd6f23dd10c..ac87cd36d158 100644
+index 5bd6f23dd10c..14e0f17409f5 100644
--- a/makedefs/MakeInc.def
+++ b/makedefs/MakeInc.def
@@ -60,7 +60,7 @@ MACHINE_FLAGS_ARM64_T8101 = -DARM64_BOARD_CONFIG_T8101 -mcpu=apple-a14
@@ -1524,12 +1525,15 @@ index 5bd6f23dd10c..ac87cd36d158 100644
#
-@@ -857,7 +857,7 @@ LDFILES_KERNEL_ONLY = $(TARGET)/all-kpi.exp $(TARGET)/all-alias.exp $(TARGET)/sy
+@@ -857,7 +857,10 @@ LDFILES_KERNEL_ONLY = $(TARGET)/all-kpi.exp $(TARGET)/all-alias.exp $(TARGET)/sy
# Default runtime libraries to be linked with the kernel
#
LD_KERNEL_LIBS = -lcc_kext
-LD_KERNEL_ARCHIVES = $(LDFLAGS_KERNEL_SDK) -lfirehose_kernel
+LD_KERNEL_ARCHIVES = $(LDFLAGS_KERNEL_SDK) -lfirehose_kernel -lpthread_kext
++# Darwin-on-redwood: objects made outside this tree, linked when the build is given any (the
++# Wi-Fi driver, see bsd/dev/redwood_lx.c)
++LD_KERNEL_ARCHIVES += $(REDWOOD_OBJECTS)
# Link opensource binary library
ifneq ($(filter T6000 T8101 VMAPPLE T6000 T8101 VMAPPLE,$(CURRENT_MACHINE_CONFIG)),)
LDFLAGS_KERNEL_ONLY += -rdynamic -Wl,-force_load,$(KDKROOT)/System/Library/KernelSupport/lib$(CURRENT_MACHINE_CONFIG).os.$(CURRENT_KERNEL_CONFIG).a