Skip to content

Commit a80faa9

Browse files
committed
pci: add unit tests to PciSegment
Add some unit tests to PciSegment. We now test that the next_device_bdf() method and the initialization logic work as expected. We also check that the configuration space of the PCI segment is correctly registered with the MMIO and, on x86, PIO bus. Signed-off-by: Babis Chalios <[email protected]>
1 parent 159fb3c commit a80faa9

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

src/vmm/src/devices/pci/pci_segment.rs

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

0 commit comments

Comments
 (0)