xref: /netbsd-src/sys/arch/evbarm/hdl_g/hdlg_machdep.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: hdlg_machdep.c,v 1.4 2006/12/18 13:50:58 nonaka Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe and Steve C. Woodford for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed for the NetBSD Project by
20  *	Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Copyright (c) 1997,1998 Mark Brinicombe.
40  * Copyright (c) 1997,1998 Causality Limited.
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by Mark Brinicombe
54  *	for the NetBSD Project.
55  * 4. The name of the company nor the name of the author may be used to
56  *    endorse or promote products derived from this software without specific
57  *    prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
60  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
61  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
63  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
64  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
65  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  * Machine dependant functions for kernel setup for GigaLANDISK
72  * using RedBoot firmware.
73  */
74 
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: hdlg_machdep.c,v 1.4 2006/12/18 13:50:58 nonaka Exp $");
77 
78 #include "opt_ddb.h"
79 #include "opt_kgdb.h"
80 #include "opt_pmap_debug.h"
81 
82 #include <sys/param.h>
83 #include <sys/device.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
86 #include <sys/exec.h>
87 #include <sys/proc.h>
88 #include <sys/msgbuf.h>
89 #include <sys/reboot.h>
90 #include <sys/termios.h>
91 #include <sys/ksyms.h>
92 
93 #include <uvm/uvm_extern.h>
94 
95 #include <dev/cons.h>
96 
97 #include <machine/db_machdep.h>
98 #include <ddb/db_sym.h>
99 #include <ddb/db_extern.h>
100 
101 #include <machine/bootconfig.h>
102 #include <machine/bus.h>
103 #include <machine/cpu.h>
104 #include <machine/frame.h>
105 #include <arm/undefined.h>
106 
107 #include <arm/arm32/machdep.h>
108 
109 #include <arm/xscale/i80321reg.h>
110 #include <arm/xscale/i80321var.h>
111 
112 #include <dev/pci/ppbreg.h>
113 
114 #include <evbarm/hdl_g/hdlgreg.h>
115 #include <evbarm/hdl_g/hdlgvar.h>
116 #include <evbarm/hdl_g/obiovar.h>
117 
118 #include "opt_ipkdb.h"
119 #include "ksyms.h"
120 
121 /* Kernel text starts 2MB in from the bottom of the kernel address space. */
122 #define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00200000)
123 #define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
124 
125 /*
126  * The range 0xc1000000 - 0xccffffff is available for kernel VM space
127  * Core-logic registers and I/O mappings occupy 0xfd000000 - 0xffffffff
128  */
129 #define KERNEL_VM_SIZE		0x0C000000
130 
131 /*
132  * Address to call from cpu_reset() to reset the machine.
133  * This is machine architecture dependant as it varies depending
134  * on where the ROM appears when you turn the MMU off.
135  *
136  * XXX Not actually used on hdlg -- clean up the generic
137  * ARM code.
138  */
139 u_int cpu_reset_address = 0x00000000;
140 
141 /* Define various stack sizes in pages */
142 #define IRQ_STACK_SIZE	1
143 #define ABT_STACK_SIZE	1
144 #ifdef IPKDB
145 #define UND_STACK_SIZE	2
146 #else
147 #define UND_STACK_SIZE	1
148 #endif
149 
150 BootConfig bootconfig;		/* Boot config storage */
151 char *boot_args = NULL;
152 char *boot_file = NULL;
153 
154 vm_offset_t physical_start;
155 vm_offset_t physical_freestart;
156 vm_offset_t physical_freeend;
157 vm_offset_t physical_end;
158 u_int free_pages;
159 vm_offset_t pagetables_start;
160 int physmem = 0;
161 
162 /*int debug_flags;*/
163 #ifndef PMAP_STATIC_L1S
164 int max_processes = 64;			/* Default number */
165 #endif	/* !PMAP_STATIC_L1S */
166 
167 /* Physical and virtual addresses for some global pages */
168 pv_addr_t systempage;
169 pv_addr_t irqstack;
170 pv_addr_t undstack;
171 pv_addr_t abtstack;
172 pv_addr_t kernelstack;
173 pv_addr_t minidataclean;
174 
175 vm_offset_t msgbufphys;
176 
177 extern u_int data_abort_handler_address;
178 extern u_int prefetch_abort_handler_address;
179 extern u_int undefined_handler_address;
180 
181 #ifdef PMAP_DEBUG
182 extern int pmap_debug_level;
183 #endif
184 
185 #define KERNEL_PT_SYS		0	/* L2 table for mapping zero page */
186 
187 #define KERNEL_PT_KERNEL	1	/* L2 table for mapping kernel */
188 #define	KERNEL_PT_KERNEL_NUM	4
189 
190 					/* L2 table for mapping i80321 */
191 #define	KERNEL_PT_IOPXS		(KERNEL_PT_KERNEL + KERNEL_PT_KERNEL_NUM)
192 
193 					/* L2 tables for mapping kernel VM */
194 #define KERNEL_PT_VMDATA	(KERNEL_PT_IOPXS + 1)
195 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
196 #define NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
197 
198 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
199 
200 struct user *proc0paddr;
201 
202 /* Prototypes */
203 void consinit(void);
204 
205 /* Static device mappings. */
206 static const struct pmap_devmap hdlg_devmap[] = {
207     /*
208      * Map the on-board devices VA == PA so that we can access them
209      * with the MMU on or off.
210      */
211     {
212 	HDLG_OBIO_BASE,
213 	HDLG_OBIO_BASE,
214 	HDLG_OBIO_SIZE,
215 	VM_PROT_READ|VM_PROT_WRITE,
216 	PTE_NOCACHE,
217     },
218 
219     {
220 	HDLG_IOW_VBASE,
221 	VERDE_OUT_XLATE_IO_WIN0_BASE,
222 	VERDE_OUT_XLATE_IO_WIN_SIZE,
223 	VM_PROT_READ|VM_PROT_WRITE,
224 	PTE_NOCACHE,
225    },
226 
227    {
228 	HDLG_80321_VBASE,
229 	VERDE_PMMR_BASE,
230 	VERDE_PMMR_SIZE,
231 	VM_PROT_READ|VM_PROT_WRITE,
232 	PTE_NOCACHE,
233    },
234 
235    {
236 	0,
237 	0,
238 	0,
239 	0,
240 	0,
241     }
242 };
243 
244 static void
245 hardclock_hook(void)
246 {
247 
248 	/* Nothing to do */
249 }
250 
251 /*
252  * u_int initarm(...)
253  *
254  * Initial entry point on startup. This gets called before main() is
255  * entered.
256  * It should be responsible for setting up everything that must be
257  * in place when main is called.
258  * This includes
259  *   Taking a copy of the boot configuration structure.
260  *   Initialising the physical console so characters can be printed.
261  *   Setting up page tables for the kernel
262  *   Relocating the kernel to the bottom of physical memory
263  */
264 u_int
265 initarm(void *arg)
266 {
267 	extern vaddr_t xscale_cache_clean_addr;
268 #ifdef DIAGNOSTIC
269 	extern vsize_t xscale_minidata_clean_size;
270 #endif
271 	int loop;
272 	int loop1;
273 	u_int l1pagetable;
274 	pv_addr_t kernel_l1pt;
275 	paddr_t memstart;
276 	psize_t memsize;
277 
278 	/* Calibrate the delay loop. */
279 	i80321_calibrate_delay();
280 	i80321_hardclock_hook = hardclock_hook;
281 
282 	/*
283 	 * Since we map the on-board devices VA==PA, and the kernel
284 	 * is running VA==PA, it's possible for us to initialize
285 	 * the console now.
286 	 */
287 	consinit();
288 
289 #ifdef VERBOSE_INIT_ARM
290 	/* Talk to the user */
291 	printf("\nNetBSD/evbarm (HDL-G) booting ...\n");
292 #endif
293 
294 	/*
295 	 * Heads up ... Setup the CPU / MMU / TLB functions
296 	 */
297 	if (set_cpufuncs())
298 		panic("CPU not recognized!");
299 
300 	/*
301 	 * We are currently running with the MMU enabled and the
302 	 * entire address space mapped VA==PA, except for the
303 	 * first 64M of RAM is also double-mapped at 0xc0000000.
304 	 * There is an L1 page table at 0xa0004000.
305 	 */
306 
307 	/*
308 	 * Fetch the SDRAM start/size from the i80321 SDRAM configuration
309 	 * registers.
310 	 */
311 	i80321_sdram_bounds(&obio_bs_tag, VERDE_PMMR_BASE + VERDE_MCU_BASE,
312 	    &memstart, &memsize);
313 
314 #ifdef VERBOSE_INIT_ARM
315 	printf("initarm: Configuring system ...\n");
316 #endif
317 
318 	/* Fake bootconfig structure for the benefit of pmap.c */
319 	/* XXX must make the memory description h/w independent */
320 	bootconfig.dramblocks = 1;
321 	bootconfig.dram[0].address = memstart;
322 	bootconfig.dram[0].pages = memsize / PAGE_SIZE;
323 
324 	/*
325 	 * Set up the variables that define the availablilty of
326 	 * physical memory.  For now, we're going to set
327 	 * physical_freestart to 0xa0200000 (where the kernel
328 	 * was loaded), and allocate the memory we need downwards.
329 	 * If we get too close to the L1 table that we set up, we
330 	 * will panic.  We will update physical_freestart and
331 	 * physical_freeend later to reflect what pmap_bootstrap()
332 	 * wants to see.
333 	 *
334 	 * XXX pmap_bootstrap() needs an enema.
335 	 */
336 	physical_start = bootconfig.dram[0].address;
337 	physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
338 
339 	physical_freestart = 0xa0009000UL;
340 	physical_freeend = 0xa0200000UL;
341 
342 	physmem = (physical_end - physical_start) / PAGE_SIZE;
343 
344 #ifdef VERBOSE_INIT_ARM
345 	/* Tell the user about the memory */
346 	printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
347 	    physical_start, physical_end - 1);
348 #endif
349 
350 	/*
351 	 * Okay, the kernel starts 2MB in from the bottom of physical
352 	 * memory.  We are going to allocate our bootstrap pages downwards
353 	 * from there.
354 	 *
355 	 * We need to allocate some fixed page tables to get the kernel
356 	 * going.  We allocate one page directory and a number of page
357 	 * tables and store the physical addresses in the kernel_pt_table
358 	 * array.
359 	 *
360 	 * The kernel page directory must be on a 16K boundary.  The page
361 	 * tables must be on 4K boundaries.  What we do is allocate the
362 	 * page directory on the first 16K boundary that we encounter, and
363 	 * the page tables on 4K boundaries otherwise.  Since we allocate
364 	 * at least 3 L2 page tables, we are guaranteed to encounter at
365 	 * least one 16K aligned region.
366 	 */
367 
368 #ifdef VERBOSE_INIT_ARM
369 	printf("Allocating page tables\n");
370 #endif
371 
372 	free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
373 
374 #ifdef VERBOSE_INIT_ARM
375 	printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
376 	       physical_freestart, free_pages, free_pages);
377 #endif
378 
379 	/* Define a macro to simplify memory allocation */
380 #define	valloc_pages(var, np)				\
381 	alloc_pages((var).pv_pa, (np));			\
382 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
383 
384 #define alloc_pages(var, np)				\
385 	physical_freeend -= ((np) * PAGE_SIZE);		\
386 	if (physical_freeend < physical_freestart)	\
387 		panic("initarm: out of memory");	\
388 	(var) = physical_freeend;			\
389 	free_pages -= (np);				\
390 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
391 
392 	loop1 = 0;
393 	kernel_l1pt.pv_pa = 0;
394 	kernel_l1pt.pv_va = 0;
395 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
396 		/* Are we 16KB aligned for an L1 ? */
397 		if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
398 		    && kernel_l1pt.pv_pa == 0) {
399 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
400 		} else {
401 			valloc_pages(kernel_pt_table[loop1],
402 			    L2_TABLE_SIZE / PAGE_SIZE);
403 			++loop1;
404 		}
405 	}
406 
407 	/* This should never be able to happen but better confirm that. */
408 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
409 		panic("initarm: Failed to align the kernel page directory");
410 
411 	/*
412 	 * Allocate a page for the system page mapped to V0x00000000
413 	 * This page will just contain the system vectors and can be
414 	 * shared by all processes.
415 	 */
416 	alloc_pages(systempage.pv_pa, 1);
417 
418 	/* Allocate stacks for all modes */
419 	valloc_pages(irqstack, IRQ_STACK_SIZE);
420 	valloc_pages(abtstack, ABT_STACK_SIZE);
421 	valloc_pages(undstack, UND_STACK_SIZE);
422 	valloc_pages(kernelstack, UPAGES);
423 
424 	/* Allocate enough pages for cleaning the Mini-Data cache. */
425 	KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
426 	valloc_pages(minidataclean, 1);
427 
428 #ifdef VERBOSE_INIT_ARM
429 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
430 	    irqstack.pv_va);
431 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
432 	    abtstack.pv_va);
433 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
434 	    undstack.pv_va);
435 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
436 	    kernelstack.pv_va);
437 #endif
438 
439 	/*
440 	 * XXX Defer this to later so that we can reclaim the memory
441 	 * XXX used by the RedBoot page tables.
442 	 */
443 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
444 
445 	/*
446 	 * Ok we have allocated physical pages for the primary kernel
447 	 * page tables
448 	 */
449 
450 #ifdef VERBOSE_INIT_ARM
451 	printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
452 #endif
453 
454 	/*
455 	 * Now we start construction of the L1 page table
456 	 * We start by mapping the L2 page tables into the L1.
457 	 * This means that we can replace L1 mappings later on if necessary
458 	 */
459 	l1pagetable = kernel_l1pt.pv_pa;
460 
461 	/* Map the L2 pages tables in the L1 page table */
462 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00400000 - 1),
463 	    &kernel_pt_table[KERNEL_PT_SYS]);
464 	for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
465 		pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
466 		    &kernel_pt_table[KERNEL_PT_KERNEL + loop]);
467 	pmap_link_l2pt(l1pagetable, HDLG_IOPXS_VBASE,
468 	    &kernel_pt_table[KERNEL_PT_IOPXS]);
469 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
470 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
471 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
472 
473 	/* update the top of the kernel VM */
474 	pmap_curmaxkvaddr =
475 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
476 
477 #ifdef VERBOSE_INIT_ARM
478 	printf("Mapping kernel\n");
479 #endif
480 
481 	/* Now we fill in the L2 pagetable for the kernel static code/data */
482 	{
483 		extern char etext[], _end[];
484 		size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
485 		size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
486 		u_int logical;
487 
488 		textsize = (textsize + PGOFSET) & ~PGOFSET;
489 		totalsize = (totalsize + PGOFSET) & ~PGOFSET;
490 
491 		logical = 0x00200000;	/* offset of kernel in RAM */
492 
493 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
494 		    physical_start + logical, textsize,
495 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
496 		logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
497 		    physical_start + logical, totalsize - textsize,
498 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
499 	}
500 
501 #ifdef VERBOSE_INIT_ARM
502 	printf("Constructing L2 page tables\n");
503 #endif
504 
505 	/* Map the stack pages */
506 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
507 	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
508 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
509 	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
510 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
511 	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
512 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
513 	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
514 
515 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
516 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
517 
518 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
519 		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
520 		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
521 		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
522 	}
523 
524 	/* Map the Mini-Data cache clean area. */
525 	xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
526 	    minidataclean.pv_pa);
527 
528 	/* Map the vector page. */
529 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
530 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
531 
532 	/* Map the statically mapped devices. */
533 	pmap_devmap_bootstrap(l1pagetable, hdlg_devmap);
534 
535 	/*
536 	 * Give the XScale global cache clean code an appropriately
537 	 * sized chunk of unmapped VA space starting at 0xff000000
538 	 * (our device mappings end before this address).
539 	 */
540 	xscale_cache_clean_addr = 0xff000000U;
541 
542 	/*
543 	 * Now we have the real page tables in place so we can switch to them.
544 	 * Once this is done we will be running with the REAL kernel page
545 	 * tables.
546 	 */
547 
548 	/*
549 	 * Update the physical_freestart/physical_freeend/free_pages
550 	 * variables.
551 	 */
552 	{
553 		extern char _end[];
554 
555 		physical_freestart = physical_start +
556 		    (((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
557 		     KERNEL_BASE);
558 		physical_freeend = physical_end;
559 		free_pages =
560 		    (physical_freeend - physical_freestart) / PAGE_SIZE;
561 	}
562 
563 	/* Switch tables */
564 #ifdef VERBOSE_INIT_ARM
565 	printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
566 	       physical_freestart, free_pages, free_pages);
567 	printf("switching to new L1 page table  @%#lx...", kernel_l1pt.pv_pa);
568 #endif
569 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
570 	setttb(kernel_l1pt.pv_pa);
571 	cpu_tlb_flushID();
572 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
573 
574 	/*
575 	 * Moved from cpu_startup() as data_abort_handler() references
576 	 * this during uvm init
577 	 */
578 	proc0paddr = (struct user *)kernelstack.pv_va;
579 	lwp0.l_addr = proc0paddr;
580 
581 #ifdef VERBOSE_INIT_ARM
582 	printf("done!\n");
583 #endif
584 
585 #ifdef VERBOSE_INIT_ARM
586 	printf("bootstrap done.\n");
587 #endif
588 
589 	arm32_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
590 
591 	/*
592 	 * Pages were allocated during the secondary bootstrap for the
593 	 * stacks for different CPU modes.
594 	 * We must now set the r13 registers in the different CPU modes to
595 	 * point to these stacks.
596 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
597 	 * of the stack memory.
598 	 */
599 #ifdef VERBOSE_INIT_ARM
600 	printf("init subsystems: stacks ");
601 #endif
602 
603 	set_stackptr(PSR_IRQ32_MODE,
604 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
605 	set_stackptr(PSR_ABT32_MODE,
606 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
607 	set_stackptr(PSR_UND32_MODE,
608 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
609 
610 	/*
611 	 * Well we should set a data abort handler.
612 	 * Once things get going this will change as we will need a proper
613 	 * handler.
614 	 * Until then we will use a handler that just panics but tells us
615 	 * why.
616 	 * Initialisation of the vectors will just panic on a data abort.
617 	 * This just fills in a slightly better one.
618 	 */
619 #ifdef VERBOSE_INIT_ARM
620 	printf("vectors ");
621 #endif
622 	data_abort_handler_address = (u_int)data_abort_handler;
623 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
624 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
625 
626 	/* Initialise the undefined instruction handlers */
627 #ifdef VERBOSE_INIT_ARM
628 	printf("undefined ");
629 #endif
630 	undefined_init();
631 
632 	/* Load memory into UVM. */
633 #ifdef VERBOSE_INIT_ARM
634 	printf("page ");
635 #endif
636 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
637 	uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
638 	    atop(physical_freestart), atop(physical_freeend),
639 	    VM_FREELIST_DEFAULT);
640 
641 	/* Boot strap pmap telling it where the kernel page table is */
642 #ifdef VERBOSE_INIT_ARM
643 	printf("pmap ");
644 #endif
645 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, KERNEL_VM_BASE,
646 	    KERNEL_VM_BASE + KERNEL_VM_SIZE);
647 
648 	/* Setup the IRQ system */
649 #ifdef VERBOSE_INIT_ARM
650 	printf("irq ");
651 #endif
652 	i80321_intr_init();
653 
654 #ifdef VERBOSE_INIT_ARM
655 	printf("done.\n");
656 #endif
657 
658 #ifdef BOOTHOWTO
659 	boothowto = BOOTHOWTO;
660 #endif
661 
662 #ifdef IPKDB
663 	/* Initialise ipkdb */
664 	ipkdb_init();
665 	if (boothowto & RB_KDB)
666 		ipkdb_connect(0);
667 #endif
668 
669 #if NKSYMS || defined(DDB) || defined(LKM)
670 	/* Firmware doesn't load symbols. */
671 	ksyms_init(0, NULL, NULL);
672 #endif
673 
674 #ifdef DDB
675 	db_machine_init();
676 	if (boothowto & RB_KDB)
677 		Debugger();
678 #endif
679 
680 	/* We return the new stack pointer address */
681 	return (kernelstack.pv_va + USPACE_SVC_STACK_TOP);
682 }
683 
684 /*
685  * void cpu_reboot(int howto, char *bootstr)
686  *
687  * Reboots the system
688  *
689  * Deal with any syncing, unmounting, dumping and shutdown hooks,
690  * then reset the CPU.
691  */
692 void
693 cpu_reboot(int howto, char *bootstr)
694 {
695 
696 	/*
697 	 * If we are still cold then hit the air brakes
698 	 * and crash to earth fast
699 	 */
700 	if (cold) {
701 		*(volatile uint8_t *)HDLG_LEDCTRL |= LEDCTRL_STAT_RED;
702 		howto |= RB_HALT;
703 		goto haltsys;
704 	}
705 
706 	/* Disable console buffering */
707 
708 	/*
709 	 * If RB_NOSYNC was not specified sync the discs.
710 	 * Note: Unless cold is set to 1 here, syslogd will die during the
711 	 * unmount.  It looks like syslogd is getting woken up only to find
712 	 * that it cannot page part of the binary in as the filesystem has
713 	 * been unmounted.
714 	 */
715 	if ((howto & RB_NOSYNC) == 0) {
716 		bootsync();
717 		/*resettodr();*/
718 	}
719 
720 	/* wait 1s */
721 	delay(1 * 1000 * 1000);
722 
723 	/* Say NO to interrupts */
724 	splhigh();
725 
726 	/* Do a dump if requested. */
727 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) {
728 		dumpsys();
729 	}
730 
731 haltsys:
732 	/* Run any shutdown hooks */
733 	doshutdownhooks();
734 
735 	/* Make sure IRQ's are disabled */
736 	IRQdisable;
737 
738 	if (howto & RB_HALT) {
739 		*(volatile uint8_t *)HDLG_PWRMNG = PWRMNG_POWOFF;
740 		delay(3 * 1000 * 1000);	/* wait 3s */
741 
742 		printf("SHUTDOWN FAILED!\n");
743 		printf("The operating system has halted.\n");
744 		printf("Please press any key to reboot.\n\n");
745 		cngetc();
746 	}
747 
748 	printf("rebooting...\n\r");
749 
750 	(void)disable_interrupts(I32_bit|F32_bit);
751 	cpu_idcache_wbinv_all();
752 	cpu_drain_writebuf();
753 
754 	*(volatile uint8_t *)HDLG_PWRMNG = PWRMNG_RESET;
755 	delay(1 * 1000 * 1000);	/* wait 1s */
756 
757 	/* ...and if that didn't work, just croak. */
758 	printf("RESET FAILED!\n");
759 	for (;;) {
760 		continue;
761 	}
762 }
763 
764 /*
765  * console
766  */
767 #include "com.h"
768 #if NCOM > 0
769 #include <dev/ic/comreg.h>
770 #include <dev/ic/comvar.h>
771 #endif
772 
773 /*
774  * Define the default console speed for the board.  This is generally
775  * what the firmware provided with the board defaults to.
776  */
777 #ifndef CONSPEED
778 #define CONSPEED B115200
779 #endif /* ! CONSPEED */
780 
781 #ifndef CONUNIT
782 #define	CONUNIT	0
783 #endif
784 
785 #ifndef CONMODE
786 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
787 #endif
788 
789 int comcnspeed = CONSPEED;
790 int comcnmode = CONMODE;
791 int comcnunit = CONUNIT;
792 
793 #if KGDB
794 #ifndef KGDB_DEVNAME
795 #error Must define KGDB_DEVNAME
796 #endif
797 const char kgdb_devname[] = KGDB_DEVNAME;
798 
799 #ifndef KGDB_DEVADDR
800 #error Must define KGDB_DEVADDR
801 #endif
802 unsigned long kgdb_devaddr = KGDB_DEVADDR;
803 
804 #ifndef KGDB_DEVRATE
805 #define KGDB_DEVRATE	CONSPEED
806 #endif
807 int kgdb_devrate = KGDB_DEVRATE;
808 
809 #ifndef KGDB_DEVMODE
810 #define KGDB_DEVMODE	CONMODE
811 #endif
812 int kgdb_devmode = KGDB_DEVMODE;
813 #endif /* KGDB */
814 
815 void
816 consinit(void)
817 {
818 	static const bus_addr_t comcnaddrs[] = {
819 		HDLG_UART1,		/* com0 */
820 	};
821 	static int consinit_called;
822 
823 	if (consinit_called)
824 		return;
825 	consinit_called = 1;
826 
827 	/*
828 	 * Console devices are mapped VA==PA.  Our devmap reflects
829 	 * this, so register it now so drivers can map the console
830 	 * device.
831 	 */
832 	pmap_devmap_register(hdlg_devmap);
833 
834 #if NCOM > 0
835 	if (comcnattach(&obio_bs_tag, comcnaddrs[comcnunit], comcnspeed,
836 	    COM_FREQ, COM_TYPE_NORMAL, comcnmode))
837 		panic("can't init serial console @%lx", comcnaddrs[comcnunit]);
838 #else
839 	panic("serial console @%lx not configured", comcnaddrs[comcnunit]);
840 #endif
841 #if KGDB
842 #if NCOM > 0
843 	if (strcmp(kgdb_devname, "com") == 0) {
844 		com_kgdb_attach(&obio_bs_tag, kgdb_devaddr, kgdb_devrate,
845 				COM_FREQ, COM_TYPE_NORMAL, kgdb_devmode);
846 	}
847 #endif	/* NCOM > 0 */
848 #endif	/* KGDB */
849 }
850