From nobody Tue Oct 7 10:00:47 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 7ACB328981F; Thu, 10 Jul 2025 19:46:12 +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=1752176772; cv=none; b=R72s9vAXlVnNo326+9TmcCEQFq15jR3Lj8ecqxJ6kxPVirJ2B9gqUWJGR3oh11gida9vLOmSxJIwyYOINAXjK4dX7hmo3Ue2gsFR+Xy2Mv8b6nPQQXElNEWSGAgU0Cfn5rd6NSJ2YYceKJd/68/ugDz6yfdwKe1nANuhHsGp7SU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752176772; c=relaxed/simple; bh=9xIivPcMtqd4qQG4/znTLHx40UFibLjJ9Qa4eYva2FM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=grxHf62uCm4/k9dpcT1tCVvcr0viq67e1oqP1UuKHp7FVUpyQo5mrbz4gYYH79/aeBxhIFBQioThQj3iVO1fD5o4zN+E7la3B7oIXf3NEPIQwIRaM+dTYYePwjZZsMI6mP4Z6Tr8/JbpD4EkeUV9JDiNqcJ3wMl1F2WICcXIoQs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=R9Wp6tm8; 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="R9Wp6tm8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F2746C4CEF4; Thu, 10 Jul 2025 19:46:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752176772; bh=9xIivPcMtqd4qQG4/znTLHx40UFibLjJ9Qa4eYva2FM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R9Wp6tm8cJCLCLczFBOXD3PJNewmSR3DzcFspxVEMYFq+xjuLm4Cro4Fmijyizvaq 7D8DyLkNIQ3I9RjM6ZfNqxxFYdpnHHGCcPKg93kNrJ57ni1bySy6SoVkK2Fer/Izbe rNDrs56P60a3K4A4OraqZ04cLW/PNy7i0bU/L2DUwhEACkq1GB0Jc7Fsy48ndHOaXf I6xc3txd+82EptIAuoISJA0/j6uEAAe9YVwBSAqgV2m1Qn6MJRbAamrwiLiBONmWmJ /7ze5otREOvHCj3qa2IQNzu8H1ps6W2Zl4ns7oUMZ1m5v71rXgIeanERBJuHKUITnK 4VuQ/dtTTIM1Q== 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 1/5] rust: dma: implement `dma::Device` trait Date: Thu, 10 Jul 2025 21:45:43 +0200 Message-ID: <20250710194556.62605-2-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250710194556.62605-1-dakr@kernel.org> References: <20250710194556.62605-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`. Signed-off-by: Danilo Krummrich Reviewed-by: Abdiel Janulgue --- 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 Tue Oct 7 10:00:47 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 84F6B28981F; Thu, 10 Jul 2025 19:46: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=1752176776; cv=none; b=PWq/7/wDeFVFjcy2gltIlN/3s5JuJqJbFAv+AxgbatD6v2Ta3vWItNPT1LP+DQAygbMlfxSCc1ZaLqKWp01+q6TjHFQIvBShuRXXjaBRizRhDn2ZOzMajiQU24o/bQCChZV3PaZnNrozSx6hwFjyhl8swy0wbEjSrefnFpaH7mM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752176776; c=relaxed/simple; bh=TnZRiss20aC4t2yXVpyKvLnTYtDXtNPoRmXyKVx2/hs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PiIYYrzqjAxLNLZLK641dk+PKwPa51Ub0roBiNoUkZyZaKXzor6VA6Svx5eVHE+c7HzOqQ5o8cjazU8aa/7TB/XXooMXa4c7uP2mxx96mpH669b0vAe0/IkcVAQlhEqEd/wehDvvQW4y0mx8t3uNo5ObP7ZT1Pg8cH67eNyyKeA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PtpoAfy3; 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="PtpoAfy3" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 69779C4CEF1; Thu, 10 Jul 2025 19:46:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752176776; bh=TnZRiss20aC4t2yXVpyKvLnTYtDXtNPoRmXyKVx2/hs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PtpoAfy3oj/6amUWRybJjQ5UWRLCIpu7RDdmvigvBr8Foo8m7K0fON6zcpJWqwMus QaiT5eeRSoxXDZ3OpMjy7SZXb2Httu19DuWa9lzcOQiYeYPGOyKteMlA537ZchFUG7 yF3NOrsBHdPOACTeNJa3RfbY4FOsNSXOgGe6ylNVCIrDiOg8dHZSQye827IJ3OyJLY KLVaKuW1YEx/fNK6gYFBkMfC1pljPQx+eoh+ADYLp1Qz/leA2+JdhvkMEvh8MBsI47 6IDubluyX+mOU8vl7dc+gL1Y0KvmBY6ovDO83YlDUzKwFJP28mdSZRyG13S85hkVLB KRboFTpsxd+AQ== 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 2/5] rust: dma: add DMA addressing capabilities Date: Thu, 10 Jul 2025 21:45:44 +0200 Message-ID: <20250710194556.62605-3-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250710194556.62605-1-dakr@kernel.org> References: <20250710194556.62605-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. Signed-off-by: Danilo Krummrich Reviewed-by: Abdiel Janulgue --- rust/helpers/dma.c | 5 +++ rust/kernel/dma.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 3 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..4b27b8279941 100644 --- a/rust/kernel/dma.rs +++ b/rust/kernel/dma.rs @@ -6,9 +6,9 @@ =20 use crate::{ bindings, build_assert, device, - device::Bound, + device::{Bound, Core}, error::code::*, - error::Result, + error::{to_result, Result}, transmute::{AsBytes, FromBytes}, types::ARef, }; @@ -18,7 +18,83 @@ /// 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: u64) -> 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) }) + } + + /// 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: u64) -> 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) }) + } + + /// 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: u64) -> 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(), mask) }) + } +} + +/// Returns a bitmask with the lowest `n` bits set to `1`. +/// +/// For `n` in `0..=3D64`, returns a mask with the lowest `n` bits set. +/// For `n > 64`, returns `u64::MAX` (all bits set). +/// +/// # Examples +/// +/// ``` +/// use kernel::dma::dma_bit_mask; +/// +/// assert_eq!(dma_bit_mask(0), 0); +/// assert_eq!(dma_bit_mask(1), 0b1); +/// assert_eq!(dma_bit_mask(64), u64::MAX); +/// assert_eq!(dma_bit_mask(100), u64::MAX); // Saturates at all bits set. +/// ``` +pub const fn dma_bit_mask(n: usize) -> u64 { + match n { + 0 =3D> 0, + 1..=3D64 =3D> u64::MAX >> (64 - n), + _ =3D> u64::MAX, + } +} =20 /// Possible attributes associated with a DMA mapping. /// --=20 2.50.0 From nobody Tue Oct 7 10:00:47 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 4FF792FD87A; Thu, 10 Jul 2025 19:46:21 +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=1752176781; cv=none; b=tiIdrZ+ddkRaXJgszq65FKLMQBT8pxHIi8eO4VNjpCk6bxUP489flOn4P9G3PnceKV7NTHX6I2xdJOqL5VAEzbPZNqp3cmeK/A0qkjpQfuypshMfGw0EEfDBkr2ntL8OQIcPGJLvsMZ602V528z16RzY5sxkdmmLLDHHbhwWctM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752176781; c=relaxed/simple; bh=EPM3YwSGkqmrGTqOYa2sCJDfuwCH5DaD+f+cAtNk8vQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rtN2kHEAKUareDwMWbuG0EVgbC1yhZ2C1OeIEwQ+aRxh7OuRoD3SAStWhrxbY9pe1hgn9BBKLjcfUqEWn3Cuh4CxM4lS+sowmCksu7CpbYpEqyoGuCCwsRjXHwL8CXMx29xi3ZwK3u5Qgzn/Y4hXCdtvNJr5Ilk3ieDgOqbjFyE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gNHfdAzl; 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="gNHfdAzl" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D40CDC4CEF4; Thu, 10 Jul 2025 19:46:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752176780; bh=EPM3YwSGkqmrGTqOYa2sCJDfuwCH5DaD+f+cAtNk8vQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gNHfdAzlC0eXssIR/vaKVWaNupOrQFDpaCROAhJQMkxq+qcmPyRRZhirNGzJnbT/m vdTeYCyPbCeyjrQDF9ELge9PI/VZ4ODSVIqWDdvJVI5rqxuz60v7X/aATbAhkYD5bk KfpjZcn73SZoyvJed1X6KW4+L3ngdHx8GLRKjKQaTBj0x1wOJIuLkdS3u3Y5ansYbP OGK3UpTyt5mE6fNK+vnGhbBY5SAt3Ek7mcNqmM4wjZIEfIedAg3HzIpuE9wK9+O/6b 4U3NFhb7CWSfWHxdheeHl5j45AqJhycRj6XBmyIbBg6duQea/gy1TNxBELvG6C3k8D DIpnvBiy6Bh8A== 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 3/5] rust: pci: implement the `dma::Device` trait Date: Thu, 10 Jul 2025 21:45:45 +0200 Message-ID: <20250710194556.62605-4-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250710194556.62605-1-dakr@kernel.org> References: <20250710194556.62605-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: Abdiel Janulgue --- 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 Tue Oct 7 10:00:47 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 CF25F2FD881; Thu, 10 Jul 2025 19:46: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=1752176785; cv=none; b=bWGztwICh+ko6kU7MSkSL++qOzNrDatSpMXg72gSCLsFiJ5EHBNYP2bUAQBJwgR+nog3kp3HlYhYcjRGq2kpyZ37zLXX3tltglRaTDuzp5/bOoFR7KMOb6LoloJGNv4BVdMXBke6NQF/xm1RKHRmzCYU25YFAsg/1Pz3hM7gmpA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752176785; c=relaxed/simple; bh=vhhxnpJlh4ltrVdKPZ0PalFNXVHgE4twQwRORIbzjCI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JA+JR8HB+mmvYofNZllnY+3YvdQx3cUgu9/+EnQlsH6AwEy+m4ka+rBO5tMZF31DZ90sVnbIFgRTHucatfjzpRrIG5CgiPAARQUdtf8X+bjyISRAusGRmNVVB3EZ7TmYjaTXDN7TrrLYCIPrgFD/CJmyTw5ykuc6F14LMP2HA+M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XU9eVGAb; 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="XU9eVGAb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 535FAC4CEF1; Thu, 10 Jul 2025 19:46:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752176785; bh=vhhxnpJlh4ltrVdKPZ0PalFNXVHgE4twQwRORIbzjCI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XU9eVGAbThRlUKPswr2WH1X3+yyOQrIXbX4LXRxkLhMueFCuLr4OqwVzS5agcJTkP RzG0pzNtt64HL+r37nJzLHaDbYrNz6D962QbwETA4XOCXVkOhVI+p8ICSspzJLV8pi Y6eMxKzqGR94EZNgNgMo2SnkBB5Z4scTQ3po8fYXZFVWEIRy5NFu/kHK3oOPP8GW7J FP23tl6ifN9ryp1pR06hGbqALUp2E27bObgfa3BzA1dvt3Lq4SU7Tmz1/mipIn/GKD VUrH2ylgqPUc0G84rs45wzOcsyCPcFxyliRZFPiLPwcmkHoU6CdjNU1topjgaycOOk 4cWjag5WD0nxw== 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 4/5] rust: platform: implement the `dma::Device` trait Date: Thu, 10 Jul 2025 21:45:46 +0200 Message-ID: <20250710194556.62605-5-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250710194556.62605-1-dakr@kernel.org> References: <20250710194556.62605-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: Abdiel Janulgue --- 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 Tue Oct 7 10:00:47 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 3246F2FD889; Thu, 10 Jul 2025 19:46: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=1752176790; cv=none; b=U5MI6EsxQ5yQWkLsunfUd09H2H5BreElUN32uBTy7FBPlyP+XeNqmrggNJ/paYI5S+Tf6igDNf6iC2C84yN3J09QCr0CzxLC6kWcVCdqgOol8iY0IX7FBT4k4GCa8MRnwn1xsyYwXNA8om2rUdxo29OdUmFB8Lm6ZFcNo05tslY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752176790; c=relaxed/simple; bh=Y2jZgyG/X4B2hLyeKjERv+lNNYhceJlVSnkd7lE+GaI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c5CN4JNfh3uEH0Ltf0PbXPGIkgl4nhzfyL+MpY+7Pwa/sIXPgTwlTGOEFkaXwNVqG8BrMR+U/+SVtHM67OlfEbxBGETLlMYXM+JbdFW29/6TCUinZnfAUoFgavzOLLcJFLbIbG8gY0Rj4SAGZo3dRR0lquYbONOVsea55rl+PBw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HtgdAefa; 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="HtgdAefa" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C61BFC4CEE3; Thu, 10 Jul 2025 19:46:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752176789; bh=Y2jZgyG/X4B2hLyeKjERv+lNNYhceJlVSnkd7lE+GaI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HtgdAefaT7Ecs/25JhW14NOW/h/V0xIZAVRM+XxSA08cTljOq1RrwRHJwE1iIgbaS Jvl7KTnTg7jwrxioqyHtLiDpgccCeguaTDmnX9Fkw2t9VW3O8zKHxNyfysyWeT6OE+ vDigOgE/F0OtxrnIOpRhn5vS0GAzfrPovh3wol8PG2i0BpaGBmtJvrogiZxfnBQ8aW WwYtAokedgDDjx5phm4tKkKhqh+m3ura9CiB0DL/NUoG36brX7zFQPanBdr9qq68a+ F9cDe5gbTjF85lpHLHusWlmBpa9vTFolykdugKchjQ9E+3zTllDWzvaxG2GA61cL+v TbVxXjpHjS5zg== 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 5/5] rust: samples: dma: set DMA mask Date: Thu, 10 Jul 2025 21:45:47 +0200 Message-ID: <20250710194556.62605-6-dakr@kernel.org> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20250710194556.62605-1-dakr@kernel.org> References: <20250710194556.62605-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. Signed-off-by: Danilo Krummrich Reviewed-by: Abdiel Janulgue --- samples/rust/rust_dma.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/samples/rust/rust_dma.rs b/samples/rust/rust_dma.rs index 9e05d5c0cdae..b295c346252f 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, dma_bit_mask}, + pci, + prelude::*, + types::ARef, +}; =20 struct DmaSampleDriver { pdev: ARef, @@ -51,6 +58,9 @@ 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 + // SAFETY: There are no concurrent calls to DMA allocation and map= ping primitives. + unsafe { pdev.dma_set_mask_and_coherent(dma_bit_mask(64))? }; + let ca: CoherentAllocation =3D CoherentAllocation::alloc_coherent(pdev.as_ref(), TEST_VALUES.= len(), GFP_KERNEL)?; =20 --=20 2.50.0