1 // SPDX-License-Identifier: GPL-2.0 2 3 //! Common page table types shared between MMU v2 and v3. 4 //! 5 //! This module provides foundational types used by both MMU versions: 6 //! - Page table level hierarchy 7 //! - Memory aperture types for PDEs and PTEs 8 9 #![expect(dead_code)] 10 11 pub(super) mod map; 12 pub(super) mod ver2; 13 pub(super) mod ver3; 14 pub(super) mod walk; 15 16 use kernel::{ 17 io::Io, 18 num::Bounded, 19 prelude::*, // 20 }; 21 22 use crate::{ 23 gpu::Architecture, 24 mm::{ 25 pramin, 26 Pfn, 27 VirtualAddress, 28 VramAddress, // 29 }, 30 }; 31 32 /// Extracts the page table index at a given level from a virtual address. 33 pub(super) trait VaLevelIndex { 34 /// Return the page table index at `level` for this virtual address. 35 fn level_index(&self, level: u64) -> u64; 36 } 37 38 /// MMU version enumeration. 39 #[derive(Debug, Clone, Copy, PartialEq, Eq)] 40 pub(crate) enum MmuVersion { 41 /// MMU v2 for Turing/Ampere/Ada. 42 V2, 43 /// MMU v3 for Hopper and later. 44 V3, 45 } 46 47 impl From<Architecture> for MmuVersion { 48 fn from(arch: Architecture) -> Self { 49 match arch { 50 Architecture::Turing | Architecture::Ampere | Architecture::Ada => Self::V2, 51 Architecture::Hopper | Architecture::BlackwellGB10x | Architecture::BlackwellGB20x => { 52 Self::V3 53 } 54 } 55 } 56 } 57 58 /// Page Table Level hierarchy for MMU v2/v3. 59 #[derive(Debug, Clone, Copy, PartialEq, Eq)] 60 pub(super) enum PageTableLevel { 61 /// Level 0 - Page Directory Base (root). 62 Pdb, 63 /// Level 1 - Intermediate page directory. 64 L1, 65 /// Level 2 - Intermediate page directory. 66 L2, 67 /// Level 3 - Intermediate page directory or dual PDE (version-dependent). 68 L3, 69 /// Level 4 - PTE level for v2, intermediate page directory for v3. 70 L4, 71 /// Level 5 - PTE level used for MMU v3 only. 72 L5, 73 } 74 75 impl PageTableLevel { 76 /// Number of entries per page table (512 for 4KB pages). 77 pub(super) const ENTRIES_PER_TABLE: usize = 512; 78 79 /// Get the next level in the hierarchy. 80 pub(super) const fn next(&self) -> Option<PageTableLevel> { 81 match self { 82 Self::Pdb => Some(Self::L1), 83 Self::L1 => Some(Self::L2), 84 Self::L2 => Some(Self::L3), 85 Self::L3 => Some(Self::L4), 86 Self::L4 => Some(Self::L5), 87 Self::L5 => None, 88 } 89 } 90 91 /// Convert level to index. 92 pub(super) const fn as_index(&self) -> u64 { 93 match self { 94 Self::Pdb => 0, 95 Self::L1 => 1, 96 Self::L2 => 2, 97 Self::L3 => 3, 98 Self::L4 => 4, 99 Self::L5 => 5, 100 } 101 } 102 } 103 104 // Trait abstractions for page table operations. 105 106 /// Operations on Page Table Entries (`PTE`s). 107 pub(super) trait PteOps: Copy + core::fmt::Debug + Into<u64> { 108 /// Create a `PTE` from a raw `u64` value. 109 fn from_raw(val: u64) -> Self; 110 111 /// Create an invalid `PTE`. 112 fn invalid() -> Self; 113 114 /// Create a valid `PTE` for the given memory aperture. 115 fn new(aperture: AperturePte, pfn: Pfn, writable: bool) -> Self; 116 117 /// Check if this `PTE` is valid. 118 fn is_valid(&self) -> bool; 119 120 /// Get the physical frame number. 121 fn frame_number(&self) -> Pfn; 122 123 /// Read a `PTE` from VRAM. 124 fn read(pramin: &mut pramin::Pramin<'_>, addr: VramAddress) -> Result<Self> { 125 let val = pramin.window_at::<u64>(addr)?.view().read_val(); 126 Ok(Self::from_raw(val)) 127 } 128 129 /// Write this `PTE` to VRAM. 130 fn write(&self, pramin: &mut pramin::Pramin<'_>, addr: VramAddress) -> Result { 131 pramin 132 .window_at::<u64>(addr)? 133 .view() 134 .write_val((*self).into()); 135 Ok(()) 136 } 137 } 138 139 /// Operations on Page Directory Entries (`PDE`s). 140 pub(super) trait PdeOps: Copy + core::fmt::Debug + Into<u64> { 141 /// Create a `PDE` from a raw `u64` value. 142 fn from_raw(val: u64) -> Self; 143 144 /// Create a valid `PDE` pointing to a page table in the given aperture. 145 fn new(aperture: AperturePde, table_pfn: Pfn) -> Self; 146 147 /// Create an invalid `PDE`. 148 fn invalid() -> Self; 149 150 /// Check if this `PDE` is valid. 151 fn is_valid(&self) -> bool; 152 153 /// Get the memory aperture of this `PDE`. 154 fn aperture(&self) -> AperturePde; 155 156 /// Get the VRAM address of the page table. 157 fn table_vram_address(&self) -> VramAddress; 158 159 /// Read a `PDE` from VRAM. 160 fn read(pramin: &mut pramin::Pramin<'_>, addr: VramAddress) -> Result<Self> { 161 let val = pramin.window_at::<u64>(addr)?.view().read_val(); 162 Ok(Self::from_raw(val)) 163 } 164 165 /// Write this `PDE` to VRAM. 166 fn write(&self, pramin: &mut pramin::Pramin<'_>, addr: VramAddress) -> Result { 167 pramin 168 .window_at::<u64>(addr)? 169 .view() 170 .write_val((*self).into()); 171 Ok(()) 172 } 173 174 /// Check if this `PDE` is valid and points to video memory. 175 fn is_valid_vram(&self) -> bool { 176 self.is_valid() && self.aperture() == AperturePde::VideoMemory 177 } 178 } 179 180 /// Operations on Dual Page Directory Entries (128-bit `DualPde`s). 181 pub(super) trait DualPdeOps: Copy + core::fmt::Debug { 182 /// Create a `DualPde` from raw 128-bit value (two `u64`s). 183 fn from_raw(big: u64, small: u64) -> Self; 184 185 /// Create a `DualPde` with only the small page table pointer set. 186 fn new_small(table_pfn: Pfn) -> Self; 187 188 /// Check if the small page table pointer is valid. 189 fn has_small(&self) -> bool; 190 191 /// Get the small page table VRAM address. 192 fn small_vram_address(&self) -> VramAddress; 193 194 /// Get the raw `u64` value of the big PDE. 195 fn big_raw_u64(&self) -> u64; 196 197 /// Get the raw `u64` value of the small PDE. 198 fn small_raw_u64(&self) -> u64; 199 200 /// Read a dual PDE (128-bit) from VRAM. 201 fn read(pramin: &mut pramin::Pramin<'_>, addr: VramAddress) -> Result<Self> { 202 let lo = pramin.window_at::<u64>(addr)?.view().read_val(); 203 let hi = pramin.window_at::<u64>(addr + 8)?.view().read_val(); 204 Ok(Self::from_raw(lo, hi)) 205 } 206 207 /// Write this dual PDE (128-bit) to VRAM. 208 fn write(&self, pramin: &mut pramin::Pramin<'_>, addr: VramAddress) -> Result { 209 pramin 210 .window_at::<u64>(addr)? 211 .view() 212 .write_val(self.big_raw_u64()); 213 pramin 214 .window_at::<u64>(addr + 8)? 215 .view() 216 .write_val(self.small_raw_u64()); 217 Ok(()) 218 } 219 } 220 221 /// MMU configuration trait -- encodes version-specific constants and types. 222 pub(super) trait MmuConfig: 'static { 223 /// Page Table Entry type. 224 type Pte: PteOps; 225 /// Page Directory Entry type. 226 type Pde: PdeOps; 227 /// Dual Page Directory Entry type (128-bit). 228 type DualPde: DualPdeOps; 229 230 /// PDE levels (excluding PTE level) for page table walking. 231 const PDE_LEVELS: &'static [PageTableLevel]; 232 /// PTE level for this MMU version. 233 const PTE_LEVEL: PageTableLevel; 234 /// Dual PDE level (128-bit entries) for this MMU version. 235 const DUAL_PDE_LEVEL: PageTableLevel; 236 237 /// Get the number of entries per page table page for a given level. 238 fn entries_per_page(level: PageTableLevel) -> usize; 239 240 /// Extract the page table index at `level` from `va`. 241 fn level_index(va: VirtualAddress, level: u64) -> u64; 242 243 /// Get the entry size in bytes for a given level. 244 fn entry_size(level: PageTableLevel) -> usize { 245 if level == Self::DUAL_PDE_LEVEL { 246 16 // 128-bit dual PDE 247 } else { 248 8 // 64-bit PDE/PTE 249 } 250 } 251 252 /// Compute upper bound on page table pages needed for `num_virt_pages`. 253 /// 254 /// Walks from PTE level up through PDE levels, accumulating the tree. 255 fn pt_pages_upper_bound(num_virt_pages: usize) -> usize { 256 let mut total = 0; 257 258 // PTE pages at the leaf level. 259 let pte_epp = Self::entries_per_page(Self::PTE_LEVEL); 260 let mut pages_at_level = num_virt_pages.div_ceil(pte_epp); 261 total += pages_at_level; 262 263 // Walk PDE levels bottom-up (reverse of PDE_LEVELS). 264 for &level in Self::PDE_LEVELS.iter().rev() { 265 let epp = Self::entries_per_page(level); 266 267 // How many pages at this level do we need to point to 268 // the previous pages_at_level? 269 pages_at_level = pages_at_level.div_ceil(epp); 270 total += pages_at_level; 271 } 272 273 total 274 } 275 } 276 277 /// Marker struct for MMU v2 (Turing/Ampere/Ada). 278 pub(super) struct MmuV2; 279 280 impl MmuConfig for MmuV2 { 281 type Pte = ver2::Pte; 282 type Pde = ver2::Pde; 283 type DualPde = ver2::DualPde; 284 285 const PDE_LEVELS: &'static [PageTableLevel] = ver2::PDE_LEVELS; 286 const PTE_LEVEL: PageTableLevel = ver2::PTE_LEVEL; 287 const DUAL_PDE_LEVEL: PageTableLevel = ver2::DUAL_PDE_LEVEL; 288 289 fn entries_per_page(level: PageTableLevel) -> usize { 290 // TODO: Calculate these values from the bitfield dynamically 291 // instead of hardcoding them. 292 match level { 293 PageTableLevel::Pdb => 4, // PD3 root: bits [48:47] = 2 bits 294 PageTableLevel::L3 => 256, // PD0 dual: bits [28:21] = 8 bits 295 _ => 512, // PD2, PD1, PT: 9 bits each 296 } 297 } 298 299 fn level_index(va: VirtualAddress, level: u64) -> u64 { 300 ver2::VirtualAddressV2::new(va).level_index(level) 301 } 302 } 303 304 /// Marker struct for MMU v3 (Hopper and later). 305 pub(super) struct MmuV3; 306 307 impl MmuConfig for MmuV3 { 308 type Pte = ver3::Pte; 309 type Pde = ver3::Pde; 310 type DualPde = ver3::DualPde; 311 312 const PDE_LEVELS: &'static [PageTableLevel] = ver3::PDE_LEVELS; 313 const PTE_LEVEL: PageTableLevel = ver3::PTE_LEVEL; 314 const DUAL_PDE_LEVEL: PageTableLevel = ver3::DUAL_PDE_LEVEL; 315 316 fn entries_per_page(level: PageTableLevel) -> usize { 317 match level { 318 PageTableLevel::Pdb => 2, // PDE4 root: bit [56] = 1 bit, 2 entries 319 PageTableLevel::L4 => 256, // PDE0 dual: bits [28:21] = 8 bits 320 _ => 512, // PDE3, PDE2, PDE1, PT: 9 bits each 321 } 322 } 323 324 fn level_index(va: VirtualAddress, level: u64) -> u64 { 325 ver3::VirtualAddressV3::new(va).level_index(level) 326 } 327 } 328 329 /// Memory aperture for Page Table Entries (`PTE`s). 330 /// 331 /// Determines which memory region the `PTE` points to. 332 #[repr(u8)] 333 #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] 334 pub(super) enum AperturePte { 335 /// Local video memory (VRAM). 336 #[default] 337 VideoMemory = 0, 338 /// Peer GPU's video memory. 339 PeerMemory = 1, 340 /// System memory with cache coherence. 341 SystemCoherent = 2, 342 /// System memory without cache coherence. 343 SystemNonCoherent = 3, 344 } 345 346 // TODO[FPRI]: Replace with `#[derive(FromPrimitive)]` when available. 347 impl From<Bounded<u64, 2>> for AperturePte { 348 fn from(val: Bounded<u64, 2>) -> Self { 349 match *val { 350 0 => Self::VideoMemory, 351 1 => Self::PeerMemory, 352 2 => Self::SystemCoherent, 353 3 => Self::SystemNonCoherent, 354 _ => Self::VideoMemory, 355 } 356 } 357 } 358 359 // TODO[FPRI]: Replace with `#[derive(ToPrimitive)]` when available. 360 impl From<AperturePte> for Bounded<u64, 2> { 361 fn from(val: AperturePte) -> Self { 362 Bounded::from_expr(val as u64 & 0x3) 363 } 364 } 365 366 /// Memory aperture for Page Directory Entries (`PDE`s). 367 /// 368 /// Note: For `PDE`s, `Invalid` (0) means the entry is not valid. 369 #[repr(u8)] 370 #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] 371 pub(super) enum AperturePde { 372 /// Invalid/unused entry. 373 #[default] 374 Invalid = 0, 375 /// Page table is in video memory. 376 VideoMemory = 1, 377 /// Page table is in system memory with coherence. 378 SystemCoherent = 2, 379 /// Page table is in system memory without coherence. 380 SystemNonCoherent = 3, 381 } 382 383 // TODO[FPRI]: Replace with `#[derive(FromPrimitive)]` when available. 384 impl From<Bounded<u64, 2>> for AperturePde { 385 fn from(val: Bounded<u64, 2>) -> Self { 386 match *val { 387 1 => Self::VideoMemory, 388 2 => Self::SystemCoherent, 389 3 => Self::SystemNonCoherent, 390 _ => Self::Invalid, 391 } 392 } 393 } 394 395 // TODO[FPRI]: Replace with `#[derive(ToPrimitive)]` when available. 396 impl From<AperturePde> for Bounded<u64, 2> { 397 fn from(val: AperturePde) -> Self { 398 Bounded::from_expr(val as u64 & 0x3) 399 } 400 } 401 402 /// Check if the PDB has valid, VRAM-backed page tables. 403 #[cfg(CONFIG_NOVA_CORE_SELFTESTS)] 404 fn check_pdb_inner<M: MmuConfig>(pramin: &mut pramin::Pramin<'_>, pdb_addr: VramAddress) -> Result { 405 let raw = pramin.window_at::<u64>(pdb_addr)?.view().read_val(); 406 407 if !M::Pde::from_raw(raw).is_valid_vram() { 408 return Err(ENOENT); 409 } 410 Ok(()) 411 } 412 413 /// Check if the PDB has valid, VRAM-backed page tables, dispatching by MMU version. 414 #[cfg(CONFIG_NOVA_CORE_SELFTESTS)] 415 pub(super) fn check_pdb_valid( 416 pramin: &mut pramin::Pramin<'_>, 417 pdb_addr: VramAddress, 418 chipset: crate::gpu::Chipset, 419 ) -> Result { 420 match MmuVersion::from(chipset.arch()) { 421 MmuVersion::V2 => check_pdb_inner::<MmuV2>(pramin, pdb_addr), 422 MmuVersion::V3 => check_pdb_inner::<MmuV3>(pramin, pdb_addr), 423 } 424 } 425