xref: /netbsd-src/sys/arch/hpcarm/hpcarm/hpc_machdep.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: hpc_machdep.c,v 1.68 2003/05/22 05:47:10 thorpej 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 "opt_ddb.h"
52 #include "opt_pmap_debug.h"
53 #include "fs_nfs.h"
54 
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/reboot.h>
59 #include <sys/proc.h>
60 #include <sys/msgbuf.h>
61 #include <sys/exec.h>
62 #include <sys/ksyms.h>
63 
64 #include <dev/cons.h>
65 
66 #include "ksyms.h"
67 
68 #if NKSYMS || defined(DDB) || defined(LKM)
69 #include <machine/db_machdep.h>
70 #include <ddb/db_sym.h>
71 #include <ddb/db_extern.h>
72 #ifndef DB_ELFSIZE
73 #error Must define DB_ELFSIZE!
74 #endif
75 #define ELFSIZE		DB_ELFSIZE
76 #include <sys/exec_elf.h>
77 #endif
78 
79 #include <uvm/uvm.h>
80 
81 #include <machine/signal.h>
82 #include <machine/frame.h>
83 #include <machine/bootconfig.h>
84 #include <machine/cpu.h>
85 #include <machine/io.h>
86 #include <machine/intr.h>
87 #include <arm/arm32/katelib.h>
88 #include <machine/bootinfo.h>
89 #include <arm/cpuconf.h>
90 #include <arm/undefined.h>
91 #include <machine/rtc.h>
92 #include <machine/platid.h>
93 
94 #include <arm/sa11x0/sa11x0_reg.h>
95 
96 #include <dev/hpc/bicons.h>
97 
98 #include "opt_ipkdb.h"
99 
100 /* XXX for consinit related hacks */
101 #include <sys/conf.h>
102 
103 #ifdef NFS
104 #include <sys/mount.h>
105 #include <nfs/rpcv2.h>
106 #include <nfs/nfsproto.h>
107 #include <nfs/nfs.h>
108 #include <nfs/nfsmount.h>
109 #endif
110 
111 /* Kernel text starts 256K in from the bottom of the kernel address space. */
112 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00040000)
113 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x00c00000)
114 #define	KERNEL_VM_SIZE		0x05000000
115 
116 /*
117  * Address to call from cpu_reset() to reset the machine.
118  * This is machine architecture dependant as it varies depending
119  * on where the ROM appears when you turn the MMU off.
120  */
121 
122 u_int cpu_reset_address = 0;
123 
124 /* Define various stack sizes in pages */
125 #define IRQ_STACK_SIZE	1
126 #define ABT_STACK_SIZE	1
127 #ifdef IPKDB
128 #define UND_STACK_SIZE	2
129 #else
130 #define UND_STACK_SIZE	1
131 #endif
132 
133 BootConfig bootconfig;		/* Boot config storage */
134 struct bootinfo *bootinfo, bootinfo_storage;
135 static char booted_kernel_storage[80];
136 char *booted_kernel = booted_kernel_storage;
137 
138 paddr_t physical_start;
139 paddr_t physical_freestart;
140 paddr_t physical_freeend;
141 paddr_t physical_end;
142 int physmem = 0;
143 
144 #ifndef PMAP_STATIC_L1S
145 int max_processes = 64;			/* Default number */
146 #endif	/* !PMAP_STATIC_L1S */
147 
148 
149 /* Physical and virtual addresses for some global pages */
150 pv_addr_t systempage;
151 pv_addr_t irqstack;
152 pv_addr_t undstack;
153 pv_addr_t abtstack;
154 pv_addr_t kernelstack;
155 
156 char *boot_args = NULL;
157 char boot_file[16];
158 
159 vaddr_t msgbufphys;
160 
161 extern u_int data_abort_handler_address;
162 extern u_int prefetch_abort_handler_address;
163 extern u_int undefined_handler_address;
164 extern int end;
165 
166 #ifdef PMAP_DEBUG
167 extern int pmap_debug_level;
168 #endif	/* PMAP_DEBUG */
169 
170 #define	KERNEL_PT_VMEM		0	/* Page table for mapping video memory */
171 #define	KERNEL_PT_SYS		1	/* Page table for mapping proc0 zero page */
172 #define	KERNEL_PT_KERNEL	2	/* Page table for mapping kernel */
173 #define	KERNEL_PT_IO		3	/* Page table for mapping IO */
174 #define	KERNEL_PT_VMDATA	4	/* Page tables for mapping kernel VM */
175 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
176 #define	NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
177 
178 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
179 
180 struct user *proc0paddr;
181 
182 #define CPU_SA110_CACHE_CLEAN_SIZE (0x4000 * 2)
183 extern unsigned int sa1_cache_clean_addr;
184 extern unsigned int sa1_cache_clean_size;
185 static vaddr_t sa1_cc_base;
186 
187 /* Non-buffered non-cachable memory needed to enter idle mode */
188 extern vaddr_t sa11x0_idle_mem;
189 
190 /* Prototypes */
191 
192 void physcon_display_base	__P((u_int addr));
193 void consinit		__P((void));
194 
195 void data_abort_handler		__P((trapframe_t *frame));
196 void prefetch_abort_handler	__P((trapframe_t *frame));
197 void undefinedinstruction_bounce	__P((trapframe_t *frame));
198 
199 u_int cpu_get_control		__P((void));
200 
201 void rpc_sa110_cc_setup(void);
202 
203 #ifdef DEBUG_BEFOREMMU
204 static void fakecninit();
205 #endif
206 
207 #ifdef BOOT_DUMP
208 void dumppages(char *, int);
209 #endif
210 
211 u_int initarm(int, char **, struct bootinfo *);
212 extern int db_trapper(u_int, u_int, trapframe_t *, int);
213 extern void dump_spl_masks	__P((void));
214 extern void dumpsys	__P((void));
215 
216 /*
217  * void cpu_reboot(int howto, char *bootstr)
218  *
219  * Reboots the system
220  *
221  * Deal with any syncing, unmounting, dumping and shutdown hooks,
222  * then reset the CPU.
223  */
224 
225 void
226 cpu_reboot(howto, bootstr)
227 	int howto;
228 	char *bootstr;
229 {
230 	/*
231 	 * If we are still cold then hit the air brakes
232 	 * and crash to earth fast
233 	 */
234 	if (cold) {
235 		doshutdownhooks();
236 		printf("Halted while still in the ICE age.\n");
237 		printf("The operating system has halted.\n");
238 		printf("Please press any key to reboot.\n\n");
239 		cngetc();
240 		printf("rebooting...\n");
241 		cpu_reset();
242 		/*NOTREACHED*/
243 	}
244 
245 	/* Disable console buffering */
246 	cnpollc(1);
247 
248 	/*
249 	 * If RB_NOSYNC was not specified sync the discs.
250 	 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
251 	 * It looks like syslogd is getting woken up only to find that it cannot
252 	 * page part of the binary in as the filesystem has been unmounted.
253 	 */
254 	if (!(howto & RB_NOSYNC))
255 		bootsync();
256 
257 	/* Say NO to interrupts */
258 	splhigh();
259 
260 	/* Do a dump if requested. */
261 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
262 		dumpsys();
263 
264 
265 	/* Run any shutdown hooks */
266 	doshutdownhooks();
267 
268 	/* Make sure IRQ's are disabled */
269 	IRQdisable;
270 
271 	if (howto & RB_HALT) {
272 		printf("The operating system has halted.\n");
273 		printf("Please press any key to reboot.\n\n");
274 		cngetc();
275 	}
276 
277 	printf("rebooting...\n");
278 	cpu_reset();
279 	/*NOTREACHED*/
280 }
281 
282 /*
283  *
284  * Initial entry point on startup. This gets called before main() is
285  * entered.
286  * It should be responsible for setting up everything that must be
287  * in place when main is called.
288  * This includes
289  *   Taking a copy of the boot configuration structure.
290  *   Initialising the physical console so characters can be printed.
291  *   Setting up page tables for the kernel
292  */
293 
294 u_int
295 initarm(argc, argv, bi)
296 	int argc;
297 	char **argv;
298 	struct bootinfo *bi;
299 {
300 	int loop;
301 	u_int kerneldatasize, symbolsize;
302 	u_int l1pagetable;
303 	vaddr_t freemempos;
304 	pv_addr_t kernel_l1pt;
305 	vsize_t pt_size;
306 #if NKSYMS || defined(DDB) || defined(LKM)
307 	Elf_Shdr *sh;
308 #endif
309 
310 	/*
311 	 * Heads up ... Setup the CPU / MMU / TLB functions
312 	 */
313 	set_cpufuncs();
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 = 8192;
330 	bootconfig.dramblocks = 1;
331 	kerneldatasize = (u_int32_t)&end - (u_int32_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 	boot_file[0] = '\0';
352 	strncpy(booted_kernel_storage, *argv, sizeof(booted_kernel_storage));
353 	for(argc--, argv++; argc; argc--, argv++)
354 		switch(**argv) {
355 		case 'a':
356 			boothowto |= RB_ASKNAME;
357 			break;
358 		case 's':
359 			boothowto |= RB_SINGLE;
360 			break;
361 		case 'b':
362 			/* boot device: -b=sd0 etc. */
363 #ifdef NFS
364 			if (strcmp(*argv + 2, "nfs") == 0)
365 				mountroot = nfs_mountroot;
366 			else
367 				strncpy(boot_file, *argv + 2,
368 				    sizeof(boot_file));
369 #else /* NFS */
370 			strncpy(boot_file, *argv + 2, sizeof(boot_file));
371 #endif /* NFS */
372 			break;
373 		default:
374 			break;
375 		}
376 
377 	/* copy bootinfo into known kernel space */
378 	bootinfo_storage = *bi;
379 	bootinfo = &bootinfo_storage;
380 
381 #ifdef BOOTINFO_FB_WIDTH
382 	bootinfo->fb_line_bytes = BOOTINFO_FB_LINE_BYTES;
383 	bootinfo->fb_width = BOOTINFO_FB_WIDTH;
384 	bootinfo->fb_height = BOOTINFO_FB_HEIGHT;
385 	bootinfo->fb_type = BOOTINFO_FB_TYPE;
386 #endif
387 
388 	/*
389 	 * hpcboot has loaded me with MMU disabled.
390 	 * So create kernel page tables and enable MMU
391 	 */
392 
393 	/*
394 	 * Set up the variables that define the availablilty of physcial
395 	 * memory
396 	 */
397 	physical_start = bootconfig.dram[0].address;
398 	physical_freestart = physical_start
399 	    + (KERNEL_TEXT_BASE - KERNEL_BASE) + kerneldatasize;
400 	physical_end = bootconfig.dram[bootconfig.dramblocks - 1].address
401 	    + bootconfig.dram[bootconfig.dramblocks - 1].pages * PAGE_SIZE;
402 	physical_freeend = physical_end;
403 
404 	for (loop = 0; loop < bootconfig.dramblocks; ++loop)
405 		physmem += bootconfig.dram[loop].pages;
406 
407 	/* XXX handle UMA framebuffer memory */
408 
409 	/* Use the first 256kB to allocate things */
410 	freemempos = KERNEL_BASE;
411 	memset((void *)KERNEL_BASE, 0, KERNEL_TEXT_BASE - KERNEL_BASE);
412 
413 	/*
414 	 * Right We have the bottom meg of memory mapped to 0x00000000
415 	 * so was can get at it. The kernel will ocupy the start of it.
416 	 * After the kernel/args we allocate some of the fixed page tables
417 	 * we need to get the system going.
418 	 * We allocate one page directory and 8 page tables and store the
419 	 * physical addresses in the kernel_pt_table array.
420 	 * Must remember that neither the page L1 or L2 page tables are the
421 	 * same size as a page !
422 	 *
423 	 * Ok the next bit of physical allocate may look complex but it is
424 	 * simple really. I have done it like this so that no memory gets
425 	 * wasted during the allocate of various pages and tables that are
426 	 * all different sizes.
427 	 * The start address will be page aligned.
428 	 * We allocate the kernel page directory on the first free 16KB
429 	 * boundry we find.
430 	 * We allocate the kernel page tables on the first 1KB boundry we find.
431 	 * We allocate 9 PT's. This means that in the process we
432 	 * KNOW that we will encounter at least 1 16KB boundry.
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 consturction 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 	pmap_link_l2pt(l1pagetable, KERNEL_BASE,
529 	    &kernel_pt_table[KERNEL_PT_KERNEL]);
530 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
531 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
532 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
533 
534 	/* update the top of the kernel VM */
535 	pmap_curmaxkvaddr =
536 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
537 #define SAIPIO_BASE		0xd0000000		/* XXX XXX */
538 	pmap_link_l2pt(l1pagetable, SAIPIO_BASE,
539 	    &kernel_pt_table[KERNEL_PT_IO]);
540 
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 any I/O modules here, as we don't have real bus_space_map() */
597 	printf("mapping IO...");
598 	pmap_map_entry(l1pagetable, SACOM3_BASE, SACOM3_HW_BASE,
599 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
600 
601 	pmap_map_chunk(l1pagetable, sa1_cache_clean_addr, 0xe0000000,
602 	    CPU_SA110_CACHE_CLEAN_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
603 	/*
604 	 * Now we have the real page tables in place so we can switch to them.
605 	 * Once this is done we will be running with the REAL kernel page
606 	 * tables.
607 	 */
608 
609 	printf("done.\n");
610 
611 	/*
612 	 * Pages were allocated during the secondary bootstrap for the
613 	 * stacks for different CPU modes.
614 	 * We must now set the r13 registers in the different CPU modes to
615 	 * point to these stacks.
616 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
617 	 * of the stack memory.
618 	 */
619 	printf("init subsystems: stacks ");
620 
621 	set_stackptr(PSR_IRQ32_MODE,
622 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
623 	set_stackptr(PSR_ABT32_MODE,
624 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
625 	set_stackptr(PSR_UND32_MODE,
626 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
627 #ifdef PMAP_DEBUG
628 	if (pmap_debug_level >= 0)
629 		printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
630 		    kernelstack.pv_pa);
631 #endif	/* PMAP_DEBUG */
632 
633 	/*
634 	 * Well we should set a data abort handler.
635 	 * Once things get going this will change as we will need a proper
636 	 * handler. Until then we will use a handler that just panics but
637 	 * tells us why.
638 	 * Initialisation of the vectors will just panic on a data abort.
639 	 * This just fills in a slighly better one.
640 	 */
641 	printf("vectors ");
642 	data_abort_handler_address = (u_int)data_abort_handler;
643 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
644 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
645 	printf("%08x %08x %08x\n", data_abort_handler_address,
646 	    prefetch_abort_handler_address, undefined_handler_address);
647 
648 	/* Initialise the undefined instruction handlers */
649 	printf("undefined ");
650 	undefined_init();
651 
652 	/* Set the page table address. */
653 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
654 	setttb(kernel_l1pt.pv_pa);
655 	cpu_tlb_flushID();
656 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
657 
658 	/*
659 	 * Moved from cpu_startup() as data_abort_handler() references
660 	 * this during uvm init
661 	 */
662 	proc0paddr = (struct user *)kernelstack.pv_va;
663 	lwp0.l_addr = proc0paddr;
664 
665 #ifdef BOOT_DUMP
666 	dumppages((char *)0xc0000000, 16 * PAGE_SIZE);
667 	dumppages((char *)0xb0100000, 64); /* XXX */
668 #endif
669 	/* Enable MMU, I-cache, D-cache, write buffer. */
670 	cpufunc_control(0x337f, 0x107d);
671 
672 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
673 
674 	consinit();
675 
676 #ifdef VERBOSE_INIT_ARM
677 	printf("freemempos=%08lx\n", freemempos);
678 	printf("MMU enabled. control=%08x\n", cpu_get_control());
679 #endif
680 
681 	/* Load memory into UVM. */
682 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
683 	for (loop = 0; loop < bootconfig.dramblocks; loop++) {
684 		paddr_t start = (paddr_t)bootconfig.dram[loop].address;
685 		paddr_t end = start + (bootconfig.dram[loop].pages * PAGE_SIZE);
686 
687 		if (start < physical_freestart)
688 			start = physical_freestart;
689 		if (end > physical_freeend)
690 			end = physical_freeend;
691 
692 		uvm_page_physload(atop(start), atop(end),
693 		    atop(start), atop(end), VM_FREELIST_DEFAULT);
694 	}
695 
696 	/* Boot strap pmap telling it where the kernel page table is */
697 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
698 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
699 
700 	if (cputype == CPU_ID_SA110)
701 		rpc_sa110_cc_setup();
702 
703 #ifdef IPKDB
704 	/* Initialise ipkdb */
705 	ipkdb_init();
706 	if (boothowto & RB_KDB)
707 		ipkdb_connect(0);
708 #endif	/* NIPKDB */
709 
710 #ifdef BOOT_DUMP
711 	dumppages((char *)kernel_l1pt.pv_va, 16);
712 	dumppages((char *)PTE_BASE, 16);
713 #endif
714 
715 #ifdef DDB
716 	db_machine_init();
717 #endif
718 #if NKSYMS || defined(DDB) || defined(LKM)
719 	ksyms_init(symbolsize, ((int *)&end), ((char *)&end) + symbolsize);
720 #endif
721 
722 	printf("kernsize=0x%x", kerneldatasize);
723 	printf(" (including 0x%x symbols)\n", symbolsize);
724 
725 #ifdef DDB
726 	if (boothowto & RB_KDB)
727 		Debugger();
728 #endif	/* DDB */
729 
730 	if (bootinfo->magic == BOOTINFO_MAGIC) {
731 		platid.dw.dw0 = bootinfo->platid_cpu;
732 		platid.dw.dw1 = bootinfo->platid_machine;
733 	}
734 
735 	/* We return the new stack pointer address */
736 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
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 void
761 fakecninit()
762 {
763 	static struct consdev fakecntab = cons_init(sacom);
764 	cn_tab = &fakecntab;
765 
766 	(*cn_tab->cn_init)(0);
767 	cn_tab->cn_pri = CN_REMOTE;
768 }
769 #endif
770 
771 
772 /*
773  * For optimal cache cleaning we need two 16K banks of
774  * virtual address space that NOTHING else will access
775  * and then we alternate the cache cleaning between the
776  * two banks.
777  * The cache cleaning code requires requires 2 banks aligned
778  * on total size boundry so the banks can be alternated by
779  * eorring the size bit (assumes the bank size is a power of 2)
780  */
781 void
782 rpc_sa110_cc_setup(void)
783 {
784 	int loop;
785 	paddr_t kaddr;
786 	pt_entry_t *pte;
787 
788 	(void) pmap_extract(pmap_kernel(), KERNEL_TEXT_BASE, &kaddr);
789 	for (loop = 0; loop < CPU_SA110_CACHE_CLEAN_SIZE; loop += PAGE_SIZE) {
790 		pte = vtopte(sa1_cc_base + loop);
791 		*pte = L2_S_PROTO | kaddr |
792 		    L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
793 		PTE_SYNC(pte);
794 	}
795 	sa1_cache_clean_addr = sa1_cc_base;
796 	sa1_cache_clean_size = CPU_SA110_CACHE_CLEAN_SIZE / 2;
797 }
798 
799 #ifdef BOOT_DUMP
800 void dumppages(char *start, int nbytes)
801 {
802 	char *p = start;
803 	char *p1;
804 	int i;
805 
806 	for(i = nbytes; i > 0; i -= 16, p += 16) {
807 		for(p1 = p + 15; p != p1; p1--) {
808 			if (*p1)
809 				break;
810 		}
811 		if (! *p1)
812 			continue;
813 		printf("%08x %02x %02x %02x %02x %02x %02x %02x %02x"
814 		    " %02x %02x %02x %02x %02x %02x %02x %02x\n",
815 		    (unsigned int)p,
816 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
817 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
818 	}
819 }
820 #endif
821 
822 /* End of machdep.c */
823