gicv3.c revision 1.19 1 /* $NetBSD: gicv3.c,v 1.19 2019/06/26 23:00:09 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
31 #define _INTR_PRIVATE
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: gicv3.c,v 1.19 2019/06/26 23:00:09 jmcneill Exp $");
35
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/device.h>
40 #include <sys/intr.h>
41 #include <sys/systm.h>
42 #include <sys/cpu.h>
43
44 #include <arm/locore.h>
45 #include <arm/armreg.h>
46
47 #include <arm/cortex/gicv3.h>
48 #include <arm/cortex/gic_reg.h>
49
50 #define PICTOSOFTC(pic) \
51 ((void *)((uintptr_t)(pic) - offsetof(struct gicv3_softc, sc_pic)))
52 #define LPITOSOFTC(lpi) \
53 ((void *)((uintptr_t)(lpi) - offsetof(struct gicv3_softc, sc_lpi)))
54
55 #define IPL_TO_PRIORITY(sc, ipl) (((0xff - (ipl)) << (sc)->sc_priority_shift) & 0xff)
56 #define IPL_TO_PMR(sc, ipl) (((0xff - (ipl)) << (sc)->sc_pmr_shift) & 0xff)
57 #define IPL_TO_LPIPRIO(sc, ipl) (((0xff - (ipl)) << 4) & 0xff)
58
59 static struct gicv3_softc *gicv3_softc;
60
61 static inline uint32_t
62 gicd_read_4(struct gicv3_softc *sc, bus_size_t reg)
63 {
64 return bus_space_read_4(sc->sc_bst, sc->sc_bsh_d, reg);
65 }
66
67 static inline void
68 gicd_write_4(struct gicv3_softc *sc, bus_size_t reg, uint32_t val)
69 {
70 bus_space_write_4(sc->sc_bst, sc->sc_bsh_d, reg, val);
71 }
72
73 static inline uint64_t
74 gicd_read_8(struct gicv3_softc *sc, bus_size_t reg)
75 {
76 return bus_space_read_8(sc->sc_bst, sc->sc_bsh_d, reg);
77 }
78
79 static inline void
80 gicd_write_8(struct gicv3_softc *sc, bus_size_t reg, uint64_t val)
81 {
82 bus_space_write_8(sc->sc_bst, sc->sc_bsh_d, reg, val);
83 }
84
85 static inline uint32_t
86 gicr_read_4(struct gicv3_softc *sc, u_int index, bus_size_t reg)
87 {
88 KASSERT(index < sc->sc_bsh_r_count);
89 return bus_space_read_4(sc->sc_bst, sc->sc_bsh_r[index], reg);
90 }
91
92 static inline void
93 gicr_write_4(struct gicv3_softc *sc, u_int index, bus_size_t reg, uint32_t val)
94 {
95 KASSERT(index < sc->sc_bsh_r_count);
96 bus_space_write_4(sc->sc_bst, sc->sc_bsh_r[index], reg, val);
97 }
98
99 static inline uint64_t
100 gicr_read_8(struct gicv3_softc *sc, u_int index, bus_size_t reg)
101 {
102 KASSERT(index < sc->sc_bsh_r_count);
103 return bus_space_read_8(sc->sc_bst, sc->sc_bsh_r[index], reg);
104 }
105
106 static inline void
107 gicr_write_8(struct gicv3_softc *sc, u_int index, bus_size_t reg, uint64_t val)
108 {
109 KASSERT(index < sc->sc_bsh_r_count);
110 bus_space_write_8(sc->sc_bst, sc->sc_bsh_r[index], reg, val);
111 }
112
113 static void
114 gicv3_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
115 {
116 struct gicv3_softc * const sc = PICTOSOFTC(pic);
117 struct cpu_info * const ci = curcpu();
118 const u_int group = irqbase / 32;
119
120 if (group == 0) {
121 sc->sc_enabled_sgippi |= mask;
122 gicr_write_4(sc, ci->ci_gic_redist, GICR_ISENABLER0, mask);
123 while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
124 ;
125 } else {
126 gicd_write_4(sc, GICD_ISENABLERn(group), mask);
127 while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
128 ;
129 }
130 }
131
132 static void
133 gicv3_block_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 sc->sc_enabled_sgippi &= ~mask;
141 gicr_write_4(sc, ci->ci_gic_redist, GICR_ICENABLER0, 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_ICENABLERn(group), mask);
146 while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
147 ;
148 }
149 }
150
151 static void
152 gicv3_establish_irq(struct pic_softc *pic, struct intrsource *is)
153 {
154 struct gicv3_softc * const sc = PICTOSOFTC(pic);
155 const u_int group = is->is_irq / 32;
156 uint32_t ipriority, icfg;
157 uint64_t irouter;
158 u_int n;
159
160 const u_int ipriority_val = IPL_TO_PRIORITY(sc, is->is_ipl);
161 const u_int ipriority_shift = (is->is_irq & 0x3) * 8;
162 const u_int icfg_shift = (is->is_irq & 0xf) * 2;
163
164 if (group == 0) {
165 /* SGIs and PPIs are always MP-safe */
166 is->is_mpsafe = true;
167
168 /* Update interrupt configuration and priority on all redistributors */
169 for (n = 0; n < sc->sc_bsh_r_count; n++) {
170 icfg = gicr_read_4(sc, n, GICR_ICFGRn(is->is_irq / 16));
171 if (is->is_type == IST_LEVEL)
172 icfg &= ~(0x2 << icfg_shift);
173 if (is->is_type == IST_EDGE)
174 icfg |= (0x2 << icfg_shift);
175 gicr_write_4(sc, n, GICR_ICFGRn(is->is_irq / 16), icfg);
176
177 ipriority = gicr_read_4(sc, n, GICR_IPRIORITYRn(is->is_irq / 4));
178 ipriority &= ~(0xff << ipriority_shift);
179 ipriority |= (ipriority_val << ipriority_shift);
180 gicr_write_4(sc, n, GICR_IPRIORITYRn(is->is_irq / 4), ipriority);
181 }
182 } else {
183 if (is->is_mpsafe) {
184 /* Route MP-safe interrupts to all participating PEs */
185 irouter = GICD_IROUTER_Interrupt_Routing_mode;
186 } else {
187 /* Route non-MP-safe interrupts to the primary PE only */
188 irouter = sc->sc_irouter[0];
189 }
190 gicd_write_8(sc, GICD_IROUTER(is->is_irq), irouter);
191
192 /* Update interrupt configuration */
193 icfg = gicd_read_4(sc, GICD_ICFGRn(is->is_irq / 16));
194 if (is->is_type == IST_LEVEL)
195 icfg &= ~(0x2 << icfg_shift);
196 if (is->is_type == IST_EDGE)
197 icfg |= (0x2 << icfg_shift);
198 gicd_write_4(sc, GICD_ICFGRn(is->is_irq / 16), icfg);
199
200 /* Update interrupt priority */
201 ipriority = gicd_read_4(sc, GICD_IPRIORITYRn(is->is_irq / 4));
202 ipriority &= ~(0xff << ipriority_shift);
203 ipriority |= (ipriority_val << ipriority_shift);
204 gicd_write_4(sc, GICD_IPRIORITYRn(is->is_irq / 4), ipriority);
205 }
206 }
207
208 static void
209 gicv3_set_priority(struct pic_softc *pic, int ipl)
210 {
211 struct gicv3_softc * const sc = PICTOSOFTC(pic);
212
213 icc_pmr_write(IPL_TO_PMR(sc, ipl));
214 }
215
216 static void
217 gicv3_dist_enable(struct gicv3_softc *sc)
218 {
219 uint32_t gicd_ctrl;
220 u_int n;
221
222 /* Disable the distributor */
223 gicd_write_4(sc, GICD_CTRL, 0);
224
225 /* Wait for register write to complete */
226 while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
227 ;
228
229 /* Clear all INTID enable bits */
230 for (n = 32; n < sc->sc_pic.pic_maxsources; n += 32)
231 gicd_write_4(sc, GICD_ICENABLERn(n / 32), ~0);
232
233 /* Set default priorities to lowest */
234 for (n = 32; n < sc->sc_pic.pic_maxsources; n += 4)
235 gicd_write_4(sc, GICD_IPRIORITYRn(n / 4), ~0);
236
237 /* Set all interrupts to G1NS */
238 for (n = 32; n < sc->sc_pic.pic_maxsources; n += 32) {
239 gicd_write_4(sc, GICD_IGROUPRn(n / 32), ~0);
240 gicd_write_4(sc, GICD_IGRPMODRn(n / 32), 0);
241 }
242
243 /* Set all interrupts level-sensitive by default */
244 for (n = 32; n < sc->sc_pic.pic_maxsources; n += 16)
245 gicd_write_4(sc, GICD_ICFGRn(n / 16), 0);
246
247 /* Wait for register writes to complete */
248 while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
249 ;
250
251 /* Enable Affinity routing and G1NS interrupts */
252 gicd_ctrl = GICD_CTRL_EnableGrp1A | GICD_CTRL_ARE_NS;
253 gicd_write_4(sc, GICD_CTRL, gicd_ctrl);
254 }
255
256 static void
257 gicv3_redist_enable(struct gicv3_softc *sc, struct cpu_info *ci)
258 {
259 uint32_t icfg;
260 u_int n, o;
261
262 /* Clear INTID enable bits */
263 gicr_write_4(sc, ci->ci_gic_redist, GICR_ICENABLER0, ~0);
264
265 /* Wait for register write to complete */
266 while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
267 ;
268
269 /* Set default priorities */
270 for (n = 0; n < 32; n += 4) {
271 uint32_t priority = 0;
272 size_t byte_shift = 0;
273 for (o = 0; o < 4; o++, byte_shift += 8) {
274 struct intrsource * const is = sc->sc_pic.pic_sources[n + o];
275 if (is == NULL)
276 priority |= 0xff << byte_shift;
277 else {
278 const u_int ipriority_val = IPL_TO_PRIORITY(sc, is->is_ipl);
279 priority |= ipriority_val << byte_shift;
280 }
281 }
282 gicr_write_4(sc, ci->ci_gic_redist, GICR_IPRIORITYRn(n / 4), priority);
283 }
284
285 /* Set all interrupts to G1NS */
286 gicr_write_4(sc, ci->ci_gic_redist, GICR_IGROUPR0, ~0);
287 gicr_write_4(sc, ci->ci_gic_redist, GICR_IGRPMODR0, 0);
288
289 /* Restore PPI configs */
290 for (n = 0, icfg = 0; n < 16; n++) {
291 struct intrsource * const is = sc->sc_pic.pic_sources[16 + n];
292 if (is != NULL && is->is_type == IST_EDGE)
293 icfg |= (0x2 << (n * 2));
294 }
295 gicr_write_4(sc, ci->ci_gic_redist, GICR_ICFGRn(1), icfg);
296
297 /* Restore current enable bits */
298 gicr_write_4(sc, ci->ci_gic_redist, GICR_ISENABLER0, sc->sc_enabled_sgippi);
299
300 /* Wait for register write to complete */
301 while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
302 ;
303 }
304
305 static uint64_t
306 gicv3_cpu_identity(void)
307 {
308 u_int aff3, aff2, aff1, aff0;
309
310 const register_t mpidr = cpu_mpidr_aff_read();
311 aff0 = __SHIFTOUT(mpidr, MPIDR_AFF0);
312 aff1 = __SHIFTOUT(mpidr, MPIDR_AFF1);
313 aff2 = __SHIFTOUT(mpidr, MPIDR_AFF2);
314 aff3 = __SHIFTOUT(mpidr, MPIDR_AFF3);
315
316 return __SHIFTIN(aff0, GICR_TYPER_Affinity_Value_Aff0) |
317 __SHIFTIN(aff1, GICR_TYPER_Affinity_Value_Aff1) |
318 __SHIFTIN(aff2, GICR_TYPER_Affinity_Value_Aff2) |
319 __SHIFTIN(aff3, GICR_TYPER_Affinity_Value_Aff3);
320 }
321
322 static u_int
323 gicv3_find_redist(struct gicv3_softc *sc)
324 {
325 uint64_t gicr_typer;
326 u_int n;
327
328 const uint64_t cpu_identity = gicv3_cpu_identity();
329
330 for (n = 0; n < sc->sc_bsh_r_count; n++) {
331 gicr_typer = gicr_read_8(sc, n, GICR_TYPER);
332 if ((gicr_typer & GICR_TYPER_Affinity_Value) == cpu_identity)
333 return n;
334 }
335
336 const u_int aff0 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff0);
337 const u_int aff1 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff1);
338 const u_int aff2 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff2);
339 const u_int aff3 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff3);
340
341 panic("%s: could not find GICv3 redistributor for cpu %d.%d.%d.%d",
342 cpu_name(curcpu()), aff3, aff2, aff1, aff0);
343 }
344
345 static uint64_t
346 gicv3_sgir(struct gicv3_softc *sc)
347 {
348 const uint64_t cpu_identity = gicv3_cpu_identity();
349
350 const u_int aff0 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff0);
351 const u_int aff1 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff1);
352 const u_int aff2 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff2);
353 const u_int aff3 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff3);
354
355 return __SHIFTIN(__BIT(aff0), ICC_SGIR_EL1_TargetList) |
356 __SHIFTIN(aff1, ICC_SGIR_EL1_Aff1) |
357 __SHIFTIN(aff2, ICC_SGIR_EL1_Aff2) |
358 __SHIFTIN(aff3, ICC_SGIR_EL1_Aff3);
359 }
360
361 static void
362 gicv3_cpu_init(struct pic_softc *pic, struct cpu_info *ci)
363 {
364 struct gicv3_softc * const sc = PICTOSOFTC(pic);
365 uint32_t icc_sre, icc_ctlr, gicr_waker;
366
367 ci->ci_gic_redist = gicv3_find_redist(sc);
368 ci->ci_gic_sgir = gicv3_sgir(sc);
369
370 /* Store route to CPU for SPIs */
371 const uint64_t cpu_identity = gicv3_cpu_identity();
372 const u_int aff0 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff0);
373 const u_int aff1 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff1);
374 const u_int aff2 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff2);
375 const u_int aff3 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff3);
376 sc->sc_irouter[cpu_index(ci)] =
377 __SHIFTIN(aff0, GICD_IROUTER_Aff0) |
378 __SHIFTIN(aff1, GICD_IROUTER_Aff1) |
379 __SHIFTIN(aff2, GICD_IROUTER_Aff2) |
380 __SHIFTIN(aff3, GICD_IROUTER_Aff3);
381
382 /* Enable System register access and disable IRQ/FIQ bypass */
383 icc_sre = ICC_SRE_EL1_SRE | ICC_SRE_EL1_DFB | ICC_SRE_EL1_DIB;
384 icc_sre_write(icc_sre);
385
386 /* Mark the connected PE as being awake */
387 gicr_waker = gicr_read_4(sc, ci->ci_gic_redist, GICR_WAKER);
388 gicr_waker &= ~GICR_WAKER_ProcessorSleep;
389 gicr_write_4(sc, ci->ci_gic_redist, GICR_WAKER, gicr_waker);
390 while (gicr_read_4(sc, ci->ci_gic_redist, GICR_WAKER) & GICR_WAKER_ChildrenAsleep)
391 ;
392
393 /* Set initial priority mask */
394 gicv3_set_priority(pic, IPL_HIGH);
395
396 /* Set the binary point field to the minimum value */
397 icc_bpr1_write(0);
398
399 /* Enable group 1 interrupt signaling */
400 icc_igrpen1_write(ICC_IGRPEN_EL1_Enable);
401
402 /* Set EOI mode */
403 icc_ctlr = icc_ctlr_read();
404 icc_ctlr &= ~ICC_CTLR_EL1_EOImode;
405 icc_ctlr_write(icc_ctlr);
406
407 /* Enable redistributor */
408 gicv3_redist_enable(sc, ci);
409
410 /* Allow IRQ exceptions */
411 cpsie(I32_bit);
412 }
413
414 #ifdef MULTIPROCESSOR
415 static void
416 gicv3_ipi_send(struct pic_softc *pic, const kcpuset_t *kcp, u_long ipi)
417 {
418 CPU_INFO_ITERATOR cii;
419 struct cpu_info *ci;
420 uint64_t intid, aff, targets;
421
422 intid = __SHIFTIN(ipi, ICC_SGIR_EL1_INTID);
423 if (kcp == NULL) {
424 /* Interrupts routed to all PEs, excluding "self" */
425 if (ncpu == 1)
426 return;
427 icc_sgi1r_write(intid | ICC_SGIR_EL1_IRM);
428 } else {
429 /* Interrupts routed to specific PEs */
430 aff = 0;
431 targets = 0;
432 for (CPU_INFO_FOREACH(cii, ci)) {
433 if (!kcpuset_isset(kcp, cpu_index(ci)))
434 continue;
435 if ((ci->ci_gic_sgir & ICC_SGIR_EL1_Aff) != aff) {
436 if (targets != 0) {
437 icc_sgi1r_write(intid | aff | targets);
438 targets = 0;
439 }
440 aff = (ci->ci_gic_sgir & ICC_SGIR_EL1_Aff);
441 }
442 targets |= (ci->ci_gic_sgir & ICC_SGIR_EL1_TargetList);
443 }
444 if (targets != 0)
445 icc_sgi1r_write(intid | aff | targets);
446 }
447 }
448
449 static void
450 gicv3_get_affinity(struct pic_softc *pic, size_t irq, kcpuset_t *affinity)
451 {
452 struct gicv3_softc * const sc = PICTOSOFTC(pic);
453 const size_t group = irq / 32;
454 int n;
455
456 kcpuset_zero(affinity);
457 if (group == 0) {
458 /* All CPUs are targets for group 0 (SGI/PPI) */
459 for (n = 0; n < ncpu; n++) {
460 if (sc->sc_irouter[n] != UINT64_MAX)
461 kcpuset_set(affinity, n);
462 }
463 } else {
464 /* Find distributor targets (SPI) */
465 const uint64_t irouter = gicd_read_8(sc, GICD_IROUTER(irq));
466 for (n = 0; n < ncpu; n++) {
467 if (irouter == GICD_IROUTER_Interrupt_Routing_mode ||
468 irouter == sc->sc_irouter[n])
469 kcpuset_set(affinity, n);
470 }
471 }
472 }
473
474 static int
475 gicv3_set_affinity(struct pic_softc *pic, size_t irq, const kcpuset_t *affinity)
476 {
477 struct gicv3_softc * const sc = PICTOSOFTC(pic);
478 const size_t group = irq / 32;
479 uint64_t irouter;
480
481 if (group == 0)
482 return EINVAL;
483
484 const int set = kcpuset_countset(affinity);
485 if (set == ncpu)
486 irouter = GICD_IROUTER_Interrupt_Routing_mode;
487 else if (set == 1)
488 irouter = sc->sc_irouter[kcpuset_ffs(affinity) - 1];
489 else
490 return EINVAL;
491
492 gicd_write_8(sc, GICD_IROUTER(irq), irouter);
493
494 return 0;
495 }
496 #endif
497
498 static const struct pic_ops gicv3_picops = {
499 .pic_unblock_irqs = gicv3_unblock_irqs,
500 .pic_block_irqs = gicv3_block_irqs,
501 .pic_establish_irq = gicv3_establish_irq,
502 .pic_set_priority = gicv3_set_priority,
503 #ifdef MULTIPROCESSOR
504 .pic_cpu_init = gicv3_cpu_init,
505 .pic_ipi_send = gicv3_ipi_send,
506 .pic_get_affinity = gicv3_get_affinity,
507 .pic_set_affinity = gicv3_set_affinity,
508 #endif
509 };
510
511 static void
512 gicv3_lpi_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
513 {
514 struct gicv3_softc * const sc = LPITOSOFTC(pic);
515 int bit;
516
517 while ((bit = ffs(mask)) != 0) {
518 sc->sc_lpiconf.base[irqbase + bit - 1] |= GIC_LPICONF_Enable;
519 mask &= ~__BIT(bit - 1);
520 }
521
522 bus_dmamap_sync(sc->sc_dmat, sc->sc_lpiconf.map, irqbase, 32, BUS_DMASYNC_PREWRITE);
523 }
524
525 static void
526 gicv3_lpi_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
527 {
528 struct gicv3_softc * const sc = LPITOSOFTC(pic);
529 int bit;
530
531 while ((bit = ffs(mask)) != 0) {
532 sc->sc_lpiconf.base[irqbase + bit - 1] &= ~GIC_LPICONF_Enable;
533 mask &= ~__BIT(bit - 1);
534 }
535
536 bus_dmamap_sync(sc->sc_dmat, sc->sc_lpiconf.map, irqbase, 32, BUS_DMASYNC_PREWRITE);
537 }
538
539 static void
540 gicv3_lpi_establish_irq(struct pic_softc *pic, struct intrsource *is)
541 {
542 struct gicv3_softc * const sc = LPITOSOFTC(pic);
543
544 sc->sc_lpiconf.base[is->is_irq] = IPL_TO_LPIPRIO(sc, is->is_ipl) | GIC_LPICONF_Res1;
545
546 bus_dmamap_sync(sc->sc_dmat, sc->sc_lpiconf.map, is->is_irq, 1, BUS_DMASYNC_PREWRITE);
547 }
548
549 static void
550 gicv3_lpi_cpu_init(struct pic_softc *pic, struct cpu_info *ci)
551 {
552 struct gicv3_softc * const sc = LPITOSOFTC(pic);
553 struct gicv3_lpi_callback *cb;
554 uint32_t ctlr;
555
556 /* If physical LPIs are not supported on this redistributor, just return. */
557 const uint64_t typer = gicr_read_8(sc, ci->ci_gic_redist, GICR_TYPER);
558 if ((typer & GICR_TYPER_PLPIS) == 0)
559 return;
560
561 /* Interrupt target address for this CPU, used by ITS when GITS_TYPER.PTA == 0 */
562 sc->sc_processor_id[cpu_index(ci)] = __SHIFTOUT(typer, GICR_TYPER_Processor_Number);
563
564 /* Disable LPIs before making changes */
565 ctlr = gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR);
566 ctlr &= ~GICR_CTLR_Enable_LPIs;
567 gicr_write_4(sc, ci->ci_gic_redist, GICR_CTLR, ctlr);
568 arm_dsb();
569
570 /* Setup the LPI configuration table */
571 const uint64_t propbase = sc->sc_lpiconf.segs[0].ds_addr |
572 __SHIFTIN(ffs(pic->pic_maxsources) - 1, GICR_PROPBASER_IDbits) |
573 __SHIFTIN(GICR_Shareability_NS, GICR_PROPBASER_Shareability) |
574 __SHIFTIN(GICR_Cache_NORMAL_NC, GICR_PROPBASER_InnerCache);
575 gicr_write_8(sc, ci->ci_gic_redist, GICR_PROPBASER, propbase);
576
577 /* Setup the LPI pending table */
578 const uint64_t pendbase = sc->sc_lpipend[cpu_index(ci)].segs[0].ds_addr |
579 __SHIFTIN(GICR_Shareability_NS, GICR_PENDBASER_Shareability) |
580 __SHIFTIN(GICR_Cache_NORMAL_NC, GICR_PENDBASER_InnerCache) |
581 GICR_PENDBASER_PTZ;
582 gicr_write_8(sc, ci->ci_gic_redist, GICR_PENDBASER, pendbase);
583
584 /* Enable LPIs */
585 ctlr = gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR);
586 ctlr |= GICR_CTLR_Enable_LPIs;
587 gicr_write_4(sc, ci->ci_gic_redist, GICR_CTLR, ctlr);
588 arm_dsb();
589
590 /* Setup ITS if present */
591 LIST_FOREACH(cb, &sc->sc_lpi_callbacks, list)
592 cb->cpu_init(cb->priv, ci);
593 }
594
595 #ifdef MULTIPROCESSOR
596 static void
597 gicv3_lpi_get_affinity(struct pic_softc *pic, size_t irq, kcpuset_t *affinity)
598 {
599 struct gicv3_softc * const sc = LPITOSOFTC(pic);
600 struct gicv3_lpi_callback *cb;
601
602 LIST_FOREACH(cb, &sc->sc_lpi_callbacks, list)
603 cb->get_affinity(cb->priv, irq, affinity);
604 }
605
606 static int
607 gicv3_lpi_set_affinity(struct pic_softc *pic, size_t irq, const kcpuset_t *affinity)
608 {
609 struct gicv3_softc * const sc = LPITOSOFTC(pic);
610 struct gicv3_lpi_callback *cb;
611 int error = EINVAL;
612
613 LIST_FOREACH(cb, &sc->sc_lpi_callbacks, list) {
614 error = cb->set_affinity(cb->priv, irq, affinity);
615 if (error)
616 return error;
617 }
618
619 return error;
620 }
621 #endif
622
623 static const struct pic_ops gicv3_lpiops = {
624 .pic_unblock_irqs = gicv3_lpi_unblock_irqs,
625 .pic_block_irqs = gicv3_lpi_block_irqs,
626 .pic_establish_irq = gicv3_lpi_establish_irq,
627 #ifdef MULTIPROCESSOR
628 .pic_cpu_init = gicv3_lpi_cpu_init,
629 .pic_get_affinity = gicv3_lpi_get_affinity,
630 .pic_set_affinity = gicv3_lpi_set_affinity,
631 #endif
632 };
633
634 void
635 gicv3_dma_alloc(struct gicv3_softc *sc, struct gicv3_dma *dma, bus_size_t len, bus_size_t align)
636 {
637 int nsegs, error;
638
639 dma->len = len;
640 error = bus_dmamem_alloc(sc->sc_dmat, dma->len, align, 0, dma->segs, 1, &nsegs, BUS_DMA_WAITOK);
641 if (error)
642 panic("bus_dmamem_alloc failed: %d", error);
643 error = bus_dmamem_map(sc->sc_dmat, dma->segs, nsegs, len, (void **)&dma->base, BUS_DMA_WAITOK);
644 if (error)
645 panic("bus_dmamem_map failed: %d", error);
646 error = bus_dmamap_create(sc->sc_dmat, len, 1, len, 0, BUS_DMA_WAITOK, &dma->map);
647 if (error)
648 panic("bus_dmamap_create failed: %d", error);
649 error = bus_dmamap_load(sc->sc_dmat, dma->map, dma->base, dma->len, NULL, BUS_DMA_WAITOK);
650 if (error)
651 panic("bus_dmamap_load failed: %d", error);
652
653 memset(dma->base, 0, dma->len);
654 bus_dmamap_sync(sc->sc_dmat, dma->map, 0, dma->len, BUS_DMASYNC_PREWRITE);
655 }
656
657 static void
658 gicv3_lpi_init(struct gicv3_softc *sc)
659 {
660 /*
661 * Allocate LPI configuration table
662 */
663 gicv3_dma_alloc(sc, &sc->sc_lpiconf, sc->sc_lpi.pic_maxsources, 0x1000);
664 KASSERT((sc->sc_lpiconf.segs[0].ds_addr & ~GICR_PROPBASER_Physical_Address) == 0);
665
666 /*
667 * Allocate LPI pending tables
668 */
669 const bus_size_t lpipend_sz = sc->sc_lpi.pic_maxsources / NBBY;
670 for (int cpuindex = 0; cpuindex < ncpu; cpuindex++) {
671 gicv3_dma_alloc(sc, &sc->sc_lpipend[cpuindex], lpipend_sz, 0x10000);
672 KASSERT((sc->sc_lpipend[cpuindex].segs[0].ds_addr & ~GICR_PENDBASER_Physical_Address) == 0);
673 }
674 }
675
676 void
677 gicv3_irq_handler(void *frame)
678 {
679 struct cpu_info * const ci = curcpu();
680 struct gicv3_softc * const sc = gicv3_softc;
681 struct pic_softc *pic;
682 const int oldipl = ci->ci_cpl;
683
684 ci->ci_data.cpu_nintr++;
685
686 for (;;) {
687 const uint32_t iar = icc_iar1_read();
688 const uint32_t irq = __SHIFTOUT(iar, ICC_IAR_INTID);
689 if (irq == ICC_IAR_INTID_SPURIOUS)
690 break;
691
692 pic = irq >= GIC_LPI_BASE ? &sc->sc_lpi : &sc->sc_pic;
693 if (irq - pic->pic_irqbase >= pic->pic_maxsources)
694 continue;
695
696 struct intrsource * const is = pic->pic_sources[irq - pic->pic_irqbase];
697 KASSERT(is != NULL);
698
699 const int ipl = is->is_ipl;
700 if (ci->ci_cpl < ipl)
701 pic_set_priority(ci, ipl);
702
703 cpsie(I32_bit);
704 pic_dispatch(is, frame);
705 cpsid(I32_bit);
706
707 icc_eoi1r_write(iar);
708 }
709
710 if (ci->ci_cpl != oldipl)
711 pic_set_priority(ci, oldipl);
712 }
713
714 static int
715 gicv3_detect_pmr_bits(struct gicv3_softc *sc)
716 {
717 const uint32_t opmr = icc_pmr_read();
718 icc_pmr_write(0xff);
719 const uint32_t npmr = icc_pmr_read();
720 icc_pmr_write(opmr);
721
722 return NBBY - (ffs(npmr) - 1);
723 }
724
725 static int
726 gicv3_detect_ipriority_bits(struct gicv3_softc *sc)
727 {
728 const uint32_t oipriorityr = gicd_read_4(sc, GICD_IPRIORITYRn(8));
729 gicd_write_4(sc, GICD_IPRIORITYRn(8), oipriorityr | 0xff);
730 const uint32_t nipriorityr = gicd_read_4(sc, GICD_IPRIORITYRn(8));
731 gicd_write_4(sc, GICD_IPRIORITYRn(8), oipriorityr);
732
733 return NBBY - (ffs(nipriorityr & 0xff) - 1);
734 }
735
736 int
737 gicv3_init(struct gicv3_softc *sc)
738 {
739 const uint32_t gicd_typer = gicd_read_4(sc, GICD_TYPER);
740 const uint32_t gicd_ctrl = gicd_read_4(sc, GICD_CTRL);
741 int n;
742
743 KASSERT(CPU_IS_PRIMARY(curcpu()));
744
745 LIST_INIT(&sc->sc_lpi_callbacks);
746
747 for (n = 0; n < MAXCPUS; n++)
748 sc->sc_irouter[n] = UINT64_MAX;
749
750 sc->sc_priority_shift = 4;
751 sc->sc_pmr_shift = 4;
752
753 if ((gicd_ctrl & GICD_CTRL_DS) == 0) {
754 const int pmr_bits = gicv3_detect_pmr_bits(sc);
755 const int ipriority_bits = gicv3_detect_ipriority_bits(sc);
756
757 if (ipriority_bits != pmr_bits)
758 --sc->sc_priority_shift;
759
760 aprint_verbose_dev(sc->sc_dev, "%d pmr bits, %d ipriority bits\n",
761 pmr_bits, ipriority_bits);
762 } else {
763 aprint_verbose_dev(sc->sc_dev, "security disabled\n");
764 }
765
766 aprint_verbose_dev(sc->sc_dev, "priority shift %d, pmr shift %d\n",
767 sc->sc_priority_shift, sc->sc_pmr_shift);
768
769 sc->sc_pic.pic_ops = &gicv3_picops;
770 sc->sc_pic.pic_maxsources = GICD_TYPER_LINES(gicd_typer);
771 snprintf(sc->sc_pic.pic_name, sizeof(sc->sc_pic.pic_name), "gicv3");
772 #ifdef MULTIPROCESSOR
773 sc->sc_pic.pic_cpus = kcpuset_running;
774 #endif
775 pic_add(&sc->sc_pic, 0);
776
777 if ((gicd_typer & GICD_TYPER_LPIS) != 0) {
778 sc->sc_lpi.pic_ops = &gicv3_lpiops;
779 sc->sc_lpi.pic_maxsources = 8192; /* Min. required by GICv3 spec */
780 snprintf(sc->sc_lpi.pic_name, sizeof(sc->sc_lpi.pic_name), "gicv3-lpi");
781 pic_add(&sc->sc_lpi, GIC_LPI_BASE);
782
783 gicv3_lpi_init(sc);
784 }
785
786 KASSERT(gicv3_softc == NULL);
787 gicv3_softc = sc;
788
789 for (int i = 0; i < sc->sc_bsh_r_count; i++) {
790 const uint64_t gicr_typer = gicr_read_8(sc, i, GICR_TYPER);
791 const u_int aff0 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff0);
792 const u_int aff1 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff1);
793 const u_int aff2 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff2);
794 const u_int aff3 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff3);
795
796 aprint_debug_dev(sc->sc_dev, "redist %d: cpu %d.%d.%d.%d\n",
797 i, aff3, aff2, aff1, aff0);
798 }
799
800 gicv3_dist_enable(sc);
801
802 gicv3_cpu_init(&sc->sc_pic, curcpu());
803 if ((gicd_typer & GICD_TYPER_LPIS) != 0)
804 gicv3_lpi_cpu_init(&sc->sc_lpi, curcpu());
805
806 #ifdef __HAVE_PIC_FAST_SOFTINTS
807 intr_establish_xname(SOFTINT_BIO, IPL_SOFTBIO, IST_MPSAFE | IST_EDGE, pic_handle_softint, (void *)SOFTINT_BIO, "softint bio");
808 intr_establish_xname(SOFTINT_CLOCK, IPL_SOFTCLOCK, IST_MPSAFE | IST_EDGE, pic_handle_softint, (void *)SOFTINT_CLOCK, "softint clock");
809 intr_establish_xname(SOFTINT_NET, IPL_SOFTNET, IST_MPSAFE | IST_EDGE, pic_handle_softint, (void *)SOFTINT_NET, "softint net");
810 intr_establish_xname(SOFTINT_SERIAL, IPL_SOFTSERIAL, IST_MPSAFE | IST_EDGE, pic_handle_softint, (void *)SOFTINT_SERIAL, "softint serial");
811 #endif
812
813 #ifdef MULTIPROCESSOR
814 intr_establish_xname(IPI_AST, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_ast, (void *)-1, "IPI ast");
815 intr_establish_xname(IPI_XCALL, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_xcall, (void *)-1, "IPI xcall");
816 intr_establish_xname(IPI_GENERIC, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_generic, (void *)-1, "IPI generic");
817 intr_establish_xname(IPI_NOP, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_nop, (void *)-1, "IPI nop");
818 intr_establish_xname(IPI_SHOOTDOWN, IPL_SCHED, IST_MPSAFE | IST_EDGE, pic_ipi_shootdown, (void *)-1, "IPI shootdown");
819 #ifdef DDB
820 intr_establish_xname(IPI_DDB, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_ddb, NULL, "IPI ddb");
821 #endif
822 #ifdef __HAVE_PREEMPTION
823 intr_establish_xname(IPI_KPREEMPT, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_kpreempt, (void *)-1, "IPI kpreempt");
824 #endif
825 #endif
826
827 return 0;
828 }
829