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