xref: /netbsd-src/sys/arch/hpcarm/hpcarm/hpc_machdep.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /*	$NetBSD: hpc_machdep.c,v 1.86 2008/06/13 13:24:10 rafal 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.86 2008/06/13 13:24:10 rafal 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 	IRQdisable;
314 
315 #ifdef DEBUG_BEFOREMMU
316 	/*
317 	 * At this point, we cannot call real consinit().
318 	 * Just call a faked up version of consinit(), which does the thing
319 	 * with MMU disabled.
320 	 */
321 	fakecninit();
322 #endif
323 
324 	/*
325 	 * XXX for now, overwrite bootconfig to hardcoded values.
326 	 * XXX kill bootconfig and directly call uvm_physload
327 	 */
328 	bootconfig.dram[0].address = 0xc0000000;
329 	bootconfig.dram[0].pages = DRAM_PAGES;
330 	bootconfig.dramblocks = 1;
331 	kerneldatasize = (uint32_t)&end - (uint32_t)KERNEL_TEXT_BASE;
332 
333 	symbolsize = 0;
334 #if NKSYMS || defined(DDB) || defined(LKM)
335 	if (!memcmp(&end, "\177ELF", 4)) {
336 		sh = (Elf_Shdr *)((char *)&end + ((Elf_Ehdr *)&end)->e_shoff);
337 		loop = ((Elf_Ehdr *)&end)->e_shnum;
338 		for (; loop; loop--, sh++)
339 			if (sh->sh_offset > 0 &&
340 			    (sh->sh_offset + sh->sh_size) > symbolsize)
341 				symbolsize = sh->sh_offset + sh->sh_size;
342 	}
343 #endif
344 
345 	printf("kernsize=0x%x\n", kerneldatasize);
346 	kerneldatasize += symbolsize;
347 	kerneldatasize = ((kerneldatasize - 1) & ~(PAGE_SIZE * 4 - 1)) +
348 	    PAGE_SIZE * 8;
349 
350 	/* parse kernel args */
351 	boothowto = 0;
352 	boot_file[0] = '\0';
353 	strncpy(booted_kernel_storage, argv[0], sizeof(booted_kernel_storage));
354 	for (i = 1; i < argc; i++) {
355 		char *cp = argv[i];
356 
357 		switch (*cp) {
358 		case 'b':
359 			/* boot device: -b=sd0 etc. */
360 			cp = cp + 2;
361 #ifdef NFS
362 			if (strcmp(cp, "nfs") == 0)
363 				mountroot = nfs_mountroot;
364 			else
365 				strncpy(boot_file, cp, sizeof(boot_file));
366 #else /* !NFS */
367 			strncpy(boot_file, cp, sizeof(boot_file));
368 #endif /* !NFS */
369 			break;
370 		default:
371 			BOOT_FLAG(*cp, boothowto);
372 			break;
373 		}
374 	}
375 
376 	/* copy bootinfo into known kernel space */
377 	bootinfo_storage = *bi;
378 	bootinfo = &bootinfo_storage;
379 
380 #ifdef BOOTINFO_FB_WIDTH
381 	bootinfo->fb_line_bytes = BOOTINFO_FB_LINE_BYTES;
382 	bootinfo->fb_width = BOOTINFO_FB_WIDTH;
383 	bootinfo->fb_height = BOOTINFO_FB_HEIGHT;
384 	bootinfo->fb_type = BOOTINFO_FB_TYPE;
385 #endif
386 
387 	/*
388 	 * hpcboot has loaded me with MMU disabled.
389 	 * So create kernel page tables and enable MMU.
390 	 */
391 
392 	/*
393 	 * Set up the variables that define the availability of physcial
394 	 * memory.
395 	 */
396 	physical_start = bootconfig.dram[0].address;
397 	physical_freestart = physical_start
398 	    + (KERNEL_TEXT_BASE - KERNEL_BASE) + kerneldatasize;
399 	physical_end = bootconfig.dram[bootconfig.dramblocks - 1].address
400 	    + bootconfig.dram[bootconfig.dramblocks - 1].pages * PAGE_SIZE;
401 	physical_freeend = physical_end;
402 
403 	for (loop = 0; loop < bootconfig.dramblocks; ++loop)
404 		physmem += bootconfig.dram[loop].pages;
405 
406 	/* XXX handle UMA framebuffer memory */
407 
408 	/* Use the first 256kB to allocate things */
409 	freemempos = KERNEL_BASE;
410 	memset((void *)KERNEL_BASE, 0, KERNEL_TEXT_BASE - KERNEL_BASE);
411 
412 	/*
413 	 * Right. We have the bottom meg of memory mapped to 0x00000000
414 	 * so was can get at it. The kernel will occupy the start of it.
415 	 * After the kernel/args we allocate some of the fixed page tables
416 	 * we need to get the system going.
417 	 * We allocate one page directory and NUM_KERNEL_PTS page tables
418 	 * and store the physical addresses in the kernel_pt_table array.
419 	 * Must remember that neither the page L1 or L2 page tables are the
420 	 * same size as a page !
421 	 *
422 	 * Ok, the next bit of physical allocate may look complex but it is
423 	 * simple really. I have done it like this so that no memory gets
424 	 * wasted during the allocate of various pages and tables that are
425 	 * all different sizes.
426 	 * The start address will be page aligned.
427 	 * We allocate the kernel page directory on the first free 16KB
428 	 * boundary we find.
429 	 * We allocate the kernel page tables on the first 1KB boundary we
430 	 * find.  We allocate at least 9 PT's (12 currently).  This means
431 	 * that in the process we KNOW that we will encounter at least one
432 	 * 16KB boundary.
433 	 *
434 	 * Eventually if the top end of the memory gets used for process L1
435 	 * page tables the kernel L1 page table may be moved up there.
436 	 */
437 
438 #ifdef VERBOSE_INIT_ARM
439 	printf("Allocating page tables\n");
440 #endif
441 
442 	/* Define a macro to simplify memory allocation */
443 #define	valloc_pages(var, np)			\
444 	(var).pv_pa = (var).pv_va = freemempos;	\
445 	freemempos += (np) * PAGE_SIZE;
446 #define	alloc_pages(var, np)			\
447 	(var) = freemempos;			\
448 	freemempos += (np) * PAGE_SIZE;
449 
450 
451 	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
452 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
453 		alloc_pages(kernel_pt_table[loop].pv_pa,
454 		    L2_TABLE_SIZE / PAGE_SIZE);
455 		kernel_pt_table[loop].pv_va = kernel_pt_table[loop].pv_pa;
456 	}
457 
458 	/*
459 	 * Allocate a page for the system page mapped to V0x00000000
460 	 * This page will just contain the system vectors and can be
461 	 * shared by all processes.
462 	 */
463 	valloc_pages(systempage, 1);
464 
465 	pt_size = round_page(freemempos) - KERNEL_BASE;
466 
467 	/* Allocate stacks for all modes */
468 	valloc_pages(irqstack, IRQ_STACK_SIZE);
469 	valloc_pages(abtstack, ABT_STACK_SIZE);
470 	valloc_pages(undstack, UND_STACK_SIZE);
471 	valloc_pages(kernelstack, UPAGES);
472 
473 #ifdef VERBOSE_INIT_ARM
474 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
475 	    irqstack.pv_va);
476 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
477 	    abtstack.pv_va);
478 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
479 	    undstack.pv_va);
480 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
481 	    kernelstack.pv_va);
482 #endif
483 
484 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
485 
486 	/*
487 	 * XXX Actually, we only need virtual space and don't need
488 	 * XXX physical memory for sa110_cc_base and sa11x0_idle_mem.
489 	 */
490 	/*
491 	 * XXX totally stuffed hack to work round problems introduced
492 	 * in recent versions of the pmap code. Due to the calls used there
493 	 * we cannot allocate virtual memory during bootstrap.
494 	 */
495 	for (;;) {
496 		alloc_pages(sa1_cc_base, 1);
497 		if (!(sa1_cc_base & (CPU_SA110_CACHE_CLEAN_SIZE - 1)))
498 			break;
499 	}
500 	{
501 		vaddr_t dummy;
502 		alloc_pages(dummy, CPU_SA110_CACHE_CLEAN_SIZE / PAGE_SIZE - 1);
503 	}
504 	sa1_cache_clean_addr = sa1_cc_base;
505 	sa1_cache_clean_size = CPU_SA110_CACHE_CLEAN_SIZE / 2;
506 
507 	alloc_pages(sa11x0_idle_mem, 1);
508 
509 	/*
510 	 * Ok, we have allocated physical pages for the primary kernel
511 	 * page tables.
512 	 */
513 
514 #ifdef VERBOSE_INIT_ARM
515 	printf("Creating L1 page table\n");
516 #endif
517 
518 	/*
519 	 * Now we start construction of the L1 page table.
520 	 * We start by mapping the L2 page tables into the L1.
521 	 * This means that we can replace L1 mappings later on if necessary.
522 	 */
523 	l1pagetable = kernel_l1pt.pv_pa;
524 
525 	/* Map the L2 pages tables in the L1 page table */
526 	pmap_link_l2pt(l1pagetable, 0x00000000,
527 	    &kernel_pt_table[KERNEL_PT_SYS]);
528 #define SAIPIO_BASE		0xd0000000		/* XXX XXX */
529 	pmap_link_l2pt(l1pagetable, SAIPIO_BASE,
530 	    &kernel_pt_table[KERNEL_PT_IO]);
531 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
532 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
533 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
534 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
535 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
536 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
537 
538 	/* update the top of the kernel VM */
539 	pmap_curmaxkvaddr =
540 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
541 
542 #ifdef VERBOSE_INIT_ARM
543 	printf("Mapping kernel\n");
544 #endif
545 
546 	/* Now we fill in the L2 pagetable for the kernel code/data */
547 
548 	/*
549 	 * XXX there is no ELF header to find RO region.
550 	 * XXX What should we do?
551 	 */
552 #if 0
553 	if (N_GETMAGIC(kernexec[0]) == ZMAGIC) {
554 		logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
555 		    physical_start, kernexec->a_text,
556 		    VM_PROT_READ, PTE_CACHE);
557 		logical += pmap_map_chunk(l1pagetable,
558 		    KERNEL_TEXT_BASE + logical, physical_start + logical,
559 		    kerneldatasize - kernexec->a_text,
560 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
561 	} else
562 #endif
563 		pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
564 		    KERNEL_TEXT_BASE, kerneldatasize,
565 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
566 
567 #ifdef VERBOSE_INIT_ARM
568 	printf("Constructing L2 page tables\n");
569 #endif
570 
571 	/* Map the stack pages */
572 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
573 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
574 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
575 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
576 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
577 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
578 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
579 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
580 
581 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
582 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
583 
584 	/* Map page tables */
585 	pmap_map_chunk(l1pagetable, KERNEL_BASE, KERNEL_BASE, pt_size,
586 	    VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
587 
588 	/* Map a page for entering idle mode */
589 	pmap_map_entry(l1pagetable, sa11x0_idle_mem, sa11x0_idle_mem,
590 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
591 
592 	/* Map the vector page. */
593 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
594 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
595 
596 	/* Map the statically mapped devices. */
597 	pmap_devmap_bootstrap(l1pagetable, sa11x0_devmap);
598 
599 	pmap_map_chunk(l1pagetable, sa1_cache_clean_addr, 0xe0000000,
600 	    CPU_SA110_CACHE_CLEAN_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
601 	/*
602 	 * Now we have the real page tables in place so we can switch to them.
603 	 * Once this is done we will be running with the REAL kernel page
604 	 * tables.
605 	 */
606 
607 	printf("done.\n");
608 
609 	/*
610 	 * Pages were allocated during the secondary bootstrap for the
611 	 * stacks for different CPU modes.
612 	 * We must now set the r13 registers in the different CPU modes to
613 	 * point to these stacks.
614 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
615 	 * of the stack memory.
616 	 */
617 	printf("init subsystems: stacks ");
618 
619 	set_stackptr(PSR_IRQ32_MODE,
620 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
621 	set_stackptr(PSR_ABT32_MODE,
622 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
623 	set_stackptr(PSR_UND32_MODE,
624 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
625 #ifdef PMAP_DEBUG
626 	if (pmap_debug_level >= 0)
627 		printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
628 		    kernelstack.pv_pa);
629 #endif /* PMAP_DEBUG */
630 
631 	/*
632 	 * Well we should set a data abort handler.
633 	 * Once things get going this will change as we will need a proper
634 	 * handler. Until then we will use a handler that just panics but
635 	 * tells us why.
636 	 * Initialization of the vectors will just panic on a data abort.
637 	 * This just fills in a slightly better one.
638 	 */
639 	printf("vectors ");
640 	data_abort_handler_address = (u_int)data_abort_handler;
641 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
642 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
643 	printf("%08x %08x %08x\n", data_abort_handler_address,
644 	    prefetch_abort_handler_address, undefined_handler_address);
645 
646 	/* Initialize the undefined instruction handlers */
647 	printf("undefined ");
648 	undefined_init();
649 
650 	/* Set the page table address. */
651 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
652 	setttb(kernel_l1pt.pv_pa);
653 	cpu_tlb_flushID();
654 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
655 
656 	/*
657 	 * Moved from cpu_startup() as data_abort_handler() references
658 	 * this during uvm init.
659 	 */
660 	proc0paddr = (struct user *)kernelstack.pv_va;
661 	lwp0.l_addr = proc0paddr;
662 
663 #ifdef BOOT_DUMP
664 	dumppages((char *)0xc0000000, 16 * PAGE_SIZE);
665 	dumppages((char *)0xb0100000, 64); /* XXX */
666 #endif
667 	/* Enable MMU, I-cache, D-cache, write buffer. */
668 	cpufunc_control(0x337f, 0x107d);
669 
670 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
671 
672 	consinit();
673 
674 #ifdef VERBOSE_INIT_ARM
675 	printf("freemempos=%08lx\n", freemempos);
676 	printf("MMU enabled. control=%08x\n", cpu_get_control());
677 #endif
678 
679 	/* Load memory into UVM. */
680 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
681 	for (loop = 0; loop < bootconfig.dramblocks; loop++) {
682 		paddr_t dblk_start = (paddr_t)bootconfig.dram[loop].address;
683 		paddr_t dblk_end = dblk_start
684 			+ (bootconfig.dram[loop].pages * PAGE_SIZE);
685 
686 		if (dblk_start < physical_freestart)
687 			dblk_start = physical_freestart;
688 		if (dblk_end > physical_freeend)
689 			dblk_end = physical_freeend;
690 
691 		uvm_page_physload(atop(dblk_start), atop(dblk_end),
692 		    atop(dblk_start), atop(dblk_end), VM_FREELIST_DEFAULT);
693 	}
694 
695 	/* Boot strap pmap telling it where the kernel page table is */
696 	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
697 
698 #ifdef BOOT_DUMP
699 	dumppages((char *)kernel_l1pt.pv_va, 16);
700 #endif
701 
702 #ifdef DDB
703 	db_machine_init();
704 #endif
705 #if NKSYMS || defined(DDB) || defined(LKM)
706 	ksyms_init(symbolsize, ((int *)&end), ((char *)&end) + symbolsize);
707 #endif
708 
709 	printf("kernsize=0x%x", kerneldatasize);
710 	printf(" (including 0x%x symbols)\n", symbolsize);
711 
712 #ifdef DDB
713 	if (boothowto & RB_KDB)
714 		Debugger();
715 #endif /* DDB */
716 
717 	if (bootinfo->magic == BOOTINFO_MAGIC) {
718 		platid.dw.dw0 = bootinfo->platid_cpu;
719 		platid.dw.dw1 = bootinfo->platid_machine;
720 	}
721 
722 	/* We return the new stack pointer address */
723 	return (kernelstack.pv_va + USPACE_SVC_STACK_TOP);
724 }
725 
726 void
727 machine_sleep(void)
728 {
729 
730 	if (__sleep_func != NULL)
731 		__sleep_func(__sleep_ctx);
732 }
733 
734 void
735 machine_standby(void)
736 {
737 
738 }
739 
740 void
741 consinit(void)
742 {
743 	static int consinit_called = 0;
744 
745 	if (consinit_called != 0)
746 		return;
747 
748 	consinit_called = 1;
749 	if (bootinfo->bi_cnuse == BI_CNUSE_SERIAL)
750 		cninit();
751 	else {
752 		/*
753 		 * Nothing to do here.  Console initialization is done at
754 		 * autoconf device attach time.
755 		 */
756 	}
757 }
758 
759 #ifdef DEBUG_BEFOREMMU
760 cons_decl(sacom);
761 
762 static void
763 fakecninit(void)
764 {
765 	static struct consdev fakecntab = cons_init(sacom);
766 	cn_tab = &fakecntab;
767 
768 	(*cn_tab->cn_init)(0);
769 	cn_tab->cn_pri = CN_REMOTE;
770 }
771 #endif
772 
773 #ifdef BOOT_DUMP
774 static void
775 dumppages(char *start, int nbytes)
776 {
777 	char *p = start;
778 	char *p1;
779 	int i;
780 
781 	for (i = nbytes; i > 0; i -= 16, p += 16) {
782 		for (p1 = p + 15; p != p1; p1--) {
783 			if (*p1)
784 				break;
785 		}
786 		if (!*p1)
787 			continue;
788 		printf("%08x %02x %02x %02x %02x %02x %02x %02x %02x"
789 		    " %02x %02x %02x %02x %02x %02x %02x %02x\n",
790 		    (unsigned int)p,
791 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
792 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
793 	}
794 }
795 #endif
796