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