Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7ff8f5a
drm/asahi: g17: build a compute queue's objects apart from its QID
jgoyvaerts Nov 30, 2026
837cbbb
drm/asahi: g17: allocate the shared compute objects in SUBMIT
jgoyvaerts Nov 30, 2026
2c8dfe7
drm/asahi: g17: allocate a queue's compute backing in SUBMIT
jgoyvaerts Nov 30, 2026
c845858
Merge the corrected client queue backing retirement
DjDeveloperr Nov 30, 2026
936c70c
drm/asahi: commit compute backing after all channel preparation succeeds
DjDeveloperr Nov 30, 2026
e005b68
Merge main into m4pro/g17-compute-reserve
jgoyvaerts Dec 4, 2026
e437e7a
Merge main into m4pro/g17-compute-reserve
jgoyvaerts Dec 5, 2026
9757985
Merge main into m4pro/g17-compute-reserve
jgoyvaerts Dec 5, 2026
176d2fe
Merge m4pro/g17-kernel-object-charge into m4pro/g17-compute-reserve
jgoyvaerts Dec 5, 2026
291f65d
Merge main into m4pro/g17-compute-reserve
jgoyvaerts Dec 6, 2026
dd68918
drm/asahi: g17: provision the compute aperture where InitData has its…
jgoyvaerts Dec 6, 2026
b5f721b
drm/asahi: g17: return compute backing to its owner after its client …
jgoyvaerts Dec 6, 2026
5adab0a
drm/asahi: g17: keep an owner's set when re-arming its queue fails
jgoyvaerts Dec 6, 2026
57b26c2
drm/asahi: g17: reclaim idle pools for a compute SUBMIT short of lowe…
jgoyvaerts Dec 6, 2026
8f12a31
drm/asahi: g17: drop the unreachable backing tag at compute bind
jgoyvaerts Dec 6, 2026
e196537
drm/asahi: g17: never restart a SUBMIT after its outputs were published
jgoyvaerts Dec 6, 2026
049dfdc
tools/asahi: g17: run the SUBMIT compute provisioning loop
jgoyvaerts Dec 6, 2026
d0294c2
tools/asahi: g17: follow #3704 in #4576's and #4522's rebind tests
jgoyvaerts Dec 6, 2026
30209c1
Merge main into m4pro/g17-compute-reserve
jgoyvaerts Dec 6, 2026
0007225
drm/asahi: g17: leave outputs untouched by a restart before the first…
jgoyvaerts Dec 6, 2026
cf209f6
Merge main into m4pro/g17-compute-reserve
jgoyvaerts Dec 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions drivers/gpu/drm/asahi/g17/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,24 @@ impl Kill {
}
}

/// One compute command of an owner, from before its SUBMIT provisions the owner's compute backing
/// until the command is dropped. While any is held, what the owner was given back for its
/// publications is kept for them ([`Context::compute_claimed`]).
pub(crate) struct ComputeClaim(Arc<Context>);

impl ComputeClaim {
pub(crate) fn new(context: &Arc<Context>) -> Self {
context.compute_claims.fetch_add(1, Ordering::AcqRel);
Self(context.clone())
}
}

impl Drop for ComputeClaim {
fn drop(&mut self) {
self.0.compute_claims.fetch_sub(1, Ordering::AcqRel);
}
}

