NvGpuVmm.cs 10 KB

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