mirror of https://github.com/torvalds/linux.git
rust: dma: add DMA addressing capabilities
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 <abdiel.janulgue@gmail.com> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Link: https://lore.kernel.org/r/20250716150354.51081-3-dakr@kernel.org [ Add DmaMask::try_new(). - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
parent
d06d5f66f5
commit
101d66828a
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@
|
|||
|
||||
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,142 @@
|
|||
/// The [`dma::Device`](Device) trait should be implemented by bus specific device representations,
|
||||
/// where the underlying bus is DMA capable, such as [`pci::Device`](::kernel::pci::Device) or
|
||||
/// [`platform::Device`](::kernel::platform::Device).
|
||||
pub trait Device: AsRef<device::Device<Core>> {}
|
||||
pub trait Device: AsRef<device::Device<Core>> {
|
||||
/// Set up the device's DMA streaming addressing capabilities.
|
||||
///
|
||||
/// This method is usually called once from `probe()` as soon as the device capabilities are
|
||||
/// known.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This method must not be called concurrently with any DMA allocation 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 device capabilities are
|
||||
/// known.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This method must not be called concurrently with any DMA allocation 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().as_raw(), mask.value()) })
|
||||
}
|
||||
|
||||
/// Set up the device's DMA addressing capabilities.
|
||||
///
|
||||
/// This is a combination of [`Device::dma_set_mask`] and [`Device::dma_set_coherent_mask`].
|
||||
///
|
||||
/// This method is usually called once from `probe()` as soon as the device capabilities are
|
||||
/// known.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This method must not be called concurrently with any DMA allocation 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(), mask.value())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A DMA mask that holds a bitmask with the lowest `n` bits set.
|
||||
///
|
||||
/// Use [`DmaMask::new`] or [`DmaMask::try_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()`.
|
||||
#[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 <= 64`, sets exactly the lowest `n` bits.
|
||||
/// For `n > 64`, results in a build error.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use kernel::dma::DmaMask;
|
||||
///
|
||||
/// let mask0 = DmaMask::new::<0>();
|
||||
/// assert_eq!(mask0.value(), 0);
|
||||
///
|
||||
/// let mask1 = DmaMask::new::<1>();
|
||||
/// assert_eq!(mask1.value(), 0b1);
|
||||
///
|
||||
/// let mask64 = DmaMask::new::<64>();
|
||||
/// assert_eq!(mask64.value(), u64::MAX);
|
||||
///
|
||||
/// // Build failure.
|
||||
/// // let mask_overflow = DmaMask::new::<100>();
|
||||
/// ```
|
||||
#[inline]
|
||||
pub const fn new<const N: u32>() -> Self {
|
||||
let Ok(mask) = Self::try_new(N) else {
|
||||
build_error!("Invalid DMA Mask.");
|
||||
};
|
||||
|
||||
mask
|
||||
}
|
||||
|
||||
/// Constructs a `DmaMask` with the lowest `n` bits set to `1`.
|
||||
///
|
||||
/// For `n <= 64`, sets exactly the lowest `n` bits.
|
||||
/// For `n > 64`, returns [`EINVAL`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use kernel::dma::DmaMask;
|
||||
///
|
||||
/// let mask0 = DmaMask::try_new(0)?;
|
||||
/// assert_eq!(mask0.value(), 0);
|
||||
///
|
||||
/// let mask1 = DmaMask::try_new(1)?;
|
||||
/// assert_eq!(mask1.value(), 0b1);
|
||||
///
|
||||
/// let mask64 = DmaMask::try_new(64)?;
|
||||
/// assert_eq!(mask64.value(), u64::MAX);
|
||||
///
|
||||
/// let mask_overflow = DmaMask::try_new(100);
|
||||
/// assert!(mask_overflow.is_err());
|
||||
/// # Ok::<(), Error>(())
|
||||
/// ```
|
||||
#[inline]
|
||||
pub const fn try_new(n: u32) -> Result<Self> {
|
||||
Ok(Self(match n {
|
||||
0 => 0,
|
||||
1..=64 => u64::MAX >> (64 - n),
|
||||
_ => return Err(EINVAL),
|
||||
}))
|
||||
}
|
||||
|
||||
/// Returns the underlying `u64` bitmask value.
|
||||
#[inline]
|
||||
pub const fn value(&self) -> u64 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Possible attributes associated with a DMA mapping.
|
||||
///
|
||||
|
|
|
|||
Loading…
Reference in New Issue