gsp_gpu.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 "core/mem_map.h"
  6. #include "core/hle/kernel/event.h"
  7. #include "core/hle/kernel/shared_memory.h"
  8. #include "gsp_gpu.h"
  9. #include "core/hw/gpu.h"
  10. #include "video_core/gpu_debugger.h"
  11. // Main graphics debugger object - TODO: Here is probably not the best place for this
  12. GraphicsDebugger g_debugger;
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // Namespace GSP_GPU
  15. namespace GSP_GPU {
  16. /// Event triggered when GSP interrupt has been signalled
  17. Kernel::SharedPtr<Kernel::Event> g_interrupt_event;
  18. /// GSP shared memoryings
  19. Kernel::SharedPtr<Kernel::SharedMemory> g_shared_memory;
  20. /// Thread index into interrupt relay queue, 1 is arbitrary
  21. u32 g_thread_id = 1;
  22. /// Gets a pointer to a thread command buffer in GSP shared memory
  23. static inline u8* GetCommandBuffer(u32 thread_id) {
  24. ResultVal<u8*> ptr = g_shared_memory->GetPointer(0x800 + (thread_id * sizeof(CommandBuffer)));
  25. return ptr.ValueOr(nullptr);
  26. }
  27. static inline FrameBufferUpdate* GetFrameBufferInfo(u32 thread_id, u32 screen_index) {
  28. DEBUG_ASSERT_MSG(screen_index < 2, "Invalid screen index");
  29. // For each thread there are two FrameBufferUpdate fields
  30. u32 offset = 0x200 + (2 * thread_id + screen_index) * sizeof(FrameBufferUpdate);
  31. ResultVal<u8*> ptr = g_shared_memory->GetPointer(offset);
  32. return reinterpret_cast<FrameBufferUpdate*>(ptr.ValueOr(nullptr));
  33. }
  34. /// Gets a pointer to the interrupt relay queue for a given thread index
  35. static inline InterruptRelayQueue* GetInterruptRelayQueue(u32 thread_id) {
  36. ResultVal<u8*> ptr = g_shared_memory->GetPointer(sizeof(InterruptRelayQueue) * thread_id);
  37. return reinterpret_cast<InterruptRelayQueue*>(ptr.ValueOr(nullptr));
  38. }
  39. /**
  40. * Checks if the parameters in a register write call are valid and logs in the case that
  41. * they are not
  42. * @param base_address The first address in the sequence of registers that will be written
  43. * @param size_in_bytes The number of registers that will be written
  44. * @return true if the parameters are valid, false otherwise
  45. */
  46. static bool CheckWriteParameters(u32 base_address, u32 size_in_bytes) {
  47. // TODO: Return proper error codes
  48. if (base_address + size_in_bytes >= 0x420000) {
  49. LOG_ERROR(Service_GSP, "Write address out of range! (address=0x%08x, size=0x%08x)",
  50. base_address, size_in_bytes);
  51. return false;
  52. }
  53. // size should be word-aligned
  54. if ((size_in_bytes % 4) != 0) {
  55. LOG_ERROR(Service_GSP, "Invalid size 0x%08x", size_in_bytes);
  56. return false;
  57. }
  58. return true;
  59. }
  60. /**
  61. * Writes sequential GSP GPU hardware registers using an array of source data
  62. *
  63. * @param base_address The address of the first register in the sequence
  64. * @param size_in_bytes The number of registers to update (size of data)
  65. * @param data A pointer to the source data
  66. */
  67. static void WriteHWRegs(u32 base_address, u32 size_in_bytes, const u32* data) {
  68. // TODO: Return proper error codes
  69. if (!CheckWriteParameters(base_address, size_in_bytes))
  70. return;
  71. while (size_in_bytes > 0) {
  72. GPU::Write<u32>(base_address + 0x1EB00000, *data);
  73. size_in_bytes -= 4;
  74. ++data;
  75. base_address += 4;
  76. }
  77. }
  78. /**
  79. * GSP_GPU::WriteHWRegs service function
  80. *
  81. * Writes sequential GSP GPU hardware registers
  82. *
  83. * Inputs:
  84. * 1 : address of first GPU register
  85. * 2 : number of registers to write sequentially
  86. * 4 : pointer to source data array
  87. */
  88. static void WriteHWRegs(Service::Interface* self) {
  89. u32* cmd_buff = Kernel::GetCommandBuffer();
  90. u32 reg_addr = cmd_buff[1];
  91. u32 size = cmd_buff[2];
  92. u32* src = (u32*)Memory::GetPointer(cmd_buff[4]);
  93. WriteHWRegs(reg_addr, size, src);
  94. }
  95. /**
  96. * Updates sequential GSP GPU hardware registers using parallel arrays of source data and masks.
  97. * For each register, the value is updated only where the mask is high
  98. *
  99. * @param base_address The address of the first register in the sequence
  100. * @param size_in_bytes The number of registers to update (size of data)
  101. * @param data A pointer to the source data to use for updates
  102. * @param masks A pointer to the masks
  103. */
  104. static void WriteHWRegsWithMask(u32 base_address, u32 size_in_bytes, const u32* data, const u32* masks) {
  105. // TODO: Return proper error codes
  106. if (!CheckWriteParameters(base_address, size_in_bytes))
  107. return;
  108. while (size_in_bytes > 0) {
  109. const u32 reg_address = base_address + 0x1EB00000;
  110. u32 reg_value;
  111. GPU::Read<u32>(reg_value, reg_address);
  112. // Update the current value of the register only for set mask bits
  113. reg_value = (reg_value & ~*masks) | (*data | *masks);
  114. GPU::Write<u32>(reg_address, reg_value);
  115. size_in_bytes -= 4;
  116. ++data;
  117. ++masks;
  118. base_address += 4;
  119. }
  120. }
  121. /**
  122. * GSP_GPU::WriteHWRegsWithMask service function
  123. *
  124. * Updates sequential GSP GPU hardware registers using masks
  125. *
  126. * Inputs:
  127. * 1 : address of first GPU register
  128. * 2 : number of registers to update sequentially
  129. * 4 : pointer to source data array
  130. * 6 : pointer to mask array
  131. */
  132. static void WriteHWRegsWithMask(Service::Interface* self) {
  133. u32* cmd_buff = Kernel::GetCommandBuffer();
  134. u32 reg_addr = cmd_buff[1];
  135. u32 size = cmd_buff[2];
  136. u32* src_data = (u32*)Memory::GetPointer(cmd_buff[4]);
  137. u32* mask_data = (u32*)Memory::GetPointer(cmd_buff[6]);
  138. WriteHWRegsWithMask(reg_addr, size, src_data, mask_data);
  139. }
  140. /// Read a GSP GPU hardware register
  141. static void ReadHWRegs(Service::Interface* self) {
  142. u32* cmd_buff = Kernel::GetCommandBuffer();
  143. u32 reg_addr = cmd_buff[1];
  144. u32 size = cmd_buff[2];
  145. // TODO: Return proper error codes
  146. if (reg_addr + size >= 0x420000) {
  147. LOG_ERROR(Service_GSP, "Read address out of range! (address=0x%08x, size=0x%08x)", reg_addr, size);
  148. return;
  149. }
  150. // size should be word-aligned
  151. if ((size % 4) != 0) {
  152. LOG_ERROR(Service_GSP, "Invalid size 0x%08x", size);
  153. return;
  154. }
  155. u32* dst = (u32*)Memory::GetPointer(cmd_buff[0x41]);
  156. while (size > 0) {
  157. GPU::Read<u32>(*dst, reg_addr + 0x1EB00000);
  158. size -= 4;
  159. ++dst;
  160. reg_addr += 4;
  161. }
  162. }
  163. static void SetBufferSwap(u32 screen_id, const FrameBufferInfo& info) {
  164. u32 base_address = 0x400000;
  165. if (info.active_fb == 0) {
  166. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].address_left1), 4, &info.address_left);
  167. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].address_right1), 4, &info.address_right);
  168. } else {
  169. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].address_left2), 4, &info.address_left);
  170. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].address_right2), 4, &info.address_right);
  171. }
  172. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].stride), 4, &info.stride);
  173. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].color_format), 4, &info.format);
  174. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].active_fb), 4, &info.shown_fb);
  175. }
  176. /**
  177. * GSP_GPU::SetBufferSwap service function
  178. *
  179. * Updates GPU display framebuffer configuration using the specified parameters.
  180. *
  181. * Inputs:
  182. * 1 : Screen ID (0 = top screen, 1 = bottom screen)
  183. * 2-7 : FrameBufferInfo structure
  184. * Outputs:
  185. * 1: Result code
  186. */
  187. static void SetBufferSwap(Service::Interface* self) {
  188. u32* cmd_buff = Kernel::GetCommandBuffer();
  189. u32 screen_id = cmd_buff[1];
  190. FrameBufferInfo* fb_info = (FrameBufferInfo*)&cmd_buff[2];
  191. SetBufferSwap(screen_id, *fb_info);
  192. cmd_buff[1] = 0; // No error
  193. }
  194. /**
  195. * GSP_GPU::FlushDataCache service function
  196. *
  197. * This Function is a no-op, We aren't emulating the CPU cache any time soon.
  198. *
  199. * Inputs:
  200. * 1 : Address
  201. * 2 : Size
  202. * 3 : Value 0, some descriptor for the KProcess Handle
  203. * 4 : KProcess handle
  204. * Outputs:
  205. * 1 : Result of function, 0 on success, otherwise error code
  206. */
  207. static void FlushDataCache(Service::Interface* self) {
  208. u32* cmd_buff = Kernel::GetCommandBuffer();
  209. u32 address = cmd_buff[1];
  210. u32 size = cmd_buff[2];
  211. u32 process = cmd_buff[4];
  212. // TODO(purpasmart96): Verify return header on HW
  213. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  214. }
  215. /**
  216. * GSP_GPU::RegisterInterruptRelayQueue service function
  217. * Inputs:
  218. * 1 : "Flags" field, purpose is unknown
  219. * 3 : Handle to GSP synchronization event
  220. * Outputs:
  221. * 0 : Result of function, 0 on success, otherwise error code
  222. * 2 : Thread index into GSP command buffer
  223. * 4 : Handle to GSP shared memory
  224. */
  225. static void RegisterInterruptRelayQueue(Service::Interface* self) {
  226. u32* cmd_buff = Kernel::GetCommandBuffer();
  227. u32 flags = cmd_buff[1];
  228. g_interrupt_event = Kernel::g_handle_table.Get<Kernel::Event>(cmd_buff[3]);
  229. ASSERT_MSG((g_interrupt_event != nullptr), "handle is not valid!");
  230. g_shared_memory = Kernel::SharedMemory::Create("GSPSharedMem");
  231. Handle shmem_handle = Kernel::g_handle_table.Create(g_shared_memory).MoveFrom();
  232. cmd_buff[1] = 0x2A07; // Value verified by 3dmoo team, purpose unknown, but needed for GSP init
  233. cmd_buff[2] = g_thread_id++; // Thread ID
  234. cmd_buff[4] = shmem_handle; // GSP shared memory
  235. g_interrupt_event->Signal(); // TODO(bunnei): Is this correct?
  236. }
  237. /**
  238. * Signals that the specified interrupt type has occurred to userland code
  239. * @param interrupt_id ID of interrupt that is being signalled
  240. * @todo This should probably take a thread_id parameter and only signal this thread?
  241. * @todo This probably does not belong in the GSP module, instead move to video_core
  242. */
  243. void SignalInterrupt(InterruptId interrupt_id) {
  244. if (0 == g_interrupt_event) {
  245. LOG_WARNING(Service_GSP, "cannot synchronize until GSP event has been created!");
  246. return;
  247. }
  248. if (nullptr == g_shared_memory) {
  249. LOG_WARNING(Service_GSP, "cannot synchronize until GSP shared memory has been created!");
  250. return;
  251. }
  252. for (int thread_id = 0; thread_id < 0x4; ++thread_id) {
  253. InterruptRelayQueue* interrupt_relay_queue = GetInterruptRelayQueue(thread_id);
  254. u8 next = interrupt_relay_queue->index;
  255. next += interrupt_relay_queue->number_interrupts;
  256. next = next % 0x34; // 0x34 is the number of interrupt slots
  257. interrupt_relay_queue->number_interrupts += 1;
  258. interrupt_relay_queue->slot[next] = interrupt_id;
  259. interrupt_relay_queue->error_code = 0x0; // No error
  260. // Update framebuffer information if requested
  261. // TODO(yuriks): Confirm where this code should be called. It is definitely updated without
  262. // executing any GSP commands, only waiting on the event.
  263. int screen_id = (interrupt_id == InterruptId::PDC0) ? 0 : (interrupt_id == InterruptId::PDC1) ? 1 : -1;
  264. if (screen_id != -1) {
  265. FrameBufferUpdate* info = GetFrameBufferInfo(thread_id, screen_id);
  266. if (info->is_dirty) {
  267. SetBufferSwap(screen_id, info->framebuffer_info[info->index]);
  268. info->is_dirty = false;
  269. }
  270. }
  271. }
  272. g_interrupt_event->Signal();
  273. }
  274. /// Executes the next GSP command
  275. static void ExecuteCommand(const Command& command, u32 thread_id) {
  276. // Utility function to convert register ID to address
  277. auto WriteGPURegister = [](u32 id, u32 data) {
  278. GPU::Write<u32>(0x1EF00000 + 4 * id, data);
  279. };
  280. switch (command.id) {
  281. // GX request DMA - typically used for copying memory from GSP heap to VRAM
  282. case CommandId::REQUEST_DMA:
  283. memcpy(Memory::GetPointer(command.dma_request.dest_address),
  284. Memory::GetPointer(command.dma_request.source_address),
  285. command.dma_request.size);
  286. SignalInterrupt(InterruptId::DMA);
  287. break;
  288. // ctrulib homebrew sends all relevant command list data with this command,
  289. // hence we do all "interesting" stuff here and do nothing in SET_COMMAND_LIST_FIRST.
  290. // TODO: This will need some rework in the future.
  291. case CommandId::SET_COMMAND_LIST_LAST:
  292. {
  293. auto& params = command.set_command_list_last;
  294. WriteGPURegister(GPU_REG_INDEX(command_processor_config.address), Memory::VirtualToPhysicalAddress(params.address) >> 3);
  295. WriteGPURegister(GPU_REG_INDEX(command_processor_config.size), params.size);
  296. // TODO: Not sure if we are supposed to always write this .. seems to trigger processing though
  297. WriteGPURegister(GPU_REG_INDEX(command_processor_config.trigger), 1);
  298. break;
  299. }
  300. // It's assumed that the two "blocks" behave equivalently.
  301. // Presumably this is done simply to allow two memory fills to run in parallel.
  302. case CommandId::SET_MEMORY_FILL:
  303. {
  304. auto& params = command.memory_fill;
  305. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[0].address_start), Memory::VirtualToPhysicalAddress(params.start1) >> 3);
  306. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[0].address_end), Memory::VirtualToPhysicalAddress(params.end1) >> 3);
  307. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[0].size), params.end1 - params.start1);
  308. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[0].value), params.value1);
  309. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[1].address_start), Memory::VirtualToPhysicalAddress(params.start2) >> 3);
  310. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[1].address_end), Memory::VirtualToPhysicalAddress(params.end2) >> 3);
  311. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[1].size), params.end2 - params.start2);
  312. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[1].value), params.value2);
  313. break;
  314. }
  315. case CommandId::SET_DISPLAY_TRANSFER:
  316. {
  317. auto& params = command.image_copy;
  318. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.input_address), Memory::VirtualToPhysicalAddress(params.in_buffer_address) >> 3);
  319. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.output_address), Memory::VirtualToPhysicalAddress(params.out_buffer_address) >> 3);
  320. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.input_size), params.in_buffer_size);
  321. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.output_size), params.out_buffer_size);
  322. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.flags), params.flags);
  323. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.trigger), 1);
  324. break;
  325. }
  326. // TODO: Check if texture copies are implemented correctly..
  327. case CommandId::SET_TEXTURE_COPY:
  328. {
  329. auto& params = command.image_copy;
  330. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.input_address), Memory::VirtualToPhysicalAddress(params.in_buffer_address) >> 3);
  331. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.output_address), Memory::VirtualToPhysicalAddress(params.out_buffer_address) >> 3);
  332. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.input_size), params.in_buffer_size);
  333. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.output_size), params.out_buffer_size);
  334. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.flags), params.flags);
  335. // TODO: Should this register be set to 1 or should instead its value be OR-ed with 1?
  336. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.trigger), 1);
  337. break;
  338. }
  339. // TODO: Figure out what exactly SET_COMMAND_LIST_FIRST and SET_COMMAND_LIST_LAST
  340. // are supposed to do.
  341. case CommandId::SET_COMMAND_LIST_FIRST:
  342. {
  343. break;
  344. }
  345. default:
  346. LOG_ERROR(Service_GSP, "unknown command 0x%08X", (int)command.id.Value());
  347. }
  348. }
  349. /// This triggers handling of the GX command written to the command buffer in shared memory.
  350. static void TriggerCmdReqQueue(Service::Interface* self) {
  351. // Iterate through each thread's command queue...
  352. for (unsigned thread_id = 0; thread_id < 0x4; ++thread_id) {
  353. CommandBuffer* command_buffer = (CommandBuffer*)GetCommandBuffer(thread_id);
  354. // Iterate through each command...
  355. for (unsigned i = 0; i < command_buffer->number_commands; ++i) {
  356. g_debugger.GXCommandProcessed((u8*)&command_buffer->commands[i]);
  357. // Decode and execute command
  358. ExecuteCommand(command_buffer->commands[i], thread_id);
  359. // Indicates that command has completed
  360. command_buffer->number_commands = command_buffer->number_commands - 1;
  361. }
  362. }
  363. u32* cmd_buff = Kernel::GetCommandBuffer();
  364. cmd_buff[1] = 0; // No error
  365. }
  366. const Interface::FunctionInfo FunctionTable[] = {
  367. {0x00010082, WriteHWRegs, "WriteHWRegs"},
  368. {0x00020084, WriteHWRegsWithMask, "WriteHWRegsWithMask"},
  369. {0x00030082, nullptr, "WriteHWRegRepeat"},
  370. {0x00040080, ReadHWRegs, "ReadHWRegs"},
  371. {0x00050200, SetBufferSwap, "SetBufferSwap"},
  372. {0x00060082, nullptr, "SetCommandList"},
  373. {0x000700C2, nullptr, "RequestDma"},
  374. {0x00080082, FlushDataCache, "FlushDataCache"},
  375. {0x00090082, nullptr, "InvalidateDataCache"},
  376. {0x000A0044, nullptr, "RegisterInterruptEvents"},
  377. {0x000B0040, nullptr, "SetLcdForceBlack"},
  378. {0x000C0000, TriggerCmdReqQueue, "TriggerCmdReqQueue"},
  379. {0x000D0140, nullptr, "SetDisplayTransfer"},
  380. {0x000E0180, nullptr, "SetTextureCopy"},
  381. {0x000F0200, nullptr, "SetMemoryFill"},
  382. {0x00100040, nullptr, "SetAxiConfigQoSMode"},
  383. {0x00110040, nullptr, "SetPerfLogMode"},
  384. {0x00120000, nullptr, "GetPerfLog"},
  385. {0x00130042, RegisterInterruptRelayQueue, "RegisterInterruptRelayQueue"},
  386. {0x00140000, nullptr, "UnregisterInterruptRelayQueue"},
  387. {0x00150002, nullptr, "TryAcquireRight"},
  388. {0x00160042, nullptr, "AcquireRight"},
  389. {0x00170000, nullptr, "ReleaseRight"},
  390. {0x00180000, nullptr, "ImportDisplayCaptureInfo"},
  391. {0x00190000, nullptr, "SaveVramSysArea"},
  392. {0x001A0000, nullptr, "RestoreVramSysArea"},
  393. {0x001B0000, nullptr, "ResetGpuCore"},
  394. {0x001C0040, nullptr, "SetLedForceOff"},
  395. {0x001D0040, nullptr, "SetTestCommand"},
  396. {0x001E0080, nullptr, "SetInternalPriorities"},
  397. {0x001F0082, nullptr, "StoreDataCache"},
  398. };
  399. ////////////////////////////////////////////////////////////////////////////////////////////////////
  400. // Interface class
  401. Interface::Interface() {
  402. Register(FunctionTable);
  403. g_interrupt_event = 0;
  404. g_shared_memory = 0;
  405. g_thread_id = 1;
  406. }
  407. } // namespace