1 /*	$NetBSD: ixm1200_machdep.c,v 1.11 2002/12/08 13:22:32 ichiro Exp $ */
2 #undef DEBUG_BEFOREMMU
3 /*
4  * Copyright (c) 2002
5  *	Ichiro FUKUHARA <ichiro@ichiro.org>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Ichiro FUKUHARA.
19  * 4. The name of the company nor the name of the author may be used to
20  *    endorse or promote products derived from this software without specific
21  *    prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 /*
36  * Copyright (c) 1997,1998 Mark Brinicombe.
37  * Copyright (c) 1997,1998 Causality Limited.
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *      This product includes software developed by Mark Brinicombe
51  *      for the NetBSD Project.
52  * 4. The name of the company nor the name of the author may be used to
53  *    endorse or promote products derived from this software without specific
54  *    prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
57  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
58  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
59  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
60  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
61  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
62  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66  * SUCH DAMAGE.
67  */
68 
69 #include "opt_ddb.h"
70 #include "opt_pmap_debug.h"
71 
72 #include <sys/param.h>
73 #include <sys/device.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/exec.h>
77 #include <sys/proc.h>
78 #include <sys/msgbuf.h>
79 #include <sys/reboot.h>
80 #include <sys/termios.h>
81 
82 #include <dev/cons.h>
83 
84 #ifdef DDB
85 #include <machine/db_machdep.h>
86 #include <ddb/db_sym.h>
87 #include <ddb/db_extern.h>
88 #ifndef DB_ELFSIZE
89 #error Must define DB_ELFSIZE!
90 #endif
91 #define ELFSIZE	DB_ELFSIZE
92 #include <sys/exec_elf.h>
93 #endif
94 
95 #include <machine/bootconfig.h>
96 #include <machine/bus.h>
97 #include <machine/cpu.h>
98 #include <machine/frame.h>
99 #include <arm/undefined.h>
100 
101 #include <arm/arm32/machdep.h>
102 
103 #include <arm/ixp12x0/ixp12x0reg.h>
104 #include <arm/ixp12x0/ixp12x0var.h>
105 #include <arm/ixp12x0/ixp12x0_comreg.h>
106 #include <arm/ixp12x0/ixp12x0_comvar.h>
107 #include <arm/ixp12x0/ixp12x0_pcireg.h>
108 
109 #include <evbarm/ixm1200/ixm1200reg.h>
110 #include <evbarm/ixm1200/ixm1200var.h>
111 
112 #include "opt_ipkdb.h"
113 
114 /* XXX for consinit related hacks */
115 #include <sys/conf.h>
116 
117 void ixp12x0_reset(void) __attribute__((noreturn));
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 = (u_int) ixp12x0_reset;
126 
127 /*
128  * Define the default console speed for the board.
129  */
130 #ifndef CONMODE
131 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB)) | CS8) /* 8N1 */
132 #endif
133 #ifndef CONSPEED
134 #define CONSPEED B38400
135 #endif
136 #ifndef CONADDR
137 #define CONADDR IXPCOM_UART_BASE
138 #endif
139 
140 cons_decl(com);
141 cons_decl(ixpcom);
142 
143 struct consdev constab[] = {
144 #if (NIXPCOM > 0)
145 	cons_init(ixpcom),
146 #endif
147 	{ 0 },
148 };
149 
150 /* Define various stack sizes in pages */
151 #define IRQ_STACK_SIZE  1
152 #define ABT_STACK_SIZE  1
153 #ifdef IPKDB
154 #define UND_STACK_SIZE  2
155 #else
156 #define UND_STACK_SIZE  1
157 #endif
158 
159 BootConfig bootconfig;          /* Boot config storage */
160 char *boot_args = NULL;
161 char *boot_file = NULL;
162 
163 vm_offset_t physical_start;
164 vm_offset_t physical_freestart;
165 vm_offset_t physical_freeend;
166 vm_offset_t physical_end;
167 u_int free_pages;
168 vm_offset_t pagetables_start;
169 int physmem = 0;
170 
171 /*int debug_flags;*/
172 #ifndef PMAP_STATIC_L1S
173 int max_processes = 64;                 /* Default number */
174 #endif  /* !PMAP_STATIC_L1S */
175 
176 /* Physical and virtual addresses for some global pages */
177 pv_addr_t systempage;
178 pv_addr_t irqstack;
179 pv_addr_t undstack;
180 pv_addr_t abtstack;
181 pv_addr_t kernelstack;
182 
183 vm_offset_t msgbufphys;
184 
185 extern u_int data_abort_handler_address;
186 extern u_int prefetch_abort_handler_address;
187 extern u_int undefined_handler_address;
188 extern int end;
189 
190 #ifdef PMAP_DEBUG
191 extern int pmap_debug_level;
192 #endif  /* PMAP_DEBUG */
193 
194 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
195 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
196 #define KERNEL_PT_KERNEL_NUM	2
197 #define KERNEL_PT_IO		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
198 					/* Page table for mapping IO */
199 #define KERNEL_PT_VMDATA	(KERNEL_PT_IO + 1)
200 					/* Page tables for mapping kernel VM */
201 #define KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
202 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
203 
204 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
205 
206 struct user *proc0paddr;
207 
208 #ifdef CPU_IXP12X0
209 #define CPU_IXP12X0_CACHE_CLEAN_SIZE (0x4000 * 2)
210 extern unsigned int ixp12x0_cache_clean_addr;
211 extern unsigned int ixp12x0_cache_clean_size;
212 static vaddr_t ixp12x0_cc_base;
213 #endif  /* CPU_IXP12X0 */
214 
215 /* Prototypes */
216 
217 void consinit		__P((void));
218 u_int cpu_get_control	__P((void));
219 
220 void ixdp_ixp12x0_cc_setup(void);
221 
222 #ifdef DEBUG_BEFOREMMU
223 static void fakecninit();
224 #endif
225 
226 extern int db_trapper(u_int, u_int, trapframe_t *, int);
227 
228 /*
229  * void cpu_reboot(int howto, char *bootstr)
230  *
231  * Reboots the system
232  *
233  * Deal with any syncing, unmounting, dumping and shutdown hooks,
234  * then reset the CPU.
235  */
236 
237 void
238 cpu_reboot(howto, bootstr)
239 	int howto;
240 	char *bootstr;
241 {
242 	/*
243 	 * If we are still cold then hit the air brakes
244 	 * and crash to earth fast
245 	 */
246 	if (cold) {
247 		doshutdownhooks();
248 		printf("Halted while still in the ICE age.\n");
249 		printf("The operating system has halted.\n");
250 		printf("Please press any key to reboot.\n\n");
251 		cngetc();
252 		printf("rebooting...\n");
253 		ixp12x0_reset();
254 	}
255 
256 	/* Disable console buffering */
257 	cnpollc(1);
258 
259 	/*
260 	 * If RB_NOSYNC was not specified sync the discs.
261 	 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
262 	 * It looks like syslogd is getting woken up only to find that it cannot
263 	 * page part of the binary in as the filesystem has been unmounted.
264 	 */
265 	if (!(howto & RB_NOSYNC))
266 		bootsync();
267 
268 	/* Say NO to interrupts */
269 	splhigh();
270 
271 	/* Do a dump if requested. */
272 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
273 		dumpsys();
274 
275 	/* Run any shutdown hooks */
276 	doshutdownhooks();
277 
278 	/* Make sure IRQ's are disabled */
279 	IRQdisable;
280 
281 	if (howto & RB_HALT) {
282 		printf("The operating system has halted.\n");
283 		printf("Please press any key to reboot.\n\n");
284 		cngetc();
285 	}
286 
287 	printf("rebooting...\n");
288 
289 	/* all interrupts are disabled */
290 	disable_interrupts(I32_bit);
291 
292 	ixp12x0_reset();
293 
294 	/* ...and if that didn't work, just croak. */
295 	printf("RESET FAILED!\n");
296 	for (;;);
297 }
298 
299 /*
300  * Initial entry point on startup. This gets called before main() is
301  * entered.
302  * It should be responsible for setting up everything that must be
303  * in place when main is called.
304  * This includes
305  *   Taking a copy of the boot configuration structure.
306  *   Initialising the physical console so characters can be printed.
307  *   Setting up page tables for the kernel
308  *   Relocating the kernel to the bottom of physical memory
309  */
310 u_int
311 initarm(void *arg)
312 {
313         int loop;
314 	int loop1;
315 	u_int kerneldatasize, symbolsize;
316 	vaddr_t l1pagetable;
317 	vaddr_t freemempos;
318 	pv_addr_t kernel_l1pt;
319 	pv_addr_t kernel_ptpt;
320 #ifdef DDB
321         Elf_Shdr *sh;
322 #endif
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          * Since we map v0xf0000000 == p0x90000000, it's possible for
334          * us to initialize the console now.
335          */
336 	consinit();
337 
338 	/* Talk to the user */
339 	printf("\nNetBSD/evbarm (IXM1200) booting ...\n");
340 
341 	/*
342 	 * Heads up ... Setup the CPU / MMU / TLB functions
343 	 */
344 	if (set_cpufuncs())
345 		panic("cpu not recognized!");
346 
347 	/* XXX overwrite bootconfig to hardcoded values */
348 	bootconfig.dram[0].address = 0xc0000000;
349 	bootconfig.dram[0].pages   = 0x10000000 / NBPG;	/* SDRAM 256MB */
350 	bootconfig.dramblocks = 1;
351 
352 	kerneldatasize = (u_int32_t)&end - (u_int32_t)KERNEL_TEXT_BASE;
353 
354 	symbolsize = 0;
355 
356 #ifdef PMAP_DEBUG
357 	pmap_debug(-1);
358 #endif
359 
360 #ifdef DDB
361         if (! memcmp(&end, "\177ELF", 4)) {
362                 sh = (Elf_Shdr *)((char *)&end + ((Elf_Ehdr *)&end)->e_shoff);
363                 loop = ((Elf_Ehdr *)&end)->e_shnum;
364                 for(; loop; loop--, sh++)
365                         if (sh->sh_offset > 0 &&
366                             (sh->sh_offset + sh->sh_size) > symbolsize)
367                                 symbolsize = sh->sh_offset + sh->sh_size;
368         }
369 #endif
370 	printf("kernsize=0x%x\n", kerneldatasize);
371 	kerneldatasize += symbolsize;
372 	kerneldatasize = ((kerneldatasize - 1) & ~(NBPG * 4 - 1)) + NBPG * 8;
373 
374 	/*
375 	 * Set up the variables that define the availablilty of physcial
376 	 * memory
377 	 */
378 	physical_start = bootconfig.dram[0].address;
379 	physical_end = physical_start + (bootconfig.dram[0].pages * NBPG);
380 
381 	physical_freestart = physical_start
382 		+ (KERNEL_TEXT_BASE - KERNEL_BASE) + kerneldatasize;
383 	physical_freeend = physical_end;
384 
385 	physmem = (physical_end - physical_start) / NBPG;
386 
387 	freemempos = 0xc0000000;
388 
389 #ifdef VERBOSE_INIT_ARM
390 	printf("Allocating page tables\n");
391 #endif
392 	free_pages = (physical_freeend - physical_freestart) / NBPG;
393 
394 #ifdef VERBOSE_INIT_ARM
395 	printf("CP15 Register1 = 0x%08x\n", cpu_get_control());
396 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
397 		physical_freestart, free_pages, free_pages);
398 	printf("physical_start = 0x%08lx, physical_end = 0x%08lx\n",
399 		physical_start, physical_end);
400 #endif
401 
402 	/* Define a macro to simplify memory allocation */
403 #define valloc_pages(var, np)			\
404 	alloc_pages((var).pv_pa, (np));		\
405 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
406 #define alloc_pages(var, np)				\
407 	(var) = freemempos;				\
408 	memset((char *)(var), 0, ((np) * NBPG));	\
409 	freemempos += (np) * NBPG;
410 
411 	loop1 = 0;
412 	kernel_l1pt.pv_pa = 0;
413 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
414 		/* Are we 16KB aligned for an L1 ? */
415 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
416 		    && kernel_l1pt.pv_pa == 0) {
417 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / NBPG);
418 		} else {
419 			alloc_pages(kernel_pt_table[loop1].pv_pa,
420 			    L2_TABLE_SIZE / NBPG);
421 			kernel_pt_table[loop1].pv_va =
422 			    kernel_pt_table[loop1].pv_pa;
423 			++loop1;
424 		}
425 	}
426 
427 #ifdef DIAGNOSTIC
428 	/* This should never be able to happen but better confirm that. */
429 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
430 		panic("initarm: Failed to align the kernel page directory");
431 #endif
432 
433 	/*
434 	 * Allocate a page for the system page mapped to V0x00000000
435 	 * This page will just contain the system vectors and can be
436 	 * shared by all processes.
437 	 */
438 	alloc_pages(systempage.pv_pa, 1);
439 
440 	/* Allocate a page for the page table to map kernel page tables. */
441 	valloc_pages(kernel_ptpt, L2_TABLE_SIZE / NBPG);
442 
443 	/* Allocate stacks for all modes */
444 	valloc_pages(irqstack, IRQ_STACK_SIZE);
445 	valloc_pages(abtstack, ABT_STACK_SIZE);
446 	valloc_pages(undstack, UND_STACK_SIZE);
447 	valloc_pages(kernelstack, UPAGES);
448 
449 #ifdef VERBOSE_INIT_ARM
450 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa, irqstack.pv_va);
451 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa, abtstack.pv_va);
452 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa, undstack.pv_va);
453 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa, kernelstack.pv_va);
454 #endif
455 
456 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / NBPG);
457 
458 #ifdef CPU_IXP12X0
459         /*
460          * XXX totally stuffed hack to work round problems introduced
461          * in recent versions of the pmap code. Due to the calls used there
462          * we cannot allocate virtual memory during bootstrap.
463          */
464 	for(;;) {
465 		alloc_pages(ixp12x0_cc_base, 1);
466 		if (! (ixp12x0_cc_base & (CPU_IXP12X0_CACHE_CLEAN_SIZE - 1)))
467 			break;
468 	}
469 	{
470 		vaddr_t dummy;
471 		alloc_pages(dummy, CPU_IXP12X0_CACHE_CLEAN_SIZE / NBPG - 1);
472 	}
473 	ixp12x0_cache_clean_addr = ixp12x0_cc_base;
474 	ixp12x0_cache_clean_size = CPU_IXP12X0_CACHE_CLEAN_SIZE / 2;
475 #endif /* CPU_IXP12X0 */
476 
477 #ifdef VERBOSE_INIT_ARM
478 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
479 #endif
480 
481 	/*
482 	 * Now we start construction of the L1 page table
483 	 * We start by mapping the L2 page tables into the L1.
484 	 * This means that we can replace L1 mappings later on if necessary
485 	 */
486 	l1pagetable = kernel_l1pt.pv_pa;
487 
488 	/* Map the L2 pages tables in the L1 page table */
489 	pmap_link_l2pt(l1pagetable, 0x00000000,
490 	    &kernel_pt_table[KERNEL_PT_SYS]);
491 
492 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
493 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
494 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
495 
496 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
497 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
498 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
499 	pmap_link_l2pt(l1pagetable, PTE_BASE, &kernel_ptpt);
500 
501 	/* update the top of the kernel VM */
502 	pmap_curmaxkvaddr =
503 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
504 
505 	pmap_link_l2pt(l1pagetable, IXP12X0_IO_VBASE,
506 	    &kernel_pt_table[KERNEL_PT_IO]);
507 
508 #ifdef VERBOSE_INIT_ARM
509 	printf("Mapping kernel\n");
510 #endif
511 
512 #if XXX
513 	/* Now we fill in the L2 pagetable for the kernel code/data */
514 	{
515 		extern char etext[], _end[];
516 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
517 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
518 		u_int logical;
519 
520 		textsize = (textsize + PGOFSET) & ~PGOFSET;
521 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
522 
523 		logical = 0x00200000;   /* offset of kernel in RAM */
524 
525 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
526 		    physical_start + logical, textsize,
527 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
528 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
529 		    physical_start + logical, totalsize - textsize,
530 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
531 	}
532 #else
533 	{
534 		pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
535                     KERNEL_TEXT_BASE, kerneldatasize,
536                     VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
537 	}
538 #endif
539 
540 #ifdef VERBOSE_INIT_ARM
541         printf("Constructing L2 page tables\n");
542 #endif
543 
544 	/* Map the stack pages */
545 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
546 	    IRQ_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
547 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
548 	    ABT_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
549 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
550 	    UND_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
551 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
552 	    UPAGES * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
553 
554 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
555 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
556 
557 	/* Map the page table that maps the kernel pages */
558 	pmap_map_entry(l1pagetable, kernel_ptpt.pv_va, kernel_ptpt.pv_pa,
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_CACHE);
570 
571 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
572 		pmap_map_entry(l1pagetable,
573 		    PTE_BASE + ((KERNEL_BASE +
574 		    (loop * 0x00400000)) >> (PGSHIFT-2)),
575 		    kernel_pt_table[KERNEL_PT_KERNEL + loop].pv_pa,
576 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
577 
578 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
579 		pmap_map_entry(l1pagetable,
580 		    PTE_BASE + ((KERNEL_VM_BASE +
581 		    (loop * 0x00400000)) >> (PGSHIFT-2)),
582 		    kernel_pt_table[KERNEL_PT_VMDATA + loop].pv_pa,
583 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
584 
585 	pmap_map_entry(l1pagetable,
586 	    PTE_BASE + (PTE_BASE >> (PGSHIFT-2)),
587 	    kernel_ptpt.pv_pa, VM_PROT_READ|VM_PROT_WRITE, 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 #ifdef VERBOSE_INIT_ARM
594 	printf("systempage (vector page): p0x%08lx v0x%08lx\n",
595 	       systempage.pv_pa, vector_page);
596 #endif
597 
598 	/*
599 	 * Map the PCI I/O spaces and IXP12x0 registers
600 	 */
601 
602 	ixp12x0_pmap_io_reg(l1pagetable);
603 
604 	printf("done.\n");
605 
606 	/*
607 	 * Map the Dcache Flush page.
608 	 * Hw Ref Manual 3.2.4.5 Software Dcache Flush
609 	 */
610 	pmap_map_chunk(l1pagetable, ixp12x0_cache_clean_addr, 0xe0000000,
611 	    CPU_IXP12X0_CACHE_CLEAN_SIZE, VM_PROT_READ, PTE_CACHE);
612 
613 	/*
614 	 * Now we have the real page tables in place so we can switch to them.
615 	 * Once this is done we will be running with the REAL kernel page
616 	 * tables.
617 	 */
618 
619 	/* Switch tables */
620 	setttb(kernel_l1pt.pv_pa);
621 	cpu_tlb_flushID();
622 
623 	/*
624 	 * We must now clean the cache again....
625 	 * Cleaning may be done by reading new data to displace any
626 	 * dirty data in the cache. This will have happened in setttb()
627 	 * but since we are boot strapping the addresses used for the read
628 	 * may have just been remapped and thus the cache could be out
629 	 * of sync. A re-clean after the switch will cure this.
630 	 * After booting there are no gross reloations of the kernel thus
631 	 * this problem will not occur after initarm().
632 	 */
633 	cpu_idcache_wbinv_all();
634 
635 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
636 
637 	/*
638 	 * Pages were allocated during the secondary bootstrap for the
639 	 * stacks for different CPU modes.
640 	 * We must now set the r13 registers in the different CPU modes to
641 	 * point to these stacks.
642 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
643 	 * of the stack memory.
644 	 */
645 	printf("init subsystems: stacks ");
646 
647 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * NBPG);
648 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * NBPG);
649 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * NBPG);
650 #ifdef PMAP_DEBUG
651 	if (pmap_debug_level >= 0)
652 		printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
653 		    kernelstack.pv_pa);
654 #endif  /* PMAP_DEBUG */
655 
656 	/*
657 	 * Well we should set a data abort handler.
658 	 * Once things get going this will change as we will need a proper
659 	 * handler. Until then we will use a handler that just panics but
660 	 * tells us why.
661 	 * Initialisation of the vetcors will just panic on a data abort.
662 	 * This just fills in a slighly better one.
663 	 */
664 	printf("vectors ");
665 	data_abort_handler_address = (u_int)data_abort_handler;
666 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
667 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
668 	printf("\ndata_abort_handler_address = %08x\n", data_abort_handler_address);
669 	printf("prefetch_abort_handler_address = %08x\n", prefetch_abort_handler_address);
670 	printf("undefined_handler_address = %08x\n", undefined_handler_address);
671 
672 	/* Initialise the undefined instruction handlers */
673 	printf("undefined ");
674 	undefined_init();
675 
676 	/* Load memory into UVM. */
677 	printf("page ");
678 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
679 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
680 	    atop(physical_freestart), atop(physical_freeend),
681 	    VM_FREELIST_DEFAULT);
682 
683 	/* Boot strap pmap telling it where the kernel page table is */
684 	printf("pmap ");
685 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, kernel_ptpt);
686 
687 	/* Setup the IRQ system */
688 	printf("irq ");
689 	ixp12x0_intr_init();
690 	printf("done.\n");
691 
692 #ifdef VERBOSE_INIT_ARM
693 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
694 		physical_freestart, free_pages, free_pages);
695 	printf("freemempos=%08lx\n", freemempos);
696 	printf("switching to new L1 page table  @%#lx... \n", kernel_l1pt.pv_pa);
697 #endif
698 
699 	consinit();
700 	printf("consinit \n");
701 
702 	ixdp_ixp12x0_cc_setup();
703 
704 	printf("bootstrap done.\n");
705 
706 #ifdef IPKDB
707 	/* Initialise ipkdb */
708 	ipkdb_init();
709 	if (boothowto & RB_KDB)
710 		ipkdb_connect(0);
711 #endif  /* NIPKDB */
712 
713 #ifdef DDB
714 	{
715 		static struct undefined_handler uh;
716 
717 		uh.uh_handler = db_trapper;
718 		install_coproc_handler_static(0, &uh);
719 	}
720 	ddb_init(symbolsize, ((int *)&end), ((char *)&end) + symbolsize);
721 
722 	if (boothowto & RB_KDB)
723 		Debugger();
724 #endif
725 
726 	/* We return the new stack pointer address */
727 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
728 }
729 
730 void
731 consinit(void)
732 {
733 	extern struct bus_space ixpsip_bs_tag;
734 	static int consinit_called = 0;
735 
736 	if (consinit_called != 0)
737 		return;
738 
739 	consinit_called = 1;
740 
741 	if (ixpcomcnattach(&ixpsip_bs_tag, IXPCOM_UART_BASE,
742 			   CONSPEED, CONMODE))
743 		panic("can't init serial console @%lx", IXPCOM_UART_BASE);
744 }
745 
746 #ifdef DEBUG_BEFOREMMU
747 cons_decl(ixpcom);
748 void
749 fakecninit()
750 {
751 	static struct consdev fakecntab = cons_init(ixpcom);
752 	cn_tab = &fakecntab;
753 
754 	(*cn_tab->cn_init)(0);
755 	cn_tab->cn_pri = CN_REMOTE;
756 }
757 #endif
758 
759 /*
760  * For optimal cache cleaning we need two 16K banks of
761  * virtual address space that NOTHING else will access
762  * and then we alternate the cache cleaning between the
763  * two banks.
764  * The cache cleaning code requires requires 2 banks aligned
765  * on total size boundry so the banks can be alternated by
766  * eorring the size bit (assumes the bank size is a power of 2)
767  */
768 void
769 ixdp_ixp12x0_cc_setup(void)
770 {
771 	int loop;
772 	paddr_t kaddr;
773 	pt_entry_t *pte;
774 
775 	(void) pmap_extract(pmap_kernel(), KERNEL_TEXT_BASE, &kaddr);
776 	for (loop = 0; loop < CPU_IXP12X0_CACHE_CLEAN_SIZE; loop += NBPG) {
777                 pte = vtopte(ixp12x0_cc_base + loop);
778                 *pte = L2_S_PROTO | kaddr |
779                     L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
780 		PTE_SYNC(pte);
781         }
782 	ixp12x0_cache_clean_addr = ixp12x0_cc_base;
783 	ixp12x0_cache_clean_size = CPU_IXP12X0_CACHE_CLEAN_SIZE / 2;
784 }
785