gpu.h 11 KB

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