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