dma_pusher.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/cityhash.h"
  4. #include "common/microprofile.h"
  5. #include "common/settings.h"
  6. #include "core/core.h"
  7. #include "video_core/dma_pusher.h"
  8. #include "video_core/engines/maxwell_3d.h"
  9. #include "video_core/gpu.h"
  10. #include "video_core/memory_manager.h"
  11. namespace Tegra {
  12. DmaPusher::DmaPusher(Core::System& system_, GPU& gpu_, MemoryManager& memory_manager_,
  13. Control::ChannelState& channel_state_)
  14. : gpu{gpu_}, system{system_}, memory_manager{memory_manager_}, puller{gpu_, memory_manager_,
  15. *this, channel_state_} {}
  16. DmaPusher::~DmaPusher() = default;
  17. MICROPROFILE_DEFINE(DispatchCalls, "GPU", "Execute command buffer", MP_RGB(128, 128, 192));
  18. void DmaPusher::DispatchCalls() {
  19. MICROPROFILE_SCOPE(DispatchCalls);
  20. dma_pushbuffer_subindex = 0;
  21. dma_state.is_last_call = true;
  22. while (system.IsPoweredOn()) {
  23. if (!Step()) {
  24. break;
  25. }
  26. }
  27. gpu.FlushCommands();
  28. gpu.OnCommandListEnd();
  29. }
  30. bool DmaPusher::Step() {
  31. if (!ib_enable || dma_pushbuffer.empty()) {
  32. // pushbuffer empty and IB empty or nonexistent - nothing to do
  33. return false;
  34. }
  35. CommandList& command_list{dma_pushbuffer.front()};
  36. ASSERT_OR_EXECUTE(
  37. command_list.command_lists.size() || command_list.prefetch_command_list.size(), {
  38. // Somehow the command_list is empty, in order to avoid a crash
  39. // We ignore it and assume its size is 0.
  40. dma_pushbuffer.pop();
  41. dma_pushbuffer_subindex = 0;
  42. return true;
  43. });
  44. if (command_list.prefetch_command_list.size()) {
  45. // Prefetched command list from nvdrv, used for things like synchronization
  46. ProcessCommands(command_list.prefetch_command_list);
  47. dma_pushbuffer.pop();
  48. } else {
  49. const CommandListHeader command_list_header{
  50. command_list.command_lists[dma_pushbuffer_subindex++]};
  51. dma_state.dma_get = command_list_header.addr;
  52. if (dma_pushbuffer_subindex >= command_list.command_lists.size()) {
  53. // We've gone through the current list, remove it from the queue
  54. dma_pushbuffer.pop();
  55. dma_pushbuffer_subindex = 0;
  56. }
  57. if (command_list_header.size == 0) {
  58. return true;
  59. }
  60. // Push buffer non-empty, read a word
  61. command_headers.resize_destructive(command_list_header.size);
  62. constexpr static u32 MacroRegistersStart = 0xE00;
  63. if (dma_state.method < MacroRegistersStart) {
  64. if (Settings::IsGPULevelHigh()) {
  65. memory_manager.ReadBlock(dma_state.dma_get, command_headers.data(),
  66. command_list_header.size * sizeof(u32));
  67. } else {
  68. memory_manager.ReadBlockUnsafe(dma_state.dma_get, command_headers.data(),
  69. command_list_header.size * sizeof(u32));
  70. }
  71. } else {
  72. const size_t copy_size = command_list_header.size * sizeof(u32);
  73. if (subchannels[dma_state.subchannel]) {
  74. subchannels[dma_state.subchannel]->current_dirty =
  75. memory_manager.IsMemoryDirty(dma_state.dma_get, copy_size);
  76. }
  77. memory_manager.ReadBlockUnsafe(dma_state.dma_get, command_headers.data(), copy_size);
  78. }
  79. ProcessCommands(command_headers);
  80. }
  81. return true;
  82. }
  83. void DmaPusher::ProcessCommands(std::span<const CommandHeader> commands) {
  84. for (std::size_t index = 0; index < commands.size();) {
  85. const CommandHeader& command_header = commands[index];
  86. if (dma_state.method_count) {
  87. // Data word of methods command
  88. dma_state.dma_word_offset = static_cast<u32>(index * sizeof(u32));
  89. if (dma_state.non_incrementing) {
  90. const u32 max_write = static_cast<u32>(
  91. std::min<std::size_t>(index + dma_state.method_count, commands.size()) - index);
  92. CallMultiMethod(&command_header.argument, max_write);
  93. dma_state.method_count -= max_write;
  94. dma_state.is_last_call = true;
  95. index += max_write;
  96. continue;
  97. } else {
  98. dma_state.is_last_call = dma_state.method_count <= 1;
  99. CallMethod(command_header.argument);
  100. }
  101. if (!dma_state.non_incrementing) {
  102. dma_state.method++;
  103. }
  104. if (dma_increment_once) {
  105. dma_state.non_incrementing = true;
  106. }
  107. dma_state.method_count--;
  108. } else {
  109. // No command active - this is the first word of a new one
  110. switch (command_header.mode) {
  111. case SubmissionMode::Increasing:
  112. SetState(command_header);
  113. dma_state.non_incrementing = false;
  114. dma_increment_once = false;
  115. break;
  116. case SubmissionMode::NonIncreasing:
  117. SetState(command_header);
  118. dma_state.non_incrementing = true;
  119. dma_increment_once = false;
  120. break;
  121. case SubmissionMode::Inline:
  122. dma_state.method = command_header.method;
  123. dma_state.subchannel = command_header.subchannel;
  124. dma_state.dma_word_offset = static_cast<u64>(
  125. -static_cast<s64>(dma_state.dma_get)); // negate to set address as 0
  126. CallMethod(command_header.arg_count);
  127. dma_state.non_incrementing = true;
  128. dma_increment_once = false;
  129. break;
  130. case SubmissionMode::IncreaseOnce:
  131. SetState(command_header);
  132. dma_state.non_incrementing = false;
  133. dma_increment_once = true;
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. index++;
  140. }
  141. }
  142. void DmaPusher::SetState(const CommandHeader& command_header) {
  143. dma_state.method = command_header.method;
  144. dma_state.subchannel = command_header.subchannel;
  145. dma_state.method_count = command_header.method_count;
  146. }
  147. void DmaPusher::CallMethod(u32 argument) const {
  148. if (dma_state.method < non_puller_methods) {
  149. puller.CallPullerMethod(Engines::Puller::MethodCall{
  150. dma_state.method,
  151. argument,
  152. dma_state.subchannel,
  153. dma_state.method_count,
  154. });
  155. } else {
  156. auto subchannel = subchannels[dma_state.subchannel];
  157. if (!subchannel->execution_mask[dma_state.method]) [[likely]] {
  158. subchannel->method_sink.emplace_back(dma_state.method, argument);
  159. return;
  160. }
  161. subchannel->ConsumeSink();
  162. subchannel->current_dma_segment = dma_state.dma_get + dma_state.dma_word_offset;
  163. subchannel->CallMethod(dma_state.method, argument, dma_state.is_last_call);
  164. }
  165. }
  166. void DmaPusher::CallMultiMethod(const u32* base_start, u32 num_methods) const {
  167. if (dma_state.method < non_puller_methods) {
  168. puller.CallMultiMethod(dma_state.method, dma_state.subchannel, base_start, num_methods,
  169. dma_state.method_count);
  170. } else {
  171. auto subchannel = subchannels[dma_state.subchannel];
  172. subchannel->ConsumeSink();
  173. subchannel->current_dma_segment = dma_state.dma_get + dma_state.dma_word_offset;
  174. subchannel->CallMultiMethod(dma_state.method, base_start, num_methods,
  175. dma_state.method_count);
  176. }
  177. }
  178. void DmaPusher::BindRasterizer(VideoCore::RasterizerInterface* rasterizer) {
  179. puller.BindRasterizer(rasterizer);
  180. }
  181. } // namespace Tegra