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