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/usb/xhci/mod.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 / usb / xhci / mod.rs` 286 lines ยท 10351 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 //! xHCI (USB 3 host controller) building blocks for the polled keyboard 17 //! driver: PCI recognition, capability decoding, register layout, port 18 //! status helpers, TRBs, rings and input contexts. All pure data layout and 19 //! bookkeeping, host-tested against the xHCI 1.2 specification; register 20 //! access and controller bring-up follow in the next part of the series. 21 22 pub mod context; 23 pub mod controller; 24 pub mod ring; 25 pub mod trb; 26 27 /// PCI class code of an xHCI controller: serial bus (0x0c), USB (0x03), 28 /// programming interface xHCI (0x30). 29 pub const XHCI_CLASS_CODE: u32 = 0x0c_03_30; 30 /// PCI command register bits the driver needs: memory decode and DMA. 31 pub use crate::dma_gate::{PCI_COMMAND_BUS_MASTER, PCI_COMMAND_MEMORY}; 32 33 /// Base of an xHCI controller's register block from its PCI header: the 34 /// class/revision dword (offset 0x08) and BAR0/BAR1 (offsets 0x10, 0x14). 35 /// `None` for another device class, an I/O or unprogrammed BAR, or a failed 36 /// (all-ones) read. 37 pub fn xhci_mmio_base(class_revision: u32, bar0_low: u32, bar0_high: u32) -> Option { 38 if class_revision == u32::MAX || class_revision >> 8 != XHCI_CLASS_CODE { 39 return None; 40 } 41 let base = match bar0_low & 0x7 { 42 // 32-bit memory BAR. 43 0b000 => u64::from(bar0_low & !0xf), 44 // 64-bit memory BAR (what the xHCI specification requires). 45 0b100 => u64::from(bar0_high) << 32 | u64::from(bar0_low & !0xf), 46 _ => return None, 47 }; 48 (base != 0 && bar0_low != u32::MAX).then_some(base) 49 } 50 51 /// Operational register offsets from the operational base (CAPLENGTH). 52 pub mod op { 53 pub const USBCMD: usize = 0x00; 54 pub const USBSTS: usize = 0x04; 55 pub const CRCR: usize = 0x18; 56 pub const DCBAAP: usize = 0x30; 57 pub const CONFIG: usize = 0x38; 58 /// PORTSC of root hub port `port` (1-based). 59 pub const fn portsc(port: u8) -> usize { 60 0x400 + 0x10 * (port as usize - 1) 61 } 62 pub const USBCMD_RUN: u32 = 1 << 0; 63 pub const USBCMD_RESET: u32 = 1 << 1; 64 pub const USBSTS_HALTED: u32 = 1 << 0; 65 pub const USBSTS_HOST_ERROR: u32 = 1 << 2; 66 pub const USBSTS_NOT_READY: u32 = 1 << 11; 67 } 68 69 /// Interrupter 0 register offsets from the runtime base (RTSOFF). Each 70 /// interrupter set is 32 bytes and set 0 starts at 0x20. 71 pub mod rt { 72 pub const IMAN: usize = 0x20; 73 pub const ERSTSZ: usize = 0x28; 74 pub const ERSTBA: usize = 0x30; 75 pub const ERDP: usize = 0x38; 76 /// ERDP Event Handler Busy, cleared by writing 1. 77 pub const ERDP_BUSY: u64 = 1 << 3; 78 } 79 80 /// What the capability registers say about a controller. 81 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 82 pub struct Capabilities { 83 /// Offset of the operational registers (CAPLENGTH). 84 pub operational_offset: usize, 85 pub runtime_offset: usize, 86 pub doorbell_offset: usize, 87 pub max_slots: u8, 88 pub max_ports: u8, 89 /// Scratchpad pages the controller asks software to provide. 90 pub scratchpad_pages: u16, 91 /// HCCPARAMS1.AC64: the controller can reach 64-bit addresses. 92 pub addresses_64: bool, 93 /// Context entry size, 32 or 64 bytes (HCCPARAMS1.CSZ). 94 pub context_bytes: usize, 95 } 96 97 impl Capabilities { 98 /// Decode CAPLENGTH/HCIVERSION (offset 0x00), HCSPARAMS1 (0x04), 99 /// HCSPARAMS2 (0x08), HCCPARAMS1 (0x10), DBOFF (0x14) and RTSOFF (0x18). 100 /// `None` when the block reads as absent or inconsistent. 101 pub fn decode( 102 version_length: u32, 103 hcsparams1: u32, 104 hcsparams2: u32, 105 hccparams1: u32, 106 dboff: u32, 107 rtsoff: u32, 108 ) -> Option { 109 let cap_length = (version_length & 0xff) as usize; 110 let version = version_length >> 16; 111 if version_length == u32::MAX || cap_length < 0x20 || version < 0x0090 { 112 return None; 113 } 114 let max_slots = (hcsparams1 & 0xff) as u8; 115 let max_ports = (hcsparams1 >> 24) as u8; 116 if max_slots == 0 || max_ports == 0 { 117 return None; 118 } 119 let scratchpad_hi = (hcsparams2 >> 21) & 0x1f; 120 let scratchpad_lo = (hcsparams2 >> 27) & 0x1f; 121 Some(Self { 122 operational_offset: cap_length, 123 runtime_offset: (rtsoff & !0x1f) as usize, 124 doorbell_offset: (dboff & !0x3) as usize, 125 max_slots, 126 max_ports, 127 scratchpad_pages: (scratchpad_hi << 5 | scratchpad_lo) as u16, 128 addresses_64: hccparams1 & 1 != 0, 129 context_bytes: if hccparams1 & (1 << 2) != 0 { 64 } else { 32 }, 130 }) 131 } 132 } 133 134 /// PORTSC bits (xHCI 1.2 section 5.4.8). 135 pub mod portsc { 136 pub const CONNECTED: u32 = 1 << 0; 137 pub const ENABLED: u32 = 1 << 1; 138 pub const RESET: u32 = 1 << 4; 139 pub const POWER: u32 = 1 << 9; 140 pub const RESET_CHANGE: u32 = 1 << 21; 141 /// Change bits, all write-1-to-clear: CSC, PEC, WRC, OCC, PRC, PLC, CEC. 142 pub const CHANGES: u32 = 0x7f << 17; 143 /// Bits that keep their value when written back: read-only fields and 144 /// the preserved read/write ones (Linux calls this state "neutral"). 145 const READ_ONLY: u32 = 1 << 0 | 1 << 3 | 0xf << 10 | 1 << 30; 146 const PRESERVED: u32 = 0xf << 5 | 1 << 9 | 0x3 << 14 | 0x7 << 25; 147 148 /// A write value that changes nothing: it keeps the preserved fields 149 /// and writes 0 to Port Enabled (writing 1 disables the port) and to 150 /// every write-1-to-clear change bit. 151 pub fn neutral(value: u32) -> u32 { 152 value & (READ_ONLY | PRESERVED) 153 } 154 155 /// Port speed ID (bits 13:10). 156 pub fn speed(value: u32) -> u8 { 157 ((value >> 10) & 0xf) as u8 158 } 159 } 160 161 #[cfg(test)] 162 mod tests { 163 use super::*; 164 165 #[test] 166 fn recognizes_xhci_class_and_memory_bars() { 167 // qemu-xhci as read in the QEMU keyboard test: class 0x0c0330 rev 1, 168 // 64-bit BAR at 0x80_0000_4000. 169 assert_eq!( 170 xhci_mmio_base(0x0c03_3001, 0x0000_4004, 0x80), 171 Some(0x80_0000_4000) 172 ); 173 assert_eq!( 174 xhci_mmio_base(0x0c03_3000, 0x0000_000c, 0x6), 175 Some(0x6_0000_0000) 176 ); 177 // A 32-bit BAR ignores the next dword (it belongs to BAR1). 178 assert_eq!( 179 xhci_mmio_base(0x0c03_3000, 0xfe00_0000, 0x1234), 180 Some(0xfe00_0000) 181 ); 182 } 183 184 #[test] 185 fn rejects_other_classes_io_and_unprogrammed_bars() { 186 assert!( 187 xhci_mmio_base(0x0c03_2000, 0x1000_0004, 0).is_none(), 188 "EHCI" 189 ); 190 assert!( 191 xhci_mmio_base(0x0c03_0000, 0x1000_0004, 0).is_none(), 192 "UHCI" 193 ); 194 assert!( 195 xhci_mmio_base(0x0600_0000, 0x1000_0004, 0).is_none(), 196 "bridge" 197 ); 198 assert!( 199 xhci_mmio_base(u32::MAX, 0x1000_0004, 0).is_none(), 200 "failed read" 201 ); 202 assert!( 203 xhci_mmio_base(0x0c03_3000, 0x0000_1001, 0).is_none(), 204 "I/O BAR" 205 ); 206 assert!( 207 xhci_mmio_base(0x0c03_3000, 0x1000_0002, 0).is_none(), 208 "below 1 MiB type" 209 ); 210 assert!( 211 xhci_mmio_base(0x0c03_3000, 0x0000_0004, 0).is_none(), 212 "unprogrammed" 213 ); 214 assert!(xhci_mmio_base(0x0c03_3000, u32::MAX, u32::MAX).is_none()); 215 } 216 217 #[test] 218 fn capabilities_decode_qemu_xhci_registers() { 219 // Read from qemu-xhci in QEMU 8.2.2: CAPLENGTH 0x40, version 1.00, 64 220 // slots, 16 interrupters, 8 ports, no scratchpad, AC64, 32-byte contexts. 221 let caps = Capabilities::decode( 222 0x0100_0040, 223 0x0800_1040, 224 0x0000_000f, 225 0x0008_7001, 226 0x2000, 227 0x1000, 228 ) 229 .unwrap(); 230 assert_eq!(caps.operational_offset, 0x40); 231 assert_eq!(caps.max_slots, 64); 232 assert_eq!(caps.max_ports, 8); 233 assert_eq!(caps.scratchpad_pages, 0); 234 assert!(caps.addresses_64); 235 assert_eq!(caps.context_bytes, 32); 236 assert_eq!( 237 (caps.doorbell_offset, caps.runtime_offset), 238 (0x2000, 0x1000) 239 ); 240 } 241 242 #[test] 243 fn capabilities_decode_scratchpads_large_contexts_and_reject_absent_blocks() { 244 // Scratchpad count hi = 1 (bit 21), lo = 2 (bit 28): 34 pages. 245 let caps = Capabilities::decode( 246 0x0110_0020, 247 0x0400_0020, 248 1 << 21 | 2 << 27, 249 1 << 2, 250 0x3003, 251 0x201f, 252 ) 253 .unwrap(); 254 assert_eq!(caps.scratchpad_pages, 34); 255 assert_eq!(caps.context_bytes, 64); 256 assert!(!caps.addresses_64); 257 assert_eq!( 258 (caps.doorbell_offset, caps.runtime_offset), 259 (0x3000, 0x2000) 260 ); 261 assert!(Capabilities::decode(u32::MAX, u32::MAX, 0, 0, 0, 0).is_none()); 262 assert!(Capabilities::decode(0x0100_0010, 0x0800_1040, 0, 0, 0, 0).is_none()); 263 assert!( 264 Capabilities::decode(0x0100_0040, 0x0800_1000, 0, 0, 0, 0).is_none(), 265 "0 slots" 266 ); 267 } 268 269 #[test] 270 fn portsc_neutral_never_disables_the_port_or_acks_changes() { 271 // Connected, enabled, powered, high speed, reset-change pending. 272 let value = portsc::CONNECTED 273 | portsc::ENABLED 274 | portsc::POWER 275 | 3 << 10 276 | portsc::RESET_CHANGE 277 | 1 << 17; 278 let neutral = portsc::neutral(value); 279 assert_eq!(neutral & portsc::ENABLED, 0, "writing PED=1 would disable"); 280 assert_eq!(neutral & portsc::CHANGES, 0, "no change bit acknowledged"); 281 assert_ne!(neutral & portsc::POWER, 0, "power stays on"); 282 assert_eq!(portsc::speed(value), 3); 283 assert_eq!(op::portsc(1), 0x400); 284 assert_eq!(op::portsc(3), 0x420); 285 } 286 }