2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-02-07 21:54:35 -05:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-12-17 10:45:06 -05:00
|
|
|
#include <atomic>
|
2019-03-03 23:54:16 -05:00
|
|
|
#include <map>
|
2018-10-30 00:03:25 -04:00
|
|
|
#include <optional>
|
2020-07-26 00:16:21 -04:00
|
|
|
#include <vector>
|
2018-04-21 12:31:30 -04:00
|
|
|
|
2018-02-07 21:54:35 -05:00
|
|
|
#include "common/common_types.h"
|
2021-11-11 15:24:40 -05:00
|
|
|
#include "common/multi_level_page_table.h"
|
2018-02-07 21:54:35 -05:00
|
|
|
|
2020-02-15 17:47:15 -05:00
|
|
|
namespace VideoCore {
|
|
|
|
class RasterizerInterface;
|
|
|
|
}
|
|
|
|
|
2019-07-09 02:17:44 -04:00
|
|
|
namespace Core {
|
|
|
|
class System;
|
|
|
|
}
|
|
|
|
|
2018-02-11 23:44:12 -05:00
|
|
|
namespace Tegra {
|
|
|
|
|
2018-02-07 21:54:35 -05:00
|
|
|
class MemoryManager final {
|
|
|
|
public:
|
2021-11-11 15:24:40 -05:00
|
|
|
explicit MemoryManager(Core::System& system_, u64 address_space_bits_ = 40,
|
|
|
|
u64 page_bits_ = 16);
|
2019-05-09 19:04:41 -04:00
|
|
|
~MemoryManager();
|
2018-02-07 21:54:35 -05:00
|
|
|
|
2021-12-17 10:45:06 -05:00
|
|
|
size_t GetID() const {
|
|
|
|
return unique_identifier;
|
|
|
|
}
|
|
|
|
|
2020-06-10 23:58:57 -04:00
|
|
|
/// Binds a renderer to the memory manager.
|
2021-01-05 02:09:39 -05:00
|
|
|
void BindRasterizer(VideoCore::RasterizerInterface* rasterizer);
|
2020-06-10 23:58:57 -04:00
|
|
|
|
2020-08-26 20:14:13 -04:00
|
|
|
[[nodiscard]] std::optional<VAddr> GpuToCpuAddress(GPUVAddr addr) const;
|
2018-02-07 21:54:35 -05:00
|
|
|
|
2021-06-12 21:34:06 -04:00
|
|
|
[[nodiscard]] std::optional<VAddr> GpuToCpuAddress(GPUVAddr addr, std::size_t size) const;
|
|
|
|
|
2019-03-03 23:54:16 -05:00
|
|
|
template <typename T>
|
2020-08-26 20:14:13 -04:00
|
|
|
[[nodiscard]] T Read(GPUVAddr addr) const;
|
2019-02-24 00:15:35 -05:00
|
|
|
|
2019-03-03 23:54:16 -05:00
|
|
|
template <typename T>
|
2019-03-09 14:06:51 -05:00
|
|
|
void Write(GPUVAddr addr, T data);
|
2019-02-24 00:15:35 -05:00
|
|
|
|
2020-08-26 20:14:13 -04:00
|
|
|
[[nodiscard]] u8* GetPointer(GPUVAddr addr);
|
|
|
|
[[nodiscard]] const u8* GetPointer(GPUVAddr addr) const;
|
2019-02-24 00:15:35 -05:00
|
|
|
|
2019-04-16 15:45:24 -04:00
|
|
|
/**
|
2019-04-16 10:11:35 -04:00
|
|
|
* ReadBlock and WriteBlock are full read and write operations over virtual
|
2019-05-09 19:02:52 -04:00
|
|
|
* GPU Memory. It's important to use these when GPU memory may not be continuous
|
2019-04-16 10:11:35 -04:00
|
|
|
* in the Host Memory counterpart. Note: This functions cause Host GPU Memory
|
|
|
|
* Flushes and Invalidations, respectively to each operation.
|
|
|
|
*/
|
2020-06-19 22:02:56 -04: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 10:11:35 -04:00
|
|
|
|
2019-04-16 15:45:24 -04:00
|
|
|
/**
|
2019-04-16 10:11:35 -04: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-19 22:02:56 -04: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);
|
2019-04-16 10:11:35 -04:00
|
|
|
|
2020-04-08 13:34:59 -04:00
|
|
|
/**
|
2021-06-20 06:25:59 -04:00
|
|
|
* Checks if a gpu region can be simply read with a pointer.
|
2020-04-08 13:34:59 -04:00
|
|
|
*/
|
2020-08-26 20:14:13 -04:00
|
|
|
[[nodiscard]] bool IsGranularRange(GPUVAddr gpu_addr, std::size_t size) const;
|
2020-04-05 17:23:49 -04:00
|
|
|
|
2021-06-12 21:34:06 -04:00
|
|
|
/**
|
2021-06-20 06:25:59 -04:00
|
|
|
* Checks if a gpu region is mapped by a single range of cpu addresses.
|
2021-06-12 21:34:06 -04:00
|
|
|
*/
|
|
|
|
[[nodiscard]] bool IsContinousRange(GPUVAddr gpu_addr, std::size_t size) const;
|
|
|
|
|
|
|
|
/**
|
2021-06-20 06:25:59 -04:00
|
|
|
* Checks if a gpu region is mapped entirely.
|
2021-06-12 21:34:06 -04:00
|
|
|
*/
|
|
|
|
[[nodiscard]] bool IsFullyMappedRange(GPUVAddr gpu_addr, std::size_t size) const;
|
|
|
|
|
|
|
|
/**
|
2021-06-20 06:25:59 -04:00
|
|
|
* Returns a vector with all the subranges of cpu addresses mapped beneath.
|
2021-06-12 21:34:06 -04:00
|
|
|
* if the region is continous, a single pair will be returned. If it's unmapped, an empty vector
|
|
|
|
* will be returned;
|
|
|
|
*/
|
|
|
|
std::vector<std::pair<GPUVAddr, std::size_t>> GetSubmappedRange(GPUVAddr gpu_addr,
|
|
|
|
std::size_t size) const;
|
|
|
|
|
2021-11-14 14:55:52 -05:00
|
|
|
GPUVAddr Map(GPUVAddr gpu_addr, VAddr cpu_addr, std::size_t size);
|
|
|
|
GPUVAddr MapSparse(GPUVAddr gpu_addr, std::size_t size);
|
2020-08-26 20:14:13 -04:00
|
|
|
[[nodiscard]] GPUVAddr MapAllocate(VAddr cpu_addr, std::size_t size, std::size_t align);
|
2020-10-26 23:07:36 -04:00
|
|
|
[[nodiscard]] GPUVAddr MapAllocate32(VAddr cpu_addr, std::size_t size);
|
2020-08-26 20:14:13 -04:00
|
|
|
[[nodiscard]] std::optional<GPUVAddr> AllocateFixed(GPUVAddr gpu_addr, std::size_t size);
|
|
|
|
[[nodiscard]] GPUVAddr Allocate(std::size_t size, std::size_t align);
|
2020-07-26 00:16:21 -04:00
|
|
|
void Unmap(GPUVAddr gpu_addr, std::size_t size);
|
2019-03-03 23:54:16 -05:00
|
|
|
|
2022-01-29 11:42:28 -05:00
|
|
|
void FlushRegion(GPUVAddr gpu_addr, size_t size) const;
|
|
|
|
|
2020-07-26 00:16:21 -04:00
|
|
|
private:
|
2020-10-26 23:07:36 -04:00
|
|
|
[[nodiscard]] std::optional<GPUVAddr> FindFreeRange(std::size_t size, std::size_t align,
|
|
|
|
bool start_32bit_address = false) const;
|
2019-03-03 23:54:16 -05:00
|
|
|
|
2021-12-30 23:36:00 -05:00
|
|
|
void ReadBlockImpl(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size,
|
|
|
|
bool is_safe) const;
|
|
|
|
void WriteBlockImpl(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size,
|
|
|
|
bool is_safe);
|
|
|
|
|
2021-11-11 15:24:40 -05:00
|
|
|
[[nodiscard]] inline std::size_t PageEntryIndex(GPUVAddr gpu_addr) const {
|
2020-07-26 00:16:21 -04:00
|
|
|
return (gpu_addr >> page_bits) & page_table_mask;
|
|
|
|
}
|
2018-04-21 14:40:51 -04:00
|
|
|
|
2020-07-26 00:16:21 -04:00
|
|
|
Core::System& system;
|
2019-03-03 23:54:16 -05:00
|
|
|
|
2021-11-11 15:24:40 -05:00
|
|
|
const u64 address_space_bits;
|
|
|
|
const u64 page_bits;
|
|
|
|
u64 address_space_size;
|
|
|
|
u64 allocate_start;
|
|
|
|
u64 page_size;
|
|
|
|
u64 page_mask;
|
|
|
|
u64 page_table_mask;
|
|
|
|
static constexpr u64 cpu_page_bits{12};
|
|
|
|
|
2020-06-10 23:58:57 -04:00
|
|
|
VideoCore::RasterizerInterface* rasterizer = nullptr;
|
2019-07-09 02:17:44 -04:00
|
|
|
|
2021-11-11 15:24:40 -05:00
|
|
|
enum class EntryType : u64 {
|
|
|
|
Free = 0,
|
|
|
|
Reserved = 1,
|
|
|
|
Mapped = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<u64> entries;
|
|
|
|
|
|
|
|
template <EntryType entry_type>
|
|
|
|
GPUVAddr PageTableOp(GPUVAddr gpu_addr, [[maybe_unused]] VAddr cpu_addr, size_t size);
|
|
|
|
|
|
|
|
EntryType GetEntry(size_t position) const;
|
2021-01-22 16:31:08 -05:00
|
|
|
|
2021-11-11 15:24:40 -05:00
|
|
|
void SetEntry(size_t position, EntryType entry);
|
2021-01-22 16:33:10 -05:00
|
|
|
|
2021-11-11 15:24:40 -05:00
|
|
|
Common::MultiLevelPageTable<u32> page_table;
|
2021-12-17 10:45:06 -05:00
|
|
|
|
|
|
|
const size_t unique_identifier;
|
|
|
|
|
|
|
|
static std::atomic<size_t> unique_identifier_generator;
|
2018-02-07 21:54:35 -05:00
|
|
|
};
|
|
|
|
|
2018-02-11 23:44:12 -05:00
|
|
|
} // namespace Tegra
|