NvGpuVmm.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Graphics.Gal;
  3. using System;
  4. using System.Collections.Concurrent;
  5. namespace Ryujinx.HLE.Gpu.Memory
  6. {
  7. class NvGpuVmm : IAMemory, IGalMemory
  8. {
  9. public const long AddrSize = 1L << 40;
  10. private const int PTLvl0Bits = 14;
  11. private const int PTLvl1Bits = 14;
  12. private const int PTPageBits = 12;
  13. private const int PTLvl0Size = 1 << PTLvl0Bits;
  14. private const int PTLvl1Size = 1 << PTLvl1Bits;
  15. public const int PageSize = 1 << PTPageBits;
  16. private const int PTLvl0Mask = PTLvl0Size - 1;
  17. private const int PTLvl1Mask = PTLvl1Size - 1;
  18. public const int PageMask = PageSize - 1;
  19. private const int PTLvl0Bit = PTPageBits + PTLvl1Bits;
  20. private const int PTLvl1Bit = PTPageBits;
  21. public AMemory Memory { get; private set; }
  22. private struct MappedMemory
  23. {
  24. public long Size;
  25. public MappedMemory(long Size)
  26. {
  27. this.Size = Size;
  28. }
  29. }
  30. private ConcurrentDictionary<long, MappedMemory> Maps;
  31. private NvGpuVmmCache Cache;
  32. private const long PteUnmapped = -1;
  33. private const long PteReserved = -2;
  34. private long[][] PageTable;
  35. public NvGpuVmm(AMemory Memory)
  36. {
  37. this.Memory = Memory;
  38. Maps = new ConcurrentDictionary<long, MappedMemory>();
  39. Cache = new NvGpuVmmCache();
  40. PageTable = new long[PTLvl0Size][];
  41. }
  42. public long Map(long PA, long VA, long Size)
  43. {
  44. lock (PageTable)
  45. {
  46. for (long Offset = 0; Offset < Size; Offset += PageSize)
  47. {
  48. if (GetPte(VA + Offset) != PteReserved)
  49. {
  50. return Map(PA, Size);
  51. }
  52. }
  53. for (long Offset = 0; Offset < Size; Offset += PageSize)
  54. {
  55. SetPte(VA + Offset, PA + Offset);
  56. }
  57. }
  58. return VA;
  59. }
  60. public long Map(long PA, long Size)
  61. {
  62. lock (PageTable)
  63. {
  64. long VA = GetFreePosition(Size);
  65. if (VA != -1)
  66. {
  67. MappedMemory Map = new MappedMemory(Size);
  68. Maps.AddOrUpdate(VA, Map, (Key, Old) => Map);
  69. for (long Offset = 0; Offset < Size; Offset += PageSize)
  70. {
  71. SetPte(VA + Offset, PA + Offset);
  72. }
  73. }
  74. return VA;
  75. }
  76. }
  77. public bool Unmap(long VA)
  78. {
  79. if (Maps.TryRemove(VA, out MappedMemory Map))
  80. {
  81. Free(VA, Map.Size);
  82. return true;
  83. }
  84. return false;
  85. }
  86. public long Reserve(long VA, long Size, long Align)
  87. {
  88. lock (PageTable)
  89. {
  90. for (long Offset = 0; Offset < Size; Offset += PageSize)
  91. {
  92. if (IsPageInUse(VA + Offset))
  93. {
  94. return Reserve(Size, Align);
  95. }
  96. }
  97. for (long Offset = 0; Offset < Size; Offset += PageSize)
  98. {
  99. SetPte(VA + Offset, PteReserved);
  100. }
  101. }
  102. return VA;
  103. }
  104. public long Reserve(long Size, long Align)
  105. {
  106. lock (PageTable)
  107. {
  108. long Position = GetFreePosition(Size, Align);
  109. if (Position != -1)
  110. {
  111. for (long Offset = 0; Offset < Size; Offset += PageSize)
  112. {
  113. SetPte(Position + Offset, PteReserved);
  114. }
  115. }
  116. return Position;
  117. }
  118. }
  119. public void Free(long VA, long Size)
  120. {
  121. lock (PageTable)
  122. {
  123. for (long Offset = 0; Offset < Size; Offset += PageSize)
  124. {
  125. SetPte(VA + Offset, PteUnmapped);
  126. }
  127. }
  128. }
  129. private long GetFreePosition(long Size, long Align = 1)
  130. {
  131. long Position = 0;
  132. long FreeSize = 0;
  133. if (Align < 1)
  134. {
  135. Align = 1;
  136. }
  137. Align = (Align + PageMask) & ~PageMask;
  138. while (Position + FreeSize < AddrSize)
  139. {
  140. if (!IsPageInUse(Position + FreeSize))
  141. {
  142. FreeSize += PageSize;
  143. if (FreeSize >= Size)
  144. {
  145. return Position;
  146. }
  147. }
  148. else
  149. {
  150. Position += FreeSize + PageSize;
  151. FreeSize = 0;
  152. long Remainder = Position % Align;
  153. if (Remainder != 0)
  154. {
  155. Position = (Position - Remainder) + Align;
  156. }
  157. }
  158. }
  159. return -1;
  160. }
  161. public long GetPhysicalAddress(long VA)
  162. {
  163. long BasePos = GetPte(VA);
  164. if (BasePos < 0)
  165. {
  166. return -1;
  167. }
  168. return BasePos + (VA & PageMask);
  169. }
  170. public bool IsRegionFree(long VA, long Size)
  171. {
  172. for (long Offset = 0; Offset < Size; Offset += PageSize)
  173. {
  174. if (IsPageInUse(VA + Offset))
  175. {
  176. return false;
  177. }
  178. }
  179. return true;
  180. }
  181. private bool IsPageInUse(long VA)
  182. {
  183. if (VA >> PTLvl0Bits + PTLvl1Bits + PTPageBits != 0)
  184. {
  185. return false;
  186. }
  187. long L0 = (VA >> PTLvl0Bit) & PTLvl0Mask;
  188. long L1 = (VA >> PTLvl1Bit) & PTLvl1Mask;
  189. if (PageTable[L0] == null)
  190. {
  191. return false;
  192. }
  193. return PageTable[L0][L1] != PteUnmapped;
  194. }
  195. private long GetPte(long Position)
  196. {
  197. long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
  198. long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
  199. if (PageTable[L0] == null)
  200. {
  201. return -1;
  202. }
  203. return PageTable[L0][L1];
  204. }
  205. private void SetPte(long Position, long TgtAddr)
  206. {
  207. long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
  208. long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
  209. if (PageTable[L0] == null)
  210. {
  211. PageTable[L0] = new long[PTLvl1Size];
  212. for (int Index = 0; Index < PTLvl1Size; Index++)
  213. {
  214. PageTable[L0][Index] = PteUnmapped;
  215. }
  216. }
  217. PageTable[L0][L1] = TgtAddr;
  218. }
  219. public bool IsRegionModified(long PA, long Size, NvGpuBufferType BufferType)
  220. {
  221. return Cache.IsRegionModified(Memory, BufferType, PA, Size);
  222. }
  223. public IntPtr GetHostAddress(long Position, long Size)
  224. {
  225. return Memory.GetHostAddress(GetPhysicalAddress(Position), Size);
  226. }
  227. public byte ReadByte(long Position)
  228. {
  229. Position = GetPhysicalAddress(Position);
  230. return Memory.ReadByte(Position);
  231. }
  232. public ushort ReadUInt16(long Position)
  233. {
  234. Position = GetPhysicalAddress(Position);
  235. return Memory.ReadUInt16(Position);
  236. }
  237. public uint ReadUInt32(long Position)
  238. {
  239. Position = GetPhysicalAddress(Position);
  240. return Memory.ReadUInt32(Position);
  241. }
  242. public ulong ReadUInt64(long Position)
  243. {
  244. Position = GetPhysicalAddress(Position);
  245. return Memory.ReadUInt64(Position);
  246. }
  247. public sbyte ReadSByte(long Position)
  248. {
  249. Position = GetPhysicalAddress(Position);
  250. return Memory.ReadSByte(Position);
  251. }
  252. public short ReadInt16(long Position)
  253. {
  254. Position = GetPhysicalAddress(Position);
  255. return Memory.ReadInt16(Position);
  256. }
  257. public int ReadInt32(long Position)
  258. {
  259. Position = GetPhysicalAddress(Position);
  260. return Memory.ReadInt32(Position);
  261. }
  262. public long ReadInt64(long Position)
  263. {
  264. Position = GetPhysicalAddress(Position);
  265. return Memory.ReadInt64(Position);
  266. }
  267. public byte[] ReadBytes(long Position, long Size)
  268. {
  269. Position = GetPhysicalAddress(Position);
  270. return Memory.ReadBytes(Position, Size);
  271. }
  272. public void WriteByte(long Position, byte Value)
  273. {
  274. Position = GetPhysicalAddress(Position);
  275. Memory.WriteByte(Position, Value);
  276. }
  277. public void WriteUInt16(long Position, ushort Value)
  278. {
  279. Position = GetPhysicalAddress(Position);
  280. Memory.WriteUInt16(Position, Value);
  281. }
  282. public void WriteUInt32(long Position, uint Value)
  283. {
  284. Position = GetPhysicalAddress(Position);
  285. Memory.WriteUInt32(Position, Value);
  286. }
  287. public void WriteUInt64(long Position, ulong Value)
  288. {
  289. Position = GetPhysicalAddress(Position);
  290. Memory.WriteUInt64(Position, Value);
  291. }
  292. public void WriteSByte(long Position, sbyte Value)
  293. {
  294. Position = GetPhysicalAddress(Position);
  295. Memory.WriteSByte(Position, Value);
  296. }
  297. public void WriteInt16(long Position, short Value)
  298. {
  299. Position = GetPhysicalAddress(Position);
  300. Memory.WriteInt16(Position, Value);
  301. }
  302. public void WriteInt32(long Position, int Value)
  303. {
  304. Position = GetPhysicalAddress(Position);
  305. Memory.WriteInt32(Position, Value);
  306. }
  307. public void WriteInt64(long Position, long Value)
  308. {
  309. Position = GetPhysicalAddress(Position);
  310. Memory.WriteInt64(Position, Value);
  311. }
  312. public void WriteBytes(long Position, byte[] Data)
  313. {
  314. Position = GetPhysicalAddress(Position);
  315. Memory.WriteBytes(Position, Data);
  316. }
  317. }
  318. }