xref: /linux/rust/kernel/drm/gem/mod.rs (revision 2446a767ffacee0d247db932621cd4b165ad721a)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 
3 //! DRM GEM API
4 //!
5 //! C header: [`include/drm/drm_gem.h`](srctree/include/drm/drm_gem.h)
6 
7 use crate::{
8     bindings,
9     drm::{
10         self,
11         device::{
12             DeviceContext,
13             Normal, //
14         },
15         driver::{
16             AllocImpl,
17             AllocOps, //
18         },
19     },
20     error::to_result,
21     prelude::*,
22     sync::aref::{
23         ARef,
24         AlwaysRefCounted, //
25     },
26     types::Opaque,
27 };
28 use core::{
29     marker::PhantomData,
30     ops::Deref,
31     ptr::NonNull, //
32 };
33 
34 #[cfg(CONFIG_RUST_DRM_GEM_SHMEM_HELPER)]
35 pub mod shmem;
36 
37 /// A macro for implementing [`AlwaysRefCounted`] for any GEM object type.
38 ///
39 /// Since all GEM objects use the same refcounting scheme.
40 #[macro_export]
41 macro_rules! impl_aref_for_gem_obj {
42     (
43         impl $( <$( $tparam_id:ident ),+> )? for $type:ty
44         $(
45             where
46                 $( $bind_param:path : $bind_trait:path ),+
47         )?
48     ) => {
49         // SAFETY: All GEM objects are refcounted.
50         unsafe impl $( <$( $tparam_id ),+> )? $crate::sync::aref::AlwaysRefCounted for $type
51         where
52             Self: IntoGEMObject,
53             $( $( $bind_param : $bind_trait ),+ )?
54         {
55             fn inc_ref(&self) {
56                 // SAFETY: The existence of a shared reference guarantees that the refcount is
57                 // non-zero.
58                 unsafe { bindings::drm_gem_object_get(self.as_raw()) };
59             }
60 
61             unsafe fn dec_ref(obj: core::ptr::NonNull<Self>) {
62                 // SAFETY: `obj` is a valid pointer to an `Object<T>`.
63                 let obj = unsafe { obj.as_ref() }.as_raw();
64 
65                 // SAFETY: The safety requirements guarantee that the refcount is non-zero.
66                 unsafe { bindings::drm_gem_object_put(obj) };
67             }
68         }
69     };
70 }
71 #[cfg_attr(not(CONFIG_RUST_DRM_GEM_SHMEM_HELPER), allow(unused))]
72 pub(crate) use impl_aref_for_gem_obj;
73 
74 /// A type alias for retrieving a [`Driver`]s [`DriverFile`] implementation from its
75 /// [`DriverObject`] implementation.
76 ///
77 /// [`Driver`]: drm::Driver
78 /// [`DriverFile`]: drm::file::DriverFile
79 pub type DriverFile<T> = drm::File<<<T as DriverObject>::Driver as drm::Driver>::File>;
80 
81 /// A type alias for retrieving the current [`AllocImpl`] for a given [`DriverObject`].
82 ///
83 /// [`Driver`]: drm::Driver
84 pub type DriverAllocImpl<T> = <<T as DriverObject>::Driver as drm::Driver>::Object;
85 
86 /// GEM object functions, which must be implemented by drivers.
87 pub trait DriverObject: Sync + Send + Sized + 'static {
88     /// Parent `Driver` for this object.
89     type Driver: drm::Driver;
90 
91     /// The data type to use for passing arguments to [`DriverObject::new`].
92     type Args;
93 
94     /// Create a new driver data object for a GEM object of a given size.
95     fn new(
96         dev: &drm::Device<Self::Driver>,
97         size: usize,
98         args: Self::Args,
99     ) -> impl PinInit<Self, Error>;
100 
101     /// Open a new handle to an existing object, associated with a File.
102     fn open(_obj: &DriverAllocImpl<Self>, _file: &DriverFile<Self>) -> Result {
103         Ok(())
104     }
105 
106     /// Close a handle to an existing object, associated with a File.
107     fn close(_obj: &DriverAllocImpl<Self>, _file: &DriverFile<Self>) {}
108 }
109 
110 /// Trait that represents a GEM object subtype
111 pub trait IntoGEMObject: Sized + super::private::Sealed {
112     /// Returns a reference to the raw `drm_gem_object` structure, which must be valid as long as
113     /// this owning object is valid.
114     fn as_raw(&self) -> *mut bindings::drm_gem_object;
115 
116     /// Converts a pointer to a `struct drm_gem_object` into a reference to `Self`.
117     ///
118     /// # Safety
119     ///
120     /// - `self_ptr` must be a valid pointer to the `struct drm_gem_object` embedded in a
121     ///   valid instance of `Self`.
122     /// - The caller promises that holding the immutable reference returned by this function does
123     ///   not violate rust's data aliasing rules and remains valid throughout the lifetime of `'a`.
124     unsafe fn from_raw<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a Self;
125 }
126 
127 extern "C" fn open_callback<T: DriverObject>(
128     raw_obj: *mut bindings::drm_gem_object,
129     raw_file: *mut bindings::drm_file,
130 ) -> core::ffi::c_int {
131     // SAFETY: `open_callback` is only ever called with a valid pointer to a `struct drm_file`.
132     let file = unsafe { DriverFile::<T>::from_raw(raw_file) };
133 
134     // SAFETY:
135     // * `open_callback` is specified in the AllocOps structure for `DriverObject`, ensuring that
136     //   `raw_obj` is contained within a `DriverAllocImpl<T>`
137     // * It is only possible for `open_callback` to be called after device registration, ensuring
138     //   that the object's device is in the `Registered` state.
139     let obj: &DriverAllocImpl<T> = unsafe { IntoGEMObject::from_raw(raw_obj) };
140 
141     match T::open(obj, file) {
142         Err(e) => e.to_errno(),
143         Ok(()) => 0,
144     }
145 }
146 
147 extern "C" fn close_callback<T: DriverObject>(
148     raw_obj: *mut bindings::drm_gem_object,
149     raw_file: *mut bindings::drm_file,
150 ) {
151     // SAFETY: `open_callback` is only ever called with a valid pointer to a `struct drm_file`.
152     let file = unsafe { DriverFile::<T>::from_raw(raw_file) };
153 
154     // SAFETY: `close_callback` is specified in the AllocOps structure for `Object<T>`, ensuring
155     // that `raw_obj` is indeed contained within a `Object<T>`.
156     let obj: &DriverAllocImpl<T> = unsafe { IntoGEMObject::from_raw(raw_obj) };
157 
158     T::close(obj, file);
159 }
160 
161 impl<T: DriverObject, Ctx: DeviceContext> IntoGEMObject for Object<T, Ctx> {
162     fn as_raw(&self) -> *mut bindings::drm_gem_object {
163         self.obj.get()
164     }
165 
166     unsafe fn from_raw<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a Self {
167         // SAFETY: `obj` is guaranteed to be in an `Object<T>` via the safety contract of this
168         // function
169         unsafe { &*crate::container_of!(Opaque::cast_from(self_ptr), Object<T, Ctx>, obj) }
170     }
171 }
172 
173 /// Base operations shared by all GEM object classes
174 pub trait BaseObject: IntoGEMObject {
175     /// Returns the size of the object in bytes.
176     fn size(&self) -> usize {
177         // SAFETY: `self.as_raw()` is guaranteed to be a pointer to a valid `struct drm_gem_object`.
178         unsafe { (*self.as_raw()).size }
179     }
180 
181     /// Creates a new handle for the object associated with a given `File`
182     /// (or returns an existing one).
183     fn create_handle<D, F>(&self, file: &drm::File<F>) -> Result<u32>
184     where
185         Self: AllocImpl<Driver = D>,
186         D: drm::Driver<Object = Self, File = F>,
187         F: drm::file::DriverFile<Driver = D>,
188     {
189         let mut handle: u32 = 0;
190         // SAFETY: The arguments are all valid per the type invariants.
191         to_result(unsafe {
192             bindings::drm_gem_handle_create(file.as_raw().cast(), self.as_raw(), &mut handle)
193         })?;
194         Ok(handle)
195     }
196 
197     /// Looks up an object by its handle for a given `File`.
198     fn lookup_handle<D, F>(file: &drm::File<F>, handle: u32) -> Result<ARef<Self>>
199     where
200         Self: AllocImpl<Driver = D> + AlwaysRefCounted,
201         D: drm::Driver<Object = Self, File = F>,
202         F: drm::file::DriverFile<Driver = D>,
203     {
204         // SAFETY: The arguments are all valid per the type invariants.
205         let ptr = unsafe { bindings::drm_gem_object_lookup(file.as_raw().cast(), handle) };
206         if ptr.is_null() {
207             return Err(ENOENT);
208         }
209 
210         // SAFETY:
211         // - A `drm::Driver` can only have a single `File` implementation.
212         // - `file` uses the same `drm::Driver` as `Self`.
213         // - Therefore, we're guaranteed that `ptr` must be a gem object embedded within `Self`.
214         // - And we check if the pointer is null befoe calling from_raw(), ensuring that `ptr` is a
215         //   valid pointer to an initialized `Self`.
216         let obj = unsafe { Self::from_raw(ptr) };
217 
218         // SAFETY:
219         // - We take ownership of the reference of `drm_gem_object_lookup()`.
220         // - Our `NonNull` comes from an immutable reference, thus ensuring it is a valid pointer to
221         //   `Self`.
222         Ok(unsafe { ARef::from_raw(obj.into()) })
223     }
224 
225     /// Creates an mmap offset to map the object from userspace.
226     fn create_mmap_offset(&self) -> Result<u64> {
227         // SAFETY: The arguments are valid per the type invariant.
228         to_result(unsafe { bindings::drm_gem_create_mmap_offset(self.as_raw()) })?;
229 
230         // SAFETY: The arguments are valid per the type invariant.
231         Ok(unsafe { bindings::drm_vma_node_offset_addr(&raw mut (*self.as_raw()).vma_node) })
232     }
233 }
234 
235 impl<T: IntoGEMObject> BaseObject for T {}
236 
237 /// Crate-private base operations shared by all GEM object classes.
238 #[cfg_attr(not(CONFIG_RUST_DRM_GEM_SHMEM_HELPER), expect(unused))]
239 pub(crate) trait BaseObjectPrivate: IntoGEMObject {
240     /// Return a pointer to this object's dma_resv.
241     fn raw_dma_resv(&self) -> *mut bindings::dma_resv {
242         // SAFETY: `self.as_raw()` always returns a valid pointer to the base DRM GEM object.
243         unsafe { (*self.as_raw()).resv }
244     }
245 }
246 
247 impl<T: IntoGEMObject> BaseObjectPrivate for T {}
248 
249 /// A base GEM object.
250 ///
251 /// # Invariants
252 ///
253 /// * `self.obj` is a valid instance of a `struct drm_gem_object`.
254 /// * Any type invariants of `Ctx` apply to the parent DRM device for this GEM object.
255 #[repr(C)]
256 #[pin_data]
257 pub struct Object<T: DriverObject + Send + Sync, Ctx: DeviceContext = Normal> {
258     obj: Opaque<bindings::drm_gem_object>,
259     #[pin]
260     data: T,
261     _ctx: PhantomData<Ctx>,
262 }
263 
264 impl<T: DriverObject, Ctx: DeviceContext> Object<T, Ctx> {
265     const OBJECT_FUNCS: bindings::drm_gem_object_funcs = bindings::drm_gem_object_funcs {
266         free: Some(Self::free_callback),
267         open: Some(open_callback::<T>),
268         close: Some(close_callback::<T>),
269         ..pin_init::zeroed()
270     };
271 
272     /// Returns the `Device` that owns this GEM object.
273     pub fn dev(&self) -> &drm::Device<T::Driver, Ctx> {
274         // SAFETY:
275         // - `struct drm_gem_object.dev` is initialized and valid for as long as the GEM
276         //   object lives.
277         // - The device we used for creating the gem object is passed as &drm::Device<T::Driver> to
278         //   Object::<T>::new(), so we know that `T::Driver` is the right generic parameter to use
279         //   here.
280         // - Any type invariants of `Ctx` are upheld by using the same `Ctx` for the `Device` we
281         //   return.
282         unsafe { drm::Device::from_raw((*self.as_raw()).dev) }
283     }
284 
285     fn as_raw(&self) -> *mut bindings::drm_gem_object {
286         self.obj.get()
287     }
288 
289     extern "C" fn free_callback(obj: *mut bindings::drm_gem_object) {
290         let ptr: *mut Opaque<bindings::drm_gem_object> = obj.cast();
291 
292         // SAFETY: All of our objects are of type `Object<T>`.
293         let this = unsafe { crate::container_of!(ptr, Self, obj) };
294 
295         // SAFETY: The C code only ever calls this callback with a valid pointer to a `struct
296         // drm_gem_object`.
297         unsafe { bindings::drm_gem_object_release(obj) };
298 
299         // SAFETY: All of our objects are allocated via `KBox`, and we're in the
300         // free callback which guarantees this object has zero remaining references,
301         // so we can drop it.
302         let _ = unsafe { KBox::from_raw(this) };
303     }
304 }
305 
306 impl<T: DriverObject> Object<T> {
307     /// Create a new GEM object.
308     pub fn new(dev: &drm::Device<T::Driver>, size: usize, args: T::Args) -> Result<ARef<Self>> {
309         let obj: Pin<KBox<Self>> = KBox::pin_init(
310             try_pin_init!(Self {
311                 obj: Opaque::new(bindings::drm_gem_object::default()),
312                 data <- T::new(dev, size, args),
313                 _ctx: PhantomData,
314             }),
315             GFP_KERNEL,
316         )?;
317 
318         // SAFETY: `obj.as_raw()` is guaranteed to be valid by the initialization above.
319         unsafe { (*obj.as_raw()).funcs = &Self::OBJECT_FUNCS };
320 
321         // INVARIANT: `dev` and the GEM object are in the same state at the moment, and upgrading
322         // the typestate in `dev` will not carry over to the GEM object.
323         if let Err(err) =
324             // SAFETY: The arguments are all valid per the type invariants.
325             to_result(unsafe {
326                 bindings::drm_gem_object_init(dev.as_raw(), obj.obj.get(), size)
327             })
328         {
329             // SAFETY: `drm_gem_object_init()` initializes the private GEM object state before
330             // failing, so `drm_gem_private_object_fini()` is the matching cleanup.
331             unsafe { bindings::drm_gem_private_object_fini(obj.obj.get()) };
332             return Err(err);
333         }
334 
335         // SAFETY: We will never move out of `Self` as `ARef<Self>` is always treated as pinned.
336         let ptr = KBox::into_raw(unsafe { Pin::into_inner_unchecked(obj) });
337 
338         // SAFETY: `ptr` comes from `KBox::into_raw` and hence can't be NULL.
339         let ptr = unsafe { NonNull::new_unchecked(ptr) };
340 
341         // SAFETY: We take over the initial reference count from `drm_gem_object_init()`.
342         Ok(unsafe { ARef::from_raw(ptr) })
343     }
344 }
345 
346 impl_aref_for_gem_obj! {
347     impl<T> for Object<T>
348     where
349         T: DriverObject
350 }
351 
352 impl<T: DriverObject, Ctx: DeviceContext> super::private::Sealed for Object<T, Ctx> {}
353 
354 impl<T: DriverObject, Ctx: DeviceContext> Deref for Object<T, Ctx> {
355     type Target = T;
356 
357     fn deref(&self) -> &Self::Target {
358         &self.data
359     }
360 }
361 
362 impl<T: DriverObject, Ctx: DeviceContext> AllocImpl for Object<T, Ctx> {
363     type Driver = T::Driver;
364 
365     const ALLOC_OPS: AllocOps = AllocOps {
366         gem_create_object: None,
367         prime_handle_to_fd: None,
368         prime_fd_to_handle: None,
369         gem_prime_import: None,
370         gem_prime_import_sg_table: None,
371         dumb_create: None,
372         dumb_map_offset: None,
373     };
374 }
375 
376 pub(super) const fn create_fops(owner: *mut bindings::module) -> bindings::file_operations {
377     let mut fops: bindings::file_operations = pin_init::zeroed();
378 
379     fops.owner = owner;
380     fops.open = Some(bindings::drm_open);
381     fops.release = Some(bindings::drm_release);
382     fops.unlocked_ioctl = Some(bindings::drm_ioctl);
383     #[cfg(CONFIG_COMPAT)]
384     {
385         fops.compat_ioctl = Some(bindings::drm_compat_ioctl);
386     }
387     fops.poll = Some(bindings::drm_poll);
388     fops.read = Some(bindings::drm_read);
389     fops.llseek = Some(bindings::noop_llseek);
390     fops.mmap = Some(bindings::drm_gem_mmap);
391     fops.fop_flags = bindings::FOP_UNSIGNED_OFFSET;
392 
393     fops
394 }
395