diff options
Diffstat (limited to 'kernel')
33 files changed, 1870 insertions, 93 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 7f418c61..3e2ed2da 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -43,14 +43,17 @@ target_sources("kernel_lib" PRIVATE # Device Subsystem "kernel/devices/block_device_utils.cpp" "kernel/devices/init.cpp" + "kernel/devices/pci.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/scan.cpp" "kernel/devices/pseudo.cpp" "kernel/devices/storage.cpp" # Bus Sumbsystem "kernel/bus/boot_modules.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 new file mode 100644 index 00000000..8697b925 --- /dev/null +++ b/kernel/kernel/bus/pci.hpp @@ -0,0 +1,12 @@ +#ifndef TEACHOS_KERNEL_BUS_PCI_HPP +#define 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/protocol.cpp b/kernel/kernel/bus/pci/protocol.cpp new file mode 100644 index 00000000..cfad574c --- /dev/null +++ b/kernel/kernel/bus/pci/protocol.cpp @@ -0,0 +1,60 @@ +#include <kernel/bus/pci/protocol.hpp> + +#include <kernel/bus/pci/claim.hpp> +#include <kernel/bus/pci/function_info.hpp> +#include <kernel/bus/pci/signature.hpp> + +#include <kapi/devices.hpp> + +#include <kstd/result.hpp> + +#include <algorithm> +#include <cstdint> +#include <ranges> + +namespace kernel::bus::pci +{ + + namespace + { + + //! Create a callable object that matches a PCI ID against the given PCI info + //! + //! @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(function_info const & info) + { + return [&info](auto const & id) { + return id.match(info).value_or(0); + }; + } + + } // namespace + + auto protocol::match(kapi::devices::device const & device, kapi::devices::driver const & driver) const + -> kstd::result<std::uint32_t> + { + auto signature = device.facet<struct signature>(); + auto claim = driver.facet<struct claim>(); + + if (!signature || !claim) + { + return kstd::failure(kapi::devices::driver_match_errc::no_match); + } + + auto supported_ids = claim->supported_ids(); + if (std::ranges::empty(supported_ids)) + { + return kstd::failure(kapi::devices::driver_match_errc::no_match); + } + + auto score = std::ranges::max(supported_ids | std::views::transform(matcher_for(signature->info()))); + if (score == 0) + { + return kstd::failure(kapi::devices::driver_match_errc::no_match); + } + + return kstd::success(score); + } + +} // 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/protocol.tests.cpp b/kernel/kernel/bus/pci/protocol.tests.cpp new file mode 100644 index 00000000..462f5dcd --- /dev/null +++ b/kernel/kernel/bus/pci/protocol.tests.cpp @@ -0,0 +1,404 @@ +#include <kernel/bus/pci/protocol.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> +#include <kapi/devices/driver_registry.hpp> + +#include <kstd/memory.hpp> +#include <kstd/result.hpp> +#include <kstd/string.hpp> +#include <kstd/test_support/result.hpp> + +#include <catch2/catch_test_macros.hpp> + +#include <cstdint> +#include <initializer_list> +#include <span> +#include <string_view> +#include <utility> +#include <vector> + +namespace pci = kernel::bus::pci; + +using kstd::tests::require_ok; +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; + } + + struct test_signature final : pci::signature + { + explicit test_signature(function_info info) + : m_info{info} + {} + + [[nodiscard]] auto info() const noexcept -> function_info const & override + { + return m_info; + } + + private: + function_info m_info; + }; + + //! A device that carries a PCI signature. + struct test_device final : kapi::devices::device + { + test_device(kstd::string name, function_info info) + : device{name} + , m_signature{info} + {} + + protected: + auto query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr<void> override + { + if (facet == pci::signature::id) + { + return kstd::make_observer<pci::signature>(&m_signature); + } + + return device::query_facet(facet); + } + + private: + test_signature m_signature; + }; + + //! A device without any PCI signature. + struct bare_device final : kapi::devices::device + { + using device::device; + }; + + //! 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 + { + explicit test_driver(std::vector<pci::id> ids, bool probe_succeeds = true) + : m_ids{std::move(ids)} + , m_probe_succeeds{probe_succeeds} + {} + + [[nodiscard]] auto probe(kapi::devices::device &) -> kstd::result<void> override + { + ++probe_calls; + + if (!m_probe_succeeds) + { + return kstd::failure(kapi::devices::driver_match_errc::no_match); + } + + return kstd::success(); + } + + auto unbind(kapi::devices::device &) -> void override {} + + [[nodiscard]] auto name() const noexcept -> std::string_view override + { + return "PCI 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::vector<pci::id> m_ids; + bool m_probe_succeeds; + }; + + //! A driver without any PCI claim. + struct bare_driver final : kapi::devices::driver + { + [[nodiscard]] auto probe(kapi::devices::device &) -> kstd::result<void> override + { + return kstd::success(); + } + + auto unbind(kapi::devices::device &) -> void override {} + + [[nodiscard]] auto name() const noexcept -> std::string_view override + { + return "Bare Test Driver"; + } + }; + + 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} + {} + }; + + auto is_no_match(kstd::result<std::uint32_t> const & result) -> bool + { + 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> + { + return ids; + } + + auto exact_id() -> pci::id + { + return {.vendor_id = vendor_intel, .device_id = device_ich9_ahci}; + } + + auto other_exact_id() -> pci::id + { + return {.vendor_id = vendor_red_hat, .device_id = device_virtio_blk}; + } + + 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("The PCI protocol rejects pairings that cannot match", "[bus][pci]") +{ + GIVEN("an AHCI controller device") + { + auto const device = test_device{"pci_protocol_ahci_device", ahci_controller()}; + + WHEN("the driver has no PCI claim") + { + auto const driver = bare_driver{}; + + THEN("there is no match") + { + REQUIRE(is_no_match(protocol_instance.match(device, driver))); + } + } + + WHEN("the driver claims an empty table") + { + auto const driver = test_driver{table({})}; + + THEN("there is no match") + { + REQUIRE(is_no_match(protocol_instance.match(device, driver))); + } + } + + WHEN("none of the driver's entries match") + { + auto const driver = test_driver{table({other_exact_id(), pci::id{.class_code = 0x02}})}; + + THEN("there is no match") + { + REQUIRE(is_no_match(protocol_instance.match(device, driver))); + } + } + } + + GIVEN("a device without a PCI signature") + { + auto const device = bare_device{"pci_protocol_bare_device"}; + + WHEN("a driver that would match an AHCI controller is tested against it") + { + auto const driver = test_driver{table({exact_id(), class_id()})}; + + THEN("there is no match") + { + REQUIRE(is_no_match(protocol_instance.match(device, driver))); + } + } + } +} + +SCENARIO("The PCI protocol scores a driver by its best entry", "[bus][pci]") +{ + GIVEN("an AHCI controller device") + { + auto const device = test_device{"pci_protocol_best_entry_device", ahci_controller()}; + + WHEN("a driver claims only a class") + { + auto const driver = test_driver{table({class_id()})}; + + THEN("the score is that of the class entry") + { + REQUIRE(require_ok(protocol_instance.match(device, driver)) == 1u); + } + } + + WHEN("a driver claims the exact device") + { + auto const driver = test_driver{table({exact_id()})}; + + THEN("the score is the exact ID score") + { + REQUIRE(require_ok(protocol_instance.match(device, driver)) == pci::id::exact_id_score); + } + } + + WHEN("a driver claims several entries that all match") + { + auto const driver = test_driver{table({class_id(), exact_id(), subclass_id()})}; + + THEN("the score is the best single entry, not the sum") + { + REQUIRE(require_ok(protocol_instance.match(device, driver)) == pci::id::exact_id_score); + } + } + + WHEN("a driver lists a non-matching entry before a matching one") + { + auto const driver = test_driver{table({other_exact_id(), subclass_id()})}; + + THEN("the matching entry is still found") + { + REQUIRE(require_ok(protocol_instance.match(device, driver)) == 2u); + } + } + + WHEN("the same entries are listed in the opposite order") + { + auto const forward = test_driver{table({class_id(), subclass_id(), full_class_id()})}; + auto const backward = test_driver{table({full_class_id(), subclass_id(), class_id()})}; + + THEN("the score does not change") + { + REQUIRE(require_ok(protocol_instance.match(device, forward)) == + require_ok(protocol_instance.match(device, backward))); + } + } + } +} + +SCENARIO("The PCI protocol lets a more specific driver win regardless of registration order", "[bus][pci]") +{ + GIVEN("a class driver that is registered before an exact-ID driver") + { + auto bus = kstd::make_shared<test_bus>("pci_selection_bus"); + auto class_driver = kstd::make_shared<test_driver>(std::vector{class_id()}); + auto exact_driver = kstd::make_shared<test_driver>(std::vector{exact_id()}); + + kapi::devices::driver_registry::get().add(class_driver); + kapi::devices::driver_registry::get().add(exact_driver); + + WHEN("an AHCI controller arrives") + { + auto device = kstd::make_shared<test_device>("pci_selection_device", ahci_controller()); + bus->add_child(device); + + THEN("the exact-ID driver is bound and the class driver is never probed") + { + REQUIRE(device->state() == kapi::devices::state::bound); + REQUIRE(device->bound_driver() == exact_driver); + REQUIRE(exact_driver->probe_calls == 1); + REQUIRE(class_driver->probe_calls == 0); + } + } + } +} + +SCENARIO("A PCI device falls back to the next best driver when the best one fails to probe", "[bus][pci]") +{ + GIVEN("an exact-ID driver that fails to probe and a class driver that succeeds") + { + auto bus = kstd::make_shared<test_bus>("pci_fallback_bus"); + auto exact_driver = kstd::make_shared<test_driver>(std::vector{exact_id()}, false); + auto class_driver = kstd::make_shared<test_driver>(std::vector{class_id()}); + + kapi::devices::driver_registry::get().add(exact_driver); + kapi::devices::driver_registry::get().add(class_driver); + + WHEN("an AHCI controller arrives") + { + auto device = kstd::make_shared<test_device>("pci_fallback_device", ahci_controller()); + bus->add_child(device); + + THEN("the better driver is tried first and the class driver ends up bound") + { + REQUIRE(exact_driver->probe_calls == 1); + REQUIRE(class_driver->probe_calls == 1); + REQUIRE(device->state() == kapi::devices::state::bound); + REQUIRE(device->bound_driver() == class_driver); + } + } + } +} + +SCENARIO("A PCI device nobody claims stays present", "[bus][pci]") +{ + GIVEN("a driver that only claims a different device") + { + auto bus = kstd::make_shared<test_bus>("pci_unclaimed_bus"); + auto driver = kstd::make_shared<test_driver>(std::vector{other_exact_id()}); + + kapi::devices::driver_registry::get().add(driver); + + WHEN("an AHCI controller arrives") + { + auto device = kstd::make_shared<test_device>("pci_unclaimed_device", ahci_controller()); + bus->add_child(device); + + THEN("the device is neither bound nor failed, and the driver is never probed") + { + REQUIRE(device->state() == kapi::devices::state::present); + REQUIRE(device->bound_driver() == nullptr); + REQUIRE(driver->probe_calls == 0); + } + } + } +} 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..ab9c889a 100644 --- a/kernel/kernel/devices/pci/init.cpp +++ b/kernel/kernel/devices/pci.cpp @@ -1,7 +1,6 @@ -#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> diff --git a/kernel/kernel/devices/pci.hpp b/kernel/kernel/devices/pci.hpp new file mode 100644 index 00000000..32e06f6a --- /dev/null +++ b/kernel/kernel/devices/pci.hpp @@ -0,0 +1,20 @@ +#ifndef TEACHOS_KERNEL_DEVICES_PCI_HPP +#define TEACHOS_KERNEL_DEVICES_PCI_HPP + +#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/registers.hpp> // IWYU pragma: export +#include <kernel/devices/pci/scan.hpp> // IWYU pragma: export + +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/ecam_segment.cpp b/kernel/kernel/devices/pci/ecam_segment.cpp index 5464ec4b..ce46bfe4 100644 --- a/kernel/kernel/devices/pci/ecam_segment.cpp +++ b/kernel/kernel/devices/pci/ecam_segment.cpp @@ -1,5 +1,6 @@ #include <kernel/devices/pci/ecam_segment.hpp> +#include <kernel/bus/pci.hpp> #include <kernel/devices/pci/registers.hpp> #include <kapi/devices.hpp> @@ -56,4 +57,28 @@ namespace kernel::devices::pci kapi::memory::mmio_window::access::read_only); } + 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) + { + return kstd::failure(make_error_code(kstd::errc::invalid_argument)); + } + + if (!contains(address.bus)) + { + return kstd::failure(make_error_code(kstd::errc::invalid_argument)); + } + + if (address.device >= devices_per_bus || address.function >= functions_per_device) + { + return kstd::failure(make_error_code(kstd::errc::invalid_argument)); + } + + auto bus_hw_address = m_base + address.bus * mmio_window_per_bus; + auto function_hw_address = bus_hw_address + function_offset(address.device, address.function); + + return kapi::memory::mmio_window::map(function_hw_address, config_space_per_function, mode); + } + } // namespace kernel::devices::pci diff --git a/kernel/kernel/devices/pci/ecam_segment.hpp b/kernel/kernel/devices/pci/ecam_segment.hpp index 8f9e18bd..1ab3595e 100644 --- a/kernel/kernel/devices/pci/ecam_segment.hpp +++ b/kernel/kernel/devices/pci/ecam_segment.hpp @@ -1,6 +1,9 @@ #ifndef TEACHOS_KERNEL_DEVICES_PCI_ECAM_SEGMENT_HPP #define TEACHOS_KERNEL_DEVICES_PCI_ECAM_SEGMENT_HPP +// IWYU pragma: private, include <kernel/devices/pci.hpp> + +#include <kernel/bus/pci.hpp> #include <kernel/devices/pci/registers.hpp> #include <kapi/devices.hpp> @@ -45,6 +48,16 @@ namespace kernel::devices::pci //! @return An MMIO window for the bus if successful, an error otherwise. [[nodiscard]] auto map_bus(std::uint8_t bus) const -> kstd::result<kapi::memory::mmio_window>; + //! Create an MMIO mapping for the function at the given address. + //! + //! @warning This function will panic if the MMIO subsystem has not been initialized. + //! + //! @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(bus::pci::function_address address, kapi::memory::mmio_window::access mode) const + -> kstd::result<kapi::memory::mmio_window>; + private: //! The hardware base address of the segment. kapi::memory::physical_address m_base; diff --git a/kernel/kernel/devices/pci/ecam_segment.tests.cpp b/kernel/kernel/devices/pci/ecam_segment.tests.cpp index 513ce6d1..0d575ac3 100644 --- a/kernel/kernel/devices/pci/ecam_segment.tests.cpp +++ b/kernel/kernel/devices/pci/ecam_segment.tests.cpp @@ -1,5 +1,6 @@ #include <kernel/devices/pci/ecam_segment.hpp> +#include <kernel/bus/pci.hpp> #include <kernel/devices/pci/registers.hpp> #include <kernel/test_support/memory.hpp> @@ -7,6 +8,7 @@ #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> @@ -27,6 +29,8 @@ namespace }; } // namespace +// NOLINTBEGIN(readability-magic-numbers) + SCENARIO("ECAM segment basic operations", "[devices][pci][ecam]") { GIVEN("A valid segment group descriptor") @@ -315,3 +319,148 @@ SCENARIO("ECAM segment mapping", "[devices][pci][ecam]") } } } + +SCENARIO("ECAM segment function mapping", "[devices][pci][ecam]") +{ + using kernel::bus::pci::function_address; + using kernel::devices::pci::function_offset; + + GIVEN("A valid segment with a non-zero group and first bus") + { + constexpr auto group = std::uint16_t{3}; + + constexpr auto descriptor = kapi::devices::pcie_segment_descriptor{ + .base_address = ecam_test_base, + .group_number = group, + .first_bus = 1, + .bus_count = (kernel::tests::memory::mmio_size / kernel::devices::pci::mmio_window_per_bus) - 1, + }; + + STATIC_REQUIRE(descriptor.bus_count > 0); + + auto const segment = kernel::devices::pci::ecam_segment{descriptor}; + + WHEN("a function's configuration space contains data") + { + auto const address = GENERATE(function_address{group, 1, 0, 0}, // first function of the first bus + function_address{group, 2, 5, 3}, // somewhere in the middle + function_address{group, 3, 31, 7} // last function of the last bus + ); + + 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 neighbour_function = static_cast<std::uint8_t>((address.function + 1) % 8); + auto const neighbour = function_address{group, address.bus, address.device, neighbour_function}; + + 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); + + THEN("a read-only mapping sees the function's own data, not its neighbour's") + { + auto const own = + kstd::tests::require_ok(segment.map_function(address, kapi::memory::mmio_window::access::read_only)); + REQUIRE(own.read<std::uint16_t>(marker_offset) == own_marker); + + auto const other = + kstd::tests::require_ok(segment.map_function(neighbour, kapi::memory::mmio_window::access::read_only)); + REQUIRE(other.read<std::uint16_t>(marker_offset) == neighbour_marker); + } + + THEN("a writable mapping reads and writes the same data") + { + auto window = + kstd::tests::require_ok(segment.map_function(address, kapi::memory::mmio_window::access::writable)); + REQUIRE(window.read<std::uint16_t>(marker_offset) == own_marker); + + 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); + } + } + + THEN("a harmless register can be written and read back") + { + // The Interrupt Line register at offset 0x3c is defined as scratch space for firmware and the OS. + constexpr auto interrupt_line = kstd::bytes{0x3c}; + + auto window = kstd::tests::require_ok( + segment.map_function(function_address{group, 1, 2, 3}, kapi::memory::mmio_window::access::writable)); + + window.write<std::uint8_t>(interrupt_line, 0x0b); + + REQUIRE(window.read<std::uint8_t>(interrupt_line) == 0x0b); + } + + THEN("functions with an invalid address cannot be mapped") + { + auto const address = + GENERATE(function_address{static_cast<std::uint16_t>(group + 1), 1, 0, 0}, // wrong segment group + function_address{group, 0, 0, 0}, // bus before the segment + function_address{group, 4, 0, 0}, // bus after the segment + function_address{group, 1, 32, 0}, // device number out of range + function_address{group, 1, 0, 8} // function number out of range + ); + + auto const result = segment.map_function(address, kapi::memory::mmio_window::access::read_only); + + REQUIRE_FALSE(result); + REQUIRE(result.error() == kstd::errc::invalid_argument); + } + } + + GIVEN("A valid segment whose buses fall outside the device space") + { + auto const descriptor = kapi::devices::pcie_segment_descriptor{ + .base_address = ecam_test_base + kernel::tests::memory::mmio_size, + .group_number = 0, + .first_bus = 0, + .bus_count = 4, + }; + + auto const segment = kernel::devices::pci::ecam_segment{descriptor}; + + THEN("no function can be mapped") + { + auto const result = + segment.map_function(function_address{0, 0, 0, 0}, kapi::memory::mmio_window::access::read_only); + + REQUIRE_FALSE(result); + REQUIRE(result.error() == kstd::errc::not_enough_memory); + } + } + + GIVEN("An empty segment") + { + auto const descriptor = kapi::devices::pcie_segment_descriptor{ + .base_address = ecam_test_base, + .group_number = 0, + .first_bus = 0, + .bus_count = 0, + }; + + auto const segment = kernel::devices::pci::ecam_segment{descriptor}; + + THEN("no function can be mapped") + { + auto const result = + segment.map_function(function_address{0, 0, 0, 0}, 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/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/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 |
