From 59942890a5fc1de0e9ee08d76ce15742fb319742 Mon Sep 17 00:00:00 2001 From: Dj <43033058+DjDeveloperr@users.noreply.github.com> Date: Thu, 26 Nov 2026 04:47:23 -0500 Subject: [PATCH] drm/asahi: g17: map validated command and queue records firmware-only Every G17 execution context includes the kernel's upper half. The kernel builds and checks records there that the firmware then reads: kick entries, command descriptors, queue records with their pointer blocks and item rings, per-publication configuration records, and scheduler pages. Several of those mappings also gave the GPU write access: - on G17P, the render kick rings, the render and compute command descriptors, each context's scheduler page and the device-wide compute scheduler; - on G17P and M5, the render queue records, configuration records and support pages, and each compute queue's graph, announcement and configuration records, preemption buffer and usage page. GPU work from any client could therefore rewrite another client's command, or a queue's pointers, owner and context, after the kernel had checked them and before the firmware read them. Map them firmware-only. The GPU reads kick rings and command descriptors through their read-only lower aliases. It reaches a compute queue's preemption buffer and usage page through the aliases a binding maps into its client VM. Neither changes. M5 already mapped its kick rings, descriptors and scheduler pages this way (commits 68e51c077c69 and 64a142ca5cdc). Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com> --- drivers/gpu/drm/asahi/g17/context.rs | 6 +++-- drivers/gpu/drm/asahi/g17/initdata.rs | 6 +++-- drivers/gpu/drm/asahi/g17/kick.rs | 11 +++------- drivers/gpu/drm/asahi/g17/queue/compute.rs | 22 ++++++++++++------- .../gpu/drm/asahi/g17/queue/render/memory.rs | 9 ++++---- 5 files changed, 30 insertions(+), 24 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/context.rs b/drivers/gpu/drm/asahi/g17/context.rs index 6e377d0f29a42..af9c1cfdbc0bc 100644 --- a/drivers/gpu/drm/asahi/g17/context.rs +++ b/drivers/gpu/drm/asahi/g17/context.rs @@ -279,20 +279,22 @@ impl Context { } else { mmu::PROT_GPU_FW_SHARED_RW }; + // The scheduler page sits in the upper half, which every execution context includes, + // and only the firmware reads and writes it: it is firmware-only. let mut scheduler = if alloc.uat.is_m5() { SchedulerStorage::Retained(pool.allocate( alloc, Family::Scheduler, UAT_PGSZ, 0, - kernel_prot, + mmu::PROT_FW_SHARED_RW, CpuMap::WriteCombined, )?) } else { SchedulerStorage::Ordinary(alloc.kernel( UAT_PGSZ, UAT_PGSZ as u64, - mmu::PROT_GPU_FW_SHARED_RW, + mmu::PROT_FW_SHARED_RW, CpuMap::WriteCombined, )?) }; diff --git a/drivers/gpu/drm/asahi/g17/initdata.rs b/drivers/gpu/drm/asahi/g17/initdata.rs index 046910905418b..eacddbe5e9394 100644 --- a/drivers/gpu/drm/asahi/g17/initdata.rs +++ b/drivers/gpu/drm/asahi/g17/initdata.rs @@ -113,7 +113,9 @@ impl ComputeShared { } /// A device-global scheduler, retained independently of whichever physical queue first -/// needed it. Its generation reservation precedes later graph allocation failures. +/// needed it. Its generation reservation precedes later graph allocation failures. Only the +/// firmware reads and writes it, and it sits in the upper half every execution context +/// includes, so it is firmware-only. pub(super) struct ComputeScheduler { object: KernelObject, } @@ -123,7 +125,7 @@ impl ComputeScheduler { let mut object = alloc.kernel( mmu::UAT_PGSZ, mmu::UAT_PGSZ as u64, - mmu::PROT_GPU_FW_SHARED_RW, + mmu::PROT_FW_SHARED_RW, CpuMap::WriteCombined, )?; object.initialize::(0, |state| { diff --git a/drivers/gpu/drm/asahi/g17/kick.rs b/drivers/gpu/drm/asahi/g17/kick.rs index 43e1115f221f0..7f365425f5d78 100644 --- a/drivers/gpu/drm/asahi/g17/kick.rs +++ b/drivers/gpu/drm/asahi/g17/kick.rs @@ -690,18 +690,13 @@ impl Queue { } } let (vm, status) = (client.id(), client.status().cloned()); - // The firmware's mapping sits in the upper half, which every execution context includes. - // On M5 it is firmware-only, as a compute ring's is; the GPU reads the ring at its lower + // The firmware's mapping sits in the upper half, which every execution context includes, + // so it is firmware-only, as a compute ring's is: the GPU reads the ring at its lower // address. - let firmware_prot = if alloc.uat.is_m5() { - mmu::PROT_FW_SHARED_RW - } else { - mmu::PROT_GPU_FW_SHARED_RW - }; let high = alloc.kernel( KICK_RING_SIZE, mmu::UAT_PGSZ as u64, - firmware_prot, + mmu::PROT_FW_SHARED_RW, CpuMap::WriteCombined, )?; let mut range = alloc.uat.kernel_lower_dynamic_range(); diff --git a/drivers/gpu/drm/asahi/g17/queue/compute.rs b/drivers/gpu/drm/asahi/g17/queue/compute.rs index b439fdb0b8e36..c3d445dc12b36 100644 --- a/drivers/gpu/drm/asahi/g17/queue/compute.rs +++ b/drivers/gpu/drm/asahi/g17/queue/compute.rs @@ -111,8 +111,8 @@ impl DescriptorRing { /// at `+0xfac`, and the host rewrites each slot every 32 kicks, so a /// firmware-cached mapping carries the same dirty-line writeback hazard. /// The hardware reads the same pages through an uncached canonical low - /// alias; on M5 the kernel mapping, in the upper half every execution - /// context includes, is firmware-only. + /// alias; the kernel mapping, in the upper half every execution context + /// includes, is firmware-only. pub(crate) fn new(alloc: &Allocator<'_>, objects: &Arc) -> Result { let size = DESCRIPTORS * COMPUTE_DESCRIPTOR_SIZE; let object = objects.allocate( @@ -120,7 +120,7 @@ impl DescriptorRing { Family::Descriptor, size, 0, - if alloc.uat.is_m5() { mmu::PROT_FW_SHARED_RW } else { mmu::PROT_GPU_FW_SHARED_RW }, + mmu::PROT_FW_SHARED_RW, CpuMap::WriteCombined, )?; let low = object.alias_in( @@ -177,14 +177,19 @@ impl Graph { mcache: bool, ) -> Result { let page = mmu::UAT_PGSZ as u64; - let prot = mmu::PROT_GPU_FW_SHARED_RW; + // The kernel's mappings of the queue's records sit in the upper half, which every + // execution context includes: only the firmware reaches them there. The GPU reaches the + // preemption buffer and usage page through the aliases a binding maps into its VM. + let prot = mmu::PROT_FW_SHARED_RW; + // The kernel lower VM is context 0's alone. + let lower = mmu::PROT_GPU_FW_SHARED_RW; let cpu = CpuMap::WriteCombined; let ring = DescriptorRing::new(alloc, objects)?; let preempt = alloc.kernel(0x4000, page, prot, cpu)?; let usage = alloc.kernel(mmu::UAT_PGSZ, page, prot, cpu)?; - let operand = alloc.lower(operand_state.size, page, prot, cpu)?; + let operand = alloc.lower(operand_state.size, page, lower, cpu)?; // The M5 support page is firmware-only; the GPU has no access to it. - let support_prot = if m5 { mmu::PROT_FW_SHARED_RW } else { prot }; + let support_prot = if m5 { mmu::PROT_FW_SHARED_RW } else { mmu::PROT_GPU_FW_SHARED_RW }; let mut support = alloc.kernel(if m5 { mmu::UAT_PGSZ } else { 0xc000 }, page, support_prot, cpu)?; if !m5 { let support_va = support.gpu_va(); @@ -227,12 +232,13 @@ impl Graph { graph.write(0, QueueRecord::new(&record))?; } let (context_low, context_high) = if m5 { (None, None) } else { - (Some(alloc.lower(CONTEXT_SIZE, page, prot, cpu)?), + (Some(alloc.lower(CONTEXT_SIZE, page, lower, cpu)?), Some(alloc.kernel(CONTEXT_SIZE, page, prot, cpu)?)) }; let records = alloc.kernel(RECORD_SLOTS * RECORD_STRIDE, page, prot, cpu)?; let m5_timestamps = if m5 { - Some(Arc::new(alloc.kernel(RECORD_SLOTS * 16, page, prot, cpu)?, GFP_KERNEL)?) + Some(Arc::new(alloc.kernel(RECORD_SLOTS * 16, page, mmu::PROT_GPU_FW_SHARED_RW, cpu)?, + GFP_KERNEL)?) } else { None }; let cached_records = objects.allocate( alloc, diff --git a/drivers/gpu/drm/asahi/g17/queue/render/memory.rs b/drivers/gpu/drm/asahi/g17/queue/render/memory.rs index dd9cf808544f7..7f678d9bb2f37 100644 --- a/drivers/gpu/drm/asahi/g17/queue/render/memory.rs +++ b/drivers/gpu/drm/asahi/g17/queue/render/memory.rs @@ -120,21 +120,22 @@ impl Graph { qids: [u8; 2], pointers: fw::variant::PointerLayout, ) -> Result { - let prot = mmu::PROT_GPU_FW_SHARED_RW; + // The kernel's mappings of a pair's command storage and queue records sit in the upper + // half, which every execution context includes: only the firmware reaches them there. + // The GPU reads command storage only through the read-only aliases below. + let prot = mmu::PROT_FW_SHARED_RW; let m5 = alloc.uat.is_m5(); // M5 selects cached firmware attributes at initial mapping creation; // write-combined CPU stores also support the uncached capacity fallback. let cpu = if m5 { CpuMap::WriteCombined } else { CpuMap::WriteBack }; let bytes = if m5 { RECORDS * fw::m5::render_assembly::COMMAND_STORAGE_BYTES } else { TA_BYTES + FRAGMENT_BYTES }; - // The GPU reads command storage only through the read-only aliases below. On M5 the - // kernel mapping, in the upper half every execution context includes, is firmware-only. let descriptors = pool.allocate( alloc, Family::Descriptor, bytes, bytes, - if m5 { mmu::PROT_FW_SHARED_RW } else { prot }, + prot, cpu, )?; let ta = Aliases::new(&descriptors, alloc, context.vm(),