gpu.h 11 KB

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