aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / pci / mod.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! PCI Bus specific protocols. |
| 4 | |
| 5 | use core::cmp::Ordering; |
| 6 | |
| 7 | use uefi_raw::protocol::pci::root_bridge::PciRootBridgeIoProtocolWidth; |
| 8 | |
| 9 | pub mod configuration; |
| 10 | #[cfg(feature = "alloc")] |
| 11 | pub mod enumeration; |
| 12 | pub mod root_bridge; |
| 13 | |
| 14 | /// IO Address for PCI/register IO operations |
| 15 | #[repr(C, packed)] |
| 16 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 17 | pub struct PciIoAddress { |
| 18 | /// Register number within the PCI device. |
| 19 | pub reg: u8, |
| 20 | /// Function number within the PCI device. |
| 21 | pub fun: u8, |
| 22 | /// Device number within the PCI bus. |
| 23 | pub dev: u8, |
| 24 | /// Bus number in the PCI hierarchy. |
| 25 | pub bus: u8, |
| 26 | /// Extended register number within the PCI device. |
| 27 | pub ext_reg: u32, |
| 28 | } |
| 29 | |
| 30 | // The address is converted to/from a raw u64 value; ensure both have the |
| 31 | // same size. |
| 32 | const _: () = assert!(size_of::<PciIoAddress>() == size_of::<u64>()); |
| 33 | |
| 34 | impl PciIoAddress { |
| 35 | /// Create address pointing to the device identified by `bus`, `dev` and `fun` ids. |
| 36 | #[must_use] |
| 37 | pub const fn new(bus: u8, dev: u8, fun: u8) -> Self { |
| 38 | Self { |
| 39 | bus, |
| 40 | dev, |
| 41 | fun, |
| 42 | reg: 0, |
| 43 | ext_reg: 0, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// Construct a new address with the bus address set to the given value |
| 48 | #[must_use] |
| 49 | pub const fn with_bus(&self, bus: u8) -> Self { |
| 50 | let mut addr = *self; |
| 51 | addr.bus = bus; |
| 52 | addr |
| 53 | } |
| 54 | |
| 55 | /// Construct a new address with the device address set to the given value |
| 56 | #[must_use] |
| 57 | pub const fn with_device(&self, dev: u8) -> Self { |
| 58 | let mut addr = *self; |
| 59 | addr.dev = dev; |
| 60 | addr |
| 61 | } |
| 62 | |
| 63 | /// Construct a new address with the function address set to the given value |
| 64 | #[must_use] |
| 65 | pub const fn with_function(&self, fun: u8) -> Self { |
| 66 | let mut addr = *self; |
| 67 | addr.fun = fun; |
| 68 | addr |
| 69 | } |
| 70 | |
| 71 | /// Configure the **byte**-offset of the register to access. |
| 72 | #[must_use] |
| 73 | pub const fn with_register(&self, reg: u8) -> Self { |
| 74 | let mut addr = *self; |
| 75 | addr.reg = reg; |
| 76 | addr.ext_reg = 0; |
| 77 | addr |
| 78 | } |
| 79 | |
| 80 | /// Configure the **byte**-offset of the extended register to access. |
| 81 | #[must_use] |
| 82 | pub const fn with_extended_register(&self, ext_reg: u32) -> Self { |
| 83 | let mut addr = *self; |
| 84 | addr.reg = 0; |
| 85 | addr.ext_reg = ext_reg; |
| 86 | addr |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | impl From<u64> for PciIoAddress { |
| 91 | fn from(value: u64) -> Self { |
| 92 | let raw = value.to_ne_bytes(); |
| 93 | Self { |
| 94 | reg: raw[0], |
| 95 | fun: raw[1], |
| 96 | dev: raw[2], |
| 97 | bus: raw[3], |
| 98 | ext_reg: u32::from_ne_bytes([raw[4], raw[5], raw[6], raw[7]]), |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl From<PciIoAddress> for u64 { |
| 104 | fn from(value: PciIoAddress) -> Self { |
| 105 | let ereg = value.ext_reg.to_ne_bytes(); |
| 106 | Self::from_ne_bytes([ |
| 107 | value.reg, value.fun, value.dev, value.bus, ereg[0], ereg[1], ereg[2], ereg[3], |
| 108 | ]) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | impl PartialOrd for PciIoAddress { |
| 113 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 114 | Some(self.cmp(other)) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | impl Ord for PciIoAddress { |
| 119 | fn cmp(&self, o: &Self) -> Ordering { |
| 120 | // extract fields because taking references to unaligned fields in packed structs is a nono |
| 121 | let (bus, dev, fun, reg, ext_reg) = (self.bus, self.dev, self.fun, self.reg, self.ext_reg); |
| 122 | let (o_bus, o_dev, o_fun, o_reg, o_ext_reg) = (o.bus, o.dev, o.fun, o.reg, o.ext_reg); |
| 123 | bus.cmp(&o_bus) |
| 124 | .then(dev.cmp(&o_dev)) |
| 125 | .then(fun.cmp(&o_fun)) |
| 126 | .then(reg.cmp(&o_reg)) |
| 127 | .then(ext_reg.cmp(&o_ext_reg)) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // ############################################################################################ |
| 132 | |
| 133 | /// Trait implemented by all data types that can natively be read from a PCI device. |
| 134 | /// Note: Not all of them have to actually be supported by the hardware at hand. |
| 135 | pub trait PciIoUnit: Sized + Default {} |
| 136 | impl PciIoUnit for u8 {} |
| 137 | impl PciIoUnit for u16 {} |
| 138 | impl PciIoUnit for u32 {} |
| 139 | impl PciIoUnit for u64 {} |
| 140 | |
| 141 | enum PciIoMode { |
| 142 | Normal, |
| 143 | Fifo, |
| 144 | Fill, |
| 145 | } |
| 146 | |
| 147 | fn encode_io_mode_and_unit<U: PciIoUnit>(mode: PciIoMode) -> PciRootBridgeIoProtocolWidth { |
| 148 | match (mode, size_of::<U>()) { |
| 149 | (PciIoMode::Normal, 1) => PciRootBridgeIoProtocolWidth::UINT8, |
| 150 | (PciIoMode::Normal, 2) => PciRootBridgeIoProtocolWidth::UINT16, |
| 151 | (PciIoMode::Normal, 4) => PciRootBridgeIoProtocolWidth::UINT32, |
| 152 | (PciIoMode::Normal, 8) => PciRootBridgeIoProtocolWidth::UINT64, |
| 153 | |
| 154 | (PciIoMode::Fifo, 1) => PciRootBridgeIoProtocolWidth::FIFO_UINT8, |
| 155 | (PciIoMode::Fifo, 2) => PciRootBridgeIoProtocolWidth::FIFO_UINT16, |
| 156 | (PciIoMode::Fifo, 4) => PciRootBridgeIoProtocolWidth::FIFO_UINT32, |
| 157 | (PciIoMode::Fifo, 8) => PciRootBridgeIoProtocolWidth::FIFO_UINT64, |
| 158 | |
| 159 | (PciIoMode::Fill, 1) => PciRootBridgeIoProtocolWidth::FILL_UINT8, |
| 160 | (PciIoMode::Fill, 2) => PciRootBridgeIoProtocolWidth::FILL_UINT16, |
| 161 | (PciIoMode::Fill, 4) => PciRootBridgeIoProtocolWidth::FILL_UINT32, |
| 162 | (PciIoMode::Fill, 8) => PciRootBridgeIoProtocolWidth::FILL_UINT64, |
| 163 | |
| 164 | _ => unreachable!("Illegal PCI IO-Mode / Unit combination"), |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | #[cfg(test)] |
| 169 | mod tests { |
| 170 | use core::cmp::Ordering; |
| 171 | |
| 172 | use super::PciIoAddress; |
| 173 | |
| 174 | #[test] |
| 175 | #[expect(clippy::unusual_byte_groupings)] |
| 176 | fn test_pci_ioaddr_raw_conversion() { |
| 177 | let srcaddr = PciIoAddress { |
| 178 | reg: 0x11, |
| 179 | fun: 0x33, |
| 180 | dev: 0x55, |
| 181 | bus: 0x77, |
| 182 | ext_reg: 0x99bbddff, |
| 183 | }; |
| 184 | let rawaddr: u64 = srcaddr.into(); |
| 185 | let dstaddr = PciIoAddress::from(rawaddr); |
| 186 | assert_eq!(rawaddr, 0x99_bb_dd_ff_7755_3311); |
| 187 | assert_eq!(srcaddr, dstaddr); |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn test_pci_order() { |
| 192 | let addr0_0_0 = PciIoAddress::new(0, 0, 0); |
| 193 | let addr0_0_1 = PciIoAddress::new(0, 0, 1); |
| 194 | let addr0_1_0 = PciIoAddress::new(0, 1, 0); |
| 195 | let addr1_0_0 = PciIoAddress::new(1, 0, 0); |
| 196 | |
| 197 | assert_eq!(addr0_0_0.cmp(&addr0_0_0), Ordering::Equal); |
| 198 | assert_eq!(addr0_0_0.cmp(&addr0_0_1), Ordering::Less); |
| 199 | assert_eq!(addr0_0_0.cmp(&addr0_1_0), Ordering::Less); |
| 200 | assert_eq!(addr0_0_0.cmp(&addr1_0_0), Ordering::Less); |
| 201 | |
| 202 | assert_eq!(addr0_0_1.cmp(&addr0_0_0), Ordering::Greater); |
| 203 | assert_eq!(addr0_0_1.cmp(&addr0_0_1), Ordering::Equal); |
| 204 | assert_eq!(addr0_0_1.cmp(&addr0_1_0), Ordering::Less); |
| 205 | assert_eq!(addr0_0_1.cmp(&addr1_0_0), Ordering::Less); |
| 206 | |
| 207 | assert_eq!(addr0_1_0.cmp(&addr0_0_0), Ordering::Greater); |
| 208 | assert_eq!(addr0_1_0.cmp(&addr0_0_1), Ordering::Greater); |
| 209 | assert_eq!(addr0_1_0.cmp(&addr0_1_0), Ordering::Equal); |
| 210 | assert_eq!(addr0_1_0.cmp(&addr1_0_0), Ordering::Less); |
| 211 | |
| 212 | assert_eq!(addr1_0_0.cmp(&addr0_0_0), Ordering::Greater); |
| 213 | assert_eq!(addr1_0_0.cmp(&addr0_0_1), Ordering::Greater); |
| 214 | assert_eq!(addr1_0_0.cmp(&addr0_1_0), Ordering::Greater); |
| 215 | assert_eq!(addr1_0_0.cmp(&addr1_0_0), Ordering::Equal); |
| 216 | |
| 217 | assert_eq!(addr0_0_0.cmp(&addr0_0_0.with_register(1)), Ordering::Less); |
| 218 | assert_eq!( |
| 219 | addr0_0_0.with_register(1).cmp(&addr0_0_0), |
| 220 | Ordering::Greater |
| 221 | ); |
| 222 | assert_eq!( |
| 223 | addr0_0_0.cmp(&addr0_0_0.with_extended_register(1)), |
| 224 | Ordering::Less |
| 225 | ); |
| 226 | assert_eq!( |
| 227 | addr0_0_0.with_extended_register(1).cmp(&addr0_0_0), |
| 228 | Ordering::Greater |
| 229 | ); |
| 230 | } |
| 231 | } |