xref: /netbsd-src/sys/arch/mips/cavium/octeon_cpunode.c (revision 68af1355b18f31a4f1debd90024505b50eb74d07)
1*68af1355Sriastradh /*      $NetBSD: octeon_cpunode.c,v 1.22 2022/03/03 06:27:41 riastradh Exp $   */
2d42188d8Sandvar 
32d299731Smatt /*-
42d299731Smatt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
52d299731Smatt  * All rights reserved.
62d299731Smatt  *
72d299731Smatt  * This code is derived from software contributed to The NetBSD Foundation
82d299731Smatt  * by Matt Thomas of 3am Software Foundry.
92d299731Smatt  *
102d299731Smatt  * Redistribution and use in source and binary forms, with or without
112d299731Smatt  * modification, are permitted provided that the following conditions
122d299731Smatt  * are met:
132d299731Smatt  * 1. Redistributions of source code must retain the above copyright
142d299731Smatt  *    notice, this list of conditions and the following disclaimer.
152d299731Smatt  * 2. Redistributions in binary form must reproduce the above copyright
162d299731Smatt  *    notice, this list of conditions and the following disclaimer in the
172d299731Smatt  *    documentation and/or other materials provided with the distribution.
182d299731Smatt  *
192d299731Smatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202d299731Smatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212d299731Smatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222d299731Smatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232d299731Smatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242d299731Smatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252d299731Smatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262d299731Smatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272d299731Smatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282d299731Smatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292d299731Smatt  * POSSIBILITY OF SUCH DAMAGE.
302d299731Smatt  */
312d299731Smatt #define __INTR_PRIVATE
322d299731Smatt #include <sys/cdefs.h>
332d299731Smatt 
34*68af1355Sriastradh __KERNEL_RCSID(0, "$NetBSD: octeon_cpunode.c,v 1.22 2022/03/03 06:27:41 riastradh Exp $");
352d299731Smatt 
362d299731Smatt #include "locators.h"
37847a1893Smatt #include "cpunode.h"
38847a1893Smatt #include "opt_multiprocessor.h"
39847a1893Smatt #include "opt_ddb.h"
402d299731Smatt 
412d299731Smatt #include <sys/param.h>
4204478e22Ssimonb #include <sys/atomic.h>
4304478e22Ssimonb #include <sys/cpu.h>
442d299731Smatt #include <sys/device.h>
452d299731Smatt #include <sys/lwp.h>
4604478e22Ssimonb #include <sys/reboot.h>
47847a1893Smatt #include <sys/wdog.h>
48847a1893Smatt 
49847a1893Smatt #include <uvm/uvm.h>
50847a1893Smatt 
51847a1893Smatt #include <dev/sysmon/sysmonvar.h>
522d299731Smatt 
532d299731Smatt #include <mips/cache.h>
542d299731Smatt #include <mips/mips_opcode.h>
55847a1893Smatt #include <mips/mips3_clock.h>
561750621aSjmcneill #include <mips/mips3_pte.h>
572d299731Smatt 
582d299731Smatt #include <mips/cavium/octeonvar.h>
592d299731Smatt #include <mips/cavium/dev/octeon_ciureg.h>
602d299731Smatt #include <mips/cavium/dev/octeon_corereg.h>
612d299731Smatt 
62793877b0Sjmcneill extern struct cpu_softc octeon_cpu_softc[];
63793877b0Sjmcneill 
642d299731Smatt struct cpunode_attach_args {
652d299731Smatt 	const char *cnaa_name;
662d299731Smatt 	int cnaa_cpunum;
672d299731Smatt };
682d299731Smatt 
69847a1893Smatt struct cpunode_softc {
70847a1893Smatt 	device_t sc_dev;
71847a1893Smatt 	device_t sc_wdog_dev;
72847a1893Smatt };
73847a1893Smatt 
742d299731Smatt static int cpunode_mainbus_match(device_t, cfdata_t, void *);
752d299731Smatt static void cpunode_mainbus_attach(device_t, device_t, void *);
762d299731Smatt 
772d299731Smatt static int cpu_cpunode_match(device_t, cfdata_t, void *);
782d299731Smatt static void cpu_cpunode_attach(device_t, device_t, void *);
792d299731Smatt 
80847a1893Smatt CFATTACH_DECL_NEW(cpunode, sizeof(struct cpunode_softc),
812d299731Smatt     cpunode_mainbus_match, cpunode_mainbus_attach, NULL, NULL);
822d299731Smatt 
83847a1893Smatt CFATTACH_DECL_NEW(cpu_cpunode, 0,
842d299731Smatt     cpu_cpunode_match, cpu_cpunode_attach, NULL, NULL);
852d299731Smatt 
86da5bbaccSsimonb #ifdef MULTIPROCESSOR
87da5bbaccSsimonb CTASSERT(MAXCPUS <= sizeof(uint64_t) * NBBY);
88da5bbaccSsimonb volatile uint64_t cpus_booted = __BIT(0);	/* cpu0 is always booted */
89da5bbaccSsimonb #endif
902d299731Smatt 
913757fcb0Smartin static void wdog_cpunode_poke(void *arg);
923757fcb0Smartin 
932d299731Smatt static int
cpunode_mainbus_print(void * aux,const char * pnp)942d299731Smatt cpunode_mainbus_print(void *aux, const char *pnp)
952d299731Smatt {
962d299731Smatt 	struct cpunode_attach_args * const cnaa = aux;
972d299731Smatt 
98d7e78fcfSmatt 	if (pnp)
99d7e78fcfSmatt 		aprint_normal("%s", pnp);
100d7e78fcfSmatt 
101847a1893Smatt 	if (cnaa->cnaa_cpunum != CPUNODECF_CORE_DEFAULT)
1022d299731Smatt 		aprint_normal(" core %d", cnaa->cnaa_cpunum);
1032d299731Smatt 
1042d299731Smatt 	return UNCONF;
1052d299731Smatt }
1062d299731Smatt 
1072d299731Smatt int
cpunode_mainbus_match(device_t parent,cfdata_t cf,void * aux)1082d299731Smatt cpunode_mainbus_match(device_t parent, cfdata_t cf, void *aux)
1092d299731Smatt {
1102d299731Smatt 
1112d299731Smatt 	return 1;
1122d299731Smatt }
1132d299731Smatt 
1142d299731Smatt void
cpunode_mainbus_attach(device_t parent,device_t self,void * aux)1152d299731Smatt cpunode_mainbus_attach(device_t parent, device_t self, void *aux)
1162d299731Smatt {
117847a1893Smatt 	struct cpunode_softc * const sc = device_private(self);
118eb61b502Ssimonb 	const uint64_t fuse = octeon_xkphys_read_8(CIU_FUSE);
1192d299731Smatt 	int cpunum = 0;
1202d299731Smatt 
121847a1893Smatt 	sc->sc_dev = self;
122847a1893Smatt 
123eb61b502Ssimonb 	aprint_naive(": %u core%s\n", popcount64(fuse), fuse == 1 ? "" : "s");
124eb61b502Ssimonb 	aprint_normal(": %u core%s", popcount64(fuse), fuse == 1 ? "" : "s");
1252d299731Smatt 
1262d299731Smatt 	const uint64_t cvmctl = mips_cp0_cvmctl_read();
1272d299731Smatt 	aprint_normal(", %scrypto", (cvmctl & CP0_CVMCTL_NOCRYPTO) ? "no " : "");
1282d299731Smatt 	aprint_normal((cvmctl & CP0_CVMCTL_KASUMI) ? "+kasumi" : "");
1292d299731Smatt 	aprint_normal(", %s64bit-mul", (cvmctl & CP0_CVMCTL_NOMUL) ? "no " : "");
1302d299731Smatt 	if (cvmctl & CP0_CVMCTL_REPUN)
1312d299731Smatt 		aprint_normal(", unaligned-access ok");
132847a1893Smatt #ifdef MULTIPROCESSOR
133da5bbaccSsimonb 	aprint_normal(", booted %#" PRIx64, cpus_booted);
134847a1893Smatt #endif
1352d299731Smatt 	aprint_normal("\n");
1362d299731Smatt 
137eb61b502Ssimonb 	for (uint64_t f = fuse; f != 0; f >>= 1, cpunum++) {
1382d299731Smatt 		struct cpunode_attach_args cnaa = {
1392d299731Smatt 			.cnaa_name = "cpu",
1402d299731Smatt 			.cnaa_cpunum = cpunum,
1412d299731Smatt 		};
142c7fb772bSthorpej 		config_found(self, &cnaa, cpunode_mainbus_print, CFARGS_NONE);
1432d299731Smatt 	}
144847a1893Smatt #if NWDOG > 0
145847a1893Smatt 	struct cpunode_attach_args cnaa = {
146847a1893Smatt 		.cnaa_name = "wdog",
147847a1893Smatt 		.cnaa_cpunum = CPUNODECF_CORE_DEFAULT,
148847a1893Smatt 	};
149c7fb772bSthorpej 	config_found(self, &cnaa, cpunode_mainbus_print, CFARGS_NONE);
150847a1893Smatt #endif
1512d299731Smatt }
1522d299731Smatt 
1532d299731Smatt int
cpu_cpunode_match(device_t parent,cfdata_t cf,void * aux)1542d299731Smatt cpu_cpunode_match(device_t parent, cfdata_t cf, void *aux)
1552d299731Smatt {
1562d299731Smatt 	struct cpunode_attach_args * const cnaa = aux;
1572d299731Smatt 	const int cpunum = cf->cf_loc[CPUNODECF_CORE];
1582d299731Smatt 
159847a1893Smatt 	return strcmp(cnaa->cnaa_name, cf->cf_name) == 0
160847a1893Smatt 	    && (cpunum == CPUNODECF_CORE_DEFAULT || cpunum == cnaa->cnaa_cpunum);
1612d299731Smatt }
1622d299731Smatt 
1632d299731Smatt #if defined(MULTIPROCESSOR)
1642d299731Smatt static bool
octeon_fixup_cpu_info_references(int32_t load_addr,uint32_t new_insns[2],void * arg)1652d299731Smatt octeon_fixup_cpu_info_references(int32_t load_addr, uint32_t new_insns[2],
1662d299731Smatt     void *arg)
1672d299731Smatt {
1682d299731Smatt 	struct cpu_info * const ci = arg;
1692d299731Smatt 
170d7e78fcfSmatt 	atomic_or_ulong(&curcpu()->ci_flags, CPUF_PRESENT);
171847a1893Smatt 
1722d299731Smatt 	KASSERT(MIPS_KSEG0_P(load_addr));
1732d299731Smatt #ifdef MULTIPROCESSOR
1742d299731Smatt 	KASSERT(!CPU_IS_PRIMARY(curcpu()));
1752d299731Smatt #endif
1762d299731Smatt 	load_addr += (intptr_t)ci - (intptr_t)&cpu_info_store;
1772d299731Smatt 
1782d299731Smatt 	KASSERT((intptr_t)ci <= load_addr);
1792d299731Smatt 	KASSERT(load_addr < (intptr_t)(ci + 1));
1802d299731Smatt 
1812d299731Smatt 	KASSERT(INSN_LUI_P(new_insns[0]));
1822d299731Smatt 	KASSERT(INSN_LOAD_P(new_insns[1]) || INSN_STORE_P(new_insns[1]));
1832d299731Smatt 
1842d299731Smatt 	/*
1852d299731Smatt 	 * Use the lui and load/store instruction as a prototype and
1862d299731Smatt 	 * make it refer to cpu1_info_store instead of cpu_info_store.
1872d299731Smatt 	 */
1882d299731Smatt 	new_insns[0] &= __BITS(31,16);
1892d299731Smatt 	new_insns[1] &= __BITS(31,16);
1902d299731Smatt 	new_insns[0] |= (uint16_t)((load_addr + 0x8000) >> 16);
1912d299731Smatt 	new_insns[1] |= (uint16_t)load_addr;
1922d299731Smatt #ifdef DEBUG_VERBOSE
1932d299731Smatt 	printf("%s: %08x: insn#1 %08x: lui r%u, %d\n",
1941bd2f79dSskrll 	    __func__, load_addr, new_insns[0],
1952d299731Smatt 	    (new_insns[0] >> 16) & 31,
1962d299731Smatt 	    (int16_t)new_insns[0]);
1972d299731Smatt 	printf("%s: %08x: insn#2 %08x: %c%c r%u, %d(r%u)\n",
198ce214d27Sskrll 	    __func__, load_addr, new_insns[1],
1992d299731Smatt 	    INSN_LOAD_P(new_insns[1]) ? 'l' : 's',
2002d299731Smatt 	    INSN_LW_P(new_insns[1]) ? 'w' : 'd',
201ce214d27Sskrll 	    (new_insns[1] >> 16) & 31,
2022d299731Smatt 	    (int16_t)new_insns[1],
203ce214d27Sskrll 	    (new_insns[1] >> 21) & 31);
2042d299731Smatt #endif
2052d299731Smatt 	return true;
2062d299731Smatt }
2072d299731Smatt 
2082d299731Smatt static void
octeon_cpu_init(struct cpu_info * ci)2092d299731Smatt octeon_cpu_init(struct cpu_info *ci)
2102d299731Smatt {
2111750621aSjmcneill 	extern const mips_locore_jumpvec_t mips64r2_locore_vec;
2122d299731Smatt 	bool ok __diagused;
2132d299731Smatt 
2141750621aSjmcneill 	mips3_cp0_pg_mask_write(MIPS3_PG_SIZE_TO_MASK(PAGE_SIZE));
2151750621aSjmcneill 	mips3_cp0_wired_write(0);
2161750621aSjmcneill 	(*mips64r2_locore_vec.ljv_tlb_invalidate_all)();
2171750621aSjmcneill 	mips3_cp0_wired_write(pmap_tlb0_info.ti_wired);
2181750621aSjmcneill 
219d42188d8Sandvar 	// First thing is setup the exception vectors for this cpu.
2202d299731Smatt 	mips64r2_vector_init(&mips_splsw);
2212d299731Smatt 
2222d299731Smatt 	// Next rewrite those exceptions to use this cpu's cpu_info.
2232d299731Smatt 	ok = mips_fixup_exceptions(octeon_fixup_cpu_info_references, ci);
2242d299731Smatt 	KASSERT(ok);
2252d299731Smatt 
226847a1893Smatt 	(void) splhigh();		// make sure interrupts are masked
2272d299731Smatt 
2282d299731Smatt 	KASSERT((mipsNN_cp0_ebase_read() & MIPS_EBASE_CPUNUM) == ci->ci_cpuid);
2292d299731Smatt 	KASSERT(curcpu() == ci);
230847a1893Smatt 	KASSERT(ci->ci_cpl == IPL_HIGH);
231847a1893Smatt 	KASSERT((mips_cp0_status_read() & MIPS_INT_MASK) == 0);
2322d299731Smatt }
2332d299731Smatt 
2342d299731Smatt static void
octeon_cpu_run(struct cpu_info * ci)2352d299731Smatt octeon_cpu_run(struct cpu_info *ci)
2362d299731Smatt {
2375a22ba8eSsimonb 
238847a1893Smatt 	octeon_intr_init(ci);
239847a1893Smatt 
240847a1893Smatt 	mips3_initclocks();
241847a1893Smatt 	KASSERTMSG(ci->ci_cpl == IPL_NONE, "cpl %d", ci->ci_cpl);
242847a1893Smatt 	KASSERT(mips_cp0_status_read() & MIPS_SR_INT_IE);
243847a1893Smatt 
244847a1893Smatt 	aprint_normal("%s: ", device_xname(ci->ci_dev));
245847a1893Smatt 	cpu_identify(ci->ci_dev);
2462d299731Smatt }
2472d299731Smatt #endif /* MULTIPROCESSOR */
2482d299731Smatt 
2492d299731Smatt static void
cpu_cpunode_attach_common(device_t self,struct cpu_info * ci)2502d299731Smatt cpu_cpunode_attach_common(device_t self, struct cpu_info *ci)
2512d299731Smatt {
252847a1893Smatt 	struct cpu_softc * const cpu __diagused = ci->ci_softc;
253847a1893Smatt 
2542d299731Smatt 	ci->ci_dev = self;
255*68af1355Sriastradh 	device_set_private(self, ci);
2562d299731Smatt 
257847a1893Smatt 	KASSERTMSG(cpu != NULL, "ci %p index %d", ci, cpu_index(ci));
258847a1893Smatt 
259847a1893Smatt #if NWDOG > 0 || defined(DDB)
260eb61b502Ssimonb 	/* XXXXXX __mips_n32 and MIPS_PHYS_TO_XKPHYS_CACHED needed here?????? */
261847a1893Smatt 	void **nmi_vector = (void *)MIPS_PHYS_TO_KSEG0(0x800 + 32*ci->ci_cpuid);
262847a1893Smatt 	*nmi_vector = octeon_reset_vector;
263847a1893Smatt 
264d7e78fcfSmatt 	struct vm_page * const pg = PMAP_ALLOC_POOLPAGE(UVM_PGA_ZERO);
265847a1893Smatt 	KASSERT(pg != NULL);
266d7e78fcfSmatt 	const vaddr_t kva = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
267847a1893Smatt 	KASSERT(kva != 0);
2683018f206Smatt 	ci->ci_nmi_stack = (void *)(kva + PAGE_SIZE - sizeof(struct kernframe));
269847a1893Smatt #endif
270847a1893Smatt 
2713757fcb0Smartin #if NWDOG > 0
272847a1893Smatt 	cpu->cpu_wdog_sih = softint_establish(SOFTINT_CLOCK|SOFTINT_MPSAFE,
273847a1893Smatt 	    wdog_cpunode_poke, cpu);
274847a1893Smatt 	KASSERT(cpu->cpu_wdog_sih != NULL);
275847a1893Smatt #endif
276847a1893Smatt 
277eb61b502Ssimonb 	aprint_normal(": %lu.%02luMHz\n",
278eb61b502Ssimonb 	    (ci->ci_cpu_freq + 5000) / 1000000,
279eb61b502Ssimonb 	    ((ci->ci_cpu_freq + 5000) % 1000000) / 10000);
280eb61b502Ssimonb 	aprint_debug_dev(self, "hz cycles = %lu, delay divisor = %lu\n",
2812d299731Smatt 	    ci->ci_cycles_per_hz, ci->ci_divisor_delay);
2822d299731Smatt 
283847a1893Smatt 	if (CPU_IS_PRIMARY(ci)) {
2842d299731Smatt 		aprint_normal("%s: ", device_xname(self));
2852d299731Smatt 		cpu_identify(self);
286847a1893Smatt 	}
2872d299731Smatt 	cpu_attach_common(self, ci);
288847a1893Smatt #ifdef MULTIPROCESSOR
289847a1893Smatt 	KASSERT(cpuid_infos[ci->ci_cpuid] == ci);
290847a1893Smatt #endif
2912d299731Smatt }
2922d299731Smatt 
2932d299731Smatt void
cpu_cpunode_attach(device_t parent,device_t self,void * aux)2942d299731Smatt cpu_cpunode_attach(device_t parent, device_t self, void *aux)
2952d299731Smatt {
2962d299731Smatt 	struct cpunode_attach_args * const cnaa = aux;
2972d299731Smatt 	const int cpunum = cnaa->cnaa_cpunum;
2982d299731Smatt 
2992d299731Smatt 	if (cpunum == 0) {
3002d299731Smatt 		cpu_cpunode_attach_common(self, curcpu());
3012d299731Smatt #ifdef MULTIPROCESSOR
3022d299731Smatt 		mips_locoresw.lsw_cpu_init = octeon_cpu_init;
3032d299731Smatt 		mips_locoresw.lsw_cpu_run = octeon_cpu_run;
3042d299731Smatt #endif
3052d299731Smatt 		return;
3062d299731Smatt 	}
3072d299731Smatt #ifdef MULTIPROCESSOR
30804478e22Ssimonb 	if ((boothowto & RB_MD1) != 0) {
30904478e22Ssimonb 		aprint_naive("\n");
31004478e22Ssimonb 		aprint_normal(": multiprocessor boot disabled\n");
31104478e22Ssimonb 		return;
31204478e22Ssimonb 	}
31304478e22Ssimonb 
314da5bbaccSsimonb 	if (!(cpus_booted & __BIT(cpunum))) {
3152d299731Smatt 		aprint_naive(" disabled\n");
3162d299731Smatt 		aprint_normal(" disabled (unresponsive)\n");
3172d299731Smatt 		return;
3182d299731Smatt 	}
3192d299731Smatt 	struct cpu_info * const ci = cpu_info_alloc(NULL, cpunum, 0, cpunum, 0);
3202d299731Smatt 
321793877b0Sjmcneill 	ci->ci_softc = &octeon_cpu_softc[cpunum];
3222d299731Smatt 	ci->ci_softc->cpu_ci = ci;
3232d299731Smatt 
3242d299731Smatt 	cpu_cpunode_attach_common(self, ci);
325847a1893Smatt 
326847a1893Smatt 	KASSERT(ci->ci_data.cpu_idlelwp != NULL);
327fcf879e0Smatt 	for (int i = 0; i < 100 && !kcpuset_isset(cpus_hatched, cpunum); i++) {
328847a1893Smatt 		delay(10000);
329847a1893Smatt 	}
330fcf879e0Smatt 	if (!kcpuset_isset(cpus_hatched, cpunum)) {
331847a1893Smatt #ifdef DDB
332d7e78fcfSmatt 		aprint_verbose_dev(self, "hatch failed ci=%p flags=%#lx\n", ci, ci->ci_flags);
333847a1893Smatt 		cpu_Debugger();
334847a1893Smatt #endif
335d7e78fcfSmatt 		panic("%s failed to hatch: ci=%p flags=%#lx",
336847a1893Smatt 		    cpu_name(ci), ci, ci->ci_flags);
337847a1893Smatt 	}
3382d299731Smatt #else
3392d299731Smatt 	aprint_naive(": disabled\n");
3402d299731Smatt 	aprint_normal(": disabled (uniprocessor kernel)\n");
3412d299731Smatt #endif
3422d299731Smatt }
343847a1893Smatt 
344847a1893Smatt #if NWDOG > 0
345847a1893Smatt struct wdog_softc {
346847a1893Smatt 	struct sysmon_wdog sc_smw;
347847a1893Smatt 	device_t sc_dev;
348847a1893Smatt 	u_int sc_wdog_period;
349847a1893Smatt 	bool sc_wdog_armed;
350847a1893Smatt };
351847a1893Smatt 
352847a1893Smatt #ifndef OCTEON_WDOG_PERIOD_DEFAULT
353847a1893Smatt #define OCTEON_WDOG_PERIOD_DEFAULT	4
354847a1893Smatt #endif
355847a1893Smatt 
356847a1893Smatt static int wdog_cpunode_match(device_t, cfdata_t, void *);
357847a1893Smatt static void wdog_cpunode_attach(device_t, device_t, void *);
358847a1893Smatt 
359847a1893Smatt CFATTACH_DECL_NEW(wdog_cpunode, sizeof(struct wdog_softc),
360847a1893Smatt     wdog_cpunode_match, wdog_cpunode_attach, NULL, NULL);
361847a1893Smatt 
362847a1893Smatt static int
wdog_cpunode_setmode(struct sysmon_wdog * smw)363847a1893Smatt wdog_cpunode_setmode(struct sysmon_wdog *smw)
364847a1893Smatt {
365847a1893Smatt 	struct wdog_softc * const sc = smw->smw_cookie;
366847a1893Smatt 
367847a1893Smatt 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
368847a1893Smatt 		if (sc->sc_wdog_armed) {
369847a1893Smatt 			CPU_INFO_ITERATOR cii;
370847a1893Smatt 			struct cpu_info *ci;
371847a1893Smatt 			for (CPU_INFO_FOREACH(cii, ci)) {
372847a1893Smatt 				struct cpu_softc * const cpu = ci->ci_softc;
373d7e78fcfSmatt 				uint64_t wdog = mips3_ld(cpu->cpu_wdog);
374847a1893Smatt 				wdog &= ~CIU_WDOGX_MODE;
375d7e78fcfSmatt 				mips3_sd(cpu->cpu_pp_poke, wdog);
376847a1893Smatt 				aprint_verbose_dev(sc->sc_dev,
377847a1893Smatt 				    "%s: disable wdog=%#"PRIx64"\n",
378847a1893Smatt 				    cpu_name(ci), wdog);
379d7e78fcfSmatt 				mips3_sd(cpu->cpu_wdog, wdog);
380d7e78fcfSmatt 				mips3_sd(cpu->cpu_pp_poke, wdog);
381847a1893Smatt 			}
382847a1893Smatt 			sc->sc_wdog_armed = false;
383847a1893Smatt 		}
384847a1893Smatt 	} else if (!sc->sc_wdog_armed) {
385847a1893Smatt 		kpreempt_disable();
386847a1893Smatt 		struct cpu_info *ci = curcpu();
387847a1893Smatt 		if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
388847a1893Smatt 			smw->smw_period = OCTEON_WDOG_PERIOD_DEFAULT;
389847a1893Smatt 		}
390847a1893Smatt 		uint64_t wdog_len = smw->smw_period * ci->ci_cpu_freq;
391847a1893Smatt 		//
392847a1893Smatt 		// This wdog is a 24-bit counter that decrements every 256
393847a1893Smatt 		// cycles.  This is then a 32-bit counter so as long wdog_len
394847a1893Smatt 		// doesn't overflow a 32-bit value, we are fine.  We write the
395847a1893Smatt 		// 16-bits of the 32-bit period.
396847a1893Smatt 		if ((wdog_len >> 32) != 0) {
397da3a80aaSmartin 			kpreempt_enable();
398847a1893Smatt 			return EINVAL;
399847a1893Smatt 		}
400847a1893Smatt 		sc->sc_wdog_period = smw->smw_period;
401847a1893Smatt 		CPU_INFO_ITERATOR cii;
402847a1893Smatt 		for (CPU_INFO_FOREACH(cii, ci)) {
403847a1893Smatt 			struct cpu_softc * const cpu = ci->ci_softc;
404d7e78fcfSmatt 			uint64_t wdog = mips3_ld(cpu->cpu_wdog);
405847a1893Smatt 			wdog &= ~(CIU_WDOGX_MODE|CIU_WDOGX_LEN);
406847a1893Smatt 			wdog |= __SHIFTIN(3, CIU_WDOGX_MODE);
407847a1893Smatt 			wdog |= __SHIFTIN(wdog_len >> 16, CIU_WDOGX_LEN);
408847a1893Smatt 			aprint_verbose_dev(sc->sc_dev,
409847a1893Smatt 			    "%s: enable wdog=%#"PRIx64" (%#"PRIx64")\n",
410847a1893Smatt 			    cpu_name(ci), wdog, wdog_len);
411d7e78fcfSmatt 			mips3_sd(cpu->cpu_wdog, wdog);
412847a1893Smatt 		}
413847a1893Smatt 		sc->sc_wdog_armed = true;
414847a1893Smatt 		kpreempt_enable();
415847a1893Smatt 	}
416847a1893Smatt 	return 0;
417847a1893Smatt }
418847a1893Smatt 
419847a1893Smatt static void
wdog_cpunode_poke(void * arg)420847a1893Smatt wdog_cpunode_poke(void *arg)
421847a1893Smatt {
422847a1893Smatt 	struct cpu_softc *cpu = arg;
4235a22ba8eSsimonb 
424d7e78fcfSmatt 	mips3_sd(cpu->cpu_pp_poke, 0);
425847a1893Smatt }
426847a1893Smatt 
427847a1893Smatt static int
wdog_cpunode_tickle(struct sysmon_wdog * smw)428847a1893Smatt wdog_cpunode_tickle(struct sysmon_wdog *smw)
429847a1893Smatt {
4305a22ba8eSsimonb 
431847a1893Smatt 	wdog_cpunode_poke(curcpu()->ci_softc);
432847a1893Smatt #ifdef MULTIPROCESSOR
433847a1893Smatt 	// We need to send IPIs to the other CPUs to poke their wdog.
434847a1893Smatt 	cpu_send_ipi(NULL, IPI_WDOG);
435847a1893Smatt #endif
436847a1893Smatt 	return 0;
437847a1893Smatt }
438847a1893Smatt 
439847a1893Smatt int
wdog_cpunode_match(device_t parent,cfdata_t cf,void * aux)440847a1893Smatt wdog_cpunode_match(device_t parent, cfdata_t cf, void *aux)
441847a1893Smatt {
442847a1893Smatt 	struct cpunode_softc * const sc = device_private(parent);
443847a1893Smatt 	struct cpunode_attach_args * const cnaa = aux;
444847a1893Smatt 	const int cpunum = cf->cf_loc[CPUNODECF_CORE];
445847a1893Smatt 
446847a1893Smatt 	return sc->sc_wdog_dev == NULL
447847a1893Smatt 	    && strcmp(cnaa->cnaa_name, cf->cf_name) == 0
448847a1893Smatt 	    && cpunum == CPUNODECF_CORE_DEFAULT;
449847a1893Smatt }
450847a1893Smatt 
451847a1893Smatt void
wdog_cpunode_attach(device_t parent,device_t self,void * aux)452847a1893Smatt wdog_cpunode_attach(device_t parent, device_t self, void *aux)
453847a1893Smatt {
454847a1893Smatt 	struct cpunode_softc * const psc = device_private(parent);
455847a1893Smatt 	struct wdog_softc * const sc = device_private(self);
456847a1893Smatt 	cfdata_t const cf = device_cfdata(self);
457847a1893Smatt 
458847a1893Smatt 	psc->sc_wdog_dev = self;
459847a1893Smatt 
460847a1893Smatt 	sc->sc_dev = self;
461847a1893Smatt 	sc->sc_smw.smw_name = device_xname(self);
462847a1893Smatt 	sc->sc_smw.smw_cookie = sc;
463847a1893Smatt 	sc->sc_smw.smw_setmode = wdog_cpunode_setmode;
464847a1893Smatt 	sc->sc_smw.smw_tickle = wdog_cpunode_tickle;
465847a1893Smatt 	sc->sc_smw.smw_period = OCTEON_WDOG_PERIOD_DEFAULT;
466847a1893Smatt 	sc->sc_wdog_period = sc->sc_smw.smw_period;
467847a1893Smatt 
468847a1893Smatt 	/*
469847a1893Smatt 	 * We need one softint per cpu.  It's to tickle the softints on
470847a1893Smatt 	 * other CPUs.
471847a1893Smatt 	 */
472ec26d828Smaya #if 0 /* XXX unused? */
473847a1893Smatt 	CPU_INFO_ITERATOR cii;
474847a1893Smatt 	struct cpu_info *ci;
475847a1893Smatt 	for (CPU_INFO_FOREACH(cii, ci)) {
476847a1893Smatt 	}
477ec26d828Smaya #endif
478847a1893Smatt 
47916e89468Sskrll         aprint_normal(": default period is %u second%s\n",
480847a1893Smatt             sc->sc_wdog_period, sc->sc_wdog_period == 1 ? "" : "s");
481847a1893Smatt 
482847a1893Smatt 	if (sysmon_wdog_register(&sc->sc_smw) != 0) {
483847a1893Smatt 		aprint_error_dev(self, "unable to register with sysmon\n");
484847a1893Smatt 		return;
485847a1893Smatt 	}
486847a1893Smatt 
487847a1893Smatt 	if (cf->cf_flags & 1) {
488847a1893Smatt 		int error = sysmon_wdog_setmode(&sc->sc_smw, WDOG_MODE_KTICKLE,
489847a1893Smatt 		    sc->sc_wdog_period);
490847a1893Smatt 		if (error)
491847a1893Smatt 			aprint_error_dev(self,
492847a1893Smatt 			    "failed to start kernel tickler: %d\n", error);
493847a1893Smatt 	}
494847a1893Smatt }
495847a1893Smatt #endif /* NWDOG > 0 */
496