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