1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 3 //! DRM device. 4 //! 5 //! C header: [`include/linux/drm/drm_device.h`](srctree/include/linux/drm/drm_device.h) 6 7 use crate::{ 8 alloc::allocator::Kmalloc, 9 bindings, device, drm, 10 drm::driver::AllocImpl, 11 error::from_err_ptr, 12 error::Result, 13 prelude::*, 14 types::{ARef, AlwaysRefCounted, Opaque}, 15 }; 16 use core::{alloc::Layout, mem, ops::Deref, ptr, ptr::NonNull}; 17 18 #[cfg(CONFIG_DRM_LEGACY)] 19 macro_rules! drm_legacy_fields { 20 ( $($field:ident: $val:expr),* $(,)? ) => { 21 bindings::drm_driver { 22 $( $field: $val ),*, 23 firstopen: None, 24 preclose: None, 25 dma_ioctl: None, 26 dma_quiescent: None, 27 context_dtor: None, 28 irq_handler: None, 29 irq_preinstall: None, 30 irq_postinstall: None, 31 irq_uninstall: None, 32 get_vblank_counter: None, 33 enable_vblank: None, 34 disable_vblank: None, 35 dev_priv_size: 0, 36 } 37 } 38 } 39 40 #[cfg(not(CONFIG_DRM_LEGACY))] 41 macro_rules! drm_legacy_fields { 42 ( $($field:ident: $val:expr),* $(,)? ) => { 43 bindings::drm_driver { 44 $( $field: $val ),* 45 } 46 } 47 } 48 49 /// A typed DRM device with a specific `drm::Driver` implementation. 50 /// 51 /// The device is always reference-counted. 52 /// 53 /// # Invariants 54 /// 55 /// `self.dev` is a valid instance of a `struct device`. 56 #[repr(C)] 57 #[pin_data] 58 pub struct Device<T: drm::Driver> { 59 dev: Opaque<bindings::drm_device>, 60 #[pin] 61 data: T::Data, 62 } 63 64 impl<T: drm::Driver> Device<T> { 65 const VTABLE: bindings::drm_driver = drm_legacy_fields! { 66 load: None, 67 open: Some(drm::File::<T::File>::open_callback), 68 postclose: Some(drm::File::<T::File>::postclose_callback), 69 unload: None, 70 release: Some(Self::release), 71 master_set: None, 72 master_drop: None, 73 debugfs_init: None, 74 gem_create_object: T::Object::ALLOC_OPS.gem_create_object, 75 prime_handle_to_fd: T::Object::ALLOC_OPS.prime_handle_to_fd, 76 prime_fd_to_handle: T::Object::ALLOC_OPS.prime_fd_to_handle, 77 gem_prime_import: T::Object::ALLOC_OPS.gem_prime_import, 78 gem_prime_import_sg_table: T::Object::ALLOC_OPS.gem_prime_import_sg_table, 79 dumb_create: T::Object::ALLOC_OPS.dumb_create, 80 dumb_map_offset: T::Object::ALLOC_OPS.dumb_map_offset, 81 show_fdinfo: None, 82 fbdev_probe: None, 83 84 major: T::INFO.major, 85 minor: T::INFO.minor, 86 patchlevel: T::INFO.patchlevel, 87 name: T::INFO.name.as_char_ptr().cast_mut(), 88 desc: T::INFO.desc.as_char_ptr().cast_mut(), 89 90 driver_features: drm::driver::FEAT_GEM, 91 ioctls: T::IOCTLS.as_ptr(), 92 num_ioctls: T::IOCTLS.len() as i32, 93 fops: &Self::GEM_FOPS, 94 }; 95 96 const GEM_FOPS: bindings::file_operations = drm::gem::create_fops(); 97 98 /// Create a new `drm::Device` for a `drm::Driver`. 99 pub fn new(dev: &device::Device, data: impl PinInit<T::Data, Error>) -> Result<ARef<Self>> { 100 // `__drm_dev_alloc` uses `kmalloc()` to allocate memory, hence ensure a `kmalloc()` 101 // compatible `Layout`. 102 let layout = Kmalloc::aligned_layout(Layout::new::<Self>()); 103 104 // SAFETY: 105 // - `VTABLE`, as a `const` is pinned to the read-only section of the compilation, 106 // - `dev` is valid by its type invarants, 107 let raw_drm: *mut Self = unsafe { 108 bindings::__drm_dev_alloc( 109 dev.as_raw(), 110 &Self::VTABLE, 111 layout.size(), 112 mem::offset_of!(Self, dev), 113 ) 114 } 115 .cast(); 116 let raw_drm = NonNull::new(from_err_ptr(raw_drm)?).ok_or(ENOMEM)?; 117 118 // SAFETY: `raw_drm` is a valid pointer to `Self`. 119 let raw_data = unsafe { ptr::addr_of_mut!((*raw_drm.as_ptr()).data) }; 120 121 // SAFETY: 122 // - `raw_data` is a valid pointer to uninitialized memory. 123 // - `raw_data` will not move until it is dropped. 124 unsafe { data.__pinned_init(raw_data) }.inspect_err(|_| { 125 // SAFETY: `__drm_dev_alloc()` was successful, hence `raw_drm` must be valid and the 126 // refcount must be non-zero. 127 unsafe { bindings::drm_dev_put(ptr::addr_of_mut!((*raw_drm.as_ptr()).dev).cast()) }; 128 })?; 129 130 // SAFETY: The reference count is one, and now we take ownership of that reference as a 131 // `drm::Device`. 132 Ok(unsafe { ARef::from_raw(raw_drm) }) 133 } 134 135 pub(crate) fn as_raw(&self) -> *mut bindings::drm_device { 136 self.dev.get() 137 } 138 139 /// # Safety 140 /// 141 /// `ptr` must be a valid pointer to a `struct device` embedded in `Self`. 142 unsafe fn from_drm_device(ptr: *const bindings::drm_device) -> *mut Self { 143 // SAFETY: By the safety requirements of this function `ptr` is a valid pointer to a 144 // `struct drm_device` embedded in `Self`. 145 unsafe { crate::container_of!(Opaque::cast_from(ptr), Self, dev) }.cast_mut() 146 } 147 148 /// Not intended to be called externally, except via declare_drm_ioctls!() 149 /// 150 /// # Safety 151 /// 152 /// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count, 153 /// i.e. it must be ensured that the reference count of the C `struct drm_device` `ptr` points 154 /// to can't drop to zero, for the duration of this function call and the entire duration when 155 /// the returned reference exists. 156 /// 157 /// Additionally, callers must ensure that the `struct device`, `ptr` is pointing to, is 158 /// embedded in `Self`. 159 #[doc(hidden)] 160 pub unsafe fn from_raw<'a>(ptr: *const bindings::drm_device) -> &'a Self { 161 // SAFETY: By the safety requirements of this function `ptr` is a valid pointer to a 162 // `struct drm_device` embedded in `Self`. 163 let ptr = unsafe { Self::from_drm_device(ptr) }; 164 165 // SAFETY: `ptr` is valid by the safety requirements of this function. 166 unsafe { &*ptr.cast() } 167 } 168 169 extern "C" fn release(ptr: *mut bindings::drm_device) { 170 // SAFETY: `ptr` is a valid pointer to a `struct drm_device` and embedded in `Self`. 171 let this = unsafe { Self::from_drm_device(ptr) }; 172 173 // SAFETY: 174 // - When `release` runs it is guaranteed that there is no further access to `this`. 175 // - `this` is valid for dropping. 176 unsafe { core::ptr::drop_in_place(this) }; 177 } 178 } 179 180 impl<T: drm::Driver> Deref for Device<T> { 181 type Target = T::Data; 182 183 fn deref(&self) -> &Self::Target { 184 &self.data 185 } 186 } 187 188 // SAFETY: DRM device objects are always reference counted and the get/put functions 189 // satisfy the requirements. 190 unsafe impl<T: drm::Driver> AlwaysRefCounted for Device<T> { 191 fn inc_ref(&self) { 192 // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. 193 unsafe { bindings::drm_dev_get(self.as_raw()) }; 194 } 195 196 unsafe fn dec_ref(obj: NonNull<Self>) { 197 // SAFETY: The safety requirements guarantee that the refcount is non-zero. 198 unsafe { bindings::drm_dev_put(obj.cast().as_ptr()) }; 199 } 200 } 201 202 impl<T: drm::Driver> AsRef<device::Device> for Device<T> { 203 fn as_ref(&self) -> &device::Device { 204 // SAFETY: `bindings::drm_device::dev` is valid as long as the DRM device itself is valid, 205 // which is guaranteed by the type invariant. 206 unsafe { device::Device::from_raw((*self.as_raw()).dev) } 207 } 208 } 209 210 // SAFETY: A `drm::Device` can be released from any thread. 211 unsafe impl<T: drm::Driver> Send for Device<T> {} 212 213 // SAFETY: A `drm::Device` can be shared among threads because all immutable methods are protected 214 // by the synchronization in `struct drm_device`. 215 unsafe impl<T: drm::Driver> Sync for Device<T> {} 216