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