vmm: device_manager: Add a PCI devices creation routine

In order to reduce the DeviceManager's new() complexity, we can move the
PCI devices creation code into its own routine.

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
Samuel Ortiz 2019-11-18 12:23:27 +01:00 committed by Sebastien Boeuf
parent 5087f633f6
commit 79b8f8e477

View File

@ -463,6 +463,59 @@ impl DeviceManager {
)?; )?;
if cfg!(feature = "pci_support") { if cfg!(feature = "pci_support") {
DeviceManager::add_pci_devices(
vm_info,
&address_manager,
mem_slots,
&mut virt_iommu,
virtio_devices,
&interrupt_info,
)?;
} else if cfg!(feature = "mmio_support") {
#[cfg(feature = "mmio_support")]
{
for (device, _) in virtio_devices {
let mmio_addr = address_manager
.allocator
.lock()
.unwrap()
.allocate_mmio_addresses(None, MMIO_LEN, Some(MMIO_LEN));
if let Some(addr) = mmio_addr {
DeviceManager::add_virtio_mmio_device(
device,
vm_info.memory,
&address_manager,
vm_info.vm_fd,
&interrupt_info,
addr,
&mut cmdline_additions,
)?;
} else {
error!("Unable to allocate MMIO address!");
}
}
}
}
Ok(DeviceManager {
address_manager,
console,
ioapic,
mmap_regions,
cmdline_additions,
virt_iommu,
})
}
#[allow(unused_variables)]
fn add_pci_devices(
vm_info: &VmInfo,
address_manager: &Arc<AddressManager>,
mem_slots: u32,
virt_iommu: &mut Option<(u32, Vec<u32>)>,
virtio_devices: Vec<(Box<dyn vm_virtio::VirtioDevice>, bool)>,
interrupt_info: &InterruptInfo,
) -> DeviceManagerResult<()> {
#[cfg(feature = "pci_support")] #[cfg(feature = "pci_support")]
{ {
let pci_root = PciRoot::new(None); let pci_root = PciRoot::new(None);
@ -534,7 +587,7 @@ impl DeviceManager {
&None, &None,
)?; )?;
virt_iommu = Some((iommu_id, iommu_attached_devices)); *virt_iommu = Some((iommu_id, iommu_attached_devices));
} }
let pci_bus = Arc::new(Mutex::new(pci_bus)); let pci_bus = Arc::new(Mutex::new(pci_bus));
@ -553,40 +606,8 @@ impl DeviceManager {
) )
.map_err(DeviceManagerError::BusError)?; .map_err(DeviceManagerError::BusError)?;
} }
} else if cfg!(feature = "mmio_support") {
#[cfg(feature = "mmio_support")]
{
for (device, _) in virtio_devices {
let mmio_addr = address_manager
.allocator
.lock()
.unwrap()
.allocate_mmio_addresses(None, MMIO_LEN, Some(MMIO_LEN));
if let Some(addr) = mmio_addr {
DeviceManager::add_virtio_mmio_device(
device,
vm_info.memory,
&address_manager,
vm_info.vm_fd,
&interrupt_info,
addr,
&mut cmdline_additions,
)?;
} else {
error!("Unable to allocate MMIO address!");
}
}
}
}
Ok(DeviceManager { Ok(())
address_manager,
console,
ioapic,
mmap_regions,
cmdline_additions,
virt_iommu,
})
} }
fn make_ioapic( fn make_ioapic(