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 12/18] rust: drm: framebuffer: add geometry accessors, refcounting and a byte-slice vmap
Date: Fri, 3 Jul 2026 04:00:59 +0100 [thread overview]
Message-ID: <20260703030123.2814-13-mike@fireburn.co.uk> (raw)
In-Reply-To: <20260703030123.2814-1-mike@fireburn.co.uk>
A driver that reads back a framebuffer's pixels (e.g. DisplayLink's evdi, whose
scanout is pulled by userspace via a GRABPIX ioctl rather than pushed to
hardware) needs three things the Framebuffer binding did not provide:
- width()/height()/pitch() geometry accessors;
- reference counting, so the framebuffer flipped in during an atomic commit can
be held (ARef<Framebuffer>) and mapped later from an ioctl -- add
AlwaysRefCounted backed by new drm_framebuffer_get()/put() helpers;
- a way to read the mapping without unsafe in the driver -- add
FramebufferVmap::as_bytes(), returning the plane-0 mapping as a &[u8] of
height * pitch(0) bytes, valid for the guard's lifetime.
Signed-off-by: Mike Lothian <mike@fireburn.co.uk>
Assisted-by: Claude:claude-opus-4-8 [Claude-Code]
---
rust/helpers/drm/drm.c | 1 +
rust/helpers/drm/framebuffer.c | 13 +++++++
rust/kernel/drm/kms/framebuffer.rs | 54 ++++++++++++++++++++++++++++++
3 files changed, 68 insertions(+)
create mode 100644 rust/helpers/drm/framebuffer.c
diff --git a/rust/helpers/drm/drm.c b/rust/helpers/drm/drm.c
index 5d700d75c0c9..24c531841ba1 100644
--- a/rust/helpers/drm/drm.c
+++ b/rust/helpers/drm/drm.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include "atomic.c"
+#include "framebuffer.c"
#include "gem.c"
#include "vma_manager.c"
#include "vblank.c"
diff --git a/rust/helpers/drm/framebuffer.c b/rust/helpers/drm/framebuffer.c
new file mode 100644
index 000000000000..280538ffb82c
--- /dev/null
+++ b/rust/helpers/drm/framebuffer.c
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <drm/drm_framebuffer.h>
+
+void rust_helper_drm_framebuffer_get(struct drm_framebuffer *fb)
+{
+ drm_framebuffer_get(fb);
+}
+
+void rust_helper_drm_framebuffer_put(struct drm_framebuffer *fb)
+{
+ drm_framebuffer_put(fb);
+}
diff --git a/rust/kernel/drm/kms/framebuffer.rs b/rust/kernel/drm/kms/framebuffer.rs
index 1ec6779ba7de..c7089b12d0d1 100644
--- a/rust/kernel/drm/kms/framebuffer.rs
+++ b/rust/kernel/drm/kms/framebuffer.rs
@@ -9,6 +9,7 @@
drm::device::Device,
error::{code::EINVAL, to_result},
prelude::*,
+ sync::aref::AlwaysRefCounted,
types::*,
};
use bindings;
@@ -46,6 +47,23 @@ fn raw_mode_obj(&self) -> *mut bindings::drm_mode_object {
}
// SAFETY: References to framebuffers are safe to be accessed from any thread
+// SAFETY: `struct drm_framebuffer` is refcounted via `drm_framebuffer_get()`/`drm_framebuffer_put()`,
+// which are the correct increment/decrement operations for it.
+unsafe impl<T: KmsDriver> AlwaysRefCounted for Framebuffer<T> {
+ #[inline]
+ fn inc_ref(&self) {
+ // SAFETY: The existence of a shared reference guarantees the refcount is non-zero.
+ unsafe { bindings::drm_framebuffer_get(self.as_raw()) };
+ }
+
+ #[inline]
+ unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
+ // SAFETY: The safety requirements guarantee the refcount is non-zero; `Framebuffer<T>` has
+ // the same layout as `struct drm_framebuffer`.
+ unsafe { bindings::drm_framebuffer_put(obj.as_ptr().cast()) };
+ }
+}
+
unsafe impl<T: KmsDriver> Send for Framebuffer<T> {}
// SAFETY: References to framebuffers are safe to be accessed from any thread
unsafe impl<T: KmsDriver> Sync for Framebuffer<T> {}
@@ -79,6 +97,30 @@ pub(crate) fn as_raw(&self) -> *mut bindings::drm_framebuffer {
self.0.get()
}
+ /// The framebuffer's width in pixels.
+ #[inline]
+ pub fn width(&self) -> u32 {
+ // SAFETY: `as_raw()` is a valid framebuffer; `width` is set at creation and immutable.
+ unsafe { (*self.as_raw()).width }
+ }
+
+ /// The framebuffer's height in pixels.
+ #[inline]
+ pub fn height(&self) -> u32 {
+ // SAFETY: `as_raw()` is a valid framebuffer; `height` is set at creation and immutable.
+ unsafe { (*self.as_raw()).height }
+ }
+
+ /// The byte stride (pitch) of colour plane `index`.
+ ///
+ /// A `drm_framebuffer` has at most four colour planes; `index` is masked into `0..=3`.
+ #[inline]
+ pub fn pitch(&self, index: usize) -> u32 {
+ // SAFETY: `pitches` has four elements; `index & 3` keeps the access in bounds. The pitches
+ // are set at creation and immutable.
+ unsafe { (*self.as_raw()).pitches[index & 3] }
+ }
+
/// Map this framebuffer's plane-0 backing pages into the kernel address space for CPU
/// access, for the duration of the returned guard.
///
@@ -123,6 +165,18 @@ pub fn as_ptr(&self) -> *const u8 {
// SAFETY: set non-null in `Framebuffer::vmap`; `vaddr` is the active union member.
(unsafe { self.map[0].__bindgen_anon_1.vaddr }) as *const u8
}
+
+ /// Plane 0's mapped pixels as a byte slice.
+ ///
+ /// The slice covers `height * pitch(0)` bytes — the full extent of colour plane 0 — and is
+ /// valid for the guard's lifetime. This lets callers read the scanout buffer without `unsafe`.
+ #[inline]
+ pub fn as_bytes(&self) -> &[u8] {
+ let len = self.fb.height() as usize * self.fb.pitch(0) as usize;
+ // SAFETY: `drm_gem_fb_vmap` mapped colour plane 0 contiguously at `as_ptr()` for at least
+ // `height * pitch(0)` bytes, and the mapping is live for `self`'s lifetime.
+ unsafe { core::slice::from_raw_parts(self.as_ptr(), len) }
+ }
}
impl<'a, T: KmsDriver> Drop for FramebufferVmap<'a, T> {
--
2.55.0
next prev parent reply other threads:[~2026-07-03 3:02 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 ` [RFC PATCH v2 07/18] rust: drm: kms: add safe accessors for common state and connector modes Mike Lothian
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 ` Mike Lothian [this message]
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-13-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