gsp_gpu.cpp 29 KB

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