Skip to content

Commit 87375c7

Browse files
committed
Move cam_offset method to Cam enum.
It may be useful for other ConfigurationAccess implementations.
1 parent 32954a5 commit 87375c7

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/transport/pci/bus.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,26 @@ impl Cam {
123123
Self::Ecam => 0x10000000,
124124
}
125125
}
126+
127+
/// Returns the offset in bytes within the CAM region for the given device, function and
128+
/// register.
129+
pub fn cam_offset(self, device_function: DeviceFunction, register_offset: u8) -> u32 {
130+
assert!(device_function.valid());
131+
132+
let bdf = (device_function.bus as u32) << 8
133+
| (device_function.device as u32) << 3
134+
| device_function.function as u32;
135+
let address =
136+
bdf << match self {
137+
Cam::MmioCam => 8,
138+
Cam::Ecam => 12,
139+
} | register_offset as u32;
140+
// Ensure that address is within range.
141+
assert!(address < self.size());
142+
// Ensure that address is word-aligned.
143+
assert!(address & 0x3 == 0);
144+
address
145+
}
126146
}
127147

128148
impl<C: ConfigurationAccess> PciRoot<C> {
@@ -324,29 +344,11 @@ impl MmioCam {
324344
cam,
325345
}
326346
}
327-
328-
fn cam_offset(&self, device_function: DeviceFunction, register_offset: u8) -> u32 {
329-
assert!(device_function.valid());
330-
331-
let bdf = (device_function.bus as u32) << 8
332-
| (device_function.device as u32) << 3
333-
| device_function.function as u32;
334-
let address =
335-
bdf << match self.cam {
336-
Cam::MmioCam => 8,
337-
Cam::Ecam => 12,
338-
} | register_offset as u32;
339-
// Ensure that address is within range.
340-
assert!(address < self.cam.size());
341-
// Ensure that address is word-aligned.
342-
assert!(address & 0x3 == 0);
343-
address
344-
}
345347
}
346348

347349
impl ConfigurationAccess for MmioCam {
348350
fn read_word(&self, device_function: DeviceFunction, register_offset: u8) -> u32 {
349-
let address = self.cam_offset(device_function, register_offset);
351+
let address = self.cam.cam_offset(device_function, register_offset);
350352
// Safe because both the `mmio_base` and the address offset are properly aligned, and the
351353
// resulting pointer is within the MMIO range of the CAM.
352354
unsafe {
@@ -356,7 +358,7 @@ impl ConfigurationAccess for MmioCam {
356358
}
357359

358360
fn write_word(&mut self, device_function: DeviceFunction, register_offset: u8, data: u32) {
359-
let address = self.cam_offset(device_function, register_offset);
361+
let address = self.cam.cam_offset(device_function, register_offset);
360362
// Safe because both the `mmio_base` and the address offset are properly aligned, and the
361363
// resulting pointer is within the MMIO range of the CAM.
362364
unsafe {

0 commit comments

Comments
 (0)