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