DeviceMemoryManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. using Ryujinx.Common.Memory;
  2. using Ryujinx.Memory;
  3. using System;
  4. using System.Buffers;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. namespace Ryujinx.Graphics.Device
  8. {
  9. /// <summary>
  10. /// Device memory manager.
  11. /// </summary>
  12. public class DeviceMemoryManager : IWritableBlock
  13. {
  14. private const int PtLvl0Bits = 10;
  15. private const int PtLvl1Bits = 10;
  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 int AddressSpaceBits = PtPageBits + PtLvl1Bits + PtLvl0Bits;
  26. public const ulong PteUnmapped = ulong.MaxValue;
  27. private readonly ulong[][] _pageTable;
  28. private readonly IVirtualMemoryManager _physical;
  29. /// <summary>
  30. /// Creates a new instance of the GPU memory manager.
  31. /// </summary>
  32. /// <param name="physicalMemory">Physical memory that this memory manager will map into</param>
  33. public DeviceMemoryManager(IVirtualMemoryManager physicalMemory)
  34. {
  35. _physical = physicalMemory;
  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="va">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 va) where T : unmanaged
  45. {
  46. int size = Unsafe.SizeOf<T>();
  47. if (IsContiguous(va, size))
  48. {
  49. return _physical.Read<T>(Translate(va));
  50. }
  51. else
  52. {
  53. Span<byte> data = new byte[size];
  54. ReadImpl(va, data);
  55. return MemoryMarshal.Cast<byte, T>(data)[0];
  56. }
  57. }
  58. /// <summary>
  59. /// Gets a read-only span of data from GPU mapped memory.
  60. /// </summary>
  61. /// <param name="va">GPU virtual address where the data is located</param>
  62. /// <param name="size">Size of the data</param>
  63. /// <returns>The span of the data at the specified memory location</returns>
  64. public ReadOnlySpan<byte> GetSpan(ulong va, int size)
  65. {
  66. if (IsContiguous(va, size))
  67. {
  68. return _physical.GetSpan(Translate(va), size);
  69. }
  70. else
  71. {
  72. Span<byte> data = new byte[size];
  73. ReadImpl(va, data);
  74. return data;
  75. }
  76. }
  77. /// <summary>
  78. /// Reads data from a possibly non-contiguous region of GPU mapped memory.
  79. /// </summary>
  80. /// <param name="va">GPU virtual address of the data</param>
  81. /// <param name="data">Span to write the read data into</param>
  82. private void ReadImpl(ulong va, Span<byte> data)
  83. {
  84. if (data.Length == 0)
  85. {
  86. return;
  87. }
  88. int offset = 0, size;
  89. if ((va & PageMask) != 0)
  90. {
  91. ulong pa = Translate(va);
  92. size = Math.Min(data.Length, (int)PageSize - (int)(va & PageMask));
  93. if (pa != PteUnmapped && _physical.IsMapped(pa))
  94. {
  95. _physical.GetSpan(pa, size).CopyTo(data[..size]);
  96. }
  97. offset += size;
  98. }
  99. for (; offset < data.Length; offset += size)
  100. {
  101. ulong pa = Translate(va + (ulong)offset);
  102. size = Math.Min(data.Length - offset, (int)PageSize);
  103. if (pa != PteUnmapped && _physical.IsMapped(pa))
  104. {
  105. _physical.GetSpan(pa, size).CopyTo(data.Slice(offset, size));
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// Gets a writable region from GPU mapped memory.
  111. /// </summary>
  112. /// <param name="va">Start address of the range</param>
  113. /// <param name="size">Size in bytes to be range</param>
  114. /// <returns>A writable region with the data at the specified memory location</returns>
  115. public WritableRegion GetWritableRegion(ulong va, int size)
  116. {
  117. if (IsContiguous(va, size))
  118. {
  119. return _physical.GetWritableRegion(Translate(va), size, tracked: true);
  120. }
  121. else
  122. {
  123. IMemoryOwner<byte> memoryOwner = ByteMemoryPool.Rent(size);
  124. GetSpan(va, size).CopyTo(memoryOwner.Memory.Span);
  125. return new WritableRegion(this, va, memoryOwner, tracked: true);
  126. }
  127. }
  128. /// <summary>
  129. /// Writes data to GPU mapped memory.
  130. /// </summary>
  131. /// <typeparam name="T">Type of the data</typeparam>
  132. /// <param name="va">GPU virtual address to write the value into</param>
  133. /// <param name="value">The value to be written</param>
  134. public void Write<T>(ulong va, T value) where T : unmanaged
  135. {
  136. Write(va, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
  137. }
  138. /// <summary>
  139. /// Writes data to GPU mapped memory.
  140. /// </summary>
  141. /// <param name="va">GPU virtual address to write the data into</param>
  142. /// <param name="data">The data to be written</param>
  143. public void Write(ulong va, ReadOnlySpan<byte> data)
  144. {
  145. if (IsContiguous(va, data.Length))
  146. {
  147. _physical.Write(Translate(va), data);
  148. }
  149. else
  150. {
  151. int offset = 0, size;
  152. if ((va & PageMask) != 0)
  153. {
  154. ulong pa = Translate(va);
  155. size = Math.Min(data.Length, (int)PageSize - (int)(va & PageMask));
  156. if (pa != PteUnmapped && _physical.IsMapped(pa))
  157. {
  158. _physical.Write(pa, data[..size]);
  159. }
  160. offset += size;
  161. }
  162. for (; offset < data.Length; offset += size)
  163. {
  164. ulong pa = Translate(va + (ulong)offset);
  165. size = Math.Min(data.Length - offset, (int)PageSize);
  166. if (pa != PteUnmapped && _physical.IsMapped(pa))
  167. {
  168. _physical.Write(pa, data.Slice(offset, size));
  169. }
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// Writes data to GPU mapped memory without write tracking.
  175. /// </summary>
  176. /// <param name="va">GPU virtual address to write the data into</param>
  177. /// <param name="data">The data to be written</param>
  178. public void WriteUntracked(ulong va, ReadOnlySpan<byte> data)
  179. {
  180. throw new NotSupportedException();
  181. }
  182. /// <summary>
  183. /// Maps a given range of pages to the specified CPU virtual address.
  184. /// </summary>
  185. /// <remarks>
  186. /// All addresses and sizes must be page aligned.
  187. /// </remarks>
  188. /// <param name="pa">CPU virtual address to map into</param>
  189. /// <param name="va">GPU virtual address to be mapped</param>
  190. /// <param name="kind">Kind of the resource located at the mapping</param>
  191. public void Map(ulong pa, ulong va, ulong size)
  192. {
  193. lock (_pageTable)
  194. {
  195. for (ulong offset = 0; offset < size; offset += PageSize)
  196. {
  197. SetPte(va + offset, PackPte(pa + offset));
  198. }
  199. }
  200. }
  201. /// <summary>
  202. /// Unmaps a given range of pages at the specified GPU virtual memory region.
  203. /// </summary>
  204. /// <param name="va">GPU virtual address to unmap</param>
  205. /// <param name="size">Size in bytes of the region being unmapped</param>
  206. public void Unmap(ulong va, ulong size)
  207. {
  208. lock (_pageTable)
  209. {
  210. for (ulong offset = 0; offset < size; offset += PageSize)
  211. {
  212. SetPte(va + offset, PteUnmapped);
  213. }
  214. }
  215. }
  216. /// <summary>
  217. /// Checks if a region of GPU mapped memory is contiguous.
  218. /// </summary>
  219. /// <param name="va">GPU virtual address of the region</param>
  220. /// <param name="size">Size of the region</param>
  221. /// <returns>True if the region is contiguous, false otherwise</returns>
  222. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  223. private bool IsContiguous(ulong va, int size)
  224. {
  225. if (!ValidateAddress(va) || GetPte(va) == PteUnmapped)
  226. {
  227. return false;
  228. }
  229. ulong endVa = (va + (ulong)size + PageMask) & ~PageMask;
  230. va &= ~PageMask;
  231. int pages = (int)((endVa - va) / PageSize);
  232. for (int page = 0; page < pages - 1; page++)
  233. {
  234. if (!ValidateAddress(va + PageSize) || GetPte(va + PageSize) == PteUnmapped)
  235. {
  236. return false;
  237. }
  238. if (Translate(va) + PageSize != Translate(va + PageSize))
  239. {
  240. return false;
  241. }
  242. va += PageSize;
  243. }
  244. return true;
  245. }
  246. /// <summary>
  247. /// Validates a GPU virtual address.
  248. /// </summary>
  249. /// <param name="va">Address to validate</param>
  250. /// <returns>True if the address is valid, false otherwise</returns>
  251. private static bool ValidateAddress(ulong va)
  252. {
  253. return va < (1UL << AddressSpaceBits);
  254. }
  255. /// <summary>
  256. /// Checks if a given page is mapped.
  257. /// </summary>
  258. /// <param name="va">GPU virtual address of the page to check</param>
  259. /// <returns>True if the page is mapped, false otherwise</returns>
  260. public bool IsMapped(ulong va)
  261. {
  262. return Translate(va) != PteUnmapped;
  263. }
  264. /// <summary>
  265. /// Translates a GPU virtual address to a CPU virtual address.
  266. /// </summary>
  267. /// <param name="va">GPU virtual address to be translated</param>
  268. /// <returns>CPU virtual address, or <see cref="PteUnmapped"/> if unmapped</returns>
  269. public ulong Translate(ulong va)
  270. {
  271. if (!ValidateAddress(va))
  272. {
  273. return PteUnmapped;
  274. }
  275. ulong pte = GetPte(va);
  276. if (pte == PteUnmapped)
  277. {
  278. return PteUnmapped;
  279. }
  280. return UnpackPaFromPte(pte) + (va & PageMask);
  281. }
  282. /// <summary>
  283. /// Gets the Page Table entry for a given GPU virtual address.
  284. /// </summary>
  285. /// <param name="va">GPU virtual address</param>
  286. /// <returns>Page table entry (CPU virtual address)</returns>
  287. private ulong GetPte(ulong va)
  288. {
  289. ulong l0 = (va >> PtLvl0Bit) & PtLvl0Mask;
  290. ulong l1 = (va >> PtLvl1Bit) & PtLvl1Mask;
  291. if (_pageTable[l0] == null)
  292. {
  293. return PteUnmapped;
  294. }
  295. return _pageTable[l0][l1];
  296. }
  297. /// <summary>
  298. /// Sets a Page Table entry at a given GPU virtual address.
  299. /// </summary>
  300. /// <param name="va">GPU virtual address</param>
  301. /// <param name="pte">Page table entry (CPU virtual address)</param>
  302. private void SetPte(ulong va, ulong pte)
  303. {
  304. ulong l0 = (va >> PtLvl0Bit) & PtLvl0Mask;
  305. ulong l1 = (va >> PtLvl1Bit) & PtLvl1Mask;
  306. if (_pageTable[l0] == null)
  307. {
  308. _pageTable[l0] = new ulong[PtLvl1Size];
  309. for (ulong index = 0; index < PtLvl1Size; index++)
  310. {
  311. _pageTable[l0][index] = PteUnmapped;
  312. }
  313. }
  314. _pageTable[l0][l1] = pte;
  315. }
  316. /// <summary>
  317. /// Creates a page table entry from a physical address and kind.
  318. /// </summary>
  319. /// <param name="pa">Physical address</param>
  320. /// <returns>Page table entry</returns>
  321. private static ulong PackPte(ulong pa)
  322. {
  323. return pa;
  324. }
  325. /// <summary>
  326. /// Unpacks physical address from a page table entry.
  327. /// </summary>
  328. /// <param name="pte">Page table entry</param>
  329. /// <returns>Physical address</returns>
  330. private static ulong UnpackPaFromPte(ulong pte)
  331. {
  332. return pte;
  333. }
  334. }
  335. }