cdma_pusher.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // MIT License
  2. //
  3. // Copyright (c) Ryujinx Team and Contributors
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  6. // associated documentation files (the "Software"), to deal in the Software without restriction,
  7. // including without limitation the rights to use, copy, modify, merge, publish, distribute,
  8. // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in all copies or
  12. // substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  15. // NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  17. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. #include "command_classes/host1x.h"
  21. #include "command_classes/nvdec.h"
  22. #include "command_classes/vic.h"
  23. #include "common/bit_util.h"
  24. #include "video_core/cdma_pusher.h"
  25. #include "video_core/command_classes/nvdec_common.h"
  26. #include "video_core/engines/maxwell_3d.h"
  27. #include "video_core/gpu.h"
  28. #include "video_core/memory_manager.h"
  29. namespace Tegra {
  30. CDmaPusher::CDmaPusher(GPU& gpu)
  31. : gpu(gpu), nvdec_processor(std::make_shared<Nvdec>(gpu)),
  32. vic_processor(std::make_unique<Vic>(gpu, nvdec_processor)),
  33. host1x_processor(std::make_unique<Host1x>(gpu)),
  34. nvdec_sync(std::make_unique<SyncptIncrManager>(gpu)),
  35. vic_sync(std::make_unique<SyncptIncrManager>(gpu)) {}
  36. CDmaPusher::~CDmaPusher() = default;
  37. void CDmaPusher::Push(ChCommandHeaderList&& entries) {
  38. cdma_queue.push(std::move(entries));
  39. }
  40. void CDmaPusher::DispatchCalls() {
  41. while (!cdma_queue.empty()) {
  42. Step();
  43. }
  44. }
  45. void CDmaPusher::Step() {
  46. const auto entries{cdma_queue.front()};
  47. cdma_queue.pop();
  48. std::vector<u32> values(entries.size());
  49. std::memcpy(values.data(), entries.data(), entries.size() * sizeof(u32));
  50. for (const u32 value : values) {
  51. if (mask != 0) {
  52. const u32 lbs = Common::CountTrailingZeroes32(mask);
  53. mask &= ~(1U << lbs);
  54. ExecuteCommand(static_cast<u32>(offset + lbs), value);
  55. continue;
  56. } else if (count != 0) {
  57. --count;
  58. ExecuteCommand(static_cast<u32>(offset), value);
  59. if (incrementing) {
  60. ++offset;
  61. }
  62. continue;
  63. }
  64. const auto mode = static_cast<ChSubmissionMode>((value >> 28) & 0xf);
  65. switch (mode) {
  66. case ChSubmissionMode::SetClass: {
  67. mask = value & 0x3f;
  68. offset = (value >> 16) & 0xfff;
  69. current_class = static_cast<ChClassId>((value >> 6) & 0x3ff);
  70. break;
  71. }
  72. case ChSubmissionMode::Incrementing:
  73. case ChSubmissionMode::NonIncrementing:
  74. count = value & 0xffff;
  75. offset = (value >> 16) & 0xfff;
  76. incrementing = mode == ChSubmissionMode::Incrementing;
  77. break;
  78. case ChSubmissionMode::Mask:
  79. mask = value & 0xffff;
  80. offset = (value >> 16) & 0xfff;
  81. break;
  82. case ChSubmissionMode::Immediate: {
  83. const u32 data = value & 0xfff;
  84. offset = (value >> 16) & 0xfff;
  85. ExecuteCommand(static_cast<u32>(offset), data);
  86. break;
  87. }
  88. default:
  89. UNIMPLEMENTED_MSG("ChSubmission mode {} is not implemented!", static_cast<u32>(mode));
  90. break;
  91. }
  92. }
  93. }
  94. void CDmaPusher::ExecuteCommand(u32 offset, u32 data) {
  95. switch (current_class) {
  96. case ChClassId::NvDec:
  97. ThiStateWrite(nvdec_thi_state, offset, {data});
  98. switch (static_cast<ThiMethod>(offset)) {
  99. case ThiMethod::IncSyncpt: {
  100. LOG_DEBUG(Service_NVDRV, "NVDEC Class IncSyncpt Method");
  101. const auto syncpoint_id = static_cast<u32>(data & 0xFF);
  102. const auto cond = static_cast<u32>((data >> 8) & 0xFF);
  103. if (cond == 0) {
  104. nvdec_sync->Increment(syncpoint_id);
  105. } else {
  106. nvdec_sync->IncrementWhenDone(static_cast<u32>(current_class), syncpoint_id);
  107. nvdec_sync->SignalDone(syncpoint_id);
  108. }
  109. break;
  110. }
  111. case ThiMethod::SetMethod1:
  112. LOG_DEBUG(Service_NVDRV, "NVDEC method 0x{:X}",
  113. static_cast<u32>(nvdec_thi_state.method_0));
  114. nvdec_processor->ProcessMethod(
  115. static_cast<Tegra::Nvdec::Method>(nvdec_thi_state.method_0), {data});
  116. break;
  117. default:
  118. break;
  119. }
  120. break;
  121. case ChClassId::GraphicsVic:
  122. ThiStateWrite(vic_thi_state, static_cast<u32>(offset), {data});
  123. switch (static_cast<ThiMethod>(offset)) {
  124. case ThiMethod::IncSyncpt: {
  125. LOG_DEBUG(Service_NVDRV, "VIC Class IncSyncpt Method");
  126. const auto syncpoint_id = static_cast<u32>(data & 0xFF);
  127. const auto cond = static_cast<u32>((data >> 8) & 0xFF);
  128. if (cond == 0) {
  129. vic_sync->Increment(syncpoint_id);
  130. } else {
  131. vic_sync->IncrementWhenDone(static_cast<u32>(current_class), syncpoint_id);
  132. vic_sync->SignalDone(syncpoint_id);
  133. }
  134. break;
  135. }
  136. case ThiMethod::SetMethod1:
  137. LOG_DEBUG(Service_NVDRV, "VIC method 0x{:X}, Args=({})",
  138. static_cast<u32>(vic_thi_state.method_0), data);
  139. vic_processor->ProcessMethod(static_cast<Tegra::Vic::Method>(vic_thi_state.method_0),
  140. {data});
  141. break;
  142. default:
  143. break;
  144. }
  145. break;
  146. case ChClassId::Host1x:
  147. // This device is mainly for syncpoint synchronization
  148. LOG_DEBUG(Service_NVDRV, "Host1X Class Method");
  149. host1x_processor->ProcessMethod(static_cast<Tegra::Host1x::Method>(offset), {data});
  150. break;
  151. default:
  152. UNIMPLEMENTED_MSG("Current class not implemented {:X}", static_cast<u32>(current_class));
  153. break;
  154. }
  155. }
  156. void CDmaPusher::ThiStateWrite(ThiRegisters& state, u32 offset, const std::vector<u32>& arguments) {
  157. u8* const state_offset = reinterpret_cast<u8*>(&state) + sizeof(u32) * offset;
  158. std::memcpy(state_offset, arguments.data(), sizeof(u32) * arguments.size());
  159. }
  160. } // namespace Tegra