Struct CoherentAllocation

Source
pub struct CoherentAllocation<T: AsBytes + FromBytes> { /* private fields */ }
Expand description

An abstraction of the dma_alloc_coherent API.

This is an abstraction around the dma_alloc_coherent API which is used to allocate and map large coherent DMA regions.

A CoherentAllocation instance contains a pointer to the allocated region (in the processor’s virtual address space) and the device address which can be given to the device as the DMA address base of the region. The region is released once CoherentAllocation is dropped.

§Invariants

  • For the lifetime of an instance of CoherentAllocation, the cpu_addr is a valid pointer to an allocated region of coherent memory and dma_handle is the DMA address base of the region.
  • The size in bytes of the allocation is equal to size_of::<T> * count.
  • size_of::<T> * count fits into a usize.

Implementations§

Source§

impl<T: AsBytes + FromBytes> CoherentAllocation<T>

Source

pub fn alloc_attrs( dev: &Device<Bound>, count: usize, gfp_flags: Flags, dma_attrs: Attrs, ) -> Result<CoherentAllocation<T>>

Allocates a region of size_of::<T> * count of coherent memory.

§Examples
use kernel::dma::{attrs::*, CoherentAllocation};

let c: CoherentAllocation<u64> =
    CoherentAllocation::alloc_attrs(dev, 4, GFP_KERNEL, DMA_ATTR_NO_WARN)?;
Source

pub fn alloc_coherent( dev: &Device<Bound>, count: usize, gfp_flags: Flags, ) -> Result<CoherentAllocation<T>>

Performs the same functionality as CoherentAllocation::alloc_attrs, except the dma_attrs is 0 by default.

Source

pub fn count(&self) -> usize

Returns the number of elements T in this allocation.

Note that this is not the size of the allocation in bytes, which is provided by Self::size.

Source

pub fn size(&self) -> usize

Returns the size in bytes of this allocation.

Source

pub fn start_ptr(&self) -> *const T

Returns the base address to the allocated region in the CPU’s virtual address space.

Source

pub fn start_ptr_mut(&mut self) -> *mut T

Returns the base address to the allocated region in the CPU’s virtual address space as a mutable pointer.

Source

pub fn dma_handle(&self) -> dma_addr_t

Returns a DMA handle which may be given to the device as the DMA address base of the region.

Source

pub fn dma_handle_with_offset(&self, offset: usize) -> Result<dma_addr_t>

Returns a DMA handle starting at offset (in units of T) which may be given to the device as the DMA address base of the region.

Returns EINVAL if offset is not within the bounds of the allocation.

Source

pub unsafe fn as_slice(&self, offset: usize, count: usize) -> Result<&[T]>

Returns the data from the region starting from offset as a slice. offset and count are in units of T, not the number of bytes.

For ringbuffer type of r/w access or use-cases where the pointer to the live data is needed, CoherentAllocation::start_ptr or CoherentAllocation::start_ptr_mut could be used instead.

§Safety
  • Callers must ensure that the device does not read/write to/from memory while the returned slice is live.
  • Callers must ensure that this call does not race with a write to the same region while the returned slice is live.
Source

pub unsafe fn as_slice_mut( &mut self, offset: usize, count: usize, ) -> Result<&mut [T]>

Performs the same functionality as CoherentAllocation::as_slice, except that a mutable slice is returned.

§Safety
  • Callers must ensure that the device does not read/write to/from memory while the returned slice is live.
  • Callers must ensure that this call does not race with a read or write to the same region while the returned slice is live.
Source

pub unsafe fn write(&mut self, src: &[T], offset: usize) -> Result

Writes data to the region starting from offset. offset is in units of T, not the number of bytes.

§Safety
  • Callers must ensure that the device does not read/write to/from memory while the returned slice is live.
  • Callers must ensure that this call does not race with a read or write to the same region that overlaps with this write.
§Examples
let somedata: [u8; 4] = [0xf; 4];
let buf: &[u8] = &somedata;
// SAFETY: There is no concurrent HW operation on the device and no other R/W access to the
// region.
unsafe { alloc.write(buf, 0)?; }

Trait Implementations§

Source§

impl<T: AsBytes + FromBytes> Drop for CoherentAllocation<T>

Note that the device configured to do DMA must be halted before this object is dropped.

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: AsBytes + FromBytes + Send> Send for CoherentAllocation<T>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Init<T> for T

Source§

unsafe fn __init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PinInit<T> for T

Source§

unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
where F: FnOnce(Pin<&mut T>) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.