xref: /netbsd-src/sys/arch/hpcarm/hpcarm/hpc_machdep.c (revision f648d12d47727113ad5330b0753bb2f2ef8e1045)
1 /*	$NetBSD: hpc_machdep.c,v 1.70 2003/09/16 08:18:22 agc 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.70 2003/09/16 08:18:22 agc 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 int db_trapper(u_int, u_int, trapframe_t *, int);
216 extern void dump_spl_masks	__P((void));
217 extern void dumpsys	__P((void));
218 
219 /*
220  * void cpu_reboot(int howto, char *bootstr)
221  *
222  * Reboots the system
223  *
224  * Deal with any syncing, unmounting, dumping and shutdown hooks,
225  * then reset the CPU.
226  */
227 
228 void
229 cpu_reboot(howto, bootstr)
230 	int howto;
231 	char *bootstr;
232 {
233 	/*
234 	 * If we are still cold then hit the air brakes
235 	 * and crash to earth fast
236 	 */
237 	if (cold) {
238 		doshutdownhooks();
239 		printf("Halted while still in the ICE age.\n");
240 		printf("The operating system has halted.\n");
241 		printf("Please press any key to reboot.\n\n");
242 		cngetc();
243 		printf("rebooting...\n");
244 		cpu_reset();
245 		/*NOTREACHED*/
246 	}
247 
248 	/* Disable console buffering */
249 	cnpollc(1);
250 
251 	/*
252 	 * If RB_NOSYNC was not specified sync the discs.
253 	 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
254 	 * It looks like syslogd is getting woken up only to find that it cannot
255 	 * page part of the binary in as the filesystem has been unmounted.
256 	 */
257 	if (!(howto & RB_NOSYNC))
258 		bootsync();
259 
260 	/* Say NO to interrupts */
261 	splhigh();
262 
263 	/* Do a dump if requested. */
264 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
265 		dumpsys();
266 
267 
268 	/* Run any shutdown hooks */
269 	doshutdownhooks();
270 
271 	/* Make sure IRQ's are disabled */
272 	IRQdisable;
273 
274 	if (howto & RB_HALT) {
275 		printf("The operating system has halted.\n");
276 		printf("Please press any key to reboot.\n\n");
277 		cngetc();
278 	}
279 
280 	printf("rebooting...\n");
281 	cpu_reset();
282 	/*NOTREACHED*/
283 }
284 
285 /* Number of DRAM pages which are installed */
286 /* Units are 4K pages, so 8192 is 32 MB of memory */
287 #ifndef DRAM_PAGES
288 #define DRAM_PAGES	8192
289 #endif
290 
291 /*
292  *
293  * Initial entry point on startup. This gets called before main() is
294  * entered.
295  * It should be responsible for setting up everything that must be
296  * in place when main is called.
297  * This includes
298  *   Taking a copy of the boot configuration structure.
299  *   Initialising the physical console so characters can be printed.
300  *   Setting up page tables for the kernel
301  */
302 
303 u_int
304 initarm(argc, argv, bi)
305 	int argc;
306 	char **argv;
307 	struct bootinfo *bi;
308 {
309 	int loop;
310 	u_int kerneldatasize, symbolsize;
311 	u_int l1pagetable;
312 	vaddr_t freemempos;
313 	pv_addr_t kernel_l1pt;
314 	vsize_t pt_size;
315 #if NKSYMS || defined(DDB) || defined(LKM)
316 	Elf_Shdr *sh;
317 #endif
318 
319 	/*
320 	 * Heads up ... Setup the CPU / MMU / TLB functions
321 	 */
322 	set_cpufuncs();
323 
324 #ifdef DEBUG_BEFOREMMU
325 	/*
326 	 * At this point, we cannot call real consinit().
327 	 * Just call a faked up version of consinit(), which does the thing
328 	 * with MMU disabled.
329 	 */
330 	fakecninit();
331 #endif
332 
333 	/*
334 	 * XXX for now, overwrite bootconfig to hardcoded values.
335 	 * XXX kill bootconfig and directly call uvm_physload
336 	 */
337 	bootconfig.dram[0].address = 0xc0000000;
338 	bootconfig.dram[0].pages = DRAM_PAGES;
339 	bootconfig.dramblocks = 1;
340 	kerneldatasize = (u_int32_t)&end - (u_int32_t)KERNEL_TEXT_BASE;
341 
342 	symbolsize = 0;
343 #if NKSYMS || defined(DDB) || defined(LKM)
344 	if (! memcmp(&end, "\177ELF", 4)) {
345 		sh = (Elf_Shdr *)((char *)&end + ((Elf_Ehdr *)&end)->e_shoff);
346 		loop = ((Elf_Ehdr *)&end)->e_shnum;
347 		for(; loop; loop--, sh++)
348 			if (sh->sh_offset > 0 &&
349 			    (sh->sh_offset + sh->sh_size) > symbolsize)
350 				symbolsize = sh->sh_offset + sh->sh_size;
351 	}
352 #endif
353 
354 	printf("kernsize=0x%x\n", kerneldatasize);
355 	kerneldatasize += symbolsize;
356 	kerneldatasize = ((kerneldatasize - 1) & ~(PAGE_SIZE * 4 - 1)) +
357 	    PAGE_SIZE * 8;
358 
359 	/* parse kernel args */
360 	boot_file[0] = '\0';
361 	strncpy(booted_kernel_storage, *argv, sizeof(booted_kernel_storage));
362 	for(argc--, argv++; argc; argc--, argv++)
363 		switch(**argv) {
364 		case 'a':
365 			boothowto |= RB_ASKNAME;
366 			break;
367 		case 's':
368 			boothowto |= RB_SINGLE;
369 			break;
370 		case 'b':
371 			/* boot device: -b=sd0 etc. */
372 #ifdef NFS
373 			if (strcmp(*argv + 2, "nfs") == 0)
374 				mountroot = nfs_mountroot;
375 			else
376 				strncpy(boot_file, *argv + 2,
377 				    sizeof(boot_file));
378 #else /* NFS */
379 			strncpy(boot_file, *argv + 2, sizeof(boot_file));
380 #endif /* NFS */
381 			break;
382 		default:
383 			break;
384 		}
385 
386 	/* copy bootinfo into known kernel space */
387 	bootinfo_storage = *bi;
388 	bootinfo = &bootinfo_storage;
389 
390 #ifdef BOOTINFO_FB_WIDTH
391 	bootinfo->fb_line_bytes = BOOTINFO_FB_LINE_BYTES;
392 	bootinfo->fb_width = BOOTINFO_FB_WIDTH;
393 	bootinfo->fb_height = BOOTINFO_FB_HEIGHT;
394 	bootinfo->fb_type = BOOTINFO_FB_TYPE;
395 #endif
396 
397 	/*
398 	 * hpcboot has loaded me with MMU disabled.
399 	 * So create kernel page tables and enable MMU
400 	 */
401 
402 	/*
403 	 * Set up the variables that define the availablilty of physcial
404 	 * memory
405 	 */
406 	physical_start = bootconfig.dram[0].address;
407 	physical_freestart = physical_start
408 	    + (KERNEL_TEXT_BASE - KERNEL_BASE) + kerneldatasize;
409 	physical_end = bootconfig.dram[bootconfig.dramblocks - 1].address
410 	    + bootconfig.dram[bootconfig.dramblocks - 1].pages * PAGE_SIZE;
411 	physical_freeend = physical_end;
412 
413 	for (loop = 0; loop < bootconfig.dramblocks; ++loop)
414 		physmem += bootconfig.dram[loop].pages;
415 
416 	/* XXX handle UMA framebuffer memory */
417 
418 	/* Use the first 256kB to allocate things */
419 	freemempos = KERNEL_BASE;
420 	memset((void *)KERNEL_BASE, 0, KERNEL_TEXT_BASE - KERNEL_BASE);
421 
422 	/*
423 	 * Right We have the bottom meg of memory mapped to 0x00000000
424 	 * so was can get at it. The kernel will ocupy the start of it.
425 	 * After the kernel/args we allocate some of the fixed page tables
426 	 * we need to get the system going.
427 	 * We allocate one page directory and 8 page tables and store the
428 	 * physical addresses in the kernel_pt_table array.
429 	 * Must remember that neither the page L1 or L2 page tables are the
430 	 * same size as a page !
431 	 *
432 	 * Ok the next bit of physical allocate may look complex but it is
433 	 * simple really. I have done it like this so that no memory gets
434 	 * wasted during the allocate of various pages and tables that are
435 	 * all different sizes.
436 	 * The start address will be page aligned.
437 	 * We allocate the kernel page directory on the first free 16KB
438 	 * boundry we find.
439 	 * We allocate the kernel page tables on the first 1KB boundry we find.
440 	 * We allocate 9 PT's. This means that in the process we
441 	 * KNOW that we will encounter at least 1 16KB boundry.
442 	 *
443 	 * Eventually if the top end of the memory gets used for process L1
444 	 * page tables the kernel L1 page table may be moved up there.
445 	 */
446 
447 #ifdef VERBOSE_INIT_ARM
448 	printf("Allocating page tables\n");
449 #endif
450 
451 	/* Define a macro to simplify memory allocation */
452 #define	valloc_pages(var, np)			\
453 	(var).pv_pa = (var).pv_va = freemempos;	\
454 	freemempos += (np) * PAGE_SIZE;
455 #define	alloc_pages(var, np)			\
456 	(var) = freemempos;			\
457 	freemempos += (np) * PAGE_SIZE;
458 
459 
460 	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
461 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
462 		alloc_pages(kernel_pt_table[loop].pv_pa,
463 		    L2_TABLE_SIZE / PAGE_SIZE);
464 		kernel_pt_table[loop].pv_va = kernel_pt_table[loop].pv_pa;
465 	}
466 
467 	/*
468 	 * Allocate a page for the system page mapped to V0x00000000
469 	 * This page will just contain the system vectors and can be
470 	 * shared by all processes.
471 	 */
472 	valloc_pages(systempage, 1);
473 
474 	pt_size = round_page(freemempos) - KERNEL_BASE;
475 
476 	/* Allocate stacks for all modes */
477 	valloc_pages(irqstack, IRQ_STACK_SIZE);
478 	valloc_pages(abtstack, ABT_STACK_SIZE);
479 	valloc_pages(undstack, UND_STACK_SIZE);
480 	valloc_pages(kernelstack, UPAGES);
481 
482 #ifdef VERBOSE_INIT_ARM
483 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
484 	    irqstack.pv_va);
485 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
486 	    abtstack.pv_va);
487 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
488 	    undstack.pv_va);
489 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
490 	    kernelstack.pv_va);
491 #endif
492 
493 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
494 
495 	/*
496 	 * XXX Actually, we only need virtual space and don't need
497 	 * XXX physical memory for sa110_cc_base and sa11x0_idle_mem.
498 	 */
499 	/*
500 	 * XXX totally stuffed hack to work round problems introduced
501 	 * in recent versions of the pmap code. Due to the calls used there
502 	 * we cannot allocate virtual memory during bootstrap.
503 	 */
504 	for(;;) {
505 		alloc_pages(sa1_cc_base, 1);
506 		if (! (sa1_cc_base & (CPU_SA110_CACHE_CLEAN_SIZE - 1)))
507 			break;
508 	}
509 	{
510 		vaddr_t dummy;
511 		alloc_pages(dummy, CPU_SA110_CACHE_CLEAN_SIZE / PAGE_SIZE - 1);
512 	}
513 	sa1_cache_clean_addr = sa1_cc_base;
514 	sa1_cache_clean_size = CPU_SA110_CACHE_CLEAN_SIZE / 2;
515 
516 	alloc_pages(sa11x0_idle_mem, 1);
517 
518 	/*
519 	 * Ok we have allocated physical pages for the primary kernel
520 	 * page tables
521 	 */
522 
523 #ifdef VERBOSE_INIT_ARM
524 	printf("Creating L1 page table\n");
525 #endif
526 
527 	/*
528 	 * Now we start consturction of the L1 page table
529 	 * We start by mapping the L2 page tables into the L1.
530 	 * This means that we can replace L1 mappings later on if necessary
531 	 */
532 	l1pagetable = kernel_l1pt.pv_pa;
533 
534 	/* Map the L2 pages tables in the L1 page table */
535 	pmap_link_l2pt(l1pagetable, 0x00000000,
536 	    &kernel_pt_table[KERNEL_PT_SYS]);
537 	pmap_link_l2pt(l1pagetable, KERNEL_BASE,
538 	    &kernel_pt_table[KERNEL_PT_KERNEL]);
539 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
540 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
541 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
542 
543 	/* update the top of the kernel VM */
544 	pmap_curmaxkvaddr =
545 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
546 #define SAIPIO_BASE		0xd0000000		/* XXX XXX */
547 	pmap_link_l2pt(l1pagetable, SAIPIO_BASE,
548 	    &kernel_pt_table[KERNEL_PT_IO]);
549 
550 
551 #ifdef VERBOSE_INIT_ARM
552 	printf("Mapping kernel\n");
553 #endif
554 
555 	/* Now we fill in the L2 pagetable for the kernel code/data */
556 
557 	/*
558 	 * XXX there is no ELF header to find RO region.
559 	 * XXX What should we do?
560 	 */
561 #if 0
562 	if (N_GETMAGIC(kernexec[0]) == ZMAGIC) {
563 		logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
564 		    physical_start, kernexec->a_text,
565 		    VM_PROT_READ, PTE_CACHE);
566 		logical += pmap_map_chunk(l1pagetable,
567 		    KERNEL_TEXT_BASE + logical, physical_start + logical,
568 		    kerneldatasize - kernexec->a_text,
569 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
570 	} else
571 #endif
572 		pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
573 		    KERNEL_TEXT_BASE, kerneldatasize,
574 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
575 
576 #ifdef VERBOSE_INIT_ARM
577 	printf("Constructing L2 page tables\n");
578 #endif
579 
580 	/* Map the stack pages */
581 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
582 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
583 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
584 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
585 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
586 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
587 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
588 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
589 
590 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
591 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
592 
593 	/* Map page tables */
594 	pmap_map_chunk(l1pagetable, KERNEL_BASE, KERNEL_BASE, pt_size,
595 	    VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
596 
597 	/* Map a page for entering idle mode */
598 	pmap_map_entry(l1pagetable, sa11x0_idle_mem, sa11x0_idle_mem,
599 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
600 
601 	/* Map the vector page. */
602 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
603 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
604 
605 	/* Map any I/O modules here, as we don't have real bus_space_map() */
606 	printf("mapping IO...");
607 	pmap_map_entry(l1pagetable, SACOM3_BASE, SACOM3_HW_BASE,
608 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
609 
610 	pmap_map_chunk(l1pagetable, sa1_cache_clean_addr, 0xe0000000,
611 	    CPU_SA110_CACHE_CLEAN_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
612 	/*
613 	 * Now we have the real page tables in place so we can switch to them.
614 	 * Once this is done we will be running with the REAL kernel page
615 	 * tables.
616 	 */
617 
618 	printf("done.\n");
619 
620 	/*
621 	 * Pages were allocated during the secondary bootstrap for the
622 	 * stacks for different CPU modes.
623 	 * We must now set the r13 registers in the different CPU modes to
624 	 * point to these stacks.
625 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
626 	 * of the stack memory.
627 	 */
628 	printf("init subsystems: stacks ");
629 
630 	set_stackptr(PSR_IRQ32_MODE,
631 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
632 	set_stackptr(PSR_ABT32_MODE,
633 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
634 	set_stackptr(PSR_UND32_MODE,
635 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
636 #ifdef PMAP_DEBUG
637 	if (pmap_debug_level >= 0)
638 		printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
639 		    kernelstack.pv_pa);
640 #endif	/* PMAP_DEBUG */
641 
642 	/*
643 	 * Well we should set a data abort handler.
644 	 * Once things get going this will change as we will need a proper
645 	 * handler. Until then we will use a handler that just panics but
646 	 * tells us why.
647 	 * Initialisation of the vectors will just panic on a data abort.
648 	 * This just fills in a slighly better one.
649 	 */
650 	printf("vectors ");
651 	data_abort_handler_address = (u_int)data_abort_handler;
652 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
653 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
654 	printf("%08x %08x %08x\n", data_abort_handler_address,
655 	    prefetch_abort_handler_address, undefined_handler_address);
656 
657 	/* Initialise the undefined instruction handlers */
658 	printf("undefined ");
659 	undefined_init();
660 
661 	/* Set the page table address. */
662 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
663 	setttb(kernel_l1pt.pv_pa);
664 	cpu_tlb_flushID();
665 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
666 
667 	/*
668 	 * Moved from cpu_startup() as data_abort_handler() references
669 	 * this during uvm init
670 	 */
671 	proc0paddr = (struct user *)kernelstack.pv_va;
672 	lwp0.l_addr = proc0paddr;
673 
674 #ifdef BOOT_DUMP
675 	dumppages((char *)0xc0000000, 16 * PAGE_SIZE);
676 	dumppages((char *)0xb0100000, 64); /* XXX */
677 #endif
678 	/* Enable MMU, I-cache, D-cache, write buffer. */
679 	cpufunc_control(0x337f, 0x107d);
680 
681 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
682 
683 	consinit();
684 
685 #ifdef VERBOSE_INIT_ARM
686 	printf("freemempos=%08lx\n", freemempos);
687 	printf("MMU enabled. control=%08x\n", cpu_get_control());
688 #endif
689 
690 	/* Load memory into UVM. */
691 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
692 	for (loop = 0; loop < bootconfig.dramblocks; loop++) {
693 		paddr_t start = (paddr_t)bootconfig.dram[loop].address;
694 		paddr_t end = start + (bootconfig.dram[loop].pages * PAGE_SIZE);
695 
696 		if (start < physical_freestart)
697 			start = physical_freestart;
698 		if (end > physical_freeend)
699 			end = physical_freeend;
700 
701 		uvm_page_physload(atop(start), atop(end),
702 		    atop(start), atop(end), VM_FREELIST_DEFAULT);
703 	}
704 
705 	/* Boot strap pmap telling it where the kernel page table is */
706 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
707 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
708 
709 	if (cputype == CPU_ID_SA110)
710 		rpc_sa110_cc_setup();
711 
712 #ifdef IPKDB
713 	/* Initialise ipkdb */
714 	ipkdb_init();
715 	if (boothowto & RB_KDB)
716 		ipkdb_connect(0);
717 #endif	/* NIPKDB */
718 
719 #ifdef BOOT_DUMP
720 	dumppages((char *)kernel_l1pt.pv_va, 16);
721 	dumppages((char *)PTE_BASE, 16);
722 #endif
723 
724 #ifdef DDB
725 	db_machine_init();
726 #endif
727 #if NKSYMS || defined(DDB) || defined(LKM)
728 	ksyms_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 += PAGE_SIZE) {
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