eb7500atx_machdep.c revision 1.24
1/*	$NetBSD: eb7500atx_machdep.c,v 1.24 2012/08/16 18:22:38 matt Exp $	*/
2
3/*
4 * Copyright (c) 2000-2002 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 dependent functions for kernel setup
43 *
44 * This file still needs a lot of work
45 *
46 * Created      : 17/09/94
47 * Updated for yet another new bootloader 28/12/02
48 */
49
50#include "opt_ddb.h"
51#include "opt_modular.h"
52#include "opt_pmap_debug.h"
53#include "vidcvideo.h"
54#include "pckbc.h"
55
56#include <sys/param.h>
57
58__KERNEL_RCSID(0, "$NetBSD: eb7500atx_machdep.c,v 1.24 2012/08/16 18:22:38 matt 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#include <sys/exec_aout.h>
67#include <sys/ksyms.h>
68#include <sys/bus.h>
69
70#include <dev/cons.h>
71
72#include <machine/db_machdep.h>
73#include <ddb/db_sym.h>
74#include <ddb/db_extern.h>
75
76#include <uvm/uvm.h>
77
78#include <machine/signal.h>
79#include <machine/frame.h>
80#include <machine/bootconfig.h>
81#include <machine/cpu.h>
82#include <machine/io.h>
83#include <machine/intr.h>
84#include <arm/cpuconf.h>
85#include <arm/arm32/katelib.h>
86#include <arm/arm32/machdep.h>
87#include <arm/undefined.h>
88#include <machine/rtc.h>
89
90#include <arm/iomd/vidc.h>
91#include <arm/iomd/iomdreg.h>
92#include <arm/iomd/iomdvar.h>
93
94#include <arm/iomd/vidcvideo.h>
95
96#include <sys/device.h>
97#include <dev/ic/pckbcvar.h>
98
99#include <dev/i2c/i2cvar.h>
100#include <dev/i2c/pcf8583var.h>
101#include <arm/iomd/iomdiicvar.h>
102
103/* static i2c_tag_t acorn32_i2c_tag;*/
104
105#include "ksyms.h"
106
107/* Kernel text starts at the base of the kernel address space. */
108#define	KERNEL_TEXT_BASE	(KERNEL_BASE + 0x00000000)
109#define	KERNEL_VM_BASE		(KERNEL_BASE + 0x01000000)
110
111/*
112 * The range 0xf1000000 - 0xf5ffffff is available for kernel VM space
113 * Fixed mappings exist from 0xf6000000 - 0xffffffff
114 */
115#define	KERNEL_VM_SIZE		0x05000000
116
117/*
118 * Address to call from cpu_reset() to reset the machine.
119 * This is machine architecture dependent as it varies depending
120 * on where the ROM appears when you turn the MMU off.
121 */
122
123#define VERBOSE_INIT_ARM
124
125struct bootconfig bootconfig;	/* Boot config storage */
126videomemory_t videomemory;	/* Video memory descriptor */
127
128char *boot_args = NULL;		/* holds the pre-processed boot arguments */
129extern char *booted_kernel;	/* used for ioctl to retrieve booted kernel */
130
131extern int       *vidc_base;
132extern uint32_t  iomd_base;
133extern struct bus_space iomd_bs_tag;
134
135paddr_t physical_start;
136paddr_t physical_freestart;
137paddr_t physical_freeend;
138paddr_t physical_end;
139paddr_t dma_range_begin;
140paddr_t dma_range_end;
141
142u_int free_pages;
143paddr_t memoryblock_end;
144
145#ifndef PMAP_STATIC_L1S
146int max_processes = 64;		/* Default number */
147#endif	/* !PMAP_STATIC_L1S */
148
149u_int videodram_size = 0;	/* Amount of DRAM to reserve for video */
150
151paddr_t msgbufphys;
152
153#ifdef PMAP_DEBUG
154extern int pmap_debug_level;
155#endif	/* PMAP_DEBUG */
156
157#define	KERNEL_PT_VMEM		0 /* Page table for mapping video memory */
158#define	KERNEL_PT_SYS		1 /* Page table for mapping proc0 zero page */
159#define	KERNEL_PT_KERNEL	2 /* Page table for mapping kernel */
160#define	KERNEL_PT_VMDATA	3 /* Page tables for mapping kernel VM */
161#define	KERNEL_PT_VMDATA_NUM	4 /* start with 16MB of KVM */
162#define	NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
163
164pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
165
166
167#ifdef CPU_SA110
168#define CPU_SA110_CACHE_CLEAN_SIZE (0x4000 * 2)
169static vaddr_t sa110_cc_base;
170#endif	/* CPU_SA110 */
171
172/* Prototypes */
173void physcon_display_base(u_int);
174extern void consinit(void);
175
176void data_abort_handler(trapframe_t *);
177void prefetch_abort_handler(trapframe_t *);
178void undefinedinstruction_bounce(trapframe_t *frame);
179
180static void canonicalise_bootconfig(struct bootconfig *, struct bootconfig *);
181static void process_kernel_args(void);
182
183extern void dump_spl_masks(void);
184
185void rpc_sa110_cc_setup(void);
186
187void parse_rpc_bootargs(char *args);
188
189extern void dumpsys(void);
190
191
192#	define console_flush()		/* empty */
193
194
195#define panic2(a) do {							\
196	memset((void *) (videomemory.vidm_vbase), 0x55, 50*1024);	\
197	consinit();							\
198	panic a;							\
199} while (/* CONSTCOND */ 0)
200
201/*
202 * void cpu_reboot(int howto, char *bootstr)
203 *
204 * Reboots the system
205 *
206 * Deal with any syncing, unmounting, dumping and shutdown hooks,
207 * then reset the CPU.
208 */
209
210/* NOTE: These variables will be removed, well some of them */
211
212extern u_int current_mask;
213
214void
215cpu_reboot(int howto, char *bootstr)
216{
217
218#ifdef DIAGNOSTIC
219	printf("boot: howto=%08x curlwp=%p\n", howto, curlwp);
220
221	printf("ipl_bio=%08x ipl_net=%08x ipl_tty=%08x ipl_vm=%08x\n",
222	    irqmasks[IPL_BIO], irqmasks[IPL_NET], irqmasks[IPL_TTY],
223	    irqmasks[IPL_VM]);
224	printf("ipl_audio=%08x ipl_clock=%08x ipl_none=%08x\n",
225	    irqmasks[IPL_AUDIO], irqmasks[IPL_CLOCK], irqmasks[IPL_NONE]);
226
227	/* dump_spl_masks(); */
228#endif	/* DIAGNOSTIC */
229
230	/*
231	 * If we are still cold then hit the air brakes
232	 * and crash to earth fast
233	 */
234	if (cold) {
235		doshutdownhooks();
236		pmf_system_shutdown(boothowto);
237		printf("Halted while still in the ICE age.\n");
238		printf("The operating system has halted.\n");
239		printf("Please press any key to reboot.\n\n");
240		cngetc();
241		printf("rebooting...\n");
242		cpu_reset();
243		/*NOTREACHED*/
244	}
245
246	/* Disable console buffering */
247	cnpollc(1);
248
249	/*
250	 * If RB_NOSYNC was not specified sync the discs.
251	 * Note: Unless cold is set to 1 here, syslogd will die during
252	 * the unmount.  It looks like syslogd is getting woken up
253	 * only to find that it cannot page part of the binary in as
254	 * the filesystem has been unmounted.
255	 */
256	if (!(howto & RB_NOSYNC))
257		bootsync();
258
259	/* Say NO to interrupts */
260	splhigh();
261
262	/* Do a dump if requested. */
263	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
264		dumpsys();
265
266	/*
267	 * Auto reboot overload protection
268	 *
269	 * This code stops the kernel entering an endless loop of reboot
270	 * - panic cycles. This will have the effect of stopping further
271	 * reboots after it has rebooted 8 times after panics. A clean
272	 * halt or reboot will reset the counter.
273	 */
274
275	/* Run any shutdown hooks */
276	doshutdownhooks();
277
278	pmf_system_shutdown(boothowto);
279
280	/* Make sure IRQ's are disabled */
281	IRQdisable;
282
283	if (howto & RB_HALT) {
284		printf("The operating system has halted.\n");
285		printf("Please press any key to reboot.\n\n");
286		cngetc();
287	}
288
289	printf("rebooting...\n");
290	cpu_reset();
291	/*NOTREACHED*/
292}
293
294
295/*
296 * u_int initarm(BootConfig *bootconf)
297 *
298 * Initial entry point on startup. This gets called before main() is
299 * entered.
300 * It should be responsible for setting up everything that must be
301 * in place when main is called.
302 * This includes
303 *   Taking a copy of the boot configuration structure.
304 *   Initialising the physical console so characters can be printed.
305 *   Setting up page tables for the kernel
306 *   Relocating the kernel to the bottom of physical memory
307 */
308
309/*
310 * this part is completely rewritten for the new bootloader ... It features
311 * a flat memory map with a mapping comparable to the EBSA arm32 machine
312 * to boost the portability and likeness of the code
313 */
314
315/*
316 * Mapping table for core kernel memory. This memory is mapped at init
317 * time with section mappings.
318 *
319 * XXX One big assumption in the current architecture seems that the kernel is
320 * XXX supposed to be mapped into bootconfig.dram[0].
321 */
322
323#define ONE_MB	0x100000
324
325struct l1_sec_map {
326	vaddr_t		va;
327	paddr_t		pa;
328	vsize_t		size;
329	vm_prot_t	prot;
330	int		cache;
331} l1_sec_table[] = {
332	/* Map 1Mb section for VIDC20 */
333	{ VIDC_BASE,		VIDC_HW_BASE,
334	    ONE_MB,		VM_PROT_READ|VM_PROT_WRITE,
335	    PTE_NOCACHE },
336
337	/* Map 1Mb section from IOMD */
338	{ IOMD_BASE,		IOMD_HW_BASE,
339	    ONE_MB,		VM_PROT_READ|VM_PROT_WRITE,
340	    PTE_NOCACHE },
341
342	/* Map 1Mb of COMBO (and module space) */
343	{ IO_BASE,		IO_HW_BASE,
344	    ONE_MB,		VM_PROT_READ|VM_PROT_WRITE,
345	    PTE_NOCACHE },
346	{ 0, 0, 0, 0, 0 }
347};
348
349
350static void
351canonicalise_bootconfig(struct bootconfig *bootconf, struct bootconfig *raw_bootconf)
352{
353	/* check for bootconfig v2+ structure */
354	if (raw_bootconf->magic == BOOTCONFIG_MAGIC) {
355		/* v2+ cleaned up structure found */
356		*bootconf = *raw_bootconf;
357		return;
358	} else {
359		panic2(("Internal error: no valid bootconfig block found"));
360	}
361}
362
363
364u_int
365initarm(void *cookie)
366{
367	struct bootconfig *raw_bootconf = cookie;
368	int loop;
369	int loop1;
370	u_int logical;
371	u_int kerneldatasize;
372	u_int l1pagetable;
373	struct exec *kernexec = (struct exec *)KERNEL_TEXT_BASE;
374
375	/*
376	 * Heads up ... Setup the CPU / MMU / TLB functions
377	 */
378	set_cpufuncs();
379
380	/* canonicalise the boot configuration structure to alow versioning */
381	canonicalise_bootconfig(&bootconfig, raw_bootconf);
382	booted_kernel = bootconfig.kernelname;
383
384	/* if the wscons interface is used, switch off VERBOSE booting :( */
385#if NVIDCVIDEO>0
386#	undef VERBOSE_INIT_ARM
387#	undef PMAP_DEBUG
388#endif
389
390	/*
391	 * Initialise the video memory descriptor
392	 *
393	 * Note: all references to the video memory virtual/physical address
394	 * should go via this structure.
395	 */
396
397	/* Hardwire it on the place the bootloader tells us */
398	videomemory.vidm_vbase = bootconfig.display_start;
399	videomemory.vidm_pbase = bootconfig.display_phys;
400	videomemory.vidm_size = bootconfig.display_size;
401	if (bootconfig.vram[0].pages)
402		videomemory.vidm_type = VIDEOMEM_TYPE_VRAM;
403	else
404		videomemory.vidm_type = VIDEOMEM_TYPE_DRAM;
405	vidc_base = (int *) VIDC_HW_BASE;
406	iomd_base =         IOMD_HW_BASE;
407
408	/*
409	 * Initialise the physical console
410	 * This is done in main() but for the moment we do it here so that
411	 * we can use printf in initarm() before main() has been called.
412	 * only for `vidcconsole!' ... not wscons
413	 */
414#if NVIDCVIDEO == 0
415	consinit();
416#endif
417
418	/*
419	 * Initialise the diagnostic serial console
420	 * This allows a means of generating output during initarm().
421	 * Once all the memory map changes are complete we can call consinit()
422	 * and not have to worry about things moving.
423	 */
424	/* fcomcnattach(DC21285_ARMCSR_BASE, comcnspeed, comcnmode); */
425	/* XXX snif .... i am still not able to this */
426
427	/*
428	 * We have the following memory map (derived from EBSA)
429	 *
430	 * virtual address == physical address apart from the areas:
431	 * 0x00000000 -> 0x000fffff which is mapped to
432	 * top 1MB of physical memory
433	 * 0xf0000000 -> 0xf0ffffff wich is mapped to
434	 * physical address 0x01000000 -> 0x01ffffff (DRAM0a, dram[0])
435	 *
436	 * This means that the kernel is mapped suitably for continuing
437	 * execution, all I/O is mapped 1:1 virtual to physical and
438	 * physical memory is accessible.
439	 *
440	 * The initarm() has the responsibility for creating the kernel
441	 * page tables.
442	 * It must also set up various memory pointers that are used
443	 * by pmap etc.
444	 */
445
446	/* START OF REAL NEW STUFF */
447
448	/* Check to make sure the page size is correct */
449	if (PAGE_SIZE != bootconfig.pagesize)
450		panic2(("Page size is %d bytes instead of %d !! (huh?)\n",
451			   bootconfig.pagesize, PAGE_SIZE));
452
453	/* process arguments */
454	process_kernel_args();
455
456
457	/*
458	 * Now set up the page tables for the kernel ... this part is copied
459	 * in a (modified?) way from the EBSA machine port....
460	 */
461
462#ifdef VERBOSE_INIT_ARM
463	printf("Allocating page tables\n");
464#endif
465	/*
466	 * Set up the variables that define the availablilty of physical
467	 * memory
468	 */
469	physical_start = 0xffffffff;
470	physical_end = 0;
471	for (loop = 0, physmem = 0; loop < bootconfig.dramblocks; ++loop) {
472	    	if (bootconfig.dram[loop].address < physical_start)
473			physical_start = bootconfig.dram[loop].address;
474		memoryblock_end = bootconfig.dram[loop].address +
475		    bootconfig.dram[loop].pages * PAGE_SIZE;
476		if (memoryblock_end > physical_end)
477			physical_end = memoryblock_end;
478		physmem += bootconfig.dram[loop].pages;
479	};
480	/* constants for now, but might be changed/configured */
481	dma_range_begin = (paddr_t) physical_start;
482	dma_range_end   = (paddr_t) MIN(physical_end, 512*1024*1024);
483	/* XXX HACK HACK XXX */
484	/* dma_range_end   = 0x18000000; */
485
486	if (physical_start !=  bootconfig.dram[0].address) {
487		int oldblocks = 0;
488
489		/*
490		 * must be a kinetic, as it's the only thing to shuffle memory
491		 * around
492		 */
493		/* hack hack - throw away the slow dram */
494		for (loop = 0; loop < bootconfig.dramblocks; ++loop) {
495			if (bootconfig.dram[loop].address <
496			    bootconfig.dram[0].address)	{
497				/* non kinetic ram */
498				bootconfig.dram[loop].address = 0;
499				physmem -= bootconfig.dram[loop].pages;
500				bootconfig.drampages -=
501				    bootconfig.dram[loop].pages;
502				bootconfig.dram[loop].pages = 0;
503				oldblocks++;
504			}
505		}
506		physical_start = bootconfig.dram[0].address;
507		bootconfig.dramblocks -= oldblocks;
508	}
509
510	physical_freestart = physical_start;
511	free_pages = bootconfig.drampages;
512	physical_freeend = physical_end;
513
514
515	/*
516	 * AHUM !! set this variable ... it was set up in the old 1st
517	 * stage bootloader
518	 */
519	kerneldatasize = bootconfig.kernsize + bootconfig.MDFsize;
520
521	/* Update the address of the first free page of physical memory */
522	/* XXX Assumption that the kernel and stuff is at the LOWEST physical memory address? XXX */
523	physical_freestart +=
524	    bootconfig.kernsize + bootconfig.MDFsize + bootconfig.scratchsize;
525	free_pages -= (physical_freestart - physical_start) / PAGE_SIZE;
526
527	/* Define a macro to simplify memory allocation */
528#define	valloc_pages(var, np)						\
529	alloc_pages((var).pv_pa, (np));					\
530	(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
531
532#define alloc_pages(var, np)						\
533	(var) = physical_freestart;					\
534	physical_freestart += ((np) * PAGE_SIZE);			\
535	free_pages -= (np);						\
536	memset((char *)(var), 0, ((np) * PAGE_SIZE));
537
538	loop1 = 0;
539	kernel_l1pt.pv_pa = 0;
540	for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
541		/* Are we 16KB aligned for an L1 ? */
542		if ((physical_freestart & (L1_TABLE_SIZE - 1)) == 0
543		    && kernel_l1pt.pv_pa == 0) {
544			valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
545		} else {
546			valloc_pages(kernel_pt_table[loop1],
547					L2_TABLE_SIZE / PAGE_SIZE);
548			++loop1;
549		}
550	}
551
552
553#ifdef DIAGNOSTIC
554	/* This should never be able to happen but better confirm that. */
555	if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE-1)) != 0)
556		panic2(("initarm: Failed to align the kernel page "
557		    "directory\n"));
558#endif
559
560	/*
561	 * Allocate a page for the system page mapped to V0x00000000
562	 * This page will just contain the system vectors and can be
563	 * shared by all processes.
564	 */
565	alloc_pages(systempage.pv_pa, 1);
566
567	/* Allocate stacks for all modes */
568	valloc_pages(irqstack, IRQ_STACK_SIZE);
569	valloc_pages(abtstack, ABT_STACK_SIZE);
570	valloc_pages(undstack, UND_STACK_SIZE);
571	valloc_pages(kernelstack, UPAGES);
572
573#ifdef VERBOSE_INIT_ARM
574	printf("Setting up stacks :\n");
575	printf("IRQ stack: p0x%08lx v0x%08lx\n",
576	    irqstack.pv_pa, irqstack.pv_va);
577	printf("ABT stack: p0x%08lx v0x%08lx\n",
578	    abtstack.pv_pa, abtstack.pv_va);
579	printf("UND stack: p0x%08lx v0x%08lx\n",
580	    undstack.pv_pa, undstack.pv_va);
581	printf("SVC stack: p0x%08lx v0x%08lx\n",
582	    kernelstack.pv_pa, kernelstack.pv_va);
583	printf("\n");
584#endif
585
586	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
587
588#ifdef CPU_SA110
589	/*
590	 * XXX totally stuffed hack to work round problems introduced
591	 * in recent versions of the pmap code. Due to the calls used there
592	 * we cannot allocate virtual memory during bootstrap.
593	 */
594	sa110_cc_base = (KERNEL_BASE + (physical_freestart - physical_start)
595	    + (CPU_SA110_CACHE_CLEAN_SIZE - 1))
596	    & ~(CPU_SA110_CACHE_CLEAN_SIZE - 1);
597#endif	/* CPU_SA110 */
598
599	/*
600	 * Ok we have allocated physical pages for the primary kernel
601	 * page tables
602	 */
603
604#ifdef VERBOSE_INIT_ARM
605	printf("Creating L1 page table\n");
606#endif
607
608	/*
609	 * Now we start construction of the L1 page table
610	 * We start by mapping the L2 page tables into the L1.
611	 * This means that we can replace L1 mappings later on if necessary
612	 */
613	l1pagetable = kernel_l1pt.pv_pa;
614
615	/* Map the L2 pages tables in the L1 page table */
616	pmap_link_l2pt(l1pagetable, 0x00000000,
617	    &kernel_pt_table[KERNEL_PT_SYS]);
618	pmap_link_l2pt(l1pagetable, KERNEL_BASE,
619	    &kernel_pt_table[KERNEL_PT_KERNEL]);
620	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
621		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
622		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
623	pmap_link_l2pt(l1pagetable, VMEM_VBASE,
624	    &kernel_pt_table[KERNEL_PT_VMEM]);
625
626	/* update the top of the kernel VM */
627	pmap_curmaxkvaddr =
628	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
629
630#ifdef VERBOSE_INIT_ARM
631	printf("Mapping kernel\n");
632#endif
633
634	/* Now we fill in the L2 pagetable for the kernel code/data */
635	/* XXX Kernel doesn't have to be on physical_start (!) use bootconfig XXX */
636	/*
637	 * The defines are a workaround for a recent problem that occurred
638	 * with ARM 610 processors and some ARM 710 processors
639	 * Other ARM 710 and StrongARM processors don't have a problem.
640	 */
641	if (N_GETMAGIC(kernexec[0]) == ZMAGIC) {
642#if defined(CPU_ARM6) || defined(CPU_ARM7)
643		logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
644		    physical_start, kernexec->a_text,
645		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
646#else	/* CPU_ARM6 || CPU_ARM7 */
647		logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
648		    physical_start, kernexec->a_text,
649		    VM_PROT_READ, PTE_CACHE);
650#endif	/* CPU_ARM6 || CPU_ARM7 */
651		logical += pmap_map_chunk(l1pagetable,
652		    KERNEL_TEXT_BASE + logical, physical_start + logical,
653		    kerneldatasize - kernexec->a_text,
654		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
655	} else {	/* !ZMAGIC */
656		/*
657		 * Most likely an ELF kernel ...
658		 * XXX no distinction yet between read only and
659		 * read/write area's ...
660		 */
661		pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
662		    physical_start, kerneldatasize,
663		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
664	};
665
666
667#ifdef VERBOSE_INIT_ARM
668	printf("Constructing L2 page tables\n");
669#endif
670
671	/* Map the stack pages */
672	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
673	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
674	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
675	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
676	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
677	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
678	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
679	    UPAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
680
681	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
682	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
683
684	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
685		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
686		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
687		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
688	}
689
690	/* Now we fill in the L2 pagetable for the VRAM */
691	/*
692	 * Current architectures mean that the VRAM is always in 1
693	 * continuous bank.  This means that we can just map the 2 meg
694	 * that the VRAM would occupy.  In theory we don't need a page
695	 * table for VRAM, we could section map it but we would need
696	 * the page tables if DRAM was in use.
697	 * XXX please map two adjacent virtual areas to ONE physical
698	 * area
699	 */
700	pmap_map_chunk(l1pagetable, VMEM_VBASE, videomemory.vidm_pbase,
701	    videomemory.vidm_size, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
702	pmap_map_chunk(l1pagetable, VMEM_VBASE + videomemory.vidm_size,
703	    videomemory.vidm_pbase, videomemory.vidm_size,
704	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
705
706	/* Map the vector page. */
707	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
708	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
709
710	/* Map the core memory needed before autoconfig */
711	loop = 0;
712	while (l1_sec_table[loop].size) {
713		vm_size_t sz;
714
715#ifdef VERBOSE_INIT_ARM
716		printf("%08lx -> %08lx @ %08lx\n", l1_sec_table[loop].pa,
717			l1_sec_table[loop].pa + l1_sec_table[loop].size - 1,
718			l1_sec_table[loop].va);
719#endif
720		for (sz = 0; sz < l1_sec_table[loop].size; sz += L1_S_SIZE)
721			pmap_map_section(l1pagetable,
722			    l1_sec_table[loop].va + sz,
723			    l1_sec_table[loop].pa + sz,
724			    l1_sec_table[loop].prot,
725			    l1_sec_table[loop].cache);
726		++loop;
727	}
728
729	/*
730	 * Now we have the real page tables in place so we can switch
731	 * to them.  Once this is done we will be running with the
732	 * REAL kernel page tables.
733	 */
734
735#ifdef VERBOSE_INIT_ARM
736	printf("switching domains\n");
737#endif
738	/* be a client to all domains */
739	cpu_domains(0x55555555);
740
741	/* Switch tables */
742#ifdef VERBOSE_INIT_ARM
743	printf("switching to new L1 page table\n");
744#endif
745	cpu_setttb(kernel_l1pt.pv_pa);
746
747	/*
748	 * We must now clean the cache again....
749	 * Cleaning may be done by reading new data to displace any
750	 * dirty data in the cache. This will have happened in cpu_setttb()
751	 * but since we are boot strapping the addresses used for the read
752	 * may have just been remapped and thus the cache could be out
753	 * of sync. A re-clean after the switch will cure this.
754	 * After booting there are no gross reloations of the kernel thus
755	 * this problem will not occur after initarm().
756	 */
757	cpu_idcache_wbinv_all();
758	cpu_tlb_flushID();
759	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
760
761	/*
762	 * Moved from cpu_startup() as data_abort_handler() references
763	 * this during uvm init
764	 */
765	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
766
767	/*
768	 * if there is support for a serial console ...we should now
769	 * reattach it
770	 */
771	/*      fcomcndetach();*/
772
773	/*
774	 * Reflect videomemory relocation in the videomemory structure
775	 * and reinit console
776	 */
777	if (bootconfig.vram[0].pages == 0) {
778		videomemory.vidm_vbase   = VMEM_VBASE;
779	} else {
780		videomemory.vidm_vbase   = VMEM_VBASE;
781		bootconfig.display_start = VMEM_VBASE;
782	};
783	vidc_base = (int *) VIDC_BASE;
784	iomd_base =         IOMD_BASE;
785
786#ifdef VERBOSE_INIT_ARM
787	printf("running on the new L1 page table!\n");
788	printf("done.\n");
789#endif
790
791	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
792
793#ifdef VERBOSE_INIT_ARM
794	printf("\n");
795#endif
796
797	/*
798	 * Pages were allocated during the secondary bootstrap for the
799	 * stacks for different CPU modes.
800	 * We must now set the r13 registers in the different CPU modes to
801	 * point to these stacks.
802	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
803	 * of the stack memory.
804	 */
805#ifdef VERBOSE_INIT_ARM
806	printf("init subsystems: stacks ");
807	console_flush();
808#endif
809
810	set_stackptr(PSR_IRQ32_MODE,
811	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
812	set_stackptr(PSR_ABT32_MODE,
813	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
814	set_stackptr(PSR_UND32_MODE,
815	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
816#ifdef PMAP_DEBUG
817	if (pmap_debug_level >= 0)
818		printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
819		    kernelstack.pv_pa);
820#endif	/* PMAP_DEBUG */
821
822	/*
823	 * Well we should set a data abort handler.
824	 * Once things get going this will change as we will need a proper
825	 * handler. Until then we will use a handler that just panics but
826	 * tells us why.
827	 * Initialisation of the vectors will just panic on a data abort.
828	 * This just fills in a slightly better one.
829	 */
830#ifdef VERBOSE_INIT_ARM
831	printf("vectors ");
832#endif
833	data_abort_handler_address = (u_int)data_abort_handler;
834	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
835	undefined_handler_address = (u_int)undefinedinstruction_bounce;
836	console_flush();
837
838
839	/*
840	 * At last !
841	 * We now have the kernel in physical memory from the bottom upwards.
842	 * Kernel page tables are physically above this.
843	 * The kernel is mapped to 0xf0000000
844	 * The kernel data PTs will handle the mapping of
845	 *   0xf1000000-0xf5ffffff (80 Mb)
846	 * 2Meg of VRAM is mapped to 0xf7000000
847	 * The page tables are mapped to 0xefc00000
848	 * The IOMD is mapped to 0xf6000000
849	 * The VIDC is mapped to 0xf6100000
850	 * The IOMD/VIDC could be pushed up higher but i havent got
851	 * sufficient documentation to do so; the addresses are not
852	 * parametized yet and hard to read... better fix this before;
853	 * its pretty unforgiving.
854	 */
855
856	/* Initialise the undefined instruction handlers */
857#ifdef VERBOSE_INIT_ARM
858	printf("undefined ");
859#endif
860	undefined_init();
861	console_flush();
862
863	/* Load memory into UVM. */
864#ifdef VERBOSE_INIT_ARM
865	printf("page ");
866#endif
867	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
868	for (loop = 0; loop < bootconfig.dramblocks; loop++) {
869		paddr_t start = (paddr_t)bootconfig.dram[loop].address;
870		paddr_t end = start + (bootconfig.dram[loop].pages * PAGE_SIZE);
871
872		if (start < physical_freestart)
873			start = physical_freestart;
874		if (end > physical_freeend)
875			end = physical_freeend;
876
877		/* XXX Consider DMA range intersection checking. */
878
879		uvm_page_physload(atop(start), atop(end),
880		    atop(start), atop(end), VM_FREELIST_DEFAULT);
881	}
882
883	/* Boot strap pmap telling it where the kernel page table is */
884#ifdef VERBOSE_INIT_ARM
885	printf("pmap ");
886#endif
887	pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
888	console_flush();
889
890	/* Setup the IRQ system */
891#ifdef VERBOSE_INIT_ARM
892	printf("irq ");
893#endif
894	console_flush();
895	irq_init();
896#ifdef VERBOSE_INIT_ARM
897	printf("done.\n\n");
898#endif
899
900#if NVIDCVIDEO>0
901	consinit();		/* necessary ? */
902#endif
903
904	/* Talk to the user */
905	printf("NetBSD/evbarm booting ... \n");
906
907	/* Tell the user if his boot loader is too old */
908	if ((bootconfig.magic < BOOTCONFIG_MAGIC) ||
909	    (bootconfig.version != BOOTCONFIG_VERSION)) {
910		printf("\nDETECTED AN OLD BOOTLOADER. PLEASE UPGRADE IT\n\n");
911		delay(5000000);
912	}
913
914	printf("Kernel loaded from file %s\n", bootconfig.kernelname);
915	printf("Kernel arg string (@%p) %s\n",
916	    bootconfig.args, bootconfig.args);
917	printf("\nBoot configuration structure reports the following "
918	    "memory\n");
919
920	printf(" DRAM block 0a at %08x size %08x "
921	    "DRAM block 0b at %08x size %08x\n\r",
922	    bootconfig.dram[0].address,
923	    bootconfig.dram[0].pages * bootconfig.pagesize,
924	    bootconfig.dram[1].address,
925	    bootconfig.dram[1].pages * bootconfig.pagesize);
926	printf(" DRAM block 1a at %08x size %08x "
927	    "DRAM block 1b at %08x size %08x\n\r",
928	    bootconfig.dram[2].address,
929	    bootconfig.dram[2].pages * bootconfig.pagesize,
930	    bootconfig.dram[3].address,
931	    bootconfig.dram[3].pages * bootconfig.pagesize);
932	printf(" VRAM block 0  at %08x size %08x\n\r",
933	    bootconfig.vram[0].address,
934	    bootconfig.vram[0].pages * bootconfig.pagesize);
935
936#if NKSYMS || defined(DDB) || defined(MODULAR)
937	ksyms_addsyms_elf(bootconfig.ksym_end - bootconfig.ksym_start,
938		(void *) bootconfig.ksym_start, (void *) bootconfig.ksym_end);
939#endif
940
941
942#ifdef DDB
943	db_machine_init();
944	if (boothowto & RB_KDB)
945		Debugger();
946#endif	/* DDB */
947
948	/* We return the new stack pointer address */
949	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
950}
951
952
953static void
954process_kernel_args(void)
955{
956	char *args;
957
958	/* Ok now we will check the arguments for interesting parameters. */
959	args = bootconfig.args;
960	boothowto = 0;
961
962	/* Only arguments itself are passed from the new bootloader */
963	while (*args == ' ')
964		++args;
965
966	boot_args = args;
967	parse_mi_bootargs(boot_args);
968	parse_rpc_bootargs(boot_args);
969}
970
971
972void
973parse_rpc_bootargs(char *args)
974{
975	int integer;
976
977	if (get_bootconf_option(args, "videodram", BOOTOPT_TYPE_INT,
978	    &integer)) {
979		videodram_size = integer;
980		/* Round to 4K page */
981		videodram_size *= 1024;
982		videodram_size = round_page(videodram_size);
983		if (videodram_size > 1024*1024)
984			videodram_size = 1024*1024;
985	}
986}
987/* End of machdep.c */
988