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