2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-02-08 03:54:35 +01:00
|
|
|
|
2021-09-09 03:43:02 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-04-23 17:57:12 +02:00
|
|
|
#include "common/alignment.h"
|
2018-02-08 03:54:35 +01:00
|
|
|
#include "common/assert.h"
|
2021-01-22 22:31:08 +01:00
|
|
|
#include "common/logging/log.h"
|
2019-07-09 07:19:27 +02:00
|
|
|
#include "core/core.h"
|
2021-02-13 02:58:31 +01:00
|
|
|
#include "core/hle/kernel/k_page_table.h"
|
2021-04-24 07:04:28 +02:00
|
|
|
#include "core/hle/kernel/k_process.h"
|
2023-12-25 07:32:16 +01:00
|
|
|
#include "video_core/guest_memory.h"
|
|
|
|
#include "video_core/host1x/host1x.h"
|
2023-01-05 04:05:20 +01:00
|
|
|
#include "video_core/invalidation_accumulator.h"
|
2018-02-12 05:44:12 +01:00
|
|
|
#include "video_core/memory_manager.h"
|
2020-02-15 23:47:15 +01:00
|
|
|
#include "video_core/rasterizer_interface.h"
|
2020-10-27 04:07:36 +01:00
|
|
|
#include "video_core/renderer_base.h"
|
2018-02-08 03:54:35 +01:00
|
|
|
|
2018-02-12 05:44:12 +01:00
|
|
|
namespace Tegra {
|
2023-12-25 07:32:16 +01:00
|
|
|
using Tegra::Memory::GuestMemoryFlags;
|
2018-02-08 03:54:35 +01:00
|
|
|
|
2021-12-17 16:45:06 +01:00
|
|
|
std::atomic<size_t> MemoryManager::unique_identifier_generator{};
|
|
|
|
|
2023-12-29 09:50:04 +01:00
|
|
|
MemoryManager::MemoryManager(Core::System& system_, MaxwellDeviceMemoryManager& memory_,
|
|
|
|
u64 address_space_bits_, u64 big_page_bits_, u64 page_bits_)
|
|
|
|
: system{system_}, memory{memory_}, address_space_bits{address_space_bits_},
|
|
|
|
page_bits{page_bits_}, big_page_bits{big_page_bits_}, entries{}, big_entries{},
|
|
|
|
page_table{address_space_bits, address_space_bits + page_bits - 38,
|
|
|
|
page_bits != big_page_bits ? page_bits : 0},
|
2022-11-17 16:36:53 +01:00
|
|
|
kind_map{PTEKind::INVALID}, unique_identifier{unique_identifier_generator.fetch_add(
|
2023-01-05 04:05:20 +01:00
|
|
|
1, std::memory_order_acq_rel)},
|
|
|
|
accumulator{std::make_unique<VideoCommon::InvalidationAccumulator>()} {
|
2021-11-11 21:24:40 +01:00
|
|
|
address_space_size = 1ULL << address_space_bits;
|
|
|
|
page_size = 1ULL << page_bits;
|
|
|
|
page_mask = page_size - 1ULL;
|
2022-02-05 18:15:26 +01:00
|
|
|
big_page_size = 1ULL << big_page_bits;
|
|
|
|
big_page_mask = big_page_size - 1ULL;
|
|
|
|
const u64 page_table_bits = address_space_bits - page_bits;
|
|
|
|
const u64 big_page_table_bits = address_space_bits - big_page_bits;
|
2021-11-11 21:24:40 +01:00
|
|
|
const u64 page_table_size = 1ULL << page_table_bits;
|
2022-02-05 18:15:26 +01:00
|
|
|
const u64 big_page_table_size = 1ULL << big_page_table_bits;
|
2021-11-11 21:24:40 +01:00
|
|
|
page_table_mask = page_table_size - 1;
|
2022-02-05 18:15:26 +01:00
|
|
|
big_page_table_mask = big_page_table_size - 1;
|
2021-11-11 21:24:40 +01:00
|
|
|
|
2022-02-05 18:15:26 +01:00
|
|
|
big_entries.resize(big_page_table_size / 32, 0);
|
2023-12-25 07:32:16 +01:00
|
|
|
big_page_table_dev.resize(big_page_table_size);
|
2023-03-30 04:26:12 +02:00
|
|
|
big_page_continuous.resize(big_page_table_size / continuous_bits, 0);
|
2021-11-11 21:24:40 +01:00
|
|
|
entries.resize(page_table_size / 32, 0);
|
|
|
|
}
|
2018-11-23 18:58:55 +01:00
|
|
|
|
2023-12-29 09:50:04 +01:00
|
|
|
MemoryManager::MemoryManager(Core::System& system_, u64 address_space_bits_, u64 big_page_bits_,
|
|
|
|
u64 page_bits_)
|
|
|
|
: MemoryManager(system_, system_.Host1x().MemoryManager(), address_space_bits_, big_page_bits_,
|
|
|
|
page_bits_) {}
|
|
|
|
|
2019-05-10 01:04:41 +02:00
|
|
|
MemoryManager::~MemoryManager() = default;
|
|
|
|
|
2022-02-05 18:15:26 +01:00
|
|
|
template <bool is_big_page>
|
2021-11-11 21:24:40 +01:00
|
|
|
MemoryManager::EntryType MemoryManager::GetEntry(size_t position) const {
|
2022-02-05 18:15:26 +01:00
|
|
|
if constexpr (is_big_page) {
|
|
|
|
position = position >> big_page_bits;
|
|
|
|
const u64 entry_mask = big_entries[position / 32];
|
|
|
|
const size_t sub_index = position % 32;
|
|
|
|
return static_cast<EntryType>((entry_mask >> (2 * sub_index)) & 0x03ULL);
|
|
|
|
} else {
|
|
|
|
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 05:58:57 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 18:15:26 +01:00
|
|
|
template <bool is_big_page>
|
2021-11-11 21:24:40 +01:00
|
|
|
void MemoryManager::SetEntry(size_t position, MemoryManager::EntryType entry) {
|
2022-02-05 18:15:26 +01:00
|
|
|
if constexpr (is_big_page) {
|
|
|
|
position = position >> big_page_bits;
|
|
|
|
const u64 entry_mask = big_entries[position / 32];
|
|
|
|
const size_t sub_index = position % 32;
|
|
|
|
big_entries[position / 32] =
|
|
|
|
(~(3ULL << sub_index * 2) & entry_mask) | (static_cast<u64>(entry) << sub_index * 2);
|
|
|
|
} else {
|
|
|
|
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);
|
|
|
|
}
|
2021-11-11 21:24:40 +01:00
|
|
|
}
|
|
|
|
|
2022-10-16 17:49:32 +02:00
|
|
|
PTEKind MemoryManager::GetPageKind(GPUVAddr gpu_addr) const {
|
2023-05-04 00:04:22 +02:00
|
|
|
std::unique_lock<std::mutex> lock(guard);
|
2022-11-17 16:36:53 +01:00
|
|
|
return kind_map.GetValueAt(gpu_addr);
|
2022-10-16 17:49:32 +02:00
|
|
|
}
|
|
|
|
|
2023-03-30 04:26:12 +02:00
|
|
|
inline bool MemoryManager::IsBigPageContinuous(size_t big_page_index) const {
|
|
|
|
const u64 entry_mask = big_page_continuous[big_page_index / continuous_bits];
|
|
|
|
const size_t sub_index = big_page_index % continuous_bits;
|
2022-02-06 18:51:07 +01:00
|
|
|
return ((entry_mask >> sub_index) & 0x1ULL) != 0;
|
|
|
|
}
|
|
|
|
|
2023-03-30 04:26:12 +02:00
|
|
|
inline void MemoryManager::SetBigPageContinuous(size_t big_page_index, bool value) {
|
|
|
|
const u64 continuous_mask = big_page_continuous[big_page_index / continuous_bits];
|
|
|
|
const size_t sub_index = big_page_index % continuous_bits;
|
|
|
|
big_page_continuous[big_page_index / continuous_bits] =
|
|
|
|
(~(1ULL << sub_index) & continuous_mask) | (value ? 1ULL << sub_index : 0);
|
2022-02-06 18:51:07 +01:00
|
|
|
}
|
|
|
|
|
2021-11-11 21:24:40 +01:00
|
|
|
template <MemoryManager::EntryType entry_type>
|
2023-12-25 07:32:16 +01:00
|
|
|
GPUVAddr MemoryManager::PageTableOp(GPUVAddr gpu_addr, [[maybe_unused]] DAddr dev_addr, size_t size,
|
2022-10-16 17:49:32 +02:00
|
|
|
PTEKind kind) {
|
2022-10-21 08:34:07 +02:00
|
|
|
[[maybe_unused]] u64 remaining_size{size};
|
2021-11-11 21:24:40 +01:00
|
|
|
if constexpr (entry_type == EntryType::Mapped) {
|
|
|
|
page_table.ReserveRange(gpu_addr, size);
|
|
|
|
}
|
2020-07-26 06:16:21 +02:00
|
|
|
for (u64 offset{}; offset < size; offset += page_size) {
|
2021-11-11 21:24:40 +01:00
|
|
|
const GPUVAddr current_gpu_addr = gpu_addr + offset;
|
2022-02-05 18:15:26 +01:00
|
|
|
[[maybe_unused]] const auto current_entry_type = GetEntry<false>(current_gpu_addr);
|
|
|
|
SetEntry<false>(current_gpu_addr, entry_type);
|
2022-01-01 22:03:37 +01:00
|
|
|
if (current_entry_type != entry_type) {
|
2023-06-19 01:19:28 +02:00
|
|
|
rasterizer->ModifyGPUMemory(unique_identifier, current_gpu_addr, page_size);
|
2022-01-01 22:03:37 +01:00
|
|
|
}
|
2021-11-11 21:24:40 +01:00
|
|
|
if constexpr (entry_type == EntryType::Mapped) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr current_dev_addr = dev_addr + offset;
|
2022-02-05 18:15:26 +01:00
|
|
|
const auto index = PageEntryIndex<false>(current_gpu_addr);
|
2023-12-25 07:32:16 +01:00
|
|
|
const u32 sub_value = static_cast<u32>(current_dev_addr >> cpu_page_bits);
|
2022-02-05 18:15:26 +01:00
|
|
|
page_table[index] = sub_value;
|
2020-07-26 06:16:21 +02:00
|
|
|
}
|
|
|
|
remaining_size -= page_size;
|
|
|
|
}
|
2022-11-17 16:36:53 +01:00
|
|
|
kind_map.Map(gpu_addr, gpu_addr + size, kind);
|
2019-03-04 05:54:16 +01:00
|
|
|
return gpu_addr;
|
|
|
|
}
|
2018-02-08 03:54:35 +01:00
|
|
|
|
2022-02-05 18:15:26 +01:00
|
|
|
template <MemoryManager::EntryType entry_type>
|
2023-12-25 07:32:16 +01:00
|
|
|
GPUVAddr MemoryManager::BigPageTableOp(GPUVAddr gpu_addr, [[maybe_unused]] DAddr dev_addr,
|
2022-10-16 17:49:32 +02:00
|
|
|
size_t size, PTEKind kind) {
|
2022-10-21 08:34:07 +02:00
|
|
|
[[maybe_unused]] u64 remaining_size{size};
|
2022-02-05 18:15:26 +01:00
|
|
|
for (u64 offset{}; offset < size; offset += big_page_size) {
|
|
|
|
const GPUVAddr current_gpu_addr = gpu_addr + offset;
|
|
|
|
[[maybe_unused]] const auto current_entry_type = GetEntry<true>(current_gpu_addr);
|
|
|
|
SetEntry<true>(current_gpu_addr, entry_type);
|
|
|
|
if (current_entry_type != entry_type) {
|
2023-06-19 01:19:28 +02:00
|
|
|
rasterizer->ModifyGPUMemory(unique_identifier, current_gpu_addr, big_page_size);
|
2022-02-05 18:15:26 +01:00
|
|
|
}
|
|
|
|
if constexpr (entry_type == EntryType::Mapped) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr current_dev_addr = dev_addr + offset;
|
2022-02-05 18:15:26 +01:00
|
|
|
const auto index = PageEntryIndex<true>(current_gpu_addr);
|
2023-12-25 07:32:16 +01:00
|
|
|
const u32 sub_value = static_cast<u32>(current_dev_addr >> cpu_page_bits);
|
|
|
|
big_page_table_dev[index] = sub_value;
|
2023-03-30 04:26:12 +02:00
|
|
|
const bool is_continuous = ([&] {
|
2022-02-06 18:51:07 +01:00
|
|
|
uintptr_t base_ptr{
|
2023-12-25 07:32:16 +01:00
|
|
|
reinterpret_cast<uintptr_t>(memory.GetPointer<u8>(current_dev_addr))};
|
2022-02-19 14:18:02 +01:00
|
|
|
if (base_ptr == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-12-25 07:32:16 +01:00
|
|
|
for (DAddr start_cpu = current_dev_addr + page_size;
|
|
|
|
start_cpu < current_dev_addr + big_page_size; start_cpu += page_size) {
|
2022-02-06 18:51:07 +01:00
|
|
|
base_ptr += page_size;
|
2023-12-25 07:32:16 +01:00
|
|
|
auto next_ptr = reinterpret_cast<uintptr_t>(memory.GetPointer<u8>(start_cpu));
|
2022-02-19 14:18:02 +01:00
|
|
|
if (next_ptr == 0 || base_ptr != next_ptr) {
|
2022-02-06 18:51:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
})();
|
2023-03-30 04:26:12 +02:00
|
|
|
SetBigPageContinuous(index, is_continuous);
|
2022-02-05 18:15:26 +01:00
|
|
|
}
|
|
|
|
remaining_size -= big_page_size;
|
|
|
|
}
|
2023-05-04 00:04:22 +02:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(guard);
|
|
|
|
kind_map.Map(gpu_addr, gpu_addr + size, kind);
|
|
|
|
}
|
2022-02-05 18:15:26 +01:00
|
|
|
return gpu_addr;
|
|
|
|
}
|
|
|
|
|
2021-11-11 21:24:40 +01:00
|
|
|
void MemoryManager::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) {
|
|
|
|
rasterizer = rasterizer_;
|
|
|
|
}
|
|
|
|
|
2023-12-25 07:32:16 +01:00
|
|
|
GPUVAddr MemoryManager::Map(GPUVAddr gpu_addr, DAddr dev_addr, std::size_t size, PTEKind kind,
|
2022-02-05 18:15:26 +01:00
|
|
|
bool is_big_pages) {
|
|
|
|
if (is_big_pages) [[likely]] {
|
2023-12-25 07:32:16 +01:00
|
|
|
return BigPageTableOp<EntryType::Mapped>(gpu_addr, dev_addr, size, kind);
|
2022-02-05 18:15:26 +01:00
|
|
|
}
|
2023-12-25 07:32:16 +01:00
|
|
|
return PageTableOp<EntryType::Mapped>(gpu_addr, dev_addr, size, kind);
|
2020-07-26 06:16:21 +02:00
|
|
|
}
|
2019-03-21 03:58:49 +01:00
|
|
|
|
2022-02-05 18:15:26 +01:00
|
|
|
GPUVAddr MemoryManager::MapSparse(GPUVAddr gpu_addr, std::size_t size, bool is_big_pages) {
|
|
|
|
if (is_big_pages) [[likely]] {
|
2022-10-16 17:49:32 +02:00
|
|
|
return BigPageTableOp<EntryType::Reserved>(gpu_addr, 0, size, PTEKind::INVALID);
|
2022-02-05 18:15:26 +01:00
|
|
|
}
|
2022-10-16 17:49:32 +02:00
|
|
|
return PageTableOp<EntryType::Reserved>(gpu_addr, 0, size, PTEKind::INVALID);
|
2021-11-14 20:55:52 +01:00
|
|
|
}
|
|
|
|
|
2020-07-26 06:16:21 +02:00
|
|
|
void MemoryManager::Unmap(GPUVAddr gpu_addr, std::size_t size) {
|
2021-01-22 22:31:08 +01:00
|
|
|
if (size == 0) {
|
2020-07-26 06:16:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-01-05 04:05:20 +01:00
|
|
|
GetSubmappedRangeImpl<false>(gpu_addr, size, page_stash);
|
2021-06-13 03:34:06 +02:00
|
|
|
|
2023-01-05 04:05:20 +01:00
|
|
|
for (const auto& [map_addr, map_size] : page_stash) {
|
|
|
|
rasterizer->UnmapMemory(map_addr, map_size);
|
2021-06-13 03:34:06 +02:00
|
|
|
}
|
2023-01-05 04:05:20 +01:00
|
|
|
page_stash.clear();
|
2021-06-13 03:34:06 +02:00
|
|
|
|
2022-10-16 17:49:32 +02:00
|
|
|
BigPageTableOp<EntryType::Free>(gpu_addr, 0, size, PTEKind::INVALID);
|
|
|
|
PageTableOp<EntryType::Free>(gpu_addr, 0, size, PTEKind::INVALID);
|
2019-03-04 05:54:16 +01:00
|
|
|
}
|
2018-07-24 17:19:51 +02:00
|
|
|
|
2023-12-25 07:32:16 +01:00
|
|
|
std::optional<DAddr> MemoryManager::GpuToCpuAddress(GPUVAddr gpu_addr) const {
|
2022-04-13 16:20:34 +02:00
|
|
|
if (!IsWithinGPUAddressRange(gpu_addr)) [[unlikely]] {
|
2022-04-02 22:38:58 +02:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-02-05 18:15:26 +01:00
|
|
|
if (GetEntry<true>(gpu_addr) != EntryType::Mapped) [[unlikely]] {
|
|
|
|
if (GetEntry<false>(gpu_addr) != EntryType::Mapped) {
|
2020-09-22 23:31:53 +02:00
|
|
|
return std::nullopt;
|
2020-07-26 06:16:21 +02:00
|
|
|
}
|
2018-11-01 03:20:37 +01:00
|
|
|
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base = static_cast<DAddr>(page_table[PageEntryIndex<false>(gpu_addr)])
|
2022-02-05 18:15:26 +01:00
|
|
|
<< cpu_page_bits;
|
2023-12-25 07:32:16 +01:00
|
|
|
return dev_addr_base + (gpu_addr & page_mask);
|
2019-03-09 20:06:51 +01:00
|
|
|
}
|
|
|
|
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
static_cast<DAddr>(big_page_table_dev[PageEntryIndex<true>(gpu_addr)]) << cpu_page_bits;
|
|
|
|
return dev_addr_base + (gpu_addr & big_page_mask);
|
2020-07-26 06:16:21 +02:00
|
|
|
}
|
|
|
|
|
2023-12-25 07:32:16 +01:00
|
|
|
std::optional<DAddr> MemoryManager::GpuToCpuAddress(GPUVAddr addr, std::size_t size) const {
|
2021-06-13 03:34:06 +02:00
|
|
|
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)};
|
2022-01-01 22:03:37 +01:00
|
|
|
if (page_addr) {
|
2021-06-13 03:34:06 +02:00
|
|
|
return page_addr;
|
|
|
|
}
|
|
|
|
++page_index;
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2020-07-26 06:16:21 +02:00
|
|
|
template <typename T>
|
|
|
|
T MemoryManager::Read(GPUVAddr addr) const {
|
|
|
|
if (auto page_pointer{GetPointer(addr)}; page_pointer) {
|
2019-03-04 05:54:16 +01:00
|
|
|
// NOTE: Avoid adding any extra logic to this fast-path block
|
|
|
|
T value;
|
2020-04-08 18:08:06 +02:00
|
|
|
std::memcpy(&value, page_pointer, sizeof(T));
|
2019-03-04 05:54:16 +01:00
|
|
|
return value;
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
2022-06-07 23:02:29 +02:00
|
|
|
ASSERT(false);
|
2020-04-09 04:51:31 +02:00
|
|
|
|
2019-03-04 05:54:16 +01:00
|
|
|
return {};
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 05:54:16 +01:00
|
|
|
template <typename T>
|
2019-03-09 20:06:51 +01:00
|
|
|
void MemoryManager::Write(GPUVAddr addr, T data) {
|
2020-07-26 06:16:21 +02:00
|
|
|
if (auto page_pointer{GetPointer(addr)}; page_pointer) {
|
2019-03-04 05:54:16 +01:00
|
|
|
// NOTE: Avoid adding any extra logic to this fast-path block
|
2020-04-08 18:08:06 +02:00
|
|
|
std::memcpy(page_pointer, &data, sizeof(T));
|
2019-03-04 05:54:16 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-05-20 21:21:06 +02:00
|
|
|
|
2022-06-07 23:02:29 +02:00
|
|
|
ASSERT(false);
|
2019-03-04 05:54:16 +01:00
|
|
|
}
|
2018-07-24 17:19:51 +02:00
|
|
|
|
2019-04-06 02:30:46 +02: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 05:54:16 +01: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 03:20:37 +01:00
|
|
|
|
2020-07-26 06:16:21 +02:00
|
|
|
u8* MemoryManager::GetPointer(GPUVAddr gpu_addr) {
|
|
|
|
const auto address{GpuToCpuAddress(gpu_addr)};
|
|
|
|
if (!address) {
|
|
|
|
return {};
|
2019-04-06 02:25:25 +02:00
|
|
|
}
|
|
|
|
|
2023-12-25 07:32:16 +01:00
|
|
|
return memory.GetPointer<u8>(*address);
|
2019-04-06 02:25:25 +02:00
|
|
|
}
|
|
|
|
|
2020-07-26 06:16:21 +02:00
|
|
|
const u8* MemoryManager::GetPointer(GPUVAddr gpu_addr) const {
|
|
|
|
const auto address{GpuToCpuAddress(gpu_addr)};
|
|
|
|
if (!address) {
|
|
|
|
return {};
|
2018-05-20 21:21:06 +02:00
|
|
|
}
|
|
|
|
|
2023-12-25 07:32:16 +01:00
|
|
|
return memory.GetPointer<u8>(*address);
|
2019-04-16 21:45:24 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 18:51:07 +01:00
|
|
|
#ifdef _MSC_VER // no need for gcc / clang but msvc's compiler is more conservative with inlining.
|
2022-02-05 18:15:26 +01:00
|
|
|
#pragma inline_recursion(on)
|
2022-02-06 18:51:07 +01:00
|
|
|
#endif
|
2022-02-05 18:15:26 +01:00
|
|
|
|
|
|
|
template <bool is_big_pages, typename FuncMapped, typename FuncReserved, typename FuncUnmapped>
|
|
|
|
inline void MemoryManager::MemoryOperation(GPUVAddr gpu_src_addr, std::size_t size,
|
|
|
|
FuncMapped&& func_mapped, FuncReserved&& func_reserved,
|
|
|
|
FuncUnmapped&& func_unmapped) const {
|
2022-10-21 01:46:51 +02:00
|
|
|
using FuncMappedReturn =
|
|
|
|
typename std::invoke_result<FuncMapped, std::size_t, std::size_t, std::size_t>::type;
|
|
|
|
using FuncReservedReturn =
|
|
|
|
typename std::invoke_result<FuncReserved, std::size_t, std::size_t, std::size_t>::type;
|
|
|
|
using FuncUnmappedReturn =
|
|
|
|
typename std::invoke_result<FuncUnmapped, std::size_t, std::size_t, std::size_t>::type;
|
|
|
|
static constexpr bool BOOL_BREAK_MAPPED = std::is_same_v<FuncMappedReturn, bool>;
|
|
|
|
static constexpr bool BOOL_BREAK_RESERVED = std::is_same_v<FuncReservedReturn, bool>;
|
|
|
|
static constexpr bool BOOL_BREAK_UNMAPPED = std::is_same_v<FuncUnmappedReturn, bool>;
|
2022-02-05 18:15:26 +01:00
|
|
|
u64 used_page_size;
|
|
|
|
u64 used_page_mask;
|
|
|
|
u64 used_page_bits;
|
|
|
|
if constexpr (is_big_pages) {
|
|
|
|
used_page_size = big_page_size;
|
|
|
|
used_page_mask = big_page_mask;
|
|
|
|
used_page_bits = big_page_bits;
|
|
|
|
} else {
|
|
|
|
used_page_size = page_size;
|
|
|
|
used_page_mask = page_mask;
|
|
|
|
used_page_bits = page_bits;
|
|
|
|
}
|
2019-03-29 02:58:28 +01:00
|
|
|
std::size_t remaining_size{size};
|
2022-02-05 18:15:26 +01:00
|
|
|
std::size_t page_index{gpu_src_addr >> used_page_bits};
|
|
|
|
std::size_t page_offset{gpu_src_addr & used_page_mask};
|
|
|
|
GPUVAddr current_address = gpu_src_addr;
|
2019-03-29 02:58:28 +01:00
|
|
|
|
|
|
|
while (remaining_size > 0) {
|
|
|
|
const std::size_t copy_amount{
|
2022-02-05 18:15:26 +01:00
|
|
|
std::min(static_cast<std::size_t>(used_page_size) - page_offset, remaining_size)};
|
|
|
|
auto entry = GetEntry<is_big_pages>(current_address);
|
|
|
|
if (entry == EntryType::Mapped) [[likely]] {
|
2022-02-06 18:51:07 +01:00
|
|
|
if constexpr (BOOL_BREAK_MAPPED) {
|
|
|
|
if (func_mapped(page_index, page_offset, copy_amount)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
func_mapped(page_index, page_offset, copy_amount);
|
|
|
|
}
|
|
|
|
|
2022-02-05 18:15:26 +01:00
|
|
|
} else if (entry == EntryType::Reserved) {
|
2022-02-06 18:51:07 +01:00
|
|
|
if constexpr (BOOL_BREAK_RESERVED) {
|
|
|
|
if (func_reserved(page_index, page_offset, copy_amount)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
func_reserved(page_index, page_offset, copy_amount);
|
|
|
|
}
|
|
|
|
|
2022-02-05 18:15:26 +01:00
|
|
|
} else [[unlikely]] {
|
2022-02-06 18:51:07 +01:00
|
|
|
if constexpr (BOOL_BREAK_UNMAPPED) {
|
|
|
|
if (func_unmapped(page_index, page_offset, copy_amount)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
func_unmapped(page_index, page_offset, copy_amount);
|
|
|
|
}
|
2020-07-26 06:16:21 +02:00
|
|
|
}
|
2019-03-29 02:58:28 +01:00
|
|
|
page_index++;
|
|
|
|
page_offset = 0;
|
|
|
|
remaining_size -= copy_amount;
|
2022-02-05 18:15:26 +01:00
|
|
|
current_address += copy_amount;
|
2019-03-29 02:58:28 +01:00
|
|
|
}
|
2019-03-04 05:54:16 +01:00
|
|
|
}
|
2019-03-29 02:58:28 +01:00
|
|
|
|
2023-01-25 11:12:04 +01:00
|
|
|
template <bool is_safe>
|
2022-11-20 03:07:14 +01:00
|
|
|
void MemoryManager::ReadBlockImpl(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size,
|
|
|
|
[[maybe_unused]] VideoCommon::CacheType which) const {
|
2022-02-05 18:15:26 +01:00
|
|
|
auto set_to_zero = [&]([[maybe_unused]] std::size_t page_index,
|
|
|
|
[[maybe_unused]] std::size_t offset, std::size_t copy_amount) {
|
|
|
|
std::memset(dest_buffer, 0, copy_amount);
|
|
|
|
dest_buffer = static_cast<u8*>(dest_buffer) + copy_amount;
|
|
|
|
};
|
|
|
|
auto mapped_normal = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(page_table[page_index]) << cpu_page_bits) + offset;
|
2022-02-05 18:15:26 +01:00
|
|
|
if constexpr (is_safe) {
|
2023-12-25 07:32:16 +01:00
|
|
|
rasterizer->FlushRegion(dev_addr_base, copy_amount, which);
|
2022-02-05 18:15:26 +01:00
|
|
|
}
|
2023-12-25 07:32:16 +01:00
|
|
|
u8* physical = memory.GetPointer<u8>(dev_addr_base);
|
2023-01-25 11:12:04 +01:00
|
|
|
std::memcpy(dest_buffer, physical, copy_amount);
|
2022-02-05 18:15:26 +01:00
|
|
|
dest_buffer = static_cast<u8*>(dest_buffer) + copy_amount;
|
|
|
|
};
|
|
|
|
auto mapped_big = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(big_page_table_dev[page_index]) << cpu_page_bits) + offset;
|
2022-02-05 18:15:26 +01:00
|
|
|
if constexpr (is_safe) {
|
2023-12-25 07:32:16 +01:00
|
|
|
rasterizer->FlushRegion(dev_addr_base, copy_amount, which);
|
2022-02-05 18:15:26 +01:00
|
|
|
}
|
2023-03-30 04:26:12 +02:00
|
|
|
if (!IsBigPageContinuous(page_index)) [[unlikely]] {
|
2023-12-25 07:32:16 +01:00
|
|
|
memory.ReadBlockUnsafe(dev_addr_base, dest_buffer, copy_amount);
|
2022-02-06 18:51:07 +01:00
|
|
|
} else {
|
2023-12-25 07:32:16 +01:00
|
|
|
u8* physical = memory.GetPointer<u8>(dev_addr_base);
|
2023-01-25 11:12:04 +01:00
|
|
|
std::memcpy(dest_buffer, physical, copy_amount);
|
2022-02-06 18:51:07 +01:00
|
|
|
}
|
2022-02-05 18:15:26 +01:00
|
|
|
dest_buffer = static_cast<u8*>(dest_buffer) + copy_amount;
|
|
|
|
};
|
|
|
|
auto read_short_pages = [&](std::size_t page_index, std::size_t offset,
|
|
|
|
std::size_t copy_amount) {
|
|
|
|
GPUVAddr base = (page_index << big_page_bits) + offset;
|
|
|
|
MemoryOperation<false>(base, copy_amount, mapped_normal, set_to_zero, set_to_zero);
|
|
|
|
};
|
|
|
|
MemoryOperation<true>(gpu_src_addr, size, mapped_big, set_to_zero, read_short_pages);
|
|
|
|
}
|
|
|
|
|
2022-11-20 03:07:14 +01:00
|
|
|
void MemoryManager::ReadBlock(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size,
|
|
|
|
VideoCommon::CacheType which) const {
|
2023-01-25 11:12:04 +01:00
|
|
|
ReadBlockImpl<true>(gpu_src_addr, dest_buffer, size, which);
|
2021-12-31 05:36:00 +01:00
|
|
|
}
|
|
|
|
|
2020-06-20 04:02:56 +02:00
|
|
|
void MemoryManager::ReadBlockUnsafe(GPUVAddr gpu_src_addr, void* dest_buffer,
|
2019-04-16 16:11:35 +02:00
|
|
|
const std::size_t size) const {
|
2023-01-25 11:12:04 +01:00
|
|
|
ReadBlockImpl<false>(gpu_src_addr, dest_buffer, size, VideoCommon::CacheType::None);
|
2019-04-16 05:01:35 +02:00
|
|
|
}
|
|
|
|
|
2022-02-05 18:15:26 +01:00
|
|
|
template <bool is_safe>
|
2022-11-20 03:07:14 +01:00
|
|
|
void MemoryManager::WriteBlockImpl(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size,
|
|
|
|
[[maybe_unused]] VideoCommon::CacheType which) {
|
2022-02-05 18:15:26 +01:00
|
|
|
auto just_advance = [&]([[maybe_unused]] std::size_t page_index,
|
|
|
|
[[maybe_unused]] std::size_t offset, std::size_t copy_amount) {
|
|
|
|
src_buffer = static_cast<const u8*>(src_buffer) + copy_amount;
|
|
|
|
};
|
|
|
|
auto mapped_normal = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(page_table[page_index]) << cpu_page_bits) + offset;
|
2022-02-05 18:15:26 +01:00
|
|
|
if constexpr (is_safe) {
|
2023-12-25 07:32:16 +01:00
|
|
|
rasterizer->InvalidateRegion(dev_addr_base, copy_amount, which);
|
2020-07-26 06:16:21 +02:00
|
|
|
}
|
2023-12-25 07:32:16 +01:00
|
|
|
u8* physical = memory.GetPointer<u8>(dev_addr_base);
|
2022-02-06 18:51:07 +01:00
|
|
|
std::memcpy(physical, src_buffer, copy_amount);
|
2019-03-29 02:58:28 +01:00
|
|
|
src_buffer = static_cast<const u8*>(src_buffer) + copy_amount;
|
2022-02-05 18:15:26 +01:00
|
|
|
};
|
|
|
|
auto mapped_big = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(big_page_table_dev[page_index]) << cpu_page_bits) + offset;
|
2022-02-05 18:15:26 +01:00
|
|
|
if constexpr (is_safe) {
|
2023-12-25 07:32:16 +01:00
|
|
|
rasterizer->InvalidateRegion(dev_addr_base, copy_amount, which);
|
2022-02-05 18:15:26 +01:00
|
|
|
}
|
2023-03-30 04:26:12 +02:00
|
|
|
if (!IsBigPageContinuous(page_index)) [[unlikely]] {
|
2023-12-25 07:32:16 +01:00
|
|
|
memory.WriteBlockUnsafe(dev_addr_base, src_buffer, copy_amount);
|
2022-02-06 18:51:07 +01:00
|
|
|
} else {
|
2023-12-25 07:32:16 +01:00
|
|
|
u8* physical = memory.GetPointer<u8>(dev_addr_base);
|
2022-02-06 18:51:07 +01:00
|
|
|
std::memcpy(physical, src_buffer, copy_amount);
|
|
|
|
}
|
2022-02-05 18:15:26 +01:00
|
|
|
src_buffer = static_cast<const u8*>(src_buffer) + copy_amount;
|
|
|
|
};
|
|
|
|
auto write_short_pages = [&](std::size_t page_index, std::size_t offset,
|
|
|
|
std::size_t copy_amount) {
|
|
|
|
GPUVAddr base = (page_index << big_page_bits) + offset;
|
|
|
|
MemoryOperation<false>(base, copy_amount, mapped_normal, just_advance, just_advance);
|
|
|
|
};
|
|
|
|
MemoryOperation<true>(gpu_dest_addr, size, mapped_big, just_advance, write_short_pages);
|
2019-03-04 05:54:16 +01:00
|
|
|
}
|
|
|
|
|
2022-11-20 03:07:14 +01:00
|
|
|
void MemoryManager::WriteBlock(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size,
|
|
|
|
VideoCommon::CacheType which) {
|
|
|
|
WriteBlockImpl<true>(gpu_dest_addr, src_buffer, size, which);
|
2021-12-31 05:36:00 +01:00
|
|
|
}
|
|
|
|
|
2020-06-20 04:02:56 +02:00
|
|
|
void MemoryManager::WriteBlockUnsafe(GPUVAddr gpu_dest_addr, const void* src_buffer,
|
2020-07-26 06:16:21 +02:00
|
|
|
std::size_t size) {
|
2022-11-20 03:07:14 +01:00
|
|
|
WriteBlockImpl<false>(gpu_dest_addr, src_buffer, size, VideoCommon::CacheType::None);
|
2019-04-16 05:01:35 +02:00
|
|
|
}
|
|
|
|
|
2023-01-05 04:05:20 +01:00
|
|
|
void MemoryManager::WriteBlockCached(GPUVAddr gpu_dest_addr, const void* src_buffer,
|
|
|
|
std::size_t size) {
|
|
|
|
WriteBlockImpl<false>(gpu_dest_addr, src_buffer, size, VideoCommon::CacheType::None);
|
|
|
|
accumulator->Add(gpu_dest_addr, size);
|
|
|
|
}
|
|
|
|
|
2022-11-20 03:07:14 +01:00
|
|
|
void MemoryManager::FlushRegion(GPUVAddr gpu_addr, size_t size,
|
|
|
|
VideoCommon::CacheType which) const {
|
2022-02-05 18:15:26 +01:00
|
|
|
auto do_nothing = [&]([[maybe_unused]] std::size_t page_index,
|
|
|
|
[[maybe_unused]] std::size_t offset,
|
|
|
|
[[maybe_unused]] std::size_t copy_amount) {};
|
|
|
|
|
|
|
|
auto mapped_normal = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(page_table[page_index]) << cpu_page_bits) + offset;
|
|
|
|
rasterizer->FlushRegion(dev_addr_base, copy_amount, which);
|
2022-02-05 18:15:26 +01:00
|
|
|
};
|
|
|
|
auto mapped_big = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(big_page_table_dev[page_index]) << cpu_page_bits) + offset;
|
|
|
|
rasterizer->FlushRegion(dev_addr_base, copy_amount, which);
|
2022-02-05 18:15:26 +01:00
|
|
|
};
|
|
|
|
auto flush_short_pages = [&](std::size_t page_index, std::size_t offset,
|
|
|
|
std::size_t copy_amount) {
|
|
|
|
GPUVAddr base = (page_index << big_page_bits) + offset;
|
|
|
|
MemoryOperation<false>(base, copy_amount, mapped_normal, do_nothing, do_nothing);
|
|
|
|
};
|
|
|
|
MemoryOperation<true>(gpu_addr, size, mapped_big, do_nothing, flush_short_pages);
|
2021-01-21 23:08:15 +01:00
|
|
|
}
|
|
|
|
|
2022-11-20 03:07:14 +01:00
|
|
|
bool MemoryManager::IsMemoryDirty(GPUVAddr gpu_addr, size_t size,
|
|
|
|
VideoCommon::CacheType which) const {
|
2022-08-14 11:36:36 +02:00
|
|
|
bool result = false;
|
|
|
|
auto do_nothing = [&]([[maybe_unused]] std::size_t page_index,
|
|
|
|
[[maybe_unused]] std::size_t offset,
|
|
|
|
[[maybe_unused]] std::size_t copy_amount) { return false; };
|
|
|
|
|
|
|
|
auto mapped_normal = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(page_table[page_index]) << cpu_page_bits) + offset;
|
|
|
|
result |= rasterizer->MustFlushRegion(dev_addr_base, copy_amount, which);
|
2022-08-14 11:36:36 +02:00
|
|
|
return result;
|
|
|
|
};
|
|
|
|
auto mapped_big = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(big_page_table_dev[page_index]) << cpu_page_bits) + offset;
|
|
|
|
result |= rasterizer->MustFlushRegion(dev_addr_base, copy_amount, which);
|
2022-08-14 11:36:36 +02:00
|
|
|
return result;
|
|
|
|
};
|
|
|
|
auto check_short_pages = [&](std::size_t page_index, std::size_t offset,
|
|
|
|
std::size_t copy_amount) {
|
|
|
|
GPUVAddr base = (page_index << big_page_bits) + offset;
|
|
|
|
MemoryOperation<false>(base, copy_amount, mapped_normal, do_nothing, do_nothing);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
MemoryOperation<true>(gpu_addr, size, mapped_big, do_nothing, check_short_pages);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-30 04:26:12 +02:00
|
|
|
size_t MemoryManager::MaxContinuousRange(GPUVAddr gpu_addr, size_t size) const {
|
2023-12-25 07:32:16 +01:00
|
|
|
std::optional<DAddr> old_page_addr{};
|
2022-08-14 11:36:36 +02:00
|
|
|
size_t range_so_far = 0;
|
|
|
|
bool result{false};
|
|
|
|
auto fail = [&]([[maybe_unused]] std::size_t page_index, [[maybe_unused]] std::size_t offset,
|
|
|
|
std::size_t copy_amount) {
|
|
|
|
result = true;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
auto short_check = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(page_table[page_index]) << cpu_page_bits) + offset;
|
|
|
|
if (old_page_addr && *old_page_addr != dev_addr_base) {
|
2022-08-14 11:36:36 +02:00
|
|
|
result = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
range_so_far += copy_amount;
|
2023-12-25 07:32:16 +01:00
|
|
|
old_page_addr = {dev_addr_base + copy_amount};
|
2022-08-14 11:36:36 +02:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
auto big_check = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(big_page_table_dev[page_index]) << cpu_page_bits) + offset;
|
|
|
|
if (old_page_addr && *old_page_addr != dev_addr_base) {
|
2022-08-14 11:36:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
range_so_far += copy_amount;
|
2023-12-25 07:32:16 +01:00
|
|
|
old_page_addr = {dev_addr_base + copy_amount};
|
2022-08-14 11:36:36 +02:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
auto check_short_pages = [&](std::size_t page_index, std::size_t offset,
|
|
|
|
std::size_t copy_amount) {
|
|
|
|
GPUVAddr base = (page_index << big_page_bits) + offset;
|
|
|
|
MemoryOperation<false>(base, copy_amount, short_check, fail, fail);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
MemoryOperation<true>(gpu_addr, size, big_check, fail, check_short_pages);
|
|
|
|
return range_so_far;
|
|
|
|
}
|
|
|
|
|
2022-11-09 17:58:10 +01:00
|
|
|
size_t MemoryManager::GetMemoryLayoutSize(GPUVAddr gpu_addr, size_t max_size) const {
|
2023-04-28 23:53:02 +02:00
|
|
|
std::unique_lock<std::mutex> lock(guard);
|
2023-03-30 04:26:12 +02:00
|
|
|
return kind_map.GetContinuousSizeFrom(gpu_addr);
|
2022-10-21 01:46:51 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 03:07:14 +01:00
|
|
|
void MemoryManager::InvalidateRegion(GPUVAddr gpu_addr, size_t size,
|
|
|
|
VideoCommon::CacheType which) const {
|
2022-08-14 11:36:36 +02:00
|
|
|
auto do_nothing = [&]([[maybe_unused]] std::size_t page_index,
|
|
|
|
[[maybe_unused]] std::size_t offset,
|
|
|
|
[[maybe_unused]] std::size_t copy_amount) {};
|
|
|
|
|
|
|
|
auto mapped_normal = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(page_table[page_index]) << cpu_page_bits) + offset;
|
|
|
|
rasterizer->InvalidateRegion(dev_addr_base, copy_amount, which);
|
2022-08-14 11:36:36 +02:00
|
|
|
};
|
|
|
|
auto mapped_big = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(big_page_table_dev[page_index]) << cpu_page_bits) + offset;
|
|
|
|
rasterizer->InvalidateRegion(dev_addr_base, copy_amount, which);
|
2022-08-14 11:36:36 +02:00
|
|
|
};
|
|
|
|
auto invalidate_short_pages = [&](std::size_t page_index, std::size_t offset,
|
|
|
|
std::size_t copy_amount) {
|
|
|
|
GPUVAddr base = (page_index << big_page_bits) + offset;
|
|
|
|
MemoryOperation<false>(base, copy_amount, mapped_normal, do_nothing, do_nothing);
|
|
|
|
};
|
|
|
|
MemoryOperation<true>(gpu_addr, size, mapped_big, do_nothing, invalidate_short_pages);
|
|
|
|
}
|
|
|
|
|
2022-11-20 03:07:14 +01:00
|
|
|
void MemoryManager::CopyBlock(GPUVAddr gpu_dest_addr, GPUVAddr gpu_src_addr, std::size_t size,
|
|
|
|
VideoCommon::CacheType which) {
|
2023-12-25 07:32:16 +01:00
|
|
|
Tegra::Memory::GpuGuestMemoryScoped<u8, GuestMemoryFlags::SafeReadWrite> data(
|
2023-05-29 01:35:51 +02:00
|
|
|
*this, gpu_src_addr, size);
|
|
|
|
data.SetAddressAndSize(gpu_dest_addr, size);
|
2022-11-20 03:07:14 +01:00
|
|
|
FlushRegion(gpu_dest_addr, size, which);
|
2018-10-13 03:52:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-24 06:37:54 +02:00
|
|
|
bool MemoryManager::IsGranularRange(GPUVAddr gpu_addr, std::size_t size) const {
|
2022-02-06 18:51:07 +01:00
|
|
|
if (GetEntry<true>(gpu_addr) == EntryType::Mapped) [[likely]] {
|
|
|
|
size_t page_index = gpu_addr >> big_page_bits;
|
2023-03-30 04:26:12 +02:00
|
|
|
if (IsBigPageContinuous(page_index)) [[likely]] {
|
2022-02-06 18:51:07 +01:00
|
|
|
const std::size_t page{(page_index & big_page_mask) + size};
|
|
|
|
return page <= big_page_size;
|
|
|
|
}
|
|
|
|
const std::size_t page{(gpu_addr & Core::Memory::YUZU_PAGEMASK) + size};
|
|
|
|
return page <= Core::Memory::YUZU_PAGESIZE;
|
|
|
|
}
|
|
|
|
if (GetEntry<false>(gpu_addr) != EntryType::Mapped) {
|
2020-08-24 06:37:54 +02:00
|
|
|
return false;
|
2019-03-04 05:54:16 +01:00
|
|
|
}
|
2022-02-06 18:51:07 +01:00
|
|
|
const std::size_t page{(gpu_addr & Core::Memory::YUZU_PAGEMASK) + size};
|
2022-08-19 01:28:55 +02:00
|
|
|
return page <= Core::Memory::YUZU_PAGESIZE;
|
2018-02-08 03:54:35 +01:00
|
|
|
}
|
|
|
|
|
2023-03-30 04:26:12 +02:00
|
|
|
bool MemoryManager::IsContinuousRange(GPUVAddr gpu_addr, std::size_t size) const {
|
2023-12-25 07:32:16 +01:00
|
|
|
std::optional<DAddr> old_page_addr{};
|
2022-02-06 18:51:07 +01:00
|
|
|
bool result{true};
|
|
|
|
auto fail = [&]([[maybe_unused]] std::size_t page_index, [[maybe_unused]] std::size_t offset,
|
|
|
|
std::size_t copy_amount) {
|
|
|
|
result = false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
auto short_check = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(page_table[page_index]) << cpu_page_bits) + offset;
|
|
|
|
if (old_page_addr && *old_page_addr != dev_addr_base) {
|
2022-02-06 18:51:07 +01:00
|
|
|
result = false;
|
|
|
|
return true;
|
2021-06-13 03:34:06 +02:00
|
|
|
}
|
2023-12-25 07:32:16 +01:00
|
|
|
old_page_addr = {dev_addr_base + copy_amount};
|
2022-02-06 18:51:07 +01:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
auto big_check = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(big_page_table_dev[page_index]) << cpu_page_bits) + offset;
|
|
|
|
if (old_page_addr && *old_page_addr != dev_addr_base) {
|
2022-02-06 18:51:07 +01:00
|
|
|
result = false;
|
|
|
|
return true;
|
2021-06-13 03:34:06 +02:00
|
|
|
}
|
2023-12-25 07:32:16 +01:00
|
|
|
old_page_addr = {dev_addr_base + copy_amount};
|
2022-02-06 18:51:07 +01:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
auto check_short_pages = [&](std::size_t page_index, std::size_t offset,
|
|
|
|
std::size_t copy_amount) {
|
|
|
|
GPUVAddr base = (page_index << big_page_bits) + offset;
|
|
|
|
MemoryOperation<false>(base, copy_amount, short_check, fail, fail);
|
|
|
|
return !result;
|
|
|
|
};
|
|
|
|
MemoryOperation<true>(gpu_addr, size, big_check, fail, check_short_pages);
|
|
|
|
return result;
|
2021-06-13 03:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MemoryManager::IsFullyMappedRange(GPUVAddr gpu_addr, std::size_t size) const {
|
2022-02-06 18:51:07 +01:00
|
|
|
bool result{true};
|
|
|
|
auto fail = [&]([[maybe_unused]] std::size_t page_index, [[maybe_unused]] std::size_t offset,
|
|
|
|
[[maybe_unused]] std::size_t copy_amount) {
|
|
|
|
result = false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
auto pass = [&]([[maybe_unused]] std::size_t page_index, [[maybe_unused]] std::size_t offset,
|
|
|
|
[[maybe_unused]] std::size_t copy_amount) { return false; };
|
|
|
|
auto check_short_pages = [&](std::size_t page_index, std::size_t offset,
|
|
|
|
std::size_t copy_amount) {
|
|
|
|
GPUVAddr base = (page_index << big_page_bits) + offset;
|
|
|
|
MemoryOperation<false>(base, copy_amount, pass, pass, fail);
|
|
|
|
return !result;
|
|
|
|
};
|
|
|
|
MemoryOperation<true>(gpu_addr, size, pass, fail, check_short_pages);
|
|
|
|
return result;
|
2021-06-13 03:34:06 +02:00
|
|
|
}
|
|
|
|
|
2023-05-23 15:45:54 +02:00
|
|
|
boost::container::small_vector<std::pair<GPUVAddr, std::size_t>, 32>
|
|
|
|
MemoryManager::GetSubmappedRange(GPUVAddr gpu_addr, std::size_t size) const {
|
|
|
|
boost::container::small_vector<std::pair<GPUVAddr, std::size_t>, 32> result{};
|
2023-01-05 04:05:20 +01:00
|
|
|
GetSubmappedRangeImpl<true>(gpu_addr, size, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool is_gpu_address>
|
|
|
|
void MemoryManager::GetSubmappedRangeImpl(
|
|
|
|
GPUVAddr gpu_addr, std::size_t size,
|
2023-05-23 15:45:54 +02:00
|
|
|
boost::container::small_vector<
|
2023-12-25 07:32:16 +01:00
|
|
|
std::pair<std::conditional_t<is_gpu_address, GPUVAddr, DAddr>, std::size_t>, 32>& result)
|
2023-05-23 15:45:54 +02:00
|
|
|
const {
|
2023-12-25 07:32:16 +01:00
|
|
|
std::optional<std::pair<std::conditional_t<is_gpu_address, GPUVAddr, DAddr>, std::size_t>>
|
2023-01-05 04:05:20 +01:00
|
|
|
last_segment{};
|
2023-12-25 07:32:16 +01:00
|
|
|
std::optional<DAddr> old_page_addr{};
|
2022-02-05 18:15:26 +01:00
|
|
|
const auto split = [&last_segment, &result]([[maybe_unused]] std::size_t page_index,
|
|
|
|
[[maybe_unused]] std::size_t offset,
|
|
|
|
[[maybe_unused]] std::size_t copy_amount) {
|
2021-06-13 03:34:06 +02:00
|
|
|
if (last_segment) {
|
|
|
|
result.push_back(*last_segment);
|
|
|
|
last_segment = std::nullopt;
|
|
|
|
}
|
|
|
|
};
|
2022-02-05 18:15:26 +01:00
|
|
|
const auto extend_size_big = [this, &split, &old_page_addr,
|
|
|
|
&last_segment](std::size_t page_index, std::size_t offset,
|
|
|
|
std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(big_page_table_dev[page_index]) << cpu_page_bits) + offset;
|
2022-02-05 18:15:26 +01:00
|
|
|
if (old_page_addr) {
|
2023-12-25 07:32:16 +01:00
|
|
|
if (*old_page_addr != dev_addr_base) {
|
2022-02-05 18:15:26 +01:00
|
|
|
split(0, 0, 0);
|
2021-06-13 03:34:06 +02:00
|
|
|
}
|
2022-02-05 18:15:26 +01:00
|
|
|
}
|
2023-12-25 07:32:16 +01:00
|
|
|
old_page_addr = {dev_addr_base + copy_amount};
|
2022-02-05 18:15:26 +01:00
|
|
|
if (!last_segment) {
|
2023-01-05 04:05:20 +01:00
|
|
|
if constexpr (is_gpu_address) {
|
|
|
|
const GPUVAddr new_base_addr = (page_index << big_page_bits) + offset;
|
|
|
|
last_segment = {new_base_addr, copy_amount};
|
|
|
|
} else {
|
2023-12-25 07:32:16 +01:00
|
|
|
last_segment = {dev_addr_base, copy_amount};
|
2023-01-05 04:05:20 +01:00
|
|
|
}
|
2021-06-13 03:34:06 +02:00
|
|
|
} else {
|
2022-02-05 18:15:26 +01:00
|
|
|
last_segment->second += copy_amount;
|
2021-06-13 03:34:06 +02:00
|
|
|
}
|
2022-02-05 18:15:26 +01:00
|
|
|
};
|
|
|
|
const auto extend_size_short = [this, &split, &old_page_addr,
|
|
|
|
&last_segment](std::size_t page_index, std::size_t offset,
|
|
|
|
std::size_t copy_amount) {
|
2023-12-25 07:32:16 +01:00
|
|
|
const DAddr dev_addr_base =
|
|
|
|
(static_cast<DAddr>(page_table[page_index]) << cpu_page_bits) + offset;
|
2022-02-05 18:15:26 +01:00
|
|
|
if (old_page_addr) {
|
2023-12-25 07:32:16 +01:00
|
|
|
if (*old_page_addr != dev_addr_base) {
|
2022-02-05 18:15:26 +01:00
|
|
|
split(0, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
2023-12-25 07:32:16 +01:00
|
|
|
old_page_addr = {dev_addr_base + copy_amount};
|
2022-02-05 18:15:26 +01:00
|
|
|
if (!last_segment) {
|
2023-01-05 04:05:20 +01:00
|
|
|
if constexpr (is_gpu_address) {
|
|
|
|
const GPUVAddr new_base_addr = (page_index << page_bits) + offset;
|
|
|
|
last_segment = {new_base_addr, copy_amount};
|
|
|
|
} else {
|
2023-12-25 07:32:16 +01:00
|
|
|
last_segment = {dev_addr_base, copy_amount};
|
2023-01-05 04:05:20 +01:00
|
|
|
}
|
2022-02-05 18:15:26 +01:00
|
|
|
} else {
|
|
|
|
last_segment->second += copy_amount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
auto do_short_pages = [&](std::size_t page_index, std::size_t offset, std::size_t copy_amount) {
|
|
|
|
GPUVAddr base = (page_index << big_page_bits) + offset;
|
|
|
|
MemoryOperation<false>(base, copy_amount, extend_size_short, split, split);
|
|
|
|
};
|
|
|
|
MemoryOperation<true>(gpu_addr, size, extend_size_big, split, do_short_pages);
|
|
|
|
split(0, 0, 0);
|
2023-01-05 04:05:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryManager::FlushCaching() {
|
|
|
|
if (!accumulator->AnyAccumulated()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
accumulator->Callback([this](GPUVAddr addr, size_t size) {
|
2023-04-28 23:53:02 +02:00
|
|
|
GetSubmappedRangeImpl<false>(addr, size, page_stash2);
|
2023-01-05 04:05:20 +01:00
|
|
|
});
|
2023-04-28 23:53:02 +02:00
|
|
|
rasterizer->InnerInvalidation(page_stash2);
|
|
|
|
page_stash2.clear();
|
2023-01-05 04:05:20 +01:00
|
|
|
accumulator->Clear();
|
2021-06-13 03:34:06 +02:00
|
|
|
}
|
|
|
|
|
2023-05-29 01:35:51 +02:00
|
|
|
const u8* MemoryManager::GetSpan(const GPUVAddr src_addr, const std::size_t size) const {
|
2024-01-05 03:47:48 +01:00
|
|
|
if (!IsContinuousRange(src_addr, size)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-12-25 07:32:16 +01:00
|
|
|
auto dev_addr = GpuToCpuAddress(src_addr);
|
|
|
|
if (dev_addr) {
|
|
|
|
return memory.GetSpan(*dev_addr, size);
|
2023-05-29 01:35:51 +02:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* MemoryManager::GetSpan(const GPUVAddr src_addr, const std::size_t size) {
|
|
|
|
if (!IsContinuousRange(src_addr, size)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-12-25 07:32:16 +01:00
|
|
|
auto dev_addr = GpuToCpuAddress(src_addr);
|
|
|
|
if (dev_addr) {
|
|
|
|
return memory.GetSpan(*dev_addr, size);
|
2023-05-29 01:35:51 +02:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-12 05:44:12 +01:00
|
|
|
} // namespace Tegra
|