memory_manager.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <map>
  6. #include <optional>
  7. #include <vector>
  8. #include "common/common_types.h"
  9. namespace VideoCore {
  10. class RasterizerInterface;
  11. }
  12. namespace Core {
  13. class System;
  14. }
  15. namespace Tegra {
  16. class PageEntry final {
  17. public:
  18. enum class State : u32 {
  19. Unmapped = static_cast<u32>(-1),
  20. Allocated = static_cast<u32>(-2),
  21. };
  22. constexpr PageEntry() = default;
  23. constexpr PageEntry(State state_) : state{state_} {}
  24. constexpr PageEntry(VAddr addr) : state{static_cast<State>(addr >> ShiftBits)} {}
  25. [[nodiscard]] constexpr bool IsUnmapped() const {
  26. return state == State::Unmapped;
  27. }
  28. [[nodiscard]] constexpr bool IsAllocated() const {
  29. return state == State::Allocated;
  30. }
  31. [[nodiscard]] constexpr bool IsValid() const {
  32. return !IsUnmapped() && !IsAllocated();
  33. }
  34. [[nodiscard]] constexpr VAddr ToAddress() const {
  35. if (!IsValid()) {
  36. return {};
  37. }
  38. return static_cast<VAddr>(state) << ShiftBits;
  39. }
  40. [[nodiscard]] constexpr PageEntry operator+(u64 offset) const {
  41. // If this is a reserved value, offsets do not apply
  42. if (!IsValid()) {
  43. return *this;
  44. }
  45. return PageEntry{(static_cast<VAddr>(state) << ShiftBits) + offset};
  46. }
  47. private:
  48. static constexpr std::size_t ShiftBits{12};
  49. State state{State::Unmapped};
  50. };
  51. static_assert(sizeof(PageEntry) == 4, "PageEntry is too large");
  52. class MemoryManager final {
  53. public:
  54. explicit MemoryManager(Core::System& system_);
  55. ~MemoryManager();
  56. /// Binds a renderer to the memory manager.
  57. void BindRasterizer(VideoCore::RasterizerInterface* rasterizer);
  58. [[nodiscard]] std::optional<VAddr> GpuToCpuAddress(GPUVAddr addr) const;
  59. [[nodiscard]] std::optional<VAddr> GpuToCpuAddress(GPUVAddr addr, std::size_t size) const;
  60. template <typename T>
  61. [[nodiscard]] T Read(GPUVAddr addr) const;
  62. template <typename T>
  63. void Write(GPUVAddr addr, T data);
  64. [[nodiscard]] u8* GetPointer(GPUVAddr addr);
  65. [[nodiscard]] const u8* GetPointer(GPUVAddr addr) const;
  66. /// Returns the number of bytes until the end of the memory map containing the given GPU address
  67. [[nodiscard]] size_t BytesToMapEnd(GPUVAddr gpu_addr) const noexcept;
  68. /**
  69. * ReadBlock and WriteBlock are full read and write operations over virtual
  70. * GPU Memory. It's important to use these when GPU memory may not be continuous
  71. * in the Host Memory counterpart. Note: This functions cause Host GPU Memory
  72. * Flushes and Invalidations, respectively to each operation.
  73. */
  74. void ReadBlock(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size) const;
  75. void WriteBlock(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size);
  76. void CopyBlock(GPUVAddr gpu_dest_addr, GPUVAddr gpu_src_addr, std::size_t size);
  77. /**
  78. * ReadBlockUnsafe and WriteBlockUnsafe are special versions of ReadBlock and
  79. * WriteBlock respectively. In this versions, no flushing or invalidation is actually
  80. * done and their performance is similar to a memcpy. This functions can be used
  81. * on either of this 2 scenarios instead of their safe counterpart:
  82. * - Memory which is sure to never be represented in the Host GPU.
  83. * - Memory Managed by a Cache Manager. Example: Texture Flushing should use
  84. * WriteBlockUnsafe instead of WriteBlock since it shouldn't invalidate the texture
  85. * being flushed.
  86. */
  87. void ReadBlockUnsafe(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size) const;
  88. void WriteBlockUnsafe(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size);
  89. /**
  90. * Checks if a gpu region can be simply read with a pointer.
  91. */
  92. [[nodiscard]] bool IsGranularRange(GPUVAddr gpu_addr, std::size_t size) const;
  93. /**
  94. * Checks if a gpu region is mapped by a single range of cpu addresses.
  95. */
  96. [[nodiscard]] bool IsContinousRange(GPUVAddr gpu_addr, std::size_t size) const;
  97. /**
  98. * Checks if a gpu region is mapped entirely.
  99. */
  100. [[nodiscard]] bool IsFullyMappedRange(GPUVAddr gpu_addr, std::size_t size) const;
  101. /**
  102. * Returns a vector with all the subranges of cpu addresses mapped beneath.
  103. * if the region is continous, a single pair will be returned. If it's unmapped, an empty vector
  104. * will be returned;
  105. */
  106. std::vector<std::pair<GPUVAddr, std::size_t>> GetSubmappedRange(GPUVAddr gpu_addr,
  107. std::size_t size) const;
  108. [[nodiscard]] GPUVAddr Map(VAddr cpu_addr, GPUVAddr gpu_addr, std::size_t size);
  109. [[nodiscard]] GPUVAddr MapAllocate(VAddr cpu_addr, std::size_t size, std::size_t align);
  110. [[nodiscard]] GPUVAddr MapAllocate32(VAddr cpu_addr, std::size_t size);
  111. [[nodiscard]] std::optional<GPUVAddr> AllocateFixed(GPUVAddr gpu_addr, std::size_t size);
  112. [[nodiscard]] GPUVAddr Allocate(std::size_t size, std::size_t align);
  113. void Unmap(GPUVAddr gpu_addr, std::size_t size);
  114. void FlushRegion(GPUVAddr gpu_addr, size_t size) const;
  115. private:
  116. [[nodiscard]] PageEntry GetPageEntry(GPUVAddr gpu_addr) const;
  117. void SetPageEntry(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size = page_size);
  118. GPUVAddr UpdateRange(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size);
  119. [[nodiscard]] std::optional<GPUVAddr> FindFreeRange(std::size_t size, std::size_t align,
  120. bool start_32bit_address = false) const;
  121. void TryLockPage(PageEntry page_entry, std::size_t size);
  122. void TryUnlockPage(PageEntry page_entry, std::size_t size);
  123. void ReadBlockImpl(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size,
  124. bool is_safe) const;
  125. void WriteBlockImpl(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size,
  126. bool is_safe);
  127. [[nodiscard]] static constexpr std::size_t PageEntryIndex(GPUVAddr gpu_addr) {
  128. return (gpu_addr >> page_bits) & page_table_mask;
  129. }
  130. static constexpr u64 address_space_size = 1ULL << 40;
  131. static constexpr u64 address_space_start = 1ULL << 32;
  132. static constexpr u64 address_space_start_low = 1ULL << 16;
  133. static constexpr u64 page_bits{16};
  134. static constexpr u64 page_size{1 << page_bits};
  135. static constexpr u64 page_mask{page_size - 1};
  136. static constexpr u64 page_table_bits{24};
  137. static constexpr u64 page_table_size{1 << page_table_bits};
  138. static constexpr u64 page_table_mask{page_table_size - 1};
  139. Core::System& system;
  140. VideoCore::RasterizerInterface* rasterizer = nullptr;
  141. std::vector<PageEntry> page_table;
  142. using MapRange = std::pair<GPUVAddr, size_t>;
  143. std::vector<MapRange> map_ranges;
  144. std::vector<std::pair<VAddr, std::size_t>> cache_invalidate_queue;
  145. };
  146. } // namespace Tegra