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