dma_pusher.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. while (system.IsPoweredOn()) {
  22. if (!Step()) {
  23. break;
  24. }
  25. }
  26. gpu.FlushCommands();
  27. gpu.SyncGuestHost();
  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. const CommandList& command_list{dma_pushbuffer.front()};
  36. ASSERT_OR_EXECUTE(!command_list.empty(), {
  37. // Somehow the command_list is empty, in order to avoid a crash
  38. // We ignore it and assume its size is 0.
  39. dma_pushbuffer.pop();
  40. dma_pushbuffer_subindex = 0;
  41. return true;
  42. });
  43. const CommandListHeader command_list_header{command_list[dma_pushbuffer_subindex++]};
  44. GPUVAddr dma_get = command_list_header.addr;
  45. GPUVAddr dma_put = dma_get + command_list_header.size * sizeof(u32);
  46. bool non_main = command_list_header.is_non_main;
  47. if (dma_pushbuffer_subindex >= command_list.size()) {
  48. // We've gone through the current list, remove it from the queue
  49. dma_pushbuffer.pop();
  50. dma_pushbuffer_subindex = 0;
  51. }
  52. if (command_list_header.size == 0) {
  53. return true;
  54. }
  55. // Push buffer non-empty, read a word
  56. command_headers.resize(command_list_header.size);
  57. gpu.MemoryManager().ReadBlockUnsafe(dma_get, command_headers.data(),
  58. command_list_header.size * sizeof(u32));
  59. for (std::size_t index = 0; index < command_headers.size();) {
  60. const CommandHeader& command_header = command_headers[index];
  61. if (dma_state.method_count) {
  62. // Data word of methods command
  63. if (dma_state.non_incrementing) {
  64. const u32 max_write = static_cast<u32>(
  65. std::min<std::size_t>(index + dma_state.method_count, command_headers.size()) -
  66. index);
  67. CallMultiMethod(&command_header.argument, max_write);
  68. dma_state.method_count -= max_write;
  69. index += max_write;
  70. continue;
  71. } else {
  72. CallMethod(command_header.argument);
  73. }
  74. if (!dma_state.non_incrementing) {
  75. dma_state.method++;
  76. }
  77. if (dma_increment_once) {
  78. dma_state.non_incrementing = true;
  79. }
  80. dma_state.method_count--;
  81. } else {
  82. // No command active - this is the first word of a new one
  83. switch (command_header.mode) {
  84. case SubmissionMode::Increasing:
  85. SetState(command_header);
  86. dma_state.non_incrementing = false;
  87. dma_increment_once = false;
  88. break;
  89. case SubmissionMode::NonIncreasing:
  90. SetState(command_header);
  91. dma_state.non_incrementing = true;
  92. dma_increment_once = false;
  93. break;
  94. case SubmissionMode::Inline:
  95. dma_state.method = command_header.method;
  96. dma_state.subchannel = command_header.subchannel;
  97. CallMethod(command_header.arg_count);
  98. dma_state.non_incrementing = true;
  99. dma_increment_once = false;
  100. break;
  101. case SubmissionMode::IncreaseOnce:
  102. SetState(command_header);
  103. dma_state.non_incrementing = false;
  104. dma_increment_once = true;
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. index++;
  111. }
  112. if (!non_main) {
  113. // TODO (degasus): This is dead code, as dma_mget is never read.
  114. dma_mget = dma_put;
  115. }
  116. return true;
  117. }
  118. void DmaPusher::SetState(const CommandHeader& command_header) {
  119. dma_state.method = command_header.method;
  120. dma_state.subchannel = command_header.subchannel;
  121. dma_state.method_count = command_header.method_count;
  122. }
  123. void DmaPusher::CallMethod(u32 argument) const {
  124. gpu.CallMethod({dma_state.method, argument, dma_state.subchannel, dma_state.method_count});
  125. }
  126. void DmaPusher::CallMultiMethod(const u32* base_start, u32 num_methods) const {
  127. gpu.CallMultiMethod(dma_state.method, dma_state.subchannel, base_start, num_methods,
  128. dma_state.method_count);
  129. }
  130. } // namespace Tegra