9 Commits
Author SHA1 Message Date
geramyloveless b190c105a8 Kernel arguments in CPU-visible VRAM, published with the HDP flush
HRX on Linux keeps a queue's kernel arguments in CPU-visible VRAM and
publishes them with the HDP flush register when the HSA runtime reports
HSA_AMD_AGENT_INFO_HDP_FLUSH and lets the CPU at the device coarse pool.
This brings that path to the Mac, where a CPU store to a GPU that stopped
answering can panic the machine, so every such store is bracketed under
the client's gate.

patches/linux/amdgpu-mmio-remap-4k-hole.patch: the NBIO/NBIF HDP flush
remap stays in the 4 KiB register hole the hardware leaves unused,
whatever the host page size (upstream moved it with PAGE_SIZE and turned
it off for pages over 4 KiB). KFD's own MMIO paths still refuse large
pages; nothing upstream maps the page into a process.

linuxu (kfd_session.c): rt_kfd_session_hdp_flush gives the host page of
the register BAR holding the remap page, after checking that no register
block of the device starts in the rest of it. VRAM can take a window VA,
and rt_kfd_bo_make_cpu_visible pins such a buffer in the CPU-visible
window: unmapped from the GPU, off the eviction fence, pinned with
CPU_ACCESS_REQUIRED, the move waited for, mapped again at the same VA.
rt_kfd_bo_bar_ranges reports its BAR ranges; a free unpins it first.

