Add DmaBuf for DMA-capable memory areas

This commit is contained in:
Jianfeng Jiang
2024-03-12 11:09:15 +00:00
committed by Tate, Hongliang Tian
parent a60a8ad3e1
commit d8a841f88a
6 changed files with 129 additions and 3 deletions

View File

@ -38,7 +38,7 @@ struct DmaStreamInner {
/// `DmaDirection` limits the data flow direction of `DmaStream` and
/// prevents users from reading and writing to `DmaStream` unexpectedly.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum DmaDirection {
ToDevice,
FromDevice,
@ -108,6 +108,10 @@ impl DmaStream {
&self.inner.vm_segment
}
pub fn nframes(&self) -> usize {
self.inner.vm_segment.nframes()
}
pub fn nbytes(&self) -> usize {
self.inner.vm_segment.nbytes()
}

View File

@ -595,6 +595,21 @@ impl<'a> VmReader<'a> {
}
copy_len
}
/// Read a value of `Pod` type.
///
/// # Panic
///
/// If the length of the `Pod` type exceeds `self.remain()`, then this method will panic.
pub fn read_val<T: Pod>(&mut self) -> T {
assert!(self.remain() >= core::mem::size_of::<T>());
let mut val = T::new_uninit();
let mut writer = VmWriter::from(val.as_bytes_mut());
let read_len = self.read(&mut writer);
val
}
}
impl<'a> From<&'a [u8]> for VmReader<'a> {

View File

@ -25,7 +25,7 @@ use core::ops::Range;
use spin::Once;
pub use self::{
dma::{DmaCoherent, DmaDirection, DmaStream, HasDaddr},
dma::{Daddr, DmaCoherent, DmaDirection, DmaStream, HasDaddr},
frame::{VmFrame, VmFrameVec, VmFrameVecIter, VmReader, VmSegment, VmWriter},
io::VmIo,
memory_set::{MapArea, MemorySet},