gpu.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <atomic>
  7. #include <condition_variable>
  8. #include <list>
  9. #include <memory>
  10. #include <mutex>
  11. #include "common/common_types.h"
  12. #include "core/hle/service/nvdrv/nvdata.h"
  13. #include "core/hle/service/nvflinger/buffer_queue.h"
  14. #include "video_core/dma_pusher.h"
  15. using CacheAddr = std::uintptr_t;
  16. inline CacheAddr ToCacheAddr(const void* host_ptr) {
  17. return reinterpret_cast<CacheAddr>(host_ptr);
  18. }
  19. inline u8* FromCacheAddr(CacheAddr cache_addr) {
  20. return reinterpret_cast<u8*>(cache_addr);
  21. }
  22. namespace Core {
  23. namespace Frontend {
  24. class EmuWindow;
  25. }
  26. class System;
  27. } // namespace Core
  28. namespace VideoCore {
  29. class RendererBase;
  30. class ShaderNotify;
  31. } // namespace VideoCore
  32. namespace Tegra {
  33. enum class RenderTargetFormat : u32 {
  34. NONE = 0x0,
  35. RGBA32_FLOAT = 0xC0,
  36. RGBA32_UINT = 0xC2,
  37. RGBA16_UNORM = 0xC6,
  38. RGBA16_SNORM = 0xC7,
  39. RGBA16_UINT = 0xC9,
  40. RGBA16_FLOAT = 0xCA,
  41. RG32_FLOAT = 0xCB,
  42. RG32_UINT = 0xCD,
  43. RGBX16_FLOAT = 0xCE,
  44. BGRA8_UNORM = 0xCF,
  45. BGRA8_SRGB = 0xD0,
  46. RGB10_A2_UNORM = 0xD1,
  47. RGBA8_UNORM = 0xD5,
  48. RGBA8_SRGB = 0xD6,
  49. RGBA8_SNORM = 0xD7,
  50. RGBA8_UINT = 0xD9,
  51. RG16_UNORM = 0xDA,
  52. RG16_SNORM = 0xDB,
  53. RG16_SINT = 0xDC,
  54. RG16_UINT = 0xDD,
  55. RG16_FLOAT = 0xDE,
  56. R11G11B10_FLOAT = 0xE0,
  57. R32_SINT = 0xE3,
  58. R32_UINT = 0xE4,
  59. R32_FLOAT = 0xE5,
  60. B5G6R5_UNORM = 0xE8,
  61. BGR5A1_UNORM = 0xE9,
  62. RG8_UNORM = 0xEA,
  63. RG8_SNORM = 0xEB,
  64. RG8_UINT = 0xED,
  65. R16_UNORM = 0xEE,
  66. R16_SNORM = 0xEF,
  67. R16_SINT = 0xF0,
  68. R16_UINT = 0xF1,
  69. R16_FLOAT = 0xF2,
  70. R8_UNORM = 0xF3,
  71. R8_UINT = 0xF6,
  72. };
  73. enum class DepthFormat : u32 {
  74. Z32_FLOAT = 0xA,
  75. Z16_UNORM = 0x13,
  76. S8_Z24_UNORM = 0x14,
  77. Z24_X8_UNORM = 0x15,
  78. Z24_S8_UNORM = 0x16,
  79. Z24_C8_UNORM = 0x18,
  80. Z32_S8_X24_FLOAT = 0x19,
  81. };
  82. struct CommandListHeader;
  83. class DebugContext;
  84. /**
  85. * Struct describing framebuffer configuration
  86. */
  87. struct FramebufferConfig {
  88. enum class PixelFormat : u32 {
  89. ABGR8 = 1,
  90. RGB565 = 4,
  91. BGRA8 = 5,
  92. };
  93. VAddr address;
  94. u32 offset;
  95. u32 width;
  96. u32 height;
  97. u32 stride;
  98. PixelFormat pixel_format;
  99. using TransformFlags = Service::NVFlinger::BufferQueue::BufferTransformFlags;
  100. TransformFlags transform_flags;
  101. Common::Rectangle<int> crop_rect;
  102. };
  103. namespace Engines {
  104. class Fermi2D;
  105. class Maxwell3D;
  106. class MaxwellDMA;
  107. class KeplerCompute;
  108. class KeplerMemory;
  109. } // namespace Engines
  110. enum class EngineID {
  111. FERMI_TWOD_A = 0x902D, // 2D Engine
  112. MAXWELL_B = 0xB197, // 3D Engine
  113. KEPLER_COMPUTE_B = 0xB1C0,
  114. KEPLER_INLINE_TO_MEMORY_B = 0xA140,
  115. MAXWELL_DMA_COPY_A = 0xB0B5,
  116. };
  117. class MemoryManager;
  118. class GPU {
  119. public:
  120. explicit GPU(Core::System& system, std::unique_ptr<VideoCore::RendererBase>&& renderer,
  121. bool is_async);
  122. virtual ~GPU();
  123. struct MethodCall {
  124. u32 method{};
  125. u32 argument{};
  126. u32 subchannel{};
  127. u32 method_count{};
  128. bool IsLastCall() const {
  129. return method_count <= 1;
  130. }
  131. MethodCall(u32 method, u32 argument, u32 subchannel = 0, u32 method_count = 0)
  132. : method(method), argument(argument), subchannel(subchannel),
  133. method_count(method_count) {}
  134. };
  135. /// Calls a GPU method.
  136. void CallMethod(const MethodCall& method_call);
  137. /// Calls a GPU multivalue method.
  138. void CallMultiMethod(u32 method, u32 subchannel, const u32* base_start, u32 amount,
  139. u32 methods_pending);
  140. /// Flush all current written commands into the host GPU for execution.
  141. void FlushCommands();
  142. /// Synchronizes CPU writes with Host GPU memory.
  143. void SyncGuestHost();
  144. /// Signal the ending of command list.
  145. virtual void OnCommandListEnd();
  146. /// Request a host GPU memory flush from the CPU.
  147. u64 RequestFlush(VAddr addr, std::size_t size);
  148. /// Obtains current flush request fence id.
  149. u64 CurrentFlushRequestFence() const {
  150. return current_flush_fence.load(std::memory_order_relaxed);
  151. }
  152. /// Tick pending requests within the GPU.
  153. void TickWork();
  154. /// Returns a reference to the Maxwell3D GPU engine.
  155. Engines::Maxwell3D& Maxwell3D();
  156. /// Returns a const reference to the Maxwell3D GPU engine.
  157. const Engines::Maxwell3D& Maxwell3D() const;
  158. /// Returns a reference to the KeplerCompute GPU engine.
  159. Engines::KeplerCompute& KeplerCompute();
  160. /// Returns a reference to the KeplerCompute GPU engine.
  161. const Engines::KeplerCompute& KeplerCompute() const;
  162. /// Returns a reference to the GPU memory manager.
  163. Tegra::MemoryManager& MemoryManager();
  164. /// Returns a const reference to the GPU memory manager.
  165. const Tegra::MemoryManager& MemoryManager() const;
  166. /// Returns a reference to the GPU DMA pusher.
  167. Tegra::DmaPusher& DmaPusher();
  168. VideoCore::RendererBase& Renderer() {
  169. return *renderer;
  170. }
  171. const VideoCore::RendererBase& Renderer() const {
  172. return *renderer;
  173. }
  174. VideoCore::ShaderNotify& ShaderNotify() {
  175. return *shader_notify;
  176. }
  177. const VideoCore::ShaderNotify& ShaderNotify() const {
  178. return *shader_notify;
  179. }
  180. // Waits for the GPU to finish working
  181. virtual void WaitIdle() const = 0;
  182. /// Allows the CPU/NvFlinger to wait on the GPU before presenting a frame.
  183. void WaitFence(u32 syncpoint_id, u32 value);
  184. void IncrementSyncPoint(u32 syncpoint_id);
  185. u32 GetSyncpointValue(u32 syncpoint_id) const;
  186. void RegisterSyncptInterrupt(u32 syncpoint_id, u32 value);
  187. bool CancelSyncptInterrupt(u32 syncpoint_id, u32 value);
  188. u64 GetTicks() const;
  189. std::unique_lock<std::mutex> LockSync() {
  190. return std::unique_lock{sync_mutex};
  191. }
  192. bool IsAsync() const {
  193. return is_async;
  194. }
  195. /// Returns a const reference to the GPU DMA pusher.
  196. const Tegra::DmaPusher& DmaPusher() const;
  197. struct Regs {
  198. static constexpr size_t NUM_REGS = 0x100;
  199. union {
  200. struct {
  201. INSERT_UNION_PADDING_WORDS(0x4);
  202. struct {
  203. u32 address_high;
  204. u32 address_low;
  205. GPUVAddr SemaphoreAddress() const {
  206. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  207. address_low);
  208. }
  209. } semaphore_address;
  210. u32 semaphore_sequence;
  211. u32 semaphore_trigger;
  212. INSERT_UNION_PADDING_WORDS(0xC);
  213. // The puser and the puller share the reference counter, the pusher only has read
  214. // access
  215. u32 reference_count;
  216. INSERT_UNION_PADDING_WORDS(0x5);
  217. u32 semaphore_acquire;
  218. u32 semaphore_release;
  219. u32 fence_value;
  220. union {
  221. BitField<4, 4, u32> operation;
  222. BitField<8, 8, u32> id;
  223. } fence_action;
  224. INSERT_UNION_PADDING_WORDS(0xE2);
  225. // Puller state
  226. u32 acquire_mode;
  227. u32 acquire_source;
  228. u32 acquire_active;
  229. u32 acquire_timeout;
  230. u32 acquire_value;
  231. };
  232. std::array<u32, NUM_REGS> reg_array;
  233. };
  234. } regs{};
  235. /// Performs any additional setup necessary in order to begin GPU emulation.
  236. /// This can be used to launch any necessary threads and register any necessary
  237. /// core timing events.
  238. virtual void Start() = 0;
  239. /// Obtain the CPU Context
  240. virtual void ObtainContext() = 0;
  241. /// Release the CPU Context
  242. virtual void ReleaseContext() = 0;
  243. /// Push GPU command entries to be processed
  244. virtual void PushGPUEntries(Tegra::CommandList&& entries) = 0;
  245. /// Swap buffers (render frame)
  246. virtual void SwapBuffers(const Tegra::FramebufferConfig* framebuffer) = 0;
  247. /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
  248. virtual void FlushRegion(VAddr addr, u64 size) = 0;
  249. /// Notify rasterizer that any caches of the specified region should be invalidated
  250. virtual void InvalidateRegion(VAddr addr, u64 size) = 0;
  251. /// Notify rasterizer that any caches of the specified region should be flushed and invalidated
  252. virtual void FlushAndInvalidateRegion(VAddr addr, u64 size) = 0;
  253. protected:
  254. virtual void TriggerCpuInterrupt(u32 syncpoint_id, u32 value) const = 0;
  255. private:
  256. void ProcessBindMethod(const MethodCall& method_call);
  257. void ProcessSemaphoreTriggerMethod();
  258. void ProcessSemaphoreRelease();
  259. void ProcessSemaphoreAcquire();
  260. /// Calls a GPU puller method.
  261. void CallPullerMethod(const MethodCall& method_call);
  262. /// Calls a GPU engine method.
  263. void CallEngineMethod(const MethodCall& method_call);
  264. /// Calls a GPU engine multivalue method.
  265. void CallEngineMultiMethod(u32 method, u32 subchannel, const u32* base_start, u32 amount,
  266. u32 methods_pending);
  267. /// Determines where the method should be executed.
  268. bool ExecuteMethodOnEngine(u32 method);
  269. protected:
  270. std::unique_ptr<Tegra::DmaPusher> dma_pusher;
  271. Core::System& system;
  272. std::unique_ptr<VideoCore::RendererBase> renderer;
  273. private:
  274. std::unique_ptr<Tegra::MemoryManager> memory_manager;
  275. /// Mapping of command subchannels to their bound engine ids
  276. std::array<EngineID, 8> bound_engines = {};
  277. /// 3D engine
  278. std::unique_ptr<Engines::Maxwell3D> maxwell_3d;
  279. /// 2D engine
  280. std::unique_ptr<Engines::Fermi2D> fermi_2d;
  281. /// Compute engine
  282. std::unique_ptr<Engines::KeplerCompute> kepler_compute;
  283. /// DMA engine
  284. std::unique_ptr<Engines::MaxwellDMA> maxwell_dma;
  285. /// Inline memory engine
  286. std::unique_ptr<Engines::KeplerMemory> kepler_memory;
  287. /// Shader build notifier
  288. std::unique_ptr<VideoCore::ShaderNotify> shader_notify;
  289. std::array<std::atomic<u32>, Service::Nvidia::MaxSyncPoints> syncpoints{};
  290. std::array<std::list<u32>, Service::Nvidia::MaxSyncPoints> syncpt_interrupts;
  291. std::mutex sync_mutex;
  292. std::condition_variable sync_cv;
  293. struct FlushRequest {
  294. FlushRequest(u64 fence, VAddr addr, std::size_t size)
  295. : fence{fence}, addr{addr}, size{size} {}
  296. u64 fence;
  297. VAddr addr;
  298. std::size_t size;
  299. };
  300. std::list<FlushRequest> flush_requests;
  301. std::atomic<u64> current_flush_fence{};
  302. u64 last_flush_fence{};
  303. std::mutex flush_request_mutex;
  304. const bool is_async;
  305. };
  306. #define ASSERT_REG_POSITION(field_name, position) \
  307. static_assert(offsetof(GPU::Regs, field_name) == position * 4, \
  308. "Field " #field_name " has invalid position")
  309. ASSERT_REG_POSITION(semaphore_address, 0x4);
  310. ASSERT_REG_POSITION(semaphore_sequence, 0x6);
  311. ASSERT_REG_POSITION(semaphore_trigger, 0x7);
  312. ASSERT_REG_POSITION(reference_count, 0x14);
  313. ASSERT_REG_POSITION(semaphore_acquire, 0x1A);
  314. ASSERT_REG_POSITION(semaphore_release, 0x1B);
  315. ASSERT_REG_POSITION(fence_value, 0x1C);
  316. ASSERT_REG_POSITION(fence_action, 0x1D);
  317. ASSERT_REG_POSITION(acquire_mode, 0x100);
  318. ASSERT_REG_POSITION(acquire_source, 0x101);
  319. ASSERT_REG_POSITION(acquire_active, 0x102);
  320. ASSERT_REG_POSITION(acquire_timeout, 0x103);
  321. ASSERT_REG_POSITION(acquire_value, 0x104);
  322. #undef ASSERT_REG_POSITION
  323. } // namespace Tegra