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