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 thin accessor on Device<Bound> that wraps device_get_dma_attr() and returns true only for DEV_DMA_COHERENT. The other attribute values (unset, non-coherent) are treated as non-coherent. Signed-off-by: Sami Tolvanen <[email protected]> Signed-off-by: Alvin Sun <[email protected]> --- rust/kernel/device.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 2291d85b6849e..57950aa71f33e 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -260,6 +260,22 @@ pub unsafe fn drvdata_borrow<T>(&self) -> Pin<&T> { } } +impl Device<Bound> { + /// Returns `true` if the device is reported as DMA-coherent by firmware. + /// + /// This wraps `device_get_dma_attr()` and only returns `true` for the + /// `DEV_DMA_COHERENT` attribute. Devices for which no DMA attribute is + /// reported (`DEV_DMA_NOT_SUPPORTED`) or which are explicitly flagged as + /// non-coherent (`DEV_DMA_NON_COHERENT`) are treated as non-coherent. + #[inline] + pub fn dma_coherent(&self) -> bool { + // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer + // to a `struct device`. + let attr = unsafe { bindings::device_get_dma_attr(self.as_raw()) }; + attr == bindings::dev_dma_attr_DEV_DMA_COHERENT + } +} + impl<Ctx: DeviceContext> Device<Ctx> { /// Obtain the raw `struct device *`. pub(crate) fn as_raw(&self) -> *mut bindings::device { -- 2.43.0
