6 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 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 bded3d87c4 hsa: pause queues while the device is suspended, report lost memory
The runtime now follows the driver's device power state.

While the device is suspending, suspended or resuming the driver
refuses GPU work with kIOReturnOffline, having submitted nothing. The
IOKit transport returns that as HSA_STATUS_ERROR_RESOURCE_BUSY and
keeps the session healthy instead of faulting it. A queue whose
doorbell is refused keeps the highest value rung (its packets are in
the ring already); its service thread polls gently and rings it again
once the device takes work, so a generation in flight pauses and
continues. Allocations, transfers, queue creation and bounded launches
fail with the same retryable status.

Once a host sleep has closed the session the device's memory is gone:
queue error callbacks and the power API report HSA_STATUS_ERROR_FATAL,
which a client takes as "reload": release everything, hsa_shut_down,
hsa_init (the next initialization probes the device afresh), load
again.

mac_hsa_power.h, included by mac_hsa.h, gives clients the agent's power
state (state, generation, flags with VRAM preserved, cause, counters,
queues holding a doorbell); mac_hsa_agent_prepare_low_power, which
stops ringing this process's doorbells, waits a bounded time for the
packets already rung to finish, and asks the driver to quiesce;
mac_hsa_agent_resume, which drops the request and rings what waited;
and a bounded wait for the next power change. The iPad app calls
prepare on willResignActive and resume on didBecomeActive; neither is
needed for correctness, since the driver handles sleep itself.

The fake transport models the driver's power state for the new host
test, which covers paused and replayed doorbells (by the service and
by resume), retryable refusals, prepare and resume, waiting for a
change, and lost memory reported to a queue's error callback. A
transport test covers the IOKit status mapping. The runtime's copy of
the protocol names its state enum PowerState, which no longer collides
with the snapshot's State word.
2026-10-03 10:12:22 -07:00
geramyloveless 604239afb9 hsa: a census of the buffers each connection holds, and who asked
The driver says how much VRAM the process holds; nothing said what holds
it. Each connection now records every VRAM and shared buffer it gets
from the driver with the return addresses of the allocating call, and
reports buffer counts and bytes, a VRAM size histogram and the callers
holding the most VRAM (frames named by dladdr where exported, otherwise
image+offset for atos).

The report is printed with every refused allocation and is available
as mac_hsa_memory_report(); MAC_HSA_VRAM_TRACE=1 also logs each buffer
as it is allocated and freed.
2026-10-03 09:29:01 -07:00
geramyloveless 8c0a67fab0 hsa: hold the host window's address space for the session
Shared (GTT) buffers are mapped at their GPU VA, which the driver picks
inside the host window. The window was reserved only long enough to
choose a base and then released as a placement hint, so the process's
own mappings could take its pages. On the iPad the model's safetensors
mmap landed at 0x406800000, inside the 16 GiB KFD window at
0x400000000, and the 32 MiB staging buffer the driver then placed there
failed to map (IOConnectMapMemory64 kIOReturnBadArgument, which XNU
reports for any mapping it cannot create). Weight binding stopped with
HSA_STATUS_ERROR_OUT_OF_RESOURCES.

The connection now keeps every free page of the window reserved
(inaccessible, no backing) once the session is ready, releases exactly
a buffer's range just before its placed mapping, and reserves it again
when the buffer is unmapped or its mapping fails. A failure to map at
the GPU VA is now reported on stderr, saying whether something else in
the process holds the range.

A host memory budget caps the bytes of shared buffers mapped at once:
MAC_HSA_HOST_MEMORY_BUDGET (bytes, K/M/G suffix) or
mac_hsa_set_host_memory_budget(), with the call winning. An allocation
past it fails with OUT_OF_RESOURCES and a message naming the budget,
and the shared pool reports the budget as its size. Unset, nothing
changes. The iOS mach_vm wrappers move to mach_vm_compat.h so the
reservation code shares them.
2026-10-02 23:05:14 -07:00
geramyloveless 953cf0149c mac_linuxgpu: upstream Linux amdgpu on macOS
Runs the unmodified upstream Linux amdgpu and amdkfd drivers on Apple
Silicon Macs inside a DriverKit system extension, for AMD GPUs attached
over Thunderbolt. The Linux sources come from a pinned submodule and are
compiled against linuxu, a userspace implementation of the Linux kernel
APIs they use. Includes the HSA runtime, the host installer app, the
setup and firmware fetch scripts, and the offline test suite.
2026-10-02 18:49:17 -07:00