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