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