| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use core::{
|
| 8 | marker::PhantomData,
|
| 9 | ptr::NonNull, |
| 10 | };
|
| 11 |
|
| 12 | use crate::{
|
| 13 | alloc,
|
| 14 | bindings,
|
| 15 | device::{
|
| 16 | Bound,
|
| 17 | Device, |
| 18 | },
|
| 19 | error::to_result,
|
| 20 | io::PhysAddr,
|
| 21 | prelude::*, |
| 22 | };
|
| 23 |
|
| 24 | use bindings::io_pgtable_fmt;
|
| 25 |
|
| 26 | |
| 27 | pub mod prot {
|
| 28 | |
| 29 | pub const READ: u32 = bindings::IOMMU_READ;
|
| 30 | |
| 31 | pub const WRITE: u32 = bindings::IOMMU_WRITE;
|
| 32 | |
| 33 | pub const CACHE: u32 = bindings::IOMMU_CACHE;
|
| 34 | |
| 35 | pub const NOEXEC: u32 = bindings::IOMMU_NOEXEC;
|
| 36 | |
| 37 | pub const MMIO: u32 = bindings::IOMMU_MMIO;
|
| 38 | |
| 39 | pub const PRIVILEGED: u32 = bindings::IOMMU_PRIV;
|
| 40 | }
|
| 41 |
|
| 42 | |
| 43 | pub struct Config {
|
| 44 | |
| 45 | pub quirks: usize,
|
| 46 | |
| 47 | pub pgsize_bitmap: usize,
|
| 48 | |
| 49 | pub ias: u32,
|
| 50 | |
| 51 | pub oas: u32,
|
| 52 | |
| 53 | pub coherent_walk: bool,
|
| 54 | }
|
| 55 |
|
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | pub struct IoPageTable<'a, F: IoPageTableFmt> {
|
| 62 | ptr: NonNull<bindings::io_pgtable_ops>,
|
| 63 | _dev: PhantomData<&'a Device<Bound>>,
|
| 64 | _marker: PhantomData<F>,
|
| 65 | }
|
| 66 |
|
| 67 | |
| 68 | unsafe impl<F: IoPageTableFmt> Send for IoPageTable<'_, F> {}
|
| 69 | |
| 70 | unsafe impl<F: IoPageTableFmt> Sync for IoPageTable<'_, F> {}
|
| 71 |
|
| 72 | |
| 73 | pub trait IoPageTableFmt: 'static {
|
| 74 | |
| 75 | const FORMAT: io_pgtable_fmt;
|
| 76 | }
|
| 77 |
|
| 78 | impl<'a, F: IoPageTableFmt> IoPageTable<'a, F> {
|
| 79 | |
| 80 | #[inline]
|
| 81 | pub fn new(dev: &'a Device<Bound>, config: Config) -> Result<IoPageTable<'a, F>> {
|
| 82 | let mut raw_cfg = bindings::io_pgtable_cfg {
|
| 83 | quirks: config.quirks,
|
| 84 | pgsize_bitmap: config.pgsize_bitmap,
|
| 85 | ias: config.ias,
|
| 86 | oas: config.oas,
|
| 87 | coherent_walk: config.coherent_walk,
|
| 88 | tlb: &raw const NOOP_FLUSH_OPS,
|
| 89 | iommu_dev: dev.as_raw(),
|
| 90 | ..Zeroable::zeroed()
|
| 91 | };
|
| 92 |
|
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | let ops = unsafe {
|
| 99 | bindings::alloc_io_pgtable_ops(F::FORMAT, &mut raw_cfg, core::ptr::null_mut())
|
| 100 | };
|
| 101 |
|
| 102 | |
| 103 | Ok(IoPageTable {
|
| 104 | ptr: NonNull::new(ops).ok_or(ENOMEM)?,
|
| 105 | _dev: PhantomData,
|
| 106 | _marker: PhantomData,
|
| 107 | })
|
| 108 | }
|
| 109 |
|
| 110 | |
| 111 | #[inline]
|
| 112 | pub fn raw_ops(&self) -> *mut bindings::io_pgtable_ops {
|
| 113 | self.ptr.as_ptr()
|
| 114 | }
|
| 115 |
|
| 116 | |
| 117 | #[inline]
|
| 118 | pub fn raw_pgtable(&self) -> *mut bindings::io_pgtable {
|
| 119 | |
| 120 | unsafe { kernel::container_of!(self.raw_ops(), bindings::io_pgtable, ops) }
|
| 121 | }
|
| 122 |
|
| 123 | |
| 124 | #[inline]
|
| 125 | pub fn raw_cfg(&self) -> *mut bindings::io_pgtable_cfg {
|
| 126 | |
| 127 | unsafe { &raw mut (*self.raw_pgtable()).cfg }
|
| 128 | }
|
| 129 |
|
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | #[inline]
|
| 151 | pub unsafe fn map_pages(
|
| 152 | &self,
|
| 153 | iova: usize,
|
| 154 | paddr: PhysAddr,
|
| 155 | pgsize: usize,
|
| 156 | pgcount: usize,
|
| 157 | prot: u32,
|
| 158 | flags: alloc::Flags,
|
| 159 | ) -> (usize, Result) {
|
| 160 | let mut mapped: usize = 0;
|
| 161 |
|
| 162 | |
| 163 | let map_pages = unsafe { (*self.raw_ops()).map_pages.unwrap_unchecked() };
|
| 164 |
|
| 165 | |
| 166 | let ret = to_result(unsafe {
|
| 167 | (map_pages)(
|
| 168 | self.raw_ops(),
|
| 169 | iova,
|
| 170 | paddr,
|
| 171 | pgsize,
|
| 172 | pgcount,
|
| 173 | prot as i32,
|
| 174 | flags.as_raw(),
|
| 175 | &mut mapped,
|
| 176 | )
|
| 177 | });
|
| 178 |
|
| 179 | (mapped, ret)
|
| 180 | }
|
| 181 |
|
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | #[inline]
|
| 194 | #[must_use]
|
| 195 | pub unsafe fn unmap_pages(&self, iova: usize, pgsize: usize, pgcount: usize) -> usize {
|
| 196 | |
| 197 | let unmap_pages = unsafe { (*self.raw_ops()).unmap_pages.unwrap_unchecked() };
|
| 198 |
|
| 199 | |
| 200 | unsafe { (unmap_pages)(self.raw_ops(), iova, pgsize, pgcount, core::ptr::null_mut()) }
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | static NOOP_FLUSH_OPS: bindings::iommu_flush_ops = bindings::iommu_flush_ops {
|
| 211 | tlb_flush_all: Some(rust_tlb_flush_all_noop),
|
| 212 | tlb_flush_walk: Some(rust_tlb_flush_walk_noop),
|
| 213 | tlb_add_page: None,
|
| 214 | };
|
| 215 |
|
| 216 | #[no_mangle]
|
| 217 | extern "C" fn rust_tlb_flush_all_noop(_cookie: *mut core::ffi::c_void) {}
|
| 218 |
|
| 219 | #[no_mangle]
|
| 220 | extern "C" fn rust_tlb_flush_walk_noop(
|
| 221 | _iova: usize,
|
| 222 | _size: usize,
|
| 223 | _granule: usize,
|
| 224 | _cookie: *mut core::ffi::c_void,
|
| 225 | ) {
|
| 226 | }
|
| 227 |
|
| 228 | impl<F: IoPageTableFmt> Drop for IoPageTable<'_, F> {
|
| 229 | fn drop(&mut self) {
|
| 230 | |
| 231 | |
| 232 | unsafe { bindings::free_io_pgtable_ops(self.raw_ops()) };
|
| 233 | }
|
| 234 | }
|
| 235 |
|
| 236 | |
| 237 | pub enum ARM64LPAES1 {}
|
| 238 |
|
| 239 | impl IoPageTableFmt for ARM64LPAES1 {
|
| 240 | const FORMAT: io_pgtable_fmt = bindings::io_pgtable_fmt_ARM_64_LPAE_S1 as io_pgtable_fmt;
|
| 241 | }
|
| 242 |
|
| 243 | impl IoPageTable<'_, ARM64LPAES1> {
|
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | #[inline]
|
| 253 | pub unsafe fn ttbr(&self) -> u64 {
|
| 254 | |
| 255 | unsafe { (*self.raw_cfg()).__bindgen_anon_1.arm_lpae_s1_cfg.ttbr }
|
| 256 | }
|
| 257 |
|
| 258 | |
| 259 | #[inline]
|
| 260 | pub fn mair(&self) -> u64 {
|
| 261 | |
| 262 | unsafe { (*self.raw_cfg()).__bindgen_anon_1.arm_lpae_s1_cfg.mair }
|
| 263 | }
|
| 264 | }
|