From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.216.248.212 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795991111; b=cHVTNAtJ2OnSvxj4GBA2nZ37LHe48fJ4AWhqvpWaOIP5bBsUxK6jGixfNJ5XfbzVxXqU 7IL2QNheXOEBa+QaV60M+oNkBgbob8Babakf2OqsVfvNtgdiurHKKcC1wZ8711vLlPXH3 PfXt6gNP5JvJvCSjpI6EMLbpft/DuhgwAId/N7YNlvZWAzJ70RwxLxtHzqVTxlsK43CnF PLy2BQVXSxQUSaLIHyMxtU+HymZDAk7QCsUPrKOPtpFkbIxRDWcoKzU7rBu5dcICDscQj NshKVUDOmB6rQnPZc98877N25lGuyGCsRSR/ET+uxDuY11U6ZyEF9idH+fOW/wKgKWQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795991111; h=DMARC-Filter:From:DKIM-Signature:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=mMFqxoHQHynXvVCY99KKkqW4LprcHwR3ktx11P9QvaM=; b=ElMUdML4+oNqGXRi3flgdcdqidS4Lu01+08uaTUMWtIDXhTT1vMI18HPprVbIjJqS2SD hS3WI2btu/KDvG79cU/F18iJgfBoy65pJDuSYLMN3PzcbywFeMwQktl83qUH8LcGkYopa trZLSkHA9gQFMzaTWuAulNvNAx3wT30OruLGGwsWY+0iewLDUMAU/KpllmjUDvfqExeQd MlI2n+eSI0U9bNGSchC1bR+/EG2p2lSIf5w92sPVxtR1n62uLgUWXII5XammZm6bhVlq2 OxFc5dP5nT4maVKVIj7lPjoib7PhHCe3keKv7gghWnxmY+xdZHsEq4SA6zFStJzXgEg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=matt3o12.de; spf=pass smtp.mailfrom=matt3o12.de; dkim=pass header.d=matt3o12.de header.i=@matt3o12.de header.a=rsa-sha256 header.s=securedbylima-20230709 header.b=AYp4f7Vd; arc=none smtp.remote-ip=91.216.248.212 Received: from bulk0.mail-out.lima-city.de (bulk0.mail-out.lima-city.de [91.216.248.212]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4B3B9606A6A0; Sun, 29 Nov 2026 22:25:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4B3B9606A6A0 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=matt3o12.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=matt3o12.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=matt3o12.de header.i=@matt3o12.de header.a=rsa-sha256 header.s=securedbylima-20230709 header.b=AYp4f7Vd From: Matteo Kloiber DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=matt3o12.de; s=securedbylima-20230709; t=1795991101; bh=CMr8y4A2HQiBx5FsG5pGUHJ/nhlY1npmeUFeXhNyfeA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AYp4f7Vd4BrjP9djHHeFRX0gQPECfdVZANpX+6vE8EzMCc8mfj2QV7PFYIDGMjkCO EvNTtv7F8X9SSAcA5x033HGv+HfHRotArrtx8s8EmDYgptVGqq75ao0U7i3kolosJN zf7CP2mywu81FSiYrDkkz19pyaxacWX2xFdR4ikcZcia3+MwV1KVBCtrxg67Uzqk+5 MH3zAR/eNGSp/Z5weolX5kQmzmmBV5bPYiiyQzmBAWCR17NzsR4sM9GGnbonq80F1M ILcHbDhnN5231OUgMhZzPeJmAPiDKJ67nGRU2nAeHjDa0z3Y7ZcASZjIElddGcTRmJ xZTuAoR0hF58w== To: Danilo Krummrich , Alexandre Courbot , Alice Ryhl , Abdiel Janulgue , Robin Murphy , Andreas Hindborg , Gary Guo Cc: Daniel Almeida , Miguel Ojeda , David Airlie , Simona Vetter , Bjorn Helgaas , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Greg Kroah-Hartman , 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 Subject: [RFC PATCH v2 06/10] rust: dma: require a Handle for coherent allocations Date: Sun, 29 Nov 2026 23:24:25 +0100 Message-ID: <20261129222429.2380652-7-kernel@matt3o12.de> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20261129222429.2380652-1-kernel@matt3o12.de> References: <20261129222429.2380652-1-kernel@matt3o12.de> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 + 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 { - 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, chipset: Chipset) -> Result { ); 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) -> impl PinInit::zeroed_slice( - dma.device(), + dma, GSP_PAGE_SIZE / size_of::(), 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 { 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(), 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 { const MSGQ_SIZE: u32 = cv!(size_of::()); 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 { - 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::::zeroed(dma.device(), GFP_KERNEL)?; +/// let _buf = Coherent::::zeroed(dma, GFP_KERNEL)?; /// # Ok::<(), Error>(()) } /// ``` pub struct Params { @@ -158,6 +158,12 @@ impl<'bound> Handle<'bound> { pub fn device(self) -> &'bound device::Device { 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) -> 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`: /// /// ``` -/// # use kernel::device::{ -/// # Bound, -/// # Device, -/// # }; /// use kernel::dma::{attrs::*, /// Coherent, /// CoherentBox, /// }; /// -/// # fn test(dev: &Device) -> 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) -> 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, + dma: Handle<'a>, count: usize, gfp_flags: kernel::alloc::Flags, dma_attrs: Attrs, ) -> Result { - 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, + dma: Handle<'a>, count: usize, gfp_flags: kernel::alloc::Flags, ) -> Result { - 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(&mut self, i: usize, init: impl Init) -> Result /// ``` /// use core::ops::Deref; /// - /// # use kernel::device::{Bound, Device}; /// use kernel::dma::{ /// attrs::*, /// CoherentBox /// }; /// - /// # fn test(dev: &Device) -> 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, + 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::::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, - data: &[T], - gfp_flags: kernel::alloc::Flags, - ) -> Result + pub fn from_slice(dma: Handle<'a>, data: &[T], gfp_flags: kernel::alloc::Flags) -> Result 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, + dma: Handle<'a>, gfp_flags: kernel::alloc::Flags, dma_attrs: Attrs, ) -> Result { - 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, gfp_flags: kernel::alloc::Flags) -> Result { - Self::zeroed_with_attrs(dev, gfp_flags, Attrs(0)) + pub fn zeroed(dma: Handle<'a>, gfp_flags: kernel::alloc::Flags) -> Result { + 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, + dma: Handle<'a>, dma_addr: DmaAddress, cpu_addr: NonNull, 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, + dma: Handle<'a>, gfp_flags: kernel::alloc::Flags, dma_attrs: Attrs, ) -> Result { @@ -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::(), &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) -> 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, + dma: Handle<'a>, gfp_flags: kernel::alloc::Flags, dma_attrs: Attrs, ) -> Result { - 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, gfp_flags: kernel::alloc::Flags) -> Result { - Self::zeroed_with_attrs(dev, gfp_flags, Attrs(0)) + pub fn zeroed(dma: Handle<'a>, gfp_flags: kernel::alloc::Flags) -> Result { + 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( - dev: &'a device::Device, + dma: Handle<'a>, gfp_flags: kernel::alloc::Flags, dma_attrs: Attrs, init: impl Init, @@ -853,7 +841,7 @@ pub fn init_with_attrs( where Error: From, { - 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( /// with `init`. #[inline] pub fn init( - dev: &'a device::Device, + dma: Handle<'a>, gfp_flags: kernel::alloc::Flags, init: impl Init, ) -> Result where Error: From, { - 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, + 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::().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) -> 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, + dma: Handle<'a>, len: usize, gfp_flags: kernel::alloc::Flags, dma_attrs: Attrs, ) -> Result> { - 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, + dma: Handle<'a>, len: usize, gfp_flags: kernel::alloc::Flags, ) -> Result> { - 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) -> 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, + 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, + dma: Handle<'a>, data: &[T], gfp_flags: kernel::alloc::Flags, ) -> Result> 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, + dma: Handle<'a>, dma_addr: DmaAddress, cpu_handle: NonNull, 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, + 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, - size: usize, - gfp_flags: kernel::alloc::Flags, - ) -> Result { - 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::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