aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-11-16 17:32:59 +0100
committerFelix Morgner <felix.morgner@ost.ch>2026-11-22 14:20:36 +0100
commit0dfdb6c4f4aa428d6e1951958edafd82b87a0af8 (patch)
tree4e4045f69f870c99912b3ca0e4aec993e3408a17
parenta42ab28f65e3ef5926137ddf4c62b188c33ca440 (diff)
downloadkernel-0dfdb6c4f4aa428d6e1951958edafd82b87a0af8.tar.xz
kernel-0dfdb6c4f4aa428d6e1951958edafd82b87a0af8.zip
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.
-rw-r--r--kernel/kernel/devices/pci/function_address.hpp61
-rw-r--r--kernel/kernel/devices/pci/function_address.tests.cpp89
2 files changed, 150 insertions, 0 deletions
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 <kstd/format.hpp>
+
+#include <array>
+#include <compare>
+#include <cstdint>
+#include <span>
+#include <string_view>
+
+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<kernel::devices::pci::function_address>
+{
+ 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<char, output_length>{};
+ 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 <kernel/devices/pci/function_address.hpp>
+
+#include <kstd/format.hpp>
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <compare>
+
+// 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)