puller.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-FileCopyrightText: 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-3.0-or-later
  3. #include "common/assert.h"
  4. #include "common/logging/log.h"
  5. #include "common/settings.h"
  6. #include "core/core.h"
  7. #include "video_core/control/channel_state.h"
  8. #include "video_core/dma_pusher.h"
  9. #include "video_core/engines/fermi_2d.h"
  10. #include "video_core/engines/kepler_compute.h"
  11. #include "video_core/engines/kepler_memory.h"
  12. #include "video_core/engines/maxwell_3d.h"
  13. #include "video_core/engines/maxwell_dma.h"
  14. #include "video_core/engines/puller.h"
  15. #include "video_core/gpu.h"
  16. #include "video_core/memory_manager.h"
  17. #include "video_core/rasterizer_interface.h"
  18. namespace Tegra::Engines {
  19. Puller::Puller(GPU& gpu_, MemoryManager& memory_manager_, DmaPusher& dma_pusher_,
  20. Control::ChannelState& channel_state_)
  21. : gpu{gpu_}, memory_manager{memory_manager_}, dma_pusher{dma_pusher_}, channel_state{
  22. channel_state_} {}
  23. Puller::~Puller() = default;
  24. void Puller::ProcessBindMethod(const MethodCall& method_call) {
  25. // Bind the current subchannel to the desired engine id.
  26. LOG_DEBUG(HW_GPU, "Binding subchannel {} to engine {}", method_call.subchannel,
  27. method_call.argument);
  28. const auto engine_id = static_cast<EngineID>(method_call.argument);
  29. bound_engines[method_call.subchannel] = engine_id;
  30. switch (engine_id) {
  31. case EngineID::FERMI_TWOD_A:
  32. dma_pusher.BindSubchannel(channel_state.fermi_2d.get(), method_call.subchannel,
  33. EngineTypes::Fermi2D);
  34. break;
  35. case EngineID::MAXWELL_B:
  36. dma_pusher.BindSubchannel(channel_state.maxwell_3d.get(), method_call.subchannel,
  37. EngineTypes::Maxwell3D);
  38. break;
  39. case EngineID::KEPLER_COMPUTE_B:
  40. dma_pusher.BindSubchannel(channel_state.kepler_compute.get(), method_call.subchannel,
  41. EngineTypes::KeplerCompute);
  42. break;
  43. case EngineID::MAXWELL_DMA_COPY_A:
  44. dma_pusher.BindSubchannel(channel_state.maxwell_dma.get(), method_call.subchannel,
  45. EngineTypes::MaxwellDMA);
  46. break;
  47. case EngineID::KEPLER_INLINE_TO_MEMORY_B:
  48. dma_pusher.BindSubchannel(channel_state.kepler_memory.get(), method_call.subchannel,
  49. EngineTypes::KeplerMemory);
  50. break;
  51. default:
  52. UNIMPLEMENTED_MSG("Unimplemented engine {:04X}", engine_id);
  53. break;
  54. }
  55. }
  56. void Puller::ProcessFenceActionMethod() {
  57. switch (regs.fence_action.op) {
  58. case Puller::FenceOperation::Acquire:
  59. // UNIMPLEMENTED_MSG("Channel Scheduling pending.");
  60. // WaitFence(regs.fence_action.syncpoint_id, regs.fence_value);
  61. rasterizer->ReleaseFences();
  62. break;
  63. case Puller::FenceOperation::Increment:
  64. rasterizer->SignalSyncPoint(regs.fence_action.syncpoint_id);
  65. break;
  66. default:
  67. UNIMPLEMENTED_MSG("Unimplemented operation {}", regs.fence_action.op.Value());
  68. break;
  69. }
  70. }
  71. void Puller::ProcessSemaphoreTriggerMethod() {
  72. const auto semaphoreOperationMask = 0xF;
  73. const auto op =
  74. static_cast<GpuSemaphoreOperation>(regs.semaphore_trigger & semaphoreOperationMask);
  75. if (op == GpuSemaphoreOperation::WriteLong) {
  76. const GPUVAddr sequence_address{regs.semaphore_address.SemaphoreAddress()};
  77. const u32 payload = regs.semaphore_sequence;
  78. rasterizer->Query(sequence_address, VideoCommon::QueryType::Payload,
  79. VideoCommon::QueryPropertiesFlags::HasTimeout, payload, 0);
  80. } else {
  81. do {
  82. const u32 word{memory_manager.Read<u32>(regs.semaphore_address.SemaphoreAddress())};
  83. regs.acquire_source = true;
  84. regs.acquire_value = regs.semaphore_sequence;
  85. if (op == GpuSemaphoreOperation::AcquireEqual) {
  86. regs.acquire_active = true;
  87. regs.acquire_mode = false;
  88. if (word != regs.acquire_value) {
  89. rasterizer->ReleaseFences();
  90. continue;
  91. }
  92. } else if (op == GpuSemaphoreOperation::AcquireGequal) {
  93. regs.acquire_active = true;
  94. regs.acquire_mode = true;
  95. if (word < regs.acquire_value) {
  96. rasterizer->ReleaseFences();
  97. continue;
  98. }
  99. } else if (op == GpuSemaphoreOperation::AcquireMask) {
  100. if (word && regs.semaphore_sequence == 0) {
  101. rasterizer->ReleaseFences();
  102. continue;
  103. }
  104. } else {
  105. LOG_ERROR(HW_GPU, "Invalid semaphore operation");
  106. }
  107. } while (false);
  108. }
  109. }
  110. void Puller::ProcessSemaphoreRelease() {
  111. const GPUVAddr sequence_address{regs.semaphore_address.SemaphoreAddress()};
  112. const u32 payload = regs.semaphore_release;
  113. rasterizer->Query(sequence_address, VideoCommon::QueryType::Payload,
  114. VideoCommon::QueryPropertiesFlags::IsAFence, payload, 0);
  115. }
  116. void Puller::ProcessSemaphoreAcquire() {
  117. u32 word = memory_manager.Read<u32>(regs.semaphore_address.SemaphoreAddress());
  118. const auto value = regs.semaphore_acquire;
  119. while (word != value) {
  120. regs.acquire_active = true;
  121. regs.acquire_value = value;
  122. rasterizer->ReleaseFences();
  123. word = memory_manager.Read<u32>(regs.semaphore_address.SemaphoreAddress());
  124. // TODO(kemathe73) figure out how to do the acquire_timeout
  125. regs.acquire_mode = false;
  126. regs.acquire_source = false;
  127. }
  128. }
  129. /// Calls a GPU puller method.
  130. void Puller::CallPullerMethod(const MethodCall& method_call) {
  131. regs.reg_array[method_call.method] = method_call.argument;
  132. const auto method = static_cast<BufferMethods>(method_call.method);
  133. switch (method) {
  134. case BufferMethods::BindObject: {
  135. ProcessBindMethod(method_call);
  136. break;
  137. }
  138. case BufferMethods::Nop:
  139. case BufferMethods::SemaphoreAddressHigh:
  140. case BufferMethods::SemaphoreAddressLow:
  141. case BufferMethods::SemaphoreSequencePayload:
  142. case BufferMethods::SyncpointPayload:
  143. case BufferMethods::WrcacheFlush:
  144. break;
  145. case BufferMethods::RefCnt:
  146. rasterizer->SignalReference();
  147. break;
  148. case BufferMethods::SyncpointOperation:
  149. ProcessFenceActionMethod();
  150. break;
  151. case BufferMethods::WaitForIdle:
  152. rasterizer->WaitForIdle();
  153. break;
  154. case BufferMethods::SemaphoreOperation: {
  155. ProcessSemaphoreTriggerMethod();
  156. break;
  157. }
  158. case BufferMethods::NonStallInterrupt: {
  159. LOG_ERROR(HW_GPU, "Special puller engine method NonStallInterrupt not implemented");
  160. break;
  161. }
  162. case BufferMethods::MemOpA: {
  163. LOG_ERROR(HW_GPU, "Memory Operation A");
  164. break;
  165. }
  166. case BufferMethods::MemOpB: {
  167. // Implement this better.
  168. rasterizer->InvalidateGPUCache();
  169. break;
  170. }
  171. case BufferMethods::MemOpC:
  172. case BufferMethods::MemOpD: {
  173. LOG_ERROR(HW_GPU, "Memory Operation C,D");
  174. break;
  175. }
  176. case BufferMethods::SemaphoreAcquire: {
  177. ProcessSemaphoreAcquire();
  178. break;
  179. }
  180. case BufferMethods::SemaphoreRelease: {
  181. ProcessSemaphoreRelease();
  182. break;
  183. }
  184. case BufferMethods::Yield: {
  185. // TODO(Kmather73): Research and implement this method.
  186. LOG_ERROR(HW_GPU, "Special puller engine method Yield not implemented");
  187. break;
  188. }
  189. default:
  190. LOG_ERROR(HW_GPU, "Special puller engine method {:X} not implemented", method);
  191. break;
  192. }
  193. }
  194. /// Calls a GPU engine method.
  195. void Puller::CallEngineMethod(const MethodCall& method_call) {
  196. const EngineID engine = bound_engines[method_call.subchannel];
  197. switch (engine) {
  198. case EngineID::FERMI_TWOD_A:
  199. channel_state.fermi_2d->CallMethod(method_call.method, method_call.argument,
  200. method_call.IsLastCall());
  201. break;
  202. case EngineID::MAXWELL_B:
  203. channel_state.maxwell_3d->CallMethod(method_call.method, method_call.argument,
  204. method_call.IsLastCall());
  205. break;
  206. case EngineID::KEPLER_COMPUTE_B:
  207. channel_state.kepler_compute->CallMethod(method_call.method, method_call.argument,
  208. method_call.IsLastCall());
  209. break;
  210. case EngineID::MAXWELL_DMA_COPY_A:
  211. channel_state.maxwell_dma->CallMethod(method_call.method, method_call.argument,
  212. method_call.IsLastCall());
  213. break;
  214. case EngineID::KEPLER_INLINE_TO_MEMORY_B:
  215. channel_state.kepler_memory->CallMethod(method_call.method, method_call.argument,
  216. method_call.IsLastCall());
  217. break;
  218. default:
  219. UNIMPLEMENTED_MSG("Unimplemented engine");
  220. break;
  221. }
  222. }
  223. /// Calls a GPU engine multivalue method.
  224. void Puller::CallEngineMultiMethod(u32 method, u32 subchannel, const u32* base_start, u32 amount,
  225. u32 methods_pending) {
  226. const EngineID engine = bound_engines[subchannel];
  227. switch (engine) {
  228. case EngineID::FERMI_TWOD_A:
  229. channel_state.fermi_2d->CallMultiMethod(method, base_start, amount, methods_pending);
  230. break;
  231. case EngineID::MAXWELL_B:
  232. channel_state.maxwell_3d->CallMultiMethod(method, base_start, amount, methods_pending);
  233. break;
  234. case EngineID::KEPLER_COMPUTE_B:
  235. channel_state.kepler_compute->CallMultiMethod(method, base_start, amount, methods_pending);
  236. break;
  237. case EngineID::MAXWELL_DMA_COPY_A:
  238. channel_state.maxwell_dma->CallMultiMethod(method, base_start, amount, methods_pending);
  239. break;
  240. case EngineID::KEPLER_INLINE_TO_MEMORY_B:
  241. channel_state.kepler_memory->CallMultiMethod(method, base_start, amount, methods_pending);
  242. break;
  243. default:
  244. UNIMPLEMENTED_MSG("Unimplemented engine");
  245. break;
  246. }
  247. }
  248. /// Calls a GPU method.
  249. void Puller::CallMethod(const MethodCall& method_call) {
  250. LOG_TRACE(HW_GPU, "Processing method {:08X} on subchannel {}", method_call.method,
  251. method_call.subchannel);
  252. ASSERT(method_call.subchannel < bound_engines.size());
  253. if (ExecuteMethodOnEngine(method_call.method)) {
  254. CallEngineMethod(method_call);
  255. } else {
  256. CallPullerMethod(method_call);
  257. }
  258. }
  259. /// Calls a GPU multivalue method.
  260. void Puller::CallMultiMethod(u32 method, u32 subchannel, const u32* base_start, u32 amount,
  261. u32 methods_pending) {
  262. LOG_TRACE(HW_GPU, "Processing method {:08X} on subchannel {}", method, subchannel);
  263. ASSERT(subchannel < bound_engines.size());
  264. if (ExecuteMethodOnEngine(method)) {
  265. CallEngineMultiMethod(method, subchannel, base_start, amount, methods_pending);
  266. } else {
  267. for (u32 i = 0; i < amount; i++) {
  268. CallPullerMethod(MethodCall{
  269. method,
  270. base_start[i],
  271. subchannel,
  272. methods_pending - i,
  273. });
  274. }
  275. }
  276. }
  277. void Puller::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) {
  278. rasterizer = rasterizer_;
  279. }
  280. /// Determines where the method should be executed.
  281. [[nodiscard]] bool Puller::ExecuteMethodOnEngine(u32 method) {
  282. const auto buffer_method = static_cast<BufferMethods>(method);
  283. return buffer_method >= BufferMethods::NonPullerMethods;
  284. }
  285. } // namespace Tegra::Engines