From: Alvin Sun Date: Sun, 22 Nov 2026 20:50:27 +0800 Subject: [PATCH v7 08/10] rust: device: expose the device DMA attribute MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261122-tyr-ioctls-v7-8-3a2af624b992@linux.dev> References: <20261122-tyr-ioctls-v7-0-3a2af624b992@linux.dev> In-Reply-To: <20261122-tyr-ioctls-v7-0-3a2af624b992@linux.dev> To: Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Lyude Paul , David Airlie , Simona Vetter , Greg Kroah-Hartman , "Rafael J. Wysocki" , Johan Hovold Cc: Sami Tolvanen , rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org, driver-core@lists.linux.dev, Ke Sun , Alvin Sun X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795352551; l=2065; i=alvin.sun@linux.dev; s=20260317; h=from:subject:message-id; bh=KscORanvQ0VZef2aYPEycjX8LK9CDe570yZ9DPQhtQk=; b=1mwtUwK8KRgY2r1xCv65FA9DeLlUC9wYJdBSFNfSUJmtbEnnzKjFFYGEkr8C4VhPE4DJlgn8M KYj4TY2QcfHDezk6XXyXpjQnxqJYBYrXEODlGvONxI2bRyLCQvY0037 X-Developer-Key: i=alvin.sun@linux.dev; a=ed25519; pk=CHcwQp8GSoj25V/L1ZWNSQjWp9eSIb0s9LKr0Nm3WuE= X-Endpoint-Received: by B4 Relay for alvin.sun@linux.dev/20260317 with auth_id=684 List-Id: B4 Relay Submissions From: Sami Tolvanen A driver may need to know whether its device is DMA-coherent, but rust/kernel exposes no accessor for the attribute today. Add a transparent DmaAttr wrapper around device_get_dma_attr(), with the three attribute values (unset, non-coherent, coherent) exposed as constants, so drivers can compare against the exact attribute they care about. Signed-off-by: Sami Tolvanen Co-developed-by: Alvin Sun Signed-off-by: Alvin Sun --- rust/kernel/device.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 2291d85b6849e..c6381d47f51e8 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -260,6 +260,33 @@ pub unsafe fn drvdata_borrow(&self) -> Pin<&T> { } } +/// The DMA attribute of a device, as reported by firmware. A transparent +/// wrapper around the kernel's `enum dev_dma_attr`, covering all its states. +#[repr(transparent)] +#[derive(Clone, Copy, PartialEq, Eq)] +pub struct DmaAttr(bindings::dev_dma_attr); + +impl DmaAttr { + /// The device has no reported DMA attributes. + pub const NOT_SUPPORTED: Self = Self(bindings::dev_dma_attr_DEV_DMA_NOT_SUPPORTED); + + /// The device is not DMA-coherent. + pub const NON_COHERENT: Self = Self(bindings::dev_dma_attr_DEV_DMA_NON_COHERENT); + + /// The device is DMA-coherent. + pub const COHERENT: Self = Self(bindings::dev_dma_attr_DEV_DMA_COHERENT); +} + +impl Device { + /// Returns the DMA attribute of the device as reported by firmware. + #[inline] + pub fn dma_attr(&self) -> DmaAttr { + // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer + // to a `struct device`. + unsafe { DmaAttr(bindings::device_get_dma_attr(self.as_raw())) } + } +} + impl Device { /// Obtain the raw `struct device *`. pub(crate) fn as_raw(&self) -> *mut bindings::device { -- 2.43.0