Home | History | Annotate | Line # | Download | only in cavium
octeon_cpunode.c revision 1.21
      1  1.21   thorpej /*      $NetBSD: octeon_cpunode.c,v 1.21 2021/08/07 16:18:59 thorpej Exp $   */
      2  1.20    andvar 
      3   1.1      matt /*-
      4   1.1      matt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5   1.1      matt  * All rights reserved.
      6   1.1      matt  *
      7   1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      matt  * by Matt Thomas of 3am Software Foundry.
      9   1.1      matt  *
     10   1.1      matt  * Redistribution and use in source and binary forms, with or without
     11   1.1      matt  * modification, are permitted provided that the following conditions
     12   1.1      matt  * are met:
     13   1.1      matt  * 1. Redistributions of source code must retain the above copyright
     14   1.1      matt  *    notice, this list of conditions and the following disclaimer.
     15   1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      matt  *    documentation and/or other materials provided with the distribution.
     18   1.1      matt  *
     19   1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1      matt  */
     31   1.1      matt #define __INTR_PRIVATE
     32   1.1      matt #include <sys/cdefs.h>
     33   1.1      matt 
     34  1.21   thorpej __KERNEL_RCSID(0, "$NetBSD: octeon_cpunode.c,v 1.21 2021/08/07 16:18:59 thorpej Exp $");
     35   1.1      matt 
     36   1.1      matt #include "locators.h"
     37   1.2      matt #include "cpunode.h"
     38   1.2      matt #include "opt_multiprocessor.h"
     39   1.2      matt #include "opt_ddb.h"
     40   1.1      matt 
     41   1.1      matt #include <sys/param.h>
     42  1.16    simonb #include <sys/atomic.h>
     43  1.16    simonb #include <sys/cpu.h>
     44   1.1      matt #include <sys/device.h>
     45   1.1      matt #include <sys/lwp.h>
     46  1.16    simonb #include <sys/reboot.h>
     47   1.2      matt #include <sys/wdog.h>
     48   1.2      matt 
     49   1.2      matt #include <uvm/uvm.h>
     50   1.2      matt 
     51   1.2      matt #include <dev/sysmon/sysmonvar.h>
     52   1.1      matt 
     53   1.1      matt #include <mips/cache.h>
     54   1.1      matt #include <mips/mips_opcode.h>
     55   1.2      matt #include <mips/mips3_clock.h>
     56  1.17  jmcneill #include <mips/mips3_pte.h>
     57   1.1      matt 
     58   1.1      matt #include <mips/cavium/octeonvar.h>
     59   1.1      matt #include <mips/cavium/dev/octeon_ciureg.h>
     60   1.1      matt #include <mips/cavium/dev/octeon_corereg.h>
     61   1.1      matt 
     62  1.14  jmcneill extern struct cpu_softc octeon_cpu_softc[];
     63  1.14  jmcneill 
     64   1.1      matt struct cpunode_attach_args {
     65   1.1      matt 	const char *cnaa_name;
     66   1.1      matt 	int cnaa_cpunum;
     67   1.1      matt };
     68   1.1      matt 
     69   1.2      matt struct cpunode_softc {
     70   1.2      matt 	device_t sc_dev;
     71   1.2      matt 	device_t sc_wdog_dev;
     72   1.2      matt };
     73   1.2      matt 
     74   1.1      matt static int cpunode_mainbus_match(device_t, cfdata_t, void *);
     75   1.1      matt static void cpunode_mainbus_attach(device_t, device_t, void *);
     76   1.1      matt 
     77   1.1      matt static int cpu_cpunode_match(device_t, cfdata_t, void *);
     78   1.1      matt static void cpu_cpunode_attach(device_t, device_t, void *);
     79   1.1      matt 
     80   1.2      matt CFATTACH_DECL_NEW(cpunode, sizeof(struct cpunode_softc),
     81   1.1      matt     cpunode_mainbus_match, cpunode_mainbus_attach, NULL, NULL);
     82   1.1      matt 
     83   1.2      matt CFATTACH_DECL_NEW(cpu_cpunode, 0,
     84   1.1      matt     cpu_cpunode_match, cpu_cpunode_attach, NULL, NULL);
     85   1.1      matt 
     86  1.18    simonb #ifdef MULTIPROCESSOR
     87  1.18    simonb CTASSERT(MAXCPUS <= sizeof(uint64_t) * NBBY);
     88  1.18    simonb volatile uint64_t cpus_booted = __BIT(0);	/* cpu0 is always booted */
     89  1.18    simonb #endif
     90   1.1      matt 
     91   1.8    martin static void wdog_cpunode_poke(void *arg);
     92   1.8    martin 
     93   1.1      matt static int
     94   1.1      matt cpunode_mainbus_print(void *aux, const char *pnp)
     95   1.1      matt {
     96   1.1      matt 	struct cpunode_attach_args * const cnaa = aux;
     97   1.1      matt 
     98   1.7      matt 	if (pnp)
     99   1.7      matt 		aprint_normal("%s", pnp);
    100   1.7      matt 
    101   1.2      matt 	if (cnaa->cnaa_cpunum != CPUNODECF_CORE_DEFAULT)
    102   1.2      matt 		aprint_normal(" core %d", cnaa->cnaa_cpunum);
    103   1.1      matt 
    104   1.1      matt 	return UNCONF;
    105   1.1      matt }
    106   1.1      matt 
    107   1.1      matt int
    108   1.1      matt cpunode_mainbus_match(device_t parent, cfdata_t cf, void *aux)
    109   1.1      matt {
    110  1.15    simonb 
    111   1.1      matt 	return 1;
    112   1.1      matt }
    113   1.1      matt 
    114   1.1      matt void
    115   1.1      matt cpunode_mainbus_attach(device_t parent, device_t self, void *aux)
    116   1.1      matt {
    117   1.2      matt 	struct cpunode_softc * const sc = device_private(self);
    118  1.13    simonb 	const uint64_t fuse = octeon_xkphys_read_8(CIU_FUSE);
    119   1.1      matt 	int cpunum = 0;
    120   1.1      matt 
    121   1.2      matt 	sc->sc_dev = self;
    122   1.2      matt 
    123  1.13    simonb 	aprint_naive(": %u core%s\n", popcount64(fuse), fuse == 1 ? "" : "s");
    124  1.13    simonb 	aprint_normal(": %u core%s", popcount64(fuse), fuse == 1 ? "" : "s");
    125  1.13    simonb 
    126   1.1      matt 	const uint64_t cvmctl = mips_cp0_cvmctl_read();
    127   1.1      matt 	aprint_normal(", %scrypto", (cvmctl & CP0_CVMCTL_NOCRYPTO) ? "no " : "");
    128   1.1      matt 	aprint_normal((cvmctl & CP0_CVMCTL_KASUMI) ? "+kasumi" : "");
    129   1.1      matt 	aprint_normal(", %s64bit-mul", (cvmctl & CP0_CVMCTL_NOMUL) ? "no " : "");
    130   1.1      matt 	if (cvmctl & CP0_CVMCTL_REPUN)
    131   1.1      matt 		aprint_normal(", unaligned-access ok");
    132   1.2      matt #ifdef MULTIPROCESSOR
    133  1.18    simonb 	aprint_normal(", booted %#" PRIx64, cpus_booted);
    134   1.2      matt #endif
    135   1.1      matt 	aprint_normal("\n");
    136   1.1      matt 
    137  1.13    simonb 	for (uint64_t f = fuse; f != 0; f >>= 1, cpunum++) {
    138   1.1      matt 		struct cpunode_attach_args cnaa = {
    139   1.1      matt 			.cnaa_name = "cpu",
    140   1.1      matt 			.cnaa_cpunum = cpunum,
    141   1.1      matt 		};
    142  1.21   thorpej 		config_found(self, &cnaa, cpunode_mainbus_print, CFARGS_NONE);
    143   1.1      matt 	}
    144   1.2      matt #if NWDOG > 0
    145   1.2      matt 	struct cpunode_attach_args cnaa = {
    146   1.2      matt 		.cnaa_name = "wdog",
    147   1.2      matt 		.cnaa_cpunum = CPUNODECF_CORE_DEFAULT,
    148   1.2      matt 	};
    149  1.21   thorpej 	config_found(self, &cnaa, cpunode_mainbus_print, CFARGS_NONE);
    150   1.2      matt #endif
    151   1.1      matt }
    152   1.1      matt 
    153   1.1      matt int
    154   1.1      matt cpu_cpunode_match(device_t parent, cfdata_t cf, void *aux)
    155   1.1      matt {
    156   1.1      matt 	struct cpunode_attach_args * const cnaa = aux;
    157   1.1      matt 	const int cpunum = cf->cf_loc[CPUNODECF_CORE];
    158   1.1      matt 
    159   1.2      matt 	return strcmp(cnaa->cnaa_name, cf->cf_name) == 0
    160   1.2      matt 	    && (cpunum == CPUNODECF_CORE_DEFAULT || cpunum == cnaa->cnaa_cpunum);
    161   1.1      matt }
    162   1.1      matt 
    163   1.1      matt #if defined(MULTIPROCESSOR)
    164   1.1      matt static bool
    165   1.1      matt octeon_fixup_cpu_info_references(int32_t load_addr, uint32_t new_insns[2],
    166   1.1      matt     void *arg)
    167   1.1      matt {
    168   1.1      matt 	struct cpu_info * const ci = arg;
    169   1.1      matt 
    170   1.7      matt 	atomic_or_ulong(&curcpu()->ci_flags, CPUF_PRESENT);
    171   1.2      matt 
    172   1.1      matt 	KASSERT(MIPS_KSEG0_P(load_addr));
    173   1.1      matt #ifdef MULTIPROCESSOR
    174   1.1      matt 	KASSERT(!CPU_IS_PRIMARY(curcpu()));
    175   1.1      matt #endif
    176   1.1      matt 	load_addr += (intptr_t)ci - (intptr_t)&cpu_info_store;
    177   1.1      matt 
    178   1.1      matt 	KASSERT((intptr_t)ci <= load_addr);
    179   1.1      matt 	KASSERT(load_addr < (intptr_t)(ci + 1));
    180   1.1      matt 
    181   1.1      matt 	KASSERT(INSN_LUI_P(new_insns[0]));
    182   1.1      matt 	KASSERT(INSN_LOAD_P(new_insns[1]) || INSN_STORE_P(new_insns[1]));
    183   1.1      matt 
    184   1.1      matt 	/*
    185   1.1      matt 	 * Use the lui and load/store instruction as a prototype and
    186   1.1      matt 	 * make it refer to cpu1_info_store instead of cpu_info_store.
    187   1.1      matt 	 */
    188   1.1      matt 	new_insns[0] &= __BITS(31,16);
    189   1.1      matt 	new_insns[1] &= __BITS(31,16);
    190   1.1      matt 	new_insns[0] |= (uint16_t)((load_addr + 0x8000) >> 16);
    191   1.1      matt 	new_insns[1] |= (uint16_t)load_addr;
    192   1.1      matt #ifdef DEBUG_VERBOSE
    193   1.1      matt 	printf("%s: %08x: insn#1 %08x: lui r%u, %d\n",
    194   1.9     skrll 	    __func__, load_addr, new_insns[0],
    195   1.1      matt 	    (new_insns[0] >> 16) & 31,
    196   1.1      matt 	    (int16_t)new_insns[0]);
    197   1.1      matt 	printf("%s: %08x: insn#2 %08x: %c%c r%u, %d(r%u)\n",
    198  1.10     skrll 	    __func__, load_addr, new_insns[1],
    199   1.1      matt 	    INSN_LOAD_P(new_insns[1]) ? 'l' : 's',
    200   1.1      matt 	    INSN_LW_P(new_insns[1]) ? 'w' : 'd',
    201  1.10     skrll 	    (new_insns[1] >> 16) & 31,
    202   1.1      matt 	    (int16_t)new_insns[1],
    203  1.10     skrll 	    (new_insns[1] >> 21) & 31);
    204   1.1      matt #endif
    205   1.1      matt 	return true;
    206   1.1      matt }
    207   1.1      matt 
    208   1.1      matt static void
    209   1.1      matt octeon_cpu_init(struct cpu_info *ci)
    210   1.1      matt {
    211  1.17  jmcneill 	extern const mips_locore_jumpvec_t mips64r2_locore_vec;
    212   1.1      matt 	bool ok __diagused;
    213   1.1      matt 
    214  1.17  jmcneill 	mips3_cp0_pg_mask_write(MIPS3_PG_SIZE_TO_MASK(PAGE_SIZE));
    215  1.17  jmcneill 	mips3_cp0_wired_write(0);
    216  1.17  jmcneill 	(*mips64r2_locore_vec.ljv_tlb_invalidate_all)();
    217  1.17  jmcneill 	mips3_cp0_wired_write(pmap_tlb0_info.ti_wired);
    218  1.17  jmcneill 
    219  1.20    andvar 	// First thing is setup the exception vectors for this cpu.
    220   1.1      matt 	mips64r2_vector_init(&mips_splsw);
    221   1.1      matt 
    222   1.1      matt 	// Next rewrite those exceptions to use this cpu's cpu_info.
    223   1.1      matt 	ok = mips_fixup_exceptions(octeon_fixup_cpu_info_references, ci);
    224   1.1      matt 	KASSERT(ok);
    225   1.1      matt 
    226   1.2      matt 	(void) splhigh();		// make sure interrupts are masked
    227   1.1      matt 
    228   1.1      matt 	KASSERT((mipsNN_cp0_ebase_read() & MIPS_EBASE_CPUNUM) == ci->ci_cpuid);
    229   1.1      matt 	KASSERT(curcpu() == ci);
    230   1.2      matt 	KASSERT(ci->ci_cpl == IPL_HIGH);
    231   1.2      matt 	KASSERT((mips_cp0_status_read() & MIPS_INT_MASK) == 0);
    232   1.1      matt }
    233   1.1      matt 
    234   1.1      matt static void
    235   1.1      matt octeon_cpu_run(struct cpu_info *ci)
    236   1.1      matt {
    237  1.15    simonb 
    238   1.2      matt 	octeon_intr_init(ci);
    239   1.2      matt 
    240   1.2      matt 	mips3_initclocks();
    241   1.2      matt 	KASSERTMSG(ci->ci_cpl == IPL_NONE, "cpl %d", ci->ci_cpl);
    242   1.2      matt 	KASSERT(mips_cp0_status_read() & MIPS_SR_INT_IE);
    243   1.2      matt 
    244   1.2      matt 	aprint_normal("%s: ", device_xname(ci->ci_dev));
    245   1.2      matt 	cpu_identify(ci->ci_dev);
    246   1.1      matt }
    247   1.1      matt #endif /* MULTIPROCESSOR */
    248   1.1      matt 
    249   1.1      matt static void
    250   1.1      matt cpu_cpunode_attach_common(device_t self, struct cpu_info *ci)
    251   1.1      matt {
    252   1.2      matt 	struct cpu_softc * const cpu __diagused = ci->ci_softc;
    253   1.2      matt 
    254   1.1      matt 	ci->ci_dev = self;
    255   1.1      matt 	self->dv_private = ci;
    256   1.1      matt 
    257   1.2      matt 	KASSERTMSG(cpu != NULL, "ci %p index %d", ci, cpu_index(ci));
    258   1.2      matt 
    259   1.2      matt #if NWDOG > 0 || defined(DDB)
    260  1.13    simonb 	/* XXXXXX __mips_n32 and MIPS_PHYS_TO_XKPHYS_CACHED needed here?????? */
    261   1.2      matt 	void **nmi_vector = (void *)MIPS_PHYS_TO_KSEG0(0x800 + 32*ci->ci_cpuid);
    262   1.2      matt 	*nmi_vector = octeon_reset_vector;
    263   1.2      matt 
    264   1.7      matt 	struct vm_page * const pg = PMAP_ALLOC_POOLPAGE(UVM_PGA_ZERO);
    265   1.2      matt 	KASSERT(pg != NULL);
    266   1.7      matt 	const vaddr_t kva = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
    267   1.2      matt 	KASSERT(kva != 0);
    268   1.3      matt 	ci->ci_nmi_stack = (void *)(kva + PAGE_SIZE - sizeof(struct kernframe));
    269   1.2      matt #endif
    270   1.2      matt 
    271   1.8    martin #if NWDOG > 0
    272   1.2      matt 	cpu->cpu_wdog_sih = softint_establish(SOFTINT_CLOCK|SOFTINT_MPSAFE,
    273   1.2      matt 	    wdog_cpunode_poke, cpu);
    274   1.2      matt 	KASSERT(cpu->cpu_wdog_sih != NULL);
    275   1.2      matt #endif
    276   1.2      matt 
    277  1.13    simonb 	aprint_normal(": %lu.%02luMHz\n",
    278  1.13    simonb 	    (ci->ci_cpu_freq + 5000) / 1000000,
    279  1.13    simonb 	    ((ci->ci_cpu_freq + 5000) % 1000000) / 10000);
    280  1.13    simonb 	aprint_debug_dev(self, "hz cycles = %lu, delay divisor = %lu\n",
    281   1.1      matt 	    ci->ci_cycles_per_hz, ci->ci_divisor_delay);
    282   1.1      matt 
    283   1.2      matt 	if (CPU_IS_PRIMARY(ci)) {
    284   1.2      matt 		aprint_normal("%s: ", device_xname(self));
    285   1.2      matt 		cpu_identify(self);
    286   1.2      matt 	}
    287   1.1      matt 	cpu_attach_common(self, ci);
    288   1.2      matt #ifdef MULTIPROCESSOR
    289   1.2      matt 	KASSERT(cpuid_infos[ci->ci_cpuid] == ci);
    290   1.2      matt #endif
    291   1.1      matt }
    292   1.1      matt 
    293   1.1      matt void
    294   1.1      matt cpu_cpunode_attach(device_t parent, device_t self, void *aux)
    295   1.1      matt {
    296   1.1      matt 	struct cpunode_attach_args * const cnaa = aux;
    297   1.1      matt 	const int cpunum = cnaa->cnaa_cpunum;
    298   1.1      matt 
    299   1.1      matt 	if (cpunum == 0) {
    300   1.1      matt 		cpu_cpunode_attach_common(self, curcpu());
    301   1.1      matt #ifdef MULTIPROCESSOR
    302   1.1      matt 		mips_locoresw.lsw_cpu_init = octeon_cpu_init;
    303   1.1      matt 		mips_locoresw.lsw_cpu_run = octeon_cpu_run;
    304   1.1      matt #endif
    305   1.1      matt 		return;
    306   1.1      matt 	}
    307   1.1      matt #ifdef MULTIPROCESSOR
    308  1.16    simonb 	if ((boothowto & RB_MD1) != 0) {
    309  1.16    simonb 		aprint_naive("\n");
    310  1.16    simonb 		aprint_normal(": multiprocessor boot disabled\n");
    311  1.16    simonb 		return;
    312  1.16    simonb 	}
    313  1.16    simonb 
    314  1.18    simonb 	if (!(cpus_booted & __BIT(cpunum))) {
    315   1.1      matt 		aprint_naive(" disabled\n");
    316   1.1      matt 		aprint_normal(" disabled (unresponsive)\n");
    317   1.1      matt 		return;
    318   1.1      matt 	}
    319   1.1      matt 	struct cpu_info * const ci = cpu_info_alloc(NULL, cpunum, 0, cpunum, 0);
    320   1.1      matt 
    321  1.14  jmcneill 	ci->ci_softc = &octeon_cpu_softc[cpunum];
    322   1.1      matt 	ci->ci_softc->cpu_ci = ci;
    323   1.1      matt 
    324   1.1      matt 	cpu_cpunode_attach_common(self, ci);
    325   1.2      matt 
    326   1.2      matt 	KASSERT(ci->ci_data.cpu_idlelwp != NULL);
    327   1.5      matt 	for (int i = 0; i < 100 && !kcpuset_isset(cpus_hatched, cpunum); i++) {
    328   1.2      matt 		delay(10000);
    329   1.2      matt 	}
    330   1.5      matt 	if (!kcpuset_isset(cpus_hatched, cpunum)) {
    331   1.2      matt #ifdef DDB
    332   1.7      matt 		aprint_verbose_dev(self, "hatch failed ci=%p flags=%#lx\n", ci, ci->ci_flags);
    333   1.2      matt 		cpu_Debugger();
    334   1.2      matt #endif
    335   1.7      matt 		panic("%s failed to hatch: ci=%p flags=%#lx",
    336   1.2      matt 		    cpu_name(ci), ci, ci->ci_flags);
    337   1.2      matt 	}
    338   1.1      matt #else
    339   1.1      matt 	aprint_naive(": disabled\n");
    340   1.1      matt 	aprint_normal(": disabled (uniprocessor kernel)\n");
    341   1.1      matt #endif
    342   1.1      matt }
    343   1.2      matt 
    344   1.2      matt #if NWDOG > 0
    345   1.2      matt struct wdog_softc {
    346   1.2      matt 	struct sysmon_wdog sc_smw;
    347   1.2      matt 	device_t sc_dev;
    348   1.2      matt 	u_int sc_wdog_period;
    349   1.2      matt 	bool sc_wdog_armed;
    350   1.2      matt };
    351   1.2      matt 
    352   1.2      matt #ifndef OCTEON_WDOG_PERIOD_DEFAULT
    353   1.2      matt #define OCTEON_WDOG_PERIOD_DEFAULT	4
    354   1.2      matt #endif
    355   1.2      matt 
    356   1.2      matt static int wdog_cpunode_match(device_t, cfdata_t, void *);
    357   1.2      matt static void wdog_cpunode_attach(device_t, device_t, void *);
    358   1.2      matt 
    359   1.2      matt CFATTACH_DECL_NEW(wdog_cpunode, sizeof(struct wdog_softc),
    360   1.2      matt     wdog_cpunode_match, wdog_cpunode_attach, NULL, NULL);
    361   1.2      matt 
    362   1.2      matt static int
    363   1.2      matt wdog_cpunode_setmode(struct sysmon_wdog *smw)
    364   1.2      matt {
    365   1.2      matt 	struct wdog_softc * const sc = smw->smw_cookie;
    366   1.2      matt 
    367   1.2      matt 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    368   1.2      matt 		if (sc->sc_wdog_armed) {
    369   1.2      matt 			CPU_INFO_ITERATOR cii;
    370   1.2      matt 			struct cpu_info *ci;
    371   1.2      matt 			for (CPU_INFO_FOREACH(cii, ci)) {
    372   1.2      matt 				struct cpu_softc * const cpu = ci->ci_softc;
    373   1.7      matt 				uint64_t wdog = mips3_ld(cpu->cpu_wdog);
    374   1.2      matt 				wdog &= ~CIU_WDOGX_MODE;
    375   1.7      matt 				mips3_sd(cpu->cpu_pp_poke, wdog);
    376   1.2      matt 				aprint_verbose_dev(sc->sc_dev,
    377   1.2      matt 				    "%s: disable wdog=%#"PRIx64"\n",
    378   1.2      matt 				    cpu_name(ci), wdog);
    379   1.7      matt 				mips3_sd(cpu->cpu_wdog, wdog);
    380   1.7      matt 				mips3_sd(cpu->cpu_pp_poke, wdog);
    381   1.2      matt 			}
    382   1.2      matt 			sc->sc_wdog_armed = false;
    383   1.2      matt 		}
    384   1.2      matt 	} else if (!sc->sc_wdog_armed) {
    385   1.2      matt 		kpreempt_disable();
    386   1.2      matt 		struct cpu_info *ci = curcpu();
    387   1.2      matt 		if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
    388   1.2      matt 			smw->smw_period = OCTEON_WDOG_PERIOD_DEFAULT;
    389   1.2      matt 		}
    390   1.2      matt 		uint64_t wdog_len = smw->smw_period * ci->ci_cpu_freq;
    391   1.2      matt 		//
    392   1.2      matt 		// This wdog is a 24-bit counter that decrements every 256
    393   1.2      matt 		// cycles.  This is then a 32-bit counter so as long wdog_len
    394   1.2      matt 		// doesn't overflow a 32-bit value, we are fine.  We write the
    395   1.2      matt 		// 16-bits of the 32-bit period.
    396   1.2      matt 		if ((wdog_len >> 32) != 0) {
    397   1.4    martin 			kpreempt_enable();
    398   1.2      matt 			return EINVAL;
    399   1.2      matt 		}
    400   1.2      matt 		sc->sc_wdog_period = smw->smw_period;
    401   1.2      matt 		CPU_INFO_ITERATOR cii;
    402   1.2      matt 		for (CPU_INFO_FOREACH(cii, ci)) {
    403   1.2      matt 			struct cpu_softc * const cpu = ci->ci_softc;
    404   1.7      matt 			uint64_t wdog = mips3_ld(cpu->cpu_wdog);
    405   1.2      matt 			wdog &= ~(CIU_WDOGX_MODE|CIU_WDOGX_LEN);
    406   1.2      matt 			wdog |= __SHIFTIN(3, CIU_WDOGX_MODE);
    407   1.2      matt 			wdog |= __SHIFTIN(wdog_len >> 16, CIU_WDOGX_LEN);
    408   1.2      matt 			aprint_verbose_dev(sc->sc_dev,
    409   1.2      matt 			    "%s: enable wdog=%#"PRIx64" (%#"PRIx64")\n",
    410   1.2      matt 			    cpu_name(ci), wdog, wdog_len);
    411   1.7      matt 			mips3_sd(cpu->cpu_wdog, wdog);
    412   1.2      matt 		}
    413   1.2      matt 		sc->sc_wdog_armed = true;
    414   1.2      matt 		kpreempt_enable();
    415   1.2      matt 	}
    416   1.2      matt 	return 0;
    417   1.2      matt }
    418   1.2      matt 
    419   1.2      matt static void
    420   1.2      matt wdog_cpunode_poke(void *arg)
    421   1.2      matt {
    422   1.2      matt 	struct cpu_softc *cpu = arg;
    423  1.15    simonb 
    424   1.7      matt 	mips3_sd(cpu->cpu_pp_poke, 0);
    425   1.2      matt }
    426   1.2      matt 
    427   1.2      matt static int
    428   1.2      matt wdog_cpunode_tickle(struct sysmon_wdog *smw)
    429   1.2      matt {
    430  1.15    simonb 
    431   1.2      matt 	wdog_cpunode_poke(curcpu()->ci_softc);
    432   1.2      matt #ifdef MULTIPROCESSOR
    433   1.2      matt 	// We need to send IPIs to the other CPUs to poke their wdog.
    434   1.2      matt 	cpu_send_ipi(NULL, IPI_WDOG);
    435   1.2      matt #endif
    436   1.2      matt 	return 0;
    437   1.2      matt }
    438   1.2      matt 
    439   1.2      matt int
    440   1.2      matt wdog_cpunode_match(device_t parent, cfdata_t cf, void *aux)
    441   1.2      matt {
    442   1.2      matt 	struct cpunode_softc * const sc = device_private(parent);
    443   1.2      matt 	struct cpunode_attach_args * const cnaa = aux;
    444   1.2      matt 	const int cpunum = cf->cf_loc[CPUNODECF_CORE];
    445   1.2      matt 
    446   1.2      matt 	return sc->sc_wdog_dev == NULL
    447   1.2      matt 	    && strcmp(cnaa->cnaa_name, cf->cf_name) == 0
    448   1.2      matt 	    && cpunum == CPUNODECF_CORE_DEFAULT;
    449   1.2      matt }
    450   1.2      matt 
    451   1.2      matt void
    452   1.2      matt wdog_cpunode_attach(device_t parent, device_t self, void *aux)
    453   1.2      matt {
    454   1.2      matt 	struct cpunode_softc * const psc = device_private(parent);
    455   1.2      matt 	struct wdog_softc * const sc = device_private(self);
    456   1.2      matt 	cfdata_t const cf = device_cfdata(self);
    457   1.2      matt 
    458   1.2      matt 	psc->sc_wdog_dev = self;
    459   1.2      matt 
    460   1.2      matt 	sc->sc_dev = self;
    461   1.2      matt 	sc->sc_smw.smw_name = device_xname(self);
    462   1.2      matt 	sc->sc_smw.smw_cookie = sc;
    463   1.2      matt 	sc->sc_smw.smw_setmode = wdog_cpunode_setmode;
    464   1.2      matt 	sc->sc_smw.smw_tickle = wdog_cpunode_tickle;
    465   1.2      matt 	sc->sc_smw.smw_period = OCTEON_WDOG_PERIOD_DEFAULT;
    466   1.2      matt 	sc->sc_wdog_period = sc->sc_smw.smw_period;
    467   1.2      matt 
    468   1.2      matt 	/*
    469   1.2      matt 	 * We need one softint per cpu.  It's to tickle the softints on
    470   1.2      matt 	 * other CPUs.
    471   1.2      matt 	 */
    472  1.12      maya #if 0 /* XXX unused? */
    473   1.2      matt 	CPU_INFO_ITERATOR cii;
    474   1.2      matt 	struct cpu_info *ci;
    475   1.2      matt 	for (CPU_INFO_FOREACH(cii, ci)) {
    476   1.2      matt 	}
    477  1.12      maya #endif
    478   1.2      matt 
    479   1.6     skrll         aprint_normal(": default period is %u second%s\n",
    480   1.2      matt             sc->sc_wdog_period, sc->sc_wdog_period == 1 ? "" : "s");
    481   1.2      matt 
    482   1.2      matt 	if (sysmon_wdog_register(&sc->sc_smw) != 0) {
    483   1.2      matt 		aprint_error_dev(self, "unable to register with sysmon\n");
    484   1.2      matt 		return;
    485   1.2      matt 	}
    486   1.2      matt 
    487   1.2      matt 	if (cf->cf_flags & 1) {
    488   1.2      matt 		int error = sysmon_wdog_setmode(&sc->sc_smw, WDOG_MODE_KTICKLE,
    489   1.2      matt 		    sc->sc_wdog_period);
    490   1.2      matt 		if (error)
    491   1.2      matt 			aprint_error_dev(self,
    492   1.2      matt 			    "failed to start kernel tickler: %d\n", error);
    493   1.2      matt 	}
    494   1.2      matt }
    495   1.2      matt #endif /* NWDOG > 0 */
    496