Introduce 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. Co-developed-by: Boris Brezillon <[email protected]> Signed-off-by: Boris Brezillon <[email protected]> Signed-off-by: Deborah Brouwer <[email protected]> --- Changes in v2: - Add documentation. - Add allocation strategy option for kernel buffer objects. drivers/gpu/drm/tyr/gem.rs | 119 ++++++++++++++++++++++++++++++++++--- 1 file changed, 112 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/tyr/gem.rs b/drivers/gpu/drm/tyr/gem.rs index 111acf33993f..f3bb58ef5eae 100644 --- a/drivers/gpu/drm/tyr/gem.rs +++ b/drivers/gpu/drm/tyr/gem.rs @@ -4,6 +4,8 @@ //! This module provides buffer object (BO) management functionality using //! DRM's GEM subsystem with shmem backing. +use core::ops::Range; + use kernel::{ drm::{ gem, @@ -11,23 +13,41 @@ DeviceContext, // }, prelude::*, - sync::aref::ARef, // + sync::{ + aref::ARef, + Arc, + ArcBorrow, // + }, }; -use crate::driver::{ - TyrDrmDevice, - TyrDrmDriver, // +use crate::{ + driver::{ + TyrDrmDevice, + TyrDrmDriver, // + }, + vm::{ + Vm, + VmMapFlags, // + }, }; -/// Tyr's DriverObject type for GEM objects. +/// Driver-specific data for Tyr GEM buffer objects. +/// +/// This structure contains Tyr-specific metadata associated with each GEM object. +/// It implements [`gem::DriverObject`] to provide driver-specific behavior for +/// buffer object creation and management. #[pin_data] pub(crate) struct BoData { + /// Buffer object creation flags (currently unused). flags: u32, } -/// Provides a way to pass arguments when creating BoData -/// as required by the gem::DriverObject trait. +/// Arguments for creating a [`BoData`] instance. +/// +/// This structure is used to pass creation parameters when instantiating +/// a new buffer object, as required by the [`gem::DriverObject`] trait. pub(crate) struct BoCreateArgs { + /// Buffer object creation flags (currently unused). flags: u32, } @@ -35,6 +55,12 @@ impl gem::DriverObject for BoData { type Driver = TyrDrmDriver; type Args = BoCreateArgs; + /// Constructs a new [`BoData`] instance for a GEM object. + /// + /// This function is called by the GEM subsystem when creating a new buffer + /// object. It initializes the driver-specific data with the provided flags. + /// The device and size parameters are currently unused but required by the + /// [`gem::DriverObject`] trait. fn new<Ctx: DeviceContext>( _dev: &TyrDrmDevice<Ctx>, _size: usize, @@ -61,3 +87,82 @@ pub(crate) fn new_dummy_object<Ctx: DeviceContext>(ddev: &TyrDrmDevice<Ctx>) -> Ok(bo) } + +/// VA allocation strategy for kernel buffer objects. +/// +/// Specifies how the GPU virtual address should be determined when creating +/// 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 { + /// The underlying GEM buffer object. + #[expect(dead_code)] + pub(crate) bo: ARef<Bo>, + /// The GPU VM this buffer is mapped into. + vm: Arc<Vm>, + /// The GPU VA range occupied by this buffer. + va_range: Range<u64>, +} + +impl KernelBo { + /// 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<Ctx: DeviceContext>( + ddev: &TyrDrmDevice<Ctx>, + vm: ArcBorrow<'_, Vm>, + size: u64, + va_alloc: KernelBoVaAlloc, + flags: VmMapFlags, + ) -> Result<Self> { + let KernelBoVaAlloc::Explicit(va) = va_alloc; + + let bo = gem::shmem::Object::<BoData>::new( + ddev, + size as usize, + 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: vm.into(), + va_range: va..(va + size), + }) + } +} + +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) { + pr_err!( + "Failed to unmap KernelBo range {:#x}..{:#x}: {:?}\n", + self.va_range.start, + self.va_range.end, + e + ); + } + } +} -- 2.52.0