dext (hdp_flush.h): QueryInfo MLG_QUERY_BAR_WRITES lets a KFD client
opt in. It gets its gate (published, so every power transition, reset,
quarantine, fault, removal and close closes it and drains), the HDP
flush page as client memory, and CPU access to hostable VRAM: BOAlloc
flag DEXT_COMPUTE_BO_FLAG_HOSTABLE, then BOMap pins it and hands out its
BAR ranges. A client that never asks gets none of it. A retired gate is
marked so the client knows it never reopens. Closes that stop the device
answering (the session's close, quarantine, reset) wait up to 1 s for
an open bracket. Build 265.

HSA runtime: mac_hsa_bar_writes_enable, mac_hsa_bar_write_begin and
mac_hsa_bar_write_end. After enable the agent reports HDP_FLUSH, the CPU
agent's access to the VRAM pool is DISALLOWED_BY_DEFAULT, VRAM is
allocated hostable, and hsa_amd_agents_allow_access with a CPU agent maps
an allocation write combined at its own address, as ROCr does on a
large-BAR device; nothing else is CPU-mapped. begin waits while the gate
is held and fails once the device is gone, after replacing every BAR
mapping of the process with host memory (bar_mapping_retire.h); kicks
that find the GPU disconnected do the same.

libmlg_drm: a transport that says the device is gone has every mapping
retired the same way, so Vulkan clients' stores through mapped VRAM and
doorbells land in host memory too.

Tests: the session test pins hostable VRAM, checks its BAR ranges, a
full window and the HDP page with and without a register block beside
it; the production test enables BAR writes, maps hostable VRAM and
ranges it; hsa-bar-writes checks what is reported before and after
enable, the CPU grant, nested brackets, a held gate and a lost device;
the gate and libmlg_drm tests check retired gates and mappings.
2026-10-06 19:29:29 -07:00
geramyloveless b856a93f95 GPU recovery: device resets behind a gate, bounded; RESET_WAIT
A hang the queue reset cannot end now runs upstream's device reset
(amdgpu_device_gpu_recover, mode1 on gfx12) when the platform allows it
(rt_recovery_set_full_reset_gate), and wedges the GPU otherwise. The
driver's gate allows it only when the personality enables device resets
(MacLinuxGPUDeviceReset, absent = off until a reset is validated over
Thunderbolt) and no client maps one of the GPU's BARs: neither a raw BAR
lease nor any Linux-file mapping through a BAR (rt_lx_bar_mappings),
since a client store into a BAR that stops decoding can take the Mac
down. Every refusal is logged with its reason as the wedge's.

Upstream's own recovery runs unchanged, with three bounds around it:
the ASIC reset is wrapped so one recovery makes at most two attempts
(upstream retries a reset whose IB tests fail without limit on bare
metal); a device reset that fails (reset_domain->reset_res) wedges the
GPU, where upstream would leave its schedulers stopped with no event;
and one that succeeds advances the reset generation, with
RT_RECOVERY_LAST_VRAM_LOST when the VRAM-lost counter moved. The KFD
and user-queue reset works go through the same path.

RESET_WAIT (selector 91, observers and session clients, registration on
the delivery thread) completes when the reset generation differs from
the one the caller knows, with the generation and MLG_RESET_FLAG_*; a
client's waits end with it. The host's daemon re-arms it to post its
notification.

Tests: device_reset_check (the CS fixture, which now has soc24's reset
method and an ASIC reset that restarts its engines) allows device
resets and hangs a queue whose reset fails: upstream's
amdgpu_device_gpu_recover runs, the GPU does not come back (no VBIOS),
the device wedges after one ASIC reset and nothing retries. test-owner-
call covers RESET_WAIT (new generation, stale generation, cancel) and
the gate (personality, raw lease, client BAR mappings);
test_lx_files counts a client's BAR mapping.
2026-10-06 17:18:27 -07:00
geramyloveless 5607122bec AQLQueueKick on the delivery thread
A doorbell cost the submitting thread an async session call: about 20 us,
twice a synchronous call through the driver (a Ping is 10 us), because an
async call sets up its completion and hops to the session queue. From
build 263, AQLQueueKick called synchronously rings the doorbell on the
delivery thread itself.

DriverKit delivers every client's calls on that thread, which must never
sleep (build 241). The doorbell there takes no session lock and reads none
of the session queue's tables:
- kick_table.h: the KFD queues that may be rung on the delivery thread, by
  handle and owner, under a spinlock nothing holds across a sleep. The
  session queue publishes a queue once it exists and retires it before
  anything of it goes: its DESTROY_QUEUE, its owner's close, the device's
  stop. A ring holds the lock across its doorbell write, so once retire
  returns no doorbell of the queue is written and its memory may go. The
  table also closes for power transitions (power_set), which waits for a
  write in progress.
- rt_kfd_queue_kick_nowait: the doorbell under the session's kick
  spinlock alone. A queue is ringable from its creation until its
  DESTROY_QUEUE (queue_release_locked clears it under that lock first,
  since the doorbell is then released to other queues). Both doorbell
  paths share the highest value written, and neither writes a lower one.
  As on Linux, where the process writes its doorbell page itself, the
  value is not checked against the write index.
- The PCI access gate under the doorbell write is a spinlock and atomic
  counters, so nothing on that path sleeps. The store to the BAR stays in
  the driver, behind its removal gate; nothing is mapped into the client.

What the fast path cannot vouch for (a legacy queue, a busy or closing
session, an unpublished queue) it answers kIOReturnUnsupported, and the
runtime sends that doorbell as the async session call. Offline,
VMError, NoDevice and BadArgument answer as the async call does.

Tests: test-kick-table races four delivery threads against queues
published, retired and freed at once, client closes and power gates, under
ThreadSanitizer and AddressSanitizer. No ring reaches a retired, freed,
foreign or gated queue. The delivery threads kept ringing while the
session thread slept (a slow DESTROY_QUEUE): rings during the sleeps 2830
under TSan, 29100 under ASan. Longest wait for the lock 137 us under TSan,
38 us under ASan. test-kfd-session covers the nowait path on the real KFD
session code: written, never lower, refused from DESTROY_QUEUE on, -EFAULT
after a GPU fault. test-dext-compute-production covers publish, owner,
gate and retire on destroy and on client close. test-hsa-power-transport
covers the runtime's synchronous form and each answer.
2026-10-06 15:39:53 -07:00
geramyloveless 6b573a0595 Tests follow SysfsWrite, the KFD fault path and the VRAM MQD
- test-owner-call and test-client-transports compile the dext's bounded
  observer runner, which now also calls observer_sysfs_write: each test
  gets a stub, as it has for observer_sysfs_read.
- test-kfd-power links the KFD session fixture, which delivers GPU page
  faults through kfd_int_process_v11: the script links that source too, as
  test-kfd-session does.
- test-queue-partition links the patched kfd_mqd_manager_v12.c, which
  allocates compute MQDs with amdgpu_bo_create_kernel
  (patches/linux/kfd-v12-mqd-vram.patch): a stub that aborts, as for the
  other allocation entry points the AQL path never reaches.
2026-10-06 11:49:41 -07:00
geramyloveless a6f59666d5 The driver's Stop and Retire never wait forever behind a session call
Stop and Retire run on the session queue, behind whatever session call
runs there. On 250 a CPU-access pin that had to evict a compute client's
buffers waited forever. The session queue stayed blocked, and the
instance lingered in "terminating for upgrade via delegate" until the
user killed it; it could neither stop nor hand over to the next version.

owner_job_main now records the running session call's selector and
start. When a Stop or Retire is asked for, a watchdog on a queue of its
own (one watch at a time) checks once a second until it runs. If a
session call has held the queue for MLG_SESSION_BLOCK_BOUND_MS (30 s),
it makes GPU work complete at once, as a removal does (rt_device_lost):
fences complete with -ECANCELED, the call returns, and the Stop or
Retire runs. The device is unusable for this instance from then on. The
session closes with it and the event names the selector that blocked.

session shutdown watchdog-never-returning:
- nothing is forced with no call running, or before the bound;
- past the bound the GPU work is forced once, even when asked twice, and
  the log names the call;
- a freed queue clears it;
- the driver's Stop starts a watch.
2026-10-05 16:46:33 -07:00
geramyloveless d9df9a3a17 Disconnect GPU closes the device for the programs on it
With the device kept up across programs (248), Disconnect GPU could no
longer bring the GPU down. Once the display daemon let go, the session
stayed up with or without programs. The menu bar said "still in use" for
any program with the driver open, even an observer such as mtopg, and
had no way to close the GPU for programs still using it.

- Retire gets a DISCONNECT op (session_state.h). It closes the session
  now through the normal close, as QUIESCE with FORCE does, whatever
  clients it has. Unlike QUIESCE it keeps admitting sessions, so the next
  program to start brings the GPU up again. A raw BAR mapping still
  refuses, since a close would quarantine. A quarantine is released only
  when provably quiescent, as for Retire. out[2] counts the clients that
  lose the GPU.
- The closed session's clients get kIOReturnNoDevice on every further
  call (session and Linux-file clients alike), and the driver log says
  once per client that the GPU was disconnected. They are never silently
  rejoined to a new session with stale state. HSA maps NoDevice to device
  lost and now says the GPU was disconnected and the program must
  restart.
- The menu bar and `disconnect-gpu` wait for the displays to stop. If the
  driver's session is still up, they close it with DISCONNECT. The GPU is
  safe to unplug once the session is closed, whatever programs still have
  the driver open. Those programs are named: they are told the GPU was
  disconnected if they use it.

Tests:
- session shutdown retire-disconnect: the session closes the normal way
  for every program with nothing refused, the old clients' generation is
  the disconnected one, the call reports IDLE once closed, and the next
  program opens a new session.
- retire-disconnect-raw-bar: refused, nothing changes.
- The observer policy admits the op.
- The display agent's readiness: safe with clients named, an open
  session means apps connected (even none), closing, and the messages.
2026-10-05 16:30:21 -07:00
geramyloveless e317a1fb11 The device spec comes back as a versioned structure
QueryInfo tag 8 was never served by this driver (it declined with
kIOReturnNotReady), and the HSA runtime asked for it as 32 scalar
outputs, twice IOKit's limit, so mac_hsa_agent_get_device_spec always
declined.

The driver now answers tag 8 with a structure output, session_state.h's
struct mlg_device_spec (version 1, the bytes filled, a mask of the field
groups present). An async call gets it through OWNER_RESULT. Every field
is upstream's own:
- the GC geometry (gfx.config) and the KFD node's per-CU properties;
- the CUs, shader arrays and render backends upstream left active
  (gfx.cu_info, backend_enable_mask, num_rbs);
- on GC 12, the shader-array disable registers upstream reads for its
  own SA bitmap, read the same way without a GRBM select.
No register is written. A caller with less room than the structure gets
its first bytes, at least the header.

mac_hsa_agent_get_device_spec keeps its 32-word layout. A group the
driver does not report is zero. Words 23-27 (SH_MEM_CONFIG/BASES, the
shader-array config registers and GRBM_GFX_CNTL) stay zero, since
reading them needs a select, which is a register write. The CU bitmap
words carry two shader arrays for each of four engines. mac_hsa.h says
so.

Tests: test-device-topology checks rt_device_spec against upstream
state and register reads, including no register read off GC 12 or
without hardware access. test-dext-compute-production checks the
structure and its room. test-client-transports checks the HSA runtime
reading it through an owner call and OWNER_RESULT; before this change it
declined (0x1001).
2026-10-05 11:57:22 -07:00
geramyloveless 5a437c81cc HSA runtime: no abort at exit, and a clear status during a session close
Every LemonSeed Engine run on 246 aborted in exit() with "mutex lock
failed: Invalid argument". The runtime's static executable registry
outlived the transport's static mutex (added in 243 with the
session-call protocol check, ff324ff). Its destructor, run by
__cxa_finalize, freed each loaded executable's GPU buffer through the
connection, and the RPC locked the already-destroyed mutex. 242 had no
such mutex on that path. The runtime's process-wide state (registries,
agents, pools, allocations, signals, queues and their mutexes) is now
never destroyed: a client that exits without hsa_shut_down leaves the
GPU memory for the driver to reclaim when its connection closes with the
process. hsa_shut_down still tears everything down.

