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