171a39211SAlice Ryhl // SPDX-License-Identifier: GPL-2.0 OR MIT 271a39211SAlice Ryhl 371a39211SAlice Ryhl use super::*; 471a39211SAlice Ryhl 571a39211SAlice Ryhl /// Represents that a given GEM object has at least one mapping on this [`GpuVm`] instance. 671a39211SAlice Ryhl /// 771a39211SAlice Ryhl /// Does not assume that GEM lock is held. 871a39211SAlice Ryhl /// 971a39211SAlice Ryhl /// # Invariants 1071a39211SAlice Ryhl /// 1171a39211SAlice Ryhl /// * Allocated with `kmalloc` and refcounted via `inner`. 1271a39211SAlice Ryhl /// * Is present in the gem list. 1371a39211SAlice Ryhl #[repr(C)] 1471a39211SAlice Ryhl #[pin_data] 1571a39211SAlice Ryhl pub struct GpuVmBo<T: DriverGpuVm> { 1671a39211SAlice Ryhl #[pin] 1771a39211SAlice Ryhl inner: Opaque<bindings::drm_gpuvm_bo>, 1871a39211SAlice Ryhl #[pin] 1971a39211SAlice Ryhl data: T::VmBoData, 2071a39211SAlice Ryhl } 2171a39211SAlice Ryhl 2271a39211SAlice Ryhl // SAFETY: By type invariants, the allocation is managed by the refcount in `self.inner`. 2371a39211SAlice Ryhl unsafe impl<T: DriverGpuVm> AlwaysRefCounted for GpuVmBo<T> { 2471a39211SAlice Ryhl fn inc_ref(&self) { 2571a39211SAlice Ryhl // SAFETY: By type invariants, the allocation is managed by the refcount in `self.inner`. 2671a39211SAlice Ryhl unsafe { bindings::drm_gpuvm_bo_get(self.inner.get()) }; 2771a39211SAlice Ryhl } 2871a39211SAlice Ryhl 2971a39211SAlice Ryhl unsafe fn dec_ref(obj: NonNull<Self>) { 3071a39211SAlice Ryhl // CAST: `drm_gpuvm_bo` is first field of repr(C) struct. 3171a39211SAlice Ryhl // SAFETY: By type invariants, the allocation is managed by the refcount in `self.inner`. 3271a39211SAlice Ryhl // This GPUVM instance uses immediate mode, so we may put the refcount using the deferred 3371a39211SAlice Ryhl // mechanism. 3471a39211SAlice Ryhl unsafe { bindings::drm_gpuvm_bo_put_deferred(obj.as_ptr().cast()) }; 3571a39211SAlice Ryhl } 3671a39211SAlice Ryhl } 3771a39211SAlice Ryhl 3871a39211SAlice Ryhl impl<T: DriverGpuVm> PartialEq for GpuVmBo<T> { 3971a39211SAlice Ryhl #[inline] 4071a39211SAlice Ryhl fn eq(&self, other: &Self) -> bool { 4171a39211SAlice Ryhl core::ptr::eq(self.as_raw(), other.as_raw()) 4271a39211SAlice Ryhl } 4371a39211SAlice Ryhl } 4471a39211SAlice Ryhl impl<T: DriverGpuVm> Eq for GpuVmBo<T> {} 4571a39211SAlice Ryhl 4671a39211SAlice Ryhl impl<T: DriverGpuVm> GpuVmBo<T> { 4771a39211SAlice Ryhl /// The function pointer for allocating a GpuVmBo stored in the gpuvm vtable. 4871a39211SAlice Ryhl /// 4971a39211SAlice Ryhl /// Allocation is always implemented according to [`Self::vm_bo_alloc`], but it is set to 5071a39211SAlice Ryhl /// `None` if the default gpuvm behavior is the same as `vm_bo_alloc`. 5171a39211SAlice Ryhl /// 5271a39211SAlice Ryhl /// This may be `Some` even if `FREE_FN` is `None`, or vice-versa. 5371a39211SAlice Ryhl pub(super) const ALLOC_FN: Option<unsafe extern "C" fn() -> *mut bindings::drm_gpuvm_bo> = { 5471a39211SAlice Ryhl use core::alloc::Layout; 5571a39211SAlice Ryhl let base = Layout::new::<bindings::drm_gpuvm_bo>(); 5671a39211SAlice Ryhl let rust = Layout::new::<Self>(); 5771a39211SAlice Ryhl assert!(base.size() <= rust.size()); 5871a39211SAlice Ryhl if base.size() != rust.size() || base.align() != rust.align() { 5971a39211SAlice Ryhl Some(Self::vm_bo_alloc) 6071a39211SAlice Ryhl } else { 6171a39211SAlice Ryhl // This causes GPUVM to allocate a `GpuVmBo<T>` with `kzalloc(sizeof(drm_gpuvm_bo))`. 6271a39211SAlice Ryhl None 6371a39211SAlice Ryhl } 6471a39211SAlice Ryhl }; 6571a39211SAlice Ryhl 6671a39211SAlice Ryhl /// The function pointer for freeing a GpuVmBo stored in the gpuvm vtable. 6771a39211SAlice Ryhl /// 6871a39211SAlice Ryhl /// Freeing is always implemented according to [`Self::vm_bo_free`], but it is set to `None` if 6971a39211SAlice Ryhl /// the default gpuvm behavior is the same as `vm_bo_free`. 7071a39211SAlice Ryhl /// 7171a39211SAlice Ryhl /// This may be `Some` even if `ALLOC_FN` is `None`, or vice-versa. 7271a39211SAlice Ryhl pub(super) const FREE_FN: Option<unsafe extern "C" fn(*mut bindings::drm_gpuvm_bo)> = { 7371a39211SAlice Ryhl if core::mem::needs_drop::<Self>() { 7471a39211SAlice Ryhl Some(Self::vm_bo_free) 7571a39211SAlice Ryhl } else { 7671a39211SAlice Ryhl // This causes GPUVM to free a `GpuVmBo<T>` with `kfree`. 7771a39211SAlice Ryhl None 7871a39211SAlice Ryhl } 7971a39211SAlice Ryhl }; 8071a39211SAlice Ryhl 8171a39211SAlice Ryhl /// Custom function for allocating a `drm_gpuvm_bo`. 8271a39211SAlice Ryhl /// 8371a39211SAlice Ryhl /// # Safety 8471a39211SAlice Ryhl /// 8571a39211SAlice Ryhl /// Always safe to call. 8671a39211SAlice Ryhl unsafe extern "C" fn vm_bo_alloc() -> *mut bindings::drm_gpuvm_bo { 8771a39211SAlice Ryhl let raw_ptr = KBox::<Self>::new_uninit(GFP_KERNEL | __GFP_ZERO) 8871a39211SAlice Ryhl .map(KBox::into_raw) 8971a39211SAlice Ryhl .unwrap_or(ptr::null_mut()); 9071a39211SAlice Ryhl 9171a39211SAlice Ryhl // CAST: `drm_gpuvm_bo` is first field of `Self`. 9271a39211SAlice Ryhl raw_ptr.cast() 9371a39211SAlice Ryhl } 9471a39211SAlice Ryhl 9571a39211SAlice Ryhl /// Custom function for freeing a `drm_gpuvm_bo`. 9671a39211SAlice Ryhl /// 9771a39211SAlice Ryhl /// # Safety 9871a39211SAlice Ryhl /// 9971a39211SAlice Ryhl /// The pointer must have been allocated with [`GpuVmBo::ALLOC_FN`], and must not be used after 10071a39211SAlice Ryhl /// this call. 10171a39211SAlice Ryhl unsafe extern "C" fn vm_bo_free(ptr: *mut bindings::drm_gpuvm_bo) { 10271a39211SAlice Ryhl // CAST: `drm_gpuvm_bo` is first field of `Self`. 10371a39211SAlice Ryhl // SAFETY: 10471a39211SAlice Ryhl // * The ptr was allocated from kmalloc with the layout of `GpuVmBo<T>`. 10571a39211SAlice Ryhl // * `ptr->inner` has no destructor. 10671a39211SAlice Ryhl // * `ptr->data` contains a valid `T::VmBoData` that we can drop. 10771a39211SAlice Ryhl drop(unsafe { KBox::<Self>::from_raw(ptr.cast()) }); 10871a39211SAlice Ryhl } 10971a39211SAlice Ryhl 11071a39211SAlice Ryhl /// Access this [`GpuVmBo`] from a raw pointer. 11171a39211SAlice Ryhl /// 11271a39211SAlice Ryhl /// # Safety 11371a39211SAlice Ryhl /// 11471a39211SAlice Ryhl /// For the duration of `'a`, the pointer must reference a valid `drm_gpuvm_bo` associated with 11571a39211SAlice Ryhl /// a [`GpuVm<T>`]. The BO must also be present in the GEM list. 11671a39211SAlice Ryhl #[inline] 11771a39211SAlice Ryhl pub(crate) unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuvm_bo) -> &'a Self { 11871a39211SAlice Ryhl // SAFETY: `drm_gpuvm_bo` is first field and `repr(C)`. 11971a39211SAlice Ryhl unsafe { &*ptr.cast() } 12071a39211SAlice Ryhl } 12171a39211SAlice Ryhl 12271a39211SAlice Ryhl /// Returns a raw pointer to underlying C value. 12371a39211SAlice Ryhl #[inline] 12471a39211SAlice Ryhl pub fn as_raw(&self) -> *mut bindings::drm_gpuvm_bo { 12571a39211SAlice Ryhl self.inner.get() 12671a39211SAlice Ryhl } 12771a39211SAlice Ryhl 12871a39211SAlice Ryhl /// The [`GpuVm`] that this GEM object is mapped in. 12971a39211SAlice Ryhl #[inline] 13071a39211SAlice Ryhl pub fn gpuvm(&self) -> &GpuVm<T> { 13171a39211SAlice Ryhl // SAFETY: The `obj` pointer is guaranteed to be valid. 13271a39211SAlice Ryhl unsafe { GpuVm::<T>::from_raw((*self.inner.get()).vm) } 13371a39211SAlice Ryhl } 13471a39211SAlice Ryhl 13571a39211SAlice Ryhl /// The [`drm_gem_object`](DriverGpuVm::Object) for these mappings. 13671a39211SAlice Ryhl #[inline] 13771a39211SAlice Ryhl pub fn obj(&self) -> &T::Object { 13871a39211SAlice Ryhl // SAFETY: The `obj` pointer is guaranteed to be valid. 13971a39211SAlice Ryhl unsafe { <T::Object as IntoGEMObject>::from_raw((*self.inner.get()).obj) } 14071a39211SAlice Ryhl } 14171a39211SAlice Ryhl 14271a39211SAlice Ryhl /// The driver data with this buffer object. 14371a39211SAlice Ryhl #[inline] 14471a39211SAlice Ryhl pub fn data(&self) -> &T::VmBoData { 14571a39211SAlice Ryhl &self.data 14671a39211SAlice Ryhl } 147*dc384604SAlice Ryhl 148*dc384604SAlice Ryhl pub(super) fn lock_gpuva(&self) -> crate::sync::MutexGuard<'_, ()> { 149*dc384604SAlice Ryhl // SAFETY: The GEM object is valid. 150*dc384604SAlice Ryhl let ptr = unsafe { &raw mut (*self.obj().as_raw()).gpuva.lock }; 151*dc384604SAlice Ryhl // SAFETY: The GEM object is valid, so the mutex is properly initialized. 152*dc384604SAlice Ryhl let mutex = unsafe { crate::sync::Mutex::from_raw(ptr) }; 153*dc384604SAlice Ryhl mutex.lock() 154*dc384604SAlice Ryhl } 15571a39211SAlice Ryhl } 15671a39211SAlice Ryhl 15771a39211SAlice Ryhl /// A pre-allocated [`GpuVmBo`] object. 15871a39211SAlice Ryhl /// 15971a39211SAlice Ryhl /// # Invariants 16071a39211SAlice Ryhl /// 16171a39211SAlice Ryhl /// Points at a `drm_gpuvm_bo` that contains a valid `T::VmBoData`, has a refcount of one, and is 16271a39211SAlice Ryhl /// absent from any gem, extobj, or evict lists. 16371a39211SAlice Ryhl pub(super) struct GpuVmBoAlloc<T: DriverGpuVm>(NonNull<GpuVmBo<T>>); 16471a39211SAlice Ryhl 16571a39211SAlice Ryhl impl<T: DriverGpuVm> GpuVmBoAlloc<T> { 16671a39211SAlice Ryhl /// Create a new pre-allocated [`GpuVmBo`]. 16771a39211SAlice Ryhl /// 16871a39211SAlice Ryhl /// It's intentional that the initializer is infallible because `drm_gpuvm_bo_put` will call 16971a39211SAlice Ryhl /// drop on the data, so we don't have a way to free it when the data is missing. 17071a39211SAlice Ryhl #[inline] 17171a39211SAlice Ryhl pub(super) fn new( 17271a39211SAlice Ryhl gpuvm: &GpuVm<T>, 17371a39211SAlice Ryhl gem: &T::Object, 17471a39211SAlice Ryhl value: impl PinInit<T::VmBoData>, 17571a39211SAlice Ryhl ) -> Result<GpuVmBoAlloc<T>, AllocError> { 17671a39211SAlice Ryhl // CAST: `GpuVmBoAlloc::vm_bo_alloc` ensures that this memory was allocated with the layout 17771a39211SAlice Ryhl // of `GpuVmBo<T>`. The type is repr(C), so `container_of` is not required. 17871a39211SAlice Ryhl // SAFETY: The provided gpuvm and gem ptrs are valid for the duration of this call. 17971a39211SAlice Ryhl let raw_ptr = unsafe { 18071a39211SAlice Ryhl bindings::drm_gpuvm_bo_create(gpuvm.as_raw(), gem.as_raw()).cast::<GpuVmBo<T>>() 18171a39211SAlice Ryhl }; 18271a39211SAlice Ryhl let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?; 18371a39211SAlice Ryhl // SAFETY: `ptr->data` is a valid pinned location. 18471a39211SAlice Ryhl let Ok(()) = unsafe { value.__pinned_init(&raw mut (*raw_ptr).data) }; 18571a39211SAlice Ryhl // INVARIANTS: We just created the vm_bo so it's absent from lists, and the data is valid 18671a39211SAlice Ryhl // as we just initialized it. 18771a39211SAlice Ryhl Ok(GpuVmBoAlloc(ptr)) 18871a39211SAlice Ryhl } 18971a39211SAlice Ryhl 19071a39211SAlice Ryhl /// Returns a raw pointer to underlying C value. 19171a39211SAlice Ryhl #[inline] 19271a39211SAlice Ryhl pub(super) fn as_raw(&self) -> *mut bindings::drm_gpuvm_bo { 19371a39211SAlice Ryhl // SAFETY: The pointer references a valid `drm_gpuvm_bo`. 19471a39211SAlice Ryhl unsafe { (*self.0.as_ptr()).inner.get() } 19571a39211SAlice Ryhl } 19671a39211SAlice Ryhl 19771a39211SAlice Ryhl /// Look up whether there is an existing [`GpuVmBo`] for this gem object. 19871a39211SAlice Ryhl /// 19971a39211SAlice Ryhl /// The caller should not hold the GEM mutex or DMA resv lock. 20071a39211SAlice Ryhl #[inline] 20171a39211SAlice Ryhl pub(super) fn obtain(self) -> ARef<GpuVmBo<T>> { 20271a39211SAlice Ryhl let me = ManuallyDrop::new(self); 20371a39211SAlice Ryhl // SAFETY: Valid `drm_gpuvm_bo` not already in the lists. We do not access `me` after this 20471a39211SAlice Ryhl // call. 20571a39211SAlice Ryhl let ptr = unsafe { bindings::drm_gpuvm_bo_obtain_prealloc(me.as_raw()) }; 20671a39211SAlice Ryhl 20771a39211SAlice Ryhl // SAFETY: `drm_gpuvm_bo_obtain_prealloc` always returns a non-null ptr 20871a39211SAlice Ryhl let nonnull = unsafe { NonNull::new_unchecked(ptr.cast()) }; 20971a39211SAlice Ryhl 21071a39211SAlice Ryhl // INVARIANTS: `drm_gpuvm_bo_obtain_prealloc` ensures that the bo is in the GEM list. 21171a39211SAlice Ryhl // SAFETY: We received one refcount from `drm_gpuvm_bo_obtain_prealloc`. 21271a39211SAlice Ryhl let ret = unsafe { ARef::<GpuVmBo<T>>::from_raw(nonnull) }; 21371a39211SAlice Ryhl 21471a39211SAlice Ryhl // Ensure that external objects are in the extobj list. 21571a39211SAlice Ryhl // 21671a39211SAlice Ryhl // Note that we must call `extobj_add` even if `ptr != me` to avoid a race condition where 21771a39211SAlice Ryhl // we could end up using the extobj before the thread with `ptr == me` calls extobj_add. 21871a39211SAlice Ryhl if ret.gpuvm().is_extobj(ret.obj()) { 21971a39211SAlice Ryhl let resv_lock = ret.gpuvm().raw_resv(); 22071a39211SAlice Ryhl // TODO: Use a proper lock guard here once a dma_resv lock abstraction exists. 22171a39211SAlice Ryhl // SAFETY: The GPUVM is still alive, so its resv lock is too. 22271a39211SAlice Ryhl unsafe { bindings::dma_resv_lock(resv_lock, ptr::null_mut()) }; 22371a39211SAlice Ryhl // SAFETY: We hold the GPUVMs resv lock. 22471a39211SAlice Ryhl unsafe { bindings::drm_gpuvm_bo_extobj_add(ptr) }; 22571a39211SAlice Ryhl // SAFETY: We took the lock, so we can unlock it. 22671a39211SAlice Ryhl unsafe { bindings::dma_resv_unlock(resv_lock) }; 22771a39211SAlice Ryhl } 22871a39211SAlice Ryhl 22971a39211SAlice Ryhl ret 23071a39211SAlice Ryhl } 23171a39211SAlice Ryhl } 23271a39211SAlice Ryhl 23371a39211SAlice Ryhl impl<T: DriverGpuVm> Deref for GpuVmBoAlloc<T> { 23471a39211SAlice Ryhl type Target = GpuVmBo<T>; 23571a39211SAlice Ryhl #[inline] 23671a39211SAlice Ryhl fn deref(&self) -> &GpuVmBo<T> { 23771a39211SAlice Ryhl // SAFETY: By the type invariants we may deref while `Self` exists. 23871a39211SAlice Ryhl unsafe { self.0.as_ref() } 23971a39211SAlice Ryhl } 24071a39211SAlice Ryhl } 24171a39211SAlice Ryhl 24271a39211SAlice Ryhl impl<T: DriverGpuVm> Drop for GpuVmBoAlloc<T> { 24371a39211SAlice Ryhl #[inline] 24471a39211SAlice Ryhl fn drop(&mut self) { 24571a39211SAlice Ryhl // TODO: Call drm_gpuvm_bo_destroy_not_in_lists() directly. 24671a39211SAlice Ryhl // SAFETY: It's safe to perform a deferred put in any context. 24771a39211SAlice Ryhl unsafe { bindings::drm_gpuvm_bo_put_deferred(self.as_raw()) }; 24871a39211SAlice Ryhl } 24971a39211SAlice Ryhl } 250