iq80310_machdep.c revision 1.1 1 1.1 matt /* $NetBSD: iq80310_machdep.c,v 1.1 2001/09/05 04:53:41 matt Exp $ */
2 1.1 matt
3 1.1 matt /*
4 1.1 matt * Copyright (c) 1997,1998 Mark Brinicombe.
5 1.1 matt * Copyright (c) 1997,1998 Causality Limited.
6 1.1 matt * All rights reserved.
7 1.1 matt *
8 1.1 matt * Redistribution and use in source and binary forms, with or without
9 1.1 matt * modification, are permitted provided that the following conditions
10 1.1 matt * are met:
11 1.1 matt * 1. Redistributions of source code must retain the above copyright
12 1.1 matt * notice, this list of conditions and the following disclaimer.
13 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer in the
15 1.1 matt * documentation and/or other materials provided with the distribution.
16 1.1 matt * 3. All advertising materials mentioning features or use of this software
17 1.1 matt * must display the following acknowledgement:
18 1.1 matt * This product includes software developed by Mark Brinicombe
19 1.1 matt * for the NetBSD Project.
20 1.1 matt * 4. The name of the company nor the name of the author may be used to
21 1.1 matt * endorse or promote products derived from this software without specific
22 1.1 matt * prior written permission.
23 1.1 matt *
24 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 1.1 matt * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 1.1 matt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 1.1 matt * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 1.1 matt * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 1.1 matt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 1.1 matt * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 matt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 matt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 matt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 matt * SUCH DAMAGE.
35 1.1 matt *
36 1.1 matt * Machine dependant functions for kernel setup for EBSA285 core architecture
37 1.1 matt * using Netwinder firmware
38 1.1 matt *
39 1.1 matt * Created : 24/11/97
40 1.1 matt */
41 1.1 matt
42 1.1 matt #include "opt_ddb.h"
43 1.1 matt #include "opt_pmap_debug.h"
44 1.1 matt
45 1.1 matt #include <sys/param.h>
46 1.1 matt #include <sys/device.h>
47 1.1 matt #include <sys/systm.h>
48 1.1 matt #include <sys/kernel.h>
49 1.1 matt #include <sys/exec.h>
50 1.1 matt #include <sys/proc.h>
51 1.1 matt #include <sys/msgbuf.h>
52 1.1 matt #include <sys/reboot.h>
53 1.1 matt #include <sys/termios.h>
54 1.1 matt
55 1.1 matt #include <dev/cons.h>
56 1.1 matt
57 1.1 matt #include <machine/db_machdep.h>
58 1.1 matt #include <ddb/db_sym.h>
59 1.1 matt #include <ddb/db_extern.h>
60 1.1 matt
61 1.1 matt #include <machine/bootconfig.h>
62 1.1 matt #include <machine/bus.h>
63 1.1 matt #include <machine/cpu.h>
64 1.1 matt #include <machine/frame.h>
65 1.1 matt #include <machine/irqhandler.h>
66 1.1 matt #include <machine/pte.h>
67 1.1 matt #include <machine/undefined.h>
68 1.1 matt
69 1.1 matt #include <machine/iq80310_boot.h>
70 1.1 matt #include <arm/xscale/i80312reg.h>
71 1.1 matt #include <arm/xscale/i80312var.h>
72 1.1 matt
73 1.1 matt #include "opt_ipkdb.h"
74 1.1 matt
75 1.1 matt #include "isa.h"
76 1.1 matt #if NISA > 0
77 1.1 matt #include <dev/isa/isareg.h>
78 1.1 matt #include <dev/isa/isavar.h>
79 1.1 matt #endif
80 1.1 matt
81 1.1 matt /*
82 1.1 matt * Address to call from cpu_reset() to reset the machine.
83 1.1 matt * This is machine architecture dependant as it varies depending
84 1.1 matt * on where the ROM appears when you turn the MMU off.
85 1.1 matt */
86 1.1 matt
87 1.1 matt u_int cpu_reset_address = I80312_ROM_BASE;
88 1.1 matt
89 1.1 matt u_int dc21285_fclk = FCLK;
90 1.1 matt
91 1.1 matt /* Define various stack sizes in pages */
92 1.1 matt #define IRQ_STACK_SIZE 1
93 1.1 matt #define ABT_STACK_SIZE 1
94 1.1 matt #ifdef IPKDB
95 1.1 matt #define UND_STACK_SIZE 2
96 1.1 matt #else
97 1.1 matt #define UND_STACK_SIZE 1
98 1.1 matt #endif
99 1.1 matt
100 1.1 matt struct nwbootinfo nwbootinfo;
101 1.1 matt BootConfig bootconfig; /* Boot config storage */
102 1.1 matt static char bootargs[MAX_BOOT_STRING + 1];
103 1.1 matt char *boot_args = NULL;
104 1.1 matt char *boot_file = NULL;
105 1.1 matt
106 1.1 matt vm_offset_t physical_start;
107 1.1 matt vm_offset_t physical_freestart;
108 1.1 matt vm_offset_t physical_freeend;
109 1.1 matt vm_offset_t physical_end;
110 1.1 matt u_int free_pages;
111 1.1 matt vm_offset_t pagetables_start;
112 1.1 matt int physmem = 0;
113 1.1 matt
114 1.1 matt /*int debug_flags;*/
115 1.1 matt #ifndef PMAP_STATIC_L1S
116 1.1 matt int max_processes = 64; /* Default number */
117 1.1 matt #endif /* !PMAP_STATIC_L1S */
118 1.1 matt
119 1.1 matt /* Physical and virtual addresses for some global pages */
120 1.1 matt pv_addr_t systempage;
121 1.1 matt pv_addr_t irqstack;
122 1.1 matt pv_addr_t undstack;
123 1.1 matt pv_addr_t abtstack;
124 1.1 matt pv_addr_t kernelstack;
125 1.1 matt
126 1.1 matt vm_offset_t msgbufphys;
127 1.1 matt
128 1.1 matt extern u_int data_abort_handler_address;
129 1.1 matt extern u_int prefetch_abort_handler_address;
130 1.1 matt extern u_int undefined_handler_address;
131 1.1 matt
132 1.1 matt #ifdef PMAP_DEBUG
133 1.1 matt extern int pmap_debug_level;
134 1.1 matt #endif
135 1.1 matt
136 1.1 matt #define KERNEL_PT_SYS 0 /* Page table for mapping proc0 zero page */
137 1.1 matt #define KERNEL_PT_KERNEL 1 /* Page table for mapping kernel */
138 1.1 matt #define KERNEL_PT_VMDATA 2 /* Page tables for mapping kernel VM */
139 1.1 matt #define KERNEL_PT_VMDATA_NUM (KERNEL_VM_SIZE >> (PDSHIFT + 2))
140 1.1 matt #define NUM_KERNEL_PTS (KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
141 1.1 matt
142 1.1 matt pt_entry_t kernel_pt_table[NUM_KERNEL_PTS];
143 1.1 matt
144 1.1 matt struct user *proc0paddr;
145 1.1 matt
146 1.1 matt /* Prototypes */
147 1.1 matt
148 1.1 matt void consinit __P((void));
149 1.1 matt
150 1.1 matt void map_section __P((vm_offset_t pt, vm_offset_t va, vm_offset_t pa,
151 1.1 matt int cacheable));
152 1.1 matt void map_pagetable __P((vm_offset_t pt, vm_offset_t va, vm_offset_t pa));
153 1.1 matt void map_entry __P((vm_offset_t pt, vm_offset_t va, vm_offset_t pa));
154 1.1 matt void map_entry_nc __P((vm_offset_t pt, vm_offset_t va, vm_offset_t pa));
155 1.1 matt void map_entry_ro __P((vm_offset_t pt, vm_offset_t va, vm_offset_t pa));
156 1.1 matt vm_size_t map_chunk __P((vm_offset_t pd, vm_offset_t pt, vm_offset_t va,
157 1.1 matt vm_offset_t pa, vm_size_t size, u_int acc,
158 1.1 matt u_int flg));
159 1.1 matt
160 1.1 matt void process_kernel_args __P((char *));
161 1.1 matt void data_abort_handler __P((trapframe_t *frame));
162 1.1 matt void prefetch_abort_handler __P((trapframe_t *frame));
163 1.1 matt void undefinedinstruction_bounce __P((trapframe_t *frame));
164 1.1 matt void zero_page_readonly __P((void));
165 1.1 matt void zero_page_readwrite __P((void));
166 1.1 matt extern void configure __P((void));
167 1.1 matt extern void db_machine_init __P((void));
168 1.1 matt extern void parse_mi_bootargs __P((char *args));
169 1.1 matt extern void dumpsys __P((void));
170 1.1 matt
171 1.1 matt /* A load of console goo. */
172 1.1 matt #include "vga.h"
173 1.1 matt #if (NVGA > 0)
174 1.1 matt #include <dev/ic/mc6845reg.h>
175 1.1 matt #include <dev/ic/pcdisplayvar.h>
176 1.1 matt #include <dev/ic/vgareg.h>
177 1.1 matt #include <dev/ic/vgavar.h>
178 1.1 matt #endif
179 1.1 matt
180 1.1 matt #include "pckbc.h"
181 1.1 matt #if (NPCKBC > 0)
182 1.1 matt #include <dev/ic/i8042reg.h>
183 1.1 matt #include <dev/ic/pckbcvar.h>
184 1.1 matt #endif
185 1.1 matt
186 1.1 matt #include "com.h"
187 1.1 matt #if (NCOM > 0)
188 1.1 matt #include <dev/ic/comreg.h>
189 1.1 matt #include <dev/ic/comvar.h>
190 1.1 matt #ifndef CONCOMADDR
191 1.1 matt #define CONCOMADDR 0
192 1.1 matt #endif
193 1.1 matt #endif
194 1.1 matt
195 1.1 matt #ifndef CONSDEVNAME
196 1.1 matt #define CONSDEVNAME "com"
197 1.1 matt #endif
198 1.1 matt
199 1.1 matt #define CONSPEED B115200
200 1.1 matt #ifndef CONSPEED
201 1.1 matt #define CONSPEED B9600 /* TTYDEF_SPEED */
202 1.1 matt #endif
203 1.1 matt #ifndef CONMODE
204 1.1 matt #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
205 1.1 matt #endif
206 1.1 matt
207 1.1 matt int comcnspeed = CONSPEED;
208 1.1 matt int comcnmode = CONMODE;
209 1.1 matt
210 1.1 matt extern struct consdev kcomcons;
211 1.1 matt static void kcomcnputc(dev_t, int);
212 1.1 matt
213 1.1 matt /*
214 1.1 matt * void cpu_reboot(int howto, char *bootstr)
215 1.1 matt *
216 1.1 matt * Reboots the system
217 1.1 matt *
218 1.1 matt * Deal with any syncing, unmounting, dumping and shutdown hooks,
219 1.1 matt * then reset the CPU.
220 1.1 matt */
221 1.1 matt
222 1.1 matt void
223 1.1 matt cpu_reboot(int howto, char *bootstr)
224 1.1 matt {
225 1.1 matt #ifdef DIAGNOSTIC
226 1.1 matt /* info */
227 1.1 matt printf("boot: howto=%08x curproc=%p\n", howto, curproc);
228 1.1 matt #endif
229 1.1 matt
230 1.1 matt /*
231 1.1 matt * If we are still cold then hit the air brakes
232 1.1 matt * and crash to earth fast
233 1.1 matt */
234 1.1 matt if (cold) {
235 1.1 matt doshutdownhooks();
236 1.1 matt printf("The operating system has halted.\n");
237 1.1 matt printf("Please press any key to reboot.\n\n");
238 1.1 matt cngetc();
239 1.1 matt printf("rebooting...\n");
240 1.1 matt cpu_reset();
241 1.1 matt /*NOTREACHED*/
242 1.1 matt }
243 1.1 matt
244 1.1 matt /* Disable console buffering */
245 1.1 matt /* cnpollc(1);*/
246 1.1 matt
247 1.1 matt /*
248 1.1 matt * If RB_NOSYNC was not specified sync the discs.
249 1.1 matt * Note: Unless cold is set to 1 here, syslogd will die during the unmount.
250 1.1 matt * It looks like syslogd is getting woken up only to find that it cannot
251 1.1 matt * page part of the binary in as the filesystem has been unmounted.
252 1.1 matt */
253 1.1 matt if (!(howto & RB_NOSYNC))
254 1.1 matt bootsync();
255 1.1 matt
256 1.1 matt /* Say NO to interrupts */
257 1.1 matt splhigh();
258 1.1 matt
259 1.1 matt /* Do a dump if requested. */
260 1.1 matt if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
261 1.1 matt dumpsys();
262 1.1 matt
263 1.1 matt /* Run any shutdown hooks */
264 1.1 matt doshutdownhooks();
265 1.1 matt
266 1.1 matt /* Make sure IRQ's are disabled */
267 1.1 matt IRQdisable;
268 1.1 matt
269 1.1 matt if (howto & RB_HALT) {
270 1.1 matt printf("The operating system has halted.\n");
271 1.1 matt printf("Please press any key to reboot.\n\n");
272 1.1 matt cngetc();
273 1.1 matt }
274 1.1 matt
275 1.1 matt printf("rebooting...\n");
276 1.1 matt cpu_reset();
277 1.1 matt /*NOTREACHED*/
278 1.1 matt }
279 1.1 matt
280 1.1 matt /*
281 1.1 matt * Mapping table for core kernel memory. This memory is mapped at init
282 1.1 matt * time with section mappings.
283 1.1 matt */
284 1.1 matt struct l1_sec_map {
285 1.1 matt vaddr_t va;
286 1.1 matt vaddr_t pa;
287 1.1 matt vsize_t size;
288 1.1 matt int flags;
289 1.1 matt } l1_sec_table[] = {
290 1.1 matt {
291 1.1 matt /* Map 1MB for CSR space */
292 1.1 matt I80312_ARMCSR_VBASE,
293 1.1 matt I80312_ARMCSR_BASE,
294 1.1 matt I80312_ARMCSR_VSIZE,
295 1.1 matt 0
296 1.1 matt }, {
297 1.1 matt /* Map 1MB for fast cache cleaning space */
298 1.1 matt I80312_CACHE_FLUSH_VBASE,
299 1.1 matt I80312_SA_CACHE_FLUSH_BASE,
300 1.1 matt I80312_CACHE_FLUSH_VSIZE,
301 1.1 matt 1
302 1.1 matt }, {
303 1.1 matt /* Map 1MB for PCI IO space */
304 1.1 matt I80312_PCI_IO_VBASE,
305 1.1 matt I80312_PCI_IO_BASE,
306 1.1 matt I80312_PCI_IO_VSIZE,
307 1.1 matt 0
308 1.1 matt }, {
309 1.1 matt /* Map 1MB for PCI IACK space */
310 1.1 matt I80312_PCI_IACK_VBASE,
311 1.1 matt I80312_PCI_IACK_SPECIAL,
312 1.1 matt I80312_PCI_IACK_VSIZE,
313 1.1 matt 0
314 1.1 matt }, {
315 1.1 matt /* Map 16MB of type 1 PCI config access */
316 1.1 matt I80312_PCI_TYPE_1_CONFIG_VBASE,
317 1.1 matt I80312_PCI_TYPE_1_CONFIG,
318 1.1 matt I80312_PCI_TYPE_1_CONFIG_VSIZE,
319 1.1 matt 0
320 1.1 matt }, {
321 1.1 matt /* Map 16MB of type 0 PCI config access */
322 1.1 matt I80312_PCI_TYPE_0_CONFIG_VBASE,
323 1.1 matt I80312_PCI_TYPE_0_CONFIG,
324 1.1 matt I80312_PCI_TYPE_0_CONFIG_VSIZE,
325 1.1 matt 0
326 1.1 matt }, {
327 1.1 matt #if NISA > 0
328 1.1 matt /* Map 1MB of 32 bit PCI address space for ISA MEM accesses via PCI */
329 1.1 matt I80312_PCI_ISA_MEM_VBASE,
330 1.1 matt I80312_PCI_MEM_BASE,
331 1.1 matt I80312_PCI_ISA_MEM_VSIZE,
332 1.1 matt 0
333 1.1 matt #endif
334 1.1 matt }, {
335 1.1 matt 0,
336 1.1 matt 0,
337 1.1 matt 0,
338 1.1 matt 0,
339 1.1 matt }
340 1.1 matt };
341 1.1 matt
342 1.1 matt /*
343 1.1 matt * u_int initarm(struct ebsaboot *bootinfo)
344 1.1 matt *
345 1.1 matt * Initial entry point on startup. This gets called before main() is
346 1.1 matt * entered.
347 1.1 matt * It should be responsible for setting up everything that must be
348 1.1 matt * in place when main is called.
349 1.1 matt * This includes
350 1.1 matt * Taking a copy of the boot configuration structure.
351 1.1 matt * Initialising the physical console so characters can be printed.
352 1.1 matt * Setting up page tables for the kernel
353 1.1 matt * Relocating the kernel to the bottom of physical memory
354 1.1 matt */
355 1.1 matt
356 1.1 matt u_int
357 1.1 matt initarm(bootinfo)
358 1.1 matt struct nwbootinfo *bootinfo;
359 1.1 matt {
360 1.1 matt int loop;
361 1.1 matt int loop1;
362 1.1 matt u_int l1pagetable;
363 1.1 matt u_int l2pagetable;
364 1.1 matt extern char page0[], page0_end[];
365 1.1 matt #if 0
366 1.1 matt extern int end[];
367 1.1 matt extern int *esym;
368 1.1 matt #endif
369 1.1 matt pv_addr_t kernel_l1pt;
370 1.1 matt pv_addr_t kernel_ptpt;
371 1.1 matt
372 1.1 matt cn_tab = &kcomcons;
373 1.1 matt /*
374 1.1 matt * Heads up ... Setup the CPU / MMU / TLB functions
375 1.1 matt */
376 1.1 matt if (set_cpufuncs())
377 1.1 matt panic("cpu not recognized!");
378 1.1 matt
379 1.1 matt /* Fake bootconfig structure for the benefit of pmap.c */
380 1.1 matt /* XXX must make the memory description h/w independant */
381 1.1 matt bootconfig.dramblocks = 1;
382 1.1 matt bootconfig.dram[0].address = 0xa0000000;
383 1.1 matt bootconfig.dram[0].pages = 0x02000000 / NBPG; /* nwbootinfo.bi_nrpages */
384 1.1 matt /* - nwbootinfo.bt_memstart) / NBPG */;
385 1.1 matt
386 1.1 matt /*
387 1.1 matt * Initialise the diagnostic serial console
388 1.1 matt * This allows a means of generating output during initarm().
389 1.1 matt * Once all the memory map changes are complete we can call consinit()
390 1.1 matt * and not have to worry about things moving.
391 1.1 matt */
392 1.1 matt
393 1.1 matt /* Talk to the user */
394 1.1 matt printf("\nNetBSD/netwinder booting ...\n");
395 1.1 matt
396 1.1 matt /*
397 1.1 matt * Ok we have the following memory map
398 1.1 matt *
399 1.1 matt * virtual address == physical address apart from the areas:
400 1.1 matt * 0x00000000 -> 0x000fffff which is mapped to
401 1.1 matt * top 1MB of physical memory
402 1.1 matt * 0x00100000 -> 0x0fffffff which is mapped to
403 1.1 matt * physical addresses 0x00100000 -> 0x0fffffff
404 1.1 matt * 0x10000000 -> 0x1fffffff which is mapped to
405 1.1 matt * physical addresses 0x00000000 -> 0x0fffffff
406 1.1 matt * 0x20000000 -> 0xefffffff which is mapped to
407 1.1 matt * physical addresses 0x20000000 -> 0xefffffff
408 1.1 matt * 0xf0000000 -> 0xf03fffff which is mapped to
409 1.1 matt * physical addresses 0xa0000000 -> 0xa03fffff
410 1.1 matt *
411 1.1 matt * This means that the kernel is mapped suitably for continuing
412 1.1 matt * execution, all I/O is mapped 1:1 virtual to physical and
413 1.1 matt * physical memory is accessible.
414 1.1 matt *
415 1.1 matt * The initarm() has the responsibility for creating the kernel
416 1.1 matt * page tables.
417 1.1 matt * It must also set up various memory pointers that are used
418 1.1 matt * by pmap etc.
419 1.1 matt */
420 1.1 matt
421 1.1 matt /*
422 1.1 matt * Examine the boot args string for options we need to know about
423 1.1 matt * now.
424 1.1 matt */
425 1.1 matt #if 0
426 1.1 matt process_kernel_args((char *)nwbootinfo.bt_args);
427 1.1 matt #endif
428 1.1 matt
429 1.1 matt printf("initarm: Configuring system ...\n");
430 1.1 matt
431 1.1 matt /*
432 1.1 matt * Set up the variables that define the availablilty of
433 1.1 matt * physical memory
434 1.1 matt */
435 1.1 matt physical_start = 0 /*nwbootinfo.bt_memstart*/;
436 1.1 matt physical_freestart = physical_start;
437 1.1 matt physical_end = /*nwbootinfo.bt_memend*/ /*nwbootinfo.bi_nrpages * NBPG */ 64*1024*1024;
438 1.1 matt physical_freeend = physical_end;
439 1.1 matt free_pages = (physical_end - physical_start) / NBPG;
440 1.1 matt
441 1.1 matt physmem = (physical_end - physical_start) / NBPG;
442 1.1 matt
443 1.1 matt /* Tell the user about the memory */
444 1.1 matt printf("physmemory: %d pages at 0x%08lx -> 0x%08lx\n", physmem,
445 1.1 matt physical_start, physical_end - 1);
446 1.1 matt
447 1.1 matt /*
448 1.1 matt * Ok the kernel occupies the bottom of physical memory.
449 1.1 matt * The first free page after the kernel can be found in
450 1.1 matt * nwbootinfo->bt_memavail
451 1.1 matt * We now need to allocate some fixed page tables to get the kernel
452 1.1 matt * going.
453 1.1 matt * We allocate one page directory and a number page tables and store
454 1.1 matt * the physical addresses in the kernel_pt_table array.
455 1.1 matt *
456 1.1 matt * Ok the next bit of physical allocation may look complex but it is
457 1.1 matt * simple really. I have done it like this so that no memory gets
458 1.1 matt * wasted during the allocation of various pages and tables that are
459 1.1 matt * all different sizes.
460 1.1 matt * The start addresses will be page aligned.
461 1.1 matt * We allocate the kernel page directory on the first free 16KB boundry
462 1.1 matt * we find.
463 1.1 matt * We allocate the kernel page tables on the first 4KB boundry we find.
464 1.1 matt * Since we allocate at least 3 L2 pagetables we know that we must
465 1.1 matt * encounter at least one 16KB aligned address.
466 1.1 matt */
467 1.1 matt
468 1.1 matt #ifdef VERBOSE_INIT_ARM
469 1.1 matt printf("Allocating page tables\n");
470 1.1 matt #endif
471 1.1 matt
472 1.1 matt #if 0
473 1.1 matt /* Update the address of the first free 16KB chunk of physical memory */
474 1.1 matt physical_freestart = ((uintptr_t) &end + PGOFSET) & ~PGOFSET;
475 1.1 matt #if 0
476 1.1 matt physical_freestart += (kernexec->a_syms + sizeof(int)
477 1.1 matt + *(u_int *)((int)end + kernexec->a_syms + sizeof(int))
478 1.1 matt + (NBPG - 1)) & ~(NBPG - 1);
479 1.1 matt #endif
480 1.1 matt #else
481 1.1 matt physical_freestart = 0x00200000; /* start at 2MB */
482 1.1 matt #endif
483 1.1 matt
484 1.1 matt free_pages -= (physical_freestart - physical_start) / NBPG;
485 1.1 matt #ifdef VERBOSE_INIT_ARM
486 1.1 matt printf("freestart = %#lx, free_pages = %d (%#x)\n",
487 1.1 matt physical_freestart, free_pages, free_pages);
488 1.1 matt #endif
489 1.1 matt
490 1.1 matt /* Define a macro to simplify memory allocation */
491 1.1 matt #define valloc_pages(var, np) \
492 1.1 matt alloc_pages((var).pv_pa, (np)); \
493 1.1 matt (var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
494 1.1 matt
495 1.1 matt #define alloc_pages(var, np) \
496 1.1 matt (var) = physical_freestart; \
497 1.1 matt physical_freestart += ((np) * NBPG); \
498 1.1 matt free_pages -= (np); \
499 1.1 matt memset((char *)(var), 0, ((np) * NBPG));
500 1.1 matt
501 1.1 matt loop1 = 0;
502 1.1 matt kernel_l1pt.pv_pa = 0;
503 1.1 matt for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
504 1.1 matt /* Are we 16KB aligned for an L1 ? */
505 1.1 matt if ((physical_freestart & (PD_SIZE - 1)) == 0
506 1.1 matt && kernel_l1pt.pv_pa == 0) {
507 1.1 matt valloc_pages(kernel_l1pt, PD_SIZE / NBPG);
508 1.1 matt } else {
509 1.1 matt alloc_pages(kernel_pt_table[loop1], PT_SIZE / NBPG);
510 1.1 matt ++loop1;
511 1.1 matt }
512 1.1 matt }
513 1.1 matt
514 1.1 matt /* This should never be able to happen but better confirm that. */
515 1.1 matt if (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (PD_SIZE-1)) != 0)
516 1.1 matt panic("initarm: Failed to align the kernel page directory\n");
517 1.1 matt
518 1.1 matt /*
519 1.1 matt * Allocate a page for the system page mapped to V0x00000000
520 1.1 matt * This page will just contain the system vectors and can be
521 1.1 matt * shared by all processes.
522 1.1 matt */
523 1.1 matt alloc_pages(systempage.pv_pa, 1);
524 1.1 matt
525 1.1 matt /* Allocate a page for the page table to map kernel page tables*/
526 1.1 matt valloc_pages(kernel_ptpt, PT_SIZE / NBPG);
527 1.1 matt
528 1.1 matt /* Allocate stacks for all modes */
529 1.1 matt valloc_pages(irqstack, IRQ_STACK_SIZE);
530 1.1 matt valloc_pages(abtstack, ABT_STACK_SIZE);
531 1.1 matt valloc_pages(undstack, UND_STACK_SIZE);
532 1.1 matt valloc_pages(kernelstack, UPAGES);
533 1.1 matt
534 1.1 matt #ifdef VERBOSE_INIT_ARM
535 1.1 matt printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa, irqstack.pv_va);
536 1.1 matt printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa, abtstack.pv_va);
537 1.1 matt printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa, undstack.pv_va);
538 1.1 matt printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa, kernelstack.pv_va);
539 1.1 matt #endif
540 1.1 matt
541 1.1 matt alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / NBPG);
542 1.1 matt
543 1.1 matt /*
544 1.1 matt * Ok we have allocated physical pages for the primary kernel
545 1.1 matt * page tables
546 1.1 matt */
547 1.1 matt
548 1.1 matt #ifdef VERBOSE_INIT_ARM
549 1.1 matt printf("Creating L1 page table at %#lx\n", kernel_l1pt.pv_pa);
550 1.1 matt #endif
551 1.1 matt
552 1.1 matt /*
553 1.1 matt * Now we start consturction of the L1 page table
554 1.1 matt * We start by mapping the L2 page tables into the L1.
555 1.1 matt * This means that we can replace L1 mappings later on if necessary
556 1.1 matt */
557 1.1 matt l1pagetable = kernel_l1pt.pv_pa;
558 1.1 matt
559 1.1 matt /* Map the L2 pages tables in the L1 page table */
560 1.1 matt map_pagetable(l1pagetable, 0x00000000,
561 1.1 matt kernel_pt_table[KERNEL_PT_SYS]);
562 1.1 matt map_pagetable(l1pagetable, KERNEL_BASE,
563 1.1 matt kernel_pt_table[KERNEL_PT_KERNEL]);
564 1.1 matt for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
565 1.1 matt map_pagetable(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
566 1.1 matt kernel_pt_table[KERNEL_PT_VMDATA + loop]);
567 1.1 matt map_pagetable(l1pagetable, PROCESS_PAGE_TBLS_BASE,
568 1.1 matt kernel_ptpt.pv_pa);
569 1.1 matt
570 1.1 matt #ifdef VERBOSE_INIT_ARM
571 1.1 matt printf("Mapping kernel\n");
572 1.1 matt #endif
573 1.1 matt
574 1.1 matt /* Now we fill in the L2 pagetable for the kernel static code/data */
575 1.1 matt l2pagetable = kernel_pt_table[KERNEL_PT_KERNEL];
576 1.1 matt
577 1.1 matt #if 0
578 1.1 matt {
579 1.1 matt u_int logical;
580 1.1 matt extern int etext, end;
581 1.1 matt size_t textsize = (uintptr_t) &etext - KERNEL_TEXT_BASE;
582 1.1 matt size_t totalsize = (uintptr_t) &end - KERNEL_TEXT_BASE;
583 1.1 matt
584 1.1 matt /* Round down text size and round up total size
585 1.1 matt */
586 1.1 matt textsize = textsize & ~PGOFSET;
587 1.1 matt totalsize = (totalsize + PGOFSET) & ~PGOFSET;
588 1.1 matt logical = map_chunk(0, l2pagetable, KERNEL_BASE,
589 1.1 matt physical_start, KERNEL_TEXT_BASE - KERNEL_BASE,
590 1.1 matt AP_KRW, PT_CACHEABLE);
591 1.1 matt logical += map_chunk(0, l2pagetable, KERNEL_BASE + logical,
592 1.1 matt physical_start + logical, textsize,
593 1.1 matt AP_KRW, PT_CACHEABLE);
594 1.1 matt logical += map_chunk(0, l2pagetable, KERNEL_BASE + logical,
595 1.1 matt physical_start + logical, totalsize - textsize,
596 1.1 matt AP_KRW, PT_CACHEABLE);
597 1.1 matt #if 0
598 1.1 matt logical += map_chunk(0, l2pagetable, KERNEL_BASE + logical,
599 1.1 matt physical_start + logical, kernexec->a_syms + sizeof(int)
600 1.1 matt + *(u_int *)((int)end + kernexec->a_syms + sizeof(int)),
601 1.1 matt AP_KRW, PT_CACHEABLE);
602 1.1 matt #endif
603 1.1 matt }
604 1.1 matt #else
605 1.1 matt map_section(l1pagetable, 0xf0000000, 0x00000000, 1);
606 1.1 matt map_section(l1pagetable, 0xf0100000, 0x00100000, 1);
607 1.1 matt #endif
608 1.1 matt #if 0
609 1.1 matt /*
610 1.1 matt * PATCH PATCH ...
611 1.1 matt *
612 1.1 matt * Fixup the first word of the kernel to be the instruction
613 1.1 matt * add pc, pc, #0x41000000
614 1.1 matt *
615 1.1 matt * This traps the case where the CPU core resets due to bus contention
616 1.1 matt * on a prototype CATS system and will reboot into the firmware.
617 1.1 matt */
618 1.1 matt *((u_int *)KERNEL_TEXT_BASE) = 0xe28ff441;
619 1.1 matt #endif
620 1.1 matt
621 1.1 matt #ifdef VERBOSE_INIT_ARM
622 1.1 matt printf("Constructing L2 page tables\n");
623 1.1 matt #endif
624 1.1 matt
625 1.1 matt /* Map the boot arguments page */
626 1.1 matt #if 0
627 1.1 matt map_entry_ro(l2pagetable, nwbootinfo.bt_vargp, nwbootinfo.bt_pargp);
628 1.1 matt #endif
629 1.1 matt
630 1.1 matt /* Map the stack pages */
631 1.1 matt map_chunk(0, l2pagetable, irqstack.pv_va, irqstack.pv_pa,
632 1.1 matt IRQ_STACK_SIZE * NBPG, AP_KRW, PT_CACHEABLE);
633 1.1 matt map_chunk(0, l2pagetable, abtstack.pv_va, abtstack.pv_pa,
634 1.1 matt ABT_STACK_SIZE * NBPG, AP_KRW, PT_CACHEABLE);
635 1.1 matt map_chunk(0, l2pagetable, undstack.pv_va, undstack.pv_pa,
636 1.1 matt UND_STACK_SIZE * NBPG, AP_KRW, PT_CACHEABLE);
637 1.1 matt map_chunk(0, l2pagetable, kernelstack.pv_va, kernelstack.pv_pa,
638 1.1 matt UPAGES * NBPG, AP_KRW, PT_CACHEABLE);
639 1.1 matt map_chunk(0, l2pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
640 1.1 matt PD_SIZE, AP_KRW, 0);
641 1.1 matt
642 1.1 matt /* Map the page table that maps the kernel pages */
643 1.1 matt map_entry_nc(l2pagetable, kernel_ptpt.pv_pa, kernel_ptpt.pv_pa);
644 1.1 matt
645 1.1 matt /*
646 1.1 matt * Map entries in the page table used to map PTE's
647 1.1 matt * Basically every kernel page table gets mapped here
648 1.1 matt */
649 1.1 matt /* The -2 is slightly bogus, it should be -log2(sizeof(pt_entry_t)) */
650 1.1 matt l2pagetable = kernel_ptpt.pv_pa;
651 1.1 matt map_entry_nc(l2pagetable, (KERNEL_BASE >> (PGSHIFT-2)),
652 1.1 matt kernel_pt_table[KERNEL_PT_KERNEL]);
653 1.1 matt map_entry_nc(l2pagetable, (PROCESS_PAGE_TBLS_BASE >> (PGSHIFT-2)),
654 1.1 matt kernel_ptpt.pv_pa);
655 1.1 matt map_entry_nc(l2pagetable, (0x00000000 >> (PGSHIFT-2)),
656 1.1 matt kernel_pt_table[KERNEL_PT_SYS]);
657 1.1 matt for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
658 1.1 matt map_entry_nc(l2pagetable, ((KERNEL_VM_BASE +
659 1.1 matt (loop * 0x00400000)) >> (PGSHIFT-2)),
660 1.1 matt kernel_pt_table[KERNEL_PT_VMDATA + loop]);
661 1.1 matt
662 1.1 matt /*
663 1.1 matt * Map the system page in the kernel page table for the bottom 1Meg
664 1.1 matt * of the virtual memory map.
665 1.1 matt */
666 1.1 matt l2pagetable = kernel_pt_table[KERNEL_PT_SYS];
667 1.1 matt map_entry(l2pagetable, 0x00000000, systempage.pv_pa);
668 1.1 matt
669 1.1 matt /* Map the core memory needed before autoconfig */
670 1.1 matt loop = 0;
671 1.1 matt while (l1_sec_table[loop].size) {
672 1.1 matt vm_size_t sz;
673 1.1 matt
674 1.1 matt #ifdef VERBOSE_INIT_ARM
675 1.1 matt printf("%08lx -> %08lx @ %08lx\n", l1_sec_table[loop].pa,
676 1.1 matt l1_sec_table[loop].pa + l1_sec_table[loop].size - 1,
677 1.1 matt l1_sec_table[loop].va);
678 1.1 matt #endif
679 1.1 matt for (sz = 0; sz < l1_sec_table[loop].size; sz += L1_SEC_SIZE)
680 1.1 matt map_section(l1pagetable, l1_sec_table[loop].va + sz,
681 1.1 matt l1_sec_table[loop].pa + sz,
682 1.1 matt l1_sec_table[loop].flags);
683 1.1 matt ++loop;
684 1.1 matt }
685 1.1 matt
686 1.1 matt /*
687 1.1 matt * Now we have the real page tables in place so we can switch to them.
688 1.1 matt * Once this is done we will be running with the REAL kernel page tables.
689 1.1 matt */
690 1.1 matt
691 1.1 matt /* Switch tables */
692 1.1 matt #ifdef VERBOSE_INIT_ARM
693 1.1 matt printf("freestart = %#lx, free_pages = %d (%#x)\n",
694 1.1 matt physical_freestart, free_pages, free_pages);
695 1.1 matt printf("switching to new L1 page table @%#lx...", kernel_l1pt.pv_pa);
696 1.1 matt #endif
697 1.1 matt
698 1.1 matt setttb(kernel_l1pt.pv_pa);
699 1.1 matt
700 1.1 matt #ifdef VERBOSE_INIT_ARM
701 1.1 matt printf("done!\n");
702 1.1 matt #endif
703 1.1 matt /*
704 1.1 matt * Ok the I80312 CSR registers have just moved.
705 1.1 matt * Detach the diagnostic serial port and reattach at the new address.
706 1.1 matt */
707 1.1 matt
708 1.1 matt /*
709 1.1 matt * XXX this should only be done in main() but it useful to
710 1.1 matt * have output earlier ...
711 1.1 matt */
712 1.1 matt consinit();
713 1.1 matt
714 1.1 matt #ifdef VERBOSE_INIT_ARM
715 1.1 matt printf("bootstrap done.\n");
716 1.1 matt #endif
717 1.1 matt
718 1.1 matt /* Right set up the vectors at the bottom of page 0 */
719 1.1 matt memcpy((char *)0x00000000, page0, page0_end - page0);
720 1.1 matt
721 1.1 matt /* We have modified a text page so sync the icache */
722 1.1 matt cpu_cache_syncI();
723 1.1 matt
724 1.1 matt /*
725 1.1 matt * Pages were allocated during the secondary bootstrap for the
726 1.1 matt * stacks for different CPU modes.
727 1.1 matt * We must now set the r13 registers in the different CPU modes to
728 1.1 matt * point to these stacks.
729 1.1 matt * Since the ARM stacks use STMFD etc. we must set r13 to the top end
730 1.1 matt * of the stack memory.
731 1.1 matt */
732 1.1 matt printf("init subsystems: stacks ");
733 1.1 matt
734 1.1 matt set_stackptr(PSR_IRQ32_MODE, irqstack.pv_va + IRQ_STACK_SIZE * NBPG);
735 1.1 matt set_stackptr(PSR_ABT32_MODE, abtstack.pv_va + ABT_STACK_SIZE * NBPG);
736 1.1 matt set_stackptr(PSR_UND32_MODE, undstack.pv_va + UND_STACK_SIZE * NBPG);
737 1.1 matt
738 1.1 matt /*
739 1.1 matt * Well we should set a data abort handler.
740 1.1 matt * Once things get going this will change as we will need a proper handler.
741 1.1 matt * Until then we will use a handler that just panics but tells us
742 1.1 matt * why.
743 1.1 matt * Initialisation of the vectors will just panic on a data abort.
744 1.1 matt * This just fills in a slighly better one.
745 1.1 matt */
746 1.1 matt printf("vectors ");
747 1.1 matt data_abort_handler_address = (u_int)data_abort_handler;
748 1.1 matt prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
749 1.1 matt undefined_handler_address = (u_int)undefinedinstruction_bounce;
750 1.1 matt
751 1.1 matt /* At last !
752 1.1 matt * We now have the kernel in physical memory from the bottom upwards.
753 1.1 matt * Kernel page tables are physically above this.
754 1.1 matt * The kernel is mapped to KERNEL_TEXT_BASE
755 1.1 matt * The kernel data PTs will handle the mapping of 0xf1000000-0xf3ffffff
756 1.1 matt * The page tables are mapped to 0xefc00000
757 1.1 matt */
758 1.1 matt
759 1.1 matt /* Initialise the undefined instruction handlers */
760 1.1 matt printf("undefined ");
761 1.1 matt undefined_init();
762 1.1 matt
763 1.1 matt /* Boot strap pmap telling it where the kernel page table is */
764 1.1 matt printf("pmap ");
765 1.1 matt pmap_bootstrap((pd_entry_t *)kernel_l1pt.pv_va, kernel_ptpt);
766 1.1 matt
767 1.1 matt /* Setup the IRQ system */
768 1.1 matt printf("irq ");
769 1.1 matt irq_init();
770 1.1 matt printf("done.\n");
771 1.1 matt
772 1.1 matt #ifdef IPKDB
773 1.1 matt /* Initialise ipkdb */
774 1.1 matt ipkdb_init();
775 1.1 matt if (boothowto & RB_KDB)
776 1.1 matt ipkdb_connect(0);
777 1.1 matt #endif
778 1.1 matt
779 1.1 matt #ifdef DDB
780 1.1 matt printf("ddb: ");
781 1.1 matt db_machine_init();
782 1.1 matt #if 0
783 1.1 matt ddb_init(end[0], end + 1, esym);
784 1.1 matt #endif
785 1.1 matt
786 1.1 matt if (boothowto & RB_KDB)
787 1.1 matt Debugger();
788 1.1 matt #endif
789 1.1 matt
790 1.1 matt /* We return the new stack pointer address */
791 1.1 matt return(kernelstack.pv_va + USPACE_SVC_STACK_TOP);
792 1.1 matt }
793 1.1 matt
794 1.1 matt void
795 1.1 matt process_kernel_args(args)
796 1.1 matt char *args;
797 1.1 matt {
798 1.1 matt
799 1.1 matt boothowto = 0;
800 1.1 matt
801 1.1 matt /* Make a local copy of the bootargs */
802 1.1 matt strncpy(bootargs, args, MAX_BOOT_STRING);
803 1.1 matt
804 1.1 matt args = bootargs;
805 1.1 matt boot_file = bootargs;
806 1.1 matt
807 1.1 matt /* Skip the kernel image filename */
808 1.1 matt while (*args != ' ' && *args != 0)
809 1.1 matt ++args;
810 1.1 matt
811 1.1 matt if (*args != 0)
812 1.1 matt *args++ = 0;
813 1.1 matt
814 1.1 matt while (*args == ' ')
815 1.1 matt ++args;
816 1.1 matt
817 1.1 matt boot_args = args;
818 1.1 matt
819 1.1 matt printf("bootfile: %s\n", boot_file);
820 1.1 matt printf("bootargs: %s\n", boot_args);
821 1.1 matt
822 1.1 matt parse_mi_bootargs(boot_args);
823 1.1 matt }
824 1.1 matt
825 1.1 matt #if 0
826 1.1 matt void
827 1.1 matt arm32_cachectl(va, len, flags)
828 1.1 matt vm_offset_t va;
829 1.1 matt int len;
830 1.1 matt int flags;
831 1.1 matt {
832 1.1 matt pt_entry_t *ptep, pte;
833 1.1 matt int loop;
834 1.1 matt vm_offset_t addr;
835 1.1 matt
836 1.1 matt /* printf("arm32_cachectl(%x,%x,%x)\n", va, len, flags);*/
837 1.1 matt
838 1.1 matt if (flags & 1) {
839 1.1 matt addr = va;
840 1.1 matt loop = len;
841 1.1 matt while (loop > 0) {
842 1.1 matt ptep = vtopte(addr & (~PGOFSET));
843 1.1 matt pte = *ptep;
844 1.1 matt
845 1.1 matt *ptep = (pte & ~(PT_C | PT_B)) | (flags & (PT_C | PT_B));
846 1.1 matt
847 1.1 matt loop -= NBPG;
848 1.1 matt addr += NBPG;
849 1.1 matt }
850 1.1 matt tlb_flush();
851 1.1 matt }
852 1.1 matt
853 1.1 matt cpu_cache_purgeD_rng(va, len);
854 1.1 matt }
855 1.1 matt #endif
856 1.1 matt
857 1.1 matt extern struct bus_space footbridge_pci_io_bs_tag;
858 1.1 matt extern struct bus_space footbridge_pci_mem_bs_tag;
859 1.1 matt void footbridge_pci_bs_tag_init __P((void));
860 1.1 matt
861 1.1 matt void
862 1.1 matt consinit(void)
863 1.1 matt {
864 1.1 matt static int consinit_called = 0;
865 1.1 matt
866 1.1 matt if (consinit_called != 0)
867 1.1 matt return;
868 1.1 matt
869 1.1 matt consinit_called = 1;
870 1.1 matt
871 1.1 matt bust = iq80310_bs_init();
872 1.1 matt #if (NCOM > 0)
873 1.1 matt if (comcnattach(&bust, CONCOMADDR, comcnspeed,
874 1.1 matt COM_FREQ, comcnmode))
875 1.1 matt panic("can't init serial console @%x", CONCOMADDR);
876 1.1 matt #else
877 1.1 matt panic("serial console @%x not configured", CONCOMADDR);
878 1.1 matt #endif
879 1.1 matt }
880 1.1 matt }
881 1.1 matt
882 1.1 matt static bus_space_handle_t kcom_base = (bus_space_handle_t) I80312_COM0_VBASE;
883 1.1 matt
884 1.1 matt u_int8_t i80312_bs_r_1(void *, bus_space_handle_t, bus_size_t);
885 1.1 matt void i80312_bs_w_1(void *, bus_space_handle_t, bus_size_t, u_int8_t);
886 1.1 matt
887 1.1 matt #define KCOM_GETBYTE(r) i80312_bs_r_1(0, kcom_base, (r))
888 1.1 matt #define KCOM_PUTBYTE(r,v) i80312_bs_w_1(0, kcom_base, (r), (v))
889 1.1 matt
890 1.1 matt static int
891 1.1 matt kcomcngetc(dev_t dev)
892 1.1 matt {
893 1.1 matt int stat, c;
894 1.1 matt
895 1.1 matt /* block until a character becomes available */
896 1.1 matt while (!ISSET(stat = KCOM_GETBYTE(com_lsr), LSR_RXRDY))
897 1.1 matt ;
898 1.1 matt
899 1.1 matt c = KCOM_GETBYTE(com_data);
900 1.1 matt stat = KCOM_GETBYTE(com_iir);
901 1.1 matt return c;
902 1.1 matt }
903 1.1 matt
904 1.1 matt /*
905 1.1 matt * Console kernel output character routine.
906 1.1 matt */
907 1.1 matt static void
908 1.1 matt kcomcnputc(dev_t dev, int c)
909 1.1 matt {
910 1.1 matt int timo;
911 1.1 matt
912 1.1 matt /* wait for any pending transmission to finish */
913 1.1 matt timo = 150000;
914 1.1 matt while (!ISSET(KCOM_GETBYTE(com_lsr), LSR_TXRDY) && --timo)
915 1.1 matt continue;
916 1.1 matt
917 1.1 matt KCOM_PUTBYTE(com_data, c);
918 1.1 matt
919 1.1 matt /* wait for this transmission to complete */
920 1.1 matt timo = 1500000;
921 1.1 matt while (!ISSET(KCOM_GETBYTE(com_lsr), LSR_TXRDY) && --timo)
922 1.1 matt continue;
923 1.1 matt }
924 1.1 matt
925 1.1 matt static void
926 1.1 matt kcomcnpollc(dev_t dev, int on)
927 1.1 matt {
928 1.1 matt }
929 1.1 matt
930 1.1 matt struct consdev kcomcons = {
931 1.1 matt NULL, NULL, kcomcngetc, kcomcnputc, kcomcnpollc, NULL,
932 1.1 matt NODEV, CN_NORMAL
933 1.1 matt };
934