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