2019-03-04 04:54:16 +00:00
|
|
|
// Copyright 2018 yuzu emulator team
|
2018-02-08 02:54:35 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-03-04 04:54:16 +00:00
|
|
|
#include <map>
|
2018-10-30 04:03:25 +00:00
|
|
|
#include <optional>
|
2020-07-26 04:16:21 +00:00
|
|
|
#include <vector>
|
2018-04-21 16:31:30 +00:00
|
|
|
|
2018-02-08 02:54:35 +00:00
|
|
|
#include "common/common_types.h"
|
|
|
|
|
2020-02-15 22:47:15 +00:00
|
|
|
namespace VideoCore {
|
|
|
|
class RasterizerInterface;
|
|
|
|
}
|
|
|
|
|
2019-07-09 06:17:44 +00:00
|
|
|
namespace Core {
|
|
|
|
class System;
|
|
|
|
}
|
|
|
|
|
2018-02-12 04:44:12 +00:00
|
|
|
namespace Tegra {
|
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
class PageEntry final {
|
|
|
|
public:
|
|
|
|
enum class State : u32 {
|
|
|
|
Unmapped = static_cast<u32>(-1),
|
|
|
|
Allocated = static_cast<u32>(-2),
|
2019-03-04 04:54:16 +00:00
|
|
|
};
|
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
constexpr PageEntry() = default;
|
|
|
|
constexpr PageEntry(State state) : state{state} {}
|
|
|
|
constexpr PageEntry(VAddr addr) : state{static_cast<State>(addr >> ShiftBits)} {}
|
|
|
|
|
|
|
|
constexpr bool IsUnmapped() const {
|
|
|
|
return state == State::Unmapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool IsAllocated() const {
|
|
|
|
return state == State::Allocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool IsValid() const {
|
|
|
|
return !IsUnmapped() && !IsAllocated();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr VAddr ToAddress() const {
|
|
|
|
if (!IsValid()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<VAddr>(state) << ShiftBits;
|
|
|
|
}
|
|
|
|
|
2020-08-27 00:11:56 +00:00
|
|
|
constexpr PageEntry operator+(u64 offset) const {
|
2020-07-26 04:16:21 +00:00
|
|
|
// If this is a reserved value, offsets do not apply
|
|
|
|
if (!IsValid()) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
return PageEntry{(static_cast<VAddr>(state) << ShiftBits) + offset};
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr std::size_t ShiftBits{12};
|
|
|
|
|
|
|
|
State state{State::Unmapped};
|
2019-03-04 04:54:16 +00:00
|
|
|
};
|
2020-07-26 04:16:21 +00:00
|
|
|
static_assert(sizeof(PageEntry) == 4, "PageEntry is too large");
|
2019-03-04 04:54:16 +00:00
|
|
|
|
2018-02-08 02:54:35 +00:00
|
|
|
class MemoryManager final {
|
|
|
|
public:
|
2020-06-11 03:58:57 +00:00
|
|
|
explicit MemoryManager(Core::System& system);
|
2019-05-09 23:04:41 +00:00
|
|
|
~MemoryManager();
|
2018-02-08 02:54:35 +00:00
|
|
|
|
2020-06-11 03:58:57 +00:00
|
|
|
/// Binds a renderer to the memory manager.
|
|
|
|
void BindRasterizer(VideoCore::RasterizerInterface& rasterizer);
|
|
|
|
|
2019-04-06 00:18:27 +00:00
|
|
|
std::optional<VAddr> GpuToCpuAddress(GPUVAddr addr) const;
|
2018-02-08 02:54:35 +00:00
|
|
|
|
2019-03-04 04:54:16 +00:00
|
|
|
template <typename T>
|
2019-04-06 00:30:46 +00:00
|
|
|
T Read(GPUVAddr addr) const;
|
2019-02-24 05:15:35 +00:00
|
|
|
|
2019-03-04 04:54:16 +00:00
|
|
|
template <typename T>
|
2019-03-09 19:06:51 +00:00
|
|
|
void Write(GPUVAddr addr, T data);
|
2019-02-24 05:15:35 +00:00
|
|
|
|
2019-03-09 19:06:51 +00:00
|
|
|
u8* GetPointer(GPUVAddr addr);
|
2019-04-06 00:25:25 +00:00
|
|
|
const u8* GetPointer(GPUVAddr addr) const;
|
2019-02-24 05:15:35 +00:00
|
|
|
|
2019-04-16 19:45:24 +00:00
|
|
|
/**
|
2019-04-16 14:11:35 +00:00
|
|
|
* ReadBlock and WriteBlock are full read and write operations over virtual
|
2019-05-09 23:02:52 +00:00
|
|
|
* GPU Memory. It's important to use these when GPU memory may not be continuous
|
2019-04-16 14:11:35 +00:00
|
|
|
* in the Host Memory counterpart. Note: This functions cause Host GPU Memory
|
|
|
|
* Flushes and Invalidations, respectively to each operation.
|
|
|
|
*/
|
2020-06-20 02:02:56 +00:00
|
|
|
void ReadBlock(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size) const;
|
|
|
|
void WriteBlock(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size);
|
|
|
|
void CopyBlock(GPUVAddr gpu_dest_addr, GPUVAddr gpu_src_addr, std::size_t size);
|
2019-04-16 14:11:35 +00:00
|
|
|
|
2019-04-16 19:45:24 +00:00
|
|
|
/**
|
2019-04-16 14:11:35 +00:00
|
|
|
* ReadBlockUnsafe and WriteBlockUnsafe are special versions of ReadBlock and
|
|
|
|
* WriteBlock respectively. In this versions, no flushing or invalidation is actually
|
|
|
|
* done and their performance is similar to a memcpy. This functions can be used
|
|
|
|
* on either of this 2 scenarios instead of their safe counterpart:
|
|
|
|
* - Memory which is sure to never be represented in the Host GPU.
|
|
|
|
* - Memory Managed by a Cache Manager. Example: Texture Flushing should use
|
|
|
|
* WriteBlockUnsafe instead of WriteBlock since it shouldn't invalidate the texture
|
|
|
|
* being flushed.
|
|
|
|
*/
|
2020-06-20 02:02:56 +00:00
|
|
|
void ReadBlockUnsafe(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size) const;
|
|
|
|
void WriteBlockUnsafe(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size);
|
|
|
|
void CopyBlockUnsafe(GPUVAddr gpu_dest_addr, GPUVAddr gpu_src_addr, std::size_t size);
|
2019-04-16 14:11:35 +00:00
|
|
|
|
2020-04-08 17:34:59 +00:00
|
|
|
/**
|
2020-07-26 04:16:21 +00:00
|
|
|
* IsGranularRange checks if a gpu region can be simply read with a pointer.
|
2020-04-08 17:34:59 +00:00
|
|
|
*/
|
2020-08-24 04:37:54 +00:00
|
|
|
bool IsGranularRange(GPUVAddr gpu_addr, std::size_t size) const;
|
2020-04-05 21:23:49 +00:00
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
GPUVAddr Map(VAddr cpu_addr, GPUVAddr gpu_addr, std::size_t size);
|
|
|
|
GPUVAddr MapAllocate(VAddr cpu_addr, std::size_t size, std::size_t align);
|
|
|
|
std::optional<GPUVAddr> AllocateFixed(GPUVAddr gpu_addr, std::size_t size);
|
|
|
|
GPUVAddr Allocate(std::size_t size, std::size_t align);
|
|
|
|
void Unmap(GPUVAddr gpu_addr, std::size_t size);
|
2019-03-04 04:54:16 +00:00
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
private:
|
|
|
|
PageEntry GetPageEntry(GPUVAddr gpu_addr) const;
|
|
|
|
void SetPageEntry(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size = page_size);
|
|
|
|
GPUVAddr UpdateRange(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size);
|
|
|
|
std::optional<GPUVAddr> FindFreeRange(std::size_t size, std::size_t align) const;
|
2019-03-04 04:54:16 +00:00
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
void TryLockPage(PageEntry page_entry, std::size_t size);
|
|
|
|
void TryUnlockPage(PageEntry page_entry, std::size_t size);
|
2019-03-04 04:54:16 +00:00
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
static constexpr std::size_t PageEntryIndex(GPUVAddr gpu_addr) {
|
|
|
|
return (gpu_addr >> page_bits) & page_table_mask;
|
|
|
|
}
|
2018-04-21 18:40:51 +00:00
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
static constexpr u64 address_space_size = 1ULL << 40;
|
|
|
|
static constexpr u64 address_space_start = 1ULL << 32;
|
2019-03-04 04:54:16 +00:00
|
|
|
static constexpr u64 page_bits{16};
|
|
|
|
static constexpr u64 page_size{1 << page_bits};
|
|
|
|
static constexpr u64 page_mask{page_size - 1};
|
2020-07-26 04:16:21 +00:00
|
|
|
static constexpr u64 page_table_bits{24};
|
|
|
|
static constexpr u64 page_table_size{1 << page_table_bits};
|
|
|
|
static constexpr u64 page_table_mask{page_table_size - 1};
|
2019-03-04 04:54:16 +00:00
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
Core::System& system;
|
2019-03-04 04:54:16 +00:00
|
|
|
|
2020-06-11 03:58:57 +00:00
|
|
|
VideoCore::RasterizerInterface* rasterizer = nullptr;
|
2019-07-09 06:17:44 +00:00
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
std::vector<PageEntry> page_table;
|
2018-02-08 02:54:35 +00:00
|
|
|
};
|
|
|
|
|
2018-02-12 04:44:12 +00:00
|
|
|
} // namespace Tegra
|