ixp12x0_intr.c revision 1.15.30.2 1 /* $NetBSD: ixp12x0_intr.c,v 1.15.30.2 2008/01/09 01:45:20 matt Exp $ */
2
3 /*
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ichiro FUKUHARA and Naoto Shimazaki.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: ixp12x0_intr.c,v 1.15.30.2 2008/01/09 01:45:20 matt Exp $");
41
42 /*
43 * Interrupt support for the Intel ixp12x0
44 */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/simplelock.h>
50 #include <sys/termios.h>
51
52 #include <uvm/uvm_extern.h>
53
54 #include <machine/bus.h>
55 #include <machine/intr.h>
56
57 #include <arm/cpufunc.h>
58
59 #include <arm/ixp12x0/ixp12x0reg.h>
60 #include <arm/ixp12x0/ixp12x0var.h>
61 #include <arm/ixp12x0/ixp12x0_comreg.h>
62 #include <arm/ixp12x0/ixp12x0_comvar.h>
63 #include <arm/ixp12x0/ixp12x0_pcireg.h>
64
65
66 extern u_int32_t ixpcom_cr; /* current cr from *_com.c */
67 extern u_int32_t ixpcom_imask; /* tell mask to *_com.c */
68
69 /* Interrupt handler queues. */
70 struct intrq intrq[NIRQ];
71
72 /* Interrupts to mask at each level. */
73 static u_int32_t imask[NIPL];
74 static u_int32_t pci_imask[NIPL];
75
76 /* Current interrupt priority level. */
77 volatile int hardware_spl_level;
78
79 /* Software copy of the IRQs we have enabled. */
80 volatile u_int32_t intr_enabled;
81 volatile u_int32_t pci_intr_enabled;
82
83 /* Interrupts pending. */
84 static volatile int ipending;
85
86 #ifdef __HAVE_FAST_SOFTINTS
87 /*
88 * Map a software interrupt queue index (to the unused bits in the
89 * ICU registers -- XXX will need to revisit this if those bits are
90 * ever used in future steppings).
91 */
92 static const u_int32_t si_to_irqbit[SI_NQUEUES] = {
93 IXP12X0_INTR_bit30, /* SI_SOFT */
94 IXP12X0_INTR_bit29, /* SI_SOFTCLOCK */
95 IXP12X0_INTR_bit28, /* SI_SOFTNET */
96 IXP12X0_INTR_bit27, /* SI_SOFTSERIAL */
97 };
98
99 #define INT_SWMASK \
100 ((1U << IXP12X0_INTR_bit30) | (1U << IXP12X0_INTR_bit29) | \
101 (1U << IXP12X0_INTR_bit28) | (1U << IXP12X0_INTR_bit27))
102
103 #define SI_TO_IRQBIT(si) (1U << si_to_irqbit[(si)])
104
105 /*
106 * Map a software interrupt queue to an interrupt priority level.
107 */
108 static const int si_to_ipl[] = {
109 [SI_SOFTBIO] = IPL_SOFTBIO,
110 [SI_SOFTCLOCK] = IPL_SOFTCLOCK,
111 [SI_SOFTNET] = IPL_SOFTNET,
112 [SI_SOFTSERIAL] = IPL_SOFTSERIAL,
113 };
114 #endif /* __HAVE_FAST_SOFTINTS */
115
116 void ixp12x0_intr_dispatch(struct irqframe *frame);
117
118 #define IXPREG(reg) *((volatile u_int32_t*) (reg))
119
120 static inline u_int32_t
121 ixp12x0_irq_read(void)
122 {
123 return IXPREG(IXP12X0_IRQ_VBASE) & IXP12X0_INTR_MASK;
124 }
125
126 static inline u_int32_t
127 ixp12x0_pci_irq_read(void)
128 {
129 return IXPREG(IXPPCI_IRQ_STATUS);
130 }
131
132 static void
133 ixp12x0_enable_uart_irq(void)
134 {
135 ixpcom_imask = 0;
136 if (ixpcom_sc)
137 bus_space_write_4(ixpcom_sc->sc_iot, ixpcom_sc->sc_ioh,
138 IXPCOM_CR, ixpcom_cr & ~ixpcom_imask);
139 }
140
141 static void
142 ixp12x0_disable_uart_irq(void)
143 {
144 ixpcom_imask = CR_RIE | CR_XIE;
145 if (ixpcom_sc)
146 bus_space_write_4(ixpcom_sc->sc_iot, ixpcom_sc->sc_ioh,
147 IXPCOM_CR, ixpcom_cr & ~ixpcom_imask);
148 }
149
150 static void
151 ixp12x0_set_intrmask(u_int32_t irqs, u_int32_t pci_irqs)
152 {
153 if (irqs & (1U << IXP12X0_INTR_UART)) {
154 ixp12x0_disable_uart_irq();
155 } else {
156 ixp12x0_enable_uart_irq();
157 }
158 IXPREG(IXPPCI_IRQ_ENABLE_CLEAR) = pci_irqs;
159 IXPREG(IXPPCI_IRQ_ENABLE_SET) = pci_intr_enabled & ~pci_irqs;
160 }
161
162 static void
163 ixp12x0_enable_irq(int irq)
164 {
165 if (irq < SYS_NIRQ) {
166 intr_enabled |= (1U << irq);
167 switch (irq) {
168 case IXP12X0_INTR_UART:
169 ixp12x0_enable_uart_irq();
170 break;
171
172 case IXP12X0_INTR_PCI:
173 /* nothing to do */
174 break;
175 default:
176 panic("enable_irq:bad IRQ %d", irq);
177 }
178 } else {
179 pci_intr_enabled |= (1U << (irq - SYS_NIRQ));
180 IXPREG(IXPPCI_IRQ_ENABLE_SET) = (1U << (irq - SYS_NIRQ));
181 }
182 }
183
184 static inline void
185 ixp12x0_disable_irq(int irq)
186 {
187 if (irq < SYS_NIRQ) {
188 intr_enabled ^= ~(1U << irq);
189 switch (irq) {
190 case IXP12X0_INTR_UART:
191 ixp12x0_disable_uart_irq();
192 break;
193
194 case IXP12X0_INTR_PCI:
195 /* nothing to do */
196 break;
197 default:
198 /* nothing to do */
199 break;
200 }
201 } else {
202 pci_intr_enabled &= ~(1U << (irq - SYS_NIRQ));
203 IXPREG(IXPPCI_IRQ_ENABLE_CLEAR) = (1U << (irq - SYS_NIRQ));
204 }
205 }
206
207 /*
208 * NOTE: This routine must be called with interrupts disabled in the CPSR.
209 */
210 static void
211 ixp12x0_intr_calculate_masks(void)
212 {
213 struct intrq *iq;
214 struct intrhand *ih;
215 int irq, ipl;
216
217 /* First, figure out which IPLs each IRQ has. */
218 for (irq = 0; irq < NIRQ; irq++) {
219 int levels = 0;
220 iq = &intrq[irq];
221 ixp12x0_disable_irq(irq);
222 for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
223 ih = TAILQ_NEXT(ih, ih_list))
224 levels |= (1U << ih->ih_ipl);
225 iq->iq_levels = levels;
226 }
227
228 /* Next, figure out which IRQs are used by each IPL. */
229 for (ipl = 0; ipl < NIPL; ipl++) {
230 int irqs = 0;
231 int pci_irqs = 0;
232 for (irq = 0; irq < SYS_NIRQ; irq++) {
233 if (intrq[irq].iq_levels & (1U << ipl))
234 irqs |= (1U << irq);
235 }
236 imask[ipl] = irqs;
237 for (irq = 0; irq < SYS_NIRQ; irq++) {
238 if (intrq[irq + SYS_NIRQ].iq_levels & (1U << ipl))
239 pci_irqs |= (1U << irq);
240 }
241 pci_imask[ipl] = pci_irqs;
242 }
243
244 KASSERT(imask[IPL_NONE] == 0);
245 KASSERT(pci_imask[IPL_NONE] == 0);
246
247 #ifdef __HAVE_FAST_SOFTINTS
248 /*
249 * Initialize the soft interrupt masks to block themselves.
250 */
251 imask[IPL_SOFTBIO] = SI_TO_IRQBIT(SI_SOFTBIO);
252 imask[IPL_SOFTCLOCK] = SI_TO_IRQBIT(SI_SOFTCLOCK);
253 imask[IPL_SOFTNET] = SI_TO_IRQBIT(SI_SOFTNET);
254 imask[IPL_SOFTSERIAL] = SI_TO_IRQBIT(SI_SOFTSERIAL);
255 #endif
256
257 /*
258 * splsoftclock() is the only interface that users of the
259 * generic software interrupt facility have to block their
260 * soft intrs, so splsoftclock() must also block IPL_SOFT.
261 */
262 imask[IPL_SOFTCLOCK] |= imask[IPL_SOFTBIO];
263 pci_imask[IPL_SOFTCLOCK] |= pci_imask[IPL_SOFTBIO];
264
265 /*
266 * splsoftnet() must also block splsoftclock(), since we don't
267 * want timer-driven network events to occur while we're
268 * processing incoming packets.
269 */
270 imask[IPL_SOFTNET] |= imask[IPL_SOFTCLOCK];
271 pci_imask[IPL_SOFTNET] |= pci_imask[IPL_SOFTCLOCK];
272
273 /*
274 * Enforce a hierarchy that gives "slow" device (or devices with
275 * limited input buffer space/"real-time" requirements) a better
276 * chance at not dropping data.
277 */
278 imask[IPL_SOFTSERIAL] |= imask[IPL_SOFTNET];
279 pci_imask[IPL_SOFTSERIAL] |= pci_imask[IPL_SOFTNET];
280
281 /*
282 * splvm() blocks all interrupts that use the kernel memory
283 * allocation facilities.
284 */
285 imask[IPL_VM] |= imask[IPL_SOFTSERIAL];
286 pci_imask[IPL_VM] |= pci_imask[IPL_SOFTSERIAL];
287
288 /*
289 * splclock() must block anything that uses the scheduler.
290 */
291 imask[IPL_CLOCK] |= imask[IPL_VM];
292 pci_imask[IPL_CLOCK] |= pci_imask[IPL_VM];
293
294 /*
295 * splhigh() must block "everything".
296 */
297 imask[IPL_HIGH] |= imask[IPL_CLOCK];
298 pci_imask[IPL_HIGH] |= pci_imask[IPL_CLOCK];
299
300 /*
301 * Now compute which IRQs must be blocked when servicing any
302 * given IRQ.
303 */
304 for (irq = 0; irq < NIRQ; irq++) {
305 int irqs;
306 int pci_irqs;
307
308 if (irq < SYS_NIRQ) {
309 irqs = (1U << irq);
310 pci_irqs = 0;
311 } else {
312 irqs = 0;
313 pci_irqs = (1U << (irq - SYS_NIRQ));
314 }
315 iq = &intrq[irq];
316 if (TAILQ_FIRST(&iq->iq_list) != NULL)
317 ixp12x0_enable_irq(irq);
318 for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
319 ih = TAILQ_NEXT(ih, ih_list)) {
320 irqs |= imask[ih->ih_ipl];
321 pci_irqs |= pci_imask[ih->ih_ipl];
322 }
323 iq->iq_mask = irqs;
324 iq->iq_pci_mask = pci_irqs;
325 }
326 }
327
328 #ifdef __HAVE_FAST_SOFTINTS
329 static void
330 ixp12x0_do_pending(void)
331 {
332 static __cpu_simple_lock_t processing = __SIMPLELOCK_UNLOCKED;
333 int new;
334 u_int oldirqstate;
335
336 if (__cpu_simple_lock_try(&processing) == 0)
337 return;
338
339 new = curcpl();
340
341 oldirqstate = disable_interrupts(I32_bit);
342
343 #define DO_SOFTINT(si) \
344 if ((ipending & ~imask[new]) & SI_TO_IRQBIT(si)) { \
345 ipending &= ~SI_TO_IRQBIT(si); \
346 set_curcpl(si_to_ipl[(si)]); \
347 restore_interrupts(oldirqstate); \
348 softintr_dispatch(si); \
349 oldirqstate = disable_interrupts(I32_bit); \
350 set_curcpl(new); \
351 }
352
353 DO_SOFTINT(SI_SOFTSERIAL);
354 DO_SOFTINT(SI_SOFTNET);
355 DO_SOFTINT(SI_SOFTCLOCK);
356 DO_SOFTINT(SI_SOFT);
357
358 __cpu_simple_unlock(&processing);
359
360 restore_interrupts(oldirqstate);
361 }
362 #endif
363
364 inline void
365 splx(int new)
366 {
367 int old;
368 u_int oldirqstate;
369
370 oldirqstate = disable_interrupts(I32_bit);
371 old = curcpl();
372 set_curcpl(new);
373 if (new != hardware_spl_level) {
374 hardware_spl_level = new;
375 ixp12x0_set_intrmask(imask[new], pci_imask[new]);
376 }
377 restore_interrupts(oldirqstate);
378
379 #ifdef __HAVE_FAST_SOFTINTS
380 /* If there are software interrupts to process, do it. */
381 if ((ipending & INT_SWMASK) & ~imask[new])
382 ixp12x0_do_pending();
383 #endif
384 }
385
386 int
387 _splraise(int ipl)
388 {
389 int old;
390 u_int oldirqstate;
391
392 oldirqstate = disable_interrupts(I32_bit);
393 old = curcpl();
394 set_curcpl(ipl);
395 restore_interrupts(oldirqstate);
396 return (old);
397 }
398
399 int
400 _spllower(int ipl)
401 {
402 int old = curcpl();
403
404 if (old <= ipl)
405 return (old);
406 splx(ipl);
407 return (old);
408 }
409
410 #ifdef __HAVE_FAST_SOFTINTS
411 void
412 _setsoftintr(int si)
413 {
414 u_int oldirqstate;
415
416 oldirqstate = disable_interrupts(I32_bit);
417 ipending |= SI_TO_IRQBIT(si);
418 restore_interrupts(oldirqstate);
419
420 /* Process unmasked pending soft interrupts. */
421 if ((ipending & INT_SWMASK) & ~imask[curcpl()])
422 ixp12x0_do_pending();
423 }
424 #endif
425
426 /*
427 * ixp12x0_intr_init:
428 *
429 * Initialize the rest of the interrupt subsystem, making it
430 * ready to handle interrupts from devices.
431 */
432 void
433 ixp12x0_intr_init(void)
434 {
435 struct intrq *iq;
436 int i;
437
438 intr_enabled = 0;
439 pci_intr_enabled = 0;
440
441 for (i = 0; i < NIRQ; i++) {
442 iq = &intrq[i];
443 TAILQ_INIT(&iq->iq_list);
444
445 sprintf(iq->iq_name, "ipl %d", i);
446 evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
447 NULL, "ixpintr", iq->iq_name);
448 }
449 curcpu()->ci_intr_depth = 0;
450 curcpu()->ci_cpl = 0;
451 hardware_spl_level = 0;
452
453 ixp12x0_intr_calculate_masks();
454
455 /* Enable IRQs (don't yet use FIQs). */
456 enable_interrupts(I32_bit);
457 }
458
459 void *
460 ixp12x0_intr_establish(int irq, int ipl, int (*ih_func)(void *), void *arg)
461 {
462 struct intrq* iq;
463 struct intrhand* ih;
464 u_int oldirqstate;
465 #ifdef DEBUG
466 printf("ixp12x0_intr_establish(irq=%d, ipl=%d, ih_func=%08x, arg=%08x)\n",
467 irq, ipl, (u_int32_t) ih_func, (u_int32_t) arg);
468 #endif
469 if (irq < 0 || irq > NIRQ)
470 panic("ixp12x0_intr_establish: IRQ %d out of range", ipl);
471 if (ipl < 0 || ipl > NIPL)
472 panic("ixp12x0_intr_establish: IPL %d out of range", ipl);
473
474 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
475 if (ih == NULL)
476 return (NULL);
477
478 ih->ih_func = ih_func;
479 ih->ih_arg = arg;
480 ih->ih_irq = irq;
481 ih->ih_ipl = ipl;
482
483 iq = &intrq[irq];
484 iq->iq_ist = IST_LEVEL;
485
486 oldirqstate = disable_interrupts(I32_bit);
487 TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
488 ixp12x0_intr_calculate_masks();
489 restore_interrupts(oldirqstate);
490
491 return (ih);
492 }
493
494 void
495 ixp12x0_intr_disestablish(void *cookie)
496 {
497 struct intrhand* ih = cookie;
498 struct intrq* iq = &intrq[ih->ih_ipl];
499 u_int oldirqstate;
500
501 oldirqstate = disable_interrupts(I32_bit);
502 TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
503 ixp12x0_intr_calculate_masks();
504 restore_interrupts(oldirqstate);
505 }
506
507 void
508 ixp12x0_intr_dispatch(struct irqframe *frame)
509 {
510 struct intrq* iq;
511 struct intrhand* ih;
512 u_int oldirqstate;
513 int pcpl;
514 u_int32_t hwpend;
515 u_int32_t pci_hwpend;
516 int irq;
517 u_int32_t ibit;
518
519 pcpl = curcpl();
520
521 hwpend = ixp12x0_irq_read();
522 pci_hwpend = ixp12x0_pci_irq_read();
523
524 hardware_spl_level = pcpl;
525 ixp12x0_set_intrmask(imask[pcpl] | hwpend,
526 pci_imask[pcpl] | pci_hwpend);
527
528 hwpend &= ~imask[pcpl];
529 pci_hwpend &= ~pci_imask[pcpl];
530
531 while (hwpend) {
532 irq = ffs(hwpend) - 1;
533 ibit = (1U << irq);
534
535 iq = &intrq[irq];
536 iq->iq_ev.ev_count++;
537 uvmexp.intrs++;
538 for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
539 ih = TAILQ_NEXT(ih, ih_list)) {
540 int ipl;
541
542 curcpu()->ci_cpl = ipl = ih->ih_ipl;
543 oldirqstate = enable_interrupts(I32_bit);
544 (void) (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
545 restore_interrupts(oldirqstate);
546 hwpend &= ~ibit;
547 }
548 }
549 while (pci_hwpend) {
550 irq = ffs(pci_hwpend) - 1;
551 ibit = (1U << irq);
552
553 iq = &intrq[irq + SYS_NIRQ];
554 iq->iq_ev.ev_count++;
555 uvmexp.intrs++;
556 for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
557 ih = TAILQ_NEXT(ih, ih_list)) {
558 int ipl;
559
560 curcpu()->ci_cpl = ipl = ih->ih_ipl;
561 oldirqstate = enable_interrupts(I32_bit);
562 (void) (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
563 restore_interrupts(oldirqstate);
564 pci_hwpend &= ~ibit;
565 }
566 }
567
568 curcpu()->ci_cpl = pcpl;
569 hardware_spl_level = pcpl;
570 ixp12x0_set_intrmask(imask[pcpl], pci_imask[pcpl]);
571
572 #ifdef __HAVE_FAST_SOFTINTS
573 /* Check for pendings soft intrs. */
574 if ((ipending & INT_SWMASK) & ~imask[pcpl]) {
575 oldirqstate = enable_interrupts(I32_bit);
576 ixp12x0_do_pending();
577 restore_interrupts(oldirqstate);
578 }
579 #endif
580 }
581