Skip to content

Commit d8c07ce

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

File tree

4 files changed

+65
-50
lines changed

4 files changed

+65
-50
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: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,19 @@ 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+
PciConfigAccess::PcieConfigRegion(entry) => unsafe { entry.read(address, offset) },
3535
}
3636
}
3737

3838
unsafe fn write(&self, address: PciAddress, offset: u16, value: u32) {
3939
match self {
40-
PciConfigAccess::PciConfigRegion(entry) => entry.write(address, offset, value),
41-
PciConfigAccess::PcieConfigRegion(entry) => entry.write(address, offset, value),
40+
PciConfigAccess::PciConfigRegion(entry) => unsafe {
41+
entry.write(address, offset, value);
42+
},
43+
PciConfigAccess::PcieConfigRegion(entry) => unsafe {
44+
entry.write(address, offset, value);
45+
},
4246
}
4347
}
4448
}
@@ -83,9 +87,15 @@ pub(crate) fn init() {
8387
debug!("Scanning PCI Busses 0 to {}", PCI_MAX_BUS_NUMBER - 1);
8488

8589
#[cfg(feature = "acpi")]
86-
if pcie::init_pcie() { return; }
90+
if pcie::init_pcie() {
91+
return;
92+
}
8793

88-
enumerate_devices(0, PCI_MAX_BUS_NUMBER, PciConfigAccess::PciConfigRegion(PciConfigRegion::new()))
94+
enumerate_devices(
95+
0,
96+
PCI_MAX_BUS_NUMBER,
97+
PciConfigAccess::PciConfigRegion(PciConfigRegion::new()),
98+
);
8999
}
90100

91101
fn enumerate_devices(bus_start: u8, bus_end: u8, access: PciConfigAccess) {
@@ -108,26 +118,29 @@ fn enumerate_devices(bus_start: u8, bus_end: u8, access: PciConfigAccess) {
108118

109119
#[cfg(feature = "acpi")]
110120
mod pcie {
111-
use core::ptr;
121+
use memory_addresses::PhysAddr;
112122
use pci_types::{ConfigRegionAccess, PciAddress};
113-
use memory_addresses::{PhysAddr, VirtAddr};
114-
use super::{PciConfigAccess, PCI_MAX_BUS_NUMBER};
115-
use crate::env;
123+
124+
use super::{PCI_MAX_BUS_NUMBER, PciConfigAccess};
116125
use crate::env::kernel::acpi;
117126

118127
pub fn init_pcie() -> bool {
119-
let Some(table) = acpi::get_mcfg_table() else { return false; };
128+
let Some(table) = acpi::get_mcfg_table() else {
129+
return false;
130+
};
120131

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);
132+
let mut start_addr: *const McfgTableEntry =
133+
core::ptr::with_exposed_provenance(table.table_start_address() + 8);
134+
let end_addr: *const McfgTableEntry =
135+
core::ptr::with_exposed_provenance(table.table_end_address() + 8);
123136

124137
if start_addr == end_addr {
125138
return false;
126139
}
127140

128141
while start_addr < end_addr {
129142
unsafe {
130-
let read = ptr::read_unaligned(start_addr);
143+
let read = core::ptr::read_unaligned(start_addr);
131144
init_pcie_bus(read);
132145
start_addr = start_addr.add(1);
133146
}
@@ -136,58 +149,41 @@ mod pcie {
136149
true
137150
}
138151

139-
#[derive(Debug)]
140-
#[repr(C)]
141-
struct PcieDeviceConfig {
142-
vendor_id: u16,
143-
device_id: u16,
144-
_reserved: [u8; 4096 - 8]
145-
}
146-
147152
#[derive(Debug, Copy, Clone)]
148153
#[repr(C)]
149154
pub(crate) struct McfgTableEntry {
150155
pub base_address: u64,
151156
pub pci_segment_number: u16,
152157
pub start_pci_bus: u8,
153158
pub end_pci_bus: u8,
154-
_reserved: u32
159+
_reserved: u32,
155160
}
156161

157162
impl McfgTableEntry {
158-
pub fn pci_config_space_address(&self, bus_number: u8, device: u8, function: u8) -> PhysAddr {
163+
pub fn pci_config_space_address(
164+
&self,
165+
bus_number: u8,
166+
device: u8,
167+
function: u8,
168+
) -> PhysAddr {
159169
PhysAddr::new(
160-
self.base_address +
161-
((bus_number as u64) << 20) |
162-
(((device as u64) & 0x1f) << 15) |
163-
(((function as u64) & 0x7) << 12)
170+
self.base_address
171+
+ ((u64::from(bus_number) << 20)
172+
| ((u64::from(device) & 0x1f) << 15)
173+
| ((u64::from(function) & 0x7) << 12)),
164174
)
165175
}
166176
}
167177

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-
184178
impl ConfigRegionAccess for McfgTableEntry {
185179
unsafe fn read(&self, address: PciAddress, offset: u16) -> u32 {
186180
assert_eq!(address.segment(), self.pci_segment_number);
187181
assert!(address.bus() >= self.start_pci_bus);
188182
assert!(address.bus() <= self.end_pci_bus);
189183

190-
let ptr = self.pci_config_space_address(address.bus(), address.device(), address.function()) + offset as u64;
184+
let ptr =
185+
self.pci_config_space_address(address.bus(), address.device(), address.function())
186+
+ u64::from(offset);
191187
let ptr = ptr.as_usize() as *const u32;
192188

193189
unsafe { *ptr }
@@ -198,10 +194,14 @@ mod pcie {
198194
assert!(address.bus() >= self.start_pci_bus);
199195
assert!(address.bus() <= self.end_pci_bus);
200196

201-
let ptr = self.pci_config_space_address(address.bus(), address.device(), address.function()) + offset as u64;
197+
let ptr =
198+
self.pci_config_space_address(address.bus(), address.device(), address.function())
199+
+ u64::from(offset);
202200
let ptr = ptr.as_usize() as *mut u32;
203201

204-
unsafe { *ptr = value; }
202+
unsafe {
203+
*ptr = value;
204+
}
205205
}
206206
}
207207

@@ -210,7 +210,15 @@ mod pcie {
210210
return;
211211
}
212212

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));
213+
let end = if bus_entry.end_pci_bus > PCI_MAX_BUS_NUMBER {
214+
PCI_MAX_BUS_NUMBER
215+
} else {
216+
bus_entry.end_pci_bus
217+
};
218+
super::enumerate_devices(
219+
bus_entry.start_pci_bus,
220+
end,
221+
PciConfigAccess::PcieConfigRegion(bus_entry),
222+
);
215223
}
216-
}
224+
}

0 commit comments

Comments
 (0)