pub(crate) struct Context {
/// The GPU runs the M5 firmware's protocol.
m5: bool,
Expand All @@ -320,6 +338,9 @@ pub(crate) struct Context {
/// Advisory count of bound compute channels: publication still checks the physical binding
/// under the device mutex. Zero skips an inline attempt known to need admission.
compute_bindings: AtomicU32,
/// This owner's compute commands from before their SUBMIT provisioned them until they are
/// dropped ([`ComputeClaim`]): while there are any, the owner's spare compute backing stays.
compute_claims: AtomicU32,
/// Shared monotonic compute completion ordinal / submit sequence across an M5 multi-channel
/// context's physical compute queues.
compute_ordinal: AtomicU32,
Expand Down Expand Up @@ -474,6 +495,7 @@ impl Context {
closed_generation: AtomicU64::new(u64::MAX),
closed_for_open_client: AtomicBool::new(false),
compute_bindings: AtomicU32::new(0),
compute_claims: AtomicU32::new(0),
compute_ordinal: AtomicU32::new(0),
render_seen: AtomicBool::new(false),
render_active: AtomicU32::new(0),
Expand Down Expand Up @@ -519,6 +541,10 @@ impl Context {
self.render_seen.store(true, Ordering::Release);
}
}
/// Whether a compute command of this owner holds a [`ComputeClaim`].
pub(crate) fn compute_claimed(&self) -> bool {
self.compute_claims.load(Ordering::Acquire) != 0
}
/// Active (published and unretired) render count for this context.
pub(crate) fn render_active(&self) -> u32 {
self.render_active.load(Ordering::Acquire)
Expand Down
108 changes: 102 additions & 6 deletions drivers/gpu/drm/asahi/g17/fence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,13 +703,29 @@ mod tests {
assert!(!untouched_refusal(false, &Ok(())));
}

#[test]
fn nothing_queued_and_restarted_leaves_outputs_untouched() {
for error in [ERESTARTSYS, EAGAIN, ENOMEM, ENOSPC] {
assert!(untouched(false, &Err(error)));
assert!(!untouched(true, &Err(error)));
}
assert!(!untouched(false, &Err(EIO)));
assert!(!untouched(false, &Err(EBUSY)));
assert!(!untouched(false, &Ok(())));
}

#[test]
fn enomem_and_enospc_after_publication_become_eio() {
assert_eq!(submit_result(true, Err(ENOMEM)), Err(EIO));
assert_eq!(submit_result(true, Err(ENOSPC)), Err(EIO));
// A restart after publication would queue the published commands again.
assert_eq!(submit_result(true, Err(ERESTARTSYS)), Err(EIO));
assert_eq!(submit_result(true, Err(EAGAIN)), Err(EIO));
// Before publication they keep their meaning: nothing changed.
assert_eq!(submit_result(false, Err(ENOMEM)), Err(ENOMEM));
assert_eq!(submit_result(false, Err(ENOSPC)), Err(ENOSPC));
assert_eq!(submit_result(false, Err(ERESTARTSYS)), Err(ERESTARTSYS));
assert_eq!(submit_result(false, Err(EAGAIN)), Err(EAGAIN));
// Other results are never rewritten.
assert_eq!(submit_result(true, Err(EINVAL)), Err(EINVAL));
assert_eq!(submit_result(true, Ok(())), Ok(()));
Expand Down Expand Up @@ -922,6 +938,68 @@ mod tests {
}
}

#[test]
fn a_restart_before_the_first_command_leaves_outputs_untouched() -> Result {
let contexts = contexts();
let allocation_failure = allocation_failure(&contexts)?;
// A binary syncobj with a completed fence, and timelines at point 1.
let binary = kernel::drm::syncobj::SyncObj::new_kunit()?;
let previous = independent(&contexts)?;
binary.replace_fence(Some(&Fence::from_fence(&previous)));
previous.signal();
let timeline = kernel::drm::syncobj::SyncObj::new_kunit()?;
let anchor = kernel::drm::syncobj::SyncObj::new_kunit()?;
for syncobj in [&timeline, &anchor] {
let point1 = independent(&contexts)?;
syncobj.add_point(kernel::dma_fence::FenceChain::new()?, &Fence::from_fence(&point1), 1);
point1.signal();
}
let cumulative = || -> Result<KVVec<SyncItem>> {
let mut syncs = KVVec::with_capacity(3, GFP_KERNEL)?;
syncs.push(cumulative_item(anchor.clone(), 2), GFP_KERNEL)?;
syncs.push(timeline_item(timeline.clone(), 2)?, GFP_KERNEL)?;
let bundle = super::super::sync_output::SignalBundle::new(&syncs, 0)?;
syncs[0].cumulative = Some(bundle);
Ok(syncs)
};
let mut syncs = KVVec::with_capacity(2, GFP_KERNEL)?;
syncs.push(SyncItem { syncobj: binary.clone(), fence: None, chain_fence: None, timeline_value: 0,
cumulative: None, timeline: false }, GFP_KERNEL)?;
syncs.push(timeline_item(timeline.clone(), 2)?, GFP_KERNEL)?;
// The submission's fence is prepared before the first command, whose compute provisioning
// is interrupted: SUBMIT's install gate (queue/mod.rs) keeps the outputs as they were.
for (syncs, error) in [(syncs, ERESTARTSYS), (cumulative()?, ERESTARTSYS), (cumulative()?, EAGAIN)] {
let mut outputs = Outputs::new(&contexts, syncs, 0, &allocation_failure)?;
let aggregate = outputs.publish_with(None, false, false)?;
if !untouched(false, &Err(error)) {
outputs.install();
}
let result = submit_result(outputs.published(), Err(error));
assert_eq!(result, Err(error));
outputs.set_result(result);
drop(outputs);
drop(aggregate);
}
let kept = binary.fence_get().unwrap();
assert_eq!(kept.raw(), Fence::from_fence(&previous).raw());
assert_eq!(timeline.fence_get().unwrap().chain_find_seqno(2).err(), Some(EINVAL));
assert_eq!(anchor.fence_get().unwrap().chain_find_seqno(2).err(), Some(EINVAL));
// The restarted SUBMIT publishes point 2 once, chained on point 1, without error.
let mut retry = Outputs::new(&contexts, cumulative()?, 0, &allocation_failure)?;
let aggregate = retry.publish_with(None, false, false)?;
let member = aggregate.member();
retry.install();
retry.set_result(Ok(()));
drop(retry);
member.complete(Ok(()));
drop(member);
for syncobj in [&anchor, &timeline] {
let point2 = syncobj.fence_get().unwrap().chain_find_seqno(2)?.unwrap();
assert!(super::super::job::fence_status(&point2) > 0);
}
Ok(())
}

#[test]
fn full_window_signal_leaves_timeline_untouched_for_restart() -> Result {
unsafe extern "C" {
Expand Down Expand Up @@ -1408,11 +1486,29 @@ pub(crate) fn untouched_refusal(published: bool, result: &Result) -> bool {
!published && matches!(result, Err(error) if *error == ENOMEM || *error == ENOSPC)
}

/// The result a submission returns. ENOMEM and ENOSPC promise that nothing changed, so the same
/// failures after its outputs were published are returned as EIO: the outputs carry the failure.
/// Whether the ioctl returns `result` to be called again as it is: interrupted (ERESTARTSYS), or
/// to retry (EAGAIN). libdrm's drmIoctl() restarts both.
pub(crate) fn retried(result: &Result) -> bool {
matches!(result, Err(error) if *error == ERESTARTSYS || *error == EAGAIN)
}

/// Whether a submission's outputs stay untouched: none of its commands was queued (`queued`
/// false), and it was refused ([`untouched_refusal`]) or is to be called again as it is
/// ([`retried`]). A compute command's SUBMIT-time provisioning can wait interruptibly before the
/// first command is queued; its fence in the outputs would carry an error for a submission the
/// restart then queues, and be the previous point a cumulative timeline chains on.
pub(crate) fn untouched(queued: bool, result: &Result) -> bool {
!queued && (untouched_refusal(false, result) || retried(result))
}

/// The result a submission returns. ENOMEM and ENOSPC promise that nothing changed, and
/// ERESTARTSYS and EAGAIN have the ioctl called again as it is, so the same results after its
/// outputs were published are returned as EIO: the outputs carry the failure, and a restart would
/// queue the commands already queued a second time. A compute command's SUBMIT-time provisioning
/// can wait for a recovery or for reclaim after earlier commands of the submission were queued.
pub(crate) fn submit_result(published: bool, result: Result) -> Result {
match result {
Err(error) if published && (error == ENOMEM || error == ENOSPC) => Err(EIO),
Err(error) if published && [ENOMEM, ENOSPC, ERESTARTSYS, EAGAIN].contains(&error) => Err(EIO),
result => result,
}
}
Expand Down Expand Up @@ -1490,7 +1586,7 @@ impl Outputs {
}
/// The fence installed, or about to be installed on error, in every output sync.
pub(crate) fn output_fence(&self) -> Option<Fence> {
if self.submission.is_none() && self.result == Err(ERESTARTSYS) {
if self.submission.is_none() && retried(&self.result) {
return None;
}
self.submission
Expand Down Expand Up @@ -1548,7 +1644,7 @@ impl Drop for Outputs {
chunk.finish_enqueue(self.result);
}
if self.submission.is_some() {
if !self.installed && !untouched_refusal(false, &self.result) {
if !self.installed && !untouched(false, &self.result) {
self.install();
}
let Some(submission) = self.submission.take() else {
Expand All @@ -1567,7 +1663,7 @@ impl Drop for Outputs {
if let Some(fallback) = self.fallback.take() {
// No work was accepted and no fence was published; leave output
// syncobjs untouched so a restarted ioctl can publish them once.
if self.result == Err(ERESTARTSYS) {
if retried(&self.result) {
return;
}
if let Err(error) = self.result {
Expand Down
95 changes: 65 additions & 30 deletions drivers/gpu/drm/asahi/g17/freelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,20 @@ impl Drop for Pages {
LOWER_FREED.fetch_add(1, Ordering::AcqRel);
}
}
impl super::queue::compute::reserve::Capacity for Pages {
fn fits(&self, required: ComputeCapacity) -> bool {
self.fits_capacity(required)
}
fn covers(&self, other: &Self) -> bool {
match (self.layout, other.layout) {
(PageLayout::Neo { blocks, .. }, PageLayout::Neo { blocks: other, .. }) => blocks >= other,
(PageLayout::M5(geometry), PageLayout::M5(other)) =>
geometry.block_bytes() == other.block_bytes() && geometry.blocks >= other.blocks,
_ => false,
}
}
}

impl Pages {
fn new(alloc: &Allocator<'_>, blocks: usize, runs: RunLayout) -> Result<Self> {
let size = TABLE_SIZE + blocks * FREE_LIST_BLOCK_SIZE;
Expand Down Expand Up @@ -436,8 +450,9 @@ impl Pages {
let (backing, charge) = match client {
Some(client) => {
let commit = crate::memacct::LEDGER.admit(client, size as u64)?;
let backing = alloc.lower_user_charged(size, mmu::UAT_PGSZ as u64,
mmu::PROT_GPU_SHARED_RW)?;
let backing = client.charging(|| alloc.lower_user_charged(
size, mmu::UAT_PGSZ as u64, mmu::PROT_GPU_SHARED_RW,
))?;
let charge = crate::memacct::Charge::user(commit, None);
charge.set_held(true);
(backing, Some(charge))
Expand Down Expand Up @@ -497,6 +512,19 @@ impl Pages {
_ => false,
}
}
/// Writes the tables of fresh backing again, for reuse by the owner that gave this backing
/// back. The caller has seen the firmware consume the pool's release.
pub(crate) fn reset(&mut self) -> Result {
match self.layout {
PageLayout::Neo { blocks, runs } => {
let base = self.backing.gpu_va();
self.backing.initialize::<PoolTables>(0, |tables| tables.init(base, blocks, runs))?;
}
PageLayout::M5(_) => self.populate_m5()?,
}
fence(Ordering::SeqCst);
Ok(())
}
fn is_m5(&self) -> bool { matches!(self.layout, PageLayout::M5(_)) }
pub(crate) fn page_list_va(&self) -> u64 { self.backing.gpu_va() }
fn run_list_va(&self) -> u64 {
Expand Down Expand Up @@ -1092,6 +1120,23 @@ impl Submitted<'_> {
/// that VM's work, so a view of it in another VM is refused
/// ([`Self::map_client`]). Another VM gets the queue only after the backing
/// was given back and fresh, zeroed backing installed ([`Self::rearm`]).
/// The control and state objects of a compute pool, allocated before a QID is reserved for it.
pub(crate) struct PoolObjects {
control: KernelObject,
state: KernelObject,
}
impl PoolObjects {
pub(crate) fn new(alloc: &Allocator<'_>) -> Result<Self> {
let object = || alloc.kernel(
CONTROL_SIZE,
mmu::UAT_PGSZ as u64,
mmu::PROT_GPU_FW_SHARED_RW,
CpuMap::WriteCombined,
);
Ok(Self { control: object()?, state: object()? })
}
}

pub(crate) struct ComputePool {
pages: Option<Pages>,
/// The VM (`mmu::Vm::id`) the backing serves, from its first client view
Expand All @@ -1108,33 +1153,22 @@ impl ComputePool {
self.pages.as_ref().is_some_and(|pages| pages.fits_capacity(required))
}

/// A pool at the admitted capacity, in the capacity's family format.
pub(crate) fn new(alloc: &Allocator<'_>, qid: u8, required: ComputeCapacity,
client: Option<&Arc<crate::memacct::Client>>) -> Result<Self> {
if qid > super::fw::kick::QID_MAX {
return Err(EINVAL);
}
let pages = Pages::for_compute(alloc, required, client)?;
/// Binds `objects` and `pages` to the pool slot of `qid`: stores only.
pub(crate) fn prepare(objects: &mut PoolObjects, qid: u8, pages: &Pages) -> Result {
if qid > super::fw::kick::QID_MAX { return Err(EINVAL); }
Self::initialize_control(&mut objects.control, pages, Self::slot(qid), objects.state.gpu_va(), [0; 8])?;
fence(Ordering::SeqCst);
Ok(())
}

pub(crate) fn bind(objects: PoolObjects, qid: u8, pages: Pages, client_vm: u64) -> Self {
let PoolObjects { control, state } = objects;
let m5 = pages.is_m5();
Self::with_pages(alloc, qid, m5, pages)
Self { pages: Some(pages), client_vm, control, state, id: Self::slot(qid), m5 }
}
fn with_pages(alloc: &Allocator<'_>, qid: u8, m5: bool, pages: Pages) -> Result<Self> {
let id = u16::from(qid) + 1;
let mut control = alloc.kernel(
CONTROL_SIZE,
mmu::UAT_PGSZ as u64,
mmu::PROT_GPU_FW_SHARED_RW,
CpuMap::WriteCombined,
)?;
let state = alloc.kernel(
CONTROL_SIZE,
mmu::UAT_PGSZ as u64,
mmu::PROT_GPU_FW_SHARED_RW,
CpuMap::WriteCombined,
)?;
Self::initialize_control(&mut control, &pages, id, state.gpu_va(), [0; 8])?;
fence(Ordering::SeqCst);
Ok(Self { pages: Some(pages), client_vm: 0, control, state, id, m5 })
/// The pool slot (buffer slot) of `qid`'s pool.
pub(crate) fn slot(qid: u8) -> u16 {
u16::from(qid) + 1
}
fn initialize_control(control: &mut KernelObject, pages: &Pages, id: u16,
submitted: u64, completed: [u8; 8]) -> Result {
Expand Down Expand Up @@ -1203,12 +1237,13 @@ impl ComputePool {
/// rewrites the page-pool descriptor row before the queue is kicked again.
/// As render repopulation does, the firmware-completed counter is carried
/// over so it stays consistent with the host submitted counter in `state`.
pub(crate) fn rearm(&mut self, pages: Pages) -> Result {
/// Takes `pages` only once they are written in: on failure they stay with the caller.
pub(crate) fn rearm(&mut self, pages: &mut Option<Pages>) -> Result {
if self.pages.is_some() {
return Err(EBUSY);
}
self.write_pages(&pages)?;
self.pages = Some(pages);
self.write_pages(pages.as_ref().ok_or(EINVAL)?)?;
self.pages = pages.take();
Ok(())
}
/// Preserve both sets of pages on failure. The caller owns the consumed
Expand Down
19 changes: 15 additions & 4 deletions drivers/gpu/drm/asahi/g17/initdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,23 @@ impl InitData {
self.compute_shared.is_some()
}

/// Install a prepared pool once while holding the device mutex. A competing preparer may
/// have installed its own first; the redundant one then drops.
pub(super) fn install_compute_shared(&mut self, shared: ComputeShared) -> Result {
/// Whether this firmware's InitData has the objects the device-wide compute aperture and
/// scheduler serve: the boot free list's control and state. An M5 build has none, whatever
/// its compute program.
pub(super) fn hosts_compute_aperture(&self) -> bool {
self.boot_control.is_some() && self.boot_state.is_some()
}

/// Install the pool in `prepared` once while holding the device mutex, taking it only once
/// the install succeeded: a failed install leaves it prepared. A competing preparer may
/// have installed its own first; the redundant one then stays in `prepared`.
pub(super) fn install_compute_shared(&mut self, prepared: &mut Option<ComputeShared>) -> Result {
if self.compute_shared.is_some() {
return Ok(());
}
if prepared.is_none() {
return Err(EIO);
}
let boot_control = self.boot_control.ok_or(ENODEV)?;
let boot_state = self.boot_state.ok_or(ENODEV)?;
let state_va = self.object(boot_state)?.gpu_va();
Expand All @@ -541,7 +552,7 @@ impl InitData {
self.object_mut(boot_control)?.write(0, control)?;
self.object_mut(boot_state)?.bytes_mut().fill(0);
fence(Ordering::SeqCst);
self.compute_shared = Some(shared);
self.compute_shared = prepared.take();
Ok(())
}

Expand Down
Loading