xref: /netbsd-src/sys/arch/arm/arm32/arm32_machdep.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: arm32_machdep.c,v 1.52 2007/10/17 19:53:30 garbled 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.52 2007/10/17 19:53:30 garbled 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 #include <sys/cpu.h>
64 
65 #include <dev/cons.h>
66 
67 #include <arm/arm32/katelib.h>
68 #include <arm/arm32/machdep.h>
69 #include <machine/bootconfig.h>
70 
71 #include "opt_ipkdb.h"
72 #include "md.h"
73 
74 struct vm_map *exec_map = NULL;
75 struct vm_map *mb_map = NULL;
76 struct vm_map *phys_map = NULL;
77 
78 extern int physmem;
79 
80 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
81 extern size_t md_root_size;		/* Memory disc size */
82 #endif	/* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
83 
84 pv_addr_t kernelstack;
85 
86 /* the following is used externally (sysctl_hw) */
87 char	machine[] = MACHINE;		/* from <machine/param.h> */
88 char	machine_arch[] = MACHINE_ARCH;	/* from <machine/param.h> */
89 
90 /* Our exported CPU info; we can have only one. */
91 struct cpu_info cpu_info_store;
92 
93 void *	msgbufaddr;
94 extern paddr_t msgbufphys;
95 
96 int kernel_debug = 0;
97 
98 struct user *proc0paddr;
99 
100 /* exported variable to be filled in by the bootloaders */
101 char *booted_kernel;
102 
103 
104 /* Prototypes */
105 
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 	vaddr_t minaddr;
214 	vaddr_t maxaddr;
215 	u_int loop;
216 	char pbuf[9];
217 
218 	/* Set the CPU control register */
219 	cpu_setup(boot_args);
220 
221 	/* Lock down zero page */
222 	vector_page_setprot(VM_PROT_READ);
223 
224 	/*
225 	 * Give pmap a chance to set up a few more things now the vm
226 	 * is initialised
227 	 */
228 	pmap_postinit();
229 
230 	/*
231 	 * Initialize error message buffer (at end of core).
232 	 */
233 
234 	/* msgbufphys was setup during the secondary boot strap */
235 	for (loop = 0; loop < btoc(MSGBUFSIZE); ++loop)
236 		pmap_kenter_pa((vaddr_t)msgbufaddr + loop * PAGE_SIZE,
237 		    msgbufphys + loop * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
238 	pmap_update(pmap_kernel());
239 	initmsgbuf(msgbufaddr, round_page(MSGBUFSIZE));
240 
241 	/*
242 	 * Identify ourselves for the msgbuf (everything printed earlier will
243 	 * not be buffered).
244 	 */
245 	printf("%s%s", copyright, version);
246 
247 	format_bytes(pbuf, sizeof(pbuf), arm_ptob(physmem));
248 	printf("total memory = %s\n", pbuf);
249 
250 	minaddr = 0;
251 
252 	/*
253 	 * Allocate a submap for exec arguments.  This map effectively
254 	 * limits the number of processes exec'ing at any time.
255 	 */
256 	exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
257 				   16*NCARGS, VM_MAP_PAGEABLE, false, NULL);
258 
259 	/*
260 	 * Allocate a submap for physio
261 	 */
262 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
263 				   VM_PHYS_SIZE, 0, false, NULL);
264 
265 	/*
266 	 * Finally, allocate mbuf cluster submap.
267 	 */
268 	mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
269 				 nmbclusters * mclbytes, VM_MAP_INTRSAFE,
270 				 false, NULL);
271 
272 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
273 	printf("avail memory = %s\n", pbuf);
274 
275 	curpcb = &lwp0.l_addr->u_pcb;
276 	curpcb->pcb_flags = 0;
277 	curpcb->pcb_un.un_32.pcb32_und_sp = (u_int)lwp0.l_addr +
278 	    USPACE_UNDEF_STACK_TOP;
279 	curpcb->pcb_un.un_32.pcb32_sp = (u_int)lwp0.l_addr +
280 	    USPACE_SVC_STACK_TOP;
281 
282         curpcb->pcb_tf = (struct trapframe *)curpcb->pcb_un.un_32.pcb32_sp - 1;
283 }
284 
285 /*
286  * machine dependent system variables.
287  */
288 static int
289 sysctl_machdep_booted_device(SYSCTLFN_ARGS)
290 {
291 	struct sysctlnode node;
292 
293 	if (booted_device == NULL)
294 		return (EOPNOTSUPP);
295 
296 	node = *rnode;
297 	node.sysctl_data = booted_device->dv_xname;
298 	node.sysctl_size = strlen(booted_device->dv_xname) + 1;
299 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
300 }
301 
302 static int
303 sysctl_machdep_booted_kernel(SYSCTLFN_ARGS)
304 {
305 	struct sysctlnode node;
306 
307 	if (booted_kernel == NULL || booted_kernel[0] == '\0')
308 		return (EOPNOTSUPP);
309 
310 	node = *rnode;
311 	node.sysctl_data = booted_kernel;
312 	node.sysctl_size = strlen(booted_kernel) + 1;
313 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
314 }
315 
316 static int
317 sysctl_machdep_powersave(SYSCTLFN_ARGS)
318 {
319 	struct sysctlnode node = *rnode;
320 	int error, newval;
321 
322 	newval = cpu_do_powersave;
323 	node.sysctl_data = &newval;
324 	if (cpufuncs.cf_sleep == (void *) cpufunc_nullop)
325 		node.sysctl_flags &= ~CTLFLAG_READWRITE;
326 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
327 	if (error || newp == NULL || newval == cpu_do_powersave)
328 		return (error);
329 
330 	if (newval < 0 || newval > 1)
331 		return (EINVAL);
332 	cpu_do_powersave = newval;
333 
334 	return (0);
335 }
336 
337 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
338 {
339 
340 	sysctl_createv(clog, 0, NULL, NULL,
341 		       CTLFLAG_PERMANENT,
342 		       CTLTYPE_NODE, "machdep", NULL,
343 		       NULL, 0, NULL, 0,
344 		       CTL_MACHDEP, CTL_EOL);
345 
346 	sysctl_createv(clog, 0, NULL, NULL,
347 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
348 		       CTLTYPE_INT, "debug", NULL,
349 		       NULL, 0, &kernel_debug, 0,
350 		       CTL_MACHDEP, CPU_DEBUG, CTL_EOL);
351 	sysctl_createv(clog, 0, NULL, NULL,
352 		       CTLFLAG_PERMANENT,
353 		       CTLTYPE_STRING, "booted_device", NULL,
354 		       sysctl_machdep_booted_device, 0, NULL, 0,
355 		       CTL_MACHDEP, CPU_BOOTED_DEVICE, CTL_EOL);
356 	sysctl_createv(clog, 0, NULL, NULL,
357 		       CTLFLAG_PERMANENT,
358 		       CTLTYPE_STRING, "booted_kernel", NULL,
359 		       sysctl_machdep_booted_kernel, 0, NULL, 0,
360 		       CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
361 	sysctl_createv(clog, 0, NULL, NULL,
362 		       CTLFLAG_PERMANENT,
363 		       CTLTYPE_STRUCT, "console_device", NULL,
364 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
365 		       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
366 	sysctl_createv(clog, 0, NULL, NULL,
367 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
368 		       CTLTYPE_INT, "powersave", NULL,
369 		       sysctl_machdep_powersave, 0, &cpu_do_powersave, 0,
370 		       CTL_MACHDEP, CPU_POWERSAVE, CTL_EOL);
371 }
372 
373 void
374 parse_mi_bootargs(args)
375 	char *args;
376 {
377 	int integer;
378 
379 	if (get_bootconf_option(args, "single", BOOTOPT_TYPE_BOOLEAN, &integer)
380 	    || get_bootconf_option(args, "-s", BOOTOPT_TYPE_BOOLEAN, &integer))
381 		if (integer)
382 			boothowto |= RB_SINGLE;
383 	if (get_bootconf_option(args, "kdb", BOOTOPT_TYPE_BOOLEAN, &integer)
384 	    || get_bootconf_option(args, "-k", BOOTOPT_TYPE_BOOLEAN, &integer))
385 		if (integer)
386 			boothowto |= RB_KDB;
387 	if (get_bootconf_option(args, "ask", BOOTOPT_TYPE_BOOLEAN, &integer)
388 	    || get_bootconf_option(args, "-a", BOOTOPT_TYPE_BOOLEAN, &integer))
389 		if (integer)
390 			boothowto |= RB_ASKNAME;
391 
392 #ifdef PMAP_DEBUG
393 	if (get_bootconf_option(args, "pmapdebug", BOOTOPT_TYPE_INT, &integer)) {
394 		pmap_debug_level = integer;
395 		pmap_debug(pmap_debug_level);
396 	}
397 #endif	/* PMAP_DEBUG */
398 
399 /*	if (get_bootconf_option(args, "nbuf", BOOTOPT_TYPE_INT, &integer))
400 		bufpages = integer;*/
401 
402 #if NMD > 0 && defined(MEMORY_DISK_HOOKS) && !defined(MEMORY_DISK_ROOT_SIZE)
403 	if (get_bootconf_option(args, "memorydisc", BOOTOPT_TYPE_INT, &integer)
404 	    || get_bootconf_option(args, "memorydisk", BOOTOPT_TYPE_INT, &integer)) {
405 		md_root_size = integer;
406 		md_root_size *= 1024;
407 		if (md_root_size < 32*1024)
408 			md_root_size = 32*1024;
409 		if (md_root_size > 2048*1024)
410 			md_root_size = 2048*1024;
411 	}
412 #endif	/* NMD && MEMORY_DISK_HOOKS && !MEMORY_DISK_ROOT_SIZE */
413 
414 	if (get_bootconf_option(args, "quiet", BOOTOPT_TYPE_BOOLEAN, &integer)
415 	    || get_bootconf_option(args, "-q", BOOTOPT_TYPE_BOOLEAN, &integer))
416 		if (integer)
417 			boothowto |= AB_QUIET;
418 	if (get_bootconf_option(args, "verbose", BOOTOPT_TYPE_BOOLEAN, &integer)
419 	    || get_bootconf_option(args, "-v", BOOTOPT_TYPE_BOOLEAN, &integer))
420 		if (integer)
421 			boothowto |= AB_VERBOSE;
422 }
423 
424 void
425 cpu_need_resched(struct cpu_info *ci, int flags)
426 {
427 	bool immed = (flags & RESCHED_IMMED) != 0;
428 
429 	if (ci->ci_want_resched && !immed)
430 		return;
431 
432 	ci->ci_want_resched = 1;
433 	if (curlwp != ci->ci_data.cpu_idlelwp)
434 		setsoftast();
435 }
436