gsp_gpu.cpp 28 KB

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