Home | History | Annotate | Line # | Download | only in alchemy
machdep.c revision 1.13
      1 /* $NetBSD: machdep.c,v 1.13 2003/10/27 23:47:00 simonb Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * the Systems Programming Group of the University of Utah Computer
      9  * Science Department, The Mach Operating System project at
     10  * Carnegie-Mellon University and Ralph Campbell.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
     37  * 	from: Utah Hdr: machdep.c 1.63 91/04/24
     38  */
     39 /*
     40  * Copyright (c) 1988 University of Utah.
     41  *
     42  * This code is derived from software contributed to Berkeley by
     43  * the Systems Programming Group of the University of Utah Computer
     44  * Science Department, The Mach Operating System project at
     45  * Carnegie-Mellon University and Ralph Campbell.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. All advertising materials mentioning features or use of this software
     56  *    must display the following acknowledgement:
     57  *	This product includes software developed by the University of
     58  *	California, Berkeley and its contributors.
     59  * 4. Neither the name of the University nor the names of its contributors
     60  *    may be used to endorse or promote products derived from this software
     61  *    without specific prior written permission.
     62  *
     63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73  * SUCH DAMAGE.
     74  *
     75  *	@(#)machdep.c	8.3 (Berkeley) 1/12/94
     76  * 	from: Utah Hdr: machdep.c 1.63 91/04/24
     77  */
     78 
     79 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     80 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.13 2003/10/27 23:47:00 simonb Exp $");
     81 
     82 #include "opt_ddb.h"
     83 #include "opt_kgdb.h"
     84 
     85 #include "opt_memsize.h"
     86 #include "opt_ethaddr.h"
     87 
     88 #include <sys/param.h>
     89 #include <sys/systm.h>
     90 #include <sys/kernel.h>
     91 #include <sys/buf.h>
     92 #include <sys/reboot.h>
     93 #include <sys/user.h>
     94 #include <sys/mount.h>
     95 #include <sys/kcore.h>
     96 #include <sys/boot_flag.h>
     97 #include <sys/termios.h>
     98 #include <sys/ksyms.h>
     99 
    100 #include <net/if.h>
    101 #include <net/if_ether.h>
    102 
    103 #include <uvm/uvm_extern.h>
    104 
    105 #include <dev/cons.h>
    106 
    107 #include "ksyms.h"
    108 
    109 #if NKSYMS || defined(DDB) || defined(LKM)
    110 #include <machine/db_machdep.h>
    111 #include <ddb/db_extern.h>
    112 #endif
    113 
    114 #include <mips/cache.h>
    115 #include <mips/locore.h>
    116 #include <machine/yamon.h>
    117 
    118 #include <evbmips/alchemy/pb1000var.h>
    119 #include <mips/alchemy/include/aureg.h>
    120 #include <mips/alchemy/include/auvar.h>
    121 #include <mips/alchemy/include/aubusvar.h>
    122 
    123 #include "aucom.h"
    124 #if NAUCOM > 0
    125 #include <mips/alchemy/dev/aucomvar.h>
    126 
    127 #ifndef CONSPEED
    128 #define CONSPEED TTYDEF_SPEED
    129 #endif
    130 int	aucomcnrate = CONSPEED;
    131 #endif /* NAUCOM > 0 */
    132 
    133 #include "ohci.h"
    134 
    135 /* The following are used externally (sysctl_hw). */
    136 extern char	cpu_model[];
    137 
    138 struct	user *proc0paddr;
    139 
    140 /* Our exported CPU info; we can have only one. */
    141 struct cpu_info cpu_info_store;
    142 
    143 /* Maps for VM objects. */
    144 struct vm_map *exec_map = NULL;
    145 struct vm_map *mb_map = NULL;
    146 struct vm_map *phys_map = NULL;
    147 
    148 int physmem;		/* # pages of physical memory */
    149 int maxmem;			/* max memory per process */
    150 
    151 int mem_cluster_cnt;
    152 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
    153 
    154 yamon_env_var *yamon_envp;
    155 struct pb1000_config pb1000_configuration;
    156 
    157 void	mach_init(int, char **, yamon_env_var *, u_long); /* XXX */
    158 
    159 void
    160 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
    161 {
    162 	struct pb1000_config *pbc = &pb1000_configuration;
    163 	bus_space_handle_t sh;
    164 	caddr_t kernend;
    165 	const char *cp;
    166 	u_long first, last;
    167 	caddr_t v;
    168 	int freqok, howto, i;
    169 
    170 	extern char edata[], end[];	/* XXX */
    171 
    172 	/* clear the BSS segment */
    173 	kernend = (caddr_t)mips_round_page(end);
    174 	memset(edata, 0, kernend - (caddr_t)edata);
    175 
    176 	/* set cpu model info for sysctl_hw */
    177 	strcpy(cpu_model, "Alchemy Semiconductor Pb1000");
    178 
    179 	/* save the yamon environment pointer */
    180 	yamon_envp = envp;
    181 
    182 	/* Use YAMON callbacks for early console I/O */
    183 	cn_tab = &yamon_promcd;
    184 
    185 	/*
    186 	 * Set up the exception vectors and cpu-specific function
    187 	 * vectors early on.  We need the wbflush() vector set up
    188 	 * before comcnattach() is called (or at least before the
    189 	 * first printf() after that is called).
    190 	 * Sets up mips_cpu_flags that may be queried by other
    191 	 * functions called during startup.
    192 	 * Also clears the I+D caches.
    193 	 */
    194 	mips_vector_init();
    195 
    196 	/*
    197 	 * Set the VM page size.
    198 	 */
    199 	uvm_setpagesize();
    200 
    201 	/*
    202 	 * Use YAMON's CPU frequency if available.
    203 	 */
    204 	freqok = yamon_setcpufreq(1);
    205 
    206 	/*
    207 	 * Initialize bus space tags.
    208 	 */
    209 	au_cpureg_bus_mem_init(&pbc->pc_cpuregt, pbc);
    210 	aubus_st = &pbc->pc_cpuregt;		/* XXX: for aubus.c */
    211 
    212 	/*
    213 	 * Calibrate the timer if YAMON failed to tell us.
    214 	 */
    215 	if (!freqok) {
    216 		bus_space_map(&pbc->pc_cpuregt, PC_BASE, PC_SIZE, 0, &sh);
    217 		au_cal_timers(&pbc->pc_cpuregt, sh);
    218 		bus_space_unmap(&pbc->pc_cpuregt, sh, PC_SIZE);
    219 	}
    220 
    221 	/*
    222 	 * Bring up the console.
    223 	 */
    224 #if NAUCOM > 0
    225 	/*
    226 	 * Delay to allow firmware putchars to complete.
    227 	 * FIFO depth * character time.
    228 	 * character time = (1000000 / (defaultrate / 10))
    229 	 */
    230 	delay(160000000 / aucomcnrate);
    231 	if (aucomcnattach(&pbc->pc_cpuregt, UART0_BASE, aucomcnrate,
    232 	    curcpu()->ci_cpu_freq / 4, COM_TYPE_NORMAL,
    233 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
    234 		panic("pb1000: unable to initialize serial console");
    235 #else
    236 	panic("pb1000: not configured to use serial console");
    237 #endif /* NAUCOM > 0 */
    238 
    239 	/*
    240 	 * Look at arguments passed to us and compute boothowto.
    241 	 */
    242 	boothowto = RB_AUTOBOOT;
    243 #ifdef KADB
    244 	boothowto |= RB_KDB;
    245 #endif
    246 	for (i = 1; i < argc; i++) {
    247 		for (cp = argv[i]; *cp; cp++) {
    248 			/* Ignore superfluous '-', if there is one */
    249 			if (*cp == '-')
    250 				continue;
    251 
    252 			howto = 0;
    253 			BOOT_FLAG(*cp, howto);
    254 			if (! howto)
    255 				printf("bootflag '%c' not recognised\n", *cp);
    256 			else
    257 				boothowto |= howto;
    258 		}
    259 	}
    260 
    261 	/*
    262 	 * Determine the memory size.  Use the `memsize' PMON
    263 	 * variable.  If that's not available, panic.
    264 	 *
    265 	 * Note: Reserve the first page!  That's where the trap
    266 	 * vectors are located.
    267 	 */
    268 
    269 #if defined(MEMSIZE)
    270 	memsize = MEMSIZE;
    271 #else
    272 	if (memsize == 0) {
    273 		if ((cp = yamon_getenv("memsize")) != NULL)
    274 			memsize = strtoul(cp, NULL, 0);
    275 		else {
    276 			printf("FATAL: `memsize' YAMON variable not set.  Set it to\n");
    277 			printf("       the amount of memory (in MB) and try again.\n");
    278 			printf("       Or, build a kernel with the `MEMSIZE' "
    279 			    "option.\n");
    280 			panic("pb1000_init");
    281 		}
    282 	}
    283 #endif /* MEMSIZE */
    284 	printf("Memory size: 0x%08lx\n", memsize);
    285 	physmem = btoc(memsize);
    286 
    287 	mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
    288 	mem_clusters[mem_cluster_cnt].size =
    289 	    memsize - mem_clusters[mem_cluster_cnt].start;
    290 	mem_cluster_cnt++;
    291 
    292 	/*
    293 	 * Load the rest of the available pages into the VM system.
    294 	 */
    295 	first = round_page(MIPS_KSEG0_TO_PHYS(kernend));
    296 	last = mem_clusters[0].start + mem_clusters[0].size;
    297 	uvm_page_physload(atop(first), atop(last), atop(first), atop(last),
    298 	    VM_FREELIST_DEFAULT);
    299 
    300 	/*
    301 	 * Initialize message buffer (at end of core).
    302 	 */
    303 	mips_init_msgbuf();
    304 
    305 	/*
    306 	 * Compute the size of system data structures.  pmap_bootstrap()
    307 	 * needs some of this information.
    308 	 */
    309 	memsize = (u_long)allocsys(NULL, NULL);
    310 
    311 	/*
    312 	 * Initialize the virtual memory system.
    313 	 */
    314 	pmap_bootstrap();
    315 
    316 	/*
    317 	 * Init mapping for u page(s) for proc0.
    318 	 */
    319 	v = (caddr_t) uvm_pageboot_alloc(USPACE);
    320 	lwp0.l_addr = proc0paddr = (struct user *)v;
    321 	lwp0.l_md.md_regs = (struct frame *)(v + USPACE) - 1;
    322 	curpcb = &lwp0.l_addr->u_pcb;
    323 	curpcb->pcb_context[11] = MIPS_INT_MASK | MIPS_SR_INT_IE; /* SR */
    324 
    325 	/*
    326 	 * Allocate space for system data structures.  These data structures
    327 	 * are allocated here instead of cpu_startup() because physical
    328 	 * memory is directly addressable.  We don't have to map these into
    329 	 * the virtual address space.
    330 	 */
    331 	v = (caddr_t)uvm_pageboot_alloc(memsize);
    332 	if ((allocsys(v, NULL) - v) != memsize)
    333 		panic("mach_init: table size inconsistency");
    334 
    335 #if NOHCI > 0
    336 	{
    337 #define	USBH_ALL   (0x1f<<10)  /* All relevant bits in USBH portion of SYS_CLKSRC */
    338 		/*
    339 		 * Assign a clock for the USB Host controller.
    340 		 */
    341 		volatile u_int32_t *scsreg;
    342 		u_int32_t	tmp;
    343 
    344 		scsreg = (volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1(SYS_CLKSRC));
    345 #if 0
    346 		auxpll = (volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1(SYS_AUXPLL));
    347 		*auxpll = 8;		/* 96Mhz */
    348 		tmp = *sfc0;
    349 		tmp |= SFC_FE0|SFC_FRDIV0;
    350 		*sfc0 = tmp;
    351 #endif
    352 		tmp = *scsreg;
    353 		tmp &= ~USBH_ALL;	/* clear all USBH bits in SYS_CLKSRC first */
    354 		tmp |= (SCS_DUH|SCS_CUH|SCS_MUH(SCS_MEx_AUX));	/* 48Mhz */
    355 		*scsreg = tmp;
    356 	}
    357 #endif   /* NOHCI */
    358 	/*
    359 	 * Initialize debuggers, and break into them, if appropriate.
    360 	 */
    361 #if NKSYMS || defined(DDB) || defined(LKM)
    362 	ksyms_init(0, 0, 0);
    363 #endif
    364 #ifdef DDB
    365 	if (boothowto & RB_KDB)
    366 		Debugger();
    367 #endif
    368 }
    369 
    370 void
    371 consinit(void)
    372 {
    373 
    374 	/*
    375 	 * Everything related to console initialization is done
    376 	 * in mach_init().
    377 	 */
    378 }
    379 
    380 void
    381 cpu_startup(void)
    382 {
    383 	char pbuf[9];
    384 	vaddr_t minaddr, maxaddr;
    385 	vsize_t size;
    386 	u_int i, base, residual;
    387 #ifdef DEBUG
    388 	extern int pmapdebug;		/* XXX */
    389 	int opmapdebug = pmapdebug;
    390 
    391 	pmapdebug = 0;		/* Shut up pmap debug during bootstrap */
    392 #endif
    393 
    394 	/*
    395 	 * Good {morning,afternoon,evening,night}.
    396 	 */
    397 	printf(version);
    398 	printf("%s\n", cpu_model);
    399 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    400 	printf("total memory = %s\n", pbuf);
    401 
    402 	/*
    403 	 * Allocate virtual address space for file I/O buffers.
    404 	 * Note they are different than the array of headers, 'buf',
    405 	 * and usually occupy more virtual memory than physical.
    406 	 */
    407 	size = MAXBSIZE * nbuf;
    408 	if (uvm_map(kernel_map, (vaddr_t *)(void *)&buffers, round_page(size),
    409 		    NULL, UVM_UNKNOWN_OFFSET, 0,
    410 		    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
    411 				UVM_ADV_NORMAL, 0)) != 0)
    412 		panic("cpu_startup: cannot allocate VM for buffers");
    413 
    414 	minaddr = (vaddr_t)buffers;
    415 	if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
    416 		bufpages = btoc(MAXBSIZE) * nbuf; /* do not overallocate RAM */
    417 	}
    418 	base = bufpages / nbuf;
    419 	residual = bufpages % nbuf;
    420 
    421 	/* now allocate RAM for buffers */
    422 	for (i = 0; i < nbuf; i++) {
    423 		vsize_t curbufsize;
    424 		vaddr_t curbuf;
    425 		struct vm_page *pg;
    426 
    427 		/*
    428 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
    429 		 * that MAXBSIZE space, we allocate and map (base+1) pages
    430 		 * for the first "residual" buffers, and then we allocate
    431 		 * "base" pages for the rest.
    432 		 */
    433 		curbuf = (vaddr_t)buffers + (i * MAXBSIZE);
    434 		curbufsize = PAGE_SIZE * ((i < residual) ? (base+1) : base);
    435 
    436 		while (curbufsize) {
    437 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
    438 			if (pg == NULL)
    439 				panic("cpu_startup: not enough memory for "
    440 				    "buffer cache");
    441 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
    442 			    VM_PROT_READ|VM_PROT_WRITE);
    443 			curbuf += PAGE_SIZE;
    444 			curbufsize -= PAGE_SIZE;
    445 		}
    446 	}
    447 	pmap_update(pmap_kernel());
    448 
    449 	/*
    450 	 * Allocate a submap for exec arguments.  This map effectively
    451 	 * limits the number of processes exec'ing at any time.
    452 	 */
    453 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    454 	    16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    455 
    456 	/*
    457 	 * Allocate a submap for physio
    458 	 */
    459 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    460 	    VM_PHYS_SIZE, 0, FALSE, NULL);
    461 
    462 	/*
    463 	 * No need to allocate an mbuf cluster submap.  Mbuf clusters
    464 	 * are allocated via the pool allocator, and we use KSEG to
    465 	 * map those pages.
    466 	 */
    467 
    468 #ifdef DEBUG
    469 	pmapdebug = opmapdebug;
    470 #endif
    471 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    472 	printf("avail memory = %s\n", pbuf);
    473 	format_bytes(pbuf, sizeof(pbuf), bufpages * PAGE_SIZE);
    474 	printf("using %u buffers containing %s of memory\n", nbuf, pbuf);
    475 
    476 	/*
    477 	 * Set up buffers, so they can be used to read disklabels.
    478 	 */
    479 	bufinit();
    480 }
    481 
    482 void
    483 cpu_reboot(int howto, char *bootstr)
    484 {
    485 	static int waittime = -1;
    486 
    487 	/* Take a snapshot before clobbering any registers. */
    488 	if (curproc)
    489 		savectx((struct user *)curpcb);
    490 
    491 	/* If "always halt" was specified as a boot flag, obey. */
    492 	if (boothowto & RB_HALT)
    493 		howto |= RB_HALT;
    494 
    495 	boothowto = howto;
    496 
    497 	/* If system is cold, just halt. */
    498 	if (cold) {
    499 		boothowto |= RB_HALT;
    500 		goto haltsys;
    501 	}
    502 
    503 	if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
    504 		waittime = 0;
    505 
    506 		/*
    507 		 * Synchronize the disks....
    508 		 */
    509 		vfs_shutdown();
    510 
    511 		/*
    512 		 * If we've been adjusting the clock, the todr
    513 		 * will be out of synch; adjust it now.
    514 		 */
    515 		resettodr();
    516 	}
    517 
    518 	/* Disable interrupts. */
    519 	splhigh();
    520 
    521 	if (boothowto & RB_DUMP)
    522 		dumpsys();
    523 
    524  haltsys:
    525 	/* Run any shutdown hooks. */
    526 	doshutdownhooks();
    527 
    528 #if 1
    529 	/* XXX
    530 	 * For some reason we are leaving the ethernet MAC in a state where
    531 	 * YAMON isn't happy with it.  So just call the reset vector (grr,
    532 	 * Alchemy YAMON doesn't have a "reset" command).
    533 	 */
    534 	printf("reseting board...\n\n");
    535 	mips_icache_sync_all();
    536 	mips_dcache_wbinv_all();
    537 	asm volatile("jr	%0" :: "r"(MIPS_RESET_EXC_VEC));
    538 #else
    539 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
    540 	yamon_exit(boothowto);
    541 	printf("Oops, back from yamon_exit()\n\nSpinning...");
    542 #endif
    543 	for (;;)
    544 		/* spin forever */ ;	/* XXX */
    545 	/*NOTREACHED*/
    546 }
    547