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            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};
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> = <<T as DriverObject>::Driver as drm::Driver>::Object;
85
86/// GEM object functions, which must be implemented by drivers.
87pub 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
111pub 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
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 = 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
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 = Normal> {
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    /// Returns the `Device` that owns this GEM object.
284    pub fn dev(&self) -> &drm::Device<T::Driver, Ctx> {
285        // SAFETY:
286        // - `struct drm_gem_object.dev` is initialized and valid for as long as the GEM
287        //   object lives.
288        // - The device we used for creating the gem object is passed as &drm::Device<T::Driver> to
289        //   Object::<T>::new(), so we know that `T::Driver` is the right generic parameter to use
290        //   here.
291        // - Any type invariants of `Ctx` are upheld by using the same `Ctx` for the `Device` we
292        //   return.
293        unsafe { drm::Device::from_raw((*self.as_raw()).dev) }
294    }
295
296    fn as_raw(&self) -> *mut bindings::drm_gem_object {
297        self.obj.get()
298    }
299
300    extern "C" fn free_callback(obj: *mut bindings::drm_gem_object) {
301        let ptr: *mut Opaque<bindings::drm_gem_object> = obj.cast();
302
303        // SAFETY: All of our objects are of type `Object<T>`.
304        let this = unsafe { crate::container_of!(ptr, Self, obj) };
305
306        // SAFETY: The C code only ever calls this callback with a valid pointer to a `struct
307        // drm_gem_object`.
308        unsafe { bindings::drm_gem_object_release(obj) };
309
310        // SAFETY: All of our objects are allocated via `KBox`, and we're in the
311        // free callback which guarantees this object has zero remaining references,
312        // so we can drop it.
313        let _ = unsafe { KBox::from_raw(this) };
314    }
315}
316
317impl<T: DriverObject> Object<T> {
318    /// Create a new GEM object.
319    pub fn new(dev: &drm::Device<T::Driver>, size: usize, args: T::Args) -> Result<ARef<Self>> {
320        let obj: Pin<KBox<Self>> = KBox::pin_init(
321            try_pin_init!(Self {
322                obj: Opaque::new(bindings::drm_gem_object::default()),
323                data <- T::new(dev, size, args),
324                _ctx: PhantomData,
325            }),
326            GFP_KERNEL,
327        )?;
328
329        // SAFETY: `obj.as_raw()` is guaranteed to be valid by the initialization above.
330        unsafe { (*obj.as_raw()).funcs = &Self::OBJECT_FUNCS };
331
332        // INVARIANT: `dev` and the GEM object are in the same state at the moment, and upgrading
333        // the typestate in `dev` will not carry over to the GEM object.
334        if let Err(err) =
335            // SAFETY: The arguments are all valid per the type invariants.
336            to_result(unsafe {
337                bindings::drm_gem_object_init(dev.as_raw(), obj.obj.get(), size)
338            })
339        {
340            // SAFETY: `drm_gem_object_init()` initializes the private GEM object state before
341            // failing, so `drm_gem_private_object_fini()` is the matching cleanup.
342            unsafe { bindings::drm_gem_private_object_fini(obj.obj.get()) };
343            return Err(err);
344        }
345
346        // SAFETY: We will never move out of `Self` as `ARef<Self>` is always treated as pinned.
347        let ptr = KBox::into_raw(unsafe { Pin::into_inner_unchecked(obj) });
348
349        // SAFETY: `ptr` comes from `KBox::into_raw` and hence can't be NULL.
350        let ptr = unsafe { NonNull::new_unchecked(ptr) };
351
352        // SAFETY: We take over the initial reference count from `drm_gem_object_init()`.
353        Ok(unsafe { ARef::from_raw(ptr) })
354    }
355}
356
357impl_aref_for_gem_obj! {
358    impl<T> for Object<T>
359    where
360        T: DriverObject
361}
362
363impl<T: DriverObject, Ctx: DeviceContext> super::private::Sealed for Object<T, Ctx> {}
364
365impl<T: DriverObject, Ctx: DeviceContext> Deref for Object<T, Ctx> {
366    type Target = T;
367
368    fn deref(&self) -> &Self::Target {
369        &self.data
370    }
371}
372
373impl<T: DriverObject, Ctx: DeviceContext> AllocImpl for Object<T, Ctx> {
374    type Driver = T::Driver;
375
376    const ALLOC_OPS: AllocOps = AllocOps {
377        gem_create_object: None,
378        prime_handle_to_fd: None,
379        prime_fd_to_handle: None,
380        gem_prime_import: None,
381        gem_prime_import_sg_table: None,
382        dumb_create: None,
383        dumb_map_offset: None,
384    };
385}
386
387pub(super) const fn create_fops() -> bindings::file_operations {
388    let mut fops: bindings::file_operations = pin_init::zeroed();
389
390    fops.owner = core::ptr::null_mut();
391    fops.open = Some(bindings::drm_open);
392    fops.release = Some(bindings::drm_release);
393    fops.unlocked_ioctl = Some(bindings::drm_ioctl);
394    #[cfg(CONFIG_COMPAT)]
395    {
396        fops.compat_ioctl = Some(bindings::drm_compat_ioctl);
397    }
398    fops.poll = Some(bindings::drm_poll);
399    fops.read = Some(bindings::drm_read);
400    fops.llseek = Some(bindings::noop_llseek);
401    fops.mmap = Some(bindings::drm_gem_mmap);
402    fops.fop_flags = bindings::FOP_UNSIGNED_OFFSET;
403
404    fops
405}