gsp.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/service/gsp.h"
  10. #include "core/hw/lcd.h"
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////
  12. /// GSP shared memory GX command buffer header
  13. union GX_CmdBufferHeader {
  14. u32 hex;
  15. // Current command index. This index is updated by GSP module after loading the command data,
  16. // right before the command is processed. When this index is updated by GSP module, the total
  17. // commands field is decreased by one as well.
  18. BitField<0,8,u32> index;
  19. // Total commands to process, must not be value 0 when GSP module handles commands. This must be
  20. // <=15 when writing a command to shared memory. This is incremented by the application when
  21. // writing a command to shared memory, after increasing this value TriggerCmdReqQueue is only
  22. // used if this field is value 1.
  23. BitField<8,8,u32> number_commands;
  24. };
  25. /// Gets the address of the start (header) of a command buffer in GSP shared memory
  26. static inline u32 GX_GetCmdBufferAddress(u32 thread_id) {
  27. return (0x10002000 + 0x800 + (thread_id * 0x200));
  28. }
  29. /// Gets a pointer to the start (header) of a command buffer in GSP shared memory
  30. static inline u8* GX_GetCmdBufferPointer(u32 thread_id, u32 offset=0) {
  31. return Memory::GetPointer(GX_GetCmdBufferAddress(thread_id) + offset);
  32. }
  33. /// Finishes execution of a GSP command
  34. void GX_FinishCommand(u32 thread_id) {
  35. GX_CmdBufferHeader* header = (GX_CmdBufferHeader*)GX_GetCmdBufferPointer(thread_id);
  36. header->number_commands = header->number_commands - 1;
  37. }
  38. ////////////////////////////////////////////////////////////////////////////////////////////////////
  39. // Namespace GSP_GPU
  40. namespace GSP_GPU {
  41. Handle g_event_handle = 0;
  42. u32 g_thread_id = 0;
  43. enum {
  44. CMD_GX_REQUEST_DMA = 0x00000000,
  45. };
  46. enum {
  47. REG_FRAMEBUFFER_1 = 0x00400468,
  48. REG_FRAMEBUFFER_2 = 0x00400494,
  49. };
  50. /// Read a GSP GPU hardware register
  51. void ReadHWRegs(Service::Interface* self) {
  52. static const u32 framebuffer_1[] = {LCD::PADDR_VRAM_TOP_LEFT_FRAME1, LCD::PADDR_VRAM_TOP_RIGHT_FRAME1};
  53. static const u32 framebuffer_2[] = {LCD::PADDR_VRAM_TOP_LEFT_FRAME2, LCD::PADDR_VRAM_TOP_RIGHT_FRAME2};
  54. u32* cmd_buff = Service::GetCommandBuffer();
  55. u32 reg_addr = cmd_buff[1];
  56. u32 size = cmd_buff[2];
  57. u32* dst = (u32*)Memory::GetPointer(cmd_buff[0x41]);
  58. switch (reg_addr) {
  59. // NOTE: Calling SetFramebufferLocation here is a hack... Not sure the correct way yet to set
  60. // whether the framebuffers should be in VRAM or GSP heap, but from what I understand, if the
  61. // user application is reading from either of these registers, then its going to be in VRAM.
  62. // Top framebuffer 1 addresses
  63. case REG_FRAMEBUFFER_1:
  64. LCD::SetFramebufferLocation(LCD::FRAMEBUFFER_LOCATION_VRAM);
  65. memcpy(dst, framebuffer_1, size);
  66. break;
  67. // Top framebuffer 2 addresses
  68. case REG_FRAMEBUFFER_2:
  69. LCD::SetFramebufferLocation(LCD::FRAMEBUFFER_LOCATION_VRAM);
  70. memcpy(dst, framebuffer_2, size);
  71. break;
  72. default:
  73. ERROR_LOG(GSP, "unknown register read at address %08X", reg_addr);
  74. }
  75. }
  76. void RegisterInterruptRelayQueue(Service::Interface* self) {
  77. u32* cmd_buff = Service::GetCommandBuffer();
  78. u32 flags = cmd_buff[1];
  79. u32 event_handle = cmd_buff[3];
  80. _assert_msg_(GSP, (event_handle != 0), "called, but event is nullptr!");
  81. g_event_handle = event_handle;
  82. Kernel::SetEventLocked(event_handle, false);
  83. // Hack - This function will permanently set the state of the GSP event such that GPU command
  84. // synchronization barriers always passthrough. Correct solution would be to set this after the
  85. // GPU as processed all queued up commands, but due to the emulator being single-threaded they
  86. // will always be ready.
  87. Kernel::SetPermanentLock(event_handle, true);
  88. cmd_buff[2] = g_thread_id; // ThreadID
  89. }
  90. /// This triggers handling of the GX command written to the command buffer in shared memory.
  91. void TriggerCmdReqQueue(Service::Interface* self) {
  92. GX_CmdBufferHeader* header = (GX_CmdBufferHeader*)GX_GetCmdBufferPointer(g_thread_id);
  93. u32* cmd_buff = (u32*)GX_GetCmdBufferPointer(g_thread_id, 0x20 + (header->index * 0x20));
  94. switch (cmd_buff[0]) {
  95. // GX request DMA - typically used for copying memory from GSP heap to VRAM
  96. case CMD_GX_REQUEST_DMA:
  97. memcpy(Memory::GetPointer(cmd_buff[2]), Memory::GetPointer(cmd_buff[1]), cmd_buff[3]);
  98. break;
  99. default:
  100. ERROR_LOG(GSP, "unknown command 0x%08X", cmd_buff[0]);
  101. }
  102. GX_FinishCommand(g_thread_id);
  103. }
  104. const Interface::FunctionInfo FunctionTable[] = {
  105. {0x00010082, nullptr, "WriteHWRegs"},
  106. {0x00020084, nullptr, "WriteHWRegsWithMask"},
  107. {0x00030082, nullptr, "WriteHWRegRepeat"},
  108. {0x00040080, ReadHWRegs, "ReadHWRegs"},
  109. {0x00050200, nullptr, "SetBufferSwap"},
  110. {0x00060082, nullptr, "SetCommandList"},
  111. {0x000700C2, nullptr, "RequestDma"},
  112. {0x00080082, nullptr, "FlushDataCache"},
  113. {0x00090082, nullptr, "InvalidateDataCache"},
  114. {0x000A0044, nullptr, "RegisterInterruptEvents"},
  115. {0x000B0040, nullptr, "SetLcdForceBlack"},
  116. {0x000C0000, TriggerCmdReqQueue, "TriggerCmdReqQueue"},
  117. {0x000D0140, nullptr, "SetDisplayTransfer"},
  118. {0x000E0180, nullptr, "SetTextureCopy"},
  119. {0x000F0200, nullptr, "SetMemoryFill"},
  120. {0x00100040, nullptr, "SetAxiConfigQoSMode"},
  121. {0x00110040, nullptr, "SetPerfLogMode"},
  122. {0x00120000, nullptr, "GetPerfLog"},
  123. {0x00130042, RegisterInterruptRelayQueue, "RegisterInterruptRelayQueue"},
  124. {0x00140000, nullptr, "UnregisterInterruptRelayQueue"},
  125. {0x00150002, nullptr, "TryAcquireRight"},
  126. {0x00160042, nullptr, "AcquireRight"},
  127. {0x00170000, nullptr, "ReleaseRight"},
  128. {0x00180000, nullptr, "ImportDisplayCaptureInfo"},
  129. {0x00190000, nullptr, "SaveVramSysArea"},
  130. {0x001A0000, nullptr, "RestoreVramSysArea"},
  131. {0x001B0000, nullptr, "ResetGpuCore"},
  132. {0x001C0040, nullptr, "SetLedForceOff"},
  133. {0x001D0040, nullptr, "SetTestCommand"},
  134. {0x001E0080, nullptr, "SetInternalPriorities"},
  135. };
  136. ////////////////////////////////////////////////////////////////////////////////////////////////////
  137. // Interface class
  138. Interface::Interface() {
  139. Register(FunctionTable, ARRAY_SIZE(FunctionTable));
  140. }
  141. Interface::~Interface() {
  142. }
  143. } // namespace