memory_manager.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/alignment.h"
  5. #include "common/assert.h"
  6. #include "common/logging/log.h"
  7. #include "core/memory.h"
  8. #include "video_core/memory_manager.h"
  9. namespace Tegra {
  10. MemoryManager::MemoryManager() {
  11. // Mark the first page as reserved, so that 0 is not a valid GPUVAddr. Otherwise, games might
  12. // try to use 0 as a valid address, which is also used to mean nullptr. This fixes a bug with
  13. // Undertale using 0 for a render target.
  14. PageSlot(0) = static_cast<u64>(PageStatus::Reserved);
  15. }
  16. GPUVAddr MemoryManager::AllocateSpace(u64 size, u64 align) {
  17. const std::optional<GPUVAddr> gpu_addr{FindFreeBlock(0, size, align, PageStatus::Unmapped)};
  18. ASSERT_MSG(gpu_addr, "unable to find available GPU memory");
  19. for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
  20. VAddr& slot{PageSlot(*gpu_addr + offset)};
  21. ASSERT(slot == static_cast<u64>(PageStatus::Unmapped));
  22. slot = static_cast<u64>(PageStatus::Allocated);
  23. }
  24. return *gpu_addr;
  25. }
  26. GPUVAddr MemoryManager::AllocateSpace(GPUVAddr gpu_addr, u64 size, u64 align) {
  27. for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
  28. VAddr& slot{PageSlot(gpu_addr + offset)};
  29. ASSERT(slot == static_cast<u64>(PageStatus::Unmapped));
  30. slot = static_cast<u64>(PageStatus::Allocated);
  31. }
  32. return gpu_addr;
  33. }
  34. GPUVAddr MemoryManager::MapBufferEx(VAddr cpu_addr, u64 size) {
  35. const std::optional<GPUVAddr> gpu_addr{FindFreeBlock(0, size, PAGE_SIZE, PageStatus::Unmapped)};
  36. ASSERT_MSG(gpu_addr, "unable to find available GPU memory");
  37. for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
  38. VAddr& slot{PageSlot(*gpu_addr + offset)};
  39. ASSERT(slot == static_cast<u64>(PageStatus::Unmapped));
  40. slot = cpu_addr + offset;
  41. }
  42. const MappedRegion region{cpu_addr, *gpu_addr, size};
  43. mapped_regions.push_back(region);
  44. return *gpu_addr;
  45. }
  46. GPUVAddr MemoryManager::MapBufferEx(VAddr cpu_addr, GPUVAddr gpu_addr, u64 size) {
  47. ASSERT((gpu_addr & PAGE_MASK) == 0);
  48. if (PageSlot(gpu_addr) != static_cast<u64>(PageStatus::Allocated)) {
  49. // Page has been already mapped. In this case, we must find a new area of memory to use that
  50. // is different than the specified one. Super Mario Odyssey hits this scenario when changing
  51. // areas, but we do not want to overwrite the old pages.
  52. // TODO(bunnei): We need to write a hardware test to confirm this behavior.
  53. LOG_ERROR(HW_GPU, "attempting to map addr 0x{:016X}, which is not available!", gpu_addr);
  54. const std::optional<GPUVAddr> new_gpu_addr{
  55. FindFreeBlock(gpu_addr, size, PAGE_SIZE, PageStatus::Allocated)};
  56. ASSERT_MSG(new_gpu_addr, "unable to find available GPU memory");
  57. gpu_addr = *new_gpu_addr;
  58. }
  59. for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
  60. VAddr& slot{PageSlot(gpu_addr + offset)};
  61. ASSERT(slot == static_cast<u64>(PageStatus::Allocated));
  62. slot = cpu_addr + offset;
  63. }
  64. const MappedRegion region{cpu_addr, gpu_addr, size};
  65. mapped_regions.push_back(region);
  66. return gpu_addr;
  67. }
  68. GPUVAddr MemoryManager::UnmapBuffer(GPUVAddr gpu_addr, u64 size) {
  69. ASSERT((gpu_addr & PAGE_MASK) == 0);
  70. for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
  71. VAddr& slot{PageSlot(gpu_addr + offset)};
  72. ASSERT(slot != static_cast<u64>(PageStatus::Allocated) &&
  73. slot != static_cast<u64>(PageStatus::Unmapped));
  74. slot = static_cast<u64>(PageStatus::Unmapped);
  75. }
  76. // Delete the region mappings that are contained within the unmapped region
  77. mapped_regions.erase(std::remove_if(mapped_regions.begin(), mapped_regions.end(),
  78. [&](const MappedRegion& region) {
  79. return region.gpu_addr <= gpu_addr &&
  80. region.gpu_addr + region.size < gpu_addr + size;
  81. }),
  82. mapped_regions.end());
  83. return gpu_addr;
  84. }
  85. GPUVAddr MemoryManager::GetRegionEnd(GPUVAddr region_start) const {
  86. for (const auto& region : mapped_regions) {
  87. const GPUVAddr region_end{region.gpu_addr + region.size};
  88. if (region_start >= region.gpu_addr && region_start < region_end) {
  89. return region_end;
  90. }
  91. }
  92. return {};
  93. }
  94. std::optional<GPUVAddr> MemoryManager::FindFreeBlock(GPUVAddr region_start, u64 size, u64 align,
  95. PageStatus status) {
  96. GPUVAddr gpu_addr{region_start};
  97. u64 free_space{};
  98. align = (align + PAGE_MASK) & ~PAGE_MASK;
  99. while (gpu_addr + free_space < MAX_ADDRESS) {
  100. if (PageSlot(gpu_addr + free_space) == static_cast<u64>(status)) {
  101. free_space += PAGE_SIZE;
  102. if (free_space >= size) {
  103. return gpu_addr;
  104. }
  105. } else {
  106. gpu_addr += free_space + PAGE_SIZE;
  107. free_space = 0;
  108. gpu_addr = Common::AlignUp(gpu_addr, align);
  109. }
  110. }
  111. return {};
  112. }
  113. std::optional<VAddr> MemoryManager::GpuToCpuAddress(GPUVAddr gpu_addr) {
  114. const VAddr base_addr{PageSlot(gpu_addr)};
  115. if (base_addr == static_cast<u64>(PageStatus::Allocated) ||
  116. base_addr == static_cast<u64>(PageStatus::Unmapped) ||
  117. base_addr == static_cast<u64>(PageStatus::Reserved)) {
  118. return {};
  119. }
  120. return base_addr + (gpu_addr & PAGE_MASK);
  121. }
  122. u8 MemoryManager::Read8(GPUVAddr addr) {
  123. return Memory::Read8(*GpuToCpuAddress(addr));
  124. }
  125. u16 MemoryManager::Read16(GPUVAddr addr) {
  126. return Memory::Read16(*GpuToCpuAddress(addr));
  127. }
  128. u32 MemoryManager::Read32(GPUVAddr addr) {
  129. return Memory::Read32(*GpuToCpuAddress(addr));
  130. }
  131. u64 MemoryManager::Read64(GPUVAddr addr) {
  132. return Memory::Read64(*GpuToCpuAddress(addr));
  133. }
  134. void MemoryManager::Write8(GPUVAddr addr, u8 data) {
  135. Memory::Write8(*GpuToCpuAddress(addr), data);
  136. }
  137. void MemoryManager::Write16(GPUVAddr addr, u16 data) {
  138. Memory::Write16(*GpuToCpuAddress(addr), data);
  139. }
  140. void MemoryManager::Write32(GPUVAddr addr, u32 data) {
  141. Memory::Write32(*GpuToCpuAddress(addr), data);
  142. }
  143. void MemoryManager::Write64(GPUVAddr addr, u64 data) {
  144. Memory::Write64(*GpuToCpuAddress(addr), data);
  145. }
  146. u8* MemoryManager::GetPointer(GPUVAddr addr) {
  147. return Memory::GetPointer(*GpuToCpuAddress(addr));
  148. }
  149. void MemoryManager::ReadBlock(GPUVAddr src_addr, void* dest_buffer, std::size_t size) {
  150. std::memcpy(dest_buffer, GetPointer(src_addr), size);
  151. }
  152. void MemoryManager::WriteBlock(GPUVAddr dest_addr, const void* src_buffer, std::size_t size) {
  153. std::memcpy(GetPointer(dest_addr), src_buffer, size);
  154. }
  155. void MemoryManager::CopyBlock(GPUVAddr dest_addr, GPUVAddr src_addr, std::size_t size) {
  156. std::memcpy(GetPointer(dest_addr), GetPointer(src_addr), size);
  157. }
  158. VAddr& MemoryManager::PageSlot(GPUVAddr gpu_addr) {
  159. auto& block{page_table[(gpu_addr >> (PAGE_BITS + PAGE_TABLE_BITS)) & PAGE_TABLE_MASK]};
  160. if (!block) {
  161. block = std::make_unique<PageBlock>();
  162. block->fill(static_cast<VAddr>(PageStatus::Unmapped));
  163. }
  164. return (*block)[(gpu_addr >> PAGE_BITS) & PAGE_BLOCK_MASK];
  165. }
  166. } // namespace Tegra