-
Notifications
You must be signed in to change notification settings - Fork 6
/
ipc.c
356 lines (311 loc) · 8.67 KB
/
ipc.c
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
/*
mini - a Free Software replacement for the Nintendo/BroadOn IOS.
inter-processor communications
Copyright (C) 2008, 2009 Hector Martin "marcan" <[email protected]>
Copyright (C) 2008, 2009 Haxx Enterprises <[email protected]>
Copyright (C) 2008, 2009 Sven Peter <[email protected]>
Copyright (C) 2009 Andre Heider "dhewg" <[email protected]>
Copyright (C) 2009 John Kelley <[email protected]>
# This code is licensed to you under the terms of the GNU GPL, version 2;
# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*/
#include <stdarg.h>
#include "string.h"
#include "types.h"
#include "irq.h"
#include "memory.h"
#include "utils.h"
#include "hollywood.h"
#include "gecko.h"
#include "ipc.h"
#include "nand.h"
#include "sdhc.h"
#include "sdmmc.h"
#include "crypto.h"
#include "boot2.h"
#include "powerpc.h"
#include "panic.h"
#define MINI_VERSION_MAJOR 1
#define MINI_VERSION_MINOR 3
static volatile ipc_request in_queue[IPC_IN_SIZE] ALIGNED(32) MEM2_BSS;
static volatile ipc_request out_queue[IPC_OUT_SIZE] ALIGNED(32) MEM2_BSS;
static volatile ipc_request slow_queue[IPC_SLOW_SIZE];
extern char __mem2_area_start[];
extern const char git_version[];
// These defines are for the ARMCTRL regs
// See http://wiibrew.org/wiki/Hardware/IPC
#define IPC_CTRL_Y1 0x01
#define IPC_CTRL_X2 0x02
#define IPC_CTRL_X1 0x04
#define IPC_CTRL_Y2 0x08
#define IPC_CTRL_IX1 0x10
#define IPC_CTRL_IX2 0x20
// Our definitions for this IPC interface
#define IPC_CTRL_OUT IPC_CTRL_Y1
#define IPC_CTRL_IN IPC_CTRL_X1
#define IPC_CTRL_IRQ_IN IPC_CTRL_IX1
// reset both flags (X* for ARM and Y* for PPC)
#define IPC_CTRL_RESET 0x06
const ipc_infohdr __ipc_info ALIGNED(32) MEM2_RODATA = {
.magic = "IPC",
.version = 1,
.mem2_boundary = __mem2_area_start,
.ipc_in = in_queue,
.ipc_in_size = IPC_IN_SIZE,
.ipc_out = out_queue,
.ipc_out_size = IPC_OUT_SIZE,
};
static u16 slow_queue_head;
static vu16 slow_queue_tail;
static u16 in_head;
static u16 out_tail;
static inline void poke_outtail(u16 num)
{
mask32(HW_IPC_ARMMSG, 0xFFFF, num);
}
static inline void poke_inhead(u16 num)
{
mask32(HW_IPC_ARMMSG, 0xFFFF0000, num<<16);
}
static inline u16 peek_intail(void)
{
return read32(HW_IPC_PPCMSG) & 0xFFFF;
}
static inline u16 peek_outhead(void)
{
return read32(HW_IPC_PPCMSG) >> 16;
}
void ipc_post(u32 code, u32 tag, u32 num_args, ...)
{
int arg = 0;
va_list ap;
u32 cookie = irq_kill();
if(peek_outhead() == ((out_tail + 1)&(IPC_OUT_SIZE-1))) {
gecko_printf("IPC: out queue full, PPC slow/dead/flooded\n");
while(peek_outhead() == ((out_tail + 1)&(IPC_OUT_SIZE-1)));
}
out_queue[out_tail].code = code;
out_queue[out_tail].tag = tag;
if(num_args) {
va_start(ap, num_args);
while(num_args--) {
out_queue[out_tail].args[arg++] = va_arg(ap, u32);
}
va_end(ap);
}
dc_flush_block_fast((void*)&out_queue[out_tail]);
out_tail = (out_tail+1)&(IPC_OUT_SIZE-1);
poke_outtail(out_tail);
write32(HW_IPC_ARMCTRL, IPC_CTRL_IRQ_IN | IPC_CTRL_OUT);
irq_restore(cookie);
}
void ipc_flush(void)
{
while(peek_outhead() != out_tail);
}
static u32 process_slow(volatile ipc_request *req)
{
//gecko_printf("IPC: process slow_queue @ %p\n",req);
//gecko_printf("IPC: req %08x %08x [%08x %08x %08x %08x %08x %08x]\n", req->code, req->tag,
// req->args[0], req->args[1], req->args[2], req->args[3], req->args[4], req->args[5]);
switch(req->device) {
case IPC_DEV_SYS:
switch(req->req) {
case IPC_SYS_PING: //PING can be both slow and fast for testing purposes
ipc_post(req->code, req->tag, 0);
break;
case IPC_SYS_JUMP:
return req->args[0];
case IPC_SYS_GETVERS:
ipc_post(req->code, req->tag, 1, MINI_VERSION_MAJOR << 16 | MINI_VERSION_MINOR);
break;
case IPC_SYS_GETGITS:
strlcpy((char *)req->args[0], git_version, 32);
dc_flushrange((void *)req->args[0], 32);
ipc_post(req->code, req->tag, 0);
break;
default:
gecko_printf("IPC: unknown SLOW SYS request %04x\n", req->req);
}
break;
case IPC_DEV_NAND:
nand_ipc(req);
break;
case IPC_DEV_SDHC:
sdhc_ipc(req);
break;
case IPC_DEV_SDMMC:
sdmmc_ipc(req);
break;
case IPC_DEV_KEYS:
crypto_ipc(req);
break;
case IPC_DEV_AES:
aes_ipc(req);
break;
case IPC_DEV_BOOT2:
return boot2_ipc(req);
break;
case IPC_DEV_PPC:
powerpc_ipc(req);
break;
default:
gecko_printf("IPC: unknown SLOW request %02x-%04x\n", req->device, req->req);
}
return 0;
}
void ipc_enqueue_slow(u8 device, u16 req, u32 num_args, ...)
{
int arg = 0;
va_list ap;
if(slow_queue_head == ((slow_queue_tail + 1)&(IPC_SLOW_SIZE-1))) {
gecko_printf("IPC: Slowqueue overrun\n");
panic2(0, PANIC_IPCOVF);
}
slow_queue[slow_queue_tail].flags = IPC_SLOW;
slow_queue[slow_queue_tail].device = device;
slow_queue[slow_queue_tail].req = req;
slow_queue[slow_queue_tail].tag = 0;
if(num_args) {
va_start(ap, num_args);
while(num_args--)
slow_queue[slow_queue_tail].args[arg++] = va_arg(ap, u32);
va_end(ap);
}
slow_queue_tail = (slow_queue_tail+1)&(IPC_SLOW_SIZE-1);
}
static void process_in(void)
{
volatile ipc_request *req = &in_queue[in_head];
//gecko_printf("IPC: process in %d @ %p\n",in_head,req);
dc_inval_block_fast((void*)req);
//gecko_printf("IPC: req %08x %08x [%08x %08x %08x %08x %08x %08x]\n", req->code, req->tag,
// req->args[0], req->args[1], req->args[2], req->args[3], req->args[4], req->args[5]);
if(req->flags & IPC_FAST) {
switch(req->device) {
case IPC_DEV_SYS:
// handle fast SYS requests here
switch(req->req) {
case IPC_SYS_PING:
ipc_post(req->code, req->tag, 0);
break;
case IPC_SYS_WRITE32:
write32(req->args[0], req->args[1]);
break;
case IPC_SYS_WRITE16:
write16(req->args[0], req->args[1]);
break;
case IPC_SYS_WRITE8:
write8(req->args[0], req->args[1]);
break;
case IPC_SYS_READ32:
ipc_post(req->code, req->tag, 1, read32(req->args[0]));
break;
case IPC_SYS_READ16:
ipc_post(req->code, req->tag, 1, read16(req->args[0]));
break;
case IPC_SYS_READ8:
ipc_post(req->code, req->tag, 1, read8(req->args[0]));
break;
case IPC_SYS_SET32:
set32(req->args[0], req->args[1]);
break;
case IPC_SYS_SET16:
set16(req->args[0], req->args[1]);
break;
case IPC_SYS_SET8:
set8(req->args[0], req->args[1]);
break;
case IPC_SYS_CLEAR32:
clear32(req->args[0], req->args[1]);
break;
case IPC_SYS_CLEAR16:
clear16(req->args[0], req->args[1]);
break;
case IPC_SYS_CLEAR8:
clear8(req->args[0], req->args[1]);
break;
case IPC_SYS_MASK32:
mask32(req->args[0], req->args[1], req->args[2]);
break;
case IPC_SYS_MASK16:
mask16(req->args[0], req->args[1], req->args[2]);
break;
case IPC_SYS_MASK8:
mask8(req->args[0], req->args[1], req->args[2]);
break;
default:
gecko_printf("IPC: unknown FAST SYS request %04x\n", req->req);
break;
}
break;
default:
gecko_printf("IPC: unknown FAST request %02x-%04x\n", req->device, req->req);
break;
}
} else {
if(slow_queue_head == ((slow_queue_tail + 1)&(IPC_SLOW_SIZE-1))) {
gecko_printf("IPC: Slowqueue overrun\n");
panic2(0, PANIC_IPCOVF);
}
slow_queue[slow_queue_tail] = *req;
slow_queue_tail = (slow_queue_tail+1)&(IPC_SLOW_SIZE-1);
}
}
void ipc_irq(void)
{
int donebell = 0;
while(read32(HW_IPC_ARMCTRL) & IPC_CTRL_IN) {
write32(HW_IPC_ARMCTRL, IPC_CTRL_IRQ_IN | IPC_CTRL_IN);
while(peek_intail() != in_head) {
process_in();
in_head = (in_head+1)&(IPC_IN_SIZE-1);
poke_inhead(in_head);
}
donebell++;
}
if(!donebell)
gecko_printf("IPC: IRQ but no bell!\n");
}
void ipc_initialize(void)
{
write32(HW_IPC_ARMMSG, 0);
write32(HW_IPC_PPCMSG, 0);
write32(HW_IPC_PPCCTRL, IPC_CTRL_RESET);
write32(HW_IPC_ARMCTRL, IPC_CTRL_RESET);
slow_queue_head = 0;
slow_queue_tail = 0;
in_head = 0;
out_tail = 0;
irq_enable(IRQ_IPC);
write32(HW_IPC_ARMCTRL, IPC_CTRL_IRQ_IN);
}
void ipc_shutdown(void)
{
// Don't kill message registers so our PPC side doesn't get confused
//write32(HW_IPC_ARMMSG, 0);
//write32(HW_IPC_PPCMSG, 0);
// Do kill flags so Nintendo's SDK doesn't get confused
write32(HW_IPC_PPCCTRL, IPC_CTRL_RESET);
write32(HW_IPC_ARMCTRL, IPC_CTRL_RESET);
irq_disable(IRQ_IPC);
}
u32 ipc_process_slow(void)
{
u32 vector = 0;
while (!vector) {
while (!vector && (slow_queue_head != slow_queue_tail)) {
vector = process_slow(&slow_queue[slow_queue_head]);
slow_queue_head = (slow_queue_head+1)&(IPC_SLOW_SIZE-1);
}
if (!vector)
{
gecko_process();
u32 cookie = irq_kill();
if(slow_queue_head == slow_queue_tail)
irq_wait();
irq_restore(cookie);
}
}
return vector;
}