|
| 1 | +// SPDX-License-Identifier: GPL-2.0 OR MIT |
| 2 | + |
| 3 | +#![expect(dead_code)] |
| 4 | +use super::*; |
| 5 | + |
| 6 | +/// Represents that a range of a GEM object is mapped in this [`GpuVm`] instance. |
| 7 | +/// |
| 8 | +/// Does not assume that GEM lock is held. |
| 9 | +/// |
| 10 | +/// # Invariants |
| 11 | +/// |
| 12 | +/// * This is a valid `drm_gpuva` object that is resident in a [`GpuVm<T>`] instance. |
| 13 | +/// * It is associated with a [`GpuVmBo<T>`]. Or in other words, it's not an |
| 14 | +/// `gpuvm->kernel_alloc_node` and `DRM_GPUVA_SPARSE` is not set. |
| 15 | +/// * The associated [`GpuVmBo<T>`] is part of the GEM list. |
| 16 | +#[repr(C)] |
| 17 | +#[pin_data] |
| 18 | +pub struct GpuVa<T: DriverGpuVm> { |
| 19 | + #[pin] |
| 20 | + inner: Opaque<bindings::drm_gpuva>, |
| 21 | + #[pin] |
| 22 | + data: T::VaData, |
| 23 | +} |
| 24 | + |
| 25 | +impl<T: DriverGpuVm> PartialEq for GpuVa<T> { |
| 26 | + #[inline] |
| 27 | + fn eq(&self, other: &Self) -> bool { |
| 28 | + core::ptr::eq(self.as_raw(), other.as_raw()) |
| 29 | + } |
| 30 | +} |
| 31 | +impl<T: DriverGpuVm> Eq for GpuVa<T> {} |
| 32 | + |
| 33 | +impl<T: DriverGpuVm> GpuVa<T> { |
| 34 | + /// Access this [`GpuVa`] from a raw pointer. |
| 35 | + /// |
| 36 | + /// # Safety |
| 37 | + /// |
| 38 | + /// * For the duration of `'a`, the pointer must reference a valid `drm_gpuva` associated with |
| 39 | + /// a [`GpuVm<T>`]. |
| 40 | + /// * It must be associated with a [`GpuVmBo<T>`]. |
| 41 | + /// * The associated [`GpuVmBo<T>`] is part of the GEM list. |
| 42 | + #[inline] |
| 43 | + pub unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuva) -> &'a Self { |
| 44 | + // CAST: `drm_gpuva` is first field and `repr(C)`. |
| 45 | + // SAFETY: The safety requirements match the invariants of `GpuVa`. |
| 46 | + unsafe { &*ptr.cast() } |
| 47 | + } |
| 48 | + |
| 49 | + /// Returns a raw pointer to underlying C value. |
| 50 | + #[inline] |
| 51 | + pub fn as_raw(&self) -> *mut bindings::drm_gpuva { |
| 52 | + self.inner.get() |
| 53 | + } |
| 54 | + |
| 55 | + /// Returns the address of this mapping in the GPU virtual address space. |
| 56 | + #[inline] |
| 57 | + pub fn addr(&self) -> u64 { |
| 58 | + // SAFETY: The `va.addr` field of `drm_gpuva` is immutable. |
| 59 | + unsafe { (*self.as_raw()).va.addr } |
| 60 | + } |
| 61 | + |
| 62 | + /// Returns the length of this mapping. |
| 63 | + #[inline] |
| 64 | + pub fn length(&self) -> u64 { |
| 65 | + // SAFETY: The `va.range` field of `drm_gpuva` is immutable. |
| 66 | + unsafe { (*self.as_raw()).va.range } |
| 67 | + } |
| 68 | + |
| 69 | + /// Returns `addr..addr+length`. |
| 70 | + #[inline] |
| 71 | + pub fn range(&self) -> Range<u64> { |
| 72 | + let addr = self.addr(); |
| 73 | + addr..addr + self.length() |
| 74 | + } |
| 75 | + |
| 76 | + /// Returns the offset within the GEM object. |
| 77 | + #[inline] |
| 78 | + pub fn gem_offset(&self) -> u64 { |
| 79 | + // SAFETY: The `gem.offset` field of `drm_gpuva` is immutable. |
| 80 | + unsafe { (*self.as_raw()).gem.offset } |
| 81 | + } |
| 82 | + |
| 83 | + /// Returns the GEM object. |
| 84 | + #[inline] |
| 85 | + pub fn obj(&self) -> &T::Object { |
| 86 | + // SAFETY: The `gem.obj` field of `drm_gpuva` is immutable. We know that it's not null |
| 87 | + // because this VA is associated with a `GpuVmBo<T>`. |
| 88 | + unsafe { <T::Object as IntoGEMObject>::from_raw((*self.as_raw()).gem.obj) } |
| 89 | + } |
| 90 | + |
| 91 | + /// Returns the underlying [`GpuVmBo`] object that backs this [`GpuVa`]. |
| 92 | + #[inline] |
| 93 | + pub fn vm_bo(&self) -> &GpuVmBo<T> { |
| 94 | + // SAFETY: The `vm_bo` field of `drm_gpuva` is immutable. We know that it's not null |
| 95 | + // because this VA is associated with a `GpuVmBo<T>`. The BO is in the GEM list by the type |
| 96 | + // invariants. |
| 97 | + unsafe { GpuVmBo::from_raw((*self.as_raw()).vm_bo) } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/// A pre-allocated [`GpuVa`] object. |
| 102 | +/// |
| 103 | +/// # Invariants |
| 104 | +/// |
| 105 | +/// The memory is zeroed. |
| 106 | +pub struct GpuVaAlloc<T: DriverGpuVm>(KBox<MaybeUninit<GpuVa<T>>>); |
| 107 | + |
| 108 | +impl<T: DriverGpuVm> GpuVaAlloc<T> { |
| 109 | + /// Pre-allocate a [`GpuVa`] object. |
| 110 | + pub fn new(flags: AllocFlags) -> Result<GpuVaAlloc<T>, AllocError> { |
| 111 | + // INVARIANTS: Memory allocated with __GFP_ZERO. |
| 112 | + Ok(GpuVaAlloc(KBox::new_uninit(flags | __GFP_ZERO)?)) |
| 113 | + } |
| 114 | + |
| 115 | + /// Prepare this `drm_gpuva` for insertion into the GPUVM. |
| 116 | + #[must_use] |
| 117 | + pub(super) fn prepare(mut self, va_data: impl PinInit<T::VaData>) -> *mut bindings::drm_gpuva { |
| 118 | + let va_ptr = MaybeUninit::as_mut_ptr(&mut self.0); |
| 119 | + // SAFETY: The `data` field is pinned. |
| 120 | + let Ok(()) = unsafe { va_data.__pinned_init(&raw mut (*va_ptr).data) }; |
| 121 | + KBox::into_raw(self.0).cast() |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/// A [`GpuVa`] object that has been removed. |
| 126 | +/// |
| 127 | +/// # Invariants |
| 128 | +/// |
| 129 | +/// The `drm_gpuva` is not resident in the [`GpuVm`]. |
| 130 | +pub struct GpuVaRemoved<T: DriverGpuVm>(KBox<GpuVa<T>>); |
| 131 | + |
| 132 | +impl<T: DriverGpuVm> GpuVaRemoved<T> { |
| 133 | + /// Convert a raw pointer into a [`GpuVaRemoved`]. |
| 134 | + /// |
| 135 | + /// # Safety |
| 136 | + /// |
| 137 | + /// * Must have been removed from a [`GpuVm<T>`]. |
| 138 | + /// * It must not be a `gpuvm->kernel_alloc_node` va. |
| 139 | + pub(super) unsafe fn from_raw(ptr: *mut bindings::drm_gpuva) -> Self { |
| 140 | + // SAFETY: Since it used to be a VA in a `GpuVm<T>` and it's not a kernel_alloc_node, this |
| 141 | + // pointer references a `GpuVa<T>` with a valid `T::VaData`. Since it has been removed, we |
| 142 | + // can take ownership of the allocation. |
| 143 | + GpuVaRemoved(unsafe { KBox::from_raw(ptr.cast()) }) |
| 144 | + } |
| 145 | + |
| 146 | + /// Take ownership of the VA data. |
| 147 | + pub fn into_inner(self) -> T::VaData |
| 148 | + where |
| 149 | + T::VaData: Unpin, |
| 150 | + { |
| 151 | + KBox::into_inner(self.0).data |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +impl<T: DriverGpuVm> Deref for GpuVaRemoved<T> { |
| 156 | + type Target = T::VaData; |
| 157 | + fn deref(&self) -> &T::VaData { |
| 158 | + &self.0.data |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl<T: DriverGpuVm> DerefMut for GpuVaRemoved<T> |
| 163 | +where |
| 164 | + T::VaData: Unpin, |
| 165 | +{ |
| 166 | + fn deref_mut(&mut self) -> &mut T::VaData { |
| 167 | + &mut self.0.data |
| 168 | + } |
| 169 | +} |
0 commit comments