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.
The driver now handles DriverKit's power changes and gives clients a
power state, a way to ask for low power, and a way to wait for changes.
SetPowerState(Low), the device in a reduced power state while the
system runs, and a client's PowerControl PREPARE quiesce the compute
session through upstream KFD suspend (rt/power.h): every user queue off
MES with its work saved, VRAM untouched. RESUME by the last holder, or
SetPowerState(On), resumes it through upstream KFD resume and the saved
work continues.
SetPowerState(Off), system sleep, takes the Thunderbolt link and with
it the endpoint's state; VRAM cannot survive it on these hosts, and
upstream's only dGPU path evicts all of VRAM to system memory through
SDMA, which neither fits on the iPad nor is safe over the link. The
driver closes the compute session through its ordinary close path
before acknowledging the change; the acknowledgement waits for the close
on the default queue, with a watcher that acknowledges at a deadline so
a stuck close never stalls the sleep. On wake the state is lost if a
session was closed, and the next InitDevice probes afresh and makes it
active again. A quiesced session closes as it is, its queues already
off MES; the KFD suspend is handed back before upstream removal takes
its own, so upstream's suspend count stays balanced across sessions.
A failed quiesce or resume hands the suspend back, so MES maps the
queues again and the close removes them through MES; a GPU that cannot
prove itself idle leaves the session to the existing quarantine rather
than freeing memory it may use.
Clients see state, generation, flags (VRAM preserved among them),
cause, error and counters through QueryInfo tag "LPWR" (observers too)
and the PowerControl selector 83: QUERY, PREPARE, RESUME and an async
WAIT completing at the next generation change. While suspending,
suspended or resuming, selectors that would put work on the GPU are
refused with kIOReturnOffline, which a client retries after resume;
frees, queue destruction and cached queries stay available. A closing
client's waits end and its hold is dropped. Observers may request
transitions only with the session-release entitlement.
power_state.h holds the state machine, the snapshot, the admission
policy and the plan each event leads to, with a host test that also
checks the runtime's copy of the protocol. The session shutdown test
covers the removal hook and the observer policy for the new tag and
selector, and read-driver-log.py prints the power state.