aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-11-18 08:39:07 +0100
committerFelix Morgner <felix.morgner@ost.ch>2026-11-22 14:20:36 +0100
commit2f9972781b66fd350f1975b2975f8998d5d07f13 (patch)
tree9985269da3fd0db07497508925f90060a533305c
parent05115c3961d316fd52409a753a88dd573cab4860 (diff)
downloadkernel-fmorgner/pcie-scan.tar.xz
kernel-fmorgner/pcie-scan.zip
kernel/devices: extract bus scanning implementationfmorgner/pcie-scan
This change extracts the PCI bus scanning logic, which previously was hidden inside an anonymous namespace, into a dedicated function. This allows for the implementation to be tested independently.
-rw-r--r--cspell.json2
-rw-r--r--kernel/CMakeLists.txt1
-rw-r--r--kernel/kernel/devices/pci/init.cpp76
-rw-r--r--kernel/kernel/devices/pci/registers.hpp12
-rw-r--r--kernel/kernel/devices/pci/scan.cpp97
-rw-r--r--kernel/kernel/devices/pci/scan.hpp29
-rw-r--r--kernel/kernel/devices/pci/scan.tests.cpp449
-rw-r--r--kernel/kernel/test_support/devices/pci/ecam_builder.cpp106
-rw-r--r--kernel/kernel/test_support/devices/pci/ecam_builder.hpp124
-rw-r--r--kernel/kernel/test_support/devices/pci/ecam_builder.tests.cpp162
10 files changed, 1008 insertions, 50 deletions
diff --git a/cspell.json b/cspell.json
index 6f37d13e..702d691c 100644
--- a/cspell.json
+++ b/cspell.json
@@ -5,6 +5,7 @@
"dictionaries": [],
"words": [
"acpi",
+ "ahci",
"APIC",
"bugprone",
"cardbus",
@@ -42,6 +43,7 @@
"nullptr",
"println",
"progbits",
+ "radeon",
"raii",
"ramdisk",
"rdmsr",
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt
index df65ed20..7f418c61 100644
--- a/kernel/CMakeLists.txt
+++ b/kernel/CMakeLists.txt
@@ -45,6 +45,7 @@ target_sources("kernel_lib" PRIVATE
"kernel/devices/init.cpp"
"kernel/devices/pci/ecam_segment.cpp"
"kernel/devices/pci/init.cpp"
+ "kernel/devices/pci/scan.cpp"
"kernel/devices/pseudo.cpp"
"kernel/devices/storage.cpp"
diff --git a/kernel/kernel/devices/pci/init.cpp b/kernel/kernel/devices/pci/init.cpp
index 05f5e7d1..6abf758c 100644
--- a/kernel/kernel/devices/pci/init.cpp
+++ b/kernel/kernel/devices/pci/init.cpp
@@ -1,12 +1,13 @@
#include <kernel/devices/pci/init.hpp>
#include <kernel/devices/pci/ecam_segment.hpp>
-#include <kernel/devices/pci/registers.hpp>
+#include <kernel/devices/pci/function_info.hpp>
+#include <kernel/devices/pci/scan.hpp>
#include <kapi/devices.hpp>
-#include <kapi/memory.hpp>
#include <kstd/print.hpp>
+#include <kstd/system_error.hpp>
#include <kstd/vector.hpp>
#include <cstddef>
@@ -15,68 +16,43 @@
namespace kernel::devices::pci
{
- namespace
+ auto init() -> void
{
+ auto descriptors = kapi::devices::get_pci_express_segment_descriptors();
+
+ if (descriptors.empty())
+ {
+ return;
+ }
+
+ kstd::println("[OS:DEV] Detected number of PCI Express segments: {}", descriptors.size());
- auto enumerate(kapi::devices::pcie_segment_descriptor const & descriptor) -> void
+ for (auto const & descriptor : descriptors)
{
auto segment = ecam_segment{descriptor};
for (std::size_t bus = segment.first_bus(); segment.contains(bus); ++bus)
{
- // Note: ecam_segment::contains(...) == true guarantees the bus number fits in a uint8_t
- auto bus_window = segment.map_bus(static_cast<std::uint8_t>(bus));
-
- if (!bus_window)
+ auto functions = scan_bus(segment, static_cast<std::uint8_t>(bus));
+ if (!functions)
{
- kstd::println("[OS:DEV] Failed to map MMIO window for bus {}", bus);
- continue;
- }
-
- for (std::uint8_t device = 0; device < devices_per_bus; ++device)
- {
- auto device_base = function_offset(device, 0);
- auto device_vendor_id = bus_window->read<std::uint16_t>(device_base + vendor_id_register_offset);
- if (device_vendor_id == invalid_vendor_id)
+ if (functions.error() == kstd::errc::not_enough_memory)
{
- continue;
+ kstd::println(kstd::print_sink::stderr, "[OS:DEV] Failed to map MMIO window for bus {}", bus);
}
-
- auto device_header_type = bus_window->read<std::uint8_t>(device_base + header_type_register_offset);
- auto function_count = (device_header_type & 0x80) ? functions_per_device : 1u;
-
- for (std::uint8_t function = 0; function < function_count; ++function)
+ else
{
- auto function_base = function_offset(device, function);
- auto function_vendor_id = bus_window->read<std::uint16_t>(function_base + vendor_id_register_offset);
- if (function_vendor_id == invalid_vendor_id)
- {
- continue;
- }
-
- auto function_device_id = bus_window->read<std::uint16_t>(function_base + device_id_register_offset);
- kstd::println("[OS:DEV] Detected PCI Express device: {:04x}:{:02x}:{:02x}.{:1} [{:04x}:{:04x}]",
- segment.group(), bus, device, function, function_vendor_id, function_device_id);
+ kstd::println(kstd::print_sink::stderr, "[OS:DEV] Failed to scan bus {}: {}", bus, functions.error());
}
+ continue;
}
- }
- }
-
- } // namespace
-
- auto init() -> void
- {
- auto segments = kapi::devices::get_pci_express_segment_descriptors();
-
- if (segments.empty())
- {
- return;
- }
- kstd::println("[OS:DEV] Detected number of PCI Express segments: {}", segments.size());
- for (auto & segment : segments)
- {
- enumerate(segment);
+ for (auto const & function : *functions)
+ {
+ kstd::println("[OS:DEV] Detected PCI Express device: {} [{:04x}:{:04x}]", function.address,
+ function.vendor_id, function.device_id);
+ }
+ }
}
}
diff --git a/kernel/kernel/devices/pci/registers.hpp b/kernel/kernel/devices/pci/registers.hpp
index 2e426be1..98136070 100644
--- a/kernel/kernel/devices/pci/registers.hpp
+++ b/kernel/kernel/devices/pci/registers.hpp
@@ -43,6 +43,18 @@ namespace kernel::devices::pci
//! The offset of the device ID register within the PCI configuration space.
constexpr auto inline device_id_register_offset = kstd::bytes{0x02};
+ //! The offset of the revision ID register within the PCI configuration space.
+ constexpr auto inline revision_id_register_offset = kstd::bytes{0x08};
+
+ //! The offset of the programming interface register within the PCI configuration space.
+ constexpr auto inline programming_interface_register_offset = kstd::bytes{0x09};
+
+ //! The offset of the subclass register within the PCI configuration space.
+ constexpr auto inline subclass_register_offset = kstd::bytes{0x0a};
+
+ //! The offset of the class code register within the PCI configuration space.
+ constexpr auto inline class_code_register_offset = kstd::bytes{0x0b};
+
//! The offset of the header type register within the PCI configuration space.
constexpr auto inline header_type_register_offset = kstd::bytes{0x0e};
diff --git a/kernel/kernel/devices/pci/scan.cpp b/kernel/kernel/devices/pci/scan.cpp
new file mode 100644
index 00000000..1cad8b4e
--- /dev/null
+++ b/kernel/kernel/devices/pci/scan.cpp
@@ -0,0 +1,97 @@
+#include <kernel/devices/pci/scan.hpp>
+
+#include <kernel/devices/pci/ecam_segment.hpp>
+#include <kernel/devices/pci/function_info.hpp>
+#include <kernel/devices/pci/registers.hpp>
+
+#include <kapi/memory.hpp>
+
+#include <kstd/result.hpp>
+#include <kstd/vector.hpp>
+
+#include <cstdint>
+#include <optional>
+#include <utility>
+
+namespace kernel::devices::pci
+{
+
+ namespace
+ {
+
+ auto read_function(kapi::memory::mmio_window const & ecam_window, std::uint16_t group, std::uint8_t bus,
+ std::uint8_t device, std::uint8_t number) -> std::optional<function_info>
+ {
+ auto offset = function_offset(device, number);
+
+ auto vendor_id = ecam_window.read<std::uint16_t>(offset + vendor_id_register_offset);
+ if (vendor_id == invalid_vendor_id)
+ {
+ return std::nullopt;
+ }
+
+ auto device_id = ecam_window.read<std::uint16_t>(offset + device_id_register_offset);
+ auto revision = ecam_window.read<std::uint8_t>(offset + revision_id_register_offset);
+ auto programming_interface = ecam_window.read<std::uint8_t>(offset + programming_interface_register_offset);
+ auto class_code = ecam_window.read<std::uint8_t>(offset + class_code_register_offset);
+ auto subclass = ecam_window.read<std::uint8_t>(offset + subclass_register_offset);
+ auto header_type = ecam_window.read<std::uint8_t>(offset + header_type_register_offset);
+
+ return function_info{
+ .address = {.segment_group = group, .bus = bus, .device = device, .function = number},
+ .vendor_id = vendor_id,
+ .device_id = device_id,
+ .revision = revision,
+ .programming_interface = programming_interface,
+ .subclass_code = subclass,
+ .class_code = class_code,
+ .raw_header_type = header_type
+ };
+ }
+
+ } // namespace
+
+ auto scan_bus(ecam_segment const & segment, std::uint8_t bus_number) -> kstd::result<kstd::vector<function_info>>
+ {
+ auto bus = segment.map_bus(bus_number);
+
+ if (!bus)
+ {
+ return kstd::failure(bus.error());
+ }
+
+ auto functions = kstd::vector<function_info>{};
+
+ for (std::uint8_t device = 0; device < devices_per_bus; ++device)
+ {
+ for (std::uint8_t function = 0; function < functions_per_device; ++function)
+ {
+ auto maybe_function = read_function(*bus, segment.group(), bus_number, device, function);
+ if (!maybe_function)
+ {
+ // According to the PCI specification, a valid device must have a function 0.
+ if (function == 0)
+ {
+ break;
+ }
+ continue;
+ }
+
+ functions.push_back(*maybe_function);
+
+ // The "multifunction" bit is only valid for function 0, otherwise it should be ignored.
+ if (function == 0)
+ {
+ auto is_multifunction_device = maybe_function->is_multifunction();
+ if (!is_multifunction_device)
+ {
+ break;
+ }
+ }
+ }
+ }
+
+ return kstd::success(std::move(functions));
+ }
+
+} // namespace kernel::devices::pci
diff --git a/kernel/kernel/devices/pci/scan.hpp b/kernel/kernel/devices/pci/scan.hpp
new file mode 100644
index 00000000..1c10d979
--- /dev/null
+++ b/kernel/kernel/devices/pci/scan.hpp
@@ -0,0 +1,29 @@
+#ifndef TEACHOS_KERNEL_DEVICES_PCI_SCAN_HPP
+#define TEACHOS_KERNEL_DEVICES_PCI_SCAN_HPP
+
+#include <kernel/devices/pci/ecam_segment.hpp>
+#include <kernel/devices/pci/function_info.hpp>
+
+#include <kstd/result.hpp>
+#include <kstd/vector.hpp>
+
+#include <cstdint>
+
+namespace kernel::devices::pci
+{
+
+ //! Scan a given PCI bus within a given ECAM segment.
+ //!
+ //! This function scans the specified PCI bus within the given ECAM segment for available functions. All found
+ //! functions are returned in ascending address order. Functions violating the PCI specification area
+ //!
+ //! @param segment The ECAM segment to scan.
+ //! @param bus_number The PCI bus number to scan.
+ //! @return A vector of function_info objects representing the functions found on the bus on success, an error
+ //! otherwise
+ [[nodiscard]] auto scan_bus(ecam_segment const & segment, std::uint8_t bus_number)
+ -> kstd::result<kstd::vector<function_info>>;
+
+} // namespace kernel::devices::pci
+
+#endif
diff --git a/kernel/kernel/devices/pci/scan.tests.cpp b/kernel/kernel/devices/pci/scan.tests.cpp
new file mode 100644
index 00000000..8915e2e0
--- /dev/null
+++ b/kernel/kernel/devices/pci/scan.tests.cpp
@@ -0,0 +1,449 @@
+#include <kernel/devices/pci/scan.hpp>
+
+#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/memory.hpp>
+
+#include <kapi/devices.hpp>
+#include <kapi/memory.hpp>
+
+#include <kstd/system_error.hpp>
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <cstdint>
+
+using kernel::tests::devices::pci::ecam_builder;
+
+namespace
+{
+ constexpr auto test_ecam_base = kapi::memory::physical_address{};
+
+ constexpr auto vendor_id_ati = 0x1002;
+ constexpr auto vendor_id_amd = 0x1022;
+
+ constexpr auto device_id_radeon_ai_pro_r9700 = 0x7551;
+ constexpr auto device_id_fch_sata_controller = 0x7901;
+ constexpr auto device_id_hdmi_dp_audio_controller = 0xab40;
+
+ constexpr auto programming_interface_vga_controller = 0x00;
+ constexpr auto programming_interface_ahci_controller = 0x01;
+ constexpr auto programming_interface_hda_compatible_controller = 0x00;
+
+ constexpr auto revision_id_c0 = 0xc0;
+ constexpr auto revision_id_51 = 0x51;
+
+ constexpr auto subclass_code_vga_compatible_controller = 0x00;
+ constexpr auto subclass_code_audio_device = 0x03;
+ constexpr auto subclass_code_ahci_controller = 0x06;
+
+ constexpr auto class_code_mass_storage_controller = 0x01;
+ 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;
+ constexpr auto distinct_device_id = 0x5678;
+ constexpr auto distinct_revision_id = 0x11;
+ constexpr auto distinct_programming_interface = 0x22;
+ constexpr auto distinct_subclass_code = 0x33;
+ constexpr auto distinct_class_code = 0x44;
+
+ 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)
+ {
+ 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);
+ }
+
+ auto build_corrupt_single_function_device(ecam_builder & builder, std::uint8_t bus, std::uint8_t device)
+ {
+ 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);
+ }
+
+ auto build_single_function_device_with_stray_function(ecam_builder & builder, std::uint8_t bus, std::uint8_t device)
+ {
+ 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);
+ }
+
+ 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")
+{
+ 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);
+
+ REQUIRE(devices);
+ 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);
+
+ 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);
+
+ REQUIRE(devices);
+ REQUIRE(devices->size() == 1);
+
+ 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.vendor_id == vendor_id_amd);
+ REQUIRE(device.device_id == device_id_fch_sata_controller);
+ REQUIRE(device.class_code == class_code_mass_storage_controller);
+ REQUIRE(device.subclass_code == subclass_code_ahci_controller);
+ REQUIRE(device.programming_interface == programming_interface_ahci_controller);
+ REQUIRE(device.revision == revision_id_51);
+ REQUIRE(device.type() == kernel::devices::pci::header_type::endpoint);
+ REQUIRE(!device.is_multifunction());
+ }
+ }
+
+ 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);
+
+ 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);
+
+ REQUIRE(devices);
+ REQUIRE(devices->size() == 2);
+
+ 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.vendor_id == vendor_id_ati);
+ REQUIRE(first.device_id == device_id_radeon_ai_pro_r9700);
+ REQUIRE(first.class_code == class_code_display_controller);
+ REQUIRE(first.subclass_code == subclass_code_vga_compatible_controller);
+ REQUIRE(first.programming_interface == programming_interface_vga_controller);
+ REQUIRE(first.revision == revision_id_c0);
+ REQUIRE(first.type() == kernel::devices::pci::header_type::endpoint);
+ REQUIRE(first.is_multifunction());
+
+ 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.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.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);
+
+ 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);
+
+ REQUIRE(devices);
+ REQUIRE(devices->size() == 3);
+
+ 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.vendor_id == vendor_id_ati);
+ REQUIRE(first.device_id == device_id_radeon_ai_pro_r9700);
+ REQUIRE(first.class_code == class_code_display_controller);
+ REQUIRE(first.subclass_code == subclass_code_vga_compatible_controller);
+ REQUIRE(first.programming_interface == programming_interface_vga_controller);
+ REQUIRE(first.revision == revision_id_c0);
+ REQUIRE(first.type() == kernel::devices::pci::header_type::endpoint);
+ REQUIRE(first.is_multifunction());
+
+ 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.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.type() == kernel::devices::pci::header_type::endpoint);
+
+ 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.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.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);
+
+ 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);
+
+ REQUIRE(devices);
+ 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);
+
+ 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);
+
+ REQUIRE(devices);
+ REQUIRE(devices->size() == 2);
+
+ 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.vendor_id == vendor_id_amd);
+ REQUIRE(first.device_id == device_id_fch_sata_controller);
+ REQUIRE(first.class_code == class_code_mass_storage_controller);
+ REQUIRE(first.subclass_code == subclass_code_ahci_controller);
+ REQUIRE(first.programming_interface == programming_interface_ahci_controller);
+ REQUIRE(first.revision == revision_id_51);
+ REQUIRE(first.type() == kernel::devices::pci::header_type::endpoint);
+ REQUIRE(!first.is_multifunction());
+
+ 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.vendor_id == vendor_id_amd);
+ REQUIRE(last.device_id == device_id_fch_sata_controller);
+ REQUIRE(last.class_code == class_code_mass_storage_controller);
+ REQUIRE(last.subclass_code == subclass_code_ahci_controller);
+ REQUIRE(last.programming_interface == programming_interface_ahci_controller);
+ REQUIRE(last.revision == revision_id_51);
+ REQUIRE(last.type() == kernel::devices::pci::header_type::endpoint);
+ REQUIRE(!last.is_multifunction());
+ }
+ }
+
+ 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)
+ .revision_id(distinct_revision_id)
+ .programming_interface(distinct_programming_interface)
+ .subclass_code(distinct_subclass_code)
+ .class_code(distinct_class_code)
+ .header_type(header_type_endpoint);
+
+ 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);
+
+ REQUIRE(devices);
+ REQUIRE(devices->size() == 1);
+
+ auto const & function = devices->at(0);
+
+ REQUIRE(function.vendor_id == distinct_vendor_id);
+ REQUIRE(function.device_id == distinct_device_id);
+ REQUIRE(function.revision == distinct_revision_id);
+ REQUIRE(function.programming_interface == distinct_programming_interface);
+ REQUIRE(function.subclass_code == distinct_subclass_code);
+ REQUIRE(function.class_code == distinct_class_code);
+ }
+ }
+
+ 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);
+
+ 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);
+
+ REQUIRE(devices);
+ REQUIRE(devices->size() == 1);
+
+ REQUIRE(devices->at(0).address ==
+ kernel::devices::pci::function_address{.segment_group = 0, .bus = 0, .device = 6, .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);
+
+ 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);
+
+ REQUIRE(devices);
+ REQUIRE(devices->size() == 1);
+
+ REQUIRE(devices->at(0).address ==
+ kernel::devices::pci::function_address{.segment_group = 3, .bus = 2, .device = 5, .function = 0});
+ }
+ }
+
+ GIVEN("A bus number outside the segment")
+ {
+ 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);
+
+ REQUIRE(!devices);
+ REQUIRE(devices.error() == kstd::errc::invalid_argument);
+ }
+ }
+
+ GIVEN("A bus outside the MMIO space")
+ {
+ 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);
+
+ REQUIRE(!devices);
+ REQUIRE(devices.error() == kstd::errc::not_enough_memory);
+ }
+ }
+}
diff --git a/kernel/kernel/test_support/devices/pci/ecam_builder.cpp b/kernel/kernel/test_support/devices/pci/ecam_builder.cpp
new file mode 100644
index 00000000..78b0b32c
--- /dev/null
+++ b/kernel/kernel/test_support/devices/pci/ecam_builder.cpp
@@ -0,0 +1,106 @@
+#include <kernel/test_support/devices/pci/ecam_builder.hpp>
+
+#include <kapi/memory.hpp>
+
+#include <kstd/units.hpp>
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <cstdint>
+#include <utility>
+
+using kapi::memory::mmio_window;
+
+namespace kernel::tests::devices::pci
+{
+
+ ecam_builder::function_builder::function_builder(ecam_builder & root, std::uint8_t bus, std::uint8_t device,
+ std::uint8_t number)
+ : m_root(&root)
+ , m_window(root.window_for(bus, device, number))
+ {}
+
+ auto ecam_builder::function_builder::vendor_id(std::uint16_t id) -> function_builder &
+ {
+ m_window.write<std::uint16_t>(vendor_id_offset, id);
+ return *this;
+ }
+
+ auto ecam_builder::function_builder::device_id(std::uint16_t id) -> function_builder &
+ {
+ m_window.write<std::uint16_t>(device_id_offset, id);
+ return *this;
+ }
+
+ auto ecam_builder::function_builder::class_code(std::uint8_t code) -> function_builder &
+ {
+ m_window.write<std::uint8_t>(class_code_offset, code);
+ return *this;
+ }
+
+ auto ecam_builder::function_builder::header_type(std::uint8_t type) -> function_builder &
+ {
+ m_window.write<std::uint8_t>(header_type_offset, type);
+ return *this;
+ }
+
+ auto ecam_builder::function_builder::subclass_code(std::uint8_t id) -> function_builder &
+ {
+ m_window.write<std::uint8_t>(subclass_code_offset, id);
+ return *this;
+ }
+
+ auto ecam_builder::function_builder::programming_interface(std::uint8_t id) -> function_builder &
+ {
+ m_window.write<std::uint8_t>(programming_interface_offset, id);
+ return *this;
+ }
+
+ auto ecam_builder::function_builder::revision_id(std::uint8_t id) -> function_builder &
+ {
+ m_window.write<std::uint8_t>(revision_id_offset, id);
+ return *this;
+ }
+
+ auto ecam_builder::function_builder::function(std::uint8_t bus, std::uint8_t device, std::uint8_t number)
+ -> function_builder
+ {
+ return m_root->function(bus, device, number);
+ }
+
+ auto ecam_builder::clear(std::uint8_t bus) -> void
+ {
+ for (std::uint8_t device_number = 0; device_number < max_devices_per_bus; ++device_number)
+ {
+ for (std::uint8_t function_number = 0; function_number < max_functions_per_device; ++function_number)
+ {
+ auto window = window_for(bus, device_number, function_number);
+ for (auto offset = kstd::bytes{0}; offset < pci_function_size; offset += kstd::bytes{8})
+ {
+ window.write<std::uint64_t>(offset, pci_clear_pattern);
+ }
+ }
+ }
+ }
+
+ auto ecam_builder::function(std::uint8_t bus, std::uint8_t device, std::uint8_t number) -> function_builder
+ {
+ return function_builder(*this, bus, device, number);
+ }
+
+ auto ecam_builder::window_for(std::uint8_t bus_number, std::uint8_t device_number, std::uint8_t function_number)
+ -> kapi::memory::mmio_window
+ {
+ REQUIRE(device_number < max_devices_per_bus);
+ REQUIRE(function_number < max_functions_per_device);
+
+ auto address = base + ((bus_number << bus_number_address_shift) | (device_number << device_number_address_shift) |
+ (function_number << function_number_address_shift));
+ auto window = mmio_window::map(address, pci_function_size, mmio_window::access::writable);
+
+ REQUIRE(window);
+
+ return std::move(*window);
+ }
+
+} // namespace kernel::tests::devices::pci
diff --git a/kernel/kernel/test_support/devices/pci/ecam_builder.hpp b/kernel/kernel/test_support/devices/pci/ecam_builder.hpp
new file mode 100644
index 00000000..31334959
--- /dev/null
+++ b/kernel/kernel/test_support/devices/pci/ecam_builder.hpp
@@ -0,0 +1,124 @@
+#ifndef TEACHOS_KERNEL_TEST_SUPPORT_DEVICES_PCI_ECAM_BUILDER_HPP
+#define TEACHOS_KERNEL_TEST_SUPPORT_DEVICES_PCI_ECAM_BUILDER_HPP
+
+#include <kapi/memory.hpp>
+
+#include <kstd/units.hpp>
+
+#include <cstdint>
+
+namespace kernel::tests::devices::pci
+{
+
+ //! A builder for constructing PCI ECAM data for testing purposes.
+ struct ecam_builder
+ {
+ constexpr auto static pci_clear_pattern = 0xffff'ffff'ffff'ffff;
+
+ constexpr auto static bus_number_address_shift = 20;
+ constexpr auto static device_number_address_shift = 15;
+ constexpr auto static function_number_address_shift = 12;
+
+ constexpr auto static max_devices_per_bus = 32;
+ constexpr auto static max_functions_per_device = 8;
+ constexpr auto static pci_function_size = kstd::KiB(4u);
+
+ constexpr auto static vendor_id_offset = kstd::bytes{0x00};
+ constexpr auto static device_id_offset = kstd::bytes{0x02};
+ constexpr auto static revision_id_offset = kstd::bytes{0x08};
+ constexpr auto static programming_interface_offset = kstd::bytes{0x09};
+ constexpr auto static subclass_code_offset = kstd::bytes{0x0a};
+ constexpr auto static class_code_offset = kstd::bytes{0x0b};
+ constexpr auto static header_type_offset = kstd::bytes{0x0e};
+
+ struct function_builder
+ {
+ explicit function_builder(ecam_builder & root, std::uint8_t bus, std::uint8_t device, std::uint8_t number);
+
+ function_builder(function_builder const &) = delete;
+ auto operator=(function_builder const &) -> function_builder & = delete;
+ function_builder(function_builder &&) = delete;
+ auto operator=(function_builder &&) -> function_builder & = delete;
+
+ //! Set the vendor ID for this function.
+ //!
+ //! @param id The vendor ID to set.
+ //! @return A reference to this function builder.
+ auto vendor_id(std::uint16_t id) -> function_builder &;
+
+ //! Set the device ID for this function.
+ //!
+ //! @param id The device ID to set.
+ //! @return A reference to this function builder.
+ auto device_id(std::uint16_t id) -> function_builder &;
+
+ //! Set the class code for this function.
+ //!
+ //! @param code The class code to set.
+ //! @return A reference to this function builder.
+ auto class_code(std::uint8_t code) -> function_builder &;
+
+ //! Set the header type for this function.
+ //!
+ //! @param type The header type to set.
+ //! @return A reference to this function builder.
+ auto header_type(std::uint8_t type) -> function_builder &;
+
+ //! Set the subclass code for this function.
+ //!
+ //! @param id The subclass code to set.
+ //! @return A reference to this function builder.
+ auto subclass_code(std::uint8_t id) -> function_builder &;
+
+ //! Set the programming interface for this function.
+ //!
+ //! @param id The programming interface to set.
+ //! @return A reference to this function builder.
+ auto programming_interface(std::uint8_t id) -> function_builder &;
+
+ //! Set the revision ID for this function.
+ //!
+ //! @param id The revision ID to set.
+ //! @return A reference to this function builder.
+ auto revision_id(std::uint8_t id) -> function_builder &;
+
+ //! Begin building another function.
+ //!
+ //! @param bus The bus number the device is attached to.
+ //! @param device The device number on the bus.
+ //! @param number The function number of the device.
+ //! @return A function builder for the specified function.
+ [[nodiscard]] auto function(std::uint8_t bus, std::uint8_t device, std::uint8_t number) -> function_builder;
+
+ private:
+ ecam_builder * m_root;
+ kapi::memory::mmio_window m_window;
+ };
+
+ //! Fill a full bus ECAM space with 0xff
+ auto clear(std::uint8_t bus) -> void;
+
+ //! Begin building a function on the specified bus and device.
+ //!
+ //! @param bus The bus number the device is attached to.
+ //! @param device The device number on the bus.
+ //! @param number The function number of the device.
+ //! @return A function builder for the specified function.
+ [[nodiscard]] auto function(std::uint8_t bus, std::uint8_t device, std::uint8_t number) -> function_builder;
+
+ kapi::memory::physical_address base;
+
+ private:
+ //! Create an MMIO window for the specified bus, device, and function number.
+ //!
+ //! @param bus_number The bus number the device is attached to.
+ //! @param device_number The number of the device on the bus.
+ //! @param function_number The number of the function on the device.
+ //! @return An MMIO window into the ECAM space for the specified function.
+ [[nodiscard]] auto window_for(std::uint8_t bus_number, std::uint8_t device_number, std::uint8_t function_number)
+ -> kapi::memory::mmio_window;
+ };
+
+} // namespace kernel::tests::devices::pci
+
+#endif // TEACHOS_KERNEL_TEST_SUPPORT_DEVICES_PCI_ECAM_BUILDER_HPP
diff --git a/kernel/kernel/test_support/devices/pci/ecam_builder.tests.cpp b/kernel/kernel/test_support/devices/pci/ecam_builder.tests.cpp
new file mode 100644
index 00000000..0ca6af69
--- /dev/null
+++ b/kernel/kernel/test_support/devices/pci/ecam_builder.tests.cpp
@@ -0,0 +1,162 @@
+#include <kernel/test_support/devices/pci/ecam_builder.hpp>
+
+#include <kapi/memory.hpp>
+
+#include <kstd/units.hpp>
+
+#include <catch2/catch_test_macros.hpp>
+#include <catch2/generators/catch_generators.hpp>
+
+#include <cstdint>
+#include <tuple>
+
+using kernel::tests::devices::pci::ecam_builder;
+using mmio_window = kapi::memory::mmio_window;
+
+namespace
+{
+ constexpr auto test_ecam_base = kapi::memory::physical_address{};
+ constexpr auto bytes_per_function = kstd::KiB(4u);
+ constexpr auto bytes_per_device = 8 * bytes_per_function;
+ constexpr auto bytes_per_bus = 32 * bytes_per_device;
+
+ constexpr auto test_vendor = 0x1234;
+ constexpr auto test_device = 0x5678;
+
+ constexpr auto test_class_display_ctrl = 0x03;
+ constexpr auto test_subclass_vga = 0x00;
+ constexpr auto test_prog_if_8514 = 0x01;
+ constexpr auto test_revision = 0x04;
+ constexpr auto test_header_type_endpoint = 0x00;
+ constexpr auto test_multifunction = 0x80;
+} // namespace
+
+SCENARIO("Building PCI ECAM structures using the ecam_builder", "[smoke][devices][pci]")
+{
+ GIVEN("An ECAM builder")
+ {
+ ecam_builder builder{.base = test_ecam_base};
+
+ WHEN("clearing a bus")
+ {
+ builder.clear(1);
+
+ THEN("the previous bus is unaffected")
+ {
+ auto base = test_ecam_base;
+ auto window = mmio_window::map(base, bytes_per_bus, mmio_window::access::read_only);
+ REQUIRE(window);
+
+ REQUIRE(window->read<std::uint32_t>(kstd::bytes{}) == 0);
+ REQUIRE(window->read<std::uint32_t>(bytes_per_bus - kstd::bytes{4}) == 0);
+ }
+
+ THEN("the bus is cleared")
+ {
+ auto base = test_ecam_base + bytes_per_bus;
+ auto window = mmio_window::map(base, bytes_per_bus, mmio_window::access::read_only);
+ REQUIRE(window);
+
+ REQUIRE(window->read<std::uint32_t>(kstd::bytes{}) == 0xffff'ffff);
+ REQUIRE(window->read<std::uint32_t>(bytes_per_bus - kstd::bytes{4}) == 0xffff'ffff);
+ }
+
+ THEN("the next bus is unaffected")
+ {
+ auto base = test_ecam_base + 2 * bytes_per_bus;
+ auto window = mmio_window::map(base, bytes_per_bus, mmio_window::access::read_only);
+ REQUIRE(window);
+
+ REQUIRE(window->read<std::uint32_t>(kstd::bytes{}) == 0);
+ REQUIRE(window->read<std::uint32_t>(bytes_per_bus - kstd::bytes{4}) == 0);
+ }
+ }
+
+ WHEN("building a function")
+ {
+ auto const [bus, device, function] = GENERATE(std::tuple{1, 0, 0}, std::tuple{1, 2, 1}, std::tuple{3, 31, 7});
+
+ builder.clear(1);
+
+ builder.function(bus, device, function)
+ .vendor_id(test_vendor)
+ .device_id(test_device)
+ .class_code(test_class_display_ctrl)
+ .subclass_code(test_subclass_vga)
+ .programming_interface(test_prog_if_8514)
+ .header_type(test_header_type_endpoint | test_multifunction)
+ .revision_id(test_revision);
+
+ THEN("the values are written correctly")
+ {
+ auto base = test_ecam_base + bus * bytes_per_bus + device * bytes_per_device + function * bytes_per_function;
+ auto window = mmio_window::map(base, bytes_per_function, mmio_window::access::read_only);
+ REQUIRE(window);
+
+ REQUIRE(window->read<std::uint16_t>(ecam_builder::vendor_id_offset) == test_vendor);
+ REQUIRE(window->read<std::uint16_t>(ecam_builder::device_id_offset) == test_device);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::class_code_offset) == test_class_display_ctrl);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::subclass_code_offset) == test_subclass_vga);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::programming_interface_offset) == test_prog_if_8514);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::revision_id_offset) == test_revision);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::header_type_offset) ==
+ (test_header_type_endpoint | test_multifunction));
+ }
+ }
+
+ WHEN("chaining function builders")
+ {
+ auto const [bus, device, function] = GENERATE(std::tuple{1, 2, 1}, std::tuple{3, 31, 7});
+
+ builder.clear(1);
+
+ builder.function(1, 0, 0)
+ .vendor_id(0)
+ .device_id(0)
+ .class_code(0)
+ .subclass_code(0)
+ .programming_interface(0)
+ .header_type(0)
+ .revision_id(0)
+ .function(bus, device, function)
+ .vendor_id(test_vendor)
+ .device_id(test_device)
+ .class_code(test_class_display_ctrl)
+ .subclass_code(test_subclass_vga)
+ .programming_interface(test_prog_if_8514)
+ .header_type(test_header_type_endpoint | test_multifunction)
+ .revision_id(test_revision);
+
+ THEN("the values are written correctly")
+ {
+ auto base = test_ecam_base + bus * bytes_per_bus + device * bytes_per_device + function * bytes_per_function;
+ auto window = mmio_window::map(base, bytes_per_function, mmio_window::access::read_only);
+ REQUIRE(window);
+
+ REQUIRE(window->read<std::uint16_t>(ecam_builder::vendor_id_offset) == test_vendor);
+ REQUIRE(window->read<std::uint16_t>(ecam_builder::device_id_offset) == test_device);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::class_code_offset) == test_class_display_ctrl);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::subclass_code_offset) == test_subclass_vga);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::programming_interface_offset) == test_prog_if_8514);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::revision_id_offset) == test_revision);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::header_type_offset) ==
+ (test_header_type_endpoint | test_multifunction));
+ }
+
+ THEN("the first function is unchanged")
+ {
+ auto base = test_ecam_base + bytes_per_bus;
+ auto window = mmio_window::map(base, bytes_per_function, mmio_window::access::read_only);
+ REQUIRE(window);
+
+ REQUIRE(window->read<std::uint16_t>(ecam_builder::vendor_id_offset) == 0);
+ REQUIRE(window->read<std::uint16_t>(ecam_builder::device_id_offset) == 0);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::class_code_offset) == 0);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::subclass_code_offset) == 0);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::programming_interface_offset) == 0);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::revision_id_offset) == 0);
+ REQUIRE(window->read<std::uint8_t>(ecam_builder::header_type_offset) == 0);
+ }
+ }
+ }
+}