@@ -21,7 +21,7 @@ use uuid::Uuid;
2121use vm_allocator:: AddressAllocator ;
2222use vm_device:: { BusDeviceSync , BusError } ;
2323
24- use crate :: arch:: { PCI_MMCONFIG_START , PCI_MMIO_CONFIG_SIZE_PER_SEGMENT } ;
24+ use crate :: arch:: { ArchVm as Vm , PCI_MMCONFIG_START , PCI_MMIO_CONFIG_SIZE_PER_SEGMENT } ;
2525use crate :: vstate:: resources:: ResourceAllocator ;
2626
2727pub struct PciSegment {
@@ -67,28 +67,21 @@ impl std::fmt::Debug for PciSegment {
6767}
6868
6969impl PciSegment {
70- fn build (
71- id : u16 ,
72- resource_allocator : & Arc < ResourceAllocator > ,
73- pci_irq_slots : & [ u8 ; 32 ] ,
74- ) -> Result < PciSegment , BusError > {
70+ fn build ( id : u16 , vm : & Arc < Vm > , pci_irq_slots : & [ u8 ; 32 ] ) -> Result < PciSegment , BusError > {
7571 let pci_root = PciRoot :: new ( None ) ;
76- let pci_bus = Arc :: new ( Mutex :: new ( PciBus :: new (
77- pci_root,
78- resource_allocator. clone ( ) ,
79- ) ) ) ;
72+ let pci_bus = Arc :: new ( Mutex :: new ( PciBus :: new ( pci_root, vm. clone ( ) ) ) ) ;
8073
8174 let pci_config_mmio = Arc :: new ( Mutex :: new ( PciConfigMmio :: new ( Arc :: clone ( & pci_bus) ) ) ) ;
8275 let mmio_config_address = PCI_MMCONFIG_START + PCI_MMIO_CONFIG_SIZE_PER_SEGMENT * id as u64 ;
8376
84- resource_allocator . mmio_bus . insert (
77+ vm . common . mmio_bus . insert (
8578 Arc :: clone ( & pci_config_mmio) as Arc < dyn BusDeviceSync > ,
8679 mmio_config_address,
8780 PCI_MMIO_CONFIG_SIZE_PER_SEGMENT ,
8881 ) ?;
8982
90- let mem32_allocator = resource_allocator. mmio32_memory . clone ( ) ;
91- let mem64_allocator = resource_allocator. mmio64_memory . clone ( ) ;
83+ let mem32_allocator = vm . common . resource_allocator . mmio32_memory . clone ( ) ;
84+ let mem64_allocator = vm . common . resource_allocator . mmio64_memory . clone ( ) ;
9285
9386 let start_of_mem32_area = mem32_allocator. lock ( ) . unwrap ( ) . base ( ) ;
9487 let end_of_mem32_area = mem32_allocator. lock ( ) . unwrap ( ) . end ( ) ;
@@ -119,13 +112,15 @@ impl PciSegment {
119112 #[ cfg( target_arch = "x86_64" ) ]
120113 pub ( crate ) fn new (
121114 id : u16 ,
122- resource_allocator : & Arc < ResourceAllocator > ,
115+ vm : & Arc < Vm > ,
123116 pci_irq_slots : & [ u8 ; 32 ] ,
124117 ) -> Result < PciSegment , BusError > {
125- let mut segment = Self :: build ( id, resource_allocator, pci_irq_slots) ?;
118+ use crate :: Vm ;
119+
120+ let mut segment = Self :: build ( id, vm, pci_irq_slots) ?;
126121 let pci_config_io = Arc :: new ( Mutex :: new ( PciConfigIo :: new ( Arc :: clone ( & segment. pci_bus ) ) ) ) ;
127122
128- resource_allocator . pio_bus . insert (
123+ vm . pio_bus . insert (
129124 pci_config_io. clone ( ) ,
130125 PCI_CONFIG_IO_PORT ,
131126 PCI_CONFIG_IO_PORT_SIZE ,
@@ -151,10 +146,10 @@ impl PciSegment {
151146 #[ cfg( target_arch = "aarch64" ) ]
152147 pub ( crate ) fn new (
153148 id : u16 ,
154- resource_allocator : & Arc < ResourceAllocator > ,
149+ vm : & Arc < Vm > ,
155150 pci_irq_slots : & [ u8 ; 32 ] ,
156151 ) -> Result < PciSegment , BusError > {
157- let segment = Self :: build ( id, resource_allocator , pci_irq_slots) ?;
152+ let segment = Self :: build ( id, vm , pci_irq_slots) ?;
158153 info ! (
159154 "pci: adding PCI segment: id={:#x}, PCI MMIO config address: {:#x}, mem32 area: \
160155 [{:#x}-{:#x}], mem64 area: [{:#x}-{:#x}]",
@@ -468,13 +463,14 @@ mod tests {
468463
469464 use super :: * ;
470465 use crate :: arch;
466+ use crate :: builder:: tests:: default_vmm;
471467 use crate :: utils:: u64_to_usize;
472468
473469 #[ test]
474470 fn test_pci_segment_build ( ) {
475- let resource_allocator = Arc :: new ( ResourceAllocator :: new ( ) . unwrap ( ) ) ;
471+ let vmm = default_vmm ( ) ;
476472 let pci_irq_slots = & [ 0u8 ; 32 ] ;
477- let pci_segment = PciSegment :: new ( 0 , & resource_allocator , pci_irq_slots) . unwrap ( ) ;
473+ let pci_segment = PciSegment :: new ( 0 , & vmm . vm , pci_irq_slots) . unwrap ( ) ;
478474
479475 assert_eq ! ( pci_segment. id, 0 ) ;
480476 assert_eq ! (
@@ -503,35 +499,34 @@ mod tests {
503499 #[ cfg( target_arch = "x86_64" ) ]
504500 #[ test]
505501 fn test_io_bus ( ) {
506- let resource_allocator = Arc :: new ( ResourceAllocator :: new ( ) . unwrap ( ) ) ;
502+ let vmm = default_vmm ( ) ;
507503 let pci_irq_slots = & [ 0u8 ; 32 ] ;
508- let pci_segment = PciSegment :: new ( 0 , & resource_allocator , pci_irq_slots) . unwrap ( ) ;
504+ let pci_segment = PciSegment :: new ( 0 , & vmm . vm , pci_irq_slots) . unwrap ( ) ;
509505
510506 let mut data = [ 0u8 ; u64_to_usize ( PCI_CONFIG_IO_PORT_SIZE ) ] ;
511- resource_allocator
512- . pio_bus
513- . read ( PCI_CONFIG_IO_PORT , & mut data)
514- . unwrap ( ) ;
507+ vmm. vm . pio_bus . read ( PCI_CONFIG_IO_PORT , & mut data) . unwrap ( ) ;
515508
516- resource_allocator
509+ vmm . vm
517510 . pio_bus
518511 . read ( PCI_CONFIG_IO_PORT + PCI_CONFIG_IO_PORT_SIZE , & mut data)
519512 . unwrap_err ( ) ;
520513 }
521514
522515 #[ test]
523516 fn test_mmio_bus ( ) {
524- let resource_allocator = Arc :: new ( ResourceAllocator :: new ( ) . unwrap ( ) ) ;
517+ let vmm = default_vmm ( ) ;
525518 let pci_irq_slots = & [ 0u8 ; 32 ] ;
526- let pci_segment = PciSegment :: new ( 0 , & resource_allocator , pci_irq_slots) . unwrap ( ) ;
519+ let pci_segment = PciSegment :: new ( 0 , & vmm . vm , pci_irq_slots) . unwrap ( ) ;
527520
528521 let mut data = [ 0u8 ; u64_to_usize ( PCI_MMIO_CONFIG_SIZE_PER_SEGMENT ) ] ;
529522
530- resource_allocator
523+ vmm. vm
524+ . common
531525 . mmio_bus
532526 . read ( pci_segment. mmio_config_address , & mut data)
533527 . unwrap ( ) ;
534- resource_allocator
528+ vmm. vm
529+ . common
535530 . mmio_bus
536531 . read (
537532 pci_segment. mmio_config_address + PCI_MMIO_CONFIG_SIZE_PER_SEGMENT ,
@@ -542,9 +537,9 @@ mod tests {
542537
543538 #[ test]
544539 fn test_next_device_bdf ( ) {
545- let resource_allocator = Arc :: new ( ResourceAllocator :: new ( ) . unwrap ( ) ) ;
540+ let vmm = default_vmm ( ) ;
546541 let pci_irq_slots = & [ 0u8 ; 32 ] ;
547- let pci_segment = PciSegment :: new ( 0 , & resource_allocator , pci_irq_slots) . unwrap ( ) ;
542+ let pci_segment = PciSegment :: new ( 0 , & vmm . vm , pci_irq_slots) . unwrap ( ) ;
548543
549544 // Start checking from device id 1, since 0 is allocated to the Root port.
550545 for dev_id in 1 ..32 {
0 commit comments