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