memory_manager.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <atomic>
  5. #include <map>
  6. #include <optional>
  7. #include <vector>
  8. #include "common/common_types.h"
  9. #include "common/multi_level_page_table.h"
  10. namespace VideoCore {
  11. class RasterizerInterface;
  12. }
  13. namespace Core {
  14. class System;
  15. }
  16. namespace Tegra {
  17. class MemoryManager final {
  18. public:
  19. explicit MemoryManager(Core::System& system_, u64 address_space_bits_ = 40,
  20. u64 page_bits_ = 16);
  21. ~MemoryManager();
  22. size_t GetID() const {
  23. return unique_identifier;
  24. }
  25. /// Binds a renderer to the memory manager.
  26. void BindRasterizer(VideoCore::RasterizerInterface* rasterizer);
  27. [[nodiscard]] std::optional<VAddr> GpuToCpuAddress(GPUVAddr addr) const;
  28. [[nodiscard]] std::optional<VAddr> GpuToCpuAddress(GPUVAddr addr, std::size_t size) const;
  29. template <typename T>
  30. [[nodiscard]] T Read(GPUVAddr addr) const;
  31. template <typename T>
  32. void Write(GPUVAddr addr, T data);
  33. [[nodiscard]] u8* GetPointer(GPUVAddr addr);
  34. [[nodiscard]] const u8* GetPointer(GPUVAddr addr) const;
  35. /**
  36. * ReadBlock and WriteBlock are full read and write operations over virtual
  37. * GPU Memory. It's important to use these when GPU memory may not be continuous
  38. * in the Host Memory counterpart. Note: This functions cause Host GPU Memory
  39. * Flushes and Invalidations, respectively to each operation.
  40. */
  41. void ReadBlock(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size) const;
  42. void WriteBlock(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size);
  43. void CopyBlock(GPUVAddr gpu_dest_addr, GPUVAddr gpu_src_addr, std::size_t size);
  44. /**
  45. * ReadBlockUnsafe and WriteBlockUnsafe are special versions of ReadBlock and
  46. * WriteBlock respectively. In this versions, no flushing or invalidation is actually
  47. * done and their performance is similar to a memcpy. This functions can be used
  48. * on either of this 2 scenarios instead of their safe counterpart:
  49. * - Memory which is sure to never be represented in the Host GPU.
  50. * - Memory Managed by a Cache Manager. Example: Texture Flushing should use
  51. * WriteBlockUnsafe instead of WriteBlock since it shouldn't invalidate the texture
  52. * being flushed.
  53. */
  54. void ReadBlockUnsafe(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size) const;
  55. void WriteBlockUnsafe(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size);
  56. /**
  57. * Checks if a gpu region can be simply read with a pointer.
  58. */
  59. [[nodiscard]] bool IsGranularRange(GPUVAddr gpu_addr, std::size_t size) const;
  60. /**
  61. * Checks if a gpu region is mapped by a single range of cpu addresses.
  62. */
  63. [[nodiscard]] bool IsContinousRange(GPUVAddr gpu_addr, std::size_t size) const;
  64. /**
  65. * Checks if a gpu region is mapped entirely.
  66. */
  67. [[nodiscard]] bool IsFullyMappedRange(GPUVAddr gpu_addr, std::size_t size) const;
  68. /**
  69. * Returns a vector with all the subranges of cpu addresses mapped beneath.
  70. * if the region is continous, a single pair will be returned. If it's unmapped, an empty vector
  71. * will be returned;
  72. */
  73. std::vector<std::pair<GPUVAddr, std::size_t>> GetSubmappedRange(GPUVAddr gpu_addr,
  74. std::size_t size) const;
  75. GPUVAddr Map(GPUVAddr gpu_addr, VAddr cpu_addr, std::size_t size);
  76. GPUVAddr MapSparse(GPUVAddr gpu_addr, std::size_t size);
  77. [[nodiscard]] GPUVAddr MapAllocate(VAddr cpu_addr, std::size_t size, std::size_t align);
  78. [[nodiscard]] GPUVAddr MapAllocate32(VAddr cpu_addr, std::size_t size);
  79. [[nodiscard]] std::optional<GPUVAddr> AllocateFixed(GPUVAddr gpu_addr, std::size_t size);
  80. [[nodiscard]] GPUVAddr Allocate(std::size_t size, std::size_t align);
  81. void Unmap(GPUVAddr gpu_addr, std::size_t size);
  82. void FlushRegion(GPUVAddr gpu_addr, size_t size) const;
  83. private:
  84. [[nodiscard]] std::optional<GPUVAddr> FindFreeRange(std::size_t size, std::size_t align,
  85. bool start_32bit_address = false) const;
  86. void ReadBlockImpl(GPUVAddr gpu_src_addr, void* dest_buffer, std::size_t size,
  87. bool is_safe) const;
  88. void WriteBlockImpl(GPUVAddr gpu_dest_addr, const void* src_buffer, std::size_t size,
  89. bool is_safe);
  90. [[nodiscard]] inline std::size_t PageEntryIndex(GPUVAddr gpu_addr) const {
  91. return (gpu_addr >> page_bits) & page_table_mask;
  92. }
  93. Core::System& system;
  94. const u64 address_space_bits;
  95. const u64 page_bits;
  96. u64 address_space_size;
  97. u64 allocate_start;
  98. u64 page_size;
  99. u64 page_mask;
  100. u64 page_table_mask;
  101. static constexpr u64 cpu_page_bits{12};
  102. VideoCore::RasterizerInterface* rasterizer = nullptr;
  103. enum class EntryType : u64 {
  104. Free = 0,
  105. Reserved = 1,
  106. Mapped = 2,
  107. };
  108. std::vector<u64> entries;
  109. template <EntryType entry_type>
  110. GPUVAddr PageTableOp(GPUVAddr gpu_addr, [[maybe_unused]] VAddr cpu_addr, size_t size);
  111. EntryType GetEntry(size_t position) const;
  112. void SetEntry(size_t position, EntryType entry);
  113. Common::MultiLevelPageTable<u32> page_table;
  114. const size_t unique_identifier;
  115. static std::atomic<size_t> unique_identifier_generator;
  116. };
  117. } // namespace Tegra