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