Home | History | Annotate | Line # | Download | only in arm32
arm32_machdep.c revision 1.38
      1 /*	$NetBSD: arm32_machdep.c,v 1.38 2003/09/21 00:26:09 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994-1998 Mark Brinicombe.
      5  * Copyright (c) 1994 Brini.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software written for Brini by Mark Brinicombe
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by Mark Brinicombe
     21  *	for the NetBSD Project.
     22  * 4. The name of the company nor the name of the author may be used to
     23  *    endorse or promote products derived from this software without specific
     24  *    prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     30  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  * Machine dependant functions for kernel setup
     39  *
     40  * Created      : 17/09/94
     41  * Updated	: 18/04/01 updated for new wscons
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: arm32_machdep.c,v 1.38 2003/09/21 00:26:09 matt Exp $");
     46 
     47 #include "opt_md.h"
     48 #include "opt_pmap_debug.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/reboot.h>
     53 #include <sys/proc.h>
     54 #include <sys/user.h>
     55 #include <sys/kernel.h>
     56 #include <sys/mbuf.h>
     57 #include <sys/mount.h>
     58 #include <sys/buf.h>
     59 #include <sys/msgbuf.h>
     60 #include <sys/device.h>
     61 #include <uvm/uvm_extern.h>
     62 #include <sys/sysctl.h>
     63 
     64 #include <dev/cons.h>
     65 
     66 #include <arm/arm32/katelib.h>
     67 #include <arm/arm32/machdep.h>
     68 #include <machine/bootconfig.h>
     69 
     70 #include "opt_ipkdb.h"
     71 #include "md.h"
     72 
     73 struct vm_map *exec_map = NULL;
     74 struct vm_map *mb_map = NULL;
     75 struct vm_map *phys_map = NULL;
     76 
     77 extern int physmem;
     78 
     79 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
     80 extern size_t md_root_size;		/* Memory disc size */
     81 #endif	/* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
     82 
     83 pv_addr_t kernelstack;
     84 
     85 /* the following is used externally (sysctl_hw) */
     86 char	machine[] = MACHINE;		/* from <machine/param.h> */
     87 char	machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
     88 
     89 /* Our exported CPU info; we can have only one. */
     90 struct cpu_info cpu_info_store;
     91 
     92 caddr_t	msgbufaddr;
     93 extern paddr_t msgbufphys;
     94 
     95 int kernel_debug = 0;
     96 
     97 struct user *proc0paddr;
     98 
     99 /* exported variable to be filled in by the bootloaders */
    100 char *booted_kernel;
    101 
    102 
    103 /* Prototypes */
    104 
    105 void data_abort_handler		__P((trapframe_t *frame));
    106 void prefetch_abort_handler	__P((trapframe_t *frame));
    107 extern void configure		__P((void));
    108 
    109 /*
    110  * arm32_vector_init:
    111  *
    112  *	Initialize the vector page, and select whether or not to
    113  *	relocate the vectors.
    114  *
    115  *	NOTE: We expect the vector page to be mapped at its expected
    116  *	destination.
    117  */
    118 void
    119 arm32_vector_init(vaddr_t va, int which)
    120 {
    121 	extern unsigned int page0[], page0_data[];
    122 	unsigned int *vectors = (int *) va;
    123 	unsigned int *vectors_data = vectors + (page0_data - page0);
    124 	int vec;
    125 
    126 	/*
    127 	 * Loop through the vectors we're taking over, and copy the
    128 	 * vector's insn and data word.
    129 	 */
    130 	for (vec = 0; vec < ARM_NVEC; vec++) {
    131 		if ((which & (1 << vec)) == 0) {
    132 			/* Don't want to take over this vector. */
    133 			continue;
    134 		}
    135 		vectors[vec] = page0[vec];
    136 		vectors_data[vec] = page0_data[vec];
    137 	}
    138 
    139 	/* Now sync the vectors. */
    140 	cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
    141 
    142 	vector_page = va;
    143 
    144 	if (va == ARM_VECTORS_HIGH) {
    145 		/*
    146 		 * Assume the MD caller knows what it's doing here, and
    147 		 * really does want the vector page relocated.
    148 		 *
    149 		 * Note: This has to be done here (and not just in
    150 		 * cpu_setup()) because the vector page needs to be
    151 		 * accessible *before* cpu_startup() is called.
    152 		 * Think ddb(9) ...
    153 		 *
    154 		 * NOTE: If the CPU control register is not readable,
    155 		 * this will totally fail!  We'll just assume that
    156 		 * any system that has high vector support has a
    157 		 * readable CPU control register, for now.  If we
    158 		 * ever encounter one that does not, we'll have to
    159 		 * rethink this.
    160 		 */
    161 		cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
    162 	}
    163 }
    164 
    165 /*
    166  * Debug function just to park the CPU
    167  */
    168 
    169 void
    170 halt()
    171 {
    172 	while (1)
    173 		cpu_sleep(0);
    174 }
    175 
    176 
    177 /* Sync the discs and unmount the filesystems */
    178 
    179 void
    180 bootsync(void)
    181 {
    182 	static int bootsyncdone = 0;
    183 
    184 	if (bootsyncdone) return;
    185 
    186 	bootsyncdone = 1;
    187 
    188 	/* Make sure we can still manage to do things */
    189 	if (GetCPSR() & I32_bit) {
    190 		/*
    191 		 * If we get here then boot has been called without RB_NOSYNC
    192 		 * and interrupts were disabled. This means the boot() call
    193 		 * did not come from a user process e.g. shutdown, but must
    194 		 * have come from somewhere in the kernel.
    195 		 */
    196 		IRQenable;
    197 		printf("Warning IRQ's disabled during boot()\n");
    198 	}
    199 
    200 	vfs_shutdown();
    201 }
    202 
    203 /*
    204  * void cpu_startup(void)
    205  *
    206  * Machine dependant startup code.
    207  *
    208  */
    209 void
    210 cpu_startup()
    211 {
    212 	paddr_t minaddr;
    213 	paddr_t maxaddr;
    214 	caddr_t sysbase;
    215 	caddr_t size;
    216 	vsize_t bufsize;
    217 	u_int loop, base, residual;
    218 	char pbuf[9];
    219 
    220 	/* Set the cpu control register */
    221 	cpu_setup(boot_args);
    222 
    223 	/* Lock down zero page */
    224 	vector_page_setprot(VM_PROT_READ);
    225 
    226 	/*
    227 	 * Give pmap a chance to set up a few more things now the vm
    228 	 * is initialised
    229 	 */
    230 	pmap_postinit();
    231 
    232 	/*
    233 	 * Initialize error message buffer (at end of core).
    234 	 */
    235 
    236 	/* msgbufphys was setup during the secondary boot strap */
    237 	for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
    238 		pmap_kenter_pa((vaddr_t)msgbufaddr + loop * PAGE_SIZE,
    239 		    msgbufphys + loop * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
    240 	pmap_update(pmap_kernel());
    241 	initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
    242 
    243 	/*
    244 	 * Identify ourselves for the msgbuf (everything printed earlier will
    245 	 * not be buffered).
    246 	 */
    247 	printf(version);
    248 
    249 	format_bytes(pbuf, sizeof(pbuf), arm_ptob(physmem));
    250 	printf("total memory = %s\n", pbuf);
    251 
    252 	/*
    253 	 * Find out how much space we need, allocate it,
    254 	 * and then give everything true virtual addresses.
    255 	 */
    256 	size = allocsys(NULL, NULL);
    257 	sysbase = (caddr_t)uvm_km_zalloc(kernel_map, round_page((vaddr_t)size));
    258 	if (sysbase == 0)
    259 		panic(
    260 		    "cpu_startup: no room for system tables; %d bytes required",
    261 		    (u_int)size);
    262 	if ((caddr_t)((allocsys(sysbase, NULL) - sysbase)) != size)
    263 		panic("cpu_startup: system table size inconsistency");
    264 
    265    	/*
    266 	 * Now allocate buffers proper.  They are different than the above
    267 	 * in that they usually occupy more virtual memory than physical.
    268 	 */
    269 	bufsize = MAXBSIZE * nbuf;
    270 	if (uvm_map(kernel_map, (void *)&buffers, round_page(bufsize),
    271 	    NULL, UVM_UNKNOWN_OFFSET, 0,
    272 	    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
    273 	    UVM_ADV_NORMAL, 0)) != 0)
    274 		panic("cpu_startup: cannot allocate UVM space for buffers");
    275 	minaddr = (vaddr_t)buffers;
    276 	if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
    277 		/* don't want to alloc more physical mem than needed */
    278 		bufpages = btoc(MAXBSIZE) * nbuf;
    279 	}
    280 
    281 	base = bufpages / nbuf;
    282 	residual = bufpages % nbuf;
    283 	for (loop = 0; loop < nbuf; ++loop) {
    284 		vsize_t curbufsize;
    285 		vaddr_t curbuf;
    286 		struct vm_page *pg;
    287 
    288 		/*
    289 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
    290 		 * that MAXBSIZE space, we allocate and map (base+1) pages
    291 		 * for the first "residual" buffers, and then we allocate
    292 		 * "base" pages for the rest.
    293 		 */
    294 		curbuf = (vaddr_t) buffers + (loop * MAXBSIZE);
    295 		curbufsize = PAGE_SIZE * ((loop < residual) ? (base+1) : base);
    296 
    297 		while (curbufsize) {
    298 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
    299 			if (pg == NULL)
    300 				panic("cpu_startup: not enough memory for buffer cache");
    301 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
    302 				VM_PROT_READ|VM_PROT_WRITE);
    303 			curbuf += PAGE_SIZE;
    304 			curbufsize -= PAGE_SIZE;
    305 		}
    306 	}
    307 	pmap_update(pmap_kernel());
    308 
    309 	/*
    310 	 * Allocate a submap for exec arguments.  This map effectively
    311 	 * limits the number of processes exec'ing at any time.
    312 	 */
    313 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    314 				   16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    315 
    316 	/*
    317 	 * Allocate a submap for physio
    318 	 */
    319 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    320 				   VM_PHYS_SIZE, 0, FALSE, NULL);
    321 
    322 	/*
    323 	 * Finally, allocate mbuf cluster submap.
    324 	 */
    325 	mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    326 				 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
    327 				 FALSE, NULL);
    328 
    329 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    330 	printf("avail memory = %s\n", pbuf);
    331 	format_bytes(pbuf, sizeof(pbuf), bufpages * PAGE_SIZE);
    332 	printf("using %u buffers containing %s of memory\n", nbuf, pbuf);
    333 
    334 	/*
    335 	 * Set up buffers, so they can be used to read disk labels.
    336 	 */
    337 	bufinit();
    338 
    339 	curpcb = &lwp0.l_addr->u_pcb;
    340 	curpcb->pcb_flags = 0;
    341 	curpcb->pcb_un.un_32.pcb32_und_sp = (u_int)lwp0.l_addr +
    342 	    USPACE_UNDEF_STACK_TOP;
    343 	curpcb->pcb_un.un_32.pcb32_sp = (u_int)lwp0.l_addr +
    344 	    USPACE_SVC_STACK_TOP;
    345 	pmap_set_pcb_pagedir(pmap_kernel(), curpcb);
    346 
    347         curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
    348 }
    349 
    350 /*
    351  * machine dependent system variables.
    352  */
    353 
    354 int
    355 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    356 	int *name;
    357 	u_int namelen;
    358 	void *oldp;
    359 	size_t *oldlenp;
    360 	void *newp;
    361 	size_t newlen;
    362 	struct proc *p;
    363 {
    364 	/* all sysctl names at this level are terminal */
    365 	if (namelen != 1)
    366 		return (ENOTDIR);		/* overloaded */
    367 
    368 	switch (name[0]) {
    369 	case CPU_DEBUG:
    370 		return(sysctl_int(oldp, oldlenp, newp, newlen, &kernel_debug));
    371 
    372 	case CPU_BOOTED_DEVICE:
    373 		if (booted_device != NULL)
    374 			return (sysctl_rdstring(oldp, oldlenp, newp,
    375 			    booted_device->dv_xname));
    376 		return (EOPNOTSUPP);
    377 
    378 	case CPU_CONSDEV: {
    379 		dev_t consdev;
    380 		if (cn_tab != NULL)
    381 			consdev = cn_tab->cn_dev;
    382 		else
    383 			consdev = NODEV;
    384 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
    385 			sizeof consdev));
    386 	}
    387 	case CPU_BOOTED_KERNEL: {
    388 		if (booted_kernel != NULL && booted_kernel[0] != '\0')
    389 			return sysctl_rdstring(oldp, oldlenp, newp,
    390 			    booted_kernel);
    391 		return (EOPNOTSUPP);
    392 	}
    393 	case CPU_POWERSAVE: {
    394 		int error, newval;
    395 
    396 		newval = cpu_do_powersave;
    397 
    398 		if (cpufuncs.cf_sleep == (void *) cpufunc_nullop)
    399 			error = sysctl_rdint(oldp, oldlenp, newp, newval);
    400 		else
    401 			error = sysctl_int(oldp, oldlenp, newp, newlen,
    402 			    &newval);
    403 		if (error || newval == cpu_do_powersave)
    404 			return (error);
    405 
    406 		if (newval < 0 || newval > 1)
    407 			return (EINVAL);
    408 
    409 		cpu_do_powersave = newval;
    410 		return (0);
    411 	}
    412 
    413 	default:
    414 		return (EOPNOTSUPP);
    415 	}
    416 	/* NOTREACHED */
    417 }
    418 
    419 void
    420 parse_mi_bootargs(args)
    421 	char *args;
    422 {
    423 	int integer;
    424 
    425 	if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
    426 	    || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
    427 		if (integer)
    428 			boothowto |= RB_SINGLE;
    429 	if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
    430 	    || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
    431 		if (integer)
    432 			boothowto |= RB_KDB;
    433 	if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
    434 	    || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
    435 		if (integer)
    436 			boothowto |= RB_ASKNAME;
    437 
    438 #ifdef PMAP_DEBUG
    439 	if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
    440 		pmap_debug_level = integer;
    441 		pmap_debug(pmap_debug_level);
    442 	}
    443 #endif	/* PMAP_DEBUG */
    444 
    445 /*	if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
    446 		bufpages = integer;*/
    447 
    448 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
    449 	if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
    450 	    || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
    451 		md_root_size = integer;
    452 		md_root_size *= 1024;
    453 		if (md_root_size < 32*1024)
    454 			md_root_size = 32*1024;
    455 		if (md_root_size > 2048*1024)
    456 			md_root_size = 2048*1024;
    457 	}
    458 #endif	/* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
    459 
    460 	if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
    461 	    || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
    462 		if (integer)
    463 			boothowto |= AB_QUIET;
    464 	if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
    465 	    || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
    466 		if (integer)
    467 			boothowto |= AB_VERBOSE;
    468 }
    469