Home | History | Annotate | Line # | Download | only in arm32
arm32_machdep.c revision 1.30
      1 /*	$NetBSD: arm32_machdep.c,v 1.30 2003/04/18 10:51:35 scw 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 #ifndef PMAP_STATIC_L1S
     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 		cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
    156 	}
    157 }
    158 
    159 /*
    160  * Debug function just to park the CPU
    161  */
    162 
    163 void
    164 halt()
    165 {
    166 	while (1)
    167 		cpu_sleep(0);
    168 }
    169 
    170 
    171 /* Sync the discs and unmount the filesystems */
    172 
    173 void
    174 bootsync(void)
    175 {
    176 	static int bootsyncdone = 0;
    177 
    178 	if (bootsyncdone) return;
    179 
    180 	bootsyncdone = 1;
    181 
    182 	/* Make sure we can still manage to do things */
    183 	if (GetCPSR() & I32_bit) {
    184 		/*
    185 		 * If we get here then boot has been called without RB_NOSYNC
    186 		 * and interrupts were disabled. This means the boot() call
    187 		 * did not come from a user process e.g. shutdown, but must
    188 		 * have come from somewhere in the kernel.
    189 		 */
    190 		IRQenable;
    191 		printf("Warning IRQ's disabled during boot()\n");
    192 	}
    193 
    194 	vfs_shutdown();
    195 }
    196 
    197 /*
    198  * void cpu_startup(void)
    199  *
    200  * Machine dependant startup code.
    201  *
    202  */
    203 void
    204 cpu_startup()
    205 {
    206 	paddr_t minaddr;
    207 	paddr_t maxaddr;
    208 	caddr_t sysbase;
    209 	caddr_t size;
    210 	vsize_t bufsize;
    211 	u_int loop, base, residual;
    212 	char pbuf[9];
    213 
    214 	proc0paddr = (struct user *)kernelstack.pv_va;
    215 	lwp0.l_addr = proc0paddr;
    216 
    217 	/* Set the cpu control register */
    218 	cpu_setup(boot_args);
    219 
    220 	/* All domains MUST be clients, permissions are VERY important */
    221 	cpu_domains(DOMAIN_CLIENT);
    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, (vaddr_t *)&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 	(void) pmap_extract(pmap_kernel(), (vaddr_t)(pmap_kernel())->pm_pdir,
    346 	    &curpcb->pcb_pagedir);
    347 
    348         curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
    349 }
    350 
    351 /*
    352  * machine dependent system variables.
    353  */
    354 
    355 int
    356 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    357 	int *name;
    358 	u_int namelen;
    359 	void *oldp;
    360 	size_t *oldlenp;
    361 	void *newp;
    362 	size_t newlen;
    363 	struct proc *p;
    364 {
    365 	/* all sysctl names at this level are terminal */
    366 	if (namelen != 1)
    367 		return (ENOTDIR);		/* overloaded */
    368 
    369 	switch (name[0]) {
    370 	case CPU_DEBUG:
    371 		return(sysctl_int(oldp, oldlenp, newp, newlen, &kernel_debug));
    372 
    373 	case CPU_BOOTED_DEVICE:
    374 		if (booted_device != NULL)
    375 			return (sysctl_rdstring(oldp, oldlenp, newp,
    376 			    booted_device->dv_xname));
    377 		return (EOPNOTSUPP);
    378 
    379 	case CPU_CONSDEV: {
    380 		dev_t consdev;
    381 		if (cn_tab != NULL)
    382 			consdev = cn_tab->cn_dev;
    383 		else
    384 			consdev = NODEV;
    385 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
    386 			sizeof consdev));
    387 	}
    388 	case CPU_BOOTED_KERNEL: {
    389 		if (booted_kernel != NULL && booted_kernel[0] != '\0')
    390 			return sysctl_rdstring(oldp, oldlenp, newp,
    391 			    booted_kernel);
    392 		return (EOPNOTSUPP);
    393 	}
    394 	case CPU_POWERSAVE: {
    395 		int error, newval;
    396 
    397 		newval = cpu_do_powersave;
    398 
    399 		if (cpufuncs.cf_sleep == (void *) cpufunc_nullop)
    400 			error = sysctl_rdint(oldp, oldlenp, newp, newval);
    401 		else
    402 			error = sysctl_int(oldp, oldlenp, newp, newlen,
    403 			    &newval);
    404 		if (error || newval == cpu_do_powersave)
    405 			return (error);
    406 
    407 		if (newval < 0 || newval > 1)
    408 			return (EINVAL);
    409 
    410 		cpu_do_powersave = newval;
    411 		return (0);
    412 	}
    413 
    414 	default:
    415 		return (EOPNOTSUPP);
    416 	}
    417 	/* NOTREACHED */
    418 }
    419 
    420 void
    421 parse_mi_bootargs(args)
    422 	char *args;
    423 {
    424 	int integer;
    425 
    426 	if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
    427 	    || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
    428 		if (integer)
    429 			boothowto |= RB_SINGLE;
    430 	if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
    431 	    || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
    432 		if (integer)
    433 			boothowto |= RB_KDB;
    434 	if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
    435 	    || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
    436 		if (integer)
    437 			boothowto |= RB_ASKNAME;
    438 
    439 #ifdef PMAP_DEBUG
    440 	if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
    441 		pmap_debug_level = integer;
    442 		pmap_debug(pmap_debug_level);
    443 	}
    444 #endif	/* PMAP_DEBUG */
    445 
    446 /*	if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
    447 		bufpages = integer;*/
    448 
    449 #ifndef PMAP_STATIC_L1S
    450 	if (get_bootconf_option(args, "maxproc", BOOTOPT_TYPE_INT, &integer)) {
    451 		max_processes = integer;
    452 		if (max_processes < 16)
    453 			max_processes = 16;
    454 		/* Limit is PDSIZE * (max_processes + 1) <= 4MB */
    455 		if (max_processes > 255)
    456 			max_processes = 255;
    457 	}
    458 #endif	/* !PMAP_STATUC_L1S */
    459 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
    460 	if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
    461 	    || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
    462 		md_root_size = integer;
    463 		md_root_size *= 1024;
    464 		if (md_root_size < 32*1024)
    465 			md_root_size = 32*1024;
    466 		if (md_root_size > 2048*1024)
    467 			md_root_size = 2048*1024;
    468 	}
    469 #endif	/* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
    470 
    471 	if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
    472 	    || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
    473 		if (integer)
    474 			boothowto |= AB_QUIET;
    475 	if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
    476 	    || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
    477 		if (integer)
    478 			boothowto |= AB_VERBOSE;
    479 }
    480