MemoryManager.cs 12 KB

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