All of lore.kernel.org
 help / color / mirror / Atom feed
From: Matteo Kloiber <kernel@matt3o12.de>
To: Danilo Krummrich <dakr@kernel.org>,
	Alexandre Courbot <acourbot@nvidia.com>,
	Alice Ryhl <aliceryhl@google.com>,
	Abdiel Janulgue <abdiel.janulgue@gmail.com>,
	Robin Murphy <robin.murphy@arm.com>,
	Andreas Hindborg <a.hindborg@kernel.org>,
	Gary Guo <gary@garyguo.net>
Cc: "Daniel Almeida" <daniel.almeida@collabora.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	rust-for-linux@vger.kernel.org, driver-core@lists.linux.dev,
	dri-devel@lists.freedesktop.org, nova-gpu@lists.linux.dev,
	linux-kernel@vger.kernel.org,
	"Matteo Kloiber" <kernel@matt3o12.de>
Subject: [RFC PATCH v2 06/10] rust: dma: require a Handle for coherent allocations
Date: Sun, 29 Nov 2026 23:24:25 +0100	[thread overview]
Message-ID: <20261129222429.2380652-7-kernel@matt3o12.de> (raw)
In-Reply-To: <20261129222429.2380652-1-kernel@matt3o12.de>

Coherent, CoherentBox and CoherentHandle allocate for a plain bound
device, so nothing ties an allocation to a completed DMA configuration.

Make their constructors take a dma::Handle and store it in place of the
device reference. A handle only exists after Setup::configure(), so
coherent memory can no longer be allocated before the DMA parameters
are set.

Convert nova-core and the DMA sample. Both already hold a handle, so
they pass it directly instead of dma.device().

Signed-off-by: Matteo Kloiber <kernel@matt3o12.de>
---
 drivers/gpu/nova-core/falcon.rs               |   2 +-
 drivers/gpu/nova-core/fb.rs                   |   2 +-
 .../nova-core/firmware/fwsec/bootloader.rs    |   2 +-
 drivers/gpu/nova-core/firmware/gsp.rs         |   3 +-
 drivers/gpu/nova-core/firmware/gsp_fmc.rs     |   2 +-
 drivers/gpu/nova-core/firmware/radix3.rs      |   2 +-
 drivers/gpu/nova-core/firmware/riscv.rs       |   2 +-
 drivers/gpu/nova-core/fsp.rs                  |   2 +-
 drivers/gpu/nova-core/gsp.rs                  |   4 +-
 drivers/gpu/nova-core/gsp/cmdq.rs             |   2 +-
 drivers/gpu/nova-core/gsp/hal/gh100.rs        |   2 +-
 drivers/gpu/nova-core/gsp/hal/tu102.rs        |   2 +-
 drivers/gpu/nova-core/gsp/logbuffer.rs        |   2 +-
 rust/kernel/dma.rs                            | 161 ++++++++----------
 samples/rust/rust_dma.rs                      |   2 +-
 15 files changed, 85 insertions(+), 107 deletions(-)

diff --git a/drivers/gpu/nova-core/falcon.rs b/drivers/gpu/nova-core/falcon.rs
index c9ffda0208b0..9de2b5880630 100644
--- a/drivers/gpu/nova-core/falcon.rs
+++ b/drivers/gpu/nova-core/falcon.rs
@@ -649,7 +649,7 @@ fn dma_load<F: FalconFirmware<Target = E> + FalconDmaLoadable>(&self, fw: &F) ->
             // DMA copies are done in chunks of `MEM_BLOCK_ALIGNMENT`, so pad the length
             // accordingly and fill with `0`.
             let mut dma_obj = CoherentBox::zeroed_slice(
-                self.dma.device(),
+                self.dma,
                 fw_slice.len().next_multiple_of(MEM_BLOCK_ALIGNMENT),
                 GFP_KERNEL,
             )?;
