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