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