Home | History | Annotate | Line # | Download | only in cavium
octeon_intr.c revision 1.16
      1  1.16  jmcneill /*	$NetBSD: octeon_intr.c,v 1.16 2020/07/17 17:57:16 jmcneill Exp $	*/
      2   1.1    hikaru /*
      3   1.1    hikaru  * Copyright 2001, 2002 Wasabi Systems, Inc.
      4   1.1    hikaru  * All rights reserved.
      5   1.1    hikaru  *
      6   1.1    hikaru  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
      7   1.1    hikaru  *
      8   1.1    hikaru  * Redistribution and use in source and binary forms, with or without
      9   1.1    hikaru  * modification, are permitted provided that the following conditions
     10   1.1    hikaru  * are met:
     11   1.1    hikaru  * 1. Redistributions of source code must retain the above copyright
     12   1.1    hikaru  *    notice, this list of conditions and the following disclaimer.
     13   1.1    hikaru  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1    hikaru  *    notice, this list of conditions and the following disclaimer in the
     15   1.1    hikaru  *    documentation and/or other materials provided with the distribution.
     16   1.1    hikaru  * 3. All advertising materials mentioning features or use of this software
     17   1.1    hikaru  *    must display the following acknowledgement:
     18   1.1    hikaru  *      This product includes software developed for the NetBSD Project by
     19   1.1    hikaru  *      Wasabi Systems, Inc.
     20   1.1    hikaru  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     21   1.1    hikaru  *    or promote products derived from this software without specific prior
     22   1.1    hikaru  *    written permission.
     23   1.1    hikaru  *
     24   1.1    hikaru  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     25   1.1    hikaru  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26   1.1    hikaru  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27   1.1    hikaru  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     28   1.1    hikaru  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29   1.1    hikaru  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30   1.1    hikaru  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31   1.1    hikaru  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32   1.1    hikaru  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33   1.1    hikaru  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34   1.1    hikaru  * POSSIBILITY OF SUCH DAMAGE.
     35   1.1    hikaru  */
     36   1.1    hikaru 
     37   1.1    hikaru /*
     38   1.1    hikaru  * Platform-specific interrupt support for the MIPS Malta.
     39   1.1    hikaru  */
     40   1.1    hikaru 
     41   1.6     skrll #include "opt_multiprocessor.h"
     42   1.6     skrll 
     43   1.4      matt #include "cpunode.h"
     44   1.1    hikaru #define __INTR_PRIVATE
     45   1.1    hikaru 
     46   1.1    hikaru #include <sys/cdefs.h>
     47  1.16  jmcneill __KERNEL_RCSID(0, "$NetBSD: octeon_intr.c,v 1.16 2020/07/17 17:57:16 jmcneill Exp $");
     48   1.1    hikaru 
     49   1.1    hikaru #include <sys/param.h>
     50   1.1    hikaru #include <sys/cpu.h>
     51   1.1    hikaru #include <sys/systm.h>
     52   1.1    hikaru #include <sys/device.h>
     53   1.1    hikaru #include <sys/intr.h>
     54   1.1    hikaru #include <sys/kernel.h>
     55   1.3      matt #include <sys/kmem.h>
     56   1.3      matt #include <sys/atomic.h>
     57   1.1    hikaru 
     58   1.1    hikaru #include <lib/libkern/libkern.h>
     59   1.1    hikaru 
     60   1.1    hikaru #include <mips/locore.h>
     61   1.1    hikaru 
     62   1.1    hikaru #include <mips/cavium/dev/octeon_ciureg.h>
     63   1.1    hikaru #include <mips/cavium/octeonvar.h>
     64   1.1    hikaru 
     65   1.1    hikaru /*
     66   1.1    hikaru  * This is a mask of bits to clear in the SR when we go to a
     67   1.1    hikaru  * given hardware interrupt priority level.
     68   1.1    hikaru  */
     69   1.1    hikaru static const struct ipl_sr_map octeon_ipl_sr_map = {
     70   1.1    hikaru     .sr_bits = {
     71   1.1    hikaru 	[IPL_NONE] =		0,
     72   1.1    hikaru 	[IPL_SOFTCLOCK] =	MIPS_SOFT_INT_MASK_0,
     73   1.1    hikaru 	[IPL_SOFTNET] =		MIPS_SOFT_INT_MASK,
     74   1.1    hikaru 	[IPL_VM] =		MIPS_SOFT_INT_MASK | MIPS_INT_MASK_0,
     75   1.1    hikaru 	[IPL_SCHED] =		MIPS_SOFT_INT_MASK | MIPS_INT_MASK_0
     76   1.8     skrll 				    | MIPS_INT_MASK_1 | MIPS_INT_MASK_5,
     77   1.3      matt 	[IPL_DDB] =		MIPS_SOFT_INT_MASK | MIPS_INT_MASK_0
     78   1.3      matt 				    | MIPS_INT_MASK_1 | MIPS_INT_MASK_5,
     79   1.1    hikaru 	[IPL_HIGH] =		MIPS_INT_MASK,
     80   1.1    hikaru     },
     81   1.1    hikaru };
     82   1.1    hikaru 
     83  1.15  jmcneill const char * octeon_intrnames[NIRQS] = {
     84   1.1    hikaru 	"workq 0",
     85   1.1    hikaru 	"workq 1",
     86   1.1    hikaru 	"workq 2",
     87   1.1    hikaru 	"workq 3",
     88   1.1    hikaru 	"workq 4",
     89   1.1    hikaru 	"workq 5",
     90   1.1    hikaru 	"workq 6",
     91   1.1    hikaru 	"workq 7",
     92   1.1    hikaru 	"workq 8",
     93   1.1    hikaru 	"workq 9",
     94   1.1    hikaru 	"workq 10",
     95   1.1    hikaru 	"workq 11",
     96   1.1    hikaru 	"workq 12",
     97   1.1    hikaru 	"workq 13",
     98   1.1    hikaru 	"workq 14",
     99   1.1    hikaru 	"workq 15",
    100   1.1    hikaru 	"gpio 0",
    101   1.1    hikaru 	"gpio 1",
    102   1.1    hikaru 	"gpio 2",
    103   1.1    hikaru 	"gpio 3",
    104   1.1    hikaru 	"gpio 4",
    105   1.1    hikaru 	"gpio 5",
    106   1.1    hikaru 	"gpio 6",
    107   1.1    hikaru 	"gpio 7",
    108   1.1    hikaru 	"gpio 8",
    109   1.1    hikaru 	"gpio 9",
    110   1.1    hikaru 	"gpio 10",
    111   1.1    hikaru 	"gpio 11",
    112   1.1    hikaru 	"gpio 12",
    113   1.1    hikaru 	"gpio 13",
    114   1.1    hikaru 	"gpio 14",
    115   1.1    hikaru 	"gpio 15",
    116   1.1    hikaru 	"mbox 0-15",
    117   1.1    hikaru 	"mbox 16-31",
    118   1.1    hikaru 	"uart 0",
    119   1.1    hikaru 	"uart 1",
    120   1.1    hikaru 	"pci inta",
    121   1.1    hikaru 	"pci intb",
    122   1.1    hikaru 	"pci intc",
    123   1.1    hikaru 	"pci intd",
    124   1.1    hikaru 	"pci msi 0-15",
    125   1.1    hikaru 	"pci msi 16-31",
    126   1.1    hikaru 	"pci msi 32-47",
    127   1.1    hikaru 	"pci msi 48-63",
    128   1.1    hikaru 	"wdog summary",
    129   1.1    hikaru 	"twsi",
    130   1.1    hikaru 	"rml",
    131   1.1    hikaru 	"trace",
    132   1.1    hikaru 	"gmx drop",
    133   1.1    hikaru 	"reserved",
    134   1.1    hikaru 	"ipd drop",
    135   1.1    hikaru 	"reserved",
    136   1.1    hikaru 	"timer 0",
    137   1.1    hikaru 	"timer 1",
    138   1.1    hikaru 	"timer 2",
    139   1.1    hikaru 	"timer 3",
    140   1.1    hikaru 	"usb",
    141   1.1    hikaru 	"pcm/tdm",
    142   1.1    hikaru 	"mpi/spi",
    143   1.1    hikaru 	"reserved",
    144   1.1    hikaru 	"reserved",
    145   1.1    hikaru 	"reserved",
    146   1.1    hikaru 	"reserved",
    147   1.1    hikaru 	"reserved",
    148   1.1    hikaru };
    149   1.1    hikaru 
    150   1.1    hikaru struct octeon_intrhand {
    151   1.1    hikaru 	int (*ih_func)(void *);
    152   1.1    hikaru 	void *ih_arg;
    153   1.1    hikaru 	int ih_irq;
    154   1.1    hikaru 	int ih_ipl;
    155   1.1    hikaru };
    156   1.1    hikaru 
    157   1.3      matt #ifdef MULTIPROCESSOR
    158   1.3      matt static int octeon_send_ipi(struct cpu_info *, int);
    159   1.3      matt static int octeon_ipi_intr(void *);
    160   1.3      matt 
    161   1.3      matt struct octeon_intrhand ipi_intrhands[2] = {
    162   1.3      matt 	[0] = {
    163   1.3      matt 		.ih_func = octeon_ipi_intr,
    164   1.3      matt 		.ih_arg = (void *)(uintptr_t)__BITS(15,0),
    165  1.12    simonb 		.ih_irq = CIU_INT_MBOX_15_0,
    166   1.3      matt 		.ih_ipl = IPL_SCHED,
    167   1.3      matt 	},
    168   1.3      matt 	[1] = {
    169   1.3      matt 		.ih_func = octeon_ipi_intr,
    170   1.3      matt 		.ih_arg = (void *)(uintptr_t)__BITS(31,16),
    171  1.12    simonb 		.ih_irq = CIU_INT_MBOX_31_16,
    172   1.3      matt 		.ih_ipl = IPL_HIGH,
    173   1.3      matt 	},
    174   1.1    hikaru };
    175   1.3      matt #endif
    176   1.1    hikaru 
    177  1.11    simonb struct octeon_intrhand *octciu_intrs[NIRQS] = {
    178   1.3      matt #ifdef MULTIPROCESSOR
    179  1.12    simonb 	[CIU_INT_MBOX_15_0] = &ipi_intrhands[0],
    180  1.12    simonb 	[CIU_INT_MBOX_31_16] = &ipi_intrhands[1],
    181   1.3      matt #endif
    182   1.1    hikaru };
    183   1.1    hikaru 
    184   1.3      matt kmutex_t octeon_intr_lock;
    185   1.1    hikaru 
    186   1.3      matt #define X(a)	MIPS_PHYS_TO_XKPHYS(OCTEON_CCA_NONE, (a))
    187   1.1    hikaru 
    188   1.3      matt struct cpu_softc octeon_cpu0_softc = {
    189   1.3      matt 	.cpu_ci = &cpu_info_store,
    190   1.3      matt 	.cpu_int0_sum0 = X(CIU_INT0_SUM0),
    191   1.3      matt 	.cpu_int1_sum0 = X(CIU_INT1_SUM0),
    192   1.3      matt 	.cpu_int2_sum0 = X(CIU_INT4_SUM0),
    193   1.1    hikaru 
    194  1.15  jmcneill 	.cpu_int_sum1 = X(CIU_INT_SUM1),
    195  1.15  jmcneill 
    196  1.16  jmcneill 	.cpu_int0_en[0] = X(CIU_INT0_EN0),
    197  1.16  jmcneill 	.cpu_int1_en[0] = X(CIU_INT1_EN0),
    198  1.16  jmcneill 	.cpu_int2_en[0] = X(CIU_INT4_EN00),
    199  1.16  jmcneill 
    200  1.16  jmcneill 	.cpu_int0_en[1] = X(CIU_INT0_EN1),
    201  1.16  jmcneill 	.cpu_int1_en[1] = X(CIU_INT1_EN1),
    202  1.16  jmcneill 	.cpu_int2_en[1] = X(CIU_INT4_EN01),
    203   1.1    hikaru 
    204   1.3      matt 	.cpu_int32_en = X(CIU_INT32_EN0),
    205   1.1    hikaru 
    206   1.4      matt 	.cpu_wdog = X(CIU_WDOG0),
    207   1.4      matt 	.cpu_pp_poke = X(CIU_PP_POKE0),
    208   1.4      matt 
    209   1.3      matt #ifdef MULTIPROCESSOR
    210   1.3      matt 	.cpu_mbox_set = X(CIU_MBOX_SET0),
    211   1.3      matt 	.cpu_mbox_clr = X(CIU_MBOX_CLR0),
    212   1.3      matt #endif
    213   1.3      matt };
    214   1.1    hikaru 
    215   1.3      matt #ifdef MULTIPROCESSOR
    216  1.12    simonb /* XXX limit of two CPUs ... */
    217   1.3      matt struct cpu_softc octeon_cpu1_softc = {
    218   1.3      matt 	.cpu_int0_sum0 = X(CIU_INT2_SUM0),
    219   1.3      matt 	.cpu_int1_sum0 = X(CIU_INT3_SUM0),
    220   1.3      matt 	.cpu_int2_sum0 = X(CIU_INT4_SUM1),
    221   1.3      matt 
    222  1.15  jmcneill 	.cpu_int_sum1 = X(CIU_INT_SUM1),
    223  1.15  jmcneill 
    224  1.16  jmcneill 	.cpu_int0_en[0] = X(CIU_INT2_EN0),
    225  1.16  jmcneill 	.cpu_int1_en[0] = X(CIU_INT3_EN0),
    226  1.16  jmcneill 	.cpu_int2_en[0] = X(CIU_INT4_EN10),
    227  1.16  jmcneill 
    228  1.16  jmcneill 	.cpu_int0_en[1] = X(CIU_INT2_EN1),
    229  1.16  jmcneill 	.cpu_int1_en[1] = X(CIU_INT3_EN1),
    230  1.16  jmcneill 	.cpu_int2_en[1] = X(CIU_INT4_EN11),
    231   1.1    hikaru 
    232   1.3      matt 	.cpu_int32_en = X(CIU_INT32_EN1),
    233   1.1    hikaru 
    234  1.12    simonb 	.cpu_wdog = X(CIU_WDOG(1)),
    235   1.4      matt 	.cpu_pp_poke = X(CIU_PP_POKE1),
    236   1.4      matt 
    237   1.3      matt 	.cpu_mbox_set = X(CIU_MBOX_SET1),
    238   1.3      matt 	.cpu_mbox_clr = X(CIU_MBOX_CLR1),
    239   1.3      matt };
    240   1.3      matt #endif
    241   1.1    hikaru 
    242   1.4      matt #ifdef DEBUG
    243   1.4      matt static void
    244   1.4      matt octeon_mbox_test(void)
    245   1.4      matt {
    246   1.4      matt 	const uint64_t mbox_clr0 = X(CIU_MBOX_CLR0);
    247   1.4      matt 	const uint64_t mbox_clr1 = X(CIU_MBOX_CLR1);
    248   1.4      matt 	const uint64_t mbox_set0 = X(CIU_MBOX_SET0);
    249   1.4      matt 	const uint64_t mbox_set1 = X(CIU_MBOX_SET1);
    250   1.4      matt 	const uint64_t int_sum0 = X(CIU_INT0_SUM0);
    251   1.4      matt 	const uint64_t int_sum1 = X(CIU_INT2_SUM0);
    252  1.12    simonb 	const uint64_t sum_mbox_lo = __BIT(CIU_INT_MBOX_15_0);
    253  1.12    simonb 	const uint64_t sum_mbox_hi = __BIT(CIU_INT_MBOX_31_16);
    254   1.4      matt 
    255   1.5      matt 	mips3_sd(mbox_clr0, ~0ULL);
    256   1.5      matt 	mips3_sd(mbox_clr1, ~0ULL);
    257   1.4      matt 
    258   1.5      matt 	uint32_t mbox0 = mips3_ld(mbox_set0);
    259   1.5      matt 	uint32_t mbox1 = mips3_ld(mbox_set1);
    260   1.4      matt 
    261   1.4      matt 	KDASSERTMSG(mbox0 == 0, "mbox0 %#x mbox1 %#x", mbox0, mbox1);
    262   1.4      matt 	KDASSERTMSG(mbox1 == 0, "mbox0 %#x mbox1 %#x", mbox0, mbox1);
    263   1.4      matt 
    264   1.5      matt 	mips3_sd(mbox_set0, __BIT(0));
    265   1.4      matt 
    266   1.5      matt 	mbox0 = mips3_ld(mbox_set0);
    267   1.5      matt 	mbox1 = mips3_ld(mbox_set1);
    268   1.4      matt 
    269   1.4      matt 	KDASSERTMSG(mbox0 == 1, "mbox0 %#x mbox1 %#x", mbox0, mbox1);
    270   1.4      matt 	KDASSERTMSG(mbox1 == 0, "mbox0 %#x mbox1 %#x", mbox0, mbox1);
    271   1.4      matt 
    272   1.5      matt 	uint64_t sum0 = mips3_ld(int_sum0);
    273   1.5      matt 	uint64_t sum1 = mips3_ld(int_sum1);
    274   1.4      matt 
    275   1.4      matt 	KDASSERTMSG((sum0 & sum_mbox_lo) != 0, "sum0 %#"PRIx64, sum0);
    276   1.4      matt 	KDASSERTMSG((sum0 & sum_mbox_hi) == 0, "sum0 %#"PRIx64, sum0);
    277   1.4      matt 
    278   1.4      matt 	KDASSERTMSG((sum1 & sum_mbox_lo) == 0, "sum1 %#"PRIx64, sum1);
    279   1.4      matt 	KDASSERTMSG((sum1 & sum_mbox_hi) == 0, "sum1 %#"PRIx64, sum1);
    280   1.4      matt 
    281   1.5      matt 	mips3_sd(mbox_clr0, mbox0);
    282   1.5      matt 	mbox0 = mips3_ld(mbox_set0);
    283   1.4      matt 	KDASSERTMSG(mbox0 == 0, "mbox0 %#x", mbox0);
    284   1.4      matt 
    285   1.5      matt 	mips3_sd(mbox_set0, __BIT(16));
    286   1.4      matt 
    287   1.5      matt 	mbox0 = mips3_ld(mbox_set0);
    288   1.5      matt 	mbox1 = mips3_ld(mbox_set1);
    289   1.4      matt 
    290   1.4      matt 	KDASSERTMSG(mbox0 == __BIT(16), "mbox0 %#x", mbox0);
    291   1.4      matt 	KDASSERTMSG(mbox1 == 0, "mbox1 %#x", mbox1);
    292   1.4      matt 
    293   1.5      matt 	sum0 = mips3_ld(int_sum0);
    294   1.5      matt 	sum1 = mips3_ld(int_sum1);
    295   1.4      matt 
    296   1.4      matt 	KDASSERTMSG((sum0 & sum_mbox_lo) == 0, "sum0 %#"PRIx64, sum0);
    297   1.4      matt 	KDASSERTMSG((sum0 & sum_mbox_hi) != 0, "sum0 %#"PRIx64, sum0);
    298   1.4      matt 
    299   1.4      matt 	KDASSERTMSG((sum1 & sum_mbox_lo) == 0, "sum1 %#"PRIx64, sum1);
    300   1.4      matt 	KDASSERTMSG((sum1 & sum_mbox_hi) == 0, "sum1 %#"PRIx64, sum1);
    301   1.4      matt }
    302   1.4      matt #endif
    303   1.4      matt 
    304   1.3      matt #undef X
    305   1.1    hikaru 
    306   1.3      matt void
    307   1.3      matt octeon_intr_init(struct cpu_info *ci)
    308   1.3      matt {
    309   1.9       mrg #ifdef DIAGNOSTIC
    310   1.3      matt 	const int cpunum = cpu_index(ci);
    311   1.9       mrg #endif
    312   1.3      matt 	const char * const xname = cpu_name(ci);
    313   1.4      matt 	struct cpu_softc *cpu = ci->ci_softc;
    314  1.16  jmcneill 	int bank;
    315   1.1    hikaru 
    316   1.1    hikaru 
    317   1.3      matt 	if (ci->ci_cpuid == 0) {
    318   1.4      matt 		KASSERT(ci->ci_softc == &octeon_cpu0_softc);
    319   1.4      matt 		ipl_sr_map = octeon_ipl_sr_map;
    320   1.3      matt 		mutex_init(&octeon_intr_lock, MUTEX_DEFAULT, IPL_HIGH);
    321   1.3      matt #ifdef MULTIPROCESSOR
    322   1.3      matt 		mips_locoresw.lsw_send_ipi = octeon_send_ipi;
    323   1.3      matt #endif
    324   1.4      matt #ifdef DEBUG
    325   1.4      matt 		octeon_mbox_test();
    326   1.4      matt #endif
    327   1.3      matt 	} else {
    328   1.3      matt 		KASSERT(cpunum == 1);
    329   1.3      matt #ifdef MULTIPROCESSOR
    330   1.4      matt 		KASSERT(ci->ci_softc == &octeon_cpu1_softc);
    331   1.3      matt #endif
    332   1.1    hikaru 	}
    333   1.1    hikaru 
    334   1.3      matt #ifdef MULTIPROCESSOR
    335   1.3      matt 	// Enable the IPIs
    336  1.16  jmcneill 	cpu->cpu_int1_enable[0] |= __BIT(CIU_INT_MBOX_15_0);
    337  1.16  jmcneill 	cpu->cpu_int2_enable[0] |= __BIT(CIU_INT_MBOX_31_16);
    338   1.1    hikaru #endif
    339   1.1    hikaru 
    340  1.16  jmcneill 	if (ci->ci_dev) {
    341  1.16  jmcneill 		for (bank = 0; bank < NBANKS; bank++) {
    342  1.16  jmcneill 			aprint_verbose_dev(ci->ci_dev,
    343  1.16  jmcneill 			    "enabling intr masks %u "
    344  1.16  jmcneill 			    " %#"PRIx64"/%#"PRIx64"/%#"PRIx64"\n",
    345  1.16  jmcneill 			    bank,
    346  1.16  jmcneill 			    cpu->cpu_int0_enable[0],
    347  1.16  jmcneill 			    cpu->cpu_int1_enable[0],
    348  1.16  jmcneill 			    cpu->cpu_int2_enable[0]);
    349  1.16  jmcneill 		}
    350  1.16  jmcneill 	}
    351  1.16  jmcneill 
    352  1.16  jmcneill 	for (bank = 0; bank < NBANKS; bank++) {
    353  1.16  jmcneill 		mips3_sd(cpu->cpu_int0_en[bank], cpu->cpu_int0_enable[bank]);
    354  1.16  jmcneill 		mips3_sd(cpu->cpu_int1_en[bank], cpu->cpu_int1_enable[bank]);
    355  1.16  jmcneill 		mips3_sd(cpu->cpu_int2_en[bank], cpu->cpu_int2_enable[bank]);
    356  1.16  jmcneill 	}
    357   1.3      matt 
    358   1.5      matt 	mips3_sd(cpu->cpu_int32_en, 0);
    359   1.3      matt 
    360   1.3      matt #ifdef MULTIPROCESSOR
    361   1.5      matt 	mips3_sd(cpu->cpu_mbox_clr, __BITS(31,0));
    362   1.3      matt #endif
    363   1.1    hikaru 
    364  1.15  jmcneill 	for (int i = 0; i < NIRQS; i++) {
    365  1.15  jmcneill 		if (octeon_intrnames[i] == NULL)
    366  1.15  jmcneill 			octeon_intrnames[i] = kmem_asprintf("irq %d", i);
    367   1.3      matt 		evcnt_attach_dynamic(&cpu->cpu_intr_evs[i],
    368   1.3      matt 		    EVCNT_TYPE_INTR, NULL, xname, octeon_intrnames[i]);
    369   1.1    hikaru 	}
    370   1.1    hikaru }
    371   1.1    hikaru 
    372   1.1    hikaru void
    373   1.1    hikaru octeon_cal_timer(int corefreq)
    374   1.1    hikaru {
    375   1.1    hikaru 	/* Compute the number of cycles per second. */
    376   1.1    hikaru 	curcpu()->ci_cpu_freq = corefreq;
    377   1.1    hikaru 
    378   1.1    hikaru 	/* Compute the number of ticks for hz. */
    379   1.1    hikaru 	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
    380   1.1    hikaru 
    381   1.1    hikaru 	/* Compute the delay divisor and reciprical. */
    382   1.1    hikaru 	curcpu()->ci_divisor_delay =
    383   1.1    hikaru 	    ((curcpu()->ci_cpu_freq + 500000) / 1000000);
    384   1.1    hikaru #if 0
    385   1.1    hikaru 	MIPS_SET_CI_RECIPRICAL(curcpu());
    386   1.1    hikaru #endif
    387   1.1    hikaru 
    388   1.1    hikaru 	mips3_cp0_count_write(0);
    389   1.1    hikaru 	mips3_cp0_compare_write(0);
    390   1.1    hikaru }
    391   1.1    hikaru 
    392   1.1    hikaru void *
    393   1.3      matt octeon_intr_establish(int irq, int ipl, int (*func)(void *), void *arg)
    394   1.1    hikaru {
    395   1.1    hikaru 	struct octeon_intrhand *ih;
    396   1.1    hikaru 
    397   1.1    hikaru 	if (irq >= NIRQS)
    398   1.1    hikaru 		panic("octeon_intr_establish: bogus IRQ %d", irq);
    399   1.3      matt 	if (ipl < IPL_VM)
    400   1.3      matt 		panic("octeon_intr_establish: bogus IPL %d", ipl);
    401   1.1    hikaru 
    402   1.3      matt 	ih = kmem_zalloc(sizeof(*ih), KM_NOSLEEP);
    403   1.1    hikaru 	if (ih == NULL)
    404   1.1    hikaru 		return (NULL);
    405   1.1    hikaru 
    406   1.1    hikaru 	ih->ih_func = func;
    407   1.1    hikaru 	ih->ih_arg = arg;
    408   1.1    hikaru 	ih->ih_irq = irq;
    409   1.3      matt 	ih->ih_ipl = ipl;
    410   1.1    hikaru 
    411   1.3      matt 	mutex_enter(&octeon_intr_lock);
    412   1.1    hikaru 
    413   1.1    hikaru 	/*
    414   1.3      matt 	 * First, make it known.
    415   1.1    hikaru 	 */
    416  1.11    simonb 	KASSERTMSG(octciu_intrs[irq] == NULL, "irq %d in use! (%p)",
    417  1.11    simonb 	    irq, octciu_intrs[irq]);
    418   1.3      matt 
    419  1.11    simonb 	octciu_intrs[irq] = ih;
    420   1.3      matt 	membar_producer();
    421   1.1    hikaru 
    422   1.1    hikaru 	/*
    423   1.1    hikaru 	 * Now enable it.
    424   1.1    hikaru 	 */
    425  1.15  jmcneill 	const int bank = irq / 64;
    426  1.15  jmcneill 	const uint64_t irq_mask = __BIT(irq % 64);
    427   1.3      matt 	struct cpu_softc * const cpu0 = &octeon_cpu0_softc;
    428   1.3      matt #if MULTIPROCESSOR
    429   1.3      matt 	struct cpu_softc * const cpu1 = &octeon_cpu1_softc;
    430   1.3      matt #endif
    431   1.3      matt 
    432   1.3      matt 	switch (ipl) {
    433   1.3      matt 	case IPL_VM:
    434  1.16  jmcneill 		cpu0->cpu_int0_enable[bank] |= irq_mask;
    435  1.16  jmcneill 		mips3_sd(cpu0->cpu_int0_en[bank], cpu0->cpu_int0_enable[bank]);
    436   1.3      matt 		break;
    437   1.1    hikaru 
    438   1.3      matt 	case IPL_SCHED:
    439  1.16  jmcneill 		cpu0->cpu_int1_enable[bank] |= irq_mask;
    440  1.16  jmcneill 		mips3_sd(cpu0->cpu_int1_en[bank], cpu0->cpu_int1_enable[bank]);
    441   1.3      matt #ifdef MULTIPROCESSOR
    442  1.16  jmcneill 		cpu1->cpu_int1_enable[bank] = cpu0->cpu_int1_enable[bank];
    443  1.16  jmcneill 		mips3_sd(cpu1->cpu_int1_en[bank], cpu1->cpu_int1_enable[bank]);
    444   1.3      matt #endif
    445  1.15  jmcneill 
    446   1.3      matt 		break;
    447   1.3      matt 
    448   1.3      matt 	case IPL_DDB:
    449   1.3      matt 	case IPL_HIGH:
    450  1.16  jmcneill 		cpu0->cpu_int2_enable[bank] |= irq_mask;
    451  1.16  jmcneill 		mips3_sd(cpu0->cpu_int2_en[bank], cpu0->cpu_int2_enable[bank]);
    452   1.3      matt #ifdef MULTIPROCESSOR
    453  1.16  jmcneill 		cpu1->cpu_int2_enable[bank] = cpu0->cpu_int2_enable[bank];
    454  1.16  jmcneill 		mips3_sd(cpu1->cpu_int2_en[bank], cpu1->cpu_int2_enable[bank]);
    455   1.3      matt #endif
    456  1.16  jmcneill 
    457   1.3      matt 		break;
    458   1.1    hikaru 	}
    459   1.1    hikaru 
    460   1.3      matt 	mutex_exit(&octeon_intr_lock);
    461   1.3      matt 
    462   1.3      matt 	return ih;
    463   1.1    hikaru }
    464   1.1    hikaru 
    465   1.1    hikaru void
    466   1.1    hikaru octeon_intr_disestablish(void *cookie)
    467   1.1    hikaru {
    468   1.3      matt 	struct octeon_intrhand * const ih = cookie;
    469   1.3      matt 	const int irq = ih->ih_irq & (NIRQS-1);
    470   1.3      matt 	const int ipl = ih->ih_ipl;
    471   1.1    hikaru 
    472   1.3      matt 	mutex_enter(&octeon_intr_lock);
    473   1.1    hikaru 
    474   1.1    hikaru 	/*
    475   1.3      matt 	 * First disable it.
    476   1.1    hikaru 	 */
    477  1.15  jmcneill 	const int bank = irq / 64;
    478  1.15  jmcneill 	const uint64_t irq_mask = ~__BIT(irq % 64);
    479   1.3      matt 	struct cpu_softc * const cpu0 = &octeon_cpu0_softc;
    480   1.3      matt #if MULTIPROCESSOR
    481   1.3      matt 	struct cpu_softc * const cpu1 = &octeon_cpu1_softc;
    482   1.3      matt #endif
    483   1.3      matt 
    484   1.3      matt 	switch (ipl) {
    485   1.3      matt 	case IPL_VM:
    486  1.16  jmcneill 		cpu0->cpu_int0_enable[bank] &= ~irq_mask;
    487  1.16  jmcneill 		mips3_sd(cpu0->cpu_int0_en[bank], cpu0->cpu_int0_enable[bank]);
    488   1.3      matt 		break;
    489   1.3      matt 
    490   1.3      matt 	case IPL_SCHED:
    491  1.16  jmcneill 		cpu0->cpu_int1_enable[bank] &= ~irq_mask;
    492  1.16  jmcneill 		mips3_sd(cpu0->cpu_int1_en[bank], cpu0->cpu_int1_enable[bank]);
    493   1.3      matt #ifdef MULTIPROCESSOR
    494  1.16  jmcneill 		cpu1->cpu_int1_enable[bank] = cpu0->cpu_int1_enable[bank];
    495  1.16  jmcneill 		mips3_sd(cpu1->cpu_int1_en[bank], cpu1->cpu_int1_enable[bank]);
    496   1.3      matt #endif
    497   1.3      matt 		break;
    498   1.3      matt 
    499   1.3      matt 	case IPL_DDB:
    500   1.3      matt 	case IPL_HIGH:
    501  1.16  jmcneill 		cpu0->cpu_int2_enable[bank] &= ~irq_mask;
    502  1.16  jmcneill 		mips3_sd(cpu0->cpu_int2_en[bank], cpu0->cpu_int2_enable[bank]);
    503   1.3      matt #ifdef MULTIPROCESSOR
    504  1.16  jmcneill 		cpu1->cpu_int2_enable[bank] = cpu0->cpu_int2_enable[bank];
    505  1.16  jmcneill 		mips3_sd(cpu1->cpu_int2_en[bank], cpu1->cpu_int2_enable[bank]);
    506   1.3      matt #endif
    507   1.3      matt 		break;
    508   1.3      matt 	}
    509   1.1    hikaru 
    510   1.1    hikaru 	/*
    511   1.3      matt 	 * Now remove it since we shouldn't get interrupts for it.
    512   1.1    hikaru 	 */
    513  1.11    simonb 	octciu_intrs[irq] = NULL;
    514   1.3      matt 
    515   1.3      matt 	mutex_exit(&octeon_intr_lock);
    516   1.1    hikaru 
    517   1.3      matt 	kmem_free(ih, sizeof(*ih));
    518   1.1    hikaru }
    519   1.1    hikaru 
    520   1.1    hikaru void
    521   1.1    hikaru octeon_iointr(int ipl, vaddr_t pc, uint32_t ipending)
    522   1.1    hikaru {
    523   1.3      matt 	struct cpu_info * const ci = curcpu();
    524   1.3      matt 	struct cpu_softc * const cpu = ci->ci_softc;
    525  1.15  jmcneill 	int bank;
    526   1.3      matt 
    527   1.4      matt 	KDASSERT(mips_cp0_status_read() & MIPS_SR_INT_IE);
    528   1.3      matt 	KASSERT((ipending & ~MIPS_INT_MASK) == 0);
    529   1.3      matt 	KASSERT(ipending & MIPS_HARD_INT_MASK);
    530  1.15  jmcneill 	uint64_t hwpend[2] = { 0, 0 };
    531  1.15  jmcneill 
    532  1.15  jmcneill 	const uint64_t sum1 = mips3_ld(cpu->cpu_int_sum1);
    533   1.1    hikaru 
    534   1.3      matt 	if (ipending & MIPS_INT_MASK_2) {
    535  1.15  jmcneill 		hwpend[0] = mips3_ld(cpu->cpu_int2_sum0)
    536  1.16  jmcneill 		    & cpu->cpu_int2_enable[0];
    537  1.16  jmcneill 		hwpend[1] = sum1 & cpu->cpu_int2_enable[1];
    538   1.3      matt 	} else if (ipending & MIPS_INT_MASK_1) {
    539  1.15  jmcneill 		hwpend[0] = mips3_ld(cpu->cpu_int1_sum0)
    540  1.16  jmcneill 		    & cpu->cpu_int1_enable[0];
    541  1.16  jmcneill 		hwpend[1] = sum1 & cpu->cpu_int1_enable[1];
    542   1.3      matt 	} else if (ipending & MIPS_INT_MASK_0) {
    543  1.15  jmcneill 		hwpend[0] = mips3_ld(cpu->cpu_int0_sum0)
    544  1.16  jmcneill 		    & cpu->cpu_int0_enable[0];
    545  1.16  jmcneill 		hwpend[1] = sum1 & cpu->cpu_int0_enable[1];
    546   1.3      matt 	} else {
    547   1.3      matt 		panic("octeon_iointr: unexpected ipending %#x", ipending);
    548   1.3      matt 	}
    549  1.15  jmcneill 	for (bank = 0; bank <= 1; bank++) {
    550  1.15  jmcneill 		while (hwpend[bank] != 0) {
    551  1.15  jmcneill 			const int bit = ffs64(hwpend[bank]) - 1;
    552  1.15  jmcneill 			const int irq = (bank * 64) + bit;
    553  1.15  jmcneill 			hwpend[bank] &= ~__BIT(bit);
    554  1.15  jmcneill 
    555  1.15  jmcneill 			struct octeon_intrhand * const ih = octciu_intrs[irq];
    556  1.15  jmcneill 			cpu->cpu_intr_evs[irq].ev_count++;
    557  1.15  jmcneill 			if (__predict_true(ih != NULL)) {
    558  1.15  jmcneill #ifdef MULTIPROCESSOR
    559  1.15  jmcneill 				if (ipl == IPL_VM) {
    560  1.15  jmcneill 					KERNEL_LOCK(1, NULL);
    561  1.15  jmcneill #endif
    562  1.15  jmcneill 					(*ih->ih_func)(ih->ih_arg);
    563  1.15  jmcneill #ifdef MULTIPROCESSOR
    564  1.15  jmcneill 					KERNEL_UNLOCK_ONE(NULL);
    565  1.15  jmcneill 				} else {
    566  1.15  jmcneill 					(*ih->ih_func)(ih->ih_arg);
    567  1.15  jmcneill 				}
    568  1.15  jmcneill #endif
    569  1.15  jmcneill 				KDASSERT(mips_cp0_status_read() & MIPS_SR_INT_IE);
    570   1.3      matt 			}
    571   1.3      matt 		}
    572   1.3      matt 	}
    573   1.4      matt 	KDASSERT(mips_cp0_status_read() & MIPS_SR_INT_IE);
    574   1.3      matt }
    575   1.3      matt 
    576   1.3      matt #ifdef MULTIPROCESSOR
    577   1.3      matt __CTASSERT(NIPIS < 16);
    578   1.3      matt 
    579   1.3      matt int
    580   1.3      matt octeon_ipi_intr(void *arg)
    581   1.3      matt {
    582   1.3      matt 	struct cpu_info * const ci = curcpu();
    583   1.3      matt 	struct cpu_softc * const cpu = ci->ci_softc;
    584   1.4      matt 	uint32_t ipi_mask = (uintptr_t) arg;
    585   1.4      matt 
    586   1.4      matt 	KASSERTMSG((ipi_mask & __BITS(31,16)) == 0 || ci->ci_cpl >= IPL_SCHED,
    587   1.4      matt 	    "ipi_mask %#"PRIx32" cpl %d", ipi_mask, ci->ci_cpl);
    588   1.3      matt 
    589   1.5      matt 	ipi_mask &= mips3_ld(cpu->cpu_mbox_set);
    590   1.4      matt 	if (ipi_mask == 0)
    591   1.4      matt 		return 0;
    592   1.4      matt 
    593   1.5      matt 	mips3_sd(cpu->cpu_mbox_clr, ipi_mask);
    594   1.3      matt 
    595   1.3      matt 	ipi_mask |= (ipi_mask >> 16);
    596   1.3      matt 	ipi_mask &= __BITS(15,0);
    597   1.3      matt 
    598   1.3      matt 	KASSERT(ipi_mask < __BIT(NIPIS));
    599   1.3      matt 
    600   1.4      matt #if NWDOG > 0
    601   1.4      matt 	// Handle WDOG requests ourselves.
    602   1.4      matt 	if (ipi_mask & __BIT(IPI_WDOG)) {
    603   1.4      matt 		softint_schedule(cpu->cpu_wdog_sih);
    604   1.4      matt 		atomic_and_64(&ci->ci_request_ipis, ~__BIT(IPI_WDOG));
    605   1.4      matt 		ipi_mask &= ~__BIT(IPI_WDOG);
    606   1.4      matt 		ci->ci_evcnt_per_ipi[IPI_WDOG].ev_count++;
    607   1.4      matt 		if (__predict_true(ipi_mask == 0))
    608   1.4      matt 			return 1;
    609   1.4      matt 	}
    610   1.4      matt #endif
    611   1.4      matt 
    612   1.3      matt 	/* if the request is clear, it was previously processed */
    613   1.3      matt 	if ((ci->ci_request_ipis & ipi_mask) == 0)
    614   1.3      matt 		return 0;
    615   1.3      matt 
    616   1.3      matt 	atomic_or_64(&ci->ci_active_ipis, ipi_mask);
    617   1.3      matt 	atomic_and_64(&ci->ci_request_ipis, ~ipi_mask);
    618   1.3      matt 
    619   1.3      matt 	ipi_process(ci, ipi_mask);
    620   1.3      matt 
    621   1.3      matt 	atomic_and_64(&ci->ci_active_ipis, ~ipi_mask);
    622   1.3      matt 
    623   1.3      matt 	return 1;
    624   1.3      matt }
    625   1.1    hikaru 
    626   1.3      matt int
    627   1.3      matt octeon_send_ipi(struct cpu_info *ci, int req)
    628   1.3      matt {
    629   1.3      matt 	KASSERT(req < NIPIS);
    630   1.3      matt 	if (ci == NULL) {
    631   1.4      matt 		CPU_INFO_ITERATOR cii;
    632   1.4      matt 		for (CPU_INFO_FOREACH(cii, ci)) {
    633   1.4      matt 			if (ci != curcpu()) {
    634   1.4      matt 				octeon_send_ipi(ci, req);
    635   1.4      matt 			}
    636   1.4      matt 		}
    637   1.4      matt 		return 0;
    638   1.1    hikaru 	}
    639   1.4      matt 	KASSERT(cold || ci->ci_softc != NULL);
    640   1.4      matt 	if (ci->ci_softc == NULL)
    641   1.4      matt 		return -1;
    642   1.3      matt 
    643   1.3      matt 	struct cpu_softc * const cpu = ci->ci_softc;
    644   1.3      matt 	uint64_t ipi_mask = __BIT(req);
    645   1.3      matt 
    646   1.7     skrll 	atomic_or_64(&ci->ci_request_ipis, ipi_mask);
    647   1.7     skrll 	if (req == IPI_SUSPEND || req == IPI_WDOG) {
    648   1.3      matt 		ipi_mask <<= 16;
    649   1.1    hikaru 	}
    650   1.3      matt 
    651   1.5      matt 	mips3_sd(cpu->cpu_mbox_set, ipi_mask);
    652   1.3      matt 	return 0;
    653   1.1    hikaru }
    654   1.3      matt #endif	/* MULTIPROCESSOR */
    655