DmaPusher.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Runtime.InteropServices;
  4. using System.Threading;
  5. namespace Ryujinx.Graphics.Gpu
  6. {
  7. /// <summary>
  8. /// GPU DMA pusher, used to push commands to the GPU.
  9. /// </summary>
  10. public class DmaPusher
  11. {
  12. private ConcurrentQueue<CommandBuffer> _commandBufferQueue;
  13. private enum CommandBufferType
  14. {
  15. Prefetch,
  16. NoPrefetch,
  17. }
  18. private struct CommandBuffer
  19. {
  20. /// <summary>
  21. /// The type of the command buffer.
  22. /// </summary>
  23. public CommandBufferType Type;
  24. /// <summary>
  25. /// Fetched data.
  26. /// </summary>
  27. public int[] Words;
  28. /// <summary>
  29. /// The GPFIFO entry address. (used in NoPrefetch mode)
  30. /// </summary>
  31. public ulong EntryAddress;
  32. /// <summary>
  33. /// The count of entries inside this GPFIFO entry.
  34. /// </summary>
  35. public uint EntryCount;
  36. /// <summary>
  37. /// Fetch the command buffer.
  38. /// </summary>
  39. public void Fetch(GpuContext context)
  40. {
  41. if (Words == null)
  42. {
  43. Words = MemoryMarshal.Cast<byte, int>(context.MemoryAccessor.GetSpan(EntryAddress, (int)EntryCount * 4)).ToArray();
  44. }
  45. }
  46. /// <summary>
  47. /// Read inside the command buffer.
  48. /// </summary>
  49. /// <param name="context">The GPU context</param>
  50. /// <param name="index">The index inside the command buffer</param>
  51. /// <returns>The value read</returns>
  52. public int ReadAt(GpuContext context, int index)
  53. {
  54. return Words[index];
  55. }
  56. }
  57. private CommandBuffer _currentCommandBuffer;
  58. private int _wordsPosition;
  59. /// <summary>
  60. /// Internal GPFIFO state.
  61. /// </summary>
  62. private struct DmaState
  63. {
  64. public int Method;
  65. public int SubChannel;
  66. public int MethodCount;
  67. public bool NonIncrementing;
  68. public bool IncrementOnce;
  69. public int LengthPending;
  70. }
  71. private DmaState _state;
  72. private bool _ibEnable;
  73. private GpuContext _context;
  74. private AutoResetEvent _event;
  75. /// <summary>
  76. /// Creates a new instance of the GPU DMA pusher.
  77. /// </summary>
  78. /// <param name="context">GPU context that the pusher belongs to</param>
  79. internal DmaPusher(GpuContext context)
  80. {
  81. _context = context;
  82. _ibEnable = true;
  83. _commandBufferQueue = new ConcurrentQueue<CommandBuffer>();
  84. _event = new AutoResetEvent(false);
  85. }
  86. /// <summary>
  87. /// Signal the pusher that there are new entries to process.
  88. /// </summary>
  89. public void SignalNewEntries()
  90. {
  91. _event.Set();
  92. }
  93. /// <summary>
  94. /// Push a GPFIFO entry in the form of a prefetched command buffer.
  95. /// It is intended to be used by nvservices to handle special cases.
  96. /// </summary>
  97. /// <param name="commandBuffer">The command buffer containing the prefetched commands</param>
  98. public void PushHostCommandBuffer(int[] commandBuffer)
  99. {
  100. _commandBufferQueue.Enqueue(new CommandBuffer
  101. {
  102. Type = CommandBufferType.Prefetch,
  103. Words = commandBuffer,
  104. EntryAddress = ulong.MaxValue,
  105. EntryCount = (uint)commandBuffer.Length
  106. });
  107. }
  108. /// <summary>
  109. /// Create a CommandBuffer from a GPFIFO entry.
  110. /// </summary>
  111. /// <param name="entry">The GPFIFO entry</param>
  112. /// <returns>A new CommandBuffer based on the GPFIFO entry</returns>
  113. private CommandBuffer CreateCommandBuffer(ulong entry)
  114. {
  115. ulong length = (entry >> 42) & 0x1fffff;
  116. ulong startAddress = entry & 0xfffffffffc;
  117. bool noPrefetch = (entry & (1UL << 63)) != 0;
  118. CommandBufferType type = CommandBufferType.Prefetch;
  119. if (noPrefetch)
  120. {
  121. type = CommandBufferType.NoPrefetch;
  122. }
  123. return new CommandBuffer
  124. {
  125. Type = type,
  126. Words = null,
  127. EntryAddress = startAddress,
  128. EntryCount = (uint)length
  129. };
  130. }
  131. /// <summary>
  132. /// Pushes GPFIFO entries.
  133. /// </summary>
  134. /// <param name="entries">GPFIFO entries</param>
  135. public void PushEntries(ReadOnlySpan<ulong> entries)
  136. {
  137. bool beforeBarrier = true;
  138. foreach (ulong entry in entries)
  139. {
  140. CommandBuffer commandBuffer = CreateCommandBuffer(entry);
  141. if (beforeBarrier && commandBuffer.Type == CommandBufferType.Prefetch)
  142. {
  143. commandBuffer.Fetch(_context);
  144. }
  145. if (commandBuffer.Type == CommandBufferType.NoPrefetch)
  146. {
  147. beforeBarrier = false;
  148. }
  149. _commandBufferQueue.Enqueue(commandBuffer);
  150. }
  151. }
  152. /// <summary>
  153. /// Waits until commands are pushed to the FIFO.
  154. /// </summary>
  155. /// <returns>True if commands were received, false if wait timed out</returns>
  156. public bool WaitForCommands()
  157. {
  158. return _event.WaitOne(8);
  159. }
  160. /// <summary>
  161. /// Processes commands pushed to the FIFO.
  162. /// </summary>
  163. public void DispatchCalls()
  164. {
  165. while (Step());
  166. }
  167. /// <summary>
  168. /// Processes a single command on the FIFO.
  169. /// </summary>
  170. /// <returns>True if the FIFO still has commands to be processed, false otherwise</returns>
  171. private bool Step()
  172. {
  173. if (_wordsPosition != _currentCommandBuffer.EntryCount)
  174. {
  175. int word = _currentCommandBuffer.ReadAt(_context, _wordsPosition++);
  176. if (_state.LengthPending != 0)
  177. {
  178. _state.LengthPending = 0;
  179. _state.MethodCount = word & 0xffffff;
  180. }
  181. else if (_state.MethodCount != 0)
  182. {
  183. CallMethod(word);
  184. if (!_state.NonIncrementing)
  185. {
  186. _state.Method++;
  187. }
  188. if (_state.IncrementOnce)
  189. {
  190. _state.NonIncrementing = true;
  191. }
  192. _state.MethodCount--;
  193. }
  194. else
  195. {
  196. int submissionMode = (word >> 29) & 7;
  197. switch (submissionMode)
  198. {
  199. case 1:
  200. // Incrementing.
  201. SetNonImmediateState(word);
  202. _state.NonIncrementing = false;
  203. _state.IncrementOnce = false;
  204. break;
  205. case 3:
  206. // Non-incrementing.
  207. SetNonImmediateState(word);
  208. _state.NonIncrementing = true;
  209. _state.IncrementOnce = false;
  210. break;
  211. case 4:
  212. // Immediate.
  213. _state.Method = (word >> 0) & 0x1fff;
  214. _state.SubChannel = (word >> 13) & 7;
  215. _state.NonIncrementing = true;
  216. _state.IncrementOnce = false;
  217. CallMethod((word >> 16) & 0x1fff);
  218. break;
  219. case 5:
  220. // Increment-once.
  221. SetNonImmediateState(word);
  222. _state.NonIncrementing = false;
  223. _state.IncrementOnce = true;
  224. break;
  225. }
  226. }
  227. }
  228. else if (_ibEnable && _commandBufferQueue.TryDequeue(out CommandBuffer entry))
  229. {
  230. _currentCommandBuffer = entry;
  231. _wordsPosition = 0;
  232. _currentCommandBuffer.Fetch(_context);
  233. }
  234. else
  235. {
  236. return false;
  237. }
  238. return true;
  239. }
  240. /// <summary>
  241. /// Sets current non-immediate method call state.
  242. /// </summary>
  243. /// <param name="word">Compressed method word</param>
  244. private void SetNonImmediateState(int word)
  245. {
  246. _state.Method = (word >> 0) & 0x1fff;
  247. _state.SubChannel = (word >> 13) & 7;
  248. _state.MethodCount = (word >> 16) & 0x1fff;
  249. }
  250. /// <summary>
  251. /// Forwards the method call to GPU engines.
  252. /// </summary>
  253. /// <param name="argument">Call argument</param>
  254. private void CallMethod(int argument)
  255. {
  256. _context.Fifo.CallMethod(new MethodParams(
  257. _state.Method,
  258. argument,
  259. _state.SubChannel,
  260. _state.MethodCount));
  261. }
  262. }
  263. }