From 05115c3961d316fd52409a753a88dd573cab4860 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 16 Nov 2026 17:41:05 +0100 Subject: kernel/devices: introduce PCI function_info Besides address information, each PCI function also has associated metadata. The function_info type carries the standard metadata for a PCI function: vendor and device IDs, the class code 3-tuple, and the header type. A bus scan reads those values when enumerating the PCI ECAM data, and returns it. The header type of the is stored in its raw form, encoding both the type of the PCI function header, as well as the multi-function flag. function_info provides two functions (type() and is_multifunction()) to extract those pieces of information from the raw header type value. In addition to the type extraction function, this change introduces an enumeration type (kernel::devices::pci::header_type) representing the standardized type values for PCI function headers. Unknown function types are presented as opaque values. Unit tests for derived attributes of a PCI function are provided. --- kernel/kernel/devices/pci/function_info.hpp | 72 +++++++++++ kernel/kernel/devices/pci/function_info.tests.cpp | 138 ++++++++++++++++++++++ 2 files changed, 210 insertions(+) create mode 100644 kernel/kernel/devices/pci/function_info.hpp create mode 100644 kernel/kernel/devices/pci/function_info.tests.cpp (limited to 'kernel') diff --git a/kernel/kernel/devices/pci/function_info.hpp b/kernel/kernel/devices/pci/function_info.hpp new file mode 100644 index 00000000..199716f5 --- /dev/null +++ b/kernel/kernel/devices/pci/function_info.hpp @@ -0,0 +1,72 @@ +#ifndef TEACHOS_KERNEL_DEVICES_PCI_FUNCTION_INFO_HPP +#define TEACHOS_KERNEL_DEVICES_PCI_FUNCTION_INFO_HPP + +#include + +#include + +namespace kernel::devices::pci +{ + + //! The type of PCI header. + //! + //! This enumeration represents the possible types of PCI headers that a PCI function can have. The values correspond + //! to the encoding in the raw header type field, with the multifunction bit masked out. Devices may report + //! non-standard header types. + enum struct header_type : std::uint8_t + { + //! The device is an ordinary PCI endpoint device. + endpoint = 0, + //! The device is a PCI-to-PCI bridge. + pci_to_pci_bridge = 1, + //! The device is a CardBus bridge. + cardbus_bridge = 2 + }; + + //! Information about a PCI function. + struct function_info + { + //! Returns the header type of the PCI function. + //! + //! @note The value returned by this function may be outside the defined enumerator values of @p header_type, if the + //! underlying hardware encodes a non-standard header type. + //! + //! @return The header type of the PCI function, with the multifunction bit masked out. + [[nodiscard]] constexpr auto type() const noexcept -> header_type + { + constexpr auto bits_0_to_6 = 0x7F; + return static_cast(raw_header_type & bits_0_to_6); + } + + //! Returns whether the PCI function is part of a multifunction device. + //! + //! @note The read bit only represents the multifunction indicator when read from function 0 of a device. + //! + //! @return @p true iff the PCI function is part of a multifunction device, @p false otherwise. + [[nodiscard]] constexpr auto is_multifunction() const noexcept -> bool + { + constexpr auto bit_7 = 0x80; + return (raw_header_type & bit_7) != 0; + } + + //! The address of the PCI function described by this structure. + function_address address{}; + //! The vendor ID of the PCI function. + std::uint16_t vendor_id{}; + //! The device ID of the PCI function. + std::uint16_t device_id{}; + //! The revision of the PCI function. + std::uint8_t revision{}; + //! The programming interface of the PCI function. + std::uint8_t programming_interface{}; + //! The subclass code of the PCI function. + std::uint8_t subclass_code{}; + //! The class code of the PCI function. + std::uint8_t class_code{}; + //! The raw (encoded) header type of the PCI function. + std::uint8_t raw_header_type{}; + }; + +} // namespace kernel::devices::pci + +#endif diff --git a/kernel/kernel/devices/pci/function_info.tests.cpp b/kernel/kernel/devices/pci/function_info.tests.cpp new file mode 100644 index 00000000..91e838e7 --- /dev/null +++ b/kernel/kernel/devices/pci/function_info.tests.cpp @@ -0,0 +1,138 @@ +#include + +#include + +// NOLINTBEGIN(readability-magic-numbers) + +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{}; + info.raw_header_type = 0x00; + + THEN("it is not a multifunction device") + { + REQUIRE_FALSE(info.is_multifunction()); + } + + THEN("it is a basic endpoint") + { + REQUIRE(info.type() == kernel::devices::pci::header_type::endpoint); + } + } + + GIVEN("A function of a multifunction PCI device that is a basic endpoint") + { + kernel::devices::pci::function_info info{}; + info.raw_header_type = 0x80; + + THEN("it is recognized as a multifunction device") + { + REQUIRE(info.is_multifunction()); + } + + THEN("it is a basic endpoint") + { + REQUIRE(info.type() == kernel::devices::pci::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{}; + info.raw_header_type = 0x01; + + THEN("it is recognized as not a multifunction device") + { + REQUIRE_FALSE(info.is_multifunction()); + } + + THEN("it is a PCI-to-PCI bridge") + { + REQUIRE(info.type() == kernel::devices::pci::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{}; + info.raw_header_type = 0x81; + + THEN("it is recognized as a multifunction device") + { + REQUIRE(info.is_multifunction()); + } + + THEN("it is a PCI-to-PCI bridge") + { + REQUIRE(info.type() == kernel::devices::pci::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{}; + info.raw_header_type = 0x02; + + THEN("it is recognized as not a multifunction device") + { + REQUIRE_FALSE(info.is_multifunction()); + } + + THEN("it is a CardBus bridge") + { + REQUIRE(info.type() == kernel::devices::pci::header_type::cardbus_bridge); + } + } + + GIVEN("A function of a multifunction PCI device that is a CardBus bridge") + { + kernel::devices::pci::function_info info{}; + info.raw_header_type = 0x82; + + THEN("it is recognized as a multifunction device") + { + REQUIRE(info.is_multifunction()); + } + + THEN("it is a CardBus bridge") + { + REQUIRE(info.type() == kernel::devices::pci::header_type::cardbus_bridge); + } + } + + GIVEN("A function of a single-function PCI device that is an unknown type") + { + kernel::devices::pci::function_info info{}; + info.raw_header_type = 0x7f; + + THEN("it is recognized as not a multifunction device") + { + REQUIRE_FALSE(info.is_multifunction()); + } + + THEN("it is an unknown type") + { + REQUIRE(info.type() == static_cast(0x7f)); + } + } + + GIVEN("A function of a multifunction PCI device that is an unknown type") + { + kernel::devices::pci::function_info info{}; + info.raw_header_type = 0xff; + + THEN("it is recognized as a multifunction device") + { + REQUIRE(info.is_multifunction()); + } + + THEN("it is an unknown type") + { + REQUIRE(info.type() == static_cast(0x7f)); + } + } +} + +// NOLINTEND(readability-magic-numbers) -- cgit v1.2.3