From: Sami Tolvanen <[email protected]> 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 <[email protected]> Co-developed-by: Alvin Sun <[email protected]> Signed-off-by: Alvin Sun <[email protected]> --- 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<T>(&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<Bound> { + /// 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<Ctx: DeviceContext> Device<Ctx> { /// Obtain the raw `struct device *`. pub(crate) fn as_raw(&self) -> *mut bindings::device { -- 2.43.0
