@@ -462,3 +462,100 @@ impl Aml for PciSegment {
462462 . append_aml_bytes ( v)
463463 }
464464}
465+
466+ #[ cfg( test) ]
467+ mod tests {
468+
469+ use super :: * ;
470+ use crate :: arch;
471+ use crate :: utils:: u64_to_usize;
472+
473+ #[ test]
474+ fn test_pci_segment_build ( ) {
475+ let resource_allocator = Arc :: new ( ResourceAllocator :: new ( ) . unwrap ( ) ) ;
476+ let pci_irq_slots = & [ 0u8 ; 32 ] ;
477+ let pci_segment = PciSegment :: new ( 0 , & resource_allocator, pci_irq_slots) . unwrap ( ) ;
478+
479+ assert_eq ! ( pci_segment. id, 0 ) ;
480+ assert_eq ! (
481+ pci_segment. start_of_mem32_area,
482+ arch:: MEM_32BIT_DEVICES_START
483+ ) ;
484+ assert_eq ! (
485+ pci_segment. end_of_mem32_area,
486+ arch:: MEM_32BIT_DEVICES_START + arch:: MEM_32BIT_DEVICES_SIZE - 1
487+ ) ;
488+ assert_eq ! (
489+ pci_segment. start_of_mem64_area,
490+ arch:: MEM_64BIT_DEVICES_START
491+ ) ;
492+ assert_eq ! (
493+ pci_segment. end_of_mem64_area,
494+ arch:: MEM_64BIT_DEVICES_START + arch:: MEM_64BIT_DEVICES_SIZE - 1
495+ ) ;
496+ assert_eq ! ( pci_segment. mmio_config_address, arch:: PCI_MMCONFIG_START ) ;
497+ assert_eq ! ( pci_segment. proximity_domain, 0 ) ;
498+ assert_eq ! ( pci_segment. pci_devices_up, 0 ) ;
499+ assert_eq ! ( pci_segment. pci_devices_down, 0 ) ;
500+ assert_eq ! ( pci_segment. pci_irq_slots, [ 0u8 ; 32 ] ) ;
501+ }
502+
503+ #[ cfg( target_arch = "x86_64" ) ]
504+ #[ test]
505+ fn test_io_bus ( ) {
506+ let resource_allocator = Arc :: new ( ResourceAllocator :: new ( ) . unwrap ( ) ) ;
507+ let pci_irq_slots = & [ 0u8 ; 32 ] ;
508+ let pci_segment = PciSegment :: new ( 0 , & resource_allocator, pci_irq_slots) . unwrap ( ) ;
509+
510+ 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 ( ) ;
515+
516+ resource_allocator
517+ . pio_bus
518+ . read ( PCI_CONFIG_IO_PORT + PCI_CONFIG_IO_PORT_SIZE , & mut data)
519+ . unwrap_err ( ) ;
520+ }
521+
522+ #[ test]
523+ fn test_mmio_bus ( ) {
524+ let resource_allocator = Arc :: new ( ResourceAllocator :: new ( ) . unwrap ( ) ) ;
525+ let pci_irq_slots = & [ 0u8 ; 32 ] ;
526+ let pci_segment = PciSegment :: new ( 0 , & resource_allocator, pci_irq_slots) . unwrap ( ) ;
527+
528+ let mut data = [ 0u8 ; u64_to_usize ( PCI_MMIO_CONFIG_SIZE_PER_SEGMENT ) ] ;
529+
530+ resource_allocator
531+ . mmio_bus
532+ . read ( pci_segment. mmio_config_address , & mut data)
533+ . unwrap ( ) ;
534+ resource_allocator
535+ . mmio_bus
536+ . read (
537+ pci_segment. mmio_config_address + PCI_MMIO_CONFIG_SIZE_PER_SEGMENT ,
538+ & mut data,
539+ )
540+ . unwrap_err ( ) ;
541+ }
542+
543+ #[ test]
544+ fn test_next_device_bdf ( ) {
545+ let resource_allocator = Arc :: new ( ResourceAllocator :: new ( ) . unwrap ( ) ) ;
546+ let pci_irq_slots = & [ 0u8 ; 32 ] ;
547+ let pci_segment = PciSegment :: new ( 0 , & resource_allocator, pci_irq_slots) . unwrap ( ) ;
548+
549+ // Start checking from device id 1, since 0 is allocated to the Root port.
550+ for dev_id in 1 ..32 {
551+ let bdf = pci_segment. next_device_bdf ( ) . unwrap ( ) ;
552+ // In our case we have a single Segment with id 0, which has
553+ // a single bus with id 0. Also, each device of ours has a
554+ // single function.
555+ assert_eq ! ( bdf, PciBdf :: new( 0 , 0 , dev_id, 0 ) ) ;
556+ }
557+
558+ // We can only have 32 devices on a segment
559+ pci_segment. next_device_bdf ( ) . unwrap_err ( ) ;
560+ }
561+ }
0 commit comments