gpu.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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/assert.h"
  5. #include "common/microprofile.h"
  6. #include "core/core.h"
  7. #include "core/core_timing.h"
  8. #include "core/core_timing_util.h"
  9. #include "core/frontend/emu_window.h"
  10. #include "core/memory.h"
  11. #include "video_core/engines/fermi_2d.h"
  12. #include "video_core/engines/kepler_compute.h"
  13. #include "video_core/engines/kepler_memory.h"
  14. #include "video_core/engines/maxwell_3d.h"
  15. #include "video_core/engines/maxwell_dma.h"
  16. #include "video_core/gpu.h"
  17. #include "video_core/memory_manager.h"
  18. #include "video_core/renderer_base.h"
  19. #include "video_core/video_core.h"
  20. namespace Tegra {
  21. MICROPROFILE_DEFINE(GPU_wait, "GPU", "Wait for the GPU", MP_RGB(128, 128, 192));
  22. GPU::GPU(Core::System& system, std::unique_ptr<VideoCore::RendererBase>&& renderer_, bool is_async)
  23. : system{system}, renderer{std::move(renderer_)}, is_async{is_async} {
  24. auto& rasterizer{renderer->Rasterizer()};
  25. memory_manager = std::make_unique<Tegra::MemoryManager>(system, rasterizer);
  26. dma_pusher = std::make_unique<Tegra::DmaPusher>(*this);
  27. maxwell_3d = std::make_unique<Engines::Maxwell3D>(system, rasterizer, *memory_manager);
  28. fermi_2d = std::make_unique<Engines::Fermi2D>(rasterizer);
  29. kepler_compute = std::make_unique<Engines::KeplerCompute>(system, rasterizer, *memory_manager);
  30. maxwell_dma = std::make_unique<Engines::MaxwellDMA>(system, *memory_manager);
  31. kepler_memory = std::make_unique<Engines::KeplerMemory>(system, *memory_manager);
  32. }
  33. GPU::~GPU() = default;
  34. Engines::Maxwell3D& GPU::Maxwell3D() {
  35. return *maxwell_3d;
  36. }
  37. const Engines::Maxwell3D& GPU::Maxwell3D() const {
  38. return *maxwell_3d;
  39. }
  40. Engines::KeplerCompute& GPU::KeplerCompute() {
  41. return *kepler_compute;
  42. }
  43. const Engines::KeplerCompute& GPU::KeplerCompute() const {
  44. return *kepler_compute;
  45. }
  46. MemoryManager& GPU::MemoryManager() {
  47. return *memory_manager;
  48. }
  49. const MemoryManager& GPU::MemoryManager() const {
  50. return *memory_manager;
  51. }
  52. DmaPusher& GPU::DmaPusher() {
  53. return *dma_pusher;
  54. }
  55. const DmaPusher& GPU::DmaPusher() const {
  56. return *dma_pusher;
  57. }
  58. void GPU::WaitFence(u32 syncpoint_id, u32 value) {
  59. // Synced GPU, is always in sync
  60. if (!is_async) {
  61. return;
  62. }
  63. MICROPROFILE_SCOPE(GPU_wait);
  64. std::unique_lock lock{sync_mutex};
  65. sync_cv.wait(lock, [=]() { return syncpoints[syncpoint_id].load() >= value; });
  66. }
  67. void GPU::IncrementSyncPoint(const u32 syncpoint_id) {
  68. syncpoints[syncpoint_id]++;
  69. std::lock_guard lock{sync_mutex};
  70. sync_cv.notify_all();
  71. if (!syncpt_interrupts[syncpoint_id].empty()) {
  72. u32 value = syncpoints[syncpoint_id].load();
  73. auto it = syncpt_interrupts[syncpoint_id].begin();
  74. while (it != syncpt_interrupts[syncpoint_id].end()) {
  75. if (value >= *it) {
  76. TriggerCpuInterrupt(syncpoint_id, *it);
  77. it = syncpt_interrupts[syncpoint_id].erase(it);
  78. continue;
  79. }
  80. it++;
  81. }
  82. }
  83. }
  84. u32 GPU::GetSyncpointValue(const u32 syncpoint_id) const {
  85. return syncpoints[syncpoint_id].load();
  86. }
  87. void GPU::RegisterSyncptInterrupt(const u32 syncpoint_id, const u32 value) {
  88. auto& interrupt = syncpt_interrupts[syncpoint_id];
  89. bool contains = std::any_of(interrupt.begin(), interrupt.end(),
  90. [value](u32 in_value) { return in_value == value; });
  91. if (contains) {
  92. return;
  93. }
  94. syncpt_interrupts[syncpoint_id].emplace_back(value);
  95. }
  96. bool GPU::CancelSyncptInterrupt(const u32 syncpoint_id, const u32 value) {
  97. std::lock_guard lock{sync_mutex};
  98. auto& interrupt = syncpt_interrupts[syncpoint_id];
  99. const auto iter =
  100. std::find_if(interrupt.begin(), interrupt.end(),
  101. [value](u32 interrupt_value) { return value == interrupt_value; });
  102. if (iter == interrupt.end()) {
  103. return false;
  104. }
  105. interrupt.erase(iter);
  106. return true;
  107. }
  108. u64 GPU::GetTicks() const {
  109. // This values were reversed engineered by fincs from NVN
  110. // The gpu clock is reported in units of 385/625 nanoseconds
  111. constexpr u64 gpu_ticks_num = 384;
  112. constexpr u64 gpu_ticks_den = 625;
  113. const u64 cpu_ticks = system.CoreTiming().GetTicks();
  114. const u64 nanoseconds = Core::Timing::CyclesToNs(cpu_ticks).count();
  115. const u64 nanoseconds_num = nanoseconds / gpu_ticks_den;
  116. const u64 nanoseconds_rem = nanoseconds % gpu_ticks_den;
  117. return nanoseconds_num * gpu_ticks_num + (nanoseconds_rem * gpu_ticks_num) / gpu_ticks_den;
  118. }
  119. void GPU::FlushCommands() {
  120. renderer->Rasterizer().FlushCommands();
  121. }
  122. // Note that, traditionally, methods are treated as 4-byte addressable locations, and hence
  123. // their numbers are written down multiplied by 4 in Docs. Here we are not multiply by 4.
  124. // So the values you see in docs might be multiplied by 4.
  125. enum class BufferMethods {
  126. BindObject = 0x0,
  127. Nop = 0x2,
  128. SemaphoreAddressHigh = 0x4,
  129. SemaphoreAddressLow = 0x5,
  130. SemaphoreSequence = 0x6,
  131. SemaphoreTrigger = 0x7,
  132. NotifyIntr = 0x8,
  133. WrcacheFlush = 0x9,
  134. Unk28 = 0xA,
  135. UnkCacheFlush = 0xB,
  136. RefCnt = 0x14,
  137. SemaphoreAcquire = 0x1A,
  138. SemaphoreRelease = 0x1B,
  139. FenceValue = 0x1C,
  140. FenceAction = 0x1D,
  141. Unk78 = 0x1E,
  142. Unk7c = 0x1F,
  143. Yield = 0x20,
  144. NonPullerMethods = 0x40,
  145. };
  146. enum class GpuSemaphoreOperation {
  147. AcquireEqual = 0x1,
  148. WriteLong = 0x2,
  149. AcquireGequal = 0x4,
  150. AcquireMask = 0x8,
  151. };
  152. void GPU::CallMethod(const MethodCall& method_call) {
  153. LOG_TRACE(HW_GPU, "Processing method {:08X} on subchannel {}", method_call.method,
  154. method_call.subchannel);
  155. ASSERT(method_call.subchannel < bound_engines.size());
  156. if (ExecuteMethodOnEngine(method_call)) {
  157. CallEngineMethod(method_call);
  158. } else {
  159. CallPullerMethod(method_call);
  160. }
  161. }
  162. bool GPU::ExecuteMethodOnEngine(const MethodCall& method_call) {
  163. const auto method = static_cast<BufferMethods>(method_call.method);
  164. return method >= BufferMethods::NonPullerMethods;
  165. }
  166. void GPU::CallPullerMethod(const MethodCall& method_call) {
  167. regs.reg_array[method_call.method] = method_call.argument;
  168. const auto method = static_cast<BufferMethods>(method_call.method);
  169. switch (method) {
  170. case BufferMethods::BindObject: {
  171. ProcessBindMethod(method_call);
  172. break;
  173. }
  174. case BufferMethods::Nop:
  175. case BufferMethods::SemaphoreAddressHigh:
  176. case BufferMethods::SemaphoreAddressLow:
  177. case BufferMethods::SemaphoreSequence:
  178. case BufferMethods::RefCnt:
  179. case BufferMethods::UnkCacheFlush:
  180. case BufferMethods::WrcacheFlush:
  181. case BufferMethods::FenceValue:
  182. case BufferMethods::FenceAction:
  183. break;
  184. case BufferMethods::SemaphoreTrigger: {
  185. ProcessSemaphoreTriggerMethod();
  186. break;
  187. }
  188. case BufferMethods::NotifyIntr: {
  189. // TODO(Kmather73): Research and implement this method.
  190. LOG_ERROR(HW_GPU, "Special puller engine method NotifyIntr not implemented");
  191. break;
  192. }
  193. case BufferMethods::Unk28: {
  194. // TODO(Kmather73): Research and implement this method.
  195. LOG_ERROR(HW_GPU, "Special puller engine method Unk28 not implemented");
  196. break;
  197. }
  198. case BufferMethods::SemaphoreAcquire: {
  199. ProcessSemaphoreAcquire();
  200. break;
  201. }
  202. case BufferMethods::SemaphoreRelease: {
  203. ProcessSemaphoreRelease();
  204. break;
  205. }
  206. case BufferMethods::Yield: {
  207. // TODO(Kmather73): Research and implement this method.
  208. LOG_ERROR(HW_GPU, "Special puller engine method Yield not implemented");
  209. break;
  210. }
  211. default:
  212. LOG_ERROR(HW_GPU, "Special puller engine method {:X} not implemented",
  213. static_cast<u32>(method));
  214. break;
  215. }
  216. }
  217. void GPU::CallEngineMethod(const MethodCall& method_call) {
  218. const EngineID engine = bound_engines[method_call.subchannel];
  219. switch (engine) {
  220. case EngineID::FERMI_TWOD_A:
  221. fermi_2d->CallMethod(method_call);
  222. break;
  223. case EngineID::MAXWELL_B:
  224. maxwell_3d->CallMethod(method_call);
  225. break;
  226. case EngineID::KEPLER_COMPUTE_B:
  227. kepler_compute->CallMethod(method_call);
  228. break;
  229. case EngineID::MAXWELL_DMA_COPY_A:
  230. maxwell_dma->CallMethod(method_call);
  231. break;
  232. case EngineID::KEPLER_INLINE_TO_MEMORY_B:
  233. kepler_memory->CallMethod(method_call);
  234. break;
  235. default:
  236. UNIMPLEMENTED_MSG("Unimplemented engine");
  237. }
  238. }
  239. void GPU::ProcessBindMethod(const MethodCall& method_call) {
  240. // Bind the current subchannel to the desired engine id.
  241. LOG_DEBUG(HW_GPU, "Binding subchannel {} to engine {}", method_call.subchannel,
  242. method_call.argument);
  243. bound_engines[method_call.subchannel] = static_cast<EngineID>(method_call.argument);
  244. }
  245. void GPU::ProcessSemaphoreTriggerMethod() {
  246. const auto semaphoreOperationMask = 0xF;
  247. const auto op =
  248. static_cast<GpuSemaphoreOperation>(regs.semaphore_trigger & semaphoreOperationMask);
  249. if (op == GpuSemaphoreOperation::WriteLong) {
  250. struct Block {
  251. u32 sequence;
  252. u32 zeros = 0;
  253. u64 timestamp;
  254. };
  255. Block block{};
  256. block.sequence = regs.semaphore_sequence;
  257. // TODO(Kmather73): Generate a real GPU timestamp and write it here instead of
  258. // CoreTiming
  259. block.timestamp = GetTicks();
  260. memory_manager->WriteBlock(regs.semaphore_address.SemaphoreAddress(), &block,
  261. sizeof(block));
  262. } else {
  263. const u32 word{memory_manager->Read<u32>(regs.semaphore_address.SemaphoreAddress())};
  264. if ((op == GpuSemaphoreOperation::AcquireEqual && word == regs.semaphore_sequence) ||
  265. (op == GpuSemaphoreOperation::AcquireGequal &&
  266. static_cast<s32>(word - regs.semaphore_sequence) > 0) ||
  267. (op == GpuSemaphoreOperation::AcquireMask && (word & regs.semaphore_sequence))) {
  268. // Nothing to do in this case
  269. } else {
  270. regs.acquire_source = true;
  271. regs.acquire_value = regs.semaphore_sequence;
  272. if (op == GpuSemaphoreOperation::AcquireEqual) {
  273. regs.acquire_active = true;
  274. regs.acquire_mode = false;
  275. } else if (op == GpuSemaphoreOperation::AcquireGequal) {
  276. regs.acquire_active = true;
  277. regs.acquire_mode = true;
  278. } else if (op == GpuSemaphoreOperation::AcquireMask) {
  279. // TODO(kemathe) The acquire mask operation waits for a value that, ANDed with
  280. // semaphore_sequence, gives a non-0 result
  281. LOG_ERROR(HW_GPU, "Invalid semaphore operation AcquireMask not implemented");
  282. } else {
  283. LOG_ERROR(HW_GPU, "Invalid semaphore operation");
  284. }
  285. }
  286. }
  287. }
  288. void GPU::ProcessSemaphoreRelease() {
  289. memory_manager->Write<u32>(regs.semaphore_address.SemaphoreAddress(), regs.semaphore_release);
  290. }
  291. void GPU::ProcessSemaphoreAcquire() {
  292. const u32 word = memory_manager->Read<u32>(regs.semaphore_address.SemaphoreAddress());
  293. const auto value = regs.semaphore_acquire;
  294. if (word != value) {
  295. regs.acquire_active = true;
  296. regs.acquire_value = value;
  297. // TODO(kemathe73) figure out how to do the acquire_timeout
  298. regs.acquire_mode = false;
  299. regs.acquire_source = false;
  300. }
  301. }
  302. } // namespace Tegra