memory_manager.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. template <typename T>
  60. [[nodiscard]] T Read(GPUVAddr addr) const;
  61. template <typename T>
  62. void Write(GPUVAddr addr, T data);
  63. [[nodiscard]] u8* GetPointer(GPUVAddr addr);
  64. [[nodiscard]] const u8* GetPointer(GPUVAddr addr) const;
  65. /// Returns the number of bytes until the end of the memory map containing the given GPU address
  66. [[nodiscard]] size_t BytesToMapEnd(GPUVAddr gpu_addr) const noexcept;
  67. /**
  68. * ReadBlock and WriteBlock are full read and write operations over virtual
  69. * GPU Memory. It's important to use these when GPU memory may not be continuous
  70. * in the Host Memory counterpart. Note: This functions cause Host GPU Memory
  71. * Flushes and Invalidations, respectively to each operation.
  72. */
  73. void ReadBlock(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size) const;
  74. void WriteBlock(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size);
  75. void CopyBlock(GPUVAddr gpu_dest_addr, GPUVAddr gpu_src_addr, std::size_t size);
  76. /**
  77. * ReadBlockUnsafe and WriteBlockUnsafe are special versions of ReadBlock and
  78. * WriteBlock respectively. In this versions, no flushing or invalidation is actually
  79. * done and their performance is similar to a memcpy. This functions can be used
  80. * on either of this 2 scenarios instead of their safe counterpart:
  81. * - Memory which is sure to never be represented in the Host GPU.
  82. * - Memory Managed by a Cache Manager. Example: Texture Flushing should use
  83. * WriteBlockUnsafe instead of WriteBlock since it shouldn't invalidate the texture
  84. * being flushed.
  85. */
  86. void ReadBlockUnsafe(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size) const;
  87. void WriteBlockUnsafe(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size);
  88. /**
  89. * IsGranularRange checks if a gpu region can be simply read with a pointer.
  90. */
  91. [[nodiscard]] bool IsGranularRange(GPUVAddr gpu_addr, std::size_t size) const;
  92. [[nodiscard]] GPUVAddr Map(VAddr cpu_addr, GPUVAddr gpu_addr, std::size_t size);
  93. [[nodiscard]] GPUVAddr MapAllocate(VAddr cpu_addr, std::size_t size, std::size_t align);
  94. [[nodiscard]] GPUVAddr MapAllocate32(VAddr cpu_addr, std::size_t size);
  95. [[nodiscard]] std::optional<GPUVAddr> AllocateFixed(GPUVAddr gpu_addr, std::size_t size);
  96. [[nodiscard]] GPUVAddr Allocate(std::size_t size, std::size_t align);
  97. void Unmap(GPUVAddr gpu_addr, std::size_t size);
  98. private:
  99. [[nodiscard]] PageEntry GetPageEntry(GPUVAddr gpu_addr) const;
  100. void SetPageEntry(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size = page_size);
  101. GPUVAddr UpdateRange(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size);
  102. [[nodiscard]] std::optional<GPUVAddr> FindFreeRange(std::size_t size, std::size_t align,
  103. bool start_32bit_address = false) const;
  104. void TryLockPage(PageEntry page_entry, std::size_t size);
  105. void TryUnlockPage(PageEntry page_entry, std::size_t size);
  106. void FlushRegion(GPUVAddr gpu_addr, size_t size) const;
  107. [[nodiscard]] static constexpr std::size_t PageEntryIndex(GPUVAddr gpu_addr) {
  108. return (gpu_addr >> page_bits) & page_table_mask;
  109. }
  110. static constexpr u64 address_space_size = 1ULL << 40;
  111. static constexpr u64 address_space_start = 1ULL << 32;
  112. static constexpr u64 address_space_start_low = 1ULL << 16;
  113. static constexpr u64 page_bits{16};
  114. static constexpr u64 page_size{1 << page_bits};
  115. static constexpr u64 page_mask{page_size - 1};
  116. static constexpr u64 page_table_bits{24};
  117. static constexpr u64 page_table_size{1 << page_table_bits};
  118. static constexpr u64 page_table_mask{page_table_size - 1};
  119. Core::System& system;
  120. VideoCore::RasterizerInterface* rasterizer = nullptr;
  121. std::vector<PageEntry> page_table;
  122. using MapRange = std::pair<GPUVAddr, size_t>;
  123. std::vector<MapRange> map_ranges;
  124. };
  125. } // namespace Tegra