memory_manager.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. constexpr bool IsUnmapped() const {
  26. return state == State::Unmapped;
  27. }
  28. constexpr bool IsAllocated() const {
  29. return state == State::Allocated;
  30. }
  31. constexpr bool IsValid() const {
  32. return !IsUnmapped() && !IsAllocated();
  33. }
  34. constexpr VAddr ToAddress() const {
  35. if (!IsValid()) {
  36. return {};
  37. }
  38. return static_cast<VAddr>(state) << ShiftBits;
  39. }
  40. constexpr PageEntry operator+(u64 offset) {
  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, VideoCore::RasterizerInterface& rasterizer);
  55. ~MemoryManager();
  56. std::optional<VAddr> GpuToCpuAddress(GPUVAddr addr) const;
  57. template <typename T>
  58. T Read(GPUVAddr addr) const;
  59. template <typename T>
  60. void Write(GPUVAddr addr, T data);
  61. u8* GetPointer(GPUVAddr addr);
  62. const u8* GetPointer(GPUVAddr addr) const;
  63. /**
  64. * ReadBlock and WriteBlock are full read and write operations over virtual
  65. * GPU Memory. It's important to use these when GPU memory may not be continuous
  66. * in the Host Memory counterpart. Note: This functions cause Host GPU Memory
  67. * Flushes and Invalidations, respectively to each operation.
  68. */
  69. void ReadBlock(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size) const;
  70. void WriteBlock(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size);
  71. void CopyBlock(GPUVAddr gpu_dest_addr, GPUVAddr gpu_src_addr, std::size_t size);
  72. /**
  73. * ReadBlockUnsafe and WriteBlockUnsafe are special versions of ReadBlock and
  74. * WriteBlock respectively. In this versions, no flushing or invalidation is actually
  75. * done and their performance is similar to a memcpy. This functions can be used
  76. * on either of this 2 scenarios instead of their safe counterpart:
  77. * - Memory which is sure to never be represented in the Host GPU.
  78. * - Memory Managed by a Cache Manager. Example: Texture Flushing should use
  79. * WriteBlockUnsafe instead of WriteBlock since it shouldn't invalidate the texture
  80. * being flushed.
  81. */
  82. void ReadBlockUnsafe(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size) const;
  83. void WriteBlockUnsafe(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size);
  84. void CopyBlockUnsafe(GPUVAddr gpu_dest_addr, GPUVAddr gpu_src_addr, std::size_t size);
  85. /**
  86. * IsGranularRange checks if a gpu region can be simply read with a pointer.
  87. */
  88. bool IsGranularRange(GPUVAddr gpu_addr, std::size_t size);
  89. GPUVAddr Map(VAddr cpu_addr, GPUVAddr gpu_addr, std::size_t size);
  90. GPUVAddr MapAllocate(VAddr cpu_addr, std::size_t size, std::size_t align);
  91. std::optional<GPUVAddr> AllocateFixed(GPUVAddr gpu_addr, std::size_t size);
  92. GPUVAddr Allocate(std::size_t size, std::size_t align);
  93. void Unmap(GPUVAddr gpu_addr, std::size_t size);
  94. private:
  95. PageEntry GetPageEntry(GPUVAddr gpu_addr) const;
  96. void SetPageEntry(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size = page_size);
  97. GPUVAddr UpdateRange(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size);
  98. std::optional<GPUVAddr> FindFreeRange(std::size_t size, std::size_t align) const;
  99. void TryLockPage(PageEntry page_entry, std::size_t size);
  100. void TryUnlockPage(PageEntry page_entry, std::size_t size);
  101. static constexpr std::size_t PageEntryIndex(GPUVAddr gpu_addr) {
  102. return (gpu_addr >> page_bits) & page_table_mask;
  103. }
  104. static constexpr u64 address_space_size = 1ULL << 40;
  105. static constexpr u64 address_space_start = 1ULL << 32;
  106. static constexpr u64 page_bits{16};
  107. static constexpr u64 page_size{1 << page_bits};
  108. static constexpr u64 page_mask{page_size - 1};
  109. static constexpr u64 page_table_bits{24};
  110. static constexpr u64 page_table_size{1 << page_table_bits};
  111. static constexpr u64 page_table_mask{page_table_size - 1};
  112. Core::System& system;
  113. VideoCore::RasterizerInterface& rasterizer;
  114. std::vector<PageEntry> page_table;
  115. };
  116. } // namespace Tegra