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
|
|
|
|
|
|
|
PAddr MemoryManager::AllocateSpace(u64 size, u64 align) {
|
|
|
|
boost::optional<PAddr> paddr = FindFreeBlock(size, align);
|
|
|
|
ASSERT(paddr);
|
|
|
|
|
2018-04-23 15:57:12 +00:00
|
|
|
for (u64 offset = 0; offset < size; offset += PAGE_SIZE) {
|
|
|
|
ASSERT(PageSlot(*paddr + offset) == static_cast<u64>(PageStatus::Unmapped));
|
2018-02-08 02:54:35 +00:00
|
|
|
PageSlot(*paddr + offset) = static_cast<u64>(PageStatus::Allocated);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *paddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PAddr MemoryManager::AllocateSpace(PAddr paddr, u64 size, u64 align) {
|
2018-04-23 15:57:12 +00:00
|
|
|
for (u64 offset = 0; offset < size; offset += PAGE_SIZE) {
|
|
|
|
ASSERT(PageSlot(paddr + offset) == static_cast<u64>(PageStatus::Unmapped));
|
2018-02-08 02:54:35 +00:00
|
|
|
PageSlot(paddr + offset) = static_cast<u64>(PageStatus::Allocated);
|
|
|
|
}
|
|
|
|
|
|
|
|
return paddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PAddr MemoryManager::MapBufferEx(VAddr vaddr, u64 size) {
|
2018-04-23 15:57:12 +00:00
|
|
|
boost::optional<PAddr> paddr = FindFreeBlock(size, PAGE_SIZE);
|
2018-02-08 02:54:35 +00:00
|
|
|
ASSERT(paddr);
|
|
|
|
|
2018-04-23 15:57:12 +00:00
|
|
|
for (u64 offset = 0; offset < size; offset += PAGE_SIZE) {
|
|
|
|
ASSERT(PageSlot(*paddr + offset) == static_cast<u64>(PageStatus::Unmapped));
|
2018-02-08 02:54:35 +00:00
|
|
|
PageSlot(*paddr + offset) = vaddr + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *paddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PAddr MemoryManager::MapBufferEx(VAddr vaddr, PAddr paddr, u64 size) {
|
2018-04-23 15:57:12 +00:00
|
|
|
ASSERT((paddr & 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) {
|
|
|
|
ASSERT(PageSlot(paddr + offset) == static_cast<u64>(PageStatus::Allocated));
|
2018-02-08 02:54:35 +00:00
|
|
|
PageSlot(paddr + offset) = vaddr + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return paddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<PAddr> MemoryManager::FindFreeBlock(u64 size, u64 align) {
|
2018-04-23 15:57:12 +00:00
|
|
|
PAddr paddr = 0;
|
|
|
|
u64 free_space = 0;
|
|
|
|
align = (align + PAGE_MASK) & ~PAGE_MASK;
|
2018-02-08 02:54:35 +00:00
|
|
|
|
|
|
|
while (paddr + free_space < MAX_ADDRESS) {
|
|
|
|
if (!IsPageMapped(paddr + 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) {
|
|
|
|
return paddr;
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-23 15:57:12 +00:00
|
|
|
paddr += free_space + PAGE_SIZE;
|
2018-02-08 02:54:35 +00:00
|
|
|
free_space = 0;
|
2018-04-23 15:57:12 +00:00
|
|
|
paddr = Common::AlignUp(paddr, align);
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr MemoryManager::PhysicalToVirtualAddress(PAddr paddr) {
|
|
|
|
VAddr base_addr = PageSlot(paddr);
|
|
|
|
ASSERT(base_addr != static_cast<u64>(PageStatus::Unmapped));
|
2018-04-23 15:57:12 +00:00
|
|
|
return base_addr + (paddr & PAGE_MASK);
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MemoryManager::IsPageMapped(PAddr paddr) {
|
|
|
|
return PageSlot(paddr) != static_cast<u64>(PageStatus::Unmapped);
|
|
|
|
}
|
|
|
|
|
|
|
|
VAddr& MemoryManager::PageSlot(PAddr paddr) {
|
2018-04-23 15:57:12 +00:00
|
|
|
auto& block = page_table[(paddr >> (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-23 15:57:12 +00:00
|
|
|
return (*block)[(paddr >> PAGE_BITS) & PAGE_BLOCK_MASK];
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 04:44:12 +00:00
|
|
|
} // namespace Tegra
|