gsp_gpu.cpp 16 KB

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