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