e500_intr.c revision 1.25 1 /* $NetBSD: e500_intr.c,v 1.25 2014/12/19 03:51:38 nonaka Exp $ */
2 /*-
3 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
8 * Agency and which was developed by Matt Thomas of 3am Software Foundry.
9 *
10 * This material is based upon work supported by the Defense Advanced Research
11 * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
12 * Contract No. N66001-09-C-2073.
13 * Approved for Public Release, Distribution Unlimited
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include "opt_mpc85xx.h"
38
39 #define __INTR_PRIVATE
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: e500_intr.c,v 1.25 2014/12/19 03:51:38 nonaka Exp $");
43
44 #include <sys/param.h>
45 #include <sys/proc.h>
46 #include <sys/intr.h>
47 #include <sys/cpu.h>
48 #include <sys/kmem.h>
49 #include <sys/atomic.h>
50 #include <sys/bus.h>
51 #include <sys/xcall.h>
52 #include <sys/ipi.h>
53 #include <sys/bitops.h>
54
55 #include <uvm/uvm_extern.h>
56
57 #ifdef __HAVE_FAST_SOFTINTS
58 #include <powerpc/softint.h>
59 #endif
60
61 #include <powerpc/spr.h>
62 #include <powerpc/booke/spr.h>
63
64 #include <powerpc/booke/cpuvar.h>
65 #include <powerpc/booke/e500reg.h>
66 #include <powerpc/booke/e500var.h>
67 #include <powerpc/booke/openpicreg.h>
68
69 #define IPL2CTPR(ipl) ((ipl) + 15 - IPL_HIGH)
70 #define CTPR2IPL(ctpr) ((ctpr) - (15 - IPL_HIGH))
71
72 #define IST_PERCPU_P(ist) ((ist) >= IST_TIMER)
73
74 struct e500_intr_irq_info {
75 bus_addr_t irq_vpr;
76 bus_addr_t irq_dr;
77 u_int irq_vector;
78 };
79
80 struct intr_source {
81 int (*is_func)(void *);
82 void *is_arg;
83 int8_t is_ipl;
84 uint8_t is_ist;
85 uint8_t is_irq;
86 bus_size_t is_vpr;
87 bus_size_t is_dr;
88 };
89
90 #define INTR_SOURCE_INITIALIZER \
91 { .is_func = e500_intr_spurious, .is_arg = NULL, \
92 .is_irq = -1, .is_ipl = IPL_NONE, .is_ist = IST_NONE, }
93
94 struct e500_intr_name {
95 uint8_t in_irq;
96 const char in_name[15];
97 };
98
99 static const struct e500_intr_name e500_onchip_intr_names[] = {
100 { ISOURCE_L2, "l2" },
101 { ISOURCE_ECM, "ecm" },
102 { ISOURCE_DDR, "ddr" },
103 { ISOURCE_LBC, "lbc" },
104 { ISOURCE_DMA_CHAN1, "dma-chan1" },
105 { ISOURCE_DMA_CHAN2, "dma-chan2" },
106 { ISOURCE_DMA_CHAN3, "dma-chan3" },
107 { ISOURCE_DMA_CHAN4, "dma-chan4" },
108 { ISOURCE_PCI1, "pci1" },
109 { ISOURCE_PCIEX2, "pcie2" },
110 { ISOURCE_PCIEX , "pcie1" },
111 { ISOURCE_PCIEX3, "pcie3" },
112 { ISOURCE_USB1, "usb1" },
113 { ISOURCE_ETSEC1_TX, "etsec1-tx" },
114 { ISOURCE_ETSEC1_RX, "etsec1-rx" },
115 { ISOURCE_ETSEC3_TX, "etsec3-tx" },
116 { ISOURCE_ETSEC3_RX, "etsec3-rx" },
117 { ISOURCE_ETSEC3_ERR, "etsec3-err" },
118 { ISOURCE_ETSEC1_ERR, "etsec1-err" },
119 { ISOURCE_ETSEC2_TX, "etsec2-tx" },
120 { ISOURCE_ETSEC2_RX, "etsec2-rx" },
121 { ISOURCE_ETSEC4_TX, "etsec4-tx" },
122 { ISOURCE_ETSEC4_RX, "etsec4-rx" },
123 { ISOURCE_ETSEC4_ERR, "etsec4-err" },
124 { ISOURCE_ETSEC2_ERR, "etsec2-err" },
125 { ISOURCE_DUART, "duart" },
126 { ISOURCE_I2C, "i2c" },
127 { ISOURCE_PERFMON, "perfmon" },
128 { ISOURCE_SECURITY1, "sec1" },
129 { ISOURCE_GPIO, "gpio" },
130 { ISOURCE_SRIO_EWPU, "srio-ewpu" },
131 { ISOURCE_SRIO_ODBELL, "srio-odbell" },
132 { ISOURCE_SRIO_IDBELL, "srio-idbell" },
133 { ISOURCE_SRIO_OMU1, "srio-omu1" },
134 { ISOURCE_SRIO_IMU1, "srio-imu1" },
135 { ISOURCE_SRIO_OMU2, "srio-omu2" },
136 { ISOURCE_SRIO_IMU2, "srio-imu2" },
137 { ISOURCE_SECURITY2, "sec2" },
138 { ISOURCE_SPI, "spi" },
139 { ISOURCE_ETSEC1_PTP, "etsec1-ptp" },
140 { ISOURCE_ETSEC2_PTP, "etsec2-ptp" },
141 { ISOURCE_ETSEC3_PTP, "etsec3-ptp" },
142 { ISOURCE_ETSEC4_PTP, "etsec4-ptp" },
143 { ISOURCE_ESDHC, "esdhc" },
144 { 0, "" },
145 };
146
147 const struct e500_intr_name default_external_intr_names[] = {
148 { 0, "" },
149 };
150
151 static const struct e500_intr_name e500_msigroup_intr_names[] = {
152 { 0, "msigroup0" },
153 { 1, "msigroup1" },
154 { 2, "msigroup2" },
155 { 3, "msigroup3" },
156 { 4, "msigroup4" },
157 { 5, "msigroup5" },
158 { 6, "msigroup6" },
159 { 7, "msigroup7" },
160 { 0, "" },
161 };
162
163 static const struct e500_intr_name e500_timer_intr_names[] = {
164 { 0, "timer0" },
165 { 1, "timer1" },
166 { 2, "timer2" },
167 { 3, "timer3" },
168 { 0, "" },
169 };
170
171 static const struct e500_intr_name e500_ipi_intr_names[] = {
172 { 0, "ipi0" },
173 { 1, "ipi1" },
174 { 2, "ipi2" },
175 { 3, "ipi3" },
176 { 0, "" },
177 };
178
179 static const struct e500_intr_name e500_mi_intr_names[] = {
180 { 0, "mi0" },
181 { 1, "mi1" },
182 { 2, "mi2" },
183 { 3, "mi3" },
184 { 0, "" },
185 };
186
187 struct e500_intr_info {
188 u_int ii_external_sources;
189 uint32_t ii_onchip_bitmap[2];
190 u_int ii_onchip_sources;
191 u_int ii_msigroup_sources;
192 u_int ii_ipi_sources; /* per-cpu */
193 u_int ii_timer_sources; /* per-cpu */
194 u_int ii_mi_sources; /* per-cpu */
195 u_int ii_percpu_sources;
196 const struct e500_intr_name *ii_external_intr_names;
197 const struct e500_intr_name *ii_onchip_intr_names;
198 u_int8_t ii_ist_vectors[IST_MAX+1];
199 };
200
201 static kmutex_t e500_intr_lock __cacheline_aligned;
202 static struct e500_intr_info e500_intr_info;
203
204 #define INTR_INFO_DECL(lc_chip, UC_CHIP) \
205 static const struct e500_intr_info lc_chip##_intr_info = { \
206 .ii_external_sources = UC_CHIP ## _EXTERNALSOURCES, \
207 .ii_onchip_bitmap = UC_CHIP ## _ONCHIPBITMAP, \
208 .ii_onchip_sources = UC_CHIP ## _ONCHIPSOURCES, \
209 .ii_msigroup_sources = UC_CHIP ## _MSIGROUPSOURCES, \
210 .ii_timer_sources = UC_CHIP ## _TIMERSOURCES, \
211 .ii_ipi_sources = UC_CHIP ## _IPISOURCES, \
212 .ii_mi_sources = UC_CHIP ## _MISOURCES, \
213 .ii_percpu_sources = UC_CHIP ## _TIMERSOURCES \
214 + UC_CHIP ## _IPISOURCES + UC_CHIP ## _MISOURCES, \
215 .ii_external_intr_names = lc_chip ## _external_intr_names, \
216 .ii_onchip_intr_names = lc_chip ## _onchip_intr_names, \
217 .ii_ist_vectors = { \
218 [IST_NONE] = ~0, \
219 [IST_EDGE] = 0, \
220 [IST_LEVEL_LOW] = 0, \
221 [IST_LEVEL_HIGH] = 0, \
222 [IST_PULSE] = 0, \
223 [IST_ONCHIP] = UC_CHIP ## _EXTERNALSOURCES, \
224 [IST_MSIGROUP] = UC_CHIP ## _EXTERNALSOURCES \
225 + UC_CHIP ## _ONCHIPSOURCES, \
226 [IST_TIMER] = UC_CHIP ## _EXTERNALSOURCES \
227 + UC_CHIP ## _ONCHIPSOURCES \
228 + UC_CHIP ## _MSIGROUPSOURCES, \
229 [IST_IPI] = UC_CHIP ## _EXTERNALSOURCES \
230 + UC_CHIP ## _ONCHIPSOURCES \
231 + UC_CHIP ## _MSIGROUPSOURCES \
232 + UC_CHIP ## _TIMERSOURCES, \
233 [IST_MI] = UC_CHIP ## _EXTERNALSOURCES \
234 + UC_CHIP ## _ONCHIPSOURCES \
235 + UC_CHIP ## _MSIGROUPSOURCES \
236 + UC_CHIP ## _TIMERSOURCES \
237 + UC_CHIP ## _IPISOURCES, \
238 [IST_MAX] = UC_CHIP ## _EXTERNALSOURCES \
239 + UC_CHIP ## _ONCHIPSOURCES \
240 + UC_CHIP ## _MSIGROUPSOURCES \
241 + UC_CHIP ## _TIMERSOURCES \
242 + UC_CHIP ## _IPISOURCES \
243 + UC_CHIP ## _MISOURCES, \
244 }, \
245 }
246
247 #ifdef MPC8536
248 #define mpc8536_external_intr_names default_external_intr_names
249 const struct e500_intr_name mpc8536_onchip_intr_names[] = {
250 { ISOURCE_SATA2, "sata2" },
251 { ISOURCE_USB2, "usb2" },
252 { ISOURCE_USB3, "usb3" },
253 { ISOURCE_SATA1, "sata1" },
254 { 0, "" },
255 };
256
257 INTR_INFO_DECL(mpc8536, MPC8536);
258 #endif
259
260 #ifdef MPC8544
261 #define mpc8544_external_intr_names default_external_intr_names
262 const struct e500_intr_name mpc8544_onchip_intr_names[] = {
263 { 0, "" },
264 };
265
266 INTR_INFO_DECL(mpc8544, MPC8544);
267 #endif
268 #ifdef MPC8548
269 #define mpc8548_external_intr_names default_external_intr_names
270 const struct e500_intr_name mpc8548_onchip_intr_names[] = {
271 { ISOURCE_PCI1, "pci1" },
272 { ISOURCE_PCI2, "pci2" },
273 { 0, "" },
274 };
275
276 INTR_INFO_DECL(mpc8548, MPC8548);
277 #endif
278 #ifdef MPC8555
279 #define mpc8555_external_intr_names default_external_intr_names
280 const struct e500_intr_name mpc8555_onchip_intr_names[] = {
281 { ISOURCE_PCI2, "pci2" },
282 { ISOURCE_CPM, "CPM" },
283 { 0, "" },
284 };
285
286 INTR_INFO_DECL(mpc8555, MPC8555);
287 #endif
288 #ifdef MPC8568
289 #define mpc8568_external_intr_names default_external_intr_names
290 const struct e500_intr_name mpc8568_onchip_intr_names[] = {
291 { ISOURCE_QEB_LOW, "QEB low" },
292 { ISOURCE_QEB_PORT, "QEB port" },
293 { ISOURCE_QEB_IECC, "QEB iram ecc" },
294 { ISOURCE_QEB_MUECC, "QEB ram ecc" },
295 { ISOURCE_TLU1, "tlu1" },
296 { ISOURCE_QEB_HIGH, "QEB high" },
297 { 0, "" },
298 };
299
300 INTR_INFO_DECL(mpc8568, MPC8568);
301 #endif
302 #ifdef MPC8572
303 #define mpc8572_external_intr_names default_external_intr_names
304 const struct e500_intr_name mpc8572_onchip_intr_names[] = {
305 { ISOURCE_PCIEX3_MPC8572, "pcie3" },
306 { ISOURCE_FEC, "fec" },
307 { ISOURCE_PME_GENERAL, "pme" },
308 { ISOURCE_TLU1, "tlu1" },
309 { ISOURCE_TLU2, "tlu2" },
310 { ISOURCE_PME_CHAN1, "pme-chan1" },
311 { ISOURCE_PME_CHAN2, "pme-chan2" },
312 { ISOURCE_PME_CHAN3, "pme-chan3" },
313 { ISOURCE_PME_CHAN4, "pme-chan4" },
314 { ISOURCE_DMA2_CHAN1, "dma2-chan1" },
315 { ISOURCE_DMA2_CHAN2, "dma2-chan2" },
316 { ISOURCE_DMA2_CHAN3, "dma2-chan3" },
317 { ISOURCE_DMA2_CHAN4, "dma2-chan4" },
318 { 0, "" },
319 };
320
321 INTR_INFO_DECL(mpc8572, MPC8572);
322 #endif
323
324 #ifdef P1025
325 #define p1025_external_intr_names default_external_intr_names
326 const struct e500_intr_name p1025_onchip_intr_names[] = {
327 { ISOURCE_PCIEX3_MPC8572, "pcie3" },
328 { ISOURCE_ETSEC1_G1_TX, "etsec1-g1-tx" },
329 { ISOURCE_ETSEC1_G1_RX, "etsec1-g1-rx" },
330 { ISOURCE_ETSEC1_G1_ERR, "etsec1-g1-error" },
331 { ISOURCE_ETSEC2_G1_TX, "etsec2-g1-tx" },
332 { ISOURCE_ETSEC2_G1_RX, "etsec2-g1-rx" },
333 { ISOURCE_ETSEC2_G1_ERR, "etsec2-g1-error" },
334 { ISOURCE_ETSEC3_G1_TX, "etsec3-g1-tx" },
335 { ISOURCE_ETSEC3_G1_RX, "etsec3-g1-rx" },
336 { ISOURCE_ETSEC3_G1_ERR, "etsec3-g1-error" },
337 { ISOURCE_QEB_MUECC, "qeb-low" },
338 { ISOURCE_QEB_HIGH, "qeb-crit" },
339 { ISOURCE_DMA2_CHAN1, "dma2-chan1" },
340 { ISOURCE_DMA2_CHAN2, "dma2-chan2" },
341 { ISOURCE_DMA2_CHAN3, "dma2-chan3" },
342 { ISOURCE_DMA2_CHAN4, "dma2-chan4" },
343 { 0, "" },
344 };
345
346 INTR_INFO_DECL(p1025, P1025);
347 #endif
348
349 #ifdef P2020
350 #define p20x0_external_intr_names default_external_intr_names
351 const struct e500_intr_name p20x0_onchip_intr_names[] = {
352 { ISOURCE_PCIEX3_MPC8572, "pcie3" },
353 { ISOURCE_DMA2_CHAN1, "dma2-chan1" },
354 { ISOURCE_DMA2_CHAN2, "dma2-chan2" },
355 { ISOURCE_DMA2_CHAN3, "dma2-chan3" },
356 { ISOURCE_DMA2_CHAN4, "dma2-chan4" },
357 { 0, "" },
358 };
359
360 INTR_INFO_DECL(p20x0, P20x0);
361 #endif
362
363 static const char ist_names[][12] = {
364 [IST_NONE] = "none",
365 [IST_EDGE] = "edge",
366 [IST_LEVEL_LOW] = "level-",
367 [IST_LEVEL_HIGH] = "level+",
368 [IST_PULSE] = "pulse",
369 [IST_MSI] = "msi",
370 [IST_ONCHIP] = "onchip",
371 [IST_MSIGROUP] = "msigroup",
372 [IST_TIMER] = "timer",
373 [IST_IPI] = "ipi",
374 [IST_MI] = "msgint",
375 };
376
377 static struct intr_source *e500_intr_sources;
378 static const struct intr_source *e500_intr_last_source;
379
380 static void *e500_intr_establish(int, int, int, int (*)(void *), void *);
381 static void e500_intr_disestablish(void *);
382 static void e500_intr_cpu_attach(struct cpu_info *ci);
383 static void e500_intr_cpu_hatch(struct cpu_info *ci);
384 static void e500_intr_cpu_send_ipi(cpuid_t, uintptr_t);
385 static void e500_intr_init(void);
386 static const char *e500_intr_string(int, int, char *, size_t);
387 static const char *e500_intr_typename(int);
388 static void e500_critintr(struct trapframe *tf);
389 static void e500_decrintr(struct trapframe *tf);
390 static void e500_extintr(struct trapframe *tf);
391 static void e500_fitintr(struct trapframe *tf);
392 static void e500_wdogintr(struct trapframe *tf);
393 static void e500_spl0(void);
394 static int e500_splraise(int);
395 static void e500_splx(int);
396
397 const struct intrsw e500_intrsw = {
398 .intrsw_establish = e500_intr_establish,
399 .intrsw_disestablish = e500_intr_disestablish,
400 .intrsw_init = e500_intr_init,
401 .intrsw_cpu_attach = e500_intr_cpu_attach,
402 .intrsw_cpu_hatch = e500_intr_cpu_hatch,
403 .intrsw_cpu_send_ipi = e500_intr_cpu_send_ipi,
404 .intrsw_string = e500_intr_string,
405 .intrsw_typename = e500_intr_typename,
406
407 .intrsw_critintr = e500_critintr,
408 .intrsw_decrintr = e500_decrintr,
409 .intrsw_extintr = e500_extintr,
410 .intrsw_fitintr = e500_fitintr,
411 .intrsw_wdogintr = e500_wdogintr,
412
413 .intrsw_splraise = e500_splraise,
414 .intrsw_splx = e500_splx,
415 .intrsw_spl0 = e500_spl0,
416
417 #ifdef __HAVE_FAST_SOFTINTS
418 .intrsw_softint_init_md = powerpc_softint_init_md,
419 .intrsw_softint_trigger = powerpc_softint_trigger,
420 #endif
421 };
422
423 static bool wdog_barked;
424
425 static inline uint32_t
426 openpic_read(struct cpu_softc *cpu, bus_size_t offset)
427 {
428
429 return bus_space_read_4(cpu->cpu_bst, cpu->cpu_bsh,
430 OPENPIC_BASE + offset);
431 }
432
433 static inline void
434 openpic_write(struct cpu_softc *cpu, bus_size_t offset, uint32_t val)
435 {
436
437 return bus_space_write_4(cpu->cpu_bst, cpu->cpu_bsh,
438 OPENPIC_BASE + offset, val);
439 }
440
441 static const char *
442 e500_intr_external_name_lookup(int irq)
443 {
444 prop_array_t extirqs = board_info_get_object("external-irqs");
445 prop_string_t irqname = prop_array_get(extirqs, irq);
446 KASSERT(irqname != NULL);
447 KASSERT(prop_object_type(irqname) == PROP_TYPE_STRING);
448
449 return prop_string_cstring_nocopy(irqname);
450 }
451
452 static const char *
453 e500_intr_name_lookup(const struct e500_intr_name *names, int irq)
454 {
455 for (; names->in_name[0] != '\0'; names++) {
456 if (names->in_irq == irq)
457 return names->in_name;
458 }
459
460 return NULL;
461 }
462
463 static const char *
464 e500_intr_onchip_name_lookup(int irq)
465 {
466 const char *name;
467
468 name = e500_intr_name_lookup(e500_intr_info.ii_onchip_intr_names, irq);
469 if (name == NULL)
470 name = e500_intr_name_lookup(e500_onchip_intr_names, irq);
471
472 return name;
473 }
474
475 static inline void
476 e500_splset(struct cpu_info *ci, int ipl)
477 {
478 struct cpu_softc * const cpu = ci->ci_softc;
479
480 KASSERT((curlwp->l_pflag & LP_INTR) == 0 || ipl != IPL_NONE);
481 const u_int ctpr = IPL2CTPR(ipl);
482 KASSERT(openpic_read(cpu, OPENPIC_CTPR) == IPL2CTPR(ci->ci_cpl));
483 openpic_write(cpu, OPENPIC_CTPR, ctpr);
484 KASSERT(openpic_read(cpu, OPENPIC_CTPR) == ctpr);
485 #ifdef DIAGNOSTIC
486 cpu->cpu_spl_tb[ipl][ci->ci_cpl] = mftb();
487 #endif
488 ci->ci_cpl = ipl;
489 }
490
491 static void
492 e500_spl0(void)
493 {
494 wrtee(0);
495
496 struct cpu_info * const ci = curcpu();
497
498 #ifdef __HAVE_FAST_SOFTINTS
499 if (__predict_false(ci->ci_data.cpu_softints != 0)) {
500 e500_splset(ci, IPL_HIGH);
501 wrtee(PSL_EE);
502 powerpc_softint(ci, IPL_NONE,
503 (vaddr_t)__builtin_return_address(0));
504 wrtee(0);
505 }
506 #endif /* __HAVE_FAST_SOFTINTS */
507 e500_splset(ci, IPL_NONE);
508
509 wrtee(PSL_EE);
510 }
511
512 static void
513 e500_splx(int ipl)
514 {
515 struct cpu_info * const ci = curcpu();
516 const int old_ipl = ci->ci_cpl;
517
518 /* if we paniced because of watchdog, PSL_CE will be clear. */
519 KASSERT(wdog_barked || (mfmsr() & PSL_CE));
520
521 if (ipl == old_ipl)
522 return;
523
524 if (__predict_false(ipl > old_ipl)) {
525 printf("%s: %p: cpl=%u: ignoring splx(%u) to raise ipl\n",
526 __func__, __builtin_return_address(0), old_ipl, ipl);
527 if (old_ipl == IPL_NONE)
528 Debugger();
529 }
530
531 // const
532 register_t msr = wrtee(0);
533 #ifdef __HAVE_FAST_SOFTINTS
534 const u_int softints = ci->ci_data.cpu_softints & (IPL_SOFTMASK << ipl);
535 if (__predict_false(softints != 0)) {
536 e500_splset(ci, IPL_HIGH);
537 wrtee(msr);
538 powerpc_softint(ci, ipl,
539 (vaddr_t)__builtin_return_address(0));
540 wrtee(0);
541 }
542 #endif /* __HAVE_FAST_SOFTINTS */
543 e500_splset(ci, ipl);
544 #if 1
545 if (ipl < IPL_VM && old_ipl >= IPL_VM)
546 msr = PSL_EE;
547 #endif
548 wrtee(msr);
549 }
550
551 static int
552 e500_splraise(int ipl)
553 {
554 struct cpu_info * const ci = curcpu();
555 const int old_ipl = ci->ci_cpl;
556
557 /* if we paniced because of watchdog, PSL_CE will be clear. */
558 KASSERT(wdog_barked || (mfmsr() & PSL_CE));
559
560 if (old_ipl < ipl) {
561 //const
562 register_t msr = wrtee(0);
563 e500_splset(ci, ipl);
564 #if 0
565 if (old_ipl < IPL_VM && ipl >= IPL_VM)
566 msr = 0;
567 #endif
568 wrtee(msr);
569 } else if (ipl == IPL_NONE) {
570 panic("%s: %p: cpl=%u: attempt to splraise(IPL_NONE)",
571 __func__, __builtin_return_address(0), old_ipl);
572 #if 0
573 } else if (old_ipl > ipl) {
574 printf("%s: %p: cpl=%u: ignoring splraise(%u) to lower ipl\n",
575 __func__, __builtin_return_address(0), old_ipl, ipl);
576 #endif
577 }
578
579 return old_ipl;
580 }
581
582 static int
583 e500_intr_spurious(void *arg)
584 {
585 return 0;
586 }
587
588 static bool
589 e500_intr_irq_info_get(struct cpu_info *ci, u_int irq, int ipl, int ist,
590 struct e500_intr_irq_info *ii)
591 {
592 const struct e500_intr_info * const info = &e500_intr_info;
593 bool ok;
594
595 #if DEBUG > 2
596 printf("%s(%p,irq=%u,ipl=%u,ist=%u,%p)\n", __func__, ci, irq, ipl, ist, ii);
597 #endif
598
599 if (ipl < IPL_VM || ipl > IPL_HIGH) {
600 #if DEBUG > 2
601 printf("%s:%d ipl=%u\n", __func__, __LINE__, ipl);
602 #endif
603 return false;
604 }
605
606 if (ist <= IST_NONE || ist >= IST_MAX) {
607 #if DEBUG > 2
608 printf("%s:%d ist=%u\n", __func__, __LINE__, ist);
609 #endif
610 return false;
611 }
612
613 ii->irq_vector = irq + info->ii_ist_vectors[ist];
614 if (IST_PERCPU_P(ist) && ist != IST_IPI)
615 ii->irq_vector += ci->ci_cpuid * info->ii_percpu_sources;
616
617 switch (ist) {
618 default:
619 ii->irq_vpr = OPENPIC_EIVPR(irq);
620 ii->irq_dr = OPENPIC_EIDR(irq);
621 ok = irq < info->ii_external_sources
622 && (ist == IST_EDGE
623 || ist == IST_LEVEL_LOW
624 || ist == IST_LEVEL_HIGH);
625 break;
626 case IST_PULSE:
627 ok = false;
628 break;
629 case IST_ONCHIP:
630 ii->irq_vpr = OPENPIC_IIVPR(irq);
631 ii->irq_dr = OPENPIC_IIDR(irq);
632 ok = irq < 32 * __arraycount(info->ii_onchip_bitmap);
633 #if DEBUG > 2
634 printf("%s: irq=%u: ok=%u\n", __func__, irq, ok);
635 #endif
636 ok = ok && (info->ii_onchip_bitmap[irq/32] & (1 << (irq & 31)));
637 #if DEBUG > 2
638 printf("%s: %08x%08x -> %08x%08x: ok=%u\n", __func__,
639 irq < 32 ? 0 : (1 << irq), irq < 32 ? (1 << irq) : 0,
640 info->ii_onchip_bitmap[1], info->ii_onchip_bitmap[0],
641 ok);
642 #endif
643 break;
644 case IST_MSIGROUP:
645 ii->irq_vpr = OPENPIC_MSIVPR(irq);
646 ii->irq_dr = OPENPIC_MSIDR(irq);
647 ok = irq < info->ii_msigroup_sources
648 && ipl == IPL_VM;
649 break;
650 case IST_TIMER:
651 ii->irq_vpr = OPENPIC_GTVPR(ci->ci_cpuid, irq);
652 ii->irq_dr = OPENPIC_GTDR(ci->ci_cpuid, irq);
653 ok = irq < info->ii_timer_sources;
654 #if DEBUG > 2
655 printf("%s: IST_TIMER irq=%u: ok=%u\n", __func__, irq, ok);
656 #endif
657 break;
658 case IST_IPI:
659 ii->irq_vpr = OPENPIC_IPIVPR(irq);
660 ii->irq_dr = OPENPIC_IPIDR(irq);
661 ok = irq < info->ii_ipi_sources;
662 break;
663 case IST_MI:
664 ii->irq_vpr = OPENPIC_MIVPR(irq);
665 ii->irq_dr = OPENPIC_MIDR(irq);
666 ok = irq < info->ii_mi_sources;
667 break;
668 }
669
670 return ok;
671 }
672
673 static const char *
674 e500_intr_string(int irq, int ist, char *buf, size_t len)
675 {
676 struct cpu_info * const ci = curcpu();
677 struct cpu_softc * const cpu = ci->ci_softc;
678 struct e500_intr_irq_info ii;
679
680 if (!e500_intr_irq_info_get(ci, irq, IPL_VM, ist, &ii))
681 return NULL;
682
683 strlcpy(buf, cpu->cpu_evcnt_intrs[ii.irq_vector].ev_name, len);
684 return buf;
685 }
686
687 __CTASSERT(__arraycount(ist_names) == IST_MAX);
688
689 static const char *
690 e500_intr_typename(int ist)
691 {
692 if (IST_NONE <= ist && ist < IST_MAX)
693 return ist_names[ist];
694
695 return NULL;
696 }
697
698 static void *
699 e500_intr_cpu_establish(struct cpu_info *ci, int irq, int ipl, int ist,
700 int (*handler)(void *), void *arg)
701 {
702 struct cpu_softc * const cpu = ci->ci_softc;
703 struct e500_intr_irq_info ii;
704
705 KASSERT(ipl >= IPL_VM && ipl <= IPL_HIGH);
706 KASSERT(ist > IST_NONE && ist < IST_MAX && ist != IST_MSI);
707
708 if (!e500_intr_irq_info_get(ci, irq, ipl, ist, &ii)) {
709 printf("%s: e500_intr_irq_info_get(%p,%u,%u,%u,%p) failed\n",
710 __func__, ci, irq, ipl, ist, &ii);
711 return NULL;
712 }
713
714 struct intr_source * const is = &e500_intr_sources[ii.irq_vector];
715 mutex_enter(&e500_intr_lock);
716 if (is->is_ipl != IPL_NONE) {
717 mutex_exit(&e500_intr_lock);
718 return NULL;
719 }
720
721 is->is_func = handler;
722 is->is_arg = arg;
723 is->is_ipl = ipl;
724 is->is_ist = ist;
725 is->is_irq = irq;
726 is->is_vpr = ii.irq_vpr;
727 is->is_dr = ii.irq_dr;
728
729 uint32_t vpr = VPR_PRIORITY_MAKE(IPL2CTPR(ipl))
730 | VPR_VECTOR_MAKE(((ii.irq_vector + 1) << 4) | ipl)
731 | (ist == IST_LEVEL_LOW
732 ? VPR_LEVEL_LOW
733 : (ist == IST_LEVEL_HIGH
734 ? VPR_LEVEL_HIGH
735 : (ist == IST_ONCHIP
736 ? VPR_P_HIGH
737 : 0)));
738
739 /*
740 * All interrupts go to the primary except per-cpu interrupts which get
741 * routed to the appropriate cpu.
742 */
743 uint32_t dr = openpic_read(cpu, ii.irq_dr);
744
745 dr |= 1 << (IST_PERCPU_P(ist) ? ci->ci_cpuid : 0);
746
747 /*
748 * Update the vector/priority and destination registers keeping the
749 * interrupt masked.
750 */
751 const register_t msr = wrtee(0); /* disable interrupts */
752 openpic_write(cpu, ii.irq_vpr, vpr | VPR_MSK);
753 openpic_write(cpu, ii.irq_dr, dr);
754
755 /*
756 * Now unmask the interrupt.
757 */
758 openpic_write(cpu, ii.irq_vpr, vpr);
759
760 wrtee(msr); /* re-enable interrupts */
761
762 mutex_exit(&e500_intr_lock);
763
764 return is;
765 }
766
767 static void *
768 e500_intr_establish(int irq, int ipl, int ist,
769 int (*handler)(void *), void *arg)
770 {
771 return e500_intr_cpu_establish(curcpu(), irq, ipl, ist, handler, arg);
772 }
773
774 static void
775 e500_intr_disestablish(void *vis)
776 {
777 struct cpu_softc * const cpu = curcpu()->ci_softc;
778 struct intr_source * const is = vis;
779 struct e500_intr_irq_info ii;
780
781 KASSERT(e500_intr_sources <= is);
782 KASSERT(is < e500_intr_last_source);
783 KASSERT(!cpu_intr_p());
784
785 bool ok = e500_intr_irq_info_get(curcpu(), is->is_irq, is->is_ipl,
786 is->is_ist, &ii);
787 (void)ok; /* appease gcc */
788 KASSERT(ok);
789 KASSERT(is - e500_intr_sources == ii.irq_vector);
790
791 mutex_enter(&e500_intr_lock);
792 /*
793 * Mask the source using the mask (MSK) bit in the vector/priority reg.
794 */
795 uint32_t vpr = openpic_read(cpu, ii.irq_vpr);
796 openpic_write(cpu, ii.irq_vpr, VPR_MSK | vpr);
797
798 /*
799 * Wait for the Activity (A) bit for the source to be cleared.
800 */
801 while (openpic_read(cpu, ii.irq_vpr) & VPR_A)
802 ;
803
804 /*
805 * Now the source can be modified.
806 */
807 openpic_write(cpu, ii.irq_dr, 0); /* stop delivery */
808 openpic_write(cpu, ii.irq_vpr, VPR_MSK); /* mask/reset it */
809
810 *is = (struct intr_source)INTR_SOURCE_INITIALIZER;
811
812 mutex_exit(&e500_intr_lock);
813 }
814
815 static void
816 e500_critintr(struct trapframe *tf)
817 {
818 panic("%s: srr0/srr1=%#lx/%#lx", __func__, tf->tf_srr0, tf->tf_srr1);
819 }
820
821 static void
822 e500_decrintr(struct trapframe *tf)
823 {
824 panic("%s: srr0/srr1=%#lx/%#lx", __func__, tf->tf_srr0, tf->tf_srr1);
825 }
826
827 static void
828 e500_fitintr(struct trapframe *tf)
829 {
830 panic("%s: srr0/srr1=%#lx/%#lx", __func__, tf->tf_srr0, tf->tf_srr1);
831 }
832
833 static void
834 e500_wdogintr(struct trapframe *tf)
835 {
836 struct cpu_info * const ci = curcpu();
837 mtspr(SPR_TSR, TSR_ENW|TSR_WIS);
838 wdog_barked = true;
839 dump_splhist(ci, NULL);
840 dump_trapframe(tf, NULL);
841 panic("%s: tf=%p tb=%"PRId64" srr0/srr1=%#lx/%#lx"
842 " cpl=%d idepth=%d, mtxcount=%d",
843 __func__, tf, mftb(), tf->tf_srr0, tf->tf_srr1,
844 ci->ci_cpl, ci->ci_idepth, ci->ci_mtx_count);
845 }
846
847 static void
848 e500_extintr(struct trapframe *tf)
849 {
850 struct cpu_info * const ci = curcpu();
851 struct cpu_softc * const cpu = ci->ci_softc;
852 const int old_ipl = ci->ci_cpl;
853
854 /* if we paniced because of watchdog, PSL_CE will be clear. */
855 KASSERT(wdog_barked || (mfmsr() & PSL_CE));
856
857 #if 0
858 // printf("%s(%p): idepth=%d enter\n", __func__, tf, ci->ci_idepth);
859 if ((register_t)tf >= (register_t)curlwp->l_addr + USPACE
860 || (register_t)tf < (register_t)curlwp->l_addr + NBPG) {
861 printf("%s(entry): pid %d.%d (%s): srr0/srr1=%#lx/%#lx: invalid tf addr %p\n",
862 __func__, curlwp->l_proc->p_pid, curlwp->l_lid,
863 curlwp->l_proc->p_comm, tf->tf_srr0, tf->tf_srr1, tf);
864 }
865 #endif
866
867
868 ci->ci_data.cpu_nintr++;
869 tf->tf_cf.cf_idepth = ci->ci_idepth++;
870 cpu->cpu_pcpls[ci->ci_idepth] = old_ipl;
871 #if 1
872 if (mfmsr() & PSL_EE)
873 panic("%s(%p): MSR[EE] is on (%#lx)!", __func__, tf, mfmsr());
874 if (old_ipl == IPL_HIGH
875 || IPL2CTPR(old_ipl) != openpic_read(cpu, OPENPIC_CTPR))
876 panic("%s(%p): old_ipl(%u) == IPL_HIGH(%u) "
877 "|| old_ipl + %u != OPENPIC_CTPR (%u)",
878 __func__, tf, old_ipl, IPL_HIGH,
879 15 - IPL_HIGH, openpic_read(cpu, OPENPIC_CTPR));
880 #else
881 if (old_ipl >= IPL_VM)
882 panic("%s(%p): old_ipl(%u) >= IPL_VM(%u) CTPR=%u",
883 __func__, tf, old_ipl, IPL_VM, openpic_read(cpu, OPENPIC_CTPR));
884 #endif
885
886 for (;;) {
887 /*
888 * Find out the pending interrupt.
889 */
890 KASSERTMSG((mfmsr() & PSL_EE) == 0,
891 "%s(%p): MSR[EE] left on (%#lx)!", __func__, tf, mfmsr());
892 if (IPL2CTPR(old_ipl) != openpic_read(cpu, OPENPIC_CTPR))
893 panic("%s(%p): %d: old_ipl(%u) + %u != OPENPIC_CTPR (%u)",
894 __func__, tf, __LINE__, old_ipl,
895 15 - IPL_HIGH, openpic_read(cpu, OPENPIC_CTPR));
896 const uint32_t iack = openpic_read(cpu, OPENPIC_IACK);
897 #ifdef DIAGNOSTIC
898 const int ipl = iack & 0xf;
899 #endif
900 const int irq = (iack >> 4) - 1;
901 #if 0
902 printf("%s: iack=%d ipl=%d irq=%d <%s>\n",
903 __func__, iack, ipl, irq,
904 (iack != IRQ_SPURIOUS ?
905 cpu->cpu_evcnt_intrs[irq].ev_name : "spurious"));
906 #endif
907 if (IPL2CTPR(old_ipl) != openpic_read(cpu, OPENPIC_CTPR))
908 panic("%s(%p): %d: old_ipl(%u) + %u != OPENPIC_CTPR (%u)",
909 __func__, tf, __LINE__, old_ipl,
910 15 - IPL_HIGH, openpic_read(cpu, OPENPIC_CTPR));
911 if (iack == IRQ_SPURIOUS)
912 break;
913
914 struct intr_source * const is = &e500_intr_sources[irq];
915 if (__predict_true(is < e500_intr_last_source)) {
916 /*
917 * Timer interrupts get their argument overriden with
918 * the pointer to the trapframe.
919 */
920 KASSERTMSG(is->is_ipl == ipl,
921 "iack %#x: is %p: irq %d ipl %d != iack ipl %d",
922 iack, is, irq, is->is_ipl, ipl);
923 void *arg = (is->is_ist == IST_TIMER ? tf : is->is_arg);
924 if (is->is_ipl <= old_ipl)
925 panic("%s(%p): %s (%u): is->is_ipl (%u) <= old_ipl (%u)\n",
926 __func__, tf,
927 cpu->cpu_evcnt_intrs[irq].ev_name, irq,
928 is->is_ipl, old_ipl);
929 KASSERT(is->is_ipl > old_ipl);
930 e500_splset(ci, is->is_ipl); /* change IPL */
931 if (__predict_false(is->is_func == NULL)) {
932 aprint_error_dev(ci->ci_dev,
933 "interrupt from unestablished irq %d\n",
934 irq);
935 } else {
936 int (*func)(void *) = is->is_func;
937 wrtee(PSL_EE);
938 int rv = (*func)(arg);
939 wrtee(0);
940 #if DEBUG > 2
941 printf("%s: %s handler %p(%p) returned %d\n",
942 __func__,
943 cpu->cpu_evcnt_intrs[irq].ev_name,
944 func, arg, rv);
945 #endif
946 if (rv == 0)
947 cpu->cpu_evcnt_spurious_intr.ev_count++;
948 }
949 e500_splset(ci, old_ipl); /* restore IPL */
950 cpu->cpu_evcnt_intrs[irq].ev_count++;
951 } else {
952 aprint_error_dev(ci->ci_dev,
953 "interrupt from illegal irq %d\n", irq);
954 cpu->cpu_evcnt_spurious_intr.ev_count++;
955 }
956 /*
957 * If this is a nested interrupt, simply ack it and exit
958 * because the loop we interrupted will complete looking
959 * for interrupts.
960 */
961 KASSERTMSG((mfmsr() & PSL_EE) == 0,
962 "%s(%p): MSR[EE] left on (%#lx)!", __func__, tf, mfmsr());
963 if (IPL2CTPR(old_ipl) != openpic_read(cpu, OPENPIC_CTPR))
964 panic("%s(%p): %d: old_ipl(%u) + %u != OPENPIC_CTPR (%u)",
965 __func__, tf, __LINE__, old_ipl,
966 15 - IPL_HIGH, openpic_read(cpu, OPENPIC_CTPR));
967
968 openpic_write(cpu, OPENPIC_EOI, 0);
969 if (IPL2CTPR(old_ipl) != openpic_read(cpu, OPENPIC_CTPR))
970 panic("%s(%p): %d: old_ipl(%u) + %u != OPENPIC_CTPR (%u)",
971 __func__, tf, __LINE__, old_ipl,
972 15 - IPL_HIGH, openpic_read(cpu, OPENPIC_CTPR));
973 if (ci->ci_idepth > 0)
974 break;
975 }
976
977 ci->ci_idepth--;
978
979 #ifdef __HAVE_FAST_SOFTINTS
980 /*
981 * Before exiting, deal with any softints that need to be dealt with.
982 */
983 const u_int softints = ci->ci_data.cpu_softints & (IPL_SOFTMASK << old_ipl);
984 if (__predict_false(softints != 0)) {
985 KASSERT(old_ipl < IPL_VM);
986 e500_splset(ci, IPL_HIGH); /* pop to high */
987 wrtee(PSL_EE); /* reenable interrupts */
988 powerpc_softint(ci, old_ipl, /* deal with them */
989 tf->tf_srr0);
990 wrtee(0); /* disable interrupts */
991 e500_splset(ci, old_ipl); /* and drop back */
992 }
993 #endif /* __HAVE_FAST_SOFTINTS */
994 KASSERT(ci->ci_cpl == old_ipl);
995
996 /*
997 * If we interrupted while power-saving and we need to exit idle,
998 * we need to clear PSL_POW so we won't go back into power-saving.
999 */
1000 if (__predict_false(tf->tf_srr1 & PSL_POW) && ci->ci_want_resched)
1001 tf->tf_srr1 &= ~PSL_POW;
1002
1003 // printf("%s(%p): idepth=%d exit\n", __func__, tf, ci->ci_idepth);
1004 }
1005
1006 static void
1007 e500_intr_init(void)
1008 {
1009 struct cpu_info * const ci = curcpu();
1010 struct cpu_softc * const cpu = ci->ci_softc;
1011 const uint32_t frr = openpic_read(cpu, OPENPIC_FRR);
1012 const u_int nirq = FRR_NIRQ_GET(frr) + 1;
1013 // const u_int ncpu = FRR_NCPU_GET(frr) + 1;
1014 struct intr_source *is;
1015 struct e500_intr_info * const ii = &e500_intr_info;
1016
1017 const uint16_t svr = (mfspr(SPR_SVR) & ~0x80000) >> 16;
1018 switch (svr) {
1019 #ifdef MPC8536
1020 case SVR_MPC8536v1 >> 16:
1021 *ii = mpc8536_intr_info;
1022 break;
1023 #endif
1024 #ifdef MPC8544
1025 case SVR_MPC8544v1 >> 16:
1026 *ii = mpc8544_intr_info;
1027 break;
1028 #endif
1029 #ifdef MPC8548
1030 case SVR_MPC8543v1 >> 16:
1031 case SVR_MPC8548v1 >> 16:
1032 *ii = mpc8548_intr_info;
1033 break;
1034 #endif
1035 #ifdef MPC8555
1036 case SVR_MPC8541v1 >> 16:
1037 case SVR_MPC8555v1 >> 16:
1038 *ii = mpc8555_intr_info;
1039 break;
1040 #endif
1041 #ifdef MPC8568
1042 case SVR_MPC8568v1 >> 16:
1043 *ii = mpc8568_intr_info;
1044 break;
1045 #endif
1046 #ifdef MPC8572
1047 case SVR_MPC8572v1 >> 16:
1048 *ii = mpc8572_intr_info;
1049 break;
1050 #endif
1051 #ifdef P1025
1052 case SVR_P1016v1 >> 16:
1053 case SVR_P1025v1 >> 16:
1054 *ii = p1025_intr_info;
1055 break;
1056 #endif
1057 #ifdef P2020
1058 case SVR_P2010v2 >> 16:
1059 case SVR_P2020v2 >> 16:
1060 *ii = p20x0_intr_info;
1061 break;
1062 #endif
1063 default:
1064 panic("%s: don't know how to deal with SVR %#lx",
1065 __func__, mfspr(SPR_SVR));
1066 }
1067
1068 /*
1069 * We need to be in mixed mode.
1070 */
1071 openpic_write(cpu, OPENPIC_GCR, GCR_M);
1072
1073 /*
1074 * Make we and the openpic both agree about the current SPL level.
1075 */
1076 e500_splset(ci, ci->ci_cpl);
1077
1078 /*
1079 * Allow the required number of interrupt sources.
1080 */
1081 is = kmem_zalloc(nirq * sizeof(*is), KM_SLEEP);
1082 KASSERT(is);
1083 e500_intr_sources = is;
1084 e500_intr_last_source = is + nirq;
1085
1086 /*
1087 * Initialize all the external interrupts as active low.
1088 */
1089 for (u_int irq = 0; irq < e500_intr_info.ii_external_sources; irq++) {
1090 openpic_write(cpu, OPENPIC_EIVPR(irq),
1091 VPR_VECTOR_MAKE(irq) | VPR_LEVEL_LOW);
1092 }
1093 }
1094
1095 static void
1096 e500_idlespin(void)
1097 {
1098 KASSERTMSG(curcpu()->ci_cpl == IPL_NONE,
1099 "%s: cpu%u: ci_cpl (%d) != 0", __func__, cpu_number(),
1100 curcpu()->ci_cpl);
1101 KASSERTMSG(CTPR2IPL(openpic_read(curcpu()->ci_softc, OPENPIC_CTPR)) == IPL_NONE,
1102 "%s: cpu%u: CTPR (%d) != IPL_NONE", __func__, cpu_number(),
1103 CTPR2IPL(openpic_read(curcpu()->ci_softc, OPENPIC_CTPR)));
1104 KASSERT(mfmsr() & PSL_EE);
1105
1106 if (powersave > 0)
1107 mtmsr(mfmsr() | PSL_POW);
1108 }
1109
1110 static void
1111 e500_intr_cpu_attach(struct cpu_info *ci)
1112 {
1113 struct cpu_softc * const cpu = ci->ci_softc;
1114 const char * const xname = device_xname(ci->ci_dev);
1115
1116 const u_int32_t frr = openpic_read(cpu, OPENPIC_FRR);
1117 const u_int nirq = FRR_NIRQ_GET(frr) + 1;
1118 // const u_int ncpu = FRR_NCPU_GET(frr) + 1;
1119
1120 const struct e500_intr_info * const info = &e500_intr_info;
1121
1122 cpu->cpu_clock_gtbcr = OPENPIC_GTBCR(ci->ci_cpuid, E500_CLOCK_TIMER);
1123
1124 cpu->cpu_evcnt_intrs =
1125 kmem_zalloc(nirq * sizeof(cpu->cpu_evcnt_intrs[0]), KM_SLEEP);
1126 KASSERT(cpu->cpu_evcnt_intrs);
1127
1128 struct evcnt *evcnt = cpu->cpu_evcnt_intrs;
1129 for (size_t j = 0; j < info->ii_external_sources; j++, evcnt++) {
1130 const char *name = e500_intr_external_name_lookup(j);
1131 evcnt_attach_dynamic(evcnt, EVCNT_TYPE_INTR, NULL, xname, name);
1132 }
1133 KASSERT(evcnt == cpu->cpu_evcnt_intrs + info->ii_ist_vectors[IST_ONCHIP]);
1134 for (size_t j = 0; j < info->ii_onchip_sources; j++, evcnt++) {
1135 if (info->ii_onchip_bitmap[j / 32] & __BIT(j & 31)) {
1136 const char *name = e500_intr_onchip_name_lookup(j);
1137 if (name != NULL) {
1138 evcnt_attach_dynamic(evcnt, EVCNT_TYPE_INTR,
1139 NULL, xname, name);
1140 #ifdef DIAGNOSTIC
1141 } else {
1142 printf("%s: missing evcnt for onchip irq %zu\n",
1143 __func__, j);
1144 #endif
1145 }
1146 }
1147 }
1148
1149 KASSERT(evcnt == cpu->cpu_evcnt_intrs + info->ii_ist_vectors[IST_MSIGROUP]);
1150 for (size_t j = 0; j < info->ii_msigroup_sources; j++, evcnt++) {
1151 evcnt_attach_dynamic(evcnt, EVCNT_TYPE_INTR,
1152 NULL, xname, e500_msigroup_intr_names[j].in_name);
1153 }
1154
1155 KASSERT(evcnt == cpu->cpu_evcnt_intrs + info->ii_ist_vectors[IST_TIMER]);
1156 evcnt += ci->ci_cpuid * info->ii_percpu_sources;
1157 for (size_t j = 0; j < info->ii_timer_sources; j++, evcnt++) {
1158 evcnt_attach_dynamic(evcnt, EVCNT_TYPE_INTR,
1159 NULL, xname, e500_timer_intr_names[j].in_name);
1160 }
1161
1162 for (size_t j = 0; j < info->ii_ipi_sources; j++, evcnt++) {
1163 evcnt_attach_dynamic(evcnt, EVCNT_TYPE_INTR,
1164 NULL, xname, e500_ipi_intr_names[j].in_name);
1165 }
1166
1167 for (size_t j = 0; j < info->ii_mi_sources; j++, evcnt++) {
1168 evcnt_attach_dynamic(evcnt, EVCNT_TYPE_INTR,
1169 NULL, xname, e500_mi_intr_names[j].in_name);
1170 }
1171
1172 ci->ci_idlespin = e500_idlespin;
1173 }
1174
1175 static void
1176 e500_intr_cpu_send_ipi(cpuid_t target, uint32_t ipimsg)
1177 {
1178 struct cpu_info * const ci = curcpu();
1179 struct cpu_softc * const cpu = ci->ci_softc;
1180 uint32_t dstmask;
1181
1182 if (target >= CPU_MAXNUM) {
1183 CPU_INFO_ITERATOR cii;
1184 struct cpu_info *dst_ci;
1185
1186 KASSERT(target == IPI_DST_NOTME || target == IPI_DST_ALL);
1187
1188 dstmask = 0;
1189 for (CPU_INFO_FOREACH(cii, dst_ci)) {
1190 if (target == IPI_DST_ALL || ci != dst_ci) {
1191 dstmask |= 1 << cpu_index(ci);
1192 if (ipimsg)
1193 atomic_or_32(&dst_ci->ci_pending_ipis,
1194 ipimsg);
1195 }
1196 }
1197 } else {
1198 struct cpu_info * const dst_ci = cpu_lookup(target);
1199 KASSERT(dst_ci != NULL);
1200 KASSERTMSG(target == cpu_index(dst_ci),
1201 "%s: target (%lu) != cpu_index(cpu%u)",
1202 __func__, target, cpu_index(dst_ci));
1203 dstmask = (1 << target);
1204 if (ipimsg)
1205 atomic_or_32(&dst_ci->ci_pending_ipis, ipimsg);
1206 }
1207
1208 openpic_write(cpu, OPENPIC_IPIDR(0), dstmask);
1209 }
1210
1211 typedef void (*ipifunc_t)(void);
1212
1213 #ifdef __HAVE_PREEEMPTION
1214 static void
1215 e500_ipi_kpreempt(void)
1216 {
1217 poowerpc_softint_trigger(1 << IPL_NONE);
1218 }
1219 #endif
1220
1221 static const ipifunc_t e500_ipifuncs[] = {
1222 [ilog2(IPI_XCALL)] = xc_ipi_handler,
1223 [ilog2(IPI_GENERIC)] = ipi_cpu_handler,
1224 [ilog2(IPI_HALT)] = e500_ipi_halt,
1225 #ifdef __HAVE_PREEMPTION
1226 [ilog2(IPI_KPREEMPT)] = e500_ipi_kpreempt,
1227 #endif
1228 [ilog2(IPI_TLB1SYNC)] = e500_tlb1_sync,
1229 };
1230
1231 static int
1232 e500_ipi_intr(void *v)
1233 {
1234 struct cpu_info * const ci = curcpu();
1235
1236 ci->ci_ev_ipi.ev_count++;
1237
1238 uint32_t pending_ipis = atomic_swap_32(&ci->ci_pending_ipis, 0);
1239 for (u_int ipi = 31; pending_ipis != 0; ipi--, pending_ipis <<= 1) {
1240 const u_int bits = __builtin_clz(pending_ipis);
1241 ipi -= bits;
1242 pending_ipis <<= bits;
1243 KASSERT(e500_ipifuncs[ipi] != NULL);
1244 (*e500_ipifuncs[ipi])();
1245 }
1246
1247 return 1;
1248 }
1249
1250 static void
1251 e500_intr_cpu_hatch(struct cpu_info *ci)
1252 {
1253 /*
1254 * Establish clock interrupt for this CPU.
1255 */
1256 if (e500_intr_cpu_establish(ci, E500_CLOCK_TIMER, IPL_CLOCK, IST_TIMER,
1257 e500_clock_intr, NULL) == NULL)
1258 panic("%s: failed to establish clock interrupt!", __func__);
1259
1260 /*
1261 * Establish the IPI interrupts for this CPU.
1262 */
1263 if (e500_intr_cpu_establish(ci, 0, IPL_VM, IST_IPI, e500_ipi_intr,
1264 NULL) == NULL)
1265 panic("%s: failed to establish ipi interrupt!", __func__);
1266
1267 /*
1268 * Enable watchdog interrupts.
1269 */
1270 uint32_t tcr = mfspr(SPR_TCR);
1271 tcr |= TCR_WIE;
1272 mtspr(SPR_TCR, tcr);
1273 }
1274