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