2018-02-08 02:54:35 +00:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-04-23 15:57:12 +00:00
|
|
|
#include "common/alignment.h"
|
2018-02-08 02:54:35 +00:00
|
|
|
#include "common/assert.h"
|
2018-02-12 04:44:12 +00:00
|
|
|
#include "video_core/memory_manager.h"
|
2018-02-08 02:54:35 +00:00
|
|
|
|
2018-02-12 04:44:12 +00:00
|
|
|
namespace Tegra {
|
2018-02-08 02:54:35 +00:00
|
|
|
|
2018-04-21 15:16:21 +00:00
|
|
|
GPUVAddr MemoryManager::AllocateSpace(u64 size, u64 align) {
|
|
|
|
boost::optional<GPUVAddr> gpu_addr = FindFreeBlock(size, align);
|
|
|
|
ASSERT(gpu_addr);
|
2018-02-08 02:54:35 +00:00
|
|
|
|
2018-04-23 15:57:12 +00:00
|
|
|
for (u64 offset = 0; offset < size; offset += PAGE_SIZE) {
|
2018-04-21 15:16:21 +00:00
|
|
|
ASSERT(PageSlot(*gpu_addr + offset) == static_cast<u64>(PageStatus::Unmapped));
|
|
|
|
PageSlot(*gpu_addr + offset) = static_cast<u64>(PageStatus::Allocated);
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:21 +00:00
|
|
|
return *gpu_addr;
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:21 +00:00
|
|
|
GPUVAddr MemoryManager::AllocateSpace(GPUVAddr gpu_addr, u64 size, u64 align) {
|
2018-04-23 15:57:12 +00:00
|
|
|
for (u64 offset = 0; offset < size; offset += PAGE_SIZE) {
|
2018-04-21 15:16:21 +00:00
|
|
|
ASSERT(PageSlot(gpu_addr + offset) == static_cast<u64>(PageStatus::Unmapped));
|
|
|
|
PageSlot(gpu_addr + offset) = static_cast<u64>(PageStatus::Allocated);
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:21 +00:00
|
|
|
return gpu_addr;
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:21 +00:00
|
|
|
GPUVAddr MemoryManager::MapBufferEx(VAddr cpu_addr, u64 size) {
|
|
|
|
boost::optional<GPUVAddr> gpu_addr = FindFreeBlock(size, PAGE_SIZE);
|
|
|
|
ASSERT(gpu_addr);
|
2018-02-08 02:54:35 +00:00
|
|
|
|
2018-04-23 15:57:12 +00:00
|
|
|
for (u64 offset = 0; offset < size; offset += PAGE_SIZE) {
|
2018-04-21 15:16:21 +00:00
|
|
|
ASSERT(PageSlot(*gpu_addr + offset) == static_cast<u64>(PageStatus::Unmapped));
|
|
|
|
PageSlot(*gpu_addr + offset) = cpu_addr + offset;
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:21 +00:00
|
|
|
return *gpu_addr;
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:21 +00:00
|
|
|
GPUVAddr MemoryManager::MapBufferEx(VAddr cpu_addr, GPUVAddr gpu_addr, u64 size) {
|
|
|
|
ASSERT((gpu_addr & PAGE_MASK) == 0);
|
2018-02-08 02:54:35 +00:00
|
|
|
|
2018-04-23 15:57:12 +00:00
|
|
|
for (u64 offset = 0; offset < size; offset += PAGE_SIZE) {
|
2018-04-21 15:16:21 +00:00
|
|
|
ASSERT(PageSlot(gpu_addr + offset) == static_cast<u64>(PageStatus::Allocated));
|
|
|
|
PageSlot(gpu_addr + offset) = cpu_addr + offset;
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:21 +00:00
|
|
|
return gpu_addr;
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:21 +00:00
|
|
|
boost::optional<GPUVAddr> MemoryManager::FindFreeBlock(u64 size, u64 align) {
|
|
|
|
GPUVAddr gpu_addr = 0;
|
2018-04-23 15:57:12 +00:00
|
|
|
u64 free_space = 0;
|
|
|
|
align = (align + PAGE_MASK) & ~PAGE_MASK;
|
2018-02-08 02:54:35 +00:00
|
|
|
|
2018-04-21 15:16:21 +00:00
|
|
|
while (gpu_addr + free_space < MAX_ADDRESS) {
|
|
|
|
if (!IsPageMapped(gpu_addr + free_space)) {
|
2018-04-23 15:57:12 +00:00
|
|
|
free_space += PAGE_SIZE;
|
2018-02-08 02:54:35 +00:00
|
|
|
if (free_space >= size) {
|
2018-04-21 15:16:21 +00:00
|
|
|
return gpu_addr;
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-04-21 15:16:21 +00:00
|
|
|
gpu_addr += free_space + PAGE_SIZE;
|
2018-02-08 02:54:35 +00:00
|
|
|
free_space = 0;
|
2018-04-21 15:16:21 +00:00
|
|
|
gpu_addr = Common::AlignUp(gpu_addr, align);
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-04-21 16:31:30 +00:00
|
|
|
boost::optional<VAddr> MemoryManager::GpuToCpuAddress(GPUVAddr gpu_addr) {
|
2018-04-21 15:16:21 +00:00
|
|
|
VAddr base_addr = PageSlot(gpu_addr);
|
2018-02-08 02:54:35 +00:00
|
|
|
ASSERT(base_addr != static_cast<u64>(PageStatus::Unmapped));
|
2018-04-21 16:31:30 +00:00
|
|
|
|
|
|
|
if (base_addr == static_cast<u64>(PageStatus::Allocated)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:21 +00:00
|
|
|
return base_addr + (gpu_addr & PAGE_MASK);
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:21 +00:00
|
|
|
bool MemoryManager::IsPageMapped(GPUVAddr gpu_addr) {
|
|
|
|
return PageSlot(gpu_addr) != static_cast<u64>(PageStatus::Unmapped);
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 15:16:21 +00:00
|
|
|
VAddr& MemoryManager::PageSlot(GPUVAddr gpu_addr) {
|
|
|
|
auto& block = page_table[(gpu_addr >> (PAGE_BITS + PAGE_TABLE_BITS)) & PAGE_TABLE_MASK];
|
2018-02-08 02:54:35 +00:00
|
|
|
if (!block) {
|
|
|
|
block = std::make_unique<PageBlock>();
|
|
|
|
for (unsigned index = 0; index < PAGE_BLOCK_SIZE; index++) {
|
|
|
|
(*block)[index] = static_cast<u64>(PageStatus::Unmapped);
|
|
|
|
}
|
|
|
|
}
|
2018-04-21 15:16:21 +00:00
|
|
|
return (*block)[(gpu_addr >> PAGE_BITS) & PAGE_BLOCK_MASK];
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 04:44:12 +00:00
|
|
|
} // namespace Tegra
|