Home | History | Annotate | Line # | Download | only in arm32
arm32_machdep.c revision 1.77
      1 /*	$NetBSD: arm32_machdep.c,v 1.77 2012/07/16 06:26:43 skrll 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 dependent 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.77 2012/07/16 06:26:43 skrll Exp $");
     46 
     47 #include "opt_modular.h"
     48 #include "opt_md.h"
     49 #include "opt_pmap_debug.h"
     50 
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/reboot.h>
     54 #include <sys/proc.h>
     55 #include <sys/kauth.h>
     56 #include <sys/kernel.h>
     57 #include <sys/mbuf.h>
     58 #include <sys/mount.h>
     59 #include <sys/buf.h>
     60 #include <sys/msgbuf.h>
     61 #include <sys/device.h>
     62 #include <sys/sysctl.h>
     63 #include <sys/cpu.h>
     64 #include <sys/module.h>
     65 
     66 #include <uvm/uvm_extern.h>
     67 
     68 #include <dev/cons.h>
     69 #include <dev/mm.h>
     70 
     71 #include <arm/arm32/katelib.h>
     72 #include <arm/arm32/machdep.h>
     73 #include <machine/bootconfig.h>
     74 
     75 struct vm_map *phys_map = NULL;
     76 
     77 #if defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
     78 extern size_t md_root_size;		/* Memory disc size */
     79 #endif	/* MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
     80 
     81 pv_addr_t kernelstack;
     82 
     83 void *	msgbufaddr;
     84 extern paddr_t msgbufphys;
     85 
     86 int kernel_debug = 0;
     87 
     88 /* exported variable to be filled in by the bootloaders */
     89 char *booted_kernel;
     90 
     91 
     92 /* Prototypes */
     93 
     94 void data_abort_handler(trapframe_t *frame);
     95 void prefetch_abort_handler(trapframe_t *frame);
     96 extern void configure(void);
     97 
     98 /*
     99  * arm32_vector_init:
    100  *
    101  *	Initialize the vector page, and select whether or not to
    102  *	relocate the vectors.
    103  *
    104  *	NOTE: We expect the vector page to be mapped at its expected
    105  *	destination.
    106  */
    107 void
    108 arm32_vector_init(vaddr_t va, int which)
    109 {
    110 	extern unsigned int page0[], page0_data[];
    111 	unsigned int *vectors = (int *) va;
    112 	unsigned int *vectors_data = vectors + (page0_data - page0);
    113 	int vec;
    114 
    115 	/*
    116 	 * Loop through the vectors we're taking over, and copy the
    117 	 * vector's insn and data word.
    118 	 */
    119 	for (vec = 0; vec < ARM_NVEC; vec++) {
    120 		if ((which & (1 << vec)) == 0) {
    121 			/* Don't want to take over this vector. */
    122 			continue;
    123 		}
    124 		vectors[vec] = page0[vec];
    125 		vectors_data[vec] = page0_data[vec];
    126 	}
    127 
    128 	/* Now sync the vectors. */
    129 	cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
    130 
    131 	vector_page = va;
    132 
    133 	if (va == ARM_VECTORS_HIGH) {
    134 		/*
    135 		 * Assume the MD caller knows what it's doing here, and
    136 		 * really does want the vector page relocated.
    137 		 *
    138 		 * Note: This has to be done here (and not just in
    139 		 * cpu_setup()) because the vector page needs to be
    140 		 * accessible *before* cpu_startup() is called.
    141 		 * Think ddb(9) ...
    142 		 *
    143 		 * NOTE: If the CPU control register is not readable,
    144 		 * this will totally fail!  We'll just assume that
    145 		 * any system that has high vector support has a
    146 		 * readable CPU control register, for now.  If we
    147 		 * ever encounter one that does not, we'll have to
    148 		 * rethink this.
    149 		 */
    150 		cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
    151 	}
    152 }
    153 
    154 /*
    155  * Debug function just to park the CPU
    156  */
    157 
    158 void
    159 halt(void)
    160 {
    161 	while (1)
    162 		cpu_sleep(0);
    163 }
    164 
    165 
    166 /* Sync the discs and unmount the filesystems */
    167 
    168 void
    169 bootsync(void)
    170 {
    171 	static bool bootsyncdone = false;
    172 
    173 	if (bootsyncdone) return;
    174 
    175 	bootsyncdone = true;
    176 
    177 	/* Make sure we can still manage to do things */
    178 	if (GetCPSR() & I32_bit) {
    179 		/*
    180 		 * If we get here then boot has been called without RB_NOSYNC
    181 		 * and interrupts were disabled. This means the boot() call
    182 		 * did not come from a user process e.g. shutdown, but must
    183 		 * have come from somewhere in the kernel.
    184 		 */
    185 		IRQenable;
    186 		printf("Warning IRQ's disabled during boot()\n");
    187 	}
    188 
    189 	vfs_shutdown();
    190 }
    191 
    192 /*
    193  * void cpu_startup(void)
    194  *
    195  * Machine dependent startup code.
    196  *
    197  */
    198 void
    199 cpu_startup(void)
    200 {
    201 	vaddr_t minaddr;
    202 	vaddr_t maxaddr;
    203 	u_int loop;
    204 	char pbuf[9];
    205 
    206 	/* Set the CPU control register */
    207 	cpu_setup(boot_args);
    208 
    209 	/* Lock down zero page */
    210 	vector_page_setprot(VM_PROT_READ);
    211 
    212 	/*
    213 	 * Give pmap a chance to set up a few more things now the vm
    214 	 * is initialised
    215 	 */
    216 	pmap_postinit();
    217 
    218 	/*
    219 	 * Initialize error message buffer (at end of core).
    220 	 */
    221 
    222 	/* msgbufphys was setup during the secondary boot strap */
    223 	for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
    224 		pmap_kenter_pa((vaddr_t)msgbufaddr + loop * PAGE_SIZE,
    225 		    msgbufphys + loop * PAGE_SIZE,
    226 		    VM_PROT_READ|VM_PROT_WRITE, 0);
    227 	pmap_update(pmap_kernel());
    228 	initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
    229 
    230 	/*
    231 	 * Identify ourselves for the msgbuf (everything printed earlier will
    232 	 * not be buffered).
    233 	 */
    234 	printf("%s%s", copyright, version);
    235 
    236 	format_bytes(pbuf, sizeof(pbuf), arm_ptob(physmem));
    237 	printf("total memory = %s\n", pbuf);
    238 
    239 	minaddr = 0;
    240 
    241 	/*
    242 	 * Allocate a submap for physio
    243 	 */
    244 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    245 				   VM_PHYS_SIZE, 0, false, NULL);
    246 
    247 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
    248 	printf("avail memory = %s\n", pbuf);
    249 
    250 	curpcb = lwp_getpcb(&lwp0);
    251 	curpcb->pcb_flags = 0;
    252 	curpcb->pcb_un.un_32.pcb32_sp =
    253 	    uvm_lwp_getuarea(&lwp0) + USPACE_SVC_STACK_TOP;
    254 	curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
    255 }
    256 
    257 /*
    258  * machine dependent system variables.
    259  */
    260 static int
    261 sysctl_machdep_booted_device(SYSCTLFN_ARGS)
    262 {
    263 	struct sysctlnode node;
    264 
    265 	if (booted_device == NULL)
    266 		return (EOPNOTSUPP);
    267 
    268 	node = *rnode;
    269 	node.sysctl_data = booted_device->dv_xname;
    270 	node.sysctl_size = strlen(booted_device->dv_xname) + 1;
    271 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    272 }
    273 
    274 static int
    275 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS)
    276 {
    277 	struct sysctlnode node;
    278 
    279 	if (booted_kernel == NULL || booted_kernel[0] == '\0')
    280 		return (EOPNOTSUPP);
    281 
    282 	node = *rnode;
    283 	node.sysctl_data = booted_kernel;
    284 	node.sysctl_size = strlen(booted_kernel) + 1;
    285 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    286 }
    287 
    288 static int
    289 sysctl_machdep_powersave(SYSCTLFN_ARGS)
    290 {
    291 	struct sysctlnode node = *rnode;
    292 	int error, newval;
    293 
    294 	newval = cpu_do_powersave;
    295 	node.sysctl_data = &newval;
    296 	if (cpufuncs.cf_sleep == (void *) cpufunc_nullop)
    297 		node.sysctl_flags &= ~CTLFLAG_READWRITE;
    298 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    299 	if (error || newp == NULL || newval == cpu_do_powersave)
    300 		return (error);
    301 
    302 	if (newval < 0 || newval > 1)
    303 		return (EINVAL);
    304 	cpu_do_powersave = newval;
    305 
    306 	return (0);
    307 }
    308 
    309 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
    310 {
    311 
    312 	sysctl_createv(clog, 0, NULL, NULL,
    313 		       CTLFLAG_PERMANENT,
    314 		       CTLTYPE_NODE, "machdep", NULL,
    315 		       NULL, 0, NULL, 0,
    316 		       CTL_MACHDEP, CTL_EOL);
    317 
    318 	sysctl_createv(clog, 0, NULL, NULL,
    319 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    320 		       CTLTYPE_INT, "debug", NULL,
    321 		       NULL, 0, &kernel_debug, 0,
    322 		       CTL_MACHDEP, CPU_DEBUG, CTL_EOL);
    323 	sysctl_createv(clog, 0, NULL, NULL,
    324 		       CTLFLAG_PERMANENT,
    325 		       CTLTYPE_STRING, "booted_device", NULL,
    326 		       sysctl_machdep_booted_device, 0, NULL, 0,
    327 		       CTL_MACHDEP, CPU_BOOTED_DEVICE, CTL_EOL);
    328 	sysctl_createv(clog, 0, NULL, NULL,
    329 		       CTLFLAG_PERMANENT,
    330 		       CTLTYPE_STRING, "booted_kernel", NULL,
    331 		       sysctl_machdep_booted_kernel, 0, NULL, 0,
    332 		       CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
    333 	sysctl_createv(clog, 0, NULL, NULL,
    334 		       CTLFLAG_PERMANENT,
    335 		       CTLTYPE_STRUCT, "console_device", NULL,
    336 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
    337 		       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
    338 	sysctl_createv(clog, 0, NULL, NULL,
    339 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    340 		       CTLTYPE_INT, "powersave", NULL,
    341 		       sysctl_machdep_powersave, 0, &cpu_do_powersave, 0,
    342 		       CTL_MACHDEP, CPU_POWERSAVE, CTL_EOL);
    343 }
    344 
    345 void
    346 parse_mi_bootargs(char *args)
    347 {
    348 	int integer;
    349 
    350 	if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
    351 	    || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
    352 		if (integer)
    353 			boothowto |= RB_SINGLE;
    354 	if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
    355 	    || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
    356 		if (integer)
    357 			boothowto |= RB_KDB;
    358 	if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
    359 	    || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
    360 		if (integer)
    361 			boothowto |= RB_ASKNAME;
    362 
    363 #ifdef PMAP_DEBUG
    364 	if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
    365 		pmap_debug_level = integer;
    366 		pmap_debug(pmap_debug_level);
    367 	}
    368 #endif	/* PMAP_DEBUG */
    369 
    370 /*	if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
    371 		bufpages = integer;*/
    372 
    373 #if defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
    374 	if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
    375 	    || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
    376 		md_root_size = integer;
    377 		md_root_size *= 1024;
    378 		if (md_root_size < 32*1024)
    379 			md_root_size = 32*1024;
    380 		if (md_root_size > 2048*1024)
    381 			md_root_size = 2048*1024;
    382 	}
    383 #endif	/* MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
    384 
    385 	if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
    386 	    || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
    387 		if (integer)
    388 			boothowto |= AB_QUIET;
    389 	if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
    390 	    || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
    391 		if (integer)
    392 			boothowto |= AB_VERBOSE;
    393 }
    394 
    395 #ifdef __HAVE_FAST_SOFTINTS
    396 #if IPL_SOFTSERIAL != IPL_SOFTNET + 1
    397 #error IPLs are screwed up
    398 #elif IPL_SOFTNET != IPL_SOFTBIO + 1
    399 #error IPLs are screwed up
    400 #elif IPL_SOFTBIO != IPL_SOFTCLOCK + 1
    401 #error IPLs are screwed up
    402 #elif !(IPL_SOFTCLOCK > IPL_NONE)
    403 #error IPLs are screwed up
    404 #elif (IPL_NONE != 0)
    405 #error IPLs are screwed up
    406 #endif
    407 
    408 #define	SOFTINT2IPLMAP \
    409 	(((IPL_SOFTSERIAL - IPL_SOFTCLOCK) << (SOFTINT_SERIAL * 4)) | \
    410 	 ((IPL_SOFTNET    - IPL_SOFTCLOCK) << (SOFTINT_NET    * 4)) | \
    411 	 ((IPL_SOFTBIO    - IPL_SOFTCLOCK) << (SOFTINT_BIO    * 4)) | \
    412 	 ((IPL_SOFTCLOCK  - IPL_SOFTCLOCK) << (SOFTINT_CLOCK  * 4)))
    413 #define	SOFTINT2IPL(l)	((SOFTINT2IPLMAP >> ((l) * 4)) & 0x0f)
    414 
    415 /*
    416  * This returns a mask of softint IPLs that be dispatch at <ipl>
    417  * SOFTIPLMASK(IPL_NONE)	= 0x0000000f
    418  * SOFTIPLMASK(IPL_SOFTCLOCK)	= 0x0000000e
    419  * SOFTIPLMASK(IPL_SOFTBIO)	= 0x0000000c
    420  * SOFTIPLMASK(IPL_SOFTNET)	= 0x00000008
    421  * SOFTIPLMASK(IPL_SOFTSERIAL)	= 0x00000000
    422  */
    423 #define	SOFTIPLMASK(ipl) (0x0f << (ipl))
    424 
    425 void softint_switch(lwp_t *, int);
    426 
    427 void
    428 softint_trigger(uintptr_t mask)
    429 {
    430 	curcpu()->ci_softints |= mask;
    431 }
    432 
    433 void
    434 softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep)
    435 {
    436 	lwp_t ** lp = &curcpu()->ci_softlwps[level];
    437 	KASSERT(*lp == NULL || *lp == l);
    438 	*lp = l;
    439 	*machdep = 1 << SOFTINT2IPL(level);
    440 	KASSERT(level != SOFTINT_CLOCK || *machdep == (1 << (IPL_SOFTCLOCK - IPL_SOFTCLOCK)));
    441 	KASSERT(level != SOFTINT_BIO || *machdep == (1 << (IPL_SOFTBIO - IPL_SOFTCLOCK)));
    442 	KASSERT(level != SOFTINT_NET || *machdep == (1 << (IPL_SOFTNET - IPL_SOFTCLOCK)));
    443 	KASSERT(level != SOFTINT_SERIAL || *machdep == (1 << (IPL_SOFTSERIAL - IPL_SOFTCLOCK)));
    444 }
    445 
    446 void
    447 dosoftints(void)
    448 {
    449 	struct cpu_info * const ci = curcpu();
    450 	const int opl = ci->ci_cpl;
    451 	const uint32_t softiplmask = SOFTIPLMASK(opl);
    452 
    453 	splhigh();
    454 	for (;;) {
    455 		u_int softints = ci->ci_softints & softiplmask;
    456 		KASSERT((softints != 0) == ((ci->ci_softints >> opl) != 0));
    457 		KASSERT(opl == IPL_NONE || (softints & (1 << (opl - IPL_SOFTCLOCK))) == 0);
    458 		if (softints == 0) {
    459 			splx(opl);
    460 			return;
    461 		}
    462 #define	DOSOFTINT(n) \
    463 		if (ci->ci_softints & (1 << (IPL_SOFT ## n - IPL_SOFTCLOCK))) { \
    464 			ci->ci_softints &= \
    465 			    ~(1 << (IPL_SOFT ## n - IPL_SOFTCLOCK)); \
    466 			softint_switch(ci->ci_softlwps[SOFTINT_ ## n], \
    467 			    IPL_SOFT ## n); \
    468 			continue; \
    469 		}
    470 		DOSOFTINT(SERIAL);
    471 		DOSOFTINT(NET);
    472 		DOSOFTINT(BIO);
    473 		DOSOFTINT(CLOCK);
    474 		panic("dosoftints wtf (softints=%u?, ipl=%d)", softints, opl);
    475 	}
    476 }
    477 #endif /* __HAVE_FAST_SOFTINTS */
    478 
    479 #ifdef MODULAR
    480 /*
    481  * Push any modules loaded by the boot loader.
    482  */
    483 void
    484 module_init_md(void)
    485 {
    486 }
    487 #endif /* MODULAR */
    488 
    489 int
    490 mm_md_physacc(paddr_t pa, vm_prot_t prot)
    491 {
    492 
    493 	return (pa < ctob(physmem)) ? 0 : EFAULT;
    494 }
    495