An optional trait method for implementing a CRTC's atomic state check. Signed-off-by: Lyude Paul <lyude@xxxxxxxxxx> --- V3: * Document uses of ManuallyDrop Signed-off-by: Lyude Paul <lyude@xxxxxxxxxx> --- rust/kernel/drm/kms/atomic.rs | 1 - rust/kernel/drm/kms/crtc.rs | 55 +++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/rust/kernel/drm/kms/atomic.rs b/rust/kernel/drm/kms/atomic.rs index 3d5c70dbc4274..e0a1b5b860d6f 100644 --- a/rust/kernel/drm/kms/atomic.rs +++ b/rust/kernel/drm/kms/atomic.rs @@ -274,7 +274,6 @@ impl<T: KmsDriver> AtomicStateComposer<T> { /// # Safety /// /// The caller guarantees that `ptr` points to a valid instance of `drm_atomic_state`. - #[allow(dead_code)] pub(crate) unsafe fn new(ptr: NonNull<bindings::drm_atomic_state>) -> Self { // SAFETY: see `AtomicStateMutator::from_raw()` Self(unsafe { AtomicStateMutator::new(ptr) }) diff --git a/rust/kernel/drm/kms/crtc.rs b/rust/kernel/drm/kms/crtc.rs index 3b9c9d97fcf24..50f5b68f4a3fe 100644 --- a/rust/kernel/drm/kms/crtc.rs +++ b/rust/kernel/drm/kms/crtc.rs @@ -12,7 +12,7 @@ alloc::KBox, bindings, drm::device::Device, - error::to_result, + error::{from_result, to_result}, init::Zeroable, prelude::*, private::Sealed, @@ -21,7 +21,7 @@ use core::{ cell::{Cell, UnsafeCell}, marker::*, - mem, + mem::{self, ManuallyDrop}, ops::{Deref, DerefMut}, ptr::{addr_of_mut, null, null_mut, NonNull}, }; @@ -78,7 +78,11 @@ pub trait DriverCrtc: Send + Sync + Sized { helper_funcs: bindings::drm_crtc_helper_funcs { atomic_disable: None, atomic_enable: None, - atomic_check: None, + atomic_check: if Self::HAS_ATOMIC_CHECK { + Some(atomic_check_callback::<Self>) + } else { + None + }, dpms: None, commit: None, prepare: None, @@ -113,6 +117,21 @@ pub trait DriverCrtc: Send + Sync + Sized { /// /// Drivers may use this to instantiate their [`DriverCrtc`] object. fn new(device: &Device<Self::Driver>, args: &Self::Args) -> impl PinInit<Self, Error>; + + /// The optional [`drm_crtc_helper_funcs.atomic_check`] hook for this crtc. + /// + /// Drivers may use this to customize the atomic check phase of their [`Crtc`] objects. The + /// result of this function determines whether the atomic check passed or failed. + /// + /// [`drm_crtc_helper_funcs.atomic_check`]: srctree/include/drm/drm_modeset_helper_vtables.h + fn atomic_check( + _crtc: &Crtc<Self>, + _old_state: &CrtcState<Self::State>, + _new_state: CrtcStateMutator<'_, CrtcState<Self::State>>, + _state: &AtomicStateComposer<Self::Driver>, + ) -> Result { + build_error::build_error("This should not be reachable") + } } /// The generated C vtable for a [`DriverCrtc`]. @@ -859,3 +878,33 @@ fn vtable(&self) -> *const Self::Vtable { // SAFETY: DRM takes ownership of the state from here, and will never move it unsafe { bindings::__drm_atomic_helper_crtc_reset(crtc.as_raw(), KBox::into_raw(new).cast()) }; } + +unsafe extern "C" fn atomic_check_callback<T: DriverCrtc>( + crtc: *mut bindings::drm_crtc, + state: *mut bindings::drm_atomic_state, +) -> i32 { + // SAFETY: + // - We're guaranteed `crtc` is of type `Crtc<T>` via type invariants. + // - We're guaranteed by DRM that `crtc` is pointing to a valid initialized state. + let crtc = unsafe { Crtc::from_raw(crtc) }; + + // SAFETY: DRM guarantees `state` points to a valid `drm_atomic_state` + // We use a ManuallyDrop here to avoid AtomicStateComposer dropping an owned reference we never + // acquired. + let state = + unsafe { ManuallyDrop::new(AtomicStateComposer::new(NonNull::new_unchecked(state))) }; + + // SAFETY: Since we are in the atomic update callback, we're guaranteed by DRM that both the old + // and new atomic state are present within `state` + let (old_state, new_state) = unsafe { + ( + state.get_old_crtc_state(crtc).unwrap_unchecked(), + state.get_new_crtc_state(crtc).unwrap_unchecked(), + ) + }; + + from_result(|| { + T::atomic_check(crtc, old_state, new_state, &state)?; + Ok(0) + }) +} -- 2.48.1