memory_manager.h 6.9 KB

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