Skip to content

Commit

Permalink
dma: deallocate
Browse files Browse the repository at this point in the history
Signed-off-by: Anhad Singh <[email protected]>
  • Loading branch information
Andy-Python-Programmer committed Aug 3, 2023
1 parent 43a71aa commit 62efc5e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/aero_kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
const_ptr_is_null, // https://github.com/rust-lang/rust/issues/74939
trait_upcasting, // https://github.com/rust-lang/rust/issues/65991
naked_functions, // https://github.com/rust-lang/rust/issues/32408
strict_provenance
)]
#![deny(trivial_numeric_casts, unused_allocation)]
#![test_runner(crate::tests::test_runner)]
Expand Down
7 changes: 7 additions & 0 deletions src/aero_kernel/src/mem/paging/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ impl LockedFrameAllocator {
.call_once(|| Mutex::new(GlobalFrameAllocator::new(memory_map)));
}

pub fn dealloc(&self, addr: PhysAddr, size_bytes: usize) {
let order = order_from_size(size_bytes as u64);

let mut allocator = self.0.get().unwrap().lock_irq();
allocator.deallocate_frame_inner(addr, order);
}

pub fn alloc(&self, size_bytes: usize) -> Option<PhysAddr> {
let order = order_from_size(size_bytes as u64);

Expand Down
10 changes: 8 additions & 2 deletions src/aero_kernel/src/utils/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub struct DmaAllocator;
// the data using the ISA or PCI system bus (which carry physical addresses).
unsafe impl Allocator for DmaAllocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
// XXX: The DMA buffer must be aligned to a page boundary.
let size_bytes = layout.size();

let phys = FRAME_ALLOCATOR.alloc_zeroed(size_bytes).ok_or(AllocError)?;
Expand All @@ -41,7 +40,14 @@ unsafe impl Allocator for DmaAllocator {
Ok(NonNull::slice_from_raw_parts(ptr, size_bytes))
}

unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout) {}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
let size_bytes = layout.size();

let addr: usize = ptr.addr().into();
let addr = VirtAddr::new(addr as u64);

FRAME_ALLOCATOR.dealloc(addr.as_hhdm_phys(), size_bytes);
}
}

pub type DmaBuffer<T> = Box<T, DmaAllocator>;
Expand Down

0 comments on commit 62efc5e

Please sign in to comment.