gsp_gpu.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/bit_field.h"
  5. #include "common/microprofile.h"
  6. #include "core/core.h"
  7. #include "core/hle/kernel/event.h"
  8. #include "core/hle/kernel/shared_memory.h"
  9. #include "core/hle/result.h"
  10. #include "core/hw/gpu.h"
  11. #include "core/hw/hw.h"
  12. #include "core/hw/lcd.h"
  13. #include "core/memory.h"
  14. #include "gsp_gpu.h"
  15. #include "video_core/debug_utils/debug_utils.h"
  16. #include "video_core/gpu_debugger.h"
  17. // Main graphics debugger object - TODO: Here is probably not the best place for this
  18. GraphicsDebugger g_debugger;
  19. namespace Service {
  20. namespace GSP {
  21. // Beginning address of HW regs
  22. const u32 REGS_BEGIN = 0x1EB00000;
  23. namespace ErrCodes {
  24. enum {
  25. // TODO(purpasmart): Check if this name fits its actual usage
  26. OutofRangeOrMisalignedAddress = 513,
  27. FirstInitialization = 519,
  28. };
  29. }
  30. constexpr ResultCode RESULT_FIRST_INITIALIZATION(ErrCodes::FirstInitialization, ErrorModule::GX,
  31. ErrorSummary::Success, ErrorLevel::Success);
  32. constexpr ResultCode ERR_REGS_OUTOFRANGE_OR_MISALIGNED(ErrCodes::OutofRangeOrMisalignedAddress,
  33. ErrorModule::GX,
  34. ErrorSummary::InvalidArgument,
  35. ErrorLevel::Usage); // 0xE0E02A01
  36. constexpr ResultCode ERR_REGS_MISALIGNED(ErrorDescription::MisalignedSize, ErrorModule::GX,
  37. ErrorSummary::InvalidArgument,
  38. ErrorLevel::Usage); // 0xE0E02BF2
  39. constexpr ResultCode ERR_REGS_INVALID_SIZE(ErrorDescription::InvalidSize, ErrorModule::GX,
  40. ErrorSummary::InvalidArgument,
  41. ErrorLevel::Usage); // 0xE0E02BEC
  42. /// Event triggered when GSP interrupt has been signalled
  43. Kernel::SharedPtr<Kernel::Event> g_interrupt_event;
  44. /// GSP shared memoryings
  45. Kernel::SharedPtr<Kernel::SharedMemory> g_shared_memory;
  46. /// Thread index into interrupt relay queue
  47. u32 g_thread_id = 0;
  48. static bool gpu_right_acquired = false;
  49. static bool first_initialization = true;
  50. /// Gets a pointer to a thread command buffer in GSP shared memory
  51. static inline u8* GetCommandBuffer(u32 thread_id) {
  52. return g_shared_memory->GetPointer(0x800 + (thread_id * sizeof(CommandBuffer)));
  53. }
  54. FrameBufferUpdate* GetFrameBufferInfo(u32 thread_id, u32 screen_index) {
  55. DEBUG_ASSERT_MSG(screen_index < 2, "Invalid screen index");
  56. // For each thread there are two FrameBufferUpdate fields
  57. u32 offset = 0x200 + (2 * thread_id + screen_index) * sizeof(FrameBufferUpdate);
  58. u8* ptr = g_shared_memory->GetPointer(offset);
  59. return reinterpret_cast<FrameBufferUpdate*>(ptr);
  60. }
  61. /// Gets a pointer to the interrupt relay queue for a given thread index
  62. static inline InterruptRelayQueue* GetInterruptRelayQueue(u32 thread_id) {
  63. u8* ptr = g_shared_memory->GetPointer(sizeof(InterruptRelayQueue) * thread_id);
  64. return reinterpret_cast<InterruptRelayQueue*>(ptr);
  65. }
  66. /**
  67. * Writes a single GSP GPU hardware registers with a single u32 value
  68. * (For internal use.)
  69. *
  70. * @param base_address The address of the register in question
  71. * @param data Data to be written
  72. */
  73. static void WriteSingleHWReg(u32 base_address, u32 data) {
  74. DEBUG_ASSERT_MSG((base_address & 3) == 0 && base_address < 0x420000,
  75. "Write address out of range or misaligned");
  76. HW::Write<u32>(base_address + REGS_BEGIN, data);
  77. }
  78. /**
  79. * Writes sequential GSP GPU hardware registers using an array of source data
  80. *
  81. * @param base_address The address of the first register in the sequence
  82. * @param size_in_bytes The number of registers to update (size of data)
  83. * @param data_vaddr A pointer to the source data
  84. * @return RESULT_SUCCESS if the parameters are valid, error code otherwise
  85. */
  86. static ResultCode WriteHWRegs(u32 base_address, u32 size_in_bytes, VAddr data_vaddr) {
  87. // This magic number is verified to be done by the gsp module
  88. const u32 max_size_in_bytes = 0x80;
  89. if (base_address & 3 || base_address >= 0x420000) {
  90. LOG_ERROR(Service_GSP,
  91. "Write address was out of range or misaligned! (address=0x%08x, size=0x%08x)",
  92. base_address, size_in_bytes);
  93. return ERR_REGS_OUTOFRANGE_OR_MISALIGNED;
  94. } else if (size_in_bytes <= max_size_in_bytes) {
  95. if (size_in_bytes & 3) {
  96. LOG_ERROR(Service_GSP, "Misaligned size 0x%08x", size_in_bytes);
  97. return ERR_REGS_MISALIGNED;
  98. } else {
  99. while (size_in_bytes > 0) {
  100. WriteSingleHWReg(base_address, Memory::Read32(data_vaddr));
  101. size_in_bytes -= 4;
  102. data_vaddr += 4;
  103. base_address += 4;
  104. }
  105. return RESULT_SUCCESS;
  106. }
  107. } else {
  108. LOG_ERROR(Service_GSP, "Out of range size 0x%08x", size_in_bytes);
  109. return ERR_REGS_INVALID_SIZE;
  110. }
  111. }
  112. /**
  113. * Updates sequential GSP GPU hardware registers using parallel arrays of source data and masks.
  114. * For each register, the value is updated only where the mask is high
  115. *
  116. * @param base_address The address of the first register in the sequence
  117. * @param size_in_bytes The number of registers to update (size of data)
  118. * @param data_vaddr A virtual address to the source data to use for updates
  119. * @param masks_vaddr A virtual address to the masks
  120. * @return RESULT_SUCCESS if the parameters are valid, error code otherwise
  121. */
  122. static ResultCode WriteHWRegsWithMask(u32 base_address, u32 size_in_bytes, VAddr data_vaddr,
  123. VAddr masks_vaddr) {
  124. // This magic number is verified to be done by the gsp module
  125. const u32 max_size_in_bytes = 0x80;
  126. if (base_address & 3 || base_address >= 0x420000) {
  127. LOG_ERROR(Service_GSP,
  128. "Write address was out of range or misaligned! (address=0x%08x, size=0x%08x)",
  129. base_address, size_in_bytes);
  130. return ERR_REGS_OUTOFRANGE_OR_MISALIGNED;
  131. } else if (size_in_bytes <= max_size_in_bytes) {
  132. if (size_in_bytes & 3) {
  133. LOG_ERROR(Service_GSP, "Misaligned size 0x%08x", size_in_bytes);
  134. return ERR_REGS_MISALIGNED;
  135. } else {
  136. while (size_in_bytes > 0) {
  137. const u32 reg_address = base_address + REGS_BEGIN;
  138. u32 reg_value;
  139. HW::Read<u32>(reg_value, reg_address);
  140. u32 data = Memory::Read32(data_vaddr);
  141. u32 mask = Memory::Read32(masks_vaddr);
  142. // Update the current value of the register only for set mask bits
  143. reg_value = (reg_value & ~mask) | (data & mask);
  144. WriteSingleHWReg(base_address, reg_value);
  145. size_in_bytes -= 4;
  146. data_vaddr += 4;
  147. masks_vaddr += 4;
  148. base_address += 4;
  149. }
  150. return RESULT_SUCCESS;
  151. }
  152. } else {
  153. LOG_ERROR(Service_GSP, "Out of range size 0x%08x", size_in_bytes);
  154. return ERR_REGS_INVALID_SIZE;
  155. }
  156. }
  157. /**
  158. * GSP_GPU::WriteHWRegs service function
  159. *
  160. * Writes sequential GSP GPU hardware registers
  161. *
  162. * Inputs:
  163. * 1 : address of first GPU register
  164. * 2 : number of registers to write sequentially
  165. * 4 : pointer to source data array
  166. */
  167. static void WriteHWRegs(Interface* self) {
  168. u32* cmd_buff = Kernel::GetCommandBuffer();
  169. u32 reg_addr = cmd_buff[1];
  170. u32 size = cmd_buff[2];
  171. VAddr src = cmd_buff[4];
  172. cmd_buff[1] = WriteHWRegs(reg_addr, size, src).raw;
  173. }
  174. /**
  175. * GSP_GPU::WriteHWRegsWithMask service function
  176. *
  177. * Updates sequential GSP GPU hardware registers using masks
  178. *
  179. * Inputs:
  180. * 1 : address of first GPU register
  181. * 2 : number of registers to update sequentially
  182. * 4 : pointer to source data array
  183. * 6 : pointer to mask array
  184. */
  185. static void WriteHWRegsWithMask(Interface* self) {
  186. u32* cmd_buff = Kernel::GetCommandBuffer();
  187. u32 reg_addr = cmd_buff[1];
  188. u32 size = cmd_buff[2];
  189. VAddr src_data = cmd_buff[4];
  190. VAddr mask_data = cmd_buff[6];
  191. cmd_buff[1] = WriteHWRegsWithMask(reg_addr, size, src_data, mask_data).raw;
  192. }
  193. /// Read a GSP GPU hardware register
  194. static void ReadHWRegs(Interface* self) {
  195. u32* cmd_buff = Kernel::GetCommandBuffer();
  196. u32 reg_addr = cmd_buff[1];
  197. u32 size = cmd_buff[2];
  198. // TODO: Return proper error codes
  199. if (reg_addr + size >= 0x420000) {
  200. LOG_ERROR(Service_GSP, "Read address out of range! (address=0x%08x, size=0x%08x)", reg_addr,
  201. size);
  202. return;
  203. }
  204. // size should be word-aligned
  205. if ((size % 4) != 0) {
  206. LOG_ERROR(Service_GSP, "Invalid size 0x%08x", size);
  207. return;
  208. }
  209. VAddr dst_vaddr = cmd_buff[0x41];
  210. while (size > 0) {
  211. u32 value;
  212. HW::Read<u32>(value, reg_addr + REGS_BEGIN);
  213. Memory::Write32(dst_vaddr, value);
  214. size -= 4;
  215. dst_vaddr += 4;
  216. reg_addr += 4;
  217. }
  218. }
  219. ResultCode SetBufferSwap(u32 screen_id, const FrameBufferInfo& info) {
  220. u32 base_address = 0x400000;
  221. PAddr phys_address_left = Memory::VirtualToPhysicalAddress(info.address_left);
  222. PAddr phys_address_right = Memory::VirtualToPhysicalAddress(info.address_right);
  223. if (info.active_fb == 0) {
  224. WriteSingleHWReg(
  225. base_address +
  226. 4 * static_cast<u32>(GPU_REG_INDEX(framebuffer_config[screen_id].address_left1)),
  227. phys_address_left);
  228. WriteSingleHWReg(
  229. base_address +
  230. 4 * static_cast<u32>(GPU_REG_INDEX(framebuffer_config[screen_id].address_right1)),
  231. phys_address_right);
  232. } else {
  233. WriteSingleHWReg(
  234. base_address +
  235. 4 * static_cast<u32>(GPU_REG_INDEX(framebuffer_config[screen_id].address_left2)),
  236. phys_address_left);
  237. WriteSingleHWReg(
  238. base_address +
  239. 4 * static_cast<u32>(GPU_REG_INDEX(framebuffer_config[screen_id].address_right2)),
  240. phys_address_right);
  241. }
  242. WriteSingleHWReg(base_address +
  243. 4 * static_cast<u32>(GPU_REG_INDEX(framebuffer_config[screen_id].stride)),
  244. info.stride);
  245. WriteSingleHWReg(
  246. base_address +
  247. 4 * static_cast<u32>(GPU_REG_INDEX(framebuffer_config[screen_id].color_format)),
  248. info.format);
  249. WriteSingleHWReg(
  250. base_address + 4 * static_cast<u32>(GPU_REG_INDEX(framebuffer_config[screen_id].active_fb)),
  251. info.shown_fb);
  252. if (Pica::g_debug_context)
  253. Pica::g_debug_context->OnEvent(Pica::DebugContext::Event::BufferSwapped, nullptr);
  254. if (screen_id == 0) {
  255. MicroProfileFlip();
  256. Core::System::GetInstance().perf_stats.EndGameFrame();
  257. }
  258. return RESULT_SUCCESS;
  259. }
  260. /**
  261. * GSP_GPU::SetBufferSwap service function
  262. *
  263. * Updates GPU display framebuffer configuration using the specified parameters.
  264. *
  265. * Inputs:
  266. * 1 : Screen ID (0 = top screen, 1 = bottom screen)
  267. * 2-7 : FrameBufferInfo structure
  268. * Outputs:
  269. * 1: Result code
  270. */
  271. static void SetBufferSwap(Interface* self) {
  272. u32* cmd_buff = Kernel::GetCommandBuffer();
  273. u32 screen_id = cmd_buff[1];
  274. FrameBufferInfo* fb_info = (FrameBufferInfo*)&cmd_buff[2];
  275. cmd_buff[1] = SetBufferSwap(screen_id, *fb_info).raw;
  276. }
  277. /**
  278. * GSP_GPU::FlushDataCache service function
  279. *
  280. * This Function is a no-op, We aren't emulating the CPU cache any time soon.
  281. *
  282. * Inputs:
  283. * 1 : Address
  284. * 2 : Size
  285. * 3 : Value 0, some descriptor for the KProcess Handle
  286. * 4 : KProcess handle
  287. * Outputs:
  288. * 1 : Result of function, 0 on success, otherwise error code
  289. */
  290. static void FlushDataCache(Interface* self) {
  291. u32* cmd_buff = Kernel::GetCommandBuffer();
  292. u32 address = cmd_buff[1];
  293. u32 size = cmd_buff[2];
  294. u32 process = cmd_buff[4];
  295. // TODO(purpasmart96): Verify return header on HW
  296. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  297. LOG_DEBUG(Service_GSP, "(STUBBED) called address=0x%08X, size=0x%08X, process=0x%08X", address,
  298. size, process);
  299. }
  300. /**
  301. * GSP_GPU::SetAxiConfigQoSMode service function
  302. * Inputs:
  303. * 1 : Mode, unused in emulator
  304. * Outputs:
  305. * 1 : Result of function, 0 on success, otherwise error code
  306. */
  307. static void SetAxiConfigQoSMode(Interface* self) {
  308. u32* cmd_buff = Kernel::GetCommandBuffer();
  309. u32 mode = cmd_buff[1];
  310. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  311. LOG_DEBUG(Service_GSP, "(STUBBED) called mode=0x%08X", mode);
  312. }
  313. /**
  314. * GSP_GPU::RegisterInterruptRelayQueue service function
  315. * Inputs:
  316. * 1 : "Flags" field, purpose is unknown
  317. * 3 : Handle to GSP synchronization event
  318. * Outputs:
  319. * 1 : Result of function, 0x2A07 on success, otherwise error code
  320. * 2 : Thread index into GSP command buffer
  321. * 4 : Handle to GSP shared memory
  322. */
  323. static void RegisterInterruptRelayQueue(Interface* self) {
  324. u32* cmd_buff = Kernel::GetCommandBuffer();
  325. u32 flags = cmd_buff[1];
  326. g_interrupt_event = Kernel::g_handle_table.Get<Kernel::Event>(cmd_buff[3]);
  327. // TODO(mailwl): return right error code instead assert
  328. ASSERT_MSG((g_interrupt_event != nullptr), "handle is not valid!");
  329. g_interrupt_event->name = "GSP_GPU::interrupt_event";
  330. if (first_initialization) {
  331. // This specific code is required for a successful initialization, rather than 0
  332. first_initialization = false;
  333. cmd_buff[1] = RESULT_FIRST_INITIALIZATION.raw;
  334. } else {
  335. cmd_buff[1] = RESULT_SUCCESS.raw;
  336. }
  337. cmd_buff[2] = g_thread_id++; // Thread ID
  338. cmd_buff[4] = Kernel::g_handle_table.Create(g_shared_memory).MoveFrom(); // GSP shared memory
  339. g_interrupt_event->Signal(); // TODO(bunnei): Is this correct?
  340. LOG_WARNING(Service_GSP, "called, flags=0x%08X", flags);
  341. }
  342. /**
  343. * GSP_GPU::UnregisterInterruptRelayQueue service function
  344. * Outputs:
  345. * 1 : Result of function, 0 on success, otherwise error code
  346. */
  347. static void UnregisterInterruptRelayQueue(Interface* self) {
  348. u32* cmd_buff = Kernel::GetCommandBuffer();
  349. g_thread_id = 0;
  350. g_interrupt_event = nullptr;
  351. cmd_buff[1] = RESULT_SUCCESS.raw;
  352. LOG_WARNING(Service_GSP, "(STUBBED) called");
  353. }
  354. /**
  355. * Signals that the specified interrupt type has occurred to userland code
  356. * @param interrupt_id ID of interrupt that is being signalled
  357. * @todo This should probably take a thread_id parameter and only signal this thread?
  358. * @todo This probably does not belong in the GSP module, instead move to video_core
  359. */
  360. void SignalInterrupt(InterruptId interrupt_id) {
  361. if (!gpu_right_acquired) {
  362. return;
  363. }
  364. if (nullptr == g_interrupt_event) {
  365. LOG_WARNING(Service_GSP, "cannot synchronize until GSP event has been created!");
  366. return;
  367. }
  368. if (nullptr == g_shared_memory) {
  369. LOG_WARNING(Service_GSP, "cannot synchronize until GSP shared memory has been created!");
  370. return;
  371. }
  372. for (int thread_id = 0; thread_id < 0x4; ++thread_id) {
  373. InterruptRelayQueue* interrupt_relay_queue = GetInterruptRelayQueue(thread_id);
  374. u8 next = interrupt_relay_queue->index;
  375. next += interrupt_relay_queue->number_interrupts;
  376. next = next % 0x34; // 0x34 is the number of interrupt slots
  377. interrupt_relay_queue->number_interrupts += 1;
  378. interrupt_relay_queue->slot[next] = interrupt_id;
  379. interrupt_relay_queue->error_code = 0x0; // No error
  380. // Update framebuffer information if requested
  381. // TODO(yuriks): Confirm where this code should be called. It is definitely updated without
  382. // executing any GSP commands, only waiting on the event.
  383. int screen_id =
  384. (interrupt_id == InterruptId::PDC0) ? 0 : (interrupt_id == InterruptId::PDC1) ? 1 : -1;
  385. if (screen_id != -1) {
  386. FrameBufferUpdate* info = GetFrameBufferInfo(thread_id, screen_id);
  387. if (info->is_dirty) {
  388. SetBufferSwap(screen_id, info->framebuffer_info[info->index]);
  389. info->is_dirty.Assign(false);
  390. }
  391. }
  392. }
  393. g_interrupt_event->Signal();
  394. }
  395. MICROPROFILE_DEFINE(GPU_GSP_DMA, "GPU", "GSP DMA", MP_RGB(100, 0, 255));
  396. /// Executes the next GSP command
  397. static void ExecuteCommand(const Command& command, u32 thread_id) {
  398. // Utility function to convert register ID to address
  399. static auto WriteGPURegister = [](u32 id, u32 data) {
  400. GPU::Write<u32>(0x1EF00000 + 4 * id, data);
  401. };
  402. switch (command.id) {
  403. // GX request DMA - typically used for copying memory from GSP heap to VRAM
  404. case CommandId::REQUEST_DMA: {
  405. MICROPROFILE_SCOPE(GPU_GSP_DMA);
  406. // TODO: Consider attempting rasterizer-accelerated surface blit if that usage is ever
  407. // possible/likely
  408. Memory::RasterizerFlushRegion(
  409. Memory::VirtualToPhysicalAddress(command.dma_request.source_address),
  410. command.dma_request.size);
  411. Memory::RasterizerFlushAndInvalidateRegion(
  412. Memory::VirtualToPhysicalAddress(command.dma_request.dest_address),
  413. command.dma_request.size);
  414. // TODO(Subv): These memory accesses should not go through the application's memory mapping.
  415. // They should go through the GSP module's memory mapping.
  416. Memory::CopyBlock(command.dma_request.dest_address, command.dma_request.source_address,
  417. command.dma_request.size);
  418. SignalInterrupt(InterruptId::DMA);
  419. break;
  420. }
  421. // TODO: This will need some rework in the future. (why?)
  422. case CommandId::SUBMIT_GPU_CMDLIST: {
  423. auto& params = command.submit_gpu_cmdlist;
  424. if (params.do_flush) {
  425. // This flag flushes the command list (params.address, params.size) from the cache.
  426. // Command lists are not processed by the hardware renderer, so we don't need to
  427. // actually flush them in Citra.
  428. }
  429. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(command_processor_config.address)),
  430. Memory::VirtualToPhysicalAddress(params.address) >> 3);
  431. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(command_processor_config.size)),
  432. params.size);
  433. // TODO: Not sure if we are supposed to always write this .. seems to trigger processing
  434. // though
  435. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(command_processor_config.trigger)), 1);
  436. // TODO(yuriks): Figure out the meaning of the `flags` field.
  437. break;
  438. }
  439. // It's assumed that the two "blocks" behave equivalently.
  440. // Presumably this is done simply to allow two memory fills to run in parallel.
  441. case CommandId::SET_MEMORY_FILL: {
  442. auto& params = command.memory_fill;
  443. if (params.start1 != 0) {
  444. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[0].address_start)),
  445. Memory::VirtualToPhysicalAddress(params.start1) >> 3);
  446. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[0].address_end)),
  447. Memory::VirtualToPhysicalAddress(params.end1) >> 3);
  448. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[0].value_32bit)),
  449. params.value1);
  450. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[0].control)),
  451. params.control1);
  452. }
  453. if (params.start2 != 0) {
  454. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[1].address_start)),
  455. Memory::VirtualToPhysicalAddress(params.start2) >> 3);
  456. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[1].address_end)),
  457. Memory::VirtualToPhysicalAddress(params.end2) >> 3);
  458. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[1].value_32bit)),
  459. params.value2);
  460. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(memory_fill_config[1].control)),
  461. params.control2);
  462. }
  463. break;
  464. }
  465. case CommandId::SET_DISPLAY_TRANSFER: {
  466. auto& params = command.display_transfer;
  467. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(display_transfer_config.input_address)),
  468. Memory::VirtualToPhysicalAddress(params.in_buffer_address) >> 3);
  469. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(display_transfer_config.output_address)),
  470. Memory::VirtualToPhysicalAddress(params.out_buffer_address) >> 3);
  471. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(display_transfer_config.input_size)),
  472. params.in_buffer_size);
  473. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(display_transfer_config.output_size)),
  474. params.out_buffer_size);
  475. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(display_transfer_config.flags)),
  476. params.flags);
  477. WriteGPURegister(static_cast<u32>(GPU_REG_INDEX(display_transfer_config.trigger)), 1);
  478. break;
  479. }
  480. case CommandId::SET_TEXTURE_COPY: {
  481. auto& params = command.texture_copy;
  482. WriteGPURegister((u32)GPU_REG_INDEX(display_transfer_config.input_address),
  483. Memory::VirtualToPhysicalAddress(params.in_buffer_address) >> 3);
  484. WriteGPURegister((u32)GPU_REG_INDEX(display_transfer_config.output_address),
  485. Memory::VirtualToPhysicalAddress(params.out_buffer_address) >> 3);
  486. WriteGPURegister((u32)GPU_REG_INDEX(display_transfer_config.texture_copy.size),
  487. params.size);
  488. WriteGPURegister((u32)GPU_REG_INDEX(display_transfer_config.texture_copy.input_size),
  489. params.in_width_gap);
  490. WriteGPURegister((u32)GPU_REG_INDEX(display_transfer_config.texture_copy.output_size),
  491. params.out_width_gap);
  492. WriteGPURegister((u32)GPU_REG_INDEX(display_transfer_config.flags), params.flags);
  493. // NOTE: Actual GSP ORs 1 with current register instead of overwriting. Doesn't seem to
  494. // matter.
  495. WriteGPURegister((u32)GPU_REG_INDEX(display_transfer_config.trigger), 1);
  496. break;
  497. }
  498. case CommandId::CACHE_FLUSH: {
  499. // NOTE: Rasterizer flushing handled elsewhere in CPU read/write and other GPU handlers
  500. // Use command.cache_flush.regions to implement this handler
  501. break;
  502. }
  503. default:
  504. LOG_ERROR(Service_GSP, "unknown command 0x%08X", (int)command.id.Value());
  505. }
  506. if (Pica::g_debug_context)
  507. Pica::g_debug_context->OnEvent(Pica::DebugContext::Event::GSPCommandProcessed,
  508. (void*)&command);
  509. }
  510. /**
  511. * GSP_GPU::SetLcdForceBlack service function
  512. *
  513. * Enable or disable REG_LCDCOLORFILL with the color black.
  514. *
  515. * Inputs:
  516. * 1: Black color fill flag (0 = don't fill, !0 = fill)
  517. * Outputs:
  518. * 1: Result code
  519. */
  520. static void SetLcdForceBlack(Interface* self) {
  521. u32* cmd_buff = Kernel::GetCommandBuffer();
  522. bool enable_black = cmd_buff[1] != 0;
  523. LCD::Regs::ColorFill data = {0};
  524. // Since data is already zeroed, there is no need to explicitly set
  525. // the color to black (all zero).
  526. data.is_enabled.Assign(enable_black);
  527. LCD::Write(HW::VADDR_LCD + 4 * LCD_REG_INDEX(color_fill_top), data.raw); // Top LCD
  528. LCD::Write(HW::VADDR_LCD + 4 * LCD_REG_INDEX(color_fill_bottom), data.raw); // Bottom LCD
  529. cmd_buff[1] = RESULT_SUCCESS.raw;
  530. }
  531. /// This triggers handling of the GX command written to the command buffer in shared memory.
  532. static void TriggerCmdReqQueue(Interface* self) {
  533. // Iterate through each thread's command queue...
  534. for (unsigned thread_id = 0; thread_id < 0x4; ++thread_id) {
  535. CommandBuffer* command_buffer = (CommandBuffer*)GetCommandBuffer(thread_id);
  536. // Iterate through each command...
  537. for (unsigned i = 0; i < command_buffer->number_commands; ++i) {
  538. g_debugger.GXCommandProcessed((u8*)&command_buffer->commands[i]);
  539. // Decode and execute command
  540. ExecuteCommand(command_buffer->commands[i], thread_id);
  541. // Indicates that command has completed
  542. command_buffer->number_commands.Assign(command_buffer->number_commands - 1);
  543. }
  544. }
  545. u32* cmd_buff = Kernel::GetCommandBuffer();
  546. cmd_buff[1] = 0; // No error
  547. }
  548. /**
  549. * GSP_GPU::ImportDisplayCaptureInfo service function
  550. *
  551. * Returns information about the current framebuffer state
  552. *
  553. * Inputs:
  554. * 0: Header 0x00180000
  555. * Outputs:
  556. * 0: Header Code[0x00180240]
  557. * 1: Result code
  558. * 2: Left framebuffer virtual address for the main screen
  559. * 3: Right framebuffer virtual address for the main screen
  560. * 4: Main screen framebuffer format
  561. * 5: Main screen framebuffer width
  562. * 6: Left framebuffer virtual address for the bottom screen
  563. * 7: Right framebuffer virtual address for the bottom screen
  564. * 8: Bottom screen framebuffer format
  565. * 9: Bottom screen framebuffer width
  566. */
  567. static void ImportDisplayCaptureInfo(Interface* self) {
  568. u32* cmd_buff = Kernel::GetCommandBuffer();
  569. // TODO(Subv): We're always returning the framebuffer structures for thread_id = 0,
  570. // because we only support a single running application at a time.
  571. // This should always return the framebuffer data that is currently displayed on the screen.
  572. u32 thread_id = 0;
  573. FrameBufferUpdate* top_screen = GetFrameBufferInfo(thread_id, 0);
  574. FrameBufferUpdate* bottom_screen = GetFrameBufferInfo(thread_id, 1);
  575. cmd_buff[0] = IPC::MakeHeader(0x18, 0x9, 0);
  576. cmd_buff[1] = RESULT_SUCCESS.raw;
  577. // Top Screen
  578. cmd_buff[2] = top_screen->framebuffer_info[top_screen->index].address_left;
  579. cmd_buff[3] = top_screen->framebuffer_info[top_screen->index].address_right;
  580. cmd_buff[4] = top_screen->framebuffer_info[top_screen->index].format;
  581. cmd_buff[5] = top_screen->framebuffer_info[top_screen->index].stride;
  582. // Bottom Screen
  583. cmd_buff[6] = bottom_screen->framebuffer_info[bottom_screen->index].address_left;
  584. cmd_buff[7] = bottom_screen->framebuffer_info[bottom_screen->index].address_right;
  585. cmd_buff[8] = bottom_screen->framebuffer_info[bottom_screen->index].format;
  586. cmd_buff[9] = bottom_screen->framebuffer_info[bottom_screen->index].stride;
  587. LOG_WARNING(Service_GSP, "called");
  588. }
  589. /**
  590. * GSP_GPU::AcquireRight service function
  591. * Outputs:
  592. * 1: Result code
  593. */
  594. static void AcquireRight(Interface* self) {
  595. u32* cmd_buff = Kernel::GetCommandBuffer();
  596. gpu_right_acquired = true;
  597. cmd_buff[1] = RESULT_SUCCESS.raw;
  598. LOG_WARNING(Service_GSP, "called");
  599. }
  600. /**
  601. * GSP_GPU::ReleaseRight service function
  602. * Outputs:
  603. * 1: Result code
  604. */
  605. static void ReleaseRight(Interface* self) {
  606. u32* cmd_buff = Kernel::GetCommandBuffer();
  607. gpu_right_acquired = false;
  608. cmd_buff[1] = RESULT_SUCCESS.raw;
  609. LOG_WARNING(Service_GSP, "called");
  610. }
  611. /**
  612. * GSP_GPU::StoreDataCache service function
  613. *
  614. * This Function is a no-op, We aren't emulating the CPU cache any time soon.
  615. *
  616. * Inputs:
  617. * 0 : Header code [0x001F0082]
  618. * 1 : Address
  619. * 2 : Size
  620. * 3 : Value 0, some descriptor for the KProcess Handle
  621. * 4 : KProcess handle
  622. * Outputs:
  623. * 1 : Result of function, 0 on success, otherwise error code
  624. */
  625. static void StoreDataCache(Interface* self) {
  626. u32* cmd_buff = Kernel::GetCommandBuffer();
  627. u32 address = cmd_buff[1];
  628. u32 size = cmd_buff[2];
  629. u32 process = cmd_buff[4];
  630. cmd_buff[0] = IPC::MakeHeader(0x1F, 0x1, 0);
  631. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  632. LOG_DEBUG(Service_GSP, "(STUBBED) called address=0x%08X, size=0x%08X, process=0x%08X", address,
  633. size, process);
  634. }
  635. const Interface::FunctionInfo FunctionTable[] = {
  636. {0x00010082, WriteHWRegs, "WriteHWRegs"},
  637. {0x00020084, WriteHWRegsWithMask, "WriteHWRegsWithMask"},
  638. {0x00030082, nullptr, "WriteHWRegRepeat"},
  639. {0x00040080, ReadHWRegs, "ReadHWRegs"},
  640. {0x00050200, SetBufferSwap, "SetBufferSwap"},
  641. {0x00060082, nullptr, "SetCommandList"},
  642. {0x000700C2, nullptr, "RequestDma"},
  643. {0x00080082, FlushDataCache, "FlushDataCache"},
  644. {0x00090082, nullptr, "InvalidateDataCache"},
  645. {0x000A0044, nullptr, "RegisterInterruptEvents"},
  646. {0x000B0040, SetLcdForceBlack, "SetLcdForceBlack"},
  647. {0x000C0000, TriggerCmdReqQueue, "TriggerCmdReqQueue"},
  648. {0x000D0140, nullptr, "SetDisplayTransfer"},
  649. {0x000E0180, nullptr, "SetTextureCopy"},
  650. {0x000F0200, nullptr, "SetMemoryFill"},
  651. {0x00100040, SetAxiConfigQoSMode, "SetAxiConfigQoSMode"},
  652. {0x00110040, nullptr, "SetPerfLogMode"},
  653. {0x00120000, nullptr, "GetPerfLog"},
  654. {0x00130042, RegisterInterruptRelayQueue, "RegisterInterruptRelayQueue"},
  655. {0x00140000, UnregisterInterruptRelayQueue, "UnregisterInterruptRelayQueue"},
  656. {0x00150002, nullptr, "TryAcquireRight"},
  657. {0x00160042, AcquireRight, "AcquireRight"},
  658. {0x00170000, ReleaseRight, "ReleaseRight"},
  659. {0x00180000, ImportDisplayCaptureInfo, "ImportDisplayCaptureInfo"},
  660. {0x00190000, nullptr, "SaveVramSysArea"},
  661. {0x001A0000, nullptr, "RestoreVramSysArea"},
  662. {0x001B0000, nullptr, "ResetGpuCore"},
  663. {0x001C0040, nullptr, "SetLedForceOff"},
  664. {0x001D0040, nullptr, "SetTestCommand"},
  665. {0x001E0080, nullptr, "SetInternalPriorities"},
  666. {0x001F0082, StoreDataCache, "StoreDataCache"},
  667. };
  668. GSP_GPU::GSP_GPU() {
  669. Register(FunctionTable);
  670. g_interrupt_event = nullptr;
  671. using Kernel::MemoryPermission;
  672. g_shared_memory = Kernel::SharedMemory::Create(nullptr, 0x1000, MemoryPermission::ReadWrite,
  673. MemoryPermission::ReadWrite, 0,
  674. Kernel::MemoryRegion::BASE, "GSP:SharedMemory");
  675. g_thread_id = 0;
  676. gpu_right_acquired = false;
  677. first_initialization = true;
  678. }
  679. GSP_GPU::~GSP_GPU() {
  680. g_interrupt_event = nullptr;
  681. g_shared_memory = nullptr;
  682. gpu_right_acquired = false;
  683. }
  684. } // namespace GSP
  685. } // namespace Service