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