DmaPusher.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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, 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 _sliEnable;
  73. private bool _sliActive;
  74. private bool _ibEnable;
  75. private GpuContext _context;
  76. private AutoResetEvent _event;
  77. /// <summary>
  78. /// Creates a new instance of the GPU DMA pusher.
  79. /// </summary>
  80. /// <param name="context">GPU context that the pusher belongs to</param>
  81. internal DmaPusher(GpuContext context)
  82. {
  83. _context = context;
  84. _ibEnable = true;
  85. _commandBufferQueue = new ConcurrentQueue<CommandBuffer>();
  86. _event = new AutoResetEvent(false);
  87. }
  88. /// <summary>
  89. /// Signal the pusher that there are new entries to process.
  90. /// </summary>
  91. public void SignalNewEntries()
  92. {
  93. _event.Set();
  94. }
  95. /// <summary>
  96. /// Push a GPFIFO entry in the form of a prefetched command buffer.
  97. /// It is intended to be used by nvservices to handle special cases.
  98. /// </summary>
  99. /// <param name="commandBuffer">The command buffer containing the prefetched commands</param>
  100. public void PushHostCommandBuffer(int[] commandBuffer)
  101. {
  102. _commandBufferQueue.Enqueue(new CommandBuffer
  103. {
  104. Type = CommandBufferType.Prefetch,
  105. Words = commandBuffer,
  106. EntryAddress = ulong.MaxValue,
  107. EntryCount = (uint)commandBuffer.Length
  108. });
  109. }
  110. /// <summary>
  111. /// Create a CommandBuffer from a GPFIFO entry.
  112. /// </summary>
  113. /// <param name="entry">The GPFIFO entry</param>
  114. /// <returns>A new CommandBuffer based on the GPFIFO entry</returns>
  115. private CommandBuffer CreateCommandBuffer(ulong entry)
  116. {
  117. ulong length = (entry >> 42) & 0x1fffff;
  118. ulong startAddress = entry & 0xfffffffffc;
  119. bool noPrefetch = (entry & (1UL << 63)) != 0;
  120. CommandBufferType type = CommandBufferType.Prefetch;
  121. if (noPrefetch)
  122. {
  123. type = CommandBufferType.NoPrefetch;
  124. }
  125. return new CommandBuffer
  126. {
  127. Type = type,
  128. Words = null,
  129. EntryAddress = startAddress,
  130. EntryCount = (uint)length
  131. };
  132. }
  133. /// <summary>
  134. /// Pushes GPFIFO entries.
  135. /// </summary>
  136. /// <param name="entries">GPFIFO entries</param>
  137. public void PushEntries(ReadOnlySpan<ulong> entries)
  138. {
  139. bool beforeBarrier = true;
  140. foreach (ulong entry in entries)
  141. {
  142. CommandBuffer commandBuffer = CreateCommandBuffer(entry);
  143. if (beforeBarrier && commandBuffer.Type == CommandBufferType.Prefetch)
  144. {
  145. commandBuffer.Fetch(_context);
  146. }
  147. if (commandBuffer.Type == CommandBufferType.NoPrefetch)
  148. {
  149. beforeBarrier = false;
  150. }
  151. _commandBufferQueue.Enqueue(commandBuffer);
  152. }
  153. }
  154. /// <summary>
  155. /// Waits until commands are pushed to the FIFO.
  156. /// </summary>
  157. /// <returns>True if commands were received, false if wait timed out</returns>
  158. public bool WaitForCommands()
  159. {
  160. return _event.WaitOne(8);
  161. }
  162. /// <summary>
  163. /// Processes commands pushed to the FIFO.
  164. /// </summary>
  165. public void DispatchCalls()
  166. {
  167. while (Step());
  168. }
  169. /// <summary>
  170. /// Processes a single command on the FIFO.
  171. /// </summary>
  172. /// <returns>True if the FIFO still has commands to be processed, false otherwise</returns>
  173. private bool Step()
  174. {
  175. if (_wordsPosition != _currentCommandBuffer.EntryCount)
  176. {
  177. int word = _currentCommandBuffer.ReadAt(_context, _wordsPosition++);
  178. if (_state.LengthPending != 0)
  179. {
  180. _state.LengthPending = 0;
  181. _state.MethodCount = word & 0xffffff;
  182. }
  183. else if (_state.MethodCount != 0)
  184. {
  185. if (!_sliEnable || _sliActive)
  186. {
  187. CallMethod(word);
  188. }
  189. if (!_state.NonIncrementing)
  190. {
  191. _state.Method++;
  192. }
  193. if (_state.IncrementOnce)
  194. {
  195. _state.NonIncrementing = true;
  196. }
  197. _state.MethodCount--;
  198. }
  199. else
  200. {
  201. int submissionMode = (word >> 29) & 7;
  202. switch (submissionMode)
  203. {
  204. case 1:
  205. // Incrementing.
  206. SetNonImmediateState(word);
  207. _state.NonIncrementing = false;
  208. _state.IncrementOnce = false;
  209. break;
  210. case 3:
  211. // Non-incrementing.
  212. SetNonImmediateState(word);
  213. _state.NonIncrementing = true;
  214. _state.IncrementOnce = false;
  215. break;
  216. case 4:
  217. // Immediate.
  218. _state.Method = (word >> 0) & 0x1fff;
  219. _state.SubChannel = (word >> 13) & 7;
  220. _state.NonIncrementing = true;
  221. _state.IncrementOnce = false;
  222. CallMethod((word >> 16) & 0x1fff);
  223. break;
  224. case 5:
  225. // Increment-once.
  226. SetNonImmediateState(word);
  227. _state.NonIncrementing = false;
  228. _state.IncrementOnce = true;
  229. break;
  230. }
  231. }
  232. }
  233. else if (_ibEnable && _commandBufferQueue.TryDequeue(out CommandBuffer entry))
  234. {
  235. _currentCommandBuffer = entry;
  236. _wordsPosition = 0;
  237. _currentCommandBuffer.Fetch(_context);
  238. }
  239. else
  240. {
  241. return false;
  242. }
  243. return true;
  244. }
  245. /// <summary>
  246. /// Sets current non-immediate method call state.
  247. /// </summary>
  248. /// <param name="word">Compressed method word</param>
  249. private void SetNonImmediateState(int word)
  250. {
  251. _state.Method = (word >> 0) & 0x1fff;
  252. _state.SubChannel = (word >> 13) & 7;
  253. _state.MethodCount = (word >> 16) & 0x1fff;
  254. }
  255. /// <summary>
  256. /// Forwards the method call to GPU engines.
  257. /// </summary>
  258. /// <param name="argument">Call argument</param>
  259. private void CallMethod(int argument)
  260. {
  261. _context.Fifo.CallMethod(new MethodParams(
  262. _state.Method,
  263. argument,
  264. _state.SubChannel,
  265. _state.MethodCount));
  266. }
  267. }
  268. }