xref: /netbsd-src/sys/arch/hpcarm/hpcarm/hpc_machdep.c (revision 62a8debe1dc62962e18a1c918def78666141273b)
1 /*	$NetBSD: hpc_machdep.c,v 1.96 2010/03/02 21:17:31 pooka 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 Brini.
21  * 4. The name of the company nor the name of the author may be used to
22  *    endorse or promote products derived from this software without specific
23  *    prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 /*
39  * Machine dependent functions for kernel setup.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: hpc_machdep.c,v 1.96 2010/03/02 21:17:31 pooka Exp $");
44 
45 #include "opt_ddb.h"
46 #include "opt_modular.h"
47 #include "opt_pmap_debug.h"
48 #include "ksyms.h"
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/reboot.h>
54 #include <sys/proc.h>
55 #include <sys/msgbuf.h>
56 #include <sys/exec.h>
57 #include <sys/ksyms.h>
58 #include <sys/boot_flag.h>
59 #include <sys/conf.h>	/* XXX for consinit related hacks */
60 #include <sys/device.h>
61 
62 #if NKSYMS || defined(DDB) || defined(MODULAR)
63 #include <machine/db_machdep.h>
64 #include <ddb/db_sym.h>
65 #include <ddb/db_extern.h>
66 #ifndef DB_ELFSIZE
67 #error Must define DB_ELFSIZE!
68 #endif
69 #define ELFSIZE		DB_ELFSIZE
70 #include <sys/exec_elf.h>
71 #endif
72 
73 #include <uvm/uvm.h>
74 
75 #include <arm/sa11x0/sa11x0_reg.h>
76 #include <arm/cpuconf.h>
77 #include <arm/undefined.h>
78 
79 #include <machine/bootconfig.h>
80 #include <machine/bootinfo.h>
81 #include <machine/cpu.h>
82 #include <machine/frame.h>
83 #include <machine/intr.h>
84 #include <machine/io.h>
85 #include <machine/platid.h>
86 #include <machine/rtc.h>
87 #include <machine/signal.h>
88 
89 #include <dev/cons.h>
90 #include <dev/hpc/apm/apmvar.h>
91 #include <dev/hpc/bicons.h>
92 
93 #include <sys/mount.h>
94 #include <nfs/rpcv2.h>
95 #include <nfs/nfsproto.h>
96 #include <nfs/nfs.h>
97 #include <nfs/nfsmount.h>
98 
99 /* Kernel text starts 256K in from the bottom of the kernel address space. */
100 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00040000)
101 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x00C00000)
102 #define	KERNEL_VM_SIZE		0x05000000
103 
104 /*
105  * Address to call from cpu_reset() to reset the machine.
106  * This is machine architecture dependent as it varies depending
107  * on where the ROM appears when you turn the MMU off.
108  */
109 u_int cpu_reset_address = 0;
110 
111 /* Define various stack sizes in pages */
112 #define IRQ_STACK_SIZE	1
113 #define ABT_STACK_SIZE	1
114 #define UND_STACK_SIZE	1
115 
116 BootConfig bootconfig;		/* Boot config storage */
117 struct bootinfo *bootinfo, bootinfo_storage;
118 static char booted_kernel_storage[80];
119 char *booted_kernel = booted_kernel_storage;
120 
121 paddr_t physical_start;
122 paddr_t physical_freestart;
123 paddr_t physical_freeend;
124 paddr_t physical_end;
125 
126 #ifndef PMAP_STATIC_L1S
127 int max_processes = 64;			/* Default number */
128 #endif /* !PMAP_STATIC_L1S */
129 
130 
131 /* Physical and virtual addresses for some global pages */
132 pv_addr_t irqstack;
133 pv_addr_t undstack;
134 pv_addr_t abtstack;
135 pv_addr_t kernelstack;
136 
137 char *boot_args = NULL;
138 char boot_file[16];
139 
140 vaddr_t msgbufphys;
141 
142 extern u_int data_abort_handler_address;
143 extern u_int prefetch_abort_handler_address;
144 extern u_int undefined_handler_address;
145 extern int end;
146 
147 #ifdef PMAP_DEBUG
148 extern int pmap_debug_level;
149 #endif /* PMAP_DEBUG */
150 
151 #define	KERNEL_PT_VMEM		0	/* Page table for mapping video memory */
152 #define	KERNEL_PT_SYS		1	/* Page table for mapping proc0 zero page */
153 #define	KERNEL_PT_IO		2	/* Page table for mapping IO */
154 #define	KERNEL_PT_KERNEL	3	/* Page table for mapping kernel */
155 #define	KERNEL_PT_KERNEL_NUM	4
156 #define KERNEL_PT_VMDATA	(KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
157 				        /* Page tables for mapping kernel VM */
158 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
159 #define	NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
160 
161 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
162 
163 #define CPU_SA110_CACHE_CLEAN_SIZE (0x4000 * 2)
164 extern unsigned int sa1_cache_clean_addr;
165 extern unsigned int sa1_cache_clean_size;
166 static vaddr_t sa1_cc_base;
167 
168 /* Mode dependent sleep function holder */
169 void (*__sleep_func)(void *);
170 void *__sleep_ctx;
171 
172 /* Non-buffered non-cacheable memory needed to enter idle mode */
173 extern vaddr_t sa11x0_idle_mem;
174 
175 /* Prototypes */
176 void		data_abort_handler(trapframe_t *);
177 void		prefetch_abort_handler(trapframe_t *);
178 void		undefinedinstruction_bounce(trapframe_t *);
179 void		dumpsys(void);
180 u_int		cpu_get_control(void);
181 
182 u_int		initarm(int, char **, struct bootinfo *);
183 
184 #ifdef DEBUG_BEFOREMMU
185 static void	fakecninit(void);
186 #endif
187 
188 #ifdef BOOT_DUMP
189 static void	dumppages(char *, int);
190 #endif
191 
192 /*
193  * Reboots the system.
194  *
195  * Deal with any syncing, unmounting, dumping and shutdown hooks,
196  * then reset the CPU.
197  */
198 void
199 cpu_reboot(int howto, char *bootstr)
200 {
201 	/*
202 	 * If we are still cold then hit the air brakes
203 	 * and crash to earth fast.
204 	 */
205 	if (cold) {
206 		doshutdownhooks();
207 		pmf_system_shutdown(boothowto);
208 		printf("Halted while still in the ICE age.\n");
209 		printf("The operating system has halted.\n");
210 		printf("Please press any key to reboot.\n\n");
211 		cngetc();
212 		printf("rebooting...\n");
213 		cpu_reset();
214 		/* NOTREACHED */
215 	}
216 
217 	/* Reset the sleep function. */
218 	__sleep_func = NULL;
219 	__sleep_ctx = NULL;
220 
221 	/* Disable console buffering. */
222 	cnpollc(1);
223 
224 	/*
225 	 * If RB_NOSYNC was not specified sync the discs.
226 	 * Note: Unless cold is set to 1 here, syslogd will die during
227 	 * the unmount.  It looks like syslogd is getting woken up only
228 	 * to find that it cannot page part of the binary in as the
229 	 * file system has been unmounted.
230 	 */
231 	if (!(howto & RB_NOSYNC))
232 		bootsync();
233 
234 	/* Say NO to interrupts. */
235 	(void)splhigh();
236 
237 	/* Do a dump if requested. */
238 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
239 		dumpsys();
240 
241 	/* Run any shutdown hooks. */
242 	doshutdownhooks();
243 
244 	pmf_system_shutdown(boothowto);
245 
246 	/* Make sure IRQs are disabled. */
247 	IRQdisable;
248 
249 	if (howto & RB_HALT) {
250 		printf("The operating system has halted.\n");
251 		printf("Please press any key to reboot.\n\n");
252 		cngetc();
253 	}
254 
255 	printf("rebooting...\n");
256 	cpu_reset();
257 	/* NOTREACHED */
258 }
259 
260 /* Number of DRAM pages which are installed */
261 /* Units are 4K pages, so 8192 is 32 MB of memory */
262 #ifndef DRAM_PAGES
263 #define DRAM_PAGES	8192
264 #endif
265 
266 /*
267  * Static device mappings. These peripheral registers are mapped at
268  * fixed virtual addresses very early in initarm() so that we can use
269  * them while booting the kernel and stay at the same address
270  * throughout whole kernel's life time.
271  */
272 static const struct pmap_devmap sa11x0_devmap[] = {
273 	/* Physical/virtual address for UART #3. */
274 	{
275 		SACOM3_VBASE,
276 		SACOM3_BASE,
277 		0x24,
278 		VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE
279 	},
280 	{ 0, 0, 0, 0, 0 }
281 };
282 
283 /*
284  * Initial entry point on startup. This gets called before main() is
285  * entered.
286  * It should be responsible for setting up everything that must be
287  * in place when main is called.
288  * This includes:
289  *   Taking a copy of the boot configuration structure.
290  *   Initializing the physical console so characters can be printed.
291  *   Setting up page tables for the kernel.
292  */
293 u_int
294 initarm(int argc, char **argv, struct bootinfo *bi)
295 {
296 	u_int kerneldatasize, symbolsize;
297 	u_int l1pagetable;
298 	vaddr_t freemempos;
299 	vsize_t pt_size;
300 	int loop, i;
301 #if NKSYMS || defined(DDB) || defined(MODULAR)
302 	Elf_Shdr *sh;
303 #endif
304 
305 	__sleep_func = NULL;
306 	__sleep_ctx = NULL;
307 
308 	/*
309 	 * Heads up ... Setup the CPU / MMU / TLB functions.
310 	 */
311 	set_cpufuncs();
312 	IRQdisable;
313 
314 #ifdef DEBUG_BEFOREMMU
315 	/*
316 	 * At this point, we cannot call real consinit().
317 	 * Just call a faked up version of consinit(), which does the thing
318 	 * with MMU disabled.
319 	 */
320 	fakecninit();
321 #endif
322 
323 	/*
324 	 * XXX for now, overwrite bootconfig to hardcoded values.
325 	 * XXX kill bootconfig and directly call uvm_physload
326 	 */
327 	bootconfig.dram[0].address = 0xc0000000;
328 	bootconfig.dram[0].pages = DRAM_PAGES;
329 	bootconfig.dramblocks = 1;
330 	kerneldatasize = (uint32_t)&end - (uint32_t)KERNEL_TEXT_BASE;
331 
332 	symbolsize = 0;
333 #if NKSYMS || defined(DDB) || defined(MODULAR)
334 	if (!memcmp(&end, "\177ELF", 4)) {
335 		sh = (Elf_Shdr *)((char *)&end + ((Elf_Ehdr *)&end)->e_shoff);
336 		loop = ((Elf_Ehdr *)&end)->e_shnum;
337 		for (; loop; loop--, sh++)
338 			if (sh->sh_offset > 0 &&
339 			    (sh->sh_offset + sh->sh_size) > symbolsize)
340 				symbolsize = sh->sh_offset + sh->sh_size;
341 	}
342 #endif
343 
344 	printf("kernsize=0x%x\n", kerneldatasize);
345 	kerneldatasize += symbolsize;
346 	kerneldatasize = ((kerneldatasize - 1) & ~(PAGE_SIZE * 4 - 1)) +
347 	    PAGE_SIZE * 8;
348 
349 	/* parse kernel args */
350 	boothowto = 0;
351 	boot_file[0] = '\0';
352 	strncpy(booted_kernel_storage, argv[0], sizeof(booted_kernel_storage));
353 	for (i = 1; i < argc; i++) {
354 		char *cp = argv[i];
355 
356 		switch (*cp) {
357 		case 'b':
358 			/* boot device: -b=sd0 etc. */
359 			cp = cp + 2;
360 			if (strcmp(cp, MOUNT_NFS) == 0)
361 				rootfstype = MOUNT_NFS;
362 			else
363 				strncpy(boot_file, cp, sizeof(boot_file));
364 			break;
365 		default:
366 			BOOT_FLAG(*cp, boothowto);
367 			break;
368 		}
369 	}
370 
371 	/* copy bootinfo into known kernel space */
372 	bootinfo_storage = *bi;
373 	bootinfo = &bootinfo_storage;
374 
375 #ifdef BOOTINFO_FB_WIDTH
376 	bootinfo->fb_line_bytes = BOOTINFO_FB_LINE_BYTES;
377 	bootinfo->fb_width = BOOTINFO_FB_WIDTH;
378 	bootinfo->fb_height = BOOTINFO_FB_HEIGHT;
379 	bootinfo->fb_type = BOOTINFO_FB_TYPE;
380 #endif
381 
382 	/*
383 	 * hpcboot has loaded me with MMU disabled.
384 	 * So create kernel page tables and enable MMU.
385 	 */
386 
387 	/*
388 	 * Set up the variables that define the availability of physcial
389 	 * memory.
390 	 */
391 	physical_start = bootconfig.dram[0].address;
392 	physical_freestart = physical_start
393 	    + (KERNEL_TEXT_BASE - KERNEL_BASE) + kerneldatasize;
394 	physical_end = bootconfig.dram[bootconfig.dramblocks - 1].address
395 	    + bootconfig.dram[bootconfig.dramblocks - 1].pages * PAGE_SIZE;
396 	physical_freeend = physical_end;
397 
398 	for (loop = 0; loop < bootconfig.dramblocks; ++loop)
399 		physmem += bootconfig.dram[loop].pages;
400 
401 	/* XXX handle UMA framebuffer memory */
402 
403 	/* Use the first 256kB to allocate things */
404 	freemempos = KERNEL_BASE;
405 	memset((void *)KERNEL_BASE, 0, KERNEL_TEXT_BASE - KERNEL_BASE);
406 
407 	/*
408 	 * Right. We have the bottom meg of memory mapped to 0x00000000
409 	 * so was can get at it. The kernel will occupy the start of it.
410 	 * After the kernel/args we allocate some of the fixed page tables
411 	 * we need to get the system going.
412 	 * We allocate one page directory and NUM_KERNEL_PTS page tables
413 	 * and store the physical addresses in the kernel_pt_table array.
414 	 * Must remember that neither the page L1 or L2 page tables are the
415 	 * same size as a page !
416 	 *
417 	 * Ok, the next bit of physical allocate may look complex but it is
418 	 * simple really. I have done it like this so that no memory gets
419 	 * wasted during the allocate of various pages and tables that are
420 	 * all different sizes.
421 	 * The start address will be page aligned.
422 	 * We allocate the kernel page directory on the first free 16KB
423 	 * boundary we find.
424 	 * We allocate the kernel page tables on the first 1KB boundary we
425 	 * find.  We allocate at least 9 PT's (12 currently).  This means
426 	 * that in the process we KNOW that we will encounter at least one
427 	 * 16KB boundary.
428 	 *
429 	 * Eventually if the top end of the memory gets used for process L1
430 	 * page tables the kernel L1 page table may be moved up there.
431 	 */
432 
433 #ifdef VERBOSE_INIT_ARM
434 	printf("Allocating page tables\n");
435 #endif
436 
437 	/* Define a macro to simplify memory allocation */
438 #define	valloc_pages(var, np)			\
439 	(var).pv_pa = (var).pv_va = freemempos;	\
440 	freemempos += (np) * PAGE_SIZE;
441 #define	alloc_pages(var, np)			\
442 	(var) = freemempos;			\
443 	freemempos += (np) * PAGE_SIZE;
444 
445 
446 	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
447 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
448 		alloc_pages(kernel_pt_table[loop].pv_pa,
449 		    L2_TABLE_SIZE / PAGE_SIZE);
450 		kernel_pt_table[loop].pv_va = kernel_pt_table[loop].pv_pa;
451 	}
452 
453 	/*
454 	 * Allocate a page for the system page mapped to V0x00000000
455 	 * This page will just contain the system vectors and can be
456 	 * shared by all processes.
457 	 */
458 	valloc_pages(systempage, 1);
459 
460 	pt_size = round_page(freemempos) - KERNEL_BASE;
461 
462 	/* Allocate stacks for all modes */
463 	valloc_pages(irqstack, IRQ_STACK_SIZE);
464 	valloc_pages(abtstack, ABT_STACK_SIZE);
465 	valloc_pages(undstack, UND_STACK_SIZE);
466 	valloc_pages(kernelstack, UPAGES);
467 
468 #ifdef VERBOSE_INIT_ARM
469 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
470 	    irqstack.pv_va);
471 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
472 	    abtstack.pv_va);
473 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
474 	    undstack.pv_va);
475 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
476 	    kernelstack.pv_va);
477 #endif
478 
479 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
480 
481 	/*
482 	 * XXX Actually, we only need virtual space and don't need
483 	 * XXX physical memory for sa110_cc_base and sa11x0_idle_mem.
484 	 */
485 	/*
486 	 * XXX totally stuffed hack to work round problems introduced
487 	 * in recent versions of the pmap code. Due to the calls used there
488 	 * we cannot allocate virtual memory during bootstrap.
489 	 */
490 	for (;;) {
491 		alloc_pages(sa1_cc_base, 1);
492 		if (!(sa1_cc_base & (CPU_SA110_CACHE_CLEAN_SIZE - 1)))
493 			break;
494 	}
495 	{
496 		vaddr_t dummy;
497 		alloc_pages(dummy, CPU_SA110_CACHE_CLEAN_SIZE / PAGE_SIZE - 1);
498 	}
499 	sa1_cache_clean_addr = sa1_cc_base;
500 	sa1_cache_clean_size = CPU_SA110_CACHE_CLEAN_SIZE / 2;
501 
502 	alloc_pages(sa11x0_idle_mem, 1);
503 
504 	/*
505 	 * Ok, we have allocated physical pages for the primary kernel
506 	 * page tables.
507 	 */
508 
509 #ifdef VERBOSE_INIT_ARM
510 	printf("Creating L1 page table\n");
511 #endif
512 
513 	/*
514 	 * Now we start construction of the L1 page table.
515 	 * We start by mapping the L2 page tables into the L1.
516 	 * This means that we can replace L1 mappings later on if necessary.
517 	 */
518 	l1pagetable = kernel_l1pt.pv_pa;
519 
520 	/* Map the L2 pages tables in the L1 page table */
521 	pmap_link_l2pt(l1pagetable, 0x00000000,
522 	    &kernel_pt_table[KERNEL_PT_SYS]);
523 #define SAIPIO_BASE		0xd0000000		/* XXX XXX */
524 	pmap_link_l2pt(l1pagetable, SAIPIO_BASE,
525 	    &kernel_pt_table[KERNEL_PT_IO]);
526 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
527 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
528 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
529 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
530 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
531 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
532 
533 	/* update the top of the kernel VM */
534 	pmap_curmaxkvaddr =
535 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
536 
537 #ifdef VERBOSE_INIT_ARM
538 	printf("Mapping kernel\n");
539 #endif
540 
541 	/* Now we fill in the L2 pagetable for the kernel code/data */
542 
543 	/*
544 	 * XXX there is no ELF header to find RO region.
545 	 * XXX What should we do?
546 	 */
547 #if 0
548 	if (N_GETMAGIC(kernexec[0]) == ZMAGIC) {
549 		logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
550 		    physical_start, kernexec->a_text,
551 		    VM_PROT_READ, PTE_CACHE);
552 		logical += pmap_map_chunk(l1pagetable,
553 		    KERNEL_TEXT_BASE + logical, physical_start + logical,
554 		    kerneldatasize - kernexec->a_text,
555 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
556 	} else
557 #endif
558 		pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
559 		    KERNEL_TEXT_BASE, kerneldatasize,
560 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
561 
562 #ifdef VERBOSE_INIT_ARM
563 	printf("Constructing L2 page tables\n");
564 #endif
565 
566 	/* Map the stack pages */
567 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
568 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
569 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
570 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
571 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
572 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
573 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
574 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
575 
576 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
577 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
578 
579 	/* Map page tables */
580 	pmap_map_chunk(l1pagetable, KERNEL_BASE, KERNEL_BASE, pt_size,
581 	    VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
582 
583 	/* Map a page for entering idle mode */
584 	pmap_map_entry(l1pagetable, sa11x0_idle_mem, sa11x0_idle_mem,
585 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
586 
587 	/* Map the vector page. */
588 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
589 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
590 
591 	/* Map the statically mapped devices. */
592 	pmap_devmap_bootstrap(l1pagetable, sa11x0_devmap);
593 
594 	pmap_map_chunk(l1pagetable, sa1_cache_clean_addr, 0xe0000000,
595 	    CPU_SA110_CACHE_CLEAN_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
596 	/*
597 	 * Now we have the real page tables in place so we can switch to them.
598 	 * Once this is done we will be running with the REAL kernel page
599 	 * tables.
600 	 */
601 
602 	printf("done.\n");
603 
604 	/*
605 	 * Pages were allocated during the secondary bootstrap for the
606 	 * stacks for different CPU modes.
607 	 * We must now set the r13 registers in the different CPU modes to
608 	 * point to these stacks.
609 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
610 	 * of the stack memory.
611 	 */
612 	printf("init subsystems: stacks ");
613 
614 	set_stackptr(PSR_IRQ32_MODE,
615 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
616 	set_stackptr(PSR_ABT32_MODE,
617 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
618 	set_stackptr(PSR_UND32_MODE,
619 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
620 #ifdef PMAP_DEBUG
621 	if (pmap_debug_level >= 0)
622 		printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
623 		    kernelstack.pv_pa);
624 #endif /* PMAP_DEBUG */
625 
626 	/*
627 	 * Well we should set a data abort handler.
628 	 * Once things get going this will change as we will need a proper
629 	 * handler. Until then we will use a handler that just panics but
630 	 * tells us why.
631 	 * Initialization of the vectors will just panic on a data abort.
632 	 * This just fills in a slightly better one.
633 	 */
634 	printf("vectors ");
635 	data_abort_handler_address = (u_int)data_abort_handler;
636 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
637 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
638 	printf("%08x %08x %08x\n", data_abort_handler_address,
639 	    prefetch_abort_handler_address, undefined_handler_address);
640 
641 	/* Initialize the undefined instruction handlers */
642 	printf("undefined ");
643 	undefined_init();
644 
645 	/* Set the page table address. */
646 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
647 	cpu_setttb(kernel_l1pt.pv_pa);
648 	cpu_tlb_flushID();
649 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
650 
651 	/*
652 	 * Moved from cpu_startup() as data_abort_handler() references
653 	 * this during uvm init.
654 	 */
655 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
656 
657 #ifdef BOOT_DUMP
658 	dumppages((char *)0xc0000000, 16 * PAGE_SIZE);
659 	dumppages((char *)0xb0100000, 64); /* XXX */
660 #endif
661 	/* Enable MMU, I-cache, D-cache, write buffer. */
662 	cpufunc_control(0x337f, 0x107d);
663 
664 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
665 
666 	consinit();
667 
668 #ifdef VERBOSE_INIT_ARM
669 	printf("freemempos=%08lx\n", freemempos);
670 	printf("MMU enabled. control=%08x\n", cpu_get_control());
671 #endif
672 
673 	/* Load memory into UVM. */
674 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
675 	for (loop = 0; loop < bootconfig.dramblocks; loop++) {
676 		paddr_t dblk_start = (paddr_t)bootconfig.dram[loop].address;
677 		paddr_t dblk_end = dblk_start
678 			+ (bootconfig.dram[loop].pages * PAGE_SIZE);
679 
680 		if (dblk_start < physical_freestart)
681 			dblk_start = physical_freestart;
682 		if (dblk_end > physical_freeend)
683 			dblk_end = physical_freeend;
684 
685 		uvm_page_physload(atop(dblk_start), atop(dblk_end),
686 		    atop(dblk_start), atop(dblk_end), VM_FREELIST_DEFAULT);
687 	}
688 
689 	/* Boot strap pmap telling it where the kernel page table is */
690 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
691 
692 #ifdef BOOT_DUMP
693 	dumppages((char *)kernel_l1pt.pv_va, 16);
694 #endif
695 
696 #ifdef DDB
697 	db_machine_init();
698 #endif
699 #if NKSYMS || defined(DDB) || defined(MODULAR)
700 	ksyms_addsyms_elf(symbolsize, ((int *)&end), ((char *)&end) + symbolsize);
701 #endif
702 
703 	printf("kernsize=0x%x", kerneldatasize);
704 	printf(" (including 0x%x symbols)\n", symbolsize);
705 
706 #ifdef DDB
707 	if (boothowto & RB_KDB)
708 		Debugger();
709 #endif /* DDB */
710 
711 	if (bootinfo->magic == BOOTINFO_MAGIC) {
712 		platid.dw.dw0 = bootinfo->platid_cpu;
713 		platid.dw.dw1 = bootinfo->platid_machine;
714 	}
715 
716 	/* We return the new stack pointer address */
717 	return (kernelstack.pv_va + USPACE_SVC_STACK_TOP);
718 }
719 
720 void
721 machine_sleep(void)
722 {
723 
724 	if (__sleep_func != NULL)
725 		__sleep_func(__sleep_ctx);
726 }
727 
728 void
729 machine_standby(void)
730 {
731 
732 }
733 
734 void
735 consinit(void)
736 {
737 	static int consinit_called = 0;
738 
739 	if (consinit_called != 0)
740 		return;
741 
742 	consinit_called = 1;
743 	if (bootinfo->bi_cnuse == BI_CNUSE_SERIAL)
744 		cninit();
745 	else {
746 		/*
747 		 * Nothing to do here.  Console initialization is done at
748 		 * autoconf device attach time.
749 		 */
750 	}
751 }
752 
753 #ifdef DEBUG_BEFOREMMU
754 cons_decl(sacom);
755 
756 static void
757 fakecninit(void)
758 {
759 	static struct consdev fakecntab = cons_init(sacom);
760 	cn_tab = &fakecntab;
761 
762 	(*cn_tab->cn_init)(0);
763 	cn_tab->cn_pri = CN_REMOTE;
764 }
765 #endif
766 
767 #ifdef BOOT_DUMP
768 static void
769 dumppages(char *start, int nbytes)
770 {
771 	char *p = start;
772 	char *p1;
773 	int i;
774 
775 	for (i = nbytes; i > 0; i -= 16, p += 16) {
776 		for (p1 = p + 15; p != p1; p1--) {
777 			if (*p1)
778 				break;
779 		}
780 		if (!*p1)
781 			continue;
782 		printf("%08x %02x %02x %02x %02x %02x %02x %02x %02x"
783 		    " %02x %02x %02x %02x %02x %02x %02x %02x\n",
784 		    (unsigned int)p,
785 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
786 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
787 	}
788 }
789 #endif
790