From 0dfdb6c4f4aa428d6e1951958edafd82b87a0af8 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 16 Nov 2026 17:32:59 +0100 Subject: kernel/devices: introduce PCI function_address A PCI function is identified system-wide by its segment group, bus, device and function number. This identifier is represented by the new function_address type. It encapsulates the identifying 4-tuple (segment group, bus, device, function). This allows the kernel to reference a specific PCI function unambiguously within the system. The type is comparable and ordered by that tuple and formats as 'ssss:bb:dd.f'. The ordering is lexicographical based on the tuple (segment group, bus, device, function). Unit tests for the API surface of function_address are included. --- kernel/kernel/devices/pci/function_address.hpp | 61 +++++++++++++++ .../kernel/devices/pci/function_address.tests.cpp | 89 ++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 kernel/kernel/devices/pci/function_address.hpp create mode 100644 kernel/kernel/devices/pci/function_address.tests.cpp (limited to 'kernel') diff --git a/kernel/kernel/devices/pci/function_address.hpp b/kernel/kernel/devices/pci/function_address.hpp new file mode 100644 index 00000000..c4b0888b --- /dev/null +++ b/kernel/kernel/devices/pci/function_address.hpp @@ -0,0 +1,61 @@ +#ifndef TEACHOS_KERNEL_DEVICES_PCI_FUNCTION_ADDRESS_HPP +#define TEACHOS_KERNEL_DEVICES_PCI_FUNCTION_ADDRESS_HPP + +#include + +#include +#include +#include +#include +#include + +namespace kernel::devices::pci +{ + + //! The address of a PCI function within the system. + struct function_address + { + //! Compare two function addresses for ordering. + constexpr auto friend operator<=>(function_address const & lhs, function_address const & rhs) noexcept + -> std::strong_ordering = default; + + //! The PCI segment group this function belongs to. + std::uint16_t segment_group{}; + //! The PCI bus this function belongs to. + std::uint8_t bus{}; + //! The PCI device this function belongs to. + //! + //! The valid range for the PCI device number is [0, 31]. + std::uint8_t device{}; + //! The PCI function number within the device. + //! + //! The valid range for the PCI function number is [0, 7]. + std::uint8_t function{}; + }; + +} // namespace kernel::devices::pci + +template<> +struct kstd::formatter +{ + constexpr auto parse(kstd::format_parse_context & context) -> kstd::format_parse_context::iterator + { + auto it = context.begin(); + if (it != context.end() && *it != '}') + { + bits::format::error("Invalid specifier for function_address."); + } + return it; + } + + auto format(kernel::devices::pci::function_address const & address, kstd::format_context & context) const -> void + { + constexpr auto output_length = 32; + auto buffer = std::array{}; + auto [span, length] = kstd::format_to(std::span{buffer}, "{:04x}:{:02x}:{:02x}.{:1x}", address.segment_group, + address.bus, address.device, address.function); + context.push(std::string_view{span.data(), length}); + } +}; + +#endif diff --git a/kernel/kernel/devices/pci/function_address.tests.cpp b/kernel/kernel/devices/pci/function_address.tests.cpp new file mode 100644 index 00000000..ae9ccfff --- /dev/null +++ b/kernel/kernel/devices/pci/function_address.tests.cpp @@ -0,0 +1,89 @@ +#include + +#include + +#include + +#include + +// NOLINTBEGIN(readability-magic-numbers) + +SCENARIO("PCI Function Address formatting") +{ + GIVEN("A zeroed PCI function address") + { + kernel::devices::pci::function_address address{}; + + THEN("The address is formatted as 0000:00:00.0") + { + REQUIRE(kstd::format("{}", address) == "0000:00:00.0"); + } + } + + GIVEN("A non-zero PCI function address") + { + kernel::devices::pci::function_address address{}; + address.segment_group = 0xabcd; + address.bus = 0xff; + address.device = 0x1f; + address.function = 7; + + THEN("The address is formatted correctly") + { + REQUIRE(kstd::format("{}", address) == "abcd:ff:1f.7"); + } + } +} + +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}; + + THEN("they compare equal") + { + REQUIRE(address1 == address2); + } + + THEN("they are ordered as equal") + { + REQUIRE((address1 <=> address2) == std::strong_ordering::equal); + } + } + + 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}; + + THEN("They are not equal") + { + REQUIRE(address1 != address2); + } + + THEN("They are ordered correctly") + { + REQUIRE(address1 < address2); + } + } + + 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}; + + THEN("They are not equal") + { + REQUIRE(address1 != address2); + } + + THEN("They are ordered correctly") + { + REQUIRE(address1 < address2); + } + } +} + +// NOLINTEND(readability-magic-numbers) -- cgit v1.2.3