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
|
#include "boot.h"
enum
{
Maxhandler= 32+16, /* max number of interrupt handlers */
};
typedef struct Handler Handler;
struct Handler
{
void (*r)(Ureg*, void*);
void *arg;
Handler *next;
int edge;
};
struct
{
Handler *ivec[128];
Handler h[Maxhandler];
int free;
} halloc;
char *excname[] = {
"reserved 0",
"system reset",
"machine check",
"data access",
"instruction access",
"external interrupt",
"alignment",
"program exception",
"floating-point unavailable",
"decrementer",
"reserved A",
"reserved B",
"system call",
"trace trap",
"floating point assist",
"reserved F",
"software emulation",
"ITLB miss",
"DTLB miss",
"ITLB error",
"DTLB error",
};
char *regname[]={
"CAUSE", "SRR1",
"PC", "GOK",
"LR", "CR",
"XER", "CTR",
"R0", "R1",
"R2", "R3",
"R4", "R5",
"R6", "R7",
"R8", "R9",
"R10", "R11",
"R12", "R13",
"R14", "R15",
"R16", "R17",
"R18", "R19",
"R20", "R21",
"R22", "R23",
"R24", "R25",
"R26", "R27",
"R28", "R29",
"R30", "R31",
};
static void intr(Ureg*);
void
sethvec(int v, void (*r)(void))
{
ulong *vp, pa, o;
if((ulong)r & 3)
panic("sethvec");
vp = (ulong*)KADDR(v);
vp[0] = 0x7c1043a6; /* MOVW R0, SPR(SPRG0) */
vp[1] = 0x7c0802a6; /* MOVW LR, R0 */
vp[2] = 0x7c1243a6; /* MOVW R0, SPR(SPRG2) */
pa = PADDR(r);
o = pa >> 25;
if(o != 0 && o != 0x7F){
/* a branch too far: running from ROM */
vp[3] = (15<<26)|(pa>>16); /* MOVW $r&~0xFFFF, R0 */
vp[4] = (24<<26)|(pa&0xFFFF); /* OR $r&0xFFFF, R0 */
vp[5] = 0x7c0803a6; /* MOVW R0, LR */
vp[6] = 0x4e800021; /* BL (LR) */
}else
vp[3] = (18<<26)|(pa&0x3FFFFFC)|3; /* bla */
}
#define LEV(n) (((n)<<1)|1)
#define IRQ(n) (((n)<<1)|0)
void
setvec(int v, void (*r)(Ureg*, void*), void *arg)
{
Handler *h;
IMM *io;
if(halloc.free >= Maxhandler)
panic("out of interrupt handlers");
v -= VectorPIC;
h = &halloc.h[halloc.free++];
h->next = halloc.ivec[v];
h->r = r;
h->arg = arg;
halloc.ivec[v] = h;
/*
* enable corresponding interrupt in SIU/CPM
*/
io = m->iomem;
if(v >= VectorCPIC){
v -= VectorCPIC;
io->cimr |= 1<<(v&0x1F);
}
else if(v >= VectorIRQ)
io->simask |= 1<<(31-IRQ(v&7));
else
io->simask |= 1<<(31-LEV(v));
}
void
trapinit(void)
{
int i;
IMM *io;
io = m->iomem;
io->sypcr &= ~(3<<2); /* disable watchdog (821/823) */
io->simask = 0; /* mask all */
io->siel = ~0; /* edge sensitive, wake on all */
io->cicr = 0; /* disable CPM interrupts */
io->cipr = ~0; /* clear all interrupts */
io->cimr = 0; /* mask all events */
io->cicr = (0xE1<<16)|(CPIClevel<<13)|(0x1F<<8);
io->cicr |= 1 << 7; /* enable */
io->tbscrk = KEEP_ALIVE_KEY;
io->tbscr = 1; /* TBE */
io->simask |= 1<<(31-LEV(CPIClevel)); /* CPM's level */
io->tbk = KEEP_ALIVE_KEY;
eieio();
putdec(~0);
/*
* set all exceptions to trap
*/
for(i = 0x0; i < 0x3000; i += 0x100)
sethvec(i, exception);
}
void
dumpregs(Ureg *ur)
{
int i;
ulong *l;
l = &ur->cause;
for(i=0; i<sizeof regname/sizeof(char*); i+=2, l+=2)
print("%s\t%.8lux\t%s\t%.8lux\n", regname[i], l[0], regname[i+1], l[1]);
}
void
trap(Ureg *ur)
{
int c;
c = ur->cause >> 8;
switch(c){
default:
{extern int predawn; predawn = 1;}
if(c < 0 || c >= nelem(excname))
print("exception/interrupt #%x\n", c);
else
print("exception %s\n", excname[c]);
dumpregs(ur);
/* spllo(); */
print("^P to reset\n");
for(;;)
;
case 0x09: /* decrementer */
clockintr(ur, 0);
return;
case 0x05: /* external interrupt */
intr(ur);
break;
}
}
static void
intr(Ureg *ur)
{
int b, v;
Handler *h;
IMM *io;
io = m->iomem;
b = io->sivec>>2;
v = b>>1;
if(b & 1) {
if(v == CPIClevel){
io->civr = 1;
eieio();
v = VectorCPIC+(io->civr>>11);
}
}else
v += VectorIRQ;
h = halloc.ivec[v];
if(h == nil){
for(;;)
;
//print("unknown interrupt %d pc=0x%lux\n", v, ur->pc);
return;
}
if(h->edge)
io->sipend |= 1<<(31-b);
/*
* call the interrupt handlers
*/
do {
(*h->r)(ur, h->arg);
h = h->next;
} while(h != nil);
if(v >= VectorCPIC)
io->cisr |= 1<<(v-VectorCPIC);
}
|