A mandatory trait method used for implementing DRM's atomic plane update callback. Signed-off-by: Lyude Paul <lyude@xxxxxxxxxx> --- rust/kernel/drm/kms/plane.rs | 45 +++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/rust/kernel/drm/kms/plane.rs b/rust/kernel/drm/kms/plane.rs index 4e73c2966ca22..b090aebc35649 100644 --- a/rust/kernel/drm/kms/plane.rs +++ b/rust/kernel/drm/kms/plane.rs @@ -70,7 +70,11 @@ pub trait DriverPlane: Send + Sync + Sized { begin_fb_access: None, end_fb_access: None, atomic_check: None, - atomic_update: None, + atomic_update: if Self::HAS_ATOMIC_UPDATE { + Some(atomic_update_callback::<Self>) + } else { + None + }, atomic_enable: None, atomic_disable: None, atomic_async_check: None, @@ -98,6 +102,21 @@ pub trait DriverPlane: Send + Sync + Sized { /// /// Drivers may use this to instantiate their [`DriverPlane`] object. fn new(device: &Device<Self::Driver>, args: Self::Args) -> impl PinInit<Self, Error>; + + /// The optional [`drm_plane_helper_funcs.atomic_update`] hook for this plane. + /// + /// Drivers may use this to customize the atomic update phase of their [`Plane`] objects. If not + /// specified, this function is a no-op. + /// + /// [`drm_plane_helper_funcs.atomic_update`]: srctree/include/drm/drm_modeset_helper_vtables.h + fn atomic_update( + _plane: &Plane<Self>, + _new_state: PlaneStateMutator<'_, PlaneState<Self::State>>, + _old_state: &PlaneState<Self::State>, + _state: &AtomicStateMutator<Self::Driver>, + ) { + build_error::build_error("This should not be reachable") + } } /// The generated C vtable for a [`DriverPlane`]. @@ -931,3 +950,27 @@ fn <D, P>(PlaneStateMutator<'a, OpaquePlaneState<D>>) -> Self // - The cast to `drm_plane_state` is safe via `PlaneState`s type invariants. unsafe { bindings::__drm_atomic_helper_plane_reset(plane, KBox::into_raw(new).cast()) }; } + +unsafe extern "C" fn atomic_update_callback<T: DriverPlane>( + plane: *mut bindings::drm_plane, + state: *mut bindings::drm_atomic_state, +) { + // SAFETY: + // - We're guaranteed `plane` is of type `Plane<T>` via type invariants. + // - We're guaranteed by DRM that `plane` is pointing to a valid initialized state. + let plane = unsafe { Plane::from_raw(plane) }; + + // SAFETY: DRM guarantees `state` points to a valid `drm_atomic_state` + let state = unsafe { AtomicStateMutator::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_plane_state(plane).unwrap_unchecked(), + state.get_new_plane_state(plane).unwrap_unchecked(), + ) + }; + + T::atomic_update(plane, new_state, old_state, &state); +} -- 2.48.1