hsa_init while the driver closes a session failed with
HSA_STATUS_ERROR_INVALID_AGENT, because NewUserClient refuses session
clients then (kIOReturnNotAttached). The runtime now asks an observer
for the session state and:
- waits for a close in progress, bounded by MAC_HSA_SESSION_CLOSE_WAIT_MS
  (60 s by default), saying so on stderr;
- returns HSA_STATUS_ERROR_RESOURCE_BUSY for a close still running at the
  bound, or a retiring driver (an upgrade);
- returns HSA_STATUS_ERROR_FATAL at once for a session that will not
  close (quarantined, restart required, GPU wedged or removed).
hsa_status_string names HSA_STATUS_ERROR_RESOURCE_BUSY.

test-client-transports:
- runs the HSA runtime's IOKit transport, as a process of its own, with
  an executable and a queue loaded and no hsa_shut_down. Before this
  change it aborts in exit() with the same "mutex lock failed".
- runs hsa_init during a close that finishes (it waits), past the bound
  (busy), during a retire (busy) and with a quarantined session (lost).
2026-10-05 11:54:50 -07:00
geramyloveless 22b50726b0 Owner calls carry up to 16 scalar outputs
An async session call's completion holds 16 words: a 4-word header and
at most 12 scalar outputs. host/owner_call.h asked for no more than 12,
and the driver's owner_call capped the selector's outputs at 12. A
selector that answers more refused the call: QueryInfo's compute
topology (tag 10, 16 words) returned kIOReturnBadArgument. The HSA
runtime then never learned the driver's queue slots, fell back to one
queue, and refused the next hsa_queue_create with
HSA_STATUS_ERROR_OUT_OF_RESOURCES before calling the driver. LemonSeed
Engine's hrx creates several queues, so on build 245 its GPU backend
never came up and it ran on the CPU. Build 242 called QueryInfo
synchronously, with 16 outputs.

