machdep.c revision 1.2 1 /* $NetBSD: machdep.c,v 1.2 2015/05/18 01:32:18 matt 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 <sys/cdefs.h>
115 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.2 2015/05/18 01:32:18 matt Exp $");
116
117 #include <sys/param.h>
118 #include <sys/systm.h>
119 #include <sys/kernel.h>
120 #include <sys/buf.h>
121 #include <sys/cpu.h>
122 #include <sys/reboot.h>
123 #include <sys/mount.h>
124 #include <sys/kcore.h>
125 #include <sys/boot_flag.h>
126 #include <sys/termios.h>
127 #include <sys/ksyms.h>
128
129 #include <uvm/uvm_extern.h>
130
131 #include <dev/cons.h>
132
133 #include "ksyms.h"
134
135 #if NKSYMS || defined(DDB) || defined(LKM)
136 #include <machine/db_machdep.h>
137 #include <ddb/db_extern.h>
138 #endif
139
140 #include <machine/psl.h>
141 #include <machine/locore.h>
142
143 #include <mips/cavium/autoconf.h>
144 #include <mips/cavium/octeonvar.h>
145 #include <mips/cavium/include/iobusvar.h>
146 #include <mips/cavium/include/bootbusvar.h>
147
148 #include <mips/cavium/dev/octeon_uartreg.h>
149 #include <mips/cavium/dev/octeon_ciureg.h>
150 #include <mips/cavium/dev/octeon_gpioreg.h>
151
152 #include <evbmips/cavium/octeon_uboot.h>
153
154 static void mach_init_bss(void);
155 static void mach_init_vector(void);
156 static void mach_init_bus_space(void);
157 static void mach_init_console(void);
158 static void mach_init_memory(u_quad_t);
159
160 #include "com.h"
161 #if NCOM > 0
162 #include <dev/ic/comreg.h>
163 #include <dev/ic/comvar.h>
164 int comcnrate = 115200; /* XXX should be config option */
165 #endif /* NCOM > 0 */
166
167 /* Maps for VM objects. */
168 struct vm_map *phys_map = NULL;
169
170 int netboot;
171
172 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
173 int mem_cluster_cnt;
174
175
176 void configure(void);
177 void mach_init(uint64_t, uint64_t, uint64_t, uint64_t);
178
179 struct octeon_config octeon_configuration;
180 struct octeon_btinfo octeon_btinfo;
181
182 /*
183 * Do all the stuff that locore normally does before calling main().
184 */
185 void
186 mach_init(uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3)
187 {
188 uint64_t btinfo_paddr;
189 u_quad_t memsize;
190 int corefreq;
191
192 mach_init_bss();
193
194 KASSERT(MIPS_XKPHYS_P(arg3));
195 btinfo_paddr = mips64_ld_a64(arg3 + OCTEON_BTINFO_PADDR_OFFSET);
196
197 /* Should be in first 256MB segment */
198 KASSERT(btinfo_paddr < 256 * 1024 * 1024);
199 memcpy(&octeon_btinfo,
200 (struct octeon_btinfo *)MIPS_PHYS_TO_KSEG0(btinfo_paddr),
201 sizeof(octeon_btinfo));
202
203 corefreq = octeon_btinfo.obt_eclock_hz;
204 memsize = octeon_btinfo.obt_dram_size * 1024 * 1024;
205
206 octeon_cal_timer(corefreq);
207
208 switch (MIPS_PRID_IMPL(mips_options.mips_cpu_id)) {
209 case 0: cpu_setmodel("Cavium Octeon CN38XX/CN36XX"); break;
210 case 1: cpu_setmodel("Cavium Octeon CN31XX/CN3020"); break;
211 case 2: cpu_setmodel("Cavium Octeon CN3005/CN3010"); break;
212 case 3: cpu_setmodel("Cavium Octeon CN58XX"); break;
213 case 4: cpu_setmodel("Cavium Octeon CN5[4-7]XX"); break;
214 case 6: cpu_setmodel("Cavium Octeon CN50XX"); break;
215 case 7: cpu_setmodel("Cavium Octeon CN52XX"); break;
216 default: cpu_setmodel("Cavium Octeon"); break;
217 }
218
219 mach_init_vector();
220
221 /* set the VM page size */
222 uvm_setpagesize();
223
224 mach_init_bus_space();
225
226 mach_init_console();
227
228 mach_init_memory(memsize);
229
230 /*
231 * Allocate uarea page for lwp0 and set it.
232 */
233 mips_init_lwp0_uarea();
234
235 boothowto = RB_AUTOBOOT;
236
237 #if defined(DDB)
238 if (boothowto & RB_KDB)
239 Debugger();
240 #endif
241 }
242
243 void
244 consinit(void)
245 {
246
247 /*
248 * Everything related to console initialization is done
249 * in mach_init().
250 */
251 }
252
253 void
254 mach_init_bss(void)
255 {
256 extern char edata[], end[];
257
258 /*
259 * Clear the BSS segment.
260 */
261 memset(edata, 0, mips_round_page(end) - (uintptr_t)edata);
262 }
263
264 void
265 mach_init_vector(void)
266 {
267
268 /* Make sure exception base at 0 (MIPS_COP_0_EBASE) */
269 asm volatile("mtc0 %0, $15, 1" : : "r"(0x80000000) );
270
271 /*
272 * Set up the exception vectors and CPU-specific function
273 * vectors early on. We need the wbflush() vector set up
274 * before comcnattach() is called (or at least before the
275 * first printf() after that is called).
276 * Also clears the I+D caches.
277 */
278 mips_vector_init(NULL, false);
279 }
280
281 void
282 mach_init_bus_space(void)
283 {
284 struct octeon_config *mcp = &octeon_configuration;
285
286 octeon_dma_init(mcp);
287
288 iobus_bootstrap(mcp);
289 bootbus_bootstrap(mcp);
290 }
291
292 void
293 mach_init_console(void)
294 {
295 #if NCOM > 0
296 struct octeon_config *mcp = &octeon_configuration;
297 int status;
298 extern int octeon_uart_com_cnattach(bus_space_tag_t, int, int);
299
300 /*
301 * Delay to allow firmware putchars to complete.
302 * FIFO depth * character time.
303 * character time = (1000000 / (defaultrate / 10))
304 */
305 delay(640000000 / comcnrate);
306
307 status = octeon_uart_com_cnattach(
308 &mcp->mc_iobus_bust,
309 0, /* XXX port 0 */
310 comcnrate);
311 if (status != 0)
312 panic("can't initialize console!"); /* XXX print to nowhere! */
313 #else
314 panic("octeon: not configured to use serial console");
315 #endif /* NCOM > 0 */
316 }
317
318 void
319 mach_init_memory(u_quad_t memsize)
320 {
321 extern char end[];
322
323 physmem = btoc(memsize);
324
325 if (memsize <= 256 * 1024 * 1024) {
326 mem_clusters[0].start = 0;
327 mem_clusters[0].size = memsize;
328 mem_cluster_cnt = 1;
329 } else if (memsize <= 512 * 1024 * 1024) {
330 mem_clusters[0].start = 0;
331 mem_clusters[0].size = 256 * 1024 * 1024;
332 mem_clusters[1].start = 0x410000000ULL;
333 mem_clusters[1].size = memsize - 256 * 1024 * 1024;
334 mem_cluster_cnt = 2;
335 } else {
336 mem_clusters[0].start = 0;
337 mem_clusters[0].size = 256 * 1024 * 1024;
338 mem_clusters[1].start = 0x20000000;
339 mem_clusters[1].size = memsize - 512 * 1024 * 1024;
340 mem_clusters[2].start = 0x410000000ULL;
341 mem_clusters[2].size = 256 * 1024 * 1024;
342 mem_cluster_cnt = 3;
343 }
344
345 /*
346 * Load the rest of the available pages into the VM system.
347 */
348 mips_page_physload(MIPS_KSEG0_START, mips_round_page(end),
349 mem_clusters, mem_cluster_cnt, NULL, 0);
350
351 /*
352 * Initialize error message buffer (at end of core).
353 */
354 mips_init_msgbuf();
355
356 pmap_bootstrap();
357 }
358
359 /*
360 * cpu_startup
361 * cpu_reboot
362 */
363
364 int waittime = -1;
365
366 /*
367 * Allocate memory for variable-sized tables,
368 */
369 void
370 cpu_startup(void)
371 {
372 /*
373 * Do the common startup items.
374 */
375 cpu_startup_common();
376
377 /*
378 * Virtual memory is bootstrapped -- notify the bus spaces
379 * that memory allocation is now safe.
380 */
381 octeon_configuration.mc_mallocsafe = 1;
382 }
383
384 void
385 cpu_reboot(int howto, char *bootstr)
386 {
387
388 /* Take a snapshot before clobbering any registers. */
389 savectx(curpcb);
390
391 if (cold) {
392 howto |= RB_HALT;
393 goto haltsys;
394 }
395
396 /* If "always halt" was specified as a boot flag, obey. */
397 if (boothowto & RB_HALT)
398 howto |= RB_HALT;
399
400 boothowto = howto;
401 if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
402 waittime = 0;
403 vfs_shutdown();
404
405 /*
406 * If we've been adjusting the clock, the todr
407 * will be out of synch; adjust it now.
408 */
409 resettodr();
410 }
411
412 splhigh();
413
414 if (howto & RB_DUMP)
415 dumpsys();
416
417 haltsys:
418 doshutdownhooks();
419
420 if (howto & RB_HALT) {
421 printf("\n");
422 printf("The operating system has halted.\n");
423 printf("Please press any key to reboot.\n\n");
424 }
425
426 printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
427
428 /*
429 * Need a small delay here, otherwise we see the first few characters of
430 * the warning below.
431 */
432 delay(80000);
433
434 /* initiate chip soft-reset */
435 octeon_write_csr(CIU_SOFT_BIST, 0x0000000000000001ULL);
436 octeon_read_csr(CIU_SOFT_RST);
437 octeon_write_csr(CIU_SOFT_RST, 0x0000000000000001ULL);
438
439 delay(1000000);
440
441 printf("WARNING: reset failed!\nSpinning...");
442
443 for (;;)
444 /* spin forever */ ; /* XXX */
445 }
446