1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
// Copyright 2014 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include "audio_core/audio_types.h"
#include "common/archives.h"
#include "common/assert.h"
#include "common/logging/log.h"
#include "core/core.h"
#include "core/hle/ipc_helpers.h"
#include "core/hle/kernel/process.h"
#include "core/hle/service/dsp/dsp_dsp.h"
using DspPipe = AudioCore::DspPipe;
using InterruptType = Service::DSP::InterruptType;
SERIALIZE_EXPORT_IMPL(Service::DSP::DSP_DSP)
SERVICE_CONSTRUCT_IMPL(Service::DSP::DSP_DSP)
namespace AudioCore {
enum class DspPipe;
}
namespace Service::DSP {
void DSP_DSP::RecvData(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const u32 register_number = rp.Pop<u32>();
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess);
rb.Push(system.DSP().RecvData(register_number));
LOG_DEBUG(Service_DSP, "register_number={}", register_number);
}
void DSP_DSP::RecvDataIsReady(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const u32 register_number = rp.Pop<u32>();
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess);
rb.Push(system.DSP().RecvDataIsReady(register_number));
LOG_DEBUG(Service_DSP, "register_number={}", register_number);
}
void DSP_DSP::SetSemaphore(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const u16 semaphore_value = rp.Pop<u16>();
system.DSP().SetSemaphore(semaphore_value);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultSuccess);
LOG_INFO(Service_DSP, "called, semaphore_value={:04X}", semaphore_value);
}
void DSP_DSP::ConvertProcessAddressFromDspDram(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const u32 address = rp.Pop<u32>();
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess);
// TODO(merry): There is a per-region offset missing in this calculation (that seems to be
// always zero).
rb.Push<u32>((address << 1) + (Memory::DSP_RAM_VADDR + 0x40000));
LOG_DEBUG(Service_DSP, "address=0x{:08X}", address);
}
void DSP_DSP::WriteProcessPipe(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const u32 channel = rp.Pop<u32>();
const u32 size = rp.Pop<u32>();
auto buffer = rp.PopStaticBuffer();
const DspPipe pipe = static_cast<DspPipe>(channel);
// This behaviour was confirmed by RE.
// The likely reason for this is that games tend to pass in garbage at these bytes
// because they read random bytes off the stack.
switch (pipe) {
case DspPipe::Audio:
ASSERT(buffer.size() >= 4);
buffer[2] = 0;
buffer[3] = 0;
break;
case DspPipe::Binary:
ASSERT(buffer.size() >= 8);
buffer[4] = 1;
buffer[5] = 0;
buffer[6] = 0;
buffer[7] = 0;
break;
default:
LOG_ERROR(Service_DSP, "Unknown pipe {}", pipe);
break;
}
system.DSP().PipeWrite(pipe, buffer);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultSuccess);
LOG_DEBUG(Service_DSP, "channel={}, size=0x{:X}, buffer_size={:X}", channel, size,
buffer.size());
}
void DSP_DSP::ReadPipe(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const u32 channel = rp.Pop<u32>();
const u32 peer = rp.Pop<u32>();
const u16 size = rp.Pop<u16>();
const DspPipe pipe = static_cast<DspPipe>(channel);
const u16 pipe_readable_size = static_cast<u16>(system.DSP().GetPipeReadableSize(pipe));
std::vector<u8> pipe_buffer;
if (pipe_readable_size >= size)
pipe_buffer = system.DSP().PipeRead(pipe, size);
else
UNREACHABLE(); // No more data is in pipe. Hardware hangs in this case; Should never happen.
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(ResultSuccess);
rb.PushStaticBuffer(std::move(pipe_buffer), 0);
LOG_DEBUG(Service_DSP, "channel={}, peer={}, size=0x{:04X}, pipe_readable_size=0x{:04X}",
channel, peer, size, pipe_readable_size);
}
void DSP_DSP::GetPipeReadableSize(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const u32 channel = rp.Pop<u32>();
const u32 peer = rp.Pop<u32>();
const DspPipe pipe = static_cast<DspPipe>(channel);
const u16 pipe_readable_size = static_cast<u16>(system.DSP().GetPipeReadableSize(pipe));
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess);
rb.Push<u16>(pipe_readable_size);
LOG_DEBUG(Service_DSP, "channel={}, peer={}, return pipe_readable_size=0x{:04X}", channel, peer,
pipe_readable_size);
}
void DSP_DSP::ReadPipeIfPossible(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const u32 channel = rp.Pop<u32>();
const u32 peer = rp.Pop<u32>();
const u16 size = rp.Pop<u16>();
const DspPipe pipe = static_cast<DspPipe>(channel);
const u16 pipe_readable_size = static_cast<u16>(system.DSP().GetPipeReadableSize(pipe));
std::vector<u8> pipe_buffer;
if (pipe_readable_size >= size) {
pipe_buffer = system.DSP().PipeRead(pipe, size);
}
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
rb.Push(ResultSuccess);
rb.Push<u16>(static_cast<u16>(pipe_buffer.size()));
rb.PushStaticBuffer(std::move(pipe_buffer), 0);
LOG_DEBUG(Service_DSP, "channel={}, peer={}, size=0x{:04X}, pipe_readable_size=0x{:04X}",
channel, peer, size, pipe_readable_size);
}
void DSP_DSP::LoadComponent(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const auto size = rp.Pop<u32>();
const auto prog_mask = rp.Pop<u16>();
const auto data_mask = rp.Pop<u16>();
auto& buffer = rp.PopMappedBuffer();
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
rb.Push(ResultSuccess);
rb.Push(true);
rb.PushMappedBuffer(buffer);
std::vector<u8> component_data(size);
buffer.Read(component_data.data(), 0, size);
system.DSP().LoadComponent(component_data);
LOG_INFO(Service_DSP, "called size=0x{:X}, prog_mask=0x{:04X}, data_mask=0x{:04X}", size,
prog_mask, data_mask);
}
void DSP_DSP::UnloadComponent(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
system.DSP().UnloadComponent();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultSuccess);
LOG_INFO(Service_DSP, "called");
}
void DSP_DSP::FlushDataCache(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
[[maybe_unused]] const VAddr address = rp.Pop<u32>();
[[maybe_unused]] const u32 size = rp.Pop<u32>();
const auto process = rp.PopObject<Kernel::Process>();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultSuccess);
LOG_TRACE(Service_DSP, "called address=0x{:08X}, size=0x{:X}, process={}", address, size,
process->process_id);
}
void DSP_DSP::InvalidateDataCache(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
[[maybe_unused]] const VAddr address = rp.Pop<u32>();
[[maybe_unused]] const u32 size = rp.Pop<u32>();
const auto process = rp.PopObject<Kernel::Process>();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultSuccess);
LOG_TRACE(Service_DSP, "called address=0x{:08X}, size=0x{:X}, process={}", address, size,
process->process_id);
}
void DSP_DSP::RegisterInterruptEvents(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const u32 interrupt = rp.Pop<u32>();
const u32 channel = rp.Pop<u32>();
auto event = rp.PopObject<Kernel::Event>();
ASSERT_MSG(interrupt < static_cast<u32>(InterruptType::Count) &&
channel < AudioCore::num_dsp_pipe,
"Invalid type or pipe: interrupt = {}, channel = {}", interrupt, channel);
const InterruptType type = static_cast<InterruptType>(interrupt);
const DspPipe pipe = static_cast<DspPipe>(channel);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
if (event) { /// Register interrupt event
if (HasTooManyEventsRegistered()) {
LOG_INFO(Service_DSP,
"Ran out of space to register interrupts (Attempted to register "
"interrupt={}, channel={}, event={})",
interrupt, channel, event->GetName());
rb.Push(Result(ErrorDescription::InvalidResultValue, ErrorModule::DSP,
ErrorSummary::OutOfResource, ErrorLevel::Status));
return;
} else {
GetInterruptEvent(type, pipe) = event;
LOG_INFO(Service_DSP, "Registered interrupt={}, channel={}, event={}", interrupt,
channel, event->GetName());
}
} else { /// Otherwise unregister event
GetInterruptEvent(type, pipe) = nullptr;
LOG_INFO(Service_DSP, "Unregistered interrupt={}, channel={}", interrupt, channel);
}
rb.Push(ResultSuccess);
}
void DSP_DSP::GetSemaphoreEventHandle(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
rb.Push(ResultSuccess);
rb.PushCopyObjects(semaphore_event);
LOG_WARNING(Service_DSP, "(STUBBED) called");
}
void DSP_DSP::SetSemaphoreMask(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
preset_semaphore = rp.Pop<u16>();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultSuccess);
LOG_WARNING(Service_DSP, "(STUBBED) called mask=0x{:04X}", preset_semaphore);
}
void DSP_DSP::GetHeadphoneStatus(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
rb.Push(ResultSuccess);
rb.Push(false); /// u8, 0 = not inserted, 1 = inserted
LOG_DEBUG(Service_DSP, "called");
}
void DSP_DSP::ForceHeadphoneOut(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp(ctx);
const u8 force = rp.Pop<u8>();
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
rb.Push(ResultSuccess);
LOG_DEBUG(Service_DSP, "(STUBBED) called, force={}", force);
}
// DSP Interrupts:
// The audio-pipe interrupt occurs every frame tick. Userland programs normally have a thread
// that's waiting for an interrupt event. Immediately after this interrupt event, userland
// normally updates the state in the next region and increments the relevant frame counter by two.
void DSP_DSP::SignalInterrupt(InterruptType type, DspPipe pipe) {
LOG_TRACE(Service_DSP, "called, type={}, pipe={}", type, pipe);
const auto& event = GetInterruptEvent(type, pipe);
if (event)
event->Signal();
}
std::shared_ptr<Kernel::Event>& DSP_DSP::GetInterruptEvent(InterruptType type, DspPipe pipe) {
switch (type) {
case InterruptType::Zero:
return interrupt_zero;
case InterruptType::One:
return interrupt_one;
case InterruptType::Pipe: {
const std::size_t pipe_index = static_cast<std::size_t>(pipe);
ASSERT(pipe_index < AudioCore::num_dsp_pipe);
return pipes[pipe_index];
}
case InterruptType::Count:
default:
break;
}
UNREACHABLE_MSG("Invalid interrupt type = {}", type);
}
bool DSP_DSP::HasTooManyEventsRegistered() const {
std::size_t number =
std::count_if(pipes.begin(), pipes.end(), [](const auto& evt) { return evt != nullptr; });
if (interrupt_zero != nullptr)
number++;
if (interrupt_one != nullptr)
number++;
LOG_DEBUG(Service_DSP, "Number of events registered = {}", number);
return number >= max_number_of_interrupt_events;
}
DSP_DSP::DSP_DSP(Core::System& system)
: ServiceFramework("dsp::DSP", DefaultMaxSessions), system(system) {
static const FunctionInfo functions[] = {
// clang-format off
{0x0001, &DSP_DSP::RecvData, "RecvData"},
{0x0002, &DSP_DSP::RecvDataIsReady, "RecvDataIsReady"},
{0x0003, nullptr, "SendData"},
{0x0004, nullptr, "SendDataIsEmpty"},
{0x0005, nullptr, "SendFifoEx"},
{0x0006, nullptr, "RecvFifoEx"},
{0x0007, &DSP_DSP::SetSemaphore, "SetSemaphore"},
{0x0008, nullptr, "GetSemaphore"},
{0x0009, nullptr, "ClearSemaphore"},
{0x000A, nullptr, "MaskSemaphore"},
{0x000B, nullptr, "CheckSemaphoreRequest"},
{0x000C, &DSP_DSP::ConvertProcessAddressFromDspDram, "ConvertProcessAddressFromDspDram"},
{0x000D, &DSP_DSP::WriteProcessPipe, "WriteProcessPipe"},
{0x000E, &DSP_DSP::ReadPipe, "ReadPipe"},
{0x000F, &DSP_DSP::GetPipeReadableSize, "GetPipeReadableSize"},
{0x0010, &DSP_DSP::ReadPipeIfPossible, "ReadPipeIfPossible"},
{0x0011, &DSP_DSP::LoadComponent, "LoadComponent"},
{0x0012, &DSP_DSP::UnloadComponent, "UnloadComponent"},
{0x0013, &DSP_DSP::FlushDataCache, "FlushDataCache"},
{0x0014, &DSP_DSP::InvalidateDataCache, "InvalidateDCache"},
{0x0015, &DSP_DSP::RegisterInterruptEvents, "RegisterInterruptEvents"},
{0x0016, &DSP_DSP::GetSemaphoreEventHandle, "GetSemaphoreEventHandle"},
{0x0017, &DSP_DSP::SetSemaphoreMask, "SetSemaphoreMask"},
{0x0018, nullptr, "GetPhysicalAddress"},
{0x0019, nullptr, "GetVirtualAddress"},
{0x001A, nullptr, "SetIirFilterI2S1_cmd1"},
{0x001B, nullptr, "SetIirFilterI2S1_cmd2"},
{0x001C, nullptr, "SetIirFilterEQ"},
{0x001D, nullptr, "ReadMultiEx_SPI2"},
{0x001E, nullptr, "WriteMultiEx_SPI2"},
{0x001F, &DSP_DSP::GetHeadphoneStatus, "GetHeadphoneStatus"},
{0x0020, &DSP_DSP::ForceHeadphoneOut, "ForceHeadphoneOut"},
{0x0021, nullptr, "GetIsDspOccupied"},
// clang-format on
};
RegisterHandlers(functions);
semaphore_event =
system.Kernel().CreateEvent(Kernel::ResetType::OneShot, "DSP_DSP::semaphore_event");
semaphore_event->SetHLENotifier(
[this]() { this->system.DSP().SetSemaphore(preset_semaphore); });
system.DSP().SetInterruptHandler([dsp_ref = this, &system](InterruptType type, DspPipe pipe) {
std::scoped_lock lock{system.Kernel().GetHLELock()};
if (dsp_ref) {
dsp_ref->SignalInterrupt(type, pipe);
}
});
}
DSP_DSP::~DSP_DSP() {
semaphore_event = nullptr;
pipes = {};
}
void InstallInterfaces(Core::System& system) {
auto& service_manager = system.ServiceManager();
auto dsp = std::make_shared<DSP_DSP>(system);
dsp->InstallAsService(service_manager);
}
} // namespace Service::DSP
|