Skip to content

Commit

Permalink
Merge pull request #152 from rcore-os/bar_iterator
Browse files Browse the repository at this point in the history
Add method to get all BARs for PCI device function.
  • Loading branch information
equation314 authored Jul 29, 2024
2 parents a86f69c + 2f78a46 commit 0b1383c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
14 changes: 4 additions & 10 deletions examples/aarch64/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,8 @@ fn allocate_bars(
device_function: DeviceFunction,
allocator: &mut PciMemory32Allocator,
) {
let mut bar_index = 0;
while bar_index < 6 {
let info = root.bar_info(device_function, bar_index).unwrap();
for (bar_index, info) in root.bars(device_function).unwrap().into_iter().enumerate() {
let Some(info) = info else { continue };
debug!("BAR {}: {}", bar_index, info);
// Ignore I/O bars, as they aren't required for the VirtIO driver.
if let BarInfo::Memory {
Expand All @@ -425,25 +424,20 @@ fn allocate_bars(
if size > 0 {
let address = allocator.allocate_memory_32(size);
debug!("Allocated address {:#010x}", address);
root.set_bar_32(device_function, bar_index, address);
root.set_bar_32(device_function, bar_index as u8, address);
}
}
MemoryBarType::Width64 => {
if size > 0 {
let address = allocator.allocate_memory_32(size);
debug!("Allocated address {:#010x}", address);
root.set_bar_64(device_function, bar_index, address.into());
root.set_bar_64(device_function, bar_index as u8, address.into());
}
}

_ => panic!("Memory BAR address type {:?} not supported.", address_type),
}
}

bar_index += 1;
if info.takes_two_entries() {
bar_index += 1;
}
}

// Enable the device to use its BARs.
Expand Down
17 changes: 17 additions & 0 deletions src/transport/pci/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use bitflags::bitflags;
use core::{
array,
convert::TryFrom,
fmt::{self, Display, Formatter},
};
Expand Down Expand Up @@ -246,6 +247,22 @@ impl PciRoot {
}
}

/// Returns information about all the given device function's BARs.
pub fn bars(
&mut self,
device_function: DeviceFunction,
) -> Result<[Option<BarInfo>; 6], PciError> {
let mut bars = array::from_fn(|_| None);
let mut bar_index = 0;
while bar_index < 6 {
let info = self.bar_info(device_function, bar_index)?;
let takes_two_entries = info.takes_two_entries();
bars[usize::from(bar_index)] = Some(info);
bar_index += if takes_two_entries { 2 } else { 1 };
}
Ok(bars)
}

/// Gets information about the given BAR of the given device function.
pub fn bar_info(
&mut self,
Expand Down

0 comments on commit 0b1383c

Please sign in to comment.