gic.c revision 1.45 1 /* $NetBSD: gic.c,v 1.45 2021/02/21 15:45:30 jmcneill Exp $ */
2 /*-
3 * Copyright (c) 2012 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 of 3am Software Foundry.
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 #include "opt_ddb.h"
32 #include "opt_multiprocessor.h"
33
34 #define _INTR_PRIVATE
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: gic.c,v 1.45 2021/02/21 15:45:30 jmcneill Exp $");
38
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/cpu.h>
42 #include <sys/device.h>
43 #include <sys/evcnt.h>
44 #include <sys/intr.h>
45 #include <sys/proc.h>
46 #include <sys/atomic.h>
47
48 #include <arm/armreg.h>
49 #include <arm/cpufunc.h>
50 #include <arm/locore.h>
51
52 #include <arm/cortex/gic_reg.h>
53 #include <arm/cortex/mpcore_var.h>
54
55 void armgic_irq_handler(void *);
56
57 #define ARMGIC_SGI_IPIBASE 0
58
59 /*
60 * SGIs 8-16 are reserved for use by ARM Trusted Firmware.
61 */
62 __CTASSERT(ARMGIC_SGI_IPIBASE + NIPI <= 8);
63
64 static int armgic_match(device_t, cfdata_t, void *);
65 static void armgic_attach(device_t, device_t, void *);
66
67 static void armgic_set_priority(struct pic_softc *, int);
68 static void armgic_unblock_irqs(struct pic_softc *, size_t, uint32_t);
69 static void armgic_block_irqs(struct pic_softc *, size_t, uint32_t);
70 static void armgic_establish_irq(struct pic_softc *, struct intrsource *);
71 #if 0
72 static void armgic_source_name(struct pic_softc *, int, char *, size_t);
73 #endif
74
75 #ifdef MULTIPROCESSOR
76 static void armgic_cpu_init(struct pic_softc *, struct cpu_info *);
77 static void armgic_ipi_send(struct pic_softc *, const kcpuset_t *, u_long);
78 static void armgic_get_affinity(struct pic_softc *, size_t, kcpuset_t *);
79 static int armgic_set_affinity(struct pic_softc *, size_t, const kcpuset_t *);
80 #endif
81
82 static const struct pic_ops armgic_picops = {
83 .pic_unblock_irqs = armgic_unblock_irqs,
84 .pic_block_irqs = armgic_block_irqs,
85 .pic_establish_irq = armgic_establish_irq,
86 #if 0
87 .pic_source_name = armgic_source_name,
88 #endif
89 .pic_set_priority = armgic_set_priority,
90 #ifdef MULTIPROCESSOR
91 .pic_cpu_init = armgic_cpu_init,
92 .pic_ipi_send = armgic_ipi_send,
93 .pic_get_affinity = armgic_get_affinity,
94 .pic_set_affinity = armgic_set_affinity,
95 #endif
96 };
97
98 #define PICTOSOFTC(pic) ((struct armgic_softc *)(pic))
99
100 static struct armgic_softc {
101 struct pic_softc sc_pic;
102 device_t sc_dev;
103 bus_space_tag_t sc_memt;
104 bus_space_handle_t sc_gicch;
105 bus_space_handle_t sc_gicdh;
106 size_t sc_gic_lines;
107 uint32_t sc_gic_type;
108 uint32_t sc_gic_valid_lines[1024/32];
109 uint32_t sc_enabled_local;
110 #ifdef MULTIPROCESSOR
111 uint32_t sc_target[MAXCPUS];
112 uint32_t sc_mptargets;
113 #endif
114 uint32_t sc_bptargets;
115 } armgic_softc = {
116 .sc_pic = {
117 .pic_ops = &armgic_picops,
118 .pic_name = "armgic",
119 },
120 };
121
122 static struct intrsource armgic_dummy_source;
123
124 __CTASSERT(NIPL == 8);
125
126 /*
127 * GIC register are always in little-endian. It is assumed the bus_space
128 * will do any endian conversion required.
129 */
130 static inline uint32_t
131 gicc_read(struct armgic_softc *sc, bus_size_t o)
132 {
133 return bus_space_read_4(sc->sc_memt, sc->sc_gicch, o);
134 }
135
136 static inline void
137 gicc_write(struct armgic_softc *sc, bus_size_t o, uint32_t v)
138 {
139 bus_space_write_4(sc->sc_memt, sc->sc_gicch, o, v);
140 }
141
142 static inline uint32_t
143 gicd_read(struct armgic_softc *sc, bus_size_t o)
144 {
145 return bus_space_read_4(sc->sc_memt, sc->sc_gicdh, o);
146 }
147
148 static inline void
149 gicd_write(struct armgic_softc *sc, bus_size_t o, uint32_t v)
150 {
151 bus_space_write_4(sc->sc_memt, sc->sc_gicdh, o, v);
152 }
153
154 static uint32_t
155 gicd_find_targets(struct armgic_softc *sc)
156 {
157 uint32_t targets = 0;
158
159 /*
160 * GICD_ITARGETSR0 through 7 are read-only, and each field returns
161 * a value that corresponds only to the processor reading the
162 * register. Use this to determine the current processor's
163 * CPU interface number.
164 */
165 for (int i = 0; i < 8; i++) {
166 targets = gicd_read(sc, GICD_ITARGETSRn(i));
167 if (targets != 0)
168 break;
169 }
170 targets |= (targets >> 16);
171 targets |= (targets >> 8);
172 targets &= 0xff;
173
174 return targets ? targets : 1;
175 }
176
177 /*
178 * In the GIC prioritization scheme, lower numbers have higher priority.
179 * Only write priorities that could be non-secure.
180 */
181 static inline uint32_t
182 armgic_ipl_to_priority(int ipl)
183 {
184 return GICC_PMR_NONSECURE
185 | ((IPL_HIGH - ipl) * GICC_PMR_NS_PRIORITIES / NIPL);
186 }
187
188 #if 0
189 static inline int
190 armgic_priority_to_ipl(uint32_t priority)
191 {
192 return IPL_HIGH
193 - (priority & ~GICC_PMR_NONSECURE) * NIPL / GICC_PMR_NS_PRIORITIES;
194 }
195 #endif
196
197 static void
198 armgic_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
199 {
200 struct armgic_softc * const sc = PICTOSOFTC(pic);
201 const size_t group = irq_base / 32;
202
203 if (group == 0)
204 sc->sc_enabled_local |= irq_mask;
205
206 gicd_write(sc, GICD_ISENABLERn(group), irq_mask);
207 }
208
209 static void
210 armgic_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
211 {
212 struct armgic_softc * const sc = PICTOSOFTC(pic);
213 const size_t group = irq_base / 32;
214
215 if (group == 0)
216 sc->sc_enabled_local &= ~irq_mask;
217
218 gicd_write(sc, GICD_ICENABLERn(group), irq_mask);
219 }
220
221 static void
222 armgic_set_priority(struct pic_softc *pic, int ipl)
223 {
224 struct armgic_softc * const sc = PICTOSOFTC(pic);
225 struct cpu_info * const ci = curcpu();
226
227 const uint32_t priority = armgic_ipl_to_priority(ipl);
228 if (priority > ci->ci_hwpl) {
229 /* Lowering priority mask */
230 ci->ci_hwpl = priority;
231 gicc_write(sc, GICC_PMR, priority);
232 }
233 }
234
235 #ifdef MULTIPROCESSOR
236 static void
237 armgic_get_affinity(struct pic_softc *pic, size_t irq, kcpuset_t *affinity)
238 {
239 struct armgic_softc * const sc = PICTOSOFTC(pic);
240 const size_t group = irq / 32;
241 int n;
242
243 kcpuset_zero(affinity);
244 if (group == 0) {
245 /* All CPUs are targets for group 0 (SGI/PPI) */
246 for (n = 0; n < MAXCPUS; n++) {
247 if (sc->sc_target[n] != 0)
248 kcpuset_set(affinity, n);
249 }
250 } else {
251 /* Find distributor targets (SPI) */
252 const u_int byte_shift = 8 * (irq & 3);
253 const bus_size_t targets_reg = GICD_ITARGETSRn(irq / 4);
254 const uint32_t targets = gicd_read(sc, targets_reg);
255 const uint32_t targets_val = (targets >> byte_shift) & 0xff;
256
257 for (n = 0; n < MAXCPUS; n++) {
258 if (sc->sc_target[n] & targets_val)
259 kcpuset_set(affinity, n);
260 }
261 }
262 }
263
264 static int
265 armgic_set_affinity(struct pic_softc *pic, size_t irq,
266 const kcpuset_t *affinity)
267 {
268 struct armgic_softc * const sc = PICTOSOFTC(pic);
269 const size_t group = irq / 32;
270 if (group == 0)
271 return EINVAL;
272
273 const u_int byte_shift = 8 * (irq & 3);
274 const bus_size_t targets_reg = GICD_ITARGETSRn(irq / 4);
275 uint32_t targets_val = 0;
276 int n;
277
278 for (n = 0; n < MAXCPUS; n++) {
279 if (kcpuset_isset(affinity, n))
280 targets_val |= sc->sc_target[n];
281 }
282
283 uint32_t targets = gicd_read(sc, targets_reg);
284 targets &= ~(0xff << byte_shift);
285 targets |= (targets_val << byte_shift);
286 gicd_write(sc, targets_reg, targets);
287
288 return 0;
289 }
290 #endif
291
292 #ifdef __HAVE_PIC_FAST_SOFTINTS
293 void
294 softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep_p)
295 {
296 lwp_t **lp = &l->l_cpu->ci_softlwps[level];
297 KASSERT(*lp == NULL || *lp == l);
298 *lp = l;
299 /*
300 * Really easy. Just tell it to trigger the local CPU.
301 */
302 *machdep_p = GICD_SGIR_TargetListFilter_Me
303 | __SHIFTIN(level, GICD_SGIR_SGIINTID);
304 }
305
306 void
307 softint_trigger(uintptr_t machdep)
308 {
309
310 gicd_write(&armgic_softc, GICD_SGIR, machdep);
311 }
312 #endif
313
314 void
315 armgic_irq_handler(void *tf)
316 {
317 struct cpu_info * const ci = curcpu();
318 struct armgic_softc * const sc = &armgic_softc;
319 const int old_ipl = ci->ci_cpl;
320 #ifdef DIAGNOSTIC
321 const int old_mtx_count = ci->ci_mtx_count;
322 const int old_l_biglocks = ci->ci_curlwp->l_biglocks;
323 #endif
324 #ifdef DEBUG
325 size_t n = 0;
326 #endif
327
328 ci->ci_data.cpu_nintr++;
329
330 const uint32_t priority = armgic_ipl_to_priority(old_ipl);
331 if (ci->ci_hwpl != priority) {
332 ci->ci_hwpl = priority;
333 gicc_write(sc, GICC_PMR, priority);
334 }
335
336 for (;;) {
337 uint32_t iar = gicc_read(sc, GICC_IAR);
338 uint32_t irq = __SHIFTOUT(iar, GICC_IAR_IRQ);
339
340 if (irq == GICC_IAR_IRQ_SPURIOUS ||
341 irq == GICC_IAR_IRQ_SSPURIOUS) {
342 iar = gicc_read(sc, GICC_IAR);
343 irq = __SHIFTOUT(iar, GICC_IAR_IRQ);
344 if (irq == GICC_IAR_IRQ_SPURIOUS)
345 break;
346 if (irq == GICC_IAR_IRQ_SSPURIOUS) {
347 break;
348 }
349 }
350
351 KASSERTMSG(old_ipl != IPL_HIGH, "old_ipl %d pmr %#x hppir %#x",
352 old_ipl, gicc_read(sc, GICC_PMR), gicc_read(sc, GICC_HPPIR));
353
354 //const uint32_t cpuid = __SHIFTOUT(iar, GICC_IAR_CPUID_MASK);
355 struct intrsource * const is = sc->sc_pic.pic_sources[irq];
356 KASSERT(is != &armgic_dummy_source);
357
358 /*
359 * GIC has asserted IPL for us so we can just update ci_cpl.
360 *
361 * But it's not that simple. We may have already bumped ci_cpl
362 * due to a high priority interrupt and now we are about to
363 * dispatch one lower than the previous. It's possible for
364 * that previous interrupt to have deferred some interrupts
365 * so we need deal with those when lowering to the current
366 * interrupt's ipl.
367 *
368 * However, if are just raising ipl, we can just update ci_cpl.
369 */
370 const int ipl = is->is_ipl;
371 if (__predict_false(ipl < ci->ci_cpl)) {
372 pic_do_pending_ints(I32_bit, ipl, tf);
373 KASSERT(ci->ci_cpl == ipl);
374 } else {
375 KASSERTMSG(ipl > ci->ci_cpl, "ipl %d cpl %d hw-ipl %#x",
376 ipl, ci->ci_cpl,
377 gicc_read(sc, GICC_PMR));
378 gicc_write(sc, GICC_PMR, armgic_ipl_to_priority(ipl));
379 ci->ci_hwpl = ci->ci_cpl = ipl;
380 }
381 ENABLE_INTERRUPT();
382 pic_dispatch(is, tf);
383 DISABLE_INTERRUPT();
384 gicc_write(sc, GICC_EOIR, iar);
385 #ifdef DEBUG
386 n++;
387 KDASSERTMSG(n < 5, "%s: processed too many (%zu)",
388 ci->ci_data.cpu_name, n);
389 #endif
390 }
391
392 /*
393 * Now handle any pending ints.
394 */
395 pic_do_pending_ints(I32_bit, old_ipl, tf);
396 KASSERTMSG(ci->ci_cpl == old_ipl, "ci_cpl %d old_ipl %d", ci->ci_cpl, old_ipl);
397 KASSERT(old_mtx_count == ci->ci_mtx_count);
398 KASSERT(old_l_biglocks == ci->ci_curlwp->l_biglocks);
399 }
400
401 void
402 armgic_establish_irq(struct pic_softc *pic, struct intrsource *is)
403 {
404 struct armgic_softc * const sc = PICTOSOFTC(pic);
405 const size_t group = is->is_irq / 32;
406 const u_int irq = is->is_irq & 31;
407 const u_int byte_shift = 8 * (irq & 3);
408 const u_int twopair_shift = 2 * (irq & 15);
409
410 KASSERTMSG(sc->sc_gic_valid_lines[group] & __BIT(irq),
411 "irq %u: not valid (group[%zu]=0x%08x [0x%08x])",
412 is->is_irq, group, sc->sc_gic_valid_lines[group],
413 (uint32_t)__BIT(irq));
414
415 KASSERTMSG(is->is_type == IST_LEVEL || is->is_type == IST_EDGE,
416 "irq %u: type %u unsupported", is->is_irq, is->is_type);
417
418 const bus_size_t targets_reg = GICD_ITARGETSRn(is->is_irq / 4);
419 const bus_size_t cfg_reg = GICD_ICFGRn(is->is_irq / 16);
420 uint32_t targets = gicd_read(sc, targets_reg);
421 uint32_t cfg = gicd_read(sc, cfg_reg);
422
423 if (group > 0) {
424 /*
425 * There are 4 irqs per TARGETS register. For now bind
426 * to the primary cpu.
427 */
428 targets &= ~(0xffU << byte_shift);
429 #if 0
430 #ifdef MULTIPROCESSOR
431 if (is->is_mpsafe) {
432 targets |= sc->sc_mptargets << byte_shift;
433 } else
434 #endif
435 #endif
436 targets |= sc->sc_bptargets << byte_shift;
437 gicd_write(sc, targets_reg, targets);
438
439 /*
440 * There are 16 irqs per CFG register. 10=EDGE 00=LEVEL
441 */
442 uint32_t new_cfg = cfg;
443 uint32_t old_cfg = (cfg >> twopair_shift) & __BITS(1, 0);
444 if (is->is_type == IST_LEVEL && (old_cfg & __BIT(1)) != 0) {
445 new_cfg &= ~(__BITS(1, 0) << twopair_shift);
446 } else if (is->is_type == IST_EDGE && (old_cfg & 2) == 0) {
447 new_cfg |= __BIT(1) << twopair_shift;
448 }
449 if (new_cfg != cfg) {
450 gicd_write(sc, cfg_reg, new_cfg);
451 }
452 #ifdef MULTIPROCESSOR
453 } else {
454 /*
455 * All group 0 interrupts are per processor and MPSAFE by
456 * default.
457 */
458 is->is_mpsafe = true;
459 #endif
460 }
461
462 /*
463 * There are 4 irqs per PRIORITY register. Map the IPL
464 * to GIC priority.
465 */
466 const bus_size_t priority_reg = GICD_IPRIORITYRn(is->is_irq / 4);
467 uint32_t priority = gicd_read(sc, priority_reg);
468 priority &= ~(0xffU << byte_shift);
469 priority |= armgic_ipl_to_priority(is->is_ipl) << byte_shift;
470 gicd_write(sc, priority_reg, priority);
471 }
472
473 #ifdef MULTIPROCESSOR
474 static void
475 armgic_cpu_init_priorities(struct armgic_softc *sc)
476 {
477 /* Set lowest priority, i.e. disable interrupts */
478 for (size_t i = 0; i < sc->sc_pic.pic_maxsources; i += 4) {
479 const bus_size_t priority_reg = GICD_IPRIORITYRn(i / 4);
480 gicd_write(sc, priority_reg, ~0);
481 }
482 }
483
484 static void
485 armgic_cpu_update_priorities(struct armgic_softc *sc)
486 {
487 uint32_t enabled = sc->sc_enabled_local;
488 for (size_t i = 0; i < sc->sc_pic.pic_maxsources; i += 4, enabled >>= 4) {
489 const bus_size_t priority_reg = GICD_IPRIORITYRn(i / 4);
490 uint32_t priority = gicd_read(sc, priority_reg);
491 uint32_t byte_mask = 0xff;
492 size_t byte_shift = 0;
493 for (size_t j = 0; j < 4; j++, byte_mask <<= 8, byte_shift += 8) {
494 struct intrsource * const is = sc->sc_pic.pic_sources[i+j];
495 priority |= byte_mask;
496 if (is == NULL || is == &armgic_dummy_source)
497 continue;
498 priority &= ~byte_mask;
499 priority |= armgic_ipl_to_priority(is->is_ipl) << byte_shift;
500 }
501 gicd_write(sc, priority_reg, priority);
502 }
503 }
504
505 static void
506 armgic_cpu_init_targets(struct armgic_softc *sc)
507 {
508 /*
509 * Update the mpsafe targets
510 */
511 for (size_t irq = 32; irq < sc->sc_pic.pic_maxsources; irq++) {
512 struct intrsource * const is = sc->sc_pic.pic_sources[irq];
513 const bus_size_t targets_reg = GICD_ITARGETSRn(irq / 4);
514 if (is != NULL && is->is_mpsafe) {
515 const u_int byte_shift = 8 * (irq & 3);
516 uint32_t targets = gicd_read(sc, targets_reg);
517 targets |= sc->sc_mptargets << byte_shift;
518 gicd_write(sc, targets_reg, targets);
519 }
520 }
521 }
522
523 void
524 armgic_cpu_init(struct pic_softc *pic, struct cpu_info *ci)
525 {
526 struct armgic_softc * const sc = PICTOSOFTC(pic);
527 sc->sc_target[cpu_index(ci)] = gicd_find_targets(sc);
528 atomic_or_32(&sc->sc_mptargets, sc->sc_target[cpu_index(ci)]);
529 KASSERTMSG(ci->ci_cpl == IPL_HIGH, "ipl %d not IPL_HIGH", ci->ci_cpl);
530 armgic_cpu_init_priorities(sc);
531 if (!CPU_IS_PRIMARY(ci)) {
532 if (popcount(sc->sc_mptargets) != 1) {
533 armgic_cpu_init_targets(sc);
534 }
535 if (sc->sc_enabled_local) {
536 armgic_cpu_update_priorities(sc);
537 gicd_write(sc, GICD_ISENABLERn(0),
538 sc->sc_enabled_local);
539 }
540 }
541 ci->ci_hwpl = armgic_ipl_to_priority(ci->ci_cpl);
542 gicc_write(sc, GICC_PMR, armgic_ipl_to_priority(ci->ci_cpl)); // set PMR
543 gicc_write(sc, GICC_CTRL, GICC_CTRL_V1_Enable); // enable interrupt
544 ENABLE_INTERRUPT(); // allow IRQ exceptions
545 }
546
547 void
548 armgic_ipi_send(struct pic_softc *pic, const kcpuset_t *kcp, u_long ipi)
549 {
550 struct armgic_softc * const sc = PICTOSOFTC(pic);
551
552 #if 0
553 if (ipi == IPI_NOP) {
554 sev();
555 return;
556 }
557 #endif
558
559 uint32_t sgir = __SHIFTIN(ARMGIC_SGI_IPIBASE + ipi, GICD_SGIR_SGIINTID);
560 if (kcp != NULL) {
561 uint32_t targets_val = 0;
562 for (int n = 0; n < MAXCPUS; n++) {
563 if (kcpuset_isset(kcp, n))
564 targets_val |= sc->sc_target[n];
565 }
566 sgir |= __SHIFTIN(targets_val, GICD_SGIR_TargetList);
567 sgir |= GICD_SGIR_TargetListFilter_List;
568 } else {
569 if (ncpu == 1)
570 return;
571 sgir |= GICD_SGIR_TargetListFilter_NotMe;
572 }
573
574 gicd_write(sc, GICD_SGIR, sgir);
575 }
576 #endif
577
578 int
579 armgic_match(device_t parent, cfdata_t cf, void *aux)
580 {
581 struct mpcore_attach_args * const mpcaa = aux;
582
583 if (strcmp(cf->cf_name, mpcaa->mpcaa_name) != 0)
584 return 0;
585
586 return 1;
587 }
588
589 void
590 armgic_attach(device_t parent, device_t self, void *aux)
591 {
592 struct armgic_softc * const sc = &armgic_softc;
593 struct mpcore_attach_args * const mpcaa = aux;
594
595 sc->sc_dev = self;
596 self->dv_private = sc;
597
598 sc->sc_memt = mpcaa->mpcaa_memt; /* provided for us */
599 bus_space_subregion(sc->sc_memt, mpcaa->mpcaa_memh, mpcaa->mpcaa_off1,
600 4096, &sc->sc_gicdh);
601 bus_space_subregion(sc->sc_memt, mpcaa->mpcaa_memh, mpcaa->mpcaa_off2,
602 4096, &sc->sc_gicch);
603
604 sc->sc_gic_type = gicd_read(sc, GICD_TYPER);
605 sc->sc_pic.pic_maxsources = GICD_TYPER_LINES(sc->sc_gic_type);
606
607 gicc_write(sc, GICC_CTRL, 0); /* disable all interrupts */
608 gicd_write(sc, GICD_CTRL, 0); /* disable all interrupts */
609
610 gicc_write(sc, GICC_PMR, 0xff);
611 uint32_t pmr = gicc_read(sc, GICC_PMR);
612 u_int priorities = 1 << popcount32(pmr);
613
614 const uint32_t iidr = gicc_read(sc, GICC_IIDR);
615 const int iidr_prod = __SHIFTOUT(iidr, GICC_IIDR_ProductID);
616 const int iidr_arch = __SHIFTOUT(iidr, GICC_IIDR_ArchVersion);
617 const int iidr_rev = __SHIFTOUT(iidr, GICC_IIDR_Revision);
618 const int iidr_imp = __SHIFTOUT(iidr, GICC_IIDR_Implementer);
619
620 /*
621 * Find the boot processor's CPU interface number.
622 */
623 sc->sc_bptargets = gicd_find_targets(sc);
624
625 /*
626 * Let's find out how many real sources we have.
627 */
628 for (size_t i = 0, group = 0;
629 i < sc->sc_pic.pic_maxsources;
630 i += 32, group++) {
631 /*
632 * To figure what sources are real, one enables all interrupts
633 * and then reads back the enable mask so which ones really
634 * got enabled.
635 */
636 gicd_write(sc, GICD_ISENABLERn(group), 0xffffffff);
637 uint32_t valid = gicd_read(sc, GICD_ISENABLERn(group));
638
639 /*
640 * Now disable (clear enable) them again.
641 */
642 gicd_write(sc, GICD_ICENABLERn(group), valid);
643
644 /*
645 * Count how many are valid.
646 */
647 sc->sc_gic_lines += popcount32(valid);
648 sc->sc_gic_valid_lines[group] = valid;
649 }
650
651 aprint_normal(": Generic Interrupt Controller, "
652 "%zu sources (%zu valid)\n",
653 sc->sc_pic.pic_maxsources, sc->sc_gic_lines);
654 aprint_debug_dev(sc->sc_dev, "Architecture version %d"
655 " (0x%x:%d rev %d)\n", iidr_arch, iidr_imp, iidr_prod,
656 iidr_rev);
657
658 #ifdef MULTIPROCESSOR
659 sc->sc_pic.pic_cpus = kcpuset_running;
660 #endif
661 pic_add(&sc->sc_pic, 0);
662
663 /*
664 * Force the GICD to IPL_HIGH and then enable interrupts.
665 */
666 struct cpu_info * const ci = curcpu();
667 KASSERTMSG(ci->ci_cpl == IPL_HIGH, "ipl %d not IPL_HIGH", ci->ci_cpl);
668 armgic_set_priority(&sc->sc_pic, ci->ci_cpl); // set PMR
669 gicd_write(sc, GICD_CTRL, GICD_CTRL_Enable); // enable Distributer
670 gicc_write(sc, GICC_CTRL, GICC_CTRL_V1_Enable); // enable CPU interrupts
671 ENABLE_INTERRUPT(); // allow interrupt exceptions
672
673 /*
674 * For each line that isn't valid, we set the intrsource for it to
675 * point at a dummy source so that pic_intr_establish will fail for it.
676 */
677 for (size_t i = 0, group = 0;
678 i < sc->sc_pic.pic_maxsources;
679 i += 32, group++) {
680 uint32_t invalid = ~sc->sc_gic_valid_lines[group];
681 for (size_t j = 0; invalid && j < 32; j++, invalid >>= 1) {
682 if (invalid & 1) {
683 sc->sc_pic.pic_sources[i + j] =
684 &armgic_dummy_source;
685 }
686 }
687 }
688 #ifdef __HAVE_PIC_FAST_SOFTINTS
689 intr_establish_xname(SOFTINT_BIO, IPL_SOFTBIO, IST_MPSAFE | IST_EDGE,
690 pic_handle_softint, (void *)SOFTINT_BIO, "softint bio");
691 intr_establish_xname(SOFTINT_CLOCK, IPL_SOFTCLOCK, IST_MPSAFE | IST_EDGE,
692 pic_handle_softint, (void *)SOFTINT_CLOCK, "softint clock");
693 intr_establish_xname(SOFTINT_NET, IPL_SOFTNET, IST_MPSAFE | IST_EDGE,
694 pic_handle_softint, (void *)SOFTINT_NET, "softint net");
695 intr_establish_xname(SOFTINT_SERIAL, IPL_SOFTSERIAL, IST_MPSAFE | IST_EDGE,
696 pic_handle_softint, (void *)SOFTINT_SERIAL, "softint serial");
697 #endif
698 #ifdef MULTIPROCESSOR
699 armgic_cpu_init(&sc->sc_pic, curcpu());
700
701 intr_establish_xname(ARMGIC_SGI_IPIBASE + IPI_AST, IPL_VM,
702 IST_MPSAFE | IST_EDGE, pic_ipi_ast, (void *)-1, "IPI ast");
703 intr_establish_xname(ARMGIC_SGI_IPIBASE + IPI_XCALL, IPL_HIGH,
704 IST_MPSAFE | IST_EDGE, pic_ipi_xcall, (void *)-1, "IPI xcall");
705 intr_establish_xname(ARMGIC_SGI_IPIBASE + IPI_GENERIC, IPL_HIGH,
706 IST_MPSAFE | IST_EDGE, pic_ipi_generic, (void *)-1, "IPI generic");
707 intr_establish_xname(ARMGIC_SGI_IPIBASE + IPI_NOP, IPL_VM,
708 IST_MPSAFE | IST_EDGE, pic_ipi_nop, (void *)-1, "IPI nop");
709 intr_establish_xname(ARMGIC_SGI_IPIBASE + IPI_SHOOTDOWN, IPL_SCHED,
710 IST_MPSAFE | IST_EDGE, pic_ipi_shootdown, (void *)-1, "IPI shootdown");
711 #ifdef DDB
712 intr_establish_xname(ARMGIC_SGI_IPIBASE + IPI_DDB, IPL_HIGH,
713 IST_MPSAFE | IST_EDGE, pic_ipi_ddb, NULL, "IPI ddb");
714 #endif
715 #ifdef __HAVE_PREEMPTION
716 intr_establish_xname(ARMGIC_SGI_IPIBASE + IPI_KPREEMPT, IPL_VM,
717 IST_MPSAFE | IST_EDGE, pic_ipi_kpreempt, (void *)-1, "IPI kpreempt");
718 #endif
719 #endif
720
721 const u_int ppis = popcount32(sc->sc_gic_valid_lines[0] >> 16);
722 const u_int sgis = popcount32(sc->sc_gic_valid_lines[0] & 0xffff);
723 aprint_normal_dev(sc->sc_dev, "%u Priorities, %zu SPIs, %u PPIs, "
724 "%u SGIs\n", priorities, sc->sc_gic_lines - ppis - sgis, ppis,
725 sgis);
726 }
727
728 CFATTACH_DECL_NEW(armgic, 0,
729 armgic_match, armgic_attach, NULL, NULL);
730