diff mbox series

[WIP,RFC,v2,27/35] rust: drm/kms: Add drm_framebuffer bindings

Message ID 20240930233257.1189730-28-lyude@redhat.com (mailing list archive)
State New, archived
Headers show
Series Rust bindings for KMS + RVKMS | expand

Commit Message

Lyude Paul Sept. 30, 2024, 11:10 p.m. UTC
This adds some very simple bindings for drm_framebuffer. We don't use them
much yet, but we'll eventually be using them when rvkms eventually gets CRC
and writeback support. Just like Connector objects, these use RcModeObject.

Signed-off-by: Lyude Paul <lyude@redhat.com>
---
 rust/bindings/bindings_helper.h    |  1 +
 rust/kernel/drm/kms.rs             |  1 +
 rust/kernel/drm/kms/framebuffer.rs | 58 ++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+)
 create mode 100644 rust/kernel/drm/kms/framebuffer.rs
diff mbox series

Patch

diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 7695f11f4363c..7c324003c3885 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -15,6 +15,7 @@ 
 #include <drm/drm_fourcc.h>
 #include <drm/drm_fbdev_dma.h>
 #include <drm/drm_fbdev_shmem.h>
+#include <drm/drm_framebuffer.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_gem_framebuffer_helper.h>
 #include <drm/drm_gem_shmem_helper.h>
diff --git a/rust/kernel/drm/kms.rs b/rust/kernel/drm/kms.rs
index 4ab039d67352e..3edd90bc0025a 100644
--- a/rust/kernel/drm/kms.rs
+++ b/rust/kernel/drm/kms.rs
@@ -7,6 +7,7 @@ 
 pub mod crtc;
 pub mod encoder;
 pub mod fbdev;
+pub mod framebuffer;
 pub mod plane;
 
 use crate::{
diff --git a/rust/kernel/drm/kms/framebuffer.rs b/rust/kernel/drm/kms/framebuffer.rs
new file mode 100644
index 0000000000000..bbe408c187670
--- /dev/null
+++ b/rust/kernel/drm/kms/framebuffer.rs
@@ -0,0 +1,58 @@ 
+// SPDX-License-Identifier: GPL-2.0 OR MIT
+//! DRM Framebuffer related bindings
+
+use crate::{
+    drm::{
+        device::Device,
+        fourcc::*,
+    },
+    types::*,
+    prelude::*,
+};
+use bindings;
+use core::{
+    marker::*,
+    slice,
+};
+use super::{ModeObject, RcModeObject, KmsDriver};
+
+/// The main interface for [`struct drm_framebuffer`].
+///
+/// [`struct drm_framebuffer`]: srctree/include/drm/drm_framebuffer.h
+#[repr(transparent)]
+pub struct Framebuffer<T: KmsDriver> {
+    inner: Opaque<bindings::drm_framebuffer>,
+    _p: PhantomData<T>,
+}
+
+impl<T: KmsDriver> ModeObject for Framebuffer<T> {
+    type Driver = T;
+
+    fn drm_dev(&self) -> &Device<Self::Driver> {
+        unsafe { Device::borrow((*self.inner.get()).dev) }
+    }
+
+    fn raw_mode_obj(&self) -> *mut bindings::drm_mode_object {
+        // SAFETY: We don't expose Framebuffer<T> to users before it's initialized, so `base` is
+        // always initialized
+        unsafe { &mut (*self.inner.get()).base }
+    }
+}
+
+// SAFETY: Framebuffers are refcounted mode objects.
+unsafe impl<T: KmsDriver> RcModeObject for Framebuffer<T> {}
+
+// SAFETY: References to framebuffers are safe to be accessed from any thread
+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> {}
+
+impl<T: KmsDriver> crate::private::Sealed for Framebuffer<T> {}
+
+impl<T: KmsDriver> Framebuffer<T> {
+    #[inline]
+    pub(super) unsafe fn from_raw<'a>(ptr: *const bindings::drm_framebuffer) -> &'a Self {
+        // SAFETY: Our data layout is identical to drm_framebuffer
+        unsafe { &*ptr.cast() }
+    }
+}