@@ -21,7 +21,7 @@ use uuid::Uuid;
21
21
use vm_allocator:: AddressAllocator ;
22
22
use vm_device:: { BusDeviceSync , BusError } ;
23
23
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 } ;
25
25
use crate :: vstate:: resources:: ResourceAllocator ;
26
26
27
27
pub struct PciSegment {
@@ -67,28 +67,21 @@ impl std::fmt::Debug for PciSegment {
67
67
}
68
68
69
69
impl 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 > {
75
71
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 ( ) ) ) ) ;
80
73
81
74
let pci_config_mmio = Arc :: new ( Mutex :: new ( PciConfigMmio :: new ( Arc :: clone ( & pci_bus) ) ) ) ;
82
75
let mmio_config_address = PCI_MMCONFIG_START + PCI_MMIO_CONFIG_SIZE_PER_SEGMENT * id as u64 ;
83
76
84
- resource_allocator . mmio_bus . insert (
77
+ vm . common . mmio_bus . insert (
85
78
Arc :: clone ( & pci_config_mmio) as Arc < dyn BusDeviceSync > ,
86
79
mmio_config_address,
87
80
PCI_MMIO_CONFIG_SIZE_PER_SEGMENT ,
88
81
) ?;
89
82
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 ( ) ;
92
85
93
86
let start_of_mem32_area = mem32_allocator. lock ( ) . unwrap ( ) . base ( ) ;
94
87
let end_of_mem32_area = mem32_allocator. lock ( ) . unwrap ( ) . end ( ) ;
@@ -119,13 +112,15 @@ impl PciSegment {
119
112
#[ cfg( target_arch = "x86_64" ) ]
120
113
pub ( crate ) fn new (
121
114
id : u16 ,
122
- resource_allocator : & Arc < ResourceAllocator > ,
115
+ vm : & Arc < Vm > ,
123
116
pci_irq_slots : & [ u8 ; 32 ] ,
124
117
) -> 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) ?;
126
121
let pci_config_io = Arc :: new ( Mutex :: new ( PciConfigIo :: new ( Arc :: clone ( & segment. pci_bus ) ) ) ) ;
127
122
128
- resource_allocator . pio_bus . insert (
123
+ vm . pio_bus . insert (
129
124
pci_config_io. clone ( ) ,
130
125
PCI_CONFIG_IO_PORT ,
131
126
PCI_CONFIG_IO_PORT_SIZE ,
@@ -151,10 +146,10 @@ impl PciSegment {
151
146
#[ cfg( target_arch = "aarch64" ) ]
152
147
pub ( crate ) fn new (
153
148
id : u16 ,
154
- resource_allocator : & Arc < ResourceAllocator > ,
149
+ vm : & Arc < Vm > ,
155
150
pci_irq_slots : & [ u8 ; 32 ] ,
156
151
) -> Result < PciSegment , BusError > {
157
- let segment = Self :: build ( id, resource_allocator , pci_irq_slots) ?;
152
+ let segment = Self :: build ( id, vm , pci_irq_slots) ?;
158
153
info ! (
159
154
"pci: adding PCI segment: id={:#x}, PCI MMIO config address: {:#x}, mem32 area: \
160
155
[{:#x}-{:#x}], mem64 area: [{:#x}-{:#x}]",
@@ -468,13 +463,14 @@ mod tests {
468
463
469
464
use super :: * ;
470
465
use crate :: arch;
466
+ use crate :: builder:: tests:: default_vmm;
471
467
use crate :: utils:: u64_to_usize;
472
468
473
469
#[ test]
474
470
fn test_pci_segment_build ( ) {
475
- let resource_allocator = Arc :: new ( ResourceAllocator :: new ( ) . unwrap ( ) ) ;
471
+ let vmm = default_vmm ( ) ;
476
472
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 ( ) ;
478
474
479
475
assert_eq ! ( pci_segment. id, 0 ) ;
480
476
assert_eq ! (
@@ -503,35 +499,34 @@ mod tests {
503
499
#[ cfg( target_arch = "x86_64" ) ]
504
500
#[ test]
505
501
fn test_io_bus ( ) {
506
- let resource_allocator = Arc :: new ( ResourceAllocator :: new ( ) . unwrap ( ) ) ;
502
+ let vmm = default_vmm ( ) ;
507
503
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 ( ) ;
509
505
510
506
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 ( ) ;
515
508
516
- resource_allocator
509
+ vmm . vm
517
510
. pio_bus
518
511
. read ( PCI_CONFIG_IO_PORT + PCI_CONFIG_IO_PORT_SIZE , & mut data)
519
512
. unwrap_err ( ) ;
520
513
}
521
514
522
515
#[ test]
523
516
fn test_mmio_bus ( ) {
524
- let resource_allocator = Arc :: new ( ResourceAllocator :: new ( ) . unwrap ( ) ) ;
517
+ let vmm = default_vmm ( ) ;
525
518
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 ( ) ;
527
520
528
521
let mut data = [ 0u8 ; u64_to_usize ( PCI_MMIO_CONFIG_SIZE_PER_SEGMENT ) ] ;
529
522
530
- resource_allocator
523
+ vmm. vm
524
+ . common
531
525
. mmio_bus
532
526
. read ( pci_segment. mmio_config_address , & mut data)
533
527
. unwrap ( ) ;
534
- resource_allocator
528
+ vmm. vm
529
+ . common
535
530
. mmio_bus
536
531
. read (
537
532
pci_segment. mmio_config_address + PCI_MMIO_CONFIG_SIZE_PER_SEGMENT ,
@@ -542,9 +537,9 @@ mod tests {
542
537
543
538
#[ test]
544
539
fn test_next_device_bdf ( ) {
545
- let resource_allocator = Arc :: new ( ResourceAllocator :: new ( ) . unwrap ( ) ) ;
540
+ let vmm = default_vmm ( ) ;
546
541
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 ( ) ;
548
543
549
544
// Start checking from device id 1, since 0 is allocated to the Root port.
550
545
for dev_id in 1 ..32 {
0 commit comments