aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/CMakeLists.txt4
-rw-r--r--kernel/kernel/bus/pci.hpp186
-rw-r--r--kernel/kernel/bus/pci/claim.hpp34
-rw-r--r--kernel/kernel/bus/pci/configuration_access.hpp32
-rw-r--r--kernel/kernel/bus/pci/function_address.hpp (renamed from kernel/kernel/devices/pci/function_address.hpp)14
-rw-r--r--kernel/kernel/bus/pci/function_address.tests.cpp (renamed from kernel/kernel/devices/pci/function_address.tests.cpp)20
-rw-r--r--kernel/kernel/bus/pci/function_info.hpp (renamed from kernel/kernel/devices/pci/function_info.hpp)12
-rw-r--r--kernel/kernel/bus/pci/function_info.tests.cpp (renamed from kernel/kernel/devices/pci/function_info.tests.cpp)37
-rw-r--r--kernel/kernel/bus/pci/id.hpp108
-rw-r--r--kernel/kernel/bus/pci/id.tests.cpp271
-rw-r--r--kernel/kernel/bus/pci/protocol.cpp (renamed from kernel/kernel/bus/pci.cpp)18
-rw-r--r--kernel/kernel/bus/pci/protocol.hpp37
-rw-r--r--kernel/kernel/bus/pci/protocol.tests.cpp (renamed from kernel/kernel/bus/pci.tests.cpp)270
-rw-r--r--kernel/kernel/bus/pci/signature.hpp30
-rw-r--r--kernel/kernel/devices/pci.cpp (renamed from kernel/kernel/devices/pci/init.cpp)3
-rw-r--r--kernel/kernel/devices/pci.hpp20
-rw-r--r--kernel/kernel/devices/pci/ecam_segment.cpp4
-rw-r--r--kernel/kernel/devices/pci/ecam_segment.hpp6
-rw-r--r--kernel/kernel/devices/pci/ecam_segment.tests.cpp4
-rw-r--r--kernel/kernel/devices/pci/endpoint.cpp11
-rw-r--r--kernel/kernel/devices/pci/endpoint.hpp6
-rw-r--r--kernel/kernel/devices/pci/endpoint.tests.cpp48
-rw-r--r--kernel/kernel/devices/pci/function_access.cpp6
-rw-r--r--kernel/kernel/devices/pci/function_access.hpp12
-rw-r--r--kernel/kernel/devices/pci/function_access.tests.cpp31
-rw-r--r--kernel/kernel/devices/pci/init.hpp14
-rw-r--r--kernel/kernel/devices/pci/registers.hpp2
-rw-r--r--kernel/kernel/devices/pci/scan.cpp19
-rw-r--r--kernel/kernel/devices/pci/scan.hpp8
-rw-r--r--kernel/kernel/devices/pci/scan.tests.cpp30
-rw-r--r--kernel/kernel/main.cpp2
-rw-r--r--kernel/kernel/test_support/devices/pci/function_addresses.cpp11
-rw-r--r--kernel/kernel/test_support/devices/pci/function_addresses.hpp9
33 files changed, 731 insertions, 588 deletions
diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt
index 92c731fe..3e2ed2da 100644
--- a/kernel/CMakeLists.txt
+++ b/kernel/CMakeLists.txt
@@ -43,17 +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/endpoint.cpp"
"kernel/devices/pci/function_access.cpp"
- "kernel/devices/pci/init.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 64697c7a..8697b925 100644
--- a/kernel/kernel/bus/pci.hpp
+++ b/kernel/kernel/bus/pci.hpp
@@ -1,180 +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 <kapi/memory.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;
- };
-
- struct pci_config_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 ~pci_config_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;
- };
-
- //! 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..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 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
index 8ca536f2..29ce28d1 100644
--- a/kernel/kernel/devices/pci/endpoint.cpp
+++ b/kernel/kernel/devices/pci/endpoint.cpp
@@ -2,7 +2,6 @@
#include <kernel/bus/pci.hpp>
#include <kernel/devices/pci/ecam_segment.hpp>
-#include <kernel/devices/pci/function_info.hpp>
#include <kapi/capabilities/facet_id.hpp>
#include <kapi/devices.hpp>
@@ -15,20 +14,20 @@
namespace kernel::devices::pci
{
- endpoint::endpoint(function_info info, ecam_segment segment)
+ 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)
+ if (facet == bus::pci::signature::id)
{
- return kstd::make_observer<bus::pci_signature>(&m_access);
+ return kstd::make_observer<bus::pci::signature>(&m_access);
}
- else if (facet == bus::pci_config_access::id)
+ else if (facet == bus::pci::configuration_access::id)
{
- return kstd::make_observer<bus::pci_config_access>(&m_access);
+ return kstd::make_observer<bus::pci::configuration_access>(&m_access);
}
return device::query_facet(facet);
diff --git a/kernel/kernel/devices/pci/endpoint.hpp b/kernel/kernel/devices/pci/endpoint.hpp
index 14d8f27b..690583f0 100644
--- a/kernel/kernel/devices/pci/endpoint.hpp
+++ b/kernel/kernel/devices/pci/endpoint.hpp
@@ -1,9 +1,11 @@
#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 <kernel/devices/pci/function_info.hpp>
#include <kapi/capabilities/facet_id.hpp>
#include <kapi/devices.hpp>
@@ -20,7 +22,7 @@ namespace kernel::devices::pci
//!
//! @param info The PCI function information for this endpoint.
//! @param segment The ECAM segment that contains this endpoint.
- endpoint(function_info info, ecam_segment segment);
+ endpoint(bus::pci::function_info info, ecam_segment segment);
protected:
//! Query a facet provided by this endpoint.
diff --git a/kernel/kernel/devices/pci/endpoint.tests.cpp b/kernel/kernel/devices/pci/endpoint.tests.cpp
index beb89e9a..752b4a14 100644
--- a/kernel/kernel/devices/pci/endpoint.tests.cpp
+++ b/kernel/kernel/devices/pci/endpoint.tests.cpp
@@ -2,8 +2,6 @@
#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/devices/pci/registers.hpp>
#include <kernel/test_support/memory.hpp>
@@ -30,14 +28,10 @@
namespace
{
- using kernel::bus::pci_claim;
- using kernel::bus::pci_config_access;
- using kernel::bus::pci_id;
- using kernel::bus::pci_signature;
+ namespace pci = kernel::bus::pci;
+
using kernel::devices::pci::ecam_segment;
using kernel::devices::pci::endpoint;
- using kernel::devices::pci::function_address;
- using kernel::devices::pci::function_info;
using kernel::devices::pci::function_offset;
constexpr auto ecam_test_base = kapi::memory::physical_address{};
@@ -52,9 +46,9 @@ namespace
.bus_count = kernel::tests::memory::mmio_size / kernel::devices::pci::mmio_window_per_bus,
};
- auto ahci_controller_at(function_address address) -> function_info
+ auto ahci_controller_at(pci::function_address address) -> pci::function_info
{
- return function_info{
+ return pci::function_info{
.address = address,
.vendor_id = 0x8086,
.device_id = 0x2922,
@@ -72,11 +66,11 @@ namespace
};
//! 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
+ struct config_driver final : kapi::devices::driver, pci::claim
{
config_driver()
: m_ids{
- pci_id{.vendor_id = 0x8086, .device_id = 0x2922}
+ pci::id{.vendor_id = 0x8086, .device_id = 0x2922}
}
{}
@@ -84,7 +78,7 @@ namespace
{
++probe_calls;
- auto access = device.facet<pci_config_access>();
+ auto access = device.facet<pci::configuration_access>();
if (!access)
{
return kstd::failure(kapi::devices::driver_match_errc::no_match);
@@ -113,7 +107,7 @@ namespace
return "PCI Config 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;
}
@@ -124,19 +118,19 @@ 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::array<pci_id, 1> m_ids{};
+ std::array<pci::id, 1> m_ids{};
};
- struct test_protocol final : kernel::bus::pci_protocol
+ struct test_protocol final : pci::protocol
{
auto enumerate(kapi::devices::bus &) -> void override {}
} constinit protocol_instance;
@@ -153,8 +147,8 @@ SCENARIO("An endpoint is named after its function's address", "[devices][pci][en
{
GIVEN("an endpoint in a non-zero segment group")
{
- auto const device =
- kstd::make_shared<endpoint>(ahci_controller_at(function_address{group, 2, 5, 3}), ecam_segment{descriptor});
+ 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")
{
@@ -172,12 +166,12 @@ SCENARIO("An endpoint offers PCI facets and no more", "[devices][pci][endpoint]"
{
GIVEN("an endpoint")
{
- auto const info = ahci_controller_at(function_address{group, 2, 5, 3});
+ 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>();
+ auto const signature = device->facet<pci::signature>();
REQUIRE(signature);
REQUIRE(signature->info().address == info.address);
@@ -187,13 +181,13 @@ SCENARIO("An endpoint offers PCI facets and no more", "[devices][pci][endpoint]"
THEN("it provides configuration space access")
{
- REQUIRE(device->has_facet<pci_config_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>());
+ REQUIRE_FALSE(device->has_facet<pci::claim>());
}
}
}
@@ -205,7 +199,7 @@ SCENARIO("An endpoint's configuration space is reachable through its facet", "[d
constexpr auto marker_offset = kstd::bytes{0x40};
constexpr auto marker = std::uint16_t{0xa5a5};
- auto const address = function_address{group, 2, 5, 3};
+ 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,
@@ -217,7 +211,7 @@ SCENARIO("An endpoint's configuration space is reachable through its facet", "[d
THEN("a mapping obtained through the facet shows that function's data")
{
- auto const access = device->facet<pci_config_access>();
+ 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));
@@ -231,7 +225,7 @@ SCENARIO("A driver keeps an endpoint's configuration window in its driver state"
{
GIVEN("a bound driver that wrote the Interrupt Line register during probe")
{
- auto const address = function_address{group, 1, 2, 3};
+ 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});
diff --git a/kernel/kernel/devices/pci/function_access.cpp b/kernel/kernel/devices/pci/function_access.cpp
index f1ac4d00..eb8c0356 100644
--- a/kernel/kernel/devices/pci/function_access.cpp
+++ b/kernel/kernel/devices/pci/function_access.cpp
@@ -1,7 +1,7 @@
#include <kernel/devices/pci/function_access.hpp>
+#include <kernel/bus/pci/function_info.hpp>
#include <kernel/devices/pci/ecam_segment.hpp>
-#include <kernel/devices/pci/function_info.hpp>
#include <kapi/memory.hpp>
@@ -12,12 +12,12 @@
namespace kernel::devices::pci
{
- function_access::function_access(function_info info, ecam_segment segment) noexcept
+ 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 -> function_info const &
+ auto function_access::info() const noexcept -> bus::pci::function_info const &
{
return m_info;
}
diff --git a/kernel/kernel/devices/pci/function_access.hpp b/kernel/kernel/devices/pci/function_access.hpp
index 685102b3..5d7ea7dc 100644
--- a/kernel/kernel/devices/pci/function_access.hpp
+++ b/kernel/kernel/devices/pci/function_access.hpp
@@ -1,9 +1,11 @@
#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 <kernel/devices/pci/function_info.hpp>
#include <kapi/memory.hpp>
@@ -13,13 +15,13 @@ namespace kernel::devices::pci
{
//! An accessor type to identify and access a PCI function.
- struct function_access final : bus::pci_signature, bus::pci_config_access
+ 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(function_info info, ecam_segment segment) noexcept;
+ function_access(bus::pci::function_info info, ecam_segment segment) noexcept;
function_access(function_access const &) = delete;
function_access(function_access &&) = delete;
@@ -28,7 +30,7 @@ namespace kernel::devices::pci
auto operator=(function_access &&) -> function_access & = delete;
//! Get the identifying information for the associated PCI function.
- [[nodiscard]] auto info() const noexcept -> function_info const & override;
+ [[nodiscard]] auto info() const noexcept -> bus::pci::function_info const & override;
//! Map the associated PCI function's configuration space into memory.
//!
@@ -39,7 +41,7 @@ namespace kernel::devices::pci
private:
//! The identifying information for the PCI function.
- function_info m_info;
+ bus::pci::function_info m_info;
//! The ECAM segment containing the PCI function.
ecam_segment m_segment;
};
diff --git a/kernel/kernel/devices/pci/function_access.tests.cpp b/kernel/kernel/devices/pci/function_access.tests.cpp
index c7622e31..08621dc5 100644
--- a/kernel/kernel/devices/pci/function_access.tests.cpp
+++ b/kernel/kernel/devices/pci/function_access.tests.cpp
@@ -2,8 +2,6 @@
#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/devices/pci/registers.hpp>
#include <kernel/test_support/memory.hpp>
@@ -20,13 +18,14 @@
// 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
{
- using kernel::devices::pci::ecam_segment;
- using kernel::devices::pci::function_access;
- using kernel::devices::pci::function_address;
- using kernel::devices::pci::function_info;
- using kernel::devices::pci::function_offset;
constexpr auto ecam_test_base = kapi::memory::physical_address{};
constexpr auto group = std::uint16_t{3};
@@ -40,9 +39,9 @@ namespace
.bus_count = (kernel::tests::memory::mmio_size / kernel::devices::pci::mmio_window_per_bus) - 1,
};
- auto info_at(function_address address) -> function_info
+ auto info_at(pci::function_address address) -> pci::function_info
{
- return function_info{
+ return pci::function_info{
.address = address,
.vendor_id = 0x8086,
.device_id = 0x2922,
@@ -59,7 +58,7 @@ SCENARIO("A function access identifies its function", "[devices][pci][function_a
{
GIVEN("A function access for a function")
{
- auto const info = info_at(function_address{group, 2, 5, 3});
+ 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")
@@ -77,7 +76,7 @@ SCENARIO("A function access identifies its function", "[devices][pci][function_a
THEN("it is usable as a PCI signature")
{
- auto const & signature = static_cast<kernel::bus::pci_signature const &>(access);
+ 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);
@@ -93,8 +92,8 @@ SCENARIO("A function access maps its function's configuration space", "[devices]
constexpr auto own_marker = std::uint16_t{0xa5a5};
constexpr auto neighbour_marker = std::uint16_t{0x5a5a};
- auto const address = function_address{group, 2, 5, 3};
- auto const neighbour = function_address{group, 2, 5, 4};
+ 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,
@@ -105,7 +104,7 @@ SCENARIO("A function access maps its function's configuration space", "[devices]
bus_window->write<std::uint16_t>(function_offset(neighbour.device, neighbour.function) + marker_offset,
neighbour_marker);
- auto const access = function_access{info_at(address), ecam_segment{descriptor}};
+ 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")
{
@@ -133,7 +132,7 @@ SCENARIO("A function access cannot map a function outside its segment", "[device
{
GIVEN("A function access whose function lies in another segment group")
{
- auto const access = function_access{info_at(function_address{group + 1, 2, 0, 0}), ecam_segment{descriptor}};
+ auto const access = function_access{info_at(pci::function_address{group + 1, 2, 0, 0}), ecam_segment{descriptor}};
THEN("mapping its configuration space fails")
{
@@ -146,7 +145,7 @@ SCENARIO("A function access cannot map a function outside its segment", "[device
GIVEN("A function access whose function lies on a bus outside the segment")
{
- auto const access = function_access{info_at(function_address{group, 0, 0, 0}), ecam_segment{descriptor}};
+ auto const access = function_access{info_at(pci::function_address{group, 0, 0, 0}), ecam_segment{descriptor}};
THEN("mapping its configuration space fails")
{
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