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