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