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