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