mirror of
https://github.com/lemonade-sdk/mac_linuxgpu.git
synced 2026-11-23 08:59:28 -10:00
A doorbell cost the submitting thread a trip through the driver: 14.7 us for the synchronous AQLQueueKick of build 263 (20 us async before it), on the one delivery thread every client's calls share. On Linux a process writes the doorbell page it mmaps from /dev/kfd: one store. From build 264 the runtime does the same. - The client maps its KFD process's doorbell slice (MLG_DOORBELL_MEMORY_TYPE, uncached): the page of the doorbell BAR KFD gave that process (rt_kfd_session_doorbells), holding only its own doorbells, as libhsakmt maps it. AQLQueueCreate asked for three outputs answers the queue's doorbell in it (out[2]), or MLG_DOORBELL_NONE for a queue rung through AQLQueueKick (a legacy queue). A KFD queue whose doorbell cannot be mapped is destroyed again and the call fails. - A store to the GPU's BARs once it stops answering can panic the Mac, so every doorbell write goes through a gate (doorbell_gate.h): a page the driver and the client share (MLG_DOORBELL_GATE_MEMORY_TYPE). The client counts itself busy, writes only while the gate is open, and counts itself out; the driver closes the gate and waits until busy is zero (Dekker's protocol, sequentially consistent both sides), so once a close returns the client writes nothing. A closed gate writes nothing: the runtime then sends the doorbell as AQLQueueKick, which answers what the device can take (Offline while it suspends: the doorbell waits for resume, as before; NoDevice once it is gone). - The driver closes every gate for a power transition (power_set), and for a device reset's whole window (the recovery hooks, before upstream halts the device until the PCI restore put the BARs back; a failed reset wedges and keeps them closed). Both doorbell paths, the clients' and the delivery thread's kick table, now hold a set of reasons instead of one flag, so a power change inside a reset opens nothing. A client's gate is retired (closed, drained, forgotten) at its close, its KFD process's release (whose doorbell slice may go to another process), the session's close or quarantine, a transport fault and a removal. The wait is a bounded spin (it may run on the delivery thread): a client stopped inside a write for 20 ms is named in the log. - Runtime: the queue keeps its doorbell and gate from creation until inactivate; a write never lowers the doorbell (as the driver's paths). Measured on the R9700 over Thunderbolt 5 (lat.cpp, display mirroring): doorbell store 14.7 -> 0.38 us median; empty dispatch round trip 43.9 -> 32.4 us (Linux on the same card: 31.3); back-to-back wall per dispatch with a doorbell each 16.8 -> 12.7 us. LemonSeed Engine decode is GPU-bound: plain 28.2 -> 28.7 tok/s, DFlash2 51.3 -> 51.4. Tests: test-doorbell-gate races client threads ringing through their gates against power holds, device-reset holds inside them, client closes and session closes, under ThreadSanitizer and AddressSanitizer; a doorbell written after a close returns fails it (dropping the drain does). It also names a client stopped mid-write. test-kfd-session checks the slice (a page of BAR 2, the process's own) and that the client's offset is the word the driver's kicks write; test-dext-compute-production the doorbell lookup, gate publish, power and reset holds and retire at the client's release; test-kick-table holds and releases both reasons; test-hsa-power-transport the runtime's maps, its direct store, a closed gate, and the failures that destroy the queue again.
linuxu tests
Standalone unit tests for the linuxu shim layer. Each test is a plain
main() program using a local EXPECT() macro (no test framework —
the dext cannot link gtest/libc-test deps).
Build & run
cd <repo>/linuxu
# host build (no DriverKit): -fsyntax-only first, then real link
clang -std=gnu11 -D__KERNEL__ -I<repo>/linuxu/headers \
<test>.c <required shim .c files> -lpthread -o <test>
./<test>
The make test target (top-level Makefile) builds and runs all of
them. Per-test required shim files:
| test | links with (shim sources) |
|---|---|
| test_kmemcheck | kmem/kmemcheck.c kmem/kmemalloc.c kmem/string.c shims/printk.c |
| test_fake_mmio | pci/pdev_mmio.c |
| test_vram | amdgpu-rt/vram.c |
| test_irq | amdgpu-rt/device.c pci/pci_stub.c shims/printk.c |
| test_workqueue | work.c sync.c shims/printk.c |
| test_timer | timer.c sync.c shims/printk.c |
| test_xarray | xarray.c |
| test_rcu | rcu.c shims/printk.c |
What is REAL vs STUB
- REAL (assertions exercise the actual algorithm): kmemcheck canaries, fake-MMIO token table + 16KB slots + readl/writel dispatch, vram bump allocator + pfn mapping, irq register/inject/unregister, workqueue (pthread worker + jiffy-delayed promotion + cancel_delayed_work_sync), timer_list (4ms reaper), xarray (node tree + xa_alloc), rcu (per-thread defer list + synchronize barrier).
- STUB (documented, no assertions beyond "does not crash"):
i2c (xfer → -ENOENT), acpi, mmu_notifier, dev_coredump, module,
firmware (loader is REAL for the host build; the dext build swaps
in the embedded-table path), debugfs/sysfs (table no-ops — could be
extended with a
test_debugfslater).
Running without a GPU
All tests run in-process on the host with no GPU, no IOKit, and no
root. The fake-MMIO and vram backends use host shadow buffers; the
LINUXU_DEXT compile flag switches them to the IOPCIDevice
backends (compiled only for the dext bundle, not for these tests).