diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-11-11 15:06:37 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-11-12 14:41:29 +0100 |
| commit | ef6bf8e59c7d86d4c3b3f43233755a060bd4f891 (patch) | |
| tree | e5a27b8364876b5fafa63f14e273e3df926aa010 /libs/acpi | |
| parent | f639297d1b96b6aeb3d3822a63b9a602e1f814fb (diff) | |
| download | kernel-ef6bf8e59c7d86d4c3b3f43233755a060bd4f891.tar.xz kernel-ef6bf8e59c7d86d4c3b3f43233755a060bd4f891.zip | |
libs/acpi: extend MCFG table interface
Diffstat (limited to 'libs/acpi')
| -rw-r--r-- | libs/acpi/acpi/common/vla_table.hpp | 13 | ||||
| -rw-r--r-- | libs/acpi/acpi/data/mcfg.cpp | 29 | ||||
| -rw-r--r-- | libs/acpi/acpi/data/mcfg.hpp | 51 | ||||
| -rw-r--r-- | libs/acpi/acpi/data/mcfg.tests.cpp | 24 |
4 files changed, 114 insertions, 3 deletions
diff --git a/libs/acpi/acpi/common/vla_table.hpp b/libs/acpi/acpi/common/vla_table.hpp index d3f33a7c..7993e2db 100644 --- a/libs/acpi/acpi/common/vla_table.hpp +++ b/libs/acpi/acpi/common/vla_table.hpp @@ -74,11 +74,10 @@ namespace acpi [[nodiscard]] auto begin() noexcept -> iterator { - auto base = std::bit_cast<std::byte *>(this); - base += sizeof(TableType); + auto base = vla_base(); auto limit = std::bit_cast<std::byte *>(this); limit += this->length().value; - return iterator{std::bit_cast<entry_type *>(base), std::bit_cast<entry_type *>(limit)}; + return iterator{base, std::bit_cast<entry_type *>(limit)}; } [[nodiscard]] auto end() noexcept -> iterator @@ -110,6 +109,14 @@ namespace acpi return end(); } + protected: + [[nodiscard]] auto vla_base() const noexcept -> entry_type * + { + auto base = std::bit_cast<std::byte *>(this); + base += sizeof(TableType); + return reinterpret_cast<entry_type *>(base); + } + private: friend TableType; diff --git a/libs/acpi/acpi/data/mcfg.cpp b/libs/acpi/acpi/data/mcfg.cpp index ffb2933d..9e1b9d4d 100644 --- a/libs/acpi/acpi/data/mcfg.cpp +++ b/libs/acpi/acpi/data/mcfg.cpp @@ -1,13 +1,23 @@ #include <acpi/data/mcfg.hpp> +#include <kstd/os/error.hpp> + +#include <cstddef> #include <cstdint> namespace { + //! The offset of the base address field within an MCFG entry. constexpr auto static base_address_offset = 0uz; + + //! The offset of the segment group number field within an MCFG entry. constexpr auto static segment_group_number_offset = 8uz; + + //! The offset of the start bus number field within an MCFG entry. constexpr auto static start_bus_number_offset = 10uz; + + //! The offset of the end bus number field within an MCFG entry. constexpr auto static end_bus_number_offset = 11uz; } // namespace @@ -35,4 +45,23 @@ namespace acpi return read<std::uint8_t, end_bus_number_offset>(); } + auto mcfg::size() const noexcept -> std::size_t + { + return (this->length().value - sizeof(*this)) / sizeof(mcfg_entry); + } + + auto mcfg::operator[](std::size_t index) const noexcept -> mcfg_entry const & + { + return vla_base()[index]; + } + + auto mcfg::at(std::size_t index) const -> mcfg_entry const & + { + if (index >= size()) + { + kstd::os::panic("[ACPI] MCFG table: index out of bounds"); + } + return (*this)[index]; + } + } // namespace acpi
\ No newline at end of file diff --git a/libs/acpi/acpi/data/mcfg.hpp b/libs/acpi/acpi/data/mcfg.hpp index 32d5fce4..881f479a 100644 --- a/libs/acpi/acpi/data/mcfg.hpp +++ b/libs/acpi/acpi/data/mcfg.hpp @@ -29,19 +29,46 @@ namespace acpi using type = struct mcfg; }; + //! An entry in the MCFG table. struct mcfg_entry { + //! Get the processor-relative base address of the memory-mapped configuration space, this entry describes. + //! + //! This is address represents the physical address of the memory-mapped configuration space. It is the + //! responsibility of the operating system to map this address appropriately. + //! + //! @return The physical address of the memory-mapped configuration space. [[nodiscard]] auto base_address() const noexcept -> std::uintptr_t; + + //! Get the PCI segment group number this entry applies to. + //! + //! @return The segment group number. [[nodiscard]] auto segment_group_number() const noexcept -> std::uint16_t; + + //! Get the number of the first bus this entry applies to. + //! + //! @return The number of the first bus. [[nodiscard]] auto start_bus_number() const noexcept -> std::uint8_t; + + //! Get the number of the last bus this entry applies to. + //! + //! @return The number of the last bus. [[nodiscard]] auto end_bus_number() const noexcept -> std::uint8_t; + //! Get the length of this entry. + //! + //! @return The length of this entry in bytes. [[nodiscard]] constexpr auto length() const noexcept -> std::size_t { return 16; } private: + //! Read a value of a specified type at a specified offset within this entry. + //! + //! @tparam ValueType The type of value to read. + //! @tparam Offset The offset within the entry to read from. + //! @return The value read from the specified offset. template<typename ValueType, std::size_t Offset> [[nodiscard]] auto read() const noexcept -> ValueType { @@ -51,12 +78,36 @@ namespace acpi return result; } + //! The raw data of this entry. std::array<std::byte, 16> m_data; }; + //! The Memory-Mapped Configuration Space (MCFG) table. + //! + //! This table provides information about the memory-mapped configuration space for PCI and PCIe devices. struct mcfg : vla_table<mcfg_entry, mcfg> { + //! Get the number of entries in this MCFG table. + [[nodiscard]] auto size() const noexcept -> std::size_t; + + //! Get the nth entry in this MCFG table. + //! + //! @warning This function does not perform bounds checking. + //! + //! @param index The index of the entry to retrieve. + //! @return The MCFG entry at the specified index. + [[nodiscard]] auto operator[](std::size_t index) const noexcept -> mcfg_entry const &; + + //! Get the nth entry in this MCFG table. + //! + //! @warning This function panics if the index is out of bounds. + //! + //! @param index The index of the entry to retrieve. + //! @return The MCFG entry at the specified index. + [[nodiscard]] auto at(std::size_t index) const -> mcfg_entry const &; + private: + //! The reserved bytes at the end of the MCFG table header. [[maybe_unused]] std::array<std::byte, 8> m_reserved{}; }; diff --git a/libs/acpi/acpi/data/mcfg.tests.cpp b/libs/acpi/acpi/data/mcfg.tests.cpp index 9cbe475e..e0eb59c7 100644 --- a/libs/acpi/acpi/data/mcfg.tests.cpp +++ b/libs/acpi/acpi/data/mcfg.tests.cpp @@ -2,6 +2,8 @@ #include <acpi/test_data/tables.hpp> +#include <kstd/test_support/os_panic.hpp> + #include <catch2/catch_test_macros.hpp> #include <iterator> @@ -31,6 +33,11 @@ SCENARIO("MCFG parsing", "[acpi][mcfg]") REQUIRE(std::ranges::distance(*mcfg) == 1); } + THEN("size is 1") + { + REQUIRE(mcfg->size() == 1); + } + THEN("the first entry has base address 0x00000000f0000000") { REQUIRE(mcfg->cbegin()->base_address() == 0x0000'0000'f000'0000); @@ -50,6 +57,23 @@ SCENARIO("MCFG parsing", "[acpi][mcfg]") { REQUIRE(mcfg->cbegin()->end_bus_number() == 0x7f); } + + THEN("accessing the first entry via subscript operator is possible") + { + auto entry = (*mcfg)[0]; + REQUIRE(entry.base_address() == mcfg->cbegin()->base_address()); + } + + THEN("accessing the first entry via at() is possible") + { + auto entry = (*mcfg).at(0); + REQUIRE(entry.base_address() == mcfg->cbegin()->base_address()); + } + + THEN("trying to access the second entry panics") + { + REQUIRE_THROWS_AS((*mcfg).at(1), kstd::tests::os_panic); + } } } }
\ No newline at end of file |
