interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / uefi / src / proto / dma / mod.rs

145 lines · 4095 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! EDK2 IOMMU protocol.
4
5use core::ffi::c_void;
6use core::marker::PhantomData;
7use core::ops::{Deref, DerefMut};
8use core::slice;
9
10use uefi_raw::table::boot::PAGE_SIZE;
11
12use crate::proto::dma::iommu::Iommu;
13
14pub mod iommu;
15
16/// A smart pointer for DMA buffers allocated through the IOMMU protocol.
17///
18/// The buffer can be accessed as a byte slice and is returned to firmware when
19/// dropped.
20#[must_use]
21#[derive(Debug)]
22pub struct DmaBuffer<'a> {
23 ptr: *mut c_void,
24 pages: usize,
25 iommu: &'a Iommu,
26}
27
28impl<'a> DmaBuffer<'a> {
29 /// Create a new DmaBuffer from a raw pointer and page count.
30 ///
31 /// # Safety
32 /// The caller must ensure that:
33 /// - `ptr` is valid for `pages * PAGE_SIZE` bytes allocated by the IOMMU protocol.
34 /// - `pages` correctly represents the number of pages allocated.
35 /// - `iommu` is the protocol instance that allocated `ptr`.
36 /// - This `DmaBuffer` is the unique owner responsible for freeing `ptr`.
37 pub const unsafe fn from_raw(ptr: *mut c_void, pages: usize, iommu: &'a Iommu) -> Self {
38 Self { ptr, pages, iommu }
39 }
40
41 /// Get the raw pointer to the buffer.
42 #[must_use]
43 pub const fn as_ptr(&self) -> *const c_void {
44 self.ptr.cast_const()
45 }
46
47 /// Get the raw mutable pointer to the buffer.
48 #[must_use]
49 pub const fn as_mut_ptr(&mut self) -> *mut c_void {
50 self.ptr
51 }
52
53 /// Get the number of pages in the buffer.
54 #[must_use]
55 pub const fn pages(&self) -> usize {
56 self.pages
57 }
58
59 /// Get the size of the buffer in bytes.
60 #[must_use]
61 pub const fn size(&self) -> usize {
62 self.pages * PAGE_SIZE
63 }
64}
65
66impl<'a> Deref for DmaBuffer<'a> {
67 type Target = [u8];
68
69 fn deref(&self) -> &[u8] {
70 // SAFETY: `DmaBuffer::from_raw` requires `ptr` to be valid for
71 // `pages * PAGE_SIZE` bytes for this buffer's lifetime.
72 unsafe { slice::from_raw_parts(self.ptr.cast(), self.pages * PAGE_SIZE) }
73 }
74}
75
76impl<'a> DerefMut for DmaBuffer<'a> {
77 fn deref_mut(&mut self) -> &mut [u8] {
78 // SAFETY: `&mut self` guarantees unique access to the owned DMA buffer,
79 // whose raw memory is valid for `pages * PAGE_SIZE` bytes.
80 unsafe { slice::from_raw_parts_mut(self.ptr.cast::<u8>(), self.pages * PAGE_SIZE) }
81 }
82}
83
84impl<'a> Drop for DmaBuffer<'a> {
85 fn drop(&mut self) {
86 if let Err(e) = self.iommu.free_buffer_raw(self.ptr, self.pages) {
87 log::error!("IOMMU free_buffer failed: {e:?}");
88 }
89 }
90}
91
92/// A smart pointer for active DMA buffer mappings.
93///
94/// The mapping keeps the firmware mapping alive and unmaps it when
95/// dropped.
96#[must_use]
97#[derive(Debug)]
98pub struct Mapping<'a, 'buf> {
99 ptr: *mut c_void,
100 iommu: &'a Iommu,
101 _buffer: PhantomData<&'buf mut DmaBuffer<'a>>,
102}
103
104impl<'a, 'buf> Mapping<'a, 'buf> {
105 /// Create a new Mapping from a raw pointer.
106 ///
107 /// # Safety
108 /// The caller must ensure that:
109 /// - `ptr` is a valid mapping pointer returned by the IOMMU protocol.
110 /// - The mapping is currently active and valid.
111 /// - `iommu` is the protocol instance that created `ptr`.
112 /// - `_buffer` is the `DmaBuffer` used to create this mapping and remains
113 /// exclusively borrowed for the returned mapping's lifetime.
114 pub const unsafe fn from_raw(
115 ptr: *mut c_void,
116 iommu: &'a Iommu,
117 _buffer: &'buf mut DmaBuffer<'a>,
118 ) -> Self {
119 Self {
120 ptr,
121 iommu,
122 _buffer: PhantomData,
123 }
124 }
125
126 /// Get the raw mapping pointer.
127 #[must_use]
128 pub const fn as_ptr(&self) -> *const c_void {
129 self.ptr.cast_const()
130 }
131
132 /// Get the raw mutable mapping pointer.
133 #[must_use]
134 pub const fn as_mut_ptr(&mut self) -> *mut c_void {
135 self.ptr
136 }
137}
138
139impl<'a, 'buf> Drop for Mapping<'a, 'buf> {
140 fn drop(&mut self) {
141 if let Err(e) = self.iommu.unmap_raw(self.as_mut_ptr()) {
142 log::error!("IOMMU unmap failed: {e:?}");
143 }
144 }
145}