Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: drm: add device abstraction

Implement the abstraction for a `struct drm_device`.

A `drm::Device` creates a static const `struct drm_driver` filled with
the data from the `drm::Driver` trait implementation of the actual
driver creating the `drm::Device`.

Reviewed-by: Maxime Ripard <mripard@kernel.org>
Signed-off-by: Asahi Lina <lina@asahilina.net>
Reviewed-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Lyude Paul <lyude@redhat.com>
Link: https://lore.kernel.org/r/20250410235546.43736-5-dakr@kernel.org
[ Rewrite of drm::Device
* full rewrite of the drm::Device abstraction using the subclassing
pattern
* original source archive: http://archive.today/5NxBo

- Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

authored by

Asahi Lina and committed by
Danilo Krummrich
1e4b8896 07c90160

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