memory_manager.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 Core {
  10. class System;
  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. explicit MemoryManager(Core::System& system);
  42. ~MemoryManager();
  43. GPUVAddr AllocateSpace(u64 size, u64 align);
  44. GPUVAddr AllocateSpace(GPUVAddr addr, u64 size, u64 align);
  45. GPUVAddr MapBufferEx(VAddr cpu_addr, u64 size);
  46. GPUVAddr MapBufferEx(VAddr cpu_addr, GPUVAddr addr, u64 size);
  47. GPUVAddr UnmapBuffer(GPUVAddr addr, u64 size);
  48. std::optional<VAddr> GpuToCpuAddress(GPUVAddr addr) const;
  49. template <typename T>
  50. T Read(GPUVAddr addr) const;
  51. template <typename T>
  52. void Write(GPUVAddr addr, T data);
  53. u8* GetPointer(GPUVAddr addr);
  54. const u8* GetPointer(GPUVAddr addr) const;
  55. /// Returns true if the block is continuous in host memory, false otherwise
  56. bool IsBlockContinuous(GPUVAddr start, std::size_t size) const;
  57. /**
  58. * ReadBlock and WriteBlock are full read and write operations over virtual
  59. * GPU Memory. It's important to use these when GPU memory may not be continuous
  60. * in the Host Memory counterpart. Note: This functions cause Host GPU Memory
  61. * Flushes and Invalidations, respectively to each operation.
  62. */
  63. void ReadBlock(GPUVAddr src_addr, void* dest_buffer, std::size_t size) const;
  64. void WriteBlock(GPUVAddr dest_addr, const void* src_buffer, std::size_t size);
  65. void CopyBlock(GPUVAddr dest_addr, GPUVAddr src_addr, std::size_t size);
  66. /**
  67. * ReadBlockUnsafe and WriteBlockUnsafe are special versions of ReadBlock and
  68. * WriteBlock respectively. In this versions, no flushing or invalidation is actually
  69. * done and their performance is similar to a memcpy. This functions can be used
  70. * on either of this 2 scenarios instead of their safe counterpart:
  71. * - Memory which is sure to never be represented in the Host GPU.
  72. * - Memory Managed by a Cache Manager. Example: Texture Flushing should use
  73. * WriteBlockUnsafe instead of WriteBlock since it shouldn't invalidate the texture
  74. * being flushed.
  75. */
  76. void ReadBlockUnsafe(GPUVAddr src_addr, void* dest_buffer, std::size_t size) const;
  77. void WriteBlockUnsafe(GPUVAddr dest_addr, const void* src_buffer, std::size_t size);
  78. void CopyBlockUnsafe(GPUVAddr dest_addr, GPUVAddr src_addr, std::size_t size);
  79. private:
  80. using VMAMap = std::map<GPUVAddr, VirtualMemoryArea>;
  81. using VMAHandle = VMAMap::const_iterator;
  82. using VMAIter = VMAMap::iterator;
  83. bool IsAddressValid(GPUVAddr addr) const;
  84. void MapPages(GPUVAddr base, u64 size, u8* memory, Common::PageType type,
  85. VAddr backing_addr = 0);
  86. void MapMemoryRegion(GPUVAddr base, u64 size, u8* target, VAddr backing_addr);
  87. void UnmapRegion(GPUVAddr base, u64 size);
  88. /// Finds the VMA in which the given address is included in, or `vma_map.end()`.
  89. VMAHandle FindVMA(GPUVAddr target) const;
  90. VMAHandle AllocateMemory(GPUVAddr target, std::size_t offset, u64 size);
  91. /**
  92. * Maps an unmanaged host memory pointer at a given address.
  93. *
  94. * @param target The guest address to start the mapping at.
  95. * @param memory The memory to be mapped.
  96. * @param size Size of the mapping in bytes.
  97. * @param backing_addr The base address of the range to back this mapping.
  98. */
  99. VMAHandle MapBackingMemory(GPUVAddr target, u8* memory, u64 size, VAddr backing_addr);
  100. /// Unmaps a range of addresses, splitting VMAs as necessary.
  101. void UnmapRange(GPUVAddr target, u64 size);
  102. /// Converts a VMAHandle to a mutable VMAIter.
  103. VMAIter StripIterConstness(const VMAHandle& iter);
  104. /// Marks as the specified VMA as allocated.
  105. VMAIter Allocate(VMAIter vma);
  106. /**
  107. * Carves a VMA of a specific size at the specified address by splitting Free VMAs while doing
  108. * the appropriate error checking.
  109. */
  110. VMAIter CarveVMA(GPUVAddr base, u64 size);
  111. /**
  112. * Splits the edges of the given range of non-Free VMAs so that there is a VMA split at each
  113. * end of the range.
  114. */
  115. VMAIter CarveVMARange(GPUVAddr base, u64 size);
  116. /**
  117. * Splits a VMA in two, at the specified offset.
  118. * @returns the right side of the split, with the original iterator becoming the left side.
  119. */
  120. VMAIter SplitVMA(VMAIter vma, u64 offset_in_vma);
  121. /**
  122. * Checks for and merges the specified VMA with adjacent ones if possible.
  123. * @returns the merged VMA or the original if no merging was possible.
  124. */
  125. VMAIter MergeAdjacent(VMAIter vma);
  126. /// Updates the pages corresponding to this VMA so they match the VMA's attributes.
  127. void UpdatePageTableForVMA(const VirtualMemoryArea& vma);
  128. /// Finds a free (unmapped region) of the specified size starting at the specified address.
  129. GPUVAddr FindFreeRegion(GPUVAddr region_start, u64 size) const;
  130. private:
  131. static constexpr u64 page_bits{16};
  132. static constexpr u64 page_size{1 << page_bits};
  133. static constexpr u64 page_mask{page_size - 1};
  134. /// Address space in bits, according to Tegra X1 TRM
  135. static constexpr u32 address_space_width{40};
  136. /// Start address for mapping, this is fairly arbitrary but must be non-zero.
  137. static constexpr GPUVAddr address_space_base{0x100000};
  138. /// End of address space, based on address space in bits.
  139. static constexpr GPUVAddr address_space_end{1ULL << address_space_width};
  140. Common::PageTable page_table{page_bits};
  141. VMAMap vma_map;
  142. Core::System& system;
  143. };
  144. } // namespace Tegra