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