gpu.h 11 KB

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