pub struct Buffer { /* private fields */ }Expand description
Concrete type for DMA buffers providing cache management, pinned memory token lifecycle, and
safe memory access backed by a mapped_vmo::Mapping.
Implementations§
Source§impl Buffer
impl Buffer
Sourcepub fn mapping_mut(&mut self) -> &mut Mapping
pub fn mapping_mut(&mut self) -> &mut Mapping
Returns a mutable reference to the underlying Mapping.
Sourcepub fn is_cache_enabled(&self) -> bool
pub fn is_cache_enabled(&self) -> bool
Returns whether CPU caching is enabled for this DMA buffer.
Sourcepub fn cache_options(&self) -> CacheOptions
pub fn cache_options(&self) -> CacheOptions
Returns the CacheOptions configured for this DMA buffer.
Sourcepub unsafe fn unpin(self) -> Result<(), Status>
pub unsafe fn unpin(self) -> Result<(), Status>
Unpins the DMA buffer’s physical memory token, consuming the buffer.
Note that Drop does not automatically unpin the buffer (instead allowing the kernel to
quarantine the pages on the BTI if dropped while still pinned). Call this method after
ensuring hardware DMA has ceased.
§Safety
The caller must ensure that hardware is no longer accessing the pinned memory (e.g. after resetting or quiescing the device).
Sourcepub fn cache_flush(&self, offset: usize, length: usize) -> Result<(), Status>
pub fn cache_flush(&self, offset: usize, length: usize) -> Result<(), Status>
Flushes CPU data cache lines (clean to memory) for the given byte range.
Use after CPU writes to the buffer so that DMA hardware can read from physical RAM
(DMA_TO_DEVICE).
Sourcepub fn cache_flush_invalidate(
&self,
offset: usize,
length: usize,
) -> Result<(), Status>
pub fn cache_flush_invalidate( &self, offset: usize, length: usize, ) -> Result<(), Status>
Flushes and invalidates CPU data cache lines for the given byte range.
Use before CPU reads from the buffer after DMA hardware writes to physical RAM
(DMA_FROM_DEVICE).
Sourcepub fn write(&self, offset: usize, src: &[u8]) -> Result<(), Status>
pub fn write(&self, offset: usize, src: &[u8]) -> Result<(), Status>
Writes src into the DMA buffer at offset and flushes the CPU cache if caching is
enabled.
Sourcepub fn read(&self, offset: usize, dst: &mut [u8]) -> Result<(), Status>
pub fn read(&self, offset: usize, dst: &mut [u8]) -> Result<(), Status>
Invalidates the CPU cache if caching is enabled and reads from the DMA buffer at offset
into dst.
Sourcepub fn write_struct<T: IntoBytes + Immutable>(
&self,
offset: usize,
val: &T,
) -> Result<(), Status>
pub fn write_struct<T: IntoBytes + Immutable>( &self, offset: usize, val: &T, ) -> Result<(), Status>
Writes a value of type T at byte offset with automatic cache flush.
Sourcepub fn read_struct<T: FromBytes + IntoBytes + KnownLayout + Immutable>(
&self,
offset: usize,
) -> Result<T, Status>
pub fn read_struct<T: FromBytes + IntoBytes + KnownLayout + Immutable>( &self, offset: usize, ) -> Result<T, Status>
Reads a value of type T from byte offset with automatic cache invalidation.
Sourcepub fn read_element<T: FromBytes + IntoBytes + KnownLayout + Immutable>(
&self,
index: usize,
) -> T
pub fn read_element<T: FromBytes + IntoBytes + KnownLayout + Immutable>( &self, index: usize, ) -> T
Reads the index-th element of type T (at byte offset index * size_of::<T>()) from the
mapped buffer.
§Panics
Panics if index * size_of::<T>() + size_of::<T>() overflows or exceeds self.size().