diff options
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/kernel/devices/pci/function_info.hpp | 72 | ||||
| -rw-r--r-- | kernel/kernel/devices/pci/function_info.tests.cpp | 138 |
2 files changed, 210 insertions, 0 deletions
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 <kernel/devices/pci/function_address.hpp> + +#include <cstdint> + +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<header_type>(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 <kernel/devices/pci/function_info.hpp> + +#include <catch2/catch_test_macros.hpp> + +// 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<kernel::devices::pci::header_type>(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<kernel::devices::pci::header_type>(0x7f)); + } + } +} + +// NOLINTEND(readability-magic-numbers) |
