Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/uefi/src/proto/dma/iommu.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / uefi / src / proto / dma / iommu.rs` 168 lines ยท 6188 bytes Raw Blame History 1 // SPDX-License-Identifier: MIT OR Apache-2.0 2 3 //! EDKII IOMMU protocol. 4 5 use crate::data_types::PhysicalAddress; 6 use crate::mem::memory_map::MemoryType; 7 use crate::proto::unsafe_protocol; 8 use crate::{Handle, Result, Status, StatusExt}; 9 use core::ffi::c_void; 10 use core::ptr; 11 use uefi_raw::table::boot::{AllocateType, PAGE_SIZE}; 12 13 pub use crate::proto::dma::{DmaBuffer, Mapping}; 14 pub use uefi_raw::protocol::iommu::{ 15 EdkiiIommuAccess, EdkiiIommuAttribute, EdkiiIommuOperation, EdkiiIommuProtocol, 16 }; 17 18 /// EDK2 IoMmu [`Protocol`]. 19 /// 20 /// [`Protocol`]: uefi::proto::Protocol 21 #[derive(Debug)] 22 #[repr(transparent)] 23 #[unsafe_protocol(EdkiiIommuProtocol::GUID)] 24 pub struct Iommu(EdkiiIommuProtocol); 25 26 impl Iommu { 27 /// Get the IOMMU protocol revision 28 #[must_use] 29 pub const fn revision(&self) -> u64 { 30 self.0.revision 31 } 32 33 /// Set access attributes for a mapping. 34 /// 35 /// # Errors 36 /// 37 /// * [`crate::Status::INVALID_PARAMETER`]: invalid device handle, mapping, or access flags 38 /// * [`crate::Status::UNSUPPORTED`]: operation not supported by this IOMMU 39 /// * [`crate::Status::OUT_OF_RESOURCES`]: insufficient resources to modify IOMMU access 40 /// * [`crate::Status::DEVICE_ERROR`]: IOMMU device reported an error 41 pub fn set_attribute( 42 &self, 43 device_handle: Handle, 44 mapping: &mut Mapping<'_, '_>, 45 iommu_access: EdkiiIommuAccess, 46 ) -> Result { 47 let mapping_raw = mapping.as_mut_ptr(); 48 // SAFETY: `mapping_raw` comes from a live `Mapping`, and `device_handle` 49 // is an opaque firmware handle passed through unchanged. 50 let status = unsafe { 51 (self.0.set_attribute)(&self.0, device_handle.as_ptr(), mapping_raw, iommu_access) 52 }; 53 54 status.to_result() 55 } 56 57 /// Map a buffer for DMA operations. 58 /// 59 /// Returns the device address, mapping handle, and actual number of bytes mapped. 60 /// The mapping is tied to `host_buffer` and will be automatically unmapped when 61 /// dropped. 62 /// 63 /// # Errors 64 /// 65 /// * [`crate::Status::INVALID_PARAMETER`]: invalid operation or buffer 66 /// * [`crate::Status::BAD_BUFFER_SIZE`]: `number_of_bytes` is larger than `host_buffer` 67 /// * [`crate::Status::UNSUPPORTED`]: host address cannot be mapped as a common buffer 68 /// * [`crate::Status::OUT_OF_RESOURCES`]: insufficient resources 69 /// * [`crate::Status::DEVICE_ERROR`]: system hardware could not map the requested address 70 pub fn map<'iommu, 'buf>( 71 &'iommu self, 72 operation: EdkiiIommuOperation, 73 host_buffer: &'buf mut DmaBuffer<'iommu>, 74 number_of_bytes: usize, 75 ) -> Result<(PhysicalAddress, Mapping<'iommu, 'buf>, usize)> { 76 if number_of_bytes > host_buffer.size() { 77 return Err(Status::BAD_BUFFER_SIZE.into()); 78 } 79 80 let mut number_of_bytes = number_of_bytes; 81 82 let mut mapping_raw: *mut c_void = ptr::null_mut(); 83 let mut device_address: u64 = 0; 84 85 let host_address: *mut c_void = host_buffer.as_mut_ptr(); 86 87 // SAFETY: `host_address` points into `host_buffer`, which is valid for 88 // `number_of_bytes` because oversized lengths were rejected above. 89 let status = unsafe { 90 (self.0.map)( 91 &self.0, 92 operation, 93 host_address, 94 &mut number_of_bytes, 95 &mut device_address, 96 &mut mapping_raw, 97 ) 98 }; 99 100 status.to_result_with_val(|| { 101 // SAFETY: `mapping_raw` was returned by this IOMMU protocol, and 102 // `host_buffer` remains mutably borrowed for the mapping lifetime. 103 let mapping = unsafe { Mapping::from_raw(mapping_raw, self, host_buffer) }; 104 (device_address, mapping, number_of_bytes) 105 }) 106 } 107 108 /// Unmap a previously mapped buffer 109 pub(crate) fn unmap_raw(&self, mapping: *mut c_void) -> Result { 110 // SAFETY: The safe `Mapping` API only stores active mapping 111 // pointers returned by this protocol, and `Drop` calls this once. 112 let status = unsafe { (self.0.unmap)(&self.0, mapping) }; 113 status.to_result() 114 } 115 116 /// Allocate a buffer suitable for DMA operations. 117 /// 118 /// The buffer will be automatically freed when dropped. 119 /// 120 /// # Errors 121 /// 122 /// * [`crate::Status::INVALID_PARAMETER`]: invalid memory type or attributes 123 /// * [`crate::Status::UNSUPPORTED`]: unsupported attributes 124 /// * [`crate::Status::OUT_OF_RESOURCES`]: memory pages could not be allocated 125 pub fn allocate_buffer( 126 &self, 127 memory_type: MemoryType, 128 pages: usize, 129 attributes: EdkiiIommuAttribute, 130 ) -> Result> { 131 let mut host_address: *mut c_void = ptr::null_mut(); 132 133 // Per spec, AllocateType is ignored by the IOMMU allocate_buffer implementation. 134 let allocate_type = AllocateType::ANY_PAGES; 135 136 // SAFETY: `host_address` is a valid out-pointer, and all other 137 // arguments are plain values forwarded to firmware. 138 let status = unsafe { 139 (self.0.allocate_buffer)( 140 &self.0, 141 allocate_type, 142 memory_type, 143 pages, 144 &mut host_address, 145 attributes, 146 ) 147 }; 148 149 status.to_result()?; 150 151 // The firmware does not guarantee zeroed memory, but `DmaBuffer` 152 // derefs to `[u8]`, so every byte must be initialized. 153 // 154 // SAFETY: The memory range is valid. 155 unsafe { host_address.cast::().write_bytes(0, pages * PAGE_SIZE) }; 156 157 // SAFETY: The memory range is valid. 158 Ok(unsafe { DmaBuffer::from_raw(host_address, pages, self) }) 159 } 160 161 /// Free a buffer allocated with allocate_buffer 162 pub(crate) fn free_buffer_raw(&self, ptr: *mut c_void, pages: usize) -> Result { 163 // SAFETY: `DmaBuffer` calls this only for buffers allocated by this 164 // protocol, preserving the original page count. 165 let status = unsafe { (self.0.free_buffer)(&self.0, pages, ptr) }; 166 status.to_result() 167 } 168 }