page_pool_alloc/device_dma.rs
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Module for device dma support.
5
6// UNSAFETY: This is required to implement the MappedDmaTarget trait which
7// unsafe because of it's requirement for the implementer to keep the
8// `base()..len()` mapped for the lifetime of the struct.
9#![expect(unsafe_code)]
10
11use crate::PAGE_SIZE;
12use crate::PagePoolHandle;
13use user_driver::memory::MappedDmaTarget;
14
15/// Page pool memory representing a DMA buffer useable by devices.
16pub struct PagePoolDmaBuffer {
17 // Holds allocation until dropped.
18 pub(crate) alloc: PagePoolHandle,
19 pub(crate) pfns: Vec<u64>,
20}
21
22/// SAFETY: This struct keeps both the shared memory region which the sparse
23/// mapping maps, along with the sparse mapping itself until the struct is drop,
24/// satisfying the trait.
25unsafe impl MappedDmaTarget for PagePoolDmaBuffer {
26 fn base(&self) -> *const u8 {
27 self.alloc
28 .inner
29 .mapping
30 .as_ptr()
31 .wrapping_byte_add(self.alloc.mapping_offset)
32 .cast()
33 }
34
35 fn len(&self) -> usize {
36 (self.alloc.size_pages * PAGE_SIZE) as usize
37 }
38
39 fn pfns(&self) -> &[u64] {
40 &self.pfns
41 }
42
43 fn pfn_bias(&self) -> u64 {
44 self.alloc.inner.pfn_bias
45 }
46}