diff options
| author | salmonik <salmonik@tuta.io> | 2026-07-31 16:10:40 +0200 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-07-31 16:10:40 +0200 |
| commit | 8b1c1a23382806fcdbd3dbc7336a6ac2da8a995b (patch) | |
| tree | 5d7b039a1baf289638d34b6e2d66c81ba9117560 /rsnix/src/pci.rs | |
| parent | style: rustfmt field alignment in RuntimeServices (diff) | |
| download | rs-dos-8b1c1a23382806fcdbd3dbc7336a6ac2da8a995b.tar.gz rs-dos-8b1c1a23382806fcdbd3dbc7336a6ac2da8a995b.zip | |
rtc + pci: CMOS clock with date command, PCI bus enumeration
- rtc.rs: read the PC RTC via CMOS ports 0x70/0x71 (UIP wait, BCD/12h
normalization, century sanity-check with 2000+ fallback); boot prints
the wall clock, shell gains 'date'
- pci.rs: legacy config-space access (0xCF8/0xCFC) with full enumeration
of buses 0-255; boot reports device count, shell gains 'pci' (lspci
style listing with class names)
- QEMU q35 verified: Q35 host bridge, stdvga, e1000, ICH9 LPC
Diffstat (limited to '')
| -rw-r--r-- | rsnix/src/pci.rs | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/rsnix/src/pci.rs b/rsnix/src/pci.rs new file mode 100644 index 0000000..2d201f3 --- /dev/null +++ b/rsnix/src/pci.rs | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | // --------------------------------------------------------------------------- | ||
| 2 | // PCI config space — legacy port-based access (0xCF8 / 0xCFC). | ||
| 3 | // | ||
| 4 | // Enumeration walks buses 0-255 × devices 0-31 × functions 0-7. Port I/O | ||
| 5 | // stays usable after ExitBootServices, so the shell can list devices and | ||
| 6 | // future drivers (AHCI, network, ...) can find their controllers. | ||
| 7 | // --------------------------------------------------------------------------- | ||
| 8 | |||
| 9 | const CONFIG_ADDRESS: u16 = 0xCF8; | ||
| 10 | const CONFIG_DATA: u16 = 0xCFC; | ||
| 11 | |||
| 12 | #[derive(Debug, Clone, Copy)] | ||
| 13 | pub struct DeviceInfo { | ||
| 14 | pub bus: u8, | ||
| 15 | pub device: u8, | ||
| 16 | pub func: u8, | ||
| 17 | pub vendor: u16, | ||
| 18 | pub device_id: u16, | ||
| 19 | pub class: u8, | ||
| 20 | pub subclass: u8, | ||
| 21 | pub prog_if: u8, | ||
| 22 | pub header_type: u8, | ||
| 23 | } | ||
| 24 | |||
| 25 | fn outl(port: u16, val: u32) { | ||
| 26 | unsafe { | ||
| 27 | core::arch::asm!( | ||
| 28 | "out dx, eax", | ||
| 29 | in("dx") port, | ||
| 30 | in("eax") val, | ||
| 31 | options(nomem, nostack, preserves_flags) | ||
| 32 | ); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | fn inl(port: u16) -> u32 { | ||
| 37 | let val: u32; | ||
| 38 | unsafe { | ||
| 39 | core::arch::asm!( | ||
| 40 | "in eax, dx", | ||
| 41 | out("eax") val, | ||
| 42 | in("dx") port, | ||
| 43 | options(nomem, nostack, preserves_flags) | ||
| 44 | ); | ||
| 45 | } | ||
| 46 | val | ||
| 47 | } | ||
| 48 | |||
| 49 | /// Read a dword from a device's config space (`reg` must be dword-aligned). | ||
| 50 | pub fn config_read(bus: u8, device: u8, func: u8, reg: u8) -> u32 { | ||
| 51 | let addr = 0x8000_0000 | ||
| 52 | | ((bus as u32) << 16) | ||
| 53 | | ((device as u32) << 11) | ||
| 54 | | ((func as u32) << 8) | ||
| 55 | | ((reg as u32) & 0xFC); | ||
| 56 | outl(CONFIG_ADDRESS, addr); | ||
| 57 | inl(CONFIG_DATA) | ||
| 58 | } | ||
| 59 | |||
| 60 | /// Enumerate every device/function present on all buses. | ||
| 61 | pub fn probe() -> alloc::vec::Vec<DeviceInfo> { | ||
| 62 | let mut out = alloc::vec::Vec::new(); | ||
| 63 | for bus in 0..=255u8 { | ||
| 64 | for dev in 0..32u8 { | ||
| 65 | for func in 0..8u8 { | ||
| 66 | let id = config_read(bus, dev, func, 0); | ||
| 67 | let vendor = (id & 0xFFFF) as u16; | ||
| 68 | if vendor == 0xFFFF { | ||
| 69 | if func == 0 { | ||
| 70 | break; // nothing on this device number | ||
| 71 | } | ||
| 72 | continue; | ||
| 73 | } | ||
| 74 | let hdr = config_read(bus, dev, func, 0x0C); | ||
| 75 | let header_type = (hdr & 0xFF) as u8; | ||
| 76 | let cc = config_read(bus, dev, func, 0x08); | ||
| 77 | out.push(DeviceInfo { | ||
| 78 | bus, | ||
| 79 | device: dev, | ||
| 80 | func, | ||
| 81 | vendor, | ||
| 82 | device_id: (id >> 16) as u16, | ||
| 83 | class: (cc >> 24) as u8, | ||
| 84 | subclass: (cc >> 16) as u8, | ||
| 85 | prog_if: (cc >> 8) as u8, | ||
| 86 | header_type, | ||
| 87 | }); | ||
| 88 | // A non-multifunction device has no functions 1-7. | ||
| 89 | if func == 0 && header_type & 0x80 == 0 { | ||
| 90 | break; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | out | ||
| 96 | } | ||
| 97 | |||
| 98 | /// Short human name for a (class, subclass) pair. | ||
| 99 | pub fn class_name(class: u8, subclass: u8) -> &'static str { | ||
| 100 | match (class, subclass) { | ||
| 101 | (0x00, 0x01) => "VGA-compatible", | ||
| 102 | (0x00, _) => "Unclassified", | ||
| 103 | (0x01, 0x01) => "IDE controller", | ||
| 104 | (0x01, 0x06) => "SATA controller", | ||
| 105 | (0x01, 0x08) => "NVM controller", | ||
| 106 | (0x01, _) => "Mass storage", | ||
| 107 | (0x02, 0x00) => "Ethernet", | ||
| 108 | (0x02, _) => "Network", | ||
| 109 | (0x03, 0x00) => "VGA", | ||
| 110 | (0x03, _) => "Display", | ||
| 111 | (0x04, _) => "Multimedia", | ||
| 112 | (0x05, _) => "Memory controller", | ||
| 113 | (0x06, 0x00) => "Host bridge", | ||
| 114 | (0x06, 0x01) => "ISA bridge", | ||
| 115 | (0x06, 0x04) => "PCI-PCI bridge", | ||
| 116 | (0x06, _) => "Bridge", | ||
| 117 | (0x08, 0x80) => "PIC", | ||
| 118 | (0x0C, 0x03) => "USB controller", | ||
| 119 | (0x0C, 0x05) => "SMBus", | ||
| 120 | (0x0C, _) => "Serial bus", | ||
| 121 | (0x11, _) => "Signal processing", | ||
| 122 | (0xFF, _) => "Unassigned", | ||
| 123 | _ => "Other", | ||
| 124 | } | ||
| 125 | } | ||
