kernel/drm/
driver.rs

1// SPDX-License-Identifier: GPL-2.0 OR MIT
2
3//! DRM driver core.
4//!
5//! C header: [`include/linux/drm/drm_drv.h`](srctree/include/linux/drm/drm_drv.h)
6
7use crate::{
8    bindings, device,
9    devres::Devres,
10    drm,
11    error::{to_result, Result},
12    prelude::*,
13    str::CStr,
14    types::ARef,
15};
16use macros::vtable;
17
18/// Driver use the GEM memory manager. This should be set for all modern drivers.
19pub(crate) const FEAT_GEM: u32 = bindings::drm_driver_feature_DRIVER_GEM;
20
21/// Information data for a DRM Driver.
22pub struct DriverInfo {
23    /// Driver major version.
24    pub major: i32,
25    /// Driver minor version.
26    pub minor: i32,
27    /// Driver patchlevel version.
28    pub patchlevel: i32,
29    /// Driver name.
30    pub name: &'static CStr,
31    /// Driver description.
32    pub desc: &'static CStr,
33}
34
35/// Internal memory management operation set, normally created by memory managers (e.g. GEM).
36pub struct AllocOps {
37    pub(crate) gem_create_object: Option<
38        unsafe extern "C" fn(
39            dev: *mut bindings::drm_device,
40            size: usize,
41        ) -> *mut bindings::drm_gem_object,
42    >,
43    pub(crate) prime_handle_to_fd: Option<
44        unsafe extern "C" fn(
45            dev: *mut bindings::drm_device,
46            file_priv: *mut bindings::drm_file,
47            handle: u32,
48            flags: u32,
49            prime_fd: *mut core::ffi::c_int,
50        ) -> core::ffi::c_int,
51    >,
52    pub(crate) prime_fd_to_handle: Option<
53        unsafe extern "C" fn(
54            dev: *mut bindings::drm_device,
55            file_priv: *mut bindings::drm_file,
56            prime_fd: core::ffi::c_int,
57            handle: *mut u32,
58        ) -> core::ffi::c_int,
59    >,
60    pub(crate) gem_prime_import: Option<
61        unsafe extern "C" fn(
62            dev: *mut bindings::drm_device,
63            dma_buf: *mut bindings::dma_buf,
64        ) -> *mut bindings::drm_gem_object,
65    >,
66    pub(crate) gem_prime_import_sg_table: Option<
67        unsafe extern "C" fn(
68            dev: *mut bindings::drm_device,
69            attach: *mut bindings::dma_buf_attachment,
70            sgt: *mut bindings::sg_table,
71        ) -> *mut bindings::drm_gem_object,
72    >,
73    pub(crate) dumb_create: Option<
74        unsafe extern "C" fn(
75            file_priv: *mut bindings::drm_file,
76            dev: *mut bindings::drm_device,
77            args: *mut bindings::drm_mode_create_dumb,
78        ) -> core::ffi::c_int,
79    >,
80    pub(crate) dumb_map_offset: Option<
81        unsafe extern "C" fn(
82            file_priv: *mut bindings::drm_file,
83            dev: *mut bindings::drm_device,
84            handle: u32,
85            offset: *mut u64,
86        ) -> core::ffi::c_int,
87    >,
88}
89
90/// Trait for memory manager implementations. Implemented internally.
91pub trait AllocImpl: super::private::Sealed + drm::gem::IntoGEMObject {
92    /// The C callback operations for this memory manager.
93    const ALLOC_OPS: AllocOps;
94}
95
96/// The DRM `Driver` trait.
97///
98/// This trait must be implemented by drivers in order to create a `struct drm_device` and `struct
99/// drm_driver` to be registered in the DRM subsystem.
100#[vtable]
101pub trait Driver {
102    /// Context data associated with the DRM driver
103    type Data: Sync + Send;
104
105    /// The type used to manage memory for this driver.
106    type Object: AllocImpl;
107
108    /// The type used to represent a DRM File (client)
109    type File: drm::file::DriverFile;
110
111    /// Driver metadata
112    const INFO: DriverInfo;
113
114    /// IOCTL list. See `kernel::drm::ioctl::declare_drm_ioctls!{}`.
115    const IOCTLS: &'static [drm::ioctl::DrmIoctlDescriptor];
116}
117
118/// The registration type of a `drm::Device`.
119///
120/// Once the `Registration` structure is dropped, the device is unregistered.
121pub struct Registration<T: Driver>(ARef<drm::Device<T>>);
122
123impl<T: Driver> Registration<T> {
124    /// Creates a new [`Registration`] and registers it.
125    fn new(drm: &drm::Device<T>, flags: usize) -> Result<Self> {
126        // SAFETY: `drm.as_raw()` is valid by the invariants of `drm::Device`.
127        to_result(unsafe { bindings::drm_dev_register(drm.as_raw(), flags) })?;
128
129        Ok(Self(drm.into()))
130    }
131
132    /// Same as [`Registration::new`}, but transfers ownership of the [`Registration`] to
133    /// [`Devres`].
134    pub fn new_foreign_owned(
135        drm: &drm::Device<T>,
136        dev: &device::Device<device::Bound>,
137        flags: usize,
138    ) -> Result {
139        if drm.as_ref().as_raw() != dev.as_raw() {
140            return Err(EINVAL);
141        }
142
143        let reg = Registration::<T>::new(drm, flags)?;
144        Devres::new_foreign_owned(dev, reg, GFP_KERNEL)
145    }
146
147    /// Returns a reference to the `Device` instance for this registration.
148    pub fn device(&self) -> &drm::Device<T> {
149        &self.0
150    }
151}
152
153// SAFETY: `Registration` doesn't offer any methods or access to fields when shared between
154// threads, hence it's safe to share it.
155unsafe impl<T: Driver> Sync for Registration<T> {}
156
157// SAFETY: Registration with and unregistration from the DRM subsystem can happen from any thread.
158unsafe impl<T: Driver> Send for Registration<T> {}
159
160impl<T: Driver> Drop for Registration<T> {
161    fn drop(&mut self) {
162        // SAFETY: Safe by the invariant of `ARef<drm::Device<T>>`. The existence of this
163        // `Registration` also guarantees the this `drm::Device` is actually registered.
164        unsafe { bindings::drm_dev_unregister(self.0.as_raw()) };
165    }
166}