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