A caller may now ask for up to 16 scalars (IOKit's limit). Up to 12 come
inline in the completion as before. When there are more, the completion
carries their count and none of them, and OWNER_RESULT returns them
(n * 8 bytes), followed by any structure output. host/owner_call.h,
which the HSA runtime, the host app and libmlg_drm build from, and
scripts/mlg_owner_call.py fetch them so. The Python twin's IOReturn
constants for kIOReturnNotAttached and kIOReturnIPCError are corrected,
and it reports kIOReturnNoSpace by its own value.

test-client-transports (test-lx-transport before) now also runs the HSA
runtime's own IOKit transport, its sources unchanged, against the dext's
extracted dispatch. The GPU's session answers are those the installed
245 driver gave the runtime on an R9700, and each handler refuses a call
that asks for fewer scalars than it answers, as the driver's do. From a
cold GPU the runtime brings the device up (HostWindow, InitDevice), opens
the compute session and creates three MULTI queues with an error
callback, as hrx does. Before this change the second hsa_queue_create
fails with 0x1008 (OUT_OF_RESOURCES) after one queue, as on the
hardware. The test also checks owner_call.h's shapes: 16 scalars, 14
with a structure output, and a caller asking for fewer than a selector
answers getting the driver's refusal. test-owner-call covers the
driver's side of 16 scalars through OWNER_RESULT.
2026-10-05 11:10:24 -07:00