vfio: pci: Implement free_bars() from the PciDevice trait

In order to provide the tools for a complete cleanup whenever a VFIO PCI
device is removed from the VM, the VfioPciDevice implements free_bars()
method from PciDevice trait. This will take care of removing the IO and
MMIO ranges previously reserved through the vm-allocator.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2020-03-11 09:35:12 +01:00 committed by Rob Bradford
parent b8e1cf2d4e
commit 34d1f435f4

View File

@ -222,6 +222,7 @@ impl Interrupt {
struct MmioRegion { struct MmioRegion {
start: GuestAddress, start: GuestAddress,
length: GuestUsize, length: GuestUsize,
type_: PciBarRegionType,
index: u32, index: u32,
mem_slot: Option<u32>, mem_slot: Option<u32>,
host_addr: Option<u64>, host_addr: Option<u64>,
@ -799,6 +800,7 @@ impl PciDevice for VfioPciDevice {
self.mmio_regions.push(MmioRegion { self.mmio_regions.push(MmioRegion {
start: bar_addr, start: bar_addr,
length: region_size, length: region_size,
type_: region_type,
index: bar_id as u32, index: bar_id as u32,
mem_slot: None, mem_slot: None,
host_addr: None, host_addr: None,
@ -818,6 +820,26 @@ impl PciDevice for VfioPciDevice {
Ok(ranges) Ok(ranges)
} }
fn free_bars(
&mut self,
allocator: &mut SystemAllocator,
) -> std::result::Result<(), PciDeviceError> {
for region in self.mmio_regions.iter() {
match region.type_ {
PciBarRegionType::IORegion => {
allocator.free_io_addresses(region.start, region.length);
}
PciBarRegionType::Memory32BitRegion => {
allocator.free_mmio_hole_addresses(region.start, region.length);
}
PciBarRegionType::Memory64BitRegion => {
allocator.free_mmio_addresses(region.start, region.length);
}
}
}
Ok(())
}
fn write_config_register(&mut self, reg_idx: usize, offset: u64, data: &[u8]) { fn write_config_register(&mut self, reg_idx: usize, offset: u64, data: &[u8]) {
// When the guest wants to write to a BAR, we trap it into // When the guest wants to write to a BAR, we trap it into
// our local configuration space. We're not reprogramming // our local configuration space. We're not reprogramming