gicv3.c revision 1.1 1 1.1 jmcneill /* $NetBSD: gicv3.c,v 1.1 2018/08/08 19:02:28 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include "opt_multiprocessor.h"
30 1.1 jmcneill
31 1.1 jmcneill #define _INTR_PRIVATE
32 1.1 jmcneill
33 1.1 jmcneill #include <sys/cdefs.h>
34 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: gicv3.c,v 1.1 2018/08/08 19:02:28 jmcneill Exp $");
35 1.1 jmcneill
36 1.1 jmcneill #include <sys/param.h>
37 1.1 jmcneill #include <sys/kernel.h>
38 1.1 jmcneill #include <sys/bus.h>
39 1.1 jmcneill #include <sys/device.h>
40 1.1 jmcneill #include <sys/intr.h>
41 1.1 jmcneill #include <sys/systm.h>
42 1.1 jmcneill #include <sys/cpu.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <arm/locore.h>
45 1.1 jmcneill #include <arm/armreg.h>
46 1.1 jmcneill
47 1.1 jmcneill #include <arm/cortex/gicv3.h>
48 1.1 jmcneill #include <arm/cortex/gic_reg.h>
49 1.1 jmcneill
50 1.1 jmcneill #define PICTOSOFTC(pic) \
51 1.1 jmcneill ((void *)((uintptr_t)(pic) - offsetof(struct gicv3_softc, sc_pic)))
52 1.1 jmcneill
53 1.1 jmcneill #define IPL_TO_PRIORITY(ipl) ((IPL_HIGH - (ipl)) << 4)
54 1.1 jmcneill
55 1.1 jmcneill static struct gicv3_softc *gicv3_softc;
56 1.1 jmcneill
57 1.1 jmcneill static inline uint32_t
58 1.1 jmcneill gicd_read_4(struct gicv3_softc *sc, bus_size_t reg)
59 1.1 jmcneill {
60 1.1 jmcneill return bus_space_read_4(sc->sc_bst, sc->sc_bsh_d, reg);
61 1.1 jmcneill }
62 1.1 jmcneill
63 1.1 jmcneill static inline void
64 1.1 jmcneill gicd_write_4(struct gicv3_softc *sc, bus_size_t reg, uint32_t val)
65 1.1 jmcneill {
66 1.1 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh_d, reg, val);
67 1.1 jmcneill }
68 1.1 jmcneill
69 1.1 jmcneill static inline void
70 1.1 jmcneill gicd_write_8(struct gicv3_softc *sc, bus_size_t reg, uint64_t val)
71 1.1 jmcneill {
72 1.1 jmcneill bus_space_write_8(sc->sc_bst, sc->sc_bsh_d, reg, val);
73 1.1 jmcneill }
74 1.1 jmcneill
75 1.1 jmcneill static inline uint32_t
76 1.1 jmcneill gicr_read_4(struct gicv3_softc *sc, u_int index, bus_size_t reg)
77 1.1 jmcneill {
78 1.1 jmcneill KASSERT(index < sc->sc_bsh_r_count);
79 1.1 jmcneill return bus_space_read_4(sc->sc_bst, sc->sc_bsh_r[index], reg);
80 1.1 jmcneill }
81 1.1 jmcneill
82 1.1 jmcneill static inline void
83 1.1 jmcneill gicr_write_4(struct gicv3_softc *sc, u_int index, bus_size_t reg, uint32_t val)
84 1.1 jmcneill {
85 1.1 jmcneill KASSERT(index < sc->sc_bsh_r_count);
86 1.1 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh_r[index], reg, val);
87 1.1 jmcneill }
88 1.1 jmcneill
89 1.1 jmcneill static inline uint64_t
90 1.1 jmcneill gicr_read_8(struct gicv3_softc *sc, u_int index, bus_size_t reg)
91 1.1 jmcneill {
92 1.1 jmcneill KASSERT(index < sc->sc_bsh_r_count);
93 1.1 jmcneill return bus_space_read_8(sc->sc_bst, sc->sc_bsh_r[index], reg);
94 1.1 jmcneill }
95 1.1 jmcneill
96 1.1 jmcneill static inline void
97 1.1 jmcneill gicr_write_8(struct gicv3_softc *sc, u_int index, bus_size_t reg, uint64_t val)
98 1.1 jmcneill {
99 1.1 jmcneill KASSERT(index < sc->sc_bsh_r_count);
100 1.1 jmcneill bus_space_write_8(sc->sc_bst, sc->sc_bsh_r[index], reg, val);
101 1.1 jmcneill }
102 1.1 jmcneill
103 1.1 jmcneill static void
104 1.1 jmcneill gicv3_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
105 1.1 jmcneill {
106 1.1 jmcneill struct gicv3_softc * const sc = PICTOSOFTC(pic);
107 1.1 jmcneill struct cpu_info * const ci = curcpu();
108 1.1 jmcneill const u_int group = irqbase / 32;
109 1.1 jmcneill
110 1.1 jmcneill if (group == 0) {
111 1.1 jmcneill sc->sc_enabled_sgippi |= mask;
112 1.1 jmcneill gicr_write_4(sc, ci->ci_gic_redist, GICR_ISENABLER0, mask);
113 1.1 jmcneill while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTRL) & GICR_CTRL_RWP)
114 1.1 jmcneill ;
115 1.1 jmcneill } else {
116 1.1 jmcneill gicd_write_4(sc, GICD_ISENABLERn(group), mask);
117 1.1 jmcneill while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
118 1.1 jmcneill ;
119 1.1 jmcneill }
120 1.1 jmcneill }
121 1.1 jmcneill
122 1.1 jmcneill static void
123 1.1 jmcneill gicv3_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
124 1.1 jmcneill {
125 1.1 jmcneill struct gicv3_softc * const sc = PICTOSOFTC(pic);
126 1.1 jmcneill struct cpu_info * const ci = curcpu();
127 1.1 jmcneill const u_int group = irqbase / 32;
128 1.1 jmcneill
129 1.1 jmcneill if (group == 0) {
130 1.1 jmcneill sc->sc_enabled_sgippi &= ~mask;
131 1.1 jmcneill gicr_write_4(sc, ci->ci_gic_redist, GICR_ICENABLER0, mask);
132 1.1 jmcneill while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTRL) & GICR_CTRL_RWP)
133 1.1 jmcneill ;
134 1.1 jmcneill } else {
135 1.1 jmcneill gicd_write_4(sc, GICD_ICENABLERn(group), mask);
136 1.1 jmcneill while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
137 1.1 jmcneill ;
138 1.1 jmcneill }
139 1.1 jmcneill }
140 1.1 jmcneill
141 1.1 jmcneill static void
142 1.1 jmcneill gicv3_establish_irq(struct pic_softc *pic, struct intrsource *is)
143 1.1 jmcneill {
144 1.1 jmcneill struct gicv3_softc * const sc = PICTOSOFTC(pic);
145 1.1 jmcneill const u_int group = is->is_irq / 32;
146 1.1 jmcneill uint32_t ipriority, icfg;
147 1.1 jmcneill uint64_t irouter;
148 1.1 jmcneill u_int n;
149 1.1 jmcneill
150 1.1 jmcneill const u_int ipriority_shift = (is->is_irq & 0x3) * 8;
151 1.1 jmcneill const u_int icfg_shift = (is->is_irq & 0xf) * 2;
152 1.1 jmcneill
153 1.1 jmcneill if (group == 0) {
154 1.1 jmcneill /* SGIs and PPIs are always MP-safe */
155 1.1 jmcneill is->is_mpsafe = true;
156 1.1 jmcneill
157 1.1 jmcneill /* Update interrupt configuration and priority on all redistributors */
158 1.1 jmcneill for (n = 0; n < sc->sc_bsh_r_count; n++) {
159 1.1 jmcneill icfg = gicr_read_4(sc, n, GICR_ICFGRn(is->is_irq / 16));
160 1.1 jmcneill if (is->is_type == IST_LEVEL)
161 1.1 jmcneill icfg &= ~(0x2 << icfg_shift);
162 1.1 jmcneill if (is->is_type == IST_EDGE)
163 1.1 jmcneill icfg |= (0x2 << icfg_shift);
164 1.1 jmcneill gicr_write_4(sc, n, GICR_ICFGRn(is->is_irq / 16), icfg);
165 1.1 jmcneill
166 1.1 jmcneill ipriority = gicr_read_4(sc, n, GICR_IPRIORITYRn(is->is_irq / 4));
167 1.1 jmcneill ipriority &= ~(0xff << ipriority_shift);
168 1.1 jmcneill ipriority |= (IPL_TO_PRIORITY(is->is_ipl) << ipriority_shift);
169 1.1 jmcneill gicr_write_4(sc, n, GICR_IPRIORITYRn(is->is_irq / 4), ipriority);
170 1.1 jmcneill }
171 1.1 jmcneill } else {
172 1.1 jmcneill if (is->is_mpsafe) {
173 1.1 jmcneill /* Route MP-safe interrupts to all participating PEs */
174 1.1 jmcneill irouter = GICD_IROUTER_Interrupt_Routing_mode;
175 1.1 jmcneill } else {
176 1.1 jmcneill /* Route non-MP-safe interrupts to the primary PE only */
177 1.1 jmcneill irouter = sc->sc_default_irouter;
178 1.1 jmcneill }
179 1.1 jmcneill gicd_write_8(sc, GICD_IROUTER(is->is_irq), irouter);
180 1.1 jmcneill
181 1.1 jmcneill /* Update interrupt configuration */
182 1.1 jmcneill icfg = gicd_read_4(sc, GICD_ICFGRn(is->is_irq / 16));
183 1.1 jmcneill if (is->is_type == IST_LEVEL)
184 1.1 jmcneill icfg &= ~(0x2 << icfg_shift);
185 1.1 jmcneill if (is->is_type == IST_EDGE)
186 1.1 jmcneill icfg |= (0x2 << icfg_shift);
187 1.1 jmcneill gicd_write_4(sc, GICD_ICFGRn(is->is_irq / 16), icfg);
188 1.1 jmcneill
189 1.1 jmcneill /* Update interrupt priority */
190 1.1 jmcneill ipriority = gicd_read_4(sc, GICD_IPRIORITYRn(is->is_irq / 4));
191 1.1 jmcneill ipriority &= ~(0xff << ipriority_shift);
192 1.1 jmcneill ipriority |= (IPL_TO_PRIORITY(is->is_ipl) << ipriority_shift);
193 1.1 jmcneill gicd_write_4(sc, GICD_IPRIORITYRn(is->is_irq / 4), ipriority);
194 1.1 jmcneill }
195 1.1 jmcneill }
196 1.1 jmcneill
197 1.1 jmcneill static void
198 1.1 jmcneill gicv3_set_priority(struct pic_softc *pic, int ipl)
199 1.1 jmcneill {
200 1.1 jmcneill icc_pmr_write(IPL_TO_PRIORITY(ipl));
201 1.1 jmcneill }
202 1.1 jmcneill
203 1.1 jmcneill static void
204 1.1 jmcneill gicv3_dist_enable(struct gicv3_softc *sc)
205 1.1 jmcneill {
206 1.1 jmcneill uint32_t gicd_ctrl;
207 1.1 jmcneill u_int n;
208 1.1 jmcneill
209 1.1 jmcneill /* Disable the distributor */
210 1.1 jmcneill gicd_write_4(sc, GICD_CTRL, 0);
211 1.1 jmcneill
212 1.1 jmcneill /* Wait for register write to complete */
213 1.1 jmcneill while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
214 1.1 jmcneill ;
215 1.1 jmcneill
216 1.1 jmcneill /* Clear all INTID enable bits */
217 1.1 jmcneill for (n = 32; n < sc->sc_pic.pic_maxsources; n += 32)
218 1.1 jmcneill gicd_write_4(sc, GICD_ICENABLERn(n / 32), ~0);
219 1.1 jmcneill
220 1.1 jmcneill /* Set default priorities to lowest */
221 1.1 jmcneill for (n = 32; n < sc->sc_pic.pic_maxsources; n += 4)
222 1.1 jmcneill gicd_write_4(sc, GICD_IPRIORITYRn(n / 4), ~0);
223 1.1 jmcneill
224 1.1 jmcneill /* Set all interrupts to G1NS */
225 1.1 jmcneill for (n = 32; n < sc->sc_pic.pic_maxsources; n += 32) {
226 1.1 jmcneill gicd_write_4(sc, GICD_IGROUPRn(n / 32), ~0);
227 1.1 jmcneill gicd_write_4(sc, GICD_IGRPMODRn(n / 32), 0);
228 1.1 jmcneill }
229 1.1 jmcneill
230 1.1 jmcneill /* Set all interrupts level-sensitive by default */
231 1.1 jmcneill for (n = 32; n < sc->sc_pic.pic_maxsources; n += 16)
232 1.1 jmcneill gicd_write_4(sc, GICD_ICFGRn(n / 16), 0);
233 1.1 jmcneill
234 1.1 jmcneill /* Wait for register writes to complete */
235 1.1 jmcneill while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
236 1.1 jmcneill ;
237 1.1 jmcneill
238 1.1 jmcneill /* Enable Affinity routing and G1NS interrupts */
239 1.1 jmcneill gicd_ctrl = GICD_CTRL_EnableGrp1NS | GICD_CTRL_Enable | GICD_CTRL_ARE_NS;
240 1.1 jmcneill gicd_write_4(sc, GICD_CTRL, gicd_ctrl);
241 1.1 jmcneill }
242 1.1 jmcneill
243 1.1 jmcneill static void
244 1.1 jmcneill gicv3_redist_enable(struct gicv3_softc *sc, struct cpu_info *ci)
245 1.1 jmcneill {
246 1.1 jmcneill uint32_t icfg;
247 1.1 jmcneill u_int n, o;
248 1.1 jmcneill
249 1.1 jmcneill /* Clear INTID enable bits */
250 1.1 jmcneill gicr_write_4(sc, ci->ci_gic_redist, GICR_ICENABLER0, ~0);
251 1.1 jmcneill
252 1.1 jmcneill /* Wait for register write to complete */
253 1.1 jmcneill while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTRL) & GICR_CTRL_RWP)
254 1.1 jmcneill ;
255 1.1 jmcneill
256 1.1 jmcneill /* Set default priorities */
257 1.1 jmcneill for (n = 0; n < 32; n += 4) {
258 1.1 jmcneill uint32_t priority = 0;
259 1.1 jmcneill size_t byte_shift = 0;
260 1.1 jmcneill for (o = 0; o < 4; o++, byte_shift += 8) {
261 1.1 jmcneill struct intrsource * const is = sc->sc_pic.pic_sources[n + o];
262 1.1 jmcneill if (is == NULL)
263 1.1 jmcneill priority |= 0xff << byte_shift;
264 1.1 jmcneill else
265 1.1 jmcneill priority |= IPL_TO_PRIORITY(is->is_ipl) << byte_shift;
266 1.1 jmcneill }
267 1.1 jmcneill gicr_write_4(sc, ci->ci_gic_redist, GICR_IPRIORITYRn(n / 4), priority);
268 1.1 jmcneill }
269 1.1 jmcneill
270 1.1 jmcneill /* Set all interrupts to G1NS */
271 1.1 jmcneill gicr_write_4(sc, ci->ci_gic_redist, GICR_IGROUPR0, ~0);
272 1.1 jmcneill gicr_write_4(sc, ci->ci_gic_redist, GICR_IGRPMODR0, 0);
273 1.1 jmcneill
274 1.1 jmcneill /* Restore PPI configs */
275 1.1 jmcneill for (n = 0, icfg = 0; n < 16; n++) {
276 1.1 jmcneill struct intrsource * const is = sc->sc_pic.pic_sources[16 + n];
277 1.1 jmcneill if (is != NULL && is->is_type == IST_EDGE)
278 1.1 jmcneill icfg |= (0x2 << (n * 2));
279 1.1 jmcneill }
280 1.1 jmcneill gicr_write_4(sc, ci->ci_gic_redist, GICR_ICFGRn(1), icfg);
281 1.1 jmcneill
282 1.1 jmcneill /* Restore current enable bits */
283 1.1 jmcneill gicr_write_4(sc, ci->ci_gic_redist, GICR_ISENABLER0, sc->sc_enabled_sgippi);
284 1.1 jmcneill
285 1.1 jmcneill /* Wait for register write to complete */
286 1.1 jmcneill while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTRL) & GICR_CTRL_RWP)
287 1.1 jmcneill ;
288 1.1 jmcneill }
289 1.1 jmcneill
290 1.1 jmcneill static uint64_t
291 1.1 jmcneill gicv3_cpu_identity(void)
292 1.1 jmcneill {
293 1.1 jmcneill u_int aff3, aff2, aff1, aff0;
294 1.1 jmcneill
295 1.1 jmcneill #ifdef __aarch64__
296 1.1 jmcneill const register_t mpidr = reg_mpidr_el1_read();
297 1.1 jmcneill aff0 = __SHIFTOUT(mpidr, MPIDR_AFF0);
298 1.1 jmcneill aff1 = __SHIFTOUT(mpidr, MPIDR_AFF1);
299 1.1 jmcneill aff2 = __SHIFTOUT(mpidr, MPIDR_AFF2);
300 1.1 jmcneill aff3 = __SHIFTOUT(mpidr, MPIDR_AFF3);
301 1.1 jmcneill #else
302 1.1 jmcneill const register_t mpidr = armreg_mpidr_read();
303 1.1 jmcneill aff0 = __SHIFTOUT(mpidr, MPIDR_AFF0);
304 1.1 jmcneill aff1 = __SHIFTOUT(mpidr, MPIDR_AFF1);
305 1.1 jmcneill aff2 = __SHIFTOUT(mpidr, MPIDR_AFF2);
306 1.1 jmcneill aff3 = 0;
307 1.1 jmcneill #endif
308 1.1 jmcneill
309 1.1 jmcneill return __SHIFTIN(aff0, GICR_TYPER_Affinity_Value_Aff0) |
310 1.1 jmcneill __SHIFTIN(aff1, GICR_TYPER_Affinity_Value_Aff1) |
311 1.1 jmcneill __SHIFTIN(aff2, GICR_TYPER_Affinity_Value_Aff2) |
312 1.1 jmcneill __SHIFTIN(aff3, GICR_TYPER_Affinity_Value_Aff3);
313 1.1 jmcneill }
314 1.1 jmcneill
315 1.1 jmcneill static u_int
316 1.1 jmcneill gicv3_find_redist(struct gicv3_softc *sc)
317 1.1 jmcneill {
318 1.1 jmcneill uint64_t gicr_typer;
319 1.1 jmcneill u_int n;
320 1.1 jmcneill
321 1.1 jmcneill const uint64_t cpu_identity = gicv3_cpu_identity();
322 1.1 jmcneill
323 1.1 jmcneill for (n = 0; n < sc->sc_bsh_r_count; n++) {
324 1.1 jmcneill gicr_typer = gicr_read_8(sc, n, GICR_TYPER);
325 1.1 jmcneill if ((gicr_typer & GICR_TYPER_Affinity_Value) == cpu_identity)
326 1.1 jmcneill return n;
327 1.1 jmcneill }
328 1.1 jmcneill
329 1.1 jmcneill const u_int aff0 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff0);
330 1.1 jmcneill const u_int aff1 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff1);
331 1.1 jmcneill const u_int aff2 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff2);
332 1.1 jmcneill const u_int aff3 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff3);
333 1.1 jmcneill
334 1.1 jmcneill panic("%s: could not find GICv3 redistributor for cpu %d.%d.%d.%d",
335 1.1 jmcneill cpu_name(curcpu()), aff3, aff2, aff1, aff0);
336 1.1 jmcneill }
337 1.1 jmcneill
338 1.1 jmcneill static uint64_t
339 1.1 jmcneill gicv3_sgir(struct gicv3_softc *sc)
340 1.1 jmcneill {
341 1.1 jmcneill const uint64_t cpu_identity = gicv3_cpu_identity();
342 1.1 jmcneill
343 1.1 jmcneill const u_int aff0 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff0);
344 1.1 jmcneill const u_int aff1 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff1);
345 1.1 jmcneill const u_int aff2 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff2);
346 1.1 jmcneill const u_int aff3 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff3);
347 1.1 jmcneill
348 1.1 jmcneill return __SHIFTIN(__BIT(aff0), ICC_SGIR_EL1_TargetList) |
349 1.1 jmcneill __SHIFTIN(aff1, ICC_SGIR_EL1_Aff1) |
350 1.1 jmcneill __SHIFTIN(aff2, ICC_SGIR_EL1_Aff2) |
351 1.1 jmcneill __SHIFTIN(aff3, ICC_SGIR_EL1_Aff3);
352 1.1 jmcneill }
353 1.1 jmcneill
354 1.1 jmcneill static void
355 1.1 jmcneill gicv3_cpu_init(struct pic_softc *pic, struct cpu_info *ci)
356 1.1 jmcneill {
357 1.1 jmcneill struct gicv3_softc * const sc = PICTOSOFTC(pic);
358 1.1 jmcneill uint32_t icc_sre, icc_ctlr, gicr_waker;
359 1.1 jmcneill
360 1.1 jmcneill ci->ci_gic_redist = gicv3_find_redist(sc);
361 1.1 jmcneill ci->ci_gic_sgir = gicv3_sgir(sc);
362 1.1 jmcneill
363 1.1 jmcneill if (CPU_IS_PRIMARY(ci)) {
364 1.1 jmcneill /* Store route to primary CPU for non-MPSAFE SPIs */
365 1.1 jmcneill const uint64_t cpu_identity = gicv3_cpu_identity();
366 1.1 jmcneill const u_int aff0 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff0);
367 1.1 jmcneill const u_int aff1 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff1);
368 1.1 jmcneill const u_int aff2 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff2);
369 1.1 jmcneill const u_int aff3 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff3);
370 1.1 jmcneill sc->sc_default_irouter =
371 1.1 jmcneill __SHIFTIN(aff0, GICD_IROUTER_Aff0) |
372 1.1 jmcneill __SHIFTIN(aff1, GICD_IROUTER_Aff1) |
373 1.1 jmcneill __SHIFTIN(aff2, GICD_IROUTER_Aff2) |
374 1.1 jmcneill __SHIFTIN(aff3, GICD_IROUTER_Aff3);
375 1.1 jmcneill }
376 1.1 jmcneill
377 1.1 jmcneill /* Enable System register access and disable IRQ/FIQ bypass */
378 1.1 jmcneill icc_sre = ICC_SRE_EL1_SRE | ICC_SRE_EL1_DFB | ICC_SRE_EL1_DIB;
379 1.1 jmcneill icc_sre_write(icc_sre);
380 1.1 jmcneill
381 1.1 jmcneill /* Mark the connected PE as being awake */
382 1.1 jmcneill gicr_waker = gicr_read_4(sc, ci->ci_gic_redist, GICR_WAKER);
383 1.1 jmcneill gicr_waker &= ~GICR_WAKER_ProcessorSleep;
384 1.1 jmcneill gicr_write_4(sc, ci->ci_gic_redist, GICR_WAKER, gicr_waker);
385 1.1 jmcneill while (gicr_read_4(sc, ci->ci_gic_redist, GICR_WAKER) & GICR_WAKER_ChildrenAsleep)
386 1.1 jmcneill ;
387 1.1 jmcneill
388 1.1 jmcneill /* Set initial priority mask */
389 1.1 jmcneill icc_pmr_write(IPL_TO_PRIORITY(IPL_HIGH));
390 1.1 jmcneill
391 1.1 jmcneill /* Disable preemption */
392 1.1 jmcneill const uint32_t icc_bpr = __SHIFTIN(0x7, ICC_BPR_EL1_BinaryPoint);
393 1.1 jmcneill icc_bpr1_write(icc_bpr);
394 1.1 jmcneill
395 1.1 jmcneill /* Enable group 1 interrupt signaling */
396 1.1 jmcneill icc_igrpen1_write(ICC_IGRPEN_EL1_Enable);
397 1.1 jmcneill
398 1.1 jmcneill /* Set EOI mode */
399 1.1 jmcneill icc_ctlr = icc_ctlr_read();
400 1.1 jmcneill icc_ctlr &= ~ICC_CTLR_EL1_EOImode;
401 1.1 jmcneill icc_ctlr_write(icc_ctlr);
402 1.1 jmcneill
403 1.1 jmcneill /* Enable redistributor */
404 1.1 jmcneill gicv3_redist_enable(sc, ci);
405 1.1 jmcneill
406 1.1 jmcneill /* Allow IRQ exceptions */
407 1.1 jmcneill cpsie(I32_bit);
408 1.1 jmcneill }
409 1.1 jmcneill
410 1.1 jmcneill #ifdef MULTIPROCESSOR
411 1.1 jmcneill static void
412 1.1 jmcneill gicv3_ipi_send(struct pic_softc *pic, const kcpuset_t *kcp, u_long ipi)
413 1.1 jmcneill {
414 1.1 jmcneill CPU_INFO_ITERATOR cii;
415 1.1 jmcneill struct cpu_info *ci;
416 1.1 jmcneill uint64_t intid, aff, targets;
417 1.1 jmcneill
418 1.1 jmcneill intid = __SHIFTIN(ipi, ICC_SGIR_EL1_INTID);
419 1.1 jmcneill if (kcp == NULL) {
420 1.1 jmcneill /* Interrupts routed to all PEs, excluding "self" */
421 1.1 jmcneill if (ncpu == 1)
422 1.1 jmcneill return;
423 1.1 jmcneill icc_sgi1r_write(intid | ICC_SGIR_EL1_IRM);
424 1.1 jmcneill } else {
425 1.1 jmcneill /* Interrupts routed to specific PEs */
426 1.1 jmcneill aff = 0;
427 1.1 jmcneill targets = 0;
428 1.1 jmcneill for (CPU_INFO_FOREACH(cii, ci)) {
429 1.1 jmcneill if ((ci->ci_gic_sgir & ICC_SGIR_EL1_Aff) != aff) {
430 1.1 jmcneill if (targets != 0) {
431 1.1 jmcneill icc_sgi1r_write(intid | aff | targets);
432 1.1 jmcneill targets = 0;
433 1.1 jmcneill }
434 1.1 jmcneill aff = (ci->ci_gic_sgir & ICC_SGIR_EL1_Aff);
435 1.1 jmcneill }
436 1.1 jmcneill targets |= (ci->ci_gic_sgir & ICC_SGIR_EL1_TargetList);
437 1.1 jmcneill }
438 1.1 jmcneill if (targets != 0)
439 1.1 jmcneill icc_sgi1r_write(intid | aff | targets);
440 1.1 jmcneill }
441 1.1 jmcneill }
442 1.1 jmcneill #endif
443 1.1 jmcneill
444 1.1 jmcneill static const struct pic_ops gicv3_picops = {
445 1.1 jmcneill .pic_unblock_irqs = gicv3_unblock_irqs,
446 1.1 jmcneill .pic_block_irqs = gicv3_block_irqs,
447 1.1 jmcneill .pic_establish_irq = gicv3_establish_irq,
448 1.1 jmcneill .pic_set_priority = gicv3_set_priority,
449 1.1 jmcneill #ifdef MULTIPROCESSOR
450 1.1 jmcneill .pic_cpu_init = gicv3_cpu_init,
451 1.1 jmcneill .pic_ipi_send = gicv3_ipi_send,
452 1.1 jmcneill #endif
453 1.1 jmcneill };
454 1.1 jmcneill
455 1.1 jmcneill void
456 1.1 jmcneill gicv3_irq_handler(void *frame)
457 1.1 jmcneill {
458 1.1 jmcneill struct cpu_info * const ci = curcpu();
459 1.1 jmcneill struct gicv3_softc * const sc = gicv3_softc;
460 1.1 jmcneill const int oldipl = ci->ci_cpl;
461 1.1 jmcneill
462 1.1 jmcneill ci->ci_data.cpu_nintr++;
463 1.1 jmcneill
464 1.1 jmcneill for (;;) {
465 1.1 jmcneill const uint32_t iar = icc_iar1_read();
466 1.1 jmcneill const uint32_t irq = __SHIFTOUT(iar, ICC_IAR_INTID);
467 1.1 jmcneill if (irq == ICC_IAR_INTID_SPURIOUS)
468 1.1 jmcneill break;
469 1.1 jmcneill
470 1.1 jmcneill if (irq >= sc->sc_pic.pic_maxsources)
471 1.1 jmcneill continue;
472 1.1 jmcneill
473 1.1 jmcneill struct intrsource * const is = sc->sc_pic.pic_sources[irq];
474 1.1 jmcneill KASSERT(is != NULL);
475 1.1 jmcneill
476 1.1 jmcneill const int ipl = is->is_ipl;
477 1.1 jmcneill if (ci->ci_cpl != ipl)
478 1.1 jmcneill pic_set_priority(ci, ipl);
479 1.1 jmcneill
480 1.1 jmcneill cpsie(I32_bit);
481 1.1 jmcneill pic_dispatch(is, frame);
482 1.1 jmcneill cpsid(I32_bit);
483 1.1 jmcneill
484 1.1 jmcneill icc_eoi1r_write(iar);
485 1.1 jmcneill }
486 1.1 jmcneill
487 1.1 jmcneill if (ci->ci_cpl != oldipl)
488 1.1 jmcneill pic_set_priority(ci, oldipl);
489 1.1 jmcneill }
490 1.1 jmcneill
491 1.1 jmcneill int
492 1.1 jmcneill gicv3_init(struct gicv3_softc *sc)
493 1.1 jmcneill {
494 1.1 jmcneill const uint32_t gicd_typer = gicd_read_4(sc, GICD_TYPER);
495 1.1 jmcneill
496 1.1 jmcneill KASSERT(CPU_IS_PRIMARY(curcpu()));
497 1.1 jmcneill
498 1.1 jmcneill sc->sc_pic.pic_ops = &gicv3_picops;
499 1.1 jmcneill sc->sc_pic.pic_maxsources = GICD_TYPER_LINES(gicd_typer);
500 1.1 jmcneill snprintf(sc->sc_pic.pic_name, sizeof(sc->sc_pic.pic_name), "gicv3");
501 1.1 jmcneill #ifdef MULTIPROCESSOR
502 1.1 jmcneill sc->sc_pic.pic_cpus = kcpuset_running;
503 1.1 jmcneill #endif
504 1.1 jmcneill pic_add(&sc->sc_pic, 0);
505 1.1 jmcneill
506 1.1 jmcneill KASSERT(gicv3_softc == NULL);
507 1.1 jmcneill gicv3_softc = sc;
508 1.1 jmcneill
509 1.1 jmcneill for (int i = 0; i < sc->sc_bsh_r_count; i++) {
510 1.1 jmcneill const uint64_t gicr_typer = gicr_read_8(sc, i, GICR_TYPER);
511 1.1 jmcneill const u_int aff0 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff0);
512 1.1 jmcneill const u_int aff1 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff1);
513 1.1 jmcneill const u_int aff2 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff2);
514 1.1 jmcneill const u_int aff3 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff3);
515 1.1 jmcneill
516 1.1 jmcneill aprint_debug_dev(sc->sc_dev, "redist %d: cpu %d.%d.%d.%d\n",
517 1.1 jmcneill i, aff3, aff2, aff1, aff0);
518 1.1 jmcneill }
519 1.1 jmcneill
520 1.1 jmcneill gicv3_dist_enable(sc);
521 1.1 jmcneill
522 1.1 jmcneill gicv3_cpu_init(&sc->sc_pic, curcpu());
523 1.1 jmcneill
524 1.1 jmcneill #ifdef __HAVE_PIC_FAST_SOFTINTS
525 1.1 jmcneill intr_establish(SOFTINT_BIO, IPL_SOFTBIO, IST_MPSAFE | IST_EDGE, pic_handle_softint, (void *)SOFTINT_BIO);
526 1.1 jmcneill intr_establish(SOFTINT_CLOCK, IPL_SOFTCLOCK, IST_MPSAFE | IST_EDGE, pic_handle_softint, (void *)SOFTINT_CLOCK);
527 1.1 jmcneill intr_establish(SOFTINT_NET, IPL_SOFTNET, IST_MPSAFE | IST_EDGE, pic_handle_softint, (void *)SOFTINT_NET);
528 1.1 jmcneill intr_establish(SOFTINT_SERIAL, IPL_SOFTSERIAL, IST_MPSAFE | IST_EDGE, pic_handle_softint, (void *)SOFTINT_SERIAL);
529 1.1 jmcneill #endif
530 1.1 jmcneill
531 1.1 jmcneill #ifdef MULTIPROCESSOR
532 1.1 jmcneill intr_establish(IPI_AST, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_ast, (void *)-1);
533 1.1 jmcneill intr_establish(IPI_XCALL, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_xcall, (void *)-1);
534 1.1 jmcneill intr_establish(IPI_GENERIC, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_generic, (void *)-1);
535 1.1 jmcneill intr_establish(IPI_NOP, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_nop, (void *)-1);
536 1.1 jmcneill intr_establish(IPI_SHOOTDOWN, IPL_SCHED, IST_MPSAFE | IST_EDGE, pic_ipi_shootdown, (void *)-1);
537 1.1 jmcneill #ifdef DDB
538 1.1 jmcneill intr_establish(IPI_DDB, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_ddb, NULL);
539 1.1 jmcneill #endif
540 1.1 jmcneill #ifdef __HAVE_PREEMPTION
541 1.1 jmcneill intr_establish(IPI_KPREEMPT, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_kpreempt, (void *)-1);
542 1.1 jmcneill #endif
543 1.1 jmcneill #endif
544 1.1 jmcneill
545 1.1 jmcneill return 0;
546 1.1 jmcneill }
547