diff options
Diffstat (limited to 'kapi')
| -rw-r--r-- | kapi/kapi/memory.hpp | 46 | ||||
| -rw-r--r-- | kapi/kapi/memory/mmio.hpp | 187 | ||||
| -rw-r--r-- | kapi/kapi/test_support/memory.hpp | 11 |
3 files changed, 199 insertions, 45 deletions
diff --git a/kapi/kapi/memory.hpp b/kapi/kapi/memory.hpp index ece85203..2abad38e 100644 --- a/kapi/kapi/memory.hpp +++ b/kapi/kapi/memory.hpp @@ -6,11 +6,11 @@ #include <kapi/memory/frame.hpp> // IWYU pragma: export #include <kapi/memory/frame_allocator.hpp> // IWYU pragma: export #include <kapi/memory/layout.hpp> // IWYU pragma: export +#include <kapi/memory/mmio.hpp> // IWYU pragma: export #include <kapi/memory/page.hpp> // IWYU pragma: export #include <kapi/memory/page_mapper.hpp> // IWYU pragma: export #include <kstd/memory.hpp> -#include <kstd/result.hpp> #include <cstddef> #include <optional> @@ -19,8 +19,6 @@ namespace kapi::memory { - using mmio_region = std::pair<linear_address, std::size_t>; - //! @addtogroup kapi-memory-kernel-defined //! @{ @@ -86,48 +84,6 @@ namespace kapi::memory //! @return The frame the page was previously mapped to. [[nodiscard]] auto unmap(page page) -> frame; - //! Initialize the Memory-mapped I/O region system. - //! - //! @param base The base address for the MMIO region. - //! @param page_count The number of pages the MMIO region is spans. - auto init_mmio(linear_address base, std::size_t page_count) -> void; - - //! Allocate a Memory-mapped I/O region of the given size. - //! - //! @warning This function will panic if the MMIO system has not been initialized! - //! - //! @param page_count The number of pages to allocate. - //! @return The newly allocated MMIO region on success, an error otherwise. - auto allocate_mmio_region(std::size_t page_count) -> kstd::result<mmio_region>; - - //! Map a region of Memory-mapped I/O address space to a given hardware address using the given flags. - //! - //! @warning This function will panic if no page mapper has been registered, or the page has already been mapped. - //! - //! @warning This function will not ensure that the frame is not already in use. - //! - //! @warning This function will panic if an attempt is made to map an MMIO region as executable. - //! - //! This function will always set the @p uncached, @p global, and @p supervisor_only flags. - //! - //! @param region The region to map. This region must cover the in-page offset of hw_base plus the range to be - //! accessed. - //! @param hw_base The base of the hardware region. - //! @param flags The flags to apply. - //! @return A pointer into the mapped region, including any frame offset, on success, an error otherwise, in which - //! case any partial mappings have been rolled back. - auto map_mmio_region(mmio_region region, physical_address hw_base, page_mapper::flags flags = {}) - -> kstd::result<kstd::observer_ptr<std::byte>>; - - //! Release (unmap and free) a Memory-mapped I/O region. - //! - //! This function is tolerant of partially mapped regions and will unmap any mapped pages within the region before - //! releasing the MMIO region. - //! - //! @warning This function will panic if the MMIO system has not been initialized! - //! @param region The region to release. - auto release_mmio_region(mmio_region region) -> void; - //! @} //! @addtogroup kapi-memory-platform-defined diff --git a/kapi/kapi/memory/mmio.hpp b/kapi/kapi/memory/mmio.hpp new file mode 100644 index 00000000..f1a7c6dc --- /dev/null +++ b/kapi/kapi/memory/mmio.hpp @@ -0,0 +1,187 @@ +#ifndef TEACHOS_KAPI_MEMORY_MMIO_HPP +#define TEACHOS_KAPI_MEMORY_MMIO_HPP + +// IWYU pragma: private, include <kapi/memory.hpp> + +#include <kapi/memory/address.hpp> + +#include <kstd/memory.hpp> +#include <kstd/result.hpp> +#include <kstd/units.hpp> + +#include <concepts> +#include <cstddef> + +namespace kapi::memory +{ + + //! This concept models the requirements for a type to be used as a MMIO datum. + //! + //! A valid MMIO datum type must be an unsigned integral type, excluding bool, and must have a size of 1, 2, 4, or 8 + //! bytes. + template<typename ValueType> + concept mmio_datum = requires { + requires std::unsigned_integral<ValueType>; + requires(!std::same_as<bool, ValueType>); + requires(sizeof(ValueType) == 1 || sizeof(ValueType) == 2 || sizeof(ValueType) == 4 || sizeof(ValueType) == 8); + }; + + //! @addtogroup kapi-memory-kernel-defined + //! @{ + + //! A window into memory-mapped I/O (MMIO) space. + //! + //! This structure represents a contiguous region of MMIO space that can be accessed according to the specified access + //! mode. The only permitted operations are read or write operations that access a single MMIO datum (see mmio_datum) + //! at a time. + //! + //! This type is move-only. A moved-from window is considered to have a size of zero and a base of null. All accesses + //! to a moved-from window are invalid and will panic. Destruction of a moved-from window does not change any mappings + //! or allocations. + struct mmio_window + { + //! The access mode for the MMIO window. + enum struct access : bool + { + //! The window can only be read from. + read_only, + //! The window can read from and written to. + writable, + }; + + //! Create a new MMIO window with the given properties. + //! + //! This function creates a new MMIO window of the specified size for the specified hardware MMIO address. The + //! mapping will always be created with the @p uncached, @p global, and @p supervisor_only page flags set. Depending + //! on the value of @p mode, the @p writable page flag will be set or not. The mapping covers every page containing + //! the requested range; accesses are nevertheless bounds-checked against @p size. All reads and writes are offset + //! from the page-aligned base address according to the in-page offset of the hardware base address. + //! + //! @warning This function will panic if the MMIO subsystem has not been initialized. + //! + //! @param hw_base The physical base address of the MMIO region. + //! @param size The size of this MMIO window. The size must not be zero. + //! @param mode The access mode for the MMIO region. + //! @return A new MMIO window on success, an error otherwise. + [[nodiscard]] auto static map(physical_address hw_base, kstd::bytes size, access mode) -> kstd::result<mmio_window>; + + mmio_window(mmio_window const &) = delete; + + //! Move-construct a new MMIO window from an existing one. + //! + //! The moved-from window will be left with a zero-sized state and a base of null. All accesses to the moved-from + //! window are invalid and will panic. + mmio_window(mmio_window && other) noexcept; + + //! Destroy this MMIO window and release its resources. + //! + //! This will unmap the MMIO window and release any associated resources. If the window has been moved from, this + //! operation does nothing. + ~mmio_window(); + + auto operator=(mmio_window const &) -> mmio_window & = delete; + + //! Move-assign an existing MMIO window to this one. + //! + //! The moved-from window will be left with a zero-sized state and a base of null. All accesses to the moved-from + //! window are invalid and will panic. If this window has been previously mapped, it will be unmapped and its + //! resources released. + //! + //! Assigning a window to itself is safe and has no effect. + auto operator=(mmio_window && other) noexcept -> mmio_window &; + + //! Read a value, at a given offset, from this MMIO window. + //! + //! @warning This function will panic if the offset is out of bounds. + //! + //! @warning This function will panic if the operation would result in an unaligned access. The check is performed + //! against the absolute address (including the in-page offset). + //! + //! @warning This function will panic if this window has been moved from. + //! + //! @tparam ValueType The type of value to read from the MMIO window. + //! @param offset The offset within the MMIO window to read from. + //! @return The value read from the MMIO window. + template<mmio_datum ValueType> + [[nodiscard]] auto read(kstd::bytes offset) const -> ValueType + { + assert_valid_access(offset, kstd::size_of<ValueType>()); + auto read_pointer = reinterpret_cast<ValueType volatile const *>(m_data_base.get() + offset.value); + return *read_pointer; + } + + //! Write a value, at a given offset, to this MMIO window. + //! + //! @warning This function will panic if the offset is out of bounds. + //! + //! @warning This function will panic if this window is read-only. + //! + //! @warning This function will panic if the operation would result in an unaligned access. The check is performed + //! against the absolute address (including the in-page offset). + //! + //! @warning This function will panic if this window has been moved from. + //! + //! @tparam ValueType The type of value to write to the MMIO window. + //! @param offset The offset within the MMIO window to write to. + //! @param value The value to write to the MMIO window. + template<mmio_datum ValueType> + auto write(kstd::bytes offset, ValueType value) -> void + { + assert_valid_access(offset, kstd::size_of<ValueType>()); + assert_writable(); + auto write_pointer = reinterpret_cast<ValueType volatile *>(m_data_base.get() + offset.value); + *write_pointer = value; + } + + private: + //! Construct a new MMIO window with the given mapping base, data base, size, and access mode. + //! + //! @param mapping_base The linear base address for the MMIO window. + //! @param data_base The offset adjusted base address for the MMIO window, as returned by the page mapper. + //! @param size The accessible size of the MMIO window. + //! @param mode The access mode of the MMIO window. + mmio_window(linear_address mapping_base, kstd::observer_ptr<std::byte> data_base, kstd::bytes size, access mode); + + //! Check if an access at the given offset and size is valid for this MMIO window. + //! + //! @warning This function will panic if the access is out of bounds. + //! + //! @warning This function will panic if the operation would result in an unaligned access. The check is performed + //! against the absolute address (including the in-page offset). + //! + //! @warning This function will panic if this window has been moved from. + //! + //! @param offset The offset within the MMIO window to check. + //! @param size The size of the access to check. + auto assert_valid_access(kstd::bytes offset, kstd::bytes size) const -> void; + + //! Check if this MMIO window is writable. + //! + //! @warning This function will panic if the window is not writable. + //! + auto assert_writable() const -> void; + + //! Release the resources associated with this MMIO window. + auto release() -> void; + + //! The linear base address of this MMIO window. + linear_address m_mapping_base; + //! The offset adjusted base address of this MMIO window. + kstd::observer_ptr<std::byte> m_data_base; + //! The accessible size of this MMIO window. + kstd::bytes m_data_size; + //! The access mode of this MMIO window. + access m_mode; + }; + + //! Initialize the Memory-mapped I/O region system. + //! + //! @param base The base address for the MMIO region. + //! @param page_count The number of pages the MMIO region spans. + auto init_mmio(linear_address base, std::size_t page_count) -> void; + + //! @} + +} // namespace kapi::memory + +#endif diff --git a/kapi/kapi/test_support/memory.hpp b/kapi/kapi/test_support/memory.hpp new file mode 100644 index 00000000..33c02bb9 --- /dev/null +++ b/kapi/kapi/test_support/memory.hpp @@ -0,0 +1,11 @@ +#ifndef TEACHOS_KAPI_TEST_SUPPORT_MEMORY_HPP +#define TEACHOS_KAPI_TEST_SUPPORT_MEMORY_HPP + +namespace kapi::test_support::memory +{ + + auto deinit_mmio() -> void; + +} // namespace kapi::test_support::memory + +#endif |
