INvDrvServices.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.HLE.HOS.Ipc;
  4. using Ryujinx.HLE.HOS.Kernel.Common;
  5. using Ryujinx.HLE.HOS.Kernel.Process;
  6. using Ryujinx.HLE.HOS.Kernel.Threading;
  7. using Ryujinx.HLE.HOS.Services.Nv.NvGpuAS;
  8. using Ryujinx.HLE.HOS.Services.Nv.NvGpuGpu;
  9. using Ryujinx.HLE.HOS.Services.Nv.NvHostChannel;
  10. using Ryujinx.HLE.HOS.Services.Nv.NvHostCtrl;
  11. using Ryujinx.HLE.HOS.Services.Nv.NvMap;
  12. using System;
  13. using System.Collections.Generic;
  14. namespace Ryujinx.HLE.HOS.Services.Nv
  15. {
  16. class INvDrvServices : IpcService
  17. {
  18. private delegate int IoctlProcessor(ServiceCtx context, int cmd);
  19. private Dictionary<int, ServiceProcessRequest> _commands;
  20. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  21. private static Dictionary<string, IoctlProcessor> _ioctlProcessors =
  22. new Dictionary<string, IoctlProcessor>()
  23. {
  24. { "/dev/nvhost-as-gpu", ProcessIoctlNvGpuAS },
  25. { "/dev/nvhost-ctrl", ProcessIoctlNvHostCtrl },
  26. { "/dev/nvhost-ctrl-gpu", ProcessIoctlNvGpuGpu },
  27. { "/dev/nvhost-gpu", ProcessIoctlNvHostChannel },
  28. { "/dev/nvhost-nvdec", ProcessIoctlNvHostChannel },
  29. { "/dev/nvhost-vic", ProcessIoctlNvHostChannel },
  30. { "/dev/nvmap", ProcessIoctlNvMap }
  31. };
  32. public static GlobalStateTable Fds { get; private set; }
  33. private KEvent _event;
  34. public INvDrvServices(Horizon system)
  35. {
  36. _commands = new Dictionary<int, ServiceProcessRequest>()
  37. {
  38. { 0, Open },
  39. { 1, Ioctl },
  40. { 2, Close },
  41. { 3, Initialize },
  42. { 4, QueryEvent },
  43. { 8, SetClientPid },
  44. { 11, Ioctl },
  45. { 13, FinishInitialize }
  46. };
  47. _event = new KEvent(system);
  48. }
  49. static INvDrvServices()
  50. {
  51. Fds = new GlobalStateTable();
  52. }
  53. public long Open(ServiceCtx context)
  54. {
  55. long namePtr = context.Request.SendBuff[0].Position;
  56. string name = MemoryHelper.ReadAsciiString(context.Memory, namePtr);
  57. int fd = Fds.Add(context.Process, new NvFd(name));
  58. context.ResponseData.Write(fd);
  59. context.ResponseData.Write(0);
  60. return 0;
  61. }
  62. public long Ioctl(ServiceCtx context)
  63. {
  64. int fd = context.RequestData.ReadInt32();
  65. int cmd = context.RequestData.ReadInt32();
  66. NvFd fdData = Fds.GetData<NvFd>(context.Process, fd);
  67. int result;
  68. if (_ioctlProcessors.TryGetValue(fdData.Name, out IoctlProcessor process))
  69. {
  70. result = process(context, cmd);
  71. }
  72. else
  73. {
  74. throw new NotImplementedException($"{fdData.Name} {cmd:x4}");
  75. }
  76. //TODO: Verify if the error codes needs to be translated.
  77. context.ResponseData.Write(result);
  78. return 0;
  79. }
  80. public long Close(ServiceCtx context)
  81. {
  82. int fd = context.RequestData.ReadInt32();
  83. Fds.Delete(context.Process, fd);
  84. context.ResponseData.Write(0);
  85. return 0;
  86. }
  87. public long Initialize(ServiceCtx context)
  88. {
  89. long transferMemSize = context.RequestData.ReadInt64();
  90. int transferMemHandle = context.Request.HandleDesc.ToCopy[0];
  91. NvMapIoctl.InitializeNvMap(context);
  92. context.ResponseData.Write(0);
  93. return 0;
  94. }
  95. public long QueryEvent(ServiceCtx context)
  96. {
  97. int fd = context.RequestData.ReadInt32();
  98. int eventId = context.RequestData.ReadInt32();
  99. //TODO: Use Fd/EventId, different channels have different events.
  100. if (context.Process.HandleTable.GenerateHandle(_event.ReadableEvent, out int handle) != KernelResult.Success)
  101. {
  102. throw new InvalidOperationException("Out of handles!");
  103. }
  104. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);
  105. context.ResponseData.Write(0);
  106. return 0;
  107. }
  108. public long SetClientPid(ServiceCtx context)
  109. {
  110. long pid = context.RequestData.ReadInt64();
  111. context.ResponseData.Write(0);
  112. return 0;
  113. }
  114. public long FinishInitialize(ServiceCtx context)
  115. {
  116. Logger.PrintStub(LogClass.ServiceNv);
  117. return 0;
  118. }
  119. private static int ProcessIoctlNvGpuAS(ServiceCtx context, int cmd)
  120. {
  121. return ProcessIoctl(context, cmd, NvGpuASIoctl.ProcessIoctl);
  122. }
  123. private static int ProcessIoctlNvHostCtrl(ServiceCtx context, int cmd)
  124. {
  125. return ProcessIoctl(context, cmd, NvHostCtrlIoctl.ProcessIoctl);
  126. }
  127. private static int ProcessIoctlNvGpuGpu(ServiceCtx context, int cmd)
  128. {
  129. return ProcessIoctl(context, cmd, NvGpuGpuIoctl.ProcessIoctl);
  130. }
  131. private static int ProcessIoctlNvHostChannel(ServiceCtx context, int cmd)
  132. {
  133. return ProcessIoctl(context, cmd, NvHostChannelIoctl.ProcessIoctl);
  134. }
  135. private static int ProcessIoctlNvMap(ServiceCtx context, int cmd)
  136. {
  137. return ProcessIoctl(context, cmd, NvMapIoctl.ProcessIoctl);
  138. }
  139. private static int ProcessIoctl(ServiceCtx context, int cmd, IoctlProcessor processor)
  140. {
  141. if (CmdIn(cmd) && context.Request.GetBufferType0x21().Position == 0)
  142. {
  143. Logger.PrintError(LogClass.ServiceNv, "Input buffer is null!");
  144. return NvResult.InvalidInput;
  145. }
  146. if (CmdOut(cmd) && context.Request.GetBufferType0x22().Position == 0)
  147. {
  148. Logger.PrintError(LogClass.ServiceNv, "Output buffer is null!");
  149. return NvResult.InvalidInput;
  150. }
  151. return processor(context, cmd);
  152. }
  153. private static bool CmdIn(int cmd)
  154. {
  155. return ((cmd >> 30) & 1) != 0;
  156. }
  157. private static bool CmdOut(int cmd)
  158. {
  159. return ((cmd >> 31) & 1) != 0;
  160. }
  161. public static void UnloadProcess(KProcess process)
  162. {
  163. Fds.DeleteProcess(process);
  164. NvGpuASIoctl.UnloadProcess(process);
  165. NvHostChannelIoctl.UnloadProcess(process);
  166. NvHostCtrlIoctl.UnloadProcess(process);
  167. NvMapIoctl.UnloadProcess(process);
  168. }
  169. }
  170. }