xref: /netbsd-src/sys/arch/evbarm/ixm1200/ixm1200_machdep.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: ixm1200_machdep.c,v 1.31 2005/12/11 12:17:09 christos Exp $ */
2 
3 /*
4  * Copyright (c) 2002, 2003
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 <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: ixm1200_machdep.c,v 1.31 2005/12/11 12:17:09 christos Exp $");
71 
72 #include "opt_ddb.h"
73 #include "opt_pmap_debug.h"
74 
75 #include <sys/param.h>
76 #include <sys/device.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/exec.h>
80 #include <sys/proc.h>
81 #include <sys/msgbuf.h>
82 #include <sys/reboot.h>
83 #include <sys/termios.h>
84 #include <sys/ksyms.h>
85 
86 #include <uvm/uvm_extern.h>
87 
88 #include <dev/cons.h>
89 
90 #include "ksyms.h"
91 
92 #if NKSYMS || defined(DDB) || defined(LKM)
93 #include <machine/db_machdep.h>
94 #include <ddb/db_sym.h>
95 #include <ddb/db_extern.h>
96 #ifndef DB_ELFSIZE
97 #error Must define DB_ELFSIZE!
98 #endif
99 #define ELFSIZE	DB_ELFSIZE
100 #include <sys/exec_elf.h>
101 #endif
102 
103 #include <machine/bootconfig.h>
104 #include <machine/bus.h>
105 #include <machine/cpu.h>
106 #include <machine/frame.h>
107 #include <arm/undefined.h>
108 
109 #include <arm/arm32/machdep.h>
110 
111 #include <arm/ixp12x0/ixp12x0reg.h>
112 #include <arm/ixp12x0/ixp12x0var.h>
113 #include <arm/ixp12x0/ixp12x0_comreg.h>
114 #include <arm/ixp12x0/ixp12x0_comvar.h>
115 #include <arm/ixp12x0/ixp12x0_pcireg.h>
116 
117 #include <evbarm/ixm1200/ixm1200reg.h>
118 #include <evbarm/ixm1200/ixm1200var.h>
119 
120 #include "opt_ipkdb.h"
121 
122 /* XXX for consinit related hacks */
123 #include <sys/conf.h>
124 
125 void ixp12x0_reset(void) __attribute__((noreturn));
126 
127 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
128 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
129 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
130 
131 /*
132  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
133  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
134  */
135 #define KERNEL_VM_SIZE		0x0C000000
136 
137 /*
138  * Address to call from cpu_reset() to reset the machine.
139  * This is machine architecture dependant as it varies depending
140  * on where the ROM appears when you turn the MMU off.
141  */
142 
143 u_int cpu_reset_address = (u_int) ixp12x0_reset;
144 
145 /*
146  * Define the default console speed for the board.
147  */
148 #ifndef CONMODE
149 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB)) | CS8) /* 8N1 */
150 #endif
151 #ifndef CONSPEED
152 #define CONSPEED B38400
153 #endif
154 #ifndef CONADDR
155 #define CONADDR IXPCOM_UART_BASE
156 #endif
157 
158 /* Define various stack sizes in pages */
159 #define IRQ_STACK_SIZE  1
160 #define ABT_STACK_SIZE  1
161 #ifdef IPKDB
162 #define UND_STACK_SIZE  2
163 #else
164 #define UND_STACK_SIZE  1
165 #endif
166 
167 BootConfig bootconfig;          /* Boot config storage */
168 char *boot_args = NULL;
169 char *boot_file = NULL;
170 
171 vm_offset_t physical_start;
172 vm_offset_t physical_freestart;
173 vm_offset_t physical_freeend;
174 vm_offset_t physical_end;
175 u_int free_pages;
176 vm_offset_t pagetables_start;
177 int physmem = 0;
178 
179 /*int debug_flags;*/
180 #ifndef PMAP_STATIC_L1S
181 int max_processes = 64;                 /* Default number */
182 #endif  /* !PMAP_STATIC_L1S */
183 
184 /* Physical and virtual addresses for some global pages */
185 pv_addr_t systempage;
186 pv_addr_t irqstack;
187 pv_addr_t undstack;
188 pv_addr_t abtstack;
189 pv_addr_t kernelstack;
190 
191 vm_offset_t msgbufphys;
192 
193 extern u_int data_abort_handler_address;
194 extern u_int prefetch_abort_handler_address;
195 extern u_int undefined_handler_address;
196 extern int end;
197 
198 #ifdef PMAP_DEBUG
199 extern int pmap_debug_level;
200 #endif  /* PMAP_DEBUG */
201 
202 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
203 #define KERNEL_PT_KERNEL	1	/* Page table for mapping kernel */
204 #define KERNEL_PT_KERNEL_NUM	2
205 #define KERNEL_PT_IO		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
206 					/* Page table for mapping IO */
207 #define KERNEL_PT_VMDATA	(KERNEL_PT_IO + 1)
208 					/* Page tables for mapping kernel VM */
209 #define KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
210 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
211 
212 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
213 
214 struct user *proc0paddr;
215 
216 #ifdef CPU_IXP12X0
217 #define CPU_IXP12X0_CACHE_CLEAN_SIZE (0x4000 * 2)
218 extern unsigned int ixp12x0_cache_clean_addr;
219 extern unsigned int ixp12x0_cache_clean_size;
220 static vaddr_t ixp12x0_cc_base;
221 #endif  /* CPU_IXP12X0 */
222 
223 /* Prototypes */
224 
225 void consinit		__P((void));
226 u_int cpu_get_control	__P((void));
227 
228 void ixdp_ixp12x0_cc_setup(void);
229 
230 /*
231  * void cpu_reboot(int howto, char *bootstr)
232  *
233  * Reboots the system
234  *
235  * Deal with any syncing, unmounting, dumping and shutdown hooks,
236  * then reset the CPU.
237  */
238 
239 void
240 cpu_reboot(howto, bootstr)
241 	int howto;
242 	char *bootstr;
243 {
244 	/*
245 	 * If we are still cold then hit the air brakes
246 	 * and crash to earth fast
247 	 */
248 	if (cold) {
249 		doshutdownhooks();
250 		printf("Halted while still in the ICE age.\n");
251 		printf("The operating system has halted.\n");
252 		printf("Please press any key to reboot.\n\n");
253 		cngetc();
254 		printf("rebooting...\n");
255 		ixp12x0_reset();
256 	}
257 
258 	/* Disable console buffering */
259 	cnpollc(1);
260 
261 	/*
262 	 * If RB_NOSYNC was not specified sync the discs.
263 	 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
264 	 * It looks like syslogd is getting woken up only to find that it cannot
265 	 * page part of the binary in as the filesystem has been unmounted.
266 	 */
267 	if (!(howto & RB_NOSYNC))
268 		bootsync();
269 
270 	/* Say NO to interrupts */
271 	splhigh();
272 
273 	/* Do a dump if requested. */
274 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
275 		dumpsys();
276 
277 	/* Run any shutdown hooks */
278 	doshutdownhooks();
279 
280 	/* Make sure IRQ's are disabled */
281 	IRQdisable;
282 
283 	if (howto & RB_HALT) {
284 		printf("The operating system has halted.\n");
285 		printf("Please press any key to reboot.\n\n");
286 		cngetc();
287 	}
288 
289 	printf("rebooting...\n");
290 
291 	/* all interrupts are disabled */
292 	disable_interrupts(I32_bit);
293 
294 	ixp12x0_reset();
295 
296 	/* ...and if that didn't work, just croak. */
297 	printf("RESET FAILED!\n");
298 	for (;;);
299 }
300 
301 /* Static device mappings. */
302 static const struct pmap_devmap ixm1200_devmap[] = {
303 	/* StrongARM System and Peripheral Registers */
304 	{
305 		IXP12X0_SYS_VBASE,
306 		IXP12X0_SYS_HWBASE,
307 		IXP12X0_SYS_SIZE,
308 		VM_PROT_READ|VM_PROT_WRITE,
309 		PTE_NOCACHE,
310 	},
311 	/* PCI Registers Accessible Through StrongARM Core */
312 	{
313 		IXP12X0_PCI_VBASE, IXP12X0_PCI_HWBASE,
314 		IXP12X0_PCI_SIZE,
315 		VM_PROT_READ|VM_PROT_WRITE,
316 		PTE_NOCACHE,
317 	},
318 	/* PCI Registers Accessible Through I/O Cycle Access */
319 	{
320 		IXP12X0_PCI_IO_VBASE, IXP12X0_PCI_IO_HWBASE,
321 		IXP12X0_PCI_IO_SIZE,
322 		VM_PROT_READ|VM_PROT_WRITE,
323 		PTE_NOCACHE,
324 	},
325 	/* PCI Type0 Configuration Space */
326 	{
327 		IXP12X0_PCI_TYPE0_VBASE, IXP12X0_PCI_TYPE0_HWBASE,
328 		IXP12X0_PCI_TYPE0_SIZE,
329 		VM_PROT_READ|VM_PROT_WRITE,
330 		PTE_NOCACHE,
331 	},
332 	/* PCI Type1 Configuration Space */
333 	{
334 		IXP12X0_PCI_TYPE1_VBASE, IXP12X0_PCI_TYPE1_HWBASE,
335 		IXP12X0_PCI_TYPE1_SIZE,
336 		VM_PROT_READ|VM_PROT_WRITE,
337 		PTE_NOCACHE,
338 	},
339 	{
340 		0,
341 		0,
342 		0,
343 		0,
344 		0
345 	},
346 };
347 
348 /*
349  * Initial entry point on startup. This gets called before main() is
350  * entered.
351  * It should be responsible for setting up everything that must be
352  * in place when main is called.
353  * This includes
354  *   Taking a copy of the boot configuration structure.
355  *   Initialising the physical console so characters can be printed.
356  *   Setting up page tables for the kernel
357  *   Relocating the kernel to the bottom of physical memory
358  */
359 u_int
360 initarm(void *arg)
361 {
362         int loop;
363 	int loop1;
364 	u_int kerneldatasize, symbolsize;
365 	vaddr_t l1pagetable;
366 	vaddr_t freemempos;
367 	pv_addr_t kernel_l1pt;
368 #if NKSYMS || defined(DDB) || defined(LKM)
369         Elf_Shdr *sh;
370 #endif
371 
372         /*
373          * Since we map v0xf0000000 == p0x90000000, it's possible for
374          * us to initialize the console now.
375          */
376 	consinit();
377 
378 #ifdef VERBOSE_INIT_ARM
379 	/* Talk to the user */
380 	printf("\nNetBSD/evbarm (IXM1200) booting ...\n");
381 #endif
382 
383 	/*
384 	 * Heads up ... Setup the CPU / MMU / TLB functions
385 	 */
386 	if (set_cpufuncs())
387 		panic("CPU not recognized!");
388 
389 	/* XXX overwrite bootconfig to hardcoded values */
390 	bootconfig.dram[0].address = 0xc0000000;
391 	bootconfig.dram[0].pages   = 0x10000000 / PAGE_SIZE; /* SDRAM 256MB */
392 	bootconfig.dramblocks = 1;
393 
394 	kerneldatasize = (u_int32_t)&end - (u_int32_t)KERNEL_TEXT_BASE;
395 
396 	symbolsize = 0;
397 
398 #ifdef PMAP_DEBUG
399 	pmap_debug(-1);
400 #endif
401 
402 #if NKSYMS || defined(DDB) || defined(LKM)
403         if (! memcmp(&end, "\177ELF", 4)) {
404                 sh = (Elf_Shdr *)((char *)&end + ((Elf_Ehdr *)&end)->e_shoff);
405                 loop = ((Elf_Ehdr *)&end)->e_shnum;
406                 for(; loop; loop--, sh++)
407                         if (sh->sh_offset > 0 &&
408                             (sh->sh_offset + sh->sh_size) > symbolsize)
409                                 symbolsize = sh->sh_offset + sh->sh_size;
410         }
411 #endif
412 #ifdef VERBOSE_INIT_ARM
413 	printf("kernsize=0x%x\n", kerneldatasize);
414 #endif
415 	kerneldatasize += symbolsize;
416 	kerneldatasize = ((kerneldatasize - 1) & ~(PAGE_SIZE * 4 - 1)) + PAGE_SIZE * 8;
417 
418 	/*
419 	 * Set up the variables that define the availablilty of physcial
420 	 * memory
421 	 */
422 	physical_start = bootconfig.dram[0].address;
423 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
424 
425 	physical_freestart = physical_start
426 		+ (KERNEL_TEXT_BASE - KERNEL_BASE) + kerneldatasize;
427 	physical_freeend = physical_end;
428 
429 	physmem = (physical_end - physical_start) / PAGE_SIZE;
430 
431 	freemempos = 0xc0000000;
432 
433 #ifdef VERBOSE_INIT_ARM
434 	printf("Allocating page tables\n");
435 #endif
436 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
437 
438 #ifdef VERBOSE_INIT_ARM
439 	printf("CP15 Register1 = 0x%08x\n", cpu_get_control());
440 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
441 		physical_freestart, free_pages, free_pages);
442 	printf("physical_start = 0x%08lx, physical_end = 0x%08lx\n",
443 		physical_start, physical_end);
444 #endif
445 
446 	/* Define a macro to simplify memory allocation */
447 #define valloc_pages(var, np)			\
448 	alloc_pages((var).pv_pa, (np));		\
449 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
450 #define alloc_pages(var, np)				\
451 	(var) = freemempos;				\
452 	memset((char *)(var), 0, ((np) * PAGE_SIZE));	\
453 	freemempos += (np) * PAGE_SIZE;
454 
455 	loop1 = 0;
456 	kernel_l1pt.pv_pa = 0;
457 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
458 		/* Are we 16KB aligned for an L1 ? */
459 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
460 		    && kernel_l1pt.pv_pa == 0) {
461 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
462 		} else {
463 			valloc_pages(kernel_pt_table[loop1],
464 			    L2_TABLE_SIZE / PAGE_SIZE);
465 			++loop1;
466 		}
467 	}
468 
469 #ifdef DIAGNOSTIC
470 	/* This should never be able to happen but better confirm that. */
471 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
472 		panic("initarm: Failed to align the kernel page directory");
473 #endif
474 
475 	/*
476 	 * Allocate a page for the system page mapped to V0x00000000
477 	 * This page will just contain the system vectors and can be
478 	 * shared by all processes.
479 	 */
480 	alloc_pages(systempage.pv_pa, 1);
481 
482 	/* Allocate stacks for all modes */
483 	valloc_pages(irqstack, IRQ_STACK_SIZE);
484 	valloc_pages(abtstack, ABT_STACK_SIZE);
485 	valloc_pages(undstack, UND_STACK_SIZE);
486 	valloc_pages(kernelstack, UPAGES);
487 
488 #ifdef VERBOSE_INIT_ARM
489 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa, irqstack.pv_va);
490 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa, abtstack.pv_va);
491 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa, undstack.pv_va);
492 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa, kernelstack.pv_va);
493 #endif
494 
495 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
496 
497 #ifdef CPU_IXP12X0
498         /*
499          * XXX totally stuffed hack to work round problems introduced
500          * in recent versions of the pmap code. Due to the calls used there
501          * we cannot allocate virtual memory during bootstrap.
502          */
503 	for(;;) {
504 		alloc_pages(ixp12x0_cc_base, 1);
505 		if (! (ixp12x0_cc_base & (CPU_IXP12X0_CACHE_CLEAN_SIZE - 1)))
506 			break;
507 	}
508 	{
509 		vaddr_t dummy;
510 		alloc_pages(dummy, CPU_IXP12X0_CACHE_CLEAN_SIZE / PAGE_SIZE - 1);
511 	}
512 	ixp12x0_cache_clean_addr = ixp12x0_cc_base;
513 	ixp12x0_cache_clean_size = CPU_IXP12X0_CACHE_CLEAN_SIZE / 2;
514 #endif /* CPU_IXP12X0 */
515 
516 #ifdef VERBOSE_INIT_ARM
517 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
518 #endif
519 
520 	/*
521 	 * Now we start construction of the L1 page table
522 	 * We start by mapping the L2 page tables into the L1.
523 	 * This means that we can replace L1 mappings later on if necessary
524 	 */
525 	l1pagetable = kernel_l1pt.pv_pa;
526 
527 	/* Map the L2 pages tables in the L1 page table */
528 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
529 	    &kernel_pt_table[KERNEL_PT_SYS]);
530 
531 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
532 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
533 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
534 
535 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
536 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
537 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
538 
539 	/* update the top of the kernel VM */
540 	pmap_curmaxkvaddr =
541 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
542 
543 	pmap_link_l2pt(l1pagetable, IXP12X0_IO_VBASE,
544 	    &kernel_pt_table[KERNEL_PT_IO]);
545 
546 #ifdef VERBOSE_INIT_ARM
547 	printf("Mapping kernel\n");
548 #endif
549 
550 #if XXX
551 	/* Now we fill in the L2 pagetable for the kernel code/data */
552 	{
553 		extern char etext[], _end[];
554 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
555 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
556 		u_int logical;
557 
558 		textsize = (textsize + PGOFSET) & ~PGOFSET;
559 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
560 
561 		logical = 0x00200000;   /* offset of kernel in RAM */
562 
563 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
564 		    physical_start + logical, textsize,
565 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
566 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
567 		    physical_start + logical, totalsize - textsize,
568 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
569 	}
570 #else
571 	{
572 		pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
573                     KERNEL_TEXT_BASE, kerneldatasize,
574                     VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
575 	}
576 #endif
577 
578 #ifdef VERBOSE_INIT_ARM
579         printf("Constructing L2 page tables\n");
580 #endif
581 
582 	/* Map the stack pages */
583 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
584 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
585 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
586 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
587 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
588 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
589 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
590 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
591 
592 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
593 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
594 
595 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
596 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
597 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
598 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
599 	}
600 
601 	/* Map the vector page. */
602 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
603 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
604 
605 #ifdef VERBOSE_INIT_ARM
606 	printf("systempage (vector page): p0x%08lx v0x%08lx\n",
607 	       systempage.pv_pa, vector_page);
608 #endif
609 
610 	/* Map the statically mapped devices. */
611 	pmap_devmap_bootstrap(l1pagetable, ixm1200_devmap);
612 
613 #ifdef VERBOSE_INIT_ARM
614 	printf("done.\n");
615 #endif
616 
617 	/*
618 	 * Map the Dcache Flush page.
619 	 * Hw Ref Manual 3.2.4.5 Software Dcache Flush
620 	 */
621 	pmap_map_chunk(l1pagetable, ixp12x0_cache_clean_addr, 0xe0000000,
622 	    CPU_IXP12X0_CACHE_CLEAN_SIZE, VM_PROT_READ, PTE_CACHE);
623 
624 	/*
625 	 * Now we have the real page tables in place so we can switch to them.
626 	 * Once this is done we will be running with the REAL kernel page
627 	 * tables.
628 	 */
629 
630 	/* Switch tables */
631 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
632 	setttb(kernel_l1pt.pv_pa);
633 	cpu_tlb_flushID();
634 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
635 
636 	/*
637 	 * Moved here from cpu_startup() as data_abort_handler() references
638 	 * this during init
639 	 */
640 	proc0paddr = (struct user *)kernelstack.pv_va;
641 	lwp0.l_addr = proc0paddr;
642 
643 	/*
644 	 * We must now clean the cache again....
645 	 * Cleaning may be done by reading new data to displace any
646 	 * dirty data in the cache. This will have happened in setttb()
647 	 * but since we are boot strapping the addresses used for the read
648 	 * may have just been remapped and thus the cache could be out
649 	 * of sync. A re-clean after the switch will cure this.
650 	 * After booting there are no gross reloations of the kernel thus
651 	 * this problem will not occur after initarm().
652 	 */
653 	cpu_idcache_wbinv_all();
654 
655 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
656 
657 	/*
658 	 * Pages were allocated during the secondary bootstrap for the
659 	 * stacks for different CPU modes.
660 	 * We must now set the r13 registers in the different CPU modes to
661 	 * point to these stacks.
662 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
663 	 * of the stack memory.
664 	 */
665 #ifdef VERBOSE_INIT_ARM
666 	printf("init subsystems: stacks ");
667 #endif
668 
669 	set_stackptr(PSR_IRQ32_MODE,
670 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
671 	set_stackptr(PSR_ABT32_MODE,
672 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
673 	set_stackptr(PSR_UND32_MODE,
674 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
675 #ifdef PMAP_DEBUG
676 	if (pmap_debug_level >= 0)
677 		printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
678 		    kernelstack.pv_pa);
679 #endif  /* PMAP_DEBUG */
680 
681 	/*
682 	 * Well we should set a data abort handler.
683 	 * Once things get going this will change as we will need a proper
684 	 * handler. Until then we will use a handler that just panics but
685 	 * tells us why.
686 	 * Initialisation of the vetcors will just panic on a data abort.
687 	 * This just fills in a slightly better one.
688 	 */
689 #ifdef VERBOSE_INIT_ARM
690 	printf("vectors ");
691 #endif
692 	data_abort_handler_address = (u_int)data_abort_handler;
693 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
694 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
695 #ifdef VERBOSE_INIT_ARM
696 	printf("\ndata_abort_handler_address = %08x\n", data_abort_handler_address);
697 	printf("prefetch_abort_handler_address = %08x\n", prefetch_abort_handler_address);
698 	printf("undefined_handler_address = %08x\n", undefined_handler_address);
699 #endif
700 
701 	/* Initialise the undefined instruction handlers */
702 #ifdef VERBOSE_INIT_ARM
703 	printf("undefined ");
704 #endif
705 	undefined_init();
706 
707 	/* Load memory into UVM. */
708 #ifdef VERBOSE_INIT_ARM
709 	printf("page ");
710 #endif
711 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
712 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
713 	    atop(physical_freestart), atop(physical_freeend),
714 	    VM_FREELIST_DEFAULT);
715 
716 	/* Boot strap pmap telling it where the kernel page table is */
717 #ifdef VERBOSE_INIT_ARM
718 	printf("pmap ");
719 #endif
720 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
721 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
722 
723 	/* Setup the IRQ system */
724 #ifdef VERBOSE_INIT_ARM
725 	printf("irq ");
726 #endif
727 	ixp12x0_intr_init();
728 
729 #ifdef VERBOSE_INIT_ARM
730 	printf("done.\n");
731 #endif
732 
733 #ifdef VERBOSE_INIT_ARM
734 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
735 		physical_freestart, free_pages, free_pages);
736 	printf("freemempos=%08lx\n", freemempos);
737 	printf("switching to new L1 page table  @%#lx... \n", kernel_l1pt.pv_pa);
738 #endif
739 
740 	consinit();
741 #ifdef VERBOSE_INIT_ARM
742 	printf("consinit \n");
743 #endif
744 
745 	ixdp_ixp12x0_cc_setup();
746 
747 #ifdef VERBOSE_INIT_ARM
748 	printf("bootstrap done.\n");
749 #endif
750 
751 #ifdef IPKDB
752 	/* Initialise ipkdb */
753 	ipkdb_init();
754 	if (boothowto & RB_KDB)
755 		ipkdb_connect(0);
756 #endif  /* NIPKDB */
757 
758 #if NKSYMS || defined(DDB) || defined(LKM)
759 	ksyms_init(symbolsize, ((int *)&end), ((char *)&end) + symbolsize);
760 #endif
761 
762 #ifdef DDB
763 	db_machine_init();
764 	if (boothowto & RB_KDB)
765 		Debugger();
766 #endif
767 
768 	/* We return the new stack pointer address */
769 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
770 }
771 
772 void
773 consinit(void)
774 {
775 	static int consinit_called = 0;
776 
777 	if (consinit_called != 0)
778 		return;
779 
780 	consinit_called = 1;
781 
782 	pmap_devmap_register(ixm1200_devmap);
783 
784 	if (ixpcomcnattach(&ixp12x0_bs_tag,
785 			   IXPCOM_UART_HWBASE, IXPCOM_UART_VBASE,
786 			   CONSPEED, CONMODE))
787 		panic("can't init serial console @%lx", IXPCOM_UART_HWBASE);
788 }
789 
790 /*
791  * For optimal cache cleaning we need two 16K banks of
792  * virtual address space that NOTHING else will access
793  * and then we alternate the cache cleaning between the
794  * two banks.
795  * The cache cleaning code requires requires 2 banks aligned
796  * on total size boundry so the banks can be alternated by
797  * eorring the size bit (assumes the bank size is a power of 2)
798  */
799 void
800 ixdp_ixp12x0_cc_setup(void)
801 {
802 	int loop;
803 	paddr_t kaddr;
804 	pt_entry_t *pte;
805 
806 	(void) pmap_extract(pmap_kernel(), KERNEL_TEXT_BASE, &kaddr);
807 	for (loop = 0; loop < CPU_IXP12X0_CACHE_CLEAN_SIZE; loop += PAGE_SIZE) {
808                 pte = vtopte(ixp12x0_cc_base + loop);
809                 *pte = L2_S_PROTO | kaddr |
810                     L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
811 		PTE_SYNC(pte);
812         }
813 	ixp12x0_cache_clean_addr = ixp12x0_cc_base;
814 	ixp12x0_cache_clean_size = CPU_IXP12X0_CACHE_CLEAN_SIZE / 2;
815 }
816