dma_pusher.cpp 4.5 KB

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