]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/arm/xscale/i8134x/crb_machdep.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / arm / xscale / i8134x / crb_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
91 #include <arm/xscale/i80321/i80321var.h> /* For i80321_calibrate_delay() */
92
93 #include <arm/xscale/i8134x/i81342reg.h>
94 #include <arm/xscale/i8134x/i81342var.h>
95 #include <arm/xscale/i8134x/obiovar.h>
96
97
98 #define KERNEL_PT_SYS           0       /* Page table for mapping proc0 zero page */
99 #define KERNEL_PT_IOPXS         1
100 #define KERNEL_PT_BEFOREKERN    2
101 #define KERNEL_PT_AFKERNEL      3       /* L2 table for mapping after kernel */
102 #define KERNEL_PT_AFKERNEL_NUM  9
103
104 /* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
105 #define NUM_KERNEL_PTS          (KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
106
107 extern u_int data_abort_handler_address;
108 extern u_int prefetch_abort_handler_address;
109 extern u_int undefined_handler_address;
110
111 struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
112
113 /* Physical and virtual addresses for some global pages */
114
115 vm_paddr_t phys_avail[10];
116 vm_paddr_t dump_avail[4];
117
118 struct pv_addr systempage;
119 struct pv_addr msgbufpv;
120 struct pv_addr irqstack;
121 struct pv_addr undstack;
122 struct pv_addr abtstack;
123 struct pv_addr kernelstack;
124
125 /* Static device mappings. */
126 static const struct pmap_devmap iq81342_devmap[] = {
127             {
128                     IOP34X_VADDR,
129                     IOP34X_HWADDR,
130                     IOP34X_SIZE,
131                     VM_PROT_READ|VM_PROT_WRITE,
132                     PTE_NOCACHE,
133             },
134             {
135                     /*
136                      * Cheat and map a whole section, this will bring
137                      * both PCI-X and PCI-E outbound I/O
138                      */
139                     IOP34X_PCIX_OIOBAR_VADDR &~ (0x100000 - 1),
140                     IOP34X_PCIX_OIOBAR &~ (0x100000 - 1),
141                     0x100000,
142                     VM_PROT_READ|VM_PROT_WRITE,
143                     PTE_NOCACHE,
144             },
145             {
146                     IOP34X_PCE1_VADDR,
147                     IOP34X_PCE1,
148                     IOP34X_PCE1_SIZE,
149                     VM_PROT_READ|VM_PROT_WRITE,
150                     PTE_NOCACHE,
151             },
152             {   
153                     0,
154                     0,
155                     0,
156                     0,
157                     0,
158             }
159 };
160
161 #define SDRAM_START 0x00000000
162
163 extern vm_offset_t xscale_cache_clean_addr;
164
165 void *
166 initarm(struct arm_boot_params *abp)
167 {
168         struct pv_addr  kernel_l1pt;
169         struct pv_addr  dpcpu;
170         int loop, i;
171         u_int l1pagetable;
172         vm_offset_t freemempos;
173         vm_offset_t freemem_pt;
174         vm_offset_t afterkern;
175         vm_offset_t freemem_after;
176         vm_offset_t lastaddr;
177         uint32_t memsize, memstart;
178
179         lastaddr = parse_boot_param(abp);
180         set_cpufuncs();
181         pcpu_init(pcpup, 0, sizeof(struct pcpu));
182         PCPU_SET(curthread, &thread0);
183
184         /* Do basic tuning, hz etc */
185         init_param1();
186
187         freemempos = 0x00200000;
188         /* Define a macro to simplify memory allocation */
189 #define valloc_pages(var, np)                   \
190         alloc_pages((var).pv_pa, (np));         \
191         (var).pv_va = (var).pv_pa + 0xc0000000;
192
193 #define alloc_pages(var, np)                    \
194         freemempos -= (np * PAGE_SIZE);         \
195         (var) = freemempos;             \
196         memset((char *)(var), 0, ((np) * PAGE_SIZE));
197
198         while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
199                 freemempos -= PAGE_SIZE;
200         valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
201         for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
202                 if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
203                         valloc_pages(kernel_pt_table[loop],
204                             L2_TABLE_SIZE / PAGE_SIZE);
205                 } else {
206                         kernel_pt_table[loop].pv_pa = freemempos +
207                             (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
208                             L2_TABLE_SIZE_REAL;
209                         kernel_pt_table[loop].pv_va =
210                             kernel_pt_table[loop].pv_pa + 0xc0000000;
211                 }
212         }
213         freemem_pt = freemempos;
214         freemempos = 0x00100000;
215         /*
216          * Allocate a page for the system page mapped to V0x00000000
217          * This page will just contain the system vectors and can be
218          * shared by all processes.
219          */
220         valloc_pages(systempage, 1);
221
222         /* Allocate dynamic per-cpu area. */
223         valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
224         dpcpu_init((void *)dpcpu.pv_va, 0);
225
226         /* Allocate stacks for all modes */
227         valloc_pages(irqstack, IRQ_STACK_SIZE);
228         valloc_pages(abtstack, ABT_STACK_SIZE);
229         valloc_pages(undstack, UND_STACK_SIZE);
230         valloc_pages(kernelstack, KSTACK_PAGES);
231         valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
232 #ifdef ARM_USE_SMALL_ALLOC
233         freemempos -= PAGE_SIZE;
234         freemem_pt = trunc_page(freemem_pt);
235         freemem_after = freemempos - ((freemem_pt - 0x00100000) /
236             PAGE_SIZE) * sizeof(struct arm_small_page);
237         arm_add_smallalloc_pages((void *)(freemem_after + 0xc0000000)
238             , (void *)0xc0100000, freemem_pt - 0x00100000, 1);
239         freemem_after -= ((freemem_after - 0x00001000) / PAGE_SIZE) *
240             sizeof(struct arm_small_page);
241 #if 0
242         arm_add_smallalloc_pages((void *)(freemem_after + 0xc0000000)
243         , (void *)0xc0001000, trunc_page(freemem_after) - 0x00001000, 0);
244 #endif
245         freemempos = trunc_page(freemem_after);
246         freemempos -= PAGE_SIZE;
247 #endif
248         /*
249          * Now we start construction of the L1 page table
250          * We start by mapping the L2 page tables into the L1.
251          * This means that we can replace L1 mappings later on if necessary
252          */
253         l1pagetable = kernel_l1pt.pv_va;
254
255         /* Map the L2 pages tables in the L1 page table */
256         pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00100000 - 1),
257             &kernel_pt_table[KERNEL_PT_SYS]);
258         pmap_map_chunk(l1pagetable, KERNBASE, SDRAM_START, 0x100000,
259             VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
260
261         pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, SDRAM_START + 0x100000,
262             0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
263
264         pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, SDRAM_START + 0x200000,
265            (((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE) & ~(L1_S_SIZE - 1),
266             VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
267         freemem_after = ((int)lastaddr + PAGE_SIZE) & ~(PAGE_SIZE - 1);
268         afterkern = round_page(((vm_offset_t)lastaddr + L1_S_SIZE) & ~(L1_S_SIZE
269             - 1));
270         for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
271                 pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
272                     &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
273         }
274         
275
276 #ifdef ARM_USE_SMALL_ALLOC
277         if ((freemem_after + 2 * PAGE_SIZE) <= afterkern) {
278                 arm_add_smallalloc_pages((void *)(freemem_after),
279                     (void*)(freemem_after + PAGE_SIZE),
280                     afterkern - (freemem_after + PAGE_SIZE), 0);
281                 
282         }
283 #endif
284
285         /* Map the vector page. */
286         pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
287             VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
288         pmap_devmap_bootstrap(l1pagetable, iq81342_devmap);
289         /*
290          * Give the XScale global cache clean code an appropriately
291          * sized chunk of unmapped VA space starting at 0xff000000
292          * (our device mappings end before this address).
293          */
294         xscale_cache_clean_addr = 0xff000000U;
295
296         cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
297         setttb(kernel_l1pt.pv_pa);
298         cpu_tlb_flushID();
299         cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
300         /*
301          * Pages were allocated during the secondary bootstrap for the
302          * stacks for different CPU modes.
303          * We must now set the r13 registers in the different CPU modes to
304          * point to these stacks.
305          * Since the ARM stacks use STMFD etc. we must set r13 to the top end
306          * of the stack memory.
307          */
308
309         set_stackptrs(0);
310
311         /*
312          * We must now clean the cache again....
313          * Cleaning may be done by reading new data to displace any
314          * dirty data in the cache. This will have happened in setttb()
315          * but since we are boot strapping the addresses used for the read
316          * may have just been remapped and thus the cache could be out
317          * of sync. A re-clean after the switch will cure this.
318          * After booting there are no gross relocations of the kernel thus
319          * this problem will not occur after initarm().
320          */
321         cpu_idcache_wbinv_all();
322         i80321_calibrate_delay();
323         i81342_sdram_bounds(&obio_bs_tag, IOP34X_VADDR, &memstart, &memsize);
324         physmem = memsize / PAGE_SIZE;
325         cninit();
326         /* Set stack for exception handlers */
327         
328         data_abort_handler_address = (u_int)data_abort_handler;
329         prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
330         undefined_handler_address = (u_int)undefinedinstruction_bounce;
331         undefined_init();
332                                 
333         init_proc0(kernelstack.pv_va);
334         
335         arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
336
337         pmap_curmaxkvaddr = afterkern + PAGE_SIZE;
338         /*
339          * ARM_USE_SMALL_ALLOC uses dump_avail, so it must be filled before
340          * calling pmap_bootstrap.
341          */
342         dump_avail[0] = 0x00000000;
343         dump_avail[1] = 0x00000000 + memsize;
344         dump_avail[2] = 0;
345         dump_avail[3] = 0;
346                                         
347         vm_max_kernel_address = 0xd0000000;
348         pmap_bootstrap(pmap_curmaxkvaddr, &kernel_l1pt);
349         msgbufp = (void*)msgbufpv.pv_va;
350         msgbufinit(msgbufp, msgbufsize);
351         mutex_init();
352         
353         i = 0;
354 #ifdef ARM_USE_SMALL_ALLOC
355         phys_avail[i++] = 0x00001000;
356         phys_avail[i++] = 0x00002000;   /*
357                                          *XXX: Gross hack to get our
358                                          * pages in the vm_page_array
359                                          . */
360 #endif
361         phys_avail[i++] = round_page(virtual_avail - KERNBASE + SDRAM_START);
362         phys_avail[i++] = trunc_page(0x00000000 + memsize - 1);
363         phys_avail[i++] = 0;
364         phys_avail[i] = 0;
365         
366         init_param2(physmem);
367         kdb_init();
368         return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
369             sizeof(struct pcb)));
370 }