diff options
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/kernel/devices/pci/function_address.hpp | 61 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/function_address.tests.cpp | 89 |
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) |
