pic.c revision 1.58 1 /* $NetBSD: pic.c,v 1.58 2020/10/25 08:29:30 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.58 2020/10/25 08:29:30 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 // If we were targeting a single CPU or this pic
212 // handles all cpus, we're done.
213 if (kcp != NULL || pic->pic_cpus == kcpuset_running)
214 return;
215 sent_p = true;
216 }
217 }
218 KASSERTMSG(cold || sent_p || ncpu <= 1, "cold %d sent_p %d ncpu %d",
219 cold, sent_p, ncpu);
220 }
221 #endif /* MULTIPROCESSOR */
222
223 #ifdef __HAVE_PIC_FAST_SOFTINTS
224 int
225 pic_handle_softint(void *arg)
226 {
227 void softint_switch(lwp_t *, int);
228 struct cpu_info * const ci = curcpu();
229 const size_t softint = (size_t) arg;
230 int s = splhigh();
231 ci->ci_intr_depth--; // don't count these as interrupts
232 softint_switch(ci->ci_softlwps[softint], s);
233 ci->ci_intr_depth++;
234 splx(s);
235 return 1;
236 }
237 #endif
238
239 int
240 pic_handle_intr(void *arg)
241 {
242 struct pic_softc * const pic = arg;
243 int rv;
244
245 rv = (*pic->pic_ops->pic_find_pending_irqs)(pic);
246
247 return rv > 0;
248 }
249
250 #if defined(__HAVE_PIC_PENDING_INTRS)
251 void
252 pic_mark_pending_source(struct pic_softc *pic, struct intrsource *is)
253 {
254 const uint32_t ipl_mask = __BIT(is->is_ipl);
255
256 atomic_or_32(&pic->pic_pending_irqs[is->is_irq >> 5],
257 __BIT(is->is_irq & 0x1f));
258
259 atomic_or_32(&pic->pic_pending_ipls, ipl_mask);
260 struct pic_pending *pend = pic_pending_get();
261 atomic_or_32(&pend->pending_ipls, ipl_mask);
262 atomic_or_32(&pend->pending_pics, __BIT(pic->pic_id));
263 pic_pending_put(pend);
264 }
265
266 void
267 pic_mark_pending(struct pic_softc *pic, int irq)
268 {
269 struct intrsource * const is = pic->pic_sources[irq];
270
271 KASSERT(irq < pic->pic_maxsources);
272 KASSERT(is != NULL);
273
274 pic_mark_pending_source(pic, is);
275 }
276
277 uint32_t
278 pic_mark_pending_sources(struct pic_softc *pic, size_t irq_base,
279 uint32_t pending)
280 {
281 struct intrsource ** const isbase = &pic->pic_sources[irq_base];
282 struct intrsource *is;
283 volatile uint32_t *ipending = &pic->pic_pending_irqs[irq_base >> 5];
284 uint32_t ipl_mask = 0;
285
286 if (pending == 0)
287 return ipl_mask;
288
289 KASSERT((irq_base & 31) == 0);
290
291 (*pic->pic_ops->pic_block_irqs)(pic, irq_base, pending);
292
293 atomic_or_32(ipending, pending);
294 while (pending != 0) {
295 int n = ffs(pending);
296 if (n-- == 0)
297 break;
298 is = isbase[n];
299 KASSERT(is != NULL);
300 KASSERT(irq_base <= is->is_irq && is->is_irq < irq_base + 32);
301 pending &= ~__BIT(n);
302 ipl_mask |= __BIT(is->is_ipl);
303 }
304
305 atomic_or_32(&pic->pic_pending_ipls, ipl_mask);
306 struct pic_pending *pend = pic_pending_get();
307 atomic_or_32(&pend->pending_ipls, ipl_mask);
308 atomic_or_32(&pend->pending_pics, __BIT(pic->pic_id));
309 pic_pending_put(pend);
310 return ipl_mask;
311 }
312
313 uint32_t
314 pic_find_pending_irqs_by_ipl(struct pic_softc *pic, size_t irq_base,
315 uint32_t pending, int ipl)
316 {
317 uint32_t ipl_irq_mask = 0;
318 uint32_t irq_mask;
319
320 for (;;) {
321 int irq = ffs(pending);
322 if (irq-- == 0)
323 return ipl_irq_mask;
324
325 irq_mask = __BIT(irq);
326 #if 1
327 KASSERTMSG(pic->pic_sources[irq_base + irq] != NULL,
328 "%s: irq_base %zu irq %d\n", __func__, irq_base, irq);
329 #else
330 if (pic->pic_sources[irq_base + irq] == NULL) {
331 aprint_error("stray interrupt? irq_base=%zu irq=%d\n",
332 irq_base, irq);
333 } else
334 #endif
335 if (pic->pic_sources[irq_base + irq]->is_ipl == ipl)
336 ipl_irq_mask |= irq_mask;
337
338 pending &= ~irq_mask;
339 }
340 }
341 #endif /* __HAVE_PIC_PENDING_INTRS */
342
343 void
344 pic_dispatch(struct intrsource *is, void *frame)
345 {
346 int (*func)(void *) = is->is_func;
347 void *arg = is->is_arg;
348
349 if (__predict_false(arg == NULL)) {
350 if (__predict_false(frame == NULL)) {
351 pic_deferral_ev.ev_count++;
352 return;
353 }
354 arg = frame;
355 }
356
357 #ifdef MULTIPROCESSOR
358 if (!is->is_mpsafe) {
359 KERNEL_LOCK(1, NULL);
360 const u_int ci_blcnt __diagused = curcpu()->ci_biglock_count;
361 const u_int l_blcnt __diagused = curlwp->l_blcnt;
362 (void)(*func)(arg);
363 KASSERT(ci_blcnt == curcpu()->ci_biglock_count);
364 KASSERT(l_blcnt == curlwp->l_blcnt);
365 KERNEL_UNLOCK_ONE(NULL);
366 } else
367 #endif
368 (void)(*func)(arg);
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 struct pic_softc *
513 pic_list_find_pic_by_pending_ipl(struct pic_pending *pend, uint32_t ipl_mask)
514 {
515 uint32_t pending_pics = pend->pending_pics;
516 struct pic_softc *pic;
517
518 for (;;) {
519 int pic_id = ffs(pending_pics);
520 if (pic_id-- == 0)
521 return NULL;
522
523 pic = pic_list[pic_id];
524 KASSERT(pic != NULL);
525 if (pic->pic_pending_ipls & ipl_mask)
526 return pic;
527 pending_pics &= ~__BIT(pic_id);
528 }
529 }
530
531 void
532 pic_list_deliver_irqs(struct pic_pending *pend, register_t psw, int ipl,
533 void *frame)
534 {
535 const uint32_t ipl_mask = __BIT(ipl);
536 struct pic_softc *pic;
537
538 while ((pic = pic_list_find_pic_by_pending_ipl(pend, ipl_mask)) != NULL) {
539 pic_deliver_irqs(pend, pic, ipl, frame);
540 KASSERT((pic->pic_pending_ipls & ipl_mask) == 0);
541 }
542 atomic_and_32(&pend->pending_ipls, ~ipl_mask);
543 }
544 #endif /* __HAVE_PIC_PENDING_INTRS */
545
546 void
547 pic_do_pending_ints(register_t psw, int newipl, void *frame)
548 {
549 struct cpu_info * const ci = curcpu();
550 if (__predict_false(newipl == IPL_HIGH)) {
551 KASSERTMSG(ci->ci_cpl == IPL_HIGH, "cpl %d", ci->ci_cpl);
552 return;
553 }
554 #if defined(__HAVE_PIC_PENDING_INTRS)
555 struct pic_pending *pend = pic_pending_get();
556 while ((pend->pending_ipls & ~__BIT(newipl)) > __BIT(newipl)) {
557 KASSERT(pend->pending_ipls < __BIT(NIPL));
558 for (;;) {
559 int ipl = 31 - __builtin_clz(pend->pending_ipls);
560 KASSERT(ipl < NIPL);
561 if (ipl <= newipl)
562 break;
563
564 pic_set_priority(ci, ipl);
565 pic_list_deliver_irqs(pend, psw, ipl, frame);
566 pic_list_unblock_irqs(pend);
567 }
568 }
569 pic_pending_put(pend);
570 #endif /* __HAVE_PIC_PENDING_INTRS */
571 #ifdef __HAVE_PREEMPTION
572 if (newipl == IPL_NONE && (ci->ci_astpending & __BIT(1))) {
573 pic_set_priority(ci, IPL_SCHED);
574 kpreempt(0);
575 }
576 #endif
577 if (ci->ci_cpl != newipl)
578 pic_set_priority(ci, newipl);
579 }
580
581 static void
582 pic_percpu_allocate(void *v0, void *v1, struct cpu_info *ci)
583 {
584 struct pic_percpu * const pcpu = v0;
585 struct pic_softc * const pic = v1;
586
587 pcpu->pcpu_evs = kmem_zalloc(pic->pic_maxsources * sizeof(pcpu->pcpu_evs[0]),
588 KM_SLEEP);
589 KASSERT(pcpu->pcpu_evs != NULL);
590
591 #define PCPU_NAMELEN 32
592 #ifdef DIAGNOSTIC
593 const size_t namelen = strlen(pic->pic_name) + 4 + strlen(ci->ci_data.cpu_name);
594 #endif
595
596 KASSERT(namelen < PCPU_NAMELEN);
597 pcpu->pcpu_name = kmem_alloc(PCPU_NAMELEN, KM_SLEEP);
598 #ifdef MULTIPROCESSOR
599 snprintf(pcpu->pcpu_name, PCPU_NAMELEN,
600 "%s (%s)", pic->pic_name, ci->ci_data.cpu_name);
601 #else
602 strlcpy(pcpu->pcpu_name, pic->pic_name, PCPU_NAMELEN);
603 #endif
604 pcpu->pcpu_magic = PICPERCPU_MAGIC;
605 #if 0
606 printf("%s: %s %s: <%s>\n",
607 __func__, ci->ci_data.cpu_name, pic->pic_name,
608 pcpu->pcpu_name);
609 #endif
610 }
611
612 static int
613 pic_init(void)
614 {
615
616 mutex_init(&pic_lock, MUTEX_DEFAULT, IPL_HIGH);
617
618 return 0;
619 }
620
621 int
622 pic_add(struct pic_softc *pic, int irqbase)
623 {
624 int slot, maybe_slot = -1;
625 size_t sourcebase;
626 static ONCE_DECL(pic_once);
627
628 RUN_ONCE(&pic_once, pic_init);
629
630 KASSERT(strlen(pic->pic_name) > 0);
631
632 #if defined(__HAVE_PIC_PENDING_INTRS) && defined(MULTIPROCESSOR)
633 if (__predict_false(pic_pending_percpu == NULL))
634 pic_pending_percpu = percpu_alloc(sizeof(struct pic_pending));
635 #endif /* __HAVE_PIC_PENDING_INTRS && MULTIPROCESSOR */
636
637 mutex_enter(&pic_lock);
638 if (irqbase == PIC_IRQBASE_ALLOC) {
639 irqbase = pic_lastbase;
640 }
641 for (slot = 0; slot < PIC_MAXPICS; slot++) {
642 struct pic_softc * const xpic = pic_list[slot];
643 if (xpic == NULL) {
644 if (maybe_slot < 0)
645 maybe_slot = slot;
646 if (irqbase < 0)
647 break;
648 continue;
649 }
650 if (irqbase < 0 || xpic->pic_irqbase < 0)
651 continue;
652 if (irqbase >= xpic->pic_irqbase + xpic->pic_maxsources)
653 continue;
654 if (irqbase + pic->pic_maxsources <= xpic->pic_irqbase)
655 continue;
656 panic("pic_add: pic %s (%zu sources @ irq %u) conflicts"
657 " with pic %s (%zu sources @ irq %u)",
658 pic->pic_name, pic->pic_maxsources, irqbase,
659 xpic->pic_name, xpic->pic_maxsources, xpic->pic_irqbase);
660 }
661 slot = maybe_slot;
662 #if 0
663 printf("%s: pic_sourcebase=%zu pic_maxsources=%zu\n",
664 pic->pic_name, pic_sourcebase, pic->pic_maxsources);
665 #endif
666 KASSERTMSG(pic->pic_maxsources <= PIC_MAXSOURCES, "%zu",
667 pic->pic_maxsources);
668 KASSERT(pic_sourcebase + pic->pic_maxsources <= PIC_MAXMAXSOURCES);
669 sourcebase = pic_sourcebase;
670 pic_sourcebase += pic->pic_maxsources;
671 if (pic_lastbase < irqbase + pic->pic_maxsources)
672 pic_lastbase = irqbase + pic->pic_maxsources;
673 mutex_exit(&pic_lock);
674
675 /*
676 * Allocate a pointer to each cpu's evcnts and then, for each cpu,
677 * allocate its evcnts and then attach an evcnt for each pin.
678 * We can't allocate the evcnt structures directly since
679 * percpu will move the contents of percpu memory around and
680 * corrupt the pointers in the evcnts themselves. Remember, any
681 * problem can be solved with sufficient indirection.
682 */
683 pic->pic_percpu = percpu_create(sizeof(struct pic_percpu),
684 pic_percpu_allocate, NULL, pic);
685
686 pic->pic_sources = &pic_sources[sourcebase];
687 pic->pic_irqbase = irqbase;
688 pic->pic_id = slot;
689 #ifdef __HAVE_PIC_SET_PRIORITY
690 KASSERT((slot == 0) == (pic->pic_ops->pic_set_priority != NULL));
691 #endif
692 #ifdef MULTIPROCESSOR
693 KASSERT((pic->pic_cpus != NULL) == (pic->pic_ops->pic_ipi_send != NULL));
694 #endif
695 pic_list[slot] = pic;
696
697 return irqbase;
698 }
699
700 int
701 pic_alloc_irq(struct pic_softc *pic)
702 {
703 int irq;
704
705 for (irq = 0; irq < pic->pic_maxsources; irq++) {
706 if (pic->pic_sources[irq] == NULL)
707 return irq;
708 }
709
710 return -1;
711 }
712
713 static void
714 pic_percpu_evcnt_attach(void *v0, void *v1, struct cpu_info *ci)
715 {
716 struct pic_percpu * const pcpu = v0;
717 struct intrsource * const is = v1;
718
719 KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
720 evcnt_attach_dynamic(&pcpu->pcpu_evs[is->is_irq], EVCNT_TYPE_INTR, NULL,
721 pcpu->pcpu_name, is->is_source);
722 }
723
724 void *
725 pic_establish_intr(struct pic_softc *pic, int irq, int ipl, int type,
726 int (*func)(void *), void *arg, const char *xname)
727 {
728 struct intrsource *is;
729 int off, nipl;
730
731 if (pic->pic_sources[irq]) {
732 printf("pic_establish_intr: pic %s irq %d already present\n",
733 pic->pic_name, irq);
734 return NULL;
735 }
736
737 is = kmem_zalloc(sizeof(*is), KM_SLEEP);
738 is->is_pic = pic;
739 is->is_irq = irq;
740 is->is_ipl = ipl;
741 is->is_type = type & 0xff;
742 is->is_func = func;
743 is->is_arg = arg;
744 #ifdef MULTIPROCESSOR
745 is->is_mpsafe = (type & IST_MPSAFE) || ipl != IPL_VM;
746 #endif
747
748 if (pic->pic_ops->pic_source_name)
749 (*pic->pic_ops->pic_source_name)(pic, irq, is->is_source,
750 sizeof(is->is_source));
751 else
752 snprintf(is->is_source, sizeof(is->is_source), "irq %d", irq);
753
754 /*
755 * Now attach the per-cpu evcnts.
756 */
757 percpu_foreach(pic->pic_percpu, pic_percpu_evcnt_attach, is);
758
759 pic->pic_sources[irq] = is;
760
761 /*
762 * First try to use an existing slot which is empty.
763 */
764 for (off = pic_ipl_offset[ipl]; off < pic_ipl_offset[ipl+1]; off++) {
765 if (pic__iplsources[off] == NULL) {
766 is->is_iplidx = off - pic_ipl_offset[ipl];
767 pic__iplsources[off] = is;
768 goto unblock;
769 }
770 }
771
772 /*
773 * Move up all the sources by one.
774 */
775 if (ipl < NIPL) {
776 off = pic_ipl_offset[ipl+1];
777 memmove(&pic__iplsources[off+1], &pic__iplsources[off],
778 sizeof(pic__iplsources[0]) * (pic_ipl_offset[NIPL] - off));
779 }
780
781 /*
782 * Advance the offset of all IPLs higher than this. Include an
783 * extra one as well. Thus the number of sources per ipl is
784 * pic_ipl_offset[ipl+1] - pic_ipl_offset[ipl].
785 */
786 for (nipl = ipl + 1; nipl <= NIPL; nipl++)
787 pic_ipl_offset[nipl]++;
788
789 /*
790 * Insert into the previously made position at the end of this IPL's
791 * sources.
792 */
793 off = pic_ipl_offset[ipl + 1] - 1;
794 is->is_iplidx = off - pic_ipl_offset[ipl];
795 pic__iplsources[off] = is;
796
797 (*pic->pic_ops->pic_establish_irq)(pic, is);
798
799 unblock:
800 (*pic->pic_ops->pic_unblock_irqs)(pic, is->is_irq & ~0x1f,
801 __BIT(is->is_irq & 0x1f));
802
803 if (xname) {
804 if (is->is_xname == NULL)
805 is->is_xname = kmem_zalloc(INTRDEVNAMEBUF, KM_SLEEP);
806 if (is->is_xname[0] != '\0')
807 strlcat(is->is_xname, ", ", INTRDEVNAMEBUF);
808 strlcat(is->is_xname, xname, INTRDEVNAMEBUF);
809 }
810
811 /* We're done. */
812 return is;
813 }
814
815 static void
816 pic_percpu_evcnt_deattach(void *v0, void *v1, struct cpu_info *ci)
817 {
818 struct pic_percpu * const pcpu = v0;
819 struct intrsource * const is = v1;
820
821 KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
822 evcnt_detach(&pcpu->pcpu_evs[is->is_irq]);
823 }
824
825 void
826 pic_disestablish_source(struct intrsource *is)
827 {
828 struct pic_softc * const pic = is->is_pic;
829 const int irq = is->is_irq;
830
831 KASSERT(is == pic->pic_sources[irq]);
832
833 (*pic->pic_ops->pic_block_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
834 pic->pic_sources[irq] = NULL;
835 pic__iplsources[pic_ipl_offset[is->is_ipl] + is->is_iplidx] = NULL;
836 if (is->is_xname != NULL) {
837 kmem_free(is->is_xname, INTRDEVNAMEBUF);
838 is->is_xname = NULL;
839 }
840 /*
841 * Now detach the per-cpu evcnts.
842 */
843 percpu_foreach(pic->pic_percpu, pic_percpu_evcnt_deattach, is);
844
845 kmem_free(is, sizeof(*is));
846 }
847
848 void *
849 intr_establish(int irq, int ipl, int type, int (*func)(void *), void *arg)
850 {
851 return intr_establish_xname(irq, ipl, type, func, arg, NULL);
852 }
853
854 void *
855 intr_establish_xname(int irq, int ipl, int type, int (*func)(void *), void *arg,
856 const char *xname)
857 {
858 KASSERT(!cpu_intr_p());
859 KASSERT(!cpu_softintr_p());
860
861 for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
862 struct pic_softc * const pic = pic_list[slot];
863 if (pic == NULL || pic->pic_irqbase < 0)
864 continue;
865 if (pic->pic_irqbase <= irq
866 && irq < pic->pic_irqbase + pic->pic_maxsources) {
867 return pic_establish_intr(pic, irq - pic->pic_irqbase,
868 ipl, type, func, arg, xname);
869 }
870 }
871
872 return NULL;
873 }
874
875 void
876 intr_disestablish(void *ih)
877 {
878 struct intrsource * const is = ih;
879
880 KASSERT(!cpu_intr_p());
881 KASSERT(!cpu_softintr_p());
882
883 pic_disestablish_source(is);
884 }
885
886 void
887 intr_mask(void *ih)
888 {
889 struct intrsource * const is = ih;
890 struct pic_softc * const pic = is->is_pic;
891 const int irq = is->is_irq;
892
893 if (atomic_inc_32_nv(&is->is_mask_count) == 1)
894 (*pic->pic_ops->pic_block_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
895 }
896
897 void
898 intr_unmask(void *ih)
899 {
900 struct intrsource * const is = ih;
901 struct pic_softc * const pic = is->is_pic;
902 const int irq = is->is_irq;
903
904 if (atomic_dec_32_nv(&is->is_mask_count) == 0)
905 (*pic->pic_ops->pic_unblock_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
906 }
907
908 const char *
909 intr_string(intr_handle_t irq, char *buf, size_t len)
910 {
911 for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
912 struct pic_softc * const pic = pic_list[slot];
913 if (pic == NULL || pic->pic_irqbase < 0)
914 continue;
915 if (pic->pic_irqbase <= irq
916 && irq < pic->pic_irqbase + pic->pic_maxsources) {
917 struct intrsource * const is = pic->pic_sources[irq - pic->pic_irqbase];
918 snprintf(buf, len, "%s %s", pic->pic_name, is->is_source);
919 return buf;
920 }
921 }
922
923 return NULL;
924 }
925
926 static struct intrsource *
927 intr_get_source(const char *intrid)
928 {
929 struct intrsource *is;
930 intrid_t buf;
931 size_t slot;
932 int irq;
933
934 KASSERT(mutex_owned(&cpu_lock));
935
936 for (slot = 0; slot < PIC_MAXPICS; slot++) {
937 struct pic_softc * const pic = pic_list[slot];
938 if (pic == NULL || pic->pic_irqbase < 0)
939 continue;
940 for (irq = 0; irq < pic->pic_maxsources; irq++) {
941 is = pic->pic_sources[irq];
942 if (is == NULL || is->is_source[0] == '\0')
943 continue;
944
945 snprintf(buf, sizeof(buf), "%s %s", pic->pic_name, is->is_source);
946 if (strcmp(buf, intrid) == 0)
947 return is;
948 }
949 }
950
951 return NULL;
952 }
953
954 struct intrids_handler *
955 interrupt_construct_intrids(const kcpuset_t *cpuset)
956 {
957 struct intrids_handler *iih;
958 struct intrsource *is;
959 int count, irq, n;
960 size_t slot;
961
962 if (kcpuset_iszero(cpuset))
963 return NULL;
964
965 count = 0;
966 for (slot = 0; slot < PIC_MAXPICS; slot++) {
967 struct pic_softc * const pic = pic_list[slot];
968 if (pic != NULL && pic->pic_irqbase >= 0) {
969 for (irq = 0; irq < pic->pic_maxsources; irq++) {
970 is = pic->pic_sources[irq];
971 if (is && is->is_source[0] != '\0')
972 count++;
973 }
974 }
975 }
976
977 iih = kmem_zalloc(sizeof(int) + sizeof(intrid_t) * count, KM_SLEEP);
978 iih->iih_nids = count;
979
980 for (n = 0, slot = 0; n < count && slot < PIC_MAXPICS; slot++) {
981 struct pic_softc * const pic = pic_list[slot];
982 if (pic == NULL || pic->pic_irqbase < 0)
983 continue;
984 for (irq = 0; irq < pic->pic_maxsources; irq++) {
985 is = pic->pic_sources[irq];
986 if (is == NULL || is->is_source[0] == '\0')
987 continue;
988
989 snprintf(iih->iih_intrids[n++], sizeof(intrid_t), "%s %s",
990 pic->pic_name, is->is_source);
991 }
992 }
993
994 return iih;
995 }
996
997 void
998 interrupt_destruct_intrids(struct intrids_handler *iih)
999 {
1000 if (iih == NULL)
1001 return;
1002
1003 kmem_free(iih, sizeof(int) + sizeof(intrid_t) * iih->iih_nids);
1004 }
1005
1006 void
1007 interrupt_get_available(kcpuset_t *cpuset)
1008 {
1009 CPU_INFO_ITERATOR cii;
1010 struct cpu_info *ci;
1011
1012 kcpuset_zero(cpuset);
1013
1014 mutex_enter(&cpu_lock);
1015 for (CPU_INFO_FOREACH(cii, ci)) {
1016 if ((ci->ci_schedstate.spc_flags & SPCF_NOINTR) == 0)
1017 kcpuset_set(cpuset, cpu_index(ci));
1018 }
1019 mutex_exit(&cpu_lock);
1020 }
1021
1022 void
1023 interrupt_get_devname(const char *intrid, char *buf, size_t len)
1024 {
1025 struct intrsource *is;
1026
1027 mutex_enter(&cpu_lock);
1028 is = intr_get_source(intrid);
1029 if (is == NULL || is->is_xname == NULL)
1030 buf[0] = '\0';
1031 else
1032 strlcpy(buf, is->is_xname, len);
1033 mutex_exit(&cpu_lock);
1034 }
1035
1036 struct interrupt_get_count_arg {
1037 struct intrsource *is;
1038 uint64_t count;
1039 u_int cpu_idx;
1040 };
1041
1042 static void
1043 interrupt_get_count_cb(void *v0, void *v1, struct cpu_info *ci)
1044 {
1045 struct pic_percpu * const pcpu = v0;
1046 struct interrupt_get_count_arg * const arg = v1;
1047
1048 if (arg->cpu_idx != cpu_index(ci))
1049 return;
1050
1051 arg->count = pcpu->pcpu_evs[arg->is->is_irq].ev_count;
1052 }
1053
1054 uint64_t
1055 interrupt_get_count(const char *intrid, u_int cpu_idx)
1056 {
1057 struct interrupt_get_count_arg arg;
1058 struct intrsource *is;
1059 uint64_t count;
1060
1061 count = 0;
1062
1063 mutex_enter(&cpu_lock);
1064 is = intr_get_source(intrid);
1065 if (is != NULL && is->is_pic != NULL) {
1066 arg.is = is;
1067 arg.count = 0;
1068 arg.cpu_idx = cpu_idx;
1069 percpu_foreach(is->is_pic->pic_percpu, interrupt_get_count_cb, &arg);
1070 count = arg.count;
1071 }
1072 mutex_exit(&cpu_lock);
1073
1074 return count;
1075 }
1076
1077 #ifdef MULTIPROCESSOR
1078 void
1079 interrupt_get_assigned(const char *intrid, kcpuset_t *cpuset)
1080 {
1081 struct intrsource *is;
1082 struct pic_softc *pic;
1083
1084 kcpuset_zero(cpuset);
1085
1086 mutex_enter(&cpu_lock);
1087 is = intr_get_source(intrid);
1088 if (is != NULL) {
1089 pic = is->is_pic;
1090 if (pic && pic->pic_ops->pic_get_affinity)
1091 pic->pic_ops->pic_get_affinity(pic, is->is_irq, cpuset);
1092 }
1093 mutex_exit(&cpu_lock);
1094 }
1095
1096 int
1097 interrupt_distribute_handler(const char *intrid, const kcpuset_t *newset,
1098 kcpuset_t *oldset)
1099 {
1100 struct intrsource *is;
1101 int error;
1102
1103 mutex_enter(&cpu_lock);
1104 is = intr_get_source(intrid);
1105 if (is == NULL) {
1106 error = ENOENT;
1107 } else {
1108 error = interrupt_distribute(is, newset, oldset);
1109 }
1110 mutex_exit(&cpu_lock);
1111
1112 return error;
1113 }
1114
1115 int
1116 interrupt_distribute(void *ih, const kcpuset_t *newset, kcpuset_t *oldset)
1117 {
1118 struct intrsource * const is = ih;
1119 struct pic_softc * const pic = is->is_pic;
1120
1121 if (pic == NULL)
1122 return EOPNOTSUPP;
1123 if (pic->pic_ops->pic_set_affinity == NULL ||
1124 pic->pic_ops->pic_get_affinity == NULL)
1125 return EOPNOTSUPP;
1126
1127 if (!is->is_mpsafe)
1128 return EINVAL;
1129
1130 if (oldset != NULL)
1131 pic->pic_ops->pic_get_affinity(pic, is->is_irq, oldset);
1132
1133 return pic->pic_ops->pic_set_affinity(pic, is->is_irq, newset);
1134 }
1135 #endif
1136