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