footbridge_irqhandler.c revision 1.6 1 1.6 chris /* $NetBSD: footbridge_irqhandler.c,v 1.6 2002/11/03 21:43:31 chris Exp $ */
2 1.1 chris
3 1.1 chris /*
4 1.1 chris * Copyright (c) 1994-1998 Mark Brinicombe.
5 1.1 chris * Copyright (c) 1997 Causality Limited
6 1.1 chris * Copyright (c) 1994 Brini.
7 1.1 chris * All rights reserved.
8 1.1 chris *
9 1.1 chris * Redistribution and use in source and binary forms, with or without
10 1.1 chris * modification, are permitted provided that the following conditions
11 1.1 chris * are met:
12 1.1 chris * 1. Redistributions of source code must retain the above copyright
13 1.1 chris * notice, this list of conditions and the following disclaimer.
14 1.1 chris * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 chris * notice, this list of conditions and the following disclaimer in the
16 1.1 chris * documentation and/or other materials provided with the distribution.
17 1.1 chris * 3. All advertising materials mentioning features or use of this software
18 1.1 chris * must display the following acknowledgement:
19 1.1 chris * This product includes software developed by Mark Brinicombe
20 1.1 chris * for the NetBSD Project.
21 1.1 chris * 4. The name of the company nor the name of the author may be used to
22 1.1 chris * endorse or promote products derived from this software without specific
23 1.1 chris * prior written permission.
24 1.1 chris *
25 1.1 chris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 1.1 chris * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 1.1 chris * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 1.1 chris * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 1.1 chris * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 1.1 chris * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 1.1 chris * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 chris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 chris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 chris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 chris * SUCH DAMAGE.
36 1.1 chris *
37 1.1 chris * from: iomd_irqhandler.c,v 1.16 $
38 1.1 chris */
39 1.1 chris
40 1.6 chris #ifndef ARM_SPL_NOINLINE
41 1.6 chris #define ARM_SPL_NOINLINE
42 1.6 chris #endif
43 1.6 chris
44 1.6 chris #include <sys/cdefs.h>
45 1.6 chris #ifndef __lint
46 1.6 chris __RCSID("$NetBSD: footbridge_irqhandler.c,v 1.6 2002/11/03 21:43:31 chris Exp $");
47 1.6 chris #endif /* !__lint */
48 1.6 chris
49 1.1 chris #include "opt_irqstats.h"
50 1.1 chris
51 1.1 chris #include <sys/param.h>
52 1.1 chris #include <sys/systm.h>
53 1.1 chris #include <sys/malloc.h>
54 1.1 chris #include <uvm/uvm_extern.h>
55 1.1 chris
56 1.2 matt #include <machine/intr.h>
57 1.1 chris #include <machine/cpu.h>
58 1.6 chris #include <arm/footbridge/dc21285mem.h>
59 1.6 chris #include <arm/footbridge/dc21285reg.h>
60 1.6 chris
61 1.6 chris #include <dev/pci/pcivar.h>
62 1.6 chris
63 1.6 chris #include "isa.h"
64 1.6 chris #if NISA > 0
65 1.6 chris #include <dev/isa/isavar.h>
66 1.6 chris #endif
67 1.6 chris
68 1.6 chris /* Interrupt handler queues. */
69 1.6 chris static struct intrq footbridge_intrq[NIRQ];
70 1.6 chris
71 1.6 chris /* Interrupts to mask at each level. */
72 1.6 chris int footbridge_imask[NIPL];
73 1.6 chris
74 1.6 chris /* Software copy of the IRQs we have enabled. */
75 1.6 chris __volatile uint32_t intr_enabled;
76 1.1 chris
77 1.6 chris /* Current interrupt priority level */
78 1.6 chris __volatile int current_spl_level;
79 1.1 chris
80 1.6 chris /* Interrupts pending */
81 1.6 chris __volatile int footbridge_ipending;
82 1.3 chris
83 1.6 chris void footbridge_intr_dispatch(struct clockframe *frame);
84 1.1 chris
85 1.6 chris const struct evcnt *footbridge_pci_intr_evcnt __P((void *, pci_intr_handle_t));
86 1.1 chris
87 1.6 chris void footbridge_do_pending(void);
88 1.1 chris
89 1.6 chris static const uint32_t si_to_irqbit[SI_NQUEUES] =
90 1.6 chris { IRQ_SOFTINT,
91 1.6 chris IRQ_RESERVED0,
92 1.6 chris IRQ_RESERVED1,
93 1.6 chris IRQ_RESERVED2 };
94 1.1 chris
95 1.6 chris #define SI_TO_IRQBIT(si) (1U << si_to_irqbit[(si)])
96 1.1 chris
97 1.6 chris /*
98 1.6 chris * Map a software interrupt queue to an interrupt priority level.
99 1.6 chris */
100 1.6 chris static const int si_to_ipl[SI_NQUEUES] = {
101 1.6 chris IPL_SOFT, /* SI_SOFT */
102 1.6 chris IPL_SOFTCLOCK, /* SI_SOFTCLOCK */
103 1.6 chris IPL_SOFTNET, /* SI_SOFTNET */
104 1.6 chris IPL_SOFTSERIAL, /* SI_SOFTSERIAL */
105 1.6 chris };
106 1.6 chris
107 1.6 chris const struct evcnt *
108 1.6 chris footbridge_pci_intr_evcnt(pcv, ih)
109 1.6 chris void *pcv;
110 1.6 chris pci_intr_handle_t ih;
111 1.6 chris {
112 1.6 chris /* XXX check range is valid */
113 1.6 chris #if NISA > 0
114 1.6 chris if (ih >= 0x80 && ih <= 0x8f) {
115 1.6 chris return isa_intr_evcnt(NULL, (ih & 0x0f));
116 1.6 chris }
117 1.6 chris #endif
118 1.6 chris return &footbridge_intrq[ih].iq_ev;
119 1.6 chris }
120 1.6 chris
121 1.6 chris static __inline void
122 1.6 chris footbridge_enable_irq(int irq)
123 1.6 chris {
124 1.6 chris intr_enabled |= (1U << irq);
125 1.6 chris
126 1.6 chris footbridge_set_intrmask();
127 1.1 chris }
128 1.1 chris
129 1.6 chris static __inline void
130 1.6 chris footbridge_disable_irq(int irq)
131 1.1 chris {
132 1.6 chris intr_enabled &= ~(1U << irq);
133 1.6 chris footbridge_set_intrmask();
134 1.1 chris }
135 1.1 chris
136 1.1 chris /*
137 1.6 chris * NOTE: This routine must be called with interrupts disabled in the CPSR.
138 1.1 chris */
139 1.6 chris static void
140 1.6 chris footbridge_intr_calculate_masks(void)
141 1.1 chris {
142 1.6 chris struct intrq *iq;
143 1.6 chris struct intrhand *ih;
144 1.6 chris int irq, ipl;
145 1.6 chris
146 1.6 chris /* First, figure out which IPLs each IRQ has. */
147 1.6 chris for (irq = 0; irq < NIRQ; irq++) {
148 1.6 chris int levels = 0;
149 1.6 chris iq = &footbridge_intrq[irq];
150 1.6 chris footbridge_disable_irq(irq);
151 1.6 chris for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
152 1.6 chris ih = TAILQ_NEXT(ih, ih_list))
153 1.6 chris levels |= (1U << ih->ih_ipl);
154 1.6 chris iq->iq_levels = levels;
155 1.6 chris }
156 1.1 chris
157 1.6 chris /* Next, figure out which IRQs are used by each IPL. */
158 1.6 chris for (ipl = 0; ipl < NIPL; ipl++) {
159 1.6 chris int irqs = 0;
160 1.6 chris for (irq = 0; irq < NIRQ; irq++) {
161 1.6 chris if (footbridge_intrq[irq].iq_levels & (1U << ipl))
162 1.6 chris irqs |= (1U << irq);
163 1.6 chris }
164 1.6 chris footbridge_imask[ipl] = irqs;
165 1.6 chris }
166 1.1 chris
167 1.6 chris /* IPL_NONE must open up all interrupts */
168 1.6 chris footbridge_imask[IPL_NONE] = 0;
169 1.1 chris
170 1.6 chris /*
171 1.6 chris * Initialize the soft interrupt masks to block themselves.
172 1.6 chris */
173 1.6 chris footbridge_imask[IPL_SOFT] = SI_TO_IRQBIT(SI_SOFT);
174 1.6 chris footbridge_imask[IPL_SOFTCLOCK] = SI_TO_IRQBIT(SI_SOFTCLOCK);
175 1.6 chris footbridge_imask[IPL_SOFTNET] = SI_TO_IRQBIT(SI_SOFTNET);
176 1.6 chris footbridge_imask[IPL_SOFTSERIAL] = SI_TO_IRQBIT(SI_SOFTSERIAL);
177 1.1 chris
178 1.6 chris footbridge_imask[IPL_SOFTCLOCK] |= footbridge_imask[IPL_SOFT];
179 1.6 chris footbridge_imask[IPL_SOFTNET] |= footbridge_imask[IPL_SOFTCLOCK];
180 1.1 chris
181 1.6 chris /*
182 1.6 chris * Enforce a heirarchy that gives "slow" device (or devices with
183 1.6 chris * limited input buffer space/"real-time" requirements) a better
184 1.6 chris * chance at not dropping data.
185 1.6 chris */
186 1.6 chris footbridge_imask[IPL_BIO] |= footbridge_imask[IPL_SOFTNET];
187 1.6 chris footbridge_imask[IPL_NET] |= footbridge_imask[IPL_BIO];
188 1.6 chris footbridge_imask[IPL_SOFTSERIAL] |= footbridge_imask[IPL_NET];
189 1.1 chris
190 1.6 chris footbridge_imask[IPL_TTY] |= footbridge_imask[IPL_SOFTSERIAL];
191 1.1 chris
192 1.6 chris /*
193 1.6 chris * splvm() blocks all interrupts that use the kernel memory
194 1.6 chris * allocation facilities.
195 1.6 chris */
196 1.6 chris footbridge_imask[IPL_IMP] |= footbridge_imask[IPL_TTY];
197 1.1 chris
198 1.1 chris /*
199 1.6 chris * Audio devices are not allowed to perform memory allocation
200 1.6 chris * in their interrupt routines, and they have fairly "real-time"
201 1.6 chris * requirements, so give them a high interrupt priority.
202 1.1 chris */
203 1.6 chris footbridge_imask[IPL_AUDIO] |= footbridge_imask[IPL_IMP];
204 1.1 chris
205 1.1 chris /*
206 1.6 chris * splclock() must block anything that uses the scheduler.
207 1.1 chris */
208 1.6 chris footbridge_imask[IPL_CLOCK] |= footbridge_imask[IPL_AUDIO];
209 1.1 chris
210 1.1 chris /*
211 1.6 chris * footbridge has seperate statclock.
212 1.6 chris */
213 1.6 chris footbridge_imask[IPL_STATCLOCK] |= footbridge_imask[IPL_CLOCK];
214 1.1 chris
215 1.1 chris /*
216 1.6 chris * splhigh() must block "everything".
217 1.6 chris */
218 1.6 chris footbridge_imask[IPL_HIGH] |= footbridge_imask[IPL_STATCLOCK];
219 1.1 chris
220 1.6 chris /*
221 1.6 chris * XXX We need serial drivers to run at the absolute highest priority
222 1.6 chris * in order to avoid overruns, so serial > high.
223 1.6 chris */
224 1.6 chris footbridge_imask[IPL_SERIAL] |= footbridge_imask[IPL_HIGH];
225 1.1 chris
226 1.1 chris /*
227 1.6 chris * Calculate the ipl level to go to when handling this interrupt
228 1.6 chris */
229 1.6 chris for (irq = 0; irq < NIRQ; irq++) {
230 1.6 chris int irqs = (1U << irq);
231 1.6 chris iq = &footbridge_intrq[irq];
232 1.6 chris if (TAILQ_FIRST(&iq->iq_list) != NULL)
233 1.6 chris footbridge_enable_irq(irq);
234 1.6 chris for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
235 1.6 chris ih = TAILQ_NEXT(ih, ih_list))
236 1.6 chris irqs |= footbridge_imask[ih->ih_ipl];
237 1.6 chris iq->iq_mask = irqs;
238 1.1 chris }
239 1.6 chris }
240 1.6 chris
241 1.6 chris int
242 1.6 chris _splraise(int ipl)
243 1.6 chris {
244 1.6 chris return (footbridge_splraise(ipl));
245 1.6 chris }
246 1.1 chris
247 1.6 chris /* this will always take us to the ipl passed in */
248 1.6 chris void
249 1.6 chris splx(int new)
250 1.6 chris {
251 1.6 chris footbridge_splx(new);
252 1.6 chris }
253 1.1 chris
254 1.6 chris int
255 1.6 chris _spllower(int ipl)
256 1.6 chris {
257 1.6 chris return (footbridge_spllower(ipl));
258 1.1 chris }
259 1.1 chris
260 1.6 chris __inline void
261 1.6 chris footbridge_do_pending(void)
262 1.6 chris {
263 1.6 chris static __cpu_simple_lock_t processing = __SIMPLELOCK_UNLOCKED;
264 1.6 chris uint32_t new, oldirqstate;
265 1.1 chris
266 1.6 chris if (__cpu_simple_lock_try(&processing) == 0)
267 1.6 chris return;
268 1.1 chris
269 1.6 chris new = current_spl_level;
270 1.6 chris
271 1.6 chris oldirqstate = disable_interrupts(I32_bit);
272 1.1 chris
273 1.6 chris #define DO_SOFTINT(si) \
274 1.6 chris if ((footbridge_ipending & ~new) & SI_TO_IRQBIT(si)) { \
275 1.6 chris footbridge_ipending &= ~SI_TO_IRQBIT(si); \
276 1.6 chris current_spl_level |= footbridge_imask[si_to_ipl[(si)]]; \
277 1.6 chris restore_interrupts(oldirqstate); \
278 1.6 chris softintr_dispatch(si); \
279 1.6 chris oldirqstate = disable_interrupts(I32_bit); \
280 1.6 chris current_spl_level = new; \
281 1.1 chris }
282 1.6 chris DO_SOFTINT(SI_SOFTSERIAL);
283 1.6 chris DO_SOFTINT(SI_SOFTNET);
284 1.6 chris DO_SOFTINT(SI_SOFTCLOCK);
285 1.6 chris DO_SOFTINT(SI_SOFT);
286 1.6 chris
287 1.6 chris __cpu_simple_unlock(&processing);
288 1.1 chris
289 1.6 chris restore_interrupts(oldirqstate);
290 1.6 chris }
291 1.1 chris
292 1.1 chris
293 1.6 chris /* called from splhigh, so the matching splx will set the interrupt up.*/
294 1.6 chris void
295 1.6 chris _setsoftintr(int si)
296 1.6 chris {
297 1.6 chris int oldirqstate;
298 1.1 chris
299 1.6 chris oldirqstate = disable_interrupts(I32_bit);
300 1.6 chris footbridge_ipending |= SI_TO_IRQBIT(si);
301 1.6 chris restore_interrupts(oldirqstate);
302 1.1 chris
303 1.6 chris /* Process unmasked pending soft interrupts. */
304 1.6 chris if ((footbridge_ipending & INT_SWMASK) & ~current_spl_level)
305 1.6 chris footbridge_do_pending();
306 1.1 chris }
307 1.1 chris
308 1.6 chris void
309 1.6 chris footbridge_intr_init(void)
310 1.6 chris {
311 1.6 chris struct intrq *iq;
312 1.6 chris int i;
313 1.6 chris
314 1.6 chris intr_enabled = 0;
315 1.6 chris current_spl_level = 0xffffffff;
316 1.6 chris footbridge_ipending = 0;
317 1.6 chris footbridge_set_intrmask();
318 1.6 chris
319 1.6 chris for (i = 0; i < NIRQ; i++) {
320 1.6 chris iq = &footbridge_intrq[i];
321 1.6 chris TAILQ_INIT(&iq->iq_list);
322 1.6 chris
323 1.6 chris sprintf(iq->iq_name, "irq %d", i);
324 1.6 chris evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
325 1.6 chris NULL, "footbridge", iq->iq_name);
326 1.6 chris }
327 1.6 chris
328 1.6 chris footbridge_intr_calculate_masks();
329 1.6 chris
330 1.6 chris /* Enable IRQ's, we don't have any FIQ's*/
331 1.6 chris enable_interrupts(I32_bit);
332 1.6 chris }
333 1.1 chris
334 1.1 chris void *
335 1.6 chris footbridge_intr_claim(int irq, int ipl, char *name, int (*func)(void *), void *arg)
336 1.1 chris {
337 1.6 chris struct intrq *iq;
338 1.6 chris struct intrhand *ih;
339 1.6 chris u_int oldirqstate;
340 1.6 chris
341 1.6 chris if (irq < 0 || irq > NIRQ)
342 1.6 chris panic("footbridge_intr_establish: IRQ %d out of range", irq);
343 1.1 chris
344 1.1 chris ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
345 1.6 chris if (ih == NULL)
346 1.6 chris {
347 1.6 chris printf("No memory");
348 1.6 chris return (NULL);
349 1.6 chris }
350 1.6 chris
351 1.6 chris ih->ih_func = func;
352 1.6 chris ih->ih_arg = arg;
353 1.6 chris ih->ih_ipl = ipl;
354 1.6 chris ih->ih_irq = irq;
355 1.6 chris
356 1.6 chris iq = &footbridge_intrq[irq];
357 1.1 chris
358 1.6 chris iq->iq_ist = IST_LEVEL;
359 1.1 chris
360 1.6 chris oldirqstate = disable_interrupts(I32_bit);
361 1.6 chris
362 1.6 chris TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
363 1.6 chris
364 1.6 chris footbridge_intr_calculate_masks();
365 1.6 chris
366 1.6 chris /* detach the existing event counter and add the new name */
367 1.6 chris evcnt_detach(&iq->iq_ev);
368 1.6 chris evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
369 1.6 chris NULL, "footbridge", name);
370 1.6 chris
371 1.6 chris restore_interrupts(oldirqstate);
372 1.6 chris
373 1.1 chris return(ih);
374 1.1 chris }
375 1.1 chris
376 1.6 chris void
377 1.6 chris footbridge_intr_disestablish(void *cookie)
378 1.6 chris {
379 1.6 chris struct intrhand *ih = cookie;
380 1.6 chris struct intrq *iq = &footbridge_intrq[ih->ih_irq];
381 1.6 chris int oldirqstate;
382 1.6 chris
383 1.6 chris /* XXX need to free ih ? */
384 1.6 chris oldirqstate = disable_interrupts(I32_bit);
385 1.1 chris
386 1.6 chris TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
387 1.6 chris
388 1.6 chris footbridge_intr_calculate_masks();
389 1.6 chris
390 1.6 chris restore_interrupts(oldirqstate);
391 1.6 chris }
392 1.6 chris
393 1.6 chris static uint32_t footbridge_intstatus(void);
394 1.6 chris
395 1.6 chris static inline uint32_t footbridge_intstatus()
396 1.6 chris {
397 1.6 chris return ((__volatile uint32_t*)(DC21285_ARMCSR_VBASE))[IRQ_STATUS>>2];
398 1.6 chris }
399 1.6 chris
400 1.6 chris /* called with external interrupts disabled */
401 1.6 chris void
402 1.6 chris footbridge_intr_dispatch(struct clockframe *frame)
403 1.1 chris {
404 1.6 chris struct intrq *iq;
405 1.6 chris struct intrhand *ih;
406 1.6 chris int oldirqstate, pcpl, irq, ibit, hwpend;
407 1.6 chris
408 1.6 chris pcpl = current_spl_level;
409 1.6 chris
410 1.6 chris hwpend = footbridge_intstatus();
411 1.6 chris
412 1.6 chris /*
413 1.6 chris * Disable all the interrupts that are pending. We will
414 1.6 chris * reenable them once they are processed and not masked.
415 1.6 chris */
416 1.6 chris intr_enabled &= ~hwpend;
417 1.6 chris footbridge_set_intrmask();
418 1.1 chris
419 1.6 chris while (hwpend != 0) {
420 1.6 chris int intr_rc = 0;
421 1.6 chris irq = ffs(hwpend) - 1;
422 1.6 chris ibit = (1U << irq);
423 1.6 chris
424 1.6 chris hwpend &= ~ibit;
425 1.6 chris
426 1.6 chris if (pcpl & ibit) {
427 1.6 chris /*
428 1.6 chris * IRQ is masked; mark it as pending and check
429 1.6 chris * the next one. Note: the IRQ is already disabled.
430 1.6 chris */
431 1.6 chris footbridge_ipending |= ibit;
432 1.6 chris continue;
433 1.6 chris }
434 1.6 chris
435 1.6 chris footbridge_ipending &= ~ibit;
436 1.6 chris
437 1.6 chris iq = &footbridge_intrq[irq];
438 1.6 chris iq->iq_ev.ev_count++;
439 1.6 chris uvmexp.intrs++;
440 1.6 chris current_spl_level |= iq->iq_mask;
441 1.6 chris oldirqstate = enable_interrupts(I32_bit);
442 1.6 chris for (ih = TAILQ_FIRST(&iq->iq_list);
443 1.6 chris ((ih != NULL) && (intr_rc != 1));
444 1.6 chris ih = TAILQ_NEXT(ih, ih_list)) {
445 1.6 chris intr_rc = (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
446 1.6 chris }
447 1.6 chris restore_interrupts(oldirqstate);
448 1.6 chris
449 1.6 chris current_spl_level = pcpl;
450 1.6 chris
451 1.6 chris /* Re-enable this interrupt now that's it's cleared. */
452 1.6 chris intr_enabled |= ibit;
453 1.6 chris footbridge_set_intrmask();
454 1.1 chris }
455 1.6 chris
456 1.6 chris /*
457 1.6 chris * restore interrupts to their state on entry, this will
458 1.6 chris * trigger pending interrupts, and soft and hard
459 1.6 chris */
460 1.6 chris splx(pcpl);
461 1.1 chris }
462