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/crates/aienos-kernel/src/gic.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 / crates / aienos-kernel / src / gic.rs` 245 lines ยท 8724 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! GICv3 register programming separated from the address and CPU interface. 17 //! This module configures registers only. It does not unmask CPU interrupts. 18 19 /// Register access implemented by volatile MMIO or a host fake. 20 pub trait GicRegisters { 21 fn read32(&mut self, offset: usize) -> u32; 22 fn write32(&mut self, offset: usize, value: u32); 23 fn write64(&mut self, offset: usize, value: u64); 24 } 25 26 /// Volatile MMIO adapter for a GIC register frame mapped at its MADT address. 27 pub struct MmioGicRegisters { 28 pub base: usize, 29 } 30 impl GicRegisters for MmioGicRegisters { 31 fn read32(&mut self, offset: usize) -> u32 { 32 // SAFETY: `base` is the GIC register frame mapped at its MADT address, 33 // and the programming code passes only GICD/GICR offsets inside it. 34 unsafe { crate::arch::MmioReg::::new(self.base + offset) }.read() 35 } 36 fn write32(&mut self, offset: usize, value: u32) { 37 // SAFETY: as in `read32`. 38 unsafe { crate::arch::MmioReg::::new(self.base + offset) }.write(value); 39 } 40 fn write64(&mut self, offset: usize, value: u64) { 41 // SAFETY: as in `read32`. 42 unsafe { crate::arch::MmioReg::::new(self.base + offset) }.write(value); 43 } 44 } 45 46 pub const GICD_CTLR: usize = 0x0000; 47 pub const GICD_IGROUPR: usize = 0x0080; 48 pub const GICD_ISENABLER: usize = 0x0100; 49 pub const GICD_IPRIORITYR: usize = 0x0400; 50 pub const GICD_IROUTER: usize = 0x6000; 51 pub const GICR_WAKER: usize = 0x0014; 52 pub const GICR_IGROUPR0: usize = 0x10080; 53 pub const GICR_ISENABLER0: usize = 0x10100; 54 pub const GICR_IPRIORITYR: usize = 0x10400; 55 56 /// Distributor register block at the MADT GICD base. 57 pub struct GicDistributor(pub R); 58 59 impl GicDistributor { 60 /// Disable the distributor before configuring it. 61 pub fn disable(&mut self) { 62 self.0.write32(GICD_CTLR, 0); 63 } 64 65 /// Enable affinity routing and non-secure Group 1 delivery at the distributor. 66 pub fn enable_group1(&mut self) { 67 self.0.write32(GICD_CTLR, (1 << 4) | (1 << 1)); 68 } 69 70 /// Place an SPI in Group 1, set its priority and affinity route, then enable it. 71 pub fn configure_spi(&mut self, intid: u32, priority: u8, affinity: u64) { 72 let word = (intid / 32) as usize; 73 let group = GICD_IGROUPR + word * 4; 74 let group_value = self.0.read32(group); 75 self.0.write32(group, group_value | (1 << (intid % 32))); 76 let pword = (intid / 4) as usize; 77 let shift = (intid % 4) * 8; 78 let old = self.0.read32(GICD_IPRIORITYR + pword * 4); 79 self.0.write32( 80 GICD_IPRIORITYR + pword * 4, 81 (old & !(0xff << shift)) | (u32::from(priority) << shift), 82 ); 83 self.0.write64( 84 GICD_IROUTER + intid as usize * 8, 85 affinity & 0x0000_00ff_00ff_ffff, 86 ); 87 self.0.write32(GICD_ISENABLER + word * 4, 1 << (intid % 32)); 88 } 89 } 90 91 /// One CPU's redistributor. The register view must start at that CPU's 92 /// RD_base (the GICR frame base from the MADT), NOT the SGI frame: offsets 93 /// are RD_base-relative, so `GICR_WAKER` is at +0x14 and the SGI/PPI 94 /// registers sit in the SGI frame at +0x10000 (`GICR_IGROUPR0` = 0x10080). 95 pub struct GicRedistributor(pub R); 96 97 impl GicRedistributor { 98 /// Wake this redistributor. Returns false if ChildrenAsleep did not clear. 99 pub fn wake(&mut self, poll_limit: usize) -> bool { 100 let value = self.0.read32(GICR_WAKER) & !(1 << 1); 101 self.0.write32(GICR_WAKER, value); 102 for _ in 0..poll_limit { 103 if self.0.read32(GICR_WAKER) & (1 << 2) == 0 { 104 return true; 105 } 106 } 107 false 108 } 109 110 /// Put an SGI or PPI in Group 1, set priority, and enable it. 111 pub fn configure_ppi(&mut self, intid: u8, priority: u8) { 112 assert!(intid < 32); 113 let group = self.0.read32(GICR_IGROUPR0); 114 self.0.write32(GICR_IGROUPR0, group | (1 << intid)); 115 let offset = GICR_IPRIORITYR + (intid as usize / 4) * 4; 116 let shift = u32::from(intid % 4) * 8; 117 let old = self.0.read32(offset); 118 self.0.write32( 119 offset, 120 (old & !(0xff << shift)) | (u32::from(priority) << shift), 121 ); 122 self.0.write32(GICR_ISENABLER0, 1 << intid); 123 } 124 } 125 126 /// CPU-interface operations that use ICC_* system registers. 127 pub trait GicCpuInterface { 128 fn enable_group1(&mut self); 129 fn set_priority_mask(&mut self, mask: u8); 130 fn acknowledge(&mut self) -> u32; 131 fn end_interrupt(&mut self, intid: u32); 132 } 133 134 /// No-op CPU interface for host-side sequencing and policy tests. 135 #[derive(Default)] 136 pub struct FakeGicCpuInterface; 137 impl GicCpuInterface for FakeGicCpuInterface { 138 fn enable_group1(&mut self) {} 139 fn set_priority_mask(&mut self, _: u8) {} 140 fn acknowledge(&mut self) -> u32 { 141 1023 142 } 143 fn end_interrupt(&mut self, _: u32) {} 144 } 145 146 /// AArch64 ICC_* CPU-interface register implementation. 147 #[cfg(target_arch = "aarch64")] 148 pub struct Aarch64GicCpuInterface; 149 #[cfg(target_arch = "aarch64")] 150 impl GicCpuInterface for Aarch64GicCpuInterface { 151 fn enable_group1(&mut self) { 152 unsafe { 153 core::arch::asm!("mov x9, #1", "msr ICC_SRE_EL1, x9", "isb", "msr ICC_IGRPEN1_EL1, x9", "isb", out("x9") _, options(nostack)); 154 } 155 } 156 fn set_priority_mask(&mut self, mask: u8) { 157 unsafe { 158 core::arch::asm!("msr ICC_PMR_EL1, {0}", "isb", in(reg) u64::from(mask), options(nostack)); 159 } 160 } 161 fn acknowledge(&mut self) -> u32 { 162 let v: u64; 163 unsafe { 164 core::arch::asm!("mrs {0}, ICC_IAR1_EL1", out(reg) v, options(nomem, nostack)); 165 } 166 v as u32 167 } 168 fn end_interrupt(&mut self, intid: u32) { 169 unsafe { 170 core::arch::asm!("msr ICC_EOIR1_EL1, {0}", "isb", in(reg) u64::from(intid), options(nostack)); 171 } 172 } 173 } 174 175 #[cfg(test)] 176 mod tests { 177 use super::*; 178 use std::vec::Vec; 179 #[derive(Default)] 180 struct Fake { 181 writes: Vec<(usize, u64)>, 182 value: u32, 183 } 184 impl GicRegisters for Fake { 185 fn read32(&mut self, _: usize) -> u32 { 186 self.value 187 } 188 fn write32(&mut self, offset: usize, value: u32) { 189 self.writes.push((offset, u64::from(value))); 190 } 191 fn write64(&mut self, offset: usize, value: u64) { 192 self.writes.push((offset, value)); 193 } 194 } 195 #[test] 196 fn distributor_spi_programming_order() { 197 let mut gic = GicDistributor(Fake { 198 value: 0x8000_0000, 199 ..Fake::default() 200 }); 201 gic.configure_spi(40, 0x80, 0x1234); 202 assert_eq!( 203 gic.0.writes, 204 vec![ 205 (GICD_IGROUPR + 4, 0x8000_0100), 206 (GICD_IPRIORITYR + 40, 0x8000_0080), 207 (GICD_IROUTER + 320, 0x1234), 208 (GICD_ISENABLER + 4, 0x100), 209 ] 210 ); 211 } 212 #[test] 213 fn register_offsets_match_gicv3_spec() { 214 // Distributor (GICD_base-relative). 215 assert_eq!( 216 (GICD_CTLR, GICD_IGROUPR, GICD_ISENABLER, GICD_IPRIORITYR), 217 (0x0, 0x80, 0x100, 0x400) 218 ); 219 // GICD_IROUTER is at 0x6000 + 8n; the first SPI (32) is at 0x6100. 220 assert_eq!(GICD_IROUTER + 32 * 8, 0x6100); 221 // Redistributor (RD_base-relative): WAKER in the RD frame, SGI/PPI 222 // registers in the SGI frame at +0x10000. 223 assert_eq!(GICR_WAKER, 0x14); 224 assert_eq!( 225 (GICR_IGROUPR0, GICR_ISENABLER0, GICR_IPRIORITYR), 226 (0x1_0080, 0x1_0100, 0x1_0400) 227 ); 228 } 229 230 #[test] 231 fn redistributor_wakes_and_sets_ppi() { 232 let mut gic = GicRedistributor(Fake::default()); 233 assert!(gic.wake(1)); 234 gic.configure_ppi(27, 0xa0); 235 assert_eq!( 236 gic.0.writes, 237 vec![ 238 (GICR_WAKER, 0), 239 (GICR_IGROUPR0, 1 << 27), 240 (GICR_IPRIORITYR + 24, 0xa000_0000), 241 (GICR_ISENABLER0, 1 << 27), 242 ] 243 ); 244 } 245 }