-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathipc.c
234 lines (188 loc) · 5.15 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
/*
BootMii - a Free Software replacement for the Nintendo/BroadOn bootloader.
Requires mini.
Copyright (C) 2008, 2009 Hector Martin "marcan" <[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 "bootmii_ppc.h"
#include "ipc.h"
#include "string.h"
#include <stdarg.h>
static volatile ipc_request *in_queue;
static volatile ipc_request *out_queue;
static int in_size;
static int out_size;
static int initialized = 0;
static u16 out_head;
static u16 in_tail;
typedef const struct {
char magic[3];
char version;
void *mem2_boundary;
volatile ipc_request *ipc_in;
u32 ipc_in_size;
volatile ipc_request *ipc_out;
u32 ipc_out_size;
} ipc_infohdr;
static ipc_infohdr *infohdr;
static u32 cur_tag;
#define HW_REG_BASE 0xd000000
#define HW_IPC_PPCMSG (HW_REG_BASE + 0x000) //PPC to ARM
#define HW_IPC_PPCCTRL (HW_REG_BASE + 0x004)
#define HW_IPC_ARMMSG (HW_REG_BASE + 0x008) //ARM to PPC
#define IPC_CTRL_SEND 0x01
// Set by peer to acknowledge a message. Write one to clear.
#define IPC_CTRL_SENT 0x02
// Set by peer to send a message. Write one to clear.
#define IPC_CTRL_RECV 0x04
// Write one acknowledge a message. Cleared when peer writes one to IPC_CTRL_SENT.
#define IPC_CTRL_RECVD 0x08
// Enable interrupt when a message is received
#define IPC_CTRL_INT_RECV 0x10
// Enable interrupt when a sent message is acknowledged
#define IPC_CTRL_INT_SENT 0x20
static inline u16 peek_outtail(void)
{
return read32(HW_IPC_ARMMSG) & 0xFFFF;
}
static inline u16 peek_inhead(void)
{
return read32(HW_IPC_ARMMSG) >> 16;
}
static inline void poke_intail(u16 num)
{
mask32(HW_IPC_PPCMSG, 0xFFFF, num);
}
static inline void poke_outhead(u16 num)
{
mask32(HW_IPC_PPCMSG, 0xFFFF0000, num<<16);
}
int ipc_initialize(void)
{
infohdr = (ipc_infohdr*)(read32(0x13fffffc)|0x80000000);
sync_before_read((void*)infohdr, sizeof(ipc_infohdr));
printf("IPC: infoheader at %p %08x\n", infohdr);
if(memcmp(infohdr->magic, "IPC", 3)) {
printf("IPC: bad magic on info structure\n",infohdr);
return -1;
}
if(infohdr->version != 1) {
printf("IPC: unknown IPC version %d\n",infohdr->version);
return -1;
}
in_queue = (void*)(((u32)infohdr->ipc_in)|0x80000000);
out_queue = (void*)(((u32)infohdr->ipc_out)|0x80000000);
in_size = infohdr->ipc_in_size;
out_size = infohdr->ipc_out_size;
in_tail = read32(HW_IPC_PPCMSG) & 0xffff;
out_head = read32(HW_IPC_PPCMSG) >> 16;
printf("IPC: initial in tail: %d, out head: %d\n", in_tail, out_head);
cur_tag = 1;
initialized = 1;
return 0;
}
void ipc_shutdown(void)
{
if(!initialized)
return;
ipc_flush();
initialized = 0;
}
void ipc_vpost(u32 code, u32 tag, u32 num_args, va_list ap)
{
int arg = 0;
int n = 0;
if(!initialized) {
printf("IPC: not inited\n");
return;
}
if(peek_inhead() == ((in_tail + 1)&(in_size-1))) {
printf("IPC: in queue full, spinning\n");
while(peek_inhead() == ((in_tail + 1)&(in_size-1))) {
udelay(10);
if(n++ > 20000) {
printf("IPC: ARM might be stuck, still waiting for inhead %d != %d\n",
peek_inhead(), ((in_tail + 1)&(in_size-1)));
n = 0;
}
}
}
in_queue[in_tail].code = code;
in_queue[in_tail].tag = tag;
while(num_args--) {
in_queue[in_tail].args[arg++] = va_arg(ap, u32);
}
sync_after_write((void*)&in_queue[in_tail], 32);
in_tail = (in_tail+1)&(in_size-1);
poke_intail(in_tail);
write32(HW_IPC_PPCCTRL, IPC_CTRL_SEND);
}
void ipc_post(u32 code, u32 tag, u32 num_args, ...)
{
va_list ap;
if(num_args)
va_start(ap, num_args);
ipc_vpost(code, tag, num_args, ap);
if(num_args)
va_end(ap);
}
void ipc_flush(void)
{
int n = 0;
if(!initialized) {
printf("IPC: not inited\n");
return;
}
while(peek_inhead() != in_tail) {
udelay(10);
if(n++ > 20000) {
printf("IPC: ARM might be stuck, still waiting for inhead %d == intail %d\n",
peek_inhead(), in_tail);
n = 0;
}
}
}
// last IPC message received, copied because we need to make space in the queue
ipc_request req_recv;
// since we're not using IRQs, we don't use the reception bell at all at the moment
ipc_request *ipc_receive(void)
{
while(peek_outtail() == out_head);
sync_before_read((void*)&out_queue[out_head], 32);
req_recv = out_queue[out_head];
out_head = (out_head+1)&(out_size-1);
poke_outhead(out_head);
return &req_recv;
}
void ipc_process_unhandled(volatile ipc_request *rep)
{
printf("IPC: Unhandled message: %08x %08x [%08x %08x %08x %08x %08x %08x]\n",
rep->code, rep->tag, rep->args[0], rep->args[1], rep->args[2], rep->args[3],
rep->args[4], rep->args[5]);
}
ipc_request *ipc_receive_tagged(u32 code, u32 tag)
{
ipc_request *rep;
rep = ipc_receive();
while(rep->code != code || rep->tag != tag) {
ipc_process_unhandled(rep);
rep = ipc_receive();
}
return rep;
}
ipc_request *ipc_exchange(u32 code, u32 num_args, ...)
{
va_list ap;
ipc_request *rep;
if(num_args)
va_start(ap, num_args);
ipc_vpost(code, cur_tag, num_args, ap);
if(num_args)
va_end(ap);
rep = ipc_receive_tagged(code, cur_tag);
cur_tag++;
return rep;
}