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