diff options
41 files changed, 2256 insertions, 525 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index e8ba16eb..73be750b 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -43,15 +43,19 @@ target_sources("kernel_lib" PRIVATE # Device Subsystem "kernel/devices/block_device_utils.cpp" "kernel/devices/init.cpp" + "kernel/devices/pci.cpp" + "kernel/devices/pci/bridge.cpp" "kernel/devices/pci/ecam_segment.cpp" - "kernel/devices/pci/init.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" # Bus Sumbsystem "kernel/bus/boot_modules.cpp" - "kernel/bus/pci.cpp" + "kernel/bus/pci/protocol.cpp" "kernel/bus/pseudo.cpp" "kernel/bus/root.cpp" diff --git a/kernel/kernel/bus/pci.hpp b/kernel/kernel/bus/pci.hpp index 782bd8b7..8697b925 100644 --- a/kernel/kernel/bus/pci.hpp +++ b/kernel/kernel/bus/pci.hpp @@ -1,164 +1,12 @@ #ifndef TEACHOS_KERNEL_BUS_PCI_HPP #define TEACHOS_KERNEL_BUS_PCI_HPP -#include <kernel/devices/pci/function_info.hpp> - -#include <kapi/capabilities/facet_id.hpp> -#include <kapi/devices.hpp> - -#include <kstd/result.hpp> - -#include <cstdint> -#include <optional> -#include <span> - -namespace kernel::bus -{ - - //! An ID of a PCI function used to match devices against drivers. - struct pci_id - { - constexpr auto static exact_id_score = 0x10u; - - //! Calculate the prefix chain match score for a given function information object against this ID. - //! - //! The match score is calculated based on the following rules: - //! - If both vendor_id and device_id of this ID object are set, a match of both fields adds 0x10 to the score. - //! - A programming_interface match requires a subclass_code match, which in turn requires a class_code match. - //! - Each successful match of class_code, subclass_code, and programming_interface adds 1 to the score. - //! - //! If neither vendor_id nor device_id are set, the match will rely solely on class_code, subclass_code, and - //! programming_interface. Any violations of the rules above will result in a non-match. - //! - //! @param info The PCI function information to match against. - //! @return The match score if applicable, otherwise std::nullopt. - [[nodiscard]] constexpr auto match(devices::pci::function_info const & info) const -> std::optional<std::uint32_t> - { - if (vendor_id.has_value() != device_id.has_value()) - { - return std::nullopt; - } - - if (subclass_code && !class_code) - { - return std::nullopt; - } - - if (programming_interface && !subclass_code) - { - return std::nullopt; - } - - if (!vendor_id && !class_code) - { - return std::nullopt; - } - - auto score = std::uint32_t{}; - - if (vendor_id) - { - if (*vendor_id != info.vendor_id || *device_id != info.device_id) - { - return std::nullopt; - } - score += exact_id_score; - } - - if (class_code) - { - if (*class_code != info.class_code) - { - return std::nullopt; - } - score += 1; - } - - if (subclass_code) - { - if (*subclass_code != info.subclass_code) - { - return std::nullopt; - } - score += 1; - } - - if (programming_interface) - { - if (*programming_interface != info.programming_interface) - { - return std::nullopt; - } - score += 1; - } - - return score; - } - - //! The vendor ID of the PCI device. - std::optional<std::uint16_t> vendor_id{}; - //! The device ID of the PCI device. - std::optional<std::uint16_t> device_id{}; - //! The class code of the PCI device. - std::optional<std::uint8_t> class_code{}; - //! The subclass code of the PCI device. - std::optional<std::uint8_t> subclass_code{}; - //! The programming interface of the PCI device. - std::optional<std::uint8_t> programming_interface{}; - }; - - //! The signature of a PCI device. - struct pci_signature - { - //! The unique ID of this facet. - constexpr auto static id = kapi::capabilities::facet_id{"sig.dev.pci"}; - - //! Virtual destructor to allow for safe deletion of derived objects through base pointers. - virtual ~pci_signature() = default; - - //! Retrieve the PCI function information associated with the device providing this facet. - //! - //! @return The PCI function information associated with the device. - [[nodiscard]] virtual auto info() const noexcept -> devices::pci::function_info const & = 0; - }; - - //! The claim a PCI driver makes on a device. - //! - //! This facet represents the set of PCI devices a driver can handle, as identified by their PCI IDs. - struct pci_claim - { - //! The unique ID of this facet. - constexpr auto static id = kapi::capabilities::facet_id{"clm.drv.pci"}; - - //! Virtual destructor to allow for safe deletion of derived objects through base pointers. - virtual ~pci_claim() = default; - - //! Get the set of PCI IDs supported by the driver. - //! - //! @return A span of PCI IDs that the driver can handle. - [[nodiscard]] virtual auto supported_ids() const noexcept -> std::span<pci_id const> = 0; - }; - - //! The base class for all PCI protocols. - struct pci_protocol : kapi::devices::bus_protocol - { - //! Enumerate the PCI devices on a given bus. - //! - //! Concrete subclasses of pci_protocol must implement this function. - //! - //! @param bus The bus to enumerate. - //! @return void. - auto enumerate(kapi::devices::bus & bus) -> void override = 0; - - //! Match a device against a driver to determine compatibility. - //! - //! @param device The device to match. - //! @param driver The driver to match against. - //! @return The match score on success, an error otherwise. - [[nodiscard]] auto match(kapi::devices::device const & device, kapi::devices::driver const & driver) const - -> kstd::result<std::uint32_t> final; - }; - -} // namespace kernel::bus - -#endif // TEACHOS_KERNEL_BUS_PCI_HPP +#include <kernel/bus/pci/claim.hpp> // IWYU pragma: export +#include <kernel/bus/pci/configuration_access.hpp> // IWYU pragma: export +#include <kernel/bus/pci/function_address.hpp> // IWYU pragma: export +#include <kernel/bus/pci/function_info.hpp> // IWYU pragma: export +#include <kernel/bus/pci/id.hpp> // IWYU pragma: export +#include <kernel/bus/pci/protocol.hpp> // IWYU pragma: export +#include <kernel/bus/pci/signature.hpp> // IWYU pragma: export + +#endif diff --git a/kernel/kernel/bus/pci/claim.hpp b/kernel/kernel/bus/pci/claim.hpp new file mode 100644 index 00000000..44200adb --- /dev/null +++ b/kernel/kernel/bus/pci/claim.hpp @@ -0,0 +1,34 @@ +#ifndef TEACHOS_KERNEL_BUS_PCI_CLAIM_HPP +#define TEACHOS_KERNEL_BUS_PCI_CLAIM_HPP + +// IWYU pragma: private, include <kernel/bus/pci.hpp> + +#include <kernel/bus/pci/id.hpp> + +#include <kapi/capabilities/facet_id.hpp> + +#include <span> + +namespace kernel::bus::pci +{ + + //! The claim a PCI driver makes on a device. + //! + //! This facet represents the set of PCI devices a driver can handle, as identified by their PCI IDs. + struct claim + { + //! The unique ID of this facet. + constexpr auto static id = kapi::capabilities::facet_id{"clm.drv.pci"}; + + //! Virtual destructor to allow for safe deletion of derived objects through base pointers. + virtual ~claim() = default; + + //! Get the set of PCI IDs supported by the driver. + //! + //! @return A span of PCI IDs that the driver can handle. + [[nodiscard]] virtual auto supported_ids() const noexcept -> std::span<struct id const> = 0; + }; + +} // namespace kernel::bus::pci + +#endif diff --git a/kernel/kernel/bus/pci/configuration_access.hpp b/kernel/kernel/bus/pci/configuration_access.hpp new file mode 100644 index 00000000..cf8e510e --- /dev/null +++ b/kernel/kernel/bus/pci/configuration_access.hpp @@ -0,0 +1,32 @@ +#ifndef TEACHOS_KERNEL_BUS_PCI_CONFIGURATION_ACCESS_HPP +#define TEACHOS_KERNEL_BUS_PCI_CONFIGURATION_ACCESS_HPP + +// IWYU pragma: private, include <kernel/bus/pci.hpp> + +#include <kapi/capabilities/facet_id.hpp> +#include <kapi/memory.hpp> + +#include <kstd/result.hpp> + +namespace kernel::bus::pci +{ + + //! A facet to allow access to the PCI configuration space of a function. + struct configuration_access + { + //! The unique ID of this facet. + constexpr auto static id = kapi::capabilities::facet_id{"cfg.dev.pci"}; + + //! Virtual destructor to allow for safe deletion of derived objects through base pointers. + virtual ~configuration_access() = default; + + //! Map the PCI configuration space for access. + //! + //! @return A pointer to the mapped PCI configuration space. + [[nodiscard]] virtual auto map_config(kapi::memory::mmio_window::access mode) const + -> kstd::result<kapi::memory::mmio_window> = 0; + }; + +} // namespace kernel::bus::pci + +#endif diff --git a/kernel/kernel/devices/pci/function_address.hpp b/kernel/kernel/bus/pci/function_address.hpp index c4b0888b..a2811f71 100644 --- a/kernel/kernel/devices/pci/function_address.hpp +++ b/kernel/kernel/bus/pci/function_address.hpp @@ -1,5 +1,7 @@ -#ifndef TEACHOS_KERNEL_DEVICES_PCI_FUNCTION_ADDRESS_HPP -#define TEACHOS_KERNEL_DEVICES_PCI_FUNCTION_ADDRESS_HPP +#ifndef TEACHOS_KERNEL_BUS_PCI_FUNCTION_ADDRESS_HPP +#define TEACHOS_KERNEL_BUS_PCI_FUNCTION_ADDRESS_HPP + +// IWYU pragma: private, include <kernel/bus/pci.hpp> #include <kstd/format.hpp> @@ -9,7 +11,7 @@ #include <span> #include <string_view> -namespace kernel::devices::pci +namespace kernel::bus::pci { //! The address of a PCI function within the system. @@ -33,10 +35,10 @@ namespace kernel::devices::pci std::uint8_t function{}; }; -} // namespace kernel::devices::pci +} // namespace kernel::bus::pci template<> -struct kstd::formatter<kernel::devices::pci::function_address> +struct kstd::formatter<kernel::bus::pci::function_address> { constexpr auto parse(kstd::format_parse_context & context) -> kstd::format_parse_context::iterator { @@ -48,7 +50,7 @@ struct kstd::formatter<kernel::devices::pci::function_address> return it; } - auto format(kernel::devices::pci::function_address const & address, kstd::format_context & context) const -> void + auto format(kernel::bus::pci::function_address const & address, kstd::format_context & context) const -> void { constexpr auto output_length = 32; auto buffer = std::array<char, output_length>{}; diff --git a/kernel/kernel/devices/pci/function_address.tests.cpp b/kernel/kernel/bus/pci/function_address.tests.cpp index ae9ccfff..86941cca 100644 --- a/kernel/kernel/devices/pci/function_address.tests.cpp +++ b/kernel/kernel/bus/pci/function_address.tests.cpp @@ -1,4 +1,4 @@ -#include <kernel/devices/pci/function_address.hpp> +#include <kernel/bus/pci/function_address.hpp> #include <kstd/format.hpp> @@ -8,11 +8,13 @@ // NOLINTBEGIN(readability-magic-numbers) +using kernel::bus::pci::function_address; + SCENARIO("PCI Function Address formatting") { GIVEN("A zeroed PCI function address") { - kernel::devices::pci::function_address address{}; + function_address address{}; THEN("The address is formatted as 0000:00:00.0") { @@ -22,7 +24,7 @@ SCENARIO("PCI Function Address formatting") GIVEN("A non-zero PCI function address") { - kernel::devices::pci::function_address address{}; + function_address address{}; address.segment_group = 0xabcd; address.bus = 0xff; address.device = 0x1f; @@ -39,8 +41,8 @@ SCENARIO("PCI Function Address comparison") { GIVEN("Two identical PCI function addresses") { - kernel::devices::pci::function_address address1{.segment_group = 1, .bus = 16, .device = 3, .function = 4}; - kernel::devices::pci::function_address address2{.segment_group = 1, .bus = 16, .device = 3, .function = 4}; + function_address address1{.segment_group = 1, .bus = 16, .device = 3, .function = 4}; + function_address address2{.segment_group = 1, .bus = 16, .device = 3, .function = 4}; THEN("they compare equal") { @@ -55,8 +57,8 @@ SCENARIO("PCI Function Address comparison") GIVEN("Different PCI function addresses that differ in the function field") { - kernel::devices::pci::function_address address1{.segment_group = 1, .bus = 16, .device = 3, .function = 4}; - kernel::devices::pci::function_address address2{.segment_group = 1, .bus = 16, .device = 3, .function = 5}; + function_address address1{.segment_group = 1, .bus = 16, .device = 3, .function = 4}; + function_address address2{.segment_group = 1, .bus = 16, .device = 3, .function = 5}; THEN("They are not equal") { @@ -71,8 +73,8 @@ SCENARIO("PCI Function Address comparison") GIVEN("Different PCI function addresses that differ in the bus field") { - kernel::devices::pci::function_address address1{.segment_group = 1, .bus = 16, .device = 3, .function = 4}; - kernel::devices::pci::function_address address2{.segment_group = 1, .bus = 32, .device = 3, .function = 4}; + function_address address1{.segment_group = 1, .bus = 16, .device = 3, .function = 4}; + function_address address2{.segment_group = 1, .bus = 32, .device = 3, .function = 4}; THEN("They are not equal") { diff --git a/kernel/kernel/devices/pci/function_info.hpp b/kernel/kernel/bus/pci/function_info.hpp index 3e71430e..14651bb7 100644 --- a/kernel/kernel/devices/pci/function_info.hpp +++ b/kernel/kernel/bus/pci/function_info.hpp @@ -1,11 +1,13 @@ -#ifndef TEACHOS_KERNEL_DEVICES_PCI_FUNCTION_INFO_HPP -#define TEACHOS_KERNEL_DEVICES_PCI_FUNCTION_INFO_HPP +#ifndef TEACHOS_KERNEL_BUS_PCI_FUNCTION_INFO_HPP +#define TEACHOS_KERNEL_BUS_PCI_FUNCTION_INFO_HPP -#include <kernel/devices/pci/function_address.hpp> +// IWYU pragma: private, include <kernel/bus/pci.hpp> + +#include <kernel/bus/pci/function_address.hpp> #include <cstdint> -namespace kernel::devices::pci +namespace kernel::bus::pci { //! The type of PCI header. @@ -71,6 +73,6 @@ namespace kernel::devices::pci std::uint8_t subordinate_bus{}; }; -} // namespace kernel::devices::pci +} // namespace kernel::bus::pci #endif diff --git a/kernel/kernel/devices/pci/function_info.tests.cpp b/kernel/kernel/bus/pci/function_info.tests.cpp index 91e838e7..dc724f55 100644 --- a/kernel/kernel/devices/pci/function_info.tests.cpp +++ b/kernel/kernel/bus/pci/function_info.tests.cpp @@ -1,14 +1,17 @@ -#include <kernel/devices/pci/function_info.hpp> +#include <kernel/bus/pci/function_info.hpp> #include <catch2/catch_test_macros.hpp> // NOLINTBEGIN(readability-magic-numbers) +using kernel::bus::pci::function_info; +using kernel::bus::pci::header_type; + SCENARIO("PCI Function Info derived properties") { GIVEN("A function of a single-function PCI device that is a basic endpoint") { - kernel::devices::pci::function_info info{}; + function_info info{}; info.raw_header_type = 0x00; THEN("it is not a multifunction device") @@ -18,13 +21,13 @@ SCENARIO("PCI Function Info derived properties") THEN("it is a basic endpoint") { - REQUIRE(info.type() == kernel::devices::pci::header_type::endpoint); + REQUIRE(info.type() == header_type::endpoint); } } GIVEN("A function of a multifunction PCI device that is a basic endpoint") { - kernel::devices::pci::function_info info{}; + function_info info{}; info.raw_header_type = 0x80; THEN("it is recognized as a multifunction device") @@ -34,13 +37,13 @@ SCENARIO("PCI Function Info derived properties") THEN("it is a basic endpoint") { - REQUIRE(info.type() == kernel::devices::pci::header_type::endpoint); + REQUIRE(info.type() == header_type::endpoint); } } GIVEN("A function of a single-function PCI device that is a PCI-to-PCI bridge") { - kernel::devices::pci::function_info info{}; + function_info info{}; info.raw_header_type = 0x01; THEN("it is recognized as not a multifunction device") @@ -50,13 +53,13 @@ SCENARIO("PCI Function Info derived properties") THEN("it is a PCI-to-PCI bridge") { - REQUIRE(info.type() == kernel::devices::pci::header_type::pci_to_pci_bridge); + REQUIRE(info.type() == header_type::pci_to_pci_bridge); } } GIVEN("A function of a multifunction PCI device that is a PCI-to-PCI bridge") { - kernel::devices::pci::function_info info{}; + function_info info{}; info.raw_header_type = 0x81; THEN("it is recognized as a multifunction device") @@ -66,13 +69,13 @@ SCENARIO("PCI Function Info derived properties") THEN("it is a PCI-to-PCI bridge") { - REQUIRE(info.type() == kernel::devices::pci::header_type::pci_to_pci_bridge); + REQUIRE(info.type() == header_type::pci_to_pci_bridge); } } GIVEN("A function of a single-function PCI device that is a CardBus bridge") { - kernel::devices::pci::function_info info{}; + function_info info{}; info.raw_header_type = 0x02; THEN("it is recognized as not a multifunction device") @@ -82,13 +85,13 @@ SCENARIO("PCI Function Info derived properties") THEN("it is a CardBus bridge") { - REQUIRE(info.type() == kernel::devices::pci::header_type::cardbus_bridge); + REQUIRE(info.type() == header_type::cardbus_bridge); } } GIVEN("A function of a multifunction PCI device that is a CardBus bridge") { - kernel::devices::pci::function_info info{}; + function_info info{}; info.raw_header_type = 0x82; THEN("it is recognized as a multifunction device") @@ -98,13 +101,13 @@ SCENARIO("PCI Function Info derived properties") THEN("it is a CardBus bridge") { - REQUIRE(info.type() == kernel::devices::pci::header_type::cardbus_bridge); + REQUIRE(info.type() == header_type::cardbus_bridge); } } GIVEN("A function of a single-function PCI device that is an unknown type") { - kernel::devices::pci::function_info info{}; + function_info info{}; info.raw_header_type = 0x7f; THEN("it is recognized as not a multifunction device") @@ -114,13 +117,13 @@ SCENARIO("PCI Function Info derived properties") THEN("it is an unknown type") { - REQUIRE(info.type() == static_cast<kernel::devices::pci::header_type>(0x7f)); + REQUIRE(info.type() == static_cast<header_type>(0x7f)); } } GIVEN("A function of a multifunction PCI device that is an unknown type") { - kernel::devices::pci::function_info info{}; + function_info info{}; info.raw_header_type = 0xff; THEN("it is recognized as a multifunction device") @@ -130,7 +133,7 @@ SCENARIO("PCI Function Info derived properties") THEN("it is an unknown type") { - REQUIRE(info.type() == static_cast<kernel::devices::pci::header_type>(0x7f)); + REQUIRE(info.type() == static_cast<header_type>(0x7f)); } } } diff --git a/kernel/kernel/bus/pci/id.hpp b/kernel/kernel/bus/pci/id.hpp new file mode 100644 index 00000000..063a9ca2 --- /dev/null +++ b/kernel/kernel/bus/pci/id.hpp @@ -0,0 +1,108 @@ +#ifndef TEACHOS_KERNEL_BUS_PCI_ID_HPP +#define TEACHOS_KERNEL_BUS_PCI_ID_HPP + +// IWYU pragma: private, include <kernel/bus/pci.hpp> + +#include <kernel/bus/pci/function_info.hpp> + +#include <cstdint> +#include <optional> + +namespace kernel::bus::pci +{ + + //! An ID of a PCI function used to match devices against drivers. + struct id + { + constexpr auto static exact_id_score = 0x10u; + + //! Calculate the prefix chain match score for a given function information object against this ID. + //! + //! The match score is calculated based on the following rules: + //! - If both vendor_id and device_id of this ID object are set, a match of both fields adds 0x10 to the score. + //! - A programming_interface match requires a subclass_code match, which in turn requires a class_code match. + //! - Each successful match of class_code, subclass_code, and programming_interface adds 1 to the score. + //! + //! If neither vendor_id nor device_id are set, the match will rely solely on class_code, subclass_code, and + //! programming_interface. Any violations of the rules above will result in a non-match. + //! + //! @param info The PCI function information to match against. + //! @return The match score if applicable, otherwise std::nullopt. + [[nodiscard]] constexpr auto match(function_info const & info) const -> std::optional<std::uint32_t> + { + if (vendor_id.has_value() != device_id.has_value()) + { + return std::nullopt; + } + + if (subclass_code && !class_code) + { + return std::nullopt; + } + + if (programming_interface && !subclass_code) + { + return std::nullopt; + } + + if (!vendor_id && !class_code) + { + return std::nullopt; + } + + auto score = std::uint32_t{}; + + if (vendor_id) + { + if (*vendor_id != info.vendor_id || *device_id != info.device_id) + { + return std::nullopt; + } + score += exact_id_score; + } + + if (class_code) + { + if (*class_code != info.class_code) + { + return std::nullopt; + } + score += 1; + } + + if (subclass_code) + { + if (*subclass_code != info.subclass_code) + { + return std::nullopt; + } + score += 1; + } + + if (programming_interface) + { + if (*programming_interface != info.programming_interface) + { + return std::nullopt; + } + score += 1; + } + + return score; + } + + //! The vendor ID of the PCI device. + std::optional<std::uint16_t> vendor_id{}; + //! The device ID of the PCI device. + std::optional<std::uint16_t> device_id{}; + //! The class code of the PCI device. + std::optional<std::uint8_t> class_code{}; + //! The subclass code of the PCI device. + std::optional<std::uint8_t> subclass_code{}; + //! The programming interface of the PCI device. + std::optional<std::uint8_t> programming_interface{}; + }; + +} // namespace kernel::bus::pci + +#endif diff --git a/kernel/kernel/bus/pci/id.tests.cpp b/kernel/kernel/bus/pci/id.tests.cpp new file mode 100644 index 00000000..7f44f5ba --- /dev/null +++ b/kernel/kernel/bus/pci/id.tests.cpp @@ -0,0 +1,271 @@ +#include <kernel/bus/pci/id.hpp> + +#include <kernel/bus/pci/function_info.hpp> + +#include <catch2/catch_test_macros.hpp> + +namespace pci = kernel::bus::pci; + +using pci::function_info; + +namespace +{ + + constexpr auto vendor_intel = 0x8086; + constexpr auto device_ich9_ahci = 0x2922; + constexpr auto class_mass_storage = 0x01; + constexpr auto subclass_sata = 0x06; + constexpr auto programming_interface_ahci = 0x01; + + constexpr auto vendor_red_hat = 0x1af4; + constexpr auto device_virtio_blk = 0x1001; + + auto ahci_controller() -> function_info + { + auto info = function_info{}; + info.vendor_id = vendor_intel; + info.device_id = device_ich9_ahci; + info.class_code = class_mass_storage; + info.subclass_code = subclass_sata; + info.programming_interface = programming_interface_ahci; + return info; + } + + auto exact_id() -> pci::id + { + return {.vendor_id = vendor_intel, .device_id = device_ich9_ahci}; + } + + auto class_id() -> pci::id + { + return {.class_code = class_mass_storage}; + } + + auto subclass_id() -> pci::id + { + return {.class_code = class_mass_storage, .subclass_code = subclass_sata}; + } + + auto full_class_id() -> pci::id + { + return { + .class_code = class_mass_storage, + .subclass_code = subclass_sata, + .programming_interface = programming_interface_ahci, + }; + } +} // namespace + +SCENARIO("PCI IDs are scored against a function", "[bus][pci]") +{ + GIVEN("an AHCI controller function") + { + auto const info = ahci_controller(); + + WHEN("an ID names the vendor and device exactly") + { + THEN("it matches with the exact ID score") + { + REQUIRE(exact_id().match(info) == pci::id::exact_id_score); + } + } + + WHEN("an ID names the right vendor but the wrong device") + { + auto const id = pci::id{.vendor_id = vendor_intel, .device_id = device_virtio_blk}; + + THEN("it does not match") + { + REQUIRE_FALSE(id.match(info).has_value()); + } + } + + WHEN("an ID names the right device but the wrong vendor") + { + auto const id = pci::id{.vendor_id = vendor_red_hat, .device_id = device_ich9_ahci}; + + THEN("it does not match") + { + REQUIRE_FALSE(id.match(info).has_value()); + } + } + + WHEN("an ID names only the class") + { + THEN("it matches with a score of 1") + { + REQUIRE(class_id().match(info) == 1u); + } + } + + WHEN("an ID names the class and subclass") + { + THEN("it matches with a score of 2") + { + REQUIRE(subclass_id().match(info) == 2u); + } + } + + WHEN("an ID names the class, subclass and programming interface") + { + THEN("it matches with a score of 3") + { + REQUIRE(full_class_id().match(info) == 3u); + } + } + + WHEN("an ID names the wrong class") + { + auto const id = pci::id{.class_code = 0x02}; + + THEN("it does not match") + { + REQUIRE_FALSE(id.match(info).has_value()); + } + } + + WHEN("an ID names the right class but the wrong subclass") + { + auto const id = pci::id{.class_code = class_mass_storage, .subclass_code = 0x07}; + + THEN("it does not match, not even partially") + { + REQUIRE_FALSE(id.match(info).has_value()); + } + } + + WHEN("an ID names the right class and subclass but the wrong programming interface") + { + auto const id = pci::id{ + .class_code = class_mass_storage, + .subclass_code = subclass_sata, + .programming_interface = 0x02, + }; + + THEN("it does not match, not even partially") + { + REQUIRE_FALSE(id.match(info).has_value()); + } + } + + WHEN("an ID names the exact device and the class") + { + auto const id = pci::id{ + .vendor_id = vendor_intel, + .device_id = device_ich9_ahci, + .class_code = class_mass_storage, + }; + + THEN("the scores add up") + { + REQUIRE(id.match(info) == pci::id::exact_id_score + 1u); + } + } + + WHEN("an ID names the exact device but the wrong class") + { + auto const id = pci::id{ + .vendor_id = vendor_intel, + .device_id = device_ich9_ahci, + .class_code = 0x02, + }; + + THEN("it does not match") + { + REQUIRE_FALSE(id.match(info).has_value()); + } + } + } +} + +SCENARIO("Malformed PCI IDs never match", "[bus][pci]") +{ + GIVEN("an AHCI controller function") + { + auto const info = ahci_controller(); + + WHEN("an ID names nothing at all") + { + THEN("it does not match") + { + REQUIRE_FALSE(pci::id{}.match(info).has_value()); + } + } + + WHEN("an ID names only a vendor") + { + THEN("it does not match") + { + REQUIRE_FALSE(pci::id{.vendor_id = vendor_intel}.match(info).has_value()); + } + } + + WHEN("an ID names only a device") + { + THEN("it does not match") + { + REQUIRE_FALSE(pci::id{.device_id = device_ich9_ahci}.match(info).has_value()); + } + } + + WHEN("an ID names a vendor without a device, but a correct class") + { + auto const id = pci::id{.vendor_id = vendor_intel, .class_code = class_mass_storage}; + + THEN("it does not match on the class alone") + { + REQUIRE_FALSE(id.match(info).has_value()); + } + } + + WHEN("an ID names a subclass without a class") + { + auto const id = pci::id{.subclass_code = subclass_sata}; + + THEN("it does not match") + { + REQUIRE_FALSE(id.match(info).has_value()); + } + } + + WHEN("an ID names a programming interface without a subclass or class") + { + auto const id = pci::id{.programming_interface = programming_interface_ahci}; + + THEN("it does not match") + { + REQUIRE_FALSE(id.match(info).has_value()); + } + } + + WHEN("an ID names a class and programming interface but no subclass") + { + auto const id = pci::id{.class_code = class_mass_storage, .programming_interface = programming_interface_ahci}; + + THEN("it does not match on the class alone") + { + REQUIRE_FALSE(id.match(info).has_value()); + } + } + } +} + +SCENARIO("More specific PCI IDs score higher", "[bus][pci]") +{ + GIVEN("an AHCI controller function") + { + auto const info = ahci_controller(); + + THEN("an exact ID outranks every class-based ID, and each additional class field raises the score") + { + auto const exact = *exact_id().match(info); + auto const full = *full_class_id().match(info); + auto const sub = *subclass_id().match(info); + auto const cls = *class_id().match(info); + + REQUIRE(exact > full); + REQUIRE(full > sub); + REQUIRE(sub > cls); + } + } +} diff --git a/kernel/kernel/bus/pci.cpp b/kernel/kernel/bus/pci/protocol.cpp index 16953910..cfad574c 100644 --- a/kernel/kernel/bus/pci.cpp +++ b/kernel/kernel/bus/pci/protocol.cpp @@ -1,6 +1,8 @@ -#include <kernel/bus/pci.hpp> +#include <kernel/bus/pci/protocol.hpp> -#include <kernel/devices/pci/function_info.hpp> +#include <kernel/bus/pci/claim.hpp> +#include <kernel/bus/pci/function_info.hpp> +#include <kernel/bus/pci/signature.hpp> #include <kapi/devices.hpp> @@ -10,7 +12,7 @@ #include <cstdint> #include <ranges> -namespace kernel::bus +namespace kernel::bus::pci { namespace @@ -20,7 +22,7 @@ namespace kernel::bus //! //! @param info The PCI info to match against //! @return A callable object that takes a PCI ID and returns the match score - constexpr auto matcher_for(devices::pci::function_info const & info) + constexpr auto matcher_for(function_info const & info) { return [&info](auto const & id) { return id.match(info).value_or(0); @@ -29,11 +31,11 @@ namespace kernel::bus } // namespace - auto pci_protocol::match(kapi::devices::device const & device, kapi::devices::driver const & driver) const + auto protocol::match(kapi::devices::device const & device, kapi::devices::driver const & driver) const -> kstd::result<std::uint32_t> { - auto signature = device.facet<pci_signature>(); - auto claim = driver.facet<pci_claim>(); + auto signature = device.facet<struct signature>(); + auto claim = driver.facet<struct claim>(); if (!signature || !claim) { @@ -55,4 +57,4 @@ namespace kernel::bus return kstd::success(score); } -} // namespace kernel::bus +} // namespace kernel::bus::pci diff --git a/kernel/kernel/bus/pci/protocol.hpp b/kernel/kernel/bus/pci/protocol.hpp new file mode 100644 index 00000000..b55ec43d --- /dev/null +++ b/kernel/kernel/bus/pci/protocol.hpp @@ -0,0 +1,37 @@ +#ifndef TEACHOS_KERNEL_BUS_PCI_PROTOCOL_HPP +#define TEACHOS_KERNEL_BUS_PCI_PROTOCOL_HPP + +// IWYU pragma: private, include <kernel/bus/pci.hpp> + +#include <kapi/devices.hpp> + +#include <kstd/result.hpp> + +#include <cstdint> + +namespace kernel::bus::pci +{ + + //! The base class for all PCI protocols. + struct protocol : kapi::devices::bus_protocol + { + //! Enumerate the PCI devices on a given bus. + //! + //! Concrete subclasses of pci_protocol must implement this function. + //! + //! @param bus The bus to enumerate. + //! @return void. + auto enumerate(kapi::devices::bus & bus) -> void override = 0; + + //! Match a device against a driver to determine compatibility. + //! + //! @param device The device to match. + //! @param driver The driver to match against. + //! @return The match score on success, an error otherwise. + [[nodiscard]] auto match(kapi::devices::device const & device, kapi::devices::driver const & driver) const + -> kstd::result<std::uint32_t> final; + }; + +} // namespace kernel::bus::pci + +#endif diff --git a/kernel/kernel/bus/pci.tests.cpp b/kernel/kernel/bus/pci/protocol.tests.cpp index f44f3f2a..462f5dcd 100644 --- a/kernel/kernel/bus/pci.tests.cpp +++ b/kernel/kernel/bus/pci/protocol.tests.cpp @@ -1,6 +1,9 @@ -#include <kernel/bus/pci.hpp> +#include <kernel/bus/pci/protocol.hpp> -#include <kernel/devices/pci/function_info.hpp> +#include <kernel/bus/pci/claim.hpp> +#include <kernel/bus/pci/function_info.hpp> +#include <kernel/bus/pci/id.hpp> +#include <kernel/bus/pci/signature.hpp> #include <kapi/capabilities/facet_id.hpp> #include <kapi/devices.hpp> @@ -15,20 +18,15 @@ #include <cstdint> #include <initializer_list> -#include <optional> #include <span> #include <string_view> #include <utility> #include <vector> -// NOLINTBEGIN(readability-magic-numbers) +namespace pci = kernel::bus::pci; -using kernel::bus::pci_claim; -using kernel::bus::pci_id; -using kernel::bus::pci_protocol; -using kernel::bus::pci_signature; -using kernel::devices::pci::function_info; using kstd::tests::require_ok; +using pci::function_info; namespace { @@ -52,7 +50,7 @@ namespace return info; } - struct test_signature final : pci_signature + struct test_signature final : pci::signature { explicit test_signature(function_info info) : m_info{info} @@ -78,9 +76,9 @@ namespace protected: auto query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> override { - if (facet == pci_signature::id) + if (facet == pci::signature::id) { - return kstd::make_observer<pci_signature>(&m_signature); + return kstd::make_observer<pci::signature>(&m_signature); } return device::query_facet(facet); @@ -97,9 +95,9 @@ namespace }; //! A driver that claims a table of PCI IDs and records how often it was probed. - struct test_driver final : kapi::devices::driver, pci_claim + struct test_driver final : kapi::devices::driver, pci::claim { - explicit test_driver(std::vector<pci_id> ids, bool probe_succeeds = true) + explicit test_driver(std::vector<pci::id> ids, bool probe_succeeds = true) : m_ids{std::move(ids)} , m_probe_succeeds{probe_succeeds} {} @@ -123,7 +121,7 @@ namespace return "PCI Test Driver"; } - [[nodiscard]] auto supported_ids() const noexcept -> std::span<pci_id const> override + [[nodiscard]] auto supported_ids() const noexcept -> std::span<pci::id const> override { return m_ids; } @@ -133,16 +131,16 @@ namespace protected: auto query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> override { - if (facet == pci_claim::id) + if (facet == pci::claim::id) { - return kstd::make_observer<pci_claim>(this); + return kstd::make_observer<pci::claim>(this); } return driver::query_facet(facet); } private: - std::vector<pci_id> m_ids; + std::vector<pci::id> m_ids; bool m_probe_succeeds; }; @@ -162,7 +160,7 @@ namespace } }; - struct test_protocol final : pci_protocol + struct test_protocol final : pci::protocol { auto enumerate(kapi::devices::bus &) -> void override {} } constinit protocol_instance; @@ -179,32 +177,32 @@ namespace return !result && result.error() == kapi::devices::make_error_code(kapi::devices::driver_match_errc::no_match); } - auto table(std::initializer_list<pci_id> ids) -> std::vector<pci_id> + auto table(std::initializer_list<pci::id> ids) -> std::vector<pci::id> { return ids; } - auto exact_id() -> pci_id + auto exact_id() -> pci::id { return {.vendor_id = vendor_intel, .device_id = device_ich9_ahci}; } - auto other_exact_id() -> pci_id + auto other_exact_id() -> pci::id { return {.vendor_id = vendor_red_hat, .device_id = device_virtio_blk}; } - auto class_id() -> pci_id + auto class_id() -> pci::id { return {.class_code = class_mass_storage}; } - auto subclass_id() -> pci_id + auto subclass_id() -> pci::id { return {.class_code = class_mass_storage, .subclass_code = subclass_sata}; } - auto full_class_id() -> pci_id + auto full_class_id() -> pci::id { return { .class_code = class_mass_storage, @@ -214,220 +212,6 @@ namespace } } // namespace -SCENARIO("PCI IDs are scored against a function", "[bus][pci]") -{ - GIVEN("an AHCI controller function") - { - auto const info = ahci_controller(); - - WHEN("an ID names the vendor and device exactly") - { - THEN("it matches with the exact ID score") - { - REQUIRE(exact_id().match(info) == pci_id::exact_id_score); - } - } - - WHEN("an ID names the right vendor but the wrong device") - { - auto const id = pci_id{.vendor_id = vendor_intel, .device_id = device_virtio_blk}; - - THEN("it does not match") - { - REQUIRE_FALSE(id.match(info).has_value()); - } - } - - WHEN("an ID names the right device but the wrong vendor") - { - auto const id = pci_id{.vendor_id = vendor_red_hat, .device_id = device_ich9_ahci}; - - THEN("it does not match") - { - REQUIRE_FALSE(id.match(info).has_value()); - } - } - - WHEN("an ID names only the class") - { - THEN("it matches with a score of 1") - { - REQUIRE(class_id().match(info) == 1u); - } - } - - WHEN("an ID names the class and subclass") - { - THEN("it matches with a score of 2") - { - REQUIRE(subclass_id().match(info) == 2u); - } - } - - WHEN("an ID names the class, subclass and programming interface") - { - THEN("it matches with a score of 3") - { - REQUIRE(full_class_id().match(info) == 3u); - } - } - - WHEN("an ID names the wrong class") - { - auto const id = pci_id{.class_code = 0x02}; - - THEN("it does not match") - { - REQUIRE_FALSE(id.match(info).has_value()); - } - } - - WHEN("an ID names the right class but the wrong subclass") - { - auto const id = pci_id{.class_code = class_mass_storage, .subclass_code = 0x07}; - - THEN("it does not match, not even partially") - { - REQUIRE_FALSE(id.match(info).has_value()); - } - } - - WHEN("an ID names the right class and subclass but the wrong programming interface") - { - auto const id = pci_id{ - .class_code = class_mass_storage, - .subclass_code = subclass_sata, - .programming_interface = 0x02, - }; - - THEN("it does not match, not even partially") - { - REQUIRE_FALSE(id.match(info).has_value()); - } - } - - WHEN("an ID names the exact device and the class") - { - auto const id = pci_id{ - .vendor_id = vendor_intel, - .device_id = device_ich9_ahci, - .class_code = class_mass_storage, - }; - - THEN("the scores add up") - { - REQUIRE(id.match(info) == pci_id::exact_id_score + 1u); - } - } - - WHEN("an ID names the exact device but the wrong class") - { - auto const id = pci_id{ - .vendor_id = vendor_intel, - .device_id = device_ich9_ahci, - .class_code = 0x02, - }; - - THEN("it does not match") - { - REQUIRE_FALSE(id.match(info).has_value()); - } - } - } -} - -SCENARIO("Malformed PCI IDs never match", "[bus][pci]") -{ - GIVEN("an AHCI controller function") - { - auto const info = ahci_controller(); - - WHEN("an ID names nothing at all") - { - THEN("it does not match") - { - REQUIRE_FALSE(pci_id{}.match(info).has_value()); - } - } - - WHEN("an ID names only a vendor") - { - THEN("it does not match") - { - REQUIRE_FALSE(pci_id{.vendor_id = vendor_intel}.match(info).has_value()); - } - } - - WHEN("an ID names only a device") - { - THEN("it does not match") - { - REQUIRE_FALSE(pci_id{.device_id = device_ich9_ahci}.match(info).has_value()); - } - } - - WHEN("an ID names a vendor without a device, but a correct class") - { - auto const id = pci_id{.vendor_id = vendor_intel, .class_code = class_mass_storage}; - - THEN("it does not match on the class alone") - { - REQUIRE_FALSE(id.match(info).has_value()); - } - } - - WHEN("an ID names a subclass without a class") - { - auto const id = pci_id{.subclass_code = subclass_sata}; - - THEN("it does not match") - { - REQUIRE_FALSE(id.match(info).has_value()); - } - } - - WHEN("an ID names a programming interface without a subclass or class") - { - auto const id = pci_id{.programming_interface = programming_interface_ahci}; - - THEN("it does not match") - { - REQUIRE_FALSE(id.match(info).has_value()); - } - } - - WHEN("an ID names a class and programming interface but no subclass") - { - auto const id = pci_id{.class_code = class_mass_storage, .programming_interface = programming_interface_ahci}; - - THEN("it does not match on the class alone") - { - REQUIRE_FALSE(id.match(info).has_value()); - } - } - } -} - -SCENARIO("More specific PCI IDs score higher", "[bus][pci]") -{ - GIVEN("an AHCI controller function") - { - auto const info = ahci_controller(); - - THEN("an exact ID outranks every class-based ID, and each additional class field raises the score") - { - auto const exact = *exact_id().match(info); - auto const full = *full_class_id().match(info); - auto const sub = *subclass_id().match(info); - auto const cls = *class_id().match(info); - - REQUIRE(exact > full); - REQUIRE(full > sub); - REQUIRE(sub > cls); - } - } -} - SCENARIO("The PCI protocol rejects pairings that cannot match", "[bus][pci]") { GIVEN("an AHCI controller device") @@ -456,7 +240,7 @@ SCENARIO("The PCI protocol rejects pairings that cannot match", "[bus][pci]") WHEN("none of the driver's entries match") { - auto const driver = test_driver{table({other_exact_id(), pci_id{.class_code = 0x02}})}; + auto const driver = test_driver{table({other_exact_id(), pci::id{.class_code = 0x02}})}; THEN("there is no match") { @@ -503,7 +287,7 @@ SCENARIO("The PCI protocol scores a driver by its best entry", "[bus][pci]") THEN("the score is the exact ID score") { - REQUIRE(require_ok(protocol_instance.match(device, driver)) == pci_id::exact_id_score); + REQUIRE(require_ok(protocol_instance.match(device, driver)) == pci::id::exact_id_score); } } @@ -513,7 +297,7 @@ SCENARIO("The PCI protocol scores a driver by its best entry", "[bus][pci]") THEN("the score is the best single entry, not the sum") { - REQUIRE(require_ok(protocol_instance.match(device, driver)) == pci_id::exact_id_score); + REQUIRE(require_ok(protocol_instance.match(device, driver)) == pci::id::exact_id_score); } } @@ -618,5 +402,3 @@ SCENARIO("A PCI device nobody claims stays present", "[bus][pci]") } } } - -// NOLINTEND(readability-magic-numbers) diff --git a/kernel/kernel/bus/pci/signature.hpp b/kernel/kernel/bus/pci/signature.hpp new file mode 100644 index 00000000..c2936634 --- /dev/null +++ b/kernel/kernel/bus/pci/signature.hpp @@ -0,0 +1,30 @@ +#ifndef TEACHOS_KERNEL_BUS_PCI_SIGNATURE_HPP +#define TEACHOS_KERNEL_BUS_PCI_SIGNATURE_HPP + +// IWYU pragma: private, include <kernel/bus/pci.hpp> + +#include <kernel/bus/pci/function_info.hpp> + +#include <kapi/capabilities/facet_id.hpp> + +namespace kernel::bus::pci +{ + + //! The signature of a PCI device. + struct signature + { + //! The unique ID of this facet. + constexpr auto static id = kapi::capabilities::facet_id{"sig.dev.pci"}; + + //! Virtual destructor to allow for safe deletion of derived objects through base pointers. + virtual ~signature() = default; + + //! Retrieve the PCI function information associated with the device providing this facet. + //! + //! @return The PCI function information associated with the device. + [[nodiscard]] virtual auto info() const noexcept -> function_info const & = 0; + }; + +} // namespace kernel::bus::pci + +#endif diff --git a/kernel/kernel/devices/pci/init.cpp b/kernel/kernel/devices/pci.cpp index f3efaa87..bfc1423a 100644 --- a/kernel/kernel/devices/pci/init.cpp +++ b/kernel/kernel/devices/pci.cpp @@ -1,11 +1,11 @@ -#include <kernel/devices/pci/init.hpp> +#include <kernel/devices/pci.hpp> #include <kernel/devices/pci/ecam_segment.hpp> -#include <kernel/devices/pci/function_info.hpp> -#include <kernel/devices/pci/scan.hpp> #include <kapi/devices.hpp> +#include <kapi/system.hpp> +#include <kstd/memory.hpp> #include <kstd/print.hpp> #include <kstd/vector.hpp> @@ -23,22 +23,21 @@ namespace kernel::devices::pci kstd::println("[OS:DEV] Detected number of PCI Express segments: {}", descriptors.size()); + auto root = kapi::devices::get_root_bus(); + for (auto const & descriptor : descriptors) { auto segment = ecam_segment{descriptor}; - auto functions = scan_segment(segment); - if (!functions) - { - kstd::println(kstd::print_sink::stderr, "[OS:DEV] Failed to scan segment {}: {}", segment.group(), - functions.error()); - continue; - } + auto host = kstd::make_shared<host_bridge>(segment); + root->add_child(host); - for (auto const & function : *functions) + auto protocol = host->facet<kapi::devices::bus_protocol>(); + if (!protocol) { - kstd::println("[OS:DEV] Detected PCI Express device: {} [{:04x}:{:04x}]", function.address, function.vendor_id, - function.device_id); + kapi::system::panic("[OS:DEV] Failed to get bus protocol for host bridge {}", host->name()); } + + protocol->enumerate(*host); } } diff --git a/kernel/kernel/devices/pci.hpp b/kernel/kernel/devices/pci.hpp new file mode 100644 index 00000000..8b1d29eb --- /dev/null +++ b/kernel/kernel/devices/pci.hpp @@ -0,0 +1,22 @@ +#ifndef TEACHOS_KERNEL_DEVICES_PCI_HPP +#define TEACHOS_KERNEL_DEVICES_PCI_HPP + +#include <kernel/devices/pci/bridge.hpp> // IWYU pragma: export +#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 + +namespace kernel::devices::pci +{ + + //! Initialize the PCI subsystem. + //! + //! This function attaches each detected PCI host bridge to the system and enumerates its devices in turn. + auto init() -> void; + +} // namespace kernel::devices::pci + +#endif diff --git a/kernel/kernel/devices/pci/bridge.cpp b/kernel/kernel/devices/pci/bridge.cpp new file mode 100644 index 00000000..aa73aad8 --- /dev/null +++ b/kernel/kernel/devices/pci/bridge.cpp @@ -0,0 +1,55 @@ +#include <kernel/devices/pci/bridge.hpp> + +#include <kernel/bus/pci.hpp> +#include <kernel/devices/pci/ecam_segment.hpp> +#include <kernel/devices/pci/function_access.hpp> + +#include <kapi/capabilities/facet_id.hpp> +#include <kapi/devices.hpp> + +#include <kstd/format.hpp> +#include <kstd/memory.hpp> + +#include <utility> + +namespace kernel::devices::pci +{ + + namespace + { + + struct bridge_protocol final : bus::pci::protocol + { + public: + auto enumerate(kapi::devices::bus &) -> void override + { + // This implementation intentionally left blank. + // + // It is the task of the host-bridge ("segment") to enumerate all devices on all buses. Therefore, when a bridge + // is constructed during host-bridge enumeration, it does not need to enumerate its own children, since the + // host-bridge's protocol will already have done so and will have attached children accordingly. + } + } constinit protocol_instance{}; + + } // namespace + + bridge::bridge(kernel::bus::pci::function_info info, ecam_segment segment) + : bus{kstd::format("{}", info.address), protocol_instance} + , m_access(std::move(info), std::move(segment)) + {} + + auto bridge::query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> + { + if (facet == kernel::bus::pci::signature::id) + { + return kstd::make_observer<kernel::bus::pci::signature>(&m_access); + } + else if (facet == kernel::bus::pci::configuration_access::id) + { + return kstd::make_observer<kernel::bus::pci::configuration_access>(&m_access); + } + + return bus::query_facet(facet); + } + +} // namespace kernel::devices::pci diff --git a/kernel/kernel/devices/pci/bridge.hpp b/kernel/kernel/devices/pci/bridge.hpp new file mode 100644 index 00000000..a12aa552 --- /dev/null +++ b/kernel/kernel/devices/pci/bridge.hpp @@ -0,0 +1,43 @@ +#ifndef TEACHOS_KERNEL_DEVICES_PCI_BRIDGE_HPP +#define TEACHOS_KERNEL_DEVICES_PCI_BRIDGE_HPP + +// IWYU pragma: private, include <kernel/devices/pci.hpp> + +#include <kernel/bus/pci.hpp> +#include <kernel/devices/pci/ecam_segment.hpp> +#include <kernel/devices/pci/function_access.hpp> + +#include <kapi/capabilities/facet_id.hpp> +#include <kapi/devices.hpp> + +#include <kstd/memory.hpp> + +namespace kernel::devices::pci +{ + + //! A PCI-to-PCI bridge device. + //! + //! Bridges are bus devices, because they are real hardware components that connect two logical PCI buses. + struct bridge final : kapi::devices::bus + { + //! Construct a new bridge device using the given function information and ECAM segment. + //! + //! @param info The PCI function information for this bridge. + //! @param segment The ECAM segment that contains this bridge. + bridge(kernel::bus::pci::function_info info, ecam_segment segment); + + protected: + //! Query a facet provided by this bridge device. + //! + //! @param facet The facet ID to query. + //! @return An observer pointer to the requested facet, or nullptr if not supported. + auto query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> final; + + private: + //! The function access object used to interact with this bridge's configuration space. + function_access m_access; + }; + +} // namespace kernel::devices::pci + +#endif diff --git a/kernel/kernel/devices/pci/bridge.tests.cpp b/kernel/kernel/devices/pci/bridge.tests.cpp new file mode 100644 index 00000000..67d44ab1 --- /dev/null +++ b/kernel/kernel/devices/pci/bridge.tests.cpp @@ -0,0 +1,268 @@ +#include <kernel/devices/pci/bridge.hpp> + +#include <kernel/bus/pci.hpp> +#include <kernel/devices/pci/ecam_segment.hpp> +#include <kernel/devices/pci/endpoint.hpp> +#include <kernel/devices/pci/registers.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 <kstd/string.hpp> +#include <kstd/test_support/result.hpp> +#include <kstd/units.hpp> + +#include <catch2/catch_test_macros.hpp> + +#include <array> +#include <cstdint> +#include <span> +#include <string_view> + +// NOLINTBEGIN(readability-magic-numbers) + +namespace +{ + namespace pci = kernel::bus::pci; + + using kernel::devices::pci::bridge; + using kernel::devices::pci::ecam_segment; + using kernel::devices::pci::endpoint; + using kernel::devices::pci::function_offset; + + constexpr auto ecam_test_base = kapi::memory::physical_address{}; + constexpr auto group = std::uint16_t{3}; + + //! A segment covering every bus of the test MMIO space. + constexpr auto descriptor = kapi::devices::pcie_segment_descriptor{ + .base_address = ecam_test_base, + .group_number = group, + .first_bus = 0, + .bus_count = kernel::tests::memory::mmio_size / kernel::devices::pci::mmio_window_per_bus, + }; + + //! A PCI-to-PCI bridge that forwards bus 1 to bus 2. + auto bridge_at(pci::function_address address) -> pci::function_info + { + return pci::function_info{ + .address = address, + .vendor_id = 0x8086, + .device_id = 0x244e, + .revision = 1, + .programming_interface = 0, + .subclass_code = 4, + .class_code = 6, + .raw_header_type = 1, + .secondary_bus = 2, + .subordinate_bus = 2, + }; + } + + auto ahci_controller_at(pci::function_address address) -> pci::function_info + { + return pci::function_info{ + .address = address, + .vendor_id = 0x8086, + .device_id = 0x2922, + .revision = 2, + .programming_interface = 1, + .subclass_code = 6, + .class_code = 1, + }; + } + + //! A driver that claims the AHCI controller and counts how often it was probed and unbound. + struct ahci_driver final : kapi::devices::driver, pci::claim + { + ahci_driver() + : m_ids{ + pci::id{.vendor_id = 0x8086, .device_id = 0x2922} + } + {} + + [[nodiscard]] auto probe(kapi::devices::device &) -> kstd::result<void> override + { + ++probe_calls; + return kstd::success(); + } + + auto unbind(kapi::devices::device &) -> void override + { + ++unbind_calls; + } + + [[nodiscard]] auto name() const noexcept -> std::string_view override + { + return "PCI AHCI Test Driver"; + } + + [[nodiscard]] auto supported_ids() const noexcept -> std::span<pci::id const> override + { + return m_ids; + } + + unsigned probe_calls{}; + unsigned unbind_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{}; + }; + + struct test_protocol final : pci::protocol + { + auto enumerate(kapi::devices::bus &) -> void override {} + } constinit protocol_instance; + + struct test_bus final : kapi::devices::bus + { + explicit test_bus(kstd::string name) + : bus{name, protocol_instance} + {} + }; +} // namespace + +SCENARIO("A bridge is named after its function's address", "[devices][pci][bridge]") +{ + GIVEN("a bridge in a non-zero segment group") + { + auto const device = + kstd::make_shared<bridge>(bridge_at(pci::function_address{group, 0, 28, 1}), ecam_segment{descriptor}); + + THEN("its name is the formatted function address") + { + REQUIRE(device->name() == "0003:00:1c.1"); + } + + THEN("it has not been attached to a bus yet") + { + REQUIRE(device->state() == kapi::devices::state::uninitialized); + } + + THEN("it has no children") + { + REQUIRE(device->children().empty()); + } + } +} + +SCENARIO("A bridge is both a PCI function and a bus", "[devices][pci][bridge]") +{ + GIVEN("a bridge") + { + auto const info = bridge_at(pci::function_address{group, 0, 28, 1}); + auto const device = kstd::make_shared<bridge>(info, ecam_segment{descriptor}); + + THEN("it provides a PCI signature carrying its function's information") + { + auto const signature = device->facet<pci::signature>(); + + REQUIRE(signature); + REQUIRE(signature->info().address == info.address); + REQUIRE(signature->info().secondary_bus == info.secondary_bus); + REQUIRE(signature->info().vendor_id == info.vendor_id); + } + + THEN("it provides configuration space access") + { + REQUIRE(device->has_facet<pci::configuration_access>()); + } + + THEN("it provides the bus and the bus protocol facets") + { + REQUIRE(device->has_facet<kapi::devices::bus>()); + REQUIRE(device->has_facet<kapi::devices::bus_protocol>()); + } + + THEN("it does not claim anything") + { + REQUIRE_FALSE(device->has_facet<pci::claim>()); + } + } +} + +SCENARIO("A bridge's configuration space is reachable through its facet", "[devices][pci][bridge]") +{ + GIVEN("a bridge whose function has data in its configuration space") + { + constexpr auto marker_offset = kstd::bytes{0x40}; + constexpr auto marker = std::uint16_t{0xa5a5}; + + auto const address = pci::function_address{group, 0, 28, 1}; + + auto const bus_base = ecam_test_base + address.bus * kernel::devices::pci::mmio_window_per_bus; + auto bus_window = kapi::memory::mmio_window::map(bus_base, kernel::devices::pci::mmio_window_per_bus, + kapi::memory::mmio_window::access::writable); + REQUIRE(bus_window); + bus_window->write<std::uint16_t>(function_offset(address.device, address.function) + marker_offset, marker); + + auto const device = kstd::make_shared<bridge>(bridge_at(address), ecam_segment{descriptor}); + + THEN("a mapping obtained through the facet shows that function's data") + { + auto const access = device->facet<pci::configuration_access>(); + REQUIRE(access); + + auto const window = kstd::tests::require_ok(access->map_config(kapi::memory::mmio_window::access::read_only)); + + REQUIRE(window.read<std::uint16_t>(marker_offset) == marker); + } + } +} + +SCENARIO("Devices behind a bridge are matched against drivers", "[devices][pci][bridge]") +{ + GIVEN("a bridge that nobody claims on a bus, and a bound endpoint behind it") + { + auto const root = kstd::make_shared<test_bus>("bridge_test_root"); + auto const driver = kstd::make_shared<ahci_driver>(); + auto const bridge_device = + kstd::make_shared<bridge>(bridge_at(pci::function_address{group, 0, 28, 1}), ecam_segment{descriptor}); + auto const endpoint_device = kstd::make_shared<endpoint>(ahci_controller_at(pci::function_address{group, 2, 0, 0}), + ecam_segment{descriptor}); + + kapi::devices::driver_registry::get().add(driver); + root->add_child(bridge_device); + bridge_device->add_child(endpoint_device); + + // The state before removal is checked as part of the setup: this scenario must keep exactly one leaf. + REQUIRE(bridge_device->parent() == root); + REQUIRE(bridge_device->state() == kapi::devices::state::present); + REQUIRE(bridge_device->bound_driver() == nullptr); + REQUIRE(endpoint_device->parent() == bridge_device); + REQUIRE(bridge_device->children().size() == 1); + REQUIRE(endpoint_device->state() == kapi::devices::state::bound); + REQUIRE(endpoint_device->bound_driver() == driver); + REQUIRE(driver->probe_calls == 1); + + WHEN("the bridge is removed from its bus") + { + REQUIRE(root->remove_child(*bridge_device)); + + THEN("the endpoint behind it is unbound and removed too") + { + REQUIRE(driver->unbind_calls == 1); + REQUIRE(endpoint_device->state() == kapi::devices::state::removed); + REQUIRE(bridge_device->state() == kapi::devices::state::removed); + REQUIRE(bridge_device->children().empty()); + } + } + } +} + +// NOLINTEND(readability-magic-numbers) diff --git a/kernel/kernel/devices/pci/ecam_segment.cpp b/kernel/kernel/devices/pci/ecam_segment.cpp index 69aebf58..ce46bfe4 100644 --- a/kernel/kernel/devices/pci/ecam_segment.cpp +++ b/kernel/kernel/devices/pci/ecam_segment.cpp @@ -1,6 +1,6 @@ #include <kernel/devices/pci/ecam_segment.hpp> -#include <kernel/devices/pci/function_address.hpp> +#include <kernel/bus/pci.hpp> #include <kernel/devices/pci/registers.hpp> #include <kapi/devices.hpp> @@ -57,7 +57,7 @@ namespace kernel::devices::pci kapi::memory::mmio_window::access::read_only); } - auto ecam_segment::map_function(function_address address, kapi::memory::mmio_window::access mode) const + auto ecam_segment::map_function(bus::pci::function_address address, kapi::memory::mmio_window::access mode) const -> kstd::result<kapi::memory::mmio_window> { if (m_group != address.segment_group) diff --git a/kernel/kernel/devices/pci/ecam_segment.hpp b/kernel/kernel/devices/pci/ecam_segment.hpp index 3baf1eb8..1ab3595e 100644 --- a/kernel/kernel/devices/pci/ecam_segment.hpp +++ b/kernel/kernel/devices/pci/ecam_segment.hpp @@ -1,7 +1,9 @@ #ifndef TEACHOS_KERNEL_DEVICES_PCI_ECAM_SEGMENT_HPP #define TEACHOS_KERNEL_DEVICES_PCI_ECAM_SEGMENT_HPP -#include <kernel/devices/pci/function_address.hpp> +// IWYU pragma: private, include <kernel/devices/pci.hpp> + +#include <kernel/bus/pci.hpp> #include <kernel/devices/pci/registers.hpp> #include <kapi/devices.hpp> @@ -53,7 +55,7 @@ namespace kernel::devices::pci //! @param address The address of the function to map. //! @param mode The access mode (read-only or read-write) for the MMIO window. //! @return An MMIO window for the function if successful, an error otherwise. - [[nodiscard]] auto map_function(function_address address, kapi::memory::mmio_window::access mode) const + [[nodiscard]] auto map_function(bus::pci::function_address address, kapi::memory::mmio_window::access mode) const -> kstd::result<kapi::memory::mmio_window>; private: diff --git a/kernel/kernel/devices/pci/ecam_segment.tests.cpp b/kernel/kernel/devices/pci/ecam_segment.tests.cpp index 275fd52b..0d575ac3 100644 --- a/kernel/kernel/devices/pci/ecam_segment.tests.cpp +++ b/kernel/kernel/devices/pci/ecam_segment.tests.cpp @@ -1,6 +1,6 @@ #include <kernel/devices/pci/ecam_segment.hpp> -#include <kernel/devices/pci/function_address.hpp> +#include <kernel/bus/pci.hpp> #include <kernel/devices/pci/registers.hpp> #include <kernel/test_support/memory.hpp> @@ -322,7 +322,7 @@ SCENARIO("ECAM segment mapping", "[devices][pci][ecam]") SCENARIO("ECAM segment function mapping", "[devices][pci][ecam]") { - using kernel::devices::pci::function_address; + using kernel::bus::pci::function_address; using kernel::devices::pci::function_offset; GIVEN("A valid segment with a non-zero group and first bus") diff --git a/kernel/kernel/devices/pci/endpoint.cpp b/kernel/kernel/devices/pci/endpoint.cpp new file mode 100644 index 00000000..29ce28d1 --- /dev/null +++ b/kernel/kernel/devices/pci/endpoint.cpp @@ -0,0 +1,36 @@ +#include <kernel/devices/pci/endpoint.hpp> + +#include <kernel/bus/pci.hpp> +#include <kernel/devices/pci/ecam_segment.hpp> + +#include <kapi/capabilities/facet_id.hpp> +#include <kapi/devices.hpp> + +#include <kstd/format.hpp> +#include <kstd/memory.hpp> + +#include <utility> + +namespace kernel::devices::pci +{ + + endpoint::endpoint(bus::pci::function_info info, ecam_segment segment) + : device{kstd::format("{}", info.address)} + , m_access{std::move(info), std::move(segment)} + {} + + auto endpoint::query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> + { + if (facet == bus::pci::signature::id) + { + return kstd::make_observer<bus::pci::signature>(&m_access); + } + else if (facet == bus::pci::configuration_access::id) + { + return kstd::make_observer<bus::pci::configuration_access>(&m_access); + } + + return device::query_facet(facet); + } + +} // namespace kernel::devices::pci diff --git a/kernel/kernel/devices/pci/endpoint.hpp b/kernel/kernel/devices/pci/endpoint.hpp new file mode 100644 index 00000000..690583f0 --- /dev/null +++ b/kernel/kernel/devices/pci/endpoint.hpp @@ -0,0 +1,41 @@ +#ifndef TEACHOS_KERNEL_DEVICES_PCI_ENDPOINT_HPP +#define TEACHOS_KERNEL_DEVICES_PCI_ENDPOINT_HPP + +// IWYU pragma: private, include <kernel/devices/pci.hpp> + +#include <kernel/bus/pci.hpp> +#include <kernel/devices/pci/ecam_segment.hpp> +#include <kernel/devices/pci/function_access.hpp> + +#include <kapi/capabilities/facet_id.hpp> +#include <kapi/devices.hpp> + +#include <kstd/memory.hpp> + +namespace kernel::devices::pci +{ + + //! A PCI endpoint device, representing a single, non-bridge function on a PCI bus. + struct endpoint final : kapi::devices::device + { + //! Construct a new endpoint device using the given function information and ECAM segment. + //! + //! @param info The PCI function information for this endpoint. + //! @param segment The ECAM segment that contains this endpoint. + endpoint(bus::pci::function_info info, ecam_segment segment); + + protected: + //! Query a facet provided by this endpoint. + //! + //! @param facet The facet ID to query. + //! @return An observer pointer to the requested facet, or nullptr if not available. + auto query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> final; + + private: + //! The function access object used to interact with this endpoint's configuration space. + function_access m_access; + }; + +} // namespace kernel::devices::pci + +#endif diff --git a/kernel/kernel/devices/pci/endpoint.tests.cpp b/kernel/kernel/devices/pci/endpoint.tests.cpp new file mode 100644 index 00000000..752b4a14 --- /dev/null +++ b/kernel/kernel/devices/pci/endpoint.tests.cpp @@ -0,0 +1,263 @@ +#include <kernel/devices/pci/endpoint.hpp> + +#include <kernel/bus/pci.hpp> +#include <kernel/devices/pci/ecam_segment.hpp> +#include <kernel/devices/pci/registers.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 <kstd/string.hpp> +#include <kstd/test_support/result.hpp> +#include <kstd/units.hpp> + +#include <catch2/catch_test_macros.hpp> + +#include <array> +#include <cstdint> +#include <span> +#include <string_view> +#include <utility> + +// NOLINTBEGIN(readability-magic-numbers) + +namespace +{ + namespace pci = kernel::bus::pci; + + using kernel::devices::pci::ecam_segment; + using kernel::devices::pci::endpoint; + using kernel::devices::pci::function_offset; + + constexpr auto ecam_test_base = kapi::memory::physical_address{}; + constexpr auto group = std::uint16_t{3}; + constexpr auto interrupt_line = kstd::bytes{0x3c}; + + //! A segment covering every bus of the test MMIO space. + constexpr auto descriptor = kapi::devices::pcie_segment_descriptor{ + .base_address = ecam_test_base, + .group_number = group, + .first_bus = 0, + .bus_count = kernel::tests::memory::mmio_size / kernel::devices::pci::mmio_window_per_bus, + }; + + auto ahci_controller_at(pci::function_address address) -> pci::function_info + { + return pci::function_info{ + .address = address, + .vendor_id = 0x8086, + .device_id = 0x2922, + .revision = 2, + .programming_interface = 1, + .subclass_code = 6, + .class_code = 1, + }; + } + + //! The state a driver keeps for a bound endpoint. + struct driver_state + { + kapi::memory::mmio_window config; + }; + + //! A driver that maps its function's configuration space and keeps the window in its driver state. + struct config_driver final : kapi::devices::driver, pci::claim + { + config_driver() + : m_ids{ + pci::id{.vendor_id = 0x8086, .device_id = 0x2922} + } + {} + + [[nodiscard]] auto probe(kapi::devices::device & device) -> kstd::result<void> override + { + ++probe_calls; + + auto access = device.facet<pci::configuration_access>(); + if (!access) + { + return kstd::failure(kapi::devices::driver_match_errc::no_match); + } + + auto window = access->map_config(kapi::memory::mmio_window::access::writable); + if (!window) + { + return kstd::failure(window.error()); + } + + window->write<std::uint8_t>(interrupt_line, 0x0b); + device.set_driver_data(kstd::make_shared<driver_state>(std::move(*window))); + + return kstd::success(); + } + + auto unbind(kapi::devices::device & device) -> void override + { + ++unbind_calls; + device.set_driver_data(nullptr); + } + + [[nodiscard]] auto name() const noexcept -> std::string_view override + { + return "PCI Config Test Driver"; + } + + [[nodiscard]] auto supported_ids() const noexcept -> std::span<pci::id const> override + { + return m_ids; + } + + unsigned probe_calls{}; + unsigned unbind_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{}; + }; + + struct test_protocol final : pci::protocol + { + auto enumerate(kapi::devices::bus &) -> void override {} + } constinit protocol_instance; + + struct test_bus final : kapi::devices::bus + { + explicit test_bus(kstd::string name) + : bus{name, protocol_instance} + {} + }; +} // namespace + +SCENARIO("An endpoint is named after its function's address", "[devices][pci][endpoint]") +{ + GIVEN("an endpoint in a non-zero segment group") + { + auto const device = kstd::make_shared<endpoint>(ahci_controller_at(pci::function_address{group, 2, 5, 3}), + ecam_segment{descriptor}); + + THEN("its name is the formatted function address") + { + REQUIRE(device->name() == "0003:02:05.3"); + } + + THEN("it has not been attached to a bus yet") + { + REQUIRE(device->state() == kapi::devices::state::uninitialized); + } + } +} + +SCENARIO("An endpoint offers PCI facets and no more", "[devices][pci][endpoint]") +{ + GIVEN("an endpoint") + { + auto const info = ahci_controller_at(pci::function_address{group, 2, 5, 3}); + auto const device = kstd::make_shared<endpoint>(info, ecam_segment{descriptor}); + + THEN("it provides a PCI signature carrying its function's information") + { + auto const signature = device->facet<pci::signature>(); + + REQUIRE(signature); + REQUIRE(signature->info().address == info.address); + REQUIRE(signature->info().vendor_id == info.vendor_id); + REQUIRE(signature->info().device_id == info.device_id); + } + + THEN("it provides configuration space access") + { + REQUIRE(device->has_facet<pci::configuration_access>()); + } + + THEN("it is a leaf: it is not a bus and does not claim anything") + { + REQUIRE_FALSE(device->has_facet<kapi::devices::bus>()); + REQUIRE_FALSE(device->has_facet<pci::claim>()); + } + } +} + +SCENARIO("An endpoint's configuration space is reachable through its facet", "[devices][pci][endpoint]") +{ + GIVEN("an endpoint whose function has data in its configuration space") + { + constexpr auto marker_offset = kstd::bytes{0x40}; + constexpr auto marker = std::uint16_t{0xa5a5}; + + auto const address = pci::function_address{group, 2, 5, 3}; + + auto const bus_base = ecam_test_base + address.bus * kernel::devices::pci::mmio_window_per_bus; + auto bus_window = kapi::memory::mmio_window::map(bus_base, kernel::devices::pci::mmio_window_per_bus, + kapi::memory::mmio_window::access::writable); + REQUIRE(bus_window); + bus_window->write<std::uint16_t>(function_offset(address.device, address.function) + marker_offset, marker); + + auto const device = kstd::make_shared<endpoint>(ahci_controller_at(address), ecam_segment{descriptor}); + + THEN("a mapping obtained through the facet shows that function's data") + { + auto const access = device->facet<pci::configuration_access>(); + REQUIRE(access); + + auto const window = kstd::tests::require_ok(access->map_config(kapi::memory::mmio_window::access::read_only)); + + REQUIRE(window.read<std::uint16_t>(marker_offset) == marker); + } + } +} + +SCENARIO("A driver keeps an endpoint's configuration window in its driver state", "[devices][pci][endpoint]") +{ + GIVEN("a bound driver that wrote the Interrupt Line register during probe") + { + auto const address = pci::function_address{group, 1, 2, 3}; + auto const bus = kstd::make_shared<test_bus>("endpoint_config_bus"); + auto const driver = kstd::make_shared<config_driver>(); + auto const device = kstd::make_shared<endpoint>(ahci_controller_at(address), ecam_segment{descriptor}); + + kapi::devices::driver_registry::get().add(driver); + bus->add_child(device); + + auto const bus_base = ecam_test_base + address.bus * kernel::devices::pci::mmio_window_per_bus; + auto const bus_window = kstd::tests::require_ok(kapi::memory::mmio_window::map( + bus_base, kernel::devices::pci::mmio_window_per_bus, kapi::memory::mmio_window::access::read_only)); + auto const register_offset = function_offset(address.device, address.function) + interrupt_line; + + REQUIRE(device->state() == kapi::devices::state::bound); + REQUIRE(driver->probe_calls == 1); + REQUIRE(device->driver_data() != nullptr); + + REQUIRE(bus_window.read<std::uint8_t>(register_offset) == 0x0b); + + WHEN("the endpoint is removed from its bus") + { + REQUIRE(bus->remove_child(*device)); + + THEN("the driver is unbound and its state, with the window, is gone") + { + REQUIRE(driver->unbind_calls == 1); + REQUIRE(device->driver_data() == nullptr); + REQUIRE(device->state() == kapi::devices::state::removed); + + REQUIRE(bus_window.read<std::uint8_t>(register_offset) == 0x0b); + } + } + } +} + +// NOLINTEND(readability-magic-numbers) diff --git a/kernel/kernel/devices/pci/function_access.cpp b/kernel/kernel/devices/pci/function_access.cpp new file mode 100644 index 00000000..eb8c0356 --- /dev/null +++ b/kernel/kernel/devices/pci/function_access.cpp @@ -0,0 +1,31 @@ +#include <kernel/devices/pci/function_access.hpp> + +#include <kernel/bus/pci/function_info.hpp> +#include <kernel/devices/pci/ecam_segment.hpp> + +#include <kapi/memory.hpp> + +#include <kstd/result.hpp> + +#include <utility> + +namespace kernel::devices::pci +{ + + function_access::function_access(bus::pci::function_info info, ecam_segment segment) noexcept + : m_info{std::move(info)} + , m_segment{std::move(segment)} + {} + + auto function_access::info() const noexcept -> bus::pci::function_info const & + { + return m_info; + } + + auto function_access::map_config(kapi::memory::mmio_window::access mode) const + -> kstd::result<kapi::memory::mmio_window> + { + return m_segment.map_function(m_info.address, mode); + } + +} // namespace kernel::devices::pci diff --git a/kernel/kernel/devices/pci/function_access.hpp b/kernel/kernel/devices/pci/function_access.hpp new file mode 100644 index 00000000..5d7ea7dc --- /dev/null +++ b/kernel/kernel/devices/pci/function_access.hpp @@ -0,0 +1,51 @@ +#ifndef TEACHOS_KERNEL_DEVICES_PCI_FUNCTION_ACCESS_HPP +#define TEACHOS_KERNEL_DEVICES_PCI_FUNCTION_ACCESS_HPP + +// IWYU pragma: private, include <kernel/devices/pci.hpp> + +#include <kernel/bus/pci.hpp> +#include <kernel/bus/pci/function_info.hpp> +#include <kernel/devices/pci/ecam_segment.hpp> + +#include <kapi/memory.hpp> + +#include <kstd/result.hpp> + +namespace kernel::devices::pci +{ + + //! An accessor type to identify and access a PCI function. + struct function_access final : bus::pci::signature, bus::pci::configuration_access + { + //! Construct a new function access object for the given PCI function and ECAM segment. + //! + //! @param info The identifying information for the PCI function. + //! @param segment The ECAM segment containing the PCI function. + function_access(bus::pci::function_info info, ecam_segment segment) noexcept; + + function_access(function_access const &) = delete; + function_access(function_access &&) = delete; + + auto operator=(function_access const &) -> function_access & = delete; + auto operator=(function_access &&) -> function_access & = delete; + + //! Get the identifying information for the associated PCI function. + [[nodiscard]] auto info() const noexcept -> bus::pci::function_info const & override; + + //! Map the associated PCI function's configuration space into memory. + //! + //! @param mode The access mode (read-only or writable) for the mapping. + //! @return The mapped MMIO window on success, an error otherwise. + [[nodiscard]] auto map_config(kapi::memory::mmio_window::access mode) const + -> kstd::result<kapi::memory::mmio_window> override; + + private: + //! The identifying information for the PCI function. + bus::pci::function_info m_info; + //! The ECAM segment containing the PCI function. + ecam_segment m_segment; + }; + +} // namespace kernel::devices::pci + +#endif diff --git a/kernel/kernel/devices/pci/function_access.tests.cpp b/kernel/kernel/devices/pci/function_access.tests.cpp new file mode 100644 index 00000000..08621dc5 --- /dev/null +++ b/kernel/kernel/devices/pci/function_access.tests.cpp @@ -0,0 +1,160 @@ +#include <kernel/devices/pci/function_access.hpp> + +#include <kernel/bus/pci.hpp> +#include <kernel/devices/pci/ecam_segment.hpp> +#include <kernel/devices/pci/registers.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 <kstd/units.hpp> + +#include <catch2/catch_test_macros.hpp> + +#include <cstdint> + +// NOLINTBEGIN(readability-magic-numbers) + +namespace pci = kernel::bus::pci; + +using kernel::devices::pci::ecam_segment; +using kernel::devices::pci::function_access; +using kernel::devices::pci::function_offset; + +namespace +{ + + constexpr auto ecam_test_base = kapi::memory::physical_address{}; + constexpr auto group = std::uint16_t{3}; + constexpr auto first_bus = std::uint8_t{1}; + + //! A segment covering every bus of the test MMIO space except bus zero. + constexpr auto descriptor = kapi::devices::pcie_segment_descriptor{ + .base_address = ecam_test_base, + .group_number = group, + .first_bus = first_bus, + .bus_count = (kernel::tests::memory::mmio_size / kernel::devices::pci::mmio_window_per_bus) - 1, + }; + + auto info_at(pci::function_address address) -> pci::function_info + { + return pci::function_info{ + .address = address, + .vendor_id = 0x8086, + .device_id = 0x2922, + .revision = 2, + .programming_interface = 1, + .subclass_code = 6, + .class_code = 1, + }; + } + +} // namespace + +SCENARIO("A function access identifies its function", "[devices][pci][function_access]") +{ + GIVEN("A function access for a function") + { + auto const info = info_at(pci::function_address{group, 2, 5, 3}); + auto const access = function_access{info, ecam_segment{descriptor}}; + + THEN("it reports the identifying information it was constructed with") + { + auto const & reported = access.info(); + + REQUIRE(reported.address == info.address); + REQUIRE(reported.vendor_id == info.vendor_id); + REQUIRE(reported.device_id == info.device_id); + REQUIRE(reported.revision == info.revision); + REQUIRE(reported.programming_interface == info.programming_interface); + REQUIRE(reported.subclass_code == info.subclass_code); + REQUIRE(reported.class_code == info.class_code); + } + + THEN("it is usable as a PCI signature") + { + auto const & signature = static_cast<kernel::bus::pci::signature const &>(access); + + REQUIRE(signature.info().address == info.address); + REQUIRE(signature.info().vendor_id == info.vendor_id); + } + } +} + +SCENARIO("A function access maps its function's configuration space", "[devices][pci][function_access]") +{ + GIVEN("A function access for a function with data in its configuration space") + { + constexpr auto marker_offset = kstd::bytes{0x40}; + constexpr auto own_marker = std::uint16_t{0xa5a5}; + constexpr auto neighbour_marker = std::uint16_t{0x5a5a}; + + auto const address = pci::function_address{group, 2, 5, 3}; + auto const neighbour = pci::function_address{group, 2, 5, 4}; + + auto const bus_base = ecam_test_base + address.bus * kernel::devices::pci::mmio_window_per_bus; + auto bus_window = kapi::memory::mmio_window::map(bus_base, kernel::devices::pci::mmio_window_per_bus, + kapi::memory::mmio_window::access::writable); + REQUIRE(bus_window); + + bus_window->write<std::uint16_t>(function_offset(address.device, address.function) + marker_offset, own_marker); + bus_window->write<std::uint16_t>(function_offset(neighbour.device, neighbour.function) + marker_offset, + neighbour_marker); + + auto const access = function_access{info_at(pci::function_address{group, 2, 5, 3}), ecam_segment{descriptor}}; + + THEN("a read-only mapping shows that function's data and not its neighbour's") + { + auto const window = kstd::tests::require_ok(access.map_config(kapi::memory::mmio_window::access::read_only)); + + REQUIRE(window.read<std::uint16_t>(marker_offset) == own_marker); + } + + THEN("a writable mapping can change that function's data and nothing else") + { + auto window = kstd::tests::require_ok(access.map_config(kapi::memory::mmio_window::access::writable)); + + window.write<std::uint16_t>(marker_offset, 0x1234); + + REQUIRE(window.read<std::uint16_t>(marker_offset) == 0x1234); + REQUIRE(bus_window->read<std::uint16_t>(function_offset(address.device, address.function) + marker_offset) == + 0x1234); + REQUIRE(bus_window->read<std::uint16_t>(function_offset(neighbour.device, neighbour.function) + marker_offset) == + neighbour_marker); + } + } +} + +SCENARIO("A function access cannot map a function outside its segment", "[devices][pci][function_access]") +{ + GIVEN("A function access whose function lies in another segment group") + { + auto const access = function_access{info_at(pci::function_address{group + 1, 2, 0, 0}), ecam_segment{descriptor}}; + + THEN("mapping its configuration space fails") + { + auto const result = access.map_config(kapi::memory::mmio_window::access::read_only); + + REQUIRE_FALSE(result); + REQUIRE(result.error() == kstd::errc::invalid_argument); + } + } + + GIVEN("A function access whose function lies on a bus outside the segment") + { + auto const access = function_access{info_at(pci::function_address{group, 0, 0, 0}), ecam_segment{descriptor}}; + + THEN("mapping its configuration space fails") + { + auto const result = access.map_config(kapi::memory::mmio_window::access::read_only); + + REQUIRE_FALSE(result); + REQUIRE(result.error() == kstd::errc::invalid_argument); + } + } +} + +// NOLINTEND(readability-magic-numbers) 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) diff --git a/kernel/kernel/devices/pci/init.hpp b/kernel/kernel/devices/pci/init.hpp deleted file mode 100644 index 8a49acbc..00000000 --- a/kernel/kernel/devices/pci/init.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef TEACHOS_KERNEL_DEVICES_PCI_INIT_HPP -#define TEACHOS_KERNEL_DEVICES_PCI_INIT_HPP - -namespace kernel::devices::pci -{ - - //! Initialize all PCI/PCIe devices in the system. - //! - //! This function scans and initializes all PCI and PCI Express devices present in the system. - auto init() -> void; - -} // namespace kernel::devices::pci - -#endif diff --git a/kernel/kernel/devices/pci/registers.hpp b/kernel/kernel/devices/pci/registers.hpp index 1caa0baf..6283adfe 100644 --- a/kernel/kernel/devices/pci/registers.hpp +++ b/kernel/kernel/devices/pci/registers.hpp @@ -1,6 +1,8 @@ #ifndef TEACHOS_KERNEL_DEVICES_PCI_REGISTERS_HPP #define TEACHOS_KERNEL_DEVICES_PCI_REGISTERS_HPP +// IWYU pragma: private, include <kernel/devices/pci.hpp> + #include <kstd/units.hpp> #include <cstdint> diff --git a/kernel/kernel/devices/pci/scan.cpp b/kernel/kernel/devices/pci/scan.cpp index 0ff7d03e..ba252307 100644 --- a/kernel/kernel/devices/pci/scan.cpp +++ b/kernel/kernel/devices/pci/scan.cpp @@ -1,7 +1,7 @@ #include <kernel/devices/pci/scan.hpp> +#include <kernel/bus/pci.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> @@ -23,7 +23,7 @@ namespace kernel::devices::pci { 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> + std::uint8_t device, std::uint8_t number) -> std::optional<bus::pci::function_info> { auto offset = function_offset(device, number); @@ -40,7 +40,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); - auto info = function_info{ + auto info = bus::pci::function_info{ .address = {.segment_group = group, .bus = bus, .device = device, .function = number}, .vendor_id = vendor_id, .device_id = device_id, @@ -51,7 +51,7 @@ namespace kernel::devices::pci .raw_header_type = header_type }; - if (info.type() == header_type::pci_to_pci_bridge) + if (info.type() == bus::pci::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); @@ -62,7 +62,8 @@ namespace kernel::devices::pci } // namespace - auto scan_bus(ecam_segment const & segment, std::uint8_t bus_number) -> kstd::result<kstd::vector<function_info>> + auto scan_bus(ecam_segment const & segment, std::uint8_t bus_number) + -> kstd::result<kstd::vector<bus::pci::function_info>> { auto bus = segment.map_bus(bus_number); @@ -71,7 +72,7 @@ namespace kernel::devices::pci return kstd::failure(bus.error()); } - auto functions = kstd::vector<function_info>{}; + auto functions = kstd::vector<bus::pci::function_info>{}; for (std::uint8_t device = 0; device < devices_per_bus; ++device) { @@ -105,12 +106,12 @@ namespace kernel::devices::pci return kstd::success(std::move(functions)); } - auto scan_segment(ecam_segment const & segment) -> kstd::result<kstd::vector<function_info>> + auto scan_segment(ecam_segment const & segment) -> kstd::result<kstd::vector<bus::pci::function_info>> { auto root_bus = segment.first_bus(); if (!segment.contains(root_bus)) { - return kstd::vector<function_info>{}; + return kstd::vector<bus::pci::function_info>{}; } auto functions = scan_bus(segment, root_bus); @@ -125,7 +126,7 @@ namespace kernel::devices::pci for (auto function = 0uz; function < functions->size(); ++function) { auto maybe_bridge = functions->at(function); - if (maybe_bridge.type() != header_type::pci_to_pci_bridge) + if (maybe_bridge.type() != bus::pci::header_type::pci_to_pci_bridge) { continue; } diff --git a/kernel/kernel/devices/pci/scan.hpp b/kernel/kernel/devices/pci/scan.hpp index 799a1c09..cf96b2d1 100644 --- a/kernel/kernel/devices/pci/scan.hpp +++ b/kernel/kernel/devices/pci/scan.hpp @@ -1,8 +1,10 @@ #ifndef TEACHOS_KERNEL_DEVICES_PCI_SCAN_HPP #define TEACHOS_KERNEL_DEVICES_PCI_SCAN_HPP +// IWYU pragma: private, include <kernel/devices/pci.hpp> + +#include <kernel/bus/pci.hpp> #include <kernel/devices/pci/ecam_segment.hpp> -#include <kernel/devices/pci/function_info.hpp> #include <kstd/result.hpp> #include <kstd/vector.hpp> @@ -22,7 +24,7 @@ namespace kernel::devices::pci //! @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>>; + -> kstd::result<kstd::vector<bus::pci::function_info>>; //! Scan an entire ECAM segment for available PCI functions. //! @@ -37,7 +39,7 @@ namespace kernel::devices::pci //! @param segment The ECAM segment to scan. //! @return A vector of function_info objects representing the functions found in the segment on success, an error //! otherwise - [[nodiscard]] auto scan_segment(ecam_segment const & segment) -> kstd::result<kstd::vector<function_info>>; + [[nodiscard]] auto scan_segment(ecam_segment const & segment) -> kstd::result<kstd::vector<bus::pci::function_info>>; } // namespace kernel::devices::pci diff --git a/kernel/kernel/devices/pci/scan.tests.cpp b/kernel/kernel/devices/pci/scan.tests.cpp index 455a25c5..a686067e 100644 --- a/kernel/kernel/devices/pci/scan.tests.cpp +++ b/kernel/kernel/devices/pci/scan.tests.cpp @@ -1,8 +1,7 @@ #include <kernel/devices/pci/scan.hpp> +#include <kernel/bus/pci.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/cio.hpp> #include <kernel/test_support/devices/pci/ecam_fixture.hpp> #include <kernel/test_support/devices/pci/function_addresses.hpp> @@ -20,8 +19,9 @@ #include <cstdint> #include <string> +using kernel::bus::pci::function_address; +using kernel::bus::pci::header_type; using kernel::devices::pci::ecam_segment; -using kernel::devices::pci::function_address; using kernel::devices::pci::scan_segment; using kernel::tests::cio::require_log_message; using kernel::tests::devices::pci::addresses; @@ -148,7 +148,7 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Scanning PCI buses", 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.type() == header_type::endpoint); REQUIRE(!device.is_multifunction()); } } @@ -177,7 +177,7 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Scanning PCI buses", 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.type() == header_type::endpoint); REQUIRE(first.is_multifunction()); auto const & second = devices.at(1); @@ -189,7 +189,7 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Scanning PCI buses", 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); + REQUIRE(second.type() == header_type::endpoint); } } @@ -218,7 +218,7 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Scanning PCI buses", 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.type() == header_type::endpoint); REQUIRE(first.is_multifunction()); auto const & second = devices.at(1); @@ -230,7 +230,7 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Scanning PCI buses", 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); + REQUIRE(second.type() == header_type::endpoint); auto const & third = devices.at(2); @@ -241,7 +241,7 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Scanning PCI buses", 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); + REQUIRE(third.type() == header_type::endpoint); } } @@ -281,7 +281,7 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Scanning PCI buses", 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.type() == header_type::endpoint); REQUIRE(!first.is_multifunction()); auto const & last = devices.at(1); @@ -294,7 +294,7 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Scanning PCI buses", 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.type() == header_type::endpoint); REQUIRE(!last.is_multifunction()); } } @@ -407,7 +407,7 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Reading bridge bus n auto const & function = functions.at(0); - REQUIRE(function.type() == kernel::devices::pci::header_type::pci_to_pci_bridge); + REQUIRE(function.type() == header_type::pci_to_pci_bridge); REQUIRE(!function.is_multifunction()); REQUIRE(function.secondary_bus == distinct_secondary_bus); REQUIRE(function.subordinate_bus == distinct_subordinate_bus); @@ -427,7 +427,7 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Reading bridge bus n auto const & function = functions.at(0); - REQUIRE(function.type() == kernel::devices::pci::header_type::pci_to_pci_bridge); + REQUIRE(function.type() == header_type::pci_to_pci_bridge); REQUIRE(function.is_multifunction()); REQUIRE(function.secondary_bus == distinct_secondary_bus); REQUIRE(function.subordinate_bus == distinct_subordinate_bus); @@ -448,7 +448,7 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Reading bridge bus n auto const & function = functions.at(0); - REQUIRE(function.type() == kernel::devices::pci::header_type::endpoint); + REQUIRE(function.type() == header_type::endpoint); REQUIRE(function.secondary_bus == 0); REQUIRE(function.subordinate_bus == 0); } @@ -471,7 +471,7 @@ SCENARIO_METHOD(kernel::tests::devices::pci::ecam_fixture, "Reading bridge bus n auto const & function = functions.at(0); - REQUIRE(function.type() == kernel::devices::pci::header_type::cardbus_bridge); + REQUIRE(function.type() == header_type::cardbus_bridge); REQUIRE(function.secondary_bus == 0); REQUIRE(function.subordinate_bus == 0); } diff --git a/kernel/kernel/main.cpp b/kernel/kernel/main.cpp index ea55451f..d6e02a01 100644 --- a/kernel/kernel/main.cpp +++ b/kernel/kernel/main.cpp @@ -1,5 +1,5 @@ #include <kernel/devices/init.hpp> -#include <kernel/devices/pci/init.hpp> +#include <kernel/devices/pci.hpp> #include <kernel/devices/storage.hpp> #include <kernel/drivers/init.hpp> #include <kernel/memory.hpp> diff --git a/kernel/kernel/test_support/devices/pci/function_addresses.cpp b/kernel/kernel/test_support/devices/pci/function_addresses.cpp index bdc93b7f..d0dd9108 100644 --- a/kernel/kernel/test_support/devices/pci/function_addresses.cpp +++ b/kernel/kernel/test_support/devices/pci/function_addresses.cpp @@ -1,7 +1,6 @@ #include <kernel/test_support/devices/pci/function_addresses.hpp> -#include <kernel/devices/pci/function_address.hpp> -#include <kernel/devices/pci/function_info.hpp> +#include <kernel/bus/pci.hpp> #include <kstd/vector.hpp> @@ -13,17 +12,17 @@ namespace kernel::tests::devices::pci { - auto addresses(kstd::vector<kernel::devices::pci::function_info> const & functions) - -> std::vector<kernel::devices::pci::function_address> + auto addresses(kstd::vector<kernel::bus::pci::function_info> const & functions) + -> std::vector<kernel::bus::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> + -> std::vector<kernel::bus::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}; + return kernel::bus::pci::function_address{group, loc.bus, loc.device, loc.function}; })}; } diff --git a/kernel/kernel/test_support/devices/pci/function_addresses.hpp b/kernel/kernel/test_support/devices/pci/function_addresses.hpp index 64fff6e5..364fdd92 100644 --- a/kernel/kernel/test_support/devices/pci/function_addresses.hpp +++ b/kernel/kernel/test_support/devices/pci/function_addresses.hpp @@ -1,8 +1,7 @@ #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 <kernel/bus/pci.hpp> #include <kstd/vector.hpp> @@ -24,8 +23,8 @@ namespace kernel::tests::devices::pci //! //! @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>; + auto addresses(kstd::vector<kernel::bus::pci::function_info> const & functions) + -> std::vector<kernel::bus::pci::function_address>; //! Create a list of function addresses from a list of function locations. //! @@ -33,7 +32,7 @@ namespace kernel::tests::devices::pci //! @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>; + -> std::vector<kernel::bus::pci::function_address>; } // namespace kernel::tests::devices::pci diff --git a/libs/kstd/kstd/flat_map.hpp b/libs/kstd/kstd/flat_map.hpp index 9f3d76b1..4159bcbe 100644 --- a/libs/kstd/kstd/flat_map.hpp +++ b/libs/kstd/kstd/flat_map.hpp @@ -668,7 +668,7 @@ namespace kstd m_containers.values.emplace(m_containers.values.begin() + offset, std::forward<Args>(args)...); return { - iterator{m_containers, offset}, + iterator{m_containers, static_cast<size_type>(offset)}, true }; } diff --git a/scripts/qemu-wrapper.sh b/scripts/qemu-wrapper.sh index dd0200df..c85aeb07 100755 --- a/scripts/qemu-wrapper.sh +++ b/scripts/qemu-wrapper.sh @@ -15,6 +15,11 @@ ARGS=( "-m" "64M" "-machine" "q35" "-smp" "4,sockets=1,cores=4,threads=1" + "-object" "rng-random,filename=/dev/random,id=rng0" + "-device" "pcie-root-port,id=rp1,bus=pcie.0,chassis=1,slot=1" + "-device" "x3130-upstream,id=up1,bus=rp1" + "-device" "xio3130-downstream,id=down1,bus=up1,chassis=2,slot=0" + "-device" "virtio-rng-pci,bus=down1,rng=rng0" "-display" "curses" "-debugcon" "file:${WORKSPACE_DIR}/qemu-debugcon-${BUILD_TYPE}.log" "-cdrom" "${ISO_PATH}" |
