vmm: move BAR mapping registration from PciBus to DeviceManager

`PciBus::register_mapping()` operates on `mmio_bus` and `io_bus`
which are passed in as external parameters and have nothing to do
with PciBus internal state. Move this logic into
`DeviceManager::register_bar_mapping()` where it belongs, and move
the `PioInsert`/`MmioInsert` error variants from `PciRootError` to
`DeviceManagerError` accordingly.

Signed-off-by: wuxinyue <wuxinyue.wxy@antgroup.com>
Assisted-by: Claude:Opus-4.6
This commit is contained in:
wuxinyue
2026-06-11 10:54:42 +08:00
committed by Rob Bradford
parent bf4b856dd2
commit f787bd7a17
2 changed files with 41 additions and 50 deletions

View File

@@ -74,8 +74,8 @@ use libc::{
};
use log::{debug, error, info, warn};
use pci::{
DeviceRelocation, MmioRegion, PciBarRegionType, PciBdf, PciDevice, VfioDmaMapping,
VfioPciDevice, VfioUserDmaMapping, VfioUserPciDevice, VfioUserPciDeviceError,
DeviceRelocation, MmioRegion, PciBarConfiguration, PciBarRegionType, PciBdf, PciDevice,
VfioDmaMapping, VfioPciDevice, VfioUserDmaMapping, VfioUserPciDevice, VfioUserPciDeviceError,
};
use rate_limiter::group::RateLimiterGroup;
use seccompiler::SeccompAction;
@@ -306,6 +306,14 @@ pub enum DeviceManagerError {
#[error("Cannot add PCI device")]
AddPciDevice(#[source] pci::PciRootError),
/// Could not add a device to the port io bus
#[error("Could not add a device to the port io bus")]
PioInsert(#[source] vm_device::BusError),
/// Could not add a device to the mmio bus
#[error("Could not add a device to the mmio bus")]
MmioInsert(#[source] vm_device::BusError),
/// Cannot open persistent memory file
#[error("Cannot open persistent memory file")]
PmemFileOpen(#[source] io::Error),
@@ -4120,25 +4128,16 @@ impl DeviceManager {
)
.map_err(DeviceManagerError::AllocateBars)?;
let mut pci_bus = self.pci_segments[segment_id as usize]
self.pci_segments[segment_id as usize]
.pci_bus
.lock()
.unwrap();
pci_bus
.unwrap()
.add_device(bdf.device(), pci_device)
.map_err(DeviceManagerError::AddPciDevice)?;
self.bus_devices.push(Arc::clone(&bus_device));
pci_bus
.register_mapping(
bus_device,
self.address_manager.io_bus.as_ref(),
self.address_manager.mmio_bus.as_ref(),
bars.clone(),
)
.map_err(DeviceManagerError::AddPciDevice)?;
self.register_bar_mapping(bus_device, &bars)?;
let mut new_resources = Vec::new();
for bar in bars {
@@ -4154,6 +4153,31 @@ impl DeviceManager {
Ok(new_resources)
}
#[expect(clippy::needless_pass_by_value)]
fn register_bar_mapping(
&mut self,
bus_device: Arc<dyn BusDeviceSync>,
bars: &[PciBarConfiguration],
) -> DeviceManagerResult<()> {
for bar in bars {
match bar.region_type() {
PciBarRegionType::IoRegion => {
self.address_manager
.io_bus
.insert(bus_device.clone(), bar.addr(), bar.size())
.map_err(DeviceManagerError::PioInsert)?;
}
PciBarRegionType::Memory32BitRegion | PciBarRegionType::Memory64BitRegion => {
self.address_manager
.mmio_bus
.insert(bus_device.clone(), bar.addr(), bar.size())
.map_err(DeviceManagerError::MmioInsert)?;
}
}
}
Ok(())
}
fn add_vfio_devices(
&mut self,
snapshot: Option<&Snapshot>,