2018-02-08 03:54:35 +01:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-04-23 17:57:12 +02:00
|
|
|
#include "common/alignment.h"
|
2018-02-08 03:54:35 +01:00
|
|
|
#include "common/assert.h"
|
2018-11-01 03:20:37 +01:00
|
|
|
#include "common/logging/log.h"
|
2019-02-24 06:15:35 +01:00
|
|
|
#include "core/memory.h"
|
2018-02-12 05:44:12 +01:00
|
|
|
#include "video_core/memory_manager.h"
|
2018-02-08 03:54:35 +01:00
|
|
|
|
2018-02-12 05:44:12 +01:00
|
|
|
namespace Tegra {
|
2018-02-08 03:54:35 +01:00
|
|
|
|
2018-11-23 18:58:55 +01:00
|
|
|
MemoryManager::MemoryManager() {
|
|
|
|
// Mark the first page as reserved, so that 0 is not a valid GPUVAddr. Otherwise, games might
|
|
|
|
// try to use 0 as a valid address, which is also used to mean nullptr. This fixes a bug with
|
|
|
|
// Undertale using 0 for a render target.
|
|
|
|
PageSlot(0) = static_cast<u64>(PageStatus::Reserved);
|
|
|
|
}
|
|
|
|
|
2018-04-21 17:16:21 +02:00
|
|
|
GPUVAddr MemoryManager::AllocateSpace(u64 size, u64 align) {
|
2018-11-01 03:20:37 +01:00
|
|
|
const std::optional<GPUVAddr> gpu_addr{FindFreeBlock(0, size, align, PageStatus::Unmapped)};
|
2018-02-08 03:54:35 +01:00
|
|
|
|
2018-11-01 03:20:37 +01:00
|
|
|
ASSERT_MSG(gpu_addr, "unable to find available GPU memory");
|
|
|
|
|
|
|
|
for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
|
|
|
|
VAddr& slot{PageSlot(*gpu_addr + offset)};
|
2018-07-24 17:19:51 +02:00
|
|
|
|
|
|
|
ASSERT(slot == static_cast<u64>(PageStatus::Unmapped));
|
2018-11-01 03:20:37 +01:00
|
|
|
|
2018-07-24 17:19:51 +02:00
|
|
|
slot = static_cast<u64>(PageStatus::Allocated);
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
2018-04-21 17:16:21 +02:00
|
|
|
return *gpu_addr;
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
2018-04-21 17:16:21 +02:00
|
|
|
GPUVAddr MemoryManager::AllocateSpace(GPUVAddr gpu_addr, u64 size, u64 align) {
|
2018-11-01 03:20:37 +01:00
|
|
|
for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
|
|
|
|
VAddr& slot{PageSlot(gpu_addr + offset)};
|
2018-07-24 17:19:51 +02:00
|
|
|
|
|
|
|
ASSERT(slot == static_cast<u64>(PageStatus::Unmapped));
|
2018-11-01 03:20:37 +01:00
|
|
|
|
2018-07-24 17:19:51 +02:00
|
|
|
slot = static_cast<u64>(PageStatus::Allocated);
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
2018-04-21 17:16:21 +02:00
|
|
|
return gpu_addr;
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
2018-04-21 17:16:21 +02:00
|
|
|
GPUVAddr MemoryManager::MapBufferEx(VAddr cpu_addr, u64 size) {
|
2018-11-01 03:20:37 +01:00
|
|
|
const std::optional<GPUVAddr> gpu_addr{FindFreeBlock(0, size, PAGE_SIZE, PageStatus::Unmapped)};
|
|
|
|
|
|
|
|
ASSERT_MSG(gpu_addr, "unable to find available GPU memory");
|
2018-02-08 03:54:35 +01:00
|
|
|
|
2018-11-01 03:20:37 +01:00
|
|
|
for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
|
|
|
|
VAddr& slot{PageSlot(*gpu_addr + offset)};
|
2018-07-24 17:19:51 +02:00
|
|
|
|
|
|
|
ASSERT(slot == static_cast<u64>(PageStatus::Unmapped));
|
2018-11-01 03:20:37 +01:00
|
|
|
|
2018-07-24 17:19:51 +02:00
|
|
|
slot = cpu_addr + offset;
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
2018-11-01 03:20:37 +01:00
|
|
|
const MappedRegion region{cpu_addr, *gpu_addr, size};
|
2018-04-21 20:40:51 +02:00
|
|
|
mapped_regions.push_back(region);
|
|
|
|
|
2018-04-21 17:16:21 +02:00
|
|
|
return *gpu_addr;
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
2018-04-21 17:16:21 +02:00
|
|
|
GPUVAddr MemoryManager::MapBufferEx(VAddr cpu_addr, GPUVAddr gpu_addr, u64 size) {
|
|
|
|
ASSERT((gpu_addr & PAGE_MASK) == 0);
|
2018-02-08 03:54:35 +01:00
|
|
|
|
2018-11-01 03:20:37 +01:00
|
|
|
if (PageSlot(gpu_addr) != static_cast<u64>(PageStatus::Allocated)) {
|
|
|
|
// Page has been already mapped. In this case, we must find a new area of memory to use that
|
|
|
|
// is different than the specified one. Super Mario Odyssey hits this scenario when changing
|
|
|
|
// areas, but we do not want to overwrite the old pages.
|
|
|
|
// TODO(bunnei): We need to write a hardware test to confirm this behavior.
|
|
|
|
|
|
|
|
LOG_ERROR(HW_GPU, "attempting to map addr 0x{:016X}, which is not available!", gpu_addr);
|
|
|
|
|
|
|
|
const std::optional<GPUVAddr> new_gpu_addr{
|
|
|
|
FindFreeBlock(gpu_addr, size, PAGE_SIZE, PageStatus::Allocated)};
|
|
|
|
|
|
|
|
ASSERT_MSG(new_gpu_addr, "unable to find available GPU memory");
|
|
|
|
|
|
|
|
gpu_addr = *new_gpu_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
|
|
|
|
VAddr& slot{PageSlot(gpu_addr + offset)};
|
2018-07-24 17:19:51 +02:00
|
|
|
|
|
|
|
ASSERT(slot == static_cast<u64>(PageStatus::Allocated));
|
2018-11-01 03:20:37 +01:00
|
|
|
|
2018-07-24 17:19:51 +02:00
|
|
|
slot = cpu_addr + offset;
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
2018-11-01 03:20:37 +01:00
|
|
|
const MappedRegion region{cpu_addr, gpu_addr, size};
|
2018-04-21 20:40:51 +02:00
|
|
|
mapped_regions.push_back(region);
|
|
|
|
|
2018-04-21 17:16:21 +02:00
|
|
|
return gpu_addr;
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
2018-05-20 21:21:06 +02:00
|
|
|
GPUVAddr MemoryManager::UnmapBuffer(GPUVAddr gpu_addr, u64 size) {
|
|
|
|
ASSERT((gpu_addr & PAGE_MASK) == 0);
|
|
|
|
|
2018-11-01 03:20:37 +01:00
|
|
|
for (u64 offset{}; offset < size; offset += PAGE_SIZE) {
|
|
|
|
VAddr& slot{PageSlot(gpu_addr + offset)};
|
2018-07-24 17:19:51 +02:00
|
|
|
|
|
|
|
ASSERT(slot != static_cast<u64>(PageStatus::Allocated) &&
|
|
|
|
slot != static_cast<u64>(PageStatus::Unmapped));
|
2018-11-01 03:20:37 +01:00
|
|
|
|
2018-07-24 17:19:51 +02:00
|
|
|
slot = static_cast<u64>(PageStatus::Unmapped);
|
2018-05-20 21:21:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the region mappings that are contained within the unmapped region
|
|
|
|
mapped_regions.erase(std::remove_if(mapped_regions.begin(), mapped_regions.end(),
|
|
|
|
[&](const MappedRegion& region) {
|
|
|
|
return region.gpu_addr <= gpu_addr &&
|
|
|
|
region.gpu_addr + region.size < gpu_addr + size;
|
|
|
|
}),
|
|
|
|
mapped_regions.end());
|
|
|
|
return gpu_addr;
|
|
|
|
}
|
|
|
|
|
2018-10-13 03:52:16 +02:00
|
|
|
GPUVAddr MemoryManager::GetRegionEnd(GPUVAddr region_start) const {
|
|
|
|
for (const auto& region : mapped_regions) {
|
|
|
|
const GPUVAddr region_end{region.gpu_addr + region.size};
|
|
|
|
if (region_start >= region.gpu_addr && region_start < region_end) {
|
|
|
|
return region_end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-11-01 03:20:37 +01:00
|
|
|
std::optional<GPUVAddr> MemoryManager::FindFreeBlock(GPUVAddr region_start, u64 size, u64 align,
|
|
|
|
PageStatus status) {
|
|
|
|
GPUVAddr gpu_addr{region_start};
|
|
|
|
u64 free_space{};
|
2018-04-23 17:57:12 +02:00
|
|
|
align = (align + PAGE_MASK) & ~PAGE_MASK;
|
2018-02-08 03:54:35 +01:00
|
|
|
|
2018-04-21 17:16:21 +02:00
|
|
|
while (gpu_addr + free_space < MAX_ADDRESS) {
|
2018-11-01 03:20:37 +01:00
|
|
|
if (PageSlot(gpu_addr + free_space) == static_cast<u64>(status)) {
|
2018-04-23 17:57:12 +02:00
|
|
|
free_space += PAGE_SIZE;
|
2018-02-08 03:54:35 +01:00
|
|
|
if (free_space >= size) {
|
2018-04-21 17:16:21 +02:00
|
|
|
return gpu_addr;
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-04-21 17:16:21 +02:00
|
|
|
gpu_addr += free_space + PAGE_SIZE;
|
2018-02-08 03:54:35 +01:00
|
|
|
free_space = 0;
|
2018-04-21 17:16:21 +02:00
|
|
|
gpu_addr = Common::AlignUp(gpu_addr, align);
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
std::optional<VAddr> MemoryManager::GpuToCpuAddress(GPUVAddr gpu_addr) {
|
2018-11-01 03:20:37 +01:00
|
|
|
const VAddr base_addr{PageSlot(gpu_addr)};
|
2018-04-21 18:31:30 +02:00
|
|
|
|
2018-07-02 16:42:48 +02:00
|
|
|
if (base_addr == static_cast<u64>(PageStatus::Allocated) ||
|
2019-01-22 04:57:04 +01:00
|
|
|
base_addr == static_cast<u64>(PageStatus::Unmapped) ||
|
|
|
|
base_addr == static_cast<u64>(PageStatus::Reserved)) {
|
2018-04-21 18:31:30 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-04-21 17:16:21 +02:00
|
|
|
return base_addr + (gpu_addr & PAGE_MASK);
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 06:15:35 +01:00
|
|
|
u8 MemoryManager::Read8(GPUVAddr addr) {
|
|
|
|
return Memory::Read8(*GpuToCpuAddress(addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 MemoryManager::Read16(GPUVAddr addr) {
|
|
|
|
return Memory::Read16(*GpuToCpuAddress(addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 MemoryManager::Read32(GPUVAddr addr) {
|
|
|
|
return Memory::Read32(*GpuToCpuAddress(addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 MemoryManager::Read64(GPUVAddr addr) {
|
|
|
|
return Memory::Read64(*GpuToCpuAddress(addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::Write8(GPUVAddr addr, u8 data) {
|
|
|
|
Memory::Write8(*GpuToCpuAddress(addr), data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::Write16(GPUVAddr addr, u16 data) {
|
|
|
|
Memory::Write16(*GpuToCpuAddress(addr), data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::Write32(GPUVAddr addr, u32 data) {
|
|
|
|
Memory::Write32(*GpuToCpuAddress(addr), data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::Write64(GPUVAddr addr, u64 data) {
|
|
|
|
Memory::Write64(*GpuToCpuAddress(addr), data);
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* MemoryManager::GetPointer(GPUVAddr addr) {
|
|
|
|
return Memory::GetPointer(*GpuToCpuAddress(addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::ReadBlock(GPUVAddr src_addr, void* dest_buffer, std::size_t size) {
|
|
|
|
std::memcpy(dest_buffer, GetPointer(src_addr), size);
|
|
|
|
}
|
|
|
|
void MemoryManager::WriteBlock(GPUVAddr dest_addr, const void* src_buffer, std::size_t size) {
|
|
|
|
std::memcpy(GetPointer(dest_addr), src_buffer, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::CopyBlock(GPUVAddr dest_addr, GPUVAddr src_addr, std::size_t size) {
|
|
|
|
std::memcpy(GetPointer(dest_addr), GetPointer(src_addr), size);
|
2018-04-21 20:40:51 +02:00
|
|
|
}
|
|
|
|
|
2018-04-21 17:16:21 +02:00
|
|
|
VAddr& MemoryManager::PageSlot(GPUVAddr gpu_addr) {
|
2018-11-01 03:20:37 +01:00
|
|
|
auto& block{page_table[(gpu_addr >> (PAGE_BITS + PAGE_TABLE_BITS)) & PAGE_TABLE_MASK]};
|
2018-02-08 03:54:35 +01:00
|
|
|
if (!block) {
|
|
|
|
block = std::make_unique<PageBlock>();
|
2018-07-24 17:51:10 +02:00
|
|
|
block->fill(static_cast<VAddr>(PageStatus::Unmapped));
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
2018-04-21 17:16:21 +02:00
|
|
|
return (*block)[(gpu_addr >> PAGE_BITS) & PAGE_BLOCK_MASK];
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 05:44:12 +01:00
|
|
|
} // namespace Tegra
|