From: Mike Lothian <mike@fireburn.co.uk>
To: rust-for-linux@vger.kernel.org
Cc: dri-devel@lists.freedesktop.org,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Danilo Krummrich" <dakr@kernel.org>,
"Lyude Paul" <lyude@redhat.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
linux-kernel@vger.kernel.org,
"Mike Lothian" <mike@fireburn.co.uk>
Subject: [RFC PATCH v2 07/18] rust: drm: kms: add safe accessors for common state and connector modes
Date: Fri, 3 Jul 2026 04:00:54 +0100 [thread overview]
Message-ID: <20260703030123.2814-8-mike@fireburn.co.uk> (raw)
In-Reply-To: <20260703030123.2814-1-mike@fireburn.co.uk>
Add safe wrappers for the handful of raw drm_kms fields and helpers a
real KMS driver reaches for, so drivers need no raw escape hatch:
- drm::Device::hotplug_event() wraps drm_kms_helper_hotplug_event().
- RawCrtcState::mode() returns the state's programmed DisplayMode.
- DisplayMode gains hdisplay()/hsync_start()/hsync_end()/htotal()/
vdisplay()/vsync_start()/vsync_end()/vtotal()/clock()/vrefresh().
- RawPlaneState::crtc_w()/crtc_h() return the destination rectangle.
- ConnectorGuard::add_edid_modes() parses an EDID blob, updates the
connector and adds its modes (the safe counterpart to the existing
add_modes_noedid()).
Signed-off-by: Mike Lothian <mike@fireburn.co.uk>
Assisted-by: Claude:claude-sonnet-5 [Claude-Code]
---
rust/kernel/drm/kms.rs | 12 ++++++
rust/kernel/drm/kms/connector.rs | 20 +++++++++
rust/kernel/drm/kms/crtc.rs | 13 +++++-
rust/kernel/drm/kms/modes.rs | 70 ++++++++++++++++++++++++++++++++
rust/kernel/drm/kms/plane.rs | 10 +++++
5 files changed, 123 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/drm/kms.rs b/rust/kernel/drm/kms.rs
index 7c7e1d59c892..7207466094e7 100644
--- a/rust/kernel/drm/kms.rs
+++ b/rust/kernel/drm/kms.rs
@@ -114,6 +114,18 @@ fn deref(&self) -> &Self::Target {
}
}
+impl<T: KmsDriver, C: DeviceContext> Device<T, C> {
+ /// Send a hotplug uevent to userspace, prompting it to re-probe connector state.
+ ///
+ /// A thin safe wrapper over `drm_kms_helper_hotplug_event()`, for drivers that detect a
+ /// connection change out of band (for example a dock delivering an EDID after bring-up).
+ pub fn hotplug_event(&self) {
+ // SAFETY: `self.as_raw()` is our live `drm_device`; a KMS driver has the KMS helper
+ // state this call requires.
+ unsafe { bindings::drm_kms_helper_hotplug_event(self.as_raw()) };
+ }
+}
+
/// A trait which must be implemented by drivers that wish to support KMS
///
/// It should be implemented for the same type that implements [`Driver`]. Drivers which don't
diff --git a/rust/kernel/drm/kms/connector.rs b/rust/kernel/drm/kms/connector.rs
index 1c7c7b586340..591153de7ab4 100644
--- a/rust/kernel/drm/kms/connector.rs
+++ b/rust/kernel/drm/kms/connector.rs
@@ -574,6 +574,26 @@ pub fn set_preferred_mode(&self, (h_pref, w_pref): (u32, u32)) {
// SAFETY: We hold the locks required to call this via our type invariants.
unsafe { bindings::drm_set_preferred_mode(self.as_raw(), h_pref, w_pref) }
}
+
+ /// Parse a raw EDID blob, update the connector's display information from it, and add the
+ /// modes it advertises to the connector's mode list.
+ ///
+ /// Returns how many modes were added (0 if the blob is rejected).
+ pub fn add_edid_modes(&self, edid: &[u8]) -> i32 {
+ // SAFETY: `edid` is a valid byte buffer of `edid.len()` bytes; `drm_edid_alloc` copies it.
+ let drm_edid = unsafe { bindings::drm_edid_alloc(edid.as_ptr().cast(), edid.len()) };
+ if drm_edid.is_null() {
+ return 0;
+ }
+ // SAFETY: We hold the locks required via our type invariants; `drm_edid` was just
+ // allocated above.
+ unsafe { bindings::drm_edid_connector_update(self.as_raw(), drm_edid) };
+ // SAFETY: Same invariants; adds the EDID-derived modes and returns the count.
+ let n = unsafe { bindings::drm_edid_connector_add_modes(self.as_raw()) };
+ // SAFETY: `drm_edid` was allocated by `drm_edid_alloc` above and is no longer needed.
+ unsafe { bindings::drm_edid_free(drm_edid) };
+ n
+ }
}
/// A trait implemented by any type which can produce a reference to a
diff --git a/rust/kernel/drm/kms/crtc.rs b/rust/kernel/drm/kms/crtc.rs
index 9b43501bfb58..fbe113676e2f 100644
--- a/rust/kernel/drm/kms/crtc.rs
+++ b/rust/kernel/drm/kms/crtc.rs
@@ -5,8 +5,8 @@
//! C header: [`include/drm/drm_crtc.h`](srctree/include/drm/drm_crtc.h)
use super::{
- atomic::*, plane::*, vblank::*, KmsDriver, ModeObject, ModeObjectVtable, StaticModeObject,
- NewKmsDevice, Probing, Sealed,
+ atomic::*, modes::DisplayMode, plane::*, vblank::*, KmsDriver, ModeObject, ModeObjectVtable,
+ StaticModeObject, NewKmsDevice, Probing, Sealed,
};
use crate::{
alloc::KBox,
@@ -643,6 +643,7 @@ pub trait AsRawCrtcState: private::AsRawCrtcState {
pub(crate) mod private {
use super::*;
+ /// The raw-pointer half of [`AsRawCrtcState`](super::AsRawCrtcState).
#[allow(unreachable_pub)]
pub trait AsRawCrtcState {
/// Return a raw pointer to the DRM CRTC state
@@ -677,6 +678,14 @@ fn active(&self) -> bool {
// this access is serialized
unsafe { (*self.as_raw()).active }
}
+
+ /// Returns the display mode programmed into this CRTC state.
+ fn mode(&self) -> &DisplayMode {
+ // SAFETY: `mode` is embedded in `drm_crtc_state`, so it shares the state's lifetime, and
+ // is only mutated from the atomic check context - invariant beyond that point, so our
+ // interface can ensure this access is serialized.
+ unsafe { DisplayMode::as_ref(&(*self.as_raw()).mode) }
+ }
}
impl<T: AsRawCrtcState> RawCrtcState for T {}
diff --git a/rust/kernel/drm/kms/modes.rs b/rust/kernel/drm/kms/modes.rs
index 88ea27729212..16024855c32f 100644
--- a/rust/kernel/drm/kms/modes.rs
+++ b/rust/kernel/drm/kms/modes.rs
@@ -78,4 +78,74 @@ pub fn crtc_vtotal(&self) -> u16 {
// SAFETY: Reading these fields is safe via our type invariants
unsafe { (*self.as_raw()).crtc_vtotal }
}
+
+ /// Horizontal active pixels (`hdisplay`).
+ #[inline]
+ pub fn hdisplay(&self) -> u16 {
+ // SAFETY: Reading these fields is safe via our type invariants
+ unsafe { (*self.as_raw()).hdisplay }
+ }
+
+ /// Horizontal sync pulse start (`hsync_start`).
+ #[inline]
+ pub fn hsync_start(&self) -> u16 {
+ // SAFETY: Reading these fields is safe via our type invariants
+ unsafe { (*self.as_raw()).hsync_start }
+ }
+
+ /// Horizontal sync pulse end (`hsync_end`).
+ #[inline]
+ pub fn hsync_end(&self) -> u16 {
+ // SAFETY: Reading these fields is safe via our type invariants
+ unsafe { (*self.as_raw()).hsync_end }
+ }
+
+ /// Horizontal total pixels including blanking (`htotal`).
+ #[inline]
+ pub fn htotal(&self) -> u16 {
+ // SAFETY: Reading these fields is safe via our type invariants
+ unsafe { (*self.as_raw()).htotal }
+ }
+
+ /// Vertical active scanlines (`vdisplay`).
+ #[inline]
+ pub fn vdisplay(&self) -> u16 {
+ // SAFETY: Reading these fields is safe via our type invariants
+ unsafe { (*self.as_raw()).vdisplay }
+ }
+
+ /// Vertical sync pulse start (`vsync_start`).
+ #[inline]
+ pub fn vsync_start(&self) -> u16 {
+ // SAFETY: Reading these fields is safe via our type invariants
+ unsafe { (*self.as_raw()).vsync_start }
+ }
+
+ /// Vertical sync pulse end (`vsync_end`).
+ #[inline]
+ pub fn vsync_end(&self) -> u16 {
+ // SAFETY: Reading these fields is safe via our type invariants
+ unsafe { (*self.as_raw()).vsync_end }
+ }
+
+ /// Vertical total scanlines including blanking (`vtotal`).
+ #[inline]
+ pub fn vtotal(&self) -> u16 {
+ // SAFETY: Reading these fields is safe via our type invariants
+ unsafe { (*self.as_raw()).vtotal }
+ }
+
+ /// Pixel clock for this display mode in kHz (`clock`).
+ #[inline]
+ pub fn clock(&self) -> i32 {
+ // SAFETY: Reading these fields is safe via our type invariants
+ unsafe { (*self.as_raw()).clock }
+ }
+
+ /// The refresh rate of this mode in Hz, as computed by `drm_mode_vrefresh()`.
+ #[inline]
+ pub fn vrefresh(&self) -> i32 {
+ // SAFETY: `drm_mode_vrefresh` only reads the mode, which is valid via our invariants.
+ unsafe { bindings::drm_mode_vrefresh(self.as_raw()) }
+ }
}
diff --git a/rust/kernel/drm/kms/plane.rs b/rust/kernel/drm/kms/plane.rs
index 375f79bd3ceb..3863dfe1b84c 100644
--- a/rust/kernel/drm/kms/plane.rs
+++ b/rust/kernel/drm/kms/plane.rs
@@ -595,6 +595,16 @@ fn plane(&self) -> &Self::Plane {
unsafe { Self::Plane::from_raw(self.as_raw().plane) }
}
+ /// The width of this plane's destination rectangle within the CRTC, in pixels (`crtc_w`).
+ fn crtc_w(&self) -> u32 {
+ self.as_raw().crtc_w
+ }
+
+ /// The height of this plane's destination rectangle within the CRTC, in pixels (`crtc_h`).
+ fn crtc_h(&self) -> u32 {
+ self.as_raw().crtc_h
+ }
+
/// Return the current [`OpaqueCrtc`] assigned to this plane, if there is one.
fn crtc<'a, 'b: 'a, D>(&'a self) -> Option<&'b OpaqueCrtc<D>>
where
--
2.55.0
next prev parent reply other threads:[~2026-07-03 3:01 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-17 15:02 [RFC PATCH 0/5] rust: drm: minimal KMS bindings, EDID read, rotation, HDCP defs Mike Lothian
2026-06-17 15:02 ` [RFC PATCH 1/4] rust: drm: add minimal KMS bindings for simple-display-pipe drivers Mike Lothian
2026-06-17 15:02 ` [RFC PATCH 2/4] rust: drm: expose drm_edid.h for reading connector EDID Mike Lothian
2026-06-17 15:02 ` [RFC PATCH 3/4] rust: drm: expose drm::Device::as_raw() Mike Lothian
2026-06-17 15:02 ` [RFC PATCH 4/4] rust: drm: expose drm_blend.h and the atomic new-CRTC-state accessor Mike Lothian
2026-06-17 15:11 ` [RFC PATCH 0/5] rust: drm: minimal KMS bindings, EDID read, rotation, HDCP defs Miguel Ojeda
2026-06-17 15:29 ` Mike Lothian
2026-07-03 3:00 ` [RFC PATCH v2 00/18] rust: drm: safe KMS mode-object layer + evdi bindings Mike Lothian
2026-07-03 3:00 ` [RFC PATCH v2 01/18] rust: drm: kms: forward-port the safe mode-object layer onto the typestate device Mike Lothian
2026-07-07 21:46 ` lyude
2026-07-07 22:21 ` lyude
2026-07-03 3:00 ` [RFC PATCH v2 02/18] rust: drm: kms: adapt the port to current drm-next Mike Lothian
2026-07-03 3:00 ` [RFC PATCH v2 03/18] rust: drm: kms: break the Driver* trait well-formedness cycle Mike Lothian
2026-07-03 3:00 ` [RFC PATCH v2 04/18] rust: drm: kms: build the kernel crate clean under -Znext-solver Mike Lothian
2026-07-03 3:00 ` [RFC PATCH v2 05/18] rust: drm: expose <drm/display/drm_hdcp.h> HDCP 2.2 message definitions Mike Lothian
2026-07-03 3:00 ` [RFC PATCH v2 06/18] rust: drm: kms: add a Framebuffer::vmap() guard Mike Lothian
2026-07-07 21:51 ` lyude
2026-07-03 3:00 ` Mike Lothian [this message]
2026-07-03 3:00 ` [RFC PATCH v2 08/18] rust: drm: tyr: add the Kms associated type Mike Lothian
2026-07-03 3:00 ` [RFC PATCH v2 09/18] rust: drm: add drm_event delivery Mike Lothian
2026-07-03 3:00 ` [RFC PATCH v2 10/18] rust: drm: allow drivers to declare ioctls from their own uAPI module Mike Lothian
2026-07-03 3:00 ` [RFC PATCH v2 11/18] rust: platform: add runtime platform device creation Mike Lothian
2026-07-03 3:00 ` [RFC PATCH v2 12/18] rust: drm: framebuffer: add geometry accessors, refcounting and a byte-slice vmap Mike Lothian
2026-07-03 3:01 ` [RFC PATCH v2 13/18] rust: i2c: add adapter-provider (bus controller) registration Mike Lothian
2026-07-03 3:01 ` [RFC PATCH v2 14/18] rust: add sysfs device attribute groups Mike Lothian
2026-07-03 3:01 ` [RFC PATCH v2 15/18] rust: drm: support hardware cursor planes with sleepable event delivery Mike Lothian
2026-07-03 3:01 ` [RFC PATCH v2 16/18] rust: drm: add CRTC gamma LUT and plane rotation property bindings Mike Lothian
2026-07-03 3:01 ` [RFC PATCH v2 17/18] rust: drm: kms: add connector detect() and mode_valid() hooks Mike Lothian
2026-07-03 3:01 ` [RFC PATCH v2 18/18] rust: drm: kms: add plane damage-clip accessors Mike Lothian
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260703030123.2814-8-mike@fireburn.co.uk \
--to=mike@fireburn.co.uk \
--cc=a.hindborg@kernel.org \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox