mirror of
https://github.com/torvalds/linux.git
synced 2025-08-15 14:11:42 +02:00

Toolchain and infrastructure: - Enable a set of Clippy lints: 'ptr_as_ptr', 'ptr_cast_constness', 'as_ptr_cast_mut', 'as_underscore', 'cast_lossless' and 'ref_as_ptr'. These are intended to avoid type casts with the 'as' operator, which are quite powerful, into restricted variants that are less powerful and thus should help to avoid mistakes. - Remove the 'author' key now that most instances were moved to the plural one in the previous cycle. 'kernel' crate: - New 'bug' module: add 'warn_on!' macro which reuses the existing 'BUG'/'WARN' infrastructure, i.e. it respects the usual sysctls and kernel parameters: warn_on!(value == 42); To avoid duplicating the assembly code, the same strategy is followed as for the static branch code in order to share the assembly between both C and Rust. This required a few rearrangements on C arch headers -- the existing C macros should still generate the same outputs, thus no functional change expected there. - 'workqueue' module: add delayed work items, including a 'DelayedWork' struct, a 'impl_has_delayed_work!' macro and an 'enqueue_delayed' method, e.g.: /// Enqueue the struct for execution on the system workqueue, /// where its value will be printed 42 jiffies later. fn print_later(value: Arc<MyStruct>) { let _ = workqueue::system().enqueue_delayed(value, 42); } - New 'bits' module: add support for 'bit' and 'genmask' functions, with runtime- and compile-time variants, e.g.: static_assert!(0b00010000 == bit_u8(4)); static_assert!(0b00011110 == genmask_u8(1..=4)); assert!(checked_bit_u32(u32::BITS).is_none()); - 'uaccess' module: add 'UserSliceReader::strcpy_into_buf', which reads NUL-terminated strings from userspace into a '&CStr'. Introduce 'UserPtr' newtype, similar in purpose to '__user' in C, to minimize mistakes handling userspace pointers, including mixing them up with integers and leaking them via the 'Debug' trait. Add it to the prelude, too. - Start preparations for the replacement of our custom 'CStr' type with the analogous type in the 'core' standard library. This will take place across several cycles to make it easier. For this one, it includes a new 'fmt' module, using upstream method names and some other cleanups. Replace 'fmt!' with a re-export, which helps Clippy lint properly, and clean up the found 'uninlined-format-args' instances. - 'dma' module: - Clarify wording and be consistent in 'coherent' nomenclature. - Convert the 'read!()' and 'write!()' macros to return a 'Result'. - Add 'as_slice()', 'write()' methods in 'CoherentAllocation'. - Expose 'count()' and 'size()' in 'CoherentAllocation' and add the corresponding type invariants. - Implement 'CoherentAllocation::dma_handle_with_offset()'. - 'time' module: - Make 'Instant' generic over clock source. This allows the compiler to assert that arithmetic expressions involving the 'Instant' use 'Instants' based on the same clock source. - Make 'HrTimer' generic over the timer mode. 'HrTimer' timers take a 'Duration' or an 'Instant' when setting the expiry time, depending on the timer mode. With this change, the compiler can check the type matches the timer mode. - Add an abstraction for 'fsleep'. 'fsleep' is a flexible sleep function that will select an appropriate sleep method depending on the requested sleep time. - Avoid 64-bit divisions on 32-bit hardware when calculating timestamps. - Seal the 'HrTimerMode' trait. This prevents users of the 'HrTimerMode' from implementing the trait on their own types. - Pass the correct timer mode ID to 'hrtimer_start_range_ns()'. - 'list' module: remove 'OFFSET' constants, allowing to remove pointer arithmetic; now 'impl_list_item!' invokes 'impl_has_list_links!' or 'impl_has_list_links_self_ptr!'. Other simplifications too. - 'types' module: remove 'ForeignOwnable::PointedTo' in favor of a constant, which avoids exposing the type of the opaque pointer, and require 'into_foreign' to return non-null. Remove the 'Either<L, R>' type as well. It is unused, and we want to encourage the use of custom enums for concrete use cases. - 'sync' module: implement 'Borrow' and 'BorrowMut' for 'Arc' types to allow them to be used in generic APIs. - 'alloc' module: implement 'Borrow' and 'BorrowMut' for 'Box<T, A>'; and 'Borrow', 'BorrowMut' and 'Default' for 'Vec<T, A>'. - 'Opaque' type: add 'cast_from' method to perform a restricted cast that cannot change the inner type and use it in callers of 'container_of!'. Rename 'raw_get' to 'cast_into' to match it. - 'rbtree' module: add 'is_empty' method. - 'sync' module: new 'aref' submodule to hold 'AlwaysRefCounted' and 'ARef', which are moved from the too general 'types' module which we want to reduce or eventually remove. Also fix a safety comment in 'static_lock_class'. 'pin-init' crate: - Add 'impl<T, E> [Pin]Init<T, E> for Result<T, E>', so results are now (pin-)initializers. - Add 'Zeroable::init_zeroed()' that delegates to 'init_zeroed()'. - New 'zeroed()', a safe version of 'mem::zeroed()' and also provide it via 'Zeroable::zeroed()'. - Implement 'Zeroable' for 'Option<&T>', 'Option<&mut T>' and for 'Option<[unsafe] [extern "abi"] fn(...args...) -> ret>' for '"Rust"' and '"C"' ABIs and up to 20 arguments. - Changed blanket impls of 'Init' and 'PinInit' from 'impl<T, E> [Pin]Init<T, E> for T' to 'impl<T> [Pin]Init<T> for T'. - Renamed 'zeroed()' to 'init_zeroed()'. - Upstream dev news: improve CI more to deny warnings, use '--all-targets'. Check the synchronization status of the two '-next' branches in upstream and the kernel. MAINTAINERS: - Add Vlastimil Babka, Liam R. Howlett, Uladzislau Rezki and Lorenzo Stoakes as reviewers (thanks everyone). And a few other cleanups and improvements. -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEPjU5OPd5QIZ9jqqOGXyLc2htIW0FAmiOWREACgkQGXyLc2ht IW39Ig/9E0ExSiBgNKdkCOaULMq31wAxnu3iWoVVisFndlh/Inv+JlaLrmA57BCi xXgBwVZ1GoMsG8Fzt6gT+gyhGYi8waNd+5KXr/WJZVTaJ9v1KpdvxuCnSz0DjCbk GaKfAfxvJ5GAOEwiIIX8X0TFu6kx911DCJY387/VrqZQ7Msh1QSM3tcZeir/EV4w lPjUdlOh1FnLJLI9CGuW20d1IhQUP7K3pdoywgJPpCZV0I8QCyMlMqCEael8Tw2S r/PzRaQtiIzk5HTx06V8paK+nEn0K2vQXqW2kV56Y6TNm1Zcv6dES/8hCITsISs2 nwney3vXEwvoZX+YkQRffZddY4i6YenWMrtLgVxZzdshBL3bn6eHqBL04Nfix+p7 pQe3qMH3G8UBtX1lugBE7RrWGWcz9ARN8sK12ClmpAUnKJOwTpo97kpqXP7pDme8 Buh/oV3voAMsqwooSbVBzuUUWnbGaQ5Oj6CiiosSadfNh6AxJLYLKHtRLKJHZEw3 0Ob/1HhoWS6JSvYKVjMyD19qcH7O8ThZE+83CfMAkI4KphXJarWhpSmN4cHkFn/v 0clQ7Y5m+up9v1XWTaEq0Biqa6CaxLQwm/qW5WU0Y/TiovmvxAFdCwsQqDkRoJNx 9kNfMJRvNl78KQxrjEDz9gl7/ajgqX1KkqP8CQbGjv29cGzFlVE= =5Wt9 -----END PGP SIGNATURE----- Merge tag 'rust-6.17' of git://git.kernel.org/pub/scm/linux/kernel/git/ojeda/linux Pull Rust updates from Miguel Ojeda: "Toolchain and infrastructure: - Enable a set of Clippy lints: 'ptr_as_ptr', 'ptr_cast_constness', 'as_ptr_cast_mut', 'as_underscore', 'cast_lossless' and 'ref_as_ptr' These are intended to avoid type casts with the 'as' operator, which are quite powerful, into restricted variants that are less powerful and thus should help to avoid mistakes - Remove the 'author' key now that most instances were moved to the plural one in the previous cycle 'kernel' crate: - New 'bug' module: add 'warn_on!' macro which reuses the existing 'BUG'/'WARN' infrastructure, i.e. it respects the usual sysctls and kernel parameters: warn_on!(value == 42); To avoid duplicating the assembly code, the same strategy is followed as for the static branch code in order to share the assembly between both C and Rust This required a few rearrangements on C arch headers -- the existing C macros should still generate the same outputs, thus no functional change expected there - 'workqueue' module: add delayed work items, including a 'DelayedWork' struct, a 'impl_has_delayed_work!' macro and an 'enqueue_delayed' method, e.g.: /// Enqueue the struct for execution on the system workqueue, /// where its value will be printed 42 jiffies later. fn print_later(value: Arc<MyStruct>) { let _ = workqueue::system().enqueue_delayed(value, 42); } - New 'bits' module: add support for 'bit' and 'genmask' functions, with runtime- and compile-time variants, e.g.: static_assert!(0b00010000 == bit_u8(4)); static_assert!(0b00011110 == genmask_u8(1..=4)); assert!(checked_bit_u32(u32::BITS).is_none()); - 'uaccess' module: add 'UserSliceReader::strcpy_into_buf', which reads NUL-terminated strings from userspace into a '&CStr' Introduce 'UserPtr' newtype, similar in purpose to '__user' in C, to minimize mistakes handling userspace pointers, including mixing them up with integers and leaking them via the 'Debug' trait. Add it to the prelude, too - Start preparations for the replacement of our custom 'CStr' type with the analogous type in the 'core' standard library. This will take place across several cycles to make it easier. For this one, it includes a new 'fmt' module, using upstream method names and some other cleanups Replace 'fmt!' with a re-export, which helps Clippy lint properly, and clean up the found 'uninlined-format-args' instances - 'dma' module: - Clarify wording and be consistent in 'coherent' nomenclature - Convert the 'read!()' and 'write!()' macros to return a 'Result' - Add 'as_slice()', 'write()' methods in 'CoherentAllocation' - Expose 'count()' and 'size()' in 'CoherentAllocation' and add the corresponding type invariants - Implement 'CoherentAllocation::dma_handle_with_offset()' - 'time' module: - Make 'Instant' generic over clock source. This allows the compiler to assert that arithmetic expressions involving the 'Instant' use 'Instants' based on the same clock source - Make 'HrTimer' generic over the timer mode. 'HrTimer' timers take a 'Duration' or an 'Instant' when setting the expiry time, depending on the timer mode. With this change, the compiler can check the type matches the timer mode - Add an abstraction for 'fsleep'. 'fsleep' is a flexible sleep function that will select an appropriate sleep method depending on the requested sleep time - Avoid 64-bit divisions on 32-bit hardware when calculating timestamps - Seal the 'HrTimerMode' trait. This prevents users of the 'HrTimerMode' from implementing the trait on their own types - Pass the correct timer mode ID to 'hrtimer_start_range_ns()' - 'list' module: remove 'OFFSET' constants, allowing to remove pointer arithmetic; now 'impl_list_item!' invokes 'impl_has_list_links!' or 'impl_has_list_links_self_ptr!'. Other simplifications too - 'types' module: remove 'ForeignOwnable::PointedTo' in favor of a constant, which avoids exposing the type of the opaque pointer, and require 'into_foreign' to return non-null Remove the 'Either<L, R>' type as well. It is unused, and we want to encourage the use of custom enums for concrete use cases - 'sync' module: implement 'Borrow' and 'BorrowMut' for 'Arc' types to allow them to be used in generic APIs - 'alloc' module: implement 'Borrow' and 'BorrowMut' for 'Box<T, A>'; and 'Borrow', 'BorrowMut' and 'Default' for 'Vec<T, A>' - 'Opaque' type: add 'cast_from' method to perform a restricted cast that cannot change the inner type and use it in callers of 'container_of!'. Rename 'raw_get' to 'cast_into' to match it - 'rbtree' module: add 'is_empty' method - 'sync' module: new 'aref' submodule to hold 'AlwaysRefCounted' and 'ARef', which are moved from the too general 'types' module which we want to reduce or eventually remove. Also fix a safety comment in 'static_lock_class' 'pin-init' crate: - Add 'impl<T, E> [Pin]Init<T, E> for Result<T, E>', so results are now (pin-)initializers - Add 'Zeroable::init_zeroed()' that delegates to 'init_zeroed()' - New 'zeroed()', a safe version of 'mem::zeroed()' and also provide it via 'Zeroable::zeroed()' - Implement 'Zeroable' for 'Option<&T>', 'Option<&mut T>' and for 'Option<[unsafe] [extern "abi"] fn(...args...) -> ret>' for '"Rust"' and '"C"' ABIs and up to 20 arguments - Changed blanket impls of 'Init' and 'PinInit' from 'impl<T, E> [Pin]Init<T, E> for T' to 'impl<T> [Pin]Init<T> for T' - Renamed 'zeroed()' to 'init_zeroed()' - Upstream dev news: improve CI more to deny warnings, use '--all-targets'. Check the synchronization status of the two '-next' branches in upstream and the kernel MAINTAINERS: - Add Vlastimil Babka, Liam R. Howlett, Uladzislau Rezki and Lorenzo Stoakes as reviewers (thanks everyone) And a few other cleanups and improvements" * tag 'rust-6.17' of git://git.kernel.org/pub/scm/linux/kernel/git/ojeda/linux: (76 commits) rust: Add warn_on macro arm64/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust riscv/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust x86/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust rust: kernel: move ARef and AlwaysRefCounted to sync::aref rust: sync: fix safety comment for `static_lock_class` rust: types: remove `Either<L, R>` rust: kernel: use `core::ffi::CStr` method names rust: str: add `CStr` methods matching `core::ffi::CStr` rust: str: remove unnecessary qualification rust: use `kernel::{fmt,prelude::fmt!}` rust: kernel: add `fmt` module rust: kernel: remove `fmt!`, fix clippy::uninlined-format-args scripts: rust: emit path candidates in panic message scripts: rust: replace length checks with match rust: list: remove nonexistent generic parameter in link rust: bits: add support for bits/genmask macros rust: list: remove OFFSET constants rust: list: add `impl_list_item!` examples rust: list: use fully qualified path ...
330 lines
12 KiB
Rust
330 lines
12 KiB
Rust
// SPDX-License-Identifier: GPL-2.0 OR MIT
|
|
|
|
//! DRM GEM API
|
|
//!
|
|
//! C header: [`include/linux/drm/drm_gem.h`](srctree/include/linux/drm/drm_gem.h)
|
|
|
|
use crate::{
|
|
alloc::flags::*,
|
|
bindings, drm,
|
|
drm::driver::{AllocImpl, AllocOps},
|
|
error::{to_result, Result},
|
|
prelude::*,
|
|
types::{ARef, AlwaysRefCounted, Opaque},
|
|
};
|
|
use core::{mem, ops::Deref, ptr::NonNull};
|
|
|
|
/// GEM object functions, which must be implemented by drivers.
|
|
pub trait BaseDriverObject<T: BaseObject>: Sync + Send + Sized {
|
|
/// Create a new driver data object for a GEM object of a given size.
|
|
fn new(dev: &drm::Device<T::Driver>, size: usize) -> impl PinInit<Self, Error>;
|
|
|
|
/// Open a new handle to an existing object, associated with a File.
|
|
fn open(
|
|
_obj: &<<T as IntoGEMObject>::Driver as drm::Driver>::Object,
|
|
_file: &drm::File<<<T as IntoGEMObject>::Driver as drm::Driver>::File>,
|
|
) -> Result {
|
|
Ok(())
|
|
}
|
|
|
|
/// Close a handle to an existing object, associated with a File.
|
|
fn close(
|
|
_obj: &<<T as IntoGEMObject>::Driver as drm::Driver>::Object,
|
|
_file: &drm::File<<<T as IntoGEMObject>::Driver as drm::Driver>::File>,
|
|
) {
|
|
}
|
|
}
|
|
|
|
/// Trait that represents a GEM object subtype
|
|
pub trait IntoGEMObject: Sized + super::private::Sealed + AlwaysRefCounted {
|
|
/// Owning driver for this type
|
|
type Driver: drm::Driver;
|
|
|
|
/// Returns a reference to the raw `drm_gem_object` structure, which must be valid as long as
|
|
/// this owning object is valid.
|
|
fn as_raw(&self) -> *mut bindings::drm_gem_object;
|
|
|
|
/// Converts a pointer to a `struct drm_gem_object` into a reference to `Self`.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// - `self_ptr` must be a valid pointer to `Self`.
|
|
/// - The caller promises that holding the immutable reference returned by this function does
|
|
/// not violate rust's data aliasing rules and remains valid throughout the lifetime of `'a`.
|
|
unsafe fn from_raw<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a Self;
|
|
}
|
|
|
|
// SAFETY: All gem objects are refcounted.
|
|
unsafe impl<T: IntoGEMObject> AlwaysRefCounted for T {
|
|
fn inc_ref(&self) {
|
|
// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
|
|
unsafe { bindings::drm_gem_object_get(self.as_raw()) };
|
|
}
|
|
|
|
unsafe fn dec_ref(obj: NonNull<Self>) {
|
|
// SAFETY: We either hold the only refcount on `obj`, or one of many - meaning that no one
|
|
// else could possibly hold a mutable reference to `obj` and thus this immutable reference
|
|
// is safe.
|
|
let obj = unsafe { obj.as_ref() }.as_raw();
|
|
|
|
// SAFETY:
|
|
// - The safety requirements guarantee that the refcount is non-zero.
|
|
// - We hold no references to `obj` now, making it safe for us to potentially deallocate it.
|
|
unsafe { bindings::drm_gem_object_put(obj) };
|
|
}
|
|
}
|
|
|
|
/// Trait which must be implemented by drivers using base GEM objects.
|
|
pub trait DriverObject: BaseDriverObject<Object<Self>> {
|
|
/// Parent `Driver` for this object.
|
|
type Driver: drm::Driver;
|
|
}
|
|
|
|
extern "C" fn open_callback<T: BaseDriverObject<U>, U: BaseObject>(
|
|
raw_obj: *mut bindings::drm_gem_object,
|
|
raw_file: *mut bindings::drm_file,
|
|
) -> core::ffi::c_int {
|
|
// SAFETY: `open_callback` is only ever called with a valid pointer to a `struct drm_file`.
|
|
let file = unsafe {
|
|
drm::File::<<<U as IntoGEMObject>::Driver as drm::Driver>::File>::from_raw(raw_file)
|
|
};
|
|
// SAFETY: `open_callback` is specified in the AllocOps structure for `Object<T>`, ensuring that
|
|
// `raw_obj` is indeed contained within a `Object<T>`.
|
|
let obj = unsafe {
|
|
<<<U as IntoGEMObject>::Driver as drm::Driver>::Object as IntoGEMObject>::from_raw(raw_obj)
|
|
};
|
|
|
|
match T::open(obj, file) {
|
|
Err(e) => e.to_errno(),
|
|
Ok(()) => 0,
|
|
}
|
|
}
|
|
|
|
extern "C" fn close_callback<T: BaseDriverObject<U>, U: BaseObject>(
|
|
raw_obj: *mut bindings::drm_gem_object,
|
|
raw_file: *mut bindings::drm_file,
|
|
) {
|
|
// SAFETY: `open_callback` is only ever called with a valid pointer to a `struct drm_file`.
|
|
let file = unsafe {
|
|
drm::File::<<<U as IntoGEMObject>::Driver as drm::Driver>::File>::from_raw(raw_file)
|
|
};
|
|
// SAFETY: `close_callback` is specified in the AllocOps structure for `Object<T>`, ensuring
|
|
// that `raw_obj` is indeed contained within a `Object<T>`.
|
|
let obj = unsafe {
|
|
<<<U as IntoGEMObject>::Driver as drm::Driver>::Object as IntoGEMObject>::from_raw(raw_obj)
|
|
};
|
|
|
|
T::close(obj, file);
|
|
}
|
|
|
|
impl<T: DriverObject> IntoGEMObject for Object<T> {
|
|
type Driver = T::Driver;
|
|
|
|
fn as_raw(&self) -> *mut bindings::drm_gem_object {
|
|
self.obj.get()
|
|
}
|
|
|
|
unsafe fn from_raw<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a Self {
|
|
// SAFETY: `obj` is guaranteed to be in an `Object<T>` via the safety contract of this
|
|
// function
|
|
unsafe { &*crate::container_of!(Opaque::cast_from(self_ptr), Object<T>, obj) }
|
|
}
|
|
}
|
|
|
|
/// Base operations shared by all GEM object classes
|
|
pub trait BaseObject: IntoGEMObject {
|
|
/// Returns the size of the object in bytes.
|
|
fn size(&self) -> usize {
|
|
// SAFETY: `self.as_raw()` is guaranteed to be a pointer to a valid `struct drm_gem_object`.
|
|
unsafe { (*self.as_raw()).size }
|
|
}
|
|
|
|
/// Creates a new handle for the object associated with a given `File`
|
|
/// (or returns an existing one).
|
|
fn create_handle(
|
|
&self,
|
|
file: &drm::File<<<Self as IntoGEMObject>::Driver as drm::Driver>::File>,
|
|
) -> Result<u32> {
|
|
let mut handle: u32 = 0;
|
|
// SAFETY: The arguments are all valid per the type invariants.
|
|
to_result(unsafe {
|
|
bindings::drm_gem_handle_create(file.as_raw().cast(), self.as_raw(), &mut handle)
|
|
})?;
|
|
Ok(handle)
|
|
}
|
|
|
|
/// Looks up an object by its handle for a given `File`.
|
|
fn lookup_handle(
|
|
file: &drm::File<<<Self as IntoGEMObject>::Driver as drm::Driver>::File>,
|
|
handle: u32,
|
|
) -> Result<ARef<Self>> {
|
|
// SAFETY: The arguments are all valid per the type invariants.
|
|
let ptr = unsafe { bindings::drm_gem_object_lookup(file.as_raw().cast(), handle) };
|
|
if ptr.is_null() {
|
|
return Err(ENOENT);
|
|
}
|
|
|
|
// SAFETY:
|
|
// - A `drm::Driver` can only have a single `File` implementation.
|
|
// - `file` uses the same `drm::Driver` as `Self`.
|
|
// - Therefore, we're guaranteed that `ptr` must be a gem object embedded within `Self`.
|
|
// - And we check if the pointer is null befoe calling from_raw(), ensuring that `ptr` is a
|
|
// valid pointer to an initialized `Self`.
|
|
let obj = unsafe { Self::from_raw(ptr) };
|
|
|
|
// SAFETY:
|
|
// - We take ownership of the reference of `drm_gem_object_lookup()`.
|
|
// - Our `NonNull` comes from an immutable reference, thus ensuring it is a valid pointer to
|
|
// `Self`.
|
|
Ok(unsafe { ARef::from_raw(obj.into()) })
|
|
}
|
|
|
|
/// Creates an mmap offset to map the object from userspace.
|
|
fn create_mmap_offset(&self) -> Result<u64> {
|
|
// SAFETY: The arguments are valid per the type invariant.
|
|
to_result(unsafe { bindings::drm_gem_create_mmap_offset(self.as_raw()) })?;
|
|
|
|
// SAFETY: The arguments are valid per the type invariant.
|
|
Ok(unsafe { bindings::drm_vma_node_offset_addr(&raw mut (*self.as_raw()).vma_node) })
|
|
}
|
|
}
|
|
|
|
impl<T: IntoGEMObject> BaseObject for T {}
|
|
|
|
/// A base GEM object.
|
|
///
|
|
/// Invariants
|
|
///
|
|
/// - `self.obj` is a valid instance of a `struct drm_gem_object`.
|
|
/// - `self.dev` is always a valid pointer to a `struct drm_device`.
|
|
#[repr(C)]
|
|
#[pin_data]
|
|
pub struct Object<T: DriverObject + Send + Sync> {
|
|
obj: Opaque<bindings::drm_gem_object>,
|
|
dev: NonNull<drm::Device<T::Driver>>,
|
|
#[pin]
|
|
data: T,
|
|
}
|
|
|
|
impl<T: DriverObject> Object<T> {
|
|
/// The size of this object's structure.
|
|
pub const SIZE: usize = mem::size_of::<Self>();
|
|
|
|
const OBJECT_FUNCS: bindings::drm_gem_object_funcs = bindings::drm_gem_object_funcs {
|
|
free: Some(Self::free_callback),
|
|
open: Some(open_callback::<T, Object<T>>),
|
|
close: Some(close_callback::<T, Object<T>>),
|
|
print_info: None,
|
|
export: None,
|
|
pin: None,
|
|
unpin: None,
|
|
get_sg_table: None,
|
|
vmap: None,
|
|
vunmap: None,
|
|
mmap: None,
|
|
status: None,
|
|
vm_ops: core::ptr::null_mut(),
|
|
evict: None,
|
|
rss: None,
|
|
};
|
|
|
|
/// Create a new GEM object.
|
|
pub fn new(dev: &drm::Device<T::Driver>, size: usize) -> Result<ARef<Self>> {
|
|
let obj: Pin<KBox<Self>> = KBox::pin_init(
|
|
try_pin_init!(Self {
|
|
obj: Opaque::new(bindings::drm_gem_object::default()),
|
|
data <- T::new(dev, size),
|
|
// INVARIANT: The drm subsystem guarantees that the `struct drm_device` will live
|
|
// as long as the GEM object lives.
|
|
dev: dev.into(),
|
|
}),
|
|
GFP_KERNEL,
|
|
)?;
|
|
|
|
// SAFETY: `obj.as_raw()` is guaranteed to be valid by the initialization above.
|
|
unsafe { (*obj.as_raw()).funcs = &Self::OBJECT_FUNCS };
|
|
|
|
// SAFETY: The arguments are all valid per the type invariants.
|
|
to_result(unsafe { bindings::drm_gem_object_init(dev.as_raw(), obj.obj.get(), size) })?;
|
|
|
|
// SAFETY: We never move out of `Self`.
|
|
let ptr = KBox::into_raw(unsafe { Pin::into_inner_unchecked(obj) });
|
|
|
|
// SAFETY: `ptr` comes from `KBox::into_raw` and hence can't be NULL.
|
|
let ptr = unsafe { NonNull::new_unchecked(ptr) };
|
|
|
|
// SAFETY: We take over the initial reference count from `drm_gem_object_init()`.
|
|
Ok(unsafe { ARef::from_raw(ptr) })
|
|
}
|
|
|
|
/// Returns the `Device` that owns this GEM object.
|
|
pub fn dev(&self) -> &drm::Device<T::Driver> {
|
|
// SAFETY: The DRM subsystem guarantees that the `struct drm_device` will live as long as
|
|
// the GEM object lives, hence the pointer must be valid.
|
|
unsafe { self.dev.as_ref() }
|
|
}
|
|
|
|
fn as_raw(&self) -> *mut bindings::drm_gem_object {
|
|
self.obj.get()
|
|
}
|
|
|
|
extern "C" fn free_callback(obj: *mut bindings::drm_gem_object) {
|
|
let ptr: *mut Opaque<bindings::drm_gem_object> = obj.cast();
|
|
|
|
// SAFETY: All of our objects are of type `Object<T>`.
|
|
let this = unsafe { crate::container_of!(ptr, Self, obj) };
|
|
|
|
// SAFETY: The C code only ever calls this callback with a valid pointer to a `struct
|
|
// drm_gem_object`.
|
|
unsafe { bindings::drm_gem_object_release(obj) };
|
|
|
|
// SAFETY: All of our objects are allocated via `KBox`, and we're in the
|
|
// free callback which guarantees this object has zero remaining references,
|
|
// so we can drop it.
|
|
let _ = unsafe { KBox::from_raw(this) };
|
|
}
|
|
}
|
|
|
|
impl<T: DriverObject> super::private::Sealed for Object<T> {}
|
|
|
|
impl<T: DriverObject> Deref for Object<T> {
|
|
type Target = T;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.data
|
|
}
|
|
}
|
|
|
|
impl<T: DriverObject> AllocImpl for Object<T> {
|
|
const ALLOC_OPS: AllocOps = AllocOps {
|
|
gem_create_object: None,
|
|
prime_handle_to_fd: None,
|
|
prime_fd_to_handle: None,
|
|
gem_prime_import: None,
|
|
gem_prime_import_sg_table: None,
|
|
dumb_create: None,
|
|
dumb_map_offset: None,
|
|
};
|
|
}
|
|
|
|
pub(super) const fn create_fops() -> bindings::file_operations {
|
|
// SAFETY: As by the type invariant, it is safe to initialize `bindings::file_operations`
|
|
// zeroed.
|
|
let mut fops: bindings::file_operations = unsafe { core::mem::zeroed() };
|
|
|
|
fops.owner = core::ptr::null_mut();
|
|
fops.open = Some(bindings::drm_open);
|
|
fops.release = Some(bindings::drm_release);
|
|
fops.unlocked_ioctl = Some(bindings::drm_ioctl);
|
|
#[cfg(CONFIG_COMPAT)]
|
|
{
|
|
fops.compat_ioctl = Some(bindings::drm_compat_ioctl);
|
|
}
|
|
fops.poll = Some(bindings::drm_poll);
|
|
fops.read = Some(bindings::drm_read);
|
|
fops.llseek = Some(bindings::noop_llseek);
|
|
fops.mmap = Some(bindings::drm_gem_mmap);
|
|
fops.fop_flags = bindings::FOP_UNSIGNED_OFFSET;
|
|
|
|
fops
|
|
}
|