pic.c revision 1.43 1 1.43 ryo /* $NetBSD: pic.c,v 1.43 2018/07/09 06:08:42 ryo Exp $ */
2 1.2 matt /*-
3 1.2 matt * Copyright (c) 2008 The NetBSD Foundation, Inc.
4 1.2 matt * All rights reserved.
5 1.2 matt *
6 1.2 matt * This code is derived from software contributed to The NetBSD Foundation
7 1.2 matt * by Matt Thomas.
8 1.2 matt *
9 1.2 matt * Redistribution and use in source and binary forms, with or without
10 1.2 matt * modification, are permitted provided that the following conditions
11 1.2 matt * are met:
12 1.2 matt * 1. Redistributions of source code must retain the above copyright
13 1.2 matt * notice, this list of conditions and the following disclaimer.
14 1.2 matt * 2. Redistributions in binary form must reproduce the above copyright
15 1.2 matt * notice, this list of conditions and the following disclaimer in the
16 1.2 matt * documentation and/or other materials provided with the distribution.
17 1.2 matt *
18 1.2 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.2 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.2 matt * POSSIBILITY OF SUCH DAMAGE.
29 1.2 matt */
30 1.21 matt
31 1.21 matt #define _INTR_PRIVATE
32 1.21 matt #include "opt_ddb.h"
33 1.25 skrll #include "opt_multiprocessor.h"
34 1.21 matt
35 1.2 matt #include <sys/cdefs.h>
36 1.43 ryo __KERNEL_RCSID(0, "$NetBSD: pic.c,v 1.43 2018/07/09 06:08:42 ryo Exp $");
37 1.2 matt
38 1.2 matt #include <sys/param.h>
39 1.13 matt #include <sys/atomic.h>
40 1.13 matt #include <sys/cpu.h>
41 1.2 matt #include <sys/evcnt.h>
42 1.13 matt #include <sys/intr.h>
43 1.13 matt #include <sys/kernel.h>
44 1.11 matt #include <sys/kmem.h>
45 1.35 skrll #include <sys/mutex.h>
46 1.35 skrll #include <sys/once.h>
47 1.13 matt #include <sys/xcall.h>
48 1.22 rmind #include <sys/ipi.h>
49 1.2 matt
50 1.2 matt #include <arm/armreg.h>
51 1.2 matt #include <arm/cpufunc.h>
52 1.42 ryo #include <arm/locore.h> /* for compat aarch64 */
53 1.2 matt
54 1.21 matt #ifdef DDB
55 1.21 matt #include <arm/db_machdep.h>
56 1.21 matt #endif
57 1.21 matt
58 1.2 matt #include <arm/pic/picvar.h>
59 1.2 matt
60 1.28 matt #if defined(__HAVE_PIC_PENDING_INTRS)
61 1.29 matt /*
62 1.29 matt * This implementation of pending interrupts on a MULTIPROCESSOR system makes
63 1.29 matt * the assumption that a PIC (pic_softc) shall only have all its interrupts
64 1.29 matt * come from the same CPU. In other words, interrupts from a single PIC will
65 1.29 matt * not be distributed among multiple CPUs.
66 1.29 matt */
67 1.29 matt struct pic_pending {
68 1.29 matt volatile uint32_t blocked_pics;
69 1.29 matt volatile uint32_t pending_pics;
70 1.29 matt volatile uint32_t pending_ipls;
71 1.29 matt };
72 1.2 matt static uint32_t
73 1.2 matt pic_find_pending_irqs_by_ipl(struct pic_softc *, size_t, uint32_t, int);
74 1.2 matt static struct pic_softc *
75 1.29 matt pic_list_find_pic_by_pending_ipl(struct pic_pending *, uint32_t);
76 1.2 matt static void
77 1.29 matt pic_deliver_irqs(struct pic_pending *, struct pic_softc *, int, void *);
78 1.2 matt static void
79 1.29 matt pic_list_deliver_irqs(struct pic_pending *, register_t, int, void *);
80 1.29 matt
81 1.29 matt #ifdef MULTIPROCESSOR
82 1.29 matt percpu_t *pic_pending_percpu;
83 1.29 matt #else
84 1.29 matt struct pic_pending pic_pending;
85 1.29 matt #endif /* MULTIPROCESSOR */
86 1.28 matt #endif /* __HAVE_PIC_PENDING_INTRS */
87 1.2 matt
88 1.2 matt struct pic_softc *pic_list[PIC_MAXPICS];
89 1.2 matt #if PIC_MAXPICS > 32
90 1.2 matt #error PIC_MAXPICS > 32 not supported
91 1.2 matt #endif
92 1.2 matt struct intrsource *pic_sources[PIC_MAXMAXSOURCES];
93 1.2 matt struct intrsource *pic__iplsources[PIC_MAXMAXSOURCES];
94 1.2 matt struct intrsource **pic_iplsource[NIPL] = {
95 1.2 matt [0 ... NIPL-1] = pic__iplsources,
96 1.2 matt };
97 1.2 matt size_t pic_ipl_offset[NIPL+1];
98 1.35 skrll
99 1.35 skrll static kmutex_t pic_lock;
100 1.2 matt size_t pic_sourcebase;
101 1.41 skrll static struct evcnt pic_deferral_ev =
102 1.2 matt EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "deferred", "intr");
103 1.2 matt EVCNT_ATTACH_STATIC(pic_deferral_ev);
104 1.2 matt
105 1.35 skrll static int pic_init(void);
106 1.35 skrll
107 1.11 matt #ifdef __HAVE_PIC_SET_PRIORITY
108 1.11 matt void
109 1.11 matt pic_set_priority(struct cpu_info *ci, int newipl)
110 1.11 matt {
111 1.13 matt register_t psw = cpsid(I32_bit);
112 1.13 matt if (pic_list[0] != NULL)
113 1.13 matt (pic_list[0]->pic_ops->pic_set_priority)(pic_list[0], newipl);
114 1.11 matt ci->ci_cpl = newipl;
115 1.13 matt if ((psw & I32_bit) == 0)
116 1.13 matt cpsie(I32_bit);
117 1.13 matt }
118 1.13 matt #endif
119 1.13 matt
120 1.13 matt #ifdef MULTIPROCESSOR
121 1.13 matt int
122 1.34 matt pic_ipi_ast(void *arg)
123 1.34 matt {
124 1.34 matt setsoftast(curcpu());
125 1.34 matt return 1;
126 1.34 matt }
127 1.34 matt
128 1.34 matt int
129 1.13 matt pic_ipi_nop(void *arg)
130 1.13 matt {
131 1.13 matt /* do nothing */
132 1.13 matt return 1;
133 1.13 matt }
134 1.13 matt
135 1.13 matt int
136 1.13 matt pic_ipi_xcall(void *arg)
137 1.13 matt {
138 1.13 matt xc_ipi_handler();
139 1.13 matt return 1;
140 1.13 matt }
141 1.13 matt
142 1.22 rmind int
143 1.22 rmind pic_ipi_generic(void *arg)
144 1.22 rmind {
145 1.22 rmind ipi_cpu_handler();
146 1.22 rmind return 1;
147 1.22 rmind }
148 1.22 rmind
149 1.21 matt #ifdef DDB
150 1.21 matt int
151 1.21 matt pic_ipi_ddb(void *arg)
152 1.21 matt {
153 1.23 skrll // printf("%s: %s: tf=%p\n", __func__, curcpu()->ci_cpuname, arg);
154 1.21 matt kdb_trap(-1, arg);
155 1.21 matt return 1;
156 1.21 matt }
157 1.39 nisimura #endif /* DDB */
158 1.34 matt
159 1.34 matt #ifdef __HAVE_PREEMPTION
160 1.34 matt int
161 1.34 matt pic_ipi_kpreempt(void *arg)
162 1.34 matt {
163 1.34 matt atomic_or_uint(&curcpu()->ci_astpending, __BIT(1));
164 1.34 matt return 1;
165 1.34 matt }
166 1.39 nisimura #endif /* __HAVE_PREEMPTION */
167 1.21 matt
168 1.13 matt void
169 1.13 matt intr_cpu_init(struct cpu_info *ci)
170 1.13 matt {
171 1.13 matt for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
172 1.13 matt struct pic_softc * const pic = pic_list[slot];
173 1.13 matt if (pic != NULL && pic->pic_ops->pic_cpu_init != NULL) {
174 1.13 matt (*pic->pic_ops->pic_cpu_init)(pic, ci);
175 1.13 matt }
176 1.13 matt }
177 1.13 matt }
178 1.13 matt
179 1.13 matt typedef void (*pic_ipi_send_func_t)(struct pic_softc *, u_long);
180 1.13 matt
181 1.13 matt void
182 1.13 matt intr_ipi_send(const kcpuset_t *kcp, u_long ipi)
183 1.13 matt {
184 1.32 matt struct cpu_info * const ci = curcpu();
185 1.13 matt KASSERT(ipi < NIPI);
186 1.29 matt bool __diagused sent_p = false;
187 1.29 matt for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
188 1.29 matt struct pic_softc * const pic = pic_list[slot];
189 1.29 matt if (pic == NULL || pic->pic_cpus == NULL)
190 1.29 matt continue;
191 1.30 matt if (kcp == NULL || kcpuset_intersecting_p(kcp, pic->pic_cpus)) {
192 1.32 matt // never send to ourself
193 1.32 matt if (pic->pic_cpus == ci->ci_kcpuset)
194 1.32 matt continue;
195 1.32 matt
196 1.29 matt (*pic->pic_ops->pic_ipi_send)(pic, kcp, ipi);
197 1.29 matt // If we were targeting a single CPU or this pic
198 1.29 matt // handles all cpus, we're done.
199 1.29 matt if (kcp != NULL || pic->pic_cpus == kcpuset_running)
200 1.29 matt return;
201 1.29 matt sent_p = true;
202 1.29 matt }
203 1.29 matt }
204 1.43 ryo KASSERT(cold || sent_p || ncpu <= 1);
205 1.13 matt }
206 1.13 matt #endif /* MULTIPROCESSOR */
207 1.13 matt
208 1.13 matt #ifdef __HAVE_PIC_FAST_SOFTINTS
209 1.13 matt int
210 1.13 matt pic_handle_softint(void *arg)
211 1.13 matt {
212 1.13 matt void softint_switch(lwp_t *, int);
213 1.41 skrll struct cpu_info * const ci = curcpu();
214 1.13 matt const size_t softint = (size_t) arg;
215 1.13 matt int s = splhigh();
216 1.13 matt ci->ci_intr_depth--; // don't count these as interrupts
217 1.13 matt softint_switch(ci->ci_softlwps[softint], s);
218 1.13 matt ci->ci_intr_depth++;
219 1.13 matt splx(s);
220 1.13 matt return 1;
221 1.11 matt }
222 1.11 matt #endif
223 1.2 matt
224 1.2 matt int
225 1.2 matt pic_handle_intr(void *arg)
226 1.2 matt {
227 1.2 matt struct pic_softc * const pic = arg;
228 1.2 matt int rv;
229 1.2 matt
230 1.2 matt rv = (*pic->pic_ops->pic_find_pending_irqs)(pic);
231 1.2 matt
232 1.2 matt return rv > 0;
233 1.2 matt }
234 1.2 matt
235 1.28 matt #if defined(__HAVE_PIC_PENDING_INTRS)
236 1.2 matt void
237 1.2 matt pic_mark_pending_source(struct pic_softc *pic, struct intrsource *is)
238 1.2 matt {
239 1.2 matt const uint32_t ipl_mask = __BIT(is->is_ipl);
240 1.2 matt
241 1.4 matt atomic_or_32(&pic->pic_pending_irqs[is->is_irq >> 5],
242 1.4 matt __BIT(is->is_irq & 0x1f));
243 1.2 matt
244 1.4 matt atomic_or_32(&pic->pic_pending_ipls, ipl_mask);
245 1.29 matt #ifdef MULTIPROCESSOR
246 1.29 matt struct pic_pending *pend = percpu_getref(pic_pending_percpu);
247 1.29 matt #else
248 1.29 matt struct pic_pending *pend = &pic_pending;
249 1.29 matt #endif
250 1.29 matt atomic_or_32(&pend->pending_ipls, ipl_mask);
251 1.29 matt atomic_or_32(&pend->pending_pics, __BIT(pic->pic_id));
252 1.29 matt #ifdef MULTIPROCESSOR
253 1.29 matt percpu_putref(pic_pending_percpu);
254 1.29 matt #endif
255 1.2 matt }
256 1.2 matt
257 1.2 matt void
258 1.2 matt pic_mark_pending(struct pic_softc *pic, int irq)
259 1.2 matt {
260 1.2 matt struct intrsource * const is = pic->pic_sources[irq];
261 1.2 matt
262 1.2 matt KASSERT(irq < pic->pic_maxsources);
263 1.2 matt KASSERT(is != NULL);
264 1.2 matt
265 1.2 matt pic_mark_pending_source(pic, is);
266 1.2 matt }
267 1.2 matt
268 1.2 matt uint32_t
269 1.2 matt pic_mark_pending_sources(struct pic_softc *pic, size_t irq_base,
270 1.2 matt uint32_t pending)
271 1.2 matt {
272 1.2 matt struct intrsource ** const isbase = &pic->pic_sources[irq_base];
273 1.2 matt struct intrsource *is;
274 1.4 matt volatile uint32_t *ipending = &pic->pic_pending_irqs[irq_base >> 5];
275 1.2 matt uint32_t ipl_mask = 0;
276 1.2 matt
277 1.2 matt if (pending == 0)
278 1.2 matt return ipl_mask;
279 1.2 matt
280 1.2 matt KASSERT((irq_base & 31) == 0);
281 1.41 skrll
282 1.2 matt (*pic->pic_ops->pic_block_irqs)(pic, irq_base, pending);
283 1.2 matt
284 1.4 matt atomic_or_32(ipending, pending);
285 1.40 skrll while (pending != 0) {
286 1.2 matt int n = ffs(pending);
287 1.2 matt if (n-- == 0)
288 1.2 matt break;
289 1.2 matt is = isbase[n];
290 1.2 matt KASSERT(is != NULL);
291 1.2 matt KASSERT(irq_base <= is->is_irq && is->is_irq < irq_base + 32);
292 1.2 matt pending &= ~__BIT(n);
293 1.2 matt ipl_mask |= __BIT(is->is_ipl);
294 1.2 matt }
295 1.2 matt
296 1.4 matt atomic_or_32(&pic->pic_pending_ipls, ipl_mask);
297 1.29 matt #ifdef MULTIPROCESSOR
298 1.29 matt struct pic_pending *pend = percpu_getref(pic_pending_percpu);
299 1.29 matt #else
300 1.29 matt struct pic_pending *pend = &pic_pending;
301 1.29 matt #endif
302 1.29 matt atomic_or_32(&pend->pending_ipls, ipl_mask);
303 1.29 matt atomic_or_32(&pend->pending_pics, __BIT(pic->pic_id));
304 1.29 matt #ifdef MULTIPROCESSOR
305 1.29 matt percpu_putref(pic_pending_percpu);
306 1.29 matt #endif
307 1.2 matt return ipl_mask;
308 1.2 matt }
309 1.2 matt
310 1.2 matt uint32_t
311 1.2 matt pic_find_pending_irqs_by_ipl(struct pic_softc *pic, size_t irq_base,
312 1.2 matt uint32_t pending, int ipl)
313 1.2 matt {
314 1.2 matt uint32_t ipl_irq_mask = 0;
315 1.2 matt uint32_t irq_mask;
316 1.2 matt
317 1.2 matt for (;;) {
318 1.2 matt int irq = ffs(pending);
319 1.2 matt if (irq-- == 0)
320 1.2 matt return ipl_irq_mask;
321 1.2 matt
322 1.2 matt irq_mask = __BIT(irq);
323 1.8 bsh #if 1
324 1.10 skrll KASSERTMSG(pic->pic_sources[irq_base + irq] != NULL,
325 1.10 skrll "%s: irq_base %zu irq %d\n", __func__, irq_base, irq);
326 1.8 bsh #else
327 1.8 bsh if (pic->pic_sources[irq_base + irq] == NULL) {
328 1.8 bsh aprint_error("stray interrupt? irq_base=%zu irq=%d\n",
329 1.8 bsh irq_base, irq);
330 1.8 bsh } else
331 1.8 bsh #endif
332 1.2 matt if (pic->pic_sources[irq_base + irq]->is_ipl == ipl)
333 1.2 matt ipl_irq_mask |= irq_mask;
334 1.2 matt
335 1.2 matt pending &= ~irq_mask;
336 1.2 matt }
337 1.2 matt }
338 1.28 matt #endif /* __HAVE_PIC_PENDING_INTRS */
339 1.2 matt
340 1.2 matt void
341 1.2 matt pic_dispatch(struct intrsource *is, void *frame)
342 1.2 matt {
343 1.20 matt int (*func)(void *) = is->is_func;
344 1.20 matt void *arg = is->is_arg;
345 1.2 matt
346 1.20 matt if (__predict_false(arg == NULL)) {
347 1.20 matt if (__predict_false(frame == NULL)) {
348 1.20 matt pic_deferral_ev.ev_count++;
349 1.20 matt return;
350 1.20 matt }
351 1.20 matt arg = frame;
352 1.2 matt }
353 1.13 matt
354 1.20 matt #ifdef MULTIPROCESSOR
355 1.20 matt if (!is->is_mpsafe) {
356 1.20 matt KERNEL_LOCK(1, NULL);
357 1.21 matt const u_int ci_blcnt __diagused = curcpu()->ci_biglock_count;
358 1.21 matt const u_int l_blcnt __diagused = curlwp->l_blcnt;
359 1.20 matt (void)(*func)(arg);
360 1.21 matt KASSERT(ci_blcnt == curcpu()->ci_biglock_count);
361 1.21 matt KASSERT(l_blcnt == curlwp->l_blcnt);
362 1.20 matt KERNEL_UNLOCK_ONE(NULL);
363 1.20 matt } else
364 1.20 matt #endif
365 1.20 matt (void)(*func)(arg);
366 1.20 matt
367 1.20 matt
368 1.13 matt struct pic_percpu * const pcpu = percpu_getref(is->is_pic->pic_percpu);
369 1.13 matt KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
370 1.13 matt pcpu->pcpu_evs[is->is_irq].ev_count++;
371 1.13 matt percpu_putref(is->is_pic->pic_percpu);
372 1.2 matt }
373 1.2 matt
374 1.28 matt #if defined(__HAVE_PIC_PENDING_INTRS)
375 1.2 matt void
376 1.29 matt pic_deliver_irqs(struct pic_pending *pend, struct pic_softc *pic, int ipl,
377 1.29 matt void *frame)
378 1.2 matt {
379 1.2 matt const uint32_t ipl_mask = __BIT(ipl);
380 1.2 matt struct intrsource *is;
381 1.4 matt volatile uint32_t *ipending = pic->pic_pending_irqs;
382 1.4 matt volatile uint32_t *iblocked = pic->pic_blocked_irqs;
383 1.2 matt size_t irq_base;
384 1.2 matt #if PIC_MAXSOURCES > 32
385 1.2 matt size_t irq_count;
386 1.6 kiyohara int poi = 0; /* Possibility of interrupting */
387 1.2 matt #endif
388 1.2 matt uint32_t pending_irqs;
389 1.2 matt uint32_t blocked_irqs;
390 1.2 matt int irq;
391 1.19 martin bool progress __diagused = false;
392 1.29 matt
393 1.2 matt KASSERT(pic->pic_pending_ipls & ipl_mask);
394 1.2 matt
395 1.2 matt irq_base = 0;
396 1.2 matt #if PIC_MAXSOURCES > 32
397 1.2 matt irq_count = 0;
398 1.2 matt #endif
399 1.2 matt
400 1.2 matt for (;;) {
401 1.2 matt pending_irqs = pic_find_pending_irqs_by_ipl(pic, irq_base,
402 1.2 matt *ipending, ipl);
403 1.2 matt KASSERT((pending_irqs & *ipending) == pending_irqs);
404 1.2 matt KASSERT((pending_irqs & ~(*ipending)) == 0);
405 1.2 matt if (pending_irqs == 0) {
406 1.2 matt #if PIC_MAXSOURCES > 32
407 1.2 matt irq_count += 32;
408 1.6 kiyohara if (__predict_true(irq_count >= pic->pic_maxsources)) {
409 1.6 kiyohara if (!poi)
410 1.6 kiyohara /*Interrupt at this level was handled.*/
411 1.6 kiyohara break;
412 1.6 kiyohara irq_base = 0;
413 1.6 kiyohara irq_count = 0;
414 1.6 kiyohara poi = 0;
415 1.2 matt ipending = pic->pic_pending_irqs;
416 1.2 matt iblocked = pic->pic_blocked_irqs;
417 1.6 kiyohara } else {
418 1.6 kiyohara irq_base += 32;
419 1.6 kiyohara ipending++;
420 1.6 kiyohara iblocked++;
421 1.6 kiyohara KASSERT(irq_base <= pic->pic_maxsources);
422 1.2 matt }
423 1.2 matt continue;
424 1.2 matt #else
425 1.2 matt break;
426 1.2 matt #endif
427 1.2 matt }
428 1.2 matt progress = true;
429 1.5 kiyohara blocked_irqs = 0;
430 1.2 matt do {
431 1.2 matt irq = ffs(pending_irqs) - 1;
432 1.2 matt KASSERT(irq >= 0);
433 1.2 matt
434 1.4 matt atomic_and_32(ipending, ~__BIT(irq));
435 1.2 matt is = pic->pic_sources[irq_base + irq];
436 1.2 matt if (is != NULL) {
437 1.2 matt cpsie(I32_bit);
438 1.2 matt pic_dispatch(is, frame);
439 1.2 matt cpsid(I32_bit);
440 1.6 kiyohara #if PIC_MAXSOURCES > 32
441 1.6 kiyohara /*
442 1.6 kiyohara * There is a possibility of interrupting
443 1.6 kiyohara * from cpsie() to cpsid().
444 1.6 kiyohara */
445 1.6 kiyohara poi = 1;
446 1.6 kiyohara #endif
447 1.5 kiyohara blocked_irqs |= __BIT(irq);
448 1.2 matt } else {
449 1.2 matt KASSERT(0);
450 1.2 matt }
451 1.2 matt pending_irqs = pic_find_pending_irqs_by_ipl(pic,
452 1.2 matt irq_base, *ipending, ipl);
453 1.2 matt } while (pending_irqs);
454 1.2 matt if (blocked_irqs) {
455 1.4 matt atomic_or_32(iblocked, blocked_irqs);
456 1.29 matt atomic_or_32(&pend->blocked_pics, __BIT(pic->pic_id));
457 1.2 matt }
458 1.2 matt }
459 1.2 matt
460 1.2 matt KASSERT(progress);
461 1.2 matt /*
462 1.2 matt * Since interrupts are disabled, we don't have to be too careful
463 1.2 matt * about these.
464 1.2 matt */
465 1.4 matt if (atomic_and_32_nv(&pic->pic_pending_ipls, ~ipl_mask) == 0)
466 1.29 matt atomic_and_32(&pend->pending_pics, ~__BIT(pic->pic_id));
467 1.2 matt }
468 1.2 matt
469 1.2 matt static void
470 1.29 matt pic_list_unblock_irqs(struct pic_pending *pend)
471 1.2 matt {
472 1.29 matt uint32_t blocked_pics = pend->blocked_pics;
473 1.29 matt
474 1.29 matt pend->blocked_pics = 0;
475 1.2 matt
476 1.2 matt for (;;) {
477 1.2 matt struct pic_softc *pic;
478 1.2 matt #if PIC_MAXSOURCES > 32
479 1.4 matt volatile uint32_t *iblocked;
480 1.4 matt uint32_t blocked;
481 1.2 matt size_t irq_base;
482 1.2 matt #endif
483 1.2 matt
484 1.4 matt int pic_id = ffs(blocked_pics);
485 1.2 matt if (pic_id-- == 0)
486 1.2 matt return;
487 1.2 matt
488 1.2 matt pic = pic_list[pic_id];
489 1.2 matt KASSERT(pic != NULL);
490 1.2 matt #if PIC_MAXSOURCES > 32
491 1.2 matt for (irq_base = 0, iblocked = pic->pic_blocked_irqs;
492 1.2 matt irq_base < pic->pic_maxsources;
493 1.2 matt irq_base += 32, iblocked++) {
494 1.4 matt if ((blocked = *iblocked) != 0) {
495 1.2 matt (*pic->pic_ops->pic_unblock_irqs)(pic,
496 1.4 matt irq_base, blocked);
497 1.4 matt atomic_and_32(iblocked, ~blocked);
498 1.2 matt }
499 1.2 matt }
500 1.2 matt #else
501 1.2 matt KASSERT(pic->pic_blocked_irqs[0] != 0);
502 1.2 matt (*pic->pic_ops->pic_unblock_irqs)(pic,
503 1.2 matt 0, pic->pic_blocked_irqs[0]);
504 1.4 matt pic->pic_blocked_irqs[0] = 0;
505 1.2 matt #endif
506 1.4 matt blocked_pics &= ~__BIT(pic_id);
507 1.2 matt }
508 1.2 matt }
509 1.2 matt
510 1.2 matt
511 1.2 matt struct pic_softc *
512 1.29 matt pic_list_find_pic_by_pending_ipl(struct pic_pending *pend, uint32_t ipl_mask)
513 1.2 matt {
514 1.29 matt uint32_t pending_pics = pend->pending_pics;
515 1.2 matt struct pic_softc *pic;
516 1.2 matt
517 1.2 matt for (;;) {
518 1.4 matt int pic_id = ffs(pending_pics);
519 1.2 matt if (pic_id-- == 0)
520 1.2 matt return NULL;
521 1.2 matt
522 1.2 matt pic = pic_list[pic_id];
523 1.2 matt KASSERT(pic != NULL);
524 1.2 matt if (pic->pic_pending_ipls & ipl_mask)
525 1.2 matt return pic;
526 1.4 matt pending_pics &= ~__BIT(pic_id);
527 1.2 matt }
528 1.2 matt }
529 1.2 matt
530 1.2 matt void
531 1.29 matt pic_list_deliver_irqs(struct pic_pending *pend, register_t psw, int ipl,
532 1.29 matt void *frame)
533 1.2 matt {
534 1.2 matt const uint32_t ipl_mask = __BIT(ipl);
535 1.2 matt struct pic_softc *pic;
536 1.2 matt
537 1.29 matt while ((pic = pic_list_find_pic_by_pending_ipl(pend, ipl_mask)) != NULL) {
538 1.29 matt pic_deliver_irqs(pend, pic, ipl, frame);
539 1.2 matt KASSERT((pic->pic_pending_ipls & ipl_mask) == 0);
540 1.2 matt }
541 1.29 matt atomic_and_32(&pend->pending_ipls, ~ipl_mask);
542 1.2 matt }
543 1.28 matt #endif /* __HAVE_PIC_PENDING_INTRS */
544 1.2 matt
545 1.2 matt void
546 1.2 matt pic_do_pending_ints(register_t psw, int newipl, void *frame)
547 1.2 matt {
548 1.2 matt struct cpu_info * const ci = curcpu();
549 1.13 matt if (__predict_false(newipl == IPL_HIGH)) {
550 1.13 matt KASSERTMSG(ci->ci_cpl == IPL_HIGH, "cpl %d", ci->ci_cpl);
551 1.2 matt return;
552 1.13 matt }
553 1.28 matt #if defined(__HAVE_PIC_PENDING_INTRS)
554 1.29 matt #ifdef MULTIPROCESSOR
555 1.29 matt struct pic_pending *pend = percpu_getref(pic_pending_percpu);
556 1.29 matt #else
557 1.29 matt struct pic_pending *pend = &pic_pending;
558 1.29 matt #endif
559 1.29 matt while ((pend->pending_ipls & ~__BIT(newipl)) > __BIT(newipl)) {
560 1.29 matt KASSERT(pend->pending_ipls < __BIT(NIPL));
561 1.2 matt for (;;) {
562 1.29 matt int ipl = 31 - __builtin_clz(pend->pending_ipls);
563 1.2 matt KASSERT(ipl < NIPL);
564 1.2 matt if (ipl <= newipl)
565 1.2 matt break;
566 1.2 matt
567 1.12 matt pic_set_priority(ci, ipl);
568 1.29 matt pic_list_deliver_irqs(pend, psw, ipl, frame);
569 1.29 matt pic_list_unblock_irqs(pend);
570 1.2 matt }
571 1.2 matt }
572 1.29 matt #ifdef MULTIPROCESSOR
573 1.29 matt percpu_putref(pic_pending_percpu);
574 1.29 matt #endif
575 1.28 matt #endif /* __HAVE_PIC_PENDING_INTRS */
576 1.33 jmcneill #ifdef __HAVE_PREEMPTION
577 1.27 matt if (newipl == IPL_NONE && (ci->ci_astpending & __BIT(1))) {
578 1.27 matt pic_set_priority(ci, IPL_SCHED);
579 1.27 matt kpreempt(0);
580 1.27 matt }
581 1.27 matt #endif
582 1.2 matt if (ci->ci_cpl != newipl)
583 1.11 matt pic_set_priority(ci, newipl);
584 1.13 matt }
585 1.13 matt
586 1.13 matt static void
587 1.13 matt pic_percpu_allocate(void *v0, void *v1, struct cpu_info *ci)
588 1.13 matt {
589 1.13 matt struct pic_percpu * const pcpu = v0;
590 1.13 matt struct pic_softc * const pic = v1;
591 1.13 matt
592 1.13 matt pcpu->pcpu_evs = kmem_zalloc(pic->pic_maxsources * sizeof(pcpu->pcpu_evs[0]),
593 1.13 matt KM_SLEEP);
594 1.13 matt KASSERT(pcpu->pcpu_evs != NULL);
595 1.13 matt
596 1.13 matt #define PCPU_NAMELEN 32
597 1.14 matt #ifdef DIAGNOSTIC
598 1.13 matt const size_t namelen = strlen(pic->pic_name) + 4 + strlen(ci->ci_data.cpu_name);
599 1.14 matt #endif
600 1.13 matt
601 1.13 matt KASSERT(namelen < PCPU_NAMELEN);
602 1.13 matt pcpu->pcpu_name = kmem_alloc(PCPU_NAMELEN, KM_SLEEP);
603 1.13 matt #ifdef MULTIPROCESSOR
604 1.13 matt snprintf(pcpu->pcpu_name, PCPU_NAMELEN,
605 1.13 matt "%s (%s)", pic->pic_name, ci->ci_data.cpu_name);
606 1.13 matt #else
607 1.13 matt strlcpy(pcpu->pcpu_name, pic->pic_name, PCPU_NAMELEN);
608 1.13 matt #endif
609 1.13 matt pcpu->pcpu_magic = PICPERCPU_MAGIC;
610 1.13 matt #if 0
611 1.13 matt printf("%s: %s %s: <%s>\n",
612 1.13 matt __func__, ci->ci_data.cpu_name, pic->pic_name,
613 1.13 matt pcpu->pcpu_name);
614 1.2 matt #endif
615 1.2 matt }
616 1.2 matt
617 1.29 matt #if defined(__HAVE_PIC_PENDING_INTRS) && defined(MULTIPROCESSOR)
618 1.29 matt static void
619 1.29 matt pic_pending_zero(void *v0, void *v1, struct cpu_info *ci)
620 1.29 matt {
621 1.29 matt struct pic_pending * const p = v0;
622 1.29 matt memset(p, 0, sizeof(*p));
623 1.29 matt }
624 1.29 matt #endif /* __HAVE_PIC_PENDING_INTRS && MULTIPROCESSOR */
625 1.29 matt
626 1.35 skrll static int
627 1.35 skrll pic_init(void)
628 1.35 skrll {
629 1.35 skrll
630 1.35 skrll mutex_init(&pic_lock, MUTEX_DEFAULT, IPL_HIGH);
631 1.35 skrll
632 1.35 skrll return 0;
633 1.35 skrll }
634 1.35 skrll
635 1.2 matt void
636 1.2 matt pic_add(struct pic_softc *pic, int irqbase)
637 1.2 matt {
638 1.2 matt int slot, maybe_slot = -1;
639 1.35 skrll size_t sourcebase;
640 1.35 skrll static ONCE_DECL(pic_once);
641 1.35 skrll
642 1.35 skrll RUN_ONCE(&pic_once, pic_init);
643 1.2 matt
644 1.13 matt KASSERT(strlen(pic->pic_name) > 0);
645 1.13 matt
646 1.29 matt #if defined(__HAVE_PIC_PENDING_INTRS) && defined(MULTIPROCESSOR)
647 1.29 matt if (__predict_false(pic_pending_percpu == NULL)) {
648 1.29 matt pic_pending_percpu = percpu_alloc(sizeof(struct pic_pending));
649 1.29 matt
650 1.29 matt /*
651 1.29 matt * Now zero the per-cpu pending data.
652 1.29 matt */
653 1.29 matt percpu_foreach(pic_pending_percpu, pic_pending_zero, NULL);
654 1.29 matt }
655 1.29 matt #endif /* __HAVE_PIC_PENDING_INTRS && MULTIPROCESSOR */
656 1.29 matt
657 1.35 skrll mutex_enter(&pic_lock);
658 1.2 matt for (slot = 0; slot < PIC_MAXPICS; slot++) {
659 1.2 matt struct pic_softc * const xpic = pic_list[slot];
660 1.2 matt if (xpic == NULL) {
661 1.2 matt if (maybe_slot < 0)
662 1.2 matt maybe_slot = slot;
663 1.2 matt if (irqbase < 0)
664 1.2 matt break;
665 1.2 matt continue;
666 1.2 matt }
667 1.2 matt if (irqbase < 0 || xpic->pic_irqbase < 0)
668 1.2 matt continue;
669 1.2 matt if (irqbase >= xpic->pic_irqbase + xpic->pic_maxsources)
670 1.2 matt continue;
671 1.2 matt if (irqbase + pic->pic_maxsources <= xpic->pic_irqbase)
672 1.2 matt continue;
673 1.2 matt panic("pic_add: pic %s (%zu sources @ irq %u) conflicts"
674 1.2 matt " with pic %s (%zu sources @ irq %u)",
675 1.2 matt pic->pic_name, pic->pic_maxsources, irqbase,
676 1.2 matt xpic->pic_name, xpic->pic_maxsources, xpic->pic_irqbase);
677 1.2 matt }
678 1.2 matt slot = maybe_slot;
679 1.2 matt #if 0
680 1.2 matt printf("%s: pic_sourcebase=%zu pic_maxsources=%zu\n",
681 1.2 matt pic->pic_name, pic_sourcebase, pic->pic_maxsources);
682 1.2 matt #endif
683 1.17 matt KASSERTMSG(pic->pic_maxsources <= PIC_MAXSOURCES, "%zu",
684 1.17 matt pic->pic_maxsources);
685 1.2 matt KASSERT(pic_sourcebase + pic->pic_maxsources <= PIC_MAXMAXSOURCES);
686 1.35 skrll sourcebase = pic_sourcebase;
687 1.35 skrll pic_sourcebase += pic->pic_maxsources;
688 1.35 skrll
689 1.35 skrll mutex_exit(&pic_lock);
690 1.2 matt
691 1.13 matt /*
692 1.13 matt * Allocate a pointer to each cpu's evcnts and then, for each cpu,
693 1.13 matt * allocate its evcnts and then attach an evcnt for each pin.
694 1.13 matt * We can't allocate the evcnt structures directly since
695 1.41 skrll * percpu will move the contents of percpu memory around and
696 1.13 matt * corrupt the pointers in the evcnts themselves. Remember, any
697 1.13 matt * problem can be solved with sufficient indirection.
698 1.13 matt */
699 1.13 matt pic->pic_percpu = percpu_alloc(sizeof(struct pic_percpu));
700 1.13 matt
701 1.13 matt /*
702 1.13 matt * Now allocate the per-cpu evcnts.
703 1.13 matt */
704 1.13 matt percpu_foreach(pic->pic_percpu, pic_percpu_allocate, pic);
705 1.13 matt
706 1.35 skrll pic->pic_sources = &pic_sources[sourcebase];
707 1.2 matt pic->pic_irqbase = irqbase;
708 1.2 matt pic->pic_id = slot;
709 1.13 matt #ifdef __HAVE_PIC_SET_PRIORITY
710 1.13 matt KASSERT((slot == 0) == (pic->pic_ops->pic_set_priority != NULL));
711 1.13 matt #endif
712 1.13 matt #ifdef MULTIPROCESSOR
713 1.29 matt KASSERT((pic->pic_cpus != NULL) == (pic->pic_ops->pic_ipi_send != NULL));
714 1.13 matt #endif
715 1.2 matt pic_list[slot] = pic;
716 1.2 matt }
717 1.2 matt
718 1.2 matt int
719 1.2 matt pic_alloc_irq(struct pic_softc *pic)
720 1.2 matt {
721 1.2 matt int irq;
722 1.2 matt
723 1.2 matt for (irq = 0; irq < pic->pic_maxsources; irq++) {
724 1.2 matt if (pic->pic_sources[irq] == NULL)
725 1.2 matt return irq;
726 1.2 matt }
727 1.2 matt
728 1.2 matt return -1;
729 1.2 matt }
730 1.2 matt
731 1.13 matt static void
732 1.13 matt pic_percpu_evcnt_attach(void *v0, void *v1, struct cpu_info *ci)
733 1.13 matt {
734 1.13 matt struct pic_percpu * const pcpu = v0;
735 1.13 matt struct intrsource * const is = v1;
736 1.13 matt
737 1.13 matt KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
738 1.13 matt evcnt_attach_dynamic(&pcpu->pcpu_evs[is->is_irq], EVCNT_TYPE_INTR, NULL,
739 1.13 matt pcpu->pcpu_name, is->is_source);
740 1.13 matt }
741 1.13 matt
742 1.2 matt void *
743 1.2 matt pic_establish_intr(struct pic_softc *pic, int irq, int ipl, int type,
744 1.2 matt int (*func)(void *), void *arg)
745 1.2 matt {
746 1.2 matt struct intrsource *is;
747 1.2 matt int off, nipl;
748 1.2 matt
749 1.2 matt if (pic->pic_sources[irq]) {
750 1.2 matt printf("pic_establish_intr: pic %s irq %d already present\n",
751 1.2 matt pic->pic_name, irq);
752 1.2 matt return NULL;
753 1.2 matt }
754 1.2 matt
755 1.11 matt is = kmem_zalloc(sizeof(*is), KM_SLEEP);
756 1.2 matt is->is_pic = pic;
757 1.2 matt is->is_irq = irq;
758 1.2 matt is->is_ipl = ipl;
759 1.21 matt is->is_type = type & 0xff;
760 1.2 matt is->is_func = func;
761 1.2 matt is->is_arg = arg;
762 1.20 matt #ifdef MULTIPROCESSOR
763 1.24 skrll is->is_mpsafe = (type & IST_MPSAFE) || ipl != IPL_VM;
764 1.20 matt #endif
765 1.13 matt
766 1.2 matt if (pic->pic_ops->pic_source_name)
767 1.2 matt (*pic->pic_ops->pic_source_name)(pic, irq, is->is_source,
768 1.2 matt sizeof(is->is_source));
769 1.2 matt else
770 1.2 matt snprintf(is->is_source, sizeof(is->is_source), "irq %d", irq);
771 1.2 matt
772 1.13 matt /*
773 1.13 matt * Now attach the per-cpu evcnts.
774 1.13 matt */
775 1.13 matt percpu_foreach(pic->pic_percpu, pic_percpu_evcnt_attach, is);
776 1.2 matt
777 1.2 matt pic->pic_sources[irq] = is;
778 1.2 matt
779 1.2 matt /*
780 1.2 matt * First try to use an existing slot which is empty.
781 1.2 matt */
782 1.2 matt for (off = pic_ipl_offset[ipl]; off < pic_ipl_offset[ipl+1]; off++) {
783 1.2 matt if (pic__iplsources[off] == NULL) {
784 1.2 matt is->is_iplidx = off - pic_ipl_offset[ipl];
785 1.2 matt pic__iplsources[off] = is;
786 1.36 mlelstv goto unblock;
787 1.2 matt }
788 1.2 matt }
789 1.2 matt
790 1.2 matt /*
791 1.2 matt * Move up all the sources by one.
792 1.2 matt */
793 1.2 matt if (ipl < NIPL) {
794 1.2 matt off = pic_ipl_offset[ipl+1];
795 1.2 matt memmove(&pic__iplsources[off+1], &pic__iplsources[off],
796 1.2 matt sizeof(pic__iplsources[0]) * (pic_ipl_offset[NIPL] - off));
797 1.2 matt }
798 1.2 matt
799 1.2 matt /*
800 1.2 matt * Advance the offset of all IPLs higher than this. Include an
801 1.2 matt * extra one as well. Thus the number of sources per ipl is
802 1.2 matt * pic_ipl_offset[ipl+1] - pic_ipl_offset[ipl].
803 1.2 matt */
804 1.2 matt for (nipl = ipl + 1; nipl <= NIPL; nipl++)
805 1.2 matt pic_ipl_offset[nipl]++;
806 1.2 matt
807 1.2 matt /*
808 1.2 matt * Insert into the previously made position at the end of this IPL's
809 1.2 matt * sources.
810 1.2 matt */
811 1.2 matt off = pic_ipl_offset[ipl + 1] - 1;
812 1.2 matt is->is_iplidx = off - pic_ipl_offset[ipl];
813 1.2 matt pic__iplsources[off] = is;
814 1.2 matt
815 1.2 matt (*pic->pic_ops->pic_establish_irq)(pic, is);
816 1.2 matt
817 1.36 mlelstv unblock:
818 1.2 matt (*pic->pic_ops->pic_unblock_irqs)(pic, is->is_irq & ~0x1f,
819 1.2 matt __BIT(is->is_irq & 0x1f));
820 1.41 skrll
821 1.2 matt /* We're done. */
822 1.2 matt return is;
823 1.2 matt }
824 1.2 matt
825 1.13 matt static void
826 1.13 matt pic_percpu_evcnt_deattach(void *v0, void *v1, struct cpu_info *ci)
827 1.13 matt {
828 1.13 matt struct pic_percpu * const pcpu = v0;
829 1.13 matt struct intrsource * const is = v1;
830 1.13 matt
831 1.13 matt KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
832 1.13 matt evcnt_detach(&pcpu->pcpu_evs[is->is_irq]);
833 1.13 matt }
834 1.13 matt
835 1.2 matt void
836 1.2 matt pic_disestablish_source(struct intrsource *is)
837 1.2 matt {
838 1.2 matt struct pic_softc * const pic = is->is_pic;
839 1.2 matt const int irq = is->is_irq;
840 1.2 matt
841 1.13 matt KASSERT(is == pic->pic_sources[irq]);
842 1.13 matt
843 1.15 msaitoh (*pic->pic_ops->pic_block_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
844 1.2 matt pic->pic_sources[irq] = NULL;
845 1.2 matt pic__iplsources[pic_ipl_offset[is->is_ipl] + is->is_iplidx] = NULL;
846 1.13 matt /*
847 1.13 matt * Now detach the per-cpu evcnts.
848 1.13 matt */
849 1.13 matt percpu_foreach(pic->pic_percpu, pic_percpu_evcnt_deattach, is);
850 1.2 matt
851 1.11 matt kmem_free(is, sizeof(*is));
852 1.2 matt }
853 1.2 matt
854 1.2 matt void *
855 1.2 matt intr_establish(int irq, int ipl, int type, int (*func)(void *), void *arg)
856 1.2 matt {
857 1.11 matt KASSERT(!cpu_intr_p());
858 1.11 matt KASSERT(!cpu_softintr_p());
859 1.11 matt
860 1.13 matt for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
861 1.2 matt struct pic_softc * const pic = pic_list[slot];
862 1.2 matt if (pic == NULL || pic->pic_irqbase < 0)
863 1.2 matt continue;
864 1.2 matt if (pic->pic_irqbase <= irq
865 1.2 matt && irq < pic->pic_irqbase + pic->pic_maxsources) {
866 1.2 matt return pic_establish_intr(pic, irq - pic->pic_irqbase,
867 1.2 matt ipl, type, func, arg);
868 1.2 matt }
869 1.2 matt }
870 1.2 matt
871 1.2 matt return NULL;
872 1.2 matt }
873 1.2 matt
874 1.2 matt void
875 1.2 matt intr_disestablish(void *ih)
876 1.2 matt {
877 1.2 matt struct intrsource * const is = ih;
878 1.13 matt
879 1.13 matt KASSERT(!cpu_intr_p());
880 1.13 matt KASSERT(!cpu_softintr_p());
881 1.13 matt
882 1.2 matt pic_disestablish_source(is);
883 1.2 matt }
884