|
| 1 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | + |
| 3 | +//! PCI Bus device function and bridge enumeration. |
| 4 | +
|
| 5 | +use core::mem; |
| 6 | + |
| 7 | +use alloc::collections::btree_set::BTreeSet; |
| 8 | + |
| 9 | +use crate::proto::pci::{FullPciIoAddress, PciIoAddress, root_bridge::PciRootBridgeIo}; |
| 10 | + |
| 11 | +#[allow(unused)] |
| 12 | +#[derive(Clone, Copy, Debug)] |
| 13 | +struct PciRegister0 { |
| 14 | + vendor_id: u16, |
| 15 | + device_id: u16, |
| 16 | +} |
| 17 | + |
| 18 | +#[allow(unused)] |
| 19 | +#[derive(Clone, Copy, Debug)] |
| 20 | +struct PciRegister2 { |
| 21 | + revision_id: u8, |
| 22 | + prog_if: u8, |
| 23 | + subclass: u8, |
| 24 | + class: u8, |
| 25 | +} |
| 26 | + |
| 27 | +#[allow(unused)] |
| 28 | +#[derive(Clone, Copy, Debug)] |
| 29 | +struct PciRegister3 { |
| 30 | + cache_line_size: u8, |
| 31 | + latency_timer: u8, |
| 32 | + header_type: u8, |
| 33 | + bist: u8, |
| 34 | +} |
| 35 | + |
| 36 | +#[allow(unused)] |
| 37 | +#[derive(Clone, Copy, Debug)] |
| 38 | +struct PciHeader1Register6 { |
| 39 | + secondary_latency_timer: u8, |
| 40 | + subordinate_bus: u8, |
| 41 | + secondary_bus: u8, |
| 42 | + primary_bus: u8, |
| 43 | +} |
| 44 | + |
| 45 | +/// Read the 4byte pci register with the given `addr` and cast it into the given structured representation. |
| 46 | +fn read_device_register_u32<T: Sized + Copy>( |
| 47 | + proto: &mut PciRootBridgeIo, |
| 48 | + addr: PciIoAddress, |
| 49 | +) -> uefi::Result<T> { |
| 50 | + unsafe { |
| 51 | + let raw = proto.pci().read_one::<u32>(addr)?; |
| 52 | + let reg: T = mem::transmute_copy(&raw); |
| 53 | + Ok(reg) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// ########################################################################################## |
| 58 | +// # Query Helpers (read from a device's configuration registers) |
| 59 | + |
| 60 | +fn get_vendor_id(proto: &mut PciRootBridgeIo, addr: PciIoAddress) -> uefi::Result<u16> { |
| 61 | + read_device_register_u32::<PciRegister0>(proto, addr.with_register(0 * 4)).map(|v| v.vendor_id) |
| 62 | +} |
| 63 | + |
| 64 | +fn get_classes(proto: &mut PciRootBridgeIo, addr: PciIoAddress) -> uefi::Result<(u8, u8)> { |
| 65 | + let reg = read_device_register_u32::<PciRegister2>(proto, addr.with_register(2 * 4))?; |
| 66 | + Ok((reg.class, reg.subclass)) |
| 67 | +} |
| 68 | + |
| 69 | +fn get_header_type(proto: &mut PciRootBridgeIo, addr: PciIoAddress) -> uefi::Result<u8> { |
| 70 | + read_device_register_u32::<PciRegister3>(proto, addr.with_register(3 * 4)) |
| 71 | + .map(|v| v.header_type) |
| 72 | +} |
| 73 | + |
| 74 | +fn get_secondary_bus_range( |
| 75 | + proto: &mut PciRootBridgeIo, |
| 76 | + addr: PciIoAddress, |
| 77 | +) -> uefi::Result<(u8, u8)> { |
| 78 | + let reg = read_device_register_u32::<PciHeader1Register6>(proto, addr.with_register(6 * 4))?; |
| 79 | + Ok((reg.secondary_bus, reg.subordinate_bus)) |
| 80 | +} |
| 81 | + |
| 82 | +// ########################################################################################## |
| 83 | +// # Recursive visitor implementation |
| 84 | + |
| 85 | +fn visit_function( |
| 86 | + proto: &mut PciRootBridgeIo, |
| 87 | + addr: PciIoAddress, |
| 88 | + queue: &mut BTreeSet<FullPciIoAddress>, |
| 89 | +) -> uefi::Result<()> { |
| 90 | + if get_vendor_id(proto, addr)? == 0xFFFF { |
| 91 | + return Ok(()); // function doesn't exist - bail instantly |
| 92 | + } |
| 93 | + queue.insert(FullPciIoAddress::new(proto.segment_nr(), addr)); |
| 94 | + let (base_class, sub_class) = get_classes(proto, addr)?; |
| 95 | + if base_class == 0x6 && sub_class == 0x4 && get_header_type(proto, addr)? == 0x01 { |
| 96 | + // This is a PCI-to-PCI bridge controller. The current `addr` is the address with which it's |
| 97 | + // mounted in the PCI tree we are currently traversing. Now we query its header, where |
| 98 | + // the bridge tells us a range of addresses [secondary;subordinate], with which the other |
| 99 | + // side of the bridge is mounted into the PCI tree. |
| 100 | + let (secondary_bus_nr, subordinate_bus_nr) = get_secondary_bus_range(proto, addr)?; |
| 101 | + if secondary_bus_nr == 0 || subordinate_bus_nr < secondary_bus_nr { |
| 102 | + // If the secondary bus number is the root number, or if the range is invalid - this hardware |
| 103 | + // is so horribly broken that we refrain from touching it. It might explode - or worse! |
| 104 | + return Ok(()); |
| 105 | + } |
| 106 | + for bus in secondary_bus_nr..=subordinate_bus_nr { |
| 107 | + // Recurse into the bus namespaces on the other side of the bridge |
| 108 | + visit_bus(proto, PciIoAddress::new(bus, 0, 0), queue)?; |
| 109 | + } |
| 110 | + } |
| 111 | + Ok(()) |
| 112 | +} |
| 113 | + |
| 114 | +fn visit_device( |
| 115 | + proto: &mut PciRootBridgeIo, |
| 116 | + addr: PciIoAddress, |
| 117 | + queue: &mut BTreeSet<FullPciIoAddress>, |
| 118 | +) -> uefi::Result<()> { |
| 119 | + if get_vendor_id(proto, addr)? == 0xFFFF { |
| 120 | + return Ok(()); // device doesn't exist |
| 121 | + } |
| 122 | + visit_function(proto, addr.with_function(0), queue)?; |
| 123 | + if get_header_type(proto, addr.with_function(0))? & 0x80 != 0 { |
| 124 | + // This is a multi-function device - also try the remaining functions [1;7] |
| 125 | + // These remaining functions can be sparsely populated - as long as function 0 exists. |
| 126 | + for fun in 1..=7 { |
| 127 | + visit_function(proto, addr.with_function(fun), queue)?; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + Ok(()) |
| 132 | +} |
| 133 | + |
| 134 | +pub(crate) fn visit_bus( |
| 135 | + proto: &mut PciRootBridgeIo, |
| 136 | + addr: PciIoAddress, |
| 137 | + queue: &mut BTreeSet<FullPciIoAddress>, |
| 138 | +) -> uefi::Result<()> { |
| 139 | + // Given a valid bus entry point - simply try all possible devices addresses |
| 140 | + for dev in 0..32 { |
| 141 | + visit_device(proto, addr.with_device(dev), queue)?; |
| 142 | + } |
| 143 | + Ok(()) |
| 144 | +} |
0 commit comments