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