machdep.c revision 1.1.2.5 1 /* $NetBSD: machdep.c,v 1.1.2.5 2009/09/22 07:47:30 cliff 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.1.2.5 2009/09/22 07:47:30 cliff Exp $");
116
117 #include "opt_ddb.h"
118 #include "opt_com.h"
119 #include "opt_execfmt.h"
120 #include "opt_memsize.h"
121
122 #include <sys/param.h>
123 #include <sys/systm.h>
124 #include <sys/kernel.h>
125 #include <sys/buf.h>
126 #include <sys/reboot.h>
127 #include <sys/user.h>
128 #include <sys/mount.h>
129 #include <sys/kcore.h>
130 #include <sys/boot_flag.h>
131 #include <sys/termios.h>
132 #include <sys/ksyms.h>
133 #include <sys/bus.h>
134 #include <sys/device.h>
135
136 #include <uvm/uvm_extern.h>
137
138 #include <dev/cons.h>
139
140 #include "ksyms.h"
141
142 #if NKSYMS || defined(DDB) || defined(LKM)
143 #include <machine/db_machdep.h>
144 #include <ddb/db_extern.h>
145 #endif
146
147 #include <machine/cpu.h>
148 #include <machine/psl.h>
149
150 #include "com.h"
151 #if NCOM == 0
152 #error no serial console
153 #endif
154
155 #include <dev/ic/comreg.h>
156 #include <dev/ic/comvar.h>
157
158 #include <mips/rmi/rmixl_comvar.h>
159 #include <mips/rmi/rmixlvar.h>
160 #include <mips/rmi/rmixl_firmware.h>
161 #include <mips/rmi/rmixlreg.h>
162
163 #ifndef CONSFREQ
164 # define CONSFREQ -1 /* inherit from firmware */
165 #endif
166 #ifndef CONSPEED
167 # define CONSPEED 38400
168 #endif
169 #ifndef CONMODE
170 # define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8)
171 #endif
172 #ifndef CONSADDR
173 # define CONSADDR RMIXL_IO_DEV_UART_1
174 #endif
175
176 int comcnfreq = CONSFREQ;
177 int comcnspeed = CONSPEED;
178 tcflag_t comcnmode = CONMODE;
179 bus_addr_t comcnaddr = (bus_addr_t)CONSADDR;
180
181 struct rmixl_config rmixl_configuration;
182
183
184 /*
185 * array of tested firmware versions
186 * if you find new ones and they work
187 * please add them
188 */
189 static uint64_t rmiclfw_psb_versions[] = {
190 0x4958d4fb00000056,
191 0x49a5a8fa00000056,
192 };
193 #define RMICLFW_PSB_VERSIONS_LEN \
194 (sizeof(rmiclfw_psb_versions)/sizeof(rmiclfw_psb_versions[0]))
195
196 /*
197 * kernel copies of firmware info
198 */
199 static rmixlfw_info_t rmixlfw_info;
200 static rmixlfw_mmap_t rmixlfw_phys_mmap;
201 static rmixlfw_mmap_t rmixlfw_avail_mmap;
202 #define RMIXLFW_INFOP_LEGAL 0x8c000000
203
204
205 /* For sysctl_hw. */
206 extern char cpu_model[];
207
208 /* Our exported CPU info; we can have only one. */
209 struct cpu_info cpu_info_store;
210
211 /* Maps for VM objects. */
212 struct vm_map *mb_map = NULL;
213 struct vm_map *phys_map = NULL;
214
215 int physmem; /* Total physical memory */
216
217 int netboot; /* Are we netbooting? */
218
219
220 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
221 int mem_cluster_cnt;
222
223 void configure(void);
224 void mach_init(int, int32_t *, void *, void *);
225 static u_long rmixlfw_init(void *);
226 static u_long mem_clusters_init(rmixlfw_mmap_t *, rmixlfw_mmap_t *);
227 static void __attribute__((__noreturn__)) rmixl_exit(int);
228
229
230 /*
231 * safepri is a safe priority for sleep to set for a spin-wait during
232 * autoconfiguration or after a panic. Used as an argument to splx().
233 */
234 int safepri = MIPS1_PSL_LOWIPL;
235
236 extern struct user *proc0paddr;
237
238 /*
239 * Do all the stuff that locore normally does before calling main().
240 */
241 void
242 mach_init(int argc, int32_t *argv, void *envp, void *infop)
243 {
244 struct rmixl_config *rcp = &rmixl_configuration;
245 void *kernend, *v;
246 size_t first, last;
247 u_long memsize;
248 extern char edata[], end[];
249
250 /*
251 * Clear the BSS segment.
252 */
253 kernend = (void *)mips_round_page(end);
254 memset(edata, 0, (char *)kernend - edata);
255
256 /*
257 * Set up the exception vectors and CPU-specific function
258 * vectors early on. We need the wbflush() vector set up
259 * before comcnattach() is called (or at least before the
260 * first printf() after that is called).
261 * Also clears the I+D caches.
262 */
263 mips_vector_init();
264
265 memsize = rmixlfw_init(infop);
266
267 /* set the VM page size */
268 uvm_setpagesize();
269
270 physmem = btoc(memsize);
271
272 rmixl_eb_bus_mem_init(&rcp->rc_eb_memt, rcp); /* need for console */
273 rmixl_el_bus_mem_init(&rcp->rc_el_memt, rcp); /* XXX defer ? */
274
275 #if NCOM > 0
276 rmixl_com_cnattach(comcnaddr, comcnspeed, -1,
277 COM_TYPE_NORMAL, comcnmode);
278 #endif
279
280 printf("\nNetBSD/rmixl\n");
281 printf("memsize = %#lx\n", memsize);
282
283 /*
284 * Obtain the cpu frequency
285 * Compute the number of ticks for hz.
286 * Compute the delay divisor.
287 * Double the Hz if this CPU runs at twice the
288 * external/cp0-count frequency
289 */
290 curcpu()->ci_cpu_freq = rmixlfw_info.cpu_frequency;
291 curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
292 curcpu()->ci_divisor_delay =
293 ((curcpu()->ci_cpu_freq + 500000) / 1000000);
294 if (mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
295 curcpu()->ci_cpu_freq *= 2;
296
297 /*
298 * Look at arguments passed to us and compute boothowto.
299 * - rmixl firmware gives us a 32 bit argv[i], so adapt
300 * by forcing sign extension in cast to (char *)
301 */
302 boothowto = RB_AUTOBOOT;
303 for (int i = 1; i < argc; i++) {
304 for (char *cp = (char *)(uint64_t)argv[i]; *cp; cp++) {
305 int howto;
306 /* Ignore superfluous '-', if there is one */
307 if (*cp == '-')
308 continue;
309
310 howto = 0;
311 BOOT_FLAG(*cp, howto);
312 if (! howto)
313 printf("bootflag '%c' not recognised\n", *cp);
314 else
315 boothowto |= howto;
316 }
317 }
318 printf("boothowto %#x\n", boothowto);
319
320 /*
321 * Load the rest of the available pages into the VM system.
322 */
323 first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
324 last = mem_clusters[0].start + mem_clusters[0].size;
325 uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
326 VM_FREELIST_DEFAULT);
327 for (int i = 1; i < mem_cluster_cnt; i++) {
328 first = round_page(mem_clusters[i].start);
329 last = mem_clusters[i].start + mem_clusters[i].size;
330 uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
331 VM_FREELIST_DEFAULT);
332 }
333
334 /*
335 * Initialize error message buffer (at end of core).
336 */
337 mips_init_msgbuf();
338
339 pmap_bootstrap();
340
341 /*
342 * Allocate space for proc0's USPACE.
343 */
344 v = (void *)uvm_pageboot_alloc(USPACE);
345 lwp0.l_addr = proc0paddr = (struct user *)v;
346 lwp0.l_md.md_regs = (struct frame *)((char *)v + USPACE) - 1;
347 #ifdef _LP64
348 lwp0.l_md.md_regs->f_regs[_R_SR] = MIPS_SR_KX;
349 #endif
350 proc0paddr->u_pcb.pcb_context.val[_L_SR] =
351 #ifdef _LP64
352 MIPS_SR_KX |
353 #endif
354 MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
355
356
357 /*
358 * Initialize debuggers, and break into them, if appropriate.
359 */
360 #if NKSYMS || defined(DDB) || defined(LKM)
361 ksyms_init(0, 0, 0);
362 #endif
363
364 #if defined(DDB)
365 if (boothowto & RB_KDB)
366 Debugger();
367 #endif
368
369 }
370
371 static u_long
372 rmixlfw_init(void *infop)
373 {
374 struct rmixl_config *rcp = &rmixl_configuration;
375 uint64_t tmp;
376
377 strcpy(cpu_model, "RMI XLS616ATX VIIA"); /* XXX */
378
379 tmp = (int64_t)infop;
380 tmp |= 0xffffffffULL << 32;
381 infop = (void *)tmp;
382 rmixlfw_info = *(rmixlfw_info_t *)infop;
383
384 for (int i=0; i < RMICLFW_PSB_VERSIONS_LEN; i++) {
385 if (rmiclfw_psb_versions[i] == rmixlfw_info.psb_version)
386 goto found;
387 }
388
389 rcp->rc_io_pbase = MIPS_KSEG1_TO_PHYS(RMIXL_IO_DEV_PBASE);
390 rmixl_putchar_init(rcp->rc_io_pbase);
391
392 rmixl_puts("\r\nWARNING: untested psb_version: ");
393 rmixl_puthex64(rmixlfw_info.psb_version);
394 rmixl_puts("\r\n");
395
396 found:
397 rcp->rc_io_pbase = MIPS_KSEG1_TO_PHYS(rmixlfw_info.io_base);
398 rmixl_putchar_init(rcp->rc_io_pbase);
399 rmixl_puts("\r\ninfop: ");
400 rmixl_puthex64((uint64_t)infop);
401 rmixl_puts("\r\nrecognized psb_version: ");
402 rmixl_puthex64(rmixlfw_info.psb_version);
403 rmixl_puts("\r\n");
404
405 return mem_clusters_init(
406 (rmixlfw_mmap_t *)rmixlfw_info.psb_physaddr_map,
407 (rmixlfw_mmap_t *)rmixlfw_info.avail_mem_map);
408 }
409
410 static u_long
411 mem_clusters_init(
412 rmixlfw_mmap_t *psb_physaddr_map,
413 rmixlfw_mmap_t *avail_mem_map)
414 {
415 uint64_t tmp;
416 uint64_t sz;
417 uint64_t sum;
418 #ifdef MEMSIZE
419 u_long memsize = MEMSIZE;
420 #endif
421
422 rmixl_puts("psb_physaddr_map: ");
423 rmixl_puthex64((uint64_t)psb_physaddr_map);
424 rmixl_puts("\r\n");
425
426 if (psb_physaddr_map == NULL)
427 rmixl_puts("WARNING: no psb_physaddr_map\r\n");
428 else
429 rmixlfw_phys_mmap = *psb_physaddr_map;
430
431 rmixl_puts("avail_mem_map: ");
432 rmixl_puthex64((uint64_t)avail_mem_map);
433 rmixl_puts("\r\n");
434 if (avail_mem_map == NULL) {
435 #ifndef MEMSIZE
436 rmixl_puts("ERROR: no avail_mem_map, "
437 "must define MEMSIZE\r\n");
438 #else
439 rmixl_puts("WARNING: no avail_mem_map, "
440 "using MEMSIZE\r\n");
441 mem_clusters[0].start = 0;
442 mem_clusters[0].size = MEMSIZE;
443 mem_cluster_cnt = 1;
444 return MEMSIZE;
445 #endif
446 }
447
448 rmixl_puts("using avail_mem_map\r\n");
449 rmixlfw_avail_mmap = *(rmixlfw_mmap_t *)rmixlfw_info.avail_mem_map;
450 rmixl_puts("memory clusters map:\r\n");
451 sum = 0;
452 mem_cluster_cnt = 0;
453 for (uint32_t i=0; i < rmixlfw_avail_mmap.nmmaps; i++) {
454 if (rmixlfw_avail_mmap.entry[i].type != RMIXLFW_MMAP_TYPE_RAM)
455 continue;
456 mem_clusters[i].start = rmixlfw_avail_mmap.entry[i].start;
457 sz = rmixlfw_avail_mmap.entry[i].size;
458 sum += sz;
459 mem_clusters[i].size = sz;
460 mem_cluster_cnt++;
461 #ifdef DEBUG
462 rmixl_puthex32(i);
463 rmixl_puts(": ");
464 rmixl_puthex64(mem_clusters[i].start);
465 rmixl_puts(": ");
466 rmixl_puthex64(sz);
467 rmixl_puts(": ");
468 rmixl_puthex64(sum);
469 rmixl_puts("\r\n");
470 #endif
471 #ifdef MEMSIZE
472 /*
473 * configurably limit memsize
474 */
475 if (sum == memsize)
476 break;
477 if (sum > memsize) {
478 tmp = sum - memsize;
479 sz -= tmp;
480 sum -= tmp;
481 mem_clusters[i].size = sz;
482 break;
483 }
484 #endif
485 }
486 return sum;
487 }
488
489 void
490 consinit(void)
491 {
492
493 /*
494 * Everything related to console initialization is done
495 * in mach_init().
496 */
497 }
498
499 /*
500 * Allocate memory for variable-sized tables,
501 */
502 void
503 cpu_startup()
504 {
505 vaddr_t minaddr, maxaddr;
506 char pbuf[9];
507
508 /*
509 * Good {morning,afternoon,evening,night}.
510 */
511 printf("%s%s", copyright, version);
512 format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
513 printf("total memory = %s\n", pbuf);
514
515 /*
516 * Virtual memory is bootstrapped -- notify the bus spaces
517 * that memory allocation is now safe.
518 */
519 rmixl_configuration.rc_mallocsafe = 1;
520
521 minaddr = 0;
522 /*
523 * Allocate a submap for physio.
524 */
525 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
526 VM_PHYS_SIZE, 0, FALSE, NULL);
527
528 /*
529 * (No need to allocate an mbuf cluster submap. Mbuf clusters
530 * are allocated via the pool allocator, and we use KSEG to
531 * map those pages.)
532 */
533
534 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
535 printf("avail memory = %s\n", pbuf);
536 }
537
538 int waittime = -1;
539
540 void
541 cpu_reboot(howto, bootstr)
542 int howto;
543 char *bootstr;
544 {
545
546 /* Take a snapshot before clobbering any registers. */
547 if (curproc)
548 savectx((struct user *)curpcb);
549
550 if (cold) {
551 howto |= RB_HALT;
552 goto haltsys;
553 }
554
555 /* If "always halt" was specified as a boot flag, obey. */
556 if (boothowto & RB_HALT)
557 howto |= RB_HALT;
558
559 boothowto = howto;
560 if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
561 waittime = 0;
562 vfs_shutdown();
563
564 /*
565 * If we've been adjusting the clock, the todr
566 * will be out of synch; adjust it now.
567 */
568 resettodr();
569 }
570
571 splhigh();
572
573 if (howto & RB_DUMP)
574 dumpsys();
575
576 haltsys:
577 doshutdownhooks();
578
579 if (howto & RB_HALT) {
580 printf("\n");
581 printf("The operating system has halted.\n");
582 printf("Please press any key to reboot.\n\n");
583 cnpollc(1); /* For proper keyboard command handling */
584 cngetc();
585 cnpollc(0);
586 }
587
588 printf("rebooting...\n\n");
589
590 rmixl_exit(0);
591 }
592
593 /*
594 * goodbye world
595 */
596 #define GPIO_CPU_RST 0xa0 /* XXX TMP */
597 void __attribute__((__noreturn__))
598 rmixl_exit(int howto)
599 {
600 /* use firmware callbak to reboot */
601 void (*reset)(void) = (void *)rmixlfw_info.warm_reset;
602 if (reset != 0) {
603 (*reset)();
604 printf("warm reset callback failed, spinning...\n");
605 } else {
606 printf("warm reset callback absent, spinning...\n");
607 }
608 for (;;);
609 }
610