-
Notifications
You must be signed in to change notification settings - Fork 133
/
sifcmd.c
345 lines (283 loc) · 8.46 KB
/
sifcmd.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
/*
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# (C)2001, Gustavo Scotti (gustavo@scotti.com)
# (c) 2003 Marcus R. Brown (mrbrown@0xd6.org)
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.
*/
/**
* @file
* EE SIF commands
* MRB: This file now contains the SIF routines included
* with libpsware. Bug reports welcome.
*/
#include <tamtypes.h>
#include <kernel.h>
#include <sifcmd.h>
#define CMD_PACKET_MAX 128
#define CMD_PACKET_DATA_MAX 112
#define SYS_CMD_HANDLER_MAX 32
#define USR_CMD_HANDLER_MAX 32
/* EE DMAC registers. */
#define DMAC_COMM_STAT 0x1000e010
#define DMAC_SIF0_CHCR 0x1000c000
#define CHCR_STR 0x100
#define STAT_SIF0 0x20
/** Even though I'm reluctant to do this, I've made this structure binary
compatible with the SCE libs and ps2lib. In all implementations, a pointer
to this data is stored in SIF register 0x80000001. Each routine that
relies on this data can then use the data referenced from that register, so
that even if a SIF library is initialized after this one, we should still
work exactly as expected. */
struct cmd_data {
/** Command packet received from the IOP */
void *pktbuf;
void *unused;
/** Address of IOP SIF DMA receive address */
void *iopbuf;
SifCmdHandlerData_t *sys_cmd_handlers;
u32 nr_sys_handlers;
SifCmdHandlerData_t *usr_cmd_handlers;
u32 nr_usr_handlers;
int *sregs;
};
extern int _iop_reboot_count;
extern struct cmd_data _sif_cmd_data;
int _SifCmdIntHandler(int channel);
#ifdef F_sif_cmd_send
static unsigned int _SifSendCmd(int cid, int mode, void *pkt, int pktsize, void *src,
void *dest, int size)
{
SifDmaTransfer_t dmat[2];
SifCmdHeader_t *header;
int count = 0;
pktsize &= 0xff;
if (pktsize > CMD_PACKET_DATA_MAX)
return 0;
header = (SifCmdHeader_t *)pkt;
header->cid = cid;
header->psize = pktsize;
header->dest = NULL;
if (size > 0) {
header->dsize = size;
header->dest = dest;
if (mode & SIF_CMD_M_WBDC) /* if mode is & 4, flush reference cache */
SifWriteBackDCache(src, size);
dmat[count].src = src;
dmat[count].dest = dest;
dmat[count].size = size;
dmat[count].attr = 0;
count++;
}
dmat[count].src = pkt;
dmat[count].dest = _sif_cmd_data.iopbuf;
dmat[count].size = pktsize;
dmat[count].attr = SIF_DMA_ERT | SIF_DMA_INT_O;
count++;
SifWriteBackDCache(pkt, pktsize);
if (mode & SIF_CMD_M_INTR) /* INTERRUPT DMA TRANSFER */
return iSifSetDma(dmat, count);
else
return SifSetDma(dmat, count);
}
unsigned int
SifSendCmd( int command, void *send_data, int send_len,
void *extra_from, void *extra_dest, int extra_len)
{
return _SifSendCmd( command, 0, send_data, send_len,
extra_from, extra_dest, extra_len);
}
unsigned int
iSifSendCmd( int command, void *send_data, int send_len,
void *extra_from, void *extra_dest, int extra_len)
{
return _SifSendCmd( command, SIF_CMD_M_INTR, send_data, send_len,
extra_from, extra_dest, extra_len);
}
#endif
#ifdef F__sif_cmd_int_handler
int _SifCmdIntHandler(int channel)
{
u128 packet[8];
u128 *pktbuf;
struct cmd_data *cmd_data = &_sif_cmd_data;
SifCmdHeader_t *header;
SifCmdHandlerData_t *cmd_handlers;
int size, pktquads, id, i = 0;
EI();
header = (SifCmdHeader_t *)cmd_data->pktbuf;
if (!(size = header->psize))
goto out;
pktquads = (size + 30) >> 4;
header->psize = 0;
if (pktquads) {
pktbuf = (u128 *)cmd_data->pktbuf;
while (pktquads--) {
packet[i] = pktbuf[i];
i++;
}
}
iSifSetDChain();
header = (SifCmdHeader_t *)packet;
/* Get the command handler id and determine which handler list to
dispatch from. */
id = header->cid & ~SIF_CMD_ID_SYSTEM;
if (header->cid & SIF_CMD_ID_SYSTEM) {
if (id < cmd_data->nr_sys_handlers)
cmd_handlers = cmd_data->sys_cmd_handlers;
else
goto out;
}
else {
if (id < cmd_data->nr_usr_handlers)
cmd_handlers = cmd_data->usr_cmd_handlers;
else
goto out;
}
if (cmd_handlers[id].handler)
cmd_handlers[id].handler(packet, cmd_handlers[id].harg);
out:
ExitHandler();
return 0;
}
#endif
#ifdef F_sif_cmd_main
static u8 pktbuf[CMD_PACKET_MAX] __attribute__((aligned(64)));
/* Define this so that in the unlikely case another SIF implementation decides
to use it, it won't crash. Otherwise unused. */
static u8 sif_unused[64] __attribute__((aligned(64)));
static SifCmdHandlerData_t sys_cmd_handlers[SYS_CMD_HANDLER_MAX];
static SifCmdHandlerData_t usr_cmd_handlers[USR_CMD_HANDLER_MAX];
static int sregs[32];
struct cmd_data _sif_cmd_data;
static int init = 0;
static int sif0_id = -1;
struct ca_pkt {
SifCmdHeader_t header;
void *buf;
};
static void change_addr(void *packet, void *harg)
{
struct cmd_data *cmd_data = (struct cmd_data *)harg;
struct ca_pkt *pkt = (struct ca_pkt *)packet;
cmd_data->iopbuf = pkt->buf;
}
struct sr_pkt {
SifCmdHeader_t header;
u32 sreg;
int val;
};
static void set_sreg(void *packet, void *harg)
{
struct cmd_data *cmd_data = (struct cmd_data *)harg;
struct sr_pkt *pkt = (struct sr_pkt *)packet;
cmd_data->sregs[pkt->sreg] = pkt->val;
}
void SifInitCmd(void)
{
static struct ca_pkt packet __attribute((aligned(64)));
int i;
static int _rb_count = 0;
if(_rb_count != _iop_reboot_count)
{
_rb_count = _iop_reboot_count;
if (sif0_id >= 0)
{
DisableDmac(DMAC_SIF0);
RemoveDmacHandler(DMAC_SIF0, sif0_id);
}
init = 0;
}
if (init)
return;
DI();
_sif_cmd_data.pktbuf = UNCACHED_SEG(pktbuf);
_sif_cmd_data.unused = UNCACHED_SEG(sif_unused);
_sif_cmd_data.sys_cmd_handlers = sys_cmd_handlers;
_sif_cmd_data.nr_sys_handlers = SYS_CMD_HANDLER_MAX;
_sif_cmd_data.usr_cmd_handlers = usr_cmd_handlers;
_sif_cmd_data.nr_usr_handlers = USR_CMD_HANDLER_MAX;
_sif_cmd_data.sregs = sregs;
for (i = 0; i < SYS_CMD_HANDLER_MAX; i++) {
_sif_cmd_data.sys_cmd_handlers[i].handler = NULL;
_sif_cmd_data.sys_cmd_handlers[i].harg = NULL;
}
for (i = 0; i < 32; i++)
_sif_cmd_data.sregs[i] = 0;
_sif_cmd_data.sys_cmd_handlers[0].handler = change_addr;
_sif_cmd_data.sys_cmd_handlers[0].harg = &_sif_cmd_data;
_sif_cmd_data.sys_cmd_handlers[1].handler = set_sreg;
_sif_cmd_data.sys_cmd_handlers[1].harg = &_sif_cmd_data;
EI();
FlushCache(0);
if (_lw(DMAC_COMM_STAT) & STAT_SIF0)
_sw(STAT_SIF0, DMAC_COMM_STAT);
//If SIF0 (IOP -> EE) is not enabled, enable it.
if (!(_lw(DMAC_SIF0_CHCR) & CHCR_STR))
SifSetDChain();
sif0_id = AddDmacHandler(DMAC_SIF0, &_SifCmdIntHandler, 0);
EnableDmac(DMAC_SIF0);
init = 1;
_sif_cmd_data.iopbuf = (void *)SifGetReg(SIF_SYSREG_SUBADDR);
if (_sif_cmd_data.iopbuf) {
/* IOP SIF CMD is already initialized, so give it our new
receive address. */
packet.buf = _sif_cmd_data.pktbuf;
SifSendCmd(SIF_CMD_CHANGE_SADDR, &packet, sizeof packet, NULL, NULL, 0);
} else {
/* Sync with the IOP side's SIFCMD implementation. */
while (!(SifGetReg(SIF_REG_SMFLAG) & SIF_STAT_CMDINIT)) ;
_sif_cmd_data.iopbuf = (void *)SifGetReg(SIF_REG_SUBADDR);
SifSetReg(SIF_SYSREG_SUBADDR, (u32)_sif_cmd_data.iopbuf);
/* See the note above about struct cmd_data, and the use of
this register. */
SifSetReg(SIF_SYSREG_MAINADDR, (u32)&_sif_cmd_data);
packet.header.opt = 0; //SIFCMD initialization on the IOP (opt = 0)
packet.buf = _sif_cmd_data.pktbuf;
SifSendCmd(SIF_CMD_INIT_CMD, &packet, sizeof packet, NULL, NULL, 0);
}
}
void SifExitCmd(void)
{
DisableDmac(DMAC_SIF0);
RemoveDmacHandler(DMAC_SIF0, sif0_id);
init = 0;
}
#endif
#ifdef F_sif_cmd_client
SifCmdHandlerData_t *SifSetCmdBuffer(SifCmdHandlerData_t *db, int size)
{
SifCmdHandlerData_t *old;
old = _sif_cmd_data.usr_cmd_handlers;
_sif_cmd_data.usr_cmd_handlers = db;
_sif_cmd_data.nr_usr_handlers = size;
return old;
}
void SifAddCmdHandler(int pos, SifCmdHandler_t handler, void *harg)
{
SifCmdHandlerData_t *cmd_handlers;
u32 id = pos & ~SIF_CMD_ID_SYSTEM;
cmd_handlers = (pos & SIF_CMD_ID_SYSTEM) ? _sif_cmd_data.sys_cmd_handlers : _sif_cmd_data.usr_cmd_handlers;
cmd_handlers[id].handler = handler;
cmd_handlers[id].harg = harg;
}
#endif
#ifdef F_sif_cmd_remove_cmdhandler
void SifRemoveCmdHandler(int pos)
{
u32 id = pos & ~SIF_CMD_ID_SYSTEM;
if (pos & SIF_CMD_ID_SYSTEM)
_sif_cmd_data.sys_cmd_handlers[id].handler = NULL;
else
_sif_cmd_data.usr_cmd_handlers[id].handler = NULL;
}
#endif
#ifdef F_sif_sreg_get
int SifGetSreg(int sreg)
{
return _sif_cmd_data.sregs[sreg];
}
#endif