dma_pusher.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. command_headers = std::move(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. const GPUVAddr 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(command_list_header.size);
  62. if (Settings::IsGPULevelHigh()) {
  63. memory_manager.ReadBlock(dma_get, command_headers.data(),
  64. command_list_header.size * sizeof(u32));
  65. } else {
  66. memory_manager.ReadBlockUnsafe(dma_get, command_headers.data(),
  67. command_list_header.size * sizeof(u32));
  68. }
  69. }
  70. for (std::size_t index = 0; index < command_headers.size();) {
  71. const CommandHeader& command_header = command_headers[index];
  72. if (dma_state.method_count) {
  73. // Data word of methods command
  74. if (dma_state.non_incrementing) {
  75. const u32 max_write = static_cast<u32>(
  76. std::min<std::size_t>(index + dma_state.method_count, command_headers.size()) -
  77. index);
  78. CallMultiMethod(&command_header.argument, max_write);
  79. dma_state.method_count -= max_write;
  80. dma_state.is_last_call = true;
  81. index += max_write;
  82. continue;
  83. } else {
  84. dma_state.is_last_call = dma_state.method_count <= 1;
  85. CallMethod(command_header.argument);
  86. }
  87. if (!dma_state.non_incrementing) {
  88. dma_state.method++;
  89. }
  90. if (dma_increment_once) {
  91. dma_state.non_incrementing = true;
  92. }
  93. dma_state.method_count--;
  94. } else {
  95. // No command active - this is the first word of a new one
  96. switch (command_header.mode) {
  97. case SubmissionMode::Increasing:
  98. SetState(command_header);
  99. dma_state.non_incrementing = false;
  100. dma_increment_once = false;
  101. break;
  102. case SubmissionMode::NonIncreasing:
  103. SetState(command_header);
  104. dma_state.non_incrementing = true;
  105. dma_increment_once = false;
  106. break;
  107. case SubmissionMode::Inline:
  108. dma_state.method = command_header.method;
  109. dma_state.subchannel = command_header.subchannel;
  110. CallMethod(command_header.arg_count);
  111. dma_state.non_incrementing = true;
  112. dma_increment_once = false;
  113. break;
  114. case SubmissionMode::IncreaseOnce:
  115. SetState(command_header);
  116. dma_state.non_incrementing = false;
  117. dma_increment_once = true;
  118. break;
  119. default:
  120. break;
  121. }
  122. }
  123. index++;
  124. }
  125. return true;
  126. }
  127. void DmaPusher::SetState(const CommandHeader& command_header) {
  128. dma_state.method = command_header.method;
  129. dma_state.subchannel = command_header.subchannel;
  130. dma_state.method_count = command_header.method_count;
  131. }
  132. void DmaPusher::CallMethod(u32 argument) const {
  133. if (dma_state.method < non_puller_methods) {
  134. puller.CallPullerMethod(Engines::Puller::MethodCall{
  135. dma_state.method,
  136. argument,
  137. dma_state.subchannel,
  138. dma_state.method_count,
  139. });
  140. } else {
  141. subchannels[dma_state.subchannel]->CallMethod(dma_state.method, argument,
  142. dma_state.is_last_call);
  143. }
  144. }
  145. void DmaPusher::CallMultiMethod(const u32* base_start, u32 num_methods) const {
  146. if (dma_state.method < non_puller_methods) {
  147. puller.CallMultiMethod(dma_state.method, dma_state.subchannel, base_start, num_methods,
  148. dma_state.method_count);
  149. } else {
  150. subchannels[dma_state.subchannel]->CallMultiMethod(dma_state.method, base_start,
  151. num_methods, dma_state.method_count);
  152. }
  153. }
  154. void DmaPusher::BindRasterizer(VideoCore::RasterizerInterface* rasterizer) {
  155. puller.BindRasterizer(rasterizer);
  156. }
  157. } // namespace Tegra