NvMemoryAllocator.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using Ryujinx.Common.Collections;
  2. using System.Collections.Generic;
  3. using Ryujinx.Common;
  4. using System;
  5. using Ryujinx.Graphics.Gpu.Memory;
  6. namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices
  7. {
  8. class NvMemoryAllocator
  9. {
  10. private const ulong AddressSpaceSize = 1UL << 40;
  11. private const ulong DefaultStart = 1UL << 32;
  12. private const ulong InvalidAddress = 0;
  13. private const ulong PageSize = MemoryManager.PageSize;
  14. private const ulong PageMask = MemoryManager.PageMask;
  15. public const ulong PteUnmapped = MemoryManager.PteUnmapped;
  16. // Key --> Start Address of Region
  17. // Value --> End Address of Region
  18. private readonly TreeDictionary<ulong, ulong> _tree = new TreeDictionary<ulong, ulong>();
  19. private readonly Dictionary<ulong, LinkedListNode<ulong>> _dictionary = new Dictionary<ulong, LinkedListNode<ulong>>();
  20. private readonly LinkedList<ulong> _list = new LinkedList<ulong>();
  21. public NvMemoryAllocator()
  22. {
  23. _tree.Add(PageSize, PageSize + AddressSpaceSize);
  24. LinkedListNode<ulong> node = _list.AddFirst(PageSize);
  25. _dictionary[PageSize] = node;
  26. }
  27. /// <summary>
  28. /// Marks a range of memory as consumed by removing it from the tree.
  29. /// This function will split memory regions if there is available space.
  30. /// </summary>
  31. /// <param name="va">Virtual address at which to allocate</param>
  32. /// <param name="size">Size of the allocation in bytes</param>
  33. /// <param name="referenceAddress">Reference to the address of memory where the allocation can take place</param>
  34. #region Memory Allocation
  35. public void AllocateRange(ulong va, ulong size, ulong referenceAddress = InvalidAddress)
  36. {
  37. lock (_tree)
  38. {
  39. if (referenceAddress != InvalidAddress)
  40. {
  41. ulong endAddress = va + size;
  42. ulong referenceEndAddress = _tree.Get(referenceAddress);
  43. if (va >= referenceAddress)
  44. {
  45. // Need Left Node
  46. if (va > referenceAddress)
  47. {
  48. ulong leftEndAddress = va;
  49. // Overwrite existing block with its new smaller range.
  50. _tree.Add(referenceAddress, leftEndAddress);
  51. }
  52. else
  53. {
  54. // We need to get rid of the large chunk.
  55. _tree.Remove(referenceAddress);
  56. }
  57. ulong rightSize = referenceEndAddress - endAddress;
  58. // If leftover space, create a right node.
  59. if (rightSize > 0)
  60. {
  61. _tree.Add(endAddress, referenceEndAddress);
  62. LinkedListNode<ulong> node = _list.AddAfter(_dictionary[referenceAddress], endAddress);
  63. _dictionary[endAddress] = node;
  64. }
  65. if (va == referenceAddress)
  66. {
  67. _list.Remove(_dictionary[referenceAddress]);
  68. _dictionary.Remove(referenceAddress);
  69. }
  70. }
  71. }
  72. }
  73. }
  74. /// <summary>
  75. /// Marks a range of memory as free by adding it to the tree.
  76. /// This function will automatically compact the tree when it determines there are multiple ranges of free memory adjacent to each other.
  77. /// </summary>
  78. /// <param name="va">Virtual address at which to deallocate</param>
  79. /// <param name="size">Size of the allocation in bytes</param>
  80. public void DeallocateRange(ulong va, ulong size)
  81. {
  82. lock (_tree)
  83. {
  84. ulong freeAddressStartPosition = _tree.Floor(va);
  85. if (freeAddressStartPosition != InvalidAddress)
  86. {
  87. LinkedListNode<ulong> node = _dictionary[freeAddressStartPosition];
  88. ulong targetPrevAddress = _dictionary[freeAddressStartPosition].Previous != null ? _dictionary[_dictionary[freeAddressStartPosition].Previous.Value].Value : InvalidAddress;
  89. ulong targetNextAddress = _dictionary[freeAddressStartPosition].Next != null ? _dictionary[_dictionary[freeAddressStartPosition].Next.Value].Value : InvalidAddress;
  90. ulong expandedStart = va;
  91. ulong expandedEnd = va + size;
  92. while (targetPrevAddress != InvalidAddress)
  93. {
  94. ulong prevAddress = targetPrevAddress;
  95. ulong prevEndAddress = _tree.Get(targetPrevAddress);
  96. if (prevEndAddress >= expandedStart)
  97. {
  98. expandedStart = targetPrevAddress;
  99. LinkedListNode<ulong> prevPtr = _dictionary[prevAddress];
  100. if (prevPtr.Previous != null)
  101. {
  102. targetPrevAddress = prevPtr.Previous.Value;
  103. }
  104. else
  105. {
  106. targetPrevAddress = InvalidAddress;
  107. }
  108. node = node.Previous;
  109. _tree.Remove(prevAddress);
  110. _list.Remove(_dictionary[prevAddress]);
  111. _dictionary.Remove(prevAddress);
  112. }
  113. else
  114. {
  115. break;
  116. }
  117. }
  118. while (targetNextAddress != InvalidAddress)
  119. {
  120. ulong nextAddress = targetNextAddress;
  121. ulong nextEndAddress = _tree.Get(targetNextAddress);
  122. if (nextAddress <= expandedEnd)
  123. {
  124. expandedEnd = Math.Max(expandedEnd, nextEndAddress);
  125. LinkedListNode<ulong> nextPtr = _dictionary[nextAddress];
  126. if (nextPtr.Next != null)
  127. {
  128. targetNextAddress = nextPtr.Next.Value;
  129. }
  130. else
  131. {
  132. targetNextAddress = InvalidAddress;
  133. }
  134. _tree.Remove(nextAddress);
  135. _list.Remove(_dictionary[nextAddress]);
  136. _dictionary.Remove(nextAddress);
  137. }
  138. else
  139. {
  140. break;
  141. }
  142. }
  143. _tree.Add(expandedStart, expandedEnd);
  144. LinkedListNode<ulong> nodePtr = _list.AddAfter(node, expandedStart);
  145. _dictionary[expandedStart] = nodePtr;
  146. }
  147. }
  148. }
  149. /// <summary>
  150. /// Gets the address of an unused (free) region of the specified size.
  151. /// </summary>
  152. /// <param name="size">Size of the region in bytes</param>
  153. /// <param name="freeAddressStartPosition">Position at which memory can be allocated</param>
  154. /// <param name="alignment">Required alignment of the region address in bytes</param>
  155. /// <param name="start">Start address of the search on the address space</param>
  156. /// <returns>GPU virtual address of the allocation, or an all ones mask in case of failure</returns>
  157. public ulong GetFreeAddress(ulong size, out ulong freeAddressStartPosition, ulong alignment = 1, ulong start = DefaultStart)
  158. {
  159. // Note: Address 0 is not considered valid by the driver,
  160. // when 0 is returned it's considered a mapping error.
  161. lock (_tree)
  162. {
  163. ulong address = start;
  164. if (alignment == 0)
  165. {
  166. alignment = 1;
  167. }
  168. alignment = (alignment + PageMask) & ~PageMask;
  169. if (address < AddressSpaceSize)
  170. {
  171. bool completedFirstPass = false;
  172. ulong targetAddress;
  173. if(start == DefaultStart)
  174. {
  175. targetAddress = _list.Last.Value;
  176. }
  177. else
  178. {
  179. targetAddress = _tree.Floor(address);
  180. if(targetAddress == InvalidAddress)
  181. {
  182. targetAddress = _tree.Ceiling(address);
  183. }
  184. }
  185. while (address < AddressSpaceSize)
  186. {
  187. if (targetAddress != InvalidAddress)
  188. {
  189. if (address >= targetAddress)
  190. {
  191. if (address + size <= _tree.Get(targetAddress))
  192. {
  193. freeAddressStartPosition = targetAddress;
  194. return address;
  195. }
  196. else
  197. {
  198. LinkedListNode<ulong> nextPtr = _dictionary[targetAddress];
  199. if (nextPtr.Next != null)
  200. {
  201. targetAddress = nextPtr.Next.Value;
  202. }
  203. else
  204. {
  205. if (completedFirstPass)
  206. {
  207. break;
  208. }
  209. else
  210. {
  211. completedFirstPass = true;
  212. address = start;
  213. targetAddress = _tree.Floor(address);
  214. }
  215. }
  216. }
  217. }
  218. else
  219. {
  220. address += PageSize * (targetAddress / PageSize - (address / PageSize));
  221. ulong remainder = address % alignment;
  222. if (remainder != 0)
  223. {
  224. address = (address - remainder) + alignment;
  225. }
  226. }
  227. }
  228. else
  229. {
  230. break;
  231. }
  232. }
  233. }
  234. freeAddressStartPosition = InvalidAddress;
  235. }
  236. return PteUnmapped;
  237. }
  238. /// <summary>
  239. /// Checks if a given memory region is mapped or reserved.
  240. /// </summary>
  241. /// <param name="gpuVa">GPU virtual address of the page</param>
  242. /// <param name="size">Size of the allocation in bytes</param>
  243. /// <param name="freeAddressStartPosition">Nearest lower address that memory can be allocated</param>
  244. /// <returns>True if the page is mapped or reserved, false otherwise</returns>
  245. public bool IsRegionInUse(ulong gpuVa, ulong size, out ulong freeAddressStartPosition)
  246. {
  247. lock (_tree)
  248. {
  249. ulong floorAddress = _tree.Floor(gpuVa);
  250. freeAddressStartPosition = floorAddress;
  251. if (floorAddress != InvalidAddress)
  252. {
  253. return !(gpuVa >= floorAddress && ((gpuVa + size) < _tree.Get(floorAddress)));
  254. }
  255. }
  256. return true;
  257. }
  258. #endregion
  259. }
  260. }