xref: /netbsd-src/sys/arch/hpcarm/hpcarm/hpc_machdep.c (revision 28c37e673e4d9b6cbdc7483062b915cc61d1ccf5)
1 /*	$NetBSD: hpc_machdep.c,v 1.54 2002/08/24 02:16:34 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1994-1998 Mark Brinicombe.
5  * Copyright (c) 1994 Brini.
6  * All rights reserved.
7  *
8  * This code is derived from software written for Brini by Mark Brinicombe
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by Brini.
21  * 4. The name of the company nor the name of the author may be used to
22  *    endorse or promote products derived from this software without specific
23  *    prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * RiscBSD kernel project
38  *
39  * machdep.c
40  *
41  * Machine dependant functions for kernel setup
42  *
43  * This file needs a lot of work.
44  *
45  * Created      : 17/09/94
46  */
47 /*
48  * hpc_machdep.c
49  */
50 
51 #include "opt_ddb.h"
52 #include "opt_pmap_debug.h"
53 #include "fs_nfs.h"
54 
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/reboot.h>
59 #include <sys/proc.h>
60 #include <sys/msgbuf.h>
61 #include <sys/exec.h>
62 
63 #include <dev/cons.h>
64 
65 #ifdef DDB
66 #include <machine/db_machdep.h>
67 #include <ddb/db_sym.h>
68 #include <ddb/db_extern.h>
69 #ifndef DB_ELFSIZE
70 #error Must define DB_ELFSIZE!
71 #endif
72 #define ELFSIZE		DB_ELFSIZE
73 #include <sys/exec_elf.h>
74 #endif
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/arm32/katelib.h>
85 #include <machine/bootinfo.h>
86 #include <arm/cpuconf.h>
87 #include <arm/undefined.h>
88 #include <machine/rtc.h>
89 #include <machine/platid.h>
90 
91 #include <arm/sa11x0/sa11x0_reg.h>
92 
93 #include <dev/hpc/bicons.h>
94 
95 #include "opt_ipkdb.h"
96 
97 /* XXX for consinit related hacks */
98 #include <sys/conf.h>
99 
100 #ifdef NFS
101 #include <sys/mount.h>
102 #include <nfs/rpcv2.h>
103 #include <nfs/nfsproto.h>
104 #include <nfs/nfs.h>
105 #include <nfs/nfsmount.h>
106 #endif
107 
108 /*
109  * Address to call from cpu_reset() to reset the machine.
110  * This is machine architecture dependant as it varies depending
111  * on where the ROM appears when you turn the MMU off.
112  */
113 
114 u_int cpu_reset_address = 0;
115 
116 /* Define various stack sizes in pages */
117 #define IRQ_STACK_SIZE	1
118 #define ABT_STACK_SIZE	1
119 #ifdef IPKDB
120 #define UND_STACK_SIZE	2
121 #else
122 #define UND_STACK_SIZE	1
123 #endif
124 
125 BootConfig bootconfig;		/* Boot config storage */
126 struct bootinfo *bootinfo, bootinfo_storage;
127 static char booted_kernel_storage[80];
128 char *booted_kernel = booted_kernel_storage;
129 
130 paddr_t physical_start;
131 paddr_t physical_freestart;
132 paddr_t physical_freeend;
133 paddr_t physical_end;
134 u_int free_pages;
135 int physmem = 0;
136 
137 #ifndef PMAP_STATIC_L1S
138 int max_processes = 64;			/* Default number */
139 #endif	/* !PMAP_STATIC_L1S */
140 
141 
142 /* Physical and virtual addresses for some global pages */
143 pv_addr_t systempage;
144 pv_addr_t irqstack;
145 pv_addr_t undstack;
146 pv_addr_t abtstack;
147 pv_addr_t kernelstack;
148 
149 char *boot_args = NULL;
150 char boot_file[16];
151 
152 vaddr_t msgbufphys;
153 
154 extern u_int data_abort_handler_address;
155 extern u_int prefetch_abort_handler_address;
156 extern u_int undefined_handler_address;
157 extern int end;
158 
159 #ifdef PMAP_DEBUG
160 extern int pmap_debug_level;
161 #endif	/* PMAP_DEBUG */
162 
163 #define	KERNEL_PT_VMEM		0	/* Page table for mapping video memory */
164 #define	KERNEL_PT_SYS		1	/* Page table for mapping proc0 zero page */
165 #define	KERNEL_PT_KERNEL	2	/* Page table for mapping kernel */
166 #define	KERNEL_PT_IO		3	/* Page table for mapping IO */
167 #define	KERNEL_PT_VMDATA	4	/* Page tables for mapping kernel VM */
168 #define	KERNEL_PT_VMDATA_NUM	4	/* start with 16MB of KVM */
169 #define	NUM_KERNEL_PTS		(KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
170 
171 pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
172 
173 struct user *proc0paddr;
174 
175 #define CPU_SA110_CACHE_CLEAN_SIZE (0x4000 * 2)
176 extern unsigned int sa1_cache_clean_addr;
177 extern unsigned int sa1_cache_clean_size;
178 static vaddr_t sa1_cc_base;
179 
180 /* Non-buffered non-cachable memory needed to enter idle mode */
181 extern vaddr_t sa11x0_idle_mem;
182 
183 /* Prototypes */
184 
185 void physcon_display_base	__P((u_int addr));
186 void consinit		__P((void));
187 
188 void data_abort_handler		__P((trapframe_t *frame));
189 void prefetch_abort_handler	__P((trapframe_t *frame));
190 void undefinedinstruction_bounce	__P((trapframe_t *frame));
191 
192 u_int cpu_get_control		__P((void));
193 
194 void rpc_sa110_cc_setup(void);
195 
196 #ifdef DEBUG_BEFOREMMU
197 static void fakecninit();
198 #endif
199 
200 #ifdef BOOT_DUMP
201 void dumppages(char *, int);
202 #endif
203 
204 extern int db_trapper();
205 
206 extern void dump_spl_masks	__P((void));
207 
208 extern void dumpsys	__P((void));
209 
210 /*
211  * void cpu_reboot(int howto, char *bootstr)
212  *
213  * Reboots the system
214  *
215  * Deal with any syncing, unmounting, dumping and shutdown hooks,
216  * then reset the CPU.
217  */
218 
219 void
220 cpu_reboot(howto, bootstr)
221 	int howto;
222 	char *bootstr;
223 {
224 	/*
225 	 * If we are still cold then hit the air brakes
226 	 * and crash to earth fast
227 	 */
228 	if (cold) {
229 		doshutdownhooks();
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 the unmount.
245 	 * It looks like syslogd is getting woken up only to find that it cannot
246 	 * page part of the binary in as the filesystem has been unmounted.
247 	 */
248 	if (!(howto & RB_NOSYNC))
249 		bootsync();
250 
251 	/* Say NO to interrupts */
252 	splhigh();
253 
254 	/* Do a dump if requested. */
255 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
256 		dumpsys();
257 
258 
259 	/* Run any shutdown hooks */
260 	doshutdownhooks();
261 
262 	/* Make sure IRQ's are disabled */
263 	IRQdisable;
264 
265 	if (howto & RB_HALT) {
266 		printf("The operating system has halted.\n");
267 		printf("Please press any key to reboot.\n\n");
268 		cngetc();
269 	}
270 
271 	printf("rebooting...\n");
272 	cpu_reset();
273 	/*NOTREACHED*/
274 }
275 
276 /*
277  *
278  * Initial entry point on startup. This gets called before main() is
279  * entered.
280  * It should be responsible for setting up everything that must be
281  * in place when main is called.
282  * This includes
283  *   Taking a copy of the boot configuration structure.
284  *   Initialising the physical console so characters can be printed.
285  *   Setting up page tables for the kernel
286  */
287 
288 u_int
289 initarm(argc, argv, bi)
290 	int argc;
291 	char **argv;
292 	struct bootinfo *bi;
293 {
294 	int loop;
295 	u_int kerneldatasize, symbolsize;
296 	u_int l1pagetable;
297 	vaddr_t freemempos;
298 	pv_addr_t kernel_l1pt;
299 	pv_addr_t kernel_ptpt;
300 #ifdef DDB
301 	Elf_Shdr *sh;
302 #endif
303 
304 	/*
305 	 * Heads up ... Setup the CPU / MMU / TLB functions
306 	 */
307 	set_cpufuncs();
308 
309 #ifdef DEBUG_BEFOREMMU
310 	/*
311 	 * At this point, we cannot call real consinit().
312 	 * Just call a faked up version of consinit(), which does the thing
313 	 * with MMU disabled.
314 	 */
315 	fakecninit();
316 #endif
317 
318 	/*
319 	 * XXX for now, overwrite bootconfig to hardcoded values.
320 	 * XXX kill bootconfig and directly call uvm_physload
321 	 */
322 	bootconfig.dram[0].address = 0xc0000000;
323 	bootconfig.dram[0].pages = 8192;
324 	bootconfig.dramblocks = 1;
325 	kerneldatasize = (u_int32_t)&end - (u_int32_t)KERNEL_TEXT_BASE;
326 
327 	symbolsize = 0;
328 #ifdef DDB
329 	if (! memcmp(&end, "\177ELF", 4)) {
330 		sh = (Elf_Shdr *)((char *)&end + ((Elf_Ehdr *)&end)->e_shoff);
331 		loop = ((Elf_Ehdr *)&end)->e_shnum;
332 		for(; loop; loop--, sh++)
333 			if (sh->sh_offset > 0 &&
334 			    (sh->sh_offset + sh->sh_size) > symbolsize)
335 				symbolsize = sh->sh_offset + sh->sh_size;
336 	}
337 #endif
338 
339 	printf("kernsize=0x%x\n", kerneldatasize);
340 	kerneldatasize += symbolsize;
341 	kerneldatasize = ((kerneldatasize - 1) & ~(NBPG * 4 - 1)) + NBPG * 8;
342 
343 	/* parse kernel args */
344 	boot_file[0] = '\0';
345 	strncpy(booted_kernel_storage, *argv, sizeof(booted_kernel_storage));
346 	for(argc--, argv++; argc; argc--, argv++)
347 		switch(**argv) {
348 		case 'a':
349 			boothowto |= RB_ASKNAME;
350 			break;
351 		case 's':
352 			boothowto |= RB_SINGLE;
353 			break;
354 		case 'b':
355 			/* boot device: -b=sd0 etc. */
356 #ifdef NFS
357 			if (strcmp(*argv + 2, "nfs") == 0)
358 				mountroot = nfs_mountroot;
359 			else
360 				strncpy(boot_file, *argv + 2,
361 				    sizeof(boot_file));
362 #else /* NFS */
363 			strncpy(boot_file, *argv + 2, sizeof(boot_file));
364 #endif /* NFS */
365 			break;
366 		default:
367 			break;
368 		}
369 
370 	/* copy bootinfo into known kernel space */
371 	bootinfo_storage = *bi;
372 	bootinfo = &bootinfo_storage;
373 
374 #ifdef BOOTINFO_FB_WIDTH
375 	bootinfo->fb_line_bytes = BOOTINFO_FB_LINE_BYTES;
376 	bootinfo->fb_width = BOOTINFO_FB_WIDTH;
377 	bootinfo->fb_height = BOOTINFO_FB_HEIGHT;
378 	bootinfo->fb_type = BOOTINFO_FB_TYPE;
379 #endif
380 
381 	/*
382 	 * hpcboot has loaded me with MMU disabled.
383 	 * So create kernel page tables and enable MMU
384 	 */
385 
386 	/*
387 	 * Set up the variables that define the availablilty of physcial
388 	 * memory
389 	 */
390 	physical_start = bootconfig.dram[0].address;
391 	physical_freestart = physical_start
392 	    + (KERNEL_TEXT_BASE - KERNEL_BASE) + kerneldatasize;
393 	physical_end = bootconfig.dram[bootconfig.dramblocks - 1].address
394 	    + bootconfig.dram[bootconfig.dramblocks - 1].pages * NBPG;
395 	physical_freeend = physical_end;
396 /*	free_pages = bootconfig.drampages;*/
397 
398 	for (loop = 0; loop < bootconfig.dramblocks; ++loop)
399 		physmem += bootconfig.dram[loop].pages;
400 
401 	/* XXX handle UMA framebuffer memory */
402 
403 	/* Use the first 1MB to allocate things */
404 	freemempos = 0xc0000000;
405 	memset((void *)0xc0000000, 0, KERNEL_TEXT_BASE - 0xc0000000);
406 
407 	/*
408 	 * Right We have the bottom meg of memory mapped to 0x00000000
409 	 * so was can get at it. The kernel will ocupy the start of it.
410 	 * After the kernel/args we allocate some of the fixed page tables
411 	 * we need to get the system going.
412 	 * We allocate one page directory and 8 page tables and store the
413 	 * physical addresses in the kernel_pt_table array.
414 	 * Must remember that neither the page L1 or L2 page tables are the
415 	 * same size as a page !
416 	 *
417 	 * Ok the next bit of physical allocate may look complex but it is
418 	 * simple really. I have done it like this so that no memory gets
419 	 * wasted during the allocate of various pages and tables that are
420 	 * all different sizes.
421 	 * The start address will be page aligned.
422 	 * We allocate the kernel page directory on the first free 16KB
423 	 * boundry we find.
424 	 * We allocate the kernel page tables on the first 1KB boundry we find.
425 	 * We allocate 9 PT's. This means that in the process we
426 	 * KNOW that we will encounter at least 1 16KB boundry.
427 	 *
428 	 * Eventually if the top end of the memory gets used for process L1
429 	 * page tables the kernel L1 page table may be moved up there.
430 	 */
431 
432 #ifdef VERBOSE_INIT_ARM
433 	printf("Allocating page tables\n");
434 #endif
435 
436 	/* Define a macro to simplify memory allocation */
437 #define	valloc_pages(var, np)			\
438 	(var).pv_pa = (var).pv_va = freemempos;	\
439 	freemempos += (np) * NBPG;
440 #define	alloc_pages(var, np)			\
441 	(var) = freemempos;			\
442 	freemempos += (np) * NBPG;
443 
444 
445 	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / NBPG);
446 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
447 		alloc_pages(kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE / NBPG);
448 		kernel_pt_table[loop].pv_va = kernel_pt_table[loop].pv_pa;
449 	}
450 
451 	/*
452 	 * Allocate a page for the system page mapped to V0x00000000
453 	 * This page will just contain the system vectors and can be
454 	 * shared by all processes.
455 	 */
456 	valloc_pages(systempage, 1);
457 
458 	/* Allocate a page for the page table to map kernel page tables*/
459 	valloc_pages(kernel_ptpt, L2_TABLE_SIZE / NBPG);
460 
461 	/* Allocate stacks for all modes */
462 	valloc_pages(irqstack, IRQ_STACK_SIZE);
463 	valloc_pages(abtstack, ABT_STACK_SIZE);
464 	valloc_pages(undstack, UND_STACK_SIZE);
465 	valloc_pages(kernelstack, UPAGES);
466 
467 #ifdef VERBOSE_INIT_ARM
468 	printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa, irqstack.pv_va);
469 	printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa, abtstack.pv_va);
470 	printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa, undstack.pv_va);
471 	printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa, kernelstack.pv_va);
472 #endif
473 
474 	alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / NBPG);
475 
476 	/*
477 	 * XXX Actually, we only need virtual space and don't need
478 	 * XXX physical memory for sa110_cc_base and sa11x0_idle_mem.
479 	 */
480 	/*
481 	 * XXX totally stuffed hack to work round problems introduced
482 	 * in recent versions of the pmap code. Due to the calls used there
483 	 * we cannot allocate virtual memory during bootstrap.
484 	 */
485 	for(;;) {
486 		alloc_pages(sa1_cc_base, 1);
487 		if (! (sa1_cc_base & (CPU_SA110_CACHE_CLEAN_SIZE - 1)))
488 			break;
489 	}
490 	{
491 		vaddr_t dummy;
492 		alloc_pages(dummy, CPU_SA110_CACHE_CLEAN_SIZE / NBPG - 1);
493 	}
494 	sa1_cache_clean_addr = sa1_cc_base;
495 	sa1_cache_clean_size = CPU_SA110_CACHE_CLEAN_SIZE / 2;
496 
497 	alloc_pages(sa11x0_idle_mem, 1);
498 
499 	/*
500 	 * Ok we have allocated physical pages for the primary kernel
501 	 * page tables
502 	 */
503 
504 #ifdef VERBOSE_INIT_ARM
505 	printf("Creating L1 page table\n");
506 #endif
507 
508 	/*
509 	 * Now we start consturction of the L1 page table
510 	 * We start by mapping the L2 page tables into the L1.
511 	 * This means that we can replace L1 mappings later on if necessary
512 	 */
513 	l1pagetable = kernel_l1pt.pv_pa;
514 
515 	/* Map the L2 pages tables in the L1 page table */
516 	pmap_link_l2pt(l1pagetable, 0x00000000,
517 	    &kernel_pt_table[KERNEL_PT_SYS]);
518 	pmap_link_l2pt(l1pagetable, KERNEL_BASE,
519 	    &kernel_pt_table[KERNEL_PT_KERNEL]);
520 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
521 		pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
522 		    &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
523 	pmap_link_l2pt(l1pagetable, PTE_BASE,
524 	    &kernel_ptpt);
525 
526 	/* update the top of the kernel VM */
527 	pmap_curmaxkvaddr =
528 	    KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
529 #define SAIPIO_BASE		0xd0000000		/* XXX XXX */
530 	pmap_link_l2pt(l1pagetable, SAIPIO_BASE,
531 	    &kernel_pt_table[KERNEL_PT_IO]);
532 
533 
534 #ifdef VERBOSE_INIT_ARM
535 	printf("Mapping kernel\n");
536 #endif
537 
538 	/* Now we fill in the L2 pagetable for the kernel code/data */
539 
540 	/*
541 	 * XXX there is no ELF header to find RO region.
542 	 * XXX What should we do?
543 	 */
544 #if 0
545 	if (N_GETMAGIC(kernexec[0]) == ZMAGIC) {
546 		logical = pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
547 		    physical_start, kernexec->a_text,
548 		    VM_PROT_READ, PTE_CACHE);
549 		logical += pmap_map_chunk(l1pagetable,
550 		    KERNEL_TEXT_BASE + logical, physical_start + logical,
551 		    kerneldatasize - kernexec->a_text,
552 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
553 	} else
554 #endif
555 		pmap_map_chunk(l1pagetable, KERNEL_TEXT_BASE,
556 		    KERNEL_TEXT_BASE, kerneldatasize,
557 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
558 
559 #ifdef VERBOSE_INIT_ARM
560 	printf("Constructing L2 page tables\n");
561 #endif
562 
563 	/* Map the stack pages */
564 	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
565 	    IRQ_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
566 	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
567 	    ABT_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
568 	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
569 	    UND_STACK_SIZE * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
570 	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
571 	    UPAGES * NBPG, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
572 
573 	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
574 	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
575 
576 	/* Map the page table that maps the kernel pages */
577 	pmap_map_entry(l1pagetable, kernel_ptpt.pv_va, kernel_ptpt.pv_pa,
578 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
579 
580 	/* Map a page for entering idle mode */
581 	pmap_map_entry(l1pagetable, sa11x0_idle_mem, sa11x0_idle_mem,
582 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
583 
584 	/*
585 	 * Map entries in the page table used to map PTE's
586 	 * Basically every kernel page table gets mapped here
587 	 */
588 	/* The -2 is slightly bogus, it should be -log2(sizeof(pt_entry_t)) */
589 	pmap_map_entry(l1pagetable,
590 	    PTE_BASE + (0x00000000 >> (PGSHIFT-2)),
591 	    kernel_pt_table[KERNEL_PT_SYS].pv_pa,
592 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
593 	pmap_map_entry(l1pagetable,
594 	    PTE_BASE + (KERNEL_BASE >> (PGSHIFT-2)),
595 	    kernel_pt_table[KERNEL_PT_KERNEL].pv_pa,
596 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
597 	for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop) {
598 		pmap_map_entry(l1pagetable,
599 		    PTE_BASE + ((KERNEL_VM_BASE +
600 		    (loop * 0x00400000)) >> (PGSHIFT-2)),
601 		    kernel_pt_table[KERNEL_PT_VMDATA + loop].pv_pa,
602 		    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
603 	}
604 	pmap_map_entry(l1pagetable,
605 	    PTE_BASE + (PTE_BASE >> (PGSHIFT-2)),
606 	    kernel_ptpt.pv_pa, VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
607 	pmap_map_entry(l1pagetable,
608 	    PTE_BASE + (SAIPIO_BASE >> (PGSHIFT-2)),
609 	    kernel_pt_table[KERNEL_PT_IO].pv_pa, VM_PROT_READ|VM_PROT_WRITE,
610 	    PTE_CACHE);
611 
612 	/* Map the vector page. */
613 	pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
614 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
615 
616 	/* Map any I/O modules here, as we don't have real bus_space_map() */
617 	printf("mapping IO...");
618 	pmap_map_entry(l1pagetable, SACOM3_BASE, SACOM3_HW_BASE,
619 	    VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
620 
621 	pmap_map_chunk(l1pagetable, sa1_cache_clean_addr, 0xe0000000,
622 	    CPU_SA110_CACHE_CLEAN_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
623 	/*
624 	 * Now we have the real page tables in place so we can switch to them.
625 	 * Once this is done we will be running with the REAL kernel page
626 	 * tables.
627 	 */
628 
629 	printf("done.\n");
630 
631 	/*
632 	 * Pages were allocated during the secondary bootstrap for the
633 	 * stacks for different CPU modes.
634 	 * We must now set the r13 registers in the different CPU modes to
635 	 * point to these stacks.
636 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
637 	 * of the stack memory.
638 	 */
639 	printf("init subsystems: stacks ");
640 
641 	set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * NBPG);
642 	set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * NBPG);
643 	set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * NBPG);
644 #ifdef PMAP_DEBUG
645 	if (pmap_debug_level >= 0)
646 		printf("kstack V%08lx P%08lx\n", kernelstack.pv_va,
647 		    kernelstack.pv_pa);
648 #endif	/* PMAP_DEBUG */
649 
650 	/*
651 	 * Well we should set a data abort handler.
652 	 * Once things get going this will change as we will need a proper
653 	 * handler. Until then we will use a handler that just panics but
654 	 * tells us why.
655 	 * Initialisation of the vectors will just panic on a data abort.
656 	 * This just fills in a slighly better one.
657 	 */
658 	printf("vectors ");
659 	data_abort_handler_address = (u_int)data_abort_handler;
660 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
661 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
662 	printf("%08x %08x %08x\n", data_abort_handler_address,
663 	    prefetch_abort_handler_address, undefined_handler_address);
664 
665 	/* Initialise the undefined instruction handlers */
666 	printf("undefined ");
667 	undefined_init();
668 
669 	/* Set the page table address. */
670 	setttb(kernel_l1pt.pv_pa);
671 
672 #ifdef BOOT_DUMP
673 	dumppages((char *)0xc0000000, 16 * NBPG);
674 	dumppages((char *)0xb0100000, 64); /* XXX */
675 #endif
676 	/* Enable MMU, I-cache, D-cache, write buffer. */
677 	cpufunc_control(0x337f, 0x107d);
678 
679 	arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
680 
681 	consinit();
682 
683 #ifdef VERBOSE_INIT_ARM
684 	printf("freemempos=%08lx\n", freemempos);
685 	printf("MMU enabled. control=%08x\n", cpu_get_control());
686 #endif
687 
688 	/* Load memory into UVM. */
689 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
690 	for (loop = 0; loop < bootconfig.dramblocks; loop++) {
691 		paddr_t start = (paddr_t)bootconfig.dram[loop].address;
692 		paddr_t end = start + (bootconfig.dram[loop].pages * NBPG);
693 
694 		if (start < physical_freestart)
695 			start = physical_freestart;
696 		if (end > physical_freeend)
697 			end = physical_freeend;
698 
699 		uvm_page_physload(atop(start), atop(end),
700 		    atop(start), atop(end), VM_FREELIST_DEFAULT);
701 	}
702 
703 	/* Boot strap pmap telling it where the kernel page table is */
704 	pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, kernel_ptpt);
705 
706 
707 	if (cputype == CPU_ID_SA110)
708 		rpc_sa110_cc_setup();
709 
710 #ifdef IPKDB
711 	/* Initialise ipkdb */
712 	ipkdb_init();
713 	if (boothowto & RB_KDB)
714 		ipkdb_connect(0);
715 #endif	/* NIPKDB */
716 
717 #ifdef BOOT_DUMP
718 	dumppages((char *)kernel_l1pt.pv_va, 16);
719 	dumppages((char *)PTE_BASE, 16);
720 #endif
721 
722 #ifdef DDB
723 	{
724 		static struct undefined_handler uh;
725 
726 		uh.uh_handler = db_trapper;
727 		install_coproc_handler_static(0, &uh);
728 	}
729 	ddb_init(symbolsize, ((int *)&end), ((char *)&end) + symbolsize);
730 #endif
731 
732 	printf("kernsize=0x%x", kerneldatasize);
733 	printf(" (including 0x%x symbols)\n", symbolsize);
734 
735 #ifdef DDB
736 	if (boothowto & RB_KDB)
737 		Debugger();
738 #endif	/* DDB */
739 
740 	if (bootinfo->magic == BOOTINFO_MAGIC) {
741 		platid.dw.dw0 = bootinfo->platid_cpu;
742 		platid.dw.dw1 = bootinfo->platid_machine;
743 	}
744 
745 	/* We return the new stack pointer address */
746 	return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
747 }
748 
749 void
750 consinit(void)
751 {
752 	static int consinit_called = 0;
753 
754 	if (consinit_called != 0)
755 		return;
756 
757 	consinit_called = 1;
758 	if (bootinfo->bi_cnuse == BI_CNUSE_SERIAL)
759 		cninit();
760 	else {
761 		/*
762 		 * Nothing to do here.  Console initialization is done at
763 		 * autoconf device attach time.
764 		 */
765 	}
766 }
767 
768 #ifdef DEBUG_BEFOREMMU
769 cons_decl(sacom);
770 void
771 fakecninit()
772 {
773 	static struct consdev fakecntab = cons_init(sacom);
774 	cn_tab = &fakecntab;
775 
776 	(*cn_tab->cn_init)(0);
777 	cn_tab->cn_pri = CN_REMOTE;
778 }
779 #endif
780 
781 
782 /*
783  * For optimal cache cleaning we need two 16K banks of
784  * virtual address space that NOTHING else will access
785  * and then we alternate the cache cleaning between the
786  * two banks.
787  * The cache cleaning code requires requires 2 banks aligned
788  * on total size boundry so the banks can be alternated by
789  * eorring the size bit (assumes the bank size is a power of 2)
790  */
791 void
792 rpc_sa110_cc_setup(void)
793 {
794 	int loop;
795 	paddr_t kaddr;
796 	pt_entry_t *pte;
797 
798 	(void) pmap_extract(pmap_kernel(), KERNEL_TEXT_BASE, &kaddr);
799 	for (loop = 0; loop < CPU_SA110_CACHE_CLEAN_SIZE; loop += NBPG) {
800 		pte = vtopte(sa1_cc_base + loop);
801 		*pte = L2_S_PROTO | kaddr |
802 		    L2_S_PROT(PTE_KERNEL, VM_PROT_READ) | pte_l2_s_cache_mode;
803 		PTE_SYNC(pte);
804 	}
805 	sa1_cache_clean_addr = sa1_cc_base;
806 	sa1_cache_clean_size = CPU_SA110_CACHE_CLEAN_SIZE / 2;
807 }
808 
809 #ifdef BOOT_DUMP
810 void dumppages(char *start, int nbytes)
811 {
812 	char *p = start;
813 	char *p1;
814 	int i;
815 
816 	for(i = nbytes; i > 0; i -= 16, p += 16) {
817 		for(p1 = p + 15; p != p1; p1--) {
818 			if (*p1)
819 				break;
820 		}
821 		if (! *p1)
822 			continue;
823 		printf("%08x %02x %02x %02x %02x %02x %02x %02x %02x"
824 		    " %02x %02x %02x %02x %02x %02x %02x %02x\n",
825 		    (unsigned int)p,
826 		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
827 		    p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
828 	}
829 }
830 #endif
831 
832 /* End of machdep.c */
833