footbridge_irqhandler.c revision 1.21 1 1.21 matt /* $NetBSD: footbridge_irqhandler.c,v 1.21 2008/04/27 18:58:44 matt 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.21 matt __KERNEL_RCSID(0,"$NetBSD: footbridge_irqhandler.c,v 1.21 2008/04/27 18:58:44 matt 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.14 perry volatile uint32_t intr_enabled;
72 1.1 chris
73 1.6 chris /* Interrupts pending */
74 1.14 perry volatile int footbridge_ipending;
75 1.3 chris
76 1.6 chris void footbridge_intr_dispatch(struct clockframe *frame);
77 1.1 chris
78 1.21 matt const struct evcnt *footbridge_pci_intr_evcnt(void *, pci_intr_handle_t);
79 1.6 chris
80 1.6 chris const struct evcnt *
81 1.21 matt footbridge_pci_intr_evcnt(void *pcv, pci_intr_handle_t ih)
82 1.6 chris {
83 1.6 chris /* XXX check range is valid */
84 1.6 chris #if NISA > 0
85 1.6 chris if (ih >= 0x80 && ih <= 0x8f) {
86 1.6 chris return isa_intr_evcnt(NULL, (ih & 0x0f));
87 1.6 chris }
88 1.6 chris #endif
89 1.6 chris return &footbridge_intrq[ih].iq_ev;
90 1.6 chris }
91 1.6 chris
92 1.14 perry static inline void
93 1.6 chris footbridge_enable_irq(int irq)
94 1.6 chris {
95 1.6 chris intr_enabled |= (1U << irq);
96 1.6 chris footbridge_set_intrmask();
97 1.1 chris }
98 1.1 chris
99 1.14 perry static inline void
100 1.6 chris footbridge_disable_irq(int irq)
101 1.1 chris {
102 1.6 chris intr_enabled &= ~(1U << irq);
103 1.6 chris footbridge_set_intrmask();
104 1.1 chris }
105 1.1 chris
106 1.1 chris /*
107 1.6 chris * NOTE: This routine must be called with interrupts disabled in the CPSR.
108 1.1 chris */
109 1.6 chris static void
110 1.6 chris footbridge_intr_calculate_masks(void)
111 1.1 chris {
112 1.6 chris struct intrq *iq;
113 1.6 chris struct intrhand *ih;
114 1.6 chris int irq, ipl;
115 1.6 chris
116 1.6 chris /* First, figure out which IPLs each IRQ has. */
117 1.6 chris for (irq = 0; irq < NIRQ; irq++) {
118 1.6 chris int levels = 0;
119 1.6 chris iq = &footbridge_intrq[irq];
120 1.6 chris footbridge_disable_irq(irq);
121 1.21 matt TAILQ_FOREACH(ih, &iq->iq_list, ih_list) {
122 1.6 chris levels |= (1U << ih->ih_ipl);
123 1.21 matt }
124 1.6 chris iq->iq_levels = levels;
125 1.6 chris }
126 1.1 chris
127 1.6 chris /* Next, figure out which IRQs are used by each IPL. */
128 1.6 chris for (ipl = 0; ipl < NIPL; ipl++) {
129 1.6 chris int irqs = 0;
130 1.6 chris for (irq = 0; irq < NIRQ; irq++) {
131 1.6 chris if (footbridge_intrq[irq].iq_levels & (1U << ipl))
132 1.6 chris irqs |= (1U << irq);
133 1.6 chris }
134 1.6 chris footbridge_imask[ipl] = irqs;
135 1.6 chris }
136 1.1 chris
137 1.6 chris /* IPL_NONE must open up all interrupts */
138 1.21 matt KASSERT(footbridge_imask[IPL_NONE] == 0);
139 1.21 matt KASSERT(footbridge_imask[IPL_SOFTCLOCK] == 0);
140 1.21 matt KASSERT(footbridge_imask[IPL_SOFTBIO] == 0);
141 1.21 matt KASSERT(footbridge_imask[IPL_SOFTNET] == 0);
142 1.21 matt KASSERT(footbridge_imask[IPL_SOFTSERIAL] == 0);
143 1.1 chris
144 1.6 chris /*
145 1.16 wiz * Enforce a hierarchy that gives "slow" device (or devices with
146 1.6 chris * limited input buffer space/"real-time" requirements) a better
147 1.6 chris * chance at not dropping data.
148 1.6 chris */
149 1.21 matt KASSERT(footbridge_imask[IPL_VM] != 0);
150 1.18 ad footbridge_imask[IPL_SCHED] |= footbridge_imask[IPL_VM];
151 1.18 ad footbridge_imask[IPL_HIGH] |= footbridge_imask[IPL_SCHED];
152 1.1 chris
153 1.1 chris /*
154 1.6 chris * Calculate the ipl level to go to when handling this interrupt
155 1.6 chris */
156 1.21 matt for (irq = 0, iq = footbridge_intrq; irq < NIRQ; irq++, iq++) {
157 1.6 chris int irqs = (1U << irq);
158 1.21 matt if (!TAILQ_EMPTY(&iq->iq_list)) {
159 1.6 chris footbridge_enable_irq(irq);
160 1.21 matt TAILQ_FOREACH(ih, &iq->iq_list, ih_list) {
161 1.21 matt irqs |= footbridge_imask[ih->ih_ipl];
162 1.21 matt }
163 1.21 matt }
164 1.6 chris iq->iq_mask = irqs;
165 1.1 chris }
166 1.6 chris }
167 1.6 chris
168 1.6 chris int
169 1.6 chris _splraise(int ipl)
170 1.6 chris {
171 1.6 chris return (footbridge_splraise(ipl));
172 1.6 chris }
173 1.1 chris
174 1.6 chris /* this will always take us to the ipl passed in */
175 1.6 chris void
176 1.6 chris splx(int new)
177 1.6 chris {
178 1.6 chris footbridge_splx(new);
179 1.6 chris }
180 1.1 chris
181 1.6 chris int
182 1.6 chris _spllower(int ipl)
183 1.6 chris {
184 1.6 chris return (footbridge_spllower(ipl));
185 1.1 chris }
186 1.1 chris
187 1.15 mrg void
188 1.6 chris footbridge_intr_init(void)
189 1.6 chris {
190 1.6 chris struct intrq *iq;
191 1.6 chris int i;
192 1.6 chris
193 1.6 chris intr_enabled = 0;
194 1.21 matt set_curcpl(0xffffffff);
195 1.6 chris footbridge_ipending = 0;
196 1.6 chris footbridge_set_intrmask();
197 1.6 chris
198 1.21 matt for (i = 0, iq = footbridge_intrq; i < NIRQ; i++, iq++) {
199 1.6 chris TAILQ_INIT(&iq->iq_list);
200 1.6 chris
201 1.6 chris sprintf(iq->iq_name, "irq %d", i);
202 1.6 chris evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
203 1.6 chris NULL, "footbridge", iq->iq_name);
204 1.6 chris }
205 1.6 chris
206 1.6 chris footbridge_intr_calculate_masks();
207 1.6 chris
208 1.6 chris /* Enable IRQ's, we don't have any FIQ's*/
209 1.6 chris enable_interrupts(I32_bit);
210 1.6 chris }
211 1.1 chris
212 1.1 chris void *
213 1.12 he footbridge_intr_claim(int irq, int ipl, const char *name, int (*func)(void *), void *arg)
214 1.1 chris {
215 1.6 chris struct intrq *iq;
216 1.6 chris struct intrhand *ih;
217 1.6 chris u_int oldirqstate;
218 1.6 chris
219 1.6 chris if (irq < 0 || irq > NIRQ)
220 1.6 chris panic("footbridge_intr_establish: IRQ %d out of range", irq);
221 1.1 chris
222 1.1 chris ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
223 1.6 chris if (ih == NULL)
224 1.6 chris {
225 1.6 chris printf("No memory");
226 1.6 chris return (NULL);
227 1.6 chris }
228 1.6 chris
229 1.6 chris ih->ih_func = func;
230 1.6 chris ih->ih_arg = arg;
231 1.6 chris ih->ih_ipl = ipl;
232 1.6 chris ih->ih_irq = irq;
233 1.6 chris
234 1.6 chris iq = &footbridge_intrq[irq];
235 1.1 chris
236 1.6 chris iq->iq_ist = IST_LEVEL;
237 1.1 chris
238 1.6 chris oldirqstate = disable_interrupts(I32_bit);
239 1.6 chris
240 1.6 chris TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
241 1.6 chris
242 1.6 chris footbridge_intr_calculate_masks();
243 1.6 chris
244 1.6 chris /* detach the existing event counter and add the new name */
245 1.6 chris evcnt_detach(&iq->iq_ev);
246 1.6 chris evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
247 1.6 chris NULL, "footbridge", name);
248 1.6 chris
249 1.6 chris restore_interrupts(oldirqstate);
250 1.6 chris
251 1.1 chris return(ih);
252 1.1 chris }
253 1.1 chris
254 1.6 chris void
255 1.6 chris footbridge_intr_disestablish(void *cookie)
256 1.6 chris {
257 1.6 chris struct intrhand *ih = cookie;
258 1.6 chris struct intrq *iq = &footbridge_intrq[ih->ih_irq];
259 1.6 chris int oldirqstate;
260 1.6 chris
261 1.6 chris /* XXX need to free ih ? */
262 1.6 chris oldirqstate = disable_interrupts(I32_bit);
263 1.1 chris
264 1.6 chris TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
265 1.6 chris
266 1.6 chris footbridge_intr_calculate_masks();
267 1.6 chris
268 1.6 chris restore_interrupts(oldirqstate);
269 1.6 chris }
270 1.6 chris
271 1.21 matt static inline uint32_t footbridge_intstatus(void)
272 1.6 chris {
273 1.21 matt return ((volatile uint32_t*)(DC21285_ARMCSR_VBASE))[IRQ_STATUS>>2];
274 1.6 chris }
275 1.6 chris
276 1.6 chris /* called with external interrupts disabled */
277 1.6 chris void
278 1.6 chris footbridge_intr_dispatch(struct clockframe *frame)
279 1.1 chris {
280 1.6 chris struct intrq *iq;
281 1.6 chris struct intrhand *ih;
282 1.21 matt int oldirqstate, irq, ibit, hwpend;
283 1.21 matt struct cpu_info * const ci = curcpu();
284 1.21 matt const int ppl = ci->ci_cpl;
285 1.21 matt const int imask = footbridge_imask[ppl];
286 1.6 chris
287 1.6 chris hwpend = footbridge_intstatus();
288 1.6 chris
289 1.6 chris /*
290 1.6 chris * Disable all the interrupts that are pending. We will
291 1.6 chris * reenable them once they are processed and not masked.
292 1.6 chris */
293 1.6 chris intr_enabled &= ~hwpend;
294 1.6 chris footbridge_set_intrmask();
295 1.1 chris
296 1.6 chris while (hwpend != 0) {
297 1.6 chris int intr_rc = 0;
298 1.6 chris irq = ffs(hwpend) - 1;
299 1.6 chris ibit = (1U << irq);
300 1.6 chris
301 1.6 chris hwpend &= ~ibit;
302 1.6 chris
303 1.21 matt if (imask & ibit) {
304 1.6 chris /*
305 1.6 chris * IRQ is masked; mark it as pending and check
306 1.6 chris * the next one. Note: the IRQ is already disabled.
307 1.6 chris */
308 1.6 chris footbridge_ipending |= ibit;
309 1.6 chris continue;
310 1.6 chris }
311 1.6 chris
312 1.6 chris footbridge_ipending &= ~ibit;
313 1.6 chris
314 1.6 chris iq = &footbridge_intrq[irq];
315 1.6 chris iq->iq_ev.ev_count++;
316 1.6 chris uvmexp.intrs++;
317 1.21 matt TAILQ_FOREACH(ih, &iq->iq_list, ih_list) {
318 1.21 matt ci->ci_cpl = ih->ih_ipl;
319 1.21 matt oldirqstate = enable_interrupts(I32_bit);
320 1.6 chris intr_rc = (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
321 1.21 matt restore_interrupts(oldirqstate);
322 1.21 matt if (intr_rc != 1)
323 1.21 matt break;
324 1.6 chris }
325 1.6 chris
326 1.21 matt ci->ci_cpl = ppl;
327 1.6 chris
328 1.6 chris /* Re-enable this interrupt now that's it's cleared. */
329 1.6 chris intr_enabled |= ibit;
330 1.6 chris footbridge_set_intrmask();
331 1.10 matt
332 1.11 wiz /* also check for any new interrupts that may have occurred,
333 1.10 matt * that we can handle at this spl level */
334 1.21 matt hwpend |= (footbridge_ipending & ICU_INT_HWMASK) & ~imask;
335 1.1 chris }
336 1.6 chris
337 1.21 matt #ifdef __HAVE_FAST_SOFTINTS
338 1.21 matt cpu_dosoftints();
339 1.21 matt #endif /* __HAVE_FAST_SOFTINTS */
340 1.1 chris }
341