Files
mac_linuxgpu/linuxu/headers
geramyloveless 4fe23bc535 A client rings its KFD queues' doorbells itself
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.
2026-10-06 17:18:27 -07:00
..