drm/tyr: firmware loading and MCU boot support
Subsystems (4)
v10 cover drm/tyr: firmware loading and MCU boot support
This series adds firmware loading and MCU boot support to the Tyr DRM driver. It includes:
- A parser for the Mali CSF firmware binary format
- A kernel-managed BO type (KernelBo) for internal driver allocations
- GPU virtual memory (VM) integration using drm_gpuvm
- An MMU module and a generic slot manager
- Loading firmware, VM activation, and MCU boot at probe()
It is based on drm-rust-next and also depends on:
[PATCH v3] rust: iommu: add device lifetime to IoPageTable
https://lore.kernel.org/rust-for-linux/[email protected]
---
Changes in v10:
- rename 'bound to 'drm when not used for bus device private data
- check that mapping/unmapping parameters are page aligned
- Link to v9: https://lore.kernel.org/r/[email protected]
Changes in v9:
- reuse SlotData when reactivating a slot
- add fn seat to SlotOperations
- don't allocate gpuva for full unmap_range
- add small sleeps instead of busy-wait for hw
- move sparse address space test to constructor
- use infallible pin_init! for registration data
- add fw stop() to disable MCU and call it from drop()
- check page alignment in parser
- validate firmware header and entry bounds
- allow firmware sections with no VM mapping footprint
- clear stale MCU interrupts before boot
- handle various errors and clean up
- remove various unreachable code
- remove trailing newlines from dev_ prints
- Link to v8: https://lore.kernel.org/r/[email protected]
Changes in v8:
- kill the vm if firmware does not initialize properly
- write to JOB_IRQ_CLEAR after MCU boots
- check that a mapping request does not run past the end of the GEM buffer
- check that the SG table gave us enough backing memory for our VA
- make the KernelBo gem object field (bo) private and add an accessor
- return unused gpuva from remap()
- always provide three gpuva to unmap() instead of two
- be stricter about interpreting MAIR bits for device and non-cacheable memory
- preserve idle slot data in case subsequent activation fails
- clear evicted slot after eviction in case a subsequent activation fails
- wait for flush and lock commands to complete before proceeding
- rewrite SectionFlags with the register macro
- use the InaBits conversion provided by the register macro
- rename the slot data argument to vm_as_data for clarity
- add various checks for size, shifting, truncating, sparse values, overflow etc.
- return errors instead of panicking from unwrap()
- use dev_* macros for all logging
- Link to v7: https://lore.kernel.org/r/[email protected]
Changes in v7:
- slot.rs: don't print an error when all slots are busy
- change all allow(dead_code) to expect(dead_code)
- use LockedSeat type alias in VmAsData
- shift the lockaddr_base so it fits properly in LOCKADDR
- replace core::fmt with kernel::fmt
- make SAFETY comments clearer for map/unmap pages
- fix arm 32-bit build failures
- release gpuvm_unique lock before calling deferred_cleanup
- Link to v6: https://lore.kernel.org/r/[email protected]
Changes in v6:
- Move gpu_info from drm::Driver::Data to RegistrationData
and remove drm::Driver::Data entirely.
- Remove the extra drm::Device ARef in TyrPlatformDriverData
since drm::Registration already owns an ARef to the drm::Device.
- Stop storing platform::Device ARef in firmware since it is unused.
- Pass a generic kernel::Device instead of the platform::Device through firmware to Vm and pagetables since this is all that they need.
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- Reduce the scope of this series back to just MCU booting.
- Drop the CSF global interface, job IRQ, wait, and arch timer patches since they will be sent as subsequent series.
- Add lifetimes to resources and store them in TyrRegistrationData.
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
New commits:
- drm/tyr: program CSF global interface
- rust: time: add arch_timer_get_rate wrapper
- drm/tyr: add CSF firmware interface support
- drm/tyr: validate presence of CSF shared section
- drm/tyr: wait for global interface readiness
- drm/tyr: add Job IRQ handling
- drm/tyr: add Wait type for GPU events
The existing commits from v3 remain unchanged.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
New commits:
- drm/tyr: remove unused device from platform data
- drm/tyr: use shmem GEM object type in TyrDrmDriver
drm/tyr: select required dependencies in Kconfig
- Rename commit since the dependencies are not limited to DRM.
- Select new RUST_DRM_GEM_SHMEM_HELPER instead of DRM_GEM_SHMEM_HELPER.
drm/tyr: set DMA mask using GPU physical address
- Use register macro to read pa_bits instead of separate helper function.
drm/tyr: add MMU module
- Switch MMU code to typed register APIs (TRANSCFG, MEMATTR, STATUS, LOCKADDR, etc.).
- Use MmuCommand enum for MMU commands instead of raw constants.
- Minor cleanups and renaming (MAX_AS, AS_PRESENT handling).
drm/tyr: add GPU virtual memory module
- Extract VA/PA bits via typed MMU_FEATURES register.
- Update the VM code to match the new GPUVM v6 and shmem GEM v10 APIs.
drm/tyr: add a kernel buffer object
- Reject zero-sized KernelBo allocations up front.
drm/tyr: add firmware loading and MCU boot support
- Use typed GPU control registers.
- Pass iomem by Arc into Firmware::new() since we store it eventually.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]/
Changes in v2:
- The whole series is rebased on drm-rust-next including v7.0-rc1.
- Each patch has its own changelog.
Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/
---
Boris Brezillon (3):
drm/tyr: add a generic slot manager drm/tyr: add Memory Management Unit (MMU) support drm/tyr: add GPU virtual memory (VM) support
Daniel Almeida (1):
drm/tyr: add parser for firmware binary
Deborah Brouwer (3):
drm/tyr: add resources to RegistrationData drm/tyr: add a kernel buffer object drm/tyr: add Microcontroller Unit (MCU) booting
drivers/gpu/drm/tyr/Kconfig | 5 + drivers/gpu/drm/tyr/driver.rs | 68 ++- drivers/gpu/drm/tyr/file.rs | 11 +- drivers/gpu/drm/tyr/fw.rs | 321 +++++++++++ drivers/gpu/drm/tyr/fw/parser.rs | 588 +++++++++++++++++++ drivers/gpu/drm/tyr/gem.rs | 134 ++++- drivers/gpu/drm/tyr/mmu.rs | 120 ++++ drivers/gpu/drm/tyr/mmu/address_space.rs | 511 +++++++++++++++++ drivers/gpu/drm/tyr/regs.rs | 135 ++++- drivers/gpu/drm/tyr/slot.rs | 404 +++++++++++++ drivers/gpu/drm/tyr/tyr.rs | 4 + drivers/gpu/drm/tyr/vm.rs | 949 +++++++++++++++++++++++++++++++ 12 files changed, 3215 insertions(+), 35 deletions(-)
---
base-commit: 951aa1f4952cbb4e30ace86c99f248f968d56705
change-id: 20260708-fw-boot-b4-f07706a265c7
Best regards,
Signature · 1 line
Deborah Brouwer <[email protected]>
PATCH v10 1/7 drm/tyr: add resources to RegistrationData
Currently Tyr is not storing any resources in its drm::Driver RegistrationData.
Move Tyr's device-private resources and gpu information from drm::Driver::Data to drm::Driver::RegistrationData. This allows Tyr to access this data safely within the lifetime of its binding to its parent platform device and while registered with userspace.
--- drivers/gpu/drm/tyr/driver.rs | 42 +++++++++++++++++++++--------------------- drivers/gpu/drm/tyr/file.rs | 11 ++++++----- 2 files changed, 27 insertions(+), 26 deletions(-)
drivers/gpu/drm/tyr/driver.rs +21 -21
@@ -6,6 +6,7 @@ OptionalClk, // }, device::{ +Bound ,Core ,Device ,DeviceContext ,//
@@ -27,10 +28,7 @@ regulator, regulator::Regulator, sizes::SZ_2M, - sync::{ - aref::ARef, - Mutex, // - }, + sync::Mutex, time, // };
@@ -53,13 +51,17 @@#[pin_data( PinnedDrop )] pub (crate )struct TyrPlatformDriverData <'bound> { - _device:ARef <TyrDrmDevice >, _reg: drm::Registration <'bound,TyrDrmDriver >, } +/// Data owned by the DRM [`Registration`]. +/// +/// This data can have references tied to the parent platform device binding scope +/// and is accessible only while the DRM device is registered with userspace. #[pin_data] -pub (crate )struct TyrDrmDeviceData { -pub (crate ) pdev:ARef <platform::Device >, +pub (crate )struct TyrDrmRegistrationData <'drm> { +/// Parent platform device. +pub (crate ) pdev: &'drm platform::Device <Bound >,#[pin] clks:Mutex <Clocks >,
@@ -67,9 +69,10 @@ pub(crate) struct TyrDrmDeviceData {#[pin] regulators: Mutex<Regulators>, -/// Some information on the GPU. -/// -/// This is mainly queried by userspace, i.e.: Mesa. +/// GPU MMIO register mapping. + pub(crate) iomem: IoMem<'drm>, + +/// GPU information read from hardware during probe. pub(crate) gpu_info: GpuInfo, }
@@ -134,10 +137,10 @@ fn probe<'bound>(// other threads of execution. unsafe { pdev.dma_set_mask_and_coherent (DmaMask ::try_new (pa_bits)?)? }; -let platform:ARef <platform::Device > = pdev.into (); +let unreg_dev = drm::UnregisteredDevice ::<TyrDrmDriver >::new (pdev,Ok (()))?; - let data = try_pin_init!(TyrDrmDeviceData { - pdev: platform.clone(), + let reg_data = try_pin_init!(TyrDrmRegistrationData { + pdev, clks <- new_mutex!(Clocks { core: core_clk, stacks: stacks_clk,
@@ -147,18 +150,15 @@ fn probe<'bound>( _mali: mali_regulator, _sram: sram_regulator, }), + iomem, gpu_info, }); -let tdev = drm::UnregisteredDevice ::<TyrDrmDriver >::new (pdev, data)?;// SAFETY: `reg` is stored in `TyrPlatformDriverData` and dropped when the driver is // unbound; it is never forgotten. -let reg =unsafe { drm::Registration ::new (pdev.as_ref (), tdev, (),0 )? }; +let reg =unsafe { drm::Registration ::new (pdev.as_ref (), unreg_dev, reg_data,0 )? }; -let driver =TyrPlatformDriverData { - _device: reg.device ().into (), - _reg: reg, - }; +let driver =TyrPlatformDriverData { _reg: reg };// We need this to be dev_info!() because dev_dbg!() does not work at // all in Rust for now, and we need to see whether probe succeeded.
@@ -184,8 +184,8 @@ fn drop(self: Pin<&mut Self>) {}#[vtable] impl drm::Driver for TyrDrmDriver { -type Data =TyrDrmDeviceData ; -type RegistrationData <'a> = (); +type Data = (); +type RegistrationData <'drm> =TyrDrmRegistrationData <'drm>;type File =TyrDrmFileData ;type Object = drm::gem::shmem::Object <BoData >;type ParentDevice <Ctx :DeviceContext > = platform::Device <Ctx >;
drivers/gpu/drm/tyr/file.rs +6 -5
@@ -12,7 +12,8 @@use crate ::driver::{TyrDrmDevice , -TyrDrmDriver ,// +TyrDrmDriver , +TyrDrmRegistrationData ,// };#[pin_data]
@@ -31,15 +32,15 @@ fn open(_dev: &drm::Device<Self::Driver>) -> Result<Pin<KBox<Self>>> {impl TyrDrmFileData {pub (crate )fn dev_query ( - ddev: &TyrDrmDevice <Registered >, - _reg_data: &(), + _ddev: &TyrDrmDevice <Registered >, + reg_data: &TyrDrmRegistrationData <'_>, devquery: &mut uapi::drm_panthor_dev_query , _file: &TyrDrmFile , ) ->Result <u32 > {if devquery.pointer ==0 {match devquery.type_ { uapi::drm_panthor_dev_query_type_DRM_PANTHOR_DEV_QUERY_GPU_INFO => { - devquery.size = core::mem::size_of_val (&ddev.gpu_info)as u32 ; + devquery.size = core::mem::size_of_val (®_data.gpu_info)as u32 ;Ok (0 ) } _ =>Err (EINVAL ),
@@ -53,7 +54,7 @@ pub(crate) fn dev_query( ) .writer(); - writer.write (&ddev.gpu_info)?; + writer.write (®_data.gpu_info)?;Ok (0 ) }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v10 1/7] drm/tyr: add resources to RegistrationData
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Critical] The DEV_QUERY ioctl handler fails with -EFAULT when userspace provides a smaller buffer size than the kernel's structure size, violating the documented Panthor UAPI. --
--- Patch [1]: [PATCH v10 1/7] drm/tyr: add resources to RegistrationData --- commit 582ca5eb256eb752ad582c7535dd1fc5bf72cd26
Author: Deborah Brouwer <[email protected]>
drm/tyr: add resources to RegistrationData
Currently Tyr is not storing any resources in its drm::Driver RegistrationData. Move Tyr's device-private resources and gpu information from drm::Driver::Data to drm::Driver::RegistrationData so they can be accessed safely while the device is registered with userspace.
Quoted 4 lines
drivers/gpu/drm/tyr/file.rs +0
-0
[ ... ]
drivers/gpu/drm/tyr/file.rs pub(crate) fn dev_query( · line 57
Quoted 6 lines
drivers/gpu/drm/tyr/file.rs +1
-1@@ -53,7 +54,7 @@ pub(crate) fn dev_query( ) .writer(); - writer.write (&ddev.gpu_info)?; + writer.write (®_data.gpu_info)?;
[Severity: Critical]
This isn't a bug introduced by this patch, but does this strict size check violate the documented Panthor UAPI?
When userspace invokes the DRM_IOCTL_PANTHOR_DEV_QUERY ioctl with a buffer size smaller than the kernel's GpuInfo struct (for example, from older userspace clients), the UserSliceWriter::write() method strictly enforces that the length must be at least the size of the structure and returns -EFAULT if it is not.
The C driver permits this as long as the size is at least the documented min_size, copying only the truncated structure. Could this strict size check in the Rust driver break backwards compatibility for older userspace?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v10 2/7 drm/tyr: add a generic slot manager
In reply to Deborah BrouwerFrom: Boris Brezillon <[email protected]>
Introduce a generic slot manager to dynamically allocate limited hardware slots to software "seats". It can be used for both address space (AS) and command stream group (CSG) slots.
The slot manager initially assigns seats to its free slots. It will continue to reuse the same slot for a seat, as long as another seat does not start to use the slot in the interim.
When contention arises because all of the slots are allocated, the slot manager will lazily evict and reuse slots that have become idle (if any).
The seat state is protected using the LockedBy pattern with the same lock that guards the SlotManager. This ensures the seat state stays consistent across slot operations.
Hardware specific behaviour is controlled through the SlotManager's specific manager type that implements the SlotOperations trait.
--- drivers/gpu/drm/tyr/slot.rs | 405 ++++++++++++++++++++++++++++++++++++++++++++ drivers/gpu/drm/tyr/tyr.rs | 1 + 2 files changed, 406 insertions(+)
drivers/gpu/drm/tyr/slot.rs new file +405 -0
@@ -0,0 +1,405 @@ +// SPDX-License-Identifier: GPL-2.0 or MIT + +//! Slot management abstraction for limited hardware resources. +//! +//! This module provides a generic [`SlotManager`] that assigns limited hardware +//! slots to logical "seats". A seat represents an entity (such as a virtual memory +//! (VM) address space) that needs access to a hardware slot. +//! +//! The [`SlotManager`] tracks slot allocation using sequence numbers (seqno) to detect +//! when a seat's binding has been invalidated. When a seat requests activation, +//! the manager will either reuse the seat's existing slot (if still valid), +//! allocate a free slot (if any are available), or evict the oldest idle slot if any +//! slots are idle. +//! +//! Hardware-specific behavior is customized by implementing the [`SlotOperations`] +//! trait, which allows callbacks when slots are activated or evicted. +//! +//! This is currently used for managing address space slots in the GPU, and it will +//! also be used to manage Command Stream Group (CSG) interface slots in the future. +//! +//! [SlotOperations]: crate::slot::SlotOperations +//! [SlotManager]: crate::slot::SlotManager +#![expect(dead_code)] + +use core::{ + mem, + ops::{ +Deref , +DerefMut ,// + },// +}; + +use kernel::{ + prelude::*, + sync::LockedBy ,// +}; + +/// Seat information. +/// +/// This can't be accessed directly by the element embedding a `Seat`, +/// but is used by the generic slot manager logic to control residency +/// of a certain object on a hardware slot. +pub (crate )struct SeatInfo { +/// Slot used by this seat. +/// +/// This index is only valid if the slot pointed to by this index +/// has its `SlotInfo::seqno` match `SeatInfo::seqno`. Otherwise, +/// it means the object has been evicted from the hardware slot, +/// and a new slot needs to be acquired to make this object +/// resident again. + slot:u8 , + +/// Sequence number encoding the last time this seat was active. +/// We also use it to check if a slot is still bound to a seat. + seqno:u64 , +} + +/// Seat state. +/// +/// This is meant to be embedded in the object that wants to acquire +/// hardware slots. It also starts in the `Seat::NoSeat` state, and +/// the slot manager will change the object value when an active/evict +/// request is issued. +#[derive( Default )] +pub (crate )enum Seat { +#[expect(clippy::enum_variant_names)] +/// Resource is not resident. +/// +/// All objects start with a seat in the `Seat::NoSeat` state. The seat also +/// gets back to that state if the user requests eviction. It +/// can also end up in that state next time an operation is done +/// on a `Seat::Idle` seat and the slot manager finds out this +/// object has been evicted from the slot. +#[default] +NoSeat , + +/// Resource is actively used and resident. +/// +/// When a seat is in the `Seat::Active` state, it can't be evicted, and the +/// slot pointed to by `SeatInfo::slot` is guaranteed to be reserved +/// for this object as long as the seat stays active. +Active (SeatInfo ), + +/// Resource is idle and might or might not be resident. +/// +/// When a seat is in the`Seat::Idle` state, we can't know for sure if the +/// object is resident or evicted until the next request we issue +/// to the slot manager. This tells the slot manager it can +/// reclaim the underlying slot if needed. +/// In order for the hardware to use this object again, the seat +/// needs to be turned into an `Seat::Active` state again +/// with a `SlotManager::activate()` call. +Idle (SeatInfo ), +} + +impl Seat { +/// Get the slot index this seat is pointing to. +/// +/// If the seat is not `Seat::Active` we can't trust the +/// `SeatInfo`. In that case `None` is returned, otherwise +/// `Some(SeatInfo::slot)` is returned. +pub (crate )fn slot (&self) ->Option <u8 > { +match self { +Self ::Active (info) =>Some (info.slot), + _ =>None , + } + } +} + +/// Information related to a slot. +struct SlotInfo <D > { +/// Type specific data attached to a slot. + slot_data:D , + +/// Sequence number from when this slot was last activated. + seqno:u64 , +} + +/// Slot state. +#[derive( Default )] +enum Slot <D > { +/// Slot is free. +#[default] +Free , + +/// Slot is active. +Active (SlotInfo <D >), + +/// Slot is idle. +Idle (SlotInfo <D >), +} + +pub (crate )type LockedSeat <T ,const MAX_SLOTS :usize > =LockedBy <Seat ,SlotManager <T ,MAX_SLOTS >>; + +/// Trait describing the slot-related operations. +pub (crate )trait SlotOperations <const MAX_SLOTS :usize >:Sized { +/// Implementation-specific data associated with each slot. +type SlotData ; + +/// Returns the seat belonging to this slot data. +fn seat (slot_data: &Self ::SlotData ) -> &LockedSeat <Self ,MAX_SLOTS >; + +/// Called when a slot is being activated for a seat. +fn activate (&mut self, _slot_idx:usize , _slot_data: &Self ::SlotData ) ->Result { +Ok (()) + } + +/// Called when a slot is being evicted and freed. +fn evict (&mut self, _slot_idx:usize , _slot_data: &Self ::SlotData ) ->Result { +Ok (()) + } +} + +/// A generic slot manager that provides access to a limited number of hardware slots. +pub (crate )struct SlotManager <T :SlotOperations <MAX_SLOTS >,const MAX_SLOTS :usize > { +/// A specific implementation of the generic slot manager. + manager:T , + +/// Number of slots actually available. + slot_count:usize , + +/// Slot array used to track the state of each slot. + slots: [Slot <T ::SlotData >;MAX_SLOTS ], + +/// Sequence number incremented each time a Seat is successfully activated + use_seqno:u64 , +} + +impl <T :SlotOperations <MAX_SLOTS >,const MAX_SLOTS :usize >SlotManager <T ,MAX_SLOTS > { +/// Creates a specific instance of a slot manager. +pub (crate )fn new (manager:T , slot_count:usize ) ->Result <Self > { +if slot_count ==0 { +return Err (EINVAL ); + } +if slot_count >MAX_SLOTS { +return Err (EINVAL ); + } +// Since the slot index is stored in SeatInfo as a u8, the maximum number of slots is 256. +if slot_count > u8::MAX as usize +1 { +return Err (EINVAL ); + } + +Ok (Self { + manager, + slot_count, + slots: [const {Slot ::Free };MAX_SLOTS ], + use_seqno:1 , + }) + } + +/// Records a newly activated slot for the given seat. +/// The slot manager takes ownership of the hardware-specific slot data. +fn record_active_slot (&mut self, slot_idx:usize , slot_data:T ::SlotData ) { +let cur_seqno = self.use_seqno; + + *T ::seat (&slot_data).access_mut (self) =Seat ::Active (SeatInfo { + slot: slot_idxas u8 , + seqno: cur_seqno, + }); + + self.slots[slot_idx] =Slot ::Active (SlotInfo { + slot_data, + seqno: cur_seqno, + }); + + self.use_seqno +=1 ; + } + +/// Reactivates an active/idle slot for a given seat without reprogramming the hardware. +/// The SlotManager reuses the existing slot_data. This ensures that the hardware-specific +/// information is not changed between subsequent uses. It also ensures that resources +/// owned by the existing slot_data remain alive while the hardware is configured to use them. +fn reactivate_slot (&mut self, slot_idx:usize , slot_data: &T ::SlotData ) ->Result { +let cur_seqno = self.use_seqno; + +let mut slot_info =match mem::take (&mut self.slots[slot_idx]) { +Slot ::Active (slot_info) |Slot ::Idle (slot_info) => slot_info, +Slot ::Free => { + *T ::seat (slot_data).access_mut (self) =Seat ::NoSeat ; +return Err (EINVAL ); + } + }; + + *T ::seat (slot_data).access_mut (self) =Seat ::Active (SeatInfo { + slot: slot_idxas u8 , + seqno: cur_seqno, + }); + + slot_info.seqno = cur_seqno; + self.slots[slot_idx] =Slot ::Active (slot_info); + + self.use_seqno +=1 ; + +Ok (()) + } + +/// Activates a slot for the given seat. +fn activate_slot (&mut self, slot_idx:usize , slot_data:T ::SlotData ) ->Result { + self.manager.activate (slot_idx, &slot_data)?; + self.record_active_slot (slot_idx, slot_data); +Ok (()) + } + +/// Finds a slot for the given seat. A free slot is preferred, but if none +/// are available, the oldest idle slot is evicted and reused. Otherwise, if +/// there are no free or idle slots, return [`EBUSY`]. +fn allocate_slot (&mut self, slot_data:T ::SlotData ) ->Result { +let slots = &self.slots[..self.slot_count]; + +let mut idle_slot_idx =None ; +let mut idle_slot_seqno:u64 =0 ; + +for (slot_idx, slot)in slots.iter ().enumerate () { +match slot { +Slot ::Free => { +return self.activate_slot (slot_idx, slot_data); + } +Slot ::Idle (slot_info) => { +if idle_slot_idx.is_none () || slot_info.seqno < idle_slot_seqno { + idle_slot_idx =Some (slot_idx); + idle_slot_seqno = slot_info.seqno; + } + } +Slot ::Active (_) => (), + } + } + +match idle_slot_idx { +Some (slot_idx) => { +// Lazily evict idle slot just before it is reused. +if let Slot ::Idle (slot_info) = &self.slots[slot_idx] { + self.manager.evict (slot_idx, &slot_info.slot_data)?; + mem::take (&mut self.slots[slot_idx]); + } + self.activate_slot (slot_idx, slot_data) + } +None =>Err (EBUSY ), + } + } + +/// Converts an active slot and its seat to idle state. +fn idle_slot (&mut self, slot_idx:usize , locked_seat: &LockedSeat <T ,MAX_SLOTS >) ->Result { +let slot = mem::take (&mut self.slots[slot_idx]); + + self.slots[slot_idx] =match slot { +// If the slot was active, make it idle. +Slot ::Active (slot_info) =>Slot ::Idle (slot_info), + +// Preserve an already-idle slot. +Slot ::Idle (slot_info) =>Slot ::Idle (slot_info), + +// A free slot remains free. +Slot ::Free =>Slot ::Free , + }; + +// If the seat was active, make it idle, or keep it idle if it was already idle. + *locked_seat.access_mut (self) =match locked_seat.access (self) { +Seat ::Active (seat_info) |Seat ::Idle (seat_info) =>Seat ::Idle (SeatInfo { + slot: seat_info.slot, + seqno: seat_info.seqno, + }), +Seat ::NoSeat =>Seat ::NoSeat , + }; +Ok (()) + } + +/// Evicts an active or idle slot: calls the eviction callback and marks the slot as free +/// and the seat as NoSeat. +fn evict_slot (&mut self, slot_idx:usize , locked_seat: &LockedSeat <T ,MAX_SLOTS >) ->Result { +match &self.slots[slot_idx] { +Slot ::Active (slot_info) |Slot ::Idle (slot_info) => { +// If hardware eviction fails (e.g. times out), the slot retains +// its SlotData so that any resources still referenced by the hardware +// will remain alive. This prevents use-after-free errors. + self.manager.evict (slot_idx, &slot_info.slot_data)?; + mem::take (&mut self.slots[slot_idx]); + } + _ => (), + } + + *locked_seat.access_mut (self) =Seat ::NoSeat ; +Ok (()) + } + +/// Checks that the seat state matches the slot's state. +/// If they don't match, the seat is stale and is reset to `NoSeat`. +fn check_seat (&mut self, locked_seat: &LockedSeat <T ,MAX_SLOTS >) { +let (slot_idx, seat_seqno, is_active) =match locked_seat.access (self) { +Seat ::Active (seat_info) => (seat_info.slotas usize , seat_info.seqno,true ), +Seat ::Idle (seat_info) => (seat_info.slotas usize , seat_info.seqno,false ), + _ =>return , + }; + +let valid =if is_active { + !kernel::warn_on!(!matches! ( + &self.slots[slot_idx], +Slot ::Active (slot_info)if slot_info.seqno == seat_seqno + )) + }else { +matches! ( + &self.slots[slot_idx], +Slot ::Idle (slot_info)if slot_info.seqno == seat_seqno + ) + }; + +if !valid { + *locked_seat.access_mut (self) =Seat ::NoSeat ; + } + } + +/// Activates a resource on any available/reclaimable slot. +pub (crate )fn activate (&mut self, slot_data:T ::SlotData ) ->Result { + self.check_seat (T ::seat (&slot_data)); + +// Copy out only the slot index so the borrow of slot_data ends here. +let slot_idx =match T ::seat (&slot_data).access (self) { +Seat ::Active (seat_info) |Seat ::Idle (seat_info) =>Some (seat_info.slotas usize ), +Seat ::NoSeat =>None , + }; + +match slot_idx { +Some (slot_idx) => self.reactivate_slot (slot_idx, &slot_data), +None => self.allocate_slot (slot_data), + } + } + +/// Flag a resource as idle. This method will be used for user VM support. +#[expect(dead_code)] +pub (crate )fn idle (&mut self, locked_seat: &LockedSeat <T ,MAX_SLOTS >) ->Result { + self.check_seat (locked_seat); +if let Seat ::Active (seat_info) = locked_seat.access (self) { + self.idle_slot (seat_info.slotas usize , locked_seat)?; + } +Ok (()) + } + +/// Evict a resource from its slot. +pub (crate )fn evict (&mut self, locked_seat: &LockedSeat <T ,MAX_SLOTS >) ->Result { + self.check_seat (locked_seat); + +match locked_seat.access (self) { +Seat ::Active (seat_info) |Seat ::Idle (seat_info) => { +let slot_idx = seat_info.slotas usize ; + self.evict_slot (slot_idx, locked_seat)?; + } + _ => (), + } + +Ok (()) + } +} + +impl <T :SlotOperations <MAX_SLOTS >,const MAX_SLOTS :usize >Deref for SlotManager <T ,MAX_SLOTS > { +type Target =T ; + +fn deref (&self) -> &Self ::Target { + &self.manager + } +} + +impl <T :SlotOperations <MAX_SLOTS >,const MAX_SLOTS :usize >DerefMut for SlotManager <T ,MAX_SLOTS > { +fn deref_mut (&mut self) -> &mut Self ::Target { + &mut self.manager + } +}
drivers/gpu/drm/tyr/tyr.rs +1 -0
@@ -12,6 +12,7 @@mod gem;mod gpu;mod regs; +mod slot; kernel::module_platform_driver! {type :TyrPlatformDriver ,
Signature · 1 line
2.55.0
PATCH v10 3/7 drm/tyr: add Memory Management Unit (MMU) support
In reply to Deborah BrouwerFrom: Boris Brezillon <[email protected]>
Add Memory Management Unit (MMU) support in Tyr. The MMU module wraps a SlotManager instance to allocate MMU address-space slots for use by virtual memory (VM) address spaces. The MMU's SlotManager uses an AddressSpaceManager to handle the hardware-specific callbacks. For example, the AddressSpaceManager activates and evicts VMs from slots by writing commands to the MMU registers.
Add an implementation block for the MMU's MEMATTR register to provide a method for translating the Memory Attribute Indirection Register (MAIR) format from the pagetable configuration to a format understood by the MMU.
Create an mmu instance during probe, it will be used by subsequent patches in this series.
Wrap the iomem stored in TyrDrmRegistrationData in an Arc. The iomem is stored in the mmu through its AddressSpaceManager. In anticipation of the iomem also being stored in the firmware object, set up shared ownership of the iomem now.
Update Kconfig to add the new MMU and IOMMU dependencies required by this MMU module.
--- drivers/gpu/drm/tyr/Kconfig | 3 + drivers/gpu/drm/tyr/driver.rs | 13 +- drivers/gpu/drm/tyr/mmu.rs | 121 ++++++++ drivers/gpu/drm/tyr/mmu/address_space.rs | 511 +++++++++++++++++++++++++++++++ drivers/gpu/drm/tyr/regs.rs | 135 +++++++- drivers/gpu/drm/tyr/slot.rs | 1 - drivers/gpu/drm/tyr/tyr.rs | 1 + 7 files changed, 780 insertions(+), 5 deletions(-)
drivers/gpu/drm/tyr/Kconfig +3 -0
@@ -5,9 +5,12 @@ config DRM_TYR depends on DRM=y depends on RUST depends on ARM || ARM64 || COMPILE_TEST + depends on MMU depends on !GENERIC_ATOMIC64 # for IOMMU_IO_PGTABLE_LPAE depends on COMMON_CLK + depends on IOMMU_SUPPORT default n + select IOMMU_IO_PGTABLE_LPAE select RUST_DRM_GEM_SHMEM_HELPER help Rust DRM driver for ARM Mali CSF-based GPUs.
drivers/gpu/drm/tyr/driver.rs +10 -3
@@ -28,7 +28,10 @@ regulator, regulator::Regulator, sizes::SZ_2M, - sync::Mutex, + sync::{ + Arc, + Mutex, // + }, time, // };
@@ -37,6 +40,7 @@ gem::BoData, gpu, gpu::GpuInfo, + mmu::Mmu, regs::gpu_control::*, // };
@@ -70,7 +74,7 @@ pub(crate) struct TyrDrmRegistrationData<'drm> { regulators: Mutex<Regulators>,/// GPU MMIO register mapping. - pub(crate) iomem: IoMem<'drm>, + pub(crate) iomem: Arc<IoMem<'drm>>,/// GPU information read from hardware during probe. pub(crate) gpu_info: GpuInfo,
@@ -121,7 +125,8 @@ fn probe<'bound>(let sram_regulator =Regulator ::<regulator::Enabled >::get (pdev.as_ref (),c"sram" )?;let request = pdev.io_request_by_index (0 ).ok_or (ENODEV )?; -let iomem = request.iomap_sized ::<SZ_2M >()?; + +let iomem =Arc ::new (request.iomap_sized ::<SZ_2M >()?,GFP_KERNEL )?;issue_soft_reset (pdev.as_ref (), &iomem)?; gpu::l2_power_on (pdev.as_ref (), &iomem)?;
@@ -139,6 +144,8 @@ fn probe<'bound>(let unreg_dev = drm::UnregisteredDevice ::<TyrDrmDriver >::new (pdev,Ok (()))?; +let _mmu =Mmu ::new (pdev.as_ref (), iomem.as_arc_borrow (), &gpu_info)?; + let reg_data = try_pin_init!(TyrDrmRegistrationData { pdev, clks <- new_mutex!(Clocks {
drivers/gpu/drm/tyr/mmu.rs new file +121 -0
121 lines not on this page · raw message
drivers/gpu/drm/tyr/mmu/address_space.rs new file +511 -0
511 lines not on this page · raw message
drivers/gpu/drm/tyr/regs.rs +134 -1
160 lines not on this page · raw message
drivers/gpu/drm/tyr/slot.rs +0 -1
@@ -20,7 +20,6 @@//! //! [SlotOperations]: crate::slot::SlotOperations //! [SlotManager]: crate::slot::SlotManager -#![expect(dead_code)] use core::{ mem,
drivers/gpu/drm/tyr/tyr.rs +1 -0
@@ -11,6 +11,7 @@mod file;mod gem;mod gpu; +mod mmu;mod regs;mod slot;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v10 3/7] drm/tyr: add Memory Management Unit (MMU) support
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Missing MmuCommand::Unlock in as_end_update() leaves the address space locked. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Missing MmuCommand::Unlock in as_end_update() leaves the address space locked. --
--- Patch [3]: [PATCH v10 3/7] drm/tyr: add Memory Management Unit (MMU) support --- commit 4673c22526eac7702f0e2418212a28a74a5e56d8
Author: Boris Brezillon <[email protected]>
drm/tyr: add Memory Management Unit (MMU) support
Add Memory Management Unit (MMU) support in Tyr, which wraps a SlotManager instance to allocate MMU address-space slots for virtual memory. This includes an AddressSpaceManager for MMU hardware register operations such as locking and flushing address spaces.
Quoted 5 lines
drivers/gpu/drm/tyr/mmu/address_space.rs new file +0
-0
[ ... ]
Quoted 8 lines
drivers/gpu/drm/tyr/mmu/address_space.rs new file +8
-0+/// Locks a region of the translation tables for an atomic update. +/// +/// Programs the MMU LOCKADDR register for the given address space and issues +/// the lock command. The hardware rounds the requested range up to a +/// power-of-two region aligned to its size. +/// +/// Returns an error if the slot is invalid or if register writes/commands fail. +fn as_start_update (&mut self, as_nr:usize , region: &Range <u64 >) ->Result {
[ ... ]
drivers/gpu/drm/tyr/mmu/address_space.rs line 426
Quoted 9 lines
drivers/gpu/drm/tyr/mmu/address_space.rs new file +9
-0+ self.as_send_cmd_and_wait (as_nr,MmuCommand ::Lock ) + } + +/// Completes an atomic translation table update. +/// +/// Returns an error if the slot is invalid or if the flush command fails. +fn as_end_update (&mut self, as_nr:usize ) ->Result { + self.validate_as_slot (as_nr)?; + self.as_send_cmd_and_wait (as_nr,MmuCommand ::FlushPt )?;
[Severity: High]
Does this function also need to send MmuCommand::Unlock to release the locked region?
The start of the update sequence in as_start_update() locks the region by sending MmuCommand::Lock, but as_end_update() currently only sends MmuCommand::FlushPt and omits the unlock command.
If the address space remains locked, GPU accesses to the updated virtual memory region could be permanently blocked. This might lead to GPU hangs or command timeouts when userspace triggers VM mapping or unmapping operations.
Quoted 2 lines
drivers/gpu/drm/tyr/mmu/address_space.rs new file +2
-0+Ok (()) + }
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Danilo Krummrich
maintainerRe: [PATCH v10 3/7] drm/tyr: add Memory Management Unit (MMU) support
In reply to Deborah Brouwerdrivers/gpu/drm/tyr/mmu.rs line 51
Quoted 3 lines · Deborah Brouwer
drivers/gpu/drm/tyr/mmu.rs new file +3
-0+/// Locked wrapper for carrying out virtual memory (VM) operations on the MMU. +#[pin_data] +pub (crate )struct Mmu <'drm> {
This shouldn't be 'drm, but 'mmu or just something generic like 'a.
The rationale is that Mmu might be shorter lived than 'drm once we have self-referencial pin-init. Currently it is straight forward, such as in
struct Foo<'foo> {
dev: &'foo platform::Device<Bound>,
}
struct Data<'bound> {
foo: Foo<'bound>,
}where the lifetime of dev really ties back to 'bound. However, with self-referencial pin-init it could looks like this:
struct Foo<'foo> {
dev: &'foo platform::Device<Bound>,
io: &'foo IoMem<'foo>,
}
struct Data<'bound> {
foo: Foo<'io>,
io: IoMem<'bound>,
}Now Foo is not constrained to 'bound anymore, as this would be longer lived than io, so it has to capture 'io instead in order to still compile.
Re: [PATCH v10 3/7] drm/tyr: add Memory Management Unit (MMU) support
Quoted 31 lines · Danilo Krummrich
drivers/gpu/drm/tyr/mmu.rs line 51
Quoted 3 lines · Deborah Brouwer
drivers/gpu/drm/tyr/mmu.rs new file +3
-0+/// Locked wrapper for carrying out virtual memory (VM) operations on the MMU. +#[pin_data] +pub (crate )struct Mmu <'drm> {This shouldn't be 'drm, but 'mmu or just something generic like 'a.
The rationale is that Mmu might be shorter lived than 'drm once we have self-referencial pin-init. Currently it is straight forward, such as in
struct Foo<'foo> { dev: &'foo platform::Device<Bound>, } struct Data<'bound> { foo: Foo<'bound>, }where the lifetime of
devreally ties back to 'bound. However, with self-referencial pin-init it could looks like this:struct Foo<'foo> { dev: &'foo platform::Device<Bound>, io: &'foo IoMem<'foo>, } struct Data<'bound> { foo: Foo<'io>, io: IoMem<'bound>, }
Quoted 2 lines
Now Foo is not constrained to 'bound anymore, as this would be longer lived than
io, so it has to capture 'io instead in order to still compile.
Danilo, would you be ok with renaming this lifetime as a follow up patch? Currently, if I understand this correctly, the mmu lifetime is still tied to the DRM registration data, so the name is not inaccurate for Tyr at least as it stands right now.
Danilo Krummrich
maintainerRe: [PATCH v10 3/7] drm/tyr: add Memory Management Unit (MMU) support
Quoted 32 lines · Deborah Brouwer
Quoted 31 lines · Danilo Krummrich
drivers/gpu/drm/tyr/mmu.rs line 51
Quoted 3 lines · Deborah Brouwer
drivers/gpu/drm/tyr/mmu.rs new file +3
-0+/// Locked wrapper for carrying out virtual memory (VM) operations on the MMU. +#[pin_data] +pub (crate )struct Mmu <'drm> {This shouldn't be 'drm, but 'mmu or just something generic like 'a.
The rationale is that Mmu might be shorter lived than 'drm once we have self-referencial pin-init. Currently it is straight forward, such as in
struct Foo<'foo> { dev: &'foo platform::Device<Bound>, } struct Data<'bound> { foo: Foo<'bound>, }where the lifetime of
devreally ties back to 'bound. However, with self-referencial pin-init it could looks like this:struct Foo<'foo> { dev: &'foo platform::Device<Bound>, io: &'foo IoMem<'foo>, } struct Data<'bound> { foo: Foo<'io>, io: IoMem<'bound>, }
Quoted 5 lines
Quoted 2 lines
Now Foo is not constrained to 'bound anymore, as this would be longer lived than
io, so it has to capture 'io instead in order to still compile.Danilo, would you be ok with renaming this lifetime as a follow up patch?
Sure; note that I also said that I'm happy for this to be a follow-up in the previous version. :)
Quoted 3 lines
Currently, if I understand this correctly, the mmu lifetime is still tied to the DRM registration data, so the name is not inaccurate for Tyr at least as it stands right now.
With the current implementation, the Mmu lifetime technically ties back to 'bound; the DRM registration data itself is shorter lived (it is dropped before 'bound ends).
The reason I mentioned not to use the 'bound lifetime name in the first place is that we agreed to not use the 'bound lifetime name on structs that may be shorter lived than the device being bound, which is true for anything other than the bus device private data struct.
PATCH v10 4/7 drm/tyr: add GPU virtual memory (VM) support
In reply to Deborah BrouwerFrom: Boris Brezillon <[email protected]>
Add GPU virtual address space management using the DRM GPUVM framework. Each virtual memory (VM) space is backed by ARM64 LPAE Stage 1 page tables and can be mapped into hardware address space (AS) slots for GPU execution.
The implementation provides memory isolation and virtual address allocation. VMs support mapping GEM buffer objects with configurable protection flags (readonly, noexec, uncached) and handle both 4KB and 2MB page sizes. A new_dummy_object() helper is provided to create a dummy GEM object for use as a GPUVM root.
The vm module integrates with the MMU for address space activation and provides map/unmap/remap operations with page table synchronization.
--- drivers/gpu/drm/tyr/Kconfig | 1 + drivers/gpu/drm/tyr/driver.rs | 4 +- drivers/gpu/drm/tyr/gem.rs | 26 +- drivers/gpu/drm/tyr/mmu.rs | 1 - drivers/gpu/drm/tyr/tyr.rs | 1 + drivers/gpu/drm/tyr/vm.rs | 950 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 978 insertions(+), 5 deletions(-)
drivers/gpu/drm/tyr/Kconfig +1 -0
@@ -12,6 +12,7 @@ config DRM_TYR default n select IOMMU_IO_PGTABLE_LPAE select RUST_DRM_GEM_SHMEM_HELPER + select RUST_DRM_GPUVM help Rust DRM driver for ARM Mali CSF-based GPUs.
drivers/gpu/drm/tyr/driver.rs +2 -2
@@ -37,7 +37,7 @@use crate ::{ file::TyrDrmFileData , - gem::BoData , + gem::Bo , gpu, gpu::GpuInfo , mmu::Mmu ,
@@ -194,7 +194,7 @@ impl drm::Driver for TyrDrmDriver {type Data = ();type RegistrationData <'drm> =TyrDrmRegistrationData <'drm>;type File =TyrDrmFileData ; -type Object = drm::gem::shmem::Object <BoData >; +type Object =Bo ;type ParentDevice <Ctx :DeviceContext > = platform::Device <Ctx >;const INFO : drm::DriverInfo =INFO ;
drivers/gpu/drm/tyr/gem.rs +24 -2
@@ -5,8 +5,12 @@//! DRM's GEM subsystem with shmem backing. use kernel::{ - drm::gem, - prelude::*,// + drm::gem::{ + self, + shmem,// + }, + prelude::*, + sync::aref::ARef ,// };use crate ::driver::{
@@ -34,3 +38,21 @@ fn new(_dev: &TyrDrmDevice, _size: usize, args: BoCreateArgs) -> impl PinInit<Setry_pin_init! (Self { flags: args.flags }) } } + +/// Type alias for Tyr GEM buffer objects. +pub (crate )type Bo = gem::shmem::Object <BoData >; + +/// Creates a dummy GEM object to serve as the root of a GPUVM. +pub (crate )fn new_dummy_object (ddev: &TyrDrmDevice ) ->Result <ARef <Bo >> { +let bo =Bo ::new ( + ddev, +4096 , + shmem::ObjectConfig { + map_wc:true , + parent_resv_obj:None , + }, +BoCreateArgs { flags:0 }, + )?; + +Ok (bo) +}
drivers/gpu/drm/tyr/mmu.rs +0 -1
@@ -10,7 +10,6 @@//! The [`SlotManager`] manages the assignment of virtual address spaces to hardware address-space //! (AS) slots. MMU commands such as updates and flushes are carried out by the //! [`AddressSpaceManager`] which actually writes to the MMU registers. -#![expect(dead_code)] use core::ops::Range ;
drivers/gpu/drm/tyr/tyr.rs +1 -0
@@ -14,6 +14,7 @@mod mmu;mod regs;mod slot; +mod vm; kernel::module_platform_driver! {type :TyrPlatformDriver ,
drivers/gpu/drm/tyr/vm.rs new file +950 -0
950 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v10 4/7] drm/tyr: add GPU virtual memory (VM) support
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] `map_bo_range` lacks a bounds check against the VM's virtual address range, allowing out-of-bounds…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] map_bo_range lacks a bounds check against the VM's virtual address range, allowing out-of-bounds MMU update commands to be sent to the hardware.
- [High] Partial unmap of 2MB page table blocks causes the ARM LPAE io-pgtable backend to reject the operation, breaking drm_gpuvm unmap and remap. --
--- Patch [4]: [PATCH v10 4/7] drm/tyr: add GPU virtual memory (VM) support --- commit 7f9a396490c23472f40e677be97d921859c50864
Author: Boris Brezillon <[email protected]>
drm/tyr: add GPU virtual memory (VM) support
Add GPU virtual address space management using the DRM GPUVM framework. Each virtual memory (VM) space is backed by ARM64 LPAE Stage 1 page tables and can be mapped into hardware address space (AS) slots for GPU execution.
The implementation provides memory isolation and virtual address allocation. VMs support mapping GEM buffer objects with configurable protection flags (readonly, noexec, uncached) and handle both 4KB and 2MB page sizes. A new_dummy_object() helper is provided to create a dummy GEM object for use as a GPUVM root.
Quoted 4 lines
drivers/gpu/drm/tyr/vm.rs new file +0
-0
[ ... ]
Quoted 8 lines
drivers/gpu/drm/tyr/vm.rs new file +8
-0+pub (crate )fn map_bo_range ( + &self, + bo: &Bo , + bo_offset:u64 , + map_size:u64 , + va:u64 , + flags:VmMapFlags , + ) ->Result {
[ ... ]
Quoted 6 lines
drivers/gpu/drm/tyr/vm.rs new file +6
-0+let bo_end = bo_offset.checked_add (map_size).ok_or (EINVAL )?; + +if bo_end > bo_size { +dev_err! ( + self.dev, +"BO mapping range {:#x}..{:#x} exceeds BO size {:#x}" ,
drivers/gpu/drm/tyr/vm.rs line 517
Quoted 10 lines
drivers/gpu/drm/tyr/vm.rs new file +10
-0+ bo_offset, + bo_end, + bo_size + ); +return Err (EINVAL ); + } + +let va_end:u64 = va.checked_add (map_size).ok_or (EINVAL )?; + +let req =VmOpRequest {
[Severity: High]
Does this code need to check if the calculated va_end exceeds the VM's virtual address range?
If a user-space caller provides a very large map_size, va + map_size could exceed the VM's valid range. Because there's no bounds check against self.va_range.end here (unlike in unmap_range()), the out-of-bounds region will be passed to PtUpdateContext::new(). This might issue a hardware lock command to the MMU before drm_gpuvm rejects the mapping. Could this lead to unhandled hardware faults or device hangs?
Quoted 7 lines
drivers/gpu/drm/tyr/vm.rs new file +7
-0+ op_type: VmOpType::Map(VmMapArgs { + vm_bo: self.gpuvm.obtain(bo, ())?, + flags, + bo_offset, + }), + region: va..va_end, + };
[ ... ]
Quoted 5 lines
drivers/gpu/drm/tyr/vm.rs new file +5
-0+fn sm_step_remap <'op>( + &mut self, + op:OpRemap <'op,Self >, + context: &mut Self ::SmContext <'_>, + ) ->Result <OpRemapped <'op,Self >,Error > {
[ ... ]
drivers/gpu/drm/tyr/vm.rs line 757
Quoted 5 lines
drivers/gpu/drm/tyr/vm.rs new file +5
-0+let unmap_length = unmap_end - unmap_start; + +if unmap_length >0 { +let region = unmap_start..(unmap_start + unmap_length); +pt_unmap (context.dev, context.pt, region.clone ()).inspect_err (|e| {
[Severity: High]
Will this fail if the region is a partial unmap of a 2MB page table block?
If a buffer object is naturally aligned to 2MB, pt_map() may opportunistically use 2MB block descriptors. If a caller later attempts to unmap a 4KB sub-region, sm_step_remap() will pass this 4KB region to pt_unmap().
The ARM LPAE io-pgtable backend (arm_lpae_unmap_pages) does not automatically split huge pages, so it might encounter the 2MB leaf descriptor, reject the operation, and return 0, causing pt_unmap() to return EINVAL.
Could this leave the DRM GPUVM tree out of sync with the hardware page tables and leak the mapping in the GPU?
Quoted 9 lines
drivers/gpu/drm/tyr/vm.rs new file +9
-0+dev_err! ( + context.dev, +"Failed to unmap remap region {:#x}..{:#x}: {:?}" , + region.start, + region.end, + e + ); + })?; + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Alice Ryhl
maintainerRe: [PATCH v10 4/7] drm/tyr: add GPU virtual memory (VM) support
In reply to Deborah BrouwerQuoted 13 lines · Deborah Brouwer
From: Boris Brezillon <[email protected]>
Add GPU virtual address space management using the DRM GPUVM framework. Each virtual memory (VM) space is backed by ARM64 LPAE Stage 1 page tables and can be mapped into hardware address space (AS) slots for GPU execution.
The implementation provides memory isolation and virtual address allocation. VMs support mapping GEM buffer objects with configurable protection flags (readonly, noexec, uncached) and handle both 4KB and 2MB page sizes. A
new_dummy_object()helper is provided to create a dummy GEM object for use as a GPUVM root.
Quoted 8 lines
The vm module integrates with the MMU for address space activation and provides map/unmap/remap operations with page table synchronization.
Signed-off-by Boris Brezillon <[email protected]>Co-developed-by Daniel Almeida <[email protected]>Signed-off-by Daniel Almeida <[email protected]>Co-developed-by Deborah Brouwer <[email protected]>Signed-off-by Deborah Brouwer <[email protected]>
There's a 32-bit build failure :(
error[E0308]: mismatched types
--> /home/runner/work/linux/linux/linux/drivers/gpu/drm/tyr/vm.rs:873:17
|
871 | pt.map_pages(
| --------- arguments to this method are incorrect
872 | curr_iova,
873 | curr_paddr,
| ^^^^^^^^^^ expected `u32`, found `u64`
|
note: method defined here
--> /home/runner/work/linux/linux/linux/rust/kernel/iommu/pgtable.rs:152:19
|
152 | pub unsafe fn map_pages(
| ^^^^^^^^^
help: you can convert a `u64` to a `u32` and panic if the converted
value doesn't fit
|
873 | curr_paddr.try_into().unwrap(),
| ++++++++++++++++++++Re: [PATCH v10 4/7] drm/tyr: add GPU virtual memory (VM) support
Quoted 37 lines · Alice Ryhl
Quoted 13 lines · Deborah Brouwer
From: Boris Brezillon <[email protected]>
Add GPU virtual address space management using the DRM GPUVM framework. Each virtual memory (VM) space is backed by ARM64 LPAE Stage 1 page tables and can be mapped into hardware address space (AS) slots for GPU execution.
The implementation provides memory isolation and virtual address allocation. VMs support mapping GEM buffer objects with configurable protection flags (readonly, noexec, uncached) and handle both 4KB and 2MB page sizes. A
new_dummy_object()helper is provided to create a dummy GEM object for use as a GPUVM root.Quoted 8 lines
The vm module integrates with the MMU for address space activation and provides map/unmap/remap operations with page table synchronization.
Signed-off-by Boris Brezillon <[email protected]>Co-developed-by Daniel Almeida <[email protected]>Signed-off-by Daniel Almeida <[email protected]>Co-developed-by Deborah Brouwer <[email protected]>Signed-off-by Deborah Brouwer <[email protected]>There's a 32-bit build failure :(
error[E0308]: mismatched types
-->/home/runner/work/linux/linux/linux/drivers/gpu/drm/tyr/vm.rs:873:17| 871 | pt.map_pages( | --------- arguments to this method are incorrect 872 | curr_iova, 873 | curr_paddr, | ^^^^^^^^^^ expected `u32`, found `u64` |note: method defined here
-->/home/runner/work/linux/linux/linux/rust/kernel/iommu/pgtable.rs:152:19
Quoted 8 lines
|
152 | pub unsafe fn map_pages(
| ^^^^^^^^^
help: you can convert a `u64` to a `u32` and panic if the converted
value doesn't fit
|
873 | curr_paddr.try_into().unwrap(),
| ++++++++++++++++++++Alice, I see you fixed it in drm-rust-next by casting curr_paddr to PhysAddr, thank you. I must have added this 32 bit regression trying to double check all of the sizes.
PATCH v10 5/7 drm/tyr: add a kernel buffer object
In reply to Deborah BrouwerIntroduce a buffer object type (KernelBo) for internal driver allocations that are managed by the kernel rather than userspace.
KernelBo wraps a GEM shmem object and automatically handles GPU virtual address space mapping during creation and unmapping on drop. This provides a safe and convenient way for the driver to both allocate and clean up internal buffers for kernel-managed resources.
--- drivers/gpu/drm/tyr/gem.rs | 113 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 109 insertions(+), 4 deletions(-)
drivers/gpu/drm/tyr/gem.rs +109 -4
@@ -4,18 +4,29 @@//! This module provides buffer object (BO) management functionality using //! DRM's GEM subsystem with shmem backing. +use core::ops::Range ; +use kernel::{ drm::gem::{ self, shmem,// }, prelude::*, - sync::aref::ARef ,// + sync::{ + aref::ARef , +Arc ,// + },// }; -use crate ::driver::{ -TyrDrmDevice , -TyrDrmDriver ,// +use crate ::{ + driver::{ +TyrDrmDevice , +TyrDrmDriver ,// + }, + vm::{ +Vm , +VmMapFlags ,// + }, };/// Tyr's DriverObject type for GEM objects.
@@ -56,3 +67,97 @@ pub(crate) fn new_dummy_object(ddev: &TyrDrmDevice) -> Result<ARef<Bo>> {Ok (bo) } + +/// Specifies how to choose a GPU virtual address for a [`KernelBo`]. +/// An automatic VA allocation strategy will be added in the future. +pub (crate )enum KernelBoVaAlloc { +/// Explicit VA address specified by the caller. +#[expect(dead_code)] +Explicit (u64 ), +} + +/// A kernel-owned buffer object with automatic GPU virtual address mapping. +/// +/// This structure represents a buffer object that is created and managed entirely +/// by the kernel driver, as opposed to userspace-created GEM objects. It combines +/// a GEM object with automatic GPU virtual address (VA) space mapping and cleanup. +/// +/// When dropped, the buffer is automatically unmapped from the GPU VA space. +pub (crate )struct KernelBo <'drm> { +/// The underlying GEM buffer object. + bo:ARef <Bo >, +/// The GPU VM this buffer is mapped into. + vm:Arc <Vm <'drm>>, +/// The GPU VA range occupied by this buffer. + va_range:Range <u64 >, +} + +impl <'drm>KernelBo <'drm> { +/// Creates a new kernel-owned buffer object and maps it into GPU VA space. +/// +/// This function allocates a new shmem-backed GEM object and immediately maps +/// it into the specified GPU virtual memory space. The mapping is automatically +/// cleaned up when the [`KernelBo`] is dropped. +#[expect(dead_code)] +pub (crate )fn new ( + ddev: &TyrDrmDevice , + vm:Arc <Vm <'drm>>, + size:u64 , + va_alloc:KernelBoVaAlloc , + flags:VmMapFlags , + ) ->Result <Self > { +if size ==0 { +dev_err! (vm.dev (),"Cannot create KernelBo with size 0" ); +return Err (EINVAL ); + } + +let KernelBoVaAlloc ::Explicit (va) = va_alloc; + +let bo_size = usize::try_from (size).map_err (|_|EOVERFLOW )?; +let va_end = va.checked_add (size).ok_or (EINVAL )?; + +let bo =Bo ::new ( + ddev, + bo_size, + shmem::ObjectConfig { + map_wc:true , + parent_resv_obj:None , + }, +BoCreateArgs { flags:0 }, + )?; + + vm.map_bo_range (&bo,0 , size, va, flags)?; + +Ok (KernelBo { + bo, + vm, + va_range: va..va_end, + }) + } + +#[expect(dead_code)] +pub (crate )fn bo (&self) -> &Bo { + &self.bo + } +} + +impl Drop for KernelBo <'_> { +fn drop (&mut self) { +let va = self.va_range.start; +let size = self.va_range.end - self.va_range.start; + +if let Err (e) = self.vm.unmap_range (va, size) { +// If unmap_range fails, it is still safe to drop the +// KernelBo and its ARef to the GEM buffer object because +// GPUVM also holds a reference to the GEM buffer object. +// The physical pages won't be freed or reallocated. +dev_err! ( + self.vm.dev (), +"Failed to unmap KernelBo range {:#x}..{:#x}: {:?}" , + self.va_range.start, + self.va_range.end, + e + ); + } + } +}
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v10 5/7] drm/tyr: add a kernel buffer object
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Resource leak: `KernelBo::drop` permanently leaks the GEM buffer object and its virtual mapping…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Resource leak: KernelBo::drop permanently leaks the GEM buffer object and its virtual mapping if vm.unmap_range() fails. --
--- Patch [5]: [PATCH v10 5/7] drm/tyr: add a kernel buffer object --- commit f17ba69672f18401371cf98a1da7881e0b938c23
Author: Deborah Brouwer <[email protected]>
drm/tyr: add a kernel buffer object
This commit introduces a buffer object type (KernelBo) for internal driver allocations managed by the kernel. It wraps a GEM shmem object and automatically handles GPU virtual address space mapping during creation and unmapping on drop.
Quoted 4 lines
drivers/gpu/drm/tyr/gem.rs +0
-0
[ ... ]
drivers/gpu/drm/tyr/gem.rs pub(crate) fn new_dummy_object(ddev: &TyrDrmDevice) -> Result<ARef<Bo>> { · line 149
Quoted 6 lines
drivers/gpu/drm/tyr/gem.rs +6
-0+impl Drop for KernelBo <'_> { +fn drop (&mut self) { +let va = self.va_range.start; +let size = self.va_range.end - self.va_range.start; + +if let Err (e) = self.vm.unmap_range (va, size) {
[Severity: Medium]
Does this leak the GEM buffer object if unmap_range() fails?
If unmap_range() encounters an error (such as a kmalloc failure due to memory pressure when preallocating GPUVA objects to handle a mapping split), it aborts the unmap operation.
Because the error is caught and ignored here, the drm_gpuvm will retain its reference to the GEM object indefinitely, leaking it along with its physical pages.
Quoted 14 lines
drivers/gpu/drm/tyr/gem.rs +14
-0+// If unmap_range fails, it is still safe to drop the +// KernelBo and its ARef to the GEM buffer object because +// GPUVM also holds a reference to the GEM buffer object. +// The physical pages won't be freed or reallocated. +dev_err! ( + self.vm.dev (), +"Failed to unmap KernelBo range {:#x}..{:#x}: {:?}" , + self.va_range.start, + self.va_range.end, + e + ); + } + } +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v10 6/7 drm/tyr: add parser for firmware binary
In reply to Deborah BrouwerFrom: Daniel Almeida <[email protected]>
Add a parser for the Mali CSF GPU firmware binary format. The firmware consists of a header followed by entries describing how to load firmware sections into the MCU's memory.
The parser extracts section metadata including virtual address ranges, data byte offsets within the binary, and section flags controlling permissions and cache modes. It validates the basic firmware structure and alignment and ignores protected-mode sections for now.
--- drivers/gpu/drm/tyr/fw/parser.rs | 588 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 588 insertions(+)
drivers/gpu/drm/tyr/fw/parser.rs new file +588 -0
@@ -0,0 +1,588 @@ +// SPDX-License-Identifier: GPL-2.0 or MIT + +//! Firmware binary parser for Mali CSF (Command Stream Frontend) GPU. +//! +//! This module implements a parser for the Mali GPU firmware binary format. The firmware +//! file contains a header followed by a sequence of entries, each describing how to load +//! firmware sections into the MCU (Microcontroller Unit) memory. The parser extracts section +//! metadata including: +//! - Virtual address ranges where sections should be mapped +//! - Data ranges (byte offsets) within the firmware binary +//! - Section flags (permissions, cache modes) + +use core::{ + mem::size_of, + ops::Range ,// +}; + +use kernel::{ + bits::bit_u32, + device::Device , + prelude::*, + sizes::SZ_4K ,// +}; + +use crate ::{ + fw::{ +CacheMode , +SectionFlags , +CSF_MCU_SHARED_REGION_START ,// + }, + vm::{ +VmFlag , +VmMapFlags ,// + },// +}; + +/// A parsed firmware section ready for loading into MCU memory. +/// +/// Represents a single firmware section extracted from the firmware binary, containing +/// all information needed to map the section's data into the MCU's virtual address space. +pub (super )struct ParsedSection { +/// Byte offset range within the firmware binary where this section's data resides. +pub (super ) data_range:Range <u32 >, +/// MCU virtual address range where this section should be mapped. +pub (super ) va:Range <u32 >, +/// Memory protection and caching flags for the mapping. +pub (super ) vm_map_flags:VmMapFlags , +} + +/// A bare-bones `std::io::Cursor<[u8]>` clone to keep track of the current position in the +/// firmware binary. +/// +/// Provides methods to sequentially read primitive types and byte arrays from the firmware +/// binary while maintaining the current read position. +struct Cursor <'a> { + dev: &'aDevice , + data: &'a [u8 ], + pos:usize , +} + +impl <'a>Cursor <'a> { +fn new (dev: &'aDevice , data: &'a [u8 ]) ->Self { +Self { dev, data, pos:0 } + } + +fn len (&self) ->usize { + self.data.len () + } + +fn pos (&self) ->usize { + self.pos + } + +/// Returns a view into the cursor's data. +/// +/// This spawns a new cursor, leaving the current cursor unchanged. +fn view (&self, range:Range <usize >) ->Result <Cursor <'_>> { +if range.start < self.pos || range.end > self.data.len () { +dev_err! ( + self.dev, +"Invalid cursor range {:?} for data of length {}" , + range, + self.data.len () + ); + +Err (EINVAL ) + }else { +Ok (Self { + dev: self.dev, + data: &self.data[range], + pos:0 , + }) + } + } + +/// Reads a slice of bytes from the current position and advances the cursor. +/// +/// Returns an error if the read would exceed the data bounds. +fn read (&mut self, nbytes:usize ) ->Result <&[u8 ]> { +let start = self.pos; +let end = start + nbytes; + +if end > self.data.len () { +dev_err! ( + self.dev, +"Invalid firmware file: read of size {} at position {} is out of bounds" , + nbytes, + start, + ); +return Err (EINVAL ); + } + + self.pos += nbytes; +Ok (&self.data[start..end]) + } + +/// Reads a little-endian `u8` from the current position and advances the cursor. +fn read_u8 (&mut self) ->Result <u8 > { +let bytes = self.read (size_of ::<u8 >())?; +Ok (bytes[0 ]) + } + +/// Reads a little-endian `u16` from the current position and advances the cursor. +fn read_u16 (&mut self) ->Result <u16 > { +let bytes: [u8 ;2 ] = self + .read (size_of ::<u16 >())? + .try_into () + .map_err (|_|EINVAL )?; + +Ok (u16::from_le_bytes (bytes)) + } + +/// Reads a little-endian `u32` from the current position and advances the cursor. +fn read_u32 (&mut self) ->Result <u32 > { +let bytes: [u8 ;4 ] = self + .read (size_of ::<u32 >())? + .try_into () + .map_err (|_|EINVAL )?; + +Ok (u32::from_le_bytes (bytes)) + } + +/// Advances the cursor position by the specified number of bytes. +/// +/// Returns an error if the advance would exceed the data bounds. +fn advance (&mut self, nbytes:usize ) ->Result { +if self.pos + nbytes > self.data.len () { +dev_err! ( + self.dev, +"Invalid firmware file: advance of size {} at position {} is out of bounds" , + nbytes, + self.pos, + ); +return Err (EINVAL ); + } + self.pos += nbytes; +Ok (()) + } +} + +/// Parser for Mali CSF GPU firmware binaries. +/// +/// Parses the firmware binary format, extracting section metadata including virtual +/// address ranges, data offsets, and memory protection flags needed to load firmware +/// into the MCU's memory. +pub (super )struct FwParser <'a> { + cursor:Cursor <'a>, +} + +impl <'a>FwParser <'a> { +/// Creates a new firmware parser for the given firmware binary data. +pub (super )fn new (dev: &'aDevice , data: &'a [u8 ]) ->Self { +Self { + cursor:Cursor ::new (dev, data), + } + } + +/// Parses the firmware binary and returns a collection of parsed sections. +/// +/// This method validates the firmware header and iterates through all entries +/// in the binary, extracting section information needed for loading. +pub (super )fn parse (&mut self) ->Result <KVec <ParsedSection >> { +let fw_header = self.parse_fw_header ()?; +let header_end = fw_header.sizeas usize ; + +let mut parsed_sections =KVec ::new (); +while self.cursor.pos () < header_end { +let entry_section = self.parse_entry (header_end)?; + +if let Some (inner) = entry_section.inner { + parsed_sections.push (inner,GFP_KERNEL )?; + } + } + +if parsed_sections.is_empty () { +dev_err! (self.cursor.dev,"Firmware contains no loadable sections" ); +return Err (EINVAL ); + } + +Ok (parsed_sections) + } + +fn parse_fw_header (&mut self) ->Result <FirmwareHeader > { +let fw_header:FirmwareHeader =match FirmwareHeader ::new (&mut self.cursor) { +Ok (fw_header) => fw_header, +Err (e) => { +dev_err! (self.cursor.dev,"Invalid firmware file: {}" , e.to_errno ()); +return Err (e); + } + }; + +if fw_header.sizeas usize > self.cursor.len () { +dev_err! (self.cursor.dev,"Firmware image is truncated" ); +return Err (EINVAL ); + } +Ok (fw_header) + } + +fn parse_entry (&mut self, header_end:usize ) ->Result <EntrySection > { +let entry_start = self.cursor.pos (); + +let entry_header_end = entry_start + .checked_add (size_of ::<EntryHeader >()) + .ok_or (EINVAL )?; + +if entry_header_end > header_end { +dev_err! ( + self.cursor.dev, +"Firmware entry header at {:#x} exceeds header region ending at {:#x}" , + entry_start, + header_end + ); +return Err (EINVAL ); + } + +let entry_section =EntrySection { + entry_hdr:EntryHeader (self.cursor.read_u32 ()?), + inner:None , + }; + +let firmware_size = self.cursor.len (); +let entry_size = entry_section.entry_hdr.size ()as usize ; + +if self.cursor.pos () %size_of ::<u32 >() !=0 + || entry_size %size_of ::<u32 >() !=0 + || entry_size <size_of ::<EntryHeader >() + { +dev_err! ( + self.cursor.dev, +"Firmware entry isn't 32 bit aligned, offset={:#x} size={:#x}" , + self.cursor.pos () -size_of ::<u32 >(), + entry_size + ); +return Err (EINVAL ); + } + +let entry_end = entry_start.checked_add (entry_size).ok_or (EINVAL )?; + +if entry_end > header_end { +dev_err! ( + self.cursor.dev, +"Firmware entry at {:#x} extends beyond header region ending at {:#x}" , + entry_start, + header_end + ); +return Err (EINVAL ); + } + +let section_hdr_size = entry_size -size_of ::<EntryHeader >(); + +let entry_section = { +let mut entry_cursor = self.cursor.view (self.cursor.pos ()..entry_end)?; + +match entry_section.entry_hdr.entry_type () { +Ok (EntryType ::Iface ) =>Ok (EntrySection { + entry_hdr: entry_section.entry_hdr, + inner:Self ::parse_section_entry (&mut entry_cursor, firmware_size)?, + }), +Ok ( +EntryType ::Config + |EntryType ::FutfTest + |EntryType ::TraceBuffer + |EntryType ::TimelineMetadata + |EntryType ::BuildInfoMetadata , + ) =>Ok (entry_section), + +Err (_) => { +if entry_section.entry_hdr.optional () { +Ok (entry_section) + }else { +dev_err! ( + self.cursor.dev, +"Failed to handle firmware entry type: {}" , + entry_section.entry_hdr.entry_type_raw () + ); +Err (EINVAL ) + } + } + } + }; + +if entry_section.is_ok () { + self.cursor.advance (section_hdr_size)?; + } + + entry_section + } + +fn parse_section_entry ( + entry_cursor: &mut Cursor <'_>, + firmware_size:usize , + ) ->Result <Option <ParsedSection >> { +let section_hdr:SectionHeader =SectionHeader ::new (entry_cursor)?; + +if section_hdr.data.end < section_hdr.data.start { +dev_err! ( + entry_cursor.dev, +"Firmware corrupted, data.end < data.start (0x{:x} < 0x{:x})" , + section_hdr.data.end, + section_hdr.data.start + ); +return Err (EINVAL ); + } + +if section_hdr.data.endas usize > firmware_size { +dev_err! ( + entry_cursor.dev, +"Firmware data range {:#x}..{:#x} exceeds firmware size {:#x}" , + section_hdr.data.start, + section_hdr.data.end, + firmware_size, + ); +return Err (EINVAL ); + } + +if section_hdr.va.startas usize %SZ_4K !=0 || section_hdr.va.endas usize %SZ_4K !=0 { +dev_err! ( + entry_cursor.dev, +"Firmware virtual address range {:#x}..{:#x} is not page aligned" , + section_hdr.va.start, + section_hdr.va.end + ); +return Err (EINVAL ); + } + +if section_hdr.section_flags.prot () { +dev_dbg! ( + entry_cursor.dev, +"Firmware protected mode entry not supported, ignoring" + ); +return Ok (None ); + } + +if section_hdr.va.start ==CSF_MCU_SHARED_REGION_START + && !section_hdr.section_flags.shared () + { +dev_err! ( + entry_cursor.dev, +"Interface at 0x{:x} must be shared" , +CSF_MCU_SHARED_REGION_START + ); +return Err (EINVAL ); + } + +if section_hdr.va.is_empty () { +return Ok (None ); + } + +let mut vm_map_flags =VmMapFlags ::empty (); + +if !section_hdr.section_flags.write () { + vm_map_flags |=VmFlag ::Readonly ; + } + +if !section_hdr.section_flags.exec () { + vm_map_flags |=VmFlag ::Noexec ; + } + +// TODO: As in Panthor, map coherent firmware sections uncached until the VM +// supports a coherent mapping attribute. +if section_hdr.section_flags.cache_mode () !=CacheMode ::Cached { + vm_map_flags |=VmFlag ::Uncached ; + } + +Ok (Some (ParsedSection { + data_range: section_hdr.data.clone (), + va: section_hdr.va, + vm_map_flags, + })) + } +} + +/// Firmware binary header containing version and size information. +/// +/// The header is located at the beginning of the firmware binary and contains +/// a magic value for validation, version information, and the total size of +/// all structured headers that follow. +#[expect(dead_code)] +struct FirmwareHeader { +/// Magic value to check binary validity. + magic:u32 , + +/// Minor firmware version. + minor:u8 , + +/// Major firmware version. + major:u8 , + +/// Padding. Must be set to zero. + _padding1:u16 , + +/// Firmware version hash. + version_hash:u32 , + +/// Padding. Must be set to zero. + _padding2:u32 , + +/// Total size of all the structured data headers at beginning of firmware binary. + size:u32 , +} + +impl FirmwareHeader { +const FW_BINARY_MAGIC :u32 =0xc3f13a6e ; +const FW_BINARY_MAJOR_MAX :u8 =0 ; + +/// Reads and validates a firmware header from the cursor. +/// +/// Verifies the magic value, version compatibility, and padding fields. +fn new (cursor: &mut Cursor <'_>) ->Result <Self > { +let magic = cursor.read_u32 ()?; +if magic !=Self ::FW_BINARY_MAGIC { +dev_err! (cursor.dev,"Invalid firmware magic" ); +return Err (EINVAL ); + } + +let minor = cursor.read_u8 ()?; +let major = cursor.read_u8 ()?; + +if major >Self ::FW_BINARY_MAJOR_MAX { +dev_err! ( + cursor.dev, +"Unsupported firmware binary header version {}.{} (expected {}.x)" , + major, + minor, +Self ::FW_BINARY_MAJOR_MAX + ); +return Err (EINVAL ); + } + +let padding1 = cursor.read_u16 ()?; +let version_hash = cursor.read_u32 ()?; +let padding2 = cursor.read_u32 ()?; +let size = cursor.read_u32 ()?; + +if padding1 !=0 || padding2 !=0 { +dev_err! ( + cursor.dev, +"Invalid firmware file: header padding is not zero" + ); +return Err (EINVAL ); + } + +let fw_header =Self { + magic, + minor, + major, + _padding1: padding1, + version_hash, + _padding2: padding2, + size, + }; + +Ok (fw_header) + } +} + +/// Firmware section header for loading binary sections into MCU memory. +#[derive( Debug )] +struct SectionHeader { + section_flags:SectionFlags , +/// MCU virtual range to map this binary section to. + va:Range <u32 >, +/// References the data in the FW binary. + data:Range <u32 >, +} + +impl SectionHeader { +/// Reads and validates a section header from the cursor. +/// +/// Parses section flags, virtual address range, and data range from the firmware binary. +fn new (cursor: &mut Cursor <'_>) ->Result <Self > { +let section_flags =SectionFlags ::try_from_fw (cursor.read_u32 ()?)?; + +let va_start = cursor.read_u32 ()?; +let va_end = cursor.read_u32 ()?; + +let va = va_start..va_end; + +if va.end < va.start { +dev_err! ( + cursor.dev, +"Invalid firmware file: VA end precedes start at pos {}" , + cursor.pos (), + ); +return Err (EINVAL ); + } + +let data_start = cursor.read_u32 ()?; +let data_end = cursor.read_u32 ()?; +let data = data_start..data_end; + +Ok (Self { + section_flags, + va, + data, + }) + } +} + +/// A firmware entry containing a header and optional parsed section data. +/// +/// Represents a single entry in the firmware binary, which may contain loadable +/// section data or metadata that doesn't require loading. +struct EntrySection { + entry_hdr:EntryHeader , + inner:Option <ParsedSection >, +} + +/// Header for a firmware entry, packed into a single u32. +/// +/// The entry header encodes the entry type, size, and optional flag in a +/// 32-bit value with the following layout: +/// - Bits 0-7: Entry type +/// - Bits 8-15: Size in bytes +/// - Bit 31: Optional flag +struct EntryHeader (u32 ); + +impl EntryHeader { +fn entry_type_raw (&self) ->u8 { + (self.0 &0xff )as u8 + } + +fn entry_type (&self) ->Result <EntryType > { +let v = self.entry_type_raw (); +EntryType ::try_from (v) + } + +fn optional (&self) ->bool { + self.0 &bit_u32 (31 ) !=0 + } + +fn size (&self) ->u32 { + self.0 >>8 &0xff + } +} + +#[derive( Clone , Copy , Debug )] +#[repr( u8 )] +enum EntryType { +/// Host <-> FW interface. +Iface =0 , +/// FW config. +Config =1 , +/// Unit tests. +FutfTest =2 , +/// Trace buffer interface. +TraceBuffer =3 , +/// Timeline metadata interface. +TimelineMetadata =4 , +/// Metadata about how the FW binary was built. +BuildInfoMetadata =6 , +} + +impl TryFrom <u8 >for EntryType { +type Error =Error ; + +fn try_from (value:u8 ) ->Result <Self ,Self ::Error > { +match value { +0 =>Ok (EntryType ::Iface ), +1 =>Ok (EntryType ::Config ), +2 =>Ok (EntryType ::FutfTest ), +3 =>Ok (EntryType ::TraceBuffer ), +4 =>Ok (EntryType ::TimelineMetadata ), +6 =>Ok (EntryType ::BuildInfoMetadata ), + _ =>Err (EINVAL ), + } + } +}
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v10 6/7] drm/tyr: add parser for firmware binary
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Missing validation that the firmware section data size does not exceed the virtual address…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Missing validation that the firmware section data size does not exceed the virtual address mapping size.
- [Low] Crate-local imports in the fw module are not sorted alphabetically.
--
--- Patch [6]: [PATCH v10 6/7] drm/tyr: add parser for firmware binary --- commit 2d4004e4d7f81ddd626b7391da06ddfa0d78eb29
Author: Daniel Almeida <[email protected]>
drm/tyr: add parser for firmware binary
This commit adds a parser for the Mali CSF GPU firmware binary format. It extracts section metadata, validates the basic firmware structure and alignment, and ignores protected-mode sections.
Quoted 5 lines
drivers/gpu/drm/tyr/fw/parser.rs new file +0
-0
[ ... ]
drivers/gpu/drm/tyr/fw/parser.rs line 29
Quoted 5 lines
drivers/gpu/drm/tyr/fw/parser.rs new file +5
-0+use crate ::{ + fw::{ +CacheMode , +SectionFlags , +CSF_MCU_SHARED_REGION_START ,//
[Severity: Low]
This isn't a bug, but the Rust coding guidelines require nested imports to be sorted alphabetically. CSF_MCU_SHARED_REGION_START should be listed before SectionFlags.
Quoted 6 lines
drivers/gpu/drm/tyr/fw/parser.rs new file +6
-0+ }, + vm::{ +VmFlag , +VmMapFlags ,// + }, // +};
[ ... ]
drivers/gpu/drm/tyr/fw/parser.rs line 315
Quoted 7 lines
drivers/gpu/drm/tyr/fw/parser.rs new file +7
-0+fn parse_section_entry ( + entry_cursor: &mut Cursor <'_>, + firmware_size:usize , + ) ->Result <Option <ParsedSection >> { +let section_hdr:SectionHeader =SectionHeader ::new (entry_cursor)?; + +if section_hdr.data.end < section_hdr.data.start {
[Severity: Medium]
Does this code need to ensure the initialization data size doesn't exceed the destination virtual address mapping size?
If a malformed firmware binary is provided where the data length (data.end - data.start) is greater than the virtual address length (va.end - va.start), could this lead to out-of-bounds writes when a firmware loader uses the virtual address size for memory allocation but copies the initialization data based on the binary data size?
Quoted 10 lines
drivers/gpu/drm/tyr/fw/parser.rs new file +10
-0+dev_err! ( + entry_cursor.dev, +"Firmware corrupted, data.end < data.start (0x{:x} < 0x{:x})" , + section_hdr.data.end, + section_hdr.data.start + ); +return Err (EINVAL ); + } + +if section_hdr.data.endas usize > firmware_size {
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v10 7/7 drm/tyr: add Microcontroller Unit (MCU) booting
In reply to Deborah BrouwerAdd a firmware module to load, parse, and map the MCU firmware sections into shared GEM memory at the required virtual addresses accessible by the GPU.
Create a firmware instance during probe and store it inside the TyrDrmRegistrationData to keep it alive after probe. Use the firmware instance to boot the MCU.
Remove the dead-code annotations from the MMU, VM, slot manager, and kernel BO code now that these paths are used by the firmware module.
Update Kconfig to add the RUST_FW_LOADER_ABSTRACTIONS dependency required by this module.
--- drivers/gpu/drm/tyr/Kconfig | 1 + drivers/gpu/drm/tyr/driver.rs | 23 ++- drivers/gpu/drm/tyr/fw.rs | 321 ++++++++++++++++++++++++++++++++++++++++++ drivers/gpu/drm/tyr/gem.rs | 3 - drivers/gpu/drm/tyr/tyr.rs | 1 + drivers/gpu/drm/tyr/vm.rs | 1 - 6 files changed, 341 insertions(+), 9 deletions(-)
drivers/gpu/drm/tyr/Kconfig +1 -0
@@ -13,6 +13,7 @@ config DRM_TYR select IOMMU_IO_PGTABLE_LPAE select RUST_DRM_GEM_SHMEM_HELPER select RUST_DRM_GPUVM + select RUST_FW_LOADER_ABSTRACTIONS help Rust DRM driver for ARM Mali CSF-based GPUs.
drivers/gpu/drm/tyr/driver.rs +18 -5
@@ -37,6 +37,7 @@use crate ::{ file::TyrDrmFileData , + fw::Firmware , gem::Bo , gpu, gpu::GpuInfo ,
@@ -67,6 +68,9 @@ pub(crate) struct TyrDrmRegistrationData<'drm> {/// Parent platform device. pub(crate) pdev: &'drm platform::Device<Bound>, +/// Firmware sections. + pub(crate) fw: Firmware<'drm>, +#[pin] clks: Mutex<Clocks>,
@@ -144,10 +148,21 @@ fn probe<'bound>(let unreg_dev = drm::UnregisteredDevice ::<TyrDrmDriver >::new (pdev,Ok (()))?; -let _mmu =Mmu ::new (pdev.as_ref (), iomem.as_arc_borrow (), &gpu_info)?; +let mmu =Mmu ::new (pdev.as_ref (), iomem.as_arc_borrow (), &gpu_info)?; + +let firmware =Firmware ::new ( + pdev.as_ref (), + iomem.clone (), + &unreg_dev, + mmu.as_arc_borrow (), + &gpu_info, + )?; + + firmware.boot ()?; - let reg_data = try_pin_init!(TyrDrmRegistrationData { + let reg_data = pin_init!(TyrDrmRegistrationData { pdev, + fw: firmware, clks <- new_mutex!(Clocks { core: core_clk, stacks: stacks_clk,
@@ -167,9 +182,7 @@ fn probe<'bound>(let driver =TyrPlatformDriverData { _reg: reg }; -// We need this to be dev_info!() because dev_dbg!() does not work at -// all in Rust for now, and we need to see whether probe succeeded. -dev_info! (pdev,"Tyr initialized correctly.\n" ); +dev_dbg! (pdev,"Tyr initialized correctly." );Ok (driver) } }
drivers/gpu/drm/tyr/fw.rs new file +321 -0
321 lines not on this page · raw message
drivers/gpu/drm/tyr/gem.rs +0 -3
@@ -72,7 +72,6 @@ pub(crate) fn new_dummy_object(ddev: &TyrDrmDevice) -> Result<ARef<Bo>> {/// An automatic VA allocation strategy will be added in the future. pub (crate )enum KernelBoVaAlloc {/// Explicit VA address specified by the caller. -#[expect(dead_code)] Explicit (u64 ), }
@@ -98,7 +97,6 @@ impl<'drm> KernelBo<'drm> {/// This function allocates a new shmem-backed GEM object and immediately maps /// it into the specified GPU virtual memory space. The mapping is automatically /// cleaned up when the [`KernelBo`] is dropped. -#[expect(dead_code)] pub (crate )fn new ( ddev: &TyrDrmDevice , vm: Arc<Vm<'drm>>,
@@ -135,7 +133,6 @@ pub(crate) fn new( }) } -#[expect(dead_code)] pub (crate )fn bo (&self) -> &Bo { &self.bo }
drivers/gpu/drm/tyr/tyr.rs +1 -0
@@ -9,6 +9,7 @@mod driver;mod file; +mod fw;mod gem;mod gpu;mod mmu;
drivers/gpu/drm/tyr/vm.rs +0 -1
@@ -6,7 +6,6 @@//! the illusion of owning the entire virtual address (VA) range, similar to CPU virtual memory. //! Each virtual memory (VM) area is backed by ARM64 LPAE Stage 1 page tables and can be //! mapped into hardware address space (AS) slots for GPU execution. -#![expect(dead_code)] use core::marker::PhantomData ;use core::ops::Range ;
Signature · 1 line
2.55.0
Quoted 8 lines · Deborah Brouwer
This series adds firmware loading and MCU boot support to the Tyr DRM driver. It includes:
- A parser for the Mali CSF firmware binary format
- A kernel-managed BO type (KernelBo) for internal driver allocations
- GPU virtual memory (VM) integration usingdrm_gpuvm
- An MMU module and a generic slot manager
- Loading firmware, VM activation, and MCU boot atprobe()
Quoted 6 lines
It is based on drm-rust-next and also depends on:
[PATCH v3] rust: iommu: add device lifetime to IoPageTable
https://lore.kernel.org/rust-for-linux/[email protected]Signed-off-by Deborah Brouwer <[email protected]>
Applied to drm-rust-next. Thanks!
I fixed up a minor build failure with 32-bit arm on apply.
Alice