dma_pusher.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/microprofile.h"
  5. #include "core/core.h"
  6. #include "core/memory.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) : gpu{gpu}, system{system} {}
  13. DmaPusher::~DmaPusher() = default;
  14. MICROPROFILE_DEFINE(DispatchCalls, "GPU", "Execute command buffer", MP_RGB(128, 128, 192));
  15. void DmaPusher::DispatchCalls() {
  16. MICROPROFILE_SCOPE(DispatchCalls);
  17. gpu.SyncGuestHost();
  18. // On entering GPU code, assume all memory may be touched by the ARM core.
  19. gpu.Maxwell3D().OnMemoryWrite();
  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 u64 next_hash = command_list.command_list_hashes[dma_pushbuffer_subindex++];
  53. const GPUVAddr 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. command_headers.resize(command_list_header.size);
  64. gpu.MemoryManager().ReadBlockUnsafe(dma_get, command_headers.data(),
  65. command_list_header.size * sizeof(u32));
  66. }
  67. for (std::size_t index = 0; index < command_headers.size();) {
  68. const CommandHeader& command_header = command_headers[index];
  69. if (dma_state.method_count) {
  70. // Data word of methods command
  71. if (dma_state.non_incrementing) {
  72. const u32 max_write = static_cast<u32>(
  73. std::min<std::size_t>(index + dma_state.method_count, command_headers.size()) -
  74. index);
  75. CallMultiMethod(&command_header.argument, max_write);
  76. dma_state.method_count -= max_write;
  77. dma_state.is_last_call = true;
  78. index += max_write;
  79. continue;
  80. } else {
  81. dma_state.is_last_call = dma_state.method_count <= 1;
  82. CallMethod(command_header.argument);
  83. }
  84. if (!dma_state.non_incrementing) {
  85. dma_state.method++;
  86. }
  87. if (dma_increment_once) {
  88. dma_state.non_incrementing = true;
  89. }
  90. dma_state.method_count--;
  91. } else {
  92. // No command active - this is the first word of a new one
  93. switch (command_header.mode) {
  94. case SubmissionMode::Increasing:
  95. SetState(command_header);
  96. dma_state.non_incrementing = false;
  97. dma_increment_once = false;
  98. break;
  99. case SubmissionMode::NonIncreasing:
  100. SetState(command_header);
  101. dma_state.non_incrementing = true;
  102. dma_increment_once = false;
  103. break;
  104. case SubmissionMode::Inline:
  105. dma_state.method = command_header.method;
  106. dma_state.subchannel = command_header.subchannel;
  107. CallMethod(command_header.arg_count);
  108. dma_state.non_incrementing = true;
  109. dma_increment_once = false;
  110. break;
  111. case SubmissionMode::IncreaseOnce:
  112. SetState(command_header);
  113. dma_state.non_incrementing = false;
  114. dma_increment_once = true;
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. index++;
  121. }
  122. return true;
  123. }
  124. void DmaPusher::SetState(const CommandHeader& command_header) {
  125. dma_state.method = command_header.method;
  126. dma_state.subchannel = command_header.subchannel;
  127. dma_state.method_count = command_header.method_count;
  128. }
  129. void DmaPusher::CallMethod(u32 argument) const {
  130. if (dma_state.method < non_puller_methods) {
  131. gpu.CallMethod({dma_state.method, argument, dma_state.subchannel, dma_state.method_count});
  132. } else {
  133. subchannels[dma_state.subchannel]->CallMethod(dma_state.method, argument,
  134. dma_state.is_last_call);
  135. }
  136. }
  137. void DmaPusher::CallMultiMethod(const u32* base_start, u32 num_methods) const {
  138. if (dma_state.method < non_puller_methods) {
  139. gpu.CallMultiMethod(dma_state.method, dma_state.subchannel, base_start, num_methods,
  140. dma_state.method_count);
  141. } else {
  142. subchannels[dma_state.subchannel]->CallMultiMethod(dma_state.method, base_start,
  143. num_methods, dma_state.method_count);
  144. }
  145. }
  146. } // namespace Tegra