gsp.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include "common/log.h"
  5. #include "common/bit_field.h"
  6. #include "core/mem_map.h"
  7. #include "core/hle/hle.h"
  8. #include "core/hle/kernel/event.h"
  9. #include "core/hle/kernel/shared_memory.h"
  10. #include "core/hle/service/gsp.h"
  11. #include "core/hw/gpu.h"
  12. #include "video_core/gpu_debugger.h"
  13. // Main graphics debugger object - TODO: Here is probably not the best place for this
  14. GraphicsDebugger g_debugger;
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////
  16. // Namespace GSP_GPU
  17. namespace GSP_GPU {
  18. Handle g_interrupt_event = 0; ///< Handle to event triggered when GSP interrupt has been signalled
  19. Handle g_shared_memory = 0; ///< Handle to GSP shared memorys
  20. u32 g_thread_id = 1; ///< Thread index into interrupt relay queue, 1 is arbitrary
  21. /// Gets a pointer to a thread command buffer in GSP shared memory
  22. static inline u8* GetCommandBuffer(u32 thread_id) {
  23. if (0 == g_shared_memory)
  24. return nullptr;
  25. return Kernel::GetSharedMemoryPointer(g_shared_memory,
  26. 0x800 + (thread_id * sizeof(CommandBuffer)));
  27. }
  28. /// Gets a pointer to the interrupt relay queue for a given thread index
  29. static inline InterruptRelayQueue* GetInterruptRelayQueue(u32 thread_id) {
  30. return (InterruptRelayQueue*)Kernel::GetSharedMemoryPointer(g_shared_memory,
  31. sizeof(InterruptRelayQueue) * thread_id);
  32. }
  33. void WriteHWRegs(u32 base_address, u32 size_in_bytes, const u32* data) {
  34. // TODO: Return proper error codes
  35. if (base_address + size_in_bytes >= 0x420000) {
  36. ERROR_LOG(GPU, "Write address out of range! (address=0x%08x, size=0x%08x)",
  37. base_address, size_in_bytes);
  38. return;
  39. }
  40. // size should be word-aligned
  41. if ((size_in_bytes % 4) != 0) {
  42. ERROR_LOG(GPU, "Invalid size 0x%08x", size_in_bytes);
  43. return;
  44. }
  45. while (size_in_bytes > 0) {
  46. GPU::Write<u32>(base_address + 0x1EB00000, *data);
  47. size_in_bytes -= 4;
  48. ++data;
  49. base_address += 4;
  50. }
  51. }
  52. /// Write a GSP GPU hardware register
  53. void WriteHWRegs(Service::Interface* self) {
  54. u32* cmd_buff = Service::GetCommandBuffer();
  55. u32 reg_addr = cmd_buff[1];
  56. u32 size = cmd_buff[2];
  57. u32* src = (u32*)Memory::GetPointer(cmd_buff[0x4]);
  58. WriteHWRegs(reg_addr, size, src);
  59. }
  60. /// Read a GSP GPU hardware register
  61. void ReadHWRegs(Service::Interface* self) {
  62. u32* cmd_buff = Service::GetCommandBuffer();
  63. u32 reg_addr = cmd_buff[1];
  64. u32 size = cmd_buff[2];
  65. // TODO: Return proper error codes
  66. if (reg_addr + size >= 0x420000) {
  67. ERROR_LOG(GPU, "Read address out of range! (address=0x%08x, size=0x%08x)", reg_addr, size);
  68. return;
  69. }
  70. // size should be word-aligned
  71. if ((size % 4) != 0) {
  72. ERROR_LOG(GPU, "Invalid size 0x%08x", size);
  73. return;
  74. }
  75. u32* dst = (u32*)Memory::GetPointer(cmd_buff[0x41]);
  76. while (size > 0) {
  77. GPU::Read<u32>(*dst, reg_addr + 0x1EB00000);
  78. size -= 4;
  79. ++dst;
  80. reg_addr += 4;
  81. }
  82. }
  83. void SetBufferSwap(u32 screen_id, const FrameBufferInfo& info) {
  84. u32 base_address = 0x400000;
  85. if (info.active_fb == 0) {
  86. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].address_left1), 4, &info.address_left);
  87. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].address_right1), 4, &info.address_right);
  88. } else {
  89. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].address_left2), 4, &info.address_left);
  90. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].address_right2), 4, &info.address_right);
  91. }
  92. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].stride), 4, &info.stride);
  93. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].color_format), 4, &info.format);
  94. WriteHWRegs(base_address + 4 * GPU_REG_INDEX(framebuffer_config[screen_id].active_fb), 4, &info.shown_fb);
  95. }
  96. /**
  97. * GSP_GPU::SetBufferSwap service function
  98. *
  99. * Updates GPU display framebuffer configuration using the specified parameters.
  100. *
  101. * Inputs:
  102. * 1 : Screen ID (0 = top screen, 1 = bottom screen)
  103. * 2-7 : FrameBufferInfo structure
  104. * Outputs:
  105. * 1: Result code
  106. */
  107. void SetBufferSwap(Service::Interface* self) {
  108. u32* cmd_buff = Service::GetCommandBuffer();
  109. u32 screen_id = cmd_buff[1];
  110. FrameBufferInfo* fb_info = (FrameBufferInfo*)&cmd_buff[2];
  111. SetBufferSwap(screen_id, *fb_info);
  112. cmd_buff[1] = 0; // No error
  113. }
  114. /**
  115. * GSP_GPU::RegisterInterruptRelayQueue service function
  116. * Inputs:
  117. * 1 : "Flags" field, purpose is unknown
  118. * 3 : Handle to GSP synchronization event
  119. * Outputs:
  120. * 0 : Result of function, 0 on success, otherwise error code
  121. * 2 : Thread index into GSP command buffer
  122. * 4 : Handle to GSP shared memory
  123. */
  124. void RegisterInterruptRelayQueue(Service::Interface* self) {
  125. u32* cmd_buff = Service::GetCommandBuffer();
  126. u32 flags = cmd_buff[1];
  127. g_interrupt_event = cmd_buff[3];
  128. g_shared_memory = Kernel::CreateSharedMemory("GSPSharedMem");
  129. _assert_msg_(GSP, (g_interrupt_event != 0), "handle is not valid!");
  130. cmd_buff[2] = g_thread_id++; // ThreadID
  131. cmd_buff[4] = g_shared_memory; // GSP shared memory
  132. Kernel::SignalEvent(g_interrupt_event); // TODO(bunnei): Is this correct?
  133. }
  134. /**
  135. * Signals that the specified interrupt type has occurred to userland code
  136. * @param interrupt_id ID of interrupt that is being signalled
  137. */
  138. void SignalInterrupt(InterruptId interrupt_id) {
  139. if (0 == g_interrupt_event) {
  140. WARN_LOG(GSP, "cannot synchronize until GSP event has been created!");
  141. return;
  142. }
  143. if (0 == g_shared_memory) {
  144. WARN_LOG(GSP, "cannot synchronize until GSP shared memory has been created!");
  145. return;
  146. }
  147. for (int thread_id = 0; thread_id < 0x4; ++thread_id) {
  148. InterruptRelayQueue* interrupt_relay_queue = GetInterruptRelayQueue(thread_id);
  149. interrupt_relay_queue->number_interrupts = interrupt_relay_queue->number_interrupts + 1;
  150. u8 next = interrupt_relay_queue->index;
  151. next += interrupt_relay_queue->number_interrupts;
  152. next = next % 0x34; // 0x34 is the number of interrupt slots
  153. interrupt_relay_queue->slot[next] = interrupt_id;
  154. interrupt_relay_queue->error_code = 0x0; // No error
  155. }
  156. Kernel::SignalEvent(g_interrupt_event);
  157. }
  158. /// Executes the next GSP command
  159. void ExecuteCommand(const Command& command) {
  160. // Utility function to convert register ID to address
  161. auto WriteGPURegister = [](u32 id, u32 data) {
  162. GPU::Write<u32>(0x1EF00000 + 4 * id, data);
  163. };
  164. switch (command.id) {
  165. // GX request DMA - typically used for copying memory from GSP heap to VRAM
  166. case CommandId::REQUEST_DMA:
  167. memcpy(Memory::GetPointer(command.dma_request.dest_address),
  168. Memory::GetPointer(command.dma_request.source_address),
  169. command.dma_request.size);
  170. break;
  171. // ctrulib homebrew sends all relevant command list data with this command,
  172. // hence we do all "interesting" stuff here and do nothing in SET_COMMAND_LIST_FIRST.
  173. // TODO: This will need some rework in the future.
  174. case CommandId::SET_COMMAND_LIST_LAST:
  175. {
  176. auto& params = command.set_command_list_last;
  177. WriteGPURegister(GPU_REG_INDEX(command_processor_config.address), Memory::VirtualToPhysicalAddress(params.address) >> 3);
  178. WriteGPURegister(GPU_REG_INDEX(command_processor_config.size), params.size >> 3);
  179. // TODO: Not sure if we are supposed to always write this .. seems to trigger processing though
  180. WriteGPURegister(GPU_REG_INDEX(command_processor_config.trigger), 1);
  181. // TODO: Move this to GPU
  182. // TODO: Not sure what units the size is measured in
  183. g_debugger.CommandListCalled(params.address,
  184. (u32*)Memory::GetPointer(params.address),
  185. params.size);
  186. SignalInterrupt(InterruptId::P3D);
  187. break;
  188. }
  189. // It's assumed that the two "blocks" behave equivalently.
  190. // Presumably this is done simply to allow two memory fills to run in parallel.
  191. case CommandId::SET_MEMORY_FILL:
  192. {
  193. auto& params = command.memory_fill;
  194. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[0].address_start), Memory::VirtualToPhysicalAddress(params.start1) >> 3);
  195. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[0].address_end), Memory::VirtualToPhysicalAddress(params.end1) >> 3);
  196. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[0].size), params.end1 - params.start1);
  197. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[0].value), params.value1);
  198. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[1].address_start), Memory::VirtualToPhysicalAddress(params.start2) >> 3);
  199. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[1].address_end), Memory::VirtualToPhysicalAddress(params.end2) >> 3);
  200. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[1].size), params.end2 - params.start2);
  201. WriteGPURegister(GPU_REG_INDEX(memory_fill_config[1].value), params.value2);
  202. break;
  203. }
  204. case CommandId::SET_DISPLAY_TRANSFER:
  205. {
  206. auto& params = command.image_copy;
  207. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.input_address), Memory::VirtualToPhysicalAddress(params.in_buffer_address) >> 3);
  208. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.output_address), Memory::VirtualToPhysicalAddress(params.out_buffer_address) >> 3);
  209. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.input_size), params.in_buffer_size);
  210. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.output_size), params.out_buffer_size);
  211. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.flags), params.flags);
  212. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.trigger), 1);
  213. // TODO(bunnei): Signalling all of these interrupts here is totally wrong, but it seems to
  214. // work well enough for running demos. Need to figure out how these all work and trigger
  215. // them correctly.
  216. SignalInterrupt(InterruptId::PSC0);
  217. SignalInterrupt(InterruptId::PSC1);
  218. SignalInterrupt(InterruptId::PPF);
  219. SignalInterrupt(InterruptId::P3D);
  220. SignalInterrupt(InterruptId::DMA);
  221. break;
  222. }
  223. // TODO: Check if texture copies are implemented correctly..
  224. case CommandId::SET_TEXTURE_COPY:
  225. {
  226. auto& params = command.image_copy;
  227. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.input_address), Memory::VirtualToPhysicalAddress(params.in_buffer_address) >> 3);
  228. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.output_address), Memory::VirtualToPhysicalAddress(params.out_buffer_address) >> 3);
  229. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.input_size), params.in_buffer_size);
  230. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.output_size), params.out_buffer_size);
  231. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.flags), params.flags);
  232. // TODO: Should this register be set to 1 or should instead its value be OR-ed with 1?
  233. WriteGPURegister(GPU_REG_INDEX(display_transfer_config.trigger), 1);
  234. break;
  235. }
  236. // TODO: Figure out what exactly SET_COMMAND_LIST_FIRST and SET_COMMAND_LIST_LAST
  237. // are supposed to do.
  238. case CommandId::SET_COMMAND_LIST_FIRST:
  239. {
  240. break;
  241. }
  242. default:
  243. ERROR_LOG(GSP, "unknown command 0x%08X", (int)command.id.Value());
  244. }
  245. }
  246. /// This triggers handling of the GX command written to the command buffer in shared memory.
  247. void TriggerCmdReqQueue(Service::Interface* self) {
  248. // Iterate through each thread's command queue...
  249. for (unsigned thread_id = 0; thread_id < 0x4; ++thread_id) {
  250. CommandBuffer* command_buffer = (CommandBuffer*)GetCommandBuffer(thread_id);
  251. // Iterate through each command...
  252. for (unsigned i = 0; i < command_buffer->number_commands; ++i) {
  253. g_debugger.GXCommandProcessed((u8*)&command_buffer->commands[i]);
  254. // Decode and execute command
  255. ExecuteCommand(command_buffer->commands[i]);
  256. // Indicates that command has completed
  257. command_buffer->number_commands = command_buffer->number_commands - 1;
  258. }
  259. }
  260. }
  261. const Interface::FunctionInfo FunctionTable[] = {
  262. {0x00010082, WriteHWRegs, "WriteHWRegs"},
  263. {0x00020084, nullptr, "WriteHWRegsWithMask"},
  264. {0x00030082, nullptr, "WriteHWRegRepeat"},
  265. {0x00040080, ReadHWRegs, "ReadHWRegs"},
  266. {0x00050200, SetBufferSwap, "SetBufferSwap"},
  267. {0x00060082, nullptr, "SetCommandList"},
  268. {0x000700C2, nullptr, "RequestDma"},
  269. {0x00080082, nullptr, "FlushDataCache"},
  270. {0x00090082, nullptr, "InvalidateDataCache"},
  271. {0x000A0044, nullptr, "RegisterInterruptEvents"},
  272. {0x000B0040, nullptr, "SetLcdForceBlack"},
  273. {0x000C0000, TriggerCmdReqQueue, "TriggerCmdReqQueue"},
  274. {0x000D0140, nullptr, "SetDisplayTransfer"},
  275. {0x000E0180, nullptr, "SetTextureCopy"},
  276. {0x000F0200, nullptr, "SetMemoryFill"},
  277. {0x00100040, nullptr, "SetAxiConfigQoSMode"},
  278. {0x00110040, nullptr, "SetPerfLogMode"},
  279. {0x00120000, nullptr, "GetPerfLog"},
  280. {0x00130042, RegisterInterruptRelayQueue, "RegisterInterruptRelayQueue"},
  281. {0x00140000, nullptr, "UnregisterInterruptRelayQueue"},
  282. {0x00150002, nullptr, "TryAcquireRight"},
  283. {0x00160042, nullptr, "AcquireRight"},
  284. {0x00170000, nullptr, "ReleaseRight"},
  285. {0x00180000, nullptr, "ImportDisplayCaptureInfo"},
  286. {0x00190000, nullptr, "SaveVramSysArea"},
  287. {0x001A0000, nullptr, "RestoreVramSysArea"},
  288. {0x001B0000, nullptr, "ResetGpuCore"},
  289. {0x001C0040, nullptr, "SetLedForceOff"},
  290. {0x001D0040, nullptr, "SetTestCommand"},
  291. {0x001E0080, nullptr, "SetInternalPriorities"},
  292. };
  293. ////////////////////////////////////////////////////////////////////////////////////////////////////
  294. // Interface class
  295. Interface::Interface() {
  296. Register(FunctionTable, ARRAY_SIZE(FunctionTable));
  297. g_interrupt_event = 0;
  298. g_shared_memory = 0;
  299. g_thread_id = 1;
  300. }
  301. Interface::~Interface() {
  302. }
  303. } // namespace