gicv3.c revision 1.52 1 /* $NetBSD: gicv3.c,v 1.52 2022/06/25 13:24:34 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "opt_multiprocessor.h"
30 #include "opt_gic.h"
31
32 #define _INTR_PRIVATE
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: gicv3.c,v 1.52 2022/06/25 13:24:34 jmcneill Exp $");
36
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/bus.h>
40 #include <sys/device.h>
41 #include <sys/intr.h>
42 #include <sys/systm.h>
43 #include <sys/cpu.h>
44 #include <sys/vmem.h>
45 #include <sys/kmem.h>
46 #include <sys/atomic.h>
47
48 #include <machine/cpufunc.h>
49
50 #include <arm/locore.h>
51 #include <arm/armreg.h>
52
53 #include <arm/cortex/gicv3.h>
54 #include <arm/cortex/gic_reg.h>
55
56 #define PICTOSOFTC(pic) \
57 container_of(pic, struct gicv3_softc, sc_pic)
58 #define LPITOSOFTC(lpi) \
59 container_of(lpi, struct gicv3_softc, sc_lpi)
60
61 #define IPL_TO_PRIORITY(sc, ipl) (((0xff - (ipl)) << (sc)->sc_priority_shift) & 0xff)
62 #define IPL_TO_PMR(sc, ipl) (((0xff - (ipl)) << (sc)->sc_pmr_shift) & 0xff)
63
64 #define GIC_SUPPORTS_1OFN(sc) (((sc)->sc_gicd_typer & GICD_TYPER_No1N) == 0)
65
66 #define GIC_PRIO_SHIFT_NS 4
67 #define GIC_PRIO_SHIFT_S 3
68
69 /*
70 * Set to true if you want to use 1 of N interrupt distribution for SPIs
71 * when available. Disabled by default because it causes issues with the
72 * USB stack.
73 */
74 bool gicv3_use_1ofn = false;
75
76 static struct gicv3_softc *gicv3_softc;
77
78 static inline uint32_t
79 gicd_read_4(struct gicv3_softc *sc, bus_size_t reg)
80 {
81 return bus_space_read_4(sc->sc_bst, sc->sc_bsh_d, reg);
82 }
83
84 static inline void
85 gicd_write_4(struct gicv3_softc *sc, bus_size_t reg, uint32_t val)
86 {
87 bus_space_write_4(sc->sc_bst, sc->sc_bsh_d, reg, val);
88 }
89
90 #ifdef MULTIPROCESSOR
91 static inline uint64_t
92 gicd_read_8(struct gicv3_softc *sc, bus_size_t reg)
93 {
94 return bus_space_read_8(sc->sc_bst, sc->sc_bsh_d, reg);
95 }
96 #endif
97
98 static inline void
99 gicd_write_8(struct gicv3_softc *sc, bus_size_t reg, uint64_t val)
100 {
101 bus_space_write_8(sc->sc_bst, sc->sc_bsh_d, reg, val);
102 }
103
104 static inline uint32_t
105 gicr_read_4(struct gicv3_softc *sc, u_int index, bus_size_t reg)
106 {
107 KASSERT(index < sc->sc_bsh_r_count);
108 return bus_space_read_4(sc->sc_bst, sc->sc_bsh_r[index], reg);
109 }
110
111 static inline void
112 gicr_write_4(struct gicv3_softc *sc, u_int index, bus_size_t reg, uint32_t val)
113 {
114 KASSERT(index < sc->sc_bsh_r_count);
115 bus_space_write_4(sc->sc_bst, sc->sc_bsh_r[index], reg, val);
116 }
117
118 static inline uint64_t
119 gicr_read_8(struct gicv3_softc *sc, u_int index, bus_size_t reg)
120 {
121 KASSERT(index < sc->sc_bsh_r_count);
122 return bus_space_read_8(sc->sc_bst, sc->sc_bsh_r[index], reg);
123 }
124
125 static inline void
126 gicr_write_8(struct gicv3_softc *sc, u_int index, bus_size_t reg, uint64_t val)
127 {
128 KASSERT(index < sc->sc_bsh_r_count);
129 bus_space_write_8(sc->sc_bst, sc->sc_bsh_r[index], reg, val);
130 }
131
132 static void
133 gicv3_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
134 {
135 struct gicv3_softc * const sc = PICTOSOFTC(pic);
136 struct cpu_info * const ci = curcpu();
137 const u_int group = irqbase / 32;
138
139 if (group == 0) {
140 atomic_or_32(&sc->sc_enabled_sgippi, mask);
141 gicr_write_4(sc, ci->ci_gic_redist, GICR_ISENABLER0, mask);
142 while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
143 ;
144 } else {
145 gicd_write_4(sc, GICD_ISENABLERn(group), mask);
146 while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
147 ;
148 }
149 }
150
151 static void
152 gicv3_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
153 {
154 struct gicv3_softc * const sc = PICTOSOFTC(pic);
155 struct cpu_info * const ci = curcpu();
156 const u_int group = irqbase / 32;
157
158 if (group == 0) {
159 atomic_and_32(&sc->sc_enabled_sgippi, ~mask);
160 gicr_write_4(sc, ci->ci_gic_redist, GICR_ICENABLER0, mask);
161 while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
162 ;
163 } else {
164 gicd_write_4(sc, GICD_ICENABLERn(group), mask);
165 while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
166 ;
167 }
168 }
169
170 static void
171 gicv3_establish_irq(struct pic_softc *pic, struct intrsource *is)
172 {
173 struct gicv3_softc * const sc = PICTOSOFTC(pic);
174 const u_int group = is->is_irq / 32;
175 uint32_t ipriority, icfg;
176 uint64_t irouter;
177 u_int n;
178
179 const u_int ipriority_val = IPL_TO_PRIORITY(sc, is->is_ipl);
180 const u_int ipriority_shift = (is->is_irq & 0x3) * 8;
181 const u_int icfg_shift = (is->is_irq & 0xf) * 2;
182
183 if (group == 0) {
184 /* SGIs and PPIs are per-CPU and always MP-safe */
185 is->is_mpsafe = true;
186 is->is_percpu = true;
187
188 /* Update interrupt configuration and priority on all redistributors */
189 for (n = 0; n < sc->sc_bsh_r_count; n++) {
190 icfg = gicr_read_4(sc, n, GICR_ICFGRn(is->is_irq / 16));
191 if (is->is_type == IST_LEVEL)
192 icfg &= ~(0x2 << icfg_shift);
193 if (is->is_type == IST_EDGE)
194 icfg |= (0x2 << icfg_shift);
195 gicr_write_4(sc, n, GICR_ICFGRn(is->is_irq / 16), icfg);
196
197 ipriority = gicr_read_4(sc, n, GICR_IPRIORITYRn(is->is_irq / 4));
198 ipriority &= ~(0xffU << ipriority_shift);
199 ipriority |= (ipriority_val << ipriority_shift);
200 gicr_write_4(sc, n, GICR_IPRIORITYRn(is->is_irq / 4), ipriority);
201 }
202 } else {
203 /*
204 * If 1 of N SPI routing is supported, route MP-safe interrupts to all
205 * participating PEs. Otherwise, just route to the primary PE.
206 */
207 if (is->is_mpsafe && GIC_SUPPORTS_1OFN(sc) && gicv3_use_1ofn) {
208 irouter = GICD_IROUTER_Interrupt_Routing_mode;
209 } else {
210 irouter = sc->sc_irouter[0];
211 }
212 gicd_write_8(sc, GICD_IROUTER(is->is_irq), irouter);
213
214 /* Update interrupt configuration */
215 icfg = gicd_read_4(sc, GICD_ICFGRn(is->is_irq / 16));
216 if (is->is_type == IST_LEVEL)
217 icfg &= ~(0x2 << icfg_shift);
218 if (is->is_type == IST_EDGE)
219 icfg |= (0x2 << icfg_shift);
220 gicd_write_4(sc, GICD_ICFGRn(is->is_irq / 16), icfg);
221
222 /* Update interrupt priority */
223 ipriority = gicd_read_4(sc, GICD_IPRIORITYRn(is->is_irq / 4));
224 ipriority &= ~(0xffU << ipriority_shift);
225 ipriority |= (ipriority_val << ipriority_shift);
226 gicd_write_4(sc, GICD_IPRIORITYRn(is->is_irq / 4), ipriority);
227 }
228 }
229
230 static void
231 gicv3_set_priority(struct pic_softc *pic, int ipl)
232 {
233 struct gicv3_softc * const sc = PICTOSOFTC(pic);
234 struct cpu_info * const ci = curcpu();
235
236 while (ipl < ci->ci_hwpl) {
237 /* Lowering priority mask */
238 ci->ci_hwpl = ipl;
239 __insn_barrier();
240 icc_pmr_write(IPL_TO_PMR(sc, ipl));
241 }
242 __insn_barrier();
243 ci->ci_cpl = ipl;
244 }
245
246 static void
247 gicv3_dist_enable(struct gicv3_softc *sc)
248 {
249 uint32_t gicd_ctrl;
250 u_int n;
251
252 /* Disable the distributor */
253 gicd_ctrl = gicd_read_4(sc, GICD_CTRL);
254 gicd_ctrl &= ~(GICD_CTRL_EnableGrp1A | GICD_CTRL_ARE_NS);
255 gicd_write_4(sc, GICD_CTRL, gicd_ctrl);
256
257 /* Wait for register write to complete */
258 while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
259 ;
260
261 /* Clear all INTID enable bits */
262 for (n = 32; n < sc->sc_pic.pic_maxsources; n += 32)
263 gicd_write_4(sc, GICD_ICENABLERn(n / 32), ~0);
264
265 /* Set default priorities to lowest */
266 for (n = 32; n < sc->sc_pic.pic_maxsources; n += 4)
267 gicd_write_4(sc, GICD_IPRIORITYRn(n / 4), ~0);
268
269 /* Set all interrupts to G1NS */
270 for (n = 32; n < sc->sc_pic.pic_maxsources; n += 32) {
271 gicd_write_4(sc, GICD_IGROUPRn(n / 32), ~0);
272 gicd_write_4(sc, GICD_IGRPMODRn(n / 32), 0);
273 }
274
275 /* Set all interrupts level-sensitive by default */
276 for (n = 32; n < sc->sc_pic.pic_maxsources; n += 16)
277 gicd_write_4(sc, GICD_ICFGRn(n / 16), 0);
278
279 /* Wait for register writes to complete */
280 while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
281 ;
282
283 /* Enable Affinity routing and G1NS interrupts */
284 gicd_ctrl = GICD_CTRL_EnableGrp1A | GICD_CTRL_ARE_NS;
285 gicd_write_4(sc, GICD_CTRL, gicd_ctrl);
286 }
287
288 static void
289 gicv3_redist_enable(struct gicv3_softc *sc, struct cpu_info *ci)
290 {
291 uint32_t icfg;
292 u_int n, o;
293
294 /* Clear INTID enable bits */
295 gicr_write_4(sc, ci->ci_gic_redist, GICR_ICENABLER0, ~0);
296
297 /* Wait for register write to complete */
298 while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
299 ;
300
301 /* Set default priorities */
302 for (n = 0; n < 32; n += 4) {
303 uint32_t priority = 0;
304 size_t byte_shift = 0;
305 for (o = 0; o < 4; o++, byte_shift += 8) {
306 struct intrsource * const is = sc->sc_pic.pic_sources[n + o];
307 if (is == NULL)
308 priority |= (0xffU << byte_shift);
309 else {
310 const u_int ipriority_val = IPL_TO_PRIORITY(sc, is->is_ipl);
311 priority |= ipriority_val << byte_shift;
312 }
313 }
314 gicr_write_4(sc, ci->ci_gic_redist, GICR_IPRIORITYRn(n / 4), priority);
315 }
316
317 /* Set all interrupts to G1NS */
318 gicr_write_4(sc, ci->ci_gic_redist, GICR_IGROUPR0, ~0);
319 gicr_write_4(sc, ci->ci_gic_redist, GICR_IGRPMODR0, 0);
320
321 /* Restore PPI configs */
322 for (n = 0, icfg = 0; n < 16; n++) {
323 struct intrsource * const is = sc->sc_pic.pic_sources[16 + n];
324 if (is != NULL && is->is_type == IST_EDGE)
325 icfg |= (0x2 << (n * 2));
326 }
327 gicr_write_4(sc, ci->ci_gic_redist, GICR_ICFGRn(1), icfg);
328
329 /* Restore current enable bits */
330 gicr_write_4(sc, ci->ci_gic_redist, GICR_ISENABLER0, sc->sc_enabled_sgippi);
331
332 /* Wait for register write to complete */
333 while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
334 ;
335 }
336
337 static uint64_t
338 gicv3_cpu_identity(void)
339 {
340 u_int aff3, aff2, aff1, aff0;
341
342 const register_t mpidr = cpu_mpidr_aff_read();
343 aff0 = __SHIFTOUT(mpidr, MPIDR_AFF0);
344 aff1 = __SHIFTOUT(mpidr, MPIDR_AFF1);
345 aff2 = __SHIFTOUT(mpidr, MPIDR_AFF2);
346 aff3 = __SHIFTOUT(mpidr, MPIDR_AFF3);
347
348 return __SHIFTIN(aff0, GICR_TYPER_Affinity_Value_Aff0) |
349 __SHIFTIN(aff1, GICR_TYPER_Affinity_Value_Aff1) |
350 __SHIFTIN(aff2, GICR_TYPER_Affinity_Value_Aff2) |
351 __SHIFTIN(aff3, GICR_TYPER_Affinity_Value_Aff3);
352 }
353
354 static u_int
355 gicv3_find_redist(struct gicv3_softc *sc)
356 {
357 uint64_t gicr_typer;
358 u_int n;
359
360 const uint64_t cpu_identity = gicv3_cpu_identity();
361
362 for (n = 0; n < sc->sc_bsh_r_count; n++) {
363 gicr_typer = gicr_read_8(sc, n, GICR_TYPER);
364 if ((gicr_typer & GICR_TYPER_Affinity_Value) == cpu_identity)
365 return n;
366 }
367
368 const u_int aff0 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff0);
369 const u_int aff1 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff1);
370 const u_int aff2 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff2);
371 const u_int aff3 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff3);
372
373 panic("%s: could not find GICv3 redistributor for cpu %d.%d.%d.%d",
374 cpu_name(curcpu()), aff3, aff2, aff1, aff0);
375 }
376
377 static uint64_t
378 gicv3_sgir(struct gicv3_softc *sc)
379 {
380 const uint64_t cpu_identity = gicv3_cpu_identity();
381
382 const u_int aff0 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff0);
383 const u_int aff1 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff1);
384 const u_int aff2 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff2);
385 const u_int aff3 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff3);
386
387 return __SHIFTIN(__BIT(aff0), ICC_SGIR_EL1_TargetList) |
388 __SHIFTIN(aff1, ICC_SGIR_EL1_Aff1) |
389 __SHIFTIN(aff2, ICC_SGIR_EL1_Aff2) |
390 __SHIFTIN(aff3, ICC_SGIR_EL1_Aff3);
391 }
392
393 static void
394 gicv3_cpu_init(struct pic_softc *pic, struct cpu_info *ci)
395 {
396 struct gicv3_softc * const sc = PICTOSOFTC(pic);
397 uint32_t icc_sre, icc_ctlr, gicr_waker;
398
399 evcnt_attach_dynamic(&ci->ci_intr_preempt, EVCNT_TYPE_MISC, NULL,
400 ci->ci_cpuname, "intr preempt");
401
402 ci->ci_gic_redist = gicv3_find_redist(sc);
403 ci->ci_gic_sgir = gicv3_sgir(sc);
404
405 /* Enable System register access and disable IRQ/FIQ bypass */
406 icc_sre = ICC_SRE_EL1_SRE | ICC_SRE_EL1_DFB | ICC_SRE_EL1_DIB;
407 icc_sre_write(icc_sre);
408
409 /* Mark the connected PE as being awake */
410 gicr_waker = gicr_read_4(sc, ci->ci_gic_redist, GICR_WAKER);
411 gicr_waker &= ~GICR_WAKER_ProcessorSleep;
412 gicr_write_4(sc, ci->ci_gic_redist, GICR_WAKER, gicr_waker);
413 while (gicr_read_4(sc, ci->ci_gic_redist, GICR_WAKER) & GICR_WAKER_ChildrenAsleep)
414 ;
415
416 /* Set initial priority mask */
417 ci->ci_hwpl = IPL_HIGH;
418 icc_pmr_write(IPL_TO_PMR(sc, IPL_HIGH));
419
420 /* Set the binary point field to the minimum value */
421 icc_bpr1_write(0);
422
423 /* Enable group 1 interrupt signaling */
424 icc_igrpen1_write(ICC_IGRPEN_EL1_Enable);
425
426 /* Set EOI mode */
427 icc_ctlr = icc_ctlr_read();
428 icc_ctlr &= ~ICC_CTLR_EL1_EOImode;
429 icc_ctlr_write(icc_ctlr);
430
431 /* Enable redistributor */
432 gicv3_redist_enable(sc, ci);
433
434 /* Allow IRQ exceptions */
435 ENABLE_INTERRUPT();
436 }
437
438 #ifdef MULTIPROCESSOR
439 static void
440 gicv3_ipi_send(struct pic_softc *pic, const kcpuset_t *kcp, u_long ipi)
441 {
442 struct cpu_info *ci;
443 uint64_t sgir;
444
445 sgir = __SHIFTIN(ipi, ICC_SGIR_EL1_INTID);
446 if (kcp == NULL) {
447 /* Interrupts routed to all PEs, excluding "self" */
448 if (ncpu == 1)
449 return;
450 sgir |= ICC_SGIR_EL1_IRM;
451 } else {
452 /* Interrupt to exactly one PE */
453 ci = cpu_lookup(kcpuset_ffs(kcp) - 1);
454 if (ci == curcpu())
455 return;
456 sgir |= ci->ci_gic_sgir;
457 }
458 icc_sgi1r_write(sgir);
459 isb();
460 }
461
462 static void
463 gicv3_get_affinity(struct pic_softc *pic, size_t irq, kcpuset_t *affinity)
464 {
465 struct gicv3_softc * const sc = PICTOSOFTC(pic);
466 const size_t group = irq / 32;
467 int n;
468
469 kcpuset_zero(affinity);
470 if (group == 0) {
471 /* All CPUs are targets for group 0 (SGI/PPI) */
472 for (n = 0; n < ncpu; n++) {
473 kcpuset_set(affinity, n);
474 }
475 } else {
476 /* Find distributor targets (SPI) */
477 const uint64_t irouter = gicd_read_8(sc, GICD_IROUTER(irq));
478 for (n = 0; n < ncpu; n++) {
479 if (irouter == GICD_IROUTER_Interrupt_Routing_mode ||
480 irouter == sc->sc_irouter[n])
481 kcpuset_set(affinity, n);
482 }
483 }
484 }
485
486 static int
487 gicv3_set_affinity(struct pic_softc *pic, size_t irq, const kcpuset_t *affinity)
488 {
489 struct gicv3_softc * const sc = PICTOSOFTC(pic);
490 const size_t group = irq / 32;
491 uint64_t irouter;
492
493 if (group == 0)
494 return EINVAL;
495
496 const int set = kcpuset_countset(affinity);
497 if (set == 1) {
498 irouter = sc->sc_irouter[kcpuset_ffs(affinity) - 1];
499 } else if (set == ncpu && GIC_SUPPORTS_1OFN(sc) && gicv3_use_1ofn) {
500 irouter = GICD_IROUTER_Interrupt_Routing_mode;
501 } else {
502 return EINVAL;
503 }
504
505 gicd_write_8(sc, GICD_IROUTER(irq), irouter);
506
507 return 0;
508 }
509 #endif
510
511 static const struct pic_ops gicv3_picops = {
512 .pic_unblock_irqs = gicv3_unblock_irqs,
513 .pic_block_irqs = gicv3_block_irqs,
514 .pic_establish_irq = gicv3_establish_irq,
515 .pic_set_priority = gicv3_set_priority,
516 #ifdef MULTIPROCESSOR
517 .pic_cpu_init = gicv3_cpu_init,
518 .pic_ipi_send = gicv3_ipi_send,
519 .pic_get_affinity = gicv3_get_affinity,
520 .pic_set_affinity = gicv3_set_affinity,
521 #endif
522 };
523
524 static void
525 gicv3_dcache_wb_range(vaddr_t va, vsize_t len)
526 {
527 cpu_dcache_wb_range(va, len);
528 dsb(sy);
529 }
530
531 static void
532 gicv3_lpi_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
533 {
534 struct gicv3_softc * const sc = LPITOSOFTC(pic);
535 int bit;
536
537 while ((bit = ffs(mask)) != 0) {
538 sc->sc_lpiconf.base[irqbase + bit - 1] |= GIC_LPICONF_Enable;
539 if (sc->sc_lpiconf_flush)
540 gicv3_dcache_wb_range((vaddr_t)&sc->sc_lpiconf.base[irqbase + bit - 1], 1);
541 mask &= ~__BIT(bit - 1);
542 }
543
544 if (!sc->sc_lpiconf_flush)
545 dsb(ishst);
546 }
547
548 static void
549 gicv3_lpi_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
550 {
551 struct gicv3_softc * const sc = LPITOSOFTC(pic);
552 int bit;
553
554 while ((bit = ffs(mask)) != 0) {
555 sc->sc_lpiconf.base[irqbase + bit - 1] &= ~GIC_LPICONF_Enable;
556 if (sc->sc_lpiconf_flush)
557 gicv3_dcache_wb_range((vaddr_t)&sc->sc_lpiconf.base[irqbase + bit - 1], 1);
558 mask &= ~__BIT(bit - 1);
559 }
560
561 if (!sc->sc_lpiconf_flush)
562 dsb(ishst);
563 }
564
565 static void
566 gicv3_lpi_establish_irq(struct pic_softc *pic, struct intrsource *is)
567 {
568 struct gicv3_softc * const sc = LPITOSOFTC(pic);
569
570 sc->sc_lpiconf.base[is->is_irq] = IPL_TO_PRIORITY(sc, is->is_ipl) | GIC_LPICONF_Res1;
571
572 if (sc->sc_lpiconf_flush)
573 gicv3_dcache_wb_range((vaddr_t)&sc->sc_lpiconf.base[is->is_irq], 1);
574 else
575 dsb(ishst);
576 }
577
578 static void
579 gicv3_lpi_cpu_init(struct pic_softc *pic, struct cpu_info *ci)
580 {
581 struct gicv3_softc * const sc = LPITOSOFTC(pic);
582 struct gicv3_lpi_callback *cb;
583 uint64_t propbase, pendbase;
584 uint32_t ctlr;
585
586 /* If physical LPIs are not supported on this redistributor, just return. */
587 const uint64_t typer = gicr_read_8(sc, ci->ci_gic_redist, GICR_TYPER);
588 if ((typer & GICR_TYPER_PLPIS) == 0)
589 return;
590
591 /* Interrupt target address for this CPU, used by ITS when GITS_TYPER.PTA == 0 */
592 sc->sc_processor_id[cpu_index(ci)] = __SHIFTOUT(typer, GICR_TYPER_Processor_Number);
593
594 /* Disable LPIs before making changes */
595 ctlr = gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR);
596 ctlr &= ~GICR_CTLR_Enable_LPIs;
597 gicr_write_4(sc, ci->ci_gic_redist, GICR_CTLR, ctlr);
598 dsb(sy);
599
600 /* Setup the LPI configuration table */
601 propbase = sc->sc_lpiconf.segs[0].ds_addr |
602 __SHIFTIN(ffs(pic->pic_maxsources) - 1, GICR_PROPBASER_IDbits) |
603 __SHIFTIN(GICR_Shareability_IS, GICR_PROPBASER_Shareability) |
604 __SHIFTIN(GICR_Cache_NORMAL_RA_WA_WB, GICR_PROPBASER_InnerCache);
605 gicr_write_8(sc, ci->ci_gic_redist, GICR_PROPBASER, propbase);
606 propbase = gicr_read_8(sc, ci->ci_gic_redist, GICR_PROPBASER);
607 if (__SHIFTOUT(propbase, GICR_PROPBASER_Shareability) != GICR_Shareability_IS) {
608 if (__SHIFTOUT(propbase, GICR_PROPBASER_Shareability) == GICR_Shareability_NS) {
609 propbase &= ~GICR_PROPBASER_Shareability;
610 propbase |= __SHIFTIN(GICR_Shareability_NS, GICR_PROPBASER_Shareability);
611 propbase &= ~GICR_PROPBASER_InnerCache;
612 propbase |= __SHIFTIN(GICR_Cache_NORMAL_NC, GICR_PROPBASER_InnerCache);
613 gicr_write_8(sc, ci->ci_gic_redist, GICR_PROPBASER, propbase);
614 }
615 sc->sc_lpiconf_flush = true;
616 }
617
618 /* Setup the LPI pending table */
619 pendbase = sc->sc_lpipend[cpu_index(ci)].segs[0].ds_addr |
620 __SHIFTIN(GICR_Shareability_IS, GICR_PENDBASER_Shareability) |
621 __SHIFTIN(GICR_Cache_NORMAL_RA_WA_WB, GICR_PENDBASER_InnerCache);
622 gicr_write_8(sc, ci->ci_gic_redist, GICR_PENDBASER, pendbase);
623 pendbase = gicr_read_8(sc, ci->ci_gic_redist, GICR_PENDBASER);
624 if (__SHIFTOUT(pendbase, GICR_PENDBASER_Shareability) == GICR_Shareability_NS) {
625 pendbase &= ~GICR_PENDBASER_Shareability;
626 pendbase |= __SHIFTIN(GICR_Shareability_NS, GICR_PENDBASER_Shareability);
627 pendbase &= ~GICR_PENDBASER_InnerCache;
628 pendbase |= __SHIFTIN(GICR_Cache_NORMAL_NC, GICR_PENDBASER_InnerCache);
629 gicr_write_8(sc, ci->ci_gic_redist, GICR_PENDBASER, pendbase);
630 }
631
632 /* Enable LPIs */
633 ctlr = gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR);
634 ctlr |= GICR_CTLR_Enable_LPIs;
635 gicr_write_4(sc, ci->ci_gic_redist, GICR_CTLR, ctlr);
636 dsb(sy);
637
638 /* Setup ITS if present */
639 LIST_FOREACH(cb, &sc->sc_lpi_callbacks, list)
640 cb->cpu_init(cb->priv, ci);
641 }
642
643 #ifdef MULTIPROCESSOR
644 static void
645 gicv3_lpi_get_affinity(struct pic_softc *pic, size_t irq, kcpuset_t *affinity)
646 {
647 struct gicv3_softc * const sc = LPITOSOFTC(pic);
648 struct gicv3_lpi_callback *cb;
649
650 kcpuset_zero(affinity);
651 LIST_FOREACH(cb, &sc->sc_lpi_callbacks, list)
652 cb->get_affinity(cb->priv, irq, affinity);
653 }
654
655 static int
656 gicv3_lpi_set_affinity(struct pic_softc *pic, size_t irq, const kcpuset_t *affinity)
657 {
658 struct gicv3_softc * const sc = LPITOSOFTC(pic);
659 struct gicv3_lpi_callback *cb;
660 int error = EINVAL;
661
662 LIST_FOREACH(cb, &sc->sc_lpi_callbacks, list) {
663 error = cb->set_affinity(cb->priv, irq, affinity);
664 if (error != EPASSTHROUGH)
665 return error;
666 }
667
668 return EINVAL;
669 }
670 #endif
671
672 static const struct pic_ops gicv3_lpiops = {
673 .pic_unblock_irqs = gicv3_lpi_unblock_irqs,
674 .pic_block_irqs = gicv3_lpi_block_irqs,
675 .pic_establish_irq = gicv3_lpi_establish_irq,
676 #ifdef MULTIPROCESSOR
677 .pic_cpu_init = gicv3_lpi_cpu_init,
678 .pic_get_affinity = gicv3_lpi_get_affinity,
679 .pic_set_affinity = gicv3_lpi_set_affinity,
680 #endif
681 };
682
683 void
684 gicv3_dma_alloc(struct gicv3_softc *sc, struct gicv3_dma *dma, bus_size_t len, bus_size_t align)
685 {
686 int nsegs, error;
687
688 dma->len = len;
689 error = bus_dmamem_alloc(sc->sc_dmat, dma->len, align, 0, dma->segs, 1, &nsegs, BUS_DMA_WAITOK);
690 if (error)
691 panic("bus_dmamem_alloc failed: %d", error);
692 error = bus_dmamem_map(sc->sc_dmat, dma->segs, nsegs, len, (void **)&dma->base, BUS_DMA_WAITOK);
693 if (error)
694 panic("bus_dmamem_map failed: %d", error);
695 error = bus_dmamap_create(sc->sc_dmat, len, 1, len, 0, BUS_DMA_WAITOK, &dma->map);
696 if (error)
697 panic("bus_dmamap_create failed: %d", error);
698 error = bus_dmamap_load(sc->sc_dmat, dma->map, dma->base, dma->len, NULL, BUS_DMA_WAITOK);
699 if (error)
700 panic("bus_dmamap_load failed: %d", error);
701
702 memset(dma->base, 0, dma->len);
703 bus_dmamap_sync(sc->sc_dmat, dma->map, 0, dma->len, BUS_DMASYNC_PREWRITE);
704 }
705
706 static void
707 gicv3_lpi_init(struct gicv3_softc *sc)
708 {
709 /*
710 * Allocate LPI configuration table
711 */
712 gicv3_dma_alloc(sc, &sc->sc_lpiconf, sc->sc_lpi.pic_maxsources, 0x1000);
713 KASSERT((sc->sc_lpiconf.segs[0].ds_addr & ~GICR_PROPBASER_Physical_Address) == 0);
714
715 /*
716 * Allocate LPI pending tables
717 */
718 const bus_size_t lpipend_sz = (8192 + sc->sc_lpi.pic_maxsources) / NBBY;
719 for (int cpuindex = 0; cpuindex < ncpu; cpuindex++) {
720 gicv3_dma_alloc(sc, &sc->sc_lpipend[cpuindex], lpipend_sz, 0x10000);
721 KASSERT((sc->sc_lpipend[cpuindex].segs[0].ds_addr & ~GICR_PENDBASER_Physical_Address) == 0);
722 }
723 }
724
725 void
726 gicv3_irq_handler(void *frame)
727 {
728 struct cpu_info * const ci = curcpu();
729 struct gicv3_softc * const sc = gicv3_softc;
730 struct pic_softc *pic;
731 const int oldipl = ci->ci_cpl;
732
733 ci->ci_data.cpu_nintr++;
734
735 if (ci->ci_hwpl != oldipl) {
736 ci->ci_hwpl = oldipl;
737 icc_pmr_write(IPL_TO_PMR(sc, oldipl));
738 if (oldipl == IPL_HIGH) {
739 return;
740 }
741 }
742
743 for (;;) {
744 const uint32_t iar = icc_iar1_read();
745 dsb(sy);
746 const uint32_t irq = __SHIFTOUT(iar, ICC_IAR_INTID);
747 if (irq == ICC_IAR_INTID_SPURIOUS)
748 break;
749
750 pic = irq >= GIC_LPI_BASE ? &sc->sc_lpi : &sc->sc_pic;
751 if (irq - pic->pic_irqbase >= pic->pic_maxsources)
752 continue;
753
754 struct intrsource * const is = pic->pic_sources[irq - pic->pic_irqbase];
755 KASSERT(is != NULL);
756
757 const bool early_eoi = irq < GIC_LPI_BASE && is->is_type == IST_EDGE;
758
759 const int ipl = is->is_ipl;
760 if (__predict_false(ipl < ci->ci_cpl)) {
761 pic_do_pending_ints(I32_bit, ipl, frame);
762 } else if (ci->ci_cpl != ipl) {
763 icc_pmr_write(IPL_TO_PMR(sc, ipl));
764 ci->ci_hwpl = ci->ci_cpl = ipl;
765 }
766
767 if (early_eoi) {
768 icc_eoi1r_write(iar);
769 isb();
770 }
771
772 const int64_t nintr = ci->ci_data.cpu_nintr;
773
774 ENABLE_INTERRUPT();
775 pic_dispatch(is, frame);
776 DISABLE_INTERRUPT();
777
778 if (nintr != ci->ci_data.cpu_nintr)
779 ci->ci_intr_preempt.ev_count++;
780
781 if (!early_eoi) {
782 icc_eoi1r_write(iar);
783 isb();
784 }
785 }
786
787 pic_do_pending_ints(I32_bit, oldipl, frame);
788 }
789
790 static bool
791 gicv3_cpuif_is_nonsecure(struct gicv3_softc *sc)
792 {
793 /*
794 * Write 0 to bit7 and see if it sticks. This is only possible if
795 * we have a non-secure view of the PMR register.
796 */
797 const uint32_t opmr = icc_pmr_read();
798 icc_pmr_write(0);
799 const uint32_t npmr = icc_pmr_read();
800 icc_pmr_write(opmr);
801
802 return (npmr & GICC_PMR_NONSECURE) == 0;
803 }
804
805 static bool
806 gicv3_dist_is_nonsecure(struct gicv3_softc *sc)
807 {
808 const uint32_t gicd_ctrl = gicd_read_4(sc, GICD_CTRL);
809
810 /*
811 * If security is enabled, we have a non-secure view of the IPRIORITYRn
812 * registers and LPI configuration priority fields.
813 */
814 return (gicd_ctrl & GICD_CTRL_DS) == 0;
815 }
816
817 /*
818 * Rockchip RK3399 provides a different view of int priority registers
819 * depending on which firmware is in use. This is hard to detect in
820 * a way that could possibly break other boards, so only do this
821 * detection if we know we are on a RK3399 SoC.
822 */
823 static void
824 gicv3_quirk_rockchip_rk3399(struct gicv3_softc *sc)
825 {
826 /* Detect the number of supported PMR bits */
827 icc_pmr_write(0xff);
828 const uint8_t pmrbits = icc_pmr_read();
829
830 /* Detect the number of supported IPRIORITYRn bits */
831 const uint32_t oiprio = gicd_read_4(sc, GICD_IPRIORITYRn(8));
832 gicd_write_4(sc, GICD_IPRIORITYRn(8), oiprio | 0xff);
833 const uint8_t pribits = gicd_read_4(sc, GICD_IPRIORITYRn(8)) & 0xff;
834 gicd_write_4(sc, GICD_IPRIORITYRn(8), oiprio);
835
836 /*
837 * If we see fewer PMR bits than IPRIORITYRn bits here, it means
838 * we have a secure view of IPRIORITYRn (this is not supposed to
839 * happen!).
840 */
841 if (pmrbits < pribits) {
842 aprint_verbose_dev(sc->sc_dev,
843 "buggy RK3399 firmware detected; applying workaround\n");
844 sc->sc_priority_shift = GIC_PRIO_SHIFT_S;
845 }
846 }
847
848 int
849 gicv3_init(struct gicv3_softc *sc)
850 {
851 CPU_INFO_ITERATOR cii;
852 struct cpu_info *ci;
853
854 KASSERT(CPU_IS_PRIMARY(curcpu()));
855
856 LIST_INIT(&sc->sc_lpi_callbacks);
857
858 /* Store route to CPU for SPIs */
859 sc->sc_irouter = kmem_zalloc(sizeof(*sc->sc_irouter) * ncpu, KM_SLEEP);
860 for (CPU_INFO_FOREACH(cii, ci)) {
861 KASSERT(cpu_index(ci) < ncpu);
862 sc->sc_irouter[cpu_index(ci)] = ci->ci_cpuid;
863 }
864
865 sc->sc_gicd_typer = gicd_read_4(sc, GICD_TYPER);
866
867 /*
868 * We don't always have a consistent view of priorities between the
869 * CPU interface (ICC_PMR_EL1) and the GICD/GICR registers. Detect
870 * if we are making secure or non-secure accesses to each, and adjust
871 * the values that we write to each accordingly.
872 */
873 const bool dist_ns = gicv3_dist_is_nonsecure(sc);
874 sc->sc_priority_shift = dist_ns ? GIC_PRIO_SHIFT_NS : GIC_PRIO_SHIFT_S;
875 const bool cpuif_ns = gicv3_cpuif_is_nonsecure(sc);
876 sc->sc_pmr_shift = cpuif_ns ? GIC_PRIO_SHIFT_NS : GIC_PRIO_SHIFT_S;
877
878 if ((sc->sc_quirks & GICV3_QUIRK_RK3399) != 0)
879 gicv3_quirk_rockchip_rk3399(sc);
880
881 aprint_verbose_dev(sc->sc_dev,
882 "iidr 0x%08x, cpuif %ssecure, dist %ssecure, "
883 "priority shift %d, pmr shift %d, quirks %#x\n",
884 gicd_read_4(sc, GICD_IIDR),
885 cpuif_ns ? "non-" : "",
886 dist_ns ? "non-" : "",
887 sc->sc_priority_shift,
888 sc->sc_pmr_shift,
889 sc->sc_quirks);
890
891 sc->sc_pic.pic_ops = &gicv3_picops;
892 sc->sc_pic.pic_maxsources = GICD_TYPER_LINES(sc->sc_gicd_typer);
893 snprintf(sc->sc_pic.pic_name, sizeof(sc->sc_pic.pic_name), "gicv3");
894 #ifdef MULTIPROCESSOR
895 sc->sc_pic.pic_cpus = kcpuset_running;
896 #endif
897 pic_add(&sc->sc_pic, 0);
898
899 if ((sc->sc_gicd_typer & GICD_TYPER_LPIS) != 0) {
900 sc->sc_lpipend = kmem_zalloc(sizeof(*sc->sc_lpipend) * ncpu, KM_SLEEP);
901 sc->sc_processor_id = kmem_zalloc(sizeof(*sc->sc_processor_id) * ncpu, KM_SLEEP);
902
903 sc->sc_lpi.pic_ops = &gicv3_lpiops;
904 sc->sc_lpi.pic_maxsources = 8192; /* Min. required by GICv3 spec */
905 snprintf(sc->sc_lpi.pic_name, sizeof(sc->sc_lpi.pic_name), "gicv3-lpi");
906 pic_add(&sc->sc_lpi, GIC_LPI_BASE);
907
908 sc->sc_lpi_pool = vmem_create("gicv3-lpi", 0, sc->sc_lpi.pic_maxsources,
909 1, NULL, NULL, NULL, 0, VM_SLEEP, IPL_HIGH);
910 if (sc->sc_lpi_pool == NULL)
911 panic("failed to create gicv3 lpi pool\n");
912
913 gicv3_lpi_init(sc);
914 }
915
916 KASSERT(gicv3_softc == NULL);
917 gicv3_softc = sc;
918
919 for (int i = 0; i < sc->sc_bsh_r_count; i++) {
920 const uint64_t gicr_typer = gicr_read_8(sc, i, GICR_TYPER);
921 const u_int aff0 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff0);
922 const u_int aff1 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff1);
923 const u_int aff2 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff2);
924 const u_int aff3 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff3);
925
926 aprint_debug_dev(sc->sc_dev, "redist %d: cpu %d.%d.%d.%d\n",
927 i, aff3, aff2, aff1, aff0);
928 }
929
930 gicv3_dist_enable(sc);
931
932 gicv3_cpu_init(&sc->sc_pic, curcpu());
933 if ((sc->sc_gicd_typer & GICD_TYPER_LPIS) != 0)
934 gicv3_lpi_cpu_init(&sc->sc_lpi, curcpu());
935
936 #ifdef MULTIPROCESSOR
937 intr_establish_xname(IPI_AST, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_ast, (void *)-1, "IPI ast");
938 intr_establish_xname(IPI_XCALL, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_xcall, (void *)-1, "IPI xcall");
939 intr_establish_xname(IPI_GENERIC, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_generic, (void *)-1, "IPI generic");
940 intr_establish_xname(IPI_NOP, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_nop, (void *)-1, "IPI nop");
941 intr_establish_xname(IPI_SHOOTDOWN, IPL_SCHED, IST_MPSAFE | IST_EDGE, pic_ipi_shootdown, (void *)-1, "IPI shootdown");
942 #ifdef DDB
943 intr_establish_xname(IPI_DDB, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_ddb, NULL, "IPI ddb");
944 #endif
945 #ifdef __HAVE_PREEMPTION
946 intr_establish_xname(IPI_KPREEMPT, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_kpreempt, (void *)-1, "IPI kpreempt");
947 #endif
948 #endif
949
950 return 0;
951 }
952