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