dma_pusher.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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(GPU& gpu) : gpu(gpu) {}
  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. // On entering GPU code, assume all memory may be touched by the ARM core.
  18. gpu.Maxwell3D().OnMemoryWrite();
  19. dma_pushbuffer_subindex = 0;
  20. while (Core::System::GetInstance().IsPoweredOn()) {
  21. if (!Step()) {
  22. break;
  23. }
  24. }
  25. gpu.FlushCommands();
  26. }
  27. bool DmaPusher::Step() {
  28. if (!ib_enable || dma_pushbuffer.empty()) {
  29. // pushbuffer empty and IB empty or nonexistent - nothing to do
  30. return false;
  31. }
  32. const CommandList& command_list{dma_pushbuffer.front()};
  33. ASSERT_OR_EXECUTE(!command_list.empty(), {
  34. // Somehow the command_list is empty, in order to avoid a crash
  35. // We ignore it and assume its size is 0.
  36. dma_pushbuffer.pop();
  37. dma_pushbuffer_subindex = 0;
  38. return true;
  39. });
  40. const CommandListHeader command_list_header{command_list[dma_pushbuffer_subindex++]};
  41. GPUVAddr dma_get = command_list_header.addr;
  42. GPUVAddr dma_put = dma_get + command_list_header.size * sizeof(u32);
  43. bool non_main = command_list_header.is_non_main;
  44. if (dma_pushbuffer_subindex >= command_list.size()) {
  45. // We've gone through the current list, remove it from the queue
  46. dma_pushbuffer.pop();
  47. dma_pushbuffer_subindex = 0;
  48. }
  49. if (command_list_header.size == 0) {
  50. return true;
  51. }
  52. // Push buffer non-empty, read a word
  53. command_headers.resize(command_list_header.size);
  54. gpu.MemoryManager().ReadBlockUnsafe(dma_get, command_headers.data(),
  55. command_list_header.size * sizeof(u32));
  56. for (const CommandHeader& command_header : command_headers) {
  57. // now, see if we're in the middle of a command
  58. if (dma_state.length_pending) {
  59. // Second word of long non-inc methods command - method count
  60. dma_state.length_pending = 0;
  61. dma_state.method_count = command_header.method_count_;
  62. } else if (dma_state.method_count) {
  63. // Data word of methods command
  64. CallMethod(command_header.argument);
  65. if (!dma_state.non_incrementing) {
  66. dma_state.method++;
  67. }
  68. if (dma_increment_once) {
  69. dma_state.non_incrementing = true;
  70. }
  71. dma_state.method_count--;
  72. } else {
  73. // No command active - this is the first word of a new one
  74. switch (command_header.mode) {
  75. case SubmissionMode::Increasing:
  76. SetState(command_header);
  77. dma_state.non_incrementing = false;
  78. dma_increment_once = false;
  79. break;
  80. case SubmissionMode::NonIncreasing:
  81. SetState(command_header);
  82. dma_state.non_incrementing = true;
  83. dma_increment_once = false;
  84. break;
  85. case SubmissionMode::Inline:
  86. dma_state.method = command_header.method;
  87. dma_state.subchannel = command_header.subchannel;
  88. CallMethod(command_header.arg_count);
  89. dma_state.non_incrementing = true;
  90. dma_increment_once = false;
  91. break;
  92. case SubmissionMode::IncreaseOnce:
  93. SetState(command_header);
  94. dma_state.non_incrementing = false;
  95. dma_increment_once = true;
  96. break;
  97. default:
  98. break;
  99. }
  100. }
  101. }
  102. if (!non_main) {
  103. // TODO (degasus): This is dead code, as dma_mget is never read.
  104. dma_mget = dma_put;
  105. }
  106. return true;
  107. }
  108. void DmaPusher::SetState(const CommandHeader& command_header) {
  109. dma_state.method = command_header.method;
  110. dma_state.subchannel = command_header.subchannel;
  111. dma_state.method_count = command_header.method_count;
  112. }
  113. void DmaPusher::CallMethod(u32 argument) const {
  114. gpu.CallMethod({dma_state.method, argument, dma_state.subchannel, dma_state.method_count});
  115. }
  116. } // namespace Tegra