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