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