diff --git a/drivers/gpu/drm/asahi/g14_v27_2.rs b/drivers/gpu/drm/asahi/g14_v27_2.rs index e9edc12813ab4..5f18d886413dd 100644 --- a/drivers/gpu/drm/asahi/g14_v27_2.rs +++ b/drivers/gpu/drm/asahi/g14_v27_2.rs @@ -340,6 +340,9 @@ pub(crate) fn start<'a>( gpu::GraphProfile::G14V27_2)?; let root = RootV27_2::build(&graph, &board)?; let pool = CommandPool::new(&mut gpu::GpuManager::alloc(&*graph), cfg)?; + // Timestamp buffers are bound in the timestamp aperture while the firmware runs, and the + // window only carries the tables that exist now: link the aperture's tables first. + graph.kernel_uat().prepare_kernel_tables(&[graph.timestamp_range()])?; window.mirror(graph.kernel_uat())?; window.install()?; window.announce(); @@ -670,7 +673,8 @@ impl Runtime { }; let last = index + 1 == packet.commands.len(); let result = match command { - Command::Compute(control) => self.publish_compute(state, &packet, control, last, slots[0]), + Command::Compute(control) => + self.publish_compute(state, &packet, index, control, last, slots[0]), Command::Render { command, usc } => self.publish_render(state, &packet, index, &command, usc, last, slots), }; @@ -687,7 +691,7 @@ impl Runtime { } fn publish_compute( - &self, state: &mut State, packet: &Arc, + &self, state: &mut State, packet: &Arc, index: usize, control: crate::m3::agx_compute::Control, last: bool, slot: usize, ) -> Result { let bind = self.graph.kernel_uat().bind(&packet.vm)?; @@ -700,6 +704,8 @@ impl Runtime { cdm_end: control.end, usc_base: control.usc_base, preempt, + timestamps: packet.timestamp_addresses(index), + helper: control.helper, }; let command = state.pool.encode(slot, &state.queue, stamp.raw(), &job)?; let submission = state.root.stage_compute(command)?; @@ -1210,8 +1216,8 @@ impl gpu::Gpu for Backend { Ok(KBox::new(crate::m3::m3_submit::Queue::new_g14_v27_2(self.runtime.clone(), self.scheduler.clone(), vm, priority, usc_exec_base, ualloc_priv, self.preempt_bytes, render)?, GFP_KERNEL)?) } - fn map_timestamp_buffer(&self, _bo: gem::ObjectRef, _range: Range) -> Result { - // Timestamp buffers would need new tables in the firmware window after boot. - Err(ENOTSUPP) + fn map_timestamp_buffer(&self, bo: gem::ObjectRef, range: Range) -> Result { + // The aperture's tables were linked before the firmware window was mirrored (start()). + ::map_timestamp_buffer(&self.graph, bo, range) } } diff --git a/drivers/gpu/drm/asahi/g14_v27_2_abi.rs b/drivers/gpu/drm/asahi/g14_v27_2_abi.rs index 4e7fd8de186e5..ab5c322a27a7b 100644 --- a/drivers/gpu/drm/asahi/g14_v27_2_abi.rs +++ b/drivers/gpu/drm/asahi/g14_v27_2_abi.rs @@ -93,6 +93,55 @@ pub(crate) const GLOBALS_PROGRESS: [(usize, u32); 8] = [ /// Size of the 27.2 Globals object. pub(crate) const GLOBALS_BYTES: usize = 0xe48; +/// Where the 27.2 compute command (type 3, 0x358 bytes) keeps the user timestamp pair: right +/// after the command time (+0x2c4) and the internal start/end pointers (+0x2cc), the order of +/// the 13.5 compute command and of the 27.2 render commands (vertex +0x5e4, fragment +0x948). +pub(crate) const COMPUTE_USER_TIMESTAMPS: usize = 0x2dc; + +/// The 27.2 compute command's JobMeta flush_stamps word, after the completion stamp, firmware +/// stamp, stamp value and slot (+0x298..+0x2b4); the render commands keep it at the same place +/// of their JobMeta (vertex +0x5a0, fragment +0x904), and the M3 27.2 compute command sets it +/// when it does not coalesce. Set, the firmware flushes the command's stamps when it completes. +pub(crate) const COMPUTE_FLUSH_STAMPS: usize = 0x2b4; +/// The 13.5 compute JobParameters1 inside the 27.2 compute command. +pub(crate) const COMPUTE_JOB_PARAMETERS1: usize = 0x68; + +/// The value of a 27.2 Timestamp operation's user word: like the 13.5 and the M3 27.2 +/// operations it names the command's start/end pair of user destinations, not a destination. +/// Zero when the command has no user timestamps. +pub(crate) fn user_timestamp_pointer(pair_va: u64, pair: [u64; 2]) -> u64 { + if pair == [0; 2] { + 0 + } else { + pair_va + } +} + +fn put64(b: &mut [u8], at: usize, v: u64) { + b[at..at + 8].copy_from_slice(&v.to_le_bytes()); +} + +fn put32(b: &mut [u8], at: usize, v: u32) { + b[at..at + 4].copy_from_slice(&v.to_le_bytes()); +} + +/// Writes a compute helper program (binary: USC offset | 1, data, configuration) into the +/// 13.5 JobParameters1 of a 27.2 compute command, at the places the 13.5 G14 compute path +/// fills: program +0x40, argument +0x48, configuration +0x50. +pub(crate) fn encode_compute_helper(cmd: &mut [u8], binary: u32, data: u64, config: u32) { + put32(cmd, COMPUTE_JOB_PARAMETERS1 + 0x40, binary); + put64(cmd, COMPUTE_JOB_PARAMETERS1 + 0x48, data); + put32(cmd, COMPUTE_JOB_PARAMETERS1 + 0x50, config); +} + +/// Writes the user timestamp pair of a zeroed 27.2 compute command at `cmd_va` and returns the +/// Timestamp operations' user word ([`user_timestamp_pointer`]). +pub(crate) fn encode_compute_timestamps(cmd: &mut [u8], cmd_va: u64, timestamps: [u64; 2]) -> u64 { + put64(cmd, COMPUTE_USER_TIMESTAMPS, timestamps[0]); + put64(cmd, COMPUTE_USER_TIMESTAMPS + 8, timestamps[1]); + user_timestamp_pointer(cmd_va + COMPUTE_USER_TIMESTAMPS as u64, timestamps) +} + #[cfg_attr(not(test), kunit_tests(asahi_g14_fw_queues))] #[cfg_attr(test, allow(dead_code))] mod tests { @@ -186,4 +235,65 @@ mod tests { assert!(GLOBALS_PROGRESS.iter().any(|&(o, v)| o == at && v != 0)); } } + + /// Size of the 27.2 compute command. + const COMPUTE_COMMAND_BYTES: usize = 0x358; + + /// Byte ranges `encode_compute` (gpu/v27_2.rs) writes in the 27.2 compute command, and + /// the words its stream hands the firmware to write (+0x264, +0x2c4, +0x304, +0x314, + /// +0x340). + const COMPUTE_WRITES: &[(usize, usize)] = &[ + (0, 4), (4, 8), (0xc, 4), (0x10, 8), + (0x68, 8), (0x70, 8), (0x78, 8), (0x80, 8), (0x88, 8), (0x90, 8), (0x98, 8), (0xa0, 8), + (0xc0, 8), + (0x1e8, 8), (0x1f0, 4), (0x220, 8), (0x228, 8), (0x264, 8), (0x270, 4), + (0x298, 0x20), (0x2c4, 0x18), (0x304, 8), (0x314, 8), (0x334, 0x14), + ]; + + /// Whether `len` bytes at `at` fit the command and are none the compute encoder uses. + fn free(at: usize, len: usize) -> bool { + at + len <= COMPUTE_COMMAND_BYTES + && COMPUTE_WRITES.iter().all(|&(o, l)| o + l <= at || at + len <= o) + } + + #[test] + fn the_compute_helper_lands_in_free_job_parameters() { + let mut cmd = [0u8; COMPUTE_COMMAND_BYTES]; + encode_compute_helper(&mut cmd, 0x1235, 0x11_2233_4400, 0x7_0000); + let read64 = |at: usize| u64::from_le_bytes(cmd[at..at + 8].try_into().unwrap()); + let read32 = |at: usize| u32::from_le_bytes(cmd[at..at + 4].try_into().unwrap()); + assert_eq!((read32(0xa8), read64(0xb0), read32(0xb8)), (0x1235, 0x11_2233_4400, 0x7_0000)); + for (at, len) in [(0xa8, 4), (0xb0, 8), (0xb8, 4)] { + assert!(free(at, len)); + } + } + + #[test] + fn flush_stamps_follows_the_compute_job_meta_and_misses_the_user_pair() { + // The JobMeta words the encoder writes end right before it: slot at +0x2b0. + assert_eq!(COMPUTE_FLUSH_STAMPS, 0x2b0 + 4); + assert!(COMPUTE_FLUSH_STAMPS + 4 <= COMPUTE_USER_TIMESTAMPS); + assert_eq!(COMPUTE_FLUSH_STAMPS % 4, 0); + } + + #[test] + fn a_timestamp_operation_names_the_pair_only_when_there_is_one() { + assert_eq!(user_timestamp_pointer(0x1000, [0, 0]), 0); + assert_eq!(user_timestamp_pointer(0x1000, [0x40, 0]), 0x1000); + assert_eq!(user_timestamp_pointer(0x1000, [0, 0x48]), 0x1000); + } + + #[test] + fn the_compute_user_pair_lands_in_a_free_place() { + let mut cmd = [0u8; COMPUTE_COMMAND_BYTES]; + let pair = [0xffff_ffa1_8140_0010, 0xffff_ffa1_8140_0018]; + assert_eq!(encode_compute_timestamps(&mut cmd, 0x8000, pair), 0x8000 + 0x2dc); + let read64 = |at: usize| u64::from_le_bytes(cmd[at..at + 8].try_into().unwrap()); + assert_eq!([read64(0x2dc), read64(0x2e4)], pair); + assert!(free(0x2dc, 16)); + // No timestamps leave the command as it was. + let mut empty = [0u8; COMPUTE_COMMAND_BYTES]; + assert_eq!(encode_compute_timestamps(&mut empty, 0x8000, [0; 2]), 0); + assert!(empty.iter().all(|&b| b == 0)); + } } diff --git a/drivers/gpu/drm/asahi/g14_v27_2_render.rs b/drivers/gpu/drm/asahi/g14_v27_2_render.rs index 10ce122708dbd..ecf9e43bc9d86 100644 --- a/drivers/gpu/drm/asahi/g14_v27_2_render.rs +++ b/drivers/gpu/drm/asahi/g14_v27_2_render.rs @@ -92,6 +92,8 @@ fn to_cmdbuf(r: &UapiRenderCommand) -> kernel::uapi::drm_asahi_cmd_render { c.sample_size_B = r.sample_size; c.ppp_ctrl = r.ppp_control; c.ppp_multisamplectl = r.multisample_control; + // The tiling parameters carry the vertex helper's configuration, as in the 13.5 command. + c.vertex_helper.cfg = r.vertex_helper.config; c } @@ -224,7 +226,8 @@ fn header(b: &mut [u8], kind: u32, c: &Common) { put64(b, 0x10, c.tracking); } -/// Write the destinations used by both internal timestamp paths. +/// Write the destinations used by both internal timestamp paths. `user` names the command's +/// pair of user destinations, or is 0 ([`crate::g14_v27_2_abi::user_timestamp_pointer`]). fn timestamp_op( s: &mut [u8], at: usize, @@ -234,7 +237,7 @@ fn timestamp_op( ts_pointers: u64, internal0: u64, internal1: u64, - user: [u64; 2], + user: u64, ) { put32(s, at, if start { 0x8000_0003 } else { 3 }); put64(s, at + 0x04, command_time); @@ -245,7 +248,7 @@ fn timestamp_op( if start { ts_pointers } else { ts_pointers + 8 }, ); put64(s, at + 0x1c, q.queue); - put64(s, at + 0x24, user[usize::from(!start)]); + put64(s, at + 0x24, user); put64(s, at + 0x2c, internal0); put64(s, at + 0x34, internal1); } @@ -430,6 +433,7 @@ pub(crate) fn encode_vertex( put64(cmd, 0x5dc, c.timestamps + 8); put64(cmd, 0x5e4, timestamps[0]); put64(cmd, 0x5ec, timestamps[1]); + let user = crate::g14_v27_2_abi::user_timestamp_pointer(c.cmd_va + 0x5e4, timestamps); let internal0 = c.cmd_va + 0x61c; let internal1 = c.cmd_va + 0x538; let flag = c.cmd_va + 0x568; @@ -469,7 +473,7 @@ pub(crate) fn encode_vertex( ts, internal0, internal1, - timestamps, + user, ); put32(s, 0x1e8, 0x0000_0100); timestamp_op( @@ -481,7 +485,7 @@ pub(crate) fn encode_vertex( ts, internal0, internal1, - timestamps, + user, ); let fin = 0x238; put32(s, fin, 6); @@ -933,6 +937,7 @@ pub(crate) fn encode_fragment( let ts = va(frag_at(offset_of!(Rf, timestamp_pointers))); put64(cmd, 0x948, timestamps[0]); put64(cmd, 0x950, timestamps[1]); + let user = crate::g14_v27_2_abi::user_timestamp_pointer(va(0x948), timestamps); let internal0 = va(0x980); let internal1 = va(0x89c); let (jp1, jp2) = (va(0x80), va(0x3c8)); @@ -989,7 +994,7 @@ pub(crate) fn encode_fragment( ts, internal0, internal1, - timestamps, + user, ); put32(s, 0x200, 0x0001_0000); // wait for the fragment pipe timestamp_op( @@ -1001,7 +1006,7 @@ pub(crate) fn encode_fragment( ts, internal0, internal1, - timestamps, + user, ); // op 8 FinalizeFragment (0xbc). let fin = 0x250; diff --git a/drivers/gpu/drm/asahi/gpu.rs b/drivers/gpu/drm/asahi/gpu.rs index 9fed9e62ec383..e153fc7490445 100644 --- a/drivers/gpu/drm/asahi/gpu.rs +++ b/drivers/gpu/drm/asahi/gpu.rs @@ -326,6 +326,8 @@ pub(crate) struct GpuManager { dyncfg: hw::DynConfig, pub(crate) initdata: fw::types::GpuObject, uat: mmu::Uat, + /// The firmware's timestamp aperture in the kernel VM, where timestamp buffers are bound. + timestamp_range: Range, crashed: AtomicBool, /// Set once the over-limit firmware-private garbage was reported as held. garbage_held: AtomicBool, @@ -746,10 +748,10 @@ impl GpuManager::ver { profile: GraphProfile, ) -> Result> { let dyncfg = Self::make_dyncfg(dev, res, cfg, &uat)?; - let private_end = match profile { - GraphProfile::Native => IOVA_KERN_PRIV_RANGE.end, + let (private_end, timestamp_range) = match profile { + GraphProfile::Native => (IOVA_KERN_PRIV_RANGE.end, IOVA_KERN_TIMESTAMP_RANGE), #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] - GraphProfile::G14V27_2 => IOVA_G14_V27_2_TIMESTAMP_RANGE.start, + GraphProfile::G14V27_2 => (IOVA_G14_V27_2_TIMESTAMP_RANGE.start, IOVA_G14_V27_2_TIMESTAMP_RANGE), }; let mut alloc = KernelAllocators { @@ -828,7 +830,8 @@ impl GpuManager::ver { false, )?); - let mut mgr = Self::make_mgr(dev, cfg, dyncfg, uat, alloc, event_manager, initdata)?; + let mut mgr = Self::make_mgr(dev, cfg, dyncfg, uat, alloc, event_manager, initdata, + timestamp_range)?; { let fwctl = mgr.fwctl_channel.lock(); @@ -1005,6 +1008,7 @@ impl GpuManager::ver { /// /// Force disable inlining to avoid blowing up the stack. #[inline(never)] + #[allow(clippy::too_many_arguments)] fn make_mgr( dev: &AsahiDrmDevice, cfg: &'static hw::HwConfig, @@ -1013,6 +1017,7 @@ impl GpuManager::ver { mut alloc: KernelAllocators, event_manager: Arc, initdata: KBox>, + timestamp_range: Range, ) -> Result>> { let mut pipes = PipeChannels::ver { vtx: KVec::new(), @@ -1113,6 +1118,7 @@ impl GpuManager::ver { dyncfg: KBox::::into_inner(dyncfg), initdata: KBox::>::into_inner(initdata), uat: KBox::::into_inner(uat), + timestamp_range, io_mappings: KVec::new(), next_mmio_iova: IOVA_KERN_MMIO_RANGE.start, rtkit <- new_mutex!(None, "rtkit"), @@ -1787,7 +1793,7 @@ impl Gpu for GpuManager::ver { crate::timestamp::Buffer::firmware(bo.map_range_into_range( self.uat.kernel_vm(), range, - IOVA_KERN_TIMESTAMP_RANGE, + self.timestamp_range.clone(), mmu::UAT_PGSZ as u64, mmu::PROT_FW_SHARED_RW, false, diff --git a/drivers/gpu/drm/asahi/gpu/v27_2.rs b/drivers/gpu/drm/asahi/gpu/v27_2.rs index 32d11d7ae6e74..6c141adf21807 100644 --- a/drivers/gpu/drm/asahi/gpu/v27_2.rs +++ b/drivers/gpu/drm/asahi/gpu/v27_2.rs @@ -79,6 +79,10 @@ pub(crate) struct ComputeJob { /// VA of the preemption buffer in the same context (13.5 layout: buffer 1 at +0, /// buffers 2 to 5 at +compute_preempt1_size + 8n). pub(crate) preempt: u64, + /// Firmware VAs of the user start and end timestamps, or 0. + pub(crate) timestamps: [u64; 2], + /// The helper program (scratch and stack setup), or zero. + pub(crate) helper: crate::m3::agx_uapi::UapiHelperProgram, } fn put32(b: &mut [u8], at: usize, v: u32) { @@ -116,12 +120,16 @@ fn encode_compute( put64(cmd, 0x2a0, q.fw_stamp); put32(cmd, 0x2a8, stamp_value); put32(cmd, 0x2b0, q.slot); + // flush_stamps, as the render commands and the M3 27.2 compute command set it. + put32(cmd, g14_v27_2_abi::COMPUTE_FLUSH_STAMPS, 1); put64(cmd, 0x2cc, timestamps); put64(cmd, 0x2d4, timestamps + 8); // Command-local dependency latch, enabled with seed 0; the firmware clears 0x10 bytes // through the pointer at +0x334. put32(cmd, 0x33c, 1); put64(cmd, 0x334, timestamps + 0x20); + let user = g14_v27_2_abi::encode_compute_timestamps(cmd, cmd_va, job.timestamps); + g14_v27_2_abi::encode_compute_helper(cmd, job.helper.binary, job.helper.data, job.helper.config); put32(stream, 0, 11); // 13.5 StartCompute.unk_28, at the same place in this layout (the 13.5 G14 path writes 1). @@ -141,6 +149,7 @@ fn encode_compute( put64(stream, at + 0x0c, cmd_va + 0x2cc); put64(stream, at + 0x14, cmd_va + selected); put64(stream, at + 0x1c, q.queue); + put64(stream, at + 0x24, user); put64(stream, at + 0x2c, cmd_va + 0x314); put64(stream, at + 0x34, cmd_va + 0x264); } @@ -357,7 +366,8 @@ impl BootJob { let cdm = self.client_va + self.preempt1 + 0x2000; let job = ComputeJob { context: 0, sequence: 0, cdm_base: cdm, cdm_end: cdm + 8, - usc_base: 0x11_0000_0000, preempt: self.client_va, + usc_base: 0x11_0000_0000, preempt: self.client_va, timestamps: [0; 2], + helper: crate::m3::agx_uapi::UapiHelperProgram { binary: 0, config: 0, data: 0 }, }; let (cmd_va, stream_va) = (self.command.gpu_va().get(), self.stream.gpu_va().get()); encode_compute(self.command.as_mut_slice(), cmd_va, self.stream.as_mut_slice(), stream_va, @@ -529,6 +539,11 @@ impl GpuManagerG14V13_5 { pub(crate) fn kernel_uat(&self) -> &mmu::Uat { &self.uat } + + /// The firmware timestamp aperture that timestamp buffers are bound in. + pub(crate) fn timestamp_range(&self) -> Range { + self.timestamp_range.clone() + } } impl RootV27_2 { diff --git a/drivers/gpu/drm/asahi/m3/agx_compute.rs b/drivers/gpu/drm/asahi/m3/agx_compute.rs index d58005c504f7a..01c5354748a21 100644 --- a/drivers/gpu/drm/asahi/m3/agx_compute.rs +++ b/drivers/gpu/drm/asahi/m3/agx_compute.rs @@ -6,4 +6,8 @@ pub(crate) struct Control { pub base: u64, pub end: u64, pub usc_base: u64, + /// The helper program, zero unless the queue's command builder encodes it + /// (`m3_submit::Queue::samplers_and_helpers`). + #[cfg_attr(not(CONFIG_DRM_ASAHI_G14_V27_2), allow(dead_code))] + pub helper: crate::m3::agx_uapi::UapiHelperProgram, } diff --git a/drivers/gpu/drm/asahi/m3/agx_uapi.rs b/drivers/gpu/drm/asahi/m3/agx_uapi.rs index 60107f8833edc..64912fffadb99 100644 --- a/drivers/gpu/drm/asahi/m3/agx_uapi.rs +++ b/drivers/gpu/drm/asahi/m3/agx_uapi.rs @@ -574,7 +574,7 @@ impl QueueUscWindow { } } -fn validate_sampler( +pub(crate) fn validate_sampler( address_space: &A, heap: u64, count: u32, @@ -599,6 +599,31 @@ fn validate_sampler( Ok(()) } +/// Validates a helper program descriptor for a queue whose commands encode helpers: either no +/// helper (all fields zero), or a tagged program offset that names a readable program in the +/// queue's USC window and an 8-byte-aligned argument block that the submitting VM maps +/// read-write. The configuration word is only accepted together with a program. +pub(crate) fn validate_helper( + queue: QueueUscWindow, + address_space: &A, + helper: UapiHelperProgram, +) -> Result<(), UapiTranslateError> { + if helper + == (UapiHelperProgram { + binary: 0, + config: 0, + data: 0, + }) + { + return Ok(()); + } + queue.full_program(address_space, helper.binary)?; + if helper.data & 7 != 0 { + return Err(UapiTranslateError::AddressAlignment); + } + require_range(address_space, helper.data, 8, GpuAccess::ReadWrite) +} + pub(crate) fn validate_attachments( address_space: &A, attachments: &UapiAttachmentList, diff --git a/drivers/gpu/drm/asahi/m3/m3_submit.rs b/drivers/gpu/drm/asahi/m3/m3_submit.rs index 5ba1f36879876..92b82e1c8097b 100644 --- a/drivers/gpu/drm/asahi/m3/m3_submit.rs +++ b/drivers/gpu/drm/asahi/m3/m3_submit.rs @@ -318,6 +318,10 @@ pub(crate) struct Queue { /// for every sample count and layer count. Otherwise only the single-sample, /// single-layer compressed depth layout is admitted. zls_compression_all_layouts:bool, + /// Whether this queue's command builders encode the render sampler heap and the render and + /// compute helper programs, in the 13.5 places. Otherwise they are refused: the M3 26.x + /// commands stream their sampler state and carry no helper. + samplers_and_helpers:bool, urgency:u8, } impl Queue { @@ -331,7 +335,7 @@ impl Queue { 1=>sched::Priority::High,0=>sched::Priority::Kernel,_=>return Err(EINVAL)}; Ok(Self {entity:sched::Entity::new(&scheduler,priority)?,_scheduler:scheduler, shared:Sink::M3(shared),vm,usc,fences:FenceContexts::new(1,c_str!("asahi_m3_queue"),kernel::static_lock_class!())?, - early_tiling,wide_visibility,zls_compression_all_layouts:false,urgency,preempt_alloc:None, + early_tiling,wide_visibility,zls_compression_all_layouts:false,samplers_and_helpers:false,urgency,preempt_alloc:None, #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] render_queue: None}) } @@ -350,6 +354,10 @@ impl Queue { // The G14 render command carries the depth and stencil load, store and partial // compression words in every layout (g14_v27_2_render.rs). zls_compression_all_layouts:true, + // Its render commands carry the sampler heap and both helper programs, and its + // compute command the 13.5 compute JobParameters1 with the helper + // (g14_v27_2_render.rs, gpu/v27_2.rs). + samplers_and_helpers:true, urgency,preempt_alloc:Some((ualloc_priv,preempt_bytes)), render_queue: Some(render_queue)}) } @@ -362,7 +370,7 @@ impl queue::Queue for Queue { while let Some(command)=parser.next_hardware().map_err(|_|EINVAL)? { if let agx_uapi::ParsedHardwareCommand::Render{payload,vertex_attachments,fragment_attachments,..}=command { if let Err(e)=validate_render(payload,self.usc,&AddressSpace(&self.vm),self.early_tiling, - self.zls_compression_all_layouts) { + self.zls_compression_all_layouts,self.samplers_and_helpers) { pr_err!("M3 render rejected {:?}: flags={:#x} size={}x{} layers={} samples={} utile={}x{} sample_size={} depth={:#x} stencil={:#x} zls={:#x} samplers={} heap={:#x}\n",e,payload.flags,payload.width,payload.height,payload.layers,payload.samples,payload.utile_width,payload.utile_height,payload.sample_size,payload.depth.base,payload.stencil.base,payload.zls_control,payload.sampler_count,payload.sampler_heap); return Err(e); } @@ -390,6 +398,17 @@ impl queue::Queue for Queue { // The common translator validates the original command/attachment // addresses; this M3 scheduling hint changes none of them. payload.flags &= !wide; + // The common translator refuses helpers; a queue that encodes one validates and + // takes it here. + let empty=agx_uapi::UapiHelperProgram{binary:0,config:0,data:0}; + let helper=if self.samplers_and_helpers { + agx_uapi::validate_helper(agx_uapi::QueueUscWindow{base:self.usc,user_start:0x4000,user_end:(1u64<<42)-0x8000}, + &AddressSpace(&self.vm),payload.helper).map_err(|e| { + pr_err!("M3 compute helper rejected {:?}: helper={:?}\n",e,payload.helper); + EINVAL + })?; + core::mem::replace(&mut payload.helper,empty) + } else {empty}; render_attachments.push([crate::m3::agx_attachments::Attachments::EMPTY;2],GFP_KERNEL)?; flushes.push(encode_attachments(&self.vm,&attachments)?,GFP_KERNEL)?; let c=agx_uapi::translate_compute_command(payload,attachments, @@ -405,7 +424,7 @@ impl queue::Queue for Queue { } timestamps.push([Destination::resolve(objects,payload.timestamps.start)?, Destination::resolve(objects,payload.timestamps.end)?,None,None],GFP_KERNEL)?; - commands.push(Command::Compute(crate::m3::agx_compute::Control{base:c.control_stream_base,end:c.control_stream_end,usc_base:self.usc}),GFP_KERNEL)?; + commands.push(Command::Compute(crate::m3::agx_compute::Control{base:c.control_stream_base,end:c.control_stream_end,usc_base:self.usc,helper}),GFP_KERNEL)?; } parser.finish().map_err(|_|EINVAL)?; let preempt=match &self.preempt_alloc { @@ -449,7 +468,7 @@ fn encode_attachments(vm: &mmu::Vm, list: &agx_uapi::UapiAttachmentList) } fn validate_render(r: agx_uapi::UapiRenderCommand, usc: u64, space: &AddressSpace<'_>, - early_tiling: bool, zls_compression_all_layouts: bool) -> Result { + early_tiling: bool, zls_compression_all_layouts: bool, samplers_and_helpers: bool) -> Result { use agx_uapi::{GpuAddressSpace, GpuAccess}; if r.sample_size % 8 != 0 {return Err(ENOTSUPP);} let empty = agx_uapi::UapiHelperProgram { binary:0, config:0, data:0 }; @@ -459,8 +478,8 @@ fn validate_render(r: agx_uapi::UapiRenderCommand, usc: u64, space: &AddressSpac #[cfg(not(CONFIG_DRM_ASAHI_M3_T8122))] let stage_scope = { let _ = early_tiling; 0 }; if r.flags & !(2|16|(1<<18)|stage_scope) != 0 || !matches!(r.samples, 1 | 2 | 4) - || r.sampler_count != 0 || r.sampler_heap != 0 - || r.vertex_helper != empty || r.fragment_helper != empty + || (!samplers_and_helpers && (r.sampler_count != 0 || r.sampler_heap != 0 + || r.vertex_helper != empty || r.fragment_helper != empty)) || (!zls_compression_all_layouts && (r.stencil.compression_base != 0 || r.stencil.compression_stride != 0)) || r.ppp_control & !0x203 != 0 { return Err(ENOTSUPP); } @@ -475,6 +494,7 @@ fn validate_render(r: agx_uapi::UapiRenderCommand, usc: u64, space: &AddressSpac || u32::from(r.sample_size)*u32::from(r.utile_width)*u32::from(r.utile_height)*u32::from(r.samples)>32768 { return Err(EINVAL); } + agx_uapi::validate_sampler(space, r.sampler_heap, r.sampler_count.into()).map_err(|_| EINVAL)?; crate::m3::agx_render_state::compact(r.vdm_base).map_err(|_| EINVAL)?; if r.vdm_base & 3 != 0 || !space.covers(r.vdm_base,4,GpuAccess::Read) || r.scissor_base == 0 || r.scissor_base & 7 != 0 || !space.covers(r.scissor_base,8,GpuAccess::Read) { @@ -489,5 +509,9 @@ fn validate_render(r: agx_uapi::UapiRenderCommand, usc: u64, space: &AddressSpac window.full_program(space,p.usc).map_err(|_| EINVAL)?; if !space.covers(usc+u64::from(p.usc & !63),64,GpuAccess::Read) { return Err(EINVAL); } } + // Only a queue that encodes helpers gets here with one (refused above otherwise). + for helper in [r.vertex_helper,r.fragment_helper] { + agx_uapi::validate_helper(window,space,helper).map_err(|_| EINVAL)?; + } Ok(()) } diff --git a/drivers/gpu/drm/asahi/mmu.rs b/drivers/gpu/drm/asahi/mmu.rs index b6d81365a9541..cf3e99f3e0600 100644 --- a/drivers/gpu/drm/asahi/mmu.rs +++ b/drivers/gpu/drm/asahi/mmu.rs @@ -3683,7 +3683,20 @@ impl Uat { /// Links the kernel lower root's tables for each range, with no mappings. A published-roots /// firmware samples that root's top-level entries only when it starts. pub(crate) fn prepare_lower_tables(&self, ranges: &[Range]) -> Result { - let mut inner = lock_vm(&self.kernel_lower_vm.gpuvm, None, false)?; + Self::prepare_tables(&self.kernel_lower_vm, ranges) + } + + /// Links the kernel window's tables for each range, with no mappings, for a firmware that + /// copies the window's second-level entries only when it starts + /// ([`FirmwareWindow::mirror`]). Page tables are only freed with their VM, so the leaf + /// tables stay where that copy names them. + #[cfg(CONFIG_DRM_ASAHI_G14_V27_2)] + pub(crate) fn prepare_kernel_tables(&self, ranges: &[Range]) -> Result { + Self::prepare_tables(&self.kernel_vm, ranges) + } + + fn prepare_tables(vm: &Vm, ranges: &[Range]) -> Result { + let mut inner = lock_vm(&vm.gpuvm, None, false)?; for range in ranges { inner.page_table.alloc_pages(range.clone())?; } diff --git a/tools/asahi/m3-uapi-tests.rs b/tools/asahi/m3-uapi-tests.rs index 4dc6012f5213e..dfbbdfe1c82ac 100644 --- a/tools/asahi/m3-uapi-tests.rs +++ b/tools/asahi/m3-uapi-tests.rs @@ -750,6 +750,63 @@ fn attachment_validation_rejects_readonly_zero_and_wrapped_ranges() { } } +#[test] +fn helpers_are_empty_or_a_mapped_program_with_a_writable_argument_block() { + let queue = QueueUscWindow { + base: USC_WINDOW_SIZE, + user_start: 0x4000, + user_end: 2 * USC_WINDOW_SIZE, + }; + let mut region = Region { + start: queue.base + 8, + end: queue.base + 0x100, + writable: true, + }; + let none = UapiHelperProgram { + binary: 0, + config: 0, + data: 0, + }; + let helper = UapiHelperProgram { + binary: 9, + config: 0x40, + data: queue.base + 0x40, + }; + assert_eq!(validate_helper(queue, ®ion, none), Ok(())); + assert_eq!(validate_helper(queue, ®ion, helper), Ok(())); + // A configuration or argument block without a program. + for stray in [ + UapiHelperProgram { config: 0x40, ..none }, + UapiHelperProgram { data: helper.data, ..none }, + ] { + assert_eq!( + validate_helper(queue, ®ion, stray), + Err(UapiTranslateError::ProgramOffset) + ); + } + // A program outside the mapping, a misaligned, unmapped or missing argument block. + assert_eq!( + validate_helper(queue, ®ion, UapiHelperProgram { binary: 0x200, ..helper }), + Err(UapiTranslateError::AddressRange) + ); + assert_eq!( + validate_helper(queue, ®ion, UapiHelperProgram { data: helper.data + 4, ..helper }), + Err(UapiTranslateError::AddressAlignment) + ); + for data in [0, queue.base + 0x100, u64::MAX & !7] { + assert_eq!( + validate_helper(queue, ®ion, UapiHelperProgram { data, ..helper }), + Err(UapiTranslateError::AddressRange) + ); + } + // The argument block must be writable. + region.writable = false; + assert_eq!( + validate_helper(queue, ®ion, helper), + Err(UapiTranslateError::AddressRange) + ); +} + #[test] fn usc_windows_reject_misalignment_overflow_and_unmapped_programs() { let queue = QueueUscWindow {