From nobody Mon Oct 6 20:57:07 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A4A720F09C; Wed, 16 Jul 2025 15:04:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752678252; cv=none; b=W1y78VOb+S5GBntdGIk71g4yw49wkI9H6/Vhdek61F38VcUsXhRsMN4tx7s3eiBtdtNzFo5IWkluYfb7dXNnh+FDEfUErszmV/8OuHBWWUdD9A3iqf6mFyp7O2vfWYc3k7CMDk1SYZSUQPjM1sTcvfvGgyGbEminunFUNv1Wesg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752678252; c=relaxed/simple; bh=8A0P5pFC62KPF2LTzI6kCXSFRrpf1gYMsQRML8Ajos0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rp5I2TZXY8nKHt/c8Y3RiZRKeR1qUFvnPde9nmlyUk+2TZWQzxaF5BlnFr622uxLx2cmwHSUsESahHZ11TOH713PSR9hpoi0pvrVeLaf+xTZ9pzDX9xOMXVPF8ailOyy5IukgSB21PdendZJkxF/GMr1cgBzu3kF03DWGBzhi74= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dGnEeUrH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dGnEeUrH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9A47CC4CEE7; Wed, 16 Jul 2025 15:04:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752678251; bh=8A0P5pFC62KPF2LTzI6kCXSFRrpf1gYMsQRML8Ajos0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dGnEeUrHSf7tYpBinPj4pTMBZbfmsuN4/JigEEtmTsoGAsJp1nh/hm+2o35eAcWmq fJLNmVKfO2AGxi8SR9zJ8q0TPgxUIfNw/T9/TVz6Ut3hZGVQrPz2CvFIYVetOUqL4U P0Dxy825wNeeO30+90TR3PnoTRgdKVrNoh3ZnV18iQumOdMXkE8s71RSse0QZrtK1F 0WydMHonLTLF0k5RgM10j1Wxs5qfrQoXEq//+4+rxkYAlrXUrdbI6qLvnnktd/aT6q 9a3NHfRMoZt6FJXy3L80t7De0ODZRnSfSH0PrJMzvH7sbF+QaTJh4dMEqKX/kQAoIa 57L2k/vDlhRPw== From: Danilo Krummrich To: abdiel.janulgue@gmail.com, daniel.almeida@collabora.com, robin.murphy@arm.com, a.hindborg@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, aliceryhl@google.com, tmgross@umich.edu, bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org Cc: rust-for-linux@vger.kernel.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 1/5] rust: dma: implement `dma::Device` trait Date: Wed, 16 Jul 2025 17:02:46 +0200 Message-ID: <20250716150354.51081-2-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250716150354.51081-1-dakr@kernel.org> References: <20250716150354.51081-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a trait that defines the DMA specific methods of devices. The `dma::Device` trait is to be implemented by bus device representations, where the underlying bus is capable of DMA, such as `pci::Device` or `platform::Device`. Reviewed-by: Abdiel Janulgue Signed-off-by: Danilo Krummrich Reviewed-by: Daniel Almeida Reviewed-by: Greg Kroah-Hartman --- rust/kernel/dma.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs index 2ac4c47aeed3..f0af23d08e8d 100644 --- a/rust/kernel/dma.rs +++ b/rust/kernel/dma.rs @@ -5,14 +5,21 @@ //! C header: [`include/linux/dma-mapping.h`](srctree/include/linux/dma-ma= pping.h) =20 use crate::{ - bindings, build_assert, - device::{Bound, Device}, + bindings, build_assert, device, + device::Bound, error::code::*, error::Result, transmute::{AsBytes, FromBytes}, types::ARef, }; =20 +/// Trait to be implemented by DMA capable bus devices. +/// +/// The [`dma::Device`](Device) trait should be implemented by bus specifi= c device representations, +/// where the underlying bus is DMA capable, such as [`pci::Device`](::ker= nel::pci::Device) or +/// [`platform::Device`](::kernel::platform::Device). +pub trait Device: AsRef> {} + /// Possible attributes associated with a DMA mapping. /// /// They can be combined with the operators `|`, `&`, and `!`. @@ -132,7 +139,7 @@ pub mod attrs { // Hence, find a way to revoke the device resources of a `CoherentAllocati= on`, but not the // entire `CoherentAllocation` including the allocated memory itself. pub struct CoherentAllocation { - dev: ARef, + dev: ARef, dma_handle: bindings::dma_addr_t, count: usize, cpu_addr: *mut T, @@ -154,7 +161,7 @@ impl CoherentAllocation { /// # Ok::<(), Error>(()) } /// ``` pub fn alloc_attrs( - dev: &Device, + dev: &device::Device, count: usize, gfp_flags: kernel::alloc::Flags, dma_attrs: Attrs, @@ -199,7 +206,7 @@ pub fn alloc_attrs( /// Performs the same functionality as [`CoherentAllocation::alloc_att= rs`], except the /// `dma_attrs` is 0 by default. pub fn alloc_coherent( - dev: &Device, + dev: &device::Device, count: usize, gfp_flags: kernel::alloc::Flags, ) -> Result> { --=20 2.50.0 From nobody Mon Oct 6 20:57:07 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4A18C20F09C; Wed, 16 Jul 2025 15:04:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752678256; cv=none; b=rdEwlVjhEfNKRmmgX5mLyuMpySF4U2yPb4vD3HXtQrh8H1cfkWcdqISfM7gXqJAwaU2T9n29vG2oetqJQ06cI4NKZNptTM/zNx0HdmrCD9eieJdK5T88IrPCxEmz3T4htaXBRGLwyT1szwTmZ7hF4URZ9nzJN3kG8fNpb9vIdiU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752678256; c=relaxed/simple; bh=Jm5T7wVKYwNR2zjbQ2WIR5doSI9DWAxN08adyYuf1CE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dPeXwkA7lwUmb/AxldBfFy7e4uBryApqGpO9voY1OeeNtGM/d00p9dM4YkjbzbhgoJlszjbyz6u2szAE3Djs9afT3crWRaCFW7uFYIsh3svFKYT58Sy1t/q/E1Ct76h1s8cOQlMxpa2w4gtxKXHhuM+sIz+Ubi1AONxfpV78oDw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WhohAihk; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WhohAihk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 237E1C4CEEB; Wed, 16 Jul 2025 15:04:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752678256; bh=Jm5T7wVKYwNR2zjbQ2WIR5doSI9DWAxN08adyYuf1CE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WhohAihksFx6OOtIMBZKbF2jkPU9rlxI8d4iurjp/JHuSnbB6iL9bg6UNODiIZUKO blcOI3CmG+HvsJL00yWrk1GGruV5ce3C2kjx8zPvr2FuYollvuw++v9Kph+SZIXGJm d+rW1wEQE0/s9UQ54mBzIJYqgR6a4xnmRVb3458f0ZniioIohNHAzSWvYEJgB8n17Z 3Ddz+vpPrafKxSR8BxE8YP6b/Yo1FHQX3kMvELEUQElgc0woCY7H7jB4vTxnlOMJSz 8G/WdDO7G9HZyly7ggzbi0dpjI6BT8RqP1k/jX3upxjaLnjrRSq/pH+bEVgoVVnwXG Ic2xDpdmuAqMQ== From: Danilo Krummrich To: abdiel.janulgue@gmail.com, daniel.almeida@collabora.com, robin.murphy@arm.com, a.hindborg@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, aliceryhl@google.com, tmgross@umich.edu, bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org Cc: rust-for-linux@vger.kernel.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 2/5] rust: dma: add DMA addressing capabilities Date: Wed, 16 Jul 2025 17:02:47 +0200 Message-ID: <20250716150354.51081-3-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250716150354.51081-1-dakr@kernel.org> References: <20250716150354.51081-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Implement `dma_set_mask()`, `dma_set_coherent_mask()` and `dma_set_mask_and_coherent()` in the `dma::Device` trait. Those methods are used to set up the device's DMA addressing capabilities. Reviewed-by: Abdiel Janulgue Signed-off-by: Danilo Krummrich Reviewed-by: Daniel Almeida Reviewed-by: Greg Kroah-Hartman --- rust/helpers/dma.c | 5 ++ rust/kernel/dma.rs | 112 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 113 insertions(+), 4 deletions(-) diff --git a/rust/helpers/dma.c b/rust/helpers/dma.c index df8b8a77355a..6e741c197242 100644 --- a/rust/helpers/dma.c +++ b/rust/helpers/dma.c @@ -14,3 +14,8 @@ void rust_helper_dma_free_attrs(struct device *dev, size_= t size, void *cpu_addr, { dma_free_attrs(dev, size, cpu_addr, dma_handle, attrs); } + +int rust_helper_dma_set_mask_and_coherent(struct device *dev, u64 mask) +{ + return dma_set_mask_and_coherent(dev, mask); +} diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs index f0af23d08e8d..afd3ba538e3c 100644 --- a/rust/kernel/dma.rs +++ b/rust/kernel/dma.rs @@ -6,9 +6,9 @@ =20 use crate::{ bindings, build_assert, device, - device::Bound, - error::code::*, - error::Result, + device::{Bound, Core}, + error::{to_result, Result}, + prelude::*, transmute::{AsBytes, FromBytes}, types::ARef, }; @@ -18,7 +18,111 @@ /// The [`dma::Device`](Device) trait should be implemented by bus specifi= c device representations, /// where the underlying bus is DMA capable, such as [`pci::Device`](::ker= nel::pci::Device) or /// [`platform::Device`](::kernel::platform::Device). -pub trait Device: AsRef> {} +pub trait Device: AsRef> { + /// Set up the device's DMA streaming addressing capabilities. + /// + /// This method is usually called once from `probe()` as soon as the d= evice capabilities are + /// known. + /// + /// # Safety + /// + /// This method must not be called concurrently with any DMA allocatio= n or mapping primitives, + /// such as [`CoherentAllocation::alloc_attrs`]. + unsafe fn dma_set_mask(&self, mask: DmaMask) -> Result { + // SAFETY: + // - By the type invariant of `device::Device`, `self.as_ref().as_= raw()` is valid. + // - The safety requirement of this function guarantees that there= are no concurrent calls + // to DMA allocation and mapping primitives using this mask. + to_result(unsafe { bindings::dma_set_mask(self.as_ref().as_raw(), = mask.value()) }) + } + + /// Set up the device's DMA coherent addressing capabilities. + /// + /// This method is usually called once from `probe()` as soon as the d= evice capabilities are + /// known. + /// + /// # Safety + /// + /// This method must not be called concurrently with any DMA allocatio= n or mapping primitives, + /// such as [`CoherentAllocation::alloc_attrs`]. + unsafe fn dma_set_coherent_mask(&self, mask: DmaMask) -> Result { + // SAFETY: + // - By the type invariant of `device::Device`, `self.as_ref().as_= raw()` is valid. + // - The safety requirement of this function guarantees that there= are no concurrent calls + // to DMA allocation and mapping primitives using this mask. + to_result(unsafe { bindings::dma_set_coherent_mask(self.as_ref().a= s_raw(), mask.value()) }) + } + + /// Set up the device's DMA addressing capabilities. + /// + /// This is a combination of [`Device::dma_set_mask`] and [`Device::dm= a_set_coherent_mask`]. + /// + /// This method is usually called once from `probe()` as soon as the d= evice capabilities are + /// known. + /// + /// # Safety + /// + /// This method must not be called concurrently with any DMA allocatio= n or mapping primitives, + /// such as [`CoherentAllocation::alloc_attrs`]. + unsafe fn dma_set_mask_and_coherent(&self, mask: DmaMask) -> Result { + // SAFETY: + // - By the type invariant of `device::Device`, `self.as_ref().as_= raw()` is valid. + // - The safety requirement of this function guarantees that there= are no concurrent calls + // to DMA allocation and mapping primitives using this mask. + to_result(unsafe { + bindings::dma_set_mask_and_coherent(self.as_ref().as_raw(), ma= sk.value()) + }) + } +} + +/// A DMA mask that holds a bitmask with the lowest `n` bits set. +/// +/// Use [`DmaMask::new`] to construct a value. Values are guaranteed to +/// never exceed the bit width of `u64`. +/// +/// This is the Rust equivalent of the C macro `DMA_BIT_MASK()`. +/// +/// # Examples +/// +/// ``` +/// use kernel::dma::DmaMask; +/// +/// let mask0 =3D DmaMask::new(0)?; +/// assert_eq!(mask0.value(), 0); +/// +/// let mask1 =3D DmaMask::new(1)?; +/// assert_eq!(mask1.value(), 0b1); +/// +/// let mask64 =3D DmaMask::new(64)?; +/// assert_eq!(mask64.value(), u64::MAX); +/// +/// let mask_overflow =3D DmaMask::new(100); +/// assert!(mask_overflow.is_err()); +/// # Ok::<(), Error>(()) +/// ``` +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct DmaMask(u64); + +impl DmaMask { + /// Constructs a `DmaMask` with the lowest `n` bits set to `1`. + /// + /// For `n <=3D 64`, sets exactly the lowest `n` bits. + /// For `n > 64`, returns [`EINVAL`]. + #[inline] + pub const fn new(n: usize) -> Result { + Ok(Self(match n { + 0 =3D> 0, + 1..=3D64 =3D> u64::MAX >> (64 - n), + _ =3D> return Err(EINVAL), + })) + } + + /// Returns the underlying `u64` bitmask value. + #[inline] + pub const fn value(&self) -> u64 { + self.0 + } +} =20 /// Possible attributes associated with a DMA mapping. /// --=20 2.50.0 From nobody Mon Oct 6 20:57:07 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 308692868B7; Wed, 16 Jul 2025 15:04:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752678261; cv=none; b=GWIPVVhzVXSBlS0KsfFbW655f6QgII0+2LDUsJMhu3MpN966J1nrl5Vc84mLC503MUpDHBvg2pUymIpKg2UjHU6w/ukA5HWONEacgSC0ya81964fDhC/EUIeobvoHTbCsVoFMD4Y/4MlxWzbDn53RtWUQyZcf4QcePNvF3T0bUU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752678261; c=relaxed/simple; bh=EPM3YwSGkqmrGTqOYa2sCJDfuwCH5DaD+f+cAtNk8vQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pzir8w1k8Nl5bTLkwyIRJDd2rQdc8co/KJFVWyxege1gwi1YHNy0/w5wyemRJDuIa2kJi7eAIVipx1xRUbOLuPmVVKjPRnet871q2SJQufTJAYm8AeULeKLJw1RDKVpyWfYUR1kUcIVQwzQ3KiH+eph9f7nLpbcomEP32owz3NQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sSteo/O1; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="sSteo/O1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A06F2C4CEF4; Wed, 16 Jul 2025 15:04:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752678260; bh=EPM3YwSGkqmrGTqOYa2sCJDfuwCH5DaD+f+cAtNk8vQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sSteo/O152ltFqRm/AV9hYstGVBylP0pTjAHQwnVveN5GNziNtagrjkVCcdHFmB6A 669ByI3FJrKGafXAi0Rg3Qr7DkLpxM95b2BafBUdf7LF5k5agIdTuHXvpdGFII9jWt D+OyjwGRRVuYA+T1r9378t+R2SFu6uMdX0/s/Kmwn89k2vn9qzReXxr5H00GnmmTnk 3R5FZ5OfeIiszDHHPZGRPbszPWhgvyWUijDyMkUMzlpl2rhy+wtsQ1EJ2vn4BwVU+I LAmfQZTgyjTfi4oZ5YYR9QrndtNGAU02dRhmXnKXCq4UfE7cwx/unG3LqtP684XetO XGJOB3TOcvcFA== From: Danilo Krummrich To: abdiel.janulgue@gmail.com, daniel.almeida@collabora.com, robin.murphy@arm.com, a.hindborg@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, aliceryhl@google.com, tmgross@umich.edu, bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org Cc: rust-for-linux@vger.kernel.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 3/5] rust: pci: implement the `dma::Device` trait Date: Wed, 16 Jul 2025 17:02:48 +0200 Message-ID: <20250716150354.51081-4-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250716150354.51081-1-dakr@kernel.org> References: <20250716150354.51081-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The PCI bus is potentially capable of performing DMA, hence implement the `dma:Device` trait for `pci::Device`. Signed-off-by: Danilo Krummrich Reviewed-by: Daniel Almeida Reviewed-by: Greg Kroah-Hartman --- rust/kernel/pci.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 8435f8132e38..c0aef0e1faac 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -434,6 +434,8 @@ pub fn set_master(&self) { kernel::impl_device_context_deref!(unsafe { Device }); kernel::impl_device_context_into_aref!(Device); =20 +impl crate::dma::Device for Device {} + // SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate::types::AlwaysRefCounted for Device { fn inc_ref(&self) { --=20 2.50.0 From nobody Mon Oct 6 20:57:07 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A55EF20F09C; Wed, 16 Jul 2025 15:04:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752678265; cv=none; b=exhWzDkA0kuEhnbmXGsYyLPLDhE1O0NgX8JeoYgeDSIz1m2AaRFL9hBjmXgLmRw77hXq9tXMGJt3PG+Wrsdh//gFvACw9GSWixo22kuM+E5F3sS0HyTDvN59NILN8wIwX/wqZsA5M+pT/YCCwEmwpx8VcJxJa5JK3GJGcJLv/g8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752678265; c=relaxed/simple; bh=vhhxnpJlh4ltrVdKPZ0PalFNXVHgE4twQwRORIbzjCI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UfzS0tRghRV0LSBvurnpSiwrBMTtboB3wo1CmpgV/jPvx2FQf7bhhijE/TxkAN53gbOMPcRwAB6RTI2G21CtF49AeMXPLm2Re7xB6Y7CBVB4TOgbrhjSz6C2fj2i0Akn/AO5crvggy2cz3uVU3oXryYscIT3adGUJcrkHR7lPgc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Fik1BATe; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Fik1BATe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 327EAC4CEE7; Wed, 16 Jul 2025 15:04:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752678265; bh=vhhxnpJlh4ltrVdKPZ0PalFNXVHgE4twQwRORIbzjCI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Fik1BATeBCOOH/UArM6WXi3OgiOC4APPr6olqKF3Vhqozrh1a0nStLkJEmydaQ/iK 8cdYHW2Er5obVJHX23FW2d14GqlSzrpQob6RVQTYe7Dx7l796x0YAOTj84HF1uHE2W vRGLYQByicKQcvgzA4qIo4Hw60RDSmqzOAzLFBfVakRJzCnmas/HUvVZVUDt92wrka 8VU0bbbhDSEDP990+OQCvdJj47puOBiTPnIhRjYu2CvAtcWEPDV7164k8FT53VwsLL rsM+LXoO9ej5vix2qeXskPj9LZGiCleWfm1vZC+IRJL+M1GGQYGG/sXUzI8OSzjLyt vdlSPQZw7POfA== From: Danilo Krummrich To: abdiel.janulgue@gmail.com, daniel.almeida@collabora.com, robin.murphy@arm.com, a.hindborg@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, aliceryhl@google.com, tmgross@umich.edu, bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org Cc: rust-for-linux@vger.kernel.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 4/5] rust: platform: implement the `dma::Device` trait Date: Wed, 16 Jul 2025 17:02:49 +0200 Message-ID: <20250716150354.51081-5-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250716150354.51081-1-dakr@kernel.org> References: <20250716150354.51081-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The platform bus is potentially capable of performing DMA, hence implement the `dma:Device` trait for `platform::Device`. Signed-off-by: Danilo Krummrich Reviewed-by: Daniel Almeida Reviewed-by: Greg Kroah-Hartman --- rust/kernel/platform.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 5b21fa517e55..7236af6482be 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -195,6 +195,8 @@ fn as_raw(&self) -> *mut bindings::platform_device { kernel::impl_device_context_deref!(unsafe { Device }); kernel::impl_device_context_into_aref!(Device); =20 +impl crate::dma::Device for Device {} + // SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate::types::AlwaysRefCounted for Device { fn inc_ref(&self) { --=20 2.50.0 From nobody Mon Oct 6 20:57:07 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3808E20F09C; Wed, 16 Jul 2025 15:04:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752678270; cv=none; b=RAE+c1o1eegOLrz1rLRQba1HzhYmhDiPEynSn7BckMNMG/iXxuS0KJvobRaZ8icNNkMzAkHqlhejWXZOzexQQf7DzdarGeSzNh7sKmYKiELSNHjXalwRk0S7Rr01ou6DKVsEmLBxS9abrr3N1Lh3Xw2aI1KOzVSH6hjSKZl9DYY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752678270; c=relaxed/simple; bh=8YbmoICxDHq8BmA5AG+ttKnGA2bK0YMKDQUYjpfZl1I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Fva0dseTIhYMm0v6xDtm50pjYSc85aXyrXQS/mOyr+OhFcc34xWhln5qwxt+FTXlPMHUEqiBT7iZFaqI/0/r/bveL/zhFkzOggqQ/SFK9aBttfe/Y57ZERMVuSoureuDEW4wHIVxVV3yNWUxEQsl3RDd4Nbg0E/ZLPzfDPHBOBo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TuaDUPA1; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="TuaDUPA1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B6643C4CEEB; Wed, 16 Jul 2025 15:04:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752678269; bh=8YbmoICxDHq8BmA5AG+ttKnGA2bK0YMKDQUYjpfZl1I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TuaDUPA1RMXdRBoIH8EMUk/TxGAcdmpsHj8SlaUxRHh1igaFiYJvrVW0YSAInybi7 DlAinAnB2yF3zZn4L4BtKJTYCgnEenmV8M5VQGiiEQMYNP3RctDsZGTqC92d61p6JH ie9Ubq/hAJ0XfcogZE+SoyHlAtIllk6Pn0p4pJYcYifbvr9TLWmcu1DwJhCuqTagJi rn2vVraS2y4G+JS4zN7WW7AaBo414b8waeFanCIF/iKesledQf++rjMycgOGxHsSCL VMgCunhPLo5Ptb8h+X3kB7YiPsiWOr05BWa3FGqFPnd+s2xox3yc1ge3l2VnpCs5N5 3UPnVf7Z+gX6w== From: Danilo Krummrich To: abdiel.janulgue@gmail.com, daniel.almeida@collabora.com, robin.murphy@arm.com, a.hindborg@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, aliceryhl@google.com, tmgross@umich.edu, bhelgaas@google.com, kwilczynski@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org Cc: rust-for-linux@vger.kernel.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 5/5] rust: samples: dma: set DMA mask Date: Wed, 16 Jul 2025 17:02:50 +0200 Message-ID: <20250716150354.51081-6-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250716150354.51081-1-dakr@kernel.org> References: <20250716150354.51081-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Set a DMA mask for the `pci::Device` in the Rust DMA sample driver. Reviewed-by: Abdiel Janulgue Signed-off-by: Danilo Krummrich Reviewed-by: Daniel Almeida Reviewed-by: Greg Kroah-Hartman --- samples/rust/rust_dma.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/samples/rust/rust_dma.rs b/samples/rust/rust_dma.rs index 9e05d5c0cdae..9422ac68c139 100644 --- a/samples/rust/rust_dma.rs +++ b/samples/rust/rust_dma.rs @@ -4,7 +4,14 @@ //! //! To make this driver probe, QEMU must be run with `-device pci-testdev`. =20 -use kernel::{bindings, device::Core, dma::CoherentAllocation, pci, prelude= ::*, types::ARef}; +use kernel::{ + bindings, + device::Core, + dma::{CoherentAllocation, Device, DmaMask}, + pci, + prelude::*, + types::ARef, +}; =20 struct DmaSampleDriver { pdev: ARef, @@ -51,6 +58,11 @@ impl pci::Driver for DmaSampleDriver { fn probe(pdev: &pci::Device, _info: &Self::IdInfo) -> Result>> { dev_info!(pdev.as_ref(), "Probe DMA test driver.\n"); =20 + let mask =3D DmaMask::new(64)?; + + // SAFETY: There are no concurrent calls to DMA allocation and map= ping primitives. + unsafe { pdev.dma_set_mask_and_coherent(mask)? }; + let ca: CoherentAllocation =3D CoherentAllocation::alloc_coherent(pdev.as_ref(), TEST_VALUES.= len(), GFP_KERNEL)?; =20 --=20 2.50.0