memory_manager.h 9.0 KB

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