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