dma_pusher.cpp 7.3 KB

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