| 1 | |
| 2 |
|
| 3 | #![cfg(CONFIG_RUST_DRM_GPUVM)]
|
| 4 |
|
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 |
|
| 13 | use kernel::{
|
| 14 | alloc::{
|
| 15 | AllocError,
|
| 16 | Flags as AllocFlags, |
| 17 | },
|
| 18 | bindings,
|
| 19 | drm,
|
| 20 | drm::gem::IntoGEMObject,
|
| 21 | error::to_result,
|
| 22 | prelude::*,
|
| 23 | sync::aref::{
|
| 24 | ARef,
|
| 25 | AlwaysRefCounted, |
| 26 | },
|
| 27 | types::Opaque, |
| 28 | };
|
| 29 |
|
| 30 | use core::{
|
| 31 | cell::UnsafeCell,
|
| 32 | marker::PhantomData,
|
| 33 | mem::{
|
| 34 | ManuallyDrop,
|
| 35 | MaybeUninit, |
| 36 | },
|
| 37 | ops::{
|
| 38 | Deref,
|
| 39 | DerefMut,
|
| 40 | Range, |
| 41 | },
|
| 42 | ptr::{
|
| 43 | self,
|
| 44 | NonNull, |
| 45 | }, |
| 46 | };
|
| 47 |
|
| 48 | mod sm_ops;
|
| 49 | pub use self::sm_ops::*;
|
| 50 |
|
| 51 | mod vm_bo;
|
| 52 | pub use self::vm_bo::*;
|
| 53 |
|
| 54 | mod va;
|
| 55 | pub use self::va::*;
|
| 56 |
|
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | #[pin_data]
|
| 68 | pub struct GpuVm<T: DriverGpuVm> {
|
| 69 | #[pin]
|
| 70 | vm: Opaque<bindings::drm_gpuvm>,
|
| 71 | |
| 72 | data: UnsafeCell<T>,
|
| 73 | }
|
| 74 |
|
| 75 | |
| 76 | |
| 77 | unsafe impl<T: DriverGpuVm> Send for GpuVm<T> {}
|
| 78 | |
| 79 | |
| 80 | |
| 81 | unsafe impl<T: DriverGpuVm> Sync for GpuVm<T> {}
|
| 82 |
|
| 83 | |
| 84 | unsafe impl<T: DriverGpuVm> AlwaysRefCounted for GpuVm<T> {
|
| 85 | fn inc_ref(&self) {
|
| 86 | |
| 87 | unsafe { bindings::drm_gpuvm_get(self.vm.get()) };
|
| 88 | }
|
| 89 |
|
| 90 | unsafe fn dec_ref(obj: NonNull<Self>) {
|
| 91 | |
| 92 | unsafe { bindings::drm_gpuvm_put((*obj.as_ptr()).vm.get()) };
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | impl<T: DriverGpuVm> PartialEq for GpuVm<T> {
|
| 97 | #[inline]
|
| 98 | fn eq(&self, other: &Self) -> bool {
|
| 99 | core::ptr::eq(self.as_raw(), other.as_raw())
|
| 100 | }
|
| 101 | }
|
| 102 | impl<T: DriverGpuVm> Eq for GpuVm<T> {}
|
| 103 |
|
| 104 | impl<T: DriverGpuVm> GpuVm<T> {
|
| 105 | const fn vtable() -> &'static bindings::drm_gpuvm_ops {
|
| 106 | &bindings::drm_gpuvm_ops {
|
| 107 | vm_free: Some(Self::vm_free),
|
| 108 | op_alloc: None,
|
| 109 | op_free: None,
|
| 110 | vm_bo_alloc: GpuVmBo::<T>::ALLOC_FN,
|
| 111 | vm_bo_free: GpuVmBo::<T>::FREE_FN,
|
| 112 | vm_bo_validate: None,
|
| 113 | sm_step_map: Some(Self::sm_step_map),
|
| 114 | sm_step_unmap: Some(Self::sm_step_unmap),
|
| 115 | sm_step_remap: Some(Self::sm_step_remap),
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | |
| 120 | #[expect(clippy::new_ret_no_self)]
|
| 121 | pub fn new<E, Ctx: drm::DeviceContext>(
|
| 122 | name: &'static CStr,
|
| 123 | dev: &drm::Device<T::Driver, Ctx>,
|
| 124 | r_obj: &T::Object,
|
| 125 | range: Range<u64>,
|
| 126 | reserve_range: Range<u64>,
|
| 127 | data: T,
|
| 128 | ) -> Result<UniqueRefGpuVm<T>, E>
|
| 129 | where
|
| 130 | E: From<AllocError>,
|
| 131 | E: From<core::convert::Infallible>,
|
| 132 | {
|
| 133 | let obj = KBox::try_pin_init::<E>(
|
| 134 | try_pin_init!(Self {
|
| 135 | data: UnsafeCell::new(data),
|
| 136 | vm <- Opaque::ffi_init(|vm| {
|
| 137 | |
| 138 | |
| 139 | |
| 140 | unsafe {
|
| 141 | bindings::drm_gpuvm_init(
|
| 142 | vm,
|
| 143 | name.as_char_ptr(),
|
| 144 | bindings::drm_gpuvm_flags_DRM_GPUVM_IMMEDIATE_MODE
|
| 145 | | bindings::drm_gpuvm_flags_DRM_GPUVM_RESV_PROTECTED,
|
| 146 | dev.as_raw(),
|
| 147 | r_obj.as_raw(),
|
| 148 | range.start,
|
| 149 | range.end - range.start,
|
| 150 | reserve_range.start,
|
| 151 | reserve_range.end - reserve_range.start,
|
| 152 | const { Self::vtable() },
|
| 153 | )
|
| 154 | }
|
| 155 | }),
|
| 156 | }? E),
|
| 157 | GFP_KERNEL | __GFP_ZERO,
|
| 158 | )?;
|
| 159 | |
| 160 | let aref = unsafe {
|
| 161 | ARef::from_raw(NonNull::new_unchecked(KBox::into_raw(
|
| 162 | Pin::into_inner_unchecked(obj),
|
| 163 | )))
|
| 164 | };
|
| 165 | |
| 166 | Ok(UniqueRefGpuVm(aref))
|
| 167 | }
|
| 168 |
|
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | #[inline]
|
| 176 | pub unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuvm) -> &'a Self {
|
| 177 | |
| 178 | |
| 179 | unsafe { &*kernel::container_of!(Opaque::cast_from(ptr), Self, vm) }
|
| 180 | }
|
| 181 |
|
| 182 | |
| 183 | #[inline]
|
| 184 | pub fn as_raw(&self) -> *mut bindings::drm_gpuvm {
|
| 185 | self.vm.get()
|
| 186 | }
|
| 187 |
|
| 188 | |
| 189 | #[inline]
|
| 190 | pub fn va_start(&self) -> u64 {
|
| 191 | |
| 192 | unsafe { (*self.as_raw()).mm_start }
|
| 193 | }
|
| 194 |
|
| 195 | |
| 196 | #[inline]
|
| 197 | pub fn va_length(&self) -> u64 {
|
| 198 | |
| 199 | unsafe { (*self.as_raw()).mm_range }
|
| 200 | }
|
| 201 |
|
| 202 | |
| 203 | #[inline]
|
| 204 | pub fn va_range(&self) -> Range<u64> {
|
| 205 | let start = self.va_start();
|
| 206 | |
| 207 | let end = start + self.va_length();
|
| 208 | Range { start, end }
|
| 209 | }
|
| 210 |
|
| 211 | |
| 212 | #[inline]
|
| 213 | pub fn obtain(
|
| 214 | &self,
|
| 215 | obj: &T::Object,
|
| 216 | data: impl PinInit<T::VmBoData>,
|
| 217 | ) -> Result<ARef<GpuVmBo<T>>, AllocError> {
|
| 218 | Ok(GpuVmBoAlloc::new(self, obj, data)?.obtain())
|
| 219 | }
|
| 220 |
|
| 221 | |
| 222 | #[inline]
|
| 223 | pub fn deferred_cleanup(&self) {
|
| 224 | |
| 225 | unsafe { bindings::drm_gpuvm_bo_deferred_cleanup(self.as_raw()) }
|
| 226 | }
|
| 227 |
|
| 228 | |
| 229 | #[inline]
|
| 230 | pub fn is_extobj(&self, obj: &T::Object) -> bool {
|
| 231 | |
| 232 | unsafe { bindings::drm_gpuvm_is_extobj(self.as_raw(), obj.as_raw()) }
|
| 233 | }
|
| 234 |
|
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | unsafe extern "C" fn vm_free(me: *mut bindings::drm_gpuvm) {
|
| 241 | |
| 242 | let me = unsafe { kernel::container_of!(Opaque::cast_from(me), Self, vm).cast_mut() };
|
| 243 | |
| 244 | drop(unsafe { KBox::from_raw(me) })
|
| 245 | }
|
| 246 |
|
| 247 | #[inline]
|
| 248 | fn raw_resv(&self) -> *mut bindings::dma_resv {
|
| 249 | |
| 250 | unsafe { (*(*self.as_raw()).r_obj).resv }
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | |
| 255 | pub trait DriverGpuVm: Sized + Send + Sync {
|
| 256 | |
| 257 | type Driver: drm::Driver;
|
| 258 |
|
| 259 | |
| 260 | type Object: drm::driver::AllocImpl<Driver = Self::Driver> + Send + Sync;
|
| 261 |
|
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | type VaData: Send;
|
| 268 |
|
| 269 | |
| 270 | type VmBoData: Send + Sync;
|
| 271 |
|
| 272 | |
| 273 | type SmContext<'ctx>
|
| 274 | where
|
| 275 | Self: 'ctx;
|
| 276 |
|
| 277 | |
| 278 | fn sm_step_map<'op, 'ctx>(
|
| 279 | &mut self,
|
| 280 | op: OpMap<'op, Self>,
|
| 281 | context: &mut Self::SmContext<'ctx>,
|
| 282 | ) -> Result<OpMapped<'op, Self>, Error>;
|
| 283 |
|
| 284 | |
| 285 | fn sm_step_unmap<'op, 'ctx>(
|
| 286 | &mut self,
|
| 287 | op: OpUnmap<'op, Self>,
|
| 288 | context: &mut Self::SmContext<'ctx>,
|
| 289 | ) -> Result<OpUnmapped<'op, Self>, Error>;
|
| 290 |
|
| 291 | |
| 292 | fn sm_step_remap<'op, 'ctx>(
|
| 293 | &mut self,
|
| 294 | op: OpRemap<'op, Self>,
|
| 295 | context: &mut Self::SmContext<'ctx>,
|
| 296 | ) -> Result<OpRemapped<'op, Self>, Error>;
|
| 297 | }
|
| 298 |
|
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | pub struct UniqueRefGpuVm<T: DriverGpuVm>(ARef<GpuVm<T>>);
|
| 310 |
|
| 311 | impl<T: DriverGpuVm> UniqueRefGpuVm<T> {
|
| 312 | |
| 313 | #[inline]
|
| 314 | pub fn data_ref(&self) -> &T {
|
| 315 | |
| 316 | unsafe { &*self.0.data.get() }
|
| 317 | }
|
| 318 |
|
| 319 | |
| 320 | #[inline]
|
| 321 | pub fn data(&mut self) -> &mut T {
|
| 322 | |
| 323 | unsafe { &mut *self.0.data.get() }
|
| 324 | }
|
| 325 | }
|
| 326 |
|
| 327 | impl<T: DriverGpuVm> Deref for UniqueRefGpuVm<T> {
|
| 328 | type Target = GpuVm<T>;
|
| 329 |
|
| 330 | #[inline]
|
| 331 | fn deref(&self) -> &GpuVm<T> {
|
| 332 | &self.0
|
| 333 | }
|
| 334 | }
|