memory_manager.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <atomic>
  5. #include <map>
  6. #include <mutex>
  7. #include <optional>
  8. #include <vector>
  9. #include <boost/container/small_vector.hpp>
  10. #include "common/common_types.h"
  11. #include "common/multi_level_page_table.h"
  12. #include "common/range_map.h"
  13. #include "common/scratch_buffer.h"
  14. #include "common/virtual_buffer.h"
  15. #include "core/memory.h"
  16. #include "video_core/cache_types.h"
  17. #include "video_core/pte_kind.h"
  18. namespace VideoCore {
  19. class RasterizerInterface;
  20. }
  21. namespace VideoCommon {
  22. class InvalidationAccumulator;
  23. }
  24. namespace Core {
  25. class DeviceMemory;
  26. namespace Memory {
  27. class Memory;
  28. } // namespace Memory
  29. class System;
  30. } // namespace Core
  31. namespace Tegra {
  32. class MemoryManager final {
  33. public:
  34. explicit MemoryManager(Core::System& system_, u64 address_space_bits_ = 40,
  35. u64 big_page_bits_ = 16, u64 page_bits_ = 12);
  36. ~MemoryManager();
  37. size_t GetID() const {
  38. return unique_identifier;
  39. }
  40. /// Binds a renderer to the memory manager.
  41. void BindRasterizer(VideoCore::RasterizerInterface* rasterizer);
  42. [[nodiscard]] std::optional<VAddr> GpuToCpuAddress(GPUVAddr addr) const;
  43. [[nodiscard]] std::optional<VAddr> GpuToCpuAddress(GPUVAddr addr, std::size_t size) const;
  44. template <typename T>
  45. [[nodiscard]] T Read(GPUVAddr addr) const;
  46. template <typename T>
  47. void Write(GPUVAddr addr, T data);
  48. [[nodiscard]] u8* GetPointer(GPUVAddr addr);
  49. [[nodiscard]] const u8* GetPointer(GPUVAddr addr) const;
  50. template <typename T>
  51. [[nodiscard]] T* GetPointer(GPUVAddr addr) {
  52. const auto address{GpuToCpuAddress(addr)};
  53. if (!address) {
  54. return {};
  55. }
  56. return memory.GetPointer(*address);
  57. }
  58. template <typename T>
  59. [[nodiscard]] const T* GetPointer(GPUVAddr addr) const {
  60. return GetPointer<T*>(addr);
  61. }
  62. /**
  63. * ReadBlock and WriteBlock are full read and write operations over virtual
  64. * GPU Memory. It's important to use these when GPU memory may not be continuous
  65. * in the Host Memory counterpart. Note: This functions cause Host GPU Memory
  66. * Flushes and Invalidations, respectively to each operation.
  67. */
  68. void ReadBlock(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size,
  69. VideoCommon::CacheType which = VideoCommon::CacheType::All) const;
  70. void WriteBlock(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size,
  71. VideoCommon::CacheType which = VideoCommon::CacheType::All);
  72. void CopyBlock(GPUVAddr gpu_dest_addr, GPUVAddr gpu_src_addr, std::size_t size,
  73. VideoCommon::CacheType which = VideoCommon::CacheType::All);
  74. /**
  75. * ReadBlockUnsafe and WriteBlockUnsafe are special versions of ReadBlock and
  76. * WriteBlock respectively. In this versions, no flushing or invalidation is actually
  77. * done and their performance is similar to a memcpy. This functions can be used
  78. * on either of this 2 scenarios instead of their safe counterpart:
  79. * - Memory which is sure to never be represented in the Host GPU.
  80. * - Memory Managed by a Cache Manager. Example: Texture Flushing should use
  81. * WriteBlockUnsafe instead of WriteBlock since it shouldn't invalidate the texture
  82. * being flushed.
  83. */
  84. void ReadBlockUnsafe(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size) const;
  85. void WriteBlockUnsafe(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size);
  86. void WriteBlockCached(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size);
  87. /**
  88. * Checks if a gpu region can be simply read with a pointer.
  89. */
  90. [[nodiscard]] bool IsGranularRange(GPUVAddr gpu_addr, std::size_t size) const;
  91. /**
  92. * Checks if a gpu region is mapped by a single range of cpu addresses.
  93. */
  94. [[nodiscard]] bool IsContinuousRange(GPUVAddr gpu_addr, std::size_t size) const;
  95. /**
  96. * Checks if a gpu region is mapped entirely.
  97. */
  98. [[nodiscard]] bool IsFullyMappedRange(GPUVAddr gpu_addr, std::size_t size) const;
  99. /**
  100. * Returns a vector with all the subranges of cpu addresses mapped beneath.
  101. * if the region is continuous, a single pair will be returned. If it's unmapped, an empty
  102. * vector will be returned;
  103. */
  104. boost::container::small_vector<std::pair<GPUVAddr, std::size_t>, 32> GetSubmappedRange(
  105. GPUVAddr gpu_addr, std::size_t size) const;
  106. GPUVAddr Map(GPUVAddr gpu_addr, VAddr cpu_addr, std::size_t size,
  107. PTEKind kind = PTEKind::INVALID, bool is_big_pages = true);
  108. GPUVAddr MapSparse(GPUVAddr gpu_addr, std::size_t size, bool is_big_pages = true);
  109. void Unmap(GPUVAddr gpu_addr, std::size_t size);
  110. void FlushRegion(GPUVAddr gpu_addr, size_t size,
  111. VideoCommon::CacheType which = VideoCommon::CacheType::All) const;
  112. void InvalidateRegion(GPUVAddr gpu_addr, size_t size,
  113. VideoCommon::CacheType which = VideoCommon::CacheType::All) const;
  114. bool IsMemoryDirty(GPUVAddr gpu_addr, size_t size,
  115. VideoCommon::CacheType which = VideoCommon::CacheType::All) const;
  116. size_t MaxContinuousRange(GPUVAddr gpu_addr, size_t size) const;
  117. bool IsWithinGPUAddressRange(GPUVAddr gpu_addr) const {
  118. return gpu_addr < address_space_size;
  119. }
  120. PTEKind GetPageKind(GPUVAddr gpu_addr) const;
  121. size_t GetMemoryLayoutSize(GPUVAddr gpu_addr,
  122. size_t max_size = std::numeric_limits<size_t>::max()) const;
  123. void FlushCaching();
  124. const u8* GetSpan(const GPUVAddr src_addr, const std::size_t size) const;
  125. u8* GetSpan(const GPUVAddr src_addr, const std::size_t size);
  126. private:
  127. template <bool is_big_pages, typename FuncMapped, typename FuncReserved, typename FuncUnmapped>
  128. inline void MemoryOperation(GPUVAddr gpu_src_addr, std::size_t size, FuncMapped&& func_mapped,
  129. FuncReserved&& func_reserved, FuncUnmapped&& func_unmapped) const;
  130. template <bool is_safe>
  131. void ReadBlockImpl(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size,
  132. VideoCommon::CacheType which) const;
  133. template <bool is_safe>
  134. void WriteBlockImpl(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size,
  135. VideoCommon::CacheType which);
  136. template <bool is_big_page>
  137. [[nodiscard]] std::size_t PageEntryIndex(GPUVAddr gpu_addr) const {
  138. if constexpr (is_big_page) {
  139. return (gpu_addr >> big_page_bits) & big_page_table_mask;
  140. } else {
  141. return (gpu_addr >> page_bits) & page_table_mask;
  142. }
  143. }
  144. inline bool IsBigPageContinuous(size_t big_page_index) const;
  145. inline void SetBigPageContinuous(size_t big_page_index, bool value);
  146. template <bool is_gpu_address>
  147. void GetSubmappedRangeImpl(
  148. GPUVAddr gpu_addr, std::size_t size,
  149. boost::container::small_vector<
  150. std::pair<std::conditional_t<is_gpu_address, GPUVAddr, VAddr>, std::size_t>, 32>&
  151. result) const;
  152. Core::System& system;
  153. Core::Memory::Memory& memory;
  154. Core::DeviceMemory& device_memory;
  155. const u64 address_space_bits;
  156. const u64 page_bits;
  157. u64 address_space_size;
  158. u64 page_size;
  159. u64 page_mask;
  160. u64 page_table_mask;
  161. static constexpr u64 cpu_page_bits{12};
  162. const u64 big_page_bits;
  163. u64 big_page_size;
  164. u64 big_page_mask;
  165. u64 big_page_table_mask;
  166. VideoCore::RasterizerInterface* rasterizer = nullptr;
  167. enum class EntryType : u64 {
  168. Free = 0,
  169. Reserved = 1,
  170. Mapped = 2,
  171. };
  172. std::vector<u64> entries;
  173. std::vector<u64> big_entries;
  174. template <EntryType entry_type>
  175. GPUVAddr PageTableOp(GPUVAddr gpu_addr, [[maybe_unused]] VAddr cpu_addr, size_t size,
  176. PTEKind kind);
  177. template <EntryType entry_type>
  178. GPUVAddr BigPageTableOp(GPUVAddr gpu_addr, [[maybe_unused]] VAddr cpu_addr, size_t size,
  179. PTEKind kind);
  180. template <bool is_big_page>
  181. inline EntryType GetEntry(size_t position) const;
  182. template <bool is_big_page>
  183. inline void SetEntry(size_t position, EntryType entry);
  184. Common::MultiLevelPageTable<u32> page_table;
  185. Common::RangeMap<GPUVAddr, PTEKind> kind_map;
  186. Common::VirtualBuffer<u32> big_page_table_cpu;
  187. std::vector<u64> big_page_continuous;
  188. boost::container::small_vector<std::pair<VAddr, std::size_t>, 32> page_stash{};
  189. boost::container::small_vector<std::pair<VAddr, std::size_t>, 32> page_stash2{};
  190. mutable std::mutex guard;
  191. static constexpr size_t continuous_bits = 64;
  192. const size_t unique_identifier;
  193. std::unique_ptr<VideoCommon::InvalidationAccumulator> accumulator;
  194. static std::atomic<size_t> unique_identifier_generator;
  195. Common::ScratchBuffer<u8> tmp_buffer;
  196. };
  197. } // namespace Tegra