NvHostChannelIoctl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.Memory;
  4. using Ryujinx.HLE.HOS.Kernel.Process;
  5. using Ryujinx.HLE.HOS.Services.Nv.NvGpuAS;
  6. using Ryujinx.HLE.HOS.Services.Nv.NvMap;
  7. using System;
  8. using System.Collections.Concurrent;
  9. namespace Ryujinx.HLE.HOS.Services.Nv.NvHostChannel
  10. {
  11. class NvHostChannelIoctl
  12. {
  13. private static ConcurrentDictionary<KProcess, NvChannel> _channels;
  14. static NvHostChannelIoctl()
  15. {
  16. _channels = new ConcurrentDictionary<KProcess, NvChannel>();
  17. }
  18. public static int ProcessIoctl(ServiceCtx context, int cmd)
  19. {
  20. switch (cmd & 0xffff)
  21. {
  22. case 0x0001: return Submit (context);
  23. case 0x0002: return GetSyncpoint (context);
  24. case 0x0003: return GetWaitBase (context);
  25. case 0x0009: return MapBuffer (context);
  26. case 0x000a: return UnmapBuffer (context);
  27. case 0x4714: return SetUserData (context);
  28. case 0x4801: return SetNvMap (context);
  29. case 0x4803: return SetTimeout (context);
  30. case 0x4808: return SubmitGpfifo (context);
  31. case 0x4809: return AllocObjCtx (context);
  32. case 0x480b: return ZcullBind (context);
  33. case 0x480c: return SetErrorNotifier (context);
  34. case 0x480d: return SetPriority (context);
  35. case 0x481a: return AllocGpfifoEx2 (context);
  36. case 0x481b: return KickoffPbWithAttr(context);
  37. }
  38. throw new NotImplementedException(cmd.ToString("x8"));
  39. }
  40. private static int Submit(ServiceCtx context)
  41. {
  42. long inputPosition = context.Request.GetBufferType0x21().Position;
  43. long outputPosition = context.Request.GetBufferType0x22().Position;
  44. NvHostChannelSubmit args = MemoryHelper.Read<NvHostChannelSubmit>(context.Memory, inputPosition);
  45. NvGpuVmm vmm = NvGpuASIoctl.GetASCtx(context).Vmm;
  46. for (int index = 0; index < args.CmdBufsCount; index++)
  47. {
  48. long cmdBufOffset = inputPosition + 0x10 + index * 0xc;
  49. NvHostChannelCmdBuf cmdBuf = MemoryHelper.Read<NvHostChannelCmdBuf>(context.Memory, cmdBufOffset);
  50. NvMapHandle map = NvMapIoctl.GetNvMap(context, cmdBuf.MemoryId);
  51. int[] cmdBufData = new int[cmdBuf.WordsCount];
  52. for (int offset = 0; offset < cmdBufData.Length; offset++)
  53. {
  54. cmdBufData[offset] = context.Memory.ReadInt32(map.Address + cmdBuf.Offset + offset * 4);
  55. }
  56. context.Device.Gpu.PushCommandBuffer(vmm, cmdBufData);
  57. }
  58. //TODO: Relocation, waitchecks, etc.
  59. return NvResult.Success;
  60. }
  61. private static int GetSyncpoint(ServiceCtx context)
  62. {
  63. //TODO
  64. long inputPosition = context.Request.GetBufferType0x21().Position;
  65. long outputPosition = context.Request.GetBufferType0x22().Position;
  66. NvHostChannelGetParamArg args = MemoryHelper.Read<NvHostChannelGetParamArg>(context.Memory, inputPosition);
  67. args.Value = 0;
  68. MemoryHelper.Write(context.Memory, outputPosition, args);
  69. return NvResult.Success;
  70. }
  71. private static int GetWaitBase(ServiceCtx context)
  72. {
  73. long inputPosition = context.Request.GetBufferType0x21().Position;
  74. long outputPosition = context.Request.GetBufferType0x22().Position;
  75. NvHostChannelGetParamArg args = MemoryHelper.Read<NvHostChannelGetParamArg>(context.Memory, inputPosition);
  76. args.Value = 0;
  77. MemoryHelper.Write(context.Memory, outputPosition, args);
  78. return NvResult.Success;
  79. }
  80. private static int MapBuffer(ServiceCtx context)
  81. {
  82. long inputPosition = context.Request.GetBufferType0x21().Position;
  83. long outputPosition = context.Request.GetBufferType0x22().Position;
  84. NvHostChannelMapBuffer args = MemoryHelper.Read<NvHostChannelMapBuffer>(context.Memory, inputPosition);
  85. NvGpuVmm vmm = NvGpuASIoctl.GetASCtx(context).Vmm;
  86. for (int index = 0; index < args.NumEntries; index++)
  87. {
  88. int handle = context.Memory.ReadInt32(inputPosition + 0xc + index * 8);
  89. NvMapHandle map = NvMapIoctl.GetNvMap(context, handle);
  90. if (map == null)
  91. {
  92. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{handle:x8}!");
  93. return NvResult.InvalidInput;
  94. }
  95. lock (map)
  96. {
  97. if (map.DmaMapAddress == 0)
  98. {
  99. map.DmaMapAddress = vmm.MapLow(map.Address, map.Size);
  100. }
  101. context.Memory.WriteInt32(outputPosition + 0xc + 4 + index * 8, (int)map.DmaMapAddress);
  102. }
  103. }
  104. return NvResult.Success;
  105. }
  106. private static int UnmapBuffer(ServiceCtx context)
  107. {
  108. long inputPosition = context.Request.GetBufferType0x21().Position;
  109. NvHostChannelMapBuffer args = MemoryHelper.Read<NvHostChannelMapBuffer>(context.Memory, inputPosition);
  110. NvGpuVmm vmm = NvGpuASIoctl.GetASCtx(context).Vmm;
  111. for (int index = 0; index < args.NumEntries; index++)
  112. {
  113. int handle = context.Memory.ReadInt32(inputPosition + 0xc + index * 8);
  114. NvMapHandle map = NvMapIoctl.GetNvMap(context, handle);
  115. if (map == null)
  116. {
  117. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{handle:x8}!");
  118. return NvResult.InvalidInput;
  119. }
  120. lock (map)
  121. {
  122. if (map.DmaMapAddress != 0)
  123. {
  124. vmm.Free(map.DmaMapAddress, map.Size);
  125. map.DmaMapAddress = 0;
  126. }
  127. }
  128. }
  129. return NvResult.Success;
  130. }
  131. private static int SetUserData(ServiceCtx context)
  132. {
  133. long inputPosition = context.Request.GetBufferType0x21().Position;
  134. long outputPosition = context.Request.GetBufferType0x22().Position;
  135. Logger.PrintStub(LogClass.ServiceNv);
  136. return NvResult.Success;
  137. }
  138. private static int SetNvMap(ServiceCtx context)
  139. {
  140. long inputPosition = context.Request.GetBufferType0x21().Position;
  141. long outputPosition = context.Request.GetBufferType0x22().Position;
  142. Logger.PrintStub(LogClass.ServiceNv);
  143. return NvResult.Success;
  144. }
  145. private static int SetTimeout(ServiceCtx context)
  146. {
  147. long inputPosition = context.Request.GetBufferType0x21().Position;
  148. GetChannel(context).Timeout = context.Memory.ReadInt32(inputPosition);
  149. return NvResult.Success;
  150. }
  151. private static int SubmitGpfifo(ServiceCtx context)
  152. {
  153. long inputPosition = context.Request.GetBufferType0x21().Position;
  154. long outputPosition = context.Request.GetBufferType0x22().Position;
  155. NvHostChannelSubmitGpfifo args = MemoryHelper.Read<NvHostChannelSubmitGpfifo>(context.Memory, inputPosition);
  156. NvGpuVmm vmm = NvGpuASIoctl.GetASCtx(context).Vmm;
  157. for (int index = 0; index < args.NumEntries; index++)
  158. {
  159. long gpfifo = context.Memory.ReadInt64(inputPosition + 0x18 + index * 8);
  160. PushGpfifo(context, vmm, gpfifo);
  161. }
  162. args.SyncptId = 0;
  163. args.SyncptValue = 0;
  164. MemoryHelper.Write(context.Memory, outputPosition, args);
  165. return NvResult.Success;
  166. }
  167. private static int AllocObjCtx(ServiceCtx context)
  168. {
  169. long inputPosition = context.Request.GetBufferType0x21().Position;
  170. long outputPosition = context.Request.GetBufferType0x22().Position;
  171. Logger.PrintStub(LogClass.ServiceNv);
  172. return NvResult.Success;
  173. }
  174. private static int ZcullBind(ServiceCtx context)
  175. {
  176. long inputPosition = context.Request.GetBufferType0x21().Position;
  177. long outputPosition = context.Request.GetBufferType0x22().Position;
  178. Logger.PrintStub(LogClass.ServiceNv);
  179. return NvResult.Success;
  180. }
  181. private static int SetErrorNotifier(ServiceCtx context)
  182. {
  183. long inputPosition = context.Request.GetBufferType0x21().Position;
  184. long outputPosition = context.Request.GetBufferType0x22().Position;
  185. Logger.PrintStub(LogClass.ServiceNv);
  186. return NvResult.Success;
  187. }
  188. private static int SetPriority(ServiceCtx context)
  189. {
  190. long inputPosition = context.Request.GetBufferType0x21().Position;
  191. long outputPosition = context.Request.GetBufferType0x22().Position;
  192. Logger.PrintStub(LogClass.ServiceNv);
  193. return NvResult.Success;
  194. }
  195. private static int AllocGpfifoEx2(ServiceCtx context)
  196. {
  197. long inputPosition = context.Request.GetBufferType0x21().Position;
  198. long outputPosition = context.Request.GetBufferType0x22().Position;
  199. Logger.PrintStub(LogClass.ServiceNv);
  200. return NvResult.Success;
  201. }
  202. private static int KickoffPbWithAttr(ServiceCtx context)
  203. {
  204. long inputPosition = context.Request.GetBufferType0x21().Position;
  205. long outputPosition = context.Request.GetBufferType0x22().Position;
  206. NvHostChannelSubmitGpfifo args = MemoryHelper.Read<NvHostChannelSubmitGpfifo>(context.Memory, inputPosition);
  207. NvGpuVmm vmm = NvGpuASIoctl.GetASCtx(context).Vmm;
  208. for (int index = 0; index < args.NumEntries; index++)
  209. {
  210. long gpfifo = context.Memory.ReadInt64(args.Address + index * 8);
  211. PushGpfifo(context, vmm, gpfifo);
  212. }
  213. args.SyncptId = 0;
  214. args.SyncptValue = 0;
  215. MemoryHelper.Write(context.Memory, outputPosition, args);
  216. return NvResult.Success;
  217. }
  218. private static void PushGpfifo(ServiceCtx context, NvGpuVmm vmm, long gpfifo)
  219. {
  220. context.Device.Gpu.Pusher.Push(vmm, gpfifo);
  221. }
  222. public static NvChannel GetChannel(ServiceCtx context)
  223. {
  224. return _channels.GetOrAdd(context.Process, (key) => new NvChannel());
  225. }
  226. public static void UnloadProcess(KProcess process)
  227. {
  228. _channels.TryRemove(process, out _);
  229. }
  230. }
  231. }