Skip to main content

kernel/drm/gem/
mod.rs

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
7use crate::{
8    bindings,
9    drm::{
10        self,
11        device::{
12            DeviceContext,
13            Registered, //
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};
28use core::{
29    marker::PhantomData,
30    ops::Deref,
31    ptr::NonNull, //
32};
33
34#[cfg(CONFIG_RUST_DRM_GEM_SHMEM_HELPER)]
35pub 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]
41macro_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))]
72pub(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
79pub 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
84pub type DriverAllocImpl<T, Ctx = Registered> =
85    <<T as DriverObject>::Driver as drm::Driver>::Object<Ctx>;
86
87/// GEM object functions, which must be implemented by drivers.
88pub trait DriverObject: Sync + Send + Sized {
89    /// Parent `Driver` for this object.
90    type Driver: drm::Driver;
91
92    /// The data type to use for passing arguments to [`DriverObject::new`].
93    type Args;
94
95    /// Create a new driver data object for a GEM object of a given size.
96    fn new<Ctx: DeviceContext>(
97        dev: &drm::Device<Self::Driver, Ctx>,
98        size: usize,
99        args: Self::Args,
100    ) -> impl PinInit<Self, Error>;
101
102    /// Open a new handle to an existing object, associated with a File.
103    fn open(_obj: &DriverAllocImpl<Self>, _file: &DriverFile<Self>) -> Result {
104        Ok(())
105    }
106
107    /// Close a handle to an existing object, associated with a File.
108    fn close(_obj: &DriverAllocImpl<Self>, _file: &DriverFile<Self>) {}
109}
110
111/// Trait that represents a GEM object subtype
112pub trait IntoGEMObject: Sized + super::private::Sealed + AlwaysRefCounted {
113    /// Returns a reference to the raw `drm_gem_object` structure, which must be valid as long as
114    /// this owning object is valid.
115    fn as_raw(&self) -> *mut bindings::drm_gem_object;
116
117    /// Converts a pointer to a `struct drm_gem_object` into a reference to `Self`.
118    ///
119    /// # Safety
120    ///
121    /// - `self_ptr` must be a valid pointer to `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
127extern "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
147extern "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
161impl<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
174pub 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<Registered> = 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>,
201        D: drm::Driver<Object<Registered> = 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
235impl<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))]
239pub(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
247impl<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]
257pub struct Object<T: DriverObject + Send + Sync, Ctx: DeviceContext = Registered> {
258    obj: Opaque<bindings::drm_gem_object>,
259    #[pin]
260    data: T,
261    _ctx: PhantomData<Ctx>,
262}
263
264impl<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        print_info: None,
270        export: None,
271        pin: None,
272        unpin: None,
273        get_sg_table: None,
274        vmap: None,
275        vunmap: None,
276        mmap: None,
277        status: None,
278        vm_ops: core::ptr::null_mut(),
279        evict: None,
280        rss: None,
281    };
282
283    /// Create a new GEM object.
284    pub fn new(
285        dev: &drm::Device<T::Driver, Ctx>,
286        size: usize,
287        args: T::Args,
288    ) -> Result<ARef<Self>> {
289        let obj: Pin<KBox<Self>> = KBox::pin_init(
290            try_pin_init!(Self {
291                obj: Opaque::new(bindings::drm_gem_object::default()),
292                data <- T::new(dev, size, args),
293                _ctx: PhantomData,
294            }),
295            GFP_KERNEL,
296        )?;
297
298        // SAFETY: `obj.as_raw()` is guaranteed to be valid by the initialization above.
299        unsafe { (*obj.as_raw()).funcs = &Self::OBJECT_FUNCS };
300
301        // INVARIANT: `dev` and the GEM object are in the same state at the moment, and upgrading
302        // the typestate in `dev` will not carry over to the GEM object.
303        if let Err(err) =
304            // SAFETY: The arguments are all valid per the type invariants.
305            to_result(unsafe {
306                bindings::drm_gem_object_init(dev.as_raw(), obj.obj.get(), size)
307            })
308        {
309            // SAFETY: `drm_gem_object_init()` initializes the private GEM object state before
310            // failing, so `drm_gem_private_object_fini()` is the matching cleanup.
311            unsafe { bindings::drm_gem_private_object_fini(obj.obj.get()) };
312            return Err(err);
313        }
314
315        // SAFETY: We will never move out of `Self` as `ARef<Self>` is always treated as pinned.
316        let ptr = KBox::into_raw(unsafe { Pin::into_inner_unchecked(obj) });
317
318        // SAFETY: `ptr` comes from `KBox::into_raw` and hence can't be NULL.
319        let ptr = unsafe { NonNull::new_unchecked(ptr) };
320
321        // SAFETY: We take over the initial reference count from `drm_gem_object_init()`.
322        Ok(unsafe { ARef::from_raw(ptr) })
323    }
324
325    /// Returns the `Device` that owns this GEM object.
326    pub fn dev(&self) -> &drm::Device<T::Driver, Ctx> {
327        // SAFETY:
328        // - `struct drm_gem_object.dev` is initialized and valid for as long as the GEM
329        //   object lives.
330        // - The device we used for creating the gem object is passed as &drm::Device<T::Driver> to
331        //   Object::<T>::new(), so we know that `T::Driver` is the right generic parameter to use
332        //   here.
333        // - Any type invariants of `Ctx` are upheld by using the same `Ctx` for the `Device` we
334        //   return.
335        unsafe { drm::Device::from_raw((*self.as_raw()).dev) }
336    }
337
338    fn as_raw(&self) -> *mut bindings::drm_gem_object {
339        self.obj.get()
340    }
341
342    extern "C" fn free_callback(obj: *mut bindings::drm_gem_object) {
343        let ptr: *mut Opaque<bindings::drm_gem_object> = obj.cast();
344
345        // SAFETY: All of our objects are of type `Object<T>`.
346        let this = unsafe { crate::container_of!(ptr, Self, obj) };
347
348        // SAFETY: The C code only ever calls this callback with a valid pointer to a `struct
349        // drm_gem_object`.
350        unsafe { bindings::drm_gem_object_release(obj) };
351
352        // SAFETY: All of our objects are allocated via `KBox`, and we're in the
353        // free callback which guarantees this object has zero remaining references,
354        // so we can drop it.
355        let _ = unsafe { KBox::from_raw(this) };
356    }
357}
358
359impl_aref_for_gem_obj! {
360    impl<T, C> for Object<T, C>
361    where
362        T: DriverObject,
363        C: DeviceContext
364}
365
366impl<T: DriverObject, Ctx: DeviceContext> super::private::Sealed for Object<T, Ctx> {}
367
368impl<T: DriverObject, Ctx: DeviceContext> Deref for Object<T, Ctx> {
369    type Target = T;
370
371    fn deref(&self) -> &Self::Target {
372        &self.data
373    }
374}
375
376impl<T: DriverObject, Ctx: DeviceContext> AllocImpl for Object<T, Ctx> {
377    type Driver = T::Driver;
378
379    const ALLOC_OPS: AllocOps = AllocOps {
380        gem_create_object: None,
381        prime_handle_to_fd: None,
382        prime_fd_to_handle: None,
383        gem_prime_import: None,
384        gem_prime_import_sg_table: None,
385        dumb_create: None,
386        dumb_map_offset: None,
387    };
388}
389
390pub(super) const fn create_fops() -> bindings::file_operations {
391    let mut fops: bindings::file_operations = pin_init::zeroed();
392
393    fops.owner = core::ptr::null_mut();
394    fops.open = Some(bindings::drm_open);
395    fops.release = Some(bindings::drm_release);
396    fops.unlocked_ioctl = Some(bindings::drm_ioctl);
397    #[cfg(CONFIG_COMPAT)]
398    {
399        fops.compat_ioctl = Some(bindings::drm_compat_ioctl);
400    }
401    fops.poll = Some(bindings::drm_poll);
402    fops.read = Some(bindings::drm_read);
403    fops.llseek = Some(bindings::noop_llseek);
404    fops.mmap = Some(bindings::drm_gem_mmap);
405    fops.fop_flags = bindings::FOP_UNSIGNED_OFFSET;
406
407    fops
408}