2018-02-07 18:54:35 -08:00
|
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
|
#include <memory>
|
2018-04-21 11:40:51 -07:00
|
|
|
|
#include <vector>
|
2018-04-21 09:31:30 -07:00
|
|
|
|
|
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
|
2018-02-07 18:54:35 -08:00
|
|
|
|
#include "common/common_types.h"
|
|
|
|
|
|
2018-02-11 20:44:12 -08:00
|
|
|
|
namespace Tegra {
|
|
|
|
|
|
|
|
|
|
/// Virtual addresses in the GPU's memory map are 64 bit.
|
|
|
|
|
using GPUVAddr = u64;
|
2018-02-07 18:54:35 -08:00
|
|
|
|
|
|
|
|
|
class MemoryManager final {
|
|
|
|
|
public:
|
|
|
|
|
MemoryManager() = default;
|
|
|
|
|
|
2018-04-21 08:16:21 -07:00
|
|
|
|
GPUVAddr AllocateSpace(u64 size, u64 align);
|
|
|
|
|
GPUVAddr AllocateSpace(GPUVAddr gpu_addr, u64 size, u64 align);
|
|
|
|
|
GPUVAddr MapBufferEx(VAddr cpu_addr, u64 size);
|
|
|
|
|
GPUVAddr MapBufferEx(VAddr cpu_addr, GPUVAddr gpu_addr, u64 size);
|
2018-05-20 12:21:06 -07:00
|
|
|
|
GPUVAddr UnmapBuffer(GPUVAddr gpu_addr, u64 size);
|
2018-10-12 18:52:16 -07:00
|
|
|
|
GPUVAddr GetRegionEnd(GPUVAddr region_start) const;
|
2018-04-21 09:31:30 -07:00
|
|
|
|
boost::optional<VAddr> GpuToCpuAddress(GPUVAddr gpu_addr);
|
2018-04-21 11:40:51 -07:00
|
|
|
|
std::vector<GPUVAddr> CpuToGpuAddress(VAddr cpu_addr) const;
|
2018-02-07 18:54:35 -08:00
|
|
|
|
|
2018-04-23 08:57:12 -07:00
|
|
|
|
static constexpr u64 PAGE_BITS = 16;
|
|
|
|
|
static constexpr u64 PAGE_SIZE = 1 << PAGE_BITS;
|
|
|
|
|
static constexpr u64 PAGE_MASK = PAGE_SIZE - 1;
|
|
|
|
|
|
2018-02-07 18:54:35 -08:00
|
|
|
|
private:
|
2018-04-21 08:16:21 -07:00
|
|
|
|
boost::optional<GPUVAddr> FindFreeBlock(u64 size, u64 align = 1);
|
|
|
|
|
bool IsPageMapped(GPUVAddr gpu_addr);
|
|
|
|
|
VAddr& PageSlot(GPUVAddr gpu_addr);
|
2018-02-07 18:54:35 -08:00
|
|
|
|
|
|
|
|
|
enum class PageStatus : u64 {
|
|
|
|
|
Unmapped = 0xFFFFFFFFFFFFFFFFULL,
|
|
|
|
|
Allocated = 0xFFFFFFFFFFFFFFFEULL,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static constexpr u64 MAX_ADDRESS{0x10000000000ULL};
|
2018-04-23 08:57:12 -07:00
|
|
|
|
static constexpr u64 PAGE_TABLE_BITS{10};
|
2018-02-07 18:54:35 -08:00
|
|
|
|
static constexpr u64 PAGE_TABLE_SIZE{1 << PAGE_TABLE_BITS};
|
|
|
|
|
static constexpr u64 PAGE_TABLE_MASK{PAGE_TABLE_SIZE - 1};
|
|
|
|
|
static constexpr u64 PAGE_BLOCK_BITS{14};
|
|
|
|
|
static constexpr u64 PAGE_BLOCK_SIZE{1 << PAGE_BLOCK_BITS};
|
|
|
|
|
static constexpr u64 PAGE_BLOCK_MASK{PAGE_BLOCK_SIZE - 1};
|
|
|
|
|
|
|
|
|
|
using PageBlock = std::array<VAddr, PAGE_BLOCK_SIZE>;
|
|
|
|
|
std::array<std::unique_ptr<PageBlock>, PAGE_TABLE_SIZE> page_table{};
|
2018-04-21 11:40:51 -07:00
|
|
|
|
|
|
|
|
|
struct MappedRegion {
|
|
|
|
|
VAddr cpu_addr;
|
|
|
|
|
GPUVAddr gpu_addr;
|
|
|
|
|
u64 size;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<MappedRegion> mapped_regions;
|
2018-02-07 18:54:35 -08:00
|
|
|
|
};
|
|
|
|
|
|
2018-02-11 20:44:12 -08:00
|
|
|
|
} // namespace Tegra
|