MemoryManager.cs 12 KB

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