diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-11-27 18:32:32 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-11-27 18:32:32 +0100 |
| commit | 12ce28c459250334be0eb2583f9007d5da2de699 (patch) | |
| tree | f46a6d9a7ea5904d0be4175f32463f7121c35d6a /kernel | |
| parent | 1f669ed896532d212271ab559d607f6f448965c5 (diff) | |
| download | kernel-12ce28c459250334be0eb2583f9007d5da2de699.tar.xz kernel-12ce28c459250334be0eb2583f9007d5da2de699.zip | |
kernel/devices: implement PCI host bridge support
This change add the implementation of the host_bridge type, representing
PCI host bridges. A host_bridge is the entrypoint for enumerating PCI
devices attached to the segment represented by the host bridge. The
associated protocol is responsible for this enumeration and attaches
the discovered devices to the host bridge as children as a tree based
on the segment scan results.
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci.hpp | 1 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/host_bridge.cpp | 142 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/host_bridge.hpp | 59 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/host_bridge.tests.cpp | 408 |
5 files changed, 611 insertions, 0 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index a9f0c703..73be750b 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -48,6 +48,7 @@ target_sources("kernel_lib" PRIVATE "kernel/devices/pci/ecam_segment.cpp" "kernel/devices/pci/endpoint.cpp" "kernel/devices/pci/function_access.cpp" + "kernel/devices/pci/host_bridge.cpp" "kernel/devices/pci/scan.cpp" "kernel/devices/pseudo.cpp" "kernel/devices/storage.cpp" diff --git a/kernel/kernel/devices/pci.hpp b/kernel/kernel/devices/pci.hpp index ddd9a8dc..6cfc4650 100644 --- a/kernel/kernel/devices/pci.hpp +++ b/kernel/kernel/devices/pci.hpp @@ -5,6 +5,7 @@ #include <kernel/devices/pci/ecam_segment.hpp> // IWYU pragma: export #include <kernel/devices/pci/endpoint.hpp> // IWYU pragma: export #include <kernel/devices/pci/function_access.hpp> // IWYU pragma: export +#include <kernel/devices/pci/host_bridge.hpp> // IWYU pragma: export #include <kernel/devices/pci/registers.hpp> // IWYU pragma: export #include <kernel/devices/pci/scan.hpp> // IWYU pragma: export diff --git a/kernel/kernel/devices/pci/host_bridge.cpp b/kernel/kernel/devices/pci/host_bridge.cpp new file mode 100644 index 00000000..9d1da9f9 --- /dev/null +++ b/kernel/kernel/devices/pci/host_bridge.cpp @@ -0,0 +1,142 @@ +#include <kernel/devices/pci/host_bridge.hpp> + +#include <kernel/bus/pci.hpp> +#include <kernel/devices/pci/bridge.hpp> +#include <kernel/devices/pci/ecam_segment.hpp> +#include <kernel/devices/pci/endpoint.hpp> +#include <kernel/devices/pci/scan.hpp> + +#include <kapi/capabilities/facet_id.hpp> +#include <kapi/devices.hpp> + +#include <kstd/flat_map.hpp> +#include <kstd/format.hpp> +#include <kstd/memory.hpp> +#include <kstd/print.hpp> + +#include <cstdint> +#include <utility> + +namespace kernel::devices::pci +{ + + namespace + { + + struct host_bridge_protocol final : bus::pci::protocol + { + using bus_ptr = kstd::observer_ptr<kapi::devices::bus>; + using bus_map = kstd::flat_map<std::uint8_t, bus_ptr>; + using function_info = bus::pci::function_info; + using header_type = bus::pci::header_type; + + //! Try to add a PCI device to the appropriate parent bus (host bridge, or PCI-to-PCI bridge) + //! + //! If a device cannot be added, e.g. because the header type is not supported or the bus is already claimed, it + //! will not be added and the failure will be logged to the kernel log. + //! + //! @param segment The ECAM segment containing the device. + //! @param buses The map of bus numbers to their corresponding bus objects. + //! @param function The PCI function information for the device. + //! @param parent The parent bus to which the device should be added. + //! @return @p true iff. the device was successfully added, @p false otherwise. + auto add_device(ecam_segment const & segment, bus_map & buses, function_info const & function, bus_ptr parent) + -> bool + { + switch (function.type()) + { + case header_type::endpoint: + { + parent->add_child(kstd::make_shared<endpoint>(function, segment)); + return true; + } + case header_type::pci_to_pci_bridge: + { + auto secondary_bus = function.secondary_bus; + if (buses.contains(secondary_bus)) + { + kstd::println( + kstd::print_sink::stderr, + "[OS:DEV] skipping bridge {} claiming to own bus {} that's already owned by a different bridge.", + function.address, secondary_bus); + return false; + } + auto device = kstd::make_shared<bridge>(function, segment); + parent->add_child(device); + buses[secondary_bus] = kstd::make_observer(device.get()); + return true; + } + default: + { + kstd::println(kstd::print_sink::stderr, "[OS:DEV] skipping device {} with unsupported header type {}.", + function.address, static_cast<int>(function.type())); + + return false; + } + } + } + + auto enumerate(kapi::devices::bus & host) -> void override + { + auto segment_accessor = host.facet<segment_access>(); + if (!segment_accessor) + { + kstd::println(kstd::print_sink::stderr, "[OS:DEV] tried to enumerate non-host-bridge PCI device."); + return; + } + + auto const & segment = segment_accessor->segment(); + + auto scan_result = scan_segment(segment); + if (!scan_result) + { + kstd::println(kstd::print_sink::stderr, "[OS:DEV] failed to scan PCI segment {}: {}.", segment.group(), + scan_result.error()); + return; + } + + auto buses = bus_map{}; + buses[segment.first_bus()] = kstd::make_observer(&host); + + for (auto const & function : scan_result.value()) + { + auto bus_number = function.address.bus; + auto parent_entry = buses.find(bus_number); + + if (parent_entry == buses.end()) + { + kstd::println(kstd::print_sink::stderr, "[OS:DEV] skipping device {} attached to unknown bus {}.", + function.address, bus_number); + continue; + } + + if (add_device(segment, buses, function, parent_entry->second)) + { + kstd::println("[OS:DEV] Detected PCI Express device: {} [{:04x}:{:04x}]", function.address, + function.vendor_id, function.device_id); + } + } + } + } constinit protocol_instance; + + } // namespace + + host_bridge::host_bridge(ecam_segment segment) + : bus{kstd::format("pci{:04x}:{:02x}", segment.group(), segment.first_bus()), protocol_instance} + , m_segment{std::move(segment)} + {} + + auto host_bridge::segment() const noexcept -> ecam_segment const & + { + return m_segment; + } + + auto host_bridge::query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> + { + if (facet == segment_access::id) + { + return kstd::make_observer<segment_access>(this); + } + return bus::query_facet(facet); + } +} // namespace kernel::devices::pci diff --git a/kernel/kernel/devices/pci/host_bridge.hpp b/kernel/kernel/devices/pci/host_bridge.hpp new file mode 100644 index 00000000..5849e0cc --- /dev/null +++ b/kernel/kernel/devices/pci/host_bridge.hpp @@ -0,0 +1,59 @@ +#ifndef TEACHOS_KERNEL_DEVICES_PCI_HOST_BRIDGE_HPP +#define TEACHOS_KERNEL_DEVICES_PCI_HOST_BRIDGE_HPP + +#include <kernel/devices/pci/ecam_segment.hpp> + +#include <kapi/capabilities/facet_id.hpp> +#include <kapi/devices.hpp> + +#include <kstd/memory.hpp> + +namespace kernel::devices::pci +{ + + //! A facet to allow access to the ECAM segment of a PCI host bridge. + struct segment_access + { + //! The unique ID of this facet. + constexpr auto static id = kapi::capabilities::facet_id{"seg.dev.pci"}; + + //! Virtual destructor to allow for safe deletion of derived objects through base pointers. + virtual ~segment_access() = default; + + //! Access the ECAM segment for the host bridge. + //! + //! @return A reference to the ECAM segment. + [[nodiscard]] virtual auto segment() const noexcept -> ecam_segment const & = 0; + }; + + //! A PCI host bridge bus device. + //! + //! A host bridge bus device does not expose a PCI signature, so no driver will ever match against it. + //! + //! @warning Enumerating the same host bridge multiple times will cause a panic due to duplicate devices being added + //! to the system. + struct host_bridge final : kapi::devices::bus, segment_access + { + //! Construct a PCI host bridge based on the given ECAM segment. + explicit host_bridge(ecam_segment segment); + + //! Access the ECAM segment for the host bridge. + //! + //! @return A reference to the ECAM segment. + auto segment() const noexcept -> ecam_segment const & override; + + protected: + //! Query for a specific facet provided by the host bridge. + //! + //! @param facet The ID of the facet to query. + //! @return An observer pointer to the requested facet, or nullptr if not available. + auto query_facet(kapi::capabilities::facet_id) -> kstd::observer_ptr<void> override; + + private: + //! The ECAM segment this host bridge manages. + ecam_segment m_segment; + }; + +} // namespace kernel::devices::pci + +#endif diff --git a/kernel/kernel/devices/pci/host_bridge.tests.cpp b/kernel/kernel/devices/pci/host_bridge.tests.cpp new file mode 100644 index 00000000..96cebca9 --- /dev/null +++ b/kernel/kernel/devices/pci/host_bridge.tests.cpp @@ -0,0 +1,408 @@ +#include <kernel/devices/pci/host_bridge.hpp> + +#include <kernel/bus/pci.hpp> +#include <kernel/devices/pci/ecam_segment.hpp> +#include <kernel/test_support/cio.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/capabilities/facet_id.hpp> +#include <kapi/devices.hpp> +#include <kapi/devices/driver_registry.hpp> +#include <kapi/memory.hpp> + +#include <kstd/memory.hpp> +#include <kstd/result.hpp> + +#include <catch2/catch_test_macros.hpp> + +#include <array> +#include <cstdint> +#include <initializer_list> +#include <span> +#include <string_view> + +// NOLINTBEGIN(readability-magic-numbers) + +namespace +{ + namespace pci = kernel::bus::pci; + + using kernel::devices::pci::ecam_segment; + using kernel::devices::pci::host_bridge; + using kernel::devices::pci::segment_access; + using kernel::tests::cio::require_log_message; + using kernel::tests::devices::pci::function_spec; + + constexpr auto vendor_intel = std::uint16_t{0x8086}; + constexpr auto device_ich9_ahci = std::uint16_t{0x2922}; + constexpr auto device_other_sata = std::uint16_t{0x1234}; + constexpr auto class_mass_storage = std::uint8_t{0x01}; + constexpr auto subclass_sata = std::uint8_t{0x06}; + constexpr auto programming_interface_ahci = std::uint8_t{0x01}; + constexpr auto header_type_cardbus_bridge = std::uint8_t{0x02}; + + //! Run the enumeration of a host bridge through its protocol, the way the platform does. + auto enumerate(kstd::shared_ptr<host_bridge> const & host) -> void + { + auto const protocol = host->facet<kapi::devices::bus_protocol>(); + REQUIRE(protocol); + protocol->enumerate(*host); + } + + //! Require that the children of a bus have exactly the given names, in the given order. + auto require_children(kapi::devices::bus const & parent, std::initializer_list<std::string_view> expected) -> void + { + auto const children = parent.children(); + REQUIRE(children.size() == expected.size()); + + auto index = 0uz; + for (auto const name : expected) + { + REQUIRE(children[index]->name() == name); + ++index; + } + } + + //! Find the child of a bus with the given name. + auto find_child(kapi::devices::bus const & parent, std::string_view name) -> kstd::shared_ptr<kapi::devices::device> + { + for (auto const & child : parent.children()) + { + if (child->name() == name) + { + return child; + } + } + + return nullptr; + } + + //! Find the child of a bus that is itself a bus. + auto find_child_bus(kapi::devices::bus const & parent, std::string_view name) + -> kstd::observer_ptr<kapi::devices::bus> + { + auto const child = find_child(parent, name); + REQUIRE(child); + + auto const child_bus = child->facet<kapi::devices::bus>(); + REQUIRE(child_bus); + return child_bus; + } + + auto ahci_controller(std::uint8_t bus, std::uint8_t device, std::uint16_t device_id = device_ich9_ahci) + -> function_spec + { + return function_spec{ + .bus = bus, + .device = device, + .vendor_id = vendor_intel, + .device_id = device_id, + .class_code = class_mass_storage, + .subclass_code = subclass_sata, + .programming_interface = programming_interface_ahci, + }; + } + + //! A driver that claims a table of PCI IDs and counts how often it was probed. + struct counting_driver final : kapi::devices::driver, pci::claim + { + explicit counting_driver(pci::id id) + : m_ids{id} + {} + + [[nodiscard]] auto probe(kapi::devices::device &) -> kstd::result<void> override + { + ++probe_calls; + return kstd::success(); + } + + auto unbind(kapi::devices::device &) -> void override {} + + [[nodiscard]] auto name() const noexcept -> std::string_view override + { + return "PCI Tree Test Driver"; + } + + [[nodiscard]] auto supported_ids() const noexcept -> std::span<pci::id const> override + { + return m_ids; + } + + unsigned probe_calls{}; + + protected: + auto query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> override + { + if (facet == pci::claim::id) + { + return kstd::make_observer<pci::claim>(this); + } + + return driver::query_facet(facet); + } + + private: + std::array<pci::id, 1> m_ids; + }; +} // namespace + +SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "A host bridge represents its segment", + "[devices][pci][host_bridge]") +{ + GIVEN("a host bridge for a segment with a non-zero group and first bus") + { + auto const segment = ecam_segment{make_descriptor(0x12, 2, 2)}; + auto const host = kstd::make_shared<host_bridge>(segment); + + THEN("it is named after the group and the first bus of the segment") + { + REQUIRE(host->name() == "pci0012:02"); + } + + THEN("it provides access to its segment") + { + auto const access = host->facet<segment_access>(); + + REQUIRE(access); + REQUIRE(access->segment().group() == 0x12); + REQUIRE(access->segment().first_bus() == 2); + } + + THEN("it is a bus with a protocol, but no function of its own") + { + REQUIRE(host->has_facet<kapi::devices::bus>()); + REQUIRE(host->has_facet<kapi::devices::bus_protocol>()); + REQUIRE_FALSE(host->has_facet<pci::signature>()); + REQUIRE_FALSE(host->has_facet<pci::configuration_access>()); + } + + THEN("it has no children before it was enumerated") + { + REQUIRE(host->children().empty()); + } + } +} + +SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "A host bridge enumerates a flat segment", + "[devices][pci][host_bridge]") +{ + GIVEN("a segment with endpoints and a multifunction device on its root bus") + { + place(endpoint(ahci_controller(0, 1))); + place(endpoint(ahci_controller(0, 2, device_other_sata))); + place(multifunction(endpoint(ahci_controller(0, 3)))); + place(endpoint(function_spec{.bus = 0, .device = 3, .function = 1})); + + auto const host = kstd::make_shared<host_bridge>(ecam_segment{make_descriptor(0, 0, 1)}); + + WHEN("the host bridge is enumerated") + { + enumerate(host); + + THEN("every function becomes an endpoint below it, in ascending address order, and stays present") + { + require_children(*host, {"0000:00:01.0", "0000:00:02.0", "0000:00:03.0", "0000:00:03.1"}); + + for (auto const & child : host->children()) + { + REQUIRE(child->has_facet<pci::signature>()); + REQUIRE(child->has_facet<pci::configuration_access>()); + REQUIRE_FALSE(child->has_facet<kapi::devices::bus>()); + REQUIRE(child->parent() == host); + REQUIRE(child->state() == kapi::devices::state::present); + } + + require_log_message("[OS:DEV] Detected PCI Express device: 0000:00:01.0 [8086:2922]"); + require_log_message("[OS:DEV] Detected PCI Express device: 0000:00:02.0 [8086:1234]"); + } + } + } +} + +SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "A host bridge nests the functions behind bridges", + "[devices][pci][host_bridge]") +{ + GIVEN("a segment with bridges on the root bus and a bridge behind a bridge") + { + // bus 0: endpoint 00:01.0, bridge 00:02.0 -> bus 1, bridge 00:03.0 -> bus 3 + // bus 1: endpoint 01:00.0, bridge 01:01.0 -> bus 2 + // bus 2: endpoint 02:00.0 + // bus 3: endpoint 03:00.0 + place(endpoint(ahci_controller(0, 1))); + place(bridge(function_spec{.bus = 0, .device = 2}, 1, 2)); + place(bridge(function_spec{.bus = 0, .device = 3}, 3, 3)); + place(endpoint(ahci_controller(1, 0))); + place(bridge(function_spec{.bus = 1, .device = 1}, 2, 2)); + place(endpoint(ahci_controller(2, 0))); + place(endpoint(ahci_controller(3, 0))); + + auto const host = kstd::make_shared<host_bridge>(ecam_segment{make_descriptor(0, 0, 4)}); + + WHEN("the host bridge is enumerated") + { + enumerate(host); + + THEN("each function hangs below the bridge that owns its bus, and only bridges are buses") + { + require_children(*host, {"0000:00:01.0", "0000:00:02.0", "0000:00:03.0"}); + + auto const first_bridge = find_child_bus(*host, "0000:00:02.0"); + require_children(*first_bridge, {"0000:01:00.0", "0000:01:01.0"}); + + auto const nested_bridge = find_child_bus(*first_bridge, "0000:01:01.0"); + require_children(*nested_bridge, {"0000:02:00.0"}); + + auto const second_bridge = find_child_bus(*host, "0000:00:03.0"); + require_children(*second_bridge, {"0000:03:00.0"}); + + auto const bridge_device = find_child(*host, "0000:00:02.0"); + REQUIRE(bridge_device->has_facet<pci::signature>()); + REQUIRE(bridge_device->has_facet<pci::configuration_access>()); + + auto const endpoint_device = find_child(*host, "0000:00:01.0"); + REQUIRE(endpoint_device); + REQUIRE_FALSE(endpoint_device->has_facet<kapi::devices::bus>()); + } + } + } +} + +SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Host bridges of different segments do not interfere", + "[devices][pci][host_bridge]") +{ + GIVEN("two segments in different groups") + { + place(endpoint(ahci_controller(0, 1))); + place(endpoint(ahci_controller(2, 1))); + + auto const first = kstd::make_shared<host_bridge>(ecam_segment{make_descriptor(0, 0, 2)}); + auto const second = kstd::make_shared<host_bridge>(ecam_segment{make_descriptor(1, 2, 2)}); + + WHEN("both host bridges are enumerated") + { + enumerate(first); + enumerate(second); + + THEN("each is named after its own segment and holds only its own functions") + { + REQUIRE(first->name() == "pci0000:00"); + REQUIRE(second->name() == "pci0001:02"); + + require_children(*first, {"0000:00:01.0"}); + require_children(*second, {"0001:02:01.0"}); + } + } + } +} + +SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "A host bridge skips functions it cannot represent", + "[devices][pci][host_bridge]") +{ + GIVEN("a segment with a function that has an unsupported header type between two endpoints") + { + place(endpoint(ahci_controller(0, 1))); + place(function_spec{.bus = 0, .device = 2, .header_type = header_type_cardbus_bridge}); + place(endpoint(ahci_controller(0, 3))); + + auto const host = kstd::make_shared<host_bridge>(ecam_segment{make_descriptor(0, 0, 1)}); + + WHEN("the host bridge is enumerated") + { + enumerate(host); + + THEN("the unsupported function is skipped and logged, and its neighbours are attached") + { + require_children(*host, {"0000:00:01.0", "0000:00:03.0"}); + require_log_message("[OS:DEV] skipping device 0000:00:02.0 with unsupported header type 2"); + } + } + } +} + +SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, + "A host bridge stays attached when its segment cannot be scanned", "[devices][pci][host_bridge]") +{ + GIVEN("a segment whose configuration space lies outside the available memory") + { + auto const descriptor = kapi::devices::pcie_segment_descriptor{ + .base_address = kapi::memory::physical_address{} + kernel::tests::memory::mmio_size, + .group_number = 0, + .first_bus = 0, + .bus_count = 1, + }; + + auto const host = kstd::make_shared<host_bridge>(ecam_segment{descriptor}); + + WHEN("the host bridge is enumerated") + { + enumerate(host); + + THEN("the failure is logged and the host bridge remains, empty") + { + require_log_message("[OS:DEV] failed to scan PCI segment 0"); + REQUIRE(host->children().empty()); + } + } + } +} + +SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, + "Drivers are matched against the functions of an enumerated tree", "[devices][pci][host_bridge]") +{ + GIVEN("a class driver registered before an exact-ID driver and a tree with functions on both sides of a bridge") + { + // bus 0: AHCI controller 00:01.0, other SATA controller 00:02.0 (class match), bridge 00:03.0 -> bus 1 + // bus 1: unrelated function 01:00.0 (nobody claims it) + place(endpoint(ahci_controller(0, 1))); + place(endpoint(ahci_controller(0, 2, device_other_sata))); + place(bridge(function_spec{.bus = 0, .device = 3}, 1, 1)); + place(endpoint(function_spec{.bus = 1, .device = 0})); + + auto const class_driver = kstd::make_shared<counting_driver>(pci::id{ + .class_code = class_mass_storage, + .subclass_code = subclass_sata, + .programming_interface = programming_interface_ahci, + }); + auto const exact_driver = + kstd::make_shared<counting_driver>(pci::id{.vendor_id = vendor_intel, .device_id = device_ich9_ahci}); + + kapi::devices::driver_registry::get().add(class_driver); + kapi::devices::driver_registry::get().add(exact_driver); + + auto const host = kstd::make_shared<host_bridge>(ecam_segment{make_descriptor(0, 0, 2)}); + + WHEN("the host bridge is enumerated") + { + enumerate(host); + + THEN("each function is bound to the best driver for it, and the rest stay present") + { + auto const ahci = find_child(*host, "0000:00:01.0"); + auto const sata = find_child(*host, "0000:00:02.0"); + auto const bridge_device = find_child(*host, "0000:00:03.0"); + auto const unclaimed = find_child(*find_child_bus(*host, "0000:00:03.0"), "0000:01:00.0"); + + REQUIRE(ahci); + REQUIRE(sata); + REQUIRE(bridge_device); + REQUIRE(unclaimed); + + REQUIRE(ahci->state() == kapi::devices::state::bound); + REQUIRE(ahci->bound_driver() == exact_driver); + + REQUIRE(sata->state() == kapi::devices::state::bound); + REQUIRE(sata->bound_driver() == class_driver); + + REQUIRE(bridge_device->state() == kapi::devices::state::present); + REQUIRE(unclaimed->state() == kapi::devices::state::present); + + REQUIRE(exact_driver->probe_calls == 1); + REQUIRE(class_driver->probe_calls == 1); + } + } + } +} + +// NOLINTEND(readability-magic-numbers) |
