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 at 55-70 Mbit/s: block ack, the radio's interrupts, the wide channel

vcvkk4c6b1843f7b3
diff --git a/prebuilt/SHA256SUMS b/prebuilt/SHA256SUMS
index b684c2b..e06707b 100644
--- a/prebuilt/SHA256SUMS
+++ b/prebuilt/SHA256SUMS
@@ -1,6 +1,6 @@
-Built 2026-10-14 20:51 from this tree.
+Built 2026-10-14 21:30 from this tree.
-351e89a9c0456a35daf1d567966dc002b665975cf6569e6742019de5344a9095 prebuilt/mach.development.bcm2837
+fa8df870cfe5dc82fe0e1d9c9d85111c37ccd044ad8f3623daebcf1e2105c46b 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 8d06291..ecc38f4 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
index 0cea23b..a6786fa 100644
--- a/xnu/files/bsd/dev/redwood_lx.c
+++ b/xnu/files/bsd/dev/redwood_lx.c
@@ -17,6 +17,7 @@
#include <kern/locks.h>
#include <kern/sched_prim.h>
#include <kern/thread.h>
+#include <os/atomic_private.h>
#include <libkern/libkern.h>
#include <mach/mach_types.h>
#include <mach/vm_param.h>
@@ -362,6 +363,164 @@ lx_os_net_input(const unsigned char *frame, unsigned long length)
}
}
+/* ---- the radio's interrupts ------------------------------------------------------------------------------- */
+
+extern kern_return_t gic_v3_spi_attach(uint32_t intid, bool edge, void (*handler)(void *context, uint32_t intid), void *context);
+extern void gic_v3_spi_mask(uint32_t intid);
+extern void gic_v3_spi_unmask(uint32_t intid);
+extern void gic_v3_spi_detach(uint32_t intid);
+extern uint64_t gic_v3_spi_count(uint32_t intid);
+
+#define LX_VECTORS 32
+
+static struct {
+ void (*function)(void *);
+ void *argument;
+ bool attached;
+} lx_vector[LX_VECTORS];
+static uint32_t lx_irq_first; /* the GIC's number of vector 0; 0: no interrupts */
+static uint32_t lx_irq_pending; /* the vectors that have fired and are not dealt with yet */
+static uint32_t lx_irq_running; /* the vectors whose function the thread is in */
+static thread_t lx_irq_thread;
+static LCK_MTX_DECLARE(lx_irq_lock, &lx_lock_group);
+
+/* In interrupt context: the vector masked, and the thread told. */
+static void
+lx_irq_arrived(__unused void *context, uint32_t intid)
+{
+ gic_v3_spi_mask(intid);
+ os_atomic_or(&lx_irq_pending, 1u << (intid - lx_irq_first), relaxed);
+ thread_wakeup((event_t)&lx_irq_pending);
+}
+
+static void
+lx_irq_runs(__unused void *parameter, __unused wait_result_t result)
+{
+ for (;;) {
+ uint32_t pending;
+
+ for (;;) {
+ assert_wait((event_t)&lx_irq_pending, THREAD_UNINT);
+ if ((pending = os_atomic_xchg(&lx_irq_pending, 0, acquire)) != 0) {
+ clear_wait(current_thread(), THREAD_AWAKENED);
+ break;
+ }
+ thread_block(THREAD_CONTINUE_NULL);
+ }
+ for (unsigned v = 0; v < LX_VECTORS; v++) {
+ if (!(pending & 1u << v)) {
+ continue;
+ }
+ lck_mtx_lock(&lx_irq_lock);
+ if (!lx_vector[v].attached) {
+ lck_mtx_unlock(&lx_irq_lock);
+ continue;
+ }
+ lx_irq_running |= 1u << v;
+ lck_mtx_unlock(&lx_irq_lock);
+ lx_vector[v].function(lx_vector[v].argument);
+ lck_mtx_lock(&lx_irq_lock);
+ lx_irq_running &= ~(1u << v);
+ lck_mtx_unlock(&lx_irq_lock);
+ thread_wakeup((event_t)&lx_irq_running);
+ }
+ }
+}
+
+int
+lx_os_irq_msi(unsigned long long *address, unsigned *data)
+{
+ uint32_t first = 0;
+
+ if (!redwood_wifi_radio_msi(address, data, &first)) {
+ return 0;
+ }
+ lx_irq_first = first;
+ return 1;
+}
+
+int
+lx_os_irq_attach(unsigned vector, void (*function)(void *), void *argument)
+{
+ kern_return_t kr;
+
+ if (vector >= LX_VECTORS || lx_irq_first == 0 || function == NULL) {
+ return EINVAL;
+ }
+ if (lx_irq_thread == THREAD_NULL) {
+ redwood_wifi_tell("lx: wifi_irq: THE GIC'S DISTRIBUTOR IS WRITTEN for the radio's interrupts: each vector's SPI (%u + v) edge-"
+ "triggered, group 1, routed to this processor, then enabled\n", lx_irq_first);
+ if (kernel_thread_start(lx_irq_runs, NULL, &lx_irq_thread) != KERN_SUCCESS) {
+ lx_irq_thread = THREAD_NULL;
+ return ENOMEM;
+ }
+ thread_set_thread_name(lx_irq_thread, "wifi-irq");
+ }
+ lck_mtx_lock(&lx_irq_lock);
+ lx_vector[vector].function = function;
+ lx_vector[vector].argument = argument;
+ lx_vector[vector].attached = true;
+ lck_mtx_unlock(&lx_irq_lock);
+ if ((kr = gic_v3_spi_attach(lx_irq_first + vector, true, lx_irq_arrived, NULL)) != KERN_SUCCESS) {
+ lck_mtx_lock(&lx_irq_lock);
+ lx_vector[vector].attached = false;
+ lck_mtx_unlock(&lx_irq_lock);
+ return kr == KERN_NAME_EXISTS ? EBUSY : EINVAL;
+ }
+ gic_v3_spi_mask(lx_irq_first + vector);
+ return 0;
+}
+
+void
+lx_os_irq_enable(unsigned vector)
+{
+ if (vector < LX_VECTORS && lx_vector[vector].attached) {
+ gic_v3_spi_unmask(lx_irq_first + vector);
+ }
+}
+
+void
+lx_os_irq_disable(unsigned vector)
+{
+ if (vector >= LX_VECTORS || !lx_vector[vector].attached) {
+ return;
+ }
+ gic_v3_spi_mask(lx_irq_first + vector);
+ if (current_thread() == lx_irq_thread) {
+ return; /* (from the vector's own function, or another's: nothing to wait for) */
+ }
+ lck_mtx_lock(&lx_irq_lock);
+ while (lx_irq_running & 1u << vector) {
+ lck_mtx_sleep(&lx_irq_lock, LCK_SLEEP_DEFAULT, (event_t)&lx_irq_running, THREAD_UNINT);
+ }
+ lck_mtx_unlock(&lx_irq_lock);
+}
+
+void
+lx_os_irq_detach_all(void)
+{
+ for (unsigned v = 0; v < LX_VECTORS; v++) {
+ if (lx_vector[v].attached) {
+ gic_v3_spi_detach(lx_irq_first + v);
+ }
+ }
+ lck_mtx_lock(&lx_irq_lock);
+ for (unsigned v = 0; v < LX_VECTORS; v++) {
+ lx_vector[v].attached = false;
+ }
+ while (lx_irq_running != 0 && current_thread() != lx_irq_thread) {
+ lck_mtx_sleep(&lx_irq_lock, LCK_SLEEP_DEFAULT, (event_t)&lx_irq_running, THREAD_UNINT);
+ }
+ lck_mtx_unlock(&lx_irq_lock);
+ os_atomic_store(&lx_irq_pending, 0, relaxed);
+}
+
+unsigned long long
+lx_os_irq_count(unsigned vector)
+{
+ return vector < LX_VECTORS && lx_irq_first != 0 ? gic_v3_spi_count(lx_irq_first + vector) : 0;
+}
+
/* ---- the station (sys/redwood_sta.h) --------------------------------------------------------------------- */
void
diff --git a/xnu/files/bsd/dev/redwood_wifi.c b/xnu/files/bsd/dev/redwood_wifi.c
index 052f966..ffb13ed 100644
--- a/xnu/files/bsd/dev/redwood_wifi.c
+++ b/xnu/files/bsd/dev/redwood_wifi.c
@@ -129,6 +129,12 @@
* wifi_station=1 with wifi_lx=2: the driver stays up and its station is the kernel's to
* ask (sys/redwood_sta.h); nothing is joined by the driver itself
* wifi_file=0 with wifi=1: the network of fw/network is not joined at the start
+ * wifi_irq=1 the radio's interrupts (MSI through its SMMU context to the GIC) instead
+ * of looking at its rings every millisecond (lx_bus.c); wifi=1 stands for it
+ * wifi_ba=1 block ack: the access point's requests to send aggregates are answered
+ * (lx_80211.c); without, the link carries about a fifth; wifi=1 stands for it
+ * wifi_wide=1 the channel as wide as the access point offers (40 or 80 MHz) instead of
+ * 20 MHz; wifi=1 stands for it
* wifi_lx_step=MS the driver's start says each stub it comes to and waits MS there: for
* a run that ends without a word (follow it with XL_FOLLOW=2)
* wifi_lx=1 step 15 is ath11k's own instead of the stand-in (redwood_lx.c), with
@@ -322,26 +328,32 @@ settle(void)
static bool whole; /* wifi=1: all of it, by itself (see the top of this file) */
static volatile bool whole_runs; /* ... and the thread that does it is at work */
-/* A number given to the kernel when it was started; with wifi=1 the ones that stands for are as if given. */
+/*
+ * A number given to the kernel when it was started; with wifi=1 the ones that stands for are as if
+ * given, unless given otherwise (wifi_irq=0: the rings looked at as before).
+ */
uint32_t
redwood_wifi_option(const char *name)
{
static const struct {
const char *name;
uint32_t value;
- } stands_for[] = { { "wifi_lx", 2 }, { "wifi_smmu", 2 }, { "wifi_station", 1 }, { "wifi_keep", 1 } };
+ } stands_for[] = { { "wifi_lx", 2 }, { "wifi_smmu", 2 }, { "wifi_station", 1 }, { "wifi_keep", 1 }, { "wifi_irq", 1 },
+ { "wifi_ba", 1 }, { "wifi_wide", 1 } };
uint32_t value = 0;
+ if (strcmp(name, "wifi_quiet") == 0) {
+ return quiet;
+ }
+ if (PE_parse_boot_argn(name, &value, sizeof(value))) {
+ return value;
+ }
for (unsigned i = 0; whole && i < sizeof(stands_for) / sizeof(stands_for[0]); i++) {
if (strcmp(name, stands_for[i].name) == 0) {
return stands_for[i].value;
}
}
- if (strcmp(name, "wifi_quiet") == 0) {
- return quiet;
- }
- PE_parse_boot_argn(name, &value, sizeof(value));
- return value;
+ return 0;
}
/* ---- register access ------------------------------------------------------------------------------- */
@@ -3587,6 +3599,16 @@ smmu_look(void)
#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 RADIO_PTE_DEVICE (RADIO_PTE | 2ull << 2 | 1ull << 53 | 1ull << 54) /* ... a device's registers: Linux's
+ * IOMMU_MMIO (MAIR entry 2), never
+ * executed */
+/*
+ * Where the radio raises an interrupt by writing (its "MSI"): resource "msi_addr" of the device
+ * tree's node for it, a register of the GIC that raises the SPI whose number is written; the
+ * node's interrupts are SPI 768..799, so vector v is the GIC's number 800 + v.
+ */
+#define RADIO_MSI_REGISTER 0x17a10040ull
+#define RADIO_FIRST_SPI 768u
#define PTE_ADDRESS 0x0000fffffffff000ull
#define SMMU_SGFSR 0x048 /* what the SMMU as a whole has refused ... */
#define SMMU_SGFSYNR0 0x050
@@ -3658,7 +3680,7 @@ radio_next(uint64_t *entry)
/* 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)
+radio_enter(uint64_t address, uint64_t physical, uint64_t size, uint64_t attributes)
{
if (radio_space.tables == 0) {
if (kmem_alloc(kernel_map, &radio_space.tables, round_page(4096 * RADIO_TABLES), KMA_KOBJECT | KMA_ZERO | KMA_DATA,
@@ -3675,7 +3697,7 @@ radio_enter(uint64_t address, uint64_t physical, uint64_t size)
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;
+ level3[at >> 12 & 0x1ff] = (physical + offset) | attributes;
}
for (vm_offset_t line = radio_space.tables; line < radio_space.tables + 4096 * RADIO_TABLES; line += 64) {
__asm__ volatile ("dc civac, %0" : : "r" (line));
@@ -3845,8 +3867,8 @@ radio_space_off(void)
* 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)
+static bool
+radio_map(uint64_t physical, uint64_t size, uint64_t attributes, unsigned long long *address)
{
size = (size + 4095) & ~4095ull;
if (radio_space.next + size > RADIO_ADDRESSES_SIZE) {
@@ -3862,7 +3884,7 @@ redwood_wifi_radio_map(uint64_t physical, uint64_t size, unsigned long long *add
"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)) {
+ if ((!radio_space.found && !radio_space_find()) || !radio_enter(*address, physical, size, attributes)) {
return false;
}
printf("wifi: %u: the radio is to see %llu KB of ours (%#llx) at %#llx\n", step, size >> 10, physical, *address);
@@ -3874,6 +3896,37 @@ redwood_wifi_radio_map(uint64_t physical, uint64_t size, unsigned long long *add
return true;
}
+bool
+redwood_wifi_radio_map(uint64_t physical, uint64_t size, unsigned long long *address)
+{
+ return radio_map(physical, size, RADIO_PTE, address);
+}
+
+/*
+ * What the radio is to write, and where, to raise its interrupt vector 0 (vector v: data + v): the
+ * GIC's register that raises an SPI, shown to the radio in its SMMU context as a device's page.
+ * Once; the GIC's number of the SPI of vector 0 goes to *first_spi.
+ */
+bool
+redwood_wifi_radio_msi(unsigned long long *address, uint32_t *data, uint32_t *first_spi)
+{
+ static unsigned long long page;
+
+ if (page == 0) {
+ tell("wifi: wifi_irq: THE RADIO'S SMMU CONTEXT GETS AN ENTRY for the GIC's register that raises an interrupt (%#llx), "
+ "as a device's page\n", RADIO_MSI_REGISTER);
+ if (!radio_map(RADIO_MSI_REGISTER & ~4095ull, 4096, RADIO_PTE_DEVICE, &page)) {
+ page = 0;
+ return false;
+ }
+ }
+ *address = page + (RADIO_MSI_REGISTER & 4095);
+ *data = RADIO_FIRST_SPI + 32;
+ *first_spi = RADIO_FIRST_SPI + 32;
+ tell("wifi: wifi_irq: the radio raises its interrupt vector v by writing %u + v at %#llx\n", *data, *address);
+ 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.
@@ -3884,7 +3937,7 @@ radio_space_try(void)
{
bool as_found;
- if (!radio_space_find() || !radio_enter(RADIO_ADDRESSES, 0, 0)) {
+ if (!radio_space_find() || !radio_enter(RADIO_ADDRESSES, 0, 0, RADIO_PTE)) {
return;
}
as_found = radio_space_on();
diff --git a/xnu/files/bsd/sys/redwood_input.h b/xnu/files/bsd/sys/redwood_input.h
index 2dc4c90..0c58317 100644
--- a/xnu/files/bsd/sys/redwood_input.h
+++ b/xnu/files/bsd/sys/redwood_input.h
@@ -67,6 +67,7 @@ void redwood_lx_net_early(void); /* ... and en0 made on the radio now, if
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 */
+bool redwood_wifi_radio_msi(unsigned long long *address, uint32_t *data, uint32_t *first_spi); /* where the radio raises its interrupts */
bool redwood_wifi_radio_address(uint8_t *address); /* the radio's own address (boot-arg wifi_mac), if one was given */
/* the network to join (fw/network of the small image): name and passphrase, buffer sizes in and lengths out */
bool redwood_wifi_network(uint8_t *name, unsigned *name_length, uint8_t *passphrase, unsigned *passphrase_length);
diff --git a/xnu/files/bsd/sys/redwood_lx.h b/xnu/files/bsd/sys/redwood_lx.h
index 1564128..ef79d87 100644
--- a/xnu/files/bsd/sys/redwood_lx.h
+++ b/xnu/files/bsd/sys/redwood_lx.h
@@ -69,6 +69,20 @@ void lx_os_net_attach(const unsigned char *address); /* there is a radio, and t
void lx_os_net_link(int up); /* frames can pass now (a network is joined) / no longer */
void lx_os_net_input(const unsigned char *frame, unsigned long length); /* an Ethernet frame has arrived */
+/*
+ * ---- the radio's interrupts (wifi_irq=1). The radio raises one by writing a number to an address
+ * (an "MSI": the GIC's register that raises the SPI of that number); vector v of its 32 is data + v.
+ * The driver's work for one is done in thread context, on the kernel's thread for them: the
+ * function given for a vector is called there after it has fired, with the vector masked until
+ * lx_os_irq_enable(). Nothing of the driver runs in an interrupt.
+ */
+int lx_os_irq_msi(unsigned long long *address, unsigned *data); /* where and what: 1, or 0 (no interrupts) */
+int lx_os_irq_attach(unsigned vector, void (*function)(void *), void *argument); /* masked to begin with; 0 or an errno */
+void lx_os_irq_enable(unsigned vector);
+void lx_os_irq_disable(unsigned vector); /* returns when its function is not running */
+void lx_os_irq_detach_all(void); /* returns when no function is running */
+unsigned long long lx_os_irq_count(unsigned vector); /* how many have arrived */
+
/* ---- the station: scanning and joining, for whoever asks the kernel (redwood_sta.h) ---- */
void lx_os_station(const struct redwood_sta_ops *ops); /* here it is, to be asked; NULL: no longer (returns when no call is under way) */
void lx_os_station_lost(int error); /* the network joined is gone by itself */
diff --git a/xnu/files/osfmk/arm64/gic_v3_irq.c b/xnu/files/osfmk/arm64/gic_v3_irq.c
index 8b87974..63ffd4b 100644
--- a/xnu/files/osfmk/arm64/gic_v3_irq.c
+++ b/xnu/files/osfmk/arm64/gic_v3_irq.c
@@ -11,6 +11,10 @@
* - with more than one processor (psci_smp.c), two software-generated
* interrupts carry the kernel's signals between them: cpu_signal() and
* the order to stop when the system goes back to the boot loader
+ * - a shared peripheral interrupt (SPI, numbers 32 to 1019) is a driver's once it
+ * is attached (gic_v3_spi_attach()): configured, routed to the processor that
+ * attaches it, enabled, and its handler called in interrupt context for each
+ * one that arrives, before it is ended
* - any other interrupt that fires is masked and counted, until a real
* driver registers a handler for it
*
@@ -22,6 +26,8 @@
*/
#include <mach/mach_types.h>
#include <kern/debug.h>
+#include <kern/simple_lock.h>
+#include <os/atomic_private.h>
#include <arm/cpu_data_internal.h>
#include <arm/cpu_internal.h>
#include <arm/machine_cpu.h>
@@ -39,7 +45,15 @@
#define GICD_CTLR_ENABLE_G1NS (1u << 1)
#define GICD_CTLR_ARE_NS (1u << 4)
#define GICD_CTLR_RWP (1u << 31)
+#define GICD_IGROUPR(n) (0x0080 + 4 * (n))
+#define GICD_ISENABLER(n) (0x0100 + 4 * (n))
#define GICD_ICENABLER(n) (0x0180 + 4 * (n))
+#define GICD_ICPENDR(n) (0x0280 + 4 * (n))
+#define GICD_ICACTIVER(n) (0x0380 + 4 * (n))
+#define GICD_IPRIORITYR(n) (0x0400 + (n))
+#define GICD_ICFGR(n) (0x0c00 + 4 * (n))
+#define GICD_IGRPMODR(n) (0x0d00 + 4 * (n))
+#define GICD_IROUTER(n) (0x6000 + 8 * (n))
#define GICR_FRAME_SIZE 0x20000 /* RD_base + SGI_base per CPU */
#define GICR_TYPER 0x0008
@@ -59,7 +73,9 @@
#define GIC_SGI_SIGNAL 0 /* cpu_signal(): another processor wants this one's attention */
#define GIC_SGI_STOP 1 /* psci_smp_stop_here(): the system is going back to the boot loader */
#define GIC_INTID_VTIMER 27 /* EL1 virtual timer PPI */
+#define GIC_INTID_SPI_MIN 32
#define GIC_INTID_SPURIOUS_MIN 1020
+#define GIC_SPI_MOST 48 /* attached at a time */
#define GIC_PRIORITY_DEFAULT 0xa0
static vm_offset_t gicd_base, gicr_base;
@@ -78,8 +94,28 @@ static uint64_t gic_active_iar[MAX_CPUS];
uint64_t gic_unhandled_count;
uint32_t gic_last_unhandled;
+/*
+ * The attached SPIs. A slot is filled before its interrupt is enabled and emptied after it has
+ * been disabled and no handler of it runs any more (busy), so the handler path reads the table
+ * without a lock.
+ */
+struct gic_spi {
+ uint32_t intid; /* 0: a free slot */
+ void (*handler)(void *context, uint32_t intid);
+ void *context;
+ uint32_t busy; /* handlers of it running now */
+ uint64_t count;
+};
+static struct gic_spi gic_spi[GIC_SPI_MOST];
+static decl_simple_lock_data(, gic_spi_lock);
+
void gic_v3_irq_cpu_init(void);
void gic_v3_irq_quiesce(void);
+kern_return_t gic_v3_spi_attach(uint32_t intid, bool edge, void (*handler)(void *context, uint32_t intid), void *context);
+void gic_v3_spi_mask(uint32_t intid);
+void gic_v3_spi_unmask(uint32_t intid);
+void gic_v3_spi_detach(uint32_t intid);
+uint64_t gic_v3_spi_count(uint32_t intid);
void gic_v3_irq_cpu_shutdown(void);
void gic_v3_irq_signal(uint32_t phys_id);
void gic_v3_irq_signal_stop(uint32_t phys_id);
@@ -134,6 +170,132 @@ gic_v3_irq_signal_stop(uint32_t phys_id)
gic_send_sgi(phys_id, GIC_SGI_STOP);
}
+/* An SPI that a driver has attached: its handler called. false: nobody has it. */
+static bool
+gic_spi_handle(uint32_t intid)
+{
+ for (unsigned i = 0; i < GIC_SPI_MOST; i++) {
+ struct gic_spi *spi = &gic_spi[i];
+
+ if (os_atomic_load(&spi->intid, acquire) == intid) {
+ os_atomic_inc(&spi->busy, acquire);
+ if (os_atomic_load(&spi->intid, relaxed) == intid) {
+ spi->count++;
+ spi->handler(spi->context, intid);
+ }
+ os_atomic_dec(&spi->busy, release);
+ return true;
+ }
+ }
+ return false;
+}
+
+static void
+gic_distributor_settle(void)
+{
+ while (gic_read32(gicd_base + GICD_CTLR) & GICD_CTLR_RWP) {
+ /* wait for the change to take effect */
+ }
+}
+
+void
+gic_v3_spi_mask(uint32_t intid)
+{
+ if (gicd_base != 0 && intid >= GIC_INTID_SPI_MIN && intid < GIC_INTID_SPURIOUS_MIN) {
+ gic_write32(gicd_base + GICD_ICENABLER(intid / 32), 1u << (intid % 32));
+ gic_distributor_settle();
+ }
+}
+
+void
+gic_v3_spi_unmask(uint32_t intid)
+{
+ if (gicd_base != 0 && intid >= GIC_INTID_SPI_MIN && intid < GIC_INTID_SPURIOUS_MIN) {
+ gic_write32(gicd_base + GICD_ISENABLER(intid / 32), 1u << (intid % 32));
+ }
+}
+
+/*
+ * A driver takes an SPI: edge- or level-triggered, Group 1, the default priority, routed to the
+ * processor this runs on, and enabled. The handler runs in interrupt context, with interrupts off,
+ * before the interrupt is ended; for a level-triggered one it has to quiet the source or mask the
+ * interrupt (gic_v3_spi_mask()). Thread context.
+ */
+kern_return_t
+gic_v3_spi_attach(uint32_t intid, bool edge, void (*handler)(void *context, uint32_t intid), void *context)
+{
+ uint64_t mpidr = __builtin_arm_rsr64("MPIDR_EL1");
+ struct gic_spi *slot = NULL;
+ uint32_t config;
+
+ if (gicd_base == 0 || intid < GIC_INTID_SPI_MIN || intid >= GIC_INTID_SPURIOUS_MIN || handler == NULL) {
+ return KERN_INVALID_ARGUMENT;
+ }
+ simple_lock(&gic_spi_lock, LCK_GRP_NULL);
+ for (unsigned i = 0; i < GIC_SPI_MOST; i++) {
+ if (gic_spi[i].intid == intid) {
+ simple_unlock(&gic_spi_lock);
+ return KERN_NAME_EXISTS;
+ }
+ if (slot == NULL && gic_spi[i].intid == 0 && gic_spi[i].busy == 0) {
+ slot = &gic_spi[i];
+ }
+ }
+ if (slot == NULL) {
+ simple_unlock(&gic_spi_lock);
+ return KERN_RESOURCE_SHORTAGE;
+ }
+ slot->handler = handler;
+ slot->context = context;
+ slot->count = 0;
+ os_atomic_store(&slot->intid, intid, release);
+ simple_unlock(&gic_spi_lock);
+
+ gic_v3_spi_mask(intid);
+ gic_write32(gicd_base + GICD_ICPENDR(intid / 32), 1u << (intid % 32));
+ gic_write32(gicd_base + GICD_ICACTIVER(intid / 32), 1u << (intid % 32));
+ gic_write32(gicd_base + GICD_IGROUPR(intid / 32), gic_read32(gicd_base + GICD_IGROUPR(intid / 32)) | 1u << (intid % 32));
+ gic_write32(gicd_base + GICD_IGRPMODR(intid / 32), gic_read32(gicd_base + GICD_IGRPMODR(intid / 32)) & ~(1u << (intid % 32)));
+ *(volatile uint8_t *)(gicd_base + GICD_IPRIORITYR(intid)) = GIC_PRIORITY_DEFAULT;
+ config = gic_read32(gicd_base + GICD_ICFGR(intid / 16)) & ~(3u << (intid % 16 * 2));
+ gic_write32(gicd_base + GICD_ICFGR(intid / 16), config | (edge ? 2u : 0u) << (intid % 16 * 2));
+ /* GICD_IROUTER: Aff3 in [39:32], Aff2.Aff1.Aff0 in [23:0]; bit 31 (any processor) clear */
+ *(volatile uint64_t *)(gicd_base + GICD_IROUTER(intid)) = (mpidr & 0xff00000000ull) | (mpidr & 0xffffff);
+ gic_distributor_settle();
+ gic_v3_spi_unmask(intid);
+ return KERN_SUCCESS;
+}
+
+/* ... and gives it back: disabled, nothing pending, and no handler of it running when this returns. */
+void
+gic_v3_spi_detach(uint32_t intid)
+{
+ gic_v3_spi_mask(intid);
+ if (gicd_base != 0 && intid >= GIC_INTID_SPI_MIN && intid < GIC_INTID_SPURIOUS_MIN) {
+ gic_write32(gicd_base + GICD_ICPENDR(intid / 32), 1u << (intid % 32));
+ }
+ for (unsigned i = 0; i < GIC_SPI_MOST; i++) {
+ if (gic_spi[i].intid == intid) {
+ os_atomic_store(&gic_spi[i].intid, 0, release);
+ while (os_atomic_load(&gic_spi[i].busy, acquire) != 0) {
+ /* a handler of it on another processor is finishing */
+ }
+ }
+ }
+}
+
+/* How many have arrived since it was attached. */
+uint64_t
+gic_v3_spi_count(uint32_t intid)
+{
+ for (unsigned i = 0; i < GIC_SPI_MOST; i++) {
+ if (gic_spi[i].intid == intid) {
+ return gic_spi[i].count;
+ }
+ }
+ return 0;
+}
+
static void
gic_irq_handler(__unused void *target, __unused void *refCon, __unused void *nub, __unused int source)
{
@@ -157,6 +319,8 @@ gic_irq_handler(__unused void *target, __unused void *refCon, __unused void *nub
} else if (intid == GIC_SGI_STOP) {
/* comes back only if nobody has in fact asked for that */
psci_smp_stop_here();
+ } else if (intid >= GIC_INTID_SPI_MIN && gic_spi_handle(intid)) {
+ /* a driver's */
} else {
/* nobody owns this interrupt yet: keep it from storming */
gic_mask(intid);
@@ -185,6 +349,14 @@ gic_v3_irq_quiesce(void)
return; /* as early as this there is no interrupt, nor a cpu number to ask for */
}
cpu = cpu_number();
+ /* (what drivers had of the shared interrupts goes with this kernel: none of them may fire under the loader) */
+ for (unsigned i = 0; i < GIC_SPI_MOST; i++) {
+ uint32_t intid = gic_spi[i].intid;
+
+ if (intid != 0) {
+ gic_write32(gicd_base + GICD_ICENABLER(intid / 32), 1u << (intid % 32));
+ }
+ }
if (gic_active_iar[cpu] != 0) {
__builtin_arm_wsr64("ICC_EOIR1_EL1", gic_active_iar[cpu] & ~GIC_IAR_TAKEN);
__builtin_arm_isb(ISB_SY);
@@ -261,6 +433,7 @@ gic_v3_irq_cpu_init(void)
uint64_t const *reg = prop;
vm_offset_t soc_base = pe_arm_get_soc_base_phys();
+ simple_lock_init(&gic_spi_lock, 0);
gicd_base = ml_io_map(soc_base + reg[0], reg[1]);
gicr_base = ml_io_map(soc_base + reg[2], reg[3]);
gicr_size = reg[3];