1/*	$NetBSD: machdep.c,v 1.2 2011/09/20 05:50:17 macallan 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/*
77 * Copyright (c) 2009, 2010 Miodrag Vallat.
78 *
79 * Permission to use, copy, modify, and distribute this software for any
80 * purpose with or without fee is hereby granted, provided that the above
81 * copyright notice and this permission notice appear in all copies.
82 *
83 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
84 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
85 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
86 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
87 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
88 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
89 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
90 */
91
92#include <sys/cdefs.h>
93__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.2 2011/09/20 05:50:17 macallan Exp $");
94
95#include "opt_ddb.h"
96#include "opt_execfmt.h"
97#include "opt_modular.h"
98
99#define _MIPS_BUS_DMA_PRIVATE
100
101#include <sys/param.h>
102#include <sys/systm.h>
103#include <sys/kernel.h>
104#include <sys/buf.h>
105#include <sys/reboot.h>
106#include <sys/mount.h>
107#include <sys/kcore.h>
108#include <sys/boot_flag.h>
109#include <sys/termios.h>
110#include <sys/ksyms.h>
111#include <sys/device.h>
112
113#include <uvm/uvm_extern.h>
114
115#include <dev/cons.h>
116
117#include "ksyms.h"
118
119#if NKSYMS || defined(DDB) || defined(MODULAR)
120#include <machine/db_machdep.h>
121#include <ddb/db_extern.h>
122#include <sys/exec_elf.h>
123#endif
124
125#include <evbmips/loongson/autoconf.h>
126#include <evbmips/loongson/loongson_intr.h>
127#include <evbmips/loongson/loongson_bus_defs.h>
128#include <machine/cpu.h>
129#include <machine/psl.h>
130
131#include <mips/locore.h>
132
133#include <mips/bonito/bonitoreg.h>
134#include <mips/bonito/bonitovar.h>
135#include <mips/pmon/pmon.h>
136#include <dev/pci/sisfb.h>
137
138#include "com.h"
139#if NCOM > 0
140#include <dev/ic/comreg.h>
141#include <dev/ic/comvar.h>
142
143bus_space_tag_t comconsiot;
144bus_addr_t comconsaddr;
145int comconsrate = 0;
146#endif /* NCOM > 0 */
147
148#ifdef LOW_DEBUG
149#define DPRINTF(x) printf x
150#define DPPRINTF(x) pmon_printf x
151#else
152#define DPRINTF(x)
153#define DPPRINTF(x)
154#endif
155
156#include "sisfb.h"
157
158
159int ex_mallocsafe = 0;
160struct extent *loongson_io_ex = NULL;
161struct extent *loongson_mem_ex = NULL;
162struct mips_bus_space bonito_iot;
163struct mips_bus_space bonito_memt;
164struct mips_bus_dma_tag bonito_dmat;
165struct mips_pci_chipset bonito_pc;
166
167uint loongson_ver;
168
169const struct platform *sys_platform;
170struct bonito_flavour {
171	const char *prefix;
172	const struct platform *platform;
173};
174
175extern const struct platform fuloong_platform;
176extern const struct platform gdium_platform;
177extern const struct platform generic2e_platform;
178extern const struct platform lynloong_platform;
179extern const struct platform yeeloong_platform;
180
181const struct bonito_flavour bonito_flavours[] = {
182	/* Lemote Fuloong 2F mini-PC */
183	{ "LM6002",	&fuloong_platform }, /* dual Ethernet, no prefix */
184	{ "LM6003",	&fuloong_platform },
185	{ "LM6004",	&fuloong_platform },
186	/* EMTEC Gdium Liberty 1000 */
187	{ "Gdium",	&gdium_platform },
188	/* Lemote Yeeloong 8.9" netbook */
189	{ "LM8089",	&yeeloong_platform },
190	/* supposedly Lemote Yeeloong 10.1" netbook, but those found so far
191	   report themselves as LM8089 */
192	{ "LM8101",	&yeeloong_platform },
193	/* Lemote Lynloong all-in-one computer */
194	{ "LM9001",	&lynloong_platform },
195	{ NULL }
196};
197
198/* For sysctl_hw. */
199extern char cpu_model[];
200
201/* Maps for VM objects. */
202struct vm_map *phys_map = NULL;
203
204int	netboot;		/* Are we netbooting? */
205
206phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
207int mem_cluster_cnt;
208
209void	mach_init(int, int32_t, int32_t, int32_t, char *);
210
211static int  pmoncngetc(dev_t);
212static void pmoncnputc(dev_t, int);
213
214struct consdev pmoncons = {
215        NULL,		/* probe */
216	NULL, 		/* init */
217	pmoncngetc, 	/* getc */
218	pmoncnputc,	/* putc */
219	nullcnpollc,	/* poolc */
220	NULL,		/* BELL */
221	makedev(0, 0),
222	CN_DEAD
223};
224
225/*
226 * Do all the stuff that locore normally does before calling main().
227 */
228void
229mach_init(int32_t argc, int32_t argva, int32_t enva, int32_t callvec,
230    char *boot_esym)
231{
232	void *kernend;
233#ifdef NOTYET
234	int howto;
235#endif
236	const char *env;
237	int i;
238	psize_t memlo, memhi;
239	const struct bonito_flavour *f;
240	char *ssym = NULL, *esym = NULL;
241	pcireg_t reg;
242	pcitag_t pcitag;
243
244	extern char edata[], end[];
245
246	/*
247	 * Clear the BSS segment.
248	 */
249	memset(edata, 0, (char *)end - edata);
250
251	pmon_init(argc, argva, enva, callvec);
252	DPPRINTF(("pmon hello\n"));
253
254	cn_tab = &pmoncons;
255
256	DPRINTF(("hello 0x%x %d 0x%x 0x%x %p stack %p\n", pmon_callvec, argc, argva, enva, boot_esym, &i));
257
258	/*
259	 * Reserve space for the symbol table, if it exists.
260	 */
261
262#if NKSYMS || defined(DDB) || defined(MODULAR)
263	/* Attempt to locate ELF header and symbol table after kernel. */
264	if (end[0] == ELFMAG0 && end[1] == ELFMAG1 &&
265	    end[2] == ELFMAG2 && end[3] == ELFMAG3) {
266		/* ELF header exists directly after kernel. */
267		ssym = end;
268		esym = boot_esym;
269		kernend = (void *)mips_round_page(esym);
270	} else {
271		ssym = (char *)(vaddr_t)*(int32_t *)end;
272		if (((long)ssym - (long)end) >= 0 &&
273		    ((long)ssym - (long)end) <= 0x1000 &&
274		    ssym[0] == ELFMAG0 && ssym[1] == ELFMAG1 &&
275		    ssym[2] == ELFMAG2 && ssym[3] == ELFMAG3) {
276			/* Pointers exist directly after kernel. */
277			esym = (char *)(vaddr_t)*((int32_t *)end + 1);
278			kernend = (void *)mips_round_page(esym);
279		} else {
280			/* Pointers aren't setup either... */
281			ssym = NULL;
282			esym = NULL;
283			kernend = (void *)mips_round_page(end);
284		}
285	}
286	DPRINTF(("ssym %p esym %p\n", ssym, esym));
287#endif
288
289	/*
290	 * Set up the exception vectors and CPU-specific function
291	 * vectors early on.  We need the wbflush() vector set up
292	 * before comcnattach() is called (or at least before the
293	 * first printf() after that is called).
294	 * Also clears the I+D caches.
295	 */
296	DPRINTF(("mips_vector_init "));
297	mips_vector_init(NULL, false);
298
299	DPRINTF(("uvm_setpagesize\n"));
300	/* set the VM page size */
301	uvm_setpagesize();
302#if NKSYMS || defined(DDB) || defined(MODULAR)
303	//ksyms_addsyms_elf((vaddr_t)esym - (vaddr_t)ssym, ssym, esym);
304#endif
305
306	/*
307	 * Try and figure out what kind of hardware we are.
308	 */
309
310	env = pmon_getenv("systype");
311	if (env == NULL) {
312		printf("Unable to figure out system type!\n");
313		goto unsupported;
314	}
315	if (strcmp(env, "Bonito") != 0) {
316		printf("This kernel doesn't support system type \"%s\".\n",
317		    env);
318		goto unsupported;
319	}
320
321	/*
322	 * While the kernel supports other processor types than Loongson,
323	 * we are not expecting a Bonito-based system with a different
324	 * processor.  Just to be on the safe side, refuse to run on
325	 * non Loongson2 processors for now.
326	 */
327
328	switch ((mips_options.mips_cpu_id >> 8) & 0xff) {
329	case MIPS_LOONGSON2:
330		switch (mips_options.mips_cpu_id & 0xff) {
331		case 0x00:
332			loongson_ver = 0x2c;
333			break;
334		case 0x02:
335			loongson_ver = 0x2e;
336			break;
337		case 0x03:
338			loongson_ver = 0x2f;
339			break;
340		case 0x05:
341			loongson_ver = 0x3a;
342			break;
343		}
344		if (loongson_ver == 0x2e || loongson_ver == 0x2f)
345			break;
346		/* FALLTHROUGH */
347	default:
348		printf("This kernel doesn't support processor type 0x%x"
349		    ", version %d.%d.\n",
350		    (mips_options.mips_cpu_id >> 8) & 0xff,
351		    (mips_options.mips_cpu_id >> 4) & 0x0f,
352		    mips_options.mips_cpu_id & 0x0f);
353		goto unsupported;
354	}
355
356	/*
357	 * Try to figure out what particular machine we run on, depending
358	 * on the PMON version information.
359	 */
360
361	env = pmon_getenv("Version");
362	if (env == NULL) {
363		/*
364		 * If this is a 2E system, use the generic code and hope
365		 * for the best.
366		 */
367		if (loongson_ver == 0x2e) {
368			sys_platform = &generic2e_platform;
369		} else {
370			printf("Unable to figure out model!\n");
371			goto unsupported;
372		}
373	} else {
374		for (f = bonito_flavours; f->prefix != NULL; f++)
375			if (strncmp(env, f->prefix, strlen(f->prefix)) ==
376			    0) {
377				sys_platform = f->platform;
378				break;
379			}
380
381		if (sys_platform == NULL) {
382			/*
383			 * Early Lemote designs shipped without a model prefix.
384			 * Hopefully these well be close enough to the first
385			 * generation Fuloong 2F design (LM6002); let's warn
386			 * the user and try this if version is 1.2.something
387			 * (1.3 onwards are expected to have a model prefix,
388			 *  and there are currently no reports of 1.1 and
389			 *  below being 2F systems).
390			 *
391			 * Note that this could be handled by adding a
392			 * "1.2." machine type entry to the flavours table,
393			 * but I prefer have it stand out.
394			 * LM6002 users are encouraged to add the system
395			 * model prefix to the `Version' variable.
396			 */
397			if (strncmp(env, "1.2.", 4) == 0) {
398				printf("No model prefix in version"
399				    " string \"%s\".\n"
400				    "Attempting to match as Lemote Fuloong\n",
401				    env);
402				sys_platform = &fuloong_platform;
403			}
404		}
405
406		if (sys_platform == NULL) {
407			printf("This kernel doesn't support model \"%s\"."
408			    "\n", env);
409			goto unsupported;
410		}
411	}
412
413	sprintf(cpu_model, "%s %s", sys_platform->vendor,
414	    sys_platform->product);
415	DPRINTF(("Found %s, setting up.\n", cpu_model));
416
417	/*
418	 * Figure out memory information.
419	 * PMON reports it in two chunks, the memory under the 256MB
420	 * CKSEG limit, and memory above that limit.  We need to do the
421	 * math ourselves.
422	 */
423
424	env = pmon_getenv("memsize");
425	if (env == NULL) {
426		printf("Could not get memory information"
427		    " from the firmware\n");
428		goto unsupported;
429	}
430	memlo = strtoul(env, NULL, 10);	/* size in MB */
431	DPRINTF(("memlo %" PRIdPSIZE, memlo));
432	if (memlo < 0 || memlo > 256) {
433		printf("Incorrect low memory size `%s'\n", env);
434		goto unsupported;
435	}
436
437	/* 3A PMON only reports up to 240MB as low memory */
438	if (memlo >= 240) {
439		env = pmon_getenv("highmemsize");
440		if (env == NULL)
441			memhi = 0;
442		else
443			memhi = strtoul(env, NULL, 10);	/* size in MB */
444		if (memhi < 0 || memhi > (64 * 1024) - 256) {
445			printf("Incorrect high memory size `%s'\n",
446			    env);
447			/* better expose the problem than limit to 256MB */
448			goto unsupported;
449		}
450	} else
451		memhi = 0;
452
453	DPRINTF(("memhi %" PRIdPSIZE "\n", memhi));
454	memlo = memlo * 1024 * 1024;
455	memhi = memhi * 1024 * 1024;
456
457	switch (loongson_ver) {
458	case 0x2e:
459		loongson2e_setup(memlo, memhi,
460		    MIPS_KSEG0_START, (vaddr_t)kernend, &bonito_dmat);
461		break;
462	default:
463	case 0x2f:
464	case 0x3a:
465		loongson2f_setup(memlo, memhi,
466		    MIPS_KSEG0_START, (vaddr_t)kernend, &bonito_dmat);
467		break;
468	}
469
470	DPRINTF(("bonito_pci_init "));
471	bonito_pci_init(&bonito_pc, sys_platform->bonito_config);
472	bonito_pc.pc_intr_v = __UNCONST(sys_platform->bonito_config);
473	bonito_pc.pc_intr_map = loongson_pci_intr_map;
474	bonito_pc.pc_intr_string = loongson_pci_intr_string;
475	bonito_pc.pc_intr_evcnt = loongson_pci_intr_evcnt;
476	bonito_pc.pc_intr_establish = loongson_pci_intr_establish;
477	bonito_pc.pc_intr_disestablish = loongson_pci_intr_disestablish;
478	bonito_pc.pc_conf_interrupt = loongson_pci_conf_interrupt;
479	bonito_pc.pc_pciide_compat_intr_establish =
480	    loongson_pciide_compat_intr_establish;
481	DPRINTF(("bonito_bus_io_init "));
482	bonito_bus_io_init(&bonito_iot, NULL);
483	DPRINTF(("bonito_bus_mem_init\n"));
484	bonito_bus_mem_init(&bonito_memt, NULL);
485
486	bonito_dmat._cookie = __UNCONST(sys_platform);
487	bonito_dmat._dmamap_ops = mips_bus_dmamap_ops;
488	bonito_dmat._dmamem_ops = mips_bus_dmamem_ops;
489	bonito_dmat._dmatag_ops = mips_bus_dmatag_ops;
490
491	DPRINTF(("sys_platform->setup %p\n", sys_platform->setup));
492	if (sys_platform->setup != NULL)
493		(*(sys_platform->setup))();
494
495#if NCOM > 0
496	DPRINTF(("comconsrate %d\n", comconsrate));
497	if (comconsrate > 0) {
498		if (comcnattach(comconsiot, comconsaddr, comconsrate,
499		    COM_FREQ, COM_TYPE_NORMAL,
500		    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
501			panic("unable to initialize serial console");
502	}
503#endif /* NCOM > 0 */
504
505	for (i = 0; i < 32 - 0x11; i++) {
506		pcitag = pci_make_tag(&bonito_pc, 0, i, 0);
507		reg = pci_conf_read(&bonito_pc, pcitag, PCI_CLASS_REG);
508		DPRINTF(("dev %d class 0x%x", i, reg));
509		reg = pci_conf_read(&bonito_pc, pcitag, PCI_ID_REG);
510		DPRINTF((" id 0x%x; ", reg));
511#if NSISFB > 0
512		if (cn_tab == &pmoncons)
513			sisfb_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
514			    pcitag, reg);
515#endif
516		if (cn_tab == &pmoncons)
517			gdium_cnattach(&bonito_memt, &bonito_iot, &bonito_pc,
518			    pcitag, reg);
519	}
520	DPRINTF(("\n"));
521
522	/*
523	 * Get the timer from PMON.
524	 */
525	DPRINTF(("search cpuclock "));
526	env = pmon_getenv("cpuclock");
527	DPRINTF(("got %s ", env));
528	if (env != NULL) {
529		curcpu()->ci_cpu_freq =
530		    strtoul(env, NULL, 10);
531	}
532
533	DPRINTF(("cpuclock %ld\n", curcpu()->ci_cpu_freq));
534
535	if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
536		curcpu()->ci_cpu_freq /= 2;
537
538
539	/* Compute the number of ticks for hz. */
540	curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
541
542	/* Compute the delay divisor. */
543	curcpu()->ci_divisor_delay =
544	    ((curcpu()->ci_cpu_freq + 500000) / 1000000);
545
546	/*
547	 * Get correct cpu frequency if the CPU runs at twice the
548	 * external/cp0-count frequency.
549	 */
550	if (mips_options.mips_cpu_flags & CPU_MIPS_DOUBLE_COUNT)
551		curcpu()->ci_cpu_freq *= 2;
552
553#ifdef DEBUG
554	printf("Timer calibration: %lu cycles/sec\n",
555	    curcpu()->ci_cpu_freq);
556#endif
557
558#if NCOM > 0 && 0
559	/*
560	 * Delay to allow firmware putchars to complete.
561	 * FIFO depth * character time.
562	 * character time = (1000000 / (defaultrate / 10))
563	 */
564	delay(160000000 / comcnrate);
565	if (comcnattach(&gc->gc_iot, MALTA_UART0ADR, comcnrate,
566	    COM_FREQ, COM_TYPE_NORMAL,
567	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
568		panic("malta: unable to initialize serial console");
569#endif /* NCOM > 0 */
570
571	/*
572	 * XXX: check argv[0] - do something if "gdb"???
573	 */
574
575	/*
576	 * Look at arguments passed to us and compute boothowto.
577	 */
578	boothowto = RB_AUTOBOOT;
579#ifdef NOTYET
580	for (i = 1; i < argc; i++) {
581		for (cp = argv[i]; *cp; cp++) {
582			/* Ignore superfluous '-', if there is one */
583			if (*cp == '-')
584				continue;
585
586			howto = 0;
587			BOOT_FLAG(*cp, howto);
588			if (! howto)
589				printf("bootflag '%c' not recognised\n", *cp);
590			else
591				boothowto |= howto;
592		}
593	}
594#endif
595
596	/*
597	 * Load the rest of the available pages into the VM system.
598	 */
599	mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
600	    mem_clusters, mem_cluster_cnt, NULL, 0);
601
602	/*
603	 * Initialize error message buffer (at end of core).
604	 */
605	DPRINTF(("mips_init_msgbuf\n"));
606	mips_init_msgbuf();
607
608	DPRINTF(("pmap_bootstrap\n"));
609	pmap_bootstrap();
610
611	/*
612	 * Allocate uarea page for lwp0 and set it.
613	 */
614	DPRINTF(("curlwp %p ", curlwp));
615	DPRINTF(("curlwp stack %p\n", curlwp->l_addr));
616	mips_init_lwp0_uarea();
617
618	DPRINTF(("curlwp %p ", curlwp));
619	DPRINTF(("curlwp stack %p\n", curlwp->l_addr));
620
621	/*
622	 * Initialize debuggers, and break into them, if appropriate.
623	 */
624#if defined(DDB)
625	if (boothowto & RB_KDB)
626		Debugger();
627#endif
628	DPRINTF(("return\n"));
629	return;
630unsupported:
631	panic("unsupported hardware\n");
632}
633
634void
635consinit(void)
636{
637
638	/*
639	 * Everything related to console initialization is done
640	 * in mach_init().
641	 */
642}
643
644/*
645 * Allocate memory for variable-sized tables,
646 */
647void
648cpu_startup(void)
649{
650	/*
651	 *  Do the common startup items.
652	 */
653	cpu_startup_common();
654
655	/*
656	 * Virtual memory is bootstrapped -- notify the bus spaces
657	 * that memory allocation is now safe.
658	 */
659	ex_mallocsafe = 1;
660
661}
662
663int	waittime = -1;
664
665void
666cpu_reboot(int howto, char *bootstr)
667{
668
669	/* Take a snapshot before clobbering any registers. */
670	savectx(curpcb);
671
672	if (cold) {
673		howto |= RB_HALT;
674		goto haltsys;
675	}
676
677	/* If "always halt" was specified as a boot flag, obey. */
678	if (boothowto & RB_HALT)
679		howto |= RB_HALT;
680
681	boothowto = howto;
682	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
683		waittime = 0;
684		vfs_shutdown();
685
686		/*
687		 * If we've been adjusting the clock, the todr
688		 * will be out of synch; adjust it now.
689		 */
690		resettodr();
691	}
692
693	splhigh();
694
695	if (howto & RB_DUMP)
696		dumpsys();
697
698haltsys:
699	doshutdownhooks();
700
701	pmf_system_shutdown(boothowto);
702
703	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
704		if (sys_platform->powerdown != NULL)
705			sys_platform->powerdown();
706	}
707
708	if (howto & RB_HALT) {
709		printf("\n");
710		printf("The operating system has halted.\n");
711		printf("Please press any key to reboot.\n\n");
712		cnpollc(1);	/* For proper keyboard command handling */
713		cngetc();
714		cnpollc(0);
715	}
716
717	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
718
719	if (sys_platform->reset != NULL)
720		sys_platform->reset();
721
722	__asm__ __volatile__ (
723		"\t.long 0x3c02bfc0\n"
724		"\t.long 0x00400008\n"
725	    ::: "v0");
726}
727
728/*
729 * Early console through pmon routines.
730 */
731
732int
733pmoncngetc(dev_t dev)
734{
735	/*
736	 * PMON does not give us a getc routine.  So try to get a whole line
737	 * and return it char by char, trying not to lose the \n.  Kind
738	 * of ugly but should work.
739	 *
740	 * Note that one could theoretically use pmon_read(STDIN, &c, 1)
741	 * but the value of STDIN within PMON is not a constant and there
742	 * does not seem to be a way of letting us know which value to use.
743	 */
744	static char buf[1 + PMON_MAXLN];
745	static char *bufpos = buf;
746	int c;
747
748	if (*bufpos == '\0') {
749		bufpos = buf;
750		if (pmon_gets(buf) == NULL) {
751			/* either an empty line or EOF. assume the former */
752			return (int)'\n';
753		} else {
754			/* put back the \n sign */
755			buf[strlen(buf)] = '\n';
756		}
757	}
758
759	c = (int)*bufpos++;
760	if (bufpos - buf > PMON_MAXLN) {
761		bufpos = buf;
762		*bufpos = '\0';
763	}
764
765	return c;
766}
767
768void
769pmoncnputc(dev_t dev, int c)
770{
771	if (c == '\n')
772		pmon_printf("\n");
773	else
774		pmon_printf("%c", c);
775}
776