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