dma_pusher.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. const CommandList& command_list{dma_pushbuffer.front()};
  37. ASSERT_OR_EXECUTE(!command_list.empty(), {
  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. const CommandListHeader command_list_header{command_list[dma_pushbuffer_subindex++]};
  45. const GPUVAddr dma_get = command_list_header.addr;
  46. if (dma_pushbuffer_subindex >= command_list.size()) {
  47. // We've gone through the current list, remove it from the queue
  48. dma_pushbuffer.pop();
  49. dma_pushbuffer_subindex = 0;
  50. }
  51. if (command_list_header.size == 0) {
  52. return true;
  53. }
  54. // Push buffer non-empty, read a word
  55. command_headers.resize(command_list_header.size);
  56. gpu.MemoryManager().ReadBlockUnsafe(dma_get, command_headers.data(),
  57. command_list_header.size * sizeof(u32));
  58. for (std::size_t index = 0; index < command_headers.size();) {
  59. const CommandHeader& command_header = command_headers[index];
  60. if (dma_state.method_count) {
  61. // Data word of methods command
  62. if (dma_state.non_incrementing) {
  63. const u32 max_write = static_cast<u32>(
  64. std::min<std::size_t>(index + dma_state.method_count, command_headers.size()) -
  65. index);
  66. CallMultiMethod(&command_header.argument, max_write);
  67. dma_state.method_count -= max_write;
  68. dma_state.is_last_call = true;
  69. index += max_write;
  70. continue;
  71. } else {
  72. dma_state.is_last_call = dma_state.method_count <= 1;
  73. CallMethod(command_header.argument);
  74. }
  75. if (!dma_state.non_incrementing) {
  76. dma_state.method++;
  77. }
  78. if (dma_increment_once) {
  79. dma_state.non_incrementing = true;
  80. }
  81. dma_state.method_count--;
  82. } else {
  83. // No command active - this is the first word of a new one
  84. switch (command_header.mode) {
  85. case SubmissionMode::Increasing:
  86. SetState(command_header);
  87. dma_state.non_incrementing = false;
  88. dma_increment_once = false;
  89. break;
  90. case SubmissionMode::NonIncreasing:
  91. SetState(command_header);
  92. dma_state.non_incrementing = true;
  93. dma_increment_once = false;
  94. break;
  95. case SubmissionMode::Inline:
  96. dma_state.method = command_header.method;
  97. dma_state.subchannel = command_header.subchannel;
  98. CallMethod(command_header.arg_count);
  99. dma_state.non_incrementing = true;
  100. dma_increment_once = false;
  101. break;
  102. case SubmissionMode::IncreaseOnce:
  103. SetState(command_header);
  104. dma_state.non_incrementing = false;
  105. dma_increment_once = true;
  106. break;
  107. default:
  108. break;
  109. }
  110. }
  111. index++;
  112. }
  113. return true;
  114. }
  115. void DmaPusher::SetState(const CommandHeader& command_header) {
  116. dma_state.method = command_header.method;
  117. dma_state.subchannel = command_header.subchannel;
  118. dma_state.method_count = command_header.method_count;
  119. }
  120. void DmaPusher::CallMethod(u32 argument) const {
  121. if (dma_state.method < non_puller_methods) {
  122. gpu.CallMethod({dma_state.method, argument, dma_state.subchannel, dma_state.method_count});
  123. } else {
  124. subchannels[dma_state.subchannel]->CallMethod(dma_state.method, argument,
  125. dma_state.is_last_call);
  126. }
  127. }
  128. void DmaPusher::CallMultiMethod(const u32* base_start, u32 num_methods) const {
  129. if (dma_state.method < non_puller_methods) {
  130. gpu.CallMultiMethod(dma_state.method, dma_state.subchannel, base_start, num_methods,
  131. dma_state.method_count);
  132. } else {
  133. subchannels[dma_state.subchannel]->CallMultiMethod(dma_state.method, base_start,
  134. num_methods, dma_state.method_count);
  135. }
  136. }
  137. } // namespace Tegra