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