memory_manager.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <map>
  6. #include <optional>
  7. #include "common/common_types.h"
  8. #include "common/page_table.h"
  9. namespace VideoCore {
  10. class RasterizerInterface;
  11. }
  12. namespace Tegra {
  13. /**
  14. * Represents a VMA in an address space. A VMA is a contiguous region of virtual addressing space
  15. * with homogeneous attributes across its extents. In this particular implementation each VMA is
  16. * also backed by a single host memory allocation.
  17. */
  18. struct VirtualMemoryArea {
  19. enum class Type : u8 {
  20. Unmapped,
  21. Allocated,
  22. Mapped,
  23. };
  24. /// Virtual base address of the region.
  25. GPUVAddr base{};
  26. /// Size of the region.
  27. u64 size{};
  28. /// Memory area mapping type.
  29. Type type{Type::Unmapped};
  30. /// CPU memory mapped address corresponding to this memory area.
  31. VAddr backing_addr{};
  32. /// Offset into the backing_memory the mapping starts from.
  33. std::size_t offset{};
  34. /// Pointer backing this VMA.
  35. u8* backing_memory{};
  36. /// Tests if this area can be merged to the right with `next`.
  37. bool CanBeMergedWith(const VirtualMemoryArea& next) const;
  38. };
  39. class MemoryManager final {
  40. public:
  41. MemoryManager(VideoCore::RasterizerInterface& rasterizer);
  42. GPUVAddr AllocateSpace(u64 size, u64 align);
  43. GPUVAddr AllocateSpace(GPUVAddr addr, u64 size, u64 align);
  44. GPUVAddr MapBufferEx(VAddr cpu_addr, u64 size);
  45. GPUVAddr MapBufferEx(VAddr cpu_addr, GPUVAddr addr, u64 size);
  46. GPUVAddr UnmapBuffer(GPUVAddr addr, u64 size);
  47. std::optional<VAddr> GpuToCpuAddress(GPUVAddr addr) const;
  48. template <typename T>
  49. T Read(GPUVAddr addr) const;
  50. template <typename T>
  51. void Write(GPUVAddr addr, T data);
  52. u8* GetPointer(GPUVAddr addr);
  53. const u8* GetPointer(GPUVAddr addr) const;
  54. // Returns true if the block is continous in host memory, false otherwise
  55. bool IsBlockContinous(const GPUVAddr start, const std::size_t size);
  56. /**
  57. * ReadBlock and WriteBlock are full read and write operations over virtual
  58. * GPU Memory. It's important to use these when GPU memory may not be continous
  59. * in the Host Memory counterpart. Note: This functions cause Host GPU Memory
  60. * Flushes and Invalidations, respectively to each operation.
  61. */
  62. void ReadBlock(GPUVAddr src_addr, void* dest_buffer, const std::size_t size) const;
  63. void WriteBlock(GPUVAddr dest_addr, const void* src_buffer, const std::size_t size);
  64. void CopyBlock(GPUVAddr dest_addr, GPUVAddr src_addr, const std::size_t size);
  65. /**
  66. * ReadBlockUnsafe and WriteBlockUnsafe are special versions of ReadBlock and
  67. * WriteBlock respectively. In this versions, no flushing or invalidation is actually
  68. * done and their performance is similar to a memcpy. This functions can be used
  69. * on either of this 2 scenarios instead of their safe counterpart:
  70. * - Memory which is sure to never be represented in the Host GPU.
  71. * - Memory Managed by a Cache Manager. Example: Texture Flushing should use
  72. * WriteBlockUnsafe instead of WriteBlock since it shouldn't invalidate the texture
  73. * being flushed.
  74. */
  75. void ReadBlockUnsafe(GPUVAddr src_addr, void* dest_buffer, const std::size_t size) const;
  76. void WriteBlockUnsafe(GPUVAddr dest_addr, const void* src_buffer, const std::size_t size);
  77. void CopyBlockUnsafe(GPUVAddr dest_addr, GPUVAddr src_addr, const std::size_t size);
  78. private:
  79. using VMAMap = std::map<GPUVAddr, VirtualMemoryArea>;
  80. using VMAHandle = VMAMap::const_iterator;
  81. using VMAIter = VMAMap::iterator;
  82. bool IsAddressValid(GPUVAddr addr) const;
  83. void MapPages(GPUVAddr base, u64 size, u8* memory, Common::PageType type,
  84. VAddr backing_addr = 0);
  85. void MapMemoryRegion(GPUVAddr base, u64 size, u8* target, VAddr backing_addr);
  86. void UnmapRegion(GPUVAddr base, u64 size);
  87. /// Finds the VMA in which the given address is included in, or `vma_map.end()`.
  88. VMAHandle FindVMA(GPUVAddr target) const;
  89. VMAHandle AllocateMemory(GPUVAddr target, std::size_t offset, u64 size);
  90. /**
  91. * Maps an unmanaged host memory pointer at a given address.
  92. *
  93. * @param target The guest address to start the mapping at.
  94. * @param memory The memory to be mapped.
  95. * @param size Size of the mapping.
  96. * @param state MemoryState tag to attach to the VMA.
  97. */
  98. VMAHandle MapBackingMemory(GPUVAddr target, u8* memory, u64 size, VAddr backing_addr);
  99. /// Unmaps a range of addresses, splitting VMAs as necessary.
  100. void UnmapRange(GPUVAddr target, u64 size);
  101. /// Converts a VMAHandle to a mutable VMAIter.
  102. VMAIter StripIterConstness(const VMAHandle& iter);
  103. /// Marks as the specfied VMA as allocated.
  104. VMAIter Allocate(VMAIter vma);
  105. /**
  106. * Carves a VMA of a specific size at the specified address by splitting Free VMAs while doing
  107. * the appropriate error checking.
  108. */
  109. VMAIter CarveVMA(GPUVAddr base, u64 size);
  110. /**
  111. * Splits the edges of the given range of non-Free VMAs so that there is a VMA split at each
  112. * end of the range.
  113. */
  114. VMAIter CarveVMARange(GPUVAddr base, u64 size);
  115. /**
  116. * Splits a VMA in two, at the specified offset.
  117. * @returns the right side of the split, with the original iterator becoming the left side.
  118. */
  119. VMAIter SplitVMA(VMAIter vma, u64 offset_in_vma);
  120. /**
  121. * Checks for and merges the specified VMA with adjacent ones if possible.
  122. * @returns the merged VMA or the original if no merging was possible.
  123. */
  124. VMAIter MergeAdjacent(VMAIter vma);
  125. /// Updates the pages corresponding to this VMA so they match the VMA's attributes.
  126. void UpdatePageTableForVMA(const VirtualMemoryArea& vma);
  127. /// Finds a free (unmapped region) of the specified size starting at the specified address.
  128. GPUVAddr FindFreeRegion(GPUVAddr region_start, u64 size) const;
  129. private:
  130. static constexpr u64 page_bits{16};
  131. static constexpr u64 page_size{1 << page_bits};
  132. static constexpr u64 page_mask{page_size - 1};
  133. /// Address space in bits, this is fairly arbitrary but sufficiently large.
  134. static constexpr u32 address_space_width{39};
  135. /// Start address for mapping, this is fairly arbitrary but must be non-zero.
  136. static constexpr GPUVAddr address_space_base{0x100000};
  137. /// End of address space, based on address space in bits.
  138. static constexpr GPUVAddr address_space_end{1ULL << address_space_width};
  139. Common::PageTable page_table{page_bits};
  140. VMAMap vma_map;
  141. VideoCore::RasterizerInterface& rasterizer;
  142. };
  143. } // namespace Tegra