1 /* $NetBSD: hpc_machdep.c,v 1.70 2003/09/16 08:18:22 agc 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 #include "opt_msgbuf.h"
49 #include "opt_ddb.h"
50
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
53
54 #define _ARM32_BUS_DMA_PRIVATE
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/sysproto.h>
58 #include <sys/signalvar.h>
59 #include <sys/imgact.h>
60 #include <sys/kernel.h>
61 #include <sys/ktr.h>
62 #include <sys/linker.h>
63 #include <sys/lock.h>
64 #include <sys/malloc.h>
65 #include <sys/mutex.h>
66 #include <sys/pcpu.h>
67 #include <sys/proc.h>
68 #include <sys/ptrace.h>
69 #include <sys/cons.h>
70 #include <sys/bio.h>
71 #include <sys/bus.h>
72 #include <sys/buf.h>
73 #include <sys/exec.h>
74 #include <sys/kdb.h>
75 #include <sys/msgbuf.h>
76 #include <machine/reg.h>
77 #include <machine/cpu.h>
78
79 #include <vm/vm.h>
80 #include <vm/pmap.h>
81 #include <vm/vm.h>
82 #include <vm/vm_object.h>
83 #include <vm/vm_page.h>
84 #include <vm/vm_pager.h>
85 #include <vm/vm_map.h>
86 #include <vm/vnode_pager.h>
87 #include <machine/pmap.h>
88 #include <machine/vmparam.h>
89 #include <machine/pcb.h>
90 #include <machine/undefined.h>
91 #include <machine/machdep.h>
92 #include <machine/metadata.h>
93 #include <machine/armreg.h>
94 #include <machine/bus.h>
95 #include <sys/reboot.h>
96
97
98 #include <arm/xscale/i80321/i80321var.h> /* For i80321_calibrate_delay() */
99
100 #include <arm/xscale/i8134x/i81342reg.h>
101 #include <arm/xscale/i8134x/i81342var.h>
102 #include <arm/xscale/i8134x/obiovar.h>
103
104
105 #define KERNEL_PT_SYS 0 /* Page table for mapping proc0 zero page */
106 #define KERNEL_PT_IOPXS 1
107 #define KERNEL_PT_BEFOREKERN 2
108 #define KERNEL_PT_AFKERNEL 3 /* L2 table for mapping after kernel */
109 #define KERNEL_PT_AFKERNEL_NUM 9
110
111 /* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
112 #define NUM_KERNEL_PTS (KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
113
114 /* Define various stack sizes in pages */
115 #define IRQ_STACK_SIZE 1
116 #define ABT_STACK_SIZE 1
117 #ifdef IPKDB
118 #define UND_STACK_SIZE 2
119 #else
120 #define UND_STACK_SIZE 1
121 #endif
122
123 extern u_int data_abort_handler_address;
124 extern u_int prefetch_abort_handler_address;
125 extern u_int undefined_handler_address;
126
127 struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
128
129 extern void *_end;
130
131 extern vm_offset_t sa1_cache_clean_addr;
132
133 extern int *end;
134
135 struct pcpu __pcpu;
136 struct pcpu *pcpup = &__pcpu;
137
138 /* Physical and virtual addresses for some global pages */
139
140 vm_paddr_t phys_avail[10];
141 vm_paddr_t dump_avail[4];
142 vm_offset_t physical_pages;
143 vm_offset_t clean_sva, clean_eva;
144
145 struct pv_addr systempage;
146 struct pv_addr msgbufpv;
147 struct pv_addr irqstack;
148 struct pv_addr undstack;
149 struct pv_addr abtstack;
150 struct pv_addr kernelstack;
151
152 static struct trapframe proc0_tf;
153
154 /* Static device mappings. */
155 static const struct pmap_devmap iq81342_devmap[] = {
156 {
157 IOP34X_VADDR,
158 IOP34X_HWADDR,
159 IOP34X_SIZE,
160 VM_PROT_READ|VM_PROT_WRITE,
161 PTE_NOCACHE,
162 },
163 {
164 /*
165 * Cheat and map a whole section, this will bring
166 * both PCI-X and PCI-E outbound I/O
167 */
168 IOP34X_PCIX_OIOBAR_VADDR &~ (0x100000 - 1),
169 IOP34X_PCIX_OIOBAR &~ (0x100000 - 1),
170 0x100000,
171 VM_PROT_READ|VM_PROT_WRITE,
172 PTE_NOCACHE,
173 },
174 {
175 IOP34X_PCE1_VADDR,
176 IOP34X_PCE1,
177 IOP34X_PCE1_SIZE,
178 VM_PROT_READ|VM_PROT_WRITE,
179 PTE_NOCACHE,
180 },
181 {
182 0,
183 0,
184 0,
185 0,
186 0,
187 }
188 };
189
190 #define SDRAM_START 0x00000000
191
192 #ifdef DDB
193 extern vm_offset_t ksym_start, ksym_end;
194 #endif
195
196 extern vm_offset_t xscale_cache_clean_addr;
197
198 void *
199 initarm(void *arg, void *arg2)
200 {
201 struct pv_addr kernel_l1pt;
202 int loop;
203 u_int l1pagetable;
204 vm_offset_t freemempos;
205 vm_offset_t freemem_pt;
206 vm_offset_t afterkern;
207 vm_offset_t freemem_after;
208 vm_offset_t lastaddr;
209 #ifdef DDB
210 vm_offset_t zstart = 0, zend = 0;
211 #endif
212 int i;
213 uint32_t fake_preload[35];
214 uint32_t memsize, memstart;
215
216 i = 0;
217
218 set_cpufuncs();
219 fake_preload[i++] = MODINFO_NAME;
220 fake_preload[i++] = strlen("elf kernel") + 1;
221 strcpy((char*)&fake_preload[i++], "elf kernel");
222 i += 2;
223 fake_preload[i++] = MODINFO_TYPE;
224 fake_preload[i++] = strlen("elf kernel") + 1;
225 strcpy((char*)&fake_preload[i++], "elf kernel");
226 i += 2;
227 fake_preload[i++] = MODINFO_ADDR;
228 fake_preload[i++] = sizeof(vm_offset_t);
229 fake_preload[i++] = KERNBASE + 0x00200000;
230 fake_preload[i++] = MODINFO_SIZE;
231 fake_preload[i++] = sizeof(uint32_t);
232 fake_preload[i++] = (uint32_t)&end - KERNBASE - 0x00200000;
233 #ifdef DDB
234 if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
235 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
236 fake_preload[i++] = sizeof(vm_offset_t);
237 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
238 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
239 fake_preload[i++] = sizeof(vm_offset_t);
240 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
241 lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
242 zend = lastaddr;
243 zstart = *(uint32_t *)(KERNVIRTADDR + 4);
244 ksym_start = zstart;
245 ksym_end = zend;
246 } else
247 #endif
248 lastaddr = (vm_offset_t)&end;
249
250 fake_preload[i++] = 0;
251 fake_preload[i] = 0;
252 preload_metadata = (void *)fake_preload;
253
254
255 pcpu_init(pcpup, 0, sizeof(struct pcpu));
256 PCPU_SET(curthread, &thread0);
257
258 #define KERNEL_TEXT_BASE (KERNBASE + 0x00200000)
259 freemempos = 0x00200000;
260 /* Define a macro to simplify memory allocation */
261 #define valloc_pages(var, np) \
262 alloc_pages((var).pv_pa, (np)); \
263 (var).pv_va = (var).pv_pa + 0xc0000000;
264
265 #define alloc_pages(var, np) \
266 freemempos -= (np * PAGE_SIZE); \
267 (var) = freemempos; \
268 memset((char *)(var), 0, ((np) * PAGE_SIZE));
269
270 while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
271 freemempos -= PAGE_SIZE;
272 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
273 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
274 if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
275 valloc_pages(kernel_pt_table[loop],
276 L2_TABLE_SIZE / PAGE_SIZE);
277 } else {
278 kernel_pt_table[loop].pv_pa = freemempos +
279 (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
280 L2_TABLE_SIZE_REAL;
281 kernel_pt_table[loop].pv_va =
282 kernel_pt_table[loop].pv_pa + 0xc0000000;
283 }
284 }
285 freemem_pt = freemempos;
286 freemempos = 0x00100000;
287 /*
288 * Allocate a page for the system page mapped to V0x00000000
289 * This page will just contain the system vectors and can be
290 * shared by all processes.
291 */
292 valloc_pages(systempage, 1);
293
294 /* Allocate stacks for all modes */
295 valloc_pages(irqstack, IRQ_STACK_SIZE);
296 valloc_pages(abtstack, ABT_STACK_SIZE);
297 valloc_pages(undstack, UND_STACK_SIZE);
298 valloc_pages(kernelstack, KSTACK_PAGES);
299 valloc_pages(msgbufpv, round_page(MSGBUF_SIZE) / PAGE_SIZE);
300 #ifdef ARM_USE_SMALL_ALLOC
301 freemempos -= PAGE_SIZE;
302 freemem_pt = trunc_page(freemem_pt);
303 freemem_after = freemempos - ((freemem_pt - 0x00100000) /
304 PAGE_SIZE) * sizeof(struct arm_small_page);
305 arm_add_smallalloc_pages((void *)(freemem_after + 0xc0000000)
306 , (void *)0xc0100000, freemem_pt - 0x00100000, 1);
307 freemem_after -= ((freemem_after - 0x00001000) / PAGE_SIZE) *
308 sizeof(struct arm_small_page);
309 #if 0
310 arm_add_smallalloc_pages((void *)(freemem_after + 0xc0000000)
311 , (void *)0xc0001000, trunc_page(freemem_after) - 0x00001000, 0);
312 #endif
313 freemempos = trunc_page(freemem_after);
314 freemempos -= PAGE_SIZE;
315 #endif
316 /*
317 * Now we start construction of the L1 page table
318 * We start by mapping the L2 page tables into the L1.
319 * This means that we can replace L1 mappings later on if necessary
320 */
321 l1pagetable = kernel_l1pt.pv_va;
322
323 /* Map the L2 pages tables in the L1 page table */
324 pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00100000 - 1),
325 &kernel_pt_table[KERNEL_PT_SYS]);
326 pmap_map_chunk(l1pagetable, KERNBASE, SDRAM_START, 0x100000,
327 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
328
329 pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, SDRAM_START + 0x100000,
330 0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
331
332 pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, SDRAM_START + 0x200000,
333 (((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE) & ~(L1_S_SIZE - 1),
334 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
335 freemem_after = ((int)lastaddr + PAGE_SIZE) & ~(PAGE_SIZE - 1);
336 afterkern = round_page(((vm_offset_t)lastaddr + L1_S_SIZE) & ~(L1_S_SIZE
337 - 1));
338 for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
339 pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
340 &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
341 }
342
343
344 #ifdef ARM_USE_SMALL_ALLOC
345 if ((freemem_after + 2 * PAGE_SIZE) <= afterkern) {
346 arm_add_smallalloc_pages((void *)(freemem_after),
347 (void*)(freemem_after + PAGE_SIZE),
348 afterkern - (freemem_after + PAGE_SIZE), 0);
349
350 }
351 #endif
352
353 /* Map the vector page. */
354 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
355 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
356 pmap_devmap_bootstrap(l1pagetable, iq81342_devmap);
357 /*
358 * Give the XScale global cache clean code an appropriately
359 * sized chunk of unmapped VA space starting at 0xff000000
360 * (our device mappings end before this address).
361 */
362 xscale_cache_clean_addr = 0xff000000U;
363
364 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
365 setttb(kernel_l1pt.pv_pa);
366 cpu_tlb_flushID();
367 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
368 /*
369 * Pages were allocated during the secondary bootstrap for the
370 * stacks for different CPU modes.
371 * We must now set the r13 registers in the different CPU modes to
372 * point to these stacks.
373 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
374 * of the stack memory.
375 */
376
377
378 set_stackptr(PSR_IRQ32_MODE,
379 irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
380 set_stackptr(PSR_ABT32_MODE,
381 abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
382 set_stackptr(PSR_UND32_MODE,
383 undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
384
385
386
387 /*
388 * We must now clean the cache again....
389 * Cleaning may be done by reading new data to displace any
390 * dirty data in the cache. This will have happened in setttb()
391 * but since we are boot strapping the addresses used for the read
392 * may have just been remapped and thus the cache could be out
393 * of sync. A re-clean after the switch will cure this.
394 * After booting there are no gross reloations of the kernel thus
395 * this problem will not occur after initarm().
396 */
397 cpu_idcache_wbinv_all();
398 i80321_calibrate_delay();
399 i81342_sdram_bounds(&obio_bs_tag, IOP34X_VADDR, &memstart, &memsize);
400 physmem = memsize / PAGE_SIZE;
401 cninit();
402 /* Set stack for exception handlers */
403
404 data_abort_handler_address = (u_int)data_abort_handler;
405 prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
406 undefined_handler_address = (u_int)undefinedinstruction_bounce;
407 undefined_init();
408
409 #ifdef KSE
410 proc_linkup(&proc0, &ksegrp0, &thread0);
411 #else
412 proc_linkup0(&proc0, &thread0);
413 #endif
414 thread0.td_kstack = kernelstack.pv_va;
415 thread0.td_pcb = (struct pcb *)
416 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
417 thread0.td_pcb->pcb_flags = 0;
418 thread0.td_frame = &proc0_tf;
419 pcpup->pc_curpcb = thread0.td_pcb;
420
421 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
422
423 pmap_curmaxkvaddr = afterkern + PAGE_SIZE;
424 /*
425 * ARM_USE_SMALL_ALLOC uses dump_avail, so it must be filled before
426 * calling pmap_bootstrap.
427 */
428 dump_avail[0] = 0x00000000;
429 dump_avail[1] = 0x00000000 + memsize;
430 dump_avail[2] = 0;
431 dump_avail[3] = 0;
432
433 pmap_bootstrap(pmap_curmaxkvaddr,
434 0xd0000000, &kernel_l1pt);
435 msgbufp = (void*)msgbufpv.pv_va;
436 msgbufinit(msgbufp, MSGBUF_SIZE);
437 mutex_init();
438
439 i = 0;
440 #ifdef ARM_USE_SMALL_ALLOC
441 phys_avail[i++] = 0x00000000;
442 phys_avail[i++] = 0x00001000; /*
443 *XXX: Gross hack to get our
444 * pages in the vm_page_array
445 . */
446 #endif
447 phys_avail[i++] = round_page(virtual_avail - KERNBASE + SDRAM_START);
448 phys_avail[i++] = trunc_page(0x00000000 + memsize - 1);
449 phys_avail[i++] = 0;
450 phys_avail[i] = 0;
451
452 /* Do basic tuning, hz etc */
453 init_param1();
454 init_param2(physmem);
455 kdb_init();
456 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
457 sizeof(struct pcb)));
458 }
Cache object: a0ca868078bf6486759ecb5f367fd907
|