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