ostd/bus/pci/device_info.rs
1// SPDX-License-Identifier: MPL-2.0
2//! PCI device Information
3use vstd::prelude::*;
4
5use core::iter;
6
7// use super::cfg_space::PciDeviceCommonCfgOffset;
8
9/*
10/// PCI device ID
11#[derive(Debug, Copy, Clone, PartialEq, Eq)]
12pub struct PciDeviceId {
13 /// Vendor ID
14 pub vendor_id: u16,
15 /// Device ID
16 pub device_id: u16,
17 /// Revision ID
18 pub revision_id: u8,
19 /// Programming Interface Byte
20 pub prog_if: u8,
21 /// Specifies the specific function the device performs.
22 pub subclass: u8,
23 /// Specifies the type of function the device performs.
24 pub class: u8,
25 /// Subsystem Vendor ID
26 pub subsystem_vendor_id: u16,
27 /// Subsystem ID
28 pub subsystem_id: u16,
29}
30
31/*
32impl PciDeviceId {
33 pub(super) fn new(location: PciDeviceLocation) -> Self {
34 let vendor_id = location.read16(PciDeviceCommonCfgOffset::VendorId as u16);
35 let device_id = location.read16(PciDeviceCommonCfgOffset::DeviceId as u16);
36 let revision_id = location.read8(PciDeviceCommonCfgOffset::RevisionId as u16);
37 let prog_if = location.read8(PciDeviceCommonCfgOffset::ClassCode as u16);
38 let subclass = location.read8(PciDeviceCommonCfgOffset::ClassCode as u16 + 1);
39 let class = location.read8(PciDeviceCommonCfgOffset::ClassCode as u16 + 2);
40 let subsystem_vendor_id =
41 location.read16(PciDeviceCommonCfgOffset::SubsystemVendorId as u16);
42 let subsystem_id = location.read16(PciDeviceCommonCfgOffset::SubsystemId as u16);
43 Self {
44 vendor_id,
45 device_id,
46 revision_id,
47 prog_if,
48 subclass,
49 class,
50 subsystem_vendor_id,
51 subsystem_id,
52 }
53 }
54}
55*/
56
57*/
58
59/// PCI device Location
60#[verus_verify]
61#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
62pub struct PciDeviceLocation {
63 /// Bus number
64 pub bus: u8,
65 /// Device number with max 31
66 pub device: u8,
67 /// Device number with max 7
68 pub function: u8,
69}
70
71impl PciDeviceLocation {
72 const MIN_BUS: u8 = 0;
73 const MAX_BUS: u8 = 255;
74 const MIN_DEVICE: u8 = 0;
75 const MAX_DEVICE: u8 = 31;
76 const MIN_FUNCTION: u8 = 0;
77 const MAX_FUNCTION: u8 = 7;
78
79 /// Returns an iterator that enumerates all possible PCI device locations.
80 pub fn all() -> impl Iterator<Item = PciDeviceLocation> {
81 iter::from_coroutine(
82 #[coroutine]
83 || {
84 for bus in Self::MIN_BUS..=Self::MAX_BUS {
85 for device in Self::MIN_DEVICE..=Self::MAX_DEVICE {
86 for function in Self::MIN_FUNCTION..=Self::MAX_FUNCTION {
87 let loc = PciDeviceLocation {
88 bus,
89 device,
90 function,
91 };
92 yield loc;
93 }
94 }
95 }
96 },
97 )
98 }
99}
100
101#[verus_verify]
102impl PciDeviceLocation {
103 /// The page table of all devices is the same. So we can use any device ID.
104 /// FIXME: distinguish different device id.
105 #[verus_verify(dual_spec)]
106 #[verus_spec(returns Self::zero())]
107 pub fn zero() -> Self {
108 Self {
109 bus: 0,
110 device: 0,
111 function: 0,
112 }
113 }
114}
115
116/*
117impl PciDeviceLocation {
118 pub(super) const BIT32_ALIGN_MASK: u16 = 0xFFFC;
119
120 pub(super) fn read8(&self, offset: u16) -> u8 {
121 let val = self.read32(offset & Self::BIT32_ALIGN_MASK);
122 ((val >> ((offset as usize & 0b11) << 3)) & 0xFF) as u8
123 }
124
125 pub(super) fn read16(&self, offset: u16) -> u16 {
126 let val = self.read32(offset & Self::BIT32_ALIGN_MASK);
127 ((val >> ((offset as usize & 0b10) << 3)) & 0xFFFF) as u16
128 }
129
130 pub(super) fn read32(&self, offset: u16) -> u32 {
131 debug_assert!(
132 (offset & 0b11) == 0,
133 "misaligned PCI configuration dword u32 read"
134 );
135 crate::arch::pci::read32(self, offset as u32).unwrap()
136 }
137
138 pub(super) fn write8(&self, offset: u16, val: u8) {
139 let old = self.read32(offset & Self::BIT32_ALIGN_MASK);
140 let dest = (offset as usize & 0b11) << 3;
141 let mask = (0xFF << dest) as u32;
142 self.write32(
143 offset & Self::BIT32_ALIGN_MASK,
144 (((val as u32) << dest) | (old & !mask)).to_le(),
145 );
146 }
147
148 pub(super) fn write16(&self, offset: u16, val: u16) {
149 let old = self.read32(offset & Self::BIT32_ALIGN_MASK);
150 let dest = (offset as usize & 0b10) << 3;
151 let mask = (0xFFFF << dest) as u32;
152 self.write32(
153 offset & Self::BIT32_ALIGN_MASK,
154 (((val as u32) << dest) | (old & !mask)).to_le(),
155 );
156 }
157
158 pub(super) fn write32(&self, offset: u16, val: u32) {
159 debug_assert!(
160 (offset & 0b11) == 0,
161 "misaligned PCI configuration dword u32 write"
162 );
163 crate::arch::pci::write32(self, offset as u32, val).unwrap()
164 }
165}
166*/