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