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]> --- drivers/gpu/drm/tyr/gem.rs | 74 +++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/tyr/gem.rs b/drivers/gpu/drm/tyr/gem.rs index 111acf33993f..3807810be7ea 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,12 +13,22 @@ 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. @@ -61,3 +73,57 @@ pub(crate) fn new_dummy_object<Ctx: DeviceContext>(ddev: &TyrDrmDevice<Ctx>) -> Ok(bo) } + +/// A buffer object that is owned and managed by Tyr rather than userspace. +pub(crate) struct KernelBo { + #[expect(dead_code)] + pub(crate) bo: ARef<Bo>, + vm: Arc<Vm>, + va_range: Range<u64>, +} + +impl KernelBo { + /// Creates a new kernel-owned buffer object and maps it into GPU VA space. + #[expect(dead_code)] + pub(crate) fn new<Ctx: DeviceContext>( + ddev: &TyrDrmDevice<Ctx>, + vm: ArcBorrow<'_, Vm>, + size: u64, + va: u64, + flags: VmMapFlags, + ) -> Result<Self> { + 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
