|
@@ -4,9 +4,6 @@ pub mod fake;
|
|
|
use crate::{Error, Result, PAGE_SIZE};
|
|
|
use core::{marker::PhantomData, ptr::NonNull};
|
|
|
|
|
|
-/// A virtual memory address in the address space of the program.
|
|
|
-pub type VirtAddr = usize;
|
|
|
-
|
|
|
/// A physical address as used for virtio.
|
|
|
pub type PhysAddr = usize;
|
|
|
|
|
@@ -14,6 +11,7 @@ pub type PhysAddr = usize;
|
|
|
#[derive(Debug)]
|
|
|
pub struct Dma<H: Hal> {
|
|
|
paddr: usize,
|
|
|
+ vaddr: NonNull<u8>,
|
|
|
pages: usize,
|
|
|
_phantom: PhantomData<H>,
|
|
|
}
|
|
@@ -22,12 +20,13 @@ impl<H: Hal> Dma<H> {
|
|
|
/// Allocates the given number of pages of physically contiguous memory to be used for DMA in
|
|
|
/// the given direction.
|
|
|
pub fn new(pages: usize, direction: BufferDirection) -> Result<Self> {
|
|
|
- let paddr = H::dma_alloc(pages, direction);
|
|
|
+ let (paddr, vaddr) = H::dma_alloc(pages, direction);
|
|
|
if paddr == 0 {
|
|
|
return Err(Error::DmaError);
|
|
|
}
|
|
|
Ok(Self {
|
|
|
paddr,
|
|
|
+ vaddr,
|
|
|
pages,
|
|
|
_phantom: PhantomData::default(),
|
|
|
})
|
|
@@ -41,7 +40,7 @@ impl<H: Hal> Dma<H> {
|
|
|
/// Returns a pointer to the given offset within the DMA region.
|
|
|
pub fn vaddr(&self, offset: usize) -> NonNull<u8> {
|
|
|
assert!(offset < self.pages * PAGE_SIZE);
|
|
|
- NonNull::new((H::phys_to_virt(self.paddr) + offset) as _).unwrap()
|
|
|
+ NonNull::new((self.vaddr.as_ptr() as usize + offset) as _).unwrap()
|
|
|
}
|
|
|
|
|
|
/// Returns a pointer to the entire DMA region as a slice.
|
|
@@ -54,23 +53,26 @@ impl<H: Hal> Dma<H> {
|
|
|
|
|
|
impl<H: Hal> Drop for Dma<H> {
|
|
|
fn drop(&mut self) {
|
|
|
- let err = H::dma_dealloc(self.paddr, self.pages);
|
|
|
+ let err = H::dma_dealloc(self.paddr, self.vaddr, self.pages);
|
|
|
assert_eq!(err, 0, "failed to deallocate DMA");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// The interface which a particular hardware implementation must implement.
|
|
|
pub trait Hal {
|
|
|
- /// Allocates the given number of contiguous physical pages of DMA memory for virtio use.
|
|
|
- fn dma_alloc(pages: usize, direction: BufferDirection) -> PhysAddr;
|
|
|
+ /// Allocates the given number of contiguous physical pages of DMA memory for VirtIO use.
|
|
|
+ ///
|
|
|
+ /// Returns both the physical address which the device can use to access the memory, and a
|
|
|
+ /// pointer to the start of it which the driver can use to access it.
|
|
|
+ fn dma_alloc(pages: usize, direction: BufferDirection) -> (PhysAddr, NonNull<u8>);
|
|
|
/// Deallocates the given contiguous physical DMA memory pages.
|
|
|
- fn dma_dealloc(paddr: PhysAddr, pages: usize) -> i32;
|
|
|
- /// Converts a physical address used for virtio to a virtual address which the program can
|
|
|
- /// access.
|
|
|
+ fn dma_dealloc(paddr: PhysAddr, vaddr: NonNull<u8>, pages: usize) -> i32;
|
|
|
+ /// Converts a physical address used for MMIO to a virtual address which the driver can access.
|
|
|
///
|
|
|
- /// This is used both for DMA regions allocated by `dma_alloc`, and for MMIO addresses within
|
|
|
- /// BARs read from the device (for the PCI transport).
|
|
|
- fn phys_to_virt(paddr: PhysAddr) -> VirtAddr;
|
|
|
+ /// This is only used for MMIO addresses within BARs read from the device, for the PCI
|
|
|
+ /// transport. It may check that the address range up to the given size is within the region
|
|
|
+ /// expected for MMIO.
|
|
|
+ fn mmio_phys_to_virt(paddr: PhysAddr, size: usize) -> NonNull<u8>;
|
|
|
/// Shares the given memory range with the device, and returns the physical address that the
|
|
|
/// device can use to access it.
|
|
|
///
|