summaryrefslogtreecommitdiff
path: root/src/core/hle/service/gsp.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2014-08-06 18:30:01 -0400
committerGravatar bunnei2014-08-06 18:30:01 -0400
commitd0c179485392903fa413543d6b6908d45bc1f0fb (patch)
treea2e85ca4b091042e2e45446fda5b36bf6f62d2b3 /src/core/hle/service/gsp.cpp
parentMerge pull request #36 from bunnei/fix-memory-unaligned-reads (diff)
parentGPU: Updated g_last_ticks variable to be more descriptive (represents CPU tic... (diff)
downloadyuzu-d0c179485392903fa413543d6b6908d45bc1f0fb.tar.gz
yuzu-d0c179485392903fa413543d6b6908d45bc1f0fb.tar.xz
yuzu-d0c179485392903fa413543d6b6908d45bc1f0fb.zip
Merge pull request #34 from bunnei/gsp-command-synch
Gsp command synch
Diffstat (limited to 'src/core/hle/service/gsp.cpp')
-rw-r--r--src/core/hle/service/gsp.cpp148
1 files changed, 89 insertions, 59 deletions
diff --git a/src/core/hle/service/gsp.cpp b/src/core/hle/service/gsp.cpp
index b20203e27..e241b31c8 100644
--- a/src/core/hle/service/gsp.cpp
+++ b/src/core/hle/service/gsp.cpp
@@ -11,55 +11,35 @@
11#include "core/hle/kernel/event.h" 11#include "core/hle/kernel/event.h"
12#include "core/hle/kernel/shared_memory.h" 12#include "core/hle/kernel/shared_memory.h"
13#include "core/hle/service/gsp.h" 13#include "core/hle/service/gsp.h"
14
15#include "core/hw/gpu.h" 14#include "core/hw/gpu.h"
16 15
17#include "video_core/gpu_debugger.h" 16#include "video_core/gpu_debugger.h"
18 17
19////////////////////////////////////////////////////////////////////////////////////////////////////
20
21// Main graphics debugger object - TODO: Here is probably not the best place for this 18// Main graphics debugger object - TODO: Here is probably not the best place for this
22GraphicsDebugger g_debugger; 19GraphicsDebugger g_debugger;
23 20
24/// GSP shared memory GX command buffer header
25union GX_CmdBufferHeader {
26 u32 hex;
27
28 // Current command index. This index is updated by GSP module after loading the command data,
29 // right before the command is processed. When this index is updated by GSP module, the total
30 // commands field is decreased by one as well.
31 BitField<0,8,u32> index;
32
33 // Total commands to process, must not be value 0 when GSP module handles commands. This must be
34 // <=15 when writing a command to shared memory. This is incremented by the application when
35 // writing a command to shared memory, after increasing this value TriggerCmdReqQueue is only
36 // used if this field is value 1.
37 BitField<8,8,u32> number_commands;
38};
39
40//////////////////////////////////////////////////////////////////////////////////////////////////// 21////////////////////////////////////////////////////////////////////////////////////////////////////
41// Namespace GSP_GPU 22// Namespace GSP_GPU
42 23
43namespace GSP_GPU { 24namespace GSP_GPU {
44 25
45Handle g_event = 0; 26Handle g_interrupt_event = 0; ///< Handle to event triggered when GSP interrupt has been signalled
46Handle g_shared_memory = 0; 27Handle g_shared_memory = 0; ///< Handle to GSP shared memorys
28u32 g_thread_id = 1; ///< Thread index into interrupt relay queue, 1 is arbitrary
47 29
48u32 g_thread_id = 0; 30/// Gets a pointer to a thread command buffer in GSP shared memory
31static inline u8* GetCommandBuffer(u32 thread_id) {
32 if (0 == g_shared_memory)
33 return nullptr;
49 34
50/// Gets a pointer to the start (header) of a command buffer in GSP shared memory 35 return Kernel::GetSharedMemoryPointer(g_shared_memory,
51static inline u8* GX_GetCmdBufferPointer(u32 thread_id, u32 offset=0) { 36 0x800 + (thread_id * sizeof(CommandBuffer)));
52 return Kernel::GetSharedMemoryPointer(g_shared_memory, 0x800 + (thread_id * 0x200) + offset);
53} 37}
54 38
55/// Finishes execution of a GSP command 39/// Gets a pointer to the interrupt relay queue for a given thread index
56void GX_FinishCommand(u32 thread_id) { 40static inline InterruptRelayQueue* GetInterruptRelayQueue(u32 thread_id) {
57 GX_CmdBufferHeader* header = (GX_CmdBufferHeader*)GX_GetCmdBufferPointer(thread_id); 41 return (InterruptRelayQueue*)Kernel::GetSharedMemoryPointer(g_shared_memory,
58 42 sizeof(InterruptRelayQueue) * thread_id);
59 g_debugger.GXCommandProcessed(GX_GetCmdBufferPointer(thread_id, 0x20 + (header->index * 0x20)));
60
61 header->number_commands = header->number_commands - 1;
62 // TODO: Increment header->index?
63} 43}
64 44
65/// Write a GSP GPU hardware register 45/// Write a GSP GPU hardware register
@@ -133,39 +113,55 @@ void ReadHWRegs(Service::Interface* self) {
133void RegisterInterruptRelayQueue(Service::Interface* self) { 113void RegisterInterruptRelayQueue(Service::Interface* self) {
134 u32* cmd_buff = Service::GetCommandBuffer(); 114 u32* cmd_buff = Service::GetCommandBuffer();
135 u32 flags = cmd_buff[1]; 115 u32 flags = cmd_buff[1];
136 g_event = cmd_buff[3]; 116 g_interrupt_event = cmd_buff[3];
137 117 g_shared_memory = Kernel::CreateSharedMemory("GSPSharedMem");
138 _assert_msg_(GSP, (g_event != 0), "handle is not valid!");
139 118
140 Kernel::SetEventLocked(g_event, false); 119 _assert_msg_(GSP, (g_interrupt_event != 0), "handle is not valid!");
141 120
142 // Hack - This function will permanently set the state of the GSP event such that GPU command 121 cmd_buff[2] = g_thread_id++; // ThreadID
143 // synchronization barriers always passthrough. Correct solution would be to set this after the 122 cmd_buff[4] = g_shared_memory; // GSP shared memory
144 // GPU as processed all queued up commands, but due to the emulator being single-threaded they
145 // will always be ready.
146 Kernel::SetPermanentLock(g_event, true);
147 123
148 cmd_buff[0] = 0; // Result - no error 124 Kernel::SignalEvent(g_interrupt_event); // TODO(bunnei): Is this correct?
149 cmd_buff[2] = g_thread_id; // ThreadID
150 cmd_buff[4] = g_shared_memory; // GSP shared memory
151} 125}
152 126
127/**
128 * Signals that the specified interrupt type has occurred to userland code
129 * @param interrupt_id ID of interrupt that is being signalled
130 */
131void SignalInterrupt(InterruptId interrupt_id) {
132 if (0 == g_interrupt_event) {
133 WARN_LOG(GSP, "cannot synchronize until GSP event has been created!");
134 return;
135 }
136 if (0 == g_shared_memory) {
137 WARN_LOG(GSP, "cannot synchronize until GSP shared memory has been created!");
138 return;
139 }
140 for (int thread_id = 0; thread_id < 0x4; ++thread_id) {
141 InterruptRelayQueue* interrupt_relay_queue = GetInterruptRelayQueue(thread_id);
142 interrupt_relay_queue->number_interrupts = interrupt_relay_queue->number_interrupts + 1;
153 143
154/// This triggers handling of the GX command written to the command buffer in shared memory. 144 u8 next = interrupt_relay_queue->index;
155void TriggerCmdReqQueue(Service::Interface* self) { 145 next += interrupt_relay_queue->number_interrupts;
146 next = next % 0x34; // 0x34 is the number of interrupt slots
156 147
148 interrupt_relay_queue->slot[next] = interrupt_id;
149 interrupt_relay_queue->error_code = 0x0; // No error
150 }
151 Kernel::SignalEvent(g_interrupt_event);
152}
153
154/// Executes the next GSP command
155void ExecuteCommand(const Command& command) {
157 // Utility function to convert register ID to address 156 // Utility function to convert register ID to address
158 auto WriteGPURegister = [](u32 id, u32 data) { 157 auto WriteGPURegister = [](u32 id, u32 data) {
159 GPU::Write<u32>(0x1EF00000 + 4 * id, data); 158 GPU::Write<u32>(0x1EF00000 + 4 * id, data);
160 }; 159 };
161 160
162 GX_CmdBufferHeader* header = (GX_CmdBufferHeader*)GX_GetCmdBufferPointer(g_thread_id);
163 auto& command = *(const GXCommand*)GX_GetCmdBufferPointer(g_thread_id, 0x20 + (header->index * 0x20));
164
165 switch (command.id) { 161 switch (command.id) {
166 162
167 // GX request DMA - typically used for copying memory from GSP heap to VRAM 163 // GX request DMA - typically used for copying memory from GSP heap to VRAM
168 case GXCommandId::REQUEST_DMA: 164 case CommandId::REQUEST_DMA:
169 memcpy(Memory::GetPointer(command.dma_request.dest_address), 165 memcpy(Memory::GetPointer(command.dma_request.dest_address),
170 Memory::GetPointer(command.dma_request.source_address), 166 Memory::GetPointer(command.dma_request.source_address),
171 command.dma_request.size); 167 command.dma_request.size);
@@ -174,24 +170,27 @@ void TriggerCmdReqQueue(Service::Interface* self) {
174 // ctrulib homebrew sends all relevant command list data with this command, 170 // ctrulib homebrew sends all relevant command list data with this command,
175 // hence we do all "interesting" stuff here and do nothing in SET_COMMAND_LIST_FIRST. 171 // hence we do all "interesting" stuff here and do nothing in SET_COMMAND_LIST_FIRST.
176 // TODO: This will need some rework in the future. 172 // TODO: This will need some rework in the future.
177 case GXCommandId::SET_COMMAND_LIST_LAST: 173 case CommandId::SET_COMMAND_LIST_LAST:
178 { 174 {
179 auto& params = command.set_command_list_last; 175 auto& params = command.set_command_list_last;
180 WriteGPURegister(GPU::Regs::CommandProcessor + 2, params.address >> 3); 176 WriteGPURegister(GPU::Regs::CommandProcessor + 2, params.address >> 3);
181 WriteGPURegister(GPU::Regs::CommandProcessor, params.size >> 3); 177 WriteGPURegister(GPU::Regs::CommandProcessor, params.size >> 3);
182 WriteGPURegister(GPU::Regs::CommandProcessor + 4, 1); // TODO: Not sure if we are supposed to always write this .. seems to trigger processing though 178
179 // TODO: Not sure if we are supposed to always write this .. seems to trigger processing though
180 WriteGPURegister(GPU::Regs::CommandProcessor + 4, 1);
183 181
184 // TODO: Move this to GPU 182 // TODO: Move this to GPU
185 // TODO: Not sure what units the size is measured in 183 // TODO: Not sure what units the size is measured in
186 g_debugger.CommandListCalled(params.address, 184 g_debugger.CommandListCalled(params.address,
187 (u32*)Memory::GetPointer(params.address), 185 (u32*)Memory::GetPointer(params.address),
188 params.size); 186 params.size);
187 SignalInterrupt(InterruptId::P3D);
189 break; 188 break;
190 } 189 }
191 190
192 // It's assumed that the two "blocks" behave equivalently. 191 // It's assumed that the two "blocks" behave equivalently.
193 // Presumably this is done simply to allow two memory fills to run in parallel. 192 // Presumably this is done simply to allow two memory fills to run in parallel.
194 case GXCommandId::SET_MEMORY_FILL: 193 case CommandId::SET_MEMORY_FILL:
195 { 194 {
196 auto& params = command.memory_fill; 195 auto& params = command.memory_fill;
197 WriteGPURegister(GPU::Regs::MemoryFill, params.start1 >> 3); 196 WriteGPURegister(GPU::Regs::MemoryFill, params.start1 >> 3);
@@ -207,8 +206,18 @@ void TriggerCmdReqQueue(Service::Interface* self) {
207 } 206 }
208 207
209 // TODO: Check if texture copies are implemented correctly.. 208 // TODO: Check if texture copies are implemented correctly..
210 case GXCommandId::SET_DISPLAY_TRANSFER: 209 case CommandId::SET_DISPLAY_TRANSFER:
211 case GXCommandId::SET_TEXTURE_COPY: 210 // TODO(bunnei): Signalling all of these interrupts here is totally wrong, but it seems to
211 // work well enough for running demos. Need to figure out how these all work and trigger
212 // them correctly.
213 SignalInterrupt(InterruptId::PSC0);
214 SignalInterrupt(InterruptId::PSC1);
215 SignalInterrupt(InterruptId::PPF);
216 SignalInterrupt(InterruptId::P3D);
217 SignalInterrupt(InterruptId::DMA);
218 break;
219
220 case CommandId::SET_TEXTURE_COPY:
212 { 221 {
213 auto& params = command.image_copy; 222 auto& params = command.image_copy;
214 WriteGPURegister(GPU::Regs::DisplayTransfer, params.in_buffer_address >> 3); 223 WriteGPURegister(GPU::Regs::DisplayTransfer, params.in_buffer_address >> 3);
@@ -225,7 +234,7 @@ void TriggerCmdReqQueue(Service::Interface* self) {
225 234
226 // TODO: Figure out what exactly SET_COMMAND_LIST_FIRST and SET_COMMAND_LIST_LAST 235 // TODO: Figure out what exactly SET_COMMAND_LIST_FIRST and SET_COMMAND_LIST_LAST
227 // are supposed to do. 236 // are supposed to do.
228 case GXCommandId::SET_COMMAND_LIST_FIRST: 237 case CommandId::SET_COMMAND_LIST_FIRST:
229 { 238 {
230 break; 239 break;
231 } 240 }
@@ -233,8 +242,26 @@ void TriggerCmdReqQueue(Service::Interface* self) {
233 default: 242 default:
234 ERROR_LOG(GSP, "unknown command 0x%08X", (int)command.id.Value()); 243 ERROR_LOG(GSP, "unknown command 0x%08X", (int)command.id.Value());
235 } 244 }
245}
236 246
237 GX_FinishCommand(g_thread_id); 247/// This triggers handling of the GX command written to the command buffer in shared memory.
248void TriggerCmdReqQueue(Service::Interface* self) {
249
250 // Iterate through each thread's command queue...
251 for (unsigned thread_id = 0; thread_id < 0x4; ++thread_id) {
252 CommandBuffer* command_buffer = (CommandBuffer*)GetCommandBuffer(thread_id);
253
254 // Iterate through each command...
255 for (unsigned i = 0; i < command_buffer->number_commands; ++i) {
256 g_debugger.GXCommandProcessed((u8*)&command_buffer->commands[i]);
257
258 // Decode and execute command
259 ExecuteCommand(command_buffer->commands[i]);
260
261 // Indicates that command has completed
262 command_buffer->number_commands = command_buffer->number_commands - 1;
263 }
264 }
238} 265}
239 266
240const Interface::FunctionInfo FunctionTable[] = { 267const Interface::FunctionInfo FunctionTable[] = {
@@ -275,7 +302,10 @@ const Interface::FunctionInfo FunctionTable[] = {
275 302
276Interface::Interface() { 303Interface::Interface() {
277 Register(FunctionTable, ARRAY_SIZE(FunctionTable)); 304 Register(FunctionTable, ARRAY_SIZE(FunctionTable));
278 g_shared_memory = Kernel::CreateSharedMemory("GSPSharedMem"); 305
306 g_interrupt_event = 0;
307 g_shared_memory = 0;
308 g_thread_id = 1;
279} 309}
280 310
281Interface::~Interface() { 311Interface::~Interface() {