Skip to content

Commit 2b4fa13

Browse files
committed
uefi: Make PciIoAddress orderable
1 parent 70a63b8 commit 2b4fa13

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

uefi/src/proto/pci/mod.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
//! PCI Bus specific protocols.
44
5+
use core::cmp::Ordering;
6+
57
use uefi_raw::protocol::pci::root_bridge::PciRootBridgeIoProtocolWidth;
68

79
pub mod root_bridge;
810

911
/// IO Address for PCI/register IO operations
1012
#[repr(C, packed)]
11-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1214
pub struct PciIoAddress {
1315
/// Register number within the PCI device.
1416
pub reg: u8,
@@ -54,12 +56,30 @@ impl PciIoAddress {
5456
}
5557
}
5658

59+
impl From<u64> for PciIoAddress {
60+
fn from(value: u64) -> Self {
61+
unsafe { core::mem::transmute(value) }
62+
}
63+
}
64+
5765
impl From<PciIoAddress> for u64 {
5866
fn from(value: PciIoAddress) -> Self {
5967
unsafe { core::mem::transmute(value) }
6068
}
6169
}
6270

71+
impl PartialOrd for PciIoAddress {
72+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
73+
Some(self.cmp(other))
74+
}
75+
}
76+
77+
impl Ord for PciIoAddress {
78+
fn cmp(&self, other: &Self) -> Ordering {
79+
u64::from(*self).cmp(&u64::from(*other))
80+
}
81+
}
82+
6383
/// Trait implemented by all data types that can natively be read from a PCI device.
6484
/// Note: Not all of them have to actually be supported by the hardware at hand.
6585
pub trait PciIoUnit: Sized + Default {}
@@ -95,3 +115,21 @@ fn encode_io_mode_and_unit<U: PciIoUnit>(mode: PciIoMode) -> PciRootBridgeIoProt
95115
_ => unreachable!("Illegal PCI IO-Mode / Unit combination"),
96116
}
97117
}
118+
119+
#[cfg(test)]
120+
mod tests {
121+
122+
#[test]
123+
fn test_pci_ioaddr_raw_conversion() {
124+
let srcaddr = PciIoAddress {
125+
reg: 0x11,
126+
fun: 0x33,
127+
dev: 0x55,
128+
bus: 0x77,
129+
ext_reg: 0x99bbddff,
130+
};
131+
let rawaddr: u64 = srcaddr.into();
132+
let dstaddr = PciIoAddress::from(rawaddr);
133+
assert_eq!(srcaddr, dstaddr);
134+
}
135+
}

0 commit comments

Comments
 (0)