Home | History | Annotate | Line # | Download | only in booke
e500_intr.c revision 1.19
      1 /*	$NetBSD: e500_intr.c,v 1.19 2012/07/15 08:44:56 matt 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.19 2012/07/15 08:44:56 matt 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_DMA2_CHAN1, "dma2-chan1" },
    337 	{ ISOURCE_DMA2_CHAN2, "dma2-chan2" },
    338 	{ ISOURCE_DMA2_CHAN3, "dma2-chan3" },
    339 	{ ISOURCE_DMA2_CHAN4, "dma2-chan4" },
    340 	{ 0, "" },
    341 };
    342 
    343 INTR_INFO_DECL(p1025, P1025);
    344 #endif
    345 
    346 #ifdef P2020
    347 #define	p20x0_external_intr_names	default_external_intr_names
    348 const struct e500_intr_name p20x0_onchip_intr_names[] = {
    349 	{ ISOURCE_PCIEX3_MPC8572, "pcie3" },
    350 	{ ISOURCE_DMA2_CHAN1, "dma2-chan1" },
    351 	{ ISOURCE_DMA2_CHAN2, "dma2-chan2" },
    352 	{ ISOURCE_DMA2_CHAN3, "dma2-chan3" },
    353 	{ ISOURCE_DMA2_CHAN4, "dma2-chan4" },
    354 	{ 0, "" },
    355 };
    356 
    357 INTR_INFO_DECL(p20x0, P20x0);
    358 #endif
    359 
    360 static const char ist_names[][12] = {
    361 	[IST_NONE] = "none",
    362 	[IST_EDGE] = "edge",
    363 	[IST_LEVEL_LOW] = "level-",
    364 	[IST_LEVEL_HIGH] = "level+",
    365 	[IST_PULSE] = "pulse",
    366 	[IST_MSI] = "msi",
    367 	[IST_ONCHIP] = "onchip",
    368 	[IST_MSIGROUP] = "msigroup",
    369 	[IST_TIMER] = "timer",
    370 	[IST_IPI] = "ipi",
    371 	[IST_MI] = "msgint",
    372 };
    373 
    374 static struct intr_source *e500_intr_sources;
    375 static const struct intr_source *e500_intr_last_source;
    376 
    377 static void 	*e500_intr_establish(int, int, int, int (*)(void *), void *);
    378 static void 	e500_intr_disestablish(void *);
    379 static void 	e500_intr_cpu_attach(struct cpu_info *ci);
    380 static void 	e500_intr_cpu_hatch(struct cpu_info *ci);
    381 static void	e500_intr_cpu_send_ipi(cpuid_t, uintptr_t);
    382 static void 	e500_intr_init(void);
    383 static const char *e500_intr_string(int, int);
    384 static const char *e500_intr_typename(int);
    385 static void 	e500_critintr(struct trapframe *tf);
    386 static void 	e500_decrintr(struct trapframe *tf);
    387 static void 	e500_extintr(struct trapframe *tf);
    388 static void 	e500_fitintr(struct trapframe *tf);
    389 static void 	e500_wdogintr(struct trapframe *tf);
    390 static void	e500_spl0(void);
    391 static int 	e500_splraise(int);
    392 static void 	e500_splx(int);
    393 
    394 const struct intrsw e500_intrsw = {
    395 	.intrsw_establish = e500_intr_establish,
    396 	.intrsw_disestablish = e500_intr_disestablish,
    397 	.intrsw_init = e500_intr_init,
    398 	.intrsw_cpu_attach = e500_intr_cpu_attach,
    399 	.intrsw_cpu_hatch = e500_intr_cpu_hatch,
    400 	.intrsw_cpu_send_ipi = e500_intr_cpu_send_ipi,
    401 	.intrsw_string = e500_intr_string,
    402 	.intrsw_typename = e500_intr_typename,
    403 
    404 	.intrsw_critintr = e500_critintr,
    405 	.intrsw_decrintr = e500_decrintr,
    406 	.intrsw_extintr = e500_extintr,
    407 	.intrsw_fitintr = e500_fitintr,
    408 	.intrsw_wdogintr = e500_wdogintr,
    409 
    410 	.intrsw_splraise = e500_splraise,
    411 	.intrsw_splx = e500_splx,
    412 	.intrsw_spl0 = e500_spl0,
    413 
    414 #ifdef __HAVE_FAST_SOFTINTS
    415 	.intrsw_softint_init_md = powerpc_softint_init_md,
    416 	.intrsw_softint_trigger = powerpc_softint_trigger,
    417 #endif
    418 };
    419 
    420 static inline uint32_t
    421 openpic_read(struct cpu_softc *cpu, bus_size_t offset)
    422 {
    423 
    424 	return bus_space_read_4(cpu->cpu_bst, cpu->cpu_bsh,
    425 	    OPENPIC_BASE + offset);
    426 }
    427 
    428 static inline void
    429 openpic_write(struct cpu_softc *cpu, bus_size_t offset, uint32_t val)
    430 {
    431 
    432 	return bus_space_write_4(cpu->cpu_bst, cpu->cpu_bsh,
    433 	    OPENPIC_BASE + offset, val);
    434 }
    435 
    436 static const char *
    437 e500_intr_external_name_lookup(int irq)
    438 {
    439 	prop_array_t extirqs = board_info_get_object("external-irqs");
    440 	prop_string_t irqname = prop_array_get(extirqs, irq);
    441 	KASSERT(irqname != NULL);
    442 	KASSERT(prop_object_type(irqname) == PROP_TYPE_STRING);
    443 
    444 	return prop_string_cstring_nocopy(irqname);
    445 }
    446 
    447 static const char *
    448 e500_intr_name_lookup(const struct e500_intr_name *names, int irq)
    449 {
    450 	for (; names->in_name[0] != '\0'; names++) {
    451 		if (names->in_irq == irq)
    452 			return names->in_name;
    453 	}
    454 
    455 	return NULL;
    456 }
    457 
    458 static const char *
    459 e500_intr_onchip_name_lookup(int irq)
    460 {
    461 	const char *name;
    462 
    463 	name = e500_intr_name_lookup(e500_intr_info.ii_onchip_intr_names, irq);
    464 	if (name == NULL)
    465 	       name = e500_intr_name_lookup(e500_onchip_intr_names, irq);
    466 
    467 	return name;
    468 }
    469 
    470 static inline void
    471 e500_splset(struct cpu_info *ci, int ipl)
    472 {
    473 	struct cpu_softc * const cpu = ci->ci_softc;
    474 
    475 	//KASSERT(!cpu_intr_p() || ipl >= IPL_VM);
    476 	KASSERT((curlwp->l_pflag & LP_INTR) == 0 || ipl != IPL_NONE);
    477 #if 0
    478 	u_int ctpr = ipl;
    479 	KASSERT(openpic_read(cpu, OPENPIC_CTPR) == ci->ci_cpl);
    480 #elif 0
    481 	u_int old_ctpr = (ci->ci_cpl >= IPL_VM ? 15 : ci->ci_cpl);
    482 	u_int ctpr = (ipl >= IPL_VM ? 15 : ipl);
    483 	KASSERT(openpic_read(cpu, OPENPIC_CTPR) == old_ctpr);
    484 #else
    485 	const u_int ctpr = IPL2CTPR(ipl);
    486 	KASSERT(openpic_read(cpu, OPENPIC_CTPR) == IPL2CTPR(ci->ci_cpl));
    487 #endif
    488 	openpic_write(cpu, OPENPIC_CTPR, ctpr);
    489 	KASSERT(openpic_read(cpu, OPENPIC_CTPR) == ctpr);
    490 	ci->ci_cpl = ipl;
    491 }
    492 
    493 static void
    494 e500_spl0(void)
    495 {
    496 	wrtee(0);
    497 
    498 	struct cpu_info * const ci = curcpu();
    499 
    500 #ifdef __HAVE_FAST_SOFTINTS
    501 	if (__predict_false(ci->ci_data.cpu_softints != 0)) {
    502 		e500_splset(ci, IPL_HIGH);
    503 		powerpc_softint(ci, IPL_NONE,
    504 		    (vaddr_t)__builtin_return_address(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(panicstr != NULL || (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 		powerpc_softint(ci, ipl,
    538 		    (vaddr_t)__builtin_return_address(0));
    539 	}
    540 #endif /* __HAVE_FAST_SOFTINTS */
    541 	e500_splset(ci, ipl);
    542 #if 1
    543 	if (ipl < IPL_VM && old_ipl >= IPL_VM)
    544 		msr = PSL_EE;
    545 #endif
    546 	wrtee(msr);
    547 }
    548 
    549 static int
    550 e500_splraise(int ipl)
    551 {
    552 	struct cpu_info * const ci = curcpu();
    553 	const int old_ipl = ci->ci_cpl;
    554 
    555 	/* if we paniced because of watchdog, PSL_CE will be clear.  */
    556 	KASSERT(panicstr != NULL || (mfmsr() & PSL_CE));
    557 
    558 	if (old_ipl < ipl) {
    559 		//const
    560 		register_t msr = wrtee(0);
    561 		e500_splset(ci, ipl);
    562 #if 1
    563 		if (old_ipl < IPL_VM && ipl >= IPL_VM)
    564 			msr = 0;
    565 #endif
    566 		wrtee(msr);
    567 	} else if (ipl == IPL_NONE) {
    568 		panic("%s: %p: cpl=%u: attempt to splraise(IPL_NONE)",
    569 		    __func__, __builtin_return_address(0), old_ipl);
    570 #if 0
    571 	} else if (old_ipl > ipl) {
    572 		printf("%s: %p: cpl=%u: ignoring splraise(%u) to lower ipl\n",
    573 		    __func__, __builtin_return_address(0), old_ipl, ipl);
    574 #endif
    575 	}
    576 
    577 	return old_ipl;
    578 }
    579 
    580 static int
    581 e500_intr_spurious(void *arg)
    582 {
    583 	return 0;
    584 }
    585 
    586 static bool
    587 e500_intr_irq_info_get(struct cpu_info *ci, u_int irq, int ipl, int ist,
    588 	struct e500_intr_irq_info *ii)
    589 {
    590 	const struct e500_intr_info * const info = &e500_intr_info;
    591 	bool ok;
    592 
    593 #if DEBUG > 2
    594 	printf("%s(%p,irq=%u,ipl=%u,ist=%u,%p)\n", __func__, ci, irq, ipl, ist, ii);
    595 #endif
    596 
    597 	if (ipl < IPL_VM || ipl > IPL_HIGH) {
    598 #if DEBUG > 2
    599 		printf("%s:%d ipl=%u\n", __func__, __LINE__, ipl);
    600 #endif
    601 		return false;
    602 	}
    603 
    604 	if (ist <= IST_NONE || ist >= IST_MAX) {
    605 #if DEBUG > 2
    606 		printf("%s:%d ist=%u\n", __func__, __LINE__, ist);
    607 #endif
    608 		return false;
    609 	}
    610 
    611 	ii->irq_vector = irq + info->ii_ist_vectors[ist];
    612 	if (IST_PERCPU_P(ist) && ist != IST_IPI)
    613 		ii->irq_vector += ci->ci_cpuid * info->ii_percpu_sources;
    614 
    615 	switch (ist) {
    616 	default:
    617 		ii->irq_vpr = OPENPIC_EIVPR(irq);
    618 		ii->irq_dr  = OPENPIC_EIDR(irq);
    619 		ok = irq < info->ii_external_sources
    620 		    && (ist == IST_EDGE
    621 			|| ist == IST_LEVEL_LOW
    622 			|| ist == IST_LEVEL_HIGH);
    623 		break;
    624 	case IST_PULSE:
    625 		ok = false;
    626 		break;
    627 	case IST_ONCHIP:
    628 		ii->irq_vpr = OPENPIC_IIVPR(irq);
    629 		ii->irq_dr  = OPENPIC_IIDR(irq);
    630 		ok = irq < 32 * __arraycount(info->ii_onchip_bitmap);
    631 #if DEBUG > 2
    632 		printf("%s: irq=%u: ok=%u\n", __func__, irq, ok);
    633 #endif
    634 		ok = ok && (info->ii_onchip_bitmap[irq/32] & (1 << (irq & 31)));
    635 #if DEBUG > 2
    636 		printf("%s: %08x%08x -> %08x%08x: ok=%u\n", __func__,
    637 		    irq < 32 ? 0 : (1 << irq), irq < 32 ? (1 << irq) : 0,
    638 		    info->ii_onchip_bitmap[1], info->ii_onchip_bitmap[0],
    639 		    ok);
    640 #endif
    641 		break;
    642 	case IST_MSIGROUP:
    643 		ii->irq_vpr = OPENPIC_MSIVPR(irq);
    644 		ii->irq_dr  = OPENPIC_MSIDR(irq);
    645 		ok = irq < info->ii_msigroup_sources
    646 		    && ipl == IPL_VM;
    647 		break;
    648 	case IST_TIMER:
    649 		ii->irq_vpr = OPENPIC_GTVPR(ci->ci_cpuid, irq);
    650 		ii->irq_dr  = OPENPIC_GTDR(ci->ci_cpuid, irq);
    651 		ok = irq < info->ii_timer_sources;
    652 #if DEBUG > 2
    653 		printf("%s: IST_TIMER irq=%u: ok=%u\n", __func__, irq, ok);
    654 #endif
    655 		break;
    656 	case IST_IPI:
    657 		ii->irq_vpr = OPENPIC_IPIVPR(irq);
    658 		ii->irq_dr  = OPENPIC_IPIDR(irq);
    659 		ok = irq < info->ii_ipi_sources;
    660 		break;
    661 	case IST_MI:
    662 		ii->irq_vpr = OPENPIC_MIVPR(irq);
    663 		ii->irq_dr  = OPENPIC_MIDR(irq);
    664 		ok = irq < info->ii_mi_sources;
    665 		break;
    666 	}
    667 
    668 	return ok;
    669 }
    670 
    671 static const char *
    672 e500_intr_string(int irq, int ist)
    673 {
    674 	struct cpu_info * const ci = curcpu();
    675 	struct cpu_softc * const cpu = ci->ci_softc;
    676 	struct e500_intr_irq_info ii;
    677 
    678 	if (!e500_intr_irq_info_get(ci, irq, IPL_VM, ist, &ii))
    679 		return NULL;
    680 
    681 	return cpu->cpu_evcnt_intrs[ii.irq_vector].ev_name;
    682 }
    683 
    684 __CTASSERT(__arraycount(ist_names) == IST_MAX);
    685 
    686 static const char *
    687 e500_intr_typename(int ist)
    688 {
    689 	if (IST_NONE <= ist && ist < IST_MAX)
    690 		return ist_names[ist];
    691 
    692 	return NULL;
    693 }
    694 
    695 static void *
    696 e500_intr_cpu_establish(struct cpu_info *ci, int irq, int ipl, int ist,
    697 	int (*handler)(void *), void *arg)
    698 {
    699 	struct cpu_softc * const cpu = ci->ci_softc;
    700 	struct e500_intr_irq_info ii;
    701 
    702 	KASSERT(ipl >= IPL_VM && ipl <= IPL_HIGH);
    703 	KASSERT(ist > IST_NONE && ist < IST_MAX && ist != IST_MSI);
    704 
    705 	if (!e500_intr_irq_info_get(ci, irq, ipl, ist, &ii)) {
    706 		printf("%s: e500_intr_irq_info_get(%p,%u,%u,%u,%p) failed\n",
    707 		    __func__, ci, irq, ipl, ist, &ii);
    708 		return NULL;
    709 	}
    710 
    711 	struct intr_source * const is = &e500_intr_sources[ii.irq_vector];
    712 	mutex_enter(&e500_intr_lock);
    713 	if (is->is_ipl != IPL_NONE)
    714 		return NULL;
    715 
    716 	is->is_func = handler;
    717 	is->is_arg = arg;
    718 	is->is_ipl = ipl;
    719 	is->is_ist = ist;
    720 	is->is_irq = irq;
    721 	is->is_vpr = ii.irq_vpr;
    722 	is->is_dr = ii.irq_dr;
    723 
    724 	uint32_t vpr = VPR_PRIORITY_MAKE(IPL2CTPR(ipl))
    725 	    | VPR_VECTOR_MAKE(((ii.irq_vector + 1) << 4) | ipl)
    726 	    | (ist == IST_LEVEL_LOW
    727 		? VPR_LEVEL_LOW
    728 		: (ist == IST_LEVEL_HIGH
    729 		    ? VPR_LEVEL_HIGH
    730 		    : (ist == IST_ONCHIP
    731 		      ? VPR_P_HIGH
    732 		      : 0)));
    733 
    734 	/*
    735 	 * All interrupts go to the primary except per-cpu interrupts which get
    736 	 * routed to the appropriate cpu.
    737 	 */
    738 	uint32_t dr = openpic_read(cpu, ii.irq_dr);
    739 
    740 	dr |= 1 << (IST_PERCPU_P(ist) ? ci->ci_cpuid : 0);
    741 
    742 	/*
    743 	 * Update the vector/priority and destination registers keeping the
    744 	 * interrupt masked.
    745 	 */
    746 	const register_t msr = wrtee(0);	/* disable interrupts */
    747 	openpic_write(cpu, ii.irq_vpr, vpr | VPR_MSK);
    748 	openpic_write(cpu, ii.irq_dr, dr);
    749 
    750 	/*
    751 	 * Now unmask the interrupt.
    752 	 */
    753 	openpic_write(cpu, ii.irq_vpr, vpr);
    754 
    755 	wrtee(msr);				/* re-enable interrupts */
    756 
    757 	mutex_exit(&e500_intr_lock);
    758 
    759 	return is;
    760 }
    761 
    762 static void *
    763 e500_intr_establish(int irq, int ipl, int ist,
    764 	int (*handler)(void *), void *arg)
    765 {
    766 	return e500_intr_cpu_establish(curcpu(), irq, ipl, ist, handler, arg);
    767 }
    768 
    769 static void
    770 e500_intr_disestablish(void *vis)
    771 {
    772 	struct cpu_softc * const cpu = curcpu()->ci_softc;
    773 	struct intr_source * const is = vis;
    774 	struct e500_intr_irq_info ii;
    775 
    776 	KASSERT(e500_intr_sources <= is);
    777 	KASSERT(is < e500_intr_last_source);
    778 	KASSERT(!cpu_intr_p());
    779 
    780 	bool ok = e500_intr_irq_info_get(curcpu(), is->is_irq, is->is_ipl,
    781 	    is->is_ist, &ii);
    782 	(void)ok;	/* appease gcc */
    783 	KASSERT(ok);
    784 	KASSERT(is - e500_intr_sources == ii.irq_vector);
    785 
    786 	mutex_enter(&e500_intr_lock);
    787 	/*
    788 	 * Mask the source using the mask (MSK) bit in the vector/priority reg.
    789 	 */
    790 	uint32_t vpr = openpic_read(cpu, ii.irq_vpr);
    791 	openpic_write(cpu, ii.irq_vpr, VPR_MSK | vpr);
    792 
    793 	/*
    794 	 * Wait for the Activity (A) bit for the source to be cleared.
    795 	 */
    796 	while (openpic_read(cpu, ii.irq_vpr) & VPR_A)
    797 		;
    798 
    799 	/*
    800 	 * Now the source can be modified.
    801 	 */
    802 	openpic_write(cpu, ii.irq_dr, 0);		/* stop delivery */
    803 	openpic_write(cpu, ii.irq_vpr, VPR_MSK);	/* mask/reset it */
    804 
    805 	*is = (struct intr_source)INTR_SOURCE_INITIALIZER;
    806 
    807 	mutex_exit(&e500_intr_lock);
    808 }
    809 
    810 static void
    811 e500_critintr(struct trapframe *tf)
    812 {
    813 	panic("%s: srr0/srr1=%#lx/%#lx", __func__, tf->tf_srr0, tf->tf_srr1);
    814 }
    815 
    816 static void
    817 e500_decrintr(struct trapframe *tf)
    818 {
    819 	panic("%s: srr0/srr1=%#lx/%#lx", __func__, tf->tf_srr0, tf->tf_srr1);
    820 }
    821 
    822 static void
    823 e500_fitintr(struct trapframe *tf)
    824 {
    825 	panic("%s: srr0/srr1=%#lx/%#lx", __func__, tf->tf_srr0, tf->tf_srr1);
    826 }
    827 
    828 static void
    829 e500_wdogintr(struct trapframe *tf)
    830 {
    831 	mtspr(SPR_TSR, TSR_ENW|TSR_WIS);
    832 	panic("%s: tf=%p tb=%"PRId64" srr0/srr1=%#lx/%#lx", __func__, tf,
    833 	    mftb(), tf->tf_srr0, tf->tf_srr1);
    834 }
    835 
    836 static void
    837 e500_extintr(struct trapframe *tf)
    838 {
    839 	struct cpu_info * const ci = curcpu();
    840 	struct cpu_softc * const cpu = ci->ci_softc;
    841 	const int old_ipl = ci->ci_cpl;
    842 
    843 	/* if we paniced because of watchdog, PSL_CE will be clear.  */
    844 	KASSERT(panicstr != NULL || (mfmsr() & PSL_CE));
    845 
    846 #if 0
    847 //	printf("%s(%p): idepth=%d enter\n", __func__, tf, ci->ci_idepth);
    848 	if ((register_t)tf >= (register_t)curlwp->l_addr + USPACE
    849 	    || (register_t)tf < (register_t)curlwp->l_addr + NBPG) {
    850 		printf("%s(entry): pid %d.%d (%s): srr0/srr1=%#lx/%#lx: invalid tf addr %p\n",
    851 		    __func__, curlwp->l_proc->p_pid, curlwp->l_lid,
    852 		    curlwp->l_proc->p_comm, tf->tf_srr0, tf->tf_srr1, tf);
    853 	}
    854 #endif
    855 
    856 
    857 	ci->ci_data.cpu_nintr++;
    858 	tf->tf_cf.cf_idepth = ci->ci_idepth++;
    859 	cpu->cpu_pcpls[ci->ci_idepth] = old_ipl;
    860 #if 1
    861 	if (mfmsr() & PSL_EE)
    862 		panic("%s(%p): MSR[EE] is on (%#lx)!", __func__, tf, mfmsr());
    863 	if (old_ipl == IPL_HIGH
    864 	    || IPL2CTPR(old_ipl) != openpic_read(cpu, OPENPIC_CTPR))
    865 		panic("%s(%p): old_ipl(%u) == IPL_HIGH(%u) "
    866 		    "|| old_ipl + %u != OPENPIC_CTPR (%u)",
    867 		    __func__, tf, old_ipl, IPL_HIGH,
    868 		    15 - IPL_HIGH, openpic_read(cpu, OPENPIC_CTPR));
    869 #else
    870 	if (old_ipl >= IPL_VM)
    871 		panic("%s(%p): old_ipl(%u) >= IPL_VM(%u) CTPR=%u",
    872 		    __func__, tf, old_ipl, IPL_VM, openpic_read(cpu, OPENPIC_CTPR));
    873 #endif
    874 
    875 	for (;;) {
    876 		/*
    877 		 * Find out the pending interrupt.
    878 		 */
    879 	if (mfmsr() & PSL_EE)
    880 		panic("%s(%p): MSR[EE] turned on (%#lx)!", __func__, tf, mfmsr());
    881 		if (IPL2CTPR(old_ipl) != openpic_read(cpu, OPENPIC_CTPR))
    882 			panic("%s(%p): %d: old_ipl(%u) + %u != OPENPIC_CTPR (%u)",
    883 			    __func__, tf, __LINE__, old_ipl,
    884 			    15 - IPL_HIGH, openpic_read(cpu, OPENPIC_CTPR));
    885 		const uint32_t iack = openpic_read(cpu, OPENPIC_IACK);
    886 #ifdef DIAGNOSTIC
    887 		const int ipl = iack & 0xf;
    888 #endif
    889 		const int irq = (iack >> 4) - 1;
    890 #if 0
    891 		printf("%s: iack=%d ipl=%d irq=%d <%s>\n",
    892 		    __func__, iack, ipl, irq,
    893 		    (iack != IRQ_SPURIOUS ?
    894 			cpu->cpu_evcnt_intrs[irq].ev_name : "spurious"));
    895 #endif
    896 		if (IPL2CTPR(old_ipl) != openpic_read(cpu, OPENPIC_CTPR))
    897 			panic("%s(%p): %d: old_ipl(%u) + %u != OPENPIC_CTPR (%u)",
    898 			    __func__, tf, __LINE__, old_ipl,
    899 			    15 - IPL_HIGH, openpic_read(cpu, OPENPIC_CTPR));
    900 		if (iack == IRQ_SPURIOUS)
    901 			break;
    902 
    903 		struct intr_source * const is = &e500_intr_sources[irq];
    904 		if (__predict_true(is < e500_intr_last_source)) {
    905 			/*
    906 			 * Timer interrupts get their argument overriden with
    907 			 * the pointer to the trapframe.
    908 			 */
    909 			KASSERT(is->is_ipl == ipl);
    910 			void *arg = (is->is_ist == IST_TIMER ? tf : is->is_arg);
    911 			if (is->is_ipl <= old_ipl)
    912 				panic("%s(%p): %s (%u): is->is_ipl (%u) <= old_ipl (%u)\n",
    913 				    __func__, tf,
    914 				    cpu->cpu_evcnt_intrs[irq].ev_name, irq,
    915 				    is->is_ipl, old_ipl);
    916 			KASSERT(is->is_ipl > old_ipl);
    917 			e500_splset(ci, is->is_ipl);	/* change IPL */
    918 			if (__predict_false(is->is_func == NULL)) {
    919 				aprint_error_dev(ci->ci_dev,
    920 				    "interrupt from unestablished irq %d\n",
    921 				    irq);
    922 			} else {
    923 				int (*func)(void *) = is->is_func;
    924 				wrtee(PSL_EE);
    925 				int rv = (*func)(arg);
    926 				wrtee(0);
    927 #if DEBUG > 2
    928 				printf("%s: %s handler %p(%p) returned %d\n",
    929 				    __func__,
    930 				    cpu->cpu_evcnt_intrs[irq].ev_name,
    931 				    func, arg, rv);
    932 #endif
    933 				if (rv == 0)
    934 					cpu->cpu_evcnt_spurious_intr.ev_count++;
    935 			}
    936 			e500_splset(ci, old_ipl);	/* restore IPL */
    937 			cpu->cpu_evcnt_intrs[irq].ev_count++;
    938 		} else {
    939 			aprint_error_dev(ci->ci_dev,
    940 			    "interrupt from illegal irq %d\n", irq);
    941 			cpu->cpu_evcnt_spurious_intr.ev_count++;
    942 		}
    943 		/*
    944 		 * If this is a nested interrupt, simply ack it and exit
    945 		 * because the loop we interrupted will complete looking
    946 		 * for interrupts.
    947 		 */
    948 	if (mfmsr() & PSL_EE)
    949 		panic("%s(%p): MSR[EE] left on (%#lx)!", __func__, tf, mfmsr());
    950 		if (IPL2CTPR(old_ipl) != openpic_read(cpu, OPENPIC_CTPR))
    951 			panic("%s(%p): %d: old_ipl(%u) + %u != OPENPIC_CTPR (%u)",
    952 			    __func__, tf, __LINE__, old_ipl,
    953 			    15 - IPL_HIGH, openpic_read(cpu, OPENPIC_CTPR));
    954 
    955 		openpic_write(cpu, OPENPIC_EOI, 0);
    956 		if (IPL2CTPR(old_ipl) != openpic_read(cpu, OPENPIC_CTPR))
    957 			panic("%s(%p): %d: old_ipl(%u) + %u != OPENPIC_CTPR (%u)",
    958 			    __func__, tf, __LINE__, old_ipl,
    959 			    15 - IPL_HIGH, openpic_read(cpu, OPENPIC_CTPR));
    960 		if (ci->ci_idepth > 0)
    961 			break;
    962 	}
    963 
    964 	ci->ci_idepth--;
    965 
    966 #ifdef __HAVE_FAST_SOFTINTS
    967 	/*
    968 	 * Before exiting, deal with any softints that need to be dealt with.
    969 	 */
    970 	const u_int softints = ci->ci_data.cpu_softints & (IPL_SOFTMASK << old_ipl);
    971 	if (__predict_false(softints != 0)) {
    972 		KASSERT(old_ipl < IPL_VM);
    973 		e500_splset(ci, IPL_HIGH);	/* pop to high */
    974 		powerpc_softint(ci, old_ipl,	/* deal with them */
    975 		    tf->tf_srr0);
    976 		e500_splset(ci, old_ipl);	/* and drop back */
    977 	}
    978 #endif /* __HAVE_FAST_SOFTINTS */
    979 #if 1
    980 	KASSERT(ci->ci_cpl == old_ipl);
    981 #else
    982 	e500_splset(ci, old_ipl);		/* and drop back */
    983 #endif
    984 
    985 	/*
    986 	 * If we interrupted while power-saving and we need to exit idle,
    987 	 * we need to clear PSL_POW so we won't go back into power-saving.
    988 	 */
    989 	if (__predict_false(tf->tf_srr1 & PSL_POW) && ci->ci_want_resched)
    990 		tf->tf_srr1 &= ~PSL_POW;
    991 
    992 //	printf("%s(%p): idepth=%d exit\n", __func__, tf, ci->ci_idepth);
    993 }
    994 
    995 static void
    996 e500_intr_init(void)
    997 {
    998 	struct cpu_info * const ci = curcpu();
    999 	struct cpu_softc * const cpu = ci->ci_softc;
   1000 	const uint32_t frr = openpic_read(cpu, OPENPIC_FRR);
   1001 	const u_int nirq = FRR_NIRQ_GET(frr) + 1;
   1002 //	const u_int ncpu = FRR_NCPU_GET(frr) + 1;
   1003 	struct intr_source *is;
   1004 	struct e500_intr_info * const ii = &e500_intr_info;
   1005 
   1006 	const uint16_t svr = (mfspr(SPR_SVR) & ~0x80000) >> 16;
   1007 	switch (svr) {
   1008 #ifdef MPC8536
   1009 	case SVR_MPC8536v1 >> 16:
   1010 		*ii = mpc8536_intr_info;
   1011 		break;
   1012 #endif
   1013 #ifdef MPC8544
   1014 	case SVR_MPC8544v1 >> 16:
   1015 		*ii = mpc8544_intr_info;
   1016 		break;
   1017 #endif
   1018 #ifdef MPC8548
   1019 	case SVR_MPC8543v1 >> 16:
   1020 	case SVR_MPC8548v1 >> 16:
   1021 		*ii = mpc8548_intr_info;
   1022 		break;
   1023 #endif
   1024 #ifdef MPC8555
   1025 	case SVR_MPC8541v1 >> 16:
   1026 	case SVR_MPC8555v1 >> 16:
   1027 		*ii = mpc8555_intr_info;
   1028 		break;
   1029 #endif
   1030 #ifdef MPC8568
   1031 	case SVR_MPC8568v1 >> 16:
   1032 		*ii = mpc8568_intr_info;
   1033 		break;
   1034 #endif
   1035 #ifdef MPC8572
   1036 	case SVR_MPC8572v1 >> 16:
   1037 		*ii = mpc8572_intr_info;
   1038 		break;
   1039 #endif
   1040 #ifdef P1025
   1041 	case SVR_P1016v1 >> 16:
   1042 	case SVR_P1025v1 >> 16:
   1043 		*ii = p1025_intr_info;
   1044 		break;
   1045 #endif
   1046 #ifdef P2020
   1047 	case SVR_P2010v2 >> 16:
   1048 	case SVR_P2020v2 >> 16:
   1049 		*ii = p20x0_intr_info;
   1050 		break;
   1051 #endif
   1052 	default:
   1053 		panic("%s: don't know how to deal with SVR %#lx",
   1054 		    __func__, mfspr(SPR_SVR));
   1055 	}
   1056 
   1057 	/*
   1058 	 * We need to be in mixed mode.
   1059 	 */
   1060 	openpic_write(cpu, OPENPIC_GCR, GCR_M);
   1061 
   1062 	/*
   1063 	 * Make we and the openpic both agree about the current SPL level.
   1064 	 */
   1065 	e500_splset(ci, ci->ci_cpl);
   1066 
   1067 	/*
   1068 	 * Allow the required number of interrupt sources.
   1069 	 */
   1070 	is = kmem_zalloc(nirq * sizeof(*is), KM_SLEEP);
   1071 	KASSERT(is);
   1072 	e500_intr_sources = is;
   1073 	e500_intr_last_source = is + nirq;
   1074 
   1075 	/*
   1076 	 * Initialize all the external interrupts as active low.
   1077 	 */
   1078 	for (u_int irq = 0; irq < e500_intr_info.ii_external_sources; irq++) {
   1079 		openpic_write(cpu, OPENPIC_EIVPR(irq),
   1080 		    VPR_VECTOR_MAKE(irq) | VPR_LEVEL_LOW);
   1081 	}
   1082 }
   1083 
   1084 static void
   1085 e500_idlespin(void)
   1086 {
   1087 	KASSERTMSG(curcpu()->ci_cpl == IPL_NONE,
   1088 	    "%s: cpu%u: ci_cpl (%d) != 0", __func__, cpu_number(),
   1089 	     curcpu()->ci_cpl);
   1090 	KASSERTMSG(CTPR2IPL(openpic_read(curcpu()->ci_softc, OPENPIC_CTPR)) == IPL_NONE,
   1091 	    "%s: cpu%u: CTPR (%d) != IPL_NONE", __func__, cpu_number(),
   1092 	     CTPR2IPL(openpic_read(curcpu()->ci_softc, OPENPIC_CTPR)));
   1093 	KASSERT(mfmsr() & PSL_EE);
   1094 
   1095 	if (powersave > 0)
   1096 		mtmsr(mfmsr() | PSL_POW);
   1097 }
   1098 
   1099 static void
   1100 e500_intr_cpu_attach(struct cpu_info *ci)
   1101 {
   1102 	struct cpu_softc * const cpu = ci->ci_softc;
   1103 	const char * const xname = device_xname(ci->ci_dev);
   1104 
   1105 	const u_int32_t frr = openpic_read(cpu, OPENPIC_FRR);
   1106 	const u_int nirq = FRR_NIRQ_GET(frr) + 1;
   1107 //	const u_int ncpu = FRR_NCPU_GET(frr) + 1;
   1108 
   1109 	const struct e500_intr_info * const info = &e500_intr_info;
   1110 
   1111 	cpu->cpu_clock_gtbcr = OPENPIC_GTBCR(ci->ci_cpuid, E500_CLOCK_TIMER);
   1112 
   1113 	cpu->cpu_evcnt_intrs =
   1114 	    kmem_zalloc(nirq * sizeof(cpu->cpu_evcnt_intrs[0]), KM_SLEEP);
   1115 	KASSERT(cpu->cpu_evcnt_intrs);
   1116 
   1117 	struct evcnt *evcnt = cpu->cpu_evcnt_intrs;
   1118 	for (size_t j = 0; j < info->ii_external_sources; j++, evcnt++) {
   1119 		const char *name = e500_intr_external_name_lookup(j);
   1120 		evcnt_attach_dynamic(evcnt, EVCNT_TYPE_INTR, NULL, xname, name);
   1121 	}
   1122 	KASSERT(evcnt == cpu->cpu_evcnt_intrs + info->ii_ist_vectors[IST_ONCHIP]);
   1123 	for (size_t j = 0; j < info->ii_onchip_sources; j++, evcnt++) {
   1124 		if (info->ii_onchip_bitmap[j / 32] & __BIT(j & 31)) {
   1125 			const char *name = e500_intr_onchip_name_lookup(j);
   1126 			if (name != NULL) {
   1127 				evcnt_attach_dynamic(evcnt, EVCNT_TYPE_INTR,
   1128 				    NULL, xname, name);
   1129 #ifdef DIAGNOSTIC
   1130 			} else {
   1131 				printf("%s: missing evcnt for onchip irq %zu\n",
   1132 				    __func__, j);
   1133 #endif
   1134 			}
   1135 		}
   1136 	}
   1137 
   1138 	KASSERT(evcnt == cpu->cpu_evcnt_intrs + info->ii_ist_vectors[IST_MSIGROUP]);
   1139 	for (size_t j = 0; j < info->ii_msigroup_sources; j++, evcnt++) {
   1140 		evcnt_attach_dynamic(evcnt, EVCNT_TYPE_INTR,
   1141 		    NULL, xname, e500_msigroup_intr_names[j].in_name);
   1142 	}
   1143 
   1144 	KASSERT(evcnt == cpu->cpu_evcnt_intrs + info->ii_ist_vectors[IST_TIMER]);
   1145 	evcnt += ci->ci_cpuid * info->ii_percpu_sources;
   1146 	for (size_t j = 0; j < info->ii_timer_sources; j++, evcnt++) {
   1147 		evcnt_attach_dynamic(evcnt, EVCNT_TYPE_INTR,
   1148 		    NULL, xname, e500_timer_intr_names[j].in_name);
   1149 	}
   1150 
   1151 	for (size_t j = 0; j < info->ii_ipi_sources; j++, evcnt++) {
   1152 		evcnt_attach_dynamic(evcnt, EVCNT_TYPE_INTR,
   1153 		    NULL, xname, e500_ipi_intr_names[j].in_name);
   1154 	}
   1155 
   1156 	for (size_t j = 0; j < info->ii_mi_sources; j++, evcnt++) {
   1157 		evcnt_attach_dynamic(evcnt, EVCNT_TYPE_INTR,
   1158 		    NULL, xname, e500_mi_intr_names[j].in_name);
   1159 	}
   1160 
   1161 	ci->ci_idlespin = e500_idlespin;
   1162 }
   1163 
   1164 static void
   1165 e500_intr_cpu_send_ipi(cpuid_t target, uint32_t ipimsg)
   1166 {
   1167 	struct cpu_info * const ci = curcpu();
   1168 	struct cpu_softc * const cpu = ci->ci_softc;
   1169 	uint32_t dstmask;
   1170 
   1171 	if (target >= CPU_MAXNUM) {
   1172 		CPU_INFO_ITERATOR cii;
   1173 		struct cpu_info *dst_ci;
   1174 
   1175 		KASSERT(target == IPI_DST_NOTME || target == IPI_DST_ALL);
   1176 
   1177 		dstmask = 0;
   1178 		for (CPU_INFO_FOREACH(cii, dst_ci)) {
   1179 			if (target == IPI_DST_ALL || ci != dst_ci) {
   1180 				dstmask |= 1 << cpu_index(ci);
   1181 				if (ipimsg)
   1182 					atomic_or_32(&dst_ci->ci_pending_ipis,
   1183 					    ipimsg);
   1184 			}
   1185 		}
   1186 	} else {
   1187 		struct cpu_info * const dst_ci = cpu_lookup(target);
   1188 		KASSERT(dst_ci != NULL);
   1189 		KASSERTMSG(target == cpu_index(dst_ci),
   1190 		    "%s: target (%lu) != cpu_index(cpu%u)",
   1191 		     __func__, target, cpu_index(dst_ci));
   1192 		dstmask = (1 << target);
   1193 		if (ipimsg)
   1194 			atomic_or_32(&dst_ci->ci_pending_ipis, ipimsg);
   1195 	}
   1196 
   1197 	openpic_write(cpu, OPENPIC_IPIDR(0), dstmask);
   1198 }
   1199 
   1200 typedef void (*ipifunc_t)(void);
   1201 
   1202 #ifdef __HAVE_PREEEMPTION
   1203 static void
   1204 e500_ipi_kpreempt(void)
   1205 {
   1206 	poowerpc_softint_trigger(1 << IPL_NONE);
   1207 }
   1208 #endif
   1209 
   1210 static const ipifunc_t e500_ipifuncs[] = {
   1211 	[ilog2(IPI_XCALL)] =	xc_ipi_handler,
   1212 	[ilog2(IPI_HALT)] =	e500_ipi_halt,
   1213 #ifdef __HAVE_PREEMPTION
   1214 	[ilog2(IPI_KPREEMPT)] =	e500_ipi_kpreempt,
   1215 #endif
   1216 	[ilog2(IPI_TLB1SYNC)] =	e500_tlb1_sync,
   1217 };
   1218 
   1219 static int
   1220 e500_ipi_intr(void *v)
   1221 {
   1222 	struct cpu_info * const ci = curcpu();
   1223 
   1224 	ci->ci_ev_ipi.ev_count++;
   1225 
   1226 	uint32_t pending_ipis = atomic_swap_32(&ci->ci_pending_ipis, 0);
   1227 	for (u_int ipi = 31; pending_ipis != 0; ipi--, pending_ipis <<= 1) {
   1228 		const u_int bits = __builtin_clz(pending_ipis);
   1229 		ipi -= bits;
   1230 		pending_ipis <<= bits;
   1231 		KASSERT(e500_ipifuncs[ipi] != NULL);
   1232 		(*e500_ipifuncs[ipi])();
   1233 	}
   1234 
   1235 	return 1;
   1236 }
   1237 
   1238 static void
   1239 e500_intr_cpu_hatch(struct cpu_info *ci)
   1240 {
   1241 	/*
   1242 	 * Establish clock interrupt for this CPU.
   1243 	 */
   1244 	if (e500_intr_cpu_establish(ci, E500_CLOCK_TIMER, IPL_CLOCK, IST_TIMER,
   1245 	    e500_clock_intr, NULL) == NULL)
   1246 		panic("%s: failed to establish clock interrupt!", __func__);
   1247 
   1248 	/*
   1249 	 * Establish the IPI interrupts for this CPU.
   1250 	 */
   1251 	if (e500_intr_cpu_establish(ci, 0, IPL_VM, IST_IPI, e500_ipi_intr,
   1252 	    NULL) == NULL)
   1253 		panic("%s: failed to establish ipi interrupt!", __func__);
   1254 
   1255 	/*
   1256 	 * Enable watchdog interrupts.
   1257 	 */
   1258 	uint32_t tcr = mfspr(SPR_TCR);
   1259 	tcr |= TCR_WIE;
   1260 	mtspr(SPR_TCR, tcr);
   1261 }
   1262