|
| 1 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | + |
| 3 | +//! PCI Bus specific protocols. |
| 4 | +
|
| 5 | +use uefi_raw::protocol::pci::root_bridge::PciRootBridgeIoProtocolWidth; |
| 6 | + |
| 7 | +pub mod root_bridge; |
| 8 | + |
| 9 | +/// IO Address for PCI/register IO operations |
| 10 | +#[repr(C, packed)] |
| 11 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 12 | +pub struct PciIoAddress { |
| 13 | + /// Register number within the PCI device. |
| 14 | + pub reg: u8, |
| 15 | + /// Function number within the PCI device. |
| 16 | + pub fun: u8, |
| 17 | + /// Device number within the PCI bus. |
| 18 | + pub dev: u8, |
| 19 | + /// Bus number in the PCI hierarchy. |
| 20 | + pub bus: u8, |
| 21 | + /// Extended register number within the PCI device. |
| 22 | + pub ext_reg: u32, |
| 23 | +} |
| 24 | + |
| 25 | +impl PciIoAddress { |
| 26 | + /// Create address pointing to the device identified by `bus`, `dev` and `fun` ids. |
| 27 | + #[must_use] |
| 28 | + pub const fn new(bus: u8, dev: u8, fun: u8) -> Self { |
| 29 | + Self { |
| 30 | + bus, |
| 31 | + dev, |
| 32 | + fun, |
| 33 | + reg: 0, |
| 34 | + ext_reg: 0, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// Configure the **byte**-offset of the register to access. |
| 39 | + #[must_use] |
| 40 | + pub const fn with_register(&self, reg: u8) -> Self { |
| 41 | + let mut addr = *self; |
| 42 | + addr.reg = reg; |
| 43 | + addr.ext_reg = 0; |
| 44 | + addr |
| 45 | + } |
| 46 | + |
| 47 | + /// Configure the **byte**-offset of the extended register to access. |
| 48 | + #[must_use] |
| 49 | + pub const fn with_extended_register(&self, ext_reg: u32) -> Self { |
| 50 | + let mut addr = *self; |
| 51 | + addr.reg = 0; |
| 52 | + addr.ext_reg = ext_reg; |
| 53 | + addr |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl From<PciIoAddress> for u64 { |
| 58 | + fn from(value: PciIoAddress) -> Self { |
| 59 | + unsafe { core::mem::transmute(value) } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// Trait implemented by all data types that can natively be read from a PCI device. |
| 64 | +/// Note: Not all of them have to actually be supported by the hardware at hand. |
| 65 | +pub trait PciIoUnit: Sized + Default {} |
| 66 | +impl PciIoUnit for u8 {} |
| 67 | +impl PciIoUnit for u16 {} |
| 68 | +impl PciIoUnit for u32 {} |
| 69 | +impl PciIoUnit for u64 {} |
| 70 | + |
| 71 | +#[allow(dead_code)] |
| 72 | +enum PciIoMode { |
| 73 | + Normal, |
| 74 | + Fifo, |
| 75 | + Fill, |
| 76 | +} |
| 77 | + |
| 78 | +fn encode_io_mode_and_unit<U: PciIoUnit>(mode: PciIoMode) -> PciRootBridgeIoProtocolWidth { |
| 79 | + match (mode, core::mem::size_of::<U>()) { |
| 80 | + (PciIoMode::Normal, 1) => PciRootBridgeIoProtocolWidth::UINT8, |
| 81 | + (PciIoMode::Normal, 2) => PciRootBridgeIoProtocolWidth::UINT16, |
| 82 | + (PciIoMode::Normal, 4) => PciRootBridgeIoProtocolWidth::UINT32, |
| 83 | + (PciIoMode::Normal, 8) => PciRootBridgeIoProtocolWidth::UINT64, |
| 84 | + |
| 85 | + (PciIoMode::Fifo, 1) => PciRootBridgeIoProtocolWidth::FIFO_UINT8, |
| 86 | + (PciIoMode::Fifo, 2) => PciRootBridgeIoProtocolWidth::FIFO_UINT16, |
| 87 | + (PciIoMode::Fifo, 4) => PciRootBridgeIoProtocolWidth::FIFO_UINT32, |
| 88 | + (PciIoMode::Fifo, 8) => PciRootBridgeIoProtocolWidth::FIFO_UINT64, |
| 89 | + |
| 90 | + (PciIoMode::Fill, 1) => PciRootBridgeIoProtocolWidth::FILL_UINT8, |
| 91 | + (PciIoMode::Fill, 2) => PciRootBridgeIoProtocolWidth::FILL_UINT16, |
| 92 | + (PciIoMode::Fill, 4) => PciRootBridgeIoProtocolWidth::FILL_UINT32, |
| 93 | + (PciIoMode::Fill, 8) => PciRootBridgeIoProtocolWidth::FILL_UINT64, |
| 94 | + |
| 95 | + _ => unreachable!("Illegal PCI IO-Mode / Unit combination"), |
| 96 | + } |
| 97 | +} |
0 commit comments