pic.c revision 1.59 1 /* $NetBSD: pic.c,v 1.59 2020/10/26 07:14:42 skrll Exp $ */
2 /*-
3 * Copyright (c) 2008 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Matt Thomas.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #define _INTR_PRIVATE
32 #include "opt_ddb.h"
33 #include "opt_multiprocessor.h"
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: pic.c,v 1.59 2020/10/26 07:14:42 skrll Exp $");
37
38 #include <sys/param.h>
39 #include <sys/atomic.h>
40 #include <sys/cpu.h>
41 #include <sys/evcnt.h>
42 #include <sys/interrupt.h>
43 #include <sys/intr.h>
44 #include <sys/ipi.h>
45 #include <sys/kernel.h>
46 #include <sys/kmem.h>
47 #include <sys/mutex.h>
48 #include <sys/once.h>
49 #include <sys/xcall.h>
50
51 #include <arm/armreg.h>
52 #include <arm/cpufunc.h>
53 #include <arm/locore.h> /* for compat aarch64 */
54
55 #ifdef DDB
56 #include <arm/db_machdep.h>
57 #endif
58
59 #include <arm/pic/picvar.h>
60
61 #if defined(__HAVE_PIC_PENDING_INTRS)
62 /*
63 * This implementation of pending interrupts on a MULTIPROCESSOR system makes
64 * the assumption that a PIC (pic_softc) shall only have all its interrupts
65 * come from the same CPU. In other words, interrupts from a single PIC will
66 * not be distributed among multiple CPUs.
67 */
68 struct pic_pending {
69 volatile uint32_t blocked_pics;
70 volatile uint32_t pending_pics;
71 volatile uint32_t pending_ipls;
72 };
73 static uint32_t
74 pic_find_pending_irqs_by_ipl(struct pic_softc *, size_t, uint32_t, int);
75 static struct pic_softc *
76 pic_list_find_pic_by_pending_ipl(struct pic_pending *, uint32_t);
77 static void
78 pic_deliver_irqs(struct pic_pending *, struct pic_softc *, int, void *);
79 static void
80 pic_list_deliver_irqs(struct pic_pending *, register_t, int, void *);
81
82 #ifdef MULTIPROCESSOR
83 percpu_t *pic_pending_percpu;
84 static struct pic_pending *
85 pic_pending_get(void)
86 {
87 return percpu_getref(pic_pending_percpu);
88 }
89 static void
90 pic_pending_put(struct pic_pending *pend)
91 {
92 percpu_putref(pic_pending_percpu);
93 }
94 #else
95 struct pic_pending pic_pending;
96 #define pic_pending_get() (&pic_pending)
97 #define pic_pending_put(pend) __nothing
98 #endif /* MULTIPROCESSOR */
99 #endif /* __HAVE_PIC_PENDING_INTRS */
100
101 struct pic_softc *pic_list[PIC_MAXPICS];
102 #if PIC_MAXPICS > 32
103 #error PIC_MAXPICS > 32 not supported
104 #endif
105 struct intrsource *pic_sources[PIC_MAXMAXSOURCES];
106 struct intrsource *pic__iplsources[PIC_MAXMAXSOURCES];
107 struct intrsource **pic_iplsource[NIPL] = {
108 [0 ... NIPL-1] = pic__iplsources,
109 };
110 size_t pic_ipl_offset[NIPL+1];
111
112 static kmutex_t pic_lock;
113 static size_t pic_sourcebase;
114 static int pic_lastbase;
115 static struct evcnt pic_deferral_ev =
116 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "deferred", "intr");
117 EVCNT_ATTACH_STATIC(pic_deferral_ev);
118
119 static int pic_init(void);
120
121 #ifdef __HAVE_PIC_SET_PRIORITY
122 void
123 pic_set_priority(struct cpu_info *ci, int newipl)
124 {
125 register_t psw = cpsid(I32_bit);
126 if (pic_list[0] != NULL)
127 (pic_list[0]->pic_ops->pic_set_priority)(pic_list[0], newipl);
128 ci->ci_cpl = newipl;
129 if ((psw & I32_bit) == 0)
130 cpsie(I32_bit);
131 }
132 #endif
133
134 #ifdef MULTIPROCESSOR
135 int
136 pic_ipi_ast(void *arg)
137 {
138 setsoftast(curcpu());
139 return 1;
140 }
141
142 int
143 pic_ipi_nop(void *arg)
144 {
145 /* do nothing */
146 return 1;
147 }
148
149 int
150 pic_ipi_xcall(void *arg)
151 {
152 xc_ipi_handler();
153 return 1;
154 }
155
156 int
157 pic_ipi_generic(void *arg)
158 {
159 ipi_cpu_handler();
160 return 1;
161 }
162
163 #ifdef DDB
164 int
165 pic_ipi_ddb(void *arg)
166 {
167 // printf("%s: %s: tf=%p\n", __func__, curcpu()->ci_cpuname, arg);
168 kdb_trap(-1, arg);
169 return 1;
170 }
171 #endif /* DDB */
172
173 #ifdef __HAVE_PREEMPTION
174 int
175 pic_ipi_kpreempt(void *arg)
176 {
177 atomic_or_uint(&curcpu()->ci_astpending, __BIT(1));
178 return 1;
179 }
180 #endif /* __HAVE_PREEMPTION */
181
182 void
183 intr_cpu_init(struct cpu_info *ci)
184 {
185 for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
186 struct pic_softc * const pic = pic_list[slot];
187 if (pic != NULL && pic->pic_ops->pic_cpu_init != NULL) {
188 (*pic->pic_ops->pic_cpu_init)(pic, ci);
189 }
190 }
191 }
192
193 typedef void (*pic_ipi_send_func_t)(struct pic_softc *, u_long);
194
195 void
196 intr_ipi_send(const kcpuset_t *kcp, u_long ipi)
197 {
198 struct cpu_info * const ci = curcpu();
199 KASSERT(ipi < NIPI);
200 bool __diagused sent_p = false;
201 for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
202 struct pic_softc * const pic = pic_list[slot];
203 if (pic == NULL || pic->pic_cpus == NULL)
204 continue;
205 if (kcp == NULL || kcpuset_intersecting_p(kcp, pic->pic_cpus)) {
206 // never send to ourself
207 if (pic->pic_cpus == ci->ci_kcpuset)
208 continue;
209
210 (*pic->pic_ops->pic_ipi_send)(pic, kcp, ipi);
211 /*
212 * If we were targeting a single CPU or this pic
213 * handles all cpus, we're done.
214 */
215 if (kcp != NULL || pic->pic_cpus == kcpuset_running)
216 return;
217 sent_p = true;
218 }
219 }
220 KASSERTMSG(cold || sent_p || ncpu <= 1, "cold %d sent_p %d ncpu %d",
221 cold, sent_p, ncpu);
222 }
223 #endif /* MULTIPROCESSOR */
224
225 #ifdef __HAVE_PIC_FAST_SOFTINTS
226 int
227 pic_handle_softint(void *arg)
228 {
229 void softint_switch(lwp_t *, int);
230 struct cpu_info * const ci = curcpu();
231 const size_t softint = (size_t) arg;
232 int s = splhigh();
233 ci->ci_intr_depth--; // don't count these as interrupts
234 softint_switch(ci->ci_softlwps[softint], s);
235 ci->ci_intr_depth++;
236 splx(s);
237 return 1;
238 }
239 #endif
240
241 int
242 pic_handle_intr(void *arg)
243 {
244 struct pic_softc * const pic = arg;
245 int rv;
246
247 rv = (*pic->pic_ops->pic_find_pending_irqs)(pic);
248
249 return rv > 0;
250 }
251
252 #if defined(__HAVE_PIC_PENDING_INTRS)
253 void
254 pic_mark_pending_source(struct pic_softc *pic, struct intrsource *is)
255 {
256 const uint32_t ipl_mask = __BIT(is->is_ipl);
257
258 atomic_or_32(&pic->pic_pending_irqs[is->is_irq >> 5],
259 __BIT(is->is_irq & 0x1f));
260
261 atomic_or_32(&pic->pic_pending_ipls, ipl_mask);
262 struct pic_pending *pend = pic_pending_get();
263 atomic_or_32(&pend->pending_ipls, ipl_mask);
264 atomic_or_32(&pend->pending_pics, __BIT(pic->pic_id));
265 pic_pending_put(pend);
266 }
267
268 void
269 pic_mark_pending(struct pic_softc *pic, int irq)
270 {
271 struct intrsource * const is = pic->pic_sources[irq];
272
273 KASSERT(irq < pic->pic_maxsources);
274 KASSERT(is != NULL);
275
276 pic_mark_pending_source(pic, is);
277 }
278
279 uint32_t
280 pic_mark_pending_sources(struct pic_softc *pic, size_t irq_base,
281 uint32_t pending)
282 {
283 struct intrsource ** const isbase = &pic->pic_sources[irq_base];
284 struct intrsource *is;
285 volatile uint32_t *ipending = &pic->pic_pending_irqs[irq_base >> 5];
286 uint32_t ipl_mask = 0;
287
288 if (pending == 0)
289 return ipl_mask;
290
291 KASSERT((irq_base & 31) == 0);
292
293 (*pic->pic_ops->pic_block_irqs)(pic, irq_base, pending);
294
295 atomic_or_32(ipending, pending);
296 while (pending != 0) {
297 int n = ffs(pending);
298 if (n-- == 0)
299 break;
300 is = isbase[n];
301 KASSERT(is != NULL);
302 KASSERT(irq_base <= is->is_irq && is->is_irq < irq_base + 32);
303 pending &= ~__BIT(n);
304 ipl_mask |= __BIT(is->is_ipl);
305 }
306
307 atomic_or_32(&pic->pic_pending_ipls, ipl_mask);
308 struct pic_pending *pend = pic_pending_get();
309 atomic_or_32(&pend->pending_ipls, ipl_mask);
310 atomic_or_32(&pend->pending_pics, __BIT(pic->pic_id));
311 pic_pending_put(pend);
312 return ipl_mask;
313 }
314
315 uint32_t
316 pic_find_pending_irqs_by_ipl(struct pic_softc *pic, size_t irq_base,
317 uint32_t pending, int ipl)
318 {
319 uint32_t ipl_irq_mask = 0;
320 uint32_t irq_mask;
321
322 for (;;) {
323 int irq = ffs(pending);
324 if (irq-- == 0)
325 return ipl_irq_mask;
326
327 irq_mask = __BIT(irq);
328 #if 1
329 KASSERTMSG(pic->pic_sources[irq_base + irq] != NULL,
330 "%s: irq_base %zu irq %d\n", __func__, irq_base, irq);
331 #else
332 if (pic->pic_sources[irq_base + irq] == NULL) {
333 aprint_error("stray interrupt? irq_base=%zu irq=%d\n",
334 irq_base, irq);
335 } else
336 #endif
337 if (pic->pic_sources[irq_base + irq]->is_ipl == ipl)
338 ipl_irq_mask |= irq_mask;
339
340 pending &= ~irq_mask;
341 }
342 }
343 #endif /* __HAVE_PIC_PENDING_INTRS */
344
345 void
346 pic_dispatch(struct intrsource *is, void *frame)
347 {
348 int (*func)(void *) = is->is_func;
349 void *arg = is->is_arg;
350
351 if (__predict_false(arg == NULL)) {
352 if (__predict_false(frame == NULL)) {
353 pic_deferral_ev.ev_count++;
354 return;
355 }
356 arg = frame;
357 }
358
359 #ifdef MULTIPROCESSOR
360 if (!is->is_mpsafe) {
361 KERNEL_LOCK(1, NULL);
362 const u_int ci_blcnt __diagused = curcpu()->ci_biglock_count;
363 const u_int l_blcnt __diagused = curlwp->l_blcnt;
364 (void)(*func)(arg);
365 KASSERT(ci_blcnt == curcpu()->ci_biglock_count);
366 KASSERT(l_blcnt == curlwp->l_blcnt);
367 KERNEL_UNLOCK_ONE(NULL);
368 } else
369 #endif
370 (void)(*func)(arg);
371
372 struct pic_percpu * const pcpu = percpu_getref(is->is_pic->pic_percpu);
373 KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
374 pcpu->pcpu_evs[is->is_irq].ev_count++;
375 percpu_putref(is->is_pic->pic_percpu);
376 }
377
378 #if defined(__HAVE_PIC_PENDING_INTRS)
379 void
380 pic_deliver_irqs(struct pic_pending *pend, struct pic_softc *pic, int ipl,
381 void *frame)
382 {
383 const uint32_t ipl_mask = __BIT(ipl);
384 struct intrsource *is;
385 volatile uint32_t *ipending = pic->pic_pending_irqs;
386 volatile uint32_t *iblocked = pic->pic_blocked_irqs;
387 size_t irq_base;
388 #if PIC_MAXSOURCES > 32
389 size_t irq_count;
390 int poi = 0; /* Possibility of interrupting */
391 #endif
392 uint32_t pending_irqs;
393 uint32_t blocked_irqs;
394 int irq;
395 bool progress __diagused = false;
396
397 KASSERT(pic->pic_pending_ipls & ipl_mask);
398
399 irq_base = 0;
400 #if PIC_MAXSOURCES > 32
401 irq_count = 0;
402 #endif
403
404 for (;;) {
405 pending_irqs = pic_find_pending_irqs_by_ipl(pic, irq_base,
406 *ipending, ipl);
407 KASSERT((pending_irqs & *ipending) == pending_irqs);
408 KASSERT((pending_irqs & ~(*ipending)) == 0);
409 if (pending_irqs == 0) {
410 #if PIC_MAXSOURCES > 32
411 irq_count += 32;
412 if (__predict_true(irq_count >= pic->pic_maxsources)) {
413 if (!poi)
414 /*Interrupt at this level was handled.*/
415 break;
416 irq_base = 0;
417 irq_count = 0;
418 poi = 0;
419 ipending = pic->pic_pending_irqs;
420 iblocked = pic->pic_blocked_irqs;
421 } else {
422 irq_base += 32;
423 ipending++;
424 iblocked++;
425 KASSERT(irq_base <= pic->pic_maxsources);
426 }
427 continue;
428 #else
429 break;
430 #endif
431 }
432 progress = true;
433 blocked_irqs = 0;
434 do {
435 irq = ffs(pending_irqs) - 1;
436 KASSERT(irq >= 0);
437
438 atomic_and_32(ipending, ~__BIT(irq));
439 is = pic->pic_sources[irq_base + irq];
440 if (is != NULL) {
441 cpsie(I32_bit);
442 pic_dispatch(is, frame);
443 cpsid(I32_bit);
444 #if PIC_MAXSOURCES > 32
445 /*
446 * There is a possibility of interrupting
447 * from cpsie() to cpsid().
448 */
449 poi = 1;
450 #endif
451 blocked_irqs |= __BIT(irq);
452 } else {
453 KASSERT(0);
454 }
455 pending_irqs = pic_find_pending_irqs_by_ipl(pic,
456 irq_base, *ipending, ipl);
457 } while (pending_irqs);
458 if (blocked_irqs) {
459 atomic_or_32(iblocked, blocked_irqs);
460 atomic_or_32(&pend->blocked_pics, __BIT(pic->pic_id));
461 }
462 }
463
464 KASSERT(progress);
465 /*
466 * Since interrupts are disabled, we don't have to be too careful
467 * about these.
468 */
469 if (atomic_and_32_nv(&pic->pic_pending_ipls, ~ipl_mask) == 0)
470 atomic_and_32(&pend->pending_pics, ~__BIT(pic->pic_id));
471 }
472
473 static void
474 pic_list_unblock_irqs(struct pic_pending *pend)
475 {
476 uint32_t blocked_pics = pend->blocked_pics;
477
478 pend->blocked_pics = 0;
479
480 for (;;) {
481 struct pic_softc *pic;
482 #if PIC_MAXSOURCES > 32
483 volatile uint32_t *iblocked;
484 uint32_t blocked;
485 size_t irq_base;
486 #endif
487
488 int pic_id = ffs(blocked_pics);
489 if (pic_id-- == 0)
490 return;
491
492 pic = pic_list[pic_id];
493 KASSERT(pic != NULL);
494 #if PIC_MAXSOURCES > 32
495 for (irq_base = 0, iblocked = pic->pic_blocked_irqs;
496 irq_base < pic->pic_maxsources;
497 irq_base += 32, iblocked++) {
498 if ((blocked = *iblocked) != 0) {
499 (*pic->pic_ops->pic_unblock_irqs)(pic,
500 irq_base, blocked);
501 atomic_and_32(iblocked, ~blocked);
502 }
503 }
504 #else
505 KASSERT(pic->pic_blocked_irqs[0] != 0);
506 (*pic->pic_ops->pic_unblock_irqs)(pic,
507 0, pic->pic_blocked_irqs[0]);
508 pic->pic_blocked_irqs[0] = 0;
509 #endif
510 blocked_pics &= ~__BIT(pic_id);
511 }
512 }
513
514 struct pic_softc *
515 pic_list_find_pic_by_pending_ipl(struct pic_pending *pend, uint32_t ipl_mask)
516 {
517 uint32_t pending_pics = pend->pending_pics;
518 struct pic_softc *pic;
519
520 for (;;) {
521 int pic_id = ffs(pending_pics);
522 if (pic_id-- == 0)
523 return NULL;
524
525 pic = pic_list[pic_id];
526 KASSERT(pic != NULL);
527 if (pic->pic_pending_ipls & ipl_mask)
528 return pic;
529 pending_pics &= ~__BIT(pic_id);
530 }
531 }
532
533 void
534 pic_list_deliver_irqs(struct pic_pending *pend, register_t psw, int ipl,
535 void *frame)
536 {
537 const uint32_t ipl_mask = __BIT(ipl);
538 struct pic_softc *pic;
539
540 while ((pic = pic_list_find_pic_by_pending_ipl(pend, ipl_mask)) != NULL) {
541 pic_deliver_irqs(pend, pic, ipl, frame);
542 KASSERT((pic->pic_pending_ipls & ipl_mask) == 0);
543 }
544 atomic_and_32(&pend->pending_ipls, ~ipl_mask);
545 }
546 #endif /* __HAVE_PIC_PENDING_INTRS */
547
548 void
549 pic_do_pending_ints(register_t psw, int newipl, void *frame)
550 {
551 struct cpu_info * const ci = curcpu();
552 if (__predict_false(newipl == IPL_HIGH)) {
553 KASSERTMSG(ci->ci_cpl == IPL_HIGH, "cpl %d", ci->ci_cpl);
554 return;
555 }
556 #if defined(__HAVE_PIC_PENDING_INTRS)
557 struct pic_pending *pend = pic_pending_get();
558 while ((pend->pending_ipls & ~__BIT(newipl)) > __BIT(newipl)) {
559 KASSERT(pend->pending_ipls < __BIT(NIPL));
560 for (;;) {
561 int ipl = 31 - __builtin_clz(pend->pending_ipls);
562 KASSERT(ipl < NIPL);
563 if (ipl <= newipl)
564 break;
565
566 pic_set_priority(ci, ipl);
567 pic_list_deliver_irqs(pend, psw, ipl, frame);
568 pic_list_unblock_irqs(pend);
569 }
570 }
571 pic_pending_put(pend);
572 #endif /* __HAVE_PIC_PENDING_INTRS */
573 #ifdef __HAVE_PREEMPTION
574 if (newipl == IPL_NONE && (ci->ci_astpending & __BIT(1))) {
575 pic_set_priority(ci, IPL_SCHED);
576 kpreempt(0);
577 }
578 #endif
579 if (ci->ci_cpl != newipl)
580 pic_set_priority(ci, newipl);
581 }
582
583 static void
584 pic_percpu_allocate(void *v0, void *v1, struct cpu_info *ci)
585 {
586 struct pic_percpu * const pcpu = v0;
587 struct pic_softc * const pic = v1;
588
589 pcpu->pcpu_evs = kmem_zalloc(pic->pic_maxsources * sizeof(pcpu->pcpu_evs[0]),
590 KM_SLEEP);
591 KASSERT(pcpu->pcpu_evs != NULL);
592
593 #define PCPU_NAMELEN 32
594 #ifdef DIAGNOSTIC
595 const size_t namelen = strlen(pic->pic_name) + 4 + strlen(ci->ci_data.cpu_name);
596 #endif
597
598 KASSERT(namelen < PCPU_NAMELEN);
599 pcpu->pcpu_name = kmem_alloc(PCPU_NAMELEN, KM_SLEEP);
600 #ifdef MULTIPROCESSOR
601 snprintf(pcpu->pcpu_name, PCPU_NAMELEN,
602 "%s (%s)", pic->pic_name, ci->ci_data.cpu_name);
603 #else
604 strlcpy(pcpu->pcpu_name, pic->pic_name, PCPU_NAMELEN);
605 #endif
606 pcpu->pcpu_magic = PICPERCPU_MAGIC;
607 #if 0
608 printf("%s: %s %s: <%s>\n",
609 __func__, ci->ci_data.cpu_name, pic->pic_name,
610 pcpu->pcpu_name);
611 #endif
612 }
613
614 static int
615 pic_init(void)
616 {
617
618 mutex_init(&pic_lock, MUTEX_DEFAULT, IPL_HIGH);
619
620 return 0;
621 }
622
623 int
624 pic_add(struct pic_softc *pic, int irqbase)
625 {
626 int slot, maybe_slot = -1;
627 size_t sourcebase;
628 static ONCE_DECL(pic_once);
629
630 RUN_ONCE(&pic_once, pic_init);
631
632 KASSERT(strlen(pic->pic_name) > 0);
633
634 #if defined(__HAVE_PIC_PENDING_INTRS) && defined(MULTIPROCESSOR)
635 if (__predict_false(pic_pending_percpu == NULL))
636 pic_pending_percpu = percpu_alloc(sizeof(struct pic_pending));
637 #endif /* __HAVE_PIC_PENDING_INTRS && MULTIPROCESSOR */
638
639 mutex_enter(&pic_lock);
640 if (irqbase == PIC_IRQBASE_ALLOC) {
641 irqbase = pic_lastbase;
642 }
643 for (slot = 0; slot < PIC_MAXPICS; slot++) {
644 struct pic_softc * const xpic = pic_list[slot];
645 if (xpic == NULL) {
646 if (maybe_slot < 0)
647 maybe_slot = slot;
648 if (irqbase < 0)
649 break;
650 continue;
651 }
652 if (irqbase < 0 || xpic->pic_irqbase < 0)
653 continue;
654 if (irqbase >= xpic->pic_irqbase + xpic->pic_maxsources)
655 continue;
656 if (irqbase + pic->pic_maxsources <= xpic->pic_irqbase)
657 continue;
658 panic("pic_add: pic %s (%zu sources @ irq %u) conflicts"
659 " with pic %s (%zu sources @ irq %u)",
660 pic->pic_name, pic->pic_maxsources, irqbase,
661 xpic->pic_name, xpic->pic_maxsources, xpic->pic_irqbase);
662 }
663 slot = maybe_slot;
664 #if 0
665 printf("%s: pic_sourcebase=%zu pic_maxsources=%zu\n",
666 pic->pic_name, pic_sourcebase, pic->pic_maxsources);
667 #endif
668 KASSERTMSG(pic->pic_maxsources <= PIC_MAXSOURCES, "%zu",
669 pic->pic_maxsources);
670 KASSERT(pic_sourcebase + pic->pic_maxsources <= PIC_MAXMAXSOURCES);
671 sourcebase = pic_sourcebase;
672 pic_sourcebase += pic->pic_maxsources;
673 if (pic_lastbase < irqbase + pic->pic_maxsources)
674 pic_lastbase = irqbase + pic->pic_maxsources;
675 mutex_exit(&pic_lock);
676
677 /*
678 * Allocate a pointer to each cpu's evcnts and then, for each cpu,
679 * allocate its evcnts and then attach an evcnt for each pin.
680 * We can't allocate the evcnt structures directly since
681 * percpu will move the contents of percpu memory around and
682 * corrupt the pointers in the evcnts themselves. Remember, any
683 * problem can be solved with sufficient indirection.
684 */
685 pic->pic_percpu = percpu_create(sizeof(struct pic_percpu),
686 pic_percpu_allocate, NULL, pic);
687
688 pic->pic_sources = &pic_sources[sourcebase];
689 pic->pic_irqbase = irqbase;
690 pic->pic_id = slot;
691 #ifdef __HAVE_PIC_SET_PRIORITY
692 KASSERT((slot == 0) == (pic->pic_ops->pic_set_priority != NULL));
693 #endif
694 #ifdef MULTIPROCESSOR
695 KASSERT((pic->pic_cpus != NULL) == (pic->pic_ops->pic_ipi_send != NULL));
696 #endif
697 pic_list[slot] = pic;
698
699 return irqbase;
700 }
701
702 int
703 pic_alloc_irq(struct pic_softc *pic)
704 {
705 int irq;
706
707 for (irq = 0; irq < pic->pic_maxsources; irq++) {
708 if (pic->pic_sources[irq] == NULL)
709 return irq;
710 }
711
712 return -1;
713 }
714
715 static void
716 pic_percpu_evcnt_attach(void *v0, void *v1, struct cpu_info *ci)
717 {
718 struct pic_percpu * const pcpu = v0;
719 struct intrsource * const is = v1;
720
721 KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
722 evcnt_attach_dynamic(&pcpu->pcpu_evs[is->is_irq], EVCNT_TYPE_INTR, NULL,
723 pcpu->pcpu_name, is->is_source);
724 }
725
726 void *
727 pic_establish_intr(struct pic_softc *pic, int irq, int ipl, int type,
728 int (*func)(void *), void *arg, const char *xname)
729 {
730 struct intrsource *is;
731 int off, nipl;
732
733 if (pic->pic_sources[irq]) {
734 printf("pic_establish_intr: pic %s irq %d already present\n",
735 pic->pic_name, irq);
736 return NULL;
737 }
738
739 is = kmem_zalloc(sizeof(*is), KM_SLEEP);
740 is->is_pic = pic;
741 is->is_irq = irq;
742 is->is_ipl = ipl;
743 is->is_type = type & 0xff;
744 is->is_func = func;
745 is->is_arg = arg;
746 #ifdef MULTIPROCESSOR
747 is->is_mpsafe = (type & IST_MPSAFE) || ipl != IPL_VM;
748 #endif
749
750 if (pic->pic_ops->pic_source_name)
751 (*pic->pic_ops->pic_source_name)(pic, irq, is->is_source,
752 sizeof(is->is_source));
753 else
754 snprintf(is->is_source, sizeof(is->is_source), "irq %d", irq);
755
756 /*
757 * Now attach the per-cpu evcnts.
758 */
759 percpu_foreach(pic->pic_percpu, pic_percpu_evcnt_attach, is);
760
761 pic->pic_sources[irq] = is;
762
763 /*
764 * First try to use an existing slot which is empty.
765 */
766 for (off = pic_ipl_offset[ipl]; off < pic_ipl_offset[ipl+1]; off++) {
767 if (pic__iplsources[off] == NULL) {
768 is->is_iplidx = off - pic_ipl_offset[ipl];
769 pic__iplsources[off] = is;
770 goto unblock;
771 }
772 }
773
774 /*
775 * Move up all the sources by one.
776 */
777 if (ipl < NIPL) {
778 off = pic_ipl_offset[ipl+1];
779 memmove(&pic__iplsources[off+1], &pic__iplsources[off],
780 sizeof(pic__iplsources[0]) * (pic_ipl_offset[NIPL] - off));
781 }
782
783 /*
784 * Advance the offset of all IPLs higher than this. Include an
785 * extra one as well. Thus the number of sources per ipl is
786 * pic_ipl_offset[ipl+1] - pic_ipl_offset[ipl].
787 */
788 for (nipl = ipl + 1; nipl <= NIPL; nipl++)
789 pic_ipl_offset[nipl]++;
790
791 /*
792 * Insert into the previously made position at the end of this IPL's
793 * sources.
794 */
795 off = pic_ipl_offset[ipl + 1] - 1;
796 is->is_iplidx = off - pic_ipl_offset[ipl];
797 pic__iplsources[off] = is;
798
799 (*pic->pic_ops->pic_establish_irq)(pic, is);
800
801 unblock:
802 (*pic->pic_ops->pic_unblock_irqs)(pic, is->is_irq & ~0x1f,
803 __BIT(is->is_irq & 0x1f));
804
805 if (xname) {
806 if (is->is_xname == NULL)
807 is->is_xname = kmem_zalloc(INTRDEVNAMEBUF, KM_SLEEP);
808 if (is->is_xname[0] != '\0')
809 strlcat(is->is_xname, ", ", INTRDEVNAMEBUF);
810 strlcat(is->is_xname, xname, INTRDEVNAMEBUF);
811 }
812
813 /* We're done. */
814 return is;
815 }
816
817 static void
818 pic_percpu_evcnt_deattach(void *v0, void *v1, struct cpu_info *ci)
819 {
820 struct pic_percpu * const pcpu = v0;
821 struct intrsource * const is = v1;
822
823 KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
824 evcnt_detach(&pcpu->pcpu_evs[is->is_irq]);
825 }
826
827 void
828 pic_disestablish_source(struct intrsource *is)
829 {
830 struct pic_softc * const pic = is->is_pic;
831 const int irq = is->is_irq;
832
833 KASSERT(is == pic->pic_sources[irq]);
834
835 (*pic->pic_ops->pic_block_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
836 pic->pic_sources[irq] = NULL;
837 pic__iplsources[pic_ipl_offset[is->is_ipl] + is->is_iplidx] = NULL;
838 if (is->is_xname != NULL) {
839 kmem_free(is->is_xname, INTRDEVNAMEBUF);
840 is->is_xname = NULL;
841 }
842 /*
843 * Now detach the per-cpu evcnts.
844 */
845 percpu_foreach(pic->pic_percpu, pic_percpu_evcnt_deattach, is);
846
847 kmem_free(is, sizeof(*is));
848 }
849
850 void *
851 intr_establish(int irq, int ipl, int type, int (*func)(void *), void *arg)
852 {
853 return intr_establish_xname(irq, ipl, type, func, arg, NULL);
854 }
855
856 void *
857 intr_establish_xname(int irq, int ipl, int type, int (*func)(void *), void *arg,
858 const char *xname)
859 {
860 KASSERT(!cpu_intr_p());
861 KASSERT(!cpu_softintr_p());
862
863 for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
864 struct pic_softc * const pic = pic_list[slot];
865 if (pic == NULL || pic->pic_irqbase < 0)
866 continue;
867 if (pic->pic_irqbase <= irq
868 && irq < pic->pic_irqbase + pic->pic_maxsources) {
869 return pic_establish_intr(pic, irq - pic->pic_irqbase,
870 ipl, type, func, arg, xname);
871 }
872 }
873
874 return NULL;
875 }
876
877 void
878 intr_disestablish(void *ih)
879 {
880 struct intrsource * const is = ih;
881
882 KASSERT(!cpu_intr_p());
883 KASSERT(!cpu_softintr_p());
884
885 pic_disestablish_source(is);
886 }
887
888 void
889 intr_mask(void *ih)
890 {
891 struct intrsource * const is = ih;
892 struct pic_softc * const pic = is->is_pic;
893 const int irq = is->is_irq;
894
895 if (atomic_inc_32_nv(&is->is_mask_count) == 1)
896 (*pic->pic_ops->pic_block_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
897 }
898
899 void
900 intr_unmask(void *ih)
901 {
902 struct intrsource * const is = ih;
903 struct pic_softc * const pic = is->is_pic;
904 const int irq = is->is_irq;
905
906 if (atomic_dec_32_nv(&is->is_mask_count) == 0)
907 (*pic->pic_ops->pic_unblock_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
908 }
909
910 const char *
911 intr_string(intr_handle_t irq, char *buf, size_t len)
912 {
913 for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
914 struct pic_softc * const pic = pic_list[slot];
915 if (pic == NULL || pic->pic_irqbase < 0)
916 continue;
917 if (pic->pic_irqbase <= irq
918 && irq < pic->pic_irqbase + pic->pic_maxsources) {
919 struct intrsource * const is = pic->pic_sources[irq - pic->pic_irqbase];
920 snprintf(buf, len, "%s %s", pic->pic_name, is->is_source);
921 return buf;
922 }
923 }
924
925 return NULL;
926 }
927
928 static struct intrsource *
929 intr_get_source(const char *intrid)
930 {
931 struct intrsource *is;
932 intrid_t buf;
933 size_t slot;
934 int irq;
935
936 KASSERT(mutex_owned(&cpu_lock));
937
938 for (slot = 0; slot < PIC_MAXPICS; slot++) {
939 struct pic_softc * const pic = pic_list[slot];
940 if (pic == NULL || pic->pic_irqbase < 0)
941 continue;
942 for (irq = 0; irq < pic->pic_maxsources; irq++) {
943 is = pic->pic_sources[irq];
944 if (is == NULL || is->is_source[0] == '\0')
945 continue;
946
947 snprintf(buf, sizeof(buf), "%s %s", pic->pic_name, is->is_source);
948 if (strcmp(buf, intrid) == 0)
949 return is;
950 }
951 }
952
953 return NULL;
954 }
955
956 struct intrids_handler *
957 interrupt_construct_intrids(const kcpuset_t *cpuset)
958 {
959 struct intrids_handler *iih;
960 struct intrsource *is;
961 int count, irq, n;
962 size_t slot;
963
964 if (kcpuset_iszero(cpuset))
965 return NULL;
966
967 count = 0;
968 for (slot = 0; slot < PIC_MAXPICS; slot++) {
969 struct pic_softc * const pic = pic_list[slot];
970 if (pic != NULL && pic->pic_irqbase >= 0) {
971 for (irq = 0; irq < pic->pic_maxsources; irq++) {
972 is = pic->pic_sources[irq];
973 if (is && is->is_source[0] != '\0')
974 count++;
975 }
976 }
977 }
978
979 iih = kmem_zalloc(sizeof(int) + sizeof(intrid_t) * count, KM_SLEEP);
980 iih->iih_nids = count;
981
982 for (n = 0, slot = 0; n < count && slot < PIC_MAXPICS; slot++) {
983 struct pic_softc * const pic = pic_list[slot];
984 if (pic == NULL || pic->pic_irqbase < 0)
985 continue;
986 for (irq = 0; irq < pic->pic_maxsources; irq++) {
987 is = pic->pic_sources[irq];
988 if (is == NULL || is->is_source[0] == '\0')
989 continue;
990
991 snprintf(iih->iih_intrids[n++], sizeof(intrid_t), "%s %s",
992 pic->pic_name, is->is_source);
993 }
994 }
995
996 return iih;
997 }
998
999 void
1000 interrupt_destruct_intrids(struct intrids_handler *iih)
1001 {
1002 if (iih == NULL)
1003 return;
1004
1005 kmem_free(iih, sizeof(int) + sizeof(intrid_t) * iih->iih_nids);
1006 }
1007
1008 void
1009 interrupt_get_available(kcpuset_t *cpuset)
1010 {
1011 CPU_INFO_ITERATOR cii;
1012 struct cpu_info *ci;
1013
1014 kcpuset_zero(cpuset);
1015
1016 mutex_enter(&cpu_lock);
1017 for (CPU_INFO_FOREACH(cii, ci)) {
1018 if ((ci->ci_schedstate.spc_flags & SPCF_NOINTR) == 0)
1019 kcpuset_set(cpuset, cpu_index(ci));
1020 }
1021 mutex_exit(&cpu_lock);
1022 }
1023
1024 void
1025 interrupt_get_devname(const char *intrid, char *buf, size_t len)
1026 {
1027 struct intrsource *is;
1028
1029 mutex_enter(&cpu_lock);
1030 is = intr_get_source(intrid);
1031 if (is == NULL || is->is_xname == NULL)
1032 buf[0] = '\0';
1033 else
1034 strlcpy(buf, is->is_xname, len);
1035 mutex_exit(&cpu_lock);
1036 }
1037
1038 struct interrupt_get_count_arg {
1039 struct intrsource *is;
1040 uint64_t count;
1041 u_int cpu_idx;
1042 };
1043
1044 static void
1045 interrupt_get_count_cb(void *v0, void *v1, struct cpu_info *ci)
1046 {
1047 struct pic_percpu * const pcpu = v0;
1048 struct interrupt_get_count_arg * const arg = v1;
1049
1050 if (arg->cpu_idx != cpu_index(ci))
1051 return;
1052
1053 arg->count = pcpu->pcpu_evs[arg->is->is_irq].ev_count;
1054 }
1055
1056 uint64_t
1057 interrupt_get_count(const char *intrid, u_int cpu_idx)
1058 {
1059 struct interrupt_get_count_arg arg;
1060 struct intrsource *is;
1061 uint64_t count;
1062
1063 count = 0;
1064
1065 mutex_enter(&cpu_lock);
1066 is = intr_get_source(intrid);
1067 if (is != NULL && is->is_pic != NULL) {
1068 arg.is = is;
1069 arg.count = 0;
1070 arg.cpu_idx = cpu_idx;
1071 percpu_foreach(is->is_pic->pic_percpu, interrupt_get_count_cb, &arg);
1072 count = arg.count;
1073 }
1074 mutex_exit(&cpu_lock);
1075
1076 return count;
1077 }
1078
1079 #ifdef MULTIPROCESSOR
1080 void
1081 interrupt_get_assigned(const char *intrid, kcpuset_t *cpuset)
1082 {
1083 struct intrsource *is;
1084 struct pic_softc *pic;
1085
1086 kcpuset_zero(cpuset);
1087
1088 mutex_enter(&cpu_lock);
1089 is = intr_get_source(intrid);
1090 if (is != NULL) {
1091 pic = is->is_pic;
1092 if (pic && pic->pic_ops->pic_get_affinity)
1093 pic->pic_ops->pic_get_affinity(pic, is->is_irq, cpuset);
1094 }
1095 mutex_exit(&cpu_lock);
1096 }
1097
1098 int
1099 interrupt_distribute_handler(const char *intrid, const kcpuset_t *newset,
1100 kcpuset_t *oldset)
1101 {
1102 struct intrsource *is;
1103 int error;
1104
1105 mutex_enter(&cpu_lock);
1106 is = intr_get_source(intrid);
1107 if (is == NULL) {
1108 error = ENOENT;
1109 } else {
1110 error = interrupt_distribute(is, newset, oldset);
1111 }
1112 mutex_exit(&cpu_lock);
1113
1114 return error;
1115 }
1116
1117 int
1118 interrupt_distribute(void *ih, const kcpuset_t *newset, kcpuset_t *oldset)
1119 {
1120 struct intrsource * const is = ih;
1121 struct pic_softc * const pic = is->is_pic;
1122
1123 if (pic == NULL)
1124 return EOPNOTSUPP;
1125 if (pic->pic_ops->pic_set_affinity == NULL ||
1126 pic->pic_ops->pic_get_affinity == NULL)
1127 return EOPNOTSUPP;
1128
1129 if (!is->is_mpsafe)
1130 return EINVAL;
1131
1132 if (oldset != NULL)
1133 pic->pic_ops->pic_get_affinity(pic, is->is_irq, oldset);
1134
1135 return pic->pic_ops->pic_set_affinity(pic, is->is_irq, newset);
1136 }
1137 #endif
1138