1 /* $NetBSD: intr.c,v 1.20 2008/01/02 11:48:27 ad Exp $ */
4 * Copyright 2002 Wasabi Systems, Inc.
7 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.20 2008/01/02 11:48:27 ad Exp $");
41 #include <sys/param.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/evcnt.h>
46 #include <uvm/uvm_extern.h>
48 #include <machine/intr.h>
49 #include <machine/psl.h>
51 #include <powerpc/cpu.h>
52 #include <powerpc/spr.h>
56 * Number of interrupts (hard + soft), irq number legality test,
57 * mapping of irq number to mask and a way to pick irq number
58 * off a mask of active intrs.
61 #define LEGAL_IRQ(x) ((x) >= 0 && (x) < ICU_LEN)
63 #define IRQ_TO_MASK(irq) (0x80000000UL >> (irq))
64 #define IRQ_OF_MASK(mask) cntlzw(mask)
67 * Assign these to unused (reserved) interrupt bits.
69 * For 405GP (and 403CGX?) interrupt bits 0-18 and 25-31 are used
70 * by hardware. This leaves us bits 19-24 for software.
72 #define IRQ_SOFTNET 19
73 #define IRQ_SOFTCLOCK 20
74 #define IRQ_SOFTSERIAL 21
76 #define IRQ_STATCLOCK 23
79 * Platform specific code may override any of the above.
83 #include <powerpc/ibm4xx/dcr403cgx.h>
84 #define INTR_STATUS DCR_EXISR
85 #define INTR_ACK DCR_EXISR
86 #define INTR_ENABLE DCR_EXIER
88 #elif defined(__virtex__)
90 #include <evbppc/virtex/dev/xintcreg.h>
91 #define INTR_STATUS XINTC_ISR
92 #define INTR_ACK XINTC_IAR
93 #define INTR_ENABLE XINTC_IER
94 #define INTR_MASTER XINTC_MER
95 #define INTR_MASTER_EN (MER_HIE|MER_ME)
100 #undef IRQ_SOFTSERIAL
103 #define IRQ_TO_MASK(i) (1 << (i)) /* Redefine mappings */
104 #define IRQ_OF_MASK(m) (31 - cntlzw(m))
105 #define IRQ_SOFTNET 31 /* Redefine "unused" pins */
106 #define IRQ_SOFTCLOCK 30
107 #define IRQ_SOFTSERIAL 29
109 #define IRQ_STATCLOCK 27
111 #else /* Generic 405 Universal Interrupt Controller */
113 #include <powerpc/ibm4xx/dcr405gp.h>
114 #define INTR_STATUS DCR_UIC0_MSR
115 #define INTR_ACK DCR_UIC0_SR
116 #define INTR_ENABLE DCR_UIC0_ER
120 #define MASK_SOFTNET IRQ_TO_MASK(IRQ_SOFTNET)
121 #define MASK_SOFTCLOCK IRQ_TO_MASK(IRQ_SOFTCLOCK)
122 #define MASK_SOFTSERIAL IRQ_TO_MASK(IRQ_SOFTSERIAL)
123 #define MASK_STATCLOCK IRQ_TO_MASK(IRQ_STATCLOCK)
124 #define MASK_CLOCK (IRQ_TO_MASK(IRQ_CLOCK) | IRQ_TO_MASK(IRQ_STATCLOCK))
125 #define MASK_SOFTINTR (MASK_SOFTCLOCK|MASK_SOFTNET|MASK_SOFTSERIAL)
126 #define MASK_HARDINTR ~(MASK_SOFTINTR|MASK_CLOCK)
128 static inline void disable_irq(int);
129 static inline void enable_irq(int);
130 static void intr_calculatemasks(void);
131 static void do_pending_int(void);
132 static const char *intr_typename(int);
136 * Interrupt handler chains. intr_establish() inserts a handler into
137 * the list. The handler is called with its (single) argument.
140 int (*ih_fun
)(void *);
142 struct intrhand
*ih_next
;
147 struct evcnt is_evcnt
;
148 struct intrhand
*is_head
;
150 int is_level
; /* spls bitmask */
151 int is_type
; /* sensitivity */
155 volatile u_int imask
[NIPL
];
156 const int mask_clock
= MASK_CLOCK
;
157 const int mask_statclock
= MASK_STATCLOCK
;
159 static struct intrsrc intrs
[ICU_LEN
] = {
160 #define DEFINTR(name) \
161 { EVCNT_INITIALIZER(EVCNT_TYPE_INTR, NULL, "uic", name), NULL, 0, 0 }
163 DEFINTR("pin0"), DEFINTR("pin1"), DEFINTR("pin2"),
164 DEFINTR("pin3"), DEFINTR("pin4"), DEFINTR("pin5"),
165 DEFINTR("pin6"), DEFINTR("pin7"), DEFINTR("pin8"),
166 DEFINTR("pin9"), DEFINTR("pin10"), DEFINTR("pin11"),
167 DEFINTR("pin12"), DEFINTR("pin13"), DEFINTR("pin14"),
168 DEFINTR("pin15"), DEFINTR("pin16"), DEFINTR("pin17"),
171 /* Reserved intrs, accounted in cpu_info */
172 DEFINTR(NULL
), /* unused "pin19", softnet */
173 DEFINTR(NULL
), /* unused "pin20", softclock */
174 DEFINTR(NULL
), /* unused "pin21", softserial */
175 DEFINTR(NULL
), /* unused "pin22", PIT hardclock */
176 DEFINTR(NULL
), /* unused "pin23", FIT statclock */
178 DEFINTR("pin24"), DEFINTR("pin25"), DEFINTR("pin26"),
179 DEFINTR("pin27"), DEFINTR("pin28"), DEFINTR("pin29"),
180 DEFINTR("pin30"), DEFINTR("pin31")
186 /* Write External Enable Immediate */
187 #define wrteei(en) __asm volatile ("wrteei %0" : : "K"(en))
189 /* Enforce In Order Execution Of I/O */
190 #define eieio() __asm volatile ("eieio")
193 * Set up interrupt mapping array.
200 for (i
= 0; i
< ICU_LEN
; i
++)
209 evcnt_attach_static(&intrs
[i
].is_evcnt
);
212 /* Initialized in powerpc/ibm4xx/cpu.c */
213 evcnt_attach_static(&curcpu()->ci_ev_softclock
);
214 evcnt_attach_static(&curcpu()->ci_ev_softnet
);
215 evcnt_attach_static(&curcpu()->ci_ev_softserial
);
217 mtdcr(INTR_ENABLE
, 0x00000000); /* mask all */
218 mtdcr(INTR_ACK
, 0xffffffff); /* acknowledge all */
220 mtdcr(INTR_MASTER
, INTR_MASTER_EN
); /* enable controller */
225 * external interrupt handler
230 struct cpu_info
*ci
= curcpu();
232 int i
, bits_to_clear
;
240 int_state
= mfdcr(INTR_STATUS
); /* Read non-masked interrupt status */
241 bits_to_clear
= int_state
;
244 i
= IRQ_OF_MASK(int_state
);
246 r_imen
= IRQ_TO_MASK(i
);
247 int_state
&= ~r_imen
;
249 if ((pcpl
& r_imen
) != 0) {
250 /* Masked! Mark as pending */
251 ci
->ci_ipending
|= r_imen
;
255 splraise(intrs
[i
].is_mask
);
256 if (intrs
[i
].is_type
== IST_LEVEL
)
260 ih
= intrs
[i
].is_head
;
262 if (ih
->ih_level
== IPL_VM
)
263 KERNEL_LOCK(1, NULL
);
264 (*ih
->ih_fun
)(ih
->ih_arg
);
266 if (ih
->ih_level
== IPL_VM
)
267 KERNEL_UNLOCK_ONE(NULL
);
271 if (intrs
[i
].is_type
== IST_LEVEL
)
275 intrs
[i
].is_evcnt
.ev_count
++;
279 mtdcr(INTR_ACK
, bits_to_clear
); /* Acknowledge all pending interrupts */
291 mask
= omask
= mfdcr(INTR_ENABLE
);
292 mask
&= ~IRQ_TO_MASK(irq
);
295 mtdcr(INTR_ENABLE
, mask
);
297 printf("irq_disable: irq=%d, mask=%08x\n",irq
,mask
);
306 mask
= omask
= mfdcr(INTR_ENABLE
);
307 mask
|= IRQ_TO_MASK(irq
);
310 mtdcr(INTR_ENABLE
, mask
);
312 printf("enable_irq: irq=%d, mask=%08x\n",irq
,mask
);
317 intr_typename(int type
)
326 return ("edge-triggered");
328 return ("level-triggered");
330 panic("intr_typename: invalid type %d", type
);
335 * Register an interrupt handler.
338 intr_establish(int irq
, int type
, int level
, int (*ih_fun
)(void *),
344 if (! LEGAL_IRQ(irq
))
345 panic("intr_establish: bogus irq %d", irq
);
347 if (type
== IST_NONE
)
348 panic("intr_establish: bogus type %d for irq %d", type
, irq
);
350 /* No point in sleeping unless someone can free memory. */
351 ih
= malloc(sizeof *ih
, M_DEVBUF
, cold
? M_NOWAIT
: M_WAITOK
);
353 panic("intr_establish: can't malloc handler info");
355 switch (intrs
[irq
].is_type
) {
357 intrs
[irq
].is_type
= type
;
362 if (type
== intrs
[irq
].is_type
)
367 if (type
!= IST_NONE
)
368 panic("intr_establish: can't share %s with %s",
369 intr_typename(intrs
[irq
].is_type
),
370 intr_typename(type
));
375 * We're not on critical paths, so just block intrs for a while.
376 * Note that spl*() at this point would use old (wrong) masks.
382 * Poke the real handler in now. We deliberately don't preserve order,
383 * the user is not allowed to make any assumptions about it anyway.
387 ih
->ih_level
= level
;
388 ih
->ih_next
= intrs
[irq
].is_head
;
389 intrs
[irq
].is_head
= ih
;
391 intr_calculatemasks();
397 printf("***** intr_establish: irq%d h=%p arg=%p\n",irq
, ih_fun
, ih_arg
);
403 * Deregister an interrupt handler.
406 intr_disestablish(void *arg
)
408 struct intrhand
*ih
= arg
;
412 /* Lookup the handler. This is expensive, but not run often. */
413 for (i
= 0; i
< ICU_LEN
; i
++)
414 for (p
= &intrs
[i
].is_head
; *p
!= NULL
; p
= &(*p
)->ih_next
)
419 panic("intr_disestablish: handler not registered");
426 intr_calculatemasks();
429 if (intrs
[i
].is_head
== NULL
)
430 intrs
[i
].is_type
= IST_NONE
;
434 * Recalculate the interrupt masks from scratch.
435 * We could code special registry and deregistry versions of this function that
436 * would be faster, but the code would be nastier, and we don't expect this to
437 * happen very much anyway. We assume PSL_EE is clear when we're called.
440 intr_calculatemasks(void)
445 /* First, figure out which levels each IRQ uses. */
446 for (irq
= 0; irq
< ICU_LEN
; irq
++) {
447 register int levels
= 0;
448 for (q
= intrs
[irq
].is_head
; q
; q
= q
->ih_next
)
449 levels
|= 1 << q
->ih_level
;
450 intrs
[irq
].is_level
= levels
;
453 /* Then figure out which IRQs use each level. */
454 for (level
= 0; level
< NIPL
; level
++) {
455 register int irqs
= 0;
456 for (irq
= 0; irq
< ICU_LEN
; irq
++)
457 if (intrs
[irq
].is_level
& (1 << level
))
458 irqs
|= IRQ_TO_MASK(irq
);
459 imask
[level
] = irqs
| MASK_SOFTINTR
;
463 * Enforce a hierarchy that gives slow devices a better chance at not
468 * Initialize the soft interrupt masks to block themselves.
471 imask
[IPL_SOFTCLOCK
] |= MASK_SOFTCLOCK
;
472 imask
[IPL_SOFTNET
] |= imask
[IPL_SOFTCLOCK
] | MASK_SOFTNET
;
473 imask
[IPL_SOFTSERIAL
] = imask
[IPL_SOFTNET
] | MASK_SOFTSERIAL
;
474 imask
[IPL_VM
] |= imask
[IPL_SOFTSERIAL
];
475 imask
[IPL_SCHED
] = imask
[IPL_VM
] | MASK_CLOCK
| MASK_STATCLOCK
;
476 imask
[IPL_HIGH
] |= imask
[IPL_SCHED
];
478 /* And eventually calculate the complete masks. */
479 for (irq
= 0; irq
< ICU_LEN
; irq
++) {
480 register int irqs
= IRQ_TO_MASK(irq
);
481 for (q
= intrs
[irq
].is_head
; q
; q
= q
->ih_next
)
482 irqs
|= imask
[q
->ih_level
];
483 intrs
[irq
].is_mask
= irqs
;
486 for (irq
= 0; irq
< ICU_LEN
; irq
++)
487 if (intrs
[irq
].is_head
!= NULL
)
496 struct cpu_info
*ci
= curcpu();
505 #ifdef __HAVE_FAST_SOFTINTS
506 #error don't count soft interrupts
513 pcpl = ci->ci_cpl; /* Turn off all */
514 #ifdef __HAVE_FAST_SOFTINTS
517 while ((hwpend = ci->ci_ipending & ~pcpl & MASK_HARDINTR) != 0) {
518 irq = IRQ_OF_MASK(hwpend);
519 if (intrs[irq].is_type != IST_LEVEL)
522 ci->ci_ipending &= ~IRQ_TO_MASK(irq);
524 splraise(intrs[irq].is_mask);
527 ih = intrs[irq].is_head;
529 if (ih->ih_level == IPL_VM)
530 KERNEL_LOCK(1, NULL);
531 (*ih->ih_fun)(ih->ih_arg);
532 if (ih->ih_level == IPL_VM)
533 KERNEL_UNLOCK_ONE(NULL);
538 if (intrs[irq].is_type == IST_LEVEL)
541 intrs[irq].is_evcnt.ev_count++;
543 #ifdef __HAVE_FAST_SOFTINTS
544 if ((ci->ci_ipending & ~pcpl) & MASK_SOFTSERIAL) {
545 ci->ci_ipending &= ~MASK_SOFTSERIAL;
548 softintr__run(IPL_SOFTSERIAL);
551 ci->ci_ev_softserial.ev_count++;
554 if ((ci->ci_ipending & ~pcpl) & MASK_SOFTNET) {
555 ci->ci_ipending &= ~MASK_SOFTNET;
558 softintr__run(IPL_SOFTNET);
561 ci->ci_ev_softnet.ev_count++;
564 if ((ci->ci_ipending & ~pcpl) & MASK_SOFTCLOCK) {
565 ci->ci_ipending &= ~MASK_SOFTCLOCK;
568 softintr__run(IPL_SOFTCLOCK);
571 ci->ci_ev_softclock.ev_count++;
575 ci->ci_cpl = pcpl; /* Don't use splx... we are here already! */
580 #ifdef __HAVE_FAST_SOFTINTS
584 static const int softmap
[3] = {
585 MASK_SOFTCLOCK
, MASK_SOFTNET
, MASK_SOFTSERIAL
589 KASSERT(idx
>= 0 && idx
< 3);
592 * This could be implemented with lwarx/stwcx to avoid the
599 curcpu()->ci_ipending
|= softmap
[idx
];
608 struct cpu_info
*ci
= curcpu();
612 * We're about to block some intrs, so make sure they don't
613 * fire while we're busy.
620 ci
->ci_cpl
|= newcpl
;
629 struct cpu_info
*ci
= curcpu();
632 if (ci
->ci_ipending
& ~newcpl
)
639 struct cpu_info
*ci
= curcpu();
644 if (ci
->ci_ipending
& ~newcpl
)