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