xref: /netbsd/sys/arch/acorn32/acorn32/rpc_machdep.c (revision bf9ec67e)
1 /*	$NetBSD: rpc_machdep.c,v 1.36 2002/05/03 16:45:21 rjs Exp $	*/
2 
3 /*
4  * Copyright (c) 2000-2001 Reinoud Zandijk.
5  * Copyright (c) 1994-1998 Mark Brinicombe.
6  * Copyright (c) 1994 Brini.
7  * All rights reserved.
8  *
9  * This code is derived from software written for Brini by Mark Brinicombe
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by Brini.
22  * 4. The name of the company nor the name of the author may be used to
23  *    endorse or promote products derived from this software without specific
24  *    prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * RiscBSD kernel project
39  *
40  * machdep.c
41  *
42  * Machine dependant functions for kernel setup
43  *
44  * This file still needs a lot of work
45  *
46  * Created      : 17/09/94
47  * Updated for new bootloader 22/10/00
48  */
49 
50 #include "opt_ddb.h"
51 #include "opt_pmap_debug.h"
52 #include "vidcvideo.h"
53 #include "rpckbd.h"
54 #include "pckbc.h"
55 
56 #include <sys/param.h>
57 
58 __KERNEL_RCSID(0, "$NetBSD: rpc_machdep.c,v 1.36 2002/05/03 16:45:21 rjs Exp $");
59 
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/reboot.h>
63 #include <sys/proc.h>
64 #include <sys/msgbuf.h>
65 #include <sys/exec.h>
66 
67 #include <dev/cons.h>
68 
69 #include <machine/db_machdep.h>
70 #include <ddb/db_sym.h>
71 #include <ddb/db_extern.h>
72 
73 #include <uvm/uvm.h>
74 
75 #include <machine/signal.h>
76 #include <machine/frame.h>
77 #include <machine/bootconfig.h>
78 #include <machine/cpu.h>
79 #include <machine/io.h>
80 #include <machine/intr.h>
81 #include <arm/cpuconf.h>
82 #include <arm/arm32/katelib.h>
83 #include <arm/arm32/machdep.h>
84 #include <machine/vconsole.h>
85 #include <arm/undefined.h>
86 #include <machine/rtc.h>
87 #include <machine/bus.h>
88 
89 #include <arm/iomd/vidc.h>
90 #include <arm/iomd/iomdreg.h>
91 #include <arm/iomd/iomdvar.h>
92 
93 #include <arm/iomd/vidcvideo.h>
94 
95 #include <sys/device.h>
96 #include <arm/iomd/rpckbdvar.h>
97 #include <dev/ic/pckbcvar.h>
98 
99 #include "opt_ipkdb.h"
100 
101 /*
102  * Address to call from cpu_reset() to reset the machine.
103  * This is machine architecture dependant as it varies depending
104  * on where the ROM appears when you turn the MMU off.
105  */
106 u_int cpu_reset_address = 0x0; /* XXX 0x3800000 too for rev0 RiscPC 600 */
107 
108 
109 #define VERBOSE_INIT_ARM
110 
111 
112 /* Define various stack sizes in pages */
113 #define IRQ_STACK_SIZE	1
114 #define ABT_STACK_SIZE	1
115 #ifdef IPKDB
116 #define UND_STACK_SIZE	2
117 #else
118 #define UND_STACK_SIZE	1
119 #endif
120 
121 
122 BootConfig bootconfig;		/* Boot config storage */
123 videomemory_t videomemory;	/* Video memory descriptor */
124 
125 char *boot_args = NULL;		/* holds the pre-processed boot arguments */
126 extern char *booted_kernel;	/* used for ioctl to retrieve booted kernel */
127 
128 extern int       *vidc_base;
129 extern u_int32_t  iomd_base;
130 extern struct bus_space iomd_bs_tag;
131 
132 paddr_t physical_start;
133 paddr_t physical_freestart;
134 paddr_t physical_freeend;
135 paddr_t physical_end;
136 paddr_t dma_range_begin;
137 paddr_t dma_range_end;
138 
139 u_int free_pages;
140 int physmem = 0;
141 paddr_t memoryblock_end;
142 
143 #ifndef PMAP_STATIC_L1S
144 int max_processes = 64;			/* Default number */
145 #endif	/* !PMAP_STATIC_L1S */
146 
147 u_int videodram_size = 0;		/* Amount of DRAM to reserve for video */
148 
149 /* Physical and virtual addresses for some global pages */
150 pv_addr_t systempage;
151 pv_addr_t irqstack;
152 pv_addr_t undstack;
153 pv_addr_t abtstack;
154 pv_addr_t kernelstack;
155 
156 paddr_t msgbufphys;
157 
158 extern u_int data_abort_handler_address;
159 extern u_int prefetch_abort_handler_address;
160 extern u_int undefined_handler_address;
161 
162 #ifdef PMAP_DEBUG
163 extern int pmap_debug_level;
164 #endif	/* PMAP_DEBUG */
165 
166 #define	KERNEL_PT_VMEM		0	/* Page table for mapping video memory */
167 #define	KERNEL_PT_SYS		1	/* Page table for mapping proc0 zero page */
168 #define	KERNEL_PT_KERNEL	2	/* Page table for mapping kernel */
169 #define	KERNEL_PT_VMDATA	3	/* Page tables for mapping kernel VM */
170 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
171 #define	NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
172 
173 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
174 
175 struct user *proc0paddr;
176 
177 #ifdef CPU_SA110
178 #define CPU_SA110_CACHE_CLEAN_SIZE (0x4000 * 2)
179 static vaddr_t sa110_cc_base;
180 #endif	/* CPU_SA110 */
181 
182 /* Prototypes */
183 void physcon_display_base	__P((u_int addr));
184 extern void consinit		__P((void));
185 
186 void data_abort_handler		__P((trapframe_t *frame));
187 void prefetch_abort_handler	__P((trapframe_t *frame));
188 void undefinedinstruction_bounce	__P((trapframe_t *frame));
189 
190 static void canonicalise_bootconfig __P((BootConfig *, BootConfig *));
191 static void process_kernel_args	__P((void));
192 
193 extern void dump_spl_masks	__P((void));
194 extern void vidcrender_reinit	__P((void));
195 extern int vidcrender_blank	__P((struct vconsole *vc, int type));
196 
197 void rpc_sa110_cc_setup		__P((void));
198 
199 extern void parse_mi_bootargs	__P((char *args));
200 void parse_rpc_bootargs		__P((char *args));
201 
202 extern void dumpsys		__P((void));
203 
204 
205 #if NVIDCVIDEO > 0
206 #	define console_flush()		/* empty */;
207 #else
208 	extern void console_flush	__P((void));
209 #endif
210 
211 
212 #define panic2(a) { \
213 	memset((void *) (videomemory.vidm_vbase), 0x55, 50*1024); \
214 	consinit(); \
215 	panic a; \
216 	}
217 
218 /*
219  * void cpu_reboot(int howto, char *bootstr)
220  *
221  * Reboots the system
222  *
223  * Deal with any syncing, unmounting, dumping and shutdown hooks,
224  * then reset the CPU.
225  */
226 
227 /* NOTE: These variables will be removed, well some of them */
228 
229 extern u_int spl_mask;
230 extern u_int current_mask;
231 
232 void
233 cpu_reboot(howto, bootstr)
234 	int howto;
235 	char *bootstr;
236 {
237 
238 #ifdef DIAGNOSTIC
239 	printf("boot: howto=%08x curproc=%p\n", howto, curproc);
240 
241 	printf("ipl_bio=%08x ipl_net=%08x ipl_tty=%08x ipl_imp=%08x\n",
242 	    irqmasks[IPL_BIO], irqmasks[IPL_NET], irqmasks[IPL_TTY],
243 	    irqmasks[IPL_IMP]);
244 	printf("ipl_audio=%08x ipl_clock=%08x ipl_none=%08x\n",
245 	    irqmasks[IPL_AUDIO], irqmasks[IPL_CLOCK], irqmasks[IPL_NONE]);
246 
247 	dump_spl_masks();
248 #endif	/* DIAGNOSTIC */
249 
250 	/*
251 	 * If we are still cold then hit the air brakes
252 	 * and crash to earth fast
253 	 */
254 	if (cold) {
255 		doshutdownhooks();
256 		printf("Halted while still in the ICE age.\n");
257 		printf("The operating system has halted.\n");
258 		printf("Please press any key to reboot.\n\n");
259 		cngetc();
260 		printf("rebooting...\n");
261 		cpu_reset();
262 		/*NOTREACHED*/
263 	}
264 
265 	/* Disable console buffering */
266 	cnpollc(1);
267 
268 	/*
269 	 * If RB_NOSYNC was not specified sync the discs.
270 	 * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
271 	 * It looks like syslogd is getting woken up only to find that it cannot
272 	 * page part of the binary in as the filesystem has been unmounted.
273 	 */
274 	if (!(howto & RB_NOSYNC))
275 		bootsync();
276 
277 	/* Say NO to interrupts */
278 	splhigh();
279 
280 	/* Do a dump if requested. */
281 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
282 		dumpsys();
283 
284 	/*
285 	 * Auto reboot overload protection
286 	 *
287 	 * This code stops the kernel entering an endless loop of reboot
288 	 * - panic cycles. This will have the effect of stopping further
289 	 * reboots after it has rebooted 8 times after panics. A clean
290 	 * halt or reboot will reset the counter.
291 	 */
292 
293 	/*
294 	 * Have we done 8 reboots in a row ? If so halt rather than reboot
295 	 * since 8 panics in a row without 1 clean halt means something is
296 	 * seriously wrong.
297 	 */
298 	if (cmos_read(RTC_ADDR_REBOOTCNT) > 8)
299 		howto |= RB_HALT;
300 
301 	/*
302 	 * If we are rebooting on a panic then up the reboot count
303 	 * otherwise reset.
304 	 * This will thus be reset if the kernel changes the boot action from
305 	 * reboot to halt due to too any reboots.
306 	 */
307 	if (((howto & RB_HALT) == 0) && panicstr)
308 		cmos_write(RTC_ADDR_REBOOTCNT,
309 		   cmos_read(RTC_ADDR_REBOOTCNT) + 1);
310 	else
311 		cmos_write(RTC_ADDR_REBOOTCNT, 0);
312 
313 	/*
314 	 * If we need a RiscBSD reboot, request it buy setting a bit in
315 	 * the CMOS RAM. This can be detected by the RiscBSD boot loader
316 	 * during a RISCOS boot. No other way to do this as RISCOS is in ROM.
317 	 */
318 	if ((howto & RB_HALT) == 0)
319 		cmos_write(RTC_ADDR_BOOTOPTS,
320 		    cmos_read(RTC_ADDR_BOOTOPTS) | 0x02);
321 
322 	/* Run any shutdown hooks */
323 	doshutdownhooks();
324 
325 	/* Make sure IRQ's are disabled */
326 	IRQdisable;
327 
328 	if (howto & RB_HALT) {
329 		printf("The operating system has halted.\n");
330 		printf("Please press any key to reboot.\n\n");
331 		cngetc();
332 	}
333 
334 	printf("rebooting...\n");
335 	cpu_reset();
336 	/*NOTREACHED*/
337 }
338 
339 
340 /*
341  * u_int initarm(BootConfig *bootconf)
342  *
343  * Initial entry point on startup. This gets called before main() is
344  * entered.
345  * It should be responsible for setting up everything that must be
346  * in place when main is called.
347  * This includes
348  *   Taking a copy of the boot configuration structure.
349  *   Initialising the physical console so characters can be printed.
350  *   Setting up page tables for the kernel
351  *   Relocating the kernel to the bottom of physical memory
352  */
353 
354 /*
355  * this part is completely rewritten for the new bootloader ... It features
356  * a flat memory map with a mapping comparable to the EBSA arm32 machine
357  * to boost the portability and likeness of the code
358  */
359 
360 /*
361  * Mapping table for core kernel memory. This memory is mapped at init
362  * time with section mappings.
363  *
364  * XXX One big assumption in the current architecture seems that the kernel is
365  * XXX supposed to be mapped into bootconfig.dram[0].
366  */
367 
368 #define ONE_MB	0x100000
369 
370 struct l1_sec_map {
371 	vaddr_t		va;
372 	paddr_t		pa;
373 	vsize_t		size;
374 	vm_prot_t	prot;
375 	int		cache;
376 } l1_sec_table[] = {
377 	/* Map 1Mb section for VIDC20 */
378 	{ VIDC_BASE,		VIDC_HW_BASE,
379 	    ONE_MB,		VM_PROT_READ|VM_PROT_WRITE,
380 	    PTE_NOCACHE },
381 
382 	/* Map 1Mb section from IOMD */
383 	{ IOMD_BASE,		IOMD_HW_BASE,
384 	    ONE_MB,		VM_PROT_READ|VM_PROT_WRITE,
385 	    PTE_NOCACHE },
386 
387 	/* Map 1Mb of COMBO (and module space) */
388 	{ IO_BASE,		IO_HW_BASE,
389 	    ONE_MB,		VM_PROT_READ|VM_PROT_WRITE,
390 	    PTE_NOCACHE },
391 
392 	{ 0, 0, 0, 0, 0 }
393 };
394 
395 
396 static void
397 canonicalise_bootconfig(bootconf, raw_bootconf)
398 	BootConfig *bootconf;
399 	BootConfig *raw_bootconf;
400 {
401 	BootConfig_v1 *old_v1_style;
402 	int block;
403 
404 	/* check for bootconfig v2+ structure */
405 	if (raw_bootconf->magic == BOOTCONFIG_MAGIC) {
406 		/* v2+ cleaned up structure found */
407 		*bootconf = *raw_bootconf;
408 		return;
409 	} else {
410 		/* old messy structure assumed */
411 		old_v1_style = (BootConfig_v1 *) raw_bootconf;
412 
413 		bootconf->magic			= old_v1_style->magic;
414 		bootconf->version		= 1;
415 		memcpy(bootconf->machine_id, old_v1_style->machine_id, 4);
416 		memcpy(bootconf->kernelname, old_v1_style->kernelname, 80);
417 		memcpy(bootconf->args, (char *) old_v1_style->argvirtualbase, 512);
418 
419 		bootconf->kernvirtualbase	= old_v1_style->kernvirtualbase;
420 		bootconf->kernphysicalbase	= old_v1_style->kernphysicalbase;
421 		bootconf->kernsize		= old_v1_style->kernsize;
422 		bootconf->scratchvirtualbase	= old_v1_style->scratchvirtualbase;
423 		bootconf->scratchphysicalbase	= old_v1_style->scratchphysicalbase;
424 		bootconf->scratchsize		= old_v1_style->scratchsize;
425 
426 		/* this shouldn't be happening */
427 		bootconf->ksym_start		= 0;
428 		bootconf->ksym_end		= 0;
429 
430 		/* Mode definition file */
431 		bootconf->MDFvirtualbase	= 0;
432 		bootconf->MDFphysicalbase	= 0;
433 		bootconf->MDFsize		= 0;
434 
435 		bootconf->display_phys		= old_v1_style->display_phys;
436 		bootconf->display_start		= old_v1_style->display_start;
437 		bootconf->display_size		= old_v1_style->display_size;
438 		bootconf->width			= old_v1_style->width;
439 		bootconf->height		= old_v1_style->height;
440 		bootconf->log2_bpp		= old_v1_style->log2_bpp;
441 		bootconf->framerate		= old_v1_style->framerate;
442 
443 		memset(bootconf->reserved, 0, 512);
444 
445 		bootconf->pagesize		= old_v1_style->pagesize;
446 		bootconf->drampages		= old_v1_style->drampages;
447 		bootconf->vrampages		= old_v1_style->vrampages;
448 		bootconf->dramblocks		= old_v1_style->dramblocks;
449 		bootconf->vramblocks		= old_v1_style->vramblocks;
450 
451 		for(block=0; block<4; block++) {
452 			bootconf->dram[block].address = old_v1_style->dram[block].address;
453 			bootconf->dram[block].pages   = old_v1_style->dram[block].pages;
454 			bootconf->dram[block].flags   = 0;	/* XXX */
455 		};
456 
457 		bootconf->vram[0].address = old_v1_style->vram[0].address;
458 		bootconf->vram[0].pages   = old_v1_style->vram[0].pages;
459 	};
460 }
461 
462 
463 u_int
464 initarm(void *cookie)
465 {
466 	BootConfig *raw_bootconf = cookie;
467 	int loop;
468 	int loop1;
469 	u_int logical;
470 	u_int kerneldatasize;
471 	u_int l1pagetable;
472 	struct exec *kernexec = (struct exec *)KERNEL_TEXT_BASE;
473 	pv_addr_t kernel_l1pt;
474 	pv_addr_t kernel_ptpt;
475 
476 	/*
477 	 * Heads up ... Setup the CPU / MMU / TLB functions
478 	 */
479 	set_cpufuncs();
480 
481 	/* canonicalise the boot configuration structure to alow versioning */
482 	canonicalise_bootconfig(&bootconfig, raw_bootconf);
483 	booted_kernel = bootconfig.kernelname;
484 
485 	/* if the wscons interface is used, switch off VERBOSE booting :( */
486 #if NVIDCVIDEO>0
487 #	undef VERBOSE_INIT_ARM
488 #	undef PMAP_DEBUG
489 #endif
490 
491 	/*
492 	 * Initialise the video memory descriptor
493 	 *
494 	 * Note: all references to the video memory virtual/physical address
495 	 * should go via this structure.
496 	 */
497 
498 	/* Hardwire it on the place the bootloader tells us */
499 	videomemory.vidm_vbase = bootconfig.display_start;
500 	videomemory.vidm_pbase = bootconfig.display_phys;
501 	videomemory.vidm_size = bootconfig.display_size;
502 	if (bootconfig.vram[0].pages)
503 		videomemory.vidm_type = VIDEOMEM_TYPE_VRAM;
504 	else
505 		videomemory.vidm_type = VIDEOMEM_TYPE_DRAM
506 	;
507 	vidc_base = (int *) VIDC_HW_BASE;
508 	iomd_base =         IOMD_HW_BASE;
509 
510 	/*
511 	 * Initialise the physical console
512 	 * This is done in main() but for the moment we do it here so that
513 	 * we can use printf in initarm() before main() has been called.
514 	 * only for `vidcconsole!' ... not wscons
515 	 */
516 #if NVIDCVIDEO == 0
517 	consinit();
518 #endif
519 
520 	/*
521 	 * Initialise the diagnostic serial console
522 	 * This allows a means of generating output during initarm().
523 	 * Once all the memory map changes are complete we can call consinit()
524 	 * and not have to worry about things moving.
525 	 */
526 /*      fcomcnattach(DC21285_ARMCSR_BASE, comcnspeed, comcnmode);*/
527 	/* XXX snif .... i am still not able to this */
528 
529 	/*
530 	 * We have the following memory map (derived from EBSA)
531 	 *
532 	 * virtual address == physical address apart from the areas:
533 	 * 0x00000000 -> 0x000fffff which is mapped to
534 	 * top 1MB of physical memory
535 	 * 0xf0000000 -> 0xf0ffffff wich is mapped to
536 	 * physical address 0x01000000 -> 0x01ffffff (DRAM0a, dram[0])
537 	 *
538 	 * This means that the kernel is mapped suitably for continuing
539 	 * execution, all I/O is mapped 1:1 virtual to physical and
540 	 * physical memory is accessible.
541 	 *
542 	 * The initarm() has the responsibility for creating the kernel
543 	 * page tables.
544 	 * It must also set up various memory pointers that are used
545 	 * by pmap etc.
546 	 */
547 
548 /** START OF REAL NEW STUFF */
549 
550 	/* Check to make sure the page size is correct */
551 	if (NBPG != bootconfig.pagesize)
552 		panic2(("Page size is %d bytes instead of %d !! (huh?)\n", bootconfig.pagesize, NBPG));
553 
554 	/* process arguments */
555 	process_kernel_args();
556 
557 
558 	/* Now set up the page tables for the kernel ... this part is copied
559 	 * in a (modified?) way from the EBSA machine port....
560 	 */
561 
562 #ifdef VERBOSE_INIT_ARM
563 	printf("Allocating page tables\n");
564 #endif
565 	/*
566 	 * Set up the variables that define the availablilty of physcial
567 	 * memory
568 	 */
569 	physical_start = bootconfig.dram[0].address;
570 
571 	physical_end = 0;
572 	for (loop = 0, physmem = 0; loop < bootconfig.dramblocks; ++loop) {
573 		memoryblock_end = bootconfig.dram[loop].address + bootconfig.dram[loop].pages * NBPG;
574 		if (memoryblock_end > physical_end) physical_end = memoryblock_end;
575 		physmem += bootconfig.dram[loop].pages;
576 	};
577 	physical_freestart = physical_start;
578 	free_pages = bootconfig.drampages;
579 	physical_freeend = physical_end;
580 
581 	/* constants for now, but might be changed/configured */
582 	dma_range_begin = (paddr_t) physical_start;
583 	dma_range_end   = (paddr_t) MIN(physical_end, 512*1024*1024);
584 /* XXX HACK HACK XXX */
585 /* dma_range_end   = 0x18000000; */
586 
587 	/* AHUM !! set this variable ... it was set up in the old 1st stage bootloader */
588 	kerneldatasize = bootconfig.kernsize + bootconfig.MDFsize;
589 
590 	/* Update the address of the first free page of physical memory */
591 	physical_freestart += bootconfig.kernsize + bootconfig.MDFsize + bootconfig.scratchsize;
592 	free_pages -= (physical_freestart - physical_start) / NBPG;
593 
594 	/* Define a macro to simplify memory allocation */
595 #define	valloc_pages(var, np)			\
596 	alloc_pages((var).pv_pa, (np));		\
597 	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
598 
599 #define alloc_pages(var, np)			\
600 	(var) = physical_freestart;		\
601 	physical_freestart += ((np) * NBPG);	\
602 	free_pages -= (np);			\
603 	memset((char *)(var), 0, ((np) * NBPG));
604 
605 	loop1 = 0;
606 	kernel_l1pt.pv_pa = 0;
607 	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
608 		/* Are we 16KB aligned for an L1 ? */
609 		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
610 		    && kernel_l1pt.pv_pa == 0) {
611 			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / NBPG);
612 		} else {
613 			alloc_pages(kernel_pt_table[loop1].pv_pa,
614 			    L2_TABLE_SIZE / NBPG);
615 			kernel_pt_table[loop1].pv_va =
616 			    kernel_pt_table[loop1].pv_pa;
617 			++loop1;
618 		}
619 	}
620 
621 
622 #ifdef DIAGNOSTIC
623 	/* This should never be able to happen but better confirm that. */
624 	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
625 		panic2(("initarm: Failed to align the kernel page directory\n"));
626 #endif
627 
628 	/*
629 	 * Allocate a page for the system page mapped to V0x00000000
630 	 * This page will just contain the system vectors and can be
631 	 * shared by all processes.
632 	 */
633 	alloc_pages(systempage.pv_pa, 1);
634 
635 	/* Allocate a page for the page table to map kernel page tables*/
636 	valloc_pages(kernel_ptpt, L2_TABLE_SIZE / NBPG);
637 
638 	/* Allocate stacks for all modes */
639 	valloc_pages(irqstack, IRQ_STACK_SIZE);
640 	valloc_pages(abtstack, ABT_STACK_SIZE);
641 	valloc_pages(undstack, UND_STACK_SIZE);
642 	valloc_pages(kernelstack, UPAGES);
643 
644 #ifdef VERBOSE_INIT_ARM
645 	printf("Setting up stacks :\n");
646 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa, irqstack.pv_va);
647 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa, abtstack.pv_va);
648 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa, undstack.pv_va);
649 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa, kernelstack.pv_va);
650 	printf("\n");
651 #endif
652 
653 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / NBPG);
654 
655 #ifdef CPU_SA110
656 	/*
657 	 * XXX totally stuffed hack to work round problems introduced
658 	 * in recent versions of the pmap code. Due to the calls used there
659 	 * we cannot allocate virtual memory during bootstrap.
660 	 */
661 	sa110_cc_base = (KERNEL_BASE + (physical_freestart - physical_start)
662 	    + (CPU_SA110_CACHE_CLEAN_SIZE - 1))
663 	    & ~(CPU_SA110_CACHE_CLEAN_SIZE - 1);
664 #endif	/* CPU_SA110 */
665 
666 	/*
667 	 * Ok we have allocated physical pages for the primary kernel
668 	 * page tables
669 	 */
670 
671 #ifdef VERBOSE_INIT_ARM
672 	printf("Creating L1 page table\n");
673 #endif
674 
675 	/*
676 	 * Now we start construction of the L1 page table
677 	 * We start by mapping the L2 page tables into the L1.
678 	 * This means that we can replace L1 mappings later on if necessary
679 	 */
680 	l1pagetable = kernel_l1pt.pv_pa;
681 
682 	/* Map the L2 pages tables in the L1 page table */
683 	pmap_link_l2pt(l1pagetable, 0x00000000,
684 	    &kernel_pt_table[KERNEL_PT_SYS]);
685 	pmap_link_l2pt(l1pagetable, KERNEL_BASE,
686 	    &kernel_pt_table[KERNEL_PT_KERNEL]);
687 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
688 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
689 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
690 	pmap_link_l2pt(l1pagetable, PTE_BASE, &kernel_ptpt);
691 	pmap_link_l2pt(l1pagetable, VMEM_VBASE,
692 	    &kernel_pt_table[KERNEL_PT_VMEM]);
693 
694 	/* update the top of the kernel VM */
695 	pmap_curmaxkvaddr =
696 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
697 
698 #ifdef VERBOSE_INIT_ARM
699 	printf("Mapping kernel\n");
700 #endif
701 
702 	/* Now we fill in the L2 pagetable for the kernel code/data */
703 
704 	/*
705 	 * The defines are a workaround for a recent problem that occurred
706 	 * with ARM 610 processors and some ARM 710 processors
707 	 * Other ARM 710 and StrongARM processors don't have a problem.
708 	 */
709 	if (N_GETMAGIC(kernexec[0]) == ZMAGIC) {
710 #if defined(CPU_ARM6) || defined(CPU_ARM7)
711 		logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
712 		    physical_start, kernexec->a_text,
713 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
714 #else	/* CPU_ARM6 || CPU_ARM7 */
715 		logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
716 		    physical_start, kernexec->a_text,
717 		    VM_PROT_READ, PTE_CACHE);
718 #endif	/* CPU_ARM6 || CPU_ARM7 */
719 		logical += pmap_map_chunk(l1pagetable,
720 		    KERNEL_TEXT_BASE + logical, physical_start + logical,
721 		    kerneldatasize - kernexec->a_text,
722 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
723 	} else {	/* !ZMAGIC */
724 		/*
725 		 * Most likely an ELF kernel ...
726 		 * XXX no distinction yet between read only and read/write area's ...
727 		 */
728 		pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
729 		    physical_start, kerneldatasize,
730 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
731 	};
732 
733 
734 #ifdef VERBOSE_INIT_ARM
735 	printf("Constructing L2 page tables\n");
736 #endif
737 
738 	/* Map the stack pages */
739 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
740 	    IRQ_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
741 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
742 	    ABT_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
743 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
744 	    UND_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
745 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
746 	    UPAGES * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
747 
748 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
749 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
750 
751 	/* Map the page table that maps the kernel pages */
752 	pmap_map_entry(l1pagetable, kernel_ptpt.pv_va, kernel_ptpt.pv_pa,
753 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
754 
755 
756 	/* Now we fill in the L2 pagetable for the VRAM */
757 	/*
758 	 * Current architectures mean that the VRAM is always in 1 continuous
759 	 * bank.
760 	 * This means that we can just map the 2 meg that the VRAM would occupy.
761 	 * In theory we don't need a page table for VRAM, we could section map
762 	 * it but we would need the page tables if DRAM was in use.
763 	 * XXX please map two adjacent virtual areas to ONE physical area
764 	 */
765 	pmap_map_chunk(l1pagetable, VMEM_VBASE, videomemory.vidm_pbase,
766 	    videomemory.vidm_size, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
767 	pmap_map_chunk(l1pagetable, VMEM_VBASE + videomemory.vidm_size,
768 	    videomemory.vidm_pbase, videomemory.vidm_size,
769 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
770 
771 
772 	/*
773 	 * Map entries in the page table used to map PTE's
774 	 * Basically every kernel page table gets mapped here
775 	 */
776 	/* The -2 is slightly bogus, it should be -log2(sizeof(pt_entry_t)) */
777 	pmap_map_entry(l1pagetable,
778 	    PTE_BASE + (KERNEL_BASE >> (PGSHIFT-2)),
779 	    kernel_pt_table[KERNEL_PT_KERNEL].pv_pa, VM_PROT_READ|VM_PROT_WRITE,
780 	    PTE_NOCACHE);
781 	pmap_map_entry(l1pagetable,
782 	    PTE_BASE + (PTE_BASE >> (PGSHIFT-2)),
783 	    kernel_ptpt.pv_pa, VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
784 	pmap_map_entry(l1pagetable,
785 	    PTE_BASE + (VMEM_VBASE >> (PGSHIFT-2)),
786 	    kernel_pt_table[KERNEL_PT_VMEM].pv_pa, VM_PROT_READ|VM_PROT_WRITE,
787 	    PTE_NOCACHE);
788 	pmap_map_entry(l1pagetable,
789 	    PTE_BASE+ (0x00000000 >> (PGSHIFT-2)),
790 	    kernel_pt_table[KERNEL_PT_SYS].pv_pa, VM_PROT_READ|VM_PROT_WRITE,
791 	    PTE_NOCACHE);
792 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop) {
793 		pmap_map_entry(l1pagetable,
794 		    PTE_BASE + ((KERNEL_VM_BASE +
795 		    (loop * 0x00400000)) >> (PGSHIFT-2)),
796 		    kernel_pt_table[KERNEL_PT_VMDATA + loop].pv_pa,
797 		    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
798 	}
799 
800 	/* Map the vector page. */
801 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
802 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
803 
804 	/* Map the core memory needed before autoconfig */
805 	loop = 0;
806 	while (l1_sec_table[loop].size) {
807 		vm_size_t sz;
808 
809 #ifdef VERBOSE_INIT_ARM
810 		printf("%08lx -> %08lx @ %08lx\n", l1_sec_table[loop].pa,
811 			l1_sec_table[loop].pa + l1_sec_table[loop].size - 1,
812 			l1_sec_table[loop].va);
813 #endif
814 		for (sz = 0; sz < l1_sec_table[loop].size; sz += L1_S_SIZE)
815 			pmap_map_section(l1pagetable,
816 			    l1_sec_table[loop].va + sz,
817 			    l1_sec_table[loop].pa + sz,
818 			    l1_sec_table[loop].prot,
819 			    l1_sec_table[loop].cache);
820 		++loop;
821 	}
822 
823 	/*
824 	 * Now we have the real page tables in place so we can switch to them.
825 	 * Once this is done we will be running with the REAL kernel page tables.
826 	 */
827 
828 	/* Switch tables */
829 #ifdef VERBOSE_INIT_ARM
830 	printf("switching to new L1 page table\n");
831 #endif
832 
833 	setttb(kernel_l1pt.pv_pa);
834 
835 	/*
836 	 * We must now clean the cache again....
837 	 * Cleaning may be done by reading new data to displace any
838 	 * dirty data in the cache. This will have happened in setttb()
839 	 * but since we are boot strapping the addresses used for the read
840 	 * may have just been remapped and thus the cache could be out
841 	 * of sync. A re-clean after the switch will cure this.
842 	 * After booting there are no gross reloations of the kernel thus
843 	 * this problem will not occur after initarm().
844 	 */
845 	cpu_idcache_wbinv_all();
846 
847 	/* if there is support for a serial console ...we should now reattach it */
848 	/*      fcomcndetach();*/
849 
850 	/*
851 	 * Reflect videomemory relocation in the videomemory structure
852 	 * and reinit console
853 	 */
854 	if (bootconfig.vram[0].pages == 0) {
855 		videomemory.vidm_vbase   = VMEM_VBASE;
856 	} else {
857 		videomemory.vidm_vbase   = VMEM_VBASE;
858 		bootconfig.display_start = VMEM_VBASE;
859 	};
860 	vidc_base = (int *) VIDC_BASE;
861 	iomd_base =         IOMD_BASE;
862 
863 #if NVIDCVIDEO == 0
864 	physcon_display_base(VMEM_VBASE);
865 	vidcrender_reinit();
866 #endif
867 
868 #ifdef VERBOSE_INIT_ARM
869 	printf("running on the new L1 page table!\n");
870 	printf("done.\n");
871 #endif
872 
873 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
874 
875 #ifdef VERBOSE_INIT_ARM
876 	printf("\n");
877 #endif
878 
879 	/*
880 	 * Pages were allocated during the secondary bootstrap for the
881 	 * stacks for different CPU modes.
882 	 * We must now set the r13 registers in the different CPU modes to
883 	 * point to these stacks.
884 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
885 	 * of the stack memory.
886 	 */
887 #ifdef VERBOSE_INIT_ARM
888 	printf("init subsystems: stacks ");
889 	console_flush();
890 #endif
891 
892 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * NBPG);
893 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * NBPG);
894 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * NBPG);
895 #ifdef PMAP_DEBUG
896 	if (pmap_debug_level >= 0)
897 		printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
898 		    kernelstack.pv_pa);
899 #endif	/* PMAP_DEBUG */
900 
901 	/*
902 	 * Well we should set a data abort handler.
903 	 * Once things get going this will change as we will need a proper
904 	 * handler. Until then we will use a handler that just panics but
905 	 * tells us why.
906 	 * Initialisation of the vectors will just panic on a data abort.
907 	 * This just fills in a slighly better one.
908 	 */
909 #ifdef VERBOSE_INIT_ARM
910 	printf("vectors ");
911 #endif
912 	data_abort_handler_address = (u_int)data_abort_handler;
913 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
914 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
915 	console_flush();
916 
917 
918 	/* At last !
919 	 * We now have the kernel in physical memory from the bottom upwards.
920 	 * Kernel page tables are physically above this.
921 	 * The kernel is mapped to 0xf0000000
922 	 * The kernel data PTs will handle the mapping of 0xf1000000-0xf5ffffff (80 Mb)
923 	 * 2Meg of VRAM is mapped to 0xf7000000
924 	 * The page tables are mapped to 0xefc00000
925 	 * The IOMD is mapped to 0xf6000000
926 	 * The VIDC is mapped to 0xf6100000
927 	 * The IOMD/VIDC could be pushed up higher but i havent got sufficient
928 	 * documentation to do so; the addresses are not parametized yet and hard
929 	 * to read... better fix this before; its pretty unforgiving.
930 	 */
931 
932 	/* Initialise the undefined instruction handlers */
933 #ifdef VERBOSE_INIT_ARM
934 	printf("undefined ");
935 #endif
936 	undefined_init();
937 	console_flush();
938 
939 	/* Boot strap pmap telling it where the kernel page table is */
940 #ifdef VERBOSE_INIT_ARM
941 	printf("pmap ");
942 #endif
943 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, kernel_ptpt);
944 	console_flush();
945 
946 	/* Setup the IRQ system */
947 #ifdef VERBOSE_INIT_ARM
948 	printf("irq ");
949 #endif
950 	console_flush();
951 	irq_init();
952 #ifdef VERBOSE_INIT_ARM
953 	printf("done.\n\n");
954 #endif
955 
956 #if NVIDCVIDEO>0
957 	consinit();		/* necessary ? */
958 #endif
959 
960 	/* Talk to the user */
961 	printf("NetBSD/acorn32 booting ... \n");
962 
963 	/* Tell the user if his boot loader is too old */
964 	if ((bootconfig.magic < BOOTCONFIG_MAGIC) || (bootconfig.version != BOOTCONFIG_VERSION)) {
965 		printf("\nDETECTED AN OLD BOOTLOADER. PLEASE UPGRADE IT\n\n");
966 		delay(5000000);
967 	}
968 
969 	printf("Kernel loaded from file %s\n", bootconfig.kernelname);
970 	printf("Kernel arg string (@%p) %s\n", bootconfig.args, bootconfig.args);
971 	printf("\nBoot configuration structure reports the following memory\n");
972 
973 	printf(" DRAM block 0a at %08x size %08x DRAM block 0b at %08x size %08x\n\r",
974 	    bootconfig.dram[0].address,
975 	    bootconfig.dram[0].pages * bootconfig.pagesize,
976 	    bootconfig.dram[1].address,
977 	    bootconfig.dram[1].pages * bootconfig.pagesize);
978 	printf(" DRAM block 1a at %08x size %08x DRAM block 1b at %08x size %08x\n\r",
979 	    bootconfig.dram[2].address,
980 	    bootconfig.dram[2].pages * bootconfig.pagesize,
981 	    bootconfig.dram[3].address,
982 	    bootconfig.dram[3].pages * bootconfig.pagesize);
983 	printf(" VRAM block 0  at %08x size %08x\n\r",
984 	    bootconfig.vram[0].address,
985 	    bootconfig.vram[0].pages * bootconfig.pagesize);
986 
987 
988 	if (cmos_read(RTC_ADDR_REBOOTCNT) > 0)
989 		printf("Warning: REBOOTCNT = %d\n",
990 		    cmos_read(RTC_ADDR_REBOOTCNT));
991 
992 #ifdef CPU_SA110
993 	if (cputype == CPU_ID_SA110)
994 		rpc_sa110_cc_setup();
995 #endif	/* CPU_SA110 */
996 
997 #ifdef IPKDB
998 	/* Initialise ipkdb */
999 	ipkdb_init();
1000 	if (boothowto & RB_KDB)
1001 		ipkdb_connect(0);
1002 #endif	/* NIPKDB */
1003 
1004 #ifdef DDB
1005 	db_machine_init();
1006 
1007 #ifdef __ELF__
1008 	ddb_init(bootconfig.ksym_end - bootconfig.ksym_start,
1009 		(void *) bootconfig.ksym_start, (void *) bootconfig.ksym_end);
1010 #else
1011 	{
1012 		extern int end, *esym;
1013 		ddb_init(*(int *)&end, ((int *)&end) + 1, esym);
1014 	};
1015 #endif
1016 
1017 
1018 	if (boothowto & RB_KDB)
1019 		Debugger();
1020 #endif	/* DDB */
1021 
1022 	/* We return the new stack pointer address */
1023 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
1024 }
1025 
1026 
1027 static void
1028 process_kernel_args(void)
1029 {
1030 	char *args;
1031 
1032 	/* Ok now we will check the arguments for interesting parameters. */
1033 	args = bootconfig.args;
1034 	boothowto = 0;
1035 
1036 	/* Only arguments itself are passed from the new bootloader */
1037 	while (*args == ' ')
1038 		++args;
1039 
1040 	boot_args = args;
1041 	parse_mi_bootargs(boot_args);
1042 	parse_rpc_bootargs(boot_args);
1043 }
1044 
1045 
1046 void
1047 parse_rpc_bootargs(args)
1048 	char *args;
1049 {
1050 	int integer;
1051 
1052 	if (get_bootconf_option(args, "videodram", BOOTOPT_TYPE_INT, &integer)) {
1053 		videodram_size = integer;
1054 		/* Round to 4K page */
1055 		videodram_size *= 1024;
1056 		videodram_size = round_page(videodram_size);
1057 		if (videodram_size > 1024*1024)
1058 			videodram_size = 1024*1024;
1059 	};
1060 
1061 #if 0
1062 	/* XXX this I would rather have in the new bootconfig structure */
1063 	if (get_bootconf_option(args, "kinetic", BOOTOPT_TYPE_BOOLEAN, &integer)) {
1064 		bootconfig.RPC_kinetic_card_support = 1;
1065 	};
1066 #endif
1067 }
1068 
1069 
1070 #ifdef CPU_SA110
1071 
1072 /*
1073  * For optimal cache cleaning we need two 16K banks of
1074  * virtual address space that NOTHING else will access
1075  * and then we alternate the cache cleaning between the
1076  * two banks.
1077  * The cache cleaning code requires requires 2 banks aligned
1078  * on total size boundry so the banks can be alternated by
1079  * eorring the size bit (assumes the bank size is a power of 2)
1080  */
1081 extern unsigned int sa1_cache_clean_addr;
1082 extern unsigned int sa1_cache_clean_size;
1083 void
1084 rpc_sa110_cc_setup(void)
1085 {
1086 	int loop;
1087 	paddr_t kaddr;
1088 	pt_entry_t *pte;
1089 
1090 	(void) pmap_extract(pmap_kernel(), KERNEL_TEXT_BASE, &kaddr);
1091 	for (loop = 0; loop < CPU_SA110_CACHE_CLEAN_SIZE; loop += NBPG) {
1092 		pte = vtopte(sa110_cc_base + loop);
1093 		*pte = L2_S_PROTO | kaddr |
1094 		    L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
1095 	}
1096 	sa1_cache_clean_addr = sa110_cc_base;
1097 	sa1_cache_clean_size = CPU_SA110_CACHE_CLEAN_SIZE / 2;
1098 }
1099 #endif	/* CPU_SA110 */
1100 
1101 /* End of machdep.c */
1102