iq80310_intr.c revision 1.4.4.7 1 1.4.4.7 thorpej /* $NetBSD: iq80310_intr.c,v 1.4.4.7 2002/08/19 21:39:35 thorpej Exp $ */
2 1.4.4.2 nathanw
3 1.4.4.2 nathanw /*
4 1.4.4.3 nathanw * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
5 1.4.4.2 nathanw * All rights reserved.
6 1.4.4.2 nathanw *
7 1.4.4.2 nathanw * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.4.4.2 nathanw *
9 1.4.4.2 nathanw * Redistribution and use in source and binary forms, with or without
10 1.4.4.2 nathanw * modification, are permitted provided that the following conditions
11 1.4.4.2 nathanw * are met:
12 1.4.4.2 nathanw * 1. Redistributions of source code must retain the above copyright
13 1.4.4.2 nathanw * notice, this list of conditions and the following disclaimer.
14 1.4.4.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
15 1.4.4.2 nathanw * notice, this list of conditions and the following disclaimer in the
16 1.4.4.2 nathanw * documentation and/or other materials provided with the distribution.
17 1.4.4.2 nathanw * 3. All advertising materials mentioning features or use of this software
18 1.4.4.2 nathanw * must display the following acknowledgement:
19 1.4.4.2 nathanw * This product includes software developed for the NetBSD Project by
20 1.4.4.2 nathanw * Wasabi Systems, Inc.
21 1.4.4.2 nathanw * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.4.4.2 nathanw * or promote products derived from this software without specific prior
23 1.4.4.2 nathanw * written permission.
24 1.4.4.2 nathanw *
25 1.4.4.2 nathanw * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.4.4.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.4.4.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.4.4.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.4.4.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.4.4.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.4.4.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.4.4.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.4.4.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.4.4.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.4.4.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
36 1.4.4.2 nathanw */
37 1.4.4.2 nathanw
38 1.4.4.2 nathanw /*
39 1.4.4.2 nathanw * Interrupt support for the Intel IQ80310.
40 1.4.4.2 nathanw */
41 1.4.4.2 nathanw
42 1.4.4.2 nathanw #include <sys/param.h>
43 1.4.4.2 nathanw #include <sys/systm.h>
44 1.4.4.2 nathanw #include <sys/malloc.h>
45 1.4.4.2 nathanw
46 1.4.4.3 nathanw #include <uvm/uvm_extern.h>
47 1.4.4.3 nathanw
48 1.4.4.2 nathanw #include <machine/bus.h>
49 1.4.4.2 nathanw #include <machine/intr.h>
50 1.4.4.3 nathanw
51 1.4.4.2 nathanw #include <arm/cpufunc.h>
52 1.4.4.2 nathanw
53 1.4.4.2 nathanw #include <arm/xscale/i80200reg.h>
54 1.4.4.3 nathanw #include <arm/xscale/i80200var.h>
55 1.4.4.2 nathanw
56 1.4.4.2 nathanw #include <evbarm/iq80310/iq80310reg.h>
57 1.4.4.2 nathanw #include <evbarm/iq80310/iq80310var.h>
58 1.4.4.2 nathanw #include <evbarm/iq80310/obiovar.h>
59 1.4.4.2 nathanw
60 1.4.4.3 nathanw /* Interrupt handler queues. */
61 1.4.4.3 nathanw struct intrq intrq[NIRQ];
62 1.4.4.2 nathanw
63 1.4.4.3 nathanw /* Interrupts to mask at each level. */
64 1.4.4.7 thorpej int iq80310_imask[NIPL];
65 1.4.4.2 nathanw
66 1.4.4.3 nathanw /* Current interrupt priority level. */
67 1.4.4.3 nathanw __volatile int current_spl_level;
68 1.4.4.2 nathanw
69 1.4.4.3 nathanw /* Interrupts pending. */
70 1.4.4.7 thorpej __volatile int iq80310_ipending;
71 1.4.4.2 nathanw
72 1.4.4.3 nathanw /* Software copy of the IRQs we have enabled. */
73 1.4.4.3 nathanw uint32_t intr_enabled;
74 1.4.4.2 nathanw
75 1.4.4.3 nathanw /*
76 1.4.4.3 nathanw * Map a software interrupt queue index (at the top of the word, and
77 1.4.4.3 nathanw * highest priority softintr is encountered first in an ffs()).
78 1.4.4.3 nathanw */
79 1.4.4.3 nathanw #define SI_TO_IRQBIT(si) (1U << (31 - (si)))
80 1.4.4.2 nathanw
81 1.4.4.3 nathanw /*
82 1.4.4.3 nathanw * Map a software interrupt queue to an interrupt priority level.
83 1.4.4.3 nathanw */
84 1.4.4.3 nathanw static const int si_to_ipl[SI_NQUEUES] = {
85 1.4.4.3 nathanw IPL_SOFT, /* SI_SOFT */
86 1.4.4.3 nathanw IPL_SOFTCLOCK, /* SI_SOFTCLOCK */
87 1.4.4.3 nathanw IPL_SOFTNET, /* SI_SOFTNET */
88 1.4.4.3 nathanw IPL_SOFTSERIAL, /* SI_SOFTSERIAL */
89 1.4.4.3 nathanw };
90 1.4.4.2 nathanw
91 1.4.4.3 nathanw void iq80310_intr_dispatch(struct clockframe *frame);
92 1.4.4.2 nathanw
93 1.4.4.3 nathanw static __inline uint32_t
94 1.4.4.2 nathanw iq80310_intstat_read(void)
95 1.4.4.2 nathanw {
96 1.4.4.2 nathanw uint32_t intstat;
97 1.4.4.2 nathanw
98 1.4.4.2 nathanw intstat = CPLD_READ(IQ80310_XINT3_STATUS) & 0x1f;
99 1.4.4.3 nathanw #if defined(IRQ_READ_XINT0)
100 1.4.4.3 nathanw if (IRQ_READ_XINT0)
101 1.4.4.2 nathanw intstat |= (CPLD_READ(IQ80310_XINT0_STATUS) & 0x7) << 5;
102 1.4.4.3 nathanw #endif
103 1.4.4.2 nathanw
104 1.4.4.3 nathanw /* XXX Why do we have to mask off? */
105 1.4.4.3 nathanw return (intstat & intr_enabled);
106 1.4.4.2 nathanw }
107 1.4.4.2 nathanw
108 1.4.4.3 nathanw static __inline void
109 1.4.4.3 nathanw iq80310_set_intrmask(void)
110 1.4.4.2 nathanw {
111 1.4.4.3 nathanw uint32_t disabled;
112 1.4.4.3 nathanw
113 1.4.4.3 nathanw intr_enabled |= IRQ_BITS_ALWAYS_ON;
114 1.4.4.2 nathanw
115 1.4.4.3 nathanw /* The XINT_MASK register sets a bit to *disable*. */
116 1.4.4.3 nathanw disabled = (~intr_enabled) & IRQ_BITS;
117 1.4.4.3 nathanw
118 1.4.4.3 nathanw CPLD_WRITE(IQ80310_XINT_MASK, disabled & 0x1f);
119 1.4.4.3 nathanw }
120 1.4.4.3 nathanw
121 1.4.4.3 nathanw static __inline void
122 1.4.4.3 nathanw iq80310_enable_irq(int irq)
123 1.4.4.3 nathanw {
124 1.4.4.3 nathanw
125 1.4.4.3 nathanw intr_enabled |= (1U << irq);
126 1.4.4.3 nathanw iq80310_set_intrmask();
127 1.4.4.3 nathanw }
128 1.4.4.3 nathanw
129 1.4.4.3 nathanw static __inline void
130 1.4.4.3 nathanw iq80310_disable_irq(int irq)
131 1.4.4.3 nathanw {
132 1.4.4.3 nathanw
133 1.4.4.3 nathanw intr_enabled &= ~(1U << irq);
134 1.4.4.3 nathanw iq80310_set_intrmask();
135 1.4.4.3 nathanw }
136 1.4.4.3 nathanw
137 1.4.4.3 nathanw /*
138 1.4.4.3 nathanw * NOTE: This routine must be called with interrupts disabled in the CPSR.
139 1.4.4.3 nathanw */
140 1.4.4.3 nathanw static void
141 1.4.4.3 nathanw iq80310_intr_calculate_masks(void)
142 1.4.4.3 nathanw {
143 1.4.4.3 nathanw struct intrq *iq;
144 1.4.4.3 nathanw struct intrhand *ih;
145 1.4.4.3 nathanw int irq, ipl;
146 1.4.4.3 nathanw
147 1.4.4.3 nathanw /* First, figure out which IPLs each IRQ has. */
148 1.4.4.3 nathanw for (irq = 0; irq < NIRQ; irq++) {
149 1.4.4.3 nathanw int levels = 0;
150 1.4.4.3 nathanw iq = &intrq[irq];
151 1.4.4.3 nathanw iq80310_disable_irq(irq);
152 1.4.4.3 nathanw for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
153 1.4.4.3 nathanw ih = TAILQ_NEXT(ih, ih_list))
154 1.4.4.3 nathanw levels |= (1U << ih->ih_ipl);
155 1.4.4.3 nathanw iq->iq_levels = levels;
156 1.4.4.3 nathanw }
157 1.4.4.3 nathanw
158 1.4.4.3 nathanw /* Next, figure out which IRQs are used by each IPL. */
159 1.4.4.3 nathanw for (ipl = 0; ipl < NIPL; ipl++) {
160 1.4.4.3 nathanw int irqs = 0;
161 1.4.4.3 nathanw for (irq = 0; irq < NIRQ; irq++) {
162 1.4.4.3 nathanw if (intrq[irq].iq_levels & (1U << ipl))
163 1.4.4.3 nathanw irqs |= (1U << irq);
164 1.4.4.3 nathanw }
165 1.4.4.7 thorpej iq80310_imask[ipl] = irqs;
166 1.4.4.3 nathanw }
167 1.4.4.3 nathanw
168 1.4.4.7 thorpej iq80310_imask[IPL_NONE] = 0;
169 1.4.4.2 nathanw
170 1.4.4.2 nathanw /*
171 1.4.4.3 nathanw * Initialize the soft interrupt masks to block themselves.
172 1.4.4.2 nathanw */
173 1.4.4.7 thorpej iq80310_imask[IPL_SOFT] = SI_TO_IRQBIT(SI_SOFT);
174 1.4.4.7 thorpej iq80310_imask[IPL_SOFTCLOCK] = SI_TO_IRQBIT(SI_SOFTCLOCK);
175 1.4.4.7 thorpej iq80310_imask[IPL_SOFTNET] = SI_TO_IRQBIT(SI_SOFTNET);
176 1.4.4.7 thorpej iq80310_imask[IPL_SOFTSERIAL] = SI_TO_IRQBIT(SI_SOFTSERIAL);
177 1.4.4.2 nathanw
178 1.4.4.2 nathanw /*
179 1.4.4.3 nathanw * splsoftclock() is the only interface that users of the
180 1.4.4.3 nathanw * generic software interrupt facility have to block their
181 1.4.4.3 nathanw * soft intrs, so splsoftclock() must also block IPL_SOFT.
182 1.4.4.2 nathanw */
183 1.4.4.7 thorpej iq80310_imask[IPL_SOFTCLOCK] |= iq80310_imask[IPL_SOFT];
184 1.4.4.2 nathanw
185 1.4.4.3 nathanw /*
186 1.4.4.3 nathanw * splsoftnet() must also block splsoftclock(), since we don't
187 1.4.4.3 nathanw * want timer-driven network events to occur while we're
188 1.4.4.3 nathanw * processing incoming packets.
189 1.4.4.3 nathanw */
190 1.4.4.7 thorpej iq80310_imask[IPL_SOFTNET] |= iq80310_imask[IPL_SOFTCLOCK];
191 1.4.4.3 nathanw
192 1.4.4.3 nathanw /*
193 1.4.4.3 nathanw * Enforce a heirarchy that gives "slow" device (or devices with
194 1.4.4.3 nathanw * limited input buffer space/"real-time" requirements) a better
195 1.4.4.3 nathanw * chance at not dropping data.
196 1.4.4.3 nathanw */
197 1.4.4.7 thorpej iq80310_imask[IPL_BIO] |= iq80310_imask[IPL_SOFTNET];
198 1.4.4.7 thorpej iq80310_imask[IPL_NET] |= iq80310_imask[IPL_BIO];
199 1.4.4.7 thorpej iq80310_imask[IPL_SOFTSERIAL] |= iq80310_imask[IPL_NET];
200 1.4.4.7 thorpej iq80310_imask[IPL_TTY] |= iq80310_imask[IPL_SOFTSERIAL];
201 1.4.4.3 nathanw
202 1.4.4.3 nathanw /*
203 1.4.4.3 nathanw * splvm() blocks all interrupts that use the kernel memory
204 1.4.4.3 nathanw * allocation facilities.
205 1.4.4.3 nathanw */
206 1.4.4.7 thorpej iq80310_imask[IPL_IMP] |= iq80310_imask[IPL_TTY];
207 1.4.4.3 nathanw
208 1.4.4.3 nathanw /*
209 1.4.4.3 nathanw * Audio devices are not allowed to perform memory allocation
210 1.4.4.3 nathanw * in their interrupt routines, and they have fairly "real-time"
211 1.4.4.3 nathanw * requirements, so give them a high interrupt priority.
212 1.4.4.3 nathanw */
213 1.4.4.7 thorpej iq80310_imask[IPL_AUDIO] |= iq80310_imask[IPL_IMP];
214 1.4.4.3 nathanw
215 1.4.4.3 nathanw /*
216 1.4.4.3 nathanw * splclock() must block anything that uses the scheduler.
217 1.4.4.3 nathanw */
218 1.4.4.7 thorpej iq80310_imask[IPL_CLOCK] |= iq80310_imask[IPL_AUDIO];
219 1.4.4.3 nathanw
220 1.4.4.3 nathanw /*
221 1.4.4.3 nathanw * No separate statclock on the IQ80310.
222 1.4.4.3 nathanw */
223 1.4.4.7 thorpej iq80310_imask[IPL_STATCLOCK] |= iq80310_imask[IPL_CLOCK];
224 1.4.4.3 nathanw
225 1.4.4.3 nathanw /*
226 1.4.4.3 nathanw * splhigh() must block "everything".
227 1.4.4.3 nathanw */
228 1.4.4.7 thorpej iq80310_imask[IPL_HIGH] |= iq80310_imask[IPL_STATCLOCK];
229 1.4.4.3 nathanw
230 1.4.4.3 nathanw /*
231 1.4.4.3 nathanw * XXX We need serial drivers to run at the absolute highest priority
232 1.4.4.3 nathanw * in order to avoid overruns, so serial > high.
233 1.4.4.3 nathanw */
234 1.4.4.7 thorpej iq80310_imask[IPL_SERIAL] |= iq80310_imask[IPL_HIGH];
235 1.4.4.3 nathanw
236 1.4.4.3 nathanw /*
237 1.4.4.3 nathanw * Now compute which IRQs must be blocked when servicing any
238 1.4.4.3 nathanw * given IRQ.
239 1.4.4.3 nathanw */
240 1.4.4.3 nathanw for (irq = 0; irq < NIRQ; irq++) {
241 1.4.4.3 nathanw int irqs = (1U << irq);
242 1.4.4.3 nathanw iq = &intrq[irq];
243 1.4.4.3 nathanw if (TAILQ_FIRST(&iq->iq_list) != NULL)
244 1.4.4.3 nathanw iq80310_enable_irq(irq);
245 1.4.4.3 nathanw for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
246 1.4.4.3 nathanw ih = TAILQ_NEXT(ih, ih_list))
247 1.4.4.7 thorpej irqs |= iq80310_imask[ih->ih_ipl];
248 1.4.4.3 nathanw iq->iq_mask = irqs;
249 1.4.4.3 nathanw }
250 1.4.4.2 nathanw }
251 1.4.4.2 nathanw
252 1.4.4.7 thorpej void
253 1.4.4.4 nathanw iq80310_do_soft(void)
254 1.4.4.2 nathanw {
255 1.4.4.3 nathanw static __cpu_simple_lock_t processing = __SIMPLELOCK_UNLOCKED;
256 1.4.4.3 nathanw int new, oldirqstate;
257 1.4.4.3 nathanw
258 1.4.4.3 nathanw if (__cpu_simple_lock_try(&processing) == 0)
259 1.4.4.3 nathanw return;
260 1.4.4.3 nathanw
261 1.4.4.3 nathanw new = current_spl_level;
262 1.4.4.2 nathanw
263 1.4.4.2 nathanw oldirqstate = disable_interrupts(I32_bit);
264 1.4.4.3 nathanw
265 1.4.4.3 nathanw #define DO_SOFTINT(si) \
266 1.4.4.7 thorpej if ((iq80310_ipending & ~new) & SI_TO_IRQBIT(si)) { \
267 1.4.4.7 thorpej iq80310_ipending &= ~SI_TO_IRQBIT(si); \
268 1.4.4.7 thorpej current_spl_level |= iq80310_imask[si_to_ipl[(si)]]; \
269 1.4.4.3 nathanw restore_interrupts(oldirqstate); \
270 1.4.4.3 nathanw softintr_dispatch(si); \
271 1.4.4.3 nathanw oldirqstate = disable_interrupts(I32_bit); \
272 1.4.4.3 nathanw current_spl_level = new; \
273 1.4.4.3 nathanw }
274 1.4.4.3 nathanw
275 1.4.4.3 nathanw DO_SOFTINT(SI_SOFTSERIAL);
276 1.4.4.3 nathanw DO_SOFTINT(SI_SOFTNET);
277 1.4.4.3 nathanw DO_SOFTINT(SI_SOFTCLOCK);
278 1.4.4.3 nathanw DO_SOFTINT(SI_SOFT);
279 1.4.4.3 nathanw
280 1.4.4.3 nathanw __cpu_simple_unlock(&processing);
281 1.4.4.3 nathanw
282 1.4.4.2 nathanw restore_interrupts(oldirqstate);
283 1.4.4.2 nathanw }
284 1.4.4.2 nathanw
285 1.4.4.7 thorpej #if defined(EVBARM_SPL_NOINLINE)
286 1.4.4.3 nathanw int
287 1.4.4.3 nathanw _splraise(int ipl)
288 1.4.4.2 nathanw {
289 1.4.4.7 thorpej int old;
290 1.4.4.3 nathanw
291 1.4.4.3 nathanw old = current_spl_level;
292 1.4.4.7 thorpej current_spl_level |= iq80310_imask[ipl];
293 1.4.4.3 nathanw
294 1.4.4.3 nathanw return (old);
295 1.4.4.3 nathanw }
296 1.4.4.3 nathanw
297 1.4.4.3 nathanw __inline void
298 1.4.4.3 nathanw splx(int new)
299 1.4.4.3 nathanw {
300 1.4.4.3 nathanw int old;
301 1.4.4.3 nathanw
302 1.4.4.3 nathanw old = current_spl_level;
303 1.4.4.3 nathanw current_spl_level = new;
304 1.4.4.3 nathanw
305 1.4.4.4 nathanw /* If there are software interrupts to process, do it. */
306 1.4.4.7 thorpej if ((iq80310_ipending & ~IRQ_BITS) & ~new)
307 1.4.4.4 nathanw iq80310_do_soft();
308 1.4.4.4 nathanw
309 1.4.4.3 nathanw /*
310 1.4.4.3 nathanw * If there are pending hardware interrupts (i.e. the
311 1.4.4.3 nathanw * external interrupt is disabled in the ICU), and all
312 1.4.4.3 nathanw * hardware interrupts are being unblocked, then re-enable
313 1.4.4.3 nathanw * the external hardware interrupt.
314 1.4.4.3 nathanw *
315 1.4.4.3 nathanw * XXX We have to wait for ALL hardware interrupts to
316 1.4.4.3 nathanw * XXX be unblocked, because we currently lose if we
317 1.4.4.3 nathanw * XXX get nested interrupts, and I don't know why yet.
318 1.4.4.3 nathanw */
319 1.4.4.7 thorpej if ((new & IRQ_BITS) == 0 && (iq80310_ipending & IRQ_BITS))
320 1.4.4.6 nathanw i80200_intr_enable(INTCTL_IM | INTCTL_PM);
321 1.4.4.3 nathanw }
322 1.4.4.3 nathanw
323 1.4.4.3 nathanw int
324 1.4.4.3 nathanw _spllower(int ipl)
325 1.4.4.3 nathanw {
326 1.4.4.3 nathanw int old = current_spl_level;
327 1.4.4.3 nathanw
328 1.4.4.7 thorpej iq80310_splx(iq80310_imask[ipl]);
329 1.4.4.3 nathanw return (old);
330 1.4.4.2 nathanw }
331 1.4.4.2 nathanw
332 1.4.4.7 thorpej #else
333 1.4.4.7 thorpej
334 1.4.4.7 thorpej #undef _splraise
335 1.4.4.7 thorpej int
336 1.4.4.7 thorpej _splraise(int ipl)
337 1.4.4.7 thorpej {
338 1.4.4.7 thorpej return iq80310_splraise(ipl);
339 1.4.4.7 thorpej }
340 1.4.4.7 thorpej
341 1.4.4.7 thorpej #undef splx
342 1.4.4.7 thorpej __inline void
343 1.4.4.7 thorpej splx(int new)
344 1.4.4.7 thorpej {
345 1.4.4.7 thorpej return iq80310_splx(new);
346 1.4.4.7 thorpej }
347 1.4.4.7 thorpej
348 1.4.4.7 thorpej #undef _spllower
349 1.4.4.7 thorpej int
350 1.4.4.7 thorpej _spllower(int ipl)
351 1.4.4.7 thorpej {
352 1.4.4.7 thorpej return iq80310_spllower(ipl);
353 1.4.4.7 thorpej }
354 1.4.4.7 thorpej #endif
355 1.4.4.7 thorpej
356 1.4.4.2 nathanw void
357 1.4.4.3 nathanw _setsoftintr(int si)
358 1.4.4.2 nathanw {
359 1.4.4.3 nathanw int oldirqstate;
360 1.4.4.3 nathanw
361 1.4.4.3 nathanw oldirqstate = disable_interrupts(I32_bit);
362 1.4.4.7 thorpej iq80310_ipending |= SI_TO_IRQBIT(si);
363 1.4.4.3 nathanw restore_interrupts(oldirqstate);
364 1.4.4.2 nathanw
365 1.4.4.3 nathanw /* Process unmasked pending soft interrupts. */
366 1.4.4.7 thorpej if ((iq80310_ipending & ~IRQ_BITS) & ~current_spl_level)
367 1.4.4.4 nathanw iq80310_do_soft();
368 1.4.4.2 nathanw }
369 1.4.4.2 nathanw
370 1.4.4.2 nathanw void
371 1.4.4.3 nathanw iq80310_intr_init(void)
372 1.4.4.2 nathanw {
373 1.4.4.3 nathanw struct intrq *iq;
374 1.4.4.3 nathanw int i;
375 1.4.4.3 nathanw
376 1.4.4.3 nathanw /*
377 1.4.4.3 nathanw * The Secondary PCI interrupts INTA, INTB, and INTC
378 1.4.4.3 nathanw * area always enabled, since they cannot be masked
379 1.4.4.3 nathanw * in the CPLD.
380 1.4.4.3 nathanw */
381 1.4.4.3 nathanw intr_enabled |= IRQ_BITS_ALWAYS_ON;
382 1.4.4.2 nathanw
383 1.4.4.3 nathanw for (i = 0; i < NIRQ; i++) {
384 1.4.4.3 nathanw iq = &intrq[i];
385 1.4.4.3 nathanw TAILQ_INIT(&iq->iq_list);
386 1.4.4.3 nathanw
387 1.4.4.3 nathanw sprintf(iq->iq_name, "irq %d", i);
388 1.4.4.3 nathanw evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
389 1.4.4.3 nathanw NULL, "iq80310", iq->iq_name);
390 1.4.4.3 nathanw }
391 1.4.4.3 nathanw
392 1.4.4.3 nathanw iq80310_intr_calculate_masks();
393 1.4.4.3 nathanw
394 1.4.4.3 nathanw /* Enable external interrupts on the i80200. */
395 1.4.4.3 nathanw i80200_extirq_dispatch = iq80310_intr_dispatch;
396 1.4.4.6 nathanw i80200_intr_enable(INTCTL_IM | INTCTL_PM);
397 1.4.4.3 nathanw
398 1.4.4.3 nathanw /* Enable IRQs (don't yet use FIQs). */
399 1.4.4.3 nathanw enable_interrupts(I32_bit);
400 1.4.4.2 nathanw }
401 1.4.4.2 nathanw
402 1.4.4.2 nathanw void *
403 1.4.4.2 nathanw iq80310_intr_establish(int irq, int ipl, int (*func)(void *), void *arg)
404 1.4.4.2 nathanw {
405 1.4.4.3 nathanw struct intrq *iq;
406 1.4.4.3 nathanw struct intrhand *ih;
407 1.4.4.2 nathanw u_int oldirqstate;
408 1.4.4.3 nathanw
409 1.4.4.3 nathanw if (irq < 0 || irq > NIRQ)
410 1.4.4.3 nathanw panic("iq80310_intr_establish: IRQ %d out of range", irq);
411 1.4.4.2 nathanw
412 1.4.4.2 nathanw ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
413 1.4.4.2 nathanw if (ih == NULL)
414 1.4.4.2 nathanw return (NULL);
415 1.4.4.2 nathanw
416 1.4.4.2 nathanw ih->ih_func = func;
417 1.4.4.2 nathanw ih->ih_arg = arg;
418 1.4.4.3 nathanw ih->ih_ipl = ipl;
419 1.4.4.3 nathanw ih->ih_irq = irq;
420 1.4.4.2 nathanw
421 1.4.4.3 nathanw iq = &intrq[irq];
422 1.4.4.2 nathanw
423 1.4.4.3 nathanw /* All IQ80310 interrupts are level-triggered. */
424 1.4.4.3 nathanw iq->iq_ist = IST_LEVEL;
425 1.4.4.2 nathanw
426 1.4.4.3 nathanw oldirqstate = disable_interrupts(I32_bit);
427 1.4.4.2 nathanw
428 1.4.4.3 nathanw TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
429 1.4.4.2 nathanw
430 1.4.4.3 nathanw iq80310_intr_calculate_masks();
431 1.4.4.2 nathanw
432 1.4.4.2 nathanw restore_interrupts(oldirqstate);
433 1.4.4.2 nathanw
434 1.4.4.2 nathanw return (ih);
435 1.4.4.2 nathanw }
436 1.4.4.2 nathanw
437 1.4.4.2 nathanw void
438 1.4.4.2 nathanw iq80310_intr_disestablish(void *cookie)
439 1.4.4.2 nathanw {
440 1.4.4.3 nathanw struct intrhand *ih = cookie;
441 1.4.4.3 nathanw struct intrq *iq = &intrq[ih->ih_irq];
442 1.4.4.3 nathanw int oldirqstate;
443 1.4.4.3 nathanw
444 1.4.4.3 nathanw oldirqstate = disable_interrupts(I32_bit);
445 1.4.4.3 nathanw
446 1.4.4.3 nathanw TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
447 1.4.4.3 nathanw
448 1.4.4.3 nathanw iq80310_intr_calculate_masks();
449 1.4.4.3 nathanw
450 1.4.4.3 nathanw restore_interrupts(oldirqstate);
451 1.4.4.3 nathanw }
452 1.4.4.3 nathanw
453 1.4.4.3 nathanw void
454 1.4.4.3 nathanw iq80310_intr_dispatch(struct clockframe *frame)
455 1.4.4.3 nathanw {
456 1.4.4.3 nathanw struct intrq *iq;
457 1.4.4.3 nathanw struct intrhand *ih;
458 1.4.4.5 nathanw int oldirqstate, pcpl, irq, ibit, hwpend, rv, stray;
459 1.4.4.5 nathanw
460 1.4.4.5 nathanw stray = 1;
461 1.4.4.3 nathanw
462 1.4.4.3 nathanw /* First, disable external IRQs. */
463 1.4.4.6 nathanw i80200_intr_disable(INTCTL_IM | INTCTL_PM);
464 1.4.4.3 nathanw
465 1.4.4.3 nathanw pcpl = current_spl_level;
466 1.4.4.3 nathanw
467 1.4.4.3 nathanw for (hwpend = iq80310_intstat_read(); hwpend != 0;) {
468 1.4.4.3 nathanw irq = ffs(hwpend) - 1;
469 1.4.4.3 nathanw ibit = (1U << irq);
470 1.4.4.3 nathanw
471 1.4.4.5 nathanw stray = 0;
472 1.4.4.5 nathanw
473 1.4.4.3 nathanw hwpend &= ~ibit;
474 1.4.4.3 nathanw
475 1.4.4.3 nathanw if (pcpl & ibit) {
476 1.4.4.3 nathanw /*
477 1.4.4.3 nathanw * IRQ is masked; mark it as pending and check
478 1.4.4.3 nathanw * the next one. Note: external IRQs are already
479 1.4.4.3 nathanw * disabled.
480 1.4.4.3 nathanw */
481 1.4.4.7 thorpej iq80310_ipending |= ibit;
482 1.4.4.3 nathanw continue;
483 1.4.4.3 nathanw }
484 1.4.4.3 nathanw
485 1.4.4.7 thorpej iq80310_ipending &= ~ibit;
486 1.4.4.5 nathanw rv = 0;
487 1.4.4.2 nathanw
488 1.4.4.3 nathanw iq = &intrq[irq];
489 1.4.4.3 nathanw iq->iq_ev.ev_count++;
490 1.4.4.3 nathanw uvmexp.intrs++;
491 1.4.4.3 nathanw current_spl_level |= iq->iq_mask;
492 1.4.4.3 nathanw oldirqstate = enable_interrupts(I32_bit);
493 1.4.4.3 nathanw for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
494 1.4.4.3 nathanw ih = TAILQ_NEXT(ih, ih_list)) {
495 1.4.4.5 nathanw rv |= (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
496 1.4.4.3 nathanw }
497 1.4.4.3 nathanw restore_interrupts(oldirqstate);
498 1.4.4.3 nathanw
499 1.4.4.3 nathanw current_spl_level = pcpl;
500 1.4.4.5 nathanw
501 1.4.4.6 nathanw #if 0 /* XXX */
502 1.4.4.5 nathanw if (rv == 0)
503 1.4.4.5 nathanw printf("Stray interrupt: IRQ %d\n", irq);
504 1.4.4.6 nathanw #endif
505 1.4.4.3 nathanw }
506 1.4.4.3 nathanw
507 1.4.4.6 nathanw #if 0 /* XXX */
508 1.4.4.5 nathanw if (stray)
509 1.4.4.5 nathanw printf("Stray external interrupt\n");
510 1.4.4.6 nathanw #endif
511 1.4.4.5 nathanw
512 1.4.4.3 nathanw /* Check for pendings soft intrs. */
513 1.4.4.7 thorpej if ((iq80310_ipending & ~IRQ_BITS) & ~current_spl_level) {
514 1.4.4.3 nathanw oldirqstate = enable_interrupts(I32_bit);
515 1.4.4.4 nathanw iq80310_do_soft();
516 1.4.4.3 nathanw restore_interrupts(oldirqstate);
517 1.4.4.3 nathanw }
518 1.4.4.3 nathanw
519 1.4.4.3 nathanw /*
520 1.4.4.3 nathanw * If no hardware interrupts are masked, re-enable external
521 1.4.4.3 nathanw * interrupts.
522 1.4.4.3 nathanw */
523 1.4.4.7 thorpej if ((iq80310_ipending & IRQ_BITS) == 0)
524 1.4.4.6 nathanw i80200_intr_enable(INTCTL_IM | INTCTL_PM);
525 1.4.4.2 nathanw }
526