MemoryManager.cs 12 KB

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