diff --git a/drivers/gpu/nova-core/fb.rs b/drivers/gpu/nova-core/fb.rs
index a2505da09a79..164e14a8b6fc 100644
--- a/drivers/gpu/nova-core/fb.rs
+++ b/drivers/gpu/nova-core/fb.rs
@@ -62,7 +62,7 @@ pub(crate) fn register(
         bar: Bar0<'sys>,
         chipset: Chipset,
     ) -> Result<Self> {
-        let page = CoherentHandle::alloc(dma.device(), kernel::page::PAGE_SIZE, GFP_KERNEL)?;
+        let page = CoherentHandle::alloc(dma, kernel::page::PAGE_SIZE, GFP_KERNEL)?;
 
         hal::fb_hal(chipset).write_sysmem_flush_page(bar, page.dma_address())?;
 
diff --git a/drivers/gpu/nova-core/firmware/fwsec/bootloader.rs b/drivers/gpu/nova-core/firmware/fwsec/bootloader.rs
index 49651a404286..d504c33d789f 100644
--- a/drivers/gpu/nova-core/firmware/fwsec/bootloader.rs
+++ b/drivers/gpu/nova-core/firmware/fwsec/bootloader.rs
@@ -80,7 +80,7 @@ pub(crate) fn new(
 
             (
                 align_padding,
-                Coherent::from_slice(dma.device(), firmware_obj.as_slice(), GFP_KERNEL)?,
+                Coherent::from_slice(dma, firmware_obj.as_slice(), GFP_KERNEL)?,
             )
         };
 
diff --git a/drivers/gpu/nova-core/firmware/gsp.rs b/drivers/gpu/nova-core/firmware/gsp.rs
index e5fce91337a8..aafa6c583421 100644
--- a/drivers/gpu/nova-core/firmware/gsp.rs
+++ b/drivers/gpu/nova-core/firmware/gsp.rs
@@ -54,8 +54,7 @@ pub(crate) fn new(dma: dma::Handle<'a>, chipset: Chipset) -> impl PinInit<Self,
             let filename = CString::try_from_fmt(fmt!("nvidia/{chip_name}/gsp/{file}"))?;
             firmware::request_into_buf(&filename, dev, fw_vvec.as_mut_slice())?;
 
-            let signatures =
-                Coherent::from_slice(dma.device(), tlv.get_bytes(b"SIGN")?, GFP_KERNEL)?;
+            let signatures = Coherent::from_slice(dma, tlv.get_bytes(b"SIGN")?, GFP_KERNEL)?;
 
             Ok(try_pin_init!(Self {
                 radix3 <- Radix3::new(dma, fw_vvec),
diff --git a/drivers/gpu/nova-core/firmware/gsp_fmc.rs b/drivers/gpu/nova-core/firmware/gsp_fmc.rs
index 230d631beaf4..6c52eb2dbed9 100644
--- a/drivers/gpu/nova-core/firmware/gsp_fmc.rs
+++ b/drivers/gpu/nova-core/firmware/gsp_fmc.rs
@@ -62,7 +62,7 @@ pub(crate) fn new(dma: dma::Handle<'a>, chipset: Chipset) -> Result<Self> {
         );
 
         let fmc_image_data = tlv.get_bytes(b"BLOB")?;
-        let fmc_image = Coherent::from_slice(dma.device(), fmc_image_data, GFP_KERNEL)?;
+        let fmc_image = Coherent::from_slice(dma, fmc_image_data, GFP_KERNEL)?;
 
         Ok(Self {
             fmc_image,
diff --git a/drivers/gpu/nova-core/firmware/radix3.rs b/drivers/gpu/nova-core/firmware/radix3.rs
index 5e10263e8313..a00a583f7351 100644
--- a/drivers/gpu/nova-core/firmware/radix3.rs
+++ b/drivers/gpu/nova-core/firmware/radix3.rs
@@ -77,7 +77,7 @@ pub(crate) fn new(dma: dma::Handle<'a>, data: VVec<u8>) -> impl PinInit<Self, Er
                     // Create level 0 page table data and fill its first entry with the level 1
                     // table.
                     let mut level0 = CoherentBox::<'_, [u64]>::zeroed_slice(
-                        dma.device(),
+                        dma,
                         GSP_PAGE_SIZE / size_of::<u64>(),
                         GFP_KERNEL
                     )?;
diff --git a/drivers/gpu/nova-core/firmware/riscv.rs b/drivers/gpu/nova-core/firmware/riscv.rs
index ce0da2efe34e..f78af9bba740 100644
--- a/drivers/gpu/nova-core/firmware/riscv.rs
+++ b/drivers/gpu/nova-core/firmware/riscv.rs
@@ -44,7 +44,7 @@ pub(crate) fn new(dma: dma::Handle<'a>, fw: &Firmware) -> Result<Self> {
         let manifest_offset = tlv.get_u32(b"MFOF")?;
         let app_version = tlv.get_u32(b"APPV")?;
 
-        let ucode = Coherent::from_slice(dma.device(), tlv.get_bytes(b"BLOB")?, GFP_KERNEL)?;
+        let ucode = Coherent::from_slice(dma, tlv.get_bytes(b"BLOB")?, GFP_KERNEL)?;
 
         Ok(Self {
             ucode,
diff --git a/drivers/gpu/nova-core/fsp.rs b/drivers/gpu/nova-core/fsp.rs
index 17da2efbf0e0..9675d3941ed5 100644
--- a/drivers/gpu/nova-core/fsp.rs
+++ b/drivers/gpu/nova-core/fsp.rs
@@ -372,7 +372,7 @@ pub(crate) fn new(
 
         Ok(Self {
             chipset,
-            fmc_boot_params: Coherent::init(dma.device(), GFP_KERNEL, init)?,
+            fmc_boot_params: Coherent::init(dma, GFP_KERNEL, init)?,
             resume,
             _wpr_meta: wpr_meta,
             _libos: libos,
diff --git a/drivers/gpu/nova-core/gsp.rs b/drivers/gpu/nova-core/gsp.rs
index 908ecfbcd2aa..e218a1628646 100644
--- a/drivers/gpu/nova-core/gsp.rs
+++ b/drivers/gpu/nova-core/gsp.rs
@@ -126,10 +126,10 @@ pub(crate) fn new(dma: dma::Handle<'gsp>, bar: Bar0<'gsp>) -> impl PinInit<Self,
             // kgspSetupLibosInitArgs_IMPL (creates pLibosInitArgs[] array)
             Ok(try_pin_init!(Self {
                 cmdq <- Cmdq::new(dma, bar),
-                rmargs: Coherent::init(dma.device(), GFP_KERNEL, GspArgumentsPadded::new(&cmdq))?,
+                rmargs: Coherent::init(dma, GFP_KERNEL, GspArgumentsPadded::new(&cmdq))?,
                 libos: {
                     let mut libos = CoherentBox::zeroed_slice(
-                        dma.device(),
+                        dma,
                         GSP_PAGE_SIZE / size_of::<LibosMemoryRegionInitArgument>(),
                         GFP_KERNEL,
                     )?;
diff --git a/drivers/gpu/nova-core/gsp/cmdq.rs b/drivers/gpu/nova-core/gsp/cmdq.rs
index d34174998f3d..e442d956ce19 100644
--- a/drivers/gpu/nova-core/gsp/cmdq.rs
+++ b/drivers/gpu/nova-core/gsp/cmdq.rs
@@ -180,7 +180,7 @@ fn new(dma: dma::Handle<'a>, bar: Bar0<'a>) -> Result<Self> {
         const MSGQ_SIZE: u32 = cv!(size_of::<Msgq>());
         const RX_HDR_OFF: u32 = cv!(mem::offset_of!(Msgq, rx));
 
-        let mut gsp_mem = CoherentBox::<'_, GspMem>::zeroed(dma.device(), GFP_KERNEL)?;
+        let mut gsp_mem = CoherentBox::<'_, GspMem>::zeroed(dma, GFP_KERNEL)?;
         gsp_mem.cpuq.tx = MsgqTxHeader::new(MSGQ_SIZE, RX_HDR_OFF, MSGQ_NUM_PAGES);
         gsp_mem.cpuq.rx = MsgqRxHeader::new();
 
diff --git a/drivers/gpu/nova-core/gsp/hal/gh100.rs b/drivers/gpu/nova-core/gsp/hal/gh100.rs
index f194df6656aa..aad4a6b222aa 100644
--- a/drivers/gpu/nova-core/gsp/hal/gh100.rs
+++ b/drivers/gpu/nova-core/gsp/hal/gh100.rs
@@ -155,7 +155,7 @@ fn boot<'gpu>(
         dev_dbg!(dev, "{:#x?}\n", fb_sizes);
 
         let wpr_meta = Coherent::init(
-            ctx.dma.device(),
+            ctx.dma,
             GFP_KERNEL,
             GspFwWprMeta::from_sizes(gsp_fw, &fb_sizes),
         )?;
diff --git a/drivers/gpu/nova-core/gsp/hal/tu102.rs b/drivers/gpu/nova-core/gsp/hal/tu102.rs
index 44a758335118..ca92b0d66e51 100644
--- a/drivers/gpu/nova-core/gsp/hal/tu102.rs
+++ b/drivers/gpu/nova-core/gsp/hal/tu102.rs
@@ -272,7 +272,7 @@ fn boot<'gpu>(
         // Declared before the unload guard so that if Booter fails while running, SEC2 is reset
         // by the guard before this allocation is freed.
         let wpr_meta = Coherent::init(
-            dma.device(),
+            dma,
             GFP_KERNEL,
             GspFwWprMeta::from_ranges(gsp_fw, &fb_ranges),
         )?;
diff --git a/drivers/gpu/nova-core/gsp/logbuffer.rs b/drivers/gpu/nova-core/gsp/logbuffer.rs
index 57d063c15502..2b3494470a83 100644
--- a/drivers/gpu/nova-core/gsp/logbuffer.rs
+++ b/drivers/gpu/nova-core/gsp/logbuffer.rs
@@ -52,7 +52,7 @@
 impl<'a> LogBuffer<'a> {
     /// Creates a new `LogBuffer` mapped on the device.
     fn new(dma: dma::Handle<'a>) -> Result<Self> {
-        let obj = Self(Coherent::zeroed(dma.device(), GFP_KERNEL)?);
+        let obj = Self(Coherent::zeroed(dma, GFP_KERNEL)?);
 
         let start_addr = obj.0.dma_address();
 
diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
index 9c3cbce77ce9..456ae9f90602 100644
--- a/rust/kernel/dma.rs
+++ b/rust/kernel/dma.rs
@@ -69,7 +69,7 @@
 ///     max_seg_size: Some(NonZeroU32::MAX),
 /// })?;
 ///
-/// let _buf = Coherent::<u64>::zeroed(dma.device(), GFP_KERNEL)?;
+/// let _buf = Coherent::<u64>::zeroed(dma, GFP_KERNEL)?;
 /// # Ok::<(), Error>(()) }
 /// ```
 pub struct Params {
@@ -158,6 +158,12 @@ impl<'bound> Handle<'bound> {
     pub fn device(self) -> &'bound device::Device<Bound> {
         self.dev
     }
+
+    /// Returns the raw `struct device` pointer of the handle's device.
+    #[inline]
+    pub(crate) fn as_raw(self) -> *mut bindings::device {
+        self.dev.as_raw()
+    }
 }
 
 /// Trait to be implemented by DMA capable bus devices.
@@ -329,13 +335,12 @@ pub const fn value(&self) -> u64 {
 /// # Examples
 ///
 /// ```
-/// # use kernel::device::{Bound, Device};
 /// use kernel::dma::{attrs::*, Coherent};
 ///
-/// # fn test(dev: &Device<Bound>) -> Result {
+/// # fn test(dma: kernel::dma::Handle<'_>) -> Result {
 /// let attribs = DMA_ATTR_FORCE_CONTIGUOUS | DMA_ATTR_NO_WARN;
 /// let c: Coherent<'_, [u64]> =
-///     Coherent::zeroed_slice_with_attrs(dev, 4, GFP_KERNEL, attribs)?;
+///     Coherent::zeroed_slice_with_attrs(dma, 4, GFP_KERNEL, attribs)?;
 /// # Ok::<(), Error>(()) }
 /// ```
 #[derive(Clone, Copy, PartialEq)]
@@ -492,17 +497,13 @@ fn from(direction: DataDirection) -> Self {
 /// `CoherentBox<T>`:
 ///
 /// ```
-/// # use kernel::device::{
-/// #     Bound,
-/// #     Device,
-/// # };
 /// use kernel::dma::{attrs::*,
 ///     Coherent,
 ///     CoherentBox,
 /// };
 ///
-/// # fn test(dev: &Device<Bound>) -> Result {
-/// let mut dmem: CoherentBox<'_, u64> = CoherentBox::zeroed(dev, GFP_KERNEL)?;
+/// # fn test(dma: kernel::dma::Handle<'_>) -> Result {
+/// let mut dmem: CoherentBox<'_, u64> = CoherentBox::zeroed(dma, GFP_KERNEL)?;
 /// *dmem = 42;
 /// let dmem: Coherent<'_, u64> = dmem.into();
 /// # Ok::<(), Error>(()) }
@@ -512,17 +513,13 @@ fn from(direction: DataDirection) -> Self {
 ///
 ///
 /// ```
-/// # use kernel::device::{
-/// #     Bound,
-/// #     Device,
-/// # };
 /// use kernel::dma::{attrs::*,
 ///     Coherent,
 ///     CoherentBox,
 /// };
 ///
-/// # fn test(dev: &Device<Bound>) -> Result {
-/// let mut dmem: CoherentBox<'_, [u64]> = CoherentBox::zeroed_slice(dev, 4, GFP_KERNEL)?;
+/// # fn test(dma: kernel::dma::Handle<'_>) -> Result {
+/// let mut dmem: CoherentBox<'_, [u64]> = CoherentBox::zeroed_slice(dma, 4, GFP_KERNEL)?;
 /// dmem.fill(42);
 /// let dmem: Coherent<'_, [u64]> = dmem.into();
 /// # Ok::<(), Error>(()) }
@@ -533,22 +530,22 @@ impl<'a, T: AsBytes + FromBytes> CoherentBox<'a, [T]> {
     /// [`CoherentBox`] variant of [`Coherent::zeroed_slice_with_attrs`].
     #[inline]
     pub fn zeroed_slice_with_attrs(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         count: usize,
         gfp_flags: kernel::alloc::Flags,
         dma_attrs: Attrs,
     ) -> Result<Self> {
-        Coherent::zeroed_slice_with_attrs(dev, count, gfp_flags, dma_attrs).map(Self)
+        Coherent::zeroed_slice_with_attrs(dma, count, gfp_flags, dma_attrs).map(Self)
     }
 
     /// Same as [CoherentBox::zeroed_slice_with_attrs], but with `dma::Attrs(0)`.
     #[inline]
     pub fn zeroed_slice(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         count: usize,
         gfp_flags: kernel::alloc::Flags,
     ) -> Result<Self> {
-        Self::zeroed_slice_with_attrs(dev, count, gfp_flags, Attrs(0))
+        Self::zeroed_slice_with_attrs(dma, count, gfp_flags, Attrs(0))
     }
 
     /// Initializes the element at `i` using the given initializer.
@@ -584,22 +581,21 @@ pub fn init_at<E>(&mut self, i: usize, init: impl Init<T, E>) -> Result
     /// ```
     /// use core::ops::Deref;
     ///
-    /// # use kernel::device::{Bound, Device};
     /// use kernel::dma::{
     ///     attrs::*,
     ///     CoherentBox
     /// };
     ///
-    /// # fn test(dev: &Device<Bound>) -> Result {
+    /// # fn test(dma: kernel::dma::Handle<'_>) -> Result {
     /// let data = [0u8, 1u8, 2u8, 3u8];
     /// let c: CoherentBox<'_, [u8]> =
-    ///     CoherentBox::from_slice_with_attrs(dev, &data, GFP_KERNEL, DMA_ATTR_NO_WARN)?;
+    ///     CoherentBox::from_slice_with_attrs(dma, &data, GFP_KERNEL, DMA_ATTR_NO_WARN)?;
     ///
     /// assert_eq!(c.deref(), &data);
     /// # Ok::<(), Error>(()) }
     /// ```
     pub fn from_slice_with_attrs(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         data: &[T],
         gfp_flags: kernel::alloc::Flags,
         dma_attrs: Attrs,
@@ -608,7 +604,7 @@ pub fn from_slice_with_attrs(
         T: Copy,
     {
         let mut slice = Self(Coherent::<T>::alloc_slice_with_attrs(
-            dev,
+            dma,
             data.len(),
             gfp_flags,
             dma_attrs,
@@ -623,15 +619,11 @@ pub fn from_slice_with_attrs(
     /// Performs the same functionality as [`CoherentBox::from_slice_with_attrs`], except the
     /// `dma_attrs` is 0 by default.
     #[inline]
-    pub fn from_slice(
-        dev: &'a device::Device<Bound>,
-        data: &[T],
-        gfp_flags: kernel::alloc::Flags,
-    ) -> Result<Self>
+    pub fn from_slice(dma: Handle<'a>, data: &[T], gfp_flags: kernel::alloc::Flags) -> Result<Self>
     where
         T: Copy,
     {
-        Self::from_slice_with_attrs(dev, data, gfp_flags, Attrs(0))
+        Self::from_slice_with_attrs(dma, data, gfp_flags, Attrs(0))
     }
 }
 
@@ -639,17 +631,17 @@ impl<'a, T: AsBytes + FromBytes> CoherentBox<'a, T> {
     /// Same as [`CoherentBox::zeroed_slice_with_attrs`], but for a single element.
     #[inline]
     pub fn zeroed_with_attrs(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         gfp_flags: kernel::alloc::Flags,
         dma_attrs: Attrs,
     ) -> Result<Self> {
-        Coherent::zeroed_with_attrs(dev, gfp_flags, dma_attrs).map(Self)
+        Coherent::zeroed_with_attrs(dma, gfp_flags, dma_attrs).map(Self)
     }
 
     /// Same as [`CoherentBox::zeroed_slice`], but for a single element.
     #[inline]
-    pub fn zeroed(dev: &'a device::Device<Bound>, gfp_flags: kernel::alloc::Flags) -> Result<Self> {
-        Self::zeroed_with_attrs(dev, gfp_flags, Attrs(0))
+    pub fn zeroed(dma: Handle<'a>, gfp_flags: kernel::alloc::Flags) -> Result<Self> {
+        Self::zeroed_with_attrs(dma, gfp_flags, Attrs(0))
     }
 }
 
@@ -707,7 +699,7 @@ fn from(value: CoherentBox<'a, T>) -> Self {
 // constructors `unsafe` to prevent this is considered too restrictive for the common case; this
 // soundness hole is accepted for now.
 pub struct Coherent<'a, T: KnownSize + ?Sized> {
-    dev: &'a device::Device<Bound>,
+    dma: Handle<'a>,
     dma_addr: DmaAddress,
     cpu_addr: NonNull<T>,
     dma_attrs: Attrs,
@@ -772,7 +764,7 @@ pub unsafe fn as_mut(&self) -> &mut T {
 impl<'a, T: AsBytes + FromBytes> Coherent<'a, T> {
     /// Allocates a region of `T` of coherent memory.
     fn alloc_with_attrs(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         gfp_flags: kernel::alloc::Flags,
         dma_attrs: Attrs,
     ) -> Result<Self> {
@@ -784,10 +776,10 @@ fn alloc_with_attrs(
         }
 
         let mut dma_addr = 0;
-        // SAFETY: Device pointer is guaranteed as valid by the type invariant on `Device`.
+        // SAFETY: Device pointer is guaranteed as valid by the type invariant on `device::Device`.
         let addr = unsafe {
             bindings::dma_alloc_attrs(
-                dev.as_raw(),
+                dma.as_raw(),
                 core::mem::size_of::<T>(),
                 &mut dma_addr,
                 gfp_flags.as_raw(),
@@ -798,9 +790,9 @@ fn alloc_with_attrs(
         // INVARIANT:
         // - We just successfully allocated a coherent region which is adequately sized for `T`,
         //   hence the cpu address is valid.
-        // - `dev` is a valid reference to a bound device that outlives this allocation.
+        // - `dma` is a configured DMA handle for a device that outlives this allocation.
         Ok(Self {
-            dev,
+            dma,
             dma_addr,
             cpu_addr,
             dma_attrs,
@@ -812,40 +804,36 @@ fn alloc_with_attrs(
     /// # Examples
     ///
     /// ```
-    /// # use kernel::device::{
-    /// #     Bound,
-    /// #     Device,
-    /// # };
     /// use kernel::dma::{
     ///     attrs::*,
     ///     Coherent,
     /// };
     ///
-    /// # fn test(dev: &Device<Bound>) -> Result {
+    /// # fn test(dma: kernel::dma::Handle<'_>) -> Result {
     /// let c: Coherent<'_, [u64; 4]> =
-    ///     Coherent::zeroed_with_attrs(dev, GFP_KERNEL, DMA_ATTR_NO_WARN)?;
+    ///     Coherent::zeroed_with_attrs(dma, GFP_KERNEL, DMA_ATTR_NO_WARN)?;
     /// # Ok::<(), Error>(()) }
     /// ```
     #[inline]
     pub fn zeroed_with_attrs(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         gfp_flags: kernel::alloc::Flags,
         dma_attrs: Attrs,
     ) -> Result<Self> {
-        Self::alloc_with_attrs(dev, gfp_flags | __GFP_ZERO, dma_attrs)
+        Self::alloc_with_attrs(dma, gfp_flags | __GFP_ZERO, dma_attrs)
     }
 
     /// Performs the same functionality as [`Coherent::zeroed_with_attrs`], except the
     /// `dma_attrs` is 0 by default.
     #[inline]
-    pub fn zeroed(dev: &'a device::Device<Bound>, gfp_flags: kernel::alloc::Flags) -> Result<Self> {
-        Self::zeroed_with_attrs(dev, gfp_flags, Attrs(0))
+    pub fn zeroed(dma: Handle<'a>, gfp_flags: kernel::alloc::Flags) -> Result<Self> {
+        Self::zeroed_with_attrs(dma, gfp_flags, Attrs(0))
     }
 
     /// Same as [`Coherent::zeroed_with_attrs`], but instead of a zero-initialization the memory is
     /// initialized with `init`.
     pub fn init_with_attrs<E>(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         gfp_flags: kernel::alloc::Flags,
         dma_attrs: Attrs,
         init: impl Init<T, E>,
@@ -853,7 +841,7 @@ pub fn init_with_attrs<E>(
     where
         Error: From<E>,
     {
-        let dmem = Self::alloc_with_attrs(dev, gfp_flags, dma_attrs)?;
+        let dmem = Self::alloc_with_attrs(dma, gfp_flags, dma_attrs)?;
         let ptr = dmem.as_mut_ptr();
 
         // SAFETY:
@@ -870,19 +858,19 @@ pub fn init_with_attrs<E>(
     /// with `init`.
     #[inline]
     pub fn init<E>(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         gfp_flags: kernel::alloc::Flags,
         init: impl Init<T, E>,
     ) -> Result<Self>
     where
         Error: From<E>,
     {
-        Self::init_with_attrs(dev, gfp_flags, Attrs(0), init)
+        Self::init_with_attrs(dma, gfp_flags, Attrs(0), init)
     }
 
     /// Allocates a region of `[T; len]` of coherent memory.
     fn alloc_slice_with_attrs(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         len: usize,
         gfp_flags: kernel::alloc::Flags,
         dma_attrs: Attrs,
@@ -901,10 +889,10 @@ fn alloc_slice_with_attrs(
 
         let size = core::mem::size_of::<T>().checked_mul(len).ok_or(ENOMEM)?;
         let mut dma_addr = 0;
-        // SAFETY: Device pointer is guaranteed as valid by the type invariant on `Device`.
+        // SAFETY: Device pointer is guaranteed as valid by the type invariant on `device::Device`.
         let addr = unsafe {
             bindings::dma_alloc_attrs(
-                dev.as_raw(),
+                dma.as_raw(),
                 size,
                 &mut dma_addr,
                 gfp_flags.as_raw(),
@@ -915,9 +903,9 @@ fn alloc_slice_with_attrs(
         // INVARIANT:
         // - We just successfully allocated a coherent region which is adequately sized for
         //   `[T; len]`, hence the cpu address is valid.
-        // - `dev` is a valid reference to a bound device that outlives this allocation.
+        // - `dma` is a configured DMA handle for a device that outlives this allocation.
         Ok(Coherent {
-            dev,
+            dma,
             dma_addr,
             cpu_addr,
             dma_attrs,
@@ -932,39 +920,35 @@ fn alloc_slice_with_attrs(
     /// # Examples
     ///
     /// ```
-    /// # use kernel::device::{
-    /// #     Bound,
-    /// #     Device,
-    /// # };
     /// use kernel::dma::{
     ///     attrs::*,
     ///     Coherent,
     /// };
     ///
-    /// # fn test(dev: &Device<Bound>) -> Result {
+    /// # fn test(dma: kernel::dma::Handle<'_>) -> Result {
     /// let c: Coherent<'_, [u64]> =
-    ///     Coherent::zeroed_slice_with_attrs(dev, 4, GFP_KERNEL, DMA_ATTR_NO_WARN)?;
+    ///     Coherent::zeroed_slice_with_attrs(dma, 4, GFP_KERNEL, DMA_ATTR_NO_WARN)?;
     /// # Ok::<(), Error>(()) }
     /// ```
     #[inline]
     pub fn zeroed_slice_with_attrs(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         len: usize,
         gfp_flags: kernel::alloc::Flags,
         dma_attrs: Attrs,
     ) -> Result<Coherent<'a, [T]>> {
-        Coherent::alloc_slice_with_attrs(dev, len, gfp_flags | __GFP_ZERO, dma_attrs)
+        Coherent::alloc_slice_with_attrs(dma, len, gfp_flags | __GFP_ZERO, dma_attrs)
     }
 
     /// Performs the same functionality as [`Coherent::zeroed_slice_with_attrs`], except the
     /// `dma_attrs` is 0 by default.
     #[inline]
     pub fn zeroed_slice(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         len: usize,
         gfp_flags: kernel::alloc::Flags,
     ) -> Result<Coherent<'a, [T]>> {
-        Self::zeroed_slice_with_attrs(dev, len, gfp_flags, Attrs(0))
+        Self::zeroed_slice_with_attrs(dma, len, gfp_flags, Attrs(0))
     }
 
     /// Allocates a region of coherent memory of the same size as `data` and initializes it with a
@@ -973,23 +957,22 @@ pub fn zeroed_slice(
     /// # Examples
     ///
     /// ```
-    /// # use kernel::device::{Bound, Device};
     /// use kernel::dma::{
     ///     attrs::*,
     ///     Coherent
     /// };
     ///
-    /// # fn test(dev: &Device<Bound>) -> Result {
+    /// # fn test(dma: kernel::dma::Handle<'_>) -> Result {
     /// let data = [0u8, 1u8, 2u8, 3u8];
     /// // `c` has the same content as `data`.
     /// let c: Coherent<'_, [u8]> =
-    ///     Coherent::from_slice_with_attrs(dev, &data, GFP_KERNEL, DMA_ATTR_NO_WARN)?;
+    ///     Coherent::from_slice_with_attrs(dma, &data, GFP_KERNEL, DMA_ATTR_NO_WARN)?;
     ///
     /// # Ok::<(), Error>(()) }
     /// ```
     #[inline]
     pub fn from_slice_with_attrs(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         data: &[T],
         gfp_flags: kernel::alloc::Flags,
         dma_attrs: Attrs,
@@ -997,21 +980,21 @@ pub fn from_slice_with_attrs(
     where
         T: Copy,
     {
-        CoherentBox::from_slice_with_attrs(dev, data, gfp_flags, dma_attrs).map(Into::into)
+        CoherentBox::from_slice_with_attrs(dma, data, gfp_flags, dma_attrs).map(Into::into)
     }
 
     /// Performs the same functionality as [`Coherent::from_slice_with_attrs`], except the
     /// `dma_attrs` is 0 by default.
     #[inline]
     pub fn from_slice(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         data: &[T],
         gfp_flags: kernel::alloc::Flags,
     ) -> Result<Coherent<'a, [T]>>
     where
         T: Copy,
     {
-        Self::from_slice_with_attrs(dev, data, gfp_flags, Attrs(0))
+        Self::from_slice_with_attrs(dma, data, gfp_flags, Attrs(0))
     }
 }
 
@@ -1036,7 +1019,7 @@ fn drop(&mut self) {
         // `Coherent`.
         unsafe {
             bindings::dma_free_attrs(
-                self.dev.as_raw(),
+                self.dma.as_raw(),
                 size,
                 self.cpu_addr.as_ptr().cast(),
                 self.dma_addr,
@@ -1103,7 +1086,7 @@ fn write_to_slice(
 /// - `dma_attrs` contains the attributes used for the allocation, always including
 ///   `DMA_ATTR_NO_KERNEL_MAPPING`.
 pub struct CoherentHandle<'a> {
-    dev: &'a device::Device<Bound>,
+    dma: Handle<'a>,
     dma_addr: DmaAddress,
     cpu_handle: NonNull<c_void>,
     size: usize,
@@ -1118,7 +1101,7 @@ impl<'a> CoherentHandle<'a> {
     ///
     /// Returns `EINVAL` if `size` is zero, `ENOMEM` if the allocation fails.
     pub fn alloc_with_attrs(
-        dev: &'a device::Device<Bound>,
+        dma: Handle<'a>,
         size: usize,
         gfp_flags: kernel::alloc::Flags,
         dma_attrs: Attrs,
@@ -1129,10 +1112,10 @@ pub fn alloc_with_attrs(
 
         let dma_attrs = dma_attrs | Attrs(bindings::DMA_ATTR_NO_KERNEL_MAPPING);
         let mut dma_addr = 0;
-        // SAFETY: `dev.as_raw()` is valid by the type invariant on `device::Device`.
+        // SAFETY: `dma.as_raw()` is valid by the type invariant on `device::Device`.
         let cpu_handle = unsafe {
             bindings::dma_alloc_attrs(
-                dev.as_raw(),
+                dma.as_raw(),
                 size,
                 &mut dma_addr,
                 gfp_flags.as_raw(),
@@ -1144,9 +1127,9 @@ pub fn alloc_with_attrs(
 
         // INVARIANT: `cpu_handle` is the opaque handle from a successful `dma_alloc_attrs` call
         // with `DMA_ATTR_NO_KERNEL_MAPPING`, `dma_addr` is the corresponding DMA address,
-        // and `dev` is a valid reference to a bound device that outlives this allocation.
+        // and `dma` is a configured DMA handle for a device that outlives this allocation.
         Ok(Self {
-            dev,
+            dma,
             dma_addr,
             cpu_handle,
             size,
@@ -1156,12 +1139,8 @@ pub fn alloc_with_attrs(
 
     /// Allocates `size` bytes of coherent DMA memory without creating a kernel virtual mapping.
     #[inline]
-    pub fn alloc(
-        dev: &'a device::Device<Bound>,
-        size: usize,
-        gfp_flags: kernel::alloc::Flags,
-    ) -> Result<Self> {
-        Self::alloc_with_attrs(dev, size, gfp_flags, Attrs(0))
+    pub fn alloc(dma: Handle<'a>, size: usize, gfp_flags: kernel::alloc::Flags) -> Result<Self> {
+        Self::alloc_with_attrs(dma, size, gfp_flags, Attrs(0))
     }
 
     /// Returns the DMA address for this allocation.
@@ -1185,7 +1164,7 @@ fn drop(&mut self) {
         // `cpu_handle` is the opaque handle from `dma_alloc_attrs` and is passed back unchanged.
         unsafe {
             bindings::dma_free_attrs(
-                self.dev.as_raw(),
+                self.dma.as_raw(),
                 self.size,
                 self.cpu_handle.as_ptr(),
                 self.dma_addr,
diff --git a/samples/rust/rust_dma.rs b/samples/rust/rust_dma.rs
index c84107e5b2c3..1233aac8fe23 100644
--- a/samples/rust/rust_dma.rs
+++ b/samples/rust/rust_dma.rs
@@ -96,7 +96,7 @@ fn probe<'bound>(
             })?;
 
             let ca: Coherent<'_, [MyStruct]> =
-                Coherent::zeroed_slice(dma.device(), TEST_VALUES.len(), GFP_KERNEL)?;
+                Coherent::zeroed_slice(dma, TEST_VALUES.len(), GFP_KERNEL)?;
 
             for (i, value) in TEST_VALUES.into_iter().enumerate() {
                 io_project!(ca, [panic: i]).copy_write(MyStruct::new(value.0, value.1));
-- 
2.51.2


  parent reply	other threads:[~2026-11-29 22:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 22:24 [RFC PATCH v2 00/10] rust: dma: require device configuration before DMA Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 01/10] rust: pci, platform: pass InitTokens to probe() Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 02/10] rust: dma: add Setup, Params and Handle Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 03/10] gpu: nova-core: configure DMA through dma::Setup Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 04/10] drm/tyr: " Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 05/10] samples: rust: dma: " Matteo Kloiber
2026-11-29 22:24 ` Matteo Kloiber [this message]
2026-11-29 22:24 ` [RFC PATCH v2 07/10] rust: scatterlist: require a dma::Handle for SGTable::new() Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 08/10] rust: iommu: require a dma::Handle for IoPageTable::new() Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 09/10] rust: drm: require a dma::Handle for shmem sg_table() Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 10/10] rust: dma: remove the Device trait and its setters Matteo Kloiber

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=20261129222429.2380652-7-kernel@matt3o12.de \
    --to=kernel@matt3o12.de \
    --cc=a.hindborg@kernel.org \
    --cc=abdiel.janulgue@gmail.com \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bhelgaas@google.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=kwilczynski@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.