2022-04-23 08:59:50 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-02-08 02:54:35 +00:00
|
|
|
|
2021-09-09 01:43:02 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-04-23 15:57:12 +00:00
|
|
|
#include "common/alignment.h"
|
2018-02-08 02:54:35 +00:00
|
|
|
#include "common/assert.h"
|
2021-01-22 21:31:08 +00:00
|
|
|
#include "common/logging/log.h"
|
2019-07-09 05:19:27 +00:00
|
|
|
#include "core/core.h"
|
2021-02-13 01:58:31 +00:00
|
|
|
#include "core/hle/kernel/k_page_table.h"
|
2021-04-24 05:04:28 +00:00
|
|
|
#include "core/hle/kernel/k_process.h"
|
2019-02-24 05:15:35 +00:00
|
|
|
#include "core/memory.h"
|
2018-02-12 04:44:12 +00:00
|
|
|
#include "video_core/memory_manager.h"
|
2020-02-15 22:47:15 +00:00
|
|
|
#include "video_core/rasterizer_interface.h"
|
2020-10-27 03:07:36 +00:00
|
|
|
#include "video_core/renderer_base.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
|
|
|
|
2021-11-11 20:24:40 +00:00
|
|
|
MemoryManager::MemoryManager(Core::System& system_, u64 address_space_bits_, u64 page_bits_)
|
|
|
|
: system{system_}, address_space_bits{address_space_bits_}, page_bits{page_bits_}, entries{},
|
|
|
|
page_table{address_space_bits, address_space_bits + page_bits - 38, page_bits} {
|
|
|
|
address_space_size = 1ULL << address_space_bits;
|
|
|
|
allocate_start = address_space_bits > 32 ? 1ULL << 32 : 0;
|
|
|
|
page_size = 1ULL << page_bits;
|
|
|
|
page_mask = page_size - 1ULL;
|
|
|
|
const u64 page_table_bits = address_space_bits - cpu_page_bits;
|
|
|
|
const u64 page_table_size = 1ULL << page_table_bits;
|
|
|
|
page_table_mask = page_table_size - 1;
|
|
|
|
|
|
|
|
entries.resize(page_table_size / 32, 0);
|
|
|
|
}
|
2018-11-23 17:58:55 +00:00
|
|
|
|
2019-05-09 23:04:41 +00:00
|
|
|
MemoryManager::~MemoryManager() = default;
|
|
|
|
|
2021-11-11 20:24:40 +00:00
|
|
|
MemoryManager::EntryType MemoryManager::GetEntry(size_t position) const {
|
|
|
|
position = position >> page_bits;
|
|
|
|
const u64 entry_mask = entries[position / 32];
|
|
|
|
const size_t sub_index = position % 32;
|
|
|
|
return static_cast<EntryType>((entry_mask >> (2 * sub_index)) & 0x03ULL);
|
2020-06-11 03:58:57 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 20:24:40 +00:00
|
|
|
void MemoryManager::SetEntry(size_t position, MemoryManager::EntryType entry) {
|
|
|
|
position = position >> page_bits;
|
|
|
|
const u64 entry_mask = entries[position / 32];
|
|
|
|
const size_t sub_index = position % 32;
|
|
|
|
entries[position / 32] =
|
|
|
|
(~(3ULL << sub_index * 2) & entry_mask) | (static_cast<u64>(entry) << sub_index * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <MemoryManager::EntryType entry_type>
|
|
|
|
GPUVAddr MemoryManager::PageTableOp(GPUVAddr gpu_addr, [[maybe_unused]] VAddr cpu_addr,
|
|
|
|
size_t size) {
|
2020-07-26 04:16:21 +00:00
|
|
|
u64 remaining_size{size};
|
2021-11-11 20:24:40 +00:00
|
|
|
if constexpr (entry_type == EntryType::Mapped) {
|
|
|
|
page_table.ReserveRange(gpu_addr, size);
|
|
|
|
}
|
2020-07-26 04:16:21 +00:00
|
|
|
for (u64 offset{}; offset < size; offset += page_size) {
|
2021-11-11 20:24:40 +00:00
|
|
|
const GPUVAddr current_gpu_addr = gpu_addr + offset;
|
|
|
|
SetEntry(current_gpu_addr, entry_type);
|
|
|
|
if constexpr (entry_type == EntryType::Mapped) {
|
|
|
|
const VAddr current_cpu_addr = cpu_addr + offset;
|
|
|
|
const auto index = PageEntryIndex(current_gpu_addr);
|
|
|
|
page_table[index] = static_cast<u32>(current_cpu_addr >> 12ULL);
|
2020-07-26 04:16:21 +00:00
|
|
|
}
|
|
|
|
remaining_size -= page_size;
|
|
|
|
}
|
2019-03-04 04:54:16 +00:00
|
|
|
return gpu_addr;
|
|
|
|
}
|
2018-02-08 02:54:35 +00:00
|
|
|
|
2021-11-11 20:24:40 +00:00
|
|
|
void MemoryManager::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) {
|
|
|
|
rasterizer = rasterizer_;
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
GPUVAddr MemoryManager::Map(VAddr cpu_addr, GPUVAddr gpu_addr, std::size_t size) {
|
2021-11-11 20:24:40 +00:00
|
|
|
return PageTableOp<EntryType::Mapped>(gpu_addr, cpu_addr, size);
|
2020-07-26 04:16:21 +00:00
|
|
|
}
|
2019-03-21 02:58:49 +00:00
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
GPUVAddr MemoryManager::MapAllocate(VAddr cpu_addr, std::size_t size, std::size_t align) {
|
|
|
|
return Map(cpu_addr, *FindFreeRange(size, align), size);
|
2019-03-04 04:54:16 +00:00
|
|
|
}
|
2018-11-01 02:20:37 +00:00
|
|
|
|
2020-10-27 03:07:36 +00:00
|
|
|
GPUVAddr MemoryManager::MapAllocate32(VAddr cpu_addr, std::size_t size) {
|
|
|
|
const std::optional<GPUVAddr> gpu_addr = FindFreeRange(size, 1, true);
|
|
|
|
ASSERT(gpu_addr);
|
|
|
|
return Map(cpu_addr, *gpu_addr, size);
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
void MemoryManager::Unmap(GPUVAddr gpu_addr, std::size_t size) {
|
2021-01-22 21:31:08 +00:00
|
|
|
if (size == 0) {
|
2020-07-26 04:16:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-06-13 01:34:06 +00:00
|
|
|
const auto submapped_ranges = GetSubmappedRange(gpu_addr, size);
|
|
|
|
|
2021-12-31 07:07:59 +00:00
|
|
|
for (const auto& [map_addr, map_size] : submapped_ranges) {
|
2021-06-13 01:34:06 +00:00
|
|
|
// Flush and invalidate through the GPU interface, to be asynchronous if possible.
|
2021-12-31 07:07:59 +00:00
|
|
|
const std::optional<VAddr> cpu_addr = GpuToCpuAddress(map_addr);
|
2021-06-13 01:34:06 +00:00
|
|
|
ASSERT(cpu_addr);
|
|
|
|
|
2021-12-31 07:07:59 +00:00
|
|
|
rasterizer->UnmapMemory(*cpu_addr, map_size);
|
2021-06-13 01:34:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 20:24:40 +00:00
|
|
|
PageTableOp<EntryType::Free>(gpu_addr, 0, size);
|
2019-03-04 04:54:16 +00:00
|
|
|
}
|
2018-07-24 15:19:51 +00:00
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
std::optional<GPUVAddr> MemoryManager::AllocateFixed(GPUVAddr gpu_addr, std::size_t size) {
|
|
|
|
for (u64 offset{}; offset < size; offset += page_size) {
|
2021-11-11 20:24:40 +00:00
|
|
|
if (GetEntry(gpu_addr + offset) != EntryType::Free) {
|
2020-09-22 21:31:53 +00:00
|
|
|
return std::nullopt;
|
2020-07-26 04:16:21 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 02:20:37 +00:00
|
|
|
|
2021-11-11 20:24:40 +00:00
|
|
|
return PageTableOp<EntryType::Reserved>(gpu_addr, 0, size);
|
2020-07-26 04:16:21 +00:00
|
|
|
}
|
2019-03-21 02:58:49 +00:00
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
GPUVAddr MemoryManager::Allocate(std::size_t size, std::size_t align) {
|
|
|
|
return *AllocateFixed(*FindFreeRange(size, align), size);
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 03:07:36 +00:00
|
|
|
std::optional<GPUVAddr> MemoryManager::FindFreeRange(std::size_t size, std::size_t align,
|
|
|
|
bool start_32bit_address) const {
|
2020-07-26 04:16:21 +00:00
|
|
|
if (!align) {
|
|
|
|
align = page_size;
|
|
|
|
} else {
|
|
|
|
align = Common::AlignUp(align, page_size);
|
2019-03-09 19:06:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
u64 available_size{};
|
2021-11-11 20:24:40 +00:00
|
|
|
GPUVAddr gpu_addr{allocate_start};
|
2020-07-26 04:16:21 +00:00
|
|
|
while (gpu_addr + available_size < address_space_size) {
|
2021-11-11 20:24:40 +00:00
|
|
|
if (GetEntry(gpu_addr + available_size) == EntryType::Free) {
|
2020-07-26 04:16:21 +00:00
|
|
|
available_size += page_size;
|
|
|
|
|
|
|
|
if (available_size >= size) {
|
|
|
|
return gpu_addr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gpu_addr += available_size + page_size;
|
|
|
|
available_size = 0;
|
|
|
|
|
|
|
|
const auto remainder{gpu_addr % align};
|
|
|
|
if (remainder) {
|
|
|
|
gpu_addr = (gpu_addr - remainder) + align;
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 02:20:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 21:31:53 +00:00
|
|
|
return std::nullopt;
|
2019-03-04 04:54:16 +00:00
|
|
|
}
|
2018-11-01 02:20:37 +00:00
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
std::optional<VAddr> MemoryManager::GpuToCpuAddress(GPUVAddr gpu_addr) const {
|
2021-11-11 20:24:40 +00:00
|
|
|
if (GetEntry(gpu_addr) != EntryType::Mapped) {
|
2020-09-22 21:31:53 +00:00
|
|
|
return std::nullopt;
|
2019-03-09 19:06:51 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 20:24:40 +00:00
|
|
|
const VAddr cpu_addr_base = static_cast<VAddr>(page_table[PageEntryIndex(gpu_addr)]) << 12ULL;
|
|
|
|
return cpu_addr_base + (gpu_addr & page_mask);
|
2020-07-26 04:16:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 01:34:06 +00:00
|
|
|
std::optional<VAddr> MemoryManager::GpuToCpuAddress(GPUVAddr addr, std::size_t size) const {
|
|
|
|
size_t page_index{addr >> page_bits};
|
|
|
|
const size_t page_last{(addr + size + page_size - 1) >> page_bits};
|
|
|
|
while (page_index < page_last) {
|
|
|
|
const auto page_addr{GpuToCpuAddress(page_index << page_bits)};
|
|
|
|
if (page_addr && *page_addr != 0) {
|
|
|
|
return page_addr;
|
|
|
|
}
|
|
|
|
++page_index;
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
template <typename T>
|
|
|
|
T MemoryManager::Read(GPUVAddr addr) const {
|
|
|
|
if (auto page_pointer{GetPointer(addr)}; page_pointer) {
|
2019-03-04 04:54:16 +00:00
|
|
|
// NOTE: Avoid adding any extra logic to this fast-path block
|
|
|
|
T value;
|
2020-04-08 16:08:06 +00:00
|
|
|
std::memcpy(&value, page_pointer, sizeof(T));
|
2019-03-04 04:54:16 +00:00
|
|
|
return value;
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 21:02:29 +00:00
|
|
|
ASSERT(false);
|
2020-04-09 02:51:31 +00:00
|
|
|
|
2019-03-04 04:54:16 +00:00
|
|
|
return {};
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 04:54:16 +00:00
|
|
|
template <typename T>
|
2019-03-09 19:06:51 +00:00
|
|
|
void MemoryManager::Write(GPUVAddr addr, T data) {
|
2020-07-26 04:16:21 +00:00
|
|
|
if (auto page_pointer{GetPointer(addr)}; page_pointer) {
|
2019-03-04 04:54:16 +00:00
|
|
|
// NOTE: Avoid adding any extra logic to this fast-path block
|
2020-04-08 16:08:06 +00:00
|
|
|
std::memcpy(page_pointer, &data, sizeof(T));
|
2019-03-04 04:54:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-20 19:21:06 +00:00
|
|
|
|
2022-06-07 21:02:29 +00:00
|
|
|
ASSERT(false);
|
2019-03-04 04:54:16 +00:00
|
|
|
}
|
2018-07-24 15:19:51 +00:00
|
|
|
|
2019-04-06 00:30:46 +00:00
|
|
|
template u8 MemoryManager::Read<u8>(GPUVAddr addr) const;
|
|
|
|
template u16 MemoryManager::Read<u16>(GPUVAddr addr) const;
|
|
|
|
template u32 MemoryManager::Read<u32>(GPUVAddr addr) const;
|
|
|
|
template u64 MemoryManager::Read<u64>(GPUVAddr addr) const;
|
2019-03-04 04:54:16 +00:00
|
|
|
template void MemoryManager::Write<u8>(GPUVAddr addr, u8 data);
|
|
|
|
template void MemoryManager::Write<u16>(GPUVAddr addr, u16 data);
|
|
|
|
template void MemoryManager::Write<u32>(GPUVAddr addr, u32 data);
|
|
|
|
template void MemoryManager::Write<u64>(GPUVAddr addr, u64 data);
|
2018-11-01 02:20:37 +00:00
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
u8* MemoryManager::GetPointer(GPUVAddr gpu_addr) {
|
|
|
|
const auto address{GpuToCpuAddress(gpu_addr)};
|
|
|
|
if (!address) {
|
|
|
|
return {};
|
2019-04-06 00:25:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
return system.Memory().GetPointer(*address);
|
2019-04-06 00:25:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
const u8* MemoryManager::GetPointer(GPUVAddr gpu_addr) const {
|
|
|
|
const auto address{GpuToCpuAddress(gpu_addr)};
|
|
|
|
if (!address) {
|
|
|
|
return {};
|
2018-05-20 19:21:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
return system.Memory().GetPointer(*address);
|
2019-04-16 19:45:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 04:36:00 +00:00
|
|
|
void MemoryManager::ReadBlockImpl(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size,
|
|
|
|
bool is_safe) const {
|
2019-03-29 01:58:28 +00:00
|
|
|
std::size_t remaining_size{size};
|
2020-06-20 02:02:56 +00:00
|
|
|
std::size_t page_index{gpu_src_addr >> page_bits};
|
|
|
|
std::size_t page_offset{gpu_src_addr & page_mask};
|
2019-03-29 01:58:28 +00:00
|
|
|
|
|
|
|
while (remaining_size > 0) {
|
|
|
|
const std::size_t copy_amount{
|
|
|
|
std::min(static_cast<std::size_t>(page_size) - page_offset, remaining_size)};
|
2021-12-31 07:07:59 +00:00
|
|
|
const auto page_addr{GpuToCpuAddress(page_index << page_bits)};
|
2021-11-11 20:24:40 +00:00
|
|
|
if (page_addr) {
|
2020-07-26 04:16:21 +00:00
|
|
|
const auto src_addr{*page_addr + page_offset};
|
2021-12-31 04:36:00 +00:00
|
|
|
if (is_safe) {
|
|
|
|
// Flush must happen on the rasterizer interface, such that memory is always
|
|
|
|
// synchronous when it is read (even when in asynchronous GPU mode).
|
|
|
|
// Fixes Dead Cells title menu.
|
|
|
|
rasterizer->FlushRegion(src_addr, copy_amount);
|
|
|
|
}
|
2020-07-26 04:16:21 +00:00
|
|
|
system.Memory().ReadBlockUnsafe(src_addr, dest_buffer, copy_amount);
|
2021-12-31 04:36:00 +00:00
|
|
|
} else {
|
|
|
|
std::memset(dest_buffer, 0, copy_amount);
|
2020-07-26 04:16:21 +00:00
|
|
|
}
|
2019-03-29 01:58:28 +00:00
|
|
|
|
|
|
|
page_index++;
|
|
|
|
page_offset = 0;
|
|
|
|
dest_buffer = static_cast<u8*>(dest_buffer) + copy_amount;
|
|
|
|
remaining_size -= copy_amount;
|
|
|
|
}
|
2019-03-04 04:54:16 +00:00
|
|
|
}
|
2019-03-29 01:58:28 +00:00
|
|
|
|
2021-12-31 04:36:00 +00:00
|
|
|
void MemoryManager::ReadBlock(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size) const {
|
|
|
|
ReadBlockImpl(gpu_src_addr, dest_buffer, size, true);
|
|
|
|
}
|
|
|
|
|
2020-06-20 02:02:56 +00:00
|
|
|
void MemoryManager::ReadBlockUnsafe(GPUVAddr gpu_src_addr, void* dest_buffer,
|
2019-04-16 14:11:35 +00:00
|
|
|
const std::size_t size) const {
|
2021-12-31 04:36:00 +00:00
|
|
|
ReadBlockImpl(gpu_src_addr, dest_buffer, size, false);
|
2019-04-16 03:01:35 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 04:36:00 +00:00
|
|
|
void MemoryManager::WriteBlockImpl(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size,
|
|
|
|
bool is_safe) {
|
2019-03-29 01:58:28 +00:00
|
|
|
std::size_t remaining_size{size};
|
2020-06-20 02:02:56 +00:00
|
|
|
std::size_t page_index{gpu_dest_addr >> page_bits};
|
|
|
|
std::size_t page_offset{gpu_dest_addr & page_mask};
|
2019-03-29 01:58:28 +00:00
|
|
|
|
|
|
|
while (remaining_size > 0) {
|
|
|
|
const std::size_t copy_amount{
|
|
|
|
std::min(static_cast<std::size_t>(page_size) - page_offset, remaining_size)};
|
2021-12-31 07:07:59 +00:00
|
|
|
const auto page_addr{GpuToCpuAddress(page_index << page_bits)};
|
2021-11-11 20:24:40 +00:00
|
|
|
if (page_addr) {
|
2020-07-26 04:16:21 +00:00
|
|
|
const auto dest_addr{*page_addr + page_offset};
|
|
|
|
|
2021-12-31 04:36:00 +00:00
|
|
|
if (is_safe) {
|
|
|
|
// Invalidate must happen on the rasterizer interface, such that memory is always
|
|
|
|
// synchronous when it is written (even when in asynchronous GPU mode).
|
|
|
|
rasterizer->InvalidateRegion(dest_addr, copy_amount);
|
|
|
|
}
|
2020-07-26 04:16:21 +00:00
|
|
|
system.Memory().WriteBlockUnsafe(dest_addr, src_buffer, copy_amount);
|
|
|
|
}
|
2019-03-29 01:58:28 +00:00
|
|
|
|
|
|
|
page_index++;
|
|
|
|
page_offset = 0;
|
|
|
|
src_buffer = static_cast<const u8*>(src_buffer) + copy_amount;
|
|
|
|
remaining_size -= copy_amount;
|
|
|
|
}
|
2019-03-04 04:54:16 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 04:36:00 +00:00
|
|
|
void MemoryManager::WriteBlock(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size) {
|
|
|
|
WriteBlockImpl(gpu_dest_addr, src_buffer, size, true);
|
|
|
|
}
|
|
|
|
|
2020-06-20 02:02:56 +00:00
|
|
|
void MemoryManager::WriteBlockUnsafe(GPUVAddr gpu_dest_addr, const void* src_buffer,
|
2020-07-26 04:16:21 +00:00
|
|
|
std::size_t size) {
|
2021-12-31 04:36:00 +00:00
|
|
|
WriteBlockImpl(gpu_dest_addr, src_buffer, size, false);
|
2019-04-16 03:01:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 22:08:15 +00:00
|
|
|
void MemoryManager::FlushRegion(GPUVAddr gpu_addr, size_t size) const {
|
|
|
|
size_t remaining_size{size};
|
|
|
|
size_t page_index{gpu_addr >> page_bits};
|
|
|
|
size_t page_offset{gpu_addr & page_mask};
|
|
|
|
while (remaining_size > 0) {
|
|
|
|
const size_t num_bytes{std::min(page_size - page_offset, remaining_size)};
|
|
|
|
if (const auto page_addr{GpuToCpuAddress(page_index << page_bits)}; page_addr) {
|
|
|
|
rasterizer->FlushRegion(*page_addr + page_offset, num_bytes);
|
|
|
|
}
|
|
|
|
++page_index;
|
|
|
|
page_offset = 0;
|
|
|
|
remaining_size -= num_bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:16:21 +00:00
|
|
|
void MemoryManager::CopyBlock(GPUVAddr gpu_dest_addr, GPUVAddr gpu_src_addr, std::size_t size) {
|
2020-04-08 16:08:06 +00:00
|
|
|
std::vector<u8> tmp_buffer(size);
|
2020-06-20 02:02:56 +00:00
|
|
|
ReadBlock(gpu_src_addr, tmp_buffer.data(), size);
|
2021-01-21 22:08:43 +00:00
|
|
|
|
|
|
|
// The output block must be flushed in case it has data modified from the GPU.
|
|
|
|
// Fixes NPC geometry in Zombie Panic in Wonderland DX
|
|
|
|
FlushRegion(gpu_dest_addr, size);
|
2020-06-20 02:02:56 +00:00
|
|
|
WriteBlock(gpu_dest_addr, tmp_buffer.data(), size);
|
2018-10-13 01:52:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 04:37:54 +00:00
|
|
|
bool MemoryManager::IsGranularRange(GPUVAddr gpu_addr, std::size_t size) const {
|
2020-07-26 04:16:21 +00:00
|
|
|
const auto cpu_addr{GpuToCpuAddress(gpu_addr)};
|
|
|
|
if (!cpu_addr) {
|
2020-08-24 04:37:54 +00:00
|
|
|
return false;
|
2019-03-04 04:54:16 +00:00
|
|
|
}
|
2022-08-18 23:28:55 +00:00
|
|
|
const std::size_t page{(*cpu_addr & Core::Memory::YUZU_PAGEMASK) + size};
|
|
|
|
return page <= Core::Memory::YUZU_PAGESIZE;
|
2018-02-08 02:54:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 01:34:06 +00:00
|
|
|
bool MemoryManager::IsContinousRange(GPUVAddr gpu_addr, std::size_t size) const {
|
|
|
|
size_t page_index{gpu_addr >> page_bits};
|
|
|
|
const size_t page_last{(gpu_addr + size + page_size - 1) >> page_bits};
|
|
|
|
std::optional<VAddr> old_page_addr{};
|
|
|
|
while (page_index != page_last) {
|
|
|
|
const auto page_addr{GpuToCpuAddress(page_index << page_bits)};
|
|
|
|
if (!page_addr || *page_addr == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (old_page_addr) {
|
|
|
|
if (*old_page_addr + page_size != *page_addr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
old_page_addr = page_addr;
|
|
|
|
++page_index;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemoryManager::IsFullyMappedRange(GPUVAddr gpu_addr, std::size_t size) const {
|
|
|
|
size_t page_index{gpu_addr >> page_bits};
|
|
|
|
const size_t page_last{(gpu_addr + size + page_size - 1) >> page_bits};
|
|
|
|
while (page_index < page_last) {
|
2021-11-11 20:24:40 +00:00
|
|
|
if (GetEntry(page_index << page_bits) == EntryType::Free) {
|
2021-06-13 01:34:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++page_index;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<GPUVAddr, std::size_t>> MemoryManager::GetSubmappedRange(
|
|
|
|
GPUVAddr gpu_addr, std::size_t size) const {
|
|
|
|
std::vector<std::pair<GPUVAddr, std::size_t>> result{};
|
|
|
|
size_t page_index{gpu_addr >> page_bits};
|
|
|
|
size_t remaining_size{size};
|
|
|
|
size_t page_offset{gpu_addr & page_mask};
|
|
|
|
std::optional<std::pair<GPUVAddr, std::size_t>> last_segment{};
|
|
|
|
std::optional<VAddr> old_page_addr{};
|
2021-11-11 20:24:40 +00:00
|
|
|
const auto extend_size = [this, &last_segment, &page_index, &page_offset](std::size_t bytes) {
|
2021-06-13 01:34:06 +00:00
|
|
|
if (!last_segment) {
|
2021-12-31 07:07:59 +00:00
|
|
|
const GPUVAddr new_base_addr = (page_index << page_bits) + page_offset;
|
2021-06-13 01:34:06 +00:00
|
|
|
last_segment = {new_base_addr, bytes};
|
|
|
|
} else {
|
|
|
|
last_segment->second += bytes;
|
|
|
|
}
|
|
|
|
};
|
2021-12-31 07:07:59 +00:00
|
|
|
const auto split = [&last_segment, &result] {
|
2021-06-13 01:34:06 +00:00
|
|
|
if (last_segment) {
|
|
|
|
result.push_back(*last_segment);
|
|
|
|
last_segment = std::nullopt;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
while (remaining_size > 0) {
|
|
|
|
const size_t num_bytes{std::min(page_size - page_offset, remaining_size)};
|
|
|
|
const auto page_addr{GpuToCpuAddress(page_index << page_bits)};
|
2021-12-31 07:07:59 +00:00
|
|
|
if (!page_addr || *page_addr == 0) {
|
2021-06-13 01:34:06 +00:00
|
|
|
split();
|
|
|
|
} else if (old_page_addr) {
|
|
|
|
if (*old_page_addr + page_size != *page_addr) {
|
|
|
|
split();
|
|
|
|
}
|
|
|
|
extend_size(num_bytes);
|
|
|
|
} else {
|
|
|
|
extend_size(num_bytes);
|
|
|
|
}
|
|
|
|
++page_index;
|
|
|
|
page_offset = 0;
|
|
|
|
remaining_size -= num_bytes;
|
2021-08-19 20:57:22 +00:00
|
|
|
old_page_addr = page_addr;
|
2021-06-13 01:34:06 +00:00
|
|
|
}
|
|
|
|
split();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-12 04:44:12 +00:00
|
|
|
} // namespace Tegra
|