Home | History | Annotate | Line # | Download | only in arm32
arm32_machdep.c revision 1.32
      1 /*	$NetBSD: arm32_machdep.c,v 1.32 2003/04/18 22:30:05 thorpej 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 "opt_md.h"
     45 #include "opt_pmap_debug.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/reboot.h>
     50 #include <sys/proc.h>
     51 #include <sys/user.h>
     52 #include <sys/kernel.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/mount.h>
     55 #include <sys/buf.h>
     56 #include <sys/msgbuf.h>
     57 #include <sys/device.h>
     58 #include <uvm/uvm_extern.h>
     59 #include <sys/sysctl.h>
     60 
     61 #include <dev/cons.h>
     62 
     63 #include <arm/arm32/katelib.h>
     64 #include <arm/arm32/machdep.h>
     65 #include <machine/bootconfig.h>
     66 
     67 #include "opt_ipkdb.h"
     68 #include "md.h"
     69 
     70 struct vm_map *exec_map = NULL;
     71 struct vm_map *mb_map = NULL;
     72 struct vm_map *phys_map = NULL;
     73 
     74 extern int physmem;
     75 
     76 #if !defined(PMAP_STATIC_L1S) && !defined(ARM32_PMAP_NEW)
     77 extern int max_processes;
     78 #endif	/* !PMAP_STATIC_L1S */
     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 u_long strtoul			__P((const char *s, char **ptr, int base));
    106 void data_abort_handler		__P((trapframe_t *frame));
    107 void prefetch_abort_handler	__P((trapframe_t *frame));
    108 extern void configure		__P((void));
    109 
    110 /*
    111  * arm32_vector_init:
    112  *
    113  *	Initialize the vector page, and select whether or not to
    114  *	relocate the vectors.
    115  *
    116  *	NOTE: We expect the vector page to be mapped at its expected
    117  *	destination.
    118  */
    119 void
    120 arm32_vector_init(vaddr_t va, int which)
    121 {
    122 	extern unsigned int page0[], page0_data[];
    123 	unsigned int *vectors = (int *) va;
    124 	unsigned int *vectors_data = vectors + (page0_data - page0);
    125 	int vec;
    126 
    127 	/*
    128 	 * Loop through the vectors we're taking over, and copy the
    129 	 * vector's insn and data word.
    130 	 */
    131 	for (vec = 0; vec < ARM_NVEC; vec++) {
    132 		if ((which & (1 << vec)) == 0) {
    133 			/* Don't want to take over this vector. */
    134 			continue;
    135 		}
    136 		vectors[vec] = page0[vec];
    137 		vectors_data[vec] = page0_data[vec];
    138 	}
    139 
    140 	/* Now sync the vectors. */
    141 	cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
    142 
    143 	vector_page = va;
    144 
    145 	if (va == ARM_VECTORS_HIGH) {
    146 		/*
    147 		 * Assume the MD caller knows what it's doing here, and
    148 		 * really does want the vector page relocated.
    149 		 *
    150 		 * Note: This has to be done here (and not just in
    151 		 * cpu_setup()) because the vector page needs to be
    152 		 * accessible *before* cpu_startup() is called.
    153 		 * Think ddb(9) ...
    154 		 *
    155 		 * NOTE: If the CPU control register is not readable,
    156 		 * this will totally fail!  We'll just assume that
    157 		 * any system that has high vector support has a
    158 		 * readable CPU control register, for now.  If we
    159 		 * ever encounter one that does not, we'll have to
    160 		 * rethink this.
    161 		 */
    162 		cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
    163 	}
    164 }
    165 
    166 /*
    167  * Debug function just to park the CPU
    168  */
    169 
    170 void
    171 halt()
    172 {
    173 	while (1)
    174 		cpu_sleep(0);
    175 }
    176 
    177 
    178 /* Sync the discs and unmount the filesystems */
    179 
    180 void
    181 bootsync(void)
    182 {
    183 	static int bootsyncdone = 0;
    184 
    185 	if (bootsyncdone) return;
    186 
    187 	bootsyncdone = 1;
    188 
    189 	/* Make sure we can still manage to do things */
    190 	if (GetCPSR() & I32_bit) {
    191 		/*
    192 		 * If we get here then boot has been called without RB_NOSYNC
    193 		 * and interrupts were disabled. This means the boot() call
    194 		 * did not come from a user process e.g. shutdown, but must
    195 		 * have come from somewhere in the kernel.
    196 		 */
    197 		IRQenable;
    198 		printf("Warning IRQ's disabled during boot()\n");
    199 	}
    200 
    201 	vfs_shutdown();
    202 }
    203 
    204 /*
    205  * void cpu_startup(void)
    206  *
    207  * Machine dependant startup code.
    208  *
    209  */
    210 void
    211 cpu_startup()
    212 {
    213 	paddr_t minaddr;
    214 	paddr_t maxaddr;
    215 	caddr_t sysbase;
    216 	caddr_t size;
    217 	vsize_t bufsize;
    218 	u_int loop, base, residual;
    219 	char pbuf[9];
    220 
    221 #ifndef ARM32_PMAP_NEW
    222 	/* This has moved to initarm() */
    223 	proc0paddr = (struct user *)kernelstack.pv_va;
    224 	lwp0.l_addr = proc0paddr;
    225 #endif
    226 
    227 	/* Set the cpu control register */
    228 	cpu_setup(boot_args);
    229 
    230 #ifndef ARM32_PMAP_NEW
    231 	/* All domains MUST be clients, permissions are VERY important */
    232 	cpu_domains(DOMAIN_CLIENT);
    233 #endif
    234 
    235 	/* Lock down zero page */
    236 	vector_page_setprot(VM_PROT_READ);
    237 
    238 	/*
    239 	 * Give pmap a chance to set up a few more things now the vm
    240 	 * is initialised
    241 	 */
    242 	pmap_postinit();
    243 
    244 	/*
    245 	 * Initialize error message buffer (at end of core).
    246 	 */
    247 
    248 	/* msgbufphys was setup during the secondary boot strap */
    249 	for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
    250 		pmap_kenter_pa((vaddr_t)msgbufaddr + loop * PAGE_SIZE,
    251 		    msgbufphys + loop * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
    252 	pmap_update(pmap_kernel());
    253 	initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
    254 
    255 	/*
    256 	 * Identify ourselves for the msgbuf (everything printed earlier will
    257 	 * not be buffered).
    258 	 */
    259 	printf(version);
    260 
    261 	format_bytes(pbuf, sizeof(pbuf), arm_ptob(physmem));
    262 	printf("total memory = %s\n", pbuf);
    263 
    264 	/*
    265 	 * Find out how much space we need, allocate it,
    266 	 * and then give everything true virtual addresses.
    267 	 */
    268 	size = allocsys(NULL, NULL);
    269 	sysbase = (caddr_t)uvm_km_zalloc(kernel_map, round_page((vaddr_t)size));
    270 	if (sysbase == 0)
    271 		panic(
    272 		    "cpu_startup: no room for system tables; %d bytes required",
    273 		    (u_int)size);
    274 	if ((caddr_t)((allocsys(sysbase, NULL) - sysbase)) != size)
    275 		panic("cpu_startup: system table size inconsistency");
    276 
    277    	/*
    278 	 * Now allocate buffers proper.  They are different than the above
    279 	 * in that they usually occupy more virtual memory than physical.
    280 	 */
    281 	bufsize = MAXBSIZE * nbuf;
    282 	if (uvm_map(kernel_map, (vaddr_t *)&buffers, round_page(bufsize),
    283 	    NULL, UVM_UNKNOWN_OFFSET, 0,
    284 	    UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
    285 	    UVM_ADV_NORMAL, 0)) != 0)
    286 		panic("cpu_startup: cannot allocate UVM space for buffers");
    287 	minaddr = (vaddr_t)buffers;
    288 	if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
    289 		/* don't want to alloc more physical mem than needed */
    290 		bufpages = btoc(MAXBSIZE) * nbuf;
    291 	}
    292 
    293 	base = bufpages / nbuf;
    294 	residual = bufpages % nbuf;
    295 	for (loop = 0; loop < nbuf; ++loop) {
    296 		vsize_t curbufsize;
    297 		vaddr_t curbuf;
    298 		struct vm_page *pg;
    299 
    300 		/*
    301 		 * Each buffer has MAXBSIZE bytes of VM space allocated.  Of
    302 		 * that MAXBSIZE space, we allocate and map (base+1) pages
    303 		 * for the first "residual" buffers, and then we allocate
    304 		 * "base" pages for the rest.
    305 		 */
    306 		curbuf = (vaddr_t) buffers + (loop * MAXBSIZE);
    307 		curbufsize = PAGE_SIZE * ((loop < residual) ? (base+1) : base);
    308 
    309 		while (curbufsize) {
    310 			pg = uvm_pagealloc(NULL, 0, NULL, 0);
    311 			if (pg == NULL)
    312 				panic("cpu_startup: not enough memory for buffer cache");
    313 			pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
    314 				VM_PROT_READ|VM_PROT_WRITE);
    315 			curbuf += PAGE_SIZE;
    316 			curbufsize -= PAGE_SIZE;
    317 		}
    318 	}
    319 	pmap_update(pmap_kernel());
    320 
    321 	/*
    322 	 * Allocate a submap for exec arguments.  This map effectively
    323 	 * limits the number of processes exec'ing at any time.
    324 	 */
    325 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    326 				   16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
    327 
    328 	/*
    329 	 * Allocate a submap for physio
    330 	 */
    331 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    332 				   VM_PHYS_SIZE, 0, FALSE, NULL);
    333 
    334 	/*
    335 	 * Finally, allocate mbuf cluster submap.
    336 	 */
    337 	mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    338 				 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
    339 				 FALSE, NULL);
    340 
    341 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    342 	printf("avail memory = %s\n", pbuf);
    343 	format_bytes(pbuf, sizeof(pbuf), bufpages * PAGE_SIZE);
    344 	printf("using %u buffers containing %s of memory\n", nbuf, pbuf);
    345 
    346 	/*
    347 	 * Set up buffers, so they can be used to read disk labels.
    348 	 */
    349 	bufinit();
    350 
    351 	curpcb = &lwp0.l_addr->u_pcb;
    352 	curpcb->pcb_flags = 0;
    353 	curpcb->pcb_un.un_32.pcb32_und_sp = (u_int)lwp0.l_addr +
    354 	    USPACE_UNDEF_STACK_TOP;
    355 	curpcb->pcb_un.un_32.pcb32_sp = (u_int)lwp0.l_addr +
    356 	    USPACE_SVC_STACK_TOP;
    357 #ifndef ARM32_PMAP_NEW
    358 	(void) pmap_extract(pmap_kernel(), (vaddr_t)(pmap_kernel())->pm_pdir,
    359 	    &curpcb->pcb_pagedir);
    360 #else
    361 	pmap_set_pcb_pagedir(pmap_kernel(), curpcb);
    362 #endif
    363 
    364         curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
    365 }
    366 
    367 /*
    368  * machine dependent system variables.
    369  */
    370 
    371 int
    372 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    373 	int *name;
    374 	u_int namelen;
    375 	void *oldp;
    376 	size_t *oldlenp;
    377 	void *newp;
    378 	size_t newlen;
    379 	struct proc *p;
    380 {
    381 	/* all sysctl names at this level are terminal */
    382 	if (namelen != 1)
    383 		return (ENOTDIR);		/* overloaded */
    384 
    385 	switch (name[0]) {
    386 	case CPU_DEBUG:
    387 		return(sysctl_int(oldp, oldlenp, newp, newlen, &kernel_debug));
    388 
    389 	case CPU_BOOTED_DEVICE:
    390 		if (booted_device != NULL)
    391 			return (sysctl_rdstring(oldp, oldlenp, newp,
    392 			    booted_device->dv_xname));
    393 		return (EOPNOTSUPP);
    394 
    395 	case CPU_CONSDEV: {
    396 		dev_t consdev;
    397 		if (cn_tab != NULL)
    398 			consdev = cn_tab->cn_dev;
    399 		else
    400 			consdev = NODEV;
    401 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
    402 			sizeof consdev));
    403 	}
    404 	case CPU_BOOTED_KERNEL: {
    405 		if (booted_kernel != NULL && booted_kernel[0] != '\0')
    406 			return sysctl_rdstring(oldp, oldlenp, newp,
    407 			    booted_kernel);
    408 		return (EOPNOTSUPP);
    409 	}
    410 	case CPU_POWERSAVE: {
    411 		int error, newval;
    412 
    413 		newval = cpu_do_powersave;
    414 
    415 		if (cpufuncs.cf_sleep == (void *) cpufunc_nullop)
    416 			error = sysctl_rdint(oldp, oldlenp, newp, newval);
    417 		else
    418 			error = sysctl_int(oldp, oldlenp, newp, newlen,
    419 			    &newval);
    420 		if (error || newval == cpu_do_powersave)
    421 			return (error);
    422 
    423 		if (newval < 0 || newval > 1)
    424 			return (EINVAL);
    425 
    426 		cpu_do_powersave = newval;
    427 		return (0);
    428 	}
    429 
    430 	default:
    431 		return (EOPNOTSUPP);
    432 	}
    433 	/* NOTREACHED */
    434 }
    435 
    436 void
    437 parse_mi_bootargs(args)
    438 	char *args;
    439 {
    440 	int integer;
    441 
    442 	if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
    443 	    || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
    444 		if (integer)
    445 			boothowto |= RB_SINGLE;
    446 	if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
    447 	    || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
    448 		if (integer)
    449 			boothowto |= RB_KDB;
    450 	if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
    451 	    || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
    452 		if (integer)
    453 			boothowto |= RB_ASKNAME;
    454 
    455 #ifdef PMAP_DEBUG
    456 	if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
    457 		pmap_debug_level = integer;
    458 		pmap_debug(pmap_debug_level);
    459 	}
    460 #endif	/* PMAP_DEBUG */
    461 
    462 /*	if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
    463 		bufpages = integer;*/
    464 
    465 #if !defined(PMAP_STATIC_L1S) && !defined(ARM32_PMAP_NEW)
    466 	if (get_bootconf_option(args, "maxproc", BOOTOPT_TYPE_INT, &integer)) {
    467 		max_processes = integer;
    468 		if (max_processes < 16)
    469 			max_processes = 16;
    470 		/* Limit is PDSIZE * (max_processes + 1) <= 4MB */
    471 		if (max_processes > 255)
    472 			max_processes = 255;
    473 	}
    474 #endif	/* !PMAP_STATUC_L1S && !ARM32_PMAP_NEW */
    475 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
    476 	if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
    477 	    || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
    478 		md_root_size = integer;
    479 		md_root_size *= 1024;
    480 		if (md_root_size < 32*1024)
    481 			md_root_size = 32*1024;
    482 		if (md_root_size > 2048*1024)
    483 			md_root_size = 2048*1024;
    484 	}
    485 #endif	/* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
    486 
    487 	if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
    488 	    || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
    489 		if (integer)
    490 			boothowto |= AB_QUIET;
    491 	if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
    492 	    || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
    493 		if (integer)
    494 			boothowto |= AB_VERBOSE;
    495 }
    496