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