machdep.c revision 1.13 1 /* $NetBSD: machdep.c,v 1.13 2020/06/05 07:17:38 simonb Exp $ */
2
3 /*
4 * Copyright 2001, 2002 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Copyright (c) 1992, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * This code is derived from software contributed to Berkeley by
43 * the Systems Programming Group of the University of Utah Computer
44 * Science Department, The Mach Operating System project at
45 * Carnegie-Mellon University and Ralph Campbell.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
72 * from: Utah Hdr: machdep.c 1.63 91/04/24
73 */
74 /*
75 * Copyright (c) 1988 University of Utah.
76 *
77 * This code is derived from software contributed to Berkeley by
78 * the Systems Programming Group of the University of Utah Computer
79 * Science Department, The Mach Operating System project at
80 * Carnegie-Mellon University and Ralph Campbell.
81 *
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
84 * are met:
85 * 1. Redistributions of source code must retain the above copyright
86 * notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 * notice, this list of conditions and the following disclaimer in the
89 * documentation and/or other materials provided with the distribution.
90 * 3. All advertising materials mentioning features or use of this software
91 * must display the following acknowledgement:
92 * This product includes software developed by the University of
93 * California, Berkeley and its contributors.
94 * 4. Neither the name of the University nor the names of its contributors
95 * may be used to endorse or promote products derived from this software
96 * without specific prior written permission.
97 *
98 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108 * SUCH DAMAGE.
109 *
110 * @(#)machdep.c 8.3 (Berkeley) 1/12/94
111 * from: Utah Hdr: machdep.c 1.63 91/04/24
112 */
113
114 #include "opt_multiprocessor.h"
115 #include "opt_cavium.h"
116
117 #include <sys/cdefs.h>
118 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.13 2020/06/05 07:17:38 simonb Exp $");
119
120 #include <sys/param.h>
121 #include <sys/systm.h>
122 #include <sys/kernel.h>
123 #include <sys/buf.h>
124 #include <sys/cpu.h>
125 #include <sys/reboot.h>
126 #include <sys/mount.h>
127 #include <sys/kcore.h>
128 #include <sys/boot_flag.h>
129 #include <sys/termios.h>
130 #include <sys/ksyms.h>
131
132 #include <uvm/uvm_extern.h>
133
134 #include <dev/cons.h>
135
136 #include "ksyms.h"
137
138 #if NKSYMS || defined(DDB) || defined(LKM)
139 #include <machine/db_machdep.h>
140 #include <ddb/db_extern.h>
141 #endif
142
143 #include <machine/psl.h>
144 #include <machine/locore.h>
145
146 #include <mips/cavium/autoconf.h>
147 #include <mips/cavium/octeonvar.h>
148 #include <mips/cavium/include/iobusvar.h>
149 #include <mips/cavium/include/bootbusvar.h>
150
151 #include <mips/cavium/dev/octeon_uartreg.h>
152 #include <mips/cavium/dev/octeon_ciureg.h>
153 #include <mips/cavium/dev/octeon_gpioreg.h>
154
155 #include <evbmips/cavium/octeon_uboot.h>
156
157 static void mach_init_vector(void);
158 static void mach_init_bus_space(void);
159 static void mach_init_console(void);
160 static void mach_init_memory(void);
161
162 #include "com.h"
163 #if NCOM > 0
164 #include <dev/ic/comreg.h>
165 #include <dev/ic/comvar.h>
166 int comcnrate = 115200; /* XXX should be config option */
167 #endif /* NCOM > 0 */
168
169 /* Maps for VM objects. */
170 struct vm_map *phys_map = NULL;
171
172 int netboot;
173
174 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
175 int mem_cluster_cnt;
176 extern char kernel_text[];
177 extern char edata[];
178 extern char end[];
179
180 void mach_init(uint64_t, uint64_t, uint64_t, uint64_t);
181
182 struct octeon_config octeon_configuration;
183 struct octeon_btinfo octeon_btinfo;
184
185 char octeon_nmi_stack[PAGE_SIZE] __section(".data1") __aligned(PAGE_SIZE);
186
187 /*
188 * Do all the stuff that locore normally does before calling main().
189 */
190 void
191 mach_init(uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3)
192 {
193 uint64_t btinfo_paddr;
194 int corefreq;
195
196 /* clear the BSS segment */
197 memset(edata, 0, end - edata);
198
199 KASSERT(MIPS_XKPHYS_P(arg3));
200 btinfo_paddr = mips3_ld(arg3 + OCTEON_BTINFO_PADDR_OFFSET);
201
202 /* Should be in first 256MB segment */
203 KASSERT(btinfo_paddr < 256 * 1024 * 1024);
204 memcpy(&octeon_btinfo,
205 (struct octeon_btinfo *)MIPS_PHYS_TO_KSEG0(btinfo_paddr),
206 sizeof(octeon_btinfo));
207
208 corefreq = octeon_btinfo.obt_eclock_hz;
209
210 octeon_cal_timer(corefreq);
211
212 switch (MIPS_PRID_IMPL(mips_options.mips_cpu_id)) {
213 case 0: cpu_setmodel("Cavium Octeon CN38XX/CN36XX"); break;
214 case 1: cpu_setmodel("Cavium Octeon CN31XX/CN3020"); break;
215 case 2: cpu_setmodel("Cavium Octeon CN3005/CN3010"); break;
216 case 3: cpu_setmodel("Cavium Octeon CN58XX"); break;
217 case 4: cpu_setmodel("Cavium Octeon CN5[4-7]XX"); break;
218 case 6: cpu_setmodel("Cavium Octeon CN50XX"); break;
219 case 7: cpu_setmodel("Cavium Octeon CN52XX"); break;
220 default: cpu_setmodel("Cavium Octeon"); break;
221 }
222
223 mach_init_vector();
224
225 uvm_md_init();
226
227 mach_init_bus_space();
228
229 mach_init_console();
230
231 mach_init_memory();
232
233 /*
234 * Allocate uarea page for lwp0 and set it.
235 */
236 mips_init_lwp0_uarea();
237
238 boothowto = RB_AUTOBOOT;
239 boothowto |= AB_VERBOSE;
240
241 #if 0
242 curcpu()->ci_nmi_stack = octeon_nmi_stack + sizeof(octeon_nmi_stack) - sizeof(struct kernframe);
243 *(uint64_t *)MIPS_PHYS_TO_KSEG0(0x800) = (intptr_t)octeon_reset_vector;
244 const uint64_t wdog_reg = MIPS_PHYS_TO_XKPHYS_UNCACHED(CIU_WDOG0);
245 uint64_t wdog = mips3_ld(wdog_reg);
246 wdog &= ~(CIU_WDOGX_MODE|CIU_WDOGX_LEN);
247 wdog |= __SHIFTIN(3, CIU_WDOGX_MODE);
248 wdog |= CIU_WDOGX_LEN; // max period
249 mips64_sd_a64(wdog_reg, wdog);
250 printf("Watchdog enabled!\n");
251 #endif
252
253 #if defined(DDB)
254 if (boothowto & RB_KDB)
255 Debugger();
256 #endif
257 }
258
259 void
260 consinit(void)
261 {
262
263 /*
264 * Everything related to console initialization is done
265 * in mach_init().
266 */
267 }
268
269 void
270 mach_init_vector(void)
271 {
272
273 /* Make sure exception base at 0 (MIPS_COP_0_EBASE) */
274 __asm __volatile("mtc0 %0, $15, 1" : : "r"(0x80000000) );
275
276 /*
277 * Set up the exception vectors and CPU-specific function
278 * vectors early on. We need the wbflush() vector set up
279 * before comcnattach() is called (or at least before the
280 * first printf() after that is called).
281 * Also clears the I+D caches.
282 */
283 mips_vector_init(NULL, true);
284 }
285
286 void
287 mach_init_bus_space(void)
288 {
289 struct octeon_config *mcp = &octeon_configuration;
290
291 octeon_dma_init(mcp);
292
293 iobus_bootstrap(mcp);
294 bootbus_bootstrap(mcp);
295 }
296
297 void
298 mach_init_console(void)
299 {
300 #if NCOM > 0
301 struct octeon_config *mcp = &octeon_configuration;
302 int status;
303 extern int octuart_com_cnattach(bus_space_tag_t, int, int);
304
305 /*
306 * Delay to allow firmware putchars to complete.
307 * FIFO depth * character time.
308 * character time = (1000000 / (defaultrate / 10))
309 */
310 delay(640000000 / comcnrate);
311
312 status = octuart_com_cnattach(
313 &mcp->mc_iobus_bust,
314 0, /* XXX port 0 */
315 comcnrate);
316 if (status != 0)
317 panic("can't initialize console!"); /* XXX print to nowhere! */
318 #else
319 panic("octeon: not configured to use serial console");
320 #endif /* NCOM > 0 */
321 }
322
323 static void
324 mach_init_memory(void)
325 {
326 struct octeon_bootmem_desc *memdesc;
327 struct octeon_bootmem_block_header *block;
328 paddr_t blockaddr;
329 int i;
330
331 mem_cluster_cnt = 0;
332
333 if (octeon_btinfo.obt_phy_mem_desc_addr == 0)
334 panic("bootmem desc is missing");
335
336 memdesc = (void *)MIPS_PHYS_TO_XKPHYS(CCA_CACHEABLE,
337 octeon_btinfo.obt_phy_mem_desc_addr);
338 printf("u-boot bootmem desc @ 0x%x version %d.%d\n",
339 octeon_btinfo.obt_phy_mem_desc_addr,
340 memdesc->bmd_major_version, memdesc->bmd_minor_version);
341 if (memdesc->bmd_major_version > 3)
342 panic("unhandled bootmem desc version %d.%d",
343 memdesc->bmd_major_version, memdesc->bmd_minor_version);
344
345 blockaddr = memdesc->bmd_head_addr;
346 if (blockaddr == 0)
347 panic("bootmem list is empty");
348
349 for (i = 0; i < VM_PHYSSEG_MAX && blockaddr != 0;
350 i++, blockaddr = block->bbh_next_block_addr) {
351 block = (void *)MIPS_PHYS_TO_XKPHYS(CCA_CACHEABLE, blockaddr);
352
353 mem_clusters[mem_cluster_cnt].start = blockaddr;
354 mem_clusters[mem_cluster_cnt].size = block->bbh_size;
355 mem_cluster_cnt++;
356 }
357
358 physmem = btoc(octeon_btinfo.obt_dram_size * 1024 * 1024);
359
360 #ifdef MULTIPROCESSOR
361 const u_int cores = mipsNN_cp0_ebase_read() & MIPS_EBASE_CPUNUM;
362 mem_clusters[0].start = cores * 4096;
363 #endif
364
365 /*
366 * Load the rest of the available pages into the VM system.
367 */
368 mips_page_physload(mips_trunc_page(kernel_text), mips_round_page(end),
369 mem_clusters, mem_cluster_cnt, NULL, 0);
370
371 /*
372 * Initialize error message buffer (at end of core).
373 */
374 mips_init_msgbuf();
375
376 pmap_bootstrap();
377 }
378
379 /*
380 * cpu_startup
381 * cpu_reboot
382 */
383
384 int waittime = -1;
385
386 /*
387 * Allocate memory for variable-sized tables,
388 */
389 void
390 cpu_startup(void)
391 {
392 #ifdef MULTIPROCESSOR
393 // Create a kcpuset so we can see on which CPUs the kernel was started.
394 kcpuset_create(&cpus_booted, true);
395 #endif
396
397 /*
398 * Do the common startup items.
399 */
400 cpu_startup_common();
401
402 /*
403 * Virtual memory is bootstrapped -- notify the bus spaces
404 * that memory allocation is now safe.
405 */
406 octeon_configuration.mc_mallocsafe = 1;
407 }
408
409 void
410 cpu_reboot(int howto, char *bootstr)
411 {
412
413 /* Take a snapshot before clobbering any registers. */
414 savectx(curpcb);
415
416 if (cold) {
417 howto |= RB_HALT;
418 goto haltsys;
419 }
420
421 /* If "always halt" was specified as a boot flag, obey. */
422 if (boothowto & RB_HALT)
423 howto |= RB_HALT;
424
425 boothowto = howto;
426 if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
427 waittime = 0;
428 vfs_shutdown();
429
430 /*
431 * If we've been adjusting the clock, the todr
432 * will be out of synch; adjust it now.
433 */
434 resettodr();
435 }
436
437 splhigh();
438
439 if (howto & RB_DUMP)
440 dumpsys();
441
442 haltsys:
443 doshutdownhooks();
444
445 if (howto & RB_HALT) {
446 printf("\n");
447 printf("The operating system has halted.\n");
448 printf("Please press any key to reboot.\n\n");
449 cnpollc(1); /* For proper keyboard command handling */
450 cngetc();
451 cnpollc(0);
452 }
453
454 printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
455
456 /*
457 * Need a small delay here, otherwise we see the first few characters of
458 * the warning below.
459 */
460 delay(80000);
461
462 /* initiate chip soft-reset */
463 uint64_t fuse = octeon_read_csr(CIU_FUSE);
464 octeon_write_csr(CIU_SOFT_BIST, fuse);
465 octeon_read_csr(CIU_SOFT_RST);
466 octeon_write_csr(CIU_SOFT_RST, fuse);
467
468 delay(1000000);
469
470 printf("WARNING: reset failed!\nSpinning...");
471
472 for (;;)
473 /* spin forever */ ; /* XXX */
474 }
475