diff options
| -rw-r--r-- | kernel/kernel/devices/pci/function_info.hpp | 4 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/registers.hpp | 6 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/scan.cpp | 10 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/scan.tests.cpp | 90 |
4 files changed, 109 insertions, 1 deletions
diff --git a/kernel/kernel/devices/pci/function_info.hpp b/kernel/kernel/devices/pci/function_info.hpp index 199716f5..3e71430e 100644 --- a/kernel/kernel/devices/pci/function_info.hpp +++ b/kernel/kernel/devices/pci/function_info.hpp @@ -65,6 +65,10 @@ namespace kernel::devices::pci std::uint8_t class_code{}; //! The raw (encoded) header type of the PCI function. std::uint8_t raw_header_type{}; + //! The bus attached downstream of this PCI function, if it is a PCI-to-PCI bridge, zero otherwise + std::uint8_t secondary_bus{}; + //! The highest bus number attached downstream of this PCI function, if it is a PCI-to-PCI bridge, zero otherwise. + std::uint8_t subordinate_bus{}; }; } // namespace kernel::devices::pci diff --git a/kernel/kernel/devices/pci/registers.hpp b/kernel/kernel/devices/pci/registers.hpp index 98136070..1caa0baf 100644 --- a/kernel/kernel/devices/pci/registers.hpp +++ b/kernel/kernel/devices/pci/registers.hpp @@ -58,6 +58,12 @@ namespace kernel::devices::pci //! The offset of the header type register within the PCI configuration space. constexpr auto inline header_type_register_offset = kstd::bytes{0x0e}; + //! The offset of the secondary bus register within the PCI configuration space. + constexpr auto inline secondary_bus_register_offset = kstd::bytes{0x19}; + + //! The offset of the subordinate bus register within the PCI configuration space. + constexpr auto inline subordinate_bus_register_offset = kstd::bytes{0x1a}; + } // namespace kernel::devices::pci #endif diff --git a/kernel/kernel/devices/pci/scan.cpp b/kernel/kernel/devices/pci/scan.cpp index 1cad8b4e..da3523ec 100644 --- a/kernel/kernel/devices/pci/scan.cpp +++ b/kernel/kernel/devices/pci/scan.cpp @@ -37,7 +37,7 @@ namespace kernel::devices::pci 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{ + auto info = function_info{ .address = {.segment_group = group, .bus = bus, .device = device, .function = number}, .vendor_id = vendor_id, .device_id = device_id, @@ -47,6 +47,14 @@ namespace kernel::devices::pci .class_code = class_code, .raw_header_type = header_type }; + + if (info.type() == header_type::pci_to_pci_bridge) + { + info.secondary_bus = ecam_window.read<std::uint8_t>(offset + secondary_bus_register_offset); + info.subordinate_bus = ecam_window.read<std::uint8_t>(offset + subordinate_bus_register_offset); + } + + return info; } } // namespace diff --git a/kernel/kernel/devices/pci/scan.tests.cpp b/kernel/kernel/devices/pci/scan.tests.cpp index c5c2e6d6..ca370533 100644 --- a/kernel/kernel/devices/pci/scan.tests.cpp +++ b/kernel/kernel/devices/pci/scan.tests.cpp @@ -46,6 +46,7 @@ namespace constexpr auto class_code_display_controller = 0x03; constexpr auto header_type_endpoint = 0x00; + constexpr auto header_type_cardbus_bridge = 0x02; constexpr auto distinct_vendor_id = 0x1234; constexpr auto distinct_device_id = 0x5678; @@ -53,6 +54,8 @@ namespace constexpr auto distinct_programming_interface = 0x22; constexpr auto distinct_subclass_code = 0x33; constexpr auto distinct_class_code = 0x44; + constexpr auto distinct_secondary_bus = 0x21; + constexpr auto distinct_subordinate_bus = 0x42; constexpr auto devices_per_bus = 32; @@ -378,3 +381,90 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Scanning PCI buses", } } } + +SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Reading bridge bus numbers while scanning PCI buses", + "[devices][pci]") +{ + GIVEN("A PCI-to-PCI bridge with distinct secondary and subordinate bus numbers") + { + place(bridge({.bus = 0, .device = 3}, distinct_secondary_bus, distinct_subordinate_bus)); + + THEN("scanning the bus reports both bus numbers") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(kernel::devices::pci::scan_bus(segment, 0)); + + REQUIRE(functions.size() == 1); + + auto const & function = functions.at(0); + + REQUIRE(function.type() == kernel::devices::pci::header_type::pci_to_pci_bridge); + REQUIRE(!function.is_multifunction()); + REQUIRE(function.secondary_bus == distinct_secondary_bus); + REQUIRE(function.subordinate_bus == distinct_subordinate_bus); + } + } + + GIVEN("A multi-function PCI-to-PCI bridge") + { + place(multifunction(bridge({.bus = 0, .device = 3}, distinct_secondary_bus, distinct_subordinate_bus))); + + THEN("the multifunction bit does not prevent reading the bus numbers") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(kernel::devices::pci::scan_bus(segment, 0)); + + REQUIRE(functions.size() == 1); + + auto const & function = functions.at(0); + + REQUIRE(function.type() == kernel::devices::pci::header_type::pci_to_pci_bridge); + REQUIRE(function.is_multifunction()); + REQUIRE(function.secondary_bus == distinct_secondary_bus); + REQUIRE(function.subordinate_bus == distinct_subordinate_bus); + } + } + + GIVEN("An endpoint with non-zero bytes where a bridge would keep its bus numbers") + { + place(endpoint( + {.bus = 0, .device = 3, .secondary_bus = distinct_secondary_bus, .subordinate_bus = distinct_subordinate_bus})); + + THEN("scanning the bus reports zero for both bus numbers") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(kernel::devices::pci::scan_bus(segment, 0)); + + REQUIRE(functions.size() == 1); + + auto const & function = functions.at(0); + + REQUIRE(function.type() == kernel::devices::pci::header_type::endpoint); + REQUIRE(function.secondary_bus == 0); + REQUIRE(function.subordinate_bus == 0); + } + } + + GIVEN("A CardBus bridge with non-zero bytes where a PCI-to-PCI bridge would keep its bus numbers") + { + place({.bus = 0, + .device = 3, + .header_type = header_type_cardbus_bridge, + .secondary_bus = distinct_secondary_bus, + .subordinate_bus = distinct_subordinate_bus}); + + THEN("scanning the bus reports zero for both bus numbers") + { + auto segment = ecam_segment{make_descriptor()}; + auto functions = require_ok(kernel::devices::pci::scan_bus(segment, 0)); + + REQUIRE(functions.size() == 1); + + auto const & function = functions.at(0); + + REQUIRE(function.type() == kernel::devices::pci::header_type::cardbus_bridge); + REQUIRE(function.secondary_bus == 0); + REQUIRE(function.subordinate_bus == 0); + } + } +} |
