diff options
| -rw-r--r-- | kernel/kernel/devices/pci/scan.tests.cpp | 319 | ||||
| -rw-r--r-- | kernel/kernel/test_support/devices/pci/ecam_fixture.cpp | 64 | ||||
| -rw-r--r-- | kernel/kernel/test_support/devices/pci/ecam_fixture.hpp | 73 | ||||
| -rw-r--r-- | kernel/kernel/test_support/devices/pci/function_addresses.cpp | 30 | ||||
| -rw-r--r-- | kernel/kernel/test_support/devices/pci/function_addresses.hpp | 40 | ||||
| -rw-r--r-- | kernel/kernel/test_support/devices/pci/function_spec.hpp | 26 | ||||
| -rw-r--r-- | kernel/kernel/test_support/kapi/memory.cpp | 8 | ||||
| -rw-r--r-- | kernel/kernel/test_support/memory.hpp | 9 | ||||
| -rw-r--r-- | kernel/kernel/test_support/simulated_memory.cpp | 9 | ||||
| -rw-r--r-- | kernel/kernel/test_support/simulated_memory.hpp | 7 |
10 files changed, 386 insertions, 199 deletions
diff --git a/kernel/kernel/devices/pci/scan.tests.cpp b/kernel/kernel/devices/pci/scan.tests.cpp index 8915e2e0..c5c2e6d6 100644 --- a/kernel/kernel/devices/pci/scan.tests.cpp +++ b/kernel/kernel/devices/pci/scan.tests.cpp @@ -3,24 +3,26 @@ #include <kernel/devices/pci/ecam_segment.hpp> #include <kernel/devices/pci/function_address.hpp> #include <kernel/devices/pci/function_info.hpp> -#include <kernel/test_support/devices/pci/ecam_builder.hpp> +#include <kernel/test_support/devices/pci/ecam_fixture.hpp> +#include <kernel/test_support/devices/pci/function_spec.hpp> #include <kernel/test_support/memory.hpp> #include <kapi/devices.hpp> -#include <kapi/memory.hpp> #include <kstd/system_error.hpp> +#include <kstd/test_support/result.hpp> #include <catch2/catch_test_macros.hpp> #include <cstdint> -using kernel::tests::devices::pci::ecam_builder; +using kernel::devices::pci::ecam_segment; +using kernel::devices::pci::function_address; +using kernel::tests::devices::pci::function_spec; +using kstd::tests::require_ok; namespace { - constexpr auto test_ecam_base = kapi::memory::physical_address{}; - constexpr auto vendor_id_ati = 0x1002; constexpr auto vendor_id_amd = 0x1022; @@ -43,7 +45,6 @@ namespace constexpr auto class_code_multimedia_controller = 0x04; constexpr auto class_code_display_controller = 0x03; - constexpr auto header_type_multifunction = 0x80; constexpr auto header_type_endpoint = 0x00; constexpr auto distinct_vendor_id = 0x1234; @@ -55,141 +56,80 @@ namespace constexpr auto devices_per_bus = 32; - auto build_amd_fch_sata_controller(ecam_builder & builder, std::uint8_t bus, std::uint8_t device) - { - builder.function(bus, device, 0) - .vendor_id(vendor_id_amd) - .device_id(device_id_fch_sata_controller) - .revision_id(revision_id_51) - .programming_interface(programming_interface_ahci_controller) - .subclass_code(subclass_code_ahci_controller) - .class_code(class_code_mass_storage_controller) - .header_type(header_type_endpoint); - } - - auto build_amd_ai_pro_r9700(ecam_builder & builder, std::uint8_t bus, std::uint8_t device) - { - builder.function(bus, device, 0) - .vendor_id(vendor_id_ati) - .device_id(device_id_radeon_ai_pro_r9700) - .revision_id(revision_id_c0) - .programming_interface(programming_interface_vga_controller) - .subclass_code(subclass_code_vga_compatible_controller) - .class_code(class_code_display_controller) - .header_type(header_type_multifunction | header_type_endpoint) - .function(bus, device, 1) - .vendor_id(vendor_id_ati) - .device_id(device_id_hdmi_dp_audio_controller) - .programming_interface(programming_interface_hda_compatible_controller) - .subclass_code(subclass_code_audio_device) - .class_code(class_code_multimedia_controller) - .header_type(header_type_endpoint); - } - - auto build_amd_ai_pro_r9700_disjoint(ecam_builder & builder, std::uint8_t bus, std::uint8_t device) + auto amd_fch_sata_controller(std::uint8_t bus, std::uint8_t device, std::uint8_t function = 0) -> function_spec { - builder.function(bus, device, 0) - .vendor_id(vendor_id_ati) - .device_id(device_id_radeon_ai_pro_r9700) - .revision_id(revision_id_c0) - .programming_interface(programming_interface_vga_controller) - .subclass_code(subclass_code_vga_compatible_controller) - .class_code(class_code_display_controller) - .header_type(header_type_multifunction | header_type_endpoint) - .function(bus, device, 2) - .vendor_id(vendor_id_ati) - .device_id(device_id_hdmi_dp_audio_controller) - .programming_interface(programming_interface_hda_compatible_controller) - .subclass_code(subclass_code_audio_device) - .class_code(class_code_multimedia_controller) - .header_type(header_type_endpoint) - .function(bus, device, 7) - .vendor_id(vendor_id_ati) - .device_id(device_id_hdmi_dp_audio_controller) - .programming_interface(programming_interface_hda_compatible_controller) - .subclass_code(subclass_code_audio_device) - .class_code(class_code_multimedia_controller) - .header_type(header_type_endpoint); + return function_spec{ + .bus = bus, + .device = device, + .function = function, + .vendor_id = vendor_id_amd, + .device_id = device_id_fch_sata_controller, + .class_code = class_code_mass_storage_controller, + .subclass_code = subclass_code_ahci_controller, + .programming_interface = programming_interface_ahci_controller, + .revision = revision_id_51, + }; } - auto build_corrupt_single_function_device(ecam_builder & builder, std::uint8_t bus, std::uint8_t device) + auto amd_ai_pro_r9700_vga(std::uint8_t bus, std::uint8_t device, std::uint8_t function = 0) -> function_spec { - builder.function(bus, device, 1) - .vendor_id(vendor_id_amd) - .device_id(device_id_fch_sata_controller) - .revision_id(revision_id_51) - .programming_interface(programming_interface_ahci_controller) - .subclass_code(subclass_code_ahci_controller) - .class_code(class_code_mass_storage_controller) - .header_type(header_type_endpoint); + return function_spec{ + .bus = bus, + .device = device, + .function = function, + .vendor_id = vendor_id_ati, + .device_id = device_id_radeon_ai_pro_r9700, + .class_code = class_code_display_controller, + .subclass_code = subclass_code_vga_compatible_controller, + .programming_interface = programming_interface_vga_controller, + .revision = revision_id_c0, + }; } - auto build_single_function_device_with_stray_function(ecam_builder & builder, std::uint8_t bus, std::uint8_t device) + auto amd_ai_pro_r9700_audio(std::uint8_t bus, std::uint8_t device, std::uint8_t function = 1) -> function_spec { - build_amd_fch_sata_controller(builder, bus, device); - builder.function(bus, device, 1) - .vendor_id(vendor_id_amd) - .device_id(device_id_fch_sata_controller) - .header_type(header_type_endpoint); + return function_spec{ + .bus = bus, + .device = device, + .function = function, + .vendor_id = vendor_id_ati, + .device_id = device_id_hdmi_dp_audio_controller, + .class_code = class_code_multimedia_controller, + .subclass_code = subclass_code_audio_device, + .programming_interface = programming_interface_hda_compatible_controller, + .revision = revision_id_c0, + }; } - auto make_ecam_segment(std::uint16_t group, std::uint8_t first_bus, std::uint8_t bus_count) - -> kernel::devices::pci::ecam_segment - { - auto descriptor = kapi::devices::pcie_segment_descriptor{.base_address = test_ecam_base, - .group_number = group, - .first_bus = first_bus, - .bus_count = bus_count}; - return kernel::devices::pci::ecam_segment{descriptor}; - } - - auto make_oob_ecam_segment(std::uint16_t group, std::uint8_t first_bus, std::uint8_t bus_count) - -> kernel::devices::pci::ecam_segment - { - auto descriptor = - kapi::devices::pcie_segment_descriptor{.base_address = test_ecam_base + kernel::tests::memory::mmio_size, - .group_number = group, - .first_bus = first_bus, - .bus_count = bus_count}; - return kernel::devices::pci::ecam_segment{descriptor}; - } } // namespace -SCENARIO("Scanning PCI buses") +SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Scanning PCI buses", "[devices][pci]") { GIVEN("An empty PCI bus") { - auto builder = ecam_builder{}; - builder.clear(0); - THEN("scanning the bus finds no devices") { - auto ecam_segment = make_ecam_segment(0, 0, 255); - auto devices = kernel::devices::pci::scan_bus(ecam_segment, 0); + auto segment = ecam_segment{make_descriptor(0, 0)}; + auto devices = require_ok(kernel::devices::pci::scan_bus(segment, 0)); - REQUIRE(devices); - REQUIRE(devices->empty()); + REQUIRE(devices.empty()); } } GIVEN("A PCI bus with a single single-function device") { - auto builder = ecam_builder{}; - builder.clear(0); - build_amd_fch_sata_controller(builder, 0, 1); + place(endpoint(amd_fch_sata_controller(0, 1))); THEN("scanning the bus finds the single function") { - auto ecam_segment = make_ecam_segment(0, 0, 255); - auto devices = kernel::devices::pci::scan_bus(ecam_segment, 0); + auto segment = ecam_segment{make_descriptor(0, 0)}; + auto devices = require_ok(kernel::devices::pci::scan_bus(segment, 0)); - REQUIRE(devices); - REQUIRE(devices->size() == 1); + REQUIRE(devices.size() == 1); - auto const & device = devices->at(0); + auto const & device = devices.at(0); - REQUIRE(device.address == - kernel::devices::pci::function_address{.segment_group = 0, .bus = 0, .device = 1, .function = 0}); + REQUIRE(device.address == function_address{.segment_group = 0, .bus = 0, .device = 1, .function = 0}); REQUIRE(device.vendor_id == vendor_id_amd); REQUIRE(device.device_id == device_id_fch_sata_controller); REQUIRE(device.class_code == class_code_mass_storage_controller); @@ -203,22 +143,22 @@ SCENARIO("Scanning PCI buses") GIVEN("A PCI bus with a single multi-function device") { - auto builder = ecam_builder{}; - builder.clear(0); - build_amd_ai_pro_r9700(builder, 0, 2); + constexpr auto bus = std::uint8_t{0}; + constexpr auto device = std::uint8_t{2}; + + place(multifunction(endpoint(amd_ai_pro_r9700_vga(bus, device)))); + place(endpoint(amd_ai_pro_r9700_audio(bus, device))); THEN("scanning the bus finds all functions of the multi-function device") { - auto ecam_segment = make_ecam_segment(0, 0, 255); - auto devices = kernel::devices::pci::scan_bus(ecam_segment, 0); + auto segment = ecam_segment{make_descriptor(0, 0)}; + auto devices = require_ok(kernel::devices::pci::scan_bus(segment, 0)); - REQUIRE(devices); - REQUIRE(devices->size() == 2); + REQUIRE(devices.size() == 2); - auto const & first = devices->at(0); + auto const & first = devices.at(0); - REQUIRE(first.address == - kernel::devices::pci::function_address{.segment_group = 0, .bus = 0, .device = 2, .function = 0}); + REQUIRE(first.address == function_address{.segment_group = 0, .bus = bus, .device = device, .function = 0}); REQUIRE(first.vendor_id == vendor_id_ati); REQUIRE(first.device_id == device_id_radeon_ai_pro_r9700); REQUIRE(first.class_code == class_code_display_controller); @@ -228,37 +168,38 @@ SCENARIO("Scanning PCI buses") REQUIRE(first.type() == kernel::devices::pci::header_type::endpoint); REQUIRE(first.is_multifunction()); - auto const & second = devices->at(1); + auto const & second = devices.at(1); - REQUIRE(second.address == - kernel::devices::pci::function_address{.segment_group = 0, .bus = 0, .device = 2, .function = 1}); + REQUIRE(second.address == function_address{.segment_group = 0, .bus = bus, .device = device, .function = 1}); REQUIRE(second.vendor_id == vendor_id_ati); REQUIRE(second.device_id == device_id_hdmi_dp_audio_controller); REQUIRE(second.class_code == class_code_multimedia_controller); REQUIRE(second.subclass_code == subclass_code_audio_device); REQUIRE(second.programming_interface == programming_interface_hda_compatible_controller); + REQUIRE(second.revision == revision_id_c0); REQUIRE(second.type() == kernel::devices::pci::header_type::endpoint); } } GIVEN("A PCI bus with a single multi-function device with disjoint functions") { - auto builder = ecam_builder{}; - builder.clear(0); - build_amd_ai_pro_r9700_disjoint(builder, 0, 0); + constexpr auto bus = std::uint8_t{0}; + constexpr auto device = std::uint8_t{0}; + + place(multifunction(endpoint(amd_ai_pro_r9700_vga(bus, device)))); + place(endpoint(amd_ai_pro_r9700_audio(bus, device, 2))); + place(endpoint(amd_ai_pro_r9700_audio(bus, device, 7))); THEN("scanning the bus finds all functions of the multi-function device") { - auto ecam_segment = make_ecam_segment(0, 0, 255); - auto devices = kernel::devices::pci::scan_bus(ecam_segment, 0); + auto segment = ecam_segment{make_descriptor(0, 0)}; + auto devices = require_ok(kernel::devices::pci::scan_bus(segment, bus)); - REQUIRE(devices); - REQUIRE(devices->size() == 3); + REQUIRE(devices.size() == 3); - auto const & first = devices->at(0); + auto const & first = devices.at(0); - REQUIRE(first.address == - kernel::devices::pci::function_address{.segment_group = 0, .bus = 0, .device = 0, .function = 0}); + REQUIRE(first.address == function_address{.segment_group = 0, .bus = bus, .device = device, .function = 0}); REQUIRE(first.vendor_id == vendor_id_ati); REQUIRE(first.device_id == device_id_radeon_ai_pro_r9700); REQUIRE(first.class_code == class_code_display_controller); @@ -268,65 +209,60 @@ SCENARIO("Scanning PCI buses") REQUIRE(first.type() == kernel::devices::pci::header_type::endpoint); REQUIRE(first.is_multifunction()); - auto const & second = devices->at(1); + auto const & second = devices.at(1); - REQUIRE(second.address == - kernel::devices::pci::function_address{.segment_group = 0, .bus = 0, .device = 0, .function = 2}); + REQUIRE(second.address == function_address{.segment_group = 0, .bus = bus, .device = device, .function = 2}); REQUIRE(second.vendor_id == vendor_id_ati); REQUIRE(second.device_id == device_id_hdmi_dp_audio_controller); REQUIRE(second.class_code == class_code_multimedia_controller); REQUIRE(second.subclass_code == subclass_code_audio_device); REQUIRE(second.programming_interface == programming_interface_hda_compatible_controller); + REQUIRE(second.revision == revision_id_c0); REQUIRE(second.type() == kernel::devices::pci::header_type::endpoint); - auto const & third = devices->at(2); + auto const & third = devices.at(2); - REQUIRE(third.address == - kernel::devices::pci::function_address{.segment_group = 0, .bus = 0, .device = 0, .function = 7}); + REQUIRE(third.address == function_address{.segment_group = 0, .bus = bus, .device = device, .function = 7}); REQUIRE(third.vendor_id == vendor_id_ati); REQUIRE(third.device_id == device_id_hdmi_dp_audio_controller); REQUIRE(third.class_code == class_code_multimedia_controller); REQUIRE(third.subclass_code == subclass_code_audio_device); REQUIRE(third.programming_interface == programming_interface_hda_compatible_controller); + REQUIRE(third.revision == revision_id_c0); REQUIRE(third.type() == kernel::devices::pci::header_type::endpoint); } } GIVEN("A PCI bus with a single corrupt single-function device") { - auto builder = ecam_builder{}; - builder.clear(0); - build_corrupt_single_function_device(builder, 0, 3); + place(endpoint(amd_fch_sata_controller(0, 3, 1))); THEN("scanning the bus finds no devices") { - auto ecam_segment = make_ecam_segment(0, 0, 255); - auto devices = kernel::devices::pci::scan_bus(ecam_segment, 0); + auto segment = ecam_segment{make_descriptor(0, 0)}; + auto devices = require_ok(kernel::devices::pci::scan_bus(segment, 0)); - REQUIRE(devices); - REQUIRE(devices->empty()); + REQUIRE(devices.empty()); } } GIVEN("A PCI bus with two single-function devices at the extremes") { - auto builder = ecam_builder{}; - builder.clear(0); - build_amd_fch_sata_controller(builder, 0, 0); - build_amd_fch_sata_controller(builder, 0, devices_per_bus - 1); + constexpr auto bus = std::uint8_t{0}; + + place(endpoint(amd_fch_sata_controller(bus, 0))); + place(endpoint(amd_fch_sata_controller(bus, devices_per_bus - 1))); THEN("scanning the bus finds both devices") { - auto ecam_segment = make_ecam_segment(0, 0, 255); - auto devices = kernel::devices::pci::scan_bus(ecam_segment, 0); + auto segment = ecam_segment{make_descriptor(0, 0)}; + auto devices = require_ok(kernel::devices::pci::scan_bus(segment, 0)); - REQUIRE(devices); - REQUIRE(devices->size() == 2); + REQUIRE(devices.size() == 2); - auto const & first = devices->at(0); + auto const & first = devices.at(0); - REQUIRE(first.address == - kernel::devices::pci::function_address{.segment_group = 0, .bus = 0, .device = 0, .function = 0}); + REQUIRE(first.address == function_address{.segment_group = 0, .bus = bus, .device = 0, .function = 0}); REQUIRE(first.vendor_id == vendor_id_amd); REQUIRE(first.device_id == device_id_fch_sata_controller); REQUIRE(first.class_code == class_code_mass_storage_controller); @@ -336,12 +272,10 @@ SCENARIO("Scanning PCI buses") REQUIRE(first.type() == kernel::devices::pci::header_type::endpoint); REQUIRE(!first.is_multifunction()); - auto const & last = devices->at(1); + auto const & last = devices.at(1); - REQUIRE(last.address == kernel::devices::pci::function_address{.segment_group = 0, - .bus = 0, - .device = devices_per_bus - 1, - .function = 0}); + REQUIRE(last.address == + function_address{.segment_group = 0, .bus = bus, .device = devices_per_bus - 1, .function = 0}); REQUIRE(last.vendor_id == vendor_id_amd); REQUIRE(last.device_id == device_id_fch_sata_controller); REQUIRE(last.class_code == class_code_mass_storage_controller); @@ -355,8 +289,6 @@ SCENARIO("Scanning PCI buses") GIVEN("A PCI function with a different value in every identification register") { - auto builder = ecam_builder{}; - builder.clear(0); builder.function(0, 4, 0) .vendor_id(distinct_vendor_id) .device_id(distinct_device_id) @@ -368,13 +300,12 @@ SCENARIO("Scanning PCI buses") THEN("each register is read into the matching field") { - auto ecam_segment = make_ecam_segment(0, 0, 255); - auto devices = kernel::devices::pci::scan_bus(ecam_segment, 0); + auto segment = ecam_segment{make_descriptor(0, 0)}; + auto devices = require_ok(kernel::devices::pci::scan_bus(segment, 0)); - REQUIRE(devices); - REQUIRE(devices->size() == 1); + REQUIRE(devices.size() == 1); - auto const & function = devices->at(0); + auto const & function = devices.at(0); REQUIRE(function.vendor_id == distinct_vendor_id); REQUIRE(function.device_id == distinct_device_id); @@ -387,39 +318,36 @@ SCENARIO("Scanning PCI buses") GIVEN("A PCI bus with a single-function device and a stray function 1 next to it") { - auto builder = ecam_builder{}; - builder.clear(0); - build_single_function_device_with_stray_function(builder, 0, 6); + constexpr auto bus = std::uint8_t{0u}; + constexpr auto device = std::uint8_t{6u}; + + place(endpoint(amd_fch_sata_controller(bus, device, 0))); + place(endpoint(amd_fch_sata_controller(bus, device, 1))); THEN("scanning the bus finds only function 0") { - auto ecam_segment = make_ecam_segment(0, 0, 255); - auto devices = kernel::devices::pci::scan_bus(ecam_segment, 0); + auto segment = ecam_segment{make_descriptor(0, 0)}; + auto devices = require_ok(kernel::devices::pci::scan_bus(segment, 0)); - REQUIRE(devices); - REQUIRE(devices->size() == 1); + REQUIRE(devices.size() == 1); - REQUIRE(devices->at(0).address == - kernel::devices::pci::function_address{.segment_group = 0, .bus = 0, .device = 6, .function = 0}); + REQUIRE(devices.at(0).address == + function_address{.segment_group = 0, .bus = bus, .device = device, .function = 0}); } } GIVEN("A device on a bus other than bus 0 in a segment group other than 0") { - auto builder = ecam_builder{}; - builder.clear(2); - build_amd_fch_sata_controller(builder, 2, 5); + place(amd_fch_sata_controller(2, 5)); THEN("the address of the function carries the segment group and the bus") { - auto ecam_segment = make_ecam_segment(3, 0, 4); - auto devices = kernel::devices::pci::scan_bus(ecam_segment, 2); + auto segment = ecam_segment{make_descriptor(3, 0, 4)}; + auto devices = require_ok(kernel::devices::pci::scan_bus(segment, 2)); - REQUIRE(devices); - REQUIRE(devices->size() == 1); + REQUIRE(devices.size() == 1); - REQUIRE(devices->at(0).address == - kernel::devices::pci::function_address{.segment_group = 3, .bus = 2, .device = 5, .function = 0}); + REQUIRE(devices.at(0).address == function_address{.segment_group = 3, .bus = 2, .device = 5, .function = 0}); } } @@ -427,8 +355,8 @@ SCENARIO("Scanning PCI buses") { THEN("scanning fails with Invalid Argument") { - auto ecam_segment = make_ecam_segment(0, 0, 32); - auto devices = kernel::devices::pci::scan_bus(ecam_segment, 64); + auto segment = ecam_segment{make_descriptor(0, 0, 32)}; + auto devices = kernel::devices::pci::scan_bus(segment, 64); REQUIRE(!devices); REQUIRE(devices.error() == kstd::errc::invalid_argument); @@ -439,8 +367,11 @@ SCENARIO("Scanning PCI buses") { THEN("scanning fails with Not Enough Memory") { - auto ecam_segment = make_oob_ecam_segment(0, 0, 32); - auto devices = kernel::devices::pci::scan_bus(ecam_segment, 0); + auto descriptor = make_descriptor(0, 0, 32); + descriptor.base_address = builder.base + kernel::tests::memory::mmio_size; + + auto segment = ecam_segment{descriptor}; + auto devices = kernel::devices::pci::scan_bus(segment, 0); REQUIRE(!devices); REQUIRE(devices.error() == kstd::errc::not_enough_memory); diff --git a/kernel/kernel/test_support/devices/pci/ecam_fixture.cpp b/kernel/kernel/test_support/devices/pci/ecam_fixture.cpp new file mode 100644 index 00000000..402d5eb3 --- /dev/null +++ b/kernel/kernel/test_support/devices/pci/ecam_fixture.cpp @@ -0,0 +1,64 @@ +#include <kernel/test_support/devices/pci/ecam_fixture.hpp> + +#include <kernel/test_support/devices/pci/function_spec.hpp> +#include <kernel/test_support/memory.hpp> + +#include <kapi/devices.hpp> + +#include <cstddef> +#include <cstdint> + +namespace kernel::tests::devices::pci +{ + + auto ecam_fixture::endpoint(function_spec spec) -> function_spec + { + spec.header_type &= 0x80; + return spec; + } + + auto ecam_fixture::bridge(function_spec spec, std::uint8_t secondary, std::uint8_t subordinate) -> function_spec + { + spec.header_type = (spec.header_type & 0x80) | 0x01; + spec.class_code = 0x06; + spec.subclass_code = 0x04; + spec.secondary_bus = secondary; + spec.subordinate_bus = subordinate; + return spec; + } + + auto ecam_fixture::multifunction(function_spec spec) -> function_spec + { + spec.header_type |= 0x80; + return spec; + } + + ecam_fixture::ecam_fixture() + { + kernel::tests::memory::fill_mmio(std::byte{0xff}); + } + + auto ecam_fixture::make_descriptor(std::uint16_t group, std::uint8_t first_bus, std::size_t count) + -> kapi::devices::pcie_segment_descriptor + { + return kapi::devices::pcie_segment_descriptor{.base_address = builder.base, + .group_number = group, + .first_bus = first_bus, + .bus_count = count}; + } + + auto ecam_fixture::place(function_spec const & spec) -> void + { + builder.function(spec.bus, spec.device, spec.function) + .vendor_id(spec.vendor_id) + .device_id(spec.device_id) + .class_code(spec.class_code) + .subclass_code(spec.subclass_code) + .programming_interface(spec.programming_interface) + .revision_id(spec.revision) + .header_type(spec.header_type) + .secondary_bus(spec.secondary_bus) + .subordinate_bus(spec.subordinate_bus); + } + +} // namespace kernel::tests::devices::pci diff --git a/kernel/kernel/test_support/devices/pci/ecam_fixture.hpp b/kernel/kernel/test_support/devices/pci/ecam_fixture.hpp new file mode 100644 index 00000000..adbadb76 --- /dev/null +++ b/kernel/kernel/test_support/devices/pci/ecam_fixture.hpp @@ -0,0 +1,73 @@ +#ifndef TEACHOS_KERNEL_TEST_SUPPORT_DEVICES_PCI_ECAM_FIXTURE_HPP +#define TEACHOS_KERNEL_TEST_SUPPORT_DEVICES_PCI_ECAM_FIXTURE_HPP + +#include <kernel/test_support/devices/pci/ecam_builder.hpp> +#include <kernel/test_support/devices/pci/function_spec.hpp> +#include <kernel/test_support/memory.hpp> + +#include <kapi/devices.hpp> + +#include <kstd/units.hpp> + +#include <algorithm> +#include <cstddef> +#include <cstdint> + +namespace kernel::tests::devices::pci +{ + + struct ecam_fixture + { + //! The maximum number of PCI buses supported by this fixture. + //! + //! The value scales with the amount of available simulated MMIO space. + constexpr auto static max_number_of_buses = std::min(kernel::tests::memory::mmio_size / kstd::MiB(1u), 256uz); + + //! Apply the "endpoint" type to the given function specification. + //! + //! @param spec The function specification to modify. + //! @return The modified function specification. + [[nodiscard]] auto static endpoint(function_spec spec = {}) -> function_spec; + + //! Apply the "bridge" type to the given function specification. + //! + //! @param spec The function specification to modify. + //! @param secondary The secondary bus number for the bridge. + //! @param subordinate The subordinate bus number for the bridge. + //! @return The modified function specification. + [[nodiscard]] auto static bridge(function_spec spec, std::uint8_t secondary, std::uint8_t subordinate) + -> function_spec; + + //! Apply the "multifunction" flag to the given function specification. + //! + //! @param spec The function specification to modify. + //! @return The modified function specification. + [[nodiscard]] auto static multifunction(function_spec spec) -> function_spec; + + //! Construct a new ECAM fixture. + //! + //! This constructor clears all MMIO memory. + ecam_fixture(); + + //! Create a new PCI segment descriptor. + //! + //! @param group The PCI segment group number. + //! @param first_bus The first bus number in the segment. + //! @param count The number of buses in the segment. + //! @return The constructed PCI segment descriptor. + [[nodiscard]] auto make_descriptor(std::uint16_t group = 0, std::uint8_t first_bus = 0, + std::size_t count = max_number_of_buses) + -> kapi::devices::pcie_segment_descriptor; + + //! Place a PCI function according to the given specification. + //! + //! @param spec The function specification describing the PCI function to place. + auto place(function_spec const & spec) -> void; + + //! The ECAM builder used to construct the PCI configuration space. + ecam_builder builder{}; + }; + +} // namespace kernel::tests::devices::pci + +#endif diff --git a/kernel/kernel/test_support/devices/pci/function_addresses.cpp b/kernel/kernel/test_support/devices/pci/function_addresses.cpp new file mode 100644 index 00000000..bdc93b7f --- /dev/null +++ b/kernel/kernel/test_support/devices/pci/function_addresses.cpp @@ -0,0 +1,30 @@ +#include <kernel/test_support/devices/pci/function_addresses.hpp> + +#include <kernel/devices/pci/function_address.hpp> +#include <kernel/devices/pci/function_info.hpp> + +#include <kstd/vector.hpp> + +#include <cstdint> +#include <initializer_list> +#include <ranges> +#include <vector> + +namespace kernel::tests::devices::pci +{ + + auto addresses(kstd::vector<kernel::devices::pci::function_info> const & functions) + -> std::vector<kernel::devices::pci::function_address> + { + return {std::from_range, std::views::transform(functions, [](auto const & info) { return info.address; })}; + } + + auto addresses_of(std::initializer_list<function_location> list, std::uint16_t group) + -> std::vector<kernel::devices::pci::function_address> + { + return {std::from_range, std::views::transform(list, [group](auto const & loc) { + return kernel::devices::pci::function_address{group, loc.bus, loc.device, loc.function}; + })}; + } + +} // namespace kernel::tests::devices::pci diff --git a/kernel/kernel/test_support/devices/pci/function_addresses.hpp b/kernel/kernel/test_support/devices/pci/function_addresses.hpp new file mode 100644 index 00000000..64fff6e5 --- /dev/null +++ b/kernel/kernel/test_support/devices/pci/function_addresses.hpp @@ -0,0 +1,40 @@ +#ifndef TEACHOS_KERNEL_TEST_SUPPORT_DEVICES_PCI_FUNCTION_ADDRESSES_HPP +#define TEACHOS_KERNEL_TEST_SUPPORT_DEVICES_PCI_FUNCTION_ADDRESSES_HPP + +#include <kernel/devices/pci/function_address.hpp> +#include <kernel/devices/pci/function_info.hpp> + +#include <kstd/vector.hpp> + +#include <cstdint> +#include <initializer_list> +#include <vector> + +namespace kernel::tests::devices::pci +{ + + struct function_location + { + std::uint8_t bus{}; + std::uint8_t device{}; + std::uint8_t function{}; + }; + + //! Extract the function addresses from a list of function info structures. + //! + //! @param functions The list of function info structures. + //! @return A vector of function addresses corresponding to the input functions. + auto addresses(kstd::vector<kernel::devices::pci::function_info> const & functions) + -> std::vector<kernel::devices::pci::function_address>; + + //! Create a list of function addresses from a list of function locations. + //! + //! @param list The list of function locations. + //! @param group The group identifier to use for all function addresses. + //! @return A vector of function addresses corresponding to the input locations. + auto addresses_of(std::initializer_list<function_location> list, std::uint16_t group = 0) + -> std::vector<kernel::devices::pci::function_address>; + +} // namespace kernel::tests::devices::pci + +#endif diff --git a/kernel/kernel/test_support/devices/pci/function_spec.hpp b/kernel/kernel/test_support/devices/pci/function_spec.hpp new file mode 100644 index 00000000..e240322a --- /dev/null +++ b/kernel/kernel/test_support/devices/pci/function_spec.hpp @@ -0,0 +1,26 @@ +#ifndef TEACHOS_KERNEL_TEST_SUPPORT_DEVICES_PCI_FUNCTION_SPEC_HPP +#define TEACHOS_KERNEL_TEST_SUPPORT_DEVICES_PCI_FUNCTION_SPEC_HPP + +#include <cstdint> +namespace kernel::tests::devices::pci +{ + + struct function_spec + { + std::uint8_t bus{}; + std::uint8_t device{}; + std::uint8_t function{}; + std::uint16_t vendor_id{0x1234}; // NOLINT(readability-magic-numbers) + std::uint16_t device_id{0x5678}; // NOLINT(readability-magic-numbers) + std::uint8_t class_code{}; + std::uint8_t subclass_code{}; + std::uint8_t programming_interface{}; + std::uint8_t revision{}; + std::uint8_t header_type{}; + std::uint8_t secondary_bus{}; + std::uint8_t subordinate_bus{}; + }; + +} // namespace kernel::tests::devices::pci + +#endif diff --git a/kernel/kernel/test_support/kapi/memory.cpp b/kernel/kernel/test_support/kapi/memory.cpp index 78b24f43..75f987bd 100644 --- a/kernel/kernel/test_support/kapi/memory.cpp +++ b/kernel/kernel/test_support/kapi/memory.cpp @@ -6,6 +6,7 @@ #include <kapi/memory.hpp> +#include <cstddef> #include <optional> namespace @@ -67,4 +68,9 @@ namespace kernel::tests::memory return test_mapper->memory.virtual_base(); } -} // namespace kernel::tests::memory
\ No newline at end of file + auto fill_mmio(std::byte value) -> void + { + test_mapper->mmio.fill(value); + } + +} // namespace kernel::tests::memory diff --git a/kernel/kernel/test_support/memory.hpp b/kernel/kernel/test_support/memory.hpp index 8565a53a..87530ce5 100644 --- a/kernel/kernel/test_support/memory.hpp +++ b/kernel/kernel/test_support/memory.hpp @@ -5,6 +5,8 @@ #include <kstd/units.hpp> +#include <cstddef> + namespace kernel::tests::memory { @@ -33,6 +35,11 @@ namespace kernel::tests::memory //! Deinitialize the memory subsystem. auto deinit() -> void; + //! Fill MMIO memory with the given value. + //! + //! @param value The bit pattern to fill the MMIO memory with. + auto fill_mmio(std::byte value) -> void; + //! Get the virtual base address of the simulated system. //! //! Since we do not have direct access to an actual MMU, we simulate the virtual address space by mapping the @@ -43,4 +50,4 @@ namespace kernel::tests::memory } // namespace kernel::tests::memory -#endif
\ No newline at end of file +#endif diff --git a/kernel/kernel/test_support/simulated_memory.cpp b/kernel/kernel/test_support/simulated_memory.cpp index b6c3c4d9..e7ad125b 100644 --- a/kernel/kernel/test_support/simulated_memory.cpp +++ b/kernel/kernel/test_support/simulated_memory.cpp @@ -65,7 +65,12 @@ namespace kernel::tests auto simulated_memory::clear() -> void { - std::memset(m_physical_base.get(), 0, m_physical_size.value); + fill(std::byte{0x00}); + } + + auto simulated_memory::fill(std::byte value) -> void + { + std::memset(m_physical_base.get(), static_cast<int>(value), m_physical_size.value); } auto simulated_memory::physical_base() noexcept -> kstd::observer_ptr<std::byte> @@ -105,4 +110,4 @@ namespace kernel::tests return kstd::observer_ptr{static_cast<std::byte *>(mapped_ptr)}; } -} // namespace kernel::tests
\ No newline at end of file +} // namespace kernel::tests diff --git a/kernel/kernel/test_support/simulated_memory.hpp b/kernel/kernel/test_support/simulated_memory.hpp index 128ef715..87a606d7 100644 --- a/kernel/kernel/test_support/simulated_memory.hpp +++ b/kernel/kernel/test_support/simulated_memory.hpp @@ -34,6 +34,11 @@ namespace kernel::tests //! Clear the contents of the physical memory of this device. auto clear() -> void; + //! Fill the contents of the physical memory of this device with the given value. + //! + //! @param value The bit pattern to fill the memory with. + auto fill(std::byte value) -> void; + //! Get the base address of the physical memory of this device. [[nodiscard]] auto physical_base() noexcept -> kstd::observer_ptr<std::byte>; @@ -67,4 +72,4 @@ namespace kernel::tests } // namespace kernel::tests -#endif
\ No newline at end of file +#endif |
