memory_manager.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "video_core/memory_manager.h"
  8. namespace Tegra {
  9. MemoryManager::MemoryManager() {
  10. // Mark the first page as reserved, so that 0 is not a valid GPUVAddr. Otherwise, games might
  11. // try to use 0 as a valid address, which is also used to mean nullptr. This fixes a bug with
  12. // Undertale using 0 for a render target.
  13. PageSlot(0) = static_cast<u64>(PageStatus::Reserved);
  14. }
  15. GPUVAddr MemoryManager::AllocateSpace(u64 size, u64 align) {
  16. const std::optional<GPUVAddr> gpu_addr{FindFreeBlock(0, size, align, PageStatus::Unmapped)};
  17. ASSERT_MSG(gpu_addr, "unable to find available GPU memory");
  18. for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
  19. VAddr& slot{PageSlot(*gpu_addr + offset)};
  20. ASSERT(slot == static_cast<u64>(PageStatus::Unmapped));
  21. slot = static_cast<u64>(PageStatus::Allocated);
  22. }
  23. return *gpu_addr;
  24. }
  25. GPUVAddr MemoryManager::AllocateSpace(GPUVAddr gpu_addr, u64 size, u64 align) {
  26. for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
  27. VAddr& slot{PageSlot(gpu_addr + offset)};
  28. ASSERT(slot == static_cast<u64>(PageStatus::Unmapped));
  29. slot = static_cast<u64>(PageStatus::Allocated);
  30. }
  31. return gpu_addr;
  32. }
  33. GPUVAddr MemoryManager::MapBufferEx(VAddr cpu_addr, u64 size) {
  34. const std::optional<GPUVAddr> gpu_addr{FindFreeBlock(0, size, PAGE_SIZE, PageStatus::Unmapped)};
  35. ASSERT_MSG(gpu_addr, "unable to find available GPU memory");
  36. for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
  37. VAddr& slot{PageSlot(*gpu_addr + offset)};
  38. ASSERT(slot == static_cast<u64>(PageStatus::Unmapped));
  39. slot = cpu_addr + offset;
  40. }
  41. const MappedRegion region{cpu_addr, *gpu_addr, size};
  42. mapped_regions.push_back(region);
  43. return *gpu_addr;
  44. }
  45. GPUVAddr MemoryManager::MapBufferEx(VAddr cpu_addr, GPUVAddr gpu_addr, u64 size) {
  46. ASSERT((gpu_addr & PAGE_MASK) == 0);
  47. if (PageSlot(gpu_addr) != static_cast<u64>(PageStatus::Allocated)) {
  48. // Page has been already mapped. In this case, we must find a new area of memory to use that
  49. // is different than the specified one. Super Mario Odyssey hits this scenario when changing
  50. // areas, but we do not want to overwrite the old pages.
  51. // TODO(bunnei): We need to write a hardware test to confirm this behavior.
  52. LOG_ERROR(HW_GPU, "attempting to map addr 0x{:016X}, which is not available!", gpu_addr);
  53. const std::optional<GPUVAddr> new_gpu_addr{
  54. FindFreeBlock(gpu_addr, size, PAGE_SIZE, PageStatus::Allocated)};
  55. ASSERT_MSG(new_gpu_addr, "unable to find available GPU memory");
  56. gpu_addr = *new_gpu_addr;
  57. }
  58. for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
  59. VAddr& slot{PageSlot(gpu_addr + offset)};
  60. ASSERT(slot == static_cast<u64>(PageStatus::Allocated));
  61. slot = cpu_addr + offset;
  62. }
  63. const MappedRegion region{cpu_addr, gpu_addr, size};
  64. mapped_regions.push_back(region);
  65. return gpu_addr;
  66. }
  67. GPUVAddr MemoryManager::UnmapBuffer(GPUVAddr gpu_addr, u64 size) {
  68. ASSERT((gpu_addr & PAGE_MASK) == 0);
  69. for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
  70. VAddr& slot{PageSlot(gpu_addr + offset)};
  71. ASSERT(slot != static_cast<u64>(PageStatus::Allocated) &&
  72. slot != static_cast<u64>(PageStatus::Unmapped));
  73. slot = static_cast<u64>(PageStatus::Unmapped);
  74. }
  75. // Delete the region mappings that are contained within the unmapped region
  76. mapped_regions.erase(std::remove_if(mapped_regions.begin(), mapped_regions.end(),
  77. [&](const MappedRegion& region) {
  78. return region.gpu_addr <= gpu_addr &&
  79. region.gpu_addr + region.size < gpu_addr + size;
  80. }),
  81. mapped_regions.end());
  82. return gpu_addr;
  83. }
  84. GPUVAddr MemoryManager::GetRegionEnd(GPUVAddr region_start) const {
  85. for (const auto& region : mapped_regions) {
  86. const GPUVAddr region_end{region.gpu_addr + region.size};
  87. if (region_start >= region.gpu_addr && region_start < region_end) {
  88. return region_end;
  89. }
  90. }
  91. return {};
  92. }
  93. std::optional<GPUVAddr> MemoryManager::FindFreeBlock(GPUVAddr region_start, u64 size, u64 align,
  94. PageStatus status) {
  95. GPUVAddr gpu_addr{region_start};
  96. u64 free_space{};
  97. align = (align + PAGE_MASK) & ~PAGE_MASK;
  98. while (gpu_addr + free_space < MAX_ADDRESS) {
  99. if (PageSlot(gpu_addr + free_space) == static_cast<u64>(status)) {
  100. free_space += PAGE_SIZE;
  101. if (free_space >= size) {
  102. return gpu_addr;
  103. }
  104. } else {
  105. gpu_addr += free_space + PAGE_SIZE;
  106. free_space = 0;
  107. gpu_addr = Common::AlignUp(gpu_addr, align);
  108. }
  109. }
  110. return {};
  111. }
  112. std::optional<VAddr> MemoryManager::GpuToCpuAddress(GPUVAddr gpu_addr) {
  113. const VAddr base_addr{PageSlot(gpu_addr)};
  114. if (base_addr == static_cast<u64>(PageStatus::Allocated) ||
  115. base_addr == static_cast<u64>(PageStatus::Unmapped)) {
  116. return {};
  117. }
  118. return base_addr + (gpu_addr & PAGE_MASK);
  119. }
  120. std::vector<GPUVAddr> MemoryManager::CpuToGpuAddress(VAddr cpu_addr) const {
  121. std::vector<GPUVAddr> results;
  122. for (const auto& region : mapped_regions) {
  123. if (cpu_addr >= region.cpu_addr && cpu_addr < (region.cpu_addr + region.size)) {
  124. const u64 offset{cpu_addr - region.cpu_addr};
  125. results.push_back(region.gpu_addr + offset);
  126. }
  127. }
  128. return results;
  129. }
  130. VAddr& MemoryManager::PageSlot(GPUVAddr gpu_addr) {
  131. auto& block{page_table[(gpu_addr >> (PAGE_BITS + PAGE_TABLE_BITS)) & PAGE_TABLE_MASK]};
  132. if (!block) {
  133. block = std::make_unique<PageBlock>();
  134. block->fill(static_cast<VAddr>(PageStatus::Unmapped));
  135. }
  136. return (*block)[(gpu_addr >> PAGE_BITS) & PAGE_BLOCK_MASK];
  137. }
  138. } // namespace Tegra