gsp_gpu.cpp 30 KB

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