]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/arm/econa/econa_machdep.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / arm / econa / econa_machdep.c
1 /*-
2  * Copyright (c) 2009 Yohanes Nugroho <yohanes@gmail.com>
3  * Copyright (c) 1994-1998 Mark Brinicombe.
4  * Copyright (c) 1994 Brini.
5  * All rights reserved.
6  *
7  * This code is derived from software written for Brini by Mark Brinicombe
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Brini.
20  * 4. The name of the company nor the name of the author may be used to
21  *    endorse or promote products derived from this software without specific
22  *    prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
25  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #define _ARM32_BUS_DMA_PRIVATE
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/sysproto.h>
45 #include <sys/signalvar.h>
46 #include <sys/imgact.h>
47 #include <sys/kernel.h>
48 #include <sys/ktr.h>
49 #include <sys/linker.h>
50 #include <sys/lock.h>
51 #include <sys/malloc.h>
52 #include <sys/mutex.h>
53 #include <sys/pcpu.h>
54 #include <sys/proc.h>
55 #include <sys/ptrace.h>
56 #include <sys/cons.h>
57 #include <sys/bio.h>
58 #include <sys/bus.h>
59 #include <sys/buf.h>
60 #include <sys/exec.h>
61 #include <sys/kdb.h>
62 #include <sys/msgbuf.h>
63 #include <machine/reg.h>
64 #include <machine/cpu.h>
65
66 #include <vm/vm.h>
67 #include <vm/pmap.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_pager.h>
71 #include <vm/vm_map.h>
72 #include <vm/vnode_pager.h>
73 #include <machine/pmap.h>
74 #include <machine/vmparam.h>
75 #include <machine/pcb.h>
76 #include <machine/undefined.h>
77 #include <machine/machdep.h>
78 #include <machine/metadata.h>
79 #include <machine/armreg.h>
80 #include <machine/bus.h>
81 #include <sys/reboot.h>
82 #include "econa_reg.h"
83
84 /* Page table for mapping proc0 zero page */
85 #define KERNEL_PT_SYS           0
86 #define KERNEL_PT_KERN          1
87 #define KERNEL_PT_KERN_NUM      22
88 /* L2 table for mapping after kernel */
89 #define KERNEL_PT_AFKERNEL      KERNEL_PT_KERN + KERNEL_PT_KERN_NUM
90 #define KERNEL_PT_AFKERNEL_NUM  5
91
92 /* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
93 #define NUM_KERNEL_PTS  (KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
94
95 /* Define various stack sizes in pages */
96 #define IRQ_STACK_SIZE  1
97 #define ABT_STACK_SIZE  1
98 #define UND_STACK_SIZE  1
99
100 extern u_int data_abort_handler_address;
101 extern u_int prefetch_abort_handler_address;
102 extern u_int undefined_handler_address;
103
104 struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
105
106 extern void *_end;
107
108 extern int *end;
109
110 struct pcpu __pcpu;
111 struct pcpu *pcpup = &__pcpu;
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 vm_offset_t physical_pages;
118
119 struct pv_addr systempage;
120 struct pv_addr msgbufpv;
121 struct pv_addr irqstack;
122 struct pv_addr undstack;
123 struct pv_addr abtstack;
124 struct pv_addr kernelstack;
125
126 static void *boot_arg1;
127 static void *boot_arg2;
128
129 static struct trapframe proc0_tf;
130
131 /* Static device mappings. */
132 static const struct pmap_devmap econa_devmap[] = {
133         {
134                 /*
135                  * This maps DDR SDRAM
136                  */
137                 ECONA_SDRAM_BASE, /*virtual*/
138                 ECONA_SDRAM_BASE, /*physical*/
139                 ECONA_SDRAM_SIZE, /*size*/
140                 VM_PROT_READ|VM_PROT_WRITE,
141                 PTE_NOCACHE,
142         },
143         /*
144          * Map the on-board devices VA == PA so that we can access them
145          * with the MMU on or off.
146          */
147         {
148                 /*
149                  * This maps the interrupt controller, the UART
150                  * and the timer.
151                  */
152                 ECONA_IO_BASE, /*virtual*/
153                 ECONA_IO_BASE, /*physical*/
154                 ECONA_IO_SIZE, /*size*/
155                 VM_PROT_READ|VM_PROT_WRITE,
156                 PTE_NOCACHE,
157         },
158         {
159                 /*
160                  * OHCI + EHCI
161                  */
162                 ECONA_OHCI_VBASE, /*virtual*/
163                 ECONA_OHCI_PBASE, /*physical*/
164                 ECONA_USB_SIZE, /*size*/
165                 VM_PROT_READ|VM_PROT_WRITE,
166                 PTE_NOCACHE,
167         },
168         {
169                 /*
170                  * CFI
171                  */
172                 ECONA_CFI_VBASE, /*virtual*/
173                 ECONA_CFI_PBASE, /*physical*/
174                 ECONA_CFI_SIZE,
175                 VM_PROT_READ|VM_PROT_WRITE,
176                 PTE_NOCACHE,
177         },
178         {
179                 0,
180                 0,
181                 0,
182                 0,
183                 0,
184         }
185 };
186
187
188 void *
189 initarm(void *arg, void *arg2)
190 {
191         struct pv_addr  kernel_l1pt;
192         volatile uint32_t * ddr = (uint32_t *)0x4000000C;
193         int loop, i;
194         u_int l1pagetable;
195         vm_offset_t afterkern;
196         vm_offset_t freemempos;
197         vm_offset_t lastaddr;
198         uint32_t memsize;
199         int mem_info;
200
201
202         boot_arg1 = arg;
203         boot_arg2 = arg2;
204         boothowto = RB_VERBOSE;
205
206         set_cpufuncs();
207         lastaddr = fake_preload_metadata();
208         pcpu_init(pcpup, 0, sizeof(struct pcpu));
209         PCPU_SET(curthread, &thread0);
210
211         /* Do basic tuning, hz etc */
212         init_param1();
213                 
214
215         freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
216         /* Define a macro to simplify memory allocation */
217 #define valloc_pages(var, np)                   \
218         alloc_pages((var).pv_va, (np));         \
219         (var).pv_pa = (var).pv_va + (KERNPHYSADDR - KERNVIRTADDR);
220
221 #define alloc_pages(var, np)                    \
222         (var) = freemempos;             \
223         freemempos += (np * PAGE_SIZE);         \
224         memset((char *)(var), 0, ((np) * PAGE_SIZE));
225
226         while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
227                 freemempos += PAGE_SIZE;
228         valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
229         for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
230                 if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
231                         valloc_pages(kernel_pt_table[loop],
232                             L2_TABLE_SIZE / PAGE_SIZE);
233                 } else {
234                         kernel_pt_table[loop].pv_va = freemempos -
235                             (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
236                             L2_TABLE_SIZE_REAL;
237                         kernel_pt_table[loop].pv_pa =
238                             kernel_pt_table[loop].pv_va - KERNVIRTADDR +
239                             KERNPHYSADDR;
240                 }
241                 i++;
242         }
243         /*
244          * Allocate a page for the system page mapped to V0x00000000
245          * This page will just contain the system vectors and can be
246          * shared by all processes.
247          */
248         valloc_pages(systempage, 1);
249
250         /* Allocate stacks for all modes */
251         valloc_pages(irqstack, IRQ_STACK_SIZE);
252         valloc_pages(abtstack, ABT_STACK_SIZE);
253         valloc_pages(undstack, UND_STACK_SIZE);
254         valloc_pages(kernelstack, KSTACK_PAGES);
255         valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
256
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,
266             &kernel_pt_table[KERNEL_PT_SYS]);
267         for (i = 0; i < KERNEL_PT_KERN_NUM; i++)
268                 pmap_link_l2pt(l1pagetable, KERNBASE + i * L1_S_SIZE,
269                     &kernel_pt_table[KERNEL_PT_KERN + i]);
270         pmap_map_chunk(l1pagetable, KERNBASE, PHYSADDR,
271            (((uint32_t)lastaddr - KERNBASE) + PAGE_SIZE) & ~(PAGE_SIZE - 1),
272             VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
273         afterkern = round_page((lastaddr + L1_S_SIZE) & ~(L1_S_SIZE - 1));
274         for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
275                 pmap_link_l2pt(l1pagetable, afterkern + i * L1_S_SIZE,
276                     &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
277         }
278
279         /* Map the vector page. */
280         pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
281             VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
282
283
284         /* Map the stack pages */
285         pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
286             IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
287         pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
288             ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
289         pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
290             UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
291         pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
292             KSTACK_PAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
293
294         pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
295             L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
296         pmap_map_chunk(l1pagetable, msgbufpv.pv_va, msgbufpv.pv_pa,
297             msgbufsize, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
298
299         for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
300                 pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
301                     kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
302                     VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
303         }
304
305         pmap_devmap_bootstrap(l1pagetable, econa_devmap);
306         cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
307         setttb(kernel_l1pt.pv_pa);
308         cpu_tlb_flushID();
309         cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
310         cninit();
311         mem_info = ((*ddr) >> 4) & 0x3;
312         memsize = (8<<mem_info)*1024*1024;
313         physmem = memsize / PAGE_SIZE;
314
315         /*
316          * Pages were allocated during the secondary bootstrap for the
317          * stacks for different CPU modes.
318          * We must now set the r13 registers in the different CPU modes to
319          * point to these stacks.
320          * Since the ARM stacks use STMFD etc. we must set r13 to the top end
321          * of the stack memory.
322          */
323         cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
324
325         set_stackptr(PSR_IRQ32_MODE,
326             irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
327         set_stackptr(PSR_ABT32_MODE,
328             abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
329         set_stackptr(PSR_UND32_MODE,
330             undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
331
332         /*
333          * We must now clean the cache again....
334          * Cleaning may be done by reading new data to displace any
335          * dirty data in the cache. This will have happened in setttb()
336          * but since we are boot strapping the addresses used for the read
337          * may have just been remapped and thus the cache could be out
338          * of sync. A re-clean after the switch will cure this.
339          * After booting there are no gross relocations of the kernel thus
340          * this problem will not occur after initarm().
341          */
342         cpu_idcache_wbinv_all();
343
344         /* Set stack for exception handlers */
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         proc_linkup0(&proc0, &thread0);
351         thread0.td_kstack = kernelstack.pv_va;
352         thread0.td_pcb = (struct pcb *)
353                 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
354         thread0.td_pcb->pcb_flags = 0;
355         thread0.td_frame = &proc0_tf;
356         pcpup->pc_curpcb = thread0.td_pcb;
357
358         arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
359
360         pmap_curmaxkvaddr = afterkern + L1_S_SIZE * (KERNEL_PT_KERN_NUM - 1);
361
362         /*
363          * ARM_USE_SMALL_ALLOC uses dump_avail, so it must be filled before
364          * calling pmap_bootstrap.
365          */
366         dump_avail[0] = PHYSADDR;
367         dump_avail[1] = PHYSADDR + memsize;
368         dump_avail[2] = 0;
369         dump_avail[3] = 0;
370
371         pmap_bootstrap(freemempos,
372             KERNVIRTADDR + 3 * memsize,
373             &kernel_l1pt);
374
375         msgbufp = (void*)msgbufpv.pv_va;
376         msgbufinit(msgbufp, msgbufsize);
377
378         mutex_init();
379
380         i = 0;
381 #if PHYSADDR != KERNPHYSADDR
382         phys_avail[i++] = PHYSADDR;
383         phys_avail[i++] = KERNPHYSADDR;
384 #endif
385         phys_avail[i++] = virtual_avail - KERNVIRTADDR + KERNPHYSADDR;
386
387         phys_avail[i++] = PHYSADDR + memsize;
388         phys_avail[i++] = 0;
389         phys_avail[i++] = 0;
390         init_param2(physmem);
391         kdb_init();
392
393         return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
394             sizeof(struct pcb)));
395 }