gpu.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <chrono>
  5. #include "common/assert.h"
  6. #include "common/microprofile.h"
  7. #include "core/core.h"
  8. #include "core/core_timing.h"
  9. #include "core/core_timing_util.h"
  10. #include "core/frontend/emu_window.h"
  11. #include "core/memory.h"
  12. #include "core/settings.h"
  13. #include "video_core/engines/fermi_2d.h"
  14. #include "video_core/engines/kepler_compute.h"
  15. #include "video_core/engines/kepler_memory.h"
  16. #include "video_core/engines/maxwell_3d.h"
  17. #include "video_core/engines/maxwell_dma.h"
  18. #include "video_core/gpu.h"
  19. #include "video_core/memory_manager.h"
  20. #include "video_core/renderer_base.h"
  21. #include "video_core/shader_notify.h"
  22. #include "video_core/video_core.h"
  23. namespace Tegra {
  24. MICROPROFILE_DEFINE(GPU_wait, "GPU", "Wait for the GPU", MP_RGB(128, 128, 192));
  25. GPU::GPU(Core::System& system_, bool is_async_, bool use_nvdec_)
  26. : system{system_}, memory_manager{std::make_unique<Tegra::MemoryManager>(system)},
  27. dma_pusher{std::make_unique<Tegra::DmaPusher>(system, *this)},
  28. cdma_pusher{std::make_unique<Tegra::CDmaPusher>(*this)}, use_nvdec{use_nvdec_},
  29. maxwell_3d{std::make_unique<Engines::Maxwell3D>(system, *memory_manager)},
  30. fermi_2d{std::make_unique<Engines::Fermi2D>()},
  31. kepler_compute{std::make_unique<Engines::KeplerCompute>(system, *memory_manager)},
  32. maxwell_dma{std::make_unique<Engines::MaxwellDMA>(system, *memory_manager)},
  33. kepler_memory{std::make_unique<Engines::KeplerMemory>(system, *memory_manager)},
  34. shader_notify{std::make_unique<VideoCore::ShaderNotify>()}, is_async{is_async_} {}
  35. GPU::~GPU() = default;
  36. void GPU::BindRenderer(std::unique_ptr<VideoCore::RendererBase> renderer_) {
  37. renderer = std::move(renderer_);
  38. VideoCore::RasterizerInterface& rasterizer = renderer->Rasterizer();
  39. memory_manager->BindRasterizer(rasterizer);
  40. maxwell_3d->BindRasterizer(rasterizer);
  41. fermi_2d->BindRasterizer(rasterizer);
  42. kepler_compute->BindRasterizer(rasterizer);
  43. }
  44. Engines::Maxwell3D& GPU::Maxwell3D() {
  45. return *maxwell_3d;
  46. }
  47. const Engines::Maxwell3D& GPU::Maxwell3D() const {
  48. return *maxwell_3d;
  49. }
  50. Engines::KeplerCompute& GPU::KeplerCompute() {
  51. return *kepler_compute;
  52. }
  53. const Engines::KeplerCompute& GPU::KeplerCompute() const {
  54. return *kepler_compute;
  55. }
  56. MemoryManager& GPU::MemoryManager() {
  57. return *memory_manager;
  58. }
  59. const MemoryManager& GPU::MemoryManager() const {
  60. return *memory_manager;
  61. }
  62. DmaPusher& GPU::DmaPusher() {
  63. return *dma_pusher;
  64. }
  65. Tegra::CDmaPusher& GPU::CDmaPusher() {
  66. return *cdma_pusher;
  67. }
  68. const DmaPusher& GPU::DmaPusher() const {
  69. return *dma_pusher;
  70. }
  71. const Tegra::CDmaPusher& GPU::CDmaPusher() const {
  72. return *cdma_pusher;
  73. }
  74. void GPU::WaitFence(u32 syncpoint_id, u32 value) {
  75. // Synced GPU, is always in sync
  76. if (!is_async) {
  77. return;
  78. }
  79. if (syncpoint_id == UINT32_MAX) {
  80. // TODO: Research what this does.
  81. LOG_ERROR(HW_GPU, "Waiting for syncpoint -1 not implemented");
  82. return;
  83. }
  84. MICROPROFILE_SCOPE(GPU_wait);
  85. std::unique_lock lock{sync_mutex};
  86. sync_cv.wait(lock, [=, this] { return syncpoints.at(syncpoint_id).load() >= value; });
  87. }
  88. void GPU::IncrementSyncPoint(const u32 syncpoint_id) {
  89. auto& syncpoint = syncpoints.at(syncpoint_id);
  90. syncpoint++;
  91. std::lock_guard lock{sync_mutex};
  92. sync_cv.notify_all();
  93. auto& interrupt = syncpt_interrupts.at(syncpoint_id);
  94. if (!interrupt.empty()) {
  95. u32 value = syncpoint.load();
  96. auto it = interrupt.begin();
  97. while (it != interrupt.end()) {
  98. if (value >= *it) {
  99. TriggerCpuInterrupt(syncpoint_id, *it);
  100. it = interrupt.erase(it);
  101. continue;
  102. }
  103. it++;
  104. }
  105. }
  106. }
  107. u32 GPU::GetSyncpointValue(const u32 syncpoint_id) const {
  108. return syncpoints.at(syncpoint_id).load();
  109. }
  110. void GPU::RegisterSyncptInterrupt(const u32 syncpoint_id, const u32 value) {
  111. auto& interrupt = syncpt_interrupts.at(syncpoint_id);
  112. bool contains = std::any_of(interrupt.begin(), interrupt.end(),
  113. [value](u32 in_value) { return in_value == value; });
  114. if (contains) {
  115. return;
  116. }
  117. interrupt.emplace_back(value);
  118. }
  119. bool GPU::CancelSyncptInterrupt(const u32 syncpoint_id, const u32 value) {
  120. std::lock_guard lock{sync_mutex};
  121. auto& interrupt = syncpt_interrupts.at(syncpoint_id);
  122. const auto iter =
  123. std::find_if(interrupt.begin(), interrupt.end(),
  124. [value](u32 interrupt_value) { return value == interrupt_value; });
  125. if (iter == interrupt.end()) {
  126. return false;
  127. }
  128. interrupt.erase(iter);
  129. return true;
  130. }
  131. u64 GPU::RequestFlush(VAddr addr, std::size_t size) {
  132. std::unique_lock lck{flush_request_mutex};
  133. const u64 fence = ++last_flush_fence;
  134. flush_requests.emplace_back(fence, addr, size);
  135. return fence;
  136. }
  137. void GPU::TickWork() {
  138. std::unique_lock lck{flush_request_mutex};
  139. while (!flush_requests.empty()) {
  140. auto& request = flush_requests.front();
  141. const u64 fence = request.fence;
  142. const VAddr addr = request.addr;
  143. const std::size_t size = request.size;
  144. flush_requests.pop_front();
  145. flush_request_mutex.unlock();
  146. renderer->Rasterizer().FlushRegion(addr, size);
  147. current_flush_fence.store(fence);
  148. flush_request_mutex.lock();
  149. }
  150. }
  151. u64 GPU::GetTicks() const {
  152. // This values were reversed engineered by fincs from NVN
  153. // The gpu clock is reported in units of 385/625 nanoseconds
  154. constexpr u64 gpu_ticks_num = 384;
  155. constexpr u64 gpu_ticks_den = 625;
  156. u64 nanoseconds = system.CoreTiming().GetGlobalTimeNs().count();
  157. if (Settings::values.use_fast_gpu_time.GetValue()) {
  158. nanoseconds /= 256;
  159. }
  160. const u64 nanoseconds_num = nanoseconds / gpu_ticks_den;
  161. const u64 nanoseconds_rem = nanoseconds % gpu_ticks_den;
  162. return nanoseconds_num * gpu_ticks_num + (nanoseconds_rem * gpu_ticks_num) / gpu_ticks_den;
  163. }
  164. void GPU::FlushCommands() {
  165. renderer->Rasterizer().FlushCommands();
  166. }
  167. void GPU::SyncGuestHost() {
  168. renderer->Rasterizer().SyncGuestHost();
  169. }
  170. void GPU::OnCommandListEnd() {
  171. renderer->Rasterizer().ReleaseFences();
  172. }
  173. enum class GpuSemaphoreOperation {
  174. AcquireEqual = 0x1,
  175. WriteLong = 0x2,
  176. AcquireGequal = 0x4,
  177. AcquireMask = 0x8,
  178. };
  179. void GPU::CallMethod(const MethodCall& method_call) {
  180. LOG_TRACE(HW_GPU, "Processing method {:08X} on subchannel {}", method_call.method,
  181. method_call.subchannel);
  182. ASSERT(method_call.subchannel < bound_engines.size());
  183. if (ExecuteMethodOnEngine(method_call.method)) {
  184. CallEngineMethod(method_call);
  185. } else {
  186. CallPullerMethod(method_call);
  187. }
  188. }
  189. void GPU::CallMultiMethod(u32 method, u32 subchannel, const u32* base_start, u32 amount,
  190. u32 methods_pending) {
  191. LOG_TRACE(HW_GPU, "Processing method {:08X} on subchannel {}", method, subchannel);
  192. ASSERT(subchannel < bound_engines.size());
  193. if (ExecuteMethodOnEngine(method)) {
  194. CallEngineMultiMethod(method, subchannel, base_start, amount, methods_pending);
  195. } else {
  196. for (std::size_t i = 0; i < amount; i++) {
  197. CallPullerMethod(
  198. {method, base_start[i], subchannel, methods_pending - static_cast<u32>(i)});
  199. }
  200. }
  201. }
  202. bool GPU::ExecuteMethodOnEngine(u32 method) {
  203. const auto buffer_method = static_cast<BufferMethods>(method);
  204. return buffer_method >= BufferMethods::NonPullerMethods;
  205. }
  206. void GPU::CallPullerMethod(const MethodCall& method_call) {
  207. regs.reg_array[method_call.method] = method_call.argument;
  208. const auto method = static_cast<BufferMethods>(method_call.method);
  209. switch (method) {
  210. case BufferMethods::BindObject: {
  211. ProcessBindMethod(method_call);
  212. break;
  213. }
  214. case BufferMethods::Nop:
  215. case BufferMethods::SemaphoreAddressHigh:
  216. case BufferMethods::SemaphoreAddressLow:
  217. case BufferMethods::SemaphoreSequence:
  218. case BufferMethods::RefCnt:
  219. case BufferMethods::UnkCacheFlush:
  220. case BufferMethods::WrcacheFlush:
  221. case BufferMethods::FenceValue:
  222. break;
  223. case BufferMethods::FenceAction:
  224. ProcessFenceActionMethod();
  225. break;
  226. case BufferMethods::WaitForInterrupt:
  227. ProcessWaitForInterruptMethod();
  228. break;
  229. case BufferMethods::SemaphoreTrigger: {
  230. ProcessSemaphoreTriggerMethod();
  231. break;
  232. }
  233. case BufferMethods::NotifyIntr: {
  234. // TODO(Kmather73): Research and implement this method.
  235. LOG_ERROR(HW_GPU, "Special puller engine method NotifyIntr not implemented");
  236. break;
  237. }
  238. case BufferMethods::Unk28: {
  239. // TODO(Kmather73): Research and implement this method.
  240. LOG_ERROR(HW_GPU, "Special puller engine method Unk28 not implemented");
  241. break;
  242. }
  243. case BufferMethods::SemaphoreAcquire: {
  244. ProcessSemaphoreAcquire();
  245. break;
  246. }
  247. case BufferMethods::SemaphoreRelease: {
  248. ProcessSemaphoreRelease();
  249. break;
  250. }
  251. case BufferMethods::Yield: {
  252. // TODO(Kmather73): Research and implement this method.
  253. LOG_ERROR(HW_GPU, "Special puller engine method Yield not implemented");
  254. break;
  255. }
  256. default:
  257. LOG_ERROR(HW_GPU, "Special puller engine method {:X} not implemented",
  258. static_cast<u32>(method));
  259. break;
  260. }
  261. }
  262. void GPU::CallEngineMethod(const MethodCall& method_call) {
  263. const EngineID engine = bound_engines[method_call.subchannel];
  264. switch (engine) {
  265. case EngineID::FERMI_TWOD_A:
  266. fermi_2d->CallMethod(method_call.method, method_call.argument, method_call.IsLastCall());
  267. break;
  268. case EngineID::MAXWELL_B:
  269. maxwell_3d->CallMethod(method_call.method, method_call.argument, method_call.IsLastCall());
  270. break;
  271. case EngineID::KEPLER_COMPUTE_B:
  272. kepler_compute->CallMethod(method_call.method, method_call.argument,
  273. method_call.IsLastCall());
  274. break;
  275. case EngineID::MAXWELL_DMA_COPY_A:
  276. maxwell_dma->CallMethod(method_call.method, method_call.argument, method_call.IsLastCall());
  277. break;
  278. case EngineID::KEPLER_INLINE_TO_MEMORY_B:
  279. kepler_memory->CallMethod(method_call.method, method_call.argument,
  280. method_call.IsLastCall());
  281. break;
  282. default:
  283. UNIMPLEMENTED_MSG("Unimplemented engine");
  284. }
  285. }
  286. void GPU::CallEngineMultiMethod(u32 method, u32 subchannel, const u32* base_start, u32 amount,
  287. u32 methods_pending) {
  288. const EngineID engine = bound_engines[subchannel];
  289. switch (engine) {
  290. case EngineID::FERMI_TWOD_A:
  291. fermi_2d->CallMultiMethod(method, base_start, amount, methods_pending);
  292. break;
  293. case EngineID::MAXWELL_B:
  294. maxwell_3d->CallMultiMethod(method, base_start, amount, methods_pending);
  295. break;
  296. case EngineID::KEPLER_COMPUTE_B:
  297. kepler_compute->CallMultiMethod(method, base_start, amount, methods_pending);
  298. break;
  299. case EngineID::MAXWELL_DMA_COPY_A:
  300. maxwell_dma->CallMultiMethod(method, base_start, amount, methods_pending);
  301. break;
  302. case EngineID::KEPLER_INLINE_TO_MEMORY_B:
  303. kepler_memory->CallMultiMethod(method, base_start, amount, methods_pending);
  304. break;
  305. default:
  306. UNIMPLEMENTED_MSG("Unimplemented engine");
  307. }
  308. }
  309. void GPU::ProcessBindMethod(const MethodCall& method_call) {
  310. // Bind the current subchannel to the desired engine id.
  311. LOG_DEBUG(HW_GPU, "Binding subchannel {} to engine {}", method_call.subchannel,
  312. method_call.argument);
  313. const auto engine_id = static_cast<EngineID>(method_call.argument);
  314. bound_engines[method_call.subchannel] = static_cast<EngineID>(engine_id);
  315. switch (engine_id) {
  316. case EngineID::FERMI_TWOD_A:
  317. dma_pusher->BindSubchannel(fermi_2d.get(), method_call.subchannel);
  318. break;
  319. case EngineID::MAXWELL_B:
  320. dma_pusher->BindSubchannel(maxwell_3d.get(), method_call.subchannel);
  321. break;
  322. case EngineID::KEPLER_COMPUTE_B:
  323. dma_pusher->BindSubchannel(kepler_compute.get(), method_call.subchannel);
  324. break;
  325. case EngineID::MAXWELL_DMA_COPY_A:
  326. dma_pusher->BindSubchannel(maxwell_dma.get(), method_call.subchannel);
  327. break;
  328. case EngineID::KEPLER_INLINE_TO_MEMORY_B:
  329. dma_pusher->BindSubchannel(kepler_memory.get(), method_call.subchannel);
  330. break;
  331. default:
  332. UNIMPLEMENTED_MSG("Unimplemented engine {:04X}", static_cast<u32>(engine_id));
  333. }
  334. }
  335. void GPU::ProcessFenceActionMethod() {
  336. switch (regs.fence_action.op) {
  337. case FenceOperation::Acquire:
  338. WaitFence(regs.fence_action.syncpoint_id, regs.fence_value);
  339. break;
  340. case FenceOperation::Increment:
  341. IncrementSyncPoint(regs.fence_action.syncpoint_id);
  342. break;
  343. default:
  344. UNIMPLEMENTED_MSG("Unimplemented operation {}",
  345. static_cast<u32>(regs.fence_action.op.Value()));
  346. }
  347. }
  348. void GPU::ProcessWaitForInterruptMethod() {
  349. // TODO(bunnei) ImplementMe
  350. LOG_WARNING(HW_GPU, "(STUBBED) called");
  351. }
  352. void GPU::ProcessSemaphoreTriggerMethod() {
  353. const auto semaphoreOperationMask = 0xF;
  354. const auto op =
  355. static_cast<GpuSemaphoreOperation>(regs.semaphore_trigger & semaphoreOperationMask);
  356. if (op == GpuSemaphoreOperation::WriteLong) {
  357. struct Block {
  358. u32 sequence;
  359. u32 zeros = 0;
  360. u64 timestamp;
  361. };
  362. Block block{};
  363. block.sequence = regs.semaphore_sequence;
  364. // TODO(Kmather73): Generate a real GPU timestamp and write it here instead of
  365. // CoreTiming
  366. block.timestamp = GetTicks();
  367. memory_manager->WriteBlock(regs.semaphore_address.SemaphoreAddress(), &block,
  368. sizeof(block));
  369. } else {
  370. const u32 word{memory_manager->Read<u32>(regs.semaphore_address.SemaphoreAddress())};
  371. if ((op == GpuSemaphoreOperation::AcquireEqual && word == regs.semaphore_sequence) ||
  372. (op == GpuSemaphoreOperation::AcquireGequal &&
  373. static_cast<s32>(word - regs.semaphore_sequence) > 0) ||
  374. (op == GpuSemaphoreOperation::AcquireMask && (word & regs.semaphore_sequence))) {
  375. // Nothing to do in this case
  376. } else {
  377. regs.acquire_source = true;
  378. regs.acquire_value = regs.semaphore_sequence;
  379. if (op == GpuSemaphoreOperation::AcquireEqual) {
  380. regs.acquire_active = true;
  381. regs.acquire_mode = false;
  382. } else if (op == GpuSemaphoreOperation::AcquireGequal) {
  383. regs.acquire_active = true;
  384. regs.acquire_mode = true;
  385. } else if (op == GpuSemaphoreOperation::AcquireMask) {
  386. // TODO(kemathe) The acquire mask operation waits for a value that, ANDed with
  387. // semaphore_sequence, gives a non-0 result
  388. LOG_ERROR(HW_GPU, "Invalid semaphore operation AcquireMask not implemented");
  389. } else {
  390. LOG_ERROR(HW_GPU, "Invalid semaphore operation");
  391. }
  392. }
  393. }
  394. }
  395. void GPU::ProcessSemaphoreRelease() {
  396. memory_manager->Write<u32>(regs.semaphore_address.SemaphoreAddress(), regs.semaphore_release);
  397. }
  398. void GPU::ProcessSemaphoreAcquire() {
  399. const u32 word = memory_manager->Read<u32>(regs.semaphore_address.SemaphoreAddress());
  400. const auto value = regs.semaphore_acquire;
  401. if (word != value) {
  402. regs.acquire_active = true;
  403. regs.acquire_value = value;
  404. // TODO(kemathe73) figure out how to do the acquire_timeout
  405. regs.acquire_mode = false;
  406. regs.acquire_source = false;
  407. }
  408. }
  409. } // namespace Tegra