dma_pusher.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/cityhash.h"
  5. #include "common/microprofile.h"
  6. #include "common/settings.h"
  7. #include "core/core.h"
  8. #include "core/memory.h"
  9. #include "video_core/dma_pusher.h"
  10. #include "video_core/engines/maxwell_3d.h"
  11. #include "video_core/gpu.h"
  12. #include "video_core/memory_manager.h"
  13. namespace Tegra {
  14. DmaPusher::DmaPusher(Core::System& system_, GPU& gpu_) : gpu{gpu_}, system{system_} {}
  15. DmaPusher::~DmaPusher() = default;
  16. MICROPROFILE_DEFINE(DispatchCalls, "GPU", "Execute command buffer", MP_RGB(128, 128, 192));
  17. void DmaPusher::DispatchCalls() {
  18. MICROPROFILE_SCOPE(DispatchCalls);
  19. gpu.SyncGuestHost();
  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.SyncGuestHost();
  29. gpu.OnCommandListEnd();
  30. }
  31. bool DmaPusher::Step() {
  32. if (!ib_enable || dma_pushbuffer.empty()) {
  33. // pushbuffer empty and IB empty or nonexistent - nothing to do
  34. return false;
  35. }
  36. CommandList& command_list{dma_pushbuffer.front()};
  37. ASSERT_OR_EXECUTE(
  38. command_list.command_lists.size() || command_list.prefetch_command_list.size(), {
  39. // Somehow the command_list is empty, in order to avoid a crash
  40. // We ignore it and assume its size is 0.
  41. dma_pushbuffer.pop();
  42. dma_pushbuffer_subindex = 0;
  43. return true;
  44. });
  45. if (command_list.prefetch_command_list.size()) {
  46. // Prefetched command list from nvdrv, used for things like synchronization
  47. command_headers = std::move(command_list.prefetch_command_list);
  48. dma_pushbuffer.pop();
  49. } else {
  50. const CommandListHeader command_list_header{
  51. command_list.command_lists[dma_pushbuffer_subindex++]};
  52. const GPUVAddr dma_get = command_list_header.addr;
  53. if (dma_pushbuffer_subindex >= command_list.command_lists.size()) {
  54. // We've gone through the current list, remove it from the queue
  55. dma_pushbuffer.pop();
  56. dma_pushbuffer_subindex = 0;
  57. }
  58. if (command_list_header.size == 0) {
  59. return true;
  60. }
  61. // Push buffer non-empty, read a word
  62. command_headers.resize(command_list_header.size);
  63. if (Settings::IsGPULevelHigh()) {
  64. gpu.MemoryManager().ReadBlock(dma_get, command_headers.data(),
  65. command_list_header.size * sizeof(u32));
  66. } else {
  67. gpu.MemoryManager().ReadBlockUnsafe(dma_get, command_headers.data(),
  68. command_list_header.size * sizeof(u32));
  69. }
  70. }
  71. for (std::size_t index = 0; index < command_headers.size();) {
  72. const CommandHeader& command_header = command_headers[index];
  73. if (dma_state.method_count) {
  74. // Data word of methods command
  75. if (dma_state.non_incrementing) {
  76. const u32 max_write = static_cast<u32>(
  77. std::min<std::size_t>(index + dma_state.method_count, command_headers.size()) -
  78. index);
  79. CallMultiMethod(&command_header.argument, max_write);
  80. dma_state.method_count -= max_write;
  81. dma_state.is_last_call = true;
  82. index += max_write;
  83. continue;
  84. } else {
  85. dma_state.is_last_call = dma_state.method_count <= 1;
  86. CallMethod(command_header.argument);
  87. }
  88. if (!dma_state.non_incrementing) {
  89. dma_state.method++;
  90. }
  91. if (dma_increment_once) {
  92. dma_state.non_incrementing = true;
  93. }
  94. dma_state.method_count--;
  95. } else {
  96. // No command active - this is the first word of a new one
  97. switch (command_header.mode) {
  98. case SubmissionMode::Increasing:
  99. SetState(command_header);
  100. dma_state.non_incrementing = false;
  101. dma_increment_once = false;
  102. break;
  103. case SubmissionMode::NonIncreasing:
  104. SetState(command_header);
  105. dma_state.non_incrementing = true;
  106. dma_increment_once = false;
  107. break;
  108. case SubmissionMode::Inline:
  109. dma_state.method = command_header.method;
  110. dma_state.subchannel = command_header.subchannel;
  111. CallMethod(command_header.arg_count);
  112. dma_state.non_incrementing = true;
  113. dma_increment_once = false;
  114. break;
  115. case SubmissionMode::IncreaseOnce:
  116. SetState(command_header);
  117. dma_state.non_incrementing = false;
  118. dma_increment_once = true;
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. index++;
  125. }
  126. return true;
  127. }
  128. void DmaPusher::SetState(const CommandHeader& command_header) {
  129. dma_state.method = command_header.method;
  130. dma_state.subchannel = command_header.subchannel;
  131. dma_state.method_count = command_header.method_count;
  132. }
  133. void DmaPusher::CallMethod(u32 argument) const {
  134. if (dma_state.method < non_puller_methods) {
  135. gpu.CallMethod(GPU::MethodCall{
  136. dma_state.method,
  137. argument,
  138. dma_state.subchannel,
  139. dma_state.method_count,
  140. });
  141. } else {
  142. subchannels[dma_state.subchannel]->CallMethod(dma_state.method, argument,
  143. dma_state.is_last_call);
  144. }
  145. }
  146. void DmaPusher::CallMultiMethod(const u32* base_start, u32 num_methods) const {
  147. if (dma_state.method < non_puller_methods) {
  148. gpu.CallMultiMethod(dma_state.method, dma_state.subchannel, base_start, num_methods,
  149. dma_state.method_count);
  150. } else {
  151. subchannels[dma_state.subchannel]->CallMultiMethod(dma_state.method, base_start,
  152. num_methods, dma_state.method_count);
  153. }
  154. }
  155. } // namespace Tegra