gsp_gpu.cpp 27 KB

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