MemoryManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using Ryujinx.Cpu;
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Graphics.Gpu.Memory
  6. {
  7. /// <summary>
  8. /// GPU memory manager.
  9. /// </summary>
  10. public class MemoryManager
  11. {
  12. private const ulong AddressSpaceSize = 1UL << 40;
  13. public const ulong BadAddress = ulong.MaxValue;
  14. private const int PtLvl0Bits = 14;
  15. private const int PtLvl1Bits = 14;
  16. public const int PtPageBits = 12;
  17. private const ulong PtLvl0Size = 1UL << PtLvl0Bits;
  18. private const ulong PtLvl1Size = 1UL << PtLvl1Bits;
  19. public const ulong PageSize = 1UL << PtPageBits;
  20. private const ulong PtLvl0Mask = PtLvl0Size - 1;
  21. private const ulong PtLvl1Mask = PtLvl1Size - 1;
  22. public const ulong PageMask = PageSize - 1;
  23. private const int PtLvl0Bit = PtPageBits + PtLvl1Bits;
  24. private const int PtLvl1Bit = PtPageBits;
  25. private const ulong PteUnmapped = 0xffffffff_ffffffff;
  26. private const ulong PteReserved = 0xffffffff_fffffffe;
  27. private readonly ulong[][] _pageTable;
  28. public event EventHandler<UnmapEventArgs> MemoryUnmapped;
  29. private GpuContext _context;
  30. /// <summary>
  31. /// Creates a new instance of the GPU memory manager.
  32. /// </summary>
  33. public MemoryManager(GpuContext context)
  34. {
  35. _context = context;
  36. _pageTable = new ulong[PtLvl0Size][];
  37. }
  38. /// <summary>
  39. /// Reads data from GPU mapped memory.
  40. /// </summary>
  41. /// <typeparam name="T">Type of the data</typeparam>
  42. /// <param name="gpuVa">GPU virtual address where the data is located</param>
  43. /// <returns>The data at the specified memory location</returns>
  44. public T Read<T>(ulong gpuVa) where T : unmanaged
  45. {
  46. ulong processVa = Translate(gpuVa);
  47. return MemoryMarshal.Cast<byte, T>(_context.PhysicalMemory.GetSpan(processVa, Unsafe.SizeOf<T>()))[0];
  48. }
  49. /// <summary>
  50. /// Gets a read-only span of data from GPU mapped memory.
  51. /// This reads as much data as possible, up to the specified maximum size.
  52. /// </summary>
  53. /// <param name="gpuVa">GPU virtual address where the data is located</param>
  54. /// <param name="size">Size of the data</param>
  55. /// <returns>The span of the data at the specified memory location</returns>
  56. public ReadOnlySpan<byte> GetSpan(ulong gpuVa, int size)
  57. {
  58. ulong processVa = Translate(gpuVa);
  59. return _context.PhysicalMemory.GetSpan(processVa, size);
  60. }
  61. /// <summary>
  62. /// Gets a writable region from GPU mapped memory.
  63. /// </summary>
  64. /// <param name="address">Start address of the range</param>
  65. /// <param name="size">Size in bytes to be range</param>
  66. /// <returns>A writable region with the data at the specified memory location</returns>
  67. public WritableRegion GetWritableRegion(ulong gpuVa, int size)
  68. {
  69. ulong processVa = Translate(gpuVa);
  70. return _context.PhysicalMemory.GetWritableRegion(processVa, size);
  71. }
  72. /// <summary>
  73. /// Writes data to GPU mapped memory.
  74. /// </summary>
  75. /// <param name="gpuVa">GPU virtual address to write the data into</param>
  76. /// <param name="data">The data to be written</param>
  77. public void Write(ulong gpuVa, ReadOnlySpan<byte> data)
  78. {
  79. ulong processVa = Translate(gpuVa);
  80. _context.PhysicalMemory.Write(processVa, data);
  81. }
  82. /// <summary>
  83. /// Maps a given range of pages to the specified CPU virtual address.
  84. /// </summary>
  85. /// <remarks>
  86. /// All addresses and sizes must be page aligned.
  87. /// </remarks>
  88. /// <param name="pa">CPU virtual address to map into</param>
  89. /// <param name="va">GPU virtual address to be mapped</param>
  90. /// <param name="size">Size in bytes of the mapping</param>
  91. /// <returns>GPU virtual address of the mapping</returns>
  92. public ulong Map(ulong pa, ulong va, ulong size)
  93. {
  94. lock (_pageTable)
  95. {
  96. for (ulong offset = 0; offset < size; offset += PageSize)
  97. {
  98. SetPte(va + offset, pa + offset);
  99. }
  100. }
  101. return va;
  102. }
  103. /// <summary>
  104. /// Maps a given range of pages to an allocated GPU virtual address.
  105. /// The memory is automatically allocated by the memory manager.
  106. /// </summary>
  107. /// <param name="pa">CPU virtual address to map into</param>
  108. /// <param name="size">Size in bytes of the mapping</param>
  109. /// <param name="alignment">Required alignment of the GPU virtual address in bytes</param>
  110. /// <returns>GPU virtual address where the range was mapped, or an all ones mask in case of failure</returns>
  111. public ulong MapAllocate(ulong pa, ulong size, ulong alignment)
  112. {
  113. lock (_pageTable)
  114. {
  115. ulong va = GetFreePosition(size, alignment);
  116. if (va != PteUnmapped)
  117. {
  118. for (ulong offset = 0; offset < size; offset += PageSize)
  119. {
  120. SetPte(va + offset, pa + offset);
  121. }
  122. }
  123. return va;
  124. }
  125. }
  126. /// <summary>
  127. /// Maps a given range of pages to an allocated GPU virtual address.
  128. /// The memory is automatically allocated by the memory manager.
  129. /// This also ensures that the mapping is always done in the first 4GB of GPU address space.
  130. /// </summary>
  131. /// <param name="pa">CPU virtual address to map into</param>
  132. /// <param name="size">Size in bytes of the mapping</param>
  133. /// <returns>GPU virtual address where the range was mapped, or an all ones mask in case of failure</returns>
  134. public ulong MapLow(ulong pa, ulong size)
  135. {
  136. lock (_pageTable)
  137. {
  138. ulong va = GetFreePosition(size, 1, PageSize);
  139. if (va != PteUnmapped && va <= uint.MaxValue && (va + size) <= uint.MaxValue)
  140. {
  141. for (ulong offset = 0; offset < size; offset += PageSize)
  142. {
  143. SetPte(va + offset, pa + offset);
  144. }
  145. }
  146. else
  147. {
  148. va = PteUnmapped;
  149. }
  150. return va;
  151. }
  152. }
  153. /// <summary>
  154. /// Reserves memory at a fixed GPU memory location.
  155. /// This prevents the reserved region from being used for memory allocation for map.
  156. /// </summary>
  157. /// <param name="va">GPU virtual address to reserve</param>
  158. /// <param name="size">Size in bytes of the reservation</param>
  159. /// <returns>GPU virtual address of the reservation, or an all ones mask in case of failure</returns>
  160. public ulong ReserveFixed(ulong va, ulong size)
  161. {
  162. lock (_pageTable)
  163. {
  164. for (ulong offset = 0; offset < size; offset += PageSize)
  165. {
  166. if (IsPageInUse(va + offset))
  167. {
  168. return PteUnmapped;
  169. }
  170. }
  171. for (ulong offset = 0; offset < size; offset += PageSize)
  172. {
  173. SetPte(va + offset, PteReserved);
  174. }
  175. }
  176. return va;
  177. }
  178. /// <summary>
  179. /// Reserves memory at any GPU memory location.
  180. /// </summary>
  181. /// <param name="size">Size in bytes of the reservation</param>
  182. /// <param name="alignment">Reservation address alignment in bytes</param>
  183. /// <returns>GPU virtual address of the reservation, or an all ones mask in case of failure</returns>
  184. public ulong Reserve(ulong size, ulong alignment)
  185. {
  186. lock (_pageTable)
  187. {
  188. ulong address = GetFreePosition(size, alignment);
  189. if (address != PteUnmapped)
  190. {
  191. for (ulong offset = 0; offset < size; offset += PageSize)
  192. {
  193. SetPte(address + offset, PteReserved);
  194. }
  195. }
  196. return address;
  197. }
  198. }
  199. /// <summary>
  200. /// Frees memory that was previously allocated by a map or reserved.
  201. /// </summary>
  202. /// <param name="va">GPU virtual address to free</param>
  203. /// <param name="size">Size in bytes of the region being freed</param>
  204. public void Free(ulong va, ulong size)
  205. {
  206. lock (_pageTable)
  207. {
  208. // Event handlers are not expected to be thread safe.
  209. MemoryUnmapped?.Invoke(this, new UnmapEventArgs(va, size));
  210. for (ulong offset = 0; offset < size; offset += PageSize)
  211. {
  212. SetPte(va + offset, PteUnmapped);
  213. }
  214. }
  215. }
  216. /// <summary>
  217. /// Gets the address of an unused (free) region of the specified size.
  218. /// </summary>
  219. /// <param name="size">Size of the region in bytes</param>
  220. /// <param name="alignment">Required alignment of the region address in bytes</param>
  221. /// <param name="start">Start address of the search on the address space</param>
  222. /// <returns>GPU virtual address of the allocation, or an all ones mask in case of failure</returns>
  223. private ulong GetFreePosition(ulong size, ulong alignment = 1, ulong start = 1UL << 32)
  224. {
  225. // Note: Address 0 is not considered valid by the driver,
  226. // when 0 is returned it's considered a mapping error.
  227. ulong address = start;
  228. ulong freeSize = 0;
  229. if (alignment == 0)
  230. {
  231. alignment = 1;
  232. }
  233. alignment = (alignment + PageMask) & ~PageMask;
  234. while (address + freeSize < AddressSpaceSize)
  235. {
  236. if (!IsPageInUse(address + freeSize))
  237. {
  238. freeSize += PageSize;
  239. if (freeSize >= size)
  240. {
  241. return address;
  242. }
  243. }
  244. else
  245. {
  246. address += freeSize + PageSize;
  247. freeSize = 0;
  248. ulong remainder = address % alignment;
  249. if (remainder != 0)
  250. {
  251. address = (address - remainder) + alignment;
  252. }
  253. }
  254. }
  255. return PteUnmapped;
  256. }
  257. /// <summary>
  258. /// Translates a GPU virtual address to a CPU virtual address.
  259. /// </summary>
  260. /// <param name="gpuVa">GPU virtual address to be translated</param>
  261. /// <returns>CPU virtual address</returns>
  262. public ulong Translate(ulong gpuVa)
  263. {
  264. ulong baseAddress = GetPte(gpuVa);
  265. if (baseAddress == PteUnmapped || baseAddress == PteReserved)
  266. {
  267. return PteUnmapped;
  268. }
  269. return baseAddress + (gpuVa & PageMask);
  270. }
  271. /// <summary>
  272. /// Checks if a given memory page is mapped or reserved.
  273. /// </summary>
  274. /// <param name="gpuVa">GPU virtual address of the page</param>
  275. /// <returns>True if the page is mapped or reserved, false otherwise</returns>
  276. private bool IsPageInUse(ulong gpuVa)
  277. {
  278. if (gpuVa >> PtLvl0Bits + PtLvl1Bits + PtPageBits != 0)
  279. {
  280. return false;
  281. }
  282. ulong l0 = (gpuVa >> PtLvl0Bit) & PtLvl0Mask;
  283. ulong l1 = (gpuVa >> PtLvl1Bit) & PtLvl1Mask;
  284. if (_pageTable[l0] == null)
  285. {
  286. return false;
  287. }
  288. return _pageTable[l0][l1] != PteUnmapped;
  289. }
  290. /// <summary>
  291. /// Gets the Page Table entry for a given GPU virtual address.
  292. /// </summary>
  293. /// <param name="gpuVa">GPU virtual address</param>
  294. /// <returns>Page table entry (CPU virtual address)</returns>
  295. private ulong GetPte(ulong gpuVa)
  296. {
  297. ulong l0 = (gpuVa >> PtLvl0Bit) & PtLvl0Mask;
  298. ulong l1 = (gpuVa >> PtLvl1Bit) & PtLvl1Mask;
  299. if (_pageTable[l0] == null)
  300. {
  301. return PteUnmapped;
  302. }
  303. return _pageTable[l0][l1];
  304. }
  305. /// <summary>
  306. /// Sets a Page Table entry at a given GPU virtual address.
  307. /// </summary>
  308. /// <param name="gpuVa">GPU virtual address</param>
  309. /// <param name="pte">Page table entry (CPU virtual address)</param>
  310. private void SetPte(ulong gpuVa, ulong pte)
  311. {
  312. ulong l0 = (gpuVa >> PtLvl0Bit) & PtLvl0Mask;
  313. ulong l1 = (gpuVa >> PtLvl1Bit) & PtLvl1Mask;
  314. if (_pageTable[l0] == null)
  315. {
  316. _pageTable[l0] = new ulong[PtLvl1Size];
  317. for (ulong index = 0; index < PtLvl1Size; index++)
  318. {
  319. _pageTable[l0][index] = PteUnmapped;
  320. }
  321. }
  322. _pageTable[l0][l1] = pte;
  323. }
  324. }
  325. }