From: Danilo Krummrich <dakr@redhat.com>
To: maarten.lankhorst@linux.intel.com, mripard@kernel.org,
tzimmermann@suse.de, airlied@gmail.com, daniel@ffwll.ch,
ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com,
boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com,
benno.lossin@proton.me, a.hindborg@samsung.com,
aliceryhl@google.com, lina@asahilina.net, pstanner@redhat.com,
ajanulgu@redhat.com, lyude@redhat.com,
gregkh@linuxfoundation.org, robh@kernel.org,
daniel.almeida@collabora.com
Cc: rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org,
nouveau@lists.freedesktop.org, Danilo Krummrich <dakr@redhat.com>
Subject: [PATCH v2 5/8] rust: drm: add DRM driver registration
Date: Wed, 19 Jun 2024 01:31:41 +0200 [thread overview]
Message-ID: <20240618233324.14217-6-dakr@redhat.com> (raw)
In-Reply-To: <20240618233324.14217-1-dakr@redhat.com>
Implement the DRM driver `Registration`.
The `Registration` structure is responsible to register and unregister a
DRM driver. It makes use of the `Devres` container in order to allow the
`Registration` to be owned by devres, such that it is automatically
dropped (and the DRM driver unregistered) once the parent device is
unbound.
Co-developed-by: Asahi Lina <lina@asahilina.net>
Signed-off-by: Asahi Lina <lina@asahilina.net>
Signed-off-by: Danilo Krummrich <dakr@redhat.com>
---
rust/kernel/drm/drv.rs | 57 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 56 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/drm/drv.rs b/rust/kernel/drm/drv.rs
index cd594a32f9e4..ebb79a8c90ee 100644
--- a/rust/kernel/drm/drv.rs
+++ b/rust/kernel/drm/drv.rs
@@ -4,7 +4,16 @@
//!
//! C header: [`include/linux/drm/drm_drv.h`](srctree/include/linux/drm/drm_drv.h)
-use crate::{bindings, drm, private::Sealed, str::CStr, types::ForeignOwnable};
+use crate::{
+ alloc::flags::*,
+ bindings,
+ devres::Devres,
+ drm,
+ error::{Error, Result},
+ private::Sealed,
+ str::CStr,
+ types::{ARef, ForeignOwnable},
+};
use macros::vtable;
/// Driver use the GEM memory manager. This should be set for all modern drivers.
@@ -139,3 +148,49 @@ pub trait Driver {
/// IOCTL list. See `kernel::drm::ioctl::declare_drm_ioctls!{}`.
const IOCTLS: &'static [drm::ioctl::DrmIoctlDescriptor];
}
+
+/// The registration type of a `drm::device::Device`.
+///
+/// Once the `Registration` structure is dropped, the device is unregistered.
+pub struct Registration<T: Driver>(ARef<drm::device::Device<T>>);
+
+impl<T: Driver> Registration<T> {
+ /// Creates a new [`Registration`] and registers it.
+ pub fn new(drm: ARef<drm::device::Device<T>>, flags: usize) -> Result<Self> {
+ // SAFETY: Safe by the invariants of `drm::device::Device`.
+ let ret = unsafe { bindings::drm_dev_register(drm.as_raw(), flags as u64) };
+ if ret < 0 {
+ return Err(Error::from_errno(ret));
+ }
+
+ Ok(Self(drm))
+ }
+
+ /// Same as [`Registration::new`}, but transfers ownership of the [`Registration`] to `Devres`.
+ pub fn new_foreign_owned(drm: ARef<drm::device::Device<T>>, flags: usize) -> Result {
+ let reg = Registration::<T>::new(drm.clone(), flags)?;
+
+ Devres::new_foreign_owned(drm.as_ref(), reg, GFP_KERNEL)
+ }
+
+ /// Returns a reference to the `Device` instance for this registration.
+ pub fn device(&self) -> &drm::device::Device<T> {
+ &self.0
+ }
+}
+
+// SAFETY: `Registration` doesn't offer any methods or access to fields when shared between
+// threads, hence it's safe to share it.
+unsafe impl<T: Driver> Sync for Registration<T> {}
+
+// SAFETY: Registration with and unregistration from the DRM subsystem can happen from any thread.
+unsafe impl<T: Driver> Send for Registration<T> {}
+
+impl<T: Driver> Drop for Registration<T> {
+ /// Removes the registration from the kernel if it has completed successfully before.
+ fn drop(&mut self) {
+ // SAFETY: Safe by the invariant of `ARef<drm::device::Device<T>>`. The existance of this
+ // `Registration` also guarantees the this `drm::device::Device` is actually registered.
+ unsafe { bindings::drm_dev_unregister(self.0.as_raw()) };
+ }
+}
--
2.45.1
next prev parent reply other threads:[~2024-06-18 23:33 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-18 23:31 [PATCH v2 0/8] DRM Rust abstractions and Nova Danilo Krummrich
2024-06-18 23:31 ` [PATCH v2 1/8] rust: drm: ioctl: Add DRM ioctl abstraction Danilo Krummrich
2024-09-02 16:15 ` Daniel Vetter
2024-09-03 11:17 ` Danilo Krummrich
2024-06-18 23:31 ` [PATCH v2 2/8] rust: Add a Sealed trait Danilo Krummrich
2024-06-18 23:31 ` [PATCH v2 3/8] rust: drm: add driver abstractions Danilo Krummrich
2024-09-02 16:29 ` Daniel Vetter
2024-09-03 11:04 ` Asahi Lina
2024-09-03 11:11 ` Danilo Krummrich
2024-09-03 12:39 ` Simona Vetter
2024-09-04 18:30 ` Lyude Paul
2024-06-18 23:31 ` [PATCH v2 4/8] rust: drm: add device abstraction Danilo Krummrich
2024-06-18 23:31 ` Danilo Krummrich [this message]
2024-07-02 17:26 ` [PATCH v2 5/8] rust: drm: add DRM driver registration Lyude Paul
2024-06-18 23:31 ` [PATCH v2 6/8] rust: drm: file: Add File abstraction Danilo Krummrich
2024-06-18 23:31 ` [PATCH v2 7/8] rust: drm: gem: Add GEM object abstraction Danilo Krummrich
2024-06-18 23:31 ` [PATCH v2 8/8] nova: add initial driver stub Danilo Krummrich
2024-06-18 23:31 ` [PATCH v2 10/10] " Danilo Krummrich
2024-06-18 23:42 ` Device / Driver and PCI Rust abstractions Danilo Krummrich
2024-09-02 16:40 ` [PATCH v2 0/8] DRM Rust abstractions and Nova Daniel Vetter
2024-09-03 7:32 ` Simona Vetter
-- strict thread matches above, loose matches on Subject: below --
2025-04-10 23:55 [PATCH v2 0/8] DRM Rust abstractions Danilo Krummrich
2025-04-10 23:55 ` [PATCH v2 5/8] rust: drm: add DRM driver registration Danilo Krummrich
2025-04-14 14:11 ` Alyssa Rosenzweig
2025-04-18 21:12 ` Lyude Paul
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=20240618233324.14217-6-dakr@redhat.com \
--to=dakr@redhat.com \
--cc=a.hindborg@samsung.com \
--cc=airlied@gmail.com \
--cc=ajanulgu@redhat.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=daniel.almeida@collabora.com \
--cc=daniel@ffwll.ch \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=lina@asahilina.net \
--cc=lyude@redhat.com \
--cc=maarten.lankhorst@linux.intel.com \
--cc=mripard@kernel.org \
--cc=nouveau@lists.freedesktop.org \
--cc=ojeda@kernel.org \
--cc=pstanner@redhat.com \
--cc=robh@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tzimmermann@suse.de \
--cc=wedsonaf@gmail.com \
/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