NvGpuVmm.cs 10 KB

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