Skip to content

Commit f4a0a63

Browse files
committed
x86_64/pci: reformat and fix clippy warnings
1 parent 58918f3 commit f4a0a63

File tree

4 files changed

+68
-51
lines changed

4 files changed

+68
-51
lines changed

src/arch/aarch64/kernel/pci.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ const PCI_MAX_FUNCTION_NUMBER: u8 = 8;
2222
#[derive(Debug, Copy, Clone)]
2323
pub(crate) struct PciConfigRegion(VirtAddr);
2424

25+
// Compatibility with x86_64
26+
pub(crate) use PciConfigRegion as PciConfigAccess;
27+
2528
impl PciConfigRegion {
2629
pub const fn new(addr: VirtAddr) -> Self {
2730
assert!(

src/arch/riscv64/kernel/pci.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use pci_types::{ConfigRegionAccess, PciAddress};
33
#[derive(Debug, Copy, Clone)]
44
pub struct PciConfigRegion;
55

6+
// Compatibility with x86_64
7+
pub use PciConfigRegion as PciConfigAccess;
8+
69
impl ConfigRegionAccess for PciConfigRegion {
710
unsafe fn read(&self, addr: PciAddress, offset: u16) -> u32 {
811
warn!("pci_config_region.read({addr}, {offset}) called but not implemented");

src/arch/x86_64/kernel/acpi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ pub fn init() {
561561
verify_checksum(table.header_start_address(), table.header.length as usize).is_ok(),
562562
"MCFG at {table_physical_address:p} has invalid checksum"
563563
);
564+
#[cfg(feature = "pci")]
564565
MCFG.set(table).unwrap();
565566
}
566567
}

src/arch/x86_64/kernel/pci.rs

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const CONFIG_ADDRESS: Port<u32> = Port::new(0xcf8);
1212
const CONFIG_DATA: Port<u32> = Port::new(0xcfc);
1313

1414
#[derive(Debug, Copy, Clone)]
15-
struct PciConfigRegion;
15+
pub struct PciConfigRegion;
1616

1717
impl PciConfigRegion {
1818
pub const fn new() -> Self {
@@ -30,15 +30,21 @@ pub enum PciConfigAccess {
3030
impl ConfigRegionAccess for PciConfigAccess {
3131
unsafe fn read(&self, address: PciAddress, offset: u16) -> u32 {
3232
match self {
33-
PciConfigAccess::PciConfigRegion(entry) => entry.read(address, offset),
34-
PciConfigAccess::PcieConfigRegion(entry) => entry.read(address, offset),
33+
PciConfigAccess::PciConfigRegion(entry) => unsafe { entry.read(address, offset) },
34+
#[cfg(feature = "acpi")]
35+
PciConfigAccess::PcieConfigRegion(entry) => unsafe { entry.read(address, offset) },
3536
}
3637
}
3738

3839
unsafe fn write(&self, address: PciAddress, offset: u16, value: u32) {
3940
match self {
40-
PciConfigAccess::PciConfigRegion(entry) => entry.write(address, offset, value),
41-
PciConfigAccess::PcieConfigRegion(entry) => entry.write(address, offset, value),
41+
PciConfigAccess::PciConfigRegion(entry) => unsafe {
42+
entry.write(address, offset, value);
43+
},
44+
#[cfg(feature = "acpi")]
45+
PciConfigAccess::PcieConfigRegion(entry) => unsafe {
46+
entry.write(address, offset, value);
47+
},
4248
}
4349
}
4450
}
@@ -83,9 +89,15 @@ pub(crate) fn init() {
8389
debug!("Scanning PCI Busses 0 to {}", PCI_MAX_BUS_NUMBER - 1);
8490

8591
#[cfg(feature = "acpi")]
86-
if pcie::init_pcie() { return; }
92+
if pcie::init_pcie() {
93+
return;
94+
}
8795

88-
enumerate_devices(0, PCI_MAX_BUS_NUMBER, PciConfigAccess::PciConfigRegion(PciConfigRegion::new()))
96+
enumerate_devices(
97+
0,
98+
PCI_MAX_BUS_NUMBER,
99+
PciConfigAccess::PciConfigRegion(PciConfigRegion::new()),
100+
);
89101
}
90102

91103
fn enumerate_devices(bus_start: u8, bus_end: u8, access: PciConfigAccess) {
@@ -108,26 +120,29 @@ fn enumerate_devices(bus_start: u8, bus_end: u8, access: PciConfigAccess) {
108120

109121
#[cfg(feature = "acpi")]
110122
mod pcie {
111-
use core::ptr;
123+
use memory_addresses::PhysAddr;
112124
use pci_types::{ConfigRegionAccess, PciAddress};
113-
use memory_addresses::{PhysAddr, VirtAddr};
114-
use super::{PciConfigAccess, PCI_MAX_BUS_NUMBER};
115-
use crate::env;
125+
126+
use super::{PCI_MAX_BUS_NUMBER, PciConfigAccess};
116127
use crate::env::kernel::acpi;
117128

118129
pub fn init_pcie() -> bool {
119-
let Some(table) = acpi::get_mcfg_table() else { return false; };
130+
let Some(table) = acpi::get_mcfg_table() else {
131+
return false;
132+
};
120133

121-
let mut start_addr: *const McfgTableEntry = core::ptr::with_exposed_provenance(table.table_start_address() + 8);
122-
let end_addr: *const McfgTableEntry = core::ptr::with_exposed_provenance(table.table_end_address() + 8);
134+
let mut start_addr: *const McfgTableEntry =
135+
core::ptr::with_exposed_provenance(table.table_start_address() + 8);
136+
let end_addr: *const McfgTableEntry =
137+
core::ptr::with_exposed_provenance(table.table_end_address() + 8);
123138

124139
if start_addr == end_addr {
125140
return false;
126141
}
127142

128143
while start_addr < end_addr {
129144
unsafe {
130-
let read = ptr::read_unaligned(start_addr);
145+
let read = core::ptr::read_unaligned(start_addr);
131146
init_pcie_bus(read);
132147
start_addr = start_addr.add(1);
133148
}
@@ -136,58 +151,41 @@ mod pcie {
136151
true
137152
}
138153

139-
#[derive(Debug)]
140-
#[repr(C)]
141-
struct PcieDeviceConfig {
142-
vendor_id: u16,
143-
device_id: u16,
144-
_reserved: [u8; 4096 - 8]
145-
}
146-
147154
#[derive(Debug, Copy, Clone)]
148155
#[repr(C)]
149156
pub(crate) struct McfgTableEntry {
150157
pub base_address: u64,
151158
pub pci_segment_number: u16,
152159
pub start_pci_bus: u8,
153160
pub end_pci_bus: u8,
154-
_reserved: u32
161+
_reserved: u32,
155162
}
156163

157164
impl McfgTableEntry {
158-
pub fn pci_config_space_address(&self, bus_number: u8, device: u8, function: u8) -> PhysAddr {
165+
pub fn pci_config_space_address(
166+
&self,
167+
bus_number: u8,
168+
device: u8,
169+
function: u8,
170+
) -> PhysAddr {
159171
PhysAddr::new(
160-
self.base_address +
161-
((bus_number as u64) << 20) |
162-
(((device as u64) & 0x1f) << 15) |
163-
(((function as u64) & 0x7) << 12)
172+
self.base_address
173+
+ ((u64::from(bus_number) << 20)
174+
| ((u64::from(device) & 0x1f) << 15)
175+
| ((u64::from(function) & 0x7) << 12)),
164176
)
165177
}
166178
}
167179

168-
#[derive(Debug)]
169-
#[cfg(feature = "pci")]
170-
struct McfgTable(alloc::vec::Vec<McfgTableEntry>);
171-
172-
impl PcieDeviceConfig {
173-
fn get<'a>(physical_address: PhysAddr) -> &'a Self {
174-
assert!(env::is_uefi());
175-
176-
// For UEFI Systems, the tables are already mapped so we only need to return a proper reference to the table
177-
let allocated_virtual_address = VirtAddr::new(physical_address.as_u64());
178-
let ptr: *const PcieDeviceConfig = allocated_virtual_address.as_ptr();
179-
180-
unsafe { ptr.as_ref().unwrap() }
181-
}
182-
}
183-
184180
impl ConfigRegionAccess for McfgTableEntry {
185181
unsafe fn read(&self, address: PciAddress, offset: u16) -> u32 {
186182
assert_eq!(address.segment(), self.pci_segment_number);
187183
assert!(address.bus() >= self.start_pci_bus);
188184
assert!(address.bus() <= self.end_pci_bus);
189185

190-
let ptr = self.pci_config_space_address(address.bus(), address.device(), address.function()) + offset as u64;
186+
let ptr =
187+
self.pci_config_space_address(address.bus(), address.device(), address.function())
188+
+ u64::from(offset);
191189
let ptr = ptr.as_usize() as *const u32;
192190

193191
unsafe { *ptr }
@@ -198,10 +196,14 @@ mod pcie {
198196
assert!(address.bus() >= self.start_pci_bus);
199197
assert!(address.bus() <= self.end_pci_bus);
200198

201-
let ptr = self.pci_config_space_address(address.bus(), address.device(), address.function()) + offset as u64;
199+
let ptr =
200+
self.pci_config_space_address(address.bus(), address.device(), address.function())
201+
+ u64::from(offset);
202202
let ptr = ptr.as_usize() as *mut u32;
203203

204-
unsafe { *ptr = value; }
204+
unsafe {
205+
*ptr = value;
206+
}
205207
}
206208
}
207209

@@ -210,7 +212,15 @@ mod pcie {
210212
return;
211213
}
212214

213-
let end = if bus_entry.end_pci_bus > PCI_MAX_BUS_NUMBER { PCI_MAX_BUS_NUMBER } else { bus_entry.end_pci_bus };
214-
super::enumerate_devices(bus_entry.start_pci_bus, end, PciConfigAccess::PcieConfigRegion(bus_entry));
215+
let end = if bus_entry.end_pci_bus > PCI_MAX_BUS_NUMBER {
216+
PCI_MAX_BUS_NUMBER
217+
} else {
218+
bus_entry.end_pci_bus
219+
};
220+
super::enumerate_devices(
221+
bus_entry.start_pci_bus,
222+
end,
223+
PciConfigAccess::PcieConfigRegion(bus_entry),
224+
);
215225
}
216-
}
226+
}

0 commit comments

Comments
 (0)