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. this.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. }