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