NvGpuVmm.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Graphics.Gal;
  3. using System;
  4. namespace Ryujinx.Graphics.Memory
  5. {
  6. public class NvGpuVmm : IMemory, IGalMemory
  7. {
  8. public const long AddrSize = 1L << 40;
  9. private const int PtLvl0Bits = 14;
  10. private const int PtLvl1Bits = 14;
  11. private const int PtPageBits = 12;
  12. private const int PtLvl0Size = 1 << PtLvl0Bits;
  13. private const int PtLvl1Size = 1 << PtLvl1Bits;
  14. public const int PageSize = 1 << PtPageBits;
  15. private const int PtLvl0Mask = PtLvl0Size - 1;
  16. private const int PtLvl1Mask = PtLvl1Size - 1;
  17. public const int PageMask = PageSize - 1;
  18. private const int PtLvl0Bit = PtPageBits + PtLvl1Bits;
  19. private const int PtLvl1Bit = PtPageBits;
  20. public MemoryManager Memory { get; private set; }
  21. private NvGpuVmmCache _cache;
  22. private const long PteUnmapped = -1;
  23. private const long PteReserved = -2;
  24. private long[][] _pageTable;
  25. public NvGpuVmm(MemoryManager memory)
  26. {
  27. Memory = memory;
  28. _cache = new NvGpuVmmCache(memory);
  29. _pageTable = new long[PtLvl0Size][];
  30. }
  31. public long Map(long pa, long va, long size)
  32. {
  33. lock (_pageTable)
  34. {
  35. for (long offset = 0; offset < size; offset += PageSize)
  36. {
  37. SetPte(va + offset, pa + offset);
  38. }
  39. }
  40. return va;
  41. }
  42. public long Map(long pa, long size)
  43. {
  44. lock (_pageTable)
  45. {
  46. long va = GetFreePosition(size);
  47. if (va != -1)
  48. {
  49. for (long offset = 0; offset < size; offset += PageSize)
  50. {
  51. SetPte(va + offset, pa + offset);
  52. }
  53. }
  54. return va;
  55. }
  56. }
  57. public long MapLow(long pa, long size)
  58. {
  59. lock (_pageTable)
  60. {
  61. long va = GetFreePosition(size, 1, PageSize);
  62. if (va != -1 && (ulong)va <= uint.MaxValue && (ulong)(va + size) <= uint.MaxValue)
  63. {
  64. for (long offset = 0; offset < size; offset += PageSize)
  65. {
  66. SetPte(va + offset, pa + offset);
  67. }
  68. }
  69. else
  70. {
  71. va = -1;
  72. }
  73. return va;
  74. }
  75. }
  76. public long ReserveFixed(long va, long size)
  77. {
  78. lock (_pageTable)
  79. {
  80. for (long offset = 0; offset < size; offset += PageSize)
  81. {
  82. if (IsPageInUse(va + offset))
  83. {
  84. return -1;
  85. }
  86. }
  87. for (long offset = 0; offset < size; offset += PageSize)
  88. {
  89. SetPte(va + offset, PteReserved);
  90. }
  91. }
  92. return va;
  93. }
  94. public long Reserve(long size, long align)
  95. {
  96. lock (_pageTable)
  97. {
  98. long position = GetFreePosition(size, align);
  99. if (position != -1)
  100. {
  101. for (long offset = 0; offset < size; offset += PageSize)
  102. {
  103. SetPte(position + offset, PteReserved);
  104. }
  105. }
  106. return position;
  107. }
  108. }
  109. public void Free(long va, long size)
  110. {
  111. lock (_pageTable)
  112. {
  113. for (long offset = 0; offset < size; offset += PageSize)
  114. {
  115. SetPte(va + offset, PteUnmapped);
  116. }
  117. }
  118. }
  119. private long GetFreePosition(long size, long align = 1, long start = 1L << 32)
  120. {
  121. //Note: Address 0 is not considered valid by the driver,
  122. //when 0 is returned it's considered a mapping error.
  123. long position = start;
  124. long freeSize = 0;
  125. if (align < 1)
  126. {
  127. align = 1;
  128. }
  129. align = (align + PageMask) & ~PageMask;
  130. while (position + freeSize < AddrSize)
  131. {
  132. if (!IsPageInUse(position + freeSize))
  133. {
  134. freeSize += PageSize;
  135. if (freeSize >= size)
  136. {
  137. return position;
  138. }
  139. }
  140. else
  141. {
  142. position += freeSize + PageSize;
  143. freeSize = 0;
  144. long remainder = position % align;
  145. if (remainder != 0)
  146. {
  147. position = (position - remainder) + align;
  148. }
  149. }
  150. }
  151. return -1;
  152. }
  153. public long GetPhysicalAddress(long va)
  154. {
  155. long basePos = GetPte(va);
  156. if (basePos < 0)
  157. {
  158. return -1;
  159. }
  160. return basePos + (va & PageMask);
  161. }
  162. public bool IsRegionFree(long va, long size)
  163. {
  164. for (long offset = 0; offset < size; offset += PageSize)
  165. {
  166. if (IsPageInUse(va + offset))
  167. {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. private bool IsPageInUse(long va)
  174. {
  175. if (va >> PtLvl0Bits + PtLvl1Bits + PtPageBits != 0)
  176. {
  177. return false;
  178. }
  179. long l0 = (va >> PtLvl0Bit) & PtLvl0Mask;
  180. long l1 = (va >> PtLvl1Bit) & PtLvl1Mask;
  181. if (_pageTable[l0] == null)
  182. {
  183. return false;
  184. }
  185. return _pageTable[l0][l1] != PteUnmapped;
  186. }
  187. private long GetPte(long position)
  188. {
  189. long l0 = (position >> PtLvl0Bit) & PtLvl0Mask;
  190. long l1 = (position >> PtLvl1Bit) & PtLvl1Mask;
  191. if (_pageTable[l0] == null)
  192. {
  193. return -1;
  194. }
  195. return _pageTable[l0][l1];
  196. }
  197. private void SetPte(long position, long tgtAddr)
  198. {
  199. long l0 = (position >> PtLvl0Bit) & PtLvl0Mask;
  200. long l1 = (position >> PtLvl1Bit) & PtLvl1Mask;
  201. if (_pageTable[l0] == null)
  202. {
  203. _pageTable[l0] = new long[PtLvl1Size];
  204. for (int index = 0; index < PtLvl1Size; index++)
  205. {
  206. _pageTable[l0][index] = PteUnmapped;
  207. }
  208. }
  209. _pageTable[l0][l1] = tgtAddr;
  210. }
  211. public bool IsRegionModified(long pa, long size, NvGpuBufferType bufferType)
  212. {
  213. return _cache.IsRegionModified(pa, size, bufferType);
  214. }
  215. public bool TryGetHostAddress(long position, long size, out IntPtr ptr)
  216. {
  217. return Memory.TryGetHostAddress(GetPhysicalAddress(position), size, out ptr);
  218. }
  219. public byte ReadByte(long position)
  220. {
  221. position = GetPhysicalAddress(position);
  222. return Memory.ReadByte(position);
  223. }
  224. public ushort ReadUInt16(long position)
  225. {
  226. position = GetPhysicalAddress(position);
  227. return Memory.ReadUInt16(position);
  228. }
  229. public uint ReadUInt32(long position)
  230. {
  231. position = GetPhysicalAddress(position);
  232. return Memory.ReadUInt32(position);
  233. }
  234. public ulong ReadUInt64(long position)
  235. {
  236. position = GetPhysicalAddress(position);
  237. return Memory.ReadUInt64(position);
  238. }
  239. public sbyte ReadSByte(long position)
  240. {
  241. position = GetPhysicalAddress(position);
  242. return Memory.ReadSByte(position);
  243. }
  244. public short ReadInt16(long position)
  245. {
  246. position = GetPhysicalAddress(position);
  247. return Memory.ReadInt16(position);
  248. }
  249. public int ReadInt32(long position)
  250. {
  251. position = GetPhysicalAddress(position);
  252. return Memory.ReadInt32(position);
  253. }
  254. public long ReadInt64(long position)
  255. {
  256. position = GetPhysicalAddress(position);
  257. return Memory.ReadInt64(position);
  258. }
  259. public byte[] ReadBytes(long position, long size)
  260. {
  261. position = GetPhysicalAddress(position);
  262. return Memory.ReadBytes(position, size);
  263. }
  264. public void WriteByte(long position, byte value)
  265. {
  266. position = GetPhysicalAddress(position);
  267. Memory.WriteByte(position, value);
  268. }
  269. public void WriteUInt16(long position, ushort value)
  270. {
  271. position = GetPhysicalAddress(position);
  272. Memory.WriteUInt16(position, value);
  273. }
  274. public void WriteUInt32(long position, uint value)
  275. {
  276. position = GetPhysicalAddress(position);
  277. Memory.WriteUInt32(position, value);
  278. }
  279. public void WriteUInt64(long position, ulong value)
  280. {
  281. position = GetPhysicalAddress(position);
  282. Memory.WriteUInt64(position, value);
  283. }
  284. public void WriteSByte(long position, sbyte value)
  285. {
  286. position = GetPhysicalAddress(position);
  287. Memory.WriteSByte(position, value);
  288. }
  289. public void WriteInt16(long position, short value)
  290. {
  291. position = GetPhysicalAddress(position);
  292. Memory.WriteInt16(position, value);
  293. }
  294. public void WriteInt32(long position, int value)
  295. {
  296. position = GetPhysicalAddress(position);
  297. Memory.WriteInt32(position, value);
  298. }
  299. public void WriteInt64(long position, long value)
  300. {
  301. position = GetPhysicalAddress(position);
  302. Memory.WriteInt64(position, value);
  303. }
  304. public void WriteBytes(long position, byte[] data)
  305. {
  306. position = GetPhysicalAddress(position);
  307. Memory.WriteBytes(position, data);
  308. }
  309. }
  310. }