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_object.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_pager.h>
84 #include <vm/vm_map.h>
85 #include <vm/vnode_pager.h>
86 #include <machine/pmap.h>
87 #include <machine/vmparam.h>
88 #include <machine/pcb.h>
89 #include <machine/undefined.h>
90 #include <machine/machdep.h>
91 #include <machine/metadata.h>
92 #include <machine/armreg.h>
93 #include <machine/bus.h>
94 #include <sys/reboot.h>
95
96 #include <arm/xscale/i80321/i80321reg.h>
97 #include <arm/xscale/i80321/i80321var.h>
98 #include <arm/xscale/i80321/iq80321reg.h>
99 #include <arm/xscale/i80321/obiovar.h>
100
101 #define KERNEL_PT_SYS 0 /* Page table for mapping proc0 zero page */
102 #define KERNEL_PT_IOPXS 1
103 #define KERNEL_PT_BEFOREKERN 2
104 #define KERNEL_PT_AFKERNEL 3 /* L2 table for mapping after kernel */
105 #define KERNEL_PT_AFKERNEL_NUM 9
106
107 /* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
108 #define NUM_KERNEL_PTS (KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
109
110 /* Define various stack sizes in pages */
111 #define IRQ_STACK_SIZE 1
112 #define ABT_STACK_SIZE 1
113 #ifdef IPKDB
114 #define UND_STACK_SIZE 2
115 #else
116 #define UND_STACK_SIZE 1
117 #endif
118
119 extern u_int data_abort_handler_address;
120 extern u_int prefetch_abort_handler_address;
121 extern u_int undefined_handler_address;
122
123 struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
124
125 extern void *_end;
126
127 extern int *end;
128
129 struct pcpu __pcpu;
130 struct pcpu *pcpup = &__pcpu;
131
132 /* Physical and virtual addresses for some global pages */
133
134 vm_paddr_t phys_avail[10];
135 vm_paddr_t dump_avail[4];
136 vm_offset_t physical_pages;
137 vm_offset_t clean_sva, clean_eva;
138
139 struct pv_addr systempage;
140 struct pv_addr msgbufpv;
141 struct pv_addr irqstack;
142 struct pv_addr undstack;
143 struct pv_addr abtstack;
144 struct pv_addr kernelstack;
145 struct pv_addr minidataclean;
146
147 static struct trapframe proc0_tf;
148
149
150 /* #define IQ80321_OBIO_BASE 0xfe800000UL */
151 /* #define IQ80321_OBIO_SIZE 0x00100000UL */
152
153 /* Static device mappings. */
154 static const struct pmap_devmap ep80219_devmap[] = {
155 /*
156 * Map the on-board devices VA == PA so that we can access them
157 * with the MMU on or off.
158 */
159 {
160 IQ80321_OBIO_BASE,
161 IQ80321_OBIO_BASE,
162 IQ80321_OBIO_SIZE,
163 VM_PROT_READ|VM_PROT_WRITE,
164 PTE_NOCACHE,
165 },
166 {
167 IQ80321_IOW_VBASE,
168 VERDE_OUT_XLATE_IO_WIN0_BASE,
169 VERDE_OUT_XLATE_IO_WIN_SIZE,
170 VM_PROT_READ|VM_PROT_WRITE,
171 PTE_NOCACHE,
172 },
173 {
174 IQ80321_80321_VBASE,
175 VERDE_PMMR_BASE,
176 VERDE_PMMR_SIZE,
177 VM_PROT_READ|VM_PROT_WRITE,
178 PTE_NOCACHE,
179 },
180 {
181 0,
182 0,
183 0,
184 0,
185 0,
186 }
187 };
188
189 #ifdef DDB
190 extern vm_offset_t ksym_start, ksym_end;
191 #endif
192
193 extern vm_offset_t xscale_cache_clean_addr;
194
195 void *
196 initarm(void *arg, void *arg2)
197 {
198 struct pv_addr kernel_l1pt;
199 int loop;
200 u_int l1pagetable;
201 vm_offset_t freemempos;
202 vm_offset_t freemem_pt;
203 vm_offset_t afterkern;
204 vm_offset_t freemem_after;
205 vm_offset_t lastaddr;
206 #ifdef DDB
207 vm_offset_t zstart = 0, zend = 0;
208 #endif
209 int i = 0;
210 uint32_t fake_preload[35];
211 uint32_t memsize, memstart;
212
213 i = 0;
214
215 set_cpufuncs();
216 fake_preload[i++] = MODINFO_NAME;
217 fake_preload[i++] = strlen("elf kernel") + 1;
218 strcpy((char*)&fake_preload[i++], "elf kernel");
219 i += 2;
220 fake_preload[i++] = MODINFO_TYPE;
221 fake_preload[i++] = strlen("elf kernel") + 1;
222 strcpy((char*)&fake_preload[i++], "elf kernel");
223 i += 2;
224 fake_preload[i++] = MODINFO_ADDR;
225 fake_preload[i++] = sizeof(vm_offset_t);
226 fake_preload[i++] = KERNBASE + 0x00200000;
227 fake_preload[i++] = MODINFO_SIZE;
228 fake_preload[i++] = sizeof(uint32_t);
229 fake_preload[i++] = (uint32_t)&end - KERNBASE - 0x00200000;
230 #ifdef DDB
231 if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
232 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
233 fake_preload[i++] = sizeof(vm_offset_t);
234 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
235 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
236 fake_preload[i++] = sizeof(vm_offset_t);
237 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
238 lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
239 zend = lastaddr;
240 zstart = *(uint32_t *)(KERNVIRTADDR + 4);
241 ksym_start = zstart;
242 ksym_end = zend;
243 } else
244 #endif
245 lastaddr = (vm_offset_t)&end;
246
247 fake_preload[i++] = 0;
248 fake_preload[i] = 0;
249 preload_metadata = (void *)fake_preload;
250
251
252 pcpu_init(pcpup, 0, sizeof(struct pcpu));
253 PCPU_SET(curthread, &thread0);
254
255 #define KERNEL_TEXT_BASE (KERNBASE + 0x00200000)
256 freemempos = 0xa0200000;
257 /* Define a macro to simplify memory allocation */
258 #define valloc_pages(var, np) \
259 alloc_pages((var).pv_pa, (np)); \
260 (var).pv_va = (var).pv_pa + 0x20000000;
261
262 #define alloc_pages(var, np) \
263 freemempos -= (np * PAGE_SIZE); \
264 (var) = freemempos; \
265 memset((char *)(var), 0, ((np) * PAGE_SIZE));
266
267 while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
268 freemempos -= PAGE_SIZE;
269 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
270 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
271 if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
272 valloc_pages(kernel_pt_table[loop],
273 L2_TABLE_SIZE / PAGE_SIZE);
274 } else {
275 kernel_pt_table[loop].pv_pa = freemempos +
276 (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
277 L2_TABLE_SIZE_REAL;
278 kernel_pt_table[loop].pv_va =
279 kernel_pt_table[loop].pv_pa + 0x20000000;
280 }
281 i++;
282 }
283 freemem_pt = freemempos;
284 freemempos = 0xa0100000;
285 /*
286 * Allocate a page for the system page mapped to V0x00000000
287 * This page will just contain the system vectors and can be
288 * shared by all processes.
289 */
290 valloc_pages(systempage, 1);
291
292 /* Allocate stacks for all modes */
293 valloc_pages(irqstack, IRQ_STACK_SIZE);
294 valloc_pages(abtstack, ABT_STACK_SIZE);
295 valloc_pages(undstack, UND_STACK_SIZE);
296 valloc_pages(kernelstack, KSTACK_PAGES);
297 alloc_pages(minidataclean.pv_pa, 1);
298 valloc_pages(msgbufpv, round_page(MSGBUF_SIZE) / PAGE_SIZE);
299 #ifdef ARM_USE_SMALL_ALLOC
300 freemempos -= PAGE_SIZE;
301 freemem_pt = trunc_page(freemem_pt);
302 freemem_after = freemempos - ((freemem_pt - 0xa0100000) /
303 PAGE_SIZE) * sizeof(struct arm_small_page);
304 arm_add_smallalloc_pages((void *)(freemem_after + 0x20000000)
305 , (void *)0xc0100000, freemem_pt - 0xa0100000, 1);
306 freemem_after -= ((freemem_after - 0xa0001000) / PAGE_SIZE) *
307 sizeof(struct arm_small_page);
308 arm_add_smallalloc_pages((void *)(freemem_after + 0x20000000),
309 (void *)0xc0001000,
310 trunc_page(freemem_after) - 0xa0001000, 0);
311
312 freemempos = trunc_page(freemem_after);
313 freemempos -= PAGE_SIZE;
314 #endif
315 /*
316 * Allocate memory for the l1 and l2 page tables. The scheme to avoid
317 * wasting memory by allocating the l1pt on the first 16k memory was
318 * taken from NetBSD rpc_machdep.c. NKPT should be greater than 12 for
319 * this to work (which is supposed to be the case).
320 */
321
322 /*
323 * Now we start construction of the L1 page table
324 * We start by mapping the L2 page tables into the L1.
325 * This means that we can replace L1 mappings later on if necessary
326 */
327 l1pagetable = kernel_l1pt.pv_va;
328
329 /* Map the L2 pages tables in the L1 page table */
330 pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00100000 - 1),
331 &kernel_pt_table[KERNEL_PT_SYS]);
332 pmap_link_l2pt(l1pagetable, IQ80321_IOPXS_VBASE,
333 &kernel_pt_table[KERNEL_PT_IOPXS]);
334 pmap_link_l2pt(l1pagetable, KERNBASE,
335 &kernel_pt_table[KERNEL_PT_BEFOREKERN]);
336 pmap_map_chunk(l1pagetable, KERNBASE, IQ80321_SDRAM_START, 0x100000,
337 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
338 pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, IQ80321_SDRAM_START + 0x100000,
339 0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
340 pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, IQ80321_SDRAM_START + 0x200000,
341 (((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE) & ~(L1_S_SIZE - 1),
342 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
343 freemem_after = ((int)lastaddr + PAGE_SIZE) & ~(PAGE_SIZE - 1);
344 afterkern = round_page(((vm_offset_t)lastaddr + L1_S_SIZE) & ~(L1_S_SIZE
345 - 1));
346 for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
347 pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
348 &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
349 }
350 pmap_map_entry(l1pagetable, afterkern, minidataclean.pv_pa,
351 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
352
353
354 #ifdef ARM_USE_SMALL_ALLOC
355 if ((freemem_after + 2 * PAGE_SIZE) <= afterkern) {
356 arm_add_smallalloc_pages((void *)(freemem_after),
357 (void*)(freemem_after + PAGE_SIZE),
358 afterkern - (freemem_after + PAGE_SIZE), 0);
359
360 }
361 #endif
362
363 /* Map the Mini-Data cache clean area. */
364 xscale_setup_minidata(l1pagetable, afterkern,
365 minidataclean.pv_pa);
366
367 /* Map the vector page. */
368 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
369 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
370 pmap_devmap_bootstrap(l1pagetable, ep80219_devmap);
371 /*
372 * Give the XScale global cache clean code an appropriately
373 * sized chunk of unmapped VA space starting at 0xff000000
374 * (our device mappings end before this address).
375 */
376 xscale_cache_clean_addr = 0xff000000U;
377
378 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
379 setttb(kernel_l1pt.pv_pa);
380 cpu_tlb_flushID();
381 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
382 /*
383 * Pages were allocated during the secondary bootstrap for the
384 * stacks for different CPU modes.
385 * We must now set the r13 registers in the different CPU modes to
386 * point to these stacks.
387 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
388 * of the stack memory.
389 */
390
391
392 set_stackptr(PSR_IRQ32_MODE,
393 irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
394 set_stackptr(PSR_ABT32_MODE,
395 abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
396 set_stackptr(PSR_UND32_MODE,
397 undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
398
399
400
401 /*
402 * We must now clean the cache again....
403 * Cleaning may be done by reading new data to displace any
404 * dirty data in the cache. This will have happened in setttb()
405 * but since we are boot strapping the addresses used for the read
406 * may have just been remapped and thus the cache could be out
407 * of sync. A re-clean after the switch will cure this.
408 * After booting there are no gross reloations of the kernel thus
409 * this problem will not occur after initarm().
410 */
411 cpu_idcache_wbinv_all();
412 /*
413 * Fetch the SDRAM start/size from the i80321 SDRAM configration
414 * registers.
415 */
416 i80321_calibrate_delay();
417 i80321_sdram_bounds(&obio_bs_tag, IQ80321_80321_VBASE + VERDE_MCU_BASE,
418 &memstart, &memsize);
419 physmem = memsize / PAGE_SIZE;
420 cninit();
421
422 /* Set stack for exception handlers */
423
424 data_abort_handler_address = (u_int)data_abort_handler;
425 prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
426 undefined_handler_address = (u_int)undefinedinstruction_bounce;
427 undefined_init();
428
429 proc_linkup0(&proc0, &thread0);
430 thread0.td_kstack = kernelstack.pv_va;
431 thread0.td_pcb = (struct pcb *)
432 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
433 thread0.td_pcb->pcb_flags = 0;
434 thread0.td_frame = &proc0_tf;
435 pcpup->pc_curpcb = thread0.td_pcb;
436
437 /* Enable MMU, I-cache, D-cache, write buffer. */
438
439 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
440
441
442
443 pmap_curmaxkvaddr = afterkern + PAGE_SIZE;
444 dump_avail[0] = 0xa0000000;
445 dump_avail[1] = 0xa0000000 + memsize;
446 dump_avail[2] = 0;
447 dump_avail[3] = 0;
448 pmap_bootstrap(pmap_curmaxkvaddr,
449 0xd0000000, &kernel_l1pt);
450 msgbufp = (void*)msgbufpv.pv_va;
451 msgbufinit(msgbufp, MSGBUF_SIZE);
452 mutex_init();
453
454 i = 0;
455 #ifdef ARM_USE_SMALL_ALLOC
456 phys_avail[i++] = 0xa0000000;
457 phys_avail[i++] = 0xa0001000; /*
458 *XXX: Gross hack to get our
459 * pages in the vm_page_array
460 . */
461 #endif
462 phys_avail[i++] = round_page(virtual_avail - KERNBASE + IQ80321_SDRAM_START);
463 phys_avail[i++] = trunc_page(0xa0000000 + memsize - 1);
464 phys_avail[i++] = 0;
465 phys_avail[i] = 0;
466
467 /* Do basic tuning, hz etc */
468 init_param1();
469 init_param2(physmem);
470 kdb_init();
471 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
472 sizeof(struct pcb)));
473 }
474
475 extern int
476 machdep_pci_route_interrupt(device_t pcib, device_t dev, int pin)
477 {
478 int bus;
479 int device;
480 int func;
481 uint32_t busno;
482 struct i80321_pci_softc *sc = device_get_softc(pcib);
483 bus = pci_get_bus(dev);
484 device = pci_get_slot(dev);
485 func = pci_get_function(dev);
486 busno = bus_space_read_4(sc->sc_st, sc->sc_atu_sh, ATU_PCIXSR);
487 busno = PCIXSR_BUSNO(busno);
488 if (busno == 0xff)
489 busno = 0;
490 if (bus != busno)
491 goto no_mapping;
492 switch (device) {
493 /* EP80219 PCI */
494 case 1: /* Ethernet i82555 10/100 */
495 printf("Device %d routed to irq %d\n", device, ICU_INT_XINT(0));
496 return (ICU_INT_XINT(0));
497 case 2: /* UART */
498 printf("Device %d routed to irq %d\n", device, ICU_INT_XINT(1));
499 return (ICU_INT_XINT(1));
500 case 3:
501 /*
502 * The S-ATA chips are behind the bridge, and all of
503 * the S-ATA interrupts are wired together.
504 */
505 printf("Device %d routed to irq %d\n", device, ICU_INT_XINT(2));
506 return (ICU_INT_XINT(2));
507 case 4: /* MINI-PIC_INT */
508 printf("Device %d routed to irq %d\n", device, ICU_INT_XINT(3));
509 return( ICU_INT_XINT(3));
510 default:
511 no_mapping:
512 printf("No mapping for %d/%d/%d/%c\n", bus, device, func, pin);
513
514 }
515 return (0);
516
517 }
Cache object: 5ec4723303692f1342d6be027ace3c4e
|