memory_manager.h 8.1 KB

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