machdep.c revision 1.1.2.22 1 /* $NetBSD: machdep.c,v 1.1.2.22 2010/02/27 21:26:28 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.1.2.22 2010/02/27 21:26:28 matt 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 #include <sys/extent.h>
136 #include <sys/malloc.h>
137
138 #include <uvm/uvm_extern.h>
139
140 #include <dev/cons.h>
141
142 #include "ksyms.h"
143
144 #if NKSYMS || defined(DDB) || defined(LKM)
145 #include <machine/db_machdep.h>
146 #include <ddb/db_extern.h>
147 #endif
148
149 #include <machine/cpu.h>
150 #include <machine/psl.h>
151
152 #include "com.h"
153 #if NCOM == 0
154 #error no serial console
155 #endif
156
157 #include <dev/ic/comreg.h>
158 #include <dev/ic/comvar.h>
159
160 #include <mips/rmi/rmixl_comvar.h>
161 #include <mips/rmi/rmixlvar.h>
162 #include <mips/rmi/rmixl_firmware.h>
163 #include <mips/rmi/rmixlreg.h>
164
165 #ifdef MACHDEP_DEBUG
166 int machdep_debug=MACHDEP_DEBUG;
167 # define DPRINTF(x) do { if (machdep_debug) printf x ; } while(0)
168 #else
169 # define DPRINTF(x)
170 #endif
171
172 #ifndef CONSFREQ
173 # define CONSFREQ 66000000
174 #endif
175 #ifndef CONSPEED
176 # define CONSPEED 38400
177 #endif
178 #ifndef CONMODE
179 # define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8)
180 #endif
181 #ifndef CONSADDR
182 # define CONSADDR RMIXL_IO_DEV_UART_1
183 #endif
184
185 int comcnfreq = CONSFREQ;
186 int comcnspeed = CONSPEED;
187 tcflag_t comcnmode = CONMODE;
188 bus_addr_t comcnaddr = (bus_addr_t)CONSADDR;
189
190 struct rmixl_config rmixl_configuration;
191
192
193 /*
194 * array of tested firmware versions
195 * if you find new ones and they work
196 * please add them
197 */
198 typedef struct rmiclfw_psb_id {
199 uint64_t psb_version;
200 rmixlfw_psb_type_t psb_type;
201 } rmiclfw_psb_id_t;
202 static rmiclfw_psb_id_t rmiclfw_psb_id[] = {
203 { 0x4958d4fb00000056ULL, PSB_TYPE_RMI },
204 { 0x49a5a8fa00000056ULL, PSB_TYPE_DELL },
205 { 0x4aacdb6a00000056ULL, PSB_TYPE_RMI },
206 };
207 #define RMICLFW_PSB_VERSIONS_LEN \
208 (sizeof(rmiclfw_psb_id)/sizeof(rmiclfw_psb_id[0]))
209
210 /*
211 * storage for fixed extent used to allocate physical address regions
212 * because extent(9) start and end values are u_long, they are only
213 * 32 bits on a 32 bit kernel, which is insuffucuent since XLS physical
214 * address is 40 bits wide. So the "physaddr" map stores regions
215 * in units of megabytes.
216 */
217 static u_long rmixl_physaddr_storage[
218 EXTENT_FIXED_STORAGE_SIZE(32)/sizeof(u_long)
219 ];
220
221 /* For sysctl_hw. */
222 extern char cpu_model[];
223
224 /* Our exported CPU info; we can have only one. */
225 struct cpu_info cpu_info_store;
226
227 /* Maps for VM objects. */
228 struct vm_map *mb_map = NULL;
229 struct vm_map *phys_map = NULL;
230
231 int physmem; /* Total physical memory */
232
233 int netboot; /* Are we netbooting? */
234
235
236 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
237 u_int mem_cluster_cnt;
238
239
240 void configure(void);
241 void mach_init(int, int32_t *, void *, int64_t);
242 static uint64_t rmixlfw_init(int64_t);
243 static uint64_t mem_clusters_init(rmixlfw_mmap_t *, rmixlfw_mmap_t *);
244 static void __attribute__((__noreturn__)) rmixl_reset(void);
245 static void rmixl_physaddr_init(void);
246 static u_int ram_seg_resv(phys_ram_seg_t *, u_int, u_quad_t, u_quad_t);
247 void rmixlfw_mmap_print(rmixlfw_mmap_t *);
248
249
250 #ifdef MULTIPROCESSOR
251 static bool rmixl_fixup_cop0_oscratch(int32_t, uint32_t [2]);
252 void rmixl_get_wakeup_info(struct rmixl_config *);
253 #ifdef MACHDEP_DEBUG
254 static void rmixl_wakeup_info_print(volatile rmixlfw_cpu_wakeup_info_t *);
255 #endif
256 #endif
257
258
259 /*
260 * safepri is a safe priority for sleep to set for a spin-wait during
261 * autoconfiguration or after a panic. Used as an argument to splx().
262 */
263 int safepri = MIPS1_PSL_LOWIPL;
264
265 /*
266 * Do all the stuff that locore normally does before calling main().
267 */
268 void
269 mach_init(int argc, int32_t *argv, void *envp, int64_t infop)
270 {
271 struct rmixl_config *rcp = &rmixl_configuration;
272 void *kernend;
273 uint64_t memsize;
274 u_int vm_cluster_cnt;
275 uint32_t r;
276 phys_ram_seg_t vm_clusters[VM_PHYSSEG_MAX];
277 extern char edata[], end[];
278
279 #ifndef MULTIPROCESSOR
280 rmixl_mtcr(0, 1); /* disable all threads except #0 */
281 rmixl_mtcr(0x400, 0); /* enable MMU clock gating */
282 /* set single MMU Thread Mode */
283 /* TLB is partitioned (1 partition) */
284 #endif
285
286 r = rmixl_mfcr(0x300);
287 r &= ~__BIT(14); /* disabled Unaligned Access */
288 rmixl_mtcr(0x300, r);
289
290 /*
291 * Clear the BSS segment.
292 */
293 kernend = (void *)mips_round_page(end);
294 memset(edata, 0, (char *)kernend - edata);
295
296 /*
297 * Set up the exception vectors and CPU-specific function
298 * vectors early on. We need the wbflush() vector set up
299 * before comcnattach() is called (or at least before the
300 * first printf() after that is called).
301 * Also clears the I+D caches.
302 */
303 mips_vector_init();
304
305 /* mips_vector_init initialized mips_options */
306 strcpy(cpu_model, mips_options.mips_cpu->cpu_name);
307
308 /* get system info from firmware */
309 memsize = rmixlfw_init(infop);
310
311 /* set the VM page size */
312 uvm_setpagesize();
313
314 physmem = btoc(memsize);
315
316 rmixl_obio_eb_bus_mem_init(&rcp->rc_obio_eb_memt, rcp);
317
318 #if NCOM > 0
319 rmixl_com_cnattach(comcnaddr, comcnspeed, comcnfreq,
320 COM_TYPE_NORMAL, comcnmode);
321 #endif
322
323 printf("\nNetBSD/rmixl\n");
324 printf("memsize = %#"PRIx64"\n", memsize);
325
326 #if defined(MULTIPROCESSOR) && defined(MACHDEP_DEBUG)
327 rmixl_wakeup_info_print(rcp->rc_cpu_wakeup_info);
328 rmixl_wakeup_info_print(rcp->rc_cpu_wakeup_info + 1);
329 printf("cpu_wakeup_info %p, cpu_wakeup_end %p\n",
330 rcp->rc_cpu_wakeup_info,
331 rcp->rc_cpu_wakeup_end);
332 printf("userapp_cpu_map: %#"PRIx64"\n",
333 rcp->rc_psb_info.userapp_cpu_map);
334 printf("wakeup: %#"PRIx64"\n", rcp->rc_psb_info.wakeup);
335 {
336 register_t sp;
337 asm volatile ("move %0, $sp\n" : "=r"(sp));
338 printf("sp: %#"PRIx64"\n", sp);
339 }
340 #endif
341
342 rmixl_physaddr_init();
343
344 /*
345 * Obtain the cpu frequency
346 * Compute the number of ticks for hz.
347 * Compute the delay divisor.
348 * Double the Hz if this CPU runs at twice the
349 * external/cp0-count frequency
350 */
351 curcpu()->ci_cpu_freq = rcp->rc_psb_info.cpu_frequency;
352 curcpu()->ci_cctr_freq = curcpu()->ci_cpu_freq;
353 curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
354 curcpu()->ci_divisor_delay =
355 ((curcpu()->ci_cpu_freq + 500000) / 1000000);
356 if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
357 curcpu()->ci_cpu_freq *= 2;
358
359 /*
360 * Look at arguments passed to us and compute boothowto.
361 * - rmixl firmware gives us a 32 bit argv[i], so adapt
362 * by forcing sign extension in cast to (char *)
363 */
364 boothowto = RB_AUTOBOOT;
365 for (int i = 1; i < argc; i++) {
366 for (char *cp = (char *)(intptr_t)argv[i]; *cp; cp++) {
367 int howto;
368 /* Ignore superfluous '-', if there is one */
369 if (*cp == '-')
370 continue;
371
372 howto = 0;
373 BOOT_FLAG(*cp, howto);
374 if (howto != 0)
375 boothowto |= howto;
376 #ifdef DIAGNOSTIC
377 else
378 printf("bootflag '%c' not recognised\n", *cp);
379 #endif
380 }
381 }
382 #ifdef DIAGNOSTIC
383 printf("boothowto %#x\n", boothowto);
384 #endif
385
386 /*
387 * Reserve pages from the VM system.
388 * to maintain mem_clusters[] as a map of raw ram,
389 * copy into temporary table vm_clusters[]
390 * work on that and use it to feed vm_physload()
391 */
392 KASSERT(sizeof(mem_clusters) == sizeof(vm_clusters));
393 memcpy(&vm_clusters, &mem_clusters, sizeof(vm_clusters));
394 vm_cluster_cnt = mem_cluster_cnt;
395
396 /* reserve 0..start..kernend pages */
397 vm_cluster_cnt = ram_seg_resv(vm_clusters, vm_cluster_cnt,
398 0, round_page(MIPS_KSEG0_TO_PHYS(kernend)));
399
400 /* reserve reset exception vector page */
401 /* should never be in our clusters anyway... */
402 vm_cluster_cnt = ram_seg_resv(vm_clusters, vm_cluster_cnt,
403 0x1FC00000, 0x1FC00000+NBPG);
404
405 #ifdef MULTIPROCEESOR
406 /* reserve the cpu_wakeup_info area */
407 vm_cluster_cnt = ram_seg_resv(vm_clusters, vm_cluster_cnt,
408 (u_quad_t)trunc_page(rcp->rc_cpu_wakeup_info),
409 (u_quad_t)round_page(rcp->rc_cpu_wakeup_end));
410 #endif
411
412 #ifdef MEMLIMIT
413 /* reserve everything > MEMLIMIT */
414 vm_cluster_cnt = ram_seg_resv(vm_clusters, vm_cluster_cnt,
415 (u_quad_t)MEMLIMIT, (u_quad_t)~0);
416 #endif
417
418 /*
419 * Load vm_clusters[] into the VM system.
420 */
421 mips_page_physload(MIPS_KSEG0_START, (vaddr_t) kernend,
422 vm_clusters, vm_cluster_cnt, NULL, 0);
423
424 /*
425 * Initialize error message buffer (at end of core).
426 */
427 mips_init_msgbuf();
428
429 pmap_bootstrap();
430
431 /*
432 * Allocate space for proc0's USPACE.
433 */
434 mips_init_lwp0_uarea();
435
436 /*
437 * Initialize debuggers, and break into them, if appropriate.
438 */
439 #if NKSYMS || defined(DDB) || defined(LKM)
440 ksyms_init(0, 0, 0);
441 #endif
442
443 #if defined(DDB)
444 if (boothowto & RB_KDB)
445 Debugger();
446 #endif
447 #ifdef MULTIPROCESSOR
448 /*
449 * Fix up the exception vector to use COP0 OSSCRATCH0
450 */
451 __asm __volatile("mtc0 %0,$%1"
452 :: "r"(&cpu_info_store), "n"(MIPS_COP_0_OSSCRATCH));
453 mips_fixup_exceptions(rmixl_fixup_cop0_oscratch);
454 #endif
455 }
456
457 #ifdef MULTIPROCESSOR
458 static bool
459 rmixl_fixup_cop0_oscratch(int32_t load_addr, uint32_t new_insns[2])
460 {
461 size_t offset = load_addr - (intptr_t)&cpu_info_store;
462
463 KASSERT(MIPS_KSEG0_P(load_addr));
464 KASSERT(offset < sizeof(struct cpu_info));
465
466 /*
467 * Fixup this direct load cpu_info_store to actually get the current
468 * CPU's cpu_info from COP0 OSSCRATCH0 and then fix the load to be
469 * relative from the start of struct cpu_info.
470 */
471
472 /* [0] = mfc0 rX, $22 (OSScratch) */
473 new_insns[0] = (020 << 26)
474 | (new_insns[0] & 0x001f0000)
475 | (MIPS_COP_0_OSSCRATCH << 11) | (0 << 0);
476
477 /* [1] = l[dw] rX, offset(rX) */
478 new_insns[1] = (new_insns[1] & 0xffff0000) | offset;
479
480 return true;
481 }
482 #endif /* MULTIPROCESSOR */
483
484 /*
485 * ram_seg_resv - cut reserved regions out of segs, fragmenting as needed
486 *
487 * we simply build a new table of segs, then copy it back over the given one
488 * this is inefficient but simple and called only a few times
489 *
490 * note: 'last' here means 1st addr past the end of the segment (start+size)
491 */
492 static u_int
493 ram_seg_resv(phys_ram_seg_t *segs, u_int nsegs,
494 u_quad_t resv_first, u_quad_t resv_last)
495 {
496 u_quad_t first, last;
497 int new_nsegs=0;
498 int resv_flag;
499 phys_ram_seg_t new_segs[VM_PHYSSEG_MAX];
500
501 for (u_int i=0; i < nsegs; i++) {
502 resv_flag = 0;
503 first = trunc_page(segs[i].start);
504 last = round_page(segs[i].start + segs[i].size);
505
506 KASSERT(new_nsegs < VM_PHYSSEG_MAX);
507 if ((resv_first <= first) && (resv_last >= last)) {
508 /* whole segment is resverved */
509 continue;
510 }
511 if ((resv_first > first) && (resv_first < last)) {
512 u_quad_t new_last;
513
514 /*
515 * reserved start in segment
516 * salvage the leading fragment
517 */
518 resv_flag = 1;
519 new_last = last - (last - resv_first);
520 KASSERT (new_last > first);
521 new_segs[new_nsegs].start = first;
522 new_segs[new_nsegs].size = new_last - first;
523 new_nsegs++;
524 }
525 if ((resv_last > first) && (resv_last < last)) {
526 u_quad_t new_first;
527
528 /*
529 * reserved end in segment
530 * salvage the trailing fragment
531 */
532 resv_flag = 1;
533 new_first = first + (resv_last - first);
534 KASSERT (last > (new_first + NBPG));
535 new_segs[new_nsegs].start = new_first;
536 new_segs[new_nsegs].size = last - new_first;
537 new_nsegs++;
538 }
539 if (resv_flag == 0) {
540 /*
541 * nothing reserved here, take it all
542 */
543 new_segs[new_nsegs].start = first;
544 new_segs[new_nsegs].size = last - first;
545 new_nsegs++;
546 }
547
548 }
549
550 memcpy(segs, new_segs, sizeof(new_segs));
551
552 return new_nsegs;
553 }
554
555 /*
556 * create an extent for physical address space
557 * these are in units of MB for sake of compression (for sake of 32 bit kernels)
558 * allocate the regions where we have known functions (DRAM, IO, etc)
559 * what remains can be allocated as needed for other stuff
560 * e.g. to configure BARs that are not already initialized and enabled.
561 */
562 static void
563 rmixl_physaddr_init(void)
564 {
565 struct extent *ext;
566 unsigned long start = 0UL;
567 unsigned long end = (__BIT(40) / (1024 * 1024)) -1;
568 u_long base;
569 u_long size;
570 uint32_t r;
571
572 ext = extent_create("physaddr", start, end, M_DEVBUF,
573 (void *)rmixl_physaddr_storage, sizeof(rmixl_physaddr_storage),
574 EX_NOWAIT | EX_NOCOALESCE);
575
576 if (ext == NULL)
577 panic("%s: extent_create failed", __func__);
578
579 /*
580 * grab regions per DRAM BARs
581 */
582 for (u_int i=0; i < RMIXL_SBC_DRAM_NBARS; i++) {
583 r = RMIXL_IOREG_READ(RMIXL_SBC_DRAM_BAR(i));
584 if ((r & RMIXL_DRAM_BAR_STATUS) == 0)
585 continue; /* not enabled */
586 base = (u_long)(DRAM_BAR_TO_BASE((uint64_t)r) / (1024 * 1024));
587 size = (u_long)(DRAM_BAR_TO_SIZE((uint64_t)r) / (1024 * 1024));
588
589 DPRINTF(("%s: %d: %d: 0x%08x -- 0x%010lx:%lu MB\n",
590 __func__, __LINE__, i, r, base * (1024 * 1024), size));
591 if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
592 panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
593 "failed", __func__, ext, base, size, EX_NOWAIT);
594 }
595
596 /*
597 * grab regions per PCIe CFG, ECFG, IO, MEM BARs
598 */
599 r = RMIXL_IOREG_READ(RMIXL_SBC_PCIE_CFG_BAR);
600 if ((r & RMIXL_PCIE_CFG_BAR_ENB) != 0) {
601 base = (u_long)(RMIXL_PCIE_CFG_BAR_TO_BA((uint64_t)r)
602 / (1024 * 1024));
603 size = (u_long)RMIXL_PCIE_CFG_SIZE / (1024 * 1024);
604 DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
605 __LINE__, "CFG", r, base * 1024 * 1024, size));
606 if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
607 panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
608 "failed", __func__, ext, base, size, EX_NOWAIT);
609 }
610 r = RMIXL_IOREG_READ(RMIXL_SBC_PCIE_ECFG_BAR);
611 if ((r & RMIXL_PCIE_ECFG_BAR_ENB) != 0) {
612 base = (u_long)(RMIXL_PCIE_ECFG_BAR_TO_BA((uint64_t)r)
613 / (1024 * 1024));
614 size = (u_long)RMIXL_PCIE_ECFG_SIZE / (1024 * 1024);
615 DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
616 __LINE__, "ECFG", r, base * 1024 * 1024, size));
617 if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
618 panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
619 "failed", __func__, ext, base, size, EX_NOWAIT);
620 }
621 r = RMIXL_IOREG_READ(RMIXL_SBC_PCIE_MEM_BAR);
622 if ((r & RMIXL_PCIE_MEM_BAR_ENB) != 0) {
623 base = (u_long)(RMIXL_PCIE_MEM_BAR_TO_BA((uint64_t)r)
624 / (1024 * 1024));
625 size = (u_long)(RMIXL_PCIE_MEM_BAR_TO_SIZE((uint64_t)r)
626 / (1024 * 1024));
627 DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
628 __LINE__, "MEM", r, base * 1024 * 1024, size));
629 if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
630 panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
631 "failed", __func__, ext, base, size, EX_NOWAIT);
632 }
633 r = RMIXL_IOREG_READ(RMIXL_SBC_PCIE_IO_BAR);
634 if ((r & RMIXL_PCIE_IO_BAR_ENB) != 0) {
635 base = (u_long)(RMIXL_PCIE_IO_BAR_TO_BA((uint64_t)r)
636 / (1024 * 1024));
637 size = (u_long)(RMIXL_PCIE_IO_BAR_TO_SIZE((uint64_t)r)
638 / (1024 * 1024));
639 DPRINTF(("%s: %d: %s: 0x%08x -- 0x%010lx:%ld MB\n", __func__,
640 __LINE__, "IO", r, base * 1024 * 1024, size));
641 if (extent_alloc_region(ext, base, size, EX_NOWAIT) != 0)
642 panic("%s: extent_alloc_region(%p, %#lx, %#lx, %#x) "
643 "failed", __func__, ext, base, size, EX_NOWAIT);
644 }
645
646 /*
647 * at this point all regions left in "physaddr" extent
648 * are unused holes in the physical adress space
649 * available for use as needed.
650 */
651 rmixl_configuration.rc_phys_ex = ext;
652 #ifdef MACHDEP_DEBUG
653 extent_print(ext);
654 #endif
655 }
656
657 static uint64_t
658 rmixlfw_init(int64_t infop)
659 {
660 struct rmixl_config *rcp = &rmixl_configuration;
661
662 #ifdef MULTIPROCESSOR
663 rmixl_get_wakeup_info(rcp);
664 #endif
665
666 infop |= MIPS_KSEG0_START;
667 rcp->rc_psb_info = *(rmixlfw_info_t *)(intptr_t)infop;
668
669 rcp->rc_psb_type = PSB_TYPE_UNKNOWN;
670 for (int i=0; i < RMICLFW_PSB_VERSIONS_LEN; i++) {
671 if (rmiclfw_psb_id[i].psb_version ==
672 rcp->rc_psb_info.psb_version) {
673 rcp->rc_psb_type = rmiclfw_psb_id[i].psb_type;
674 goto found;
675 }
676 }
677
678 rcp->rc_io_pbase = RMIXL_IO_DEV_PBASE;
679 rmixl_putchar_init(rcp->rc_io_pbase);
680
681 #ifdef DIAGNOSTIC
682 rmixl_puts("\r\nWARNING: untested psb_version: ");
683 rmixl_puthex64(rcp->rc_psb_info.psb_version);
684 rmixl_puts("\r\n");
685 #endif
686
687 #ifdef MEMSIZE
688 /* XXX trust and use MEMSIZE */
689 mem_clusters[0].start = 0;
690 mem_clusters[0].size = MEMSIZE;
691 mem_cluster_cnt = 1;
692 return MEMSIZE;
693 #else
694 rmixl_puts("\r\nERROR: configure MEMSIZE\r\n");
695 cpu_reboot(RB_HALT, NULL);
696 /* NOTREACHED */
697 #endif
698
699 found:
700 rcp->rc_io_pbase = MIPS_KSEG1_TO_PHYS(rcp->rc_psb_info.io_base);
701 rmixl_putchar_init(rcp->rc_io_pbase);
702 #ifdef MACHDEP_DEBUG
703 rmixl_puts("\r\ninfop: ");
704 rmixl_puthex64((uint64_t)(intptr_t)infop);
705 #endif
706 #ifdef DIAGNOSTIC
707 rmixl_puts("\r\nrecognized psb_version=");
708 rmixl_puthex64(rcp->rc_psb_info.psb_version);
709 rmixl_puts(", psb_type=");
710 rmixl_puts(rmixlfw_psb_type_name(rcp->rc_psb_type));
711 rmixl_puts("\r\n");
712 #endif
713
714 return mem_clusters_init(
715 (rmixlfw_mmap_t *)(intptr_t)rcp->rc_psb_info.psb_physaddr_map,
716 (rmixlfw_mmap_t *)(intptr_t)rcp->rc_psb_info.avail_mem_map);
717 }
718
719 void
720 rmixlfw_mmap_print(rmixlfw_mmap_t *map)
721 {
722 #ifdef MACHDEP_DEBUG
723 for (uint32_t i=0; i < map->nmmaps; i++) {
724 rmixl_puthex32(i);
725 rmixl_puts(", ");
726 rmixl_puthex64(map->entry[i].start);
727 rmixl_puts(", ");
728 rmixl_puthex64(map->entry[i].size);
729 rmixl_puts(", ");
730 rmixl_puthex32(map->entry[i].type);
731 rmixl_puts("\r\n");
732 }
733 #endif
734 }
735
736 /*
737 * mem_clusters_init
738 *
739 * initialize mem_clusters[] table based on memory address mapping
740 * provided by boot firmware.
741 *
742 * prefer avail_mem_map if we can, otherwise use psb_physaddr_map.
743 * these will be limited by MEMSIZE if it is configured.
744 * if neither are available, just use MEMSIZE.
745 */
746 static uint64_t
747 mem_clusters_init(
748 rmixlfw_mmap_t *psb_physaddr_map,
749 rmixlfw_mmap_t *avail_mem_map)
750 {
751 rmixlfw_mmap_t *map = NULL;
752 const char *mapname;
753 uint64_t sz;
754 uint64_t sum;
755 u_int cnt;
756 #ifdef MEMSIZE
757 uint64_t memsize = MEMSIZE;
758 #endif
759
760 #ifdef MACHDEP_DEBUG
761 rmixl_puts("psb_physaddr_map: ");
762 rmixl_puthex64((uint64_t)(intptr_t)psb_physaddr_map);
763 rmixl_puts("\r\n");
764 #endif
765 if (psb_physaddr_map != NULL) {
766 map = psb_physaddr_map;
767 mapname = "psb_physaddr_map";
768 rmixlfw_mmap_print(map);
769 }
770 #ifdef DIAGNOSTIC
771 else {
772 rmixl_puts("WARNING: no psb_physaddr_map\r\n");
773 }
774 #endif
775
776 #ifdef MACHDEP_DEBUG
777 rmixl_puts("avail_mem_map: ");
778 rmixl_puthex64((uint64_t)(intptr_t)avail_mem_map);
779 rmixl_puts("\r\n");
780 #endif
781 if (avail_mem_map != NULL) {
782 map = avail_mem_map;
783 mapname = "avail_mem_map";
784 rmixlfw_mmap_print(map);
785 }
786 #ifdef DIAGNOSTIC
787 else {
788 rmixl_puts("WARNING: no avail_mem_map\r\n");
789 }
790 #endif
791
792 if (map == NULL) {
793 #ifndef MEMSIZE
794 rmixl_puts("panic: no firmware memory map, "
795 "must configure MEMSIZE\r\n");
796 for(;;); /* XXX */
797 #else
798 #ifdef DIAGNOSTIC
799 rmixl_puts("WARNING: no avail_mem_map, "
800 "using MEMSIZE\r\n");
801 #endif
802
803 mem_clusters[0].start = 0;
804 mem_clusters[0].size = MEMSIZE;
805 mem_cluster_cnt = 1;
806 return MEMSIZE;
807 #endif /* MEMSIZE */
808 }
809
810 #ifdef DIAGNOSTIC
811 rmixl_puts("using ");
812 rmixl_puts(mapname);
813 rmixl_puts("\r\n");
814 #endif
815 #ifdef MACHDEP_DEBUG
816 rmixl_puts("memory clusters:\r\n");
817 #endif
818 sum = 0;
819 cnt = 0;
820 for (uint32_t i=0; i < map->nmmaps; i++) {
821 if (map->entry[i].type != RMIXLFW_MMAP_TYPE_RAM)
822 continue;
823 mem_clusters[cnt].start = map->entry[i].start;
824 sz = map->entry[i].size;
825 sum += sz;
826 mem_clusters[cnt].size = sz;
827 #ifdef MACHDEP_DEBUG
828 rmixl_puthex32(i);
829 rmixl_puts(": ");
830 rmixl_puthex64(mem_clusters[cnt].start);
831 rmixl_puts(", ");
832 rmixl_puthex64(sz);
833 rmixl_puts(": ");
834 rmixl_puthex64(sum);
835 rmixl_puts("\r\n");
836 #endif
837 #ifdef MEMSIZE
838 /*
839 * configurably limit memsize
840 */
841 if (sum == memsize)
842 break;
843 if (sum > memsize) {
844 uint64_t tmp;
845
846 tmp = sum - memsize;
847 sz -= tmp;
848 sum -= tmp;
849 mem_clusters[cnt].size = sz;
850 cnt++;
851 break;
852 }
853 #endif
854 cnt++;
855 }
856 mem_cluster_cnt = cnt;
857 return sum;
858 }
859
860 #ifdef MULTIPROCESSOR
861 /*
862 * firmware passes wakeup info structure in CP0 OS Scratch reg #7
863 * they do not explicitly give us the size of the wakeup area.
864 * we "know" that firmware loader sets wip->gp thusly:
865 * gp = stack_start[vcpu] = round_page(wakeup_end) + (vcpu * (PAGE_SIZE * 2))
866 * so
867 * round_page(wakeup_end) == gp - (vcpu * (PAGE_SIZE * 2))
868 * Only the "master" cpu runs this function, so
869 * vcpu = wip->master_cpu
870 */
871 void
872 rmixl_get_wakeup_info(struct rmixl_config *rcp)
873 {
874 volatile rmixlfw_cpu_wakeup_info_t *wip;
875 int32_t scratch_7;
876 intptr_t end;
877
878 __asm__ volatile(
879 ".set push" "\n"
880 ".set noreorder" "\n"
881 ".set mips64" "\n"
882 "dmfc0 %0, $22, 7" "\n"
883 ".set pop" "\n"
884 : "=r"(scratch_7));
885
886 wip = (volatile rmixlfw_cpu_wakeup_info_t *)
887 (intptr_t)scratch_7;
888 end = wip->entry.gp - (wip->master_cpu & (PAGE_SIZE * 2));;
889
890 if (wip->valid == 1) {
891 rcp->rc_cpu_wakeup_end = (const void *)end;
892 rcp->rc_cpu_wakeup_info = wip;
893 }
894 };
895
896 #ifdef MACHDEP_DEBUG
897 static void
898 rmixl_wakeup_info_print(volatile rmixlfw_cpu_wakeup_info_t *wip)
899 {
900 int i;
901
902 printf("%s: wip %p, size %lu\n", __func__, wip, sizeof(*wip));
903
904 printf("cpu_status %#x\n", wip->cpu_status);
905 printf("valid: %d\n", wip->valid);
906 printf("entry: addr %#x, args %#x, sp %#"PRIx64", gp %#"PRIx64"\n",
907 wip->entry.addr,
908 wip->entry.args,
909 wip->entry.sp,
910 wip->entry.gp);
911 printf("master_cpu %d\n", wip->master_cpu);
912 printf("master_cpu_mask %#x\n", wip->master_cpu_mask);
913 printf("buddy_cpu_mask %#x\n", wip->buddy_cpu_mask);
914 printf("psb_os_cpu_map %#x\n", wip->psb_os_cpu_map);
915 printf("argc %d\n", wip->argc);
916 printf("argv:");
917 for (i=0; i < wip->argc; i++)
918 printf(" %#x", wip->argv[i]);
919 printf("\n");
920 printf("valid_tlb_entries %d\n", wip->valid_tlb_entries);
921 printf("tlb_map:\n");
922 for (i=0; i < wip->valid_tlb_entries; i++) {
923 volatile const struct lib_cpu_tlb_mapping *m =
924 &wip->tlb_map[i];
925 printf(" %d", m->page_size);
926 printf(", %d", m->asid);
927 printf(", %d", m->coherency);
928 printf(", %d", m->coherency);
929 printf(", %d", m->attr);
930 printf(", %#x", m->virt);
931 printf(", %#"PRIx64"\n", m->phys);
932 }
933 printf("elf segs:\n");
934 for (i=0; i < MAX_ELF_SEGMENTS; i++) {
935 volatile const struct core_segment_info *e =
936 &wip->seg_info[i];
937 printf(" %#"PRIx64"", e->vaddr);
938 printf(", %#"PRIx64"", e->memsz);
939 printf(", %#x\n", e->flags);
940 }
941 printf("envc %d\n", wip->envc);
942 for (i=0; i < wip->envc; i++)
943 printf(" %#x \"%s\"", wip->envs[i],
944 (char *)(intptr_t)(int32_t)(wip->envs[i]));
945 printf("\n");
946 printf("app_mode %d\n", wip->app_mode);
947 printf("printk_lock %#x\n", wip->printk_lock);
948 printf("kseg_master %d\n", wip->kseg_master);
949 printf("kuseg_reentry_function %#x\n", wip->kuseg_reentry_function);
950 printf("kuseg_reentry_args %#x\n", wip->kuseg_reentry_args);
951 printf("app_shared_mem_addr %#"PRIx64"\n", wip->app_shared_mem_addr);
952 printf("app_shared_mem_size %#"PRIx64"\n", wip->app_shared_mem_size);
953 printf("app_shared_mem_orig %#"PRIx64"\n", wip->app_shared_mem_orig);
954 printf("loader_lock %#x\n", wip->loader_lock);
955 printf("global_wakeup_mask %#x\n", wip->global_wakeup_mask);
956 printf("unused_0 %#x\n", wip->unused_0);
957 }
958 #endif /* MACHDEP_DEBUG */
959 #endif /* MULTIPROCESSOR */
960
961 void
962 consinit(void)
963 {
964
965 /*
966 * Everything related to console initialization is done
967 * in mach_init().
968 */
969 }
970
971 /*
972 * Allocate memory for variable-sized tables,
973 */
974 void
975 cpu_startup()
976 {
977 vaddr_t minaddr, maxaddr;
978 char pbuf[9];
979
980 /*
981 * Good {morning,afternoon,evening,night}.
982 */
983 printf("%s%s", copyright, version);
984 format_bytes(pbuf, sizeof(pbuf), ctob((uint64_t)physmem));
985 printf("total memory = %s\n", pbuf);
986
987 /*
988 * Virtual memory is bootstrapped -- notify the bus spaces
989 * that memory allocation is now safe.
990 */
991 rmixl_configuration.rc_mallocsafe = 1;
992
993 minaddr = 0;
994 /*
995 * Allocate a submap for physio.
996 */
997 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
998 VM_PHYS_SIZE, 0, FALSE, NULL);
999
1000 /*
1001 * (No need to allocate an mbuf cluster submap. Mbuf clusters
1002 * are allocated via the pool allocator, and we use XKSEG to
1003 * map those pages.)
1004 */
1005
1006 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
1007 printf("avail memory = %s\n", pbuf);
1008 }
1009
1010 int waittime = -1;
1011
1012 void
1013 cpu_reboot(howto, bootstr)
1014 int howto;
1015 char *bootstr;
1016 {
1017
1018 /* Take a snapshot before clobbering any registers. */
1019 savectx(curlwp->l_addr);
1020
1021 if (cold) {
1022 howto |= RB_HALT;
1023 goto haltsys;
1024 }
1025
1026 /* If "always halt" was specified as a boot flag, obey. */
1027 if (boothowto & RB_HALT)
1028 howto |= RB_HALT;
1029
1030 boothowto = howto;
1031 if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
1032 waittime = 0;
1033 vfs_shutdown();
1034
1035 /*
1036 * If we've been adjusting the clock, the todr
1037 * will be out of synch; adjust it now.
1038 */
1039 resettodr();
1040 }
1041
1042 splhigh();
1043
1044 if (howto & RB_DUMP)
1045 dumpsys();
1046
1047 haltsys:
1048 doshutdownhooks();
1049
1050 if (howto & RB_HALT) {
1051 printf("\n");
1052 printf("The operating system has halted.\n");
1053 printf("Please press any key to reboot.\n\n");
1054 cnpollc(1); /* For proper keyboard command handling */
1055 cngetc();
1056 cnpollc(0);
1057 }
1058
1059 printf("rebooting...\n\n");
1060
1061 rmixl_reset();
1062 }
1063
1064 /*
1065 * goodbye world
1066 */
1067 void __attribute__((__noreturn__))
1068 rmixl_reset(void)
1069 {
1070 uint32_t r;
1071
1072 r = RMIXL_IOREG_READ(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_RESET);
1073 r |= RMIXL_GPIO_RESET_RESET;
1074 RMIXL_IOREG_WRITE(RMIXL_IO_DEV_GPIO + RMIXL_GPIO_RESET, r);
1075
1076 printf("soft reset failed, spinning...\n");
1077 for (;;);
1078 }
1079