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
|
/* $OpenBSD: i80321_intr.c,v 1.14 2010/09/20 06:33:47 matthew Exp $ */
/*
* Copyright (c) 2006 Dale Rahn <drahn@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/evcount.h>
#include <uvm/uvm.h> /* uvmexp */
#include <machine/intr.h>
#include <arm/cpufunc.h>
#include <arm/xscale/i80321reg.h>
#include <arm/xscale/i80321var.h>
/*
* autoconf glue
*/
int i80321intc_match(struct device *, void *, void *);
void i80321intc_attach(struct device *, struct device *, void *);
/* internal functions */
static void i80321intc_write_intctl(uint32_t mask);
void i80321intc_write_steer(uint32_t mask);
uint32_t i80321intc_read_intsrc(void);
void i80321intc_calc_mask(void);
void i80321intc_init(void);
void i80321intc_intr_init(void);
static void i80321intc_setipl(int new);
void i80321intc_do_pending(void);
uint32_t i80321intc_imask[NIPL];
uint32_t i80321intc_smask[NIPL];
#define SI_TO_IRQBIT(x) (1 << (x))
__volatile int current_ipl_level;
__volatile int softint_pending;
struct cfattach i80321intc_ca = {
sizeof(struct device), i80321intc_match, i80321intc_attach
};
struct cfdriver i80321intc_cd = {
NULL, "i80321intc", DV_DULL
};
int i80321intc_attached = 0;
int
i80321intc_match(struct device *parent, void *v, void *aux)
{
if (i80321intc_attached == 0)
return 1;
i80321intc_attached = 1;
return 0;
}
void
i80321intc_attach(struct device *parent, struct device *self, void *args)
{
i80321intc_init();
}
static inline void
i80321intc_write_intctl(uint32_t mask)
{
__asm__ volatile ("mcr p6, 0, %0, c0, c0, 0" : : "r" (mask));
}
void
i80321intc_write_steer(uint32_t mask)
{
__asm__ volatile ("mcr p6, 0, %0, c4, c0, 0" : : "r" (mask));
}
uint32_t
i80321intc_read_intsrc(void)
{
uint32_t mask;
__asm__ volatile ("mrc p6, 0, %0, c8, c0, 0" : "=r" (mask));
return mask;
}
static inline void
i80321intc_setipl(int new)
{
int psw;
psw = disable_interrupts(I32_bit);
current_ipl_level = new;
i80321intc_write_intctl(i80321intc_imask[new]);
restore_interrupts(psw);
}
struct intrq i80321_handler[NIRQ];
/*
* Recompute the irq mask bits.
* Must be called with interrupts disabled.
*/
void
i80321intc_calc_mask(void)
{
int irq;
struct intrhand *ih;
int i;
for (irq = 0; irq < NIRQ; irq++) {
int i;
int max = IPL_NONE;
int min = IPL_HIGH;
TAILQ_FOREACH(ih, &i80321_handler[irq].iq_list, ih_list) {
if (ih->ih_ipl > max)
max = ih->ih_ipl;
if (ih->ih_ipl < min)
min = ih->ih_ipl;
}
i80321_handler[irq].iq_irq = max;
if (max == IPL_NONE)
min = IPL_NONE; /* interrupt not enabled */
#if 0
printf("irq %d: min %x max %x\n", irq, min, max);
#endif
/* Enable interrupts at lower levels */
for (i = 0; i < min; i++)
i80321intc_imask[i] |= (1 << irq);
/* Disable interrupts at upper levels */
for (;i <= IPL_HIGH; i++)
i80321intc_imask[i] &= ~(1 << irq);
}
/* initialize soft interrupt mask */
for (i = IPL_NONE; i <= IPL_HIGH; i++) {
i80321intc_smask[i] = 0;
if (i < IPL_SOFT)
i80321intc_smask[i] |= SI_TO_IRQBIT(SI_SOFT);
if (i < IPL_SOFTCLOCK)
i80321intc_smask[i] |= SI_TO_IRQBIT(SI_SOFTCLOCK);
if (i < IPL_SOFTNET)
i80321intc_smask[i] |= SI_TO_IRQBIT(SI_SOFTNET);
if (i < IPL_SOFTTTY)
i80321intc_smask[i] |= SI_TO_IRQBIT(SI_SOFTTTY);
#if 0
printf("mask[%d]: %x %x\n", i, i80321intc_smask[i],
i80321intc_imask[i]);
#endif
}
i80321intc_setipl(current_ipl_level);
}
void
i80321intc_do_pending(void)
{
static int processing = 0;
int oldirqstate, spl_save;
oldirqstate = disable_interrupts(I32_bit);
spl_save = current_ipl_level;
if (processing == 1) {
restore_interrupts(oldirqstate);
return;
}
#define DO_SOFTINT(si, ipl) \
if ((softint_pending & i80321intc_smask[current_ipl_level]) & \
SI_TO_IRQBIT(si)) { \
softint_pending &= ~SI_TO_IRQBIT(si); \
if (current_ipl_level < ipl) \
i80321intc_setipl(ipl); \
restore_interrupts(oldirqstate); \
softintr_dispatch(si); \
oldirqstate = disable_interrupts(I32_bit); \
i80321intc_setipl(spl_save); \
}
do {
DO_SOFTINT(SI_SOFTTTY, IPL_SOFTTTY);
DO_SOFTINT(SI_SOFTNET, IPL_SOFTNET);
DO_SOFTINT(SI_SOFTCLOCK, IPL_SOFTCLOCK);
DO_SOFTINT(SI_SOFT, IPL_SOFT);
} while (softint_pending & i80321intc_smask[current_ipl_level]);
processing = 0;
restore_interrupts(oldirqstate);
}
void
splx(int new)
{
i80321intc_setipl(new);
if (softint_pending & i80321intc_smask[current_ipl_level])
i80321intc_do_pending();
}
int
_spllower(int new)
{
int old = current_ipl_level;
splx(new);
return (old);
}
int
_splraise(int new)
{
int old;
old = current_ipl_level;
/*
* setipl must always be called because there is a race window
* where the variable is updated before the mask is set
* an interrupt occurs in that window without the mask always
* being set, the hardware might not get updated on the next
* splraise completely messing up spl protection.
*/
if (old > new)
new = old;
i80321intc_setipl(new);
return (old);
}
void
_setsoftintr(int si)
{
int oldirqstate;
oldirqstate = disable_interrupts(I32_bit);
softint_pending |= SI_TO_IRQBIT(si);
restore_interrupts(oldirqstate);
/* Process unmasked pending soft interrupts. */
if (softint_pending & i80321intc_smask[current_ipl_level])
i80321intc_do_pending();
}
/*
* i80321_icu_init:
*
* Initialize the i80321 ICU. Called early in bootstrap
* to make sure the ICU is in a pristine state.
*/
void
i80321intc_intr_init(void)
{
i80321intc_write_intctl(0);
i80321intc_write_steer(0);
}
/*
* i80321_intr_init:
*
* Initialize the rest of the interrupt subsystem, making it
* ready to handle interrupts from devices.
*/
void
i80321intc_init(void)
{
struct intrq *iq;
int i;
for (i = 0; i < NIRQ; i++) {
iq = &i80321_handler[i];
TAILQ_INIT(&iq->iq_list);
}
i80321intc_calc_mask();
/* Enable IRQs (don't yet use FIQs). */
enable_interrupts(I32_bit);
}
void *
i80321_intr_establish(int irq, int ipl, int (*func)(void *), void *arg,
const char *name)
{
struct intrq *iq;
struct intrhand *ih;
uint32_t oldirqstate;
if (irq < 0 || irq > NIRQ)
panic("i80321_intr_establish: IRQ %d out of range", irq);
ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
if (ih == NULL)
return (NULL);
ih->ih_func = func;
ih->ih_arg = arg;
ih->ih_ipl = ipl;
ih->ih_name = name;
ih->ih_irq = irq;
iq = &i80321_handler[irq];
if (name != NULL)
evcount_attach(&ih->ih_count, name, &ih->ih_irq);
/* All IOP321 interrupts are level-triggered. */
iq->iq_ist = IST_LEVEL;
oldirqstate = disable_interrupts(I32_bit);
TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
i80321intc_calc_mask();
restore_interrupts(oldirqstate);
return (ih);
}
void
i80321_intr_disestablish(void *cookie)
{
struct intrhand *ih = cookie;
struct intrq *iq = &i80321_handler[ih->ih_irq];
int oldirqstate;
oldirqstate = disable_interrupts(I32_bit);
TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
if (ih->ih_name != NULL)
evcount_detach(&ih->ih_count);
i80321intc_calc_mask();
restore_interrupts(oldirqstate);
}
void
i80321_irq_handler(void *arg)
{
struct clockframe *frame = arg;
uint32_t hwpend;
int irq;
int saved_spl_level;
struct intrhand *ih;
saved_spl_level = current_ipl_level;
/* get pending IRQs */
hwpend = i80321intc_read_intsrc();
while ((irq = find_first_bit(hwpend)) >= 0) {
/* XXX: Should we handle IRQs in priority order? */
/* raise spl to stop interrupts of lower priorities */
if (saved_spl_level < i80321_handler[irq].iq_irq)
i80321intc_setipl(i80321_handler[irq].iq_irq);
/* Enable interrupt */
enable_interrupts(I32_bit);
TAILQ_FOREACH(ih, &i80321_handler[irq].iq_list, ih_list) {
if ((ih->ih_func)( ih->ih_arg == 0
? frame : ih->ih_arg))
ih->ih_count.ec_count++;
}
/* Disable interrupt */
disable_interrupts(I32_bit);
hwpend &= ~(1<<irq);
}
uvmexp.intrs++;
/* restore spl to that was when this interrupt happen */
i80321intc_setipl(saved_spl_level);
if(softint_pending & i80321intc_smask[current_ipl_level])
i80321intc_do_pending();
}
#ifdef DIAGNOSTIC
void
i80321_splassert_check(int wantipl, const char *func)
{
int oldipl = current_ipl_level;
if (oldipl < wantipl) {
splassert_fail(wantipl, oldipl, func);
/*
* If the splassert_ctl is set to not panic, raise the ipl
* in a feeble attempt to reduce damage.
*/
i80321intc_setipl(wantipl);
}
}
#endif
|