i80321_icu.c revision 1.18 1 1.18 matt /* $NetBSD: i80321_icu.c,v 1.18 2008/04/27 18:58:45 matt Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.13 scw * Copyright (c) 2001, 2002, 2006 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.13 scw * Written by Jason R. Thorpe and Steve C. Woodford for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.8 lukem
38 1.8 lukem #include <sys/cdefs.h>
39 1.18 matt __KERNEL_RCSID(0, "$NetBSD: i80321_icu.c,v 1.18 2008/04/27 18:58:45 matt Exp $");
40 1.1 thorpej
41 1.6 thorpej #ifndef EVBARM_SPL_NOINLINE
42 1.6 thorpej #define EVBARM_SPL_NOINLINE
43 1.6 thorpej #endif
44 1.6 thorpej
45 1.1 thorpej /*
46 1.1 thorpej * Interrupt support for the Intel i80321 I/O Processor.
47 1.1 thorpej */
48 1.1 thorpej
49 1.1 thorpej #include <sys/param.h>
50 1.1 thorpej #include <sys/systm.h>
51 1.1 thorpej #include <sys/malloc.h>
52 1.1 thorpej
53 1.1 thorpej #include <uvm/uvm_extern.h>
54 1.1 thorpej
55 1.1 thorpej #include <machine/bus.h>
56 1.1 thorpej #include <machine/intr.h>
57 1.1 thorpej
58 1.1 thorpej #include <arm/cpufunc.h>
59 1.1 thorpej
60 1.1 thorpej #include <arm/xscale/i80321reg.h>
61 1.1 thorpej #include <arm/xscale/i80321var.h>
62 1.1 thorpej
63 1.1 thorpej /* Interrupt handler queues. */
64 1.1 thorpej struct intrq intrq[NIRQ];
65 1.1 thorpej
66 1.1 thorpej /* Interrupts to mask at each level. */
67 1.5 briggs int i80321_imask[NIPL];
68 1.1 thorpej
69 1.1 thorpej /* Interrupts pending. */
70 1.11 perry volatile int i80321_ipending;
71 1.1 thorpej
72 1.1 thorpej /* Software copy of the IRQs we have enabled. */
73 1.11 perry volatile uint32_t intr_enabled;
74 1.1 thorpej
75 1.1 thorpej /* Mask if interrupts steered to FIQs. */
76 1.1 thorpej uint32_t intr_steer;
77 1.1 thorpej
78 1.1 thorpej /*
79 1.3 thorpej * Interrupt bit names.
80 1.3 thorpej */
81 1.18 matt const char * const i80321_irqnames[] = {
82 1.3 thorpej "DMA0 EOT",
83 1.3 thorpej "DMA0 EOC",
84 1.3 thorpej "DMA1 EOT",
85 1.3 thorpej "DMA1 EOC",
86 1.3 thorpej "irq 4",
87 1.3 thorpej "irq 5",
88 1.3 thorpej "AAU EOT",
89 1.3 thorpej "AAU EOC",
90 1.3 thorpej "core PMU",
91 1.3 thorpej "TMR0 (hardclock)",
92 1.3 thorpej "TMR1",
93 1.3 thorpej "I2C0",
94 1.3 thorpej "I2C1",
95 1.3 thorpej "MU",
96 1.3 thorpej "BIST",
97 1.3 thorpej "periph PMU",
98 1.3 thorpej "XScale PMU",
99 1.3 thorpej "BIU error",
100 1.3 thorpej "ATU error",
101 1.3 thorpej "MCU error",
102 1.3 thorpej "DMA0 error",
103 1.3 thorpej "DMA1 error",
104 1.3 thorpej "irq 22",
105 1.3 thorpej "AAU error",
106 1.3 thorpej "MU error",
107 1.3 thorpej "SSP",
108 1.3 thorpej "irq 26",
109 1.3 thorpej "irq 27",
110 1.3 thorpej "irq 28",
111 1.3 thorpej "irq 29",
112 1.3 thorpej "irq 30",
113 1.3 thorpej "irq 31",
114 1.3 thorpej };
115 1.3 thorpej
116 1.1 thorpej void i80321_intr_dispatch(struct clockframe *frame);
117 1.1 thorpej
118 1.11 perry static inline uint32_t
119 1.1 thorpej i80321_iintsrc_read(void)
120 1.1 thorpej {
121 1.1 thorpej uint32_t iintsrc;
122 1.1 thorpej
123 1.11 perry __asm volatile("mrc p6, 0, %0, c8, c0, 0"
124 1.1 thorpej : "=r" (iintsrc));
125 1.1 thorpej
126 1.1 thorpej /*
127 1.1 thorpej * The IINTSRC register shows bits that are active even
128 1.1 thorpej * if they are masked in INTCTL, so we have to mask them
129 1.1 thorpej * off with the interrupts we consider enabled.
130 1.1 thorpej */
131 1.1 thorpej return (iintsrc & intr_enabled);
132 1.1 thorpej }
133 1.1 thorpej
134 1.11 perry static inline void
135 1.1 thorpej i80321_set_intrsteer(void)
136 1.1 thorpej {
137 1.1 thorpej
138 1.11 perry __asm volatile("mcr p6, 0, %0, c4, c0, 0"
139 1.1 thorpej :
140 1.1 thorpej : "r" (intr_steer & ICU_INT_HWMASK));
141 1.1 thorpej }
142 1.1 thorpej
143 1.11 perry static inline void
144 1.1 thorpej i80321_enable_irq(int irq)
145 1.1 thorpej {
146 1.1 thorpej
147 1.1 thorpej intr_enabled |= (1U << irq);
148 1.1 thorpej i80321_set_intrmask();
149 1.1 thorpej }
150 1.1 thorpej
151 1.11 perry static inline void
152 1.1 thorpej i80321_disable_irq(int irq)
153 1.1 thorpej {
154 1.1 thorpej
155 1.1 thorpej intr_enabled &= ~(1U << irq);
156 1.1 thorpej i80321_set_intrmask();
157 1.1 thorpej }
158 1.1 thorpej
159 1.1 thorpej /*
160 1.1 thorpej * NOTE: This routine must be called with interrupts disabled in the CPSR.
161 1.1 thorpej */
162 1.1 thorpej static void
163 1.1 thorpej i80321_intr_calculate_masks(void)
164 1.1 thorpej {
165 1.1 thorpej struct intrq *iq;
166 1.1 thorpej struct intrhand *ih;
167 1.1 thorpej int irq, ipl;
168 1.1 thorpej
169 1.1 thorpej /* First, figure out which IPLs each IRQ has. */
170 1.1 thorpej for (irq = 0; irq < NIRQ; irq++) {
171 1.1 thorpej int levels = 0;
172 1.1 thorpej iq = &intrq[irq];
173 1.1 thorpej i80321_disable_irq(irq);
174 1.1 thorpej for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
175 1.1 thorpej ih = TAILQ_NEXT(ih, ih_list))
176 1.1 thorpej levels |= (1U << ih->ih_ipl);
177 1.1 thorpej iq->iq_levels = levels;
178 1.1 thorpej }
179 1.1 thorpej
180 1.1 thorpej /* Next, figure out which IRQs are used by each IPL. */
181 1.1 thorpej for (ipl = 0; ipl < NIPL; ipl++) {
182 1.1 thorpej int irqs = 0;
183 1.1 thorpej for (irq = 0; irq < NIRQ; irq++) {
184 1.1 thorpej if (intrq[irq].iq_levels & (1U << ipl))
185 1.1 thorpej irqs |= (1U << irq);
186 1.1 thorpej }
187 1.5 briggs i80321_imask[ipl] = irqs;
188 1.1 thorpej }
189 1.1 thorpej
190 1.5 briggs i80321_imask[IPL_NONE] = 0;
191 1.1 thorpej
192 1.1 thorpej /*
193 1.15 ad * Enforce a hierarchy that gives "slow" device (or devices with
194 1.15 ad * limited input buffer space/"real-time" requirements) a better
195 1.15 ad * chance at not dropping data.
196 1.1 thorpej */
197 1.1 thorpej
198 1.18 matt KASSERT(i80321_imask[IPL_VM] != 0);
199 1.18 matt i80321_imask[IPL_SCHED] |= i80321_imask[IPL_VM];
200 1.15 ad i80321_imask[IPL_HIGH] |= i80321_imask[IPL_SCHED];
201 1.1 thorpej
202 1.1 thorpej /*
203 1.1 thorpej * Now compute which IRQs must be blocked when servicing any
204 1.1 thorpej * given IRQ.
205 1.1 thorpej */
206 1.1 thorpej for (irq = 0; irq < NIRQ; irq++) {
207 1.1 thorpej int irqs = (1U << irq);
208 1.1 thorpej iq = &intrq[irq];
209 1.1 thorpej if (TAILQ_FIRST(&iq->iq_list) != NULL)
210 1.1 thorpej i80321_enable_irq(irq);
211 1.1 thorpej for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
212 1.1 thorpej ih = TAILQ_NEXT(ih, ih_list))
213 1.5 briggs irqs |= i80321_imask[ih->ih_ipl];
214 1.1 thorpej iq->iq_mask = irqs;
215 1.1 thorpej }
216 1.1 thorpej }
217 1.1 thorpej
218 1.12 mrg void
219 1.1 thorpej splx(int new)
220 1.1 thorpej {
221 1.6 thorpej i80321_splx(new);
222 1.5 briggs }
223 1.5 briggs
224 1.5 briggs int
225 1.1 thorpej _spllower(int ipl)
226 1.1 thorpej {
227 1.6 thorpej return (i80321_spllower(ipl));
228 1.5 briggs }
229 1.5 briggs
230 1.5 briggs int
231 1.5 briggs _splraise(int ipl)
232 1.5 briggs {
233 1.6 thorpej return (i80321_splraise(ipl));
234 1.1 thorpej }
235 1.5 briggs
236 1.1 thorpej /*
237 1.1 thorpej * i80321_icu_init:
238 1.1 thorpej *
239 1.1 thorpej * Initialize the i80321 ICU. Called early in bootstrap
240 1.1 thorpej * to make sure the ICU is in a pristine state.
241 1.1 thorpej */
242 1.1 thorpej void
243 1.1 thorpej i80321_icu_init(void)
244 1.1 thorpej {
245 1.1 thorpej
246 1.1 thorpej intr_enabled = 0; /* All interrupts disabled */
247 1.1 thorpej i80321_set_intrmask();
248 1.1 thorpej
249 1.1 thorpej intr_steer = 0; /* All interrupts steered to IRQ */
250 1.1 thorpej i80321_set_intrsteer();
251 1.1 thorpej }
252 1.1 thorpej
253 1.1 thorpej /*
254 1.1 thorpej * i80321_intr_init:
255 1.1 thorpej *
256 1.1 thorpej * Initialize the rest of the interrupt subsystem, making it
257 1.1 thorpej * ready to handle interrupts from devices.
258 1.1 thorpej */
259 1.1 thorpej void
260 1.1 thorpej i80321_intr_init(void)
261 1.1 thorpej {
262 1.1 thorpej struct intrq *iq;
263 1.1 thorpej int i;
264 1.1 thorpej
265 1.1 thorpej intr_enabled = 0;
266 1.1 thorpej
267 1.1 thorpej for (i = 0; i < NIRQ; i++) {
268 1.1 thorpej iq = &intrq[i];
269 1.1 thorpej TAILQ_INIT(&iq->iq_list);
270 1.1 thorpej
271 1.1 thorpej evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
272 1.3 thorpej NULL, "iop321", i80321_irqnames[i]);
273 1.1 thorpej }
274 1.1 thorpej
275 1.1 thorpej i80321_intr_calculate_masks();
276 1.1 thorpej
277 1.1 thorpej /* Enable IRQs (don't yet use FIQs). */
278 1.1 thorpej enable_interrupts(I32_bit);
279 1.1 thorpej }
280 1.1 thorpej
281 1.1 thorpej void *
282 1.1 thorpej i80321_intr_establish(int irq, int ipl, int (*func)(void *), void *arg)
283 1.1 thorpej {
284 1.1 thorpej struct intrq *iq;
285 1.1 thorpej struct intrhand *ih;
286 1.1 thorpej u_int oldirqstate;
287 1.1 thorpej
288 1.1 thorpej if (irq < 0 || irq > NIRQ)
289 1.1 thorpej panic("i80321_intr_establish: IRQ %d out of range", irq);
290 1.1 thorpej
291 1.1 thorpej ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
292 1.1 thorpej if (ih == NULL)
293 1.1 thorpej return (NULL);
294 1.1 thorpej
295 1.1 thorpej ih->ih_func = func;
296 1.1 thorpej ih->ih_arg = arg;
297 1.1 thorpej ih->ih_ipl = ipl;
298 1.1 thorpej ih->ih_irq = irq;
299 1.1 thorpej
300 1.1 thorpej iq = &intrq[irq];
301 1.1 thorpej
302 1.1 thorpej /* All IOP321 interrupts are level-triggered. */
303 1.1 thorpej iq->iq_ist = IST_LEVEL;
304 1.1 thorpej
305 1.1 thorpej oldirqstate = disable_interrupts(I32_bit);
306 1.1 thorpej
307 1.1 thorpej TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
308 1.1 thorpej
309 1.1 thorpej i80321_intr_calculate_masks();
310 1.1 thorpej
311 1.1 thorpej restore_interrupts(oldirqstate);
312 1.1 thorpej
313 1.1 thorpej return (ih);
314 1.1 thorpej }
315 1.1 thorpej
316 1.1 thorpej void
317 1.1 thorpej i80321_intr_disestablish(void *cookie)
318 1.1 thorpej {
319 1.1 thorpej struct intrhand *ih = cookie;
320 1.1 thorpej struct intrq *iq = &intrq[ih->ih_irq];
321 1.1 thorpej int oldirqstate;
322 1.1 thorpej
323 1.1 thorpej oldirqstate = disable_interrupts(I32_bit);
324 1.1 thorpej
325 1.1 thorpej TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
326 1.1 thorpej
327 1.1 thorpej i80321_intr_calculate_masks();
328 1.1 thorpej
329 1.1 thorpej restore_interrupts(oldirqstate);
330 1.1 thorpej }
331 1.1 thorpej
332 1.13 scw /*
333 1.13 scw * Hardware interrupt handler.
334 1.13 scw *
335 1.13 scw * If I80321_HPI_ENABLED is defined, this code attempts to deal with
336 1.13 scw * HPI interrupts as best it can.
337 1.13 scw *
338 1.13 scw * The problem is that HPIs cannot be masked at the interrupt controller;
339 1.13 scw * they can only be masked by disabling IRQs in the XScale core.
340 1.13 scw *
341 1.13 scw * So, if an HPI comes in and we determine that it should be masked at
342 1.13 scw * the current IPL then we mark it pending in the usual way and set
343 1.13 scw * I32_bit in the interrupt frame. This ensures that when we return from
344 1.13 scw * i80321_intr_dispatch(), IRQs will be disabled in the XScale core. (To
345 1.13 scw * ensure IRQs are enabled later, i80321_splx() has been modified to do
346 1.13 scw * just that when a pending HPI interrupt is unmasked.) Additionally,
347 1.13 scw * because HPIs are level-triggered, the registered handler for the HPI
348 1.13 scw * interrupt will also be invoked with IRQs disabled. If a masked HPI
349 1.13 scw * occurs at the same time as another unmasked higher priority interrupt,
350 1.13 scw * the higher priority handler will also be invoked with IRQs disabled.
351 1.13 scw * As a result, the system could end up executing a lot of code with IRQs
352 1.13 scw * completely disabled if the HPI's IPL is relatively low.
353 1.13 scw *
354 1.13 scw * At the present time, the only known use of HPI is for the console UART
355 1.13 scw * on a couple of boards. This is probably the least intrusive use of HPI
356 1.13 scw * as IPL_SERIAL is the highest priority IPL in the system anyway. The
357 1.13 scw * code has not been tested with HPI hooked up to a class of device which
358 1.13 scw * interrupts below IPL_SERIAL. Indeed, such a configuration is likely to
359 1.13 scw * perform very poorly if at all, even though the following code has been
360 1.13 scw * designed (hopefully) to cope with it.
361 1.13 scw */
362 1.13 scw
363 1.1 thorpej void
364 1.1 thorpej i80321_intr_dispatch(struct clockframe *frame)
365 1.1 thorpej {
366 1.1 thorpej struct intrq *iq;
367 1.1 thorpej struct intrhand *ih;
368 1.18 matt int oldirqstate, irq, ibit, hwpend;
369 1.13 scw #ifdef I80321_HPI_ENABLED
370 1.13 scw int oldpending;
371 1.13 scw #endif
372 1.18 matt struct cpu_info * const ci = curcpu();
373 1.18 matt const int ppl = ci->ci_cpl;
374 1.18 matt const uint32_t imask = i80321_imask[ppl];
375 1.1 thorpej
376 1.1 thorpej hwpend = i80321_iintsrc_read();
377 1.1 thorpej
378 1.1 thorpej /*
379 1.1 thorpej * Disable all the interrupts that are pending. We will
380 1.1 thorpej * reenable them once they are processed and not masked.
381 1.1 thorpej */
382 1.1 thorpej intr_enabled &= ~hwpend;
383 1.1 thorpej i80321_set_intrmask();
384 1.1 thorpej
385 1.13 scw #ifdef I80321_HPI_ENABLED
386 1.13 scw oldirqstate = 0; /* XXX: quell gcc warning */
387 1.13 scw #endif
388 1.13 scw
389 1.1 thorpej while (hwpend != 0) {
390 1.13 scw #ifdef I80321_HPI_ENABLED
391 1.13 scw /* Deal with HPI interrupt first */
392 1.13 scw if (__predict_false(hwpend & INT_HPIMASK))
393 1.13 scw irq = ICU_INT_HPI;
394 1.13 scw else
395 1.13 scw #endif
396 1.1 thorpej irq = ffs(hwpend) - 1;
397 1.1 thorpej ibit = (1U << irq);
398 1.1 thorpej
399 1.1 thorpej hwpend &= ~ibit;
400 1.1 thorpej
401 1.18 matt if (imask & ibit) {
402 1.1 thorpej /*
403 1.1 thorpej * IRQ is masked; mark it as pending and check
404 1.1 thorpej * the next one. Note: the IRQ is already disabled.
405 1.1 thorpej */
406 1.13 scw #ifdef I80321_HPI_ENABLED
407 1.13 scw if (__predict_false(irq == ICU_INT_HPI)) {
408 1.13 scw /*
409 1.13 scw * This is an HPI. We *must* disable
410 1.13 scw * IRQs in the interrupt frame until
411 1.13 scw * INT_HPIMASK is cleared by a later
412 1.13 scw * call to splx(). Otherwise the level-
413 1.13 scw * triggered interrupt will just keep
414 1.13 scw * coming back.
415 1.13 scw */
416 1.13 scw frame->cf_if.if_spsr |= I32_bit;
417 1.13 scw }
418 1.13 scw #endif
419 1.5 briggs i80321_ipending |= ibit;
420 1.1 thorpej continue;
421 1.1 thorpej }
422 1.1 thorpej
423 1.13 scw #ifdef I80321_HPI_ENABLED
424 1.13 scw oldpending = i80321_ipending | ibit;
425 1.13 scw #endif
426 1.5 briggs i80321_ipending &= ~ibit;
427 1.1 thorpej
428 1.1 thorpej iq = &intrq[irq];
429 1.1 thorpej iq->iq_ev.ev_count++;
430 1.1 thorpej uvmexp.intrs++;
431 1.13 scw #ifdef I80321_HPI_ENABLED
432 1.13 scw /*
433 1.13 scw * Re-enable interrupts iff an HPI is not pending
434 1.13 scw */
435 1.18 matt if (__predict_true((oldpending & INT_HPIMASK) == 0)) {
436 1.13 scw #endif
437 1.18 matt TAILQ_FOREACH (ih, &iq->iq_list, ih_list) {
438 1.18 matt ci->ci_cpl = ih->ih_ipl;
439 1.18 matt oldirqstate = enable_interrupts(I32_bit);
440 1.18 matt (void) (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
441 1.18 matt restore_interrupts(oldirqstate);
442 1.18 matt }
443 1.13 scw #ifdef I80321_HPI_ENABLED
444 1.18 matt } else if (irq == ICU_INT_HPI) {
445 1.13 scw /*
446 1.13 scw * We've just handled the HPI. Make sure IRQs
447 1.13 scw * are enabled in the interrupt frame.
448 1.13 scw * Here's hoping the handler really did clear
449 1.13 scw * down the source...
450 1.13 scw */
451 1.13 scw frame->cf_if.if_spsr &= ~I32_bit;
452 1.13 scw }
453 1.13 scw #endif
454 1.18 matt ci->ci_cpl = ppl;
455 1.1 thorpej
456 1.1 thorpej /* Re-enable this interrupt now that's it's cleared. */
457 1.1 thorpej intr_enabled |= ibit;
458 1.1 thorpej i80321_set_intrmask();
459 1.9 scw
460 1.9 scw /*
461 1.9 scw * Don't forget to include interrupts which may have
462 1.9 scw * arrived in the meantime.
463 1.9 scw */
464 1.18 matt hwpend |= ((i80321_ipending & ICU_INT_HWMASK) & ~imask);
465 1.1 thorpej }
466 1.1 thorpej
467 1.18 matt #ifdef __HAVE_FAST_SOFTINTS
468 1.18 matt cpu_dosoftints();
469 1.13 scw #endif
470 1.1 thorpej }
471