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