From 0c34846ef6fe5da49159dcc04a968cbf1da80ba2 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Thu, 7 Apr 2022 17:20:25 +0200 Subject: [PATCH] vmm: Return new PCI resources from add_pci_device() By returning the new PCI resources from add_pci_device(), we allow the factorization of the code translating the BARs into resources. This allows VIRTIO, VFIO and vfio-user to add the resources to the DeviceTree node. Signed-off-by: Sebastien Boeuf --- vmm/src/device_manager.rs | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index efddac89d..a4b5fca39 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -3196,7 +3196,7 @@ impl DeviceManager { let vfio_pci_device = Arc::new(Mutex::new(vfio_pci_device)); - self.add_pci_device( + let new_resources = self.add_pci_device( vfio_pci_device.clone(), vfio_pci_device.clone(), pci_segment_id, @@ -3221,6 +3221,8 @@ impl DeviceManager { }); } + // Update the device tree with correct resource information. + node.resources = new_resources; node.pci_bdf = Some(pci_device_bdf); node.pci_device_handle = Some(PciDeviceHandle::Vfio(vfio_pci_device)); @@ -3239,7 +3241,7 @@ impl DeviceManager { segment_id: u16, bdf: PciBdf, resources: Option>, - ) -> DeviceManagerResult> { + ) -> DeviceManagerResult> { let bars = pci_device .lock() .unwrap() @@ -3274,7 +3276,23 @@ impl DeviceManager { ) .map_err(DeviceManagerError::AddPciDevice)?; - Ok(bars) + let mut new_resources = Vec::new(); + for pci_bar in bars.iter() { + match pci_bar.2 { + PciBarRegionType::IoRegion => new_resources.push(Resource::PioAddressRange { + base: pci_bar.0.raw_value() as u16, + size: pci_bar.1 as u16, + }), + PciBarRegionType::Memory32BitRegion | PciBarRegionType::Memory64BitRegion => { + new_resources.push(Resource::MmioAddressRange { + base: pci_bar.0.raw_value(), + size: pci_bar.1 as u64, + }) + } + } + } + + Ok(new_resources) } fn add_vfio_devices(&mut self) -> DeviceManagerResult> { @@ -3373,7 +3391,7 @@ impl DeviceManager { let vfio_user_pci_device = Arc::new(Mutex::new(vfio_user_pci_device)); - self.add_pci_device( + let new_resources = self.add_pci_device( vfio_user_pci_device.clone(), vfio_user_pci_device.clone(), pci_segment_id, @@ -3383,6 +3401,8 @@ impl DeviceManager { let mut node = device_node!(vfio_user_name); + // Update the device tree with correct resource information. + node.resources = new_resources; node.pci_bdf = Some(pci_device_bdf); node.pci_device_handle = Some(PciDeviceHandle::VfioUser(vfio_user_pci_device)); @@ -3516,7 +3536,7 @@ impl DeviceManager { .map_err(DeviceManagerError::VirtioDevice)?, )); - let bars = self.add_pci_device( + let new_resources = self.add_pci_device( virtio_pci_device.clone(), virtio_pci_device.clone(), pci_segment_id, @@ -3534,13 +3554,6 @@ impl DeviceManager { } // Update the device tree with correct resource information. - let mut new_resources = Vec::new(); - for pci_bar in bars.iter() { - new_resources.push(Resource::MmioAddressRange { - base: pci_bar.0.raw_value(), - size: pci_bar.1 as u64, - }); - } node.resources = new_resources; node.migratable = Some(Arc::clone(&virtio_pci_device) as Arc>); node.pci_bdf = Some(pci_device_bdf);