]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/arm/sa11x0/assabet_machdep.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / arm / sa11x0 / assabet_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
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
51
52 #include "opt_md.h"
53
54 #define _ARM32_BUS_DMA_PRIVATE
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/sysproto.h>
58 #include <sys/signalvar.h>
59 #include <sys/imgact.h>
60 #include <sys/kernel.h>
61 #include <sys/ktr.h>
62 #include <sys/linker.h>
63 #include <sys/lock.h>
64 #include <sys/malloc.h>
65 #include <sys/mutex.h>
66 #include <sys/pcpu.h>
67 #include <sys/proc.h>
68 #include <sys/ptrace.h>
69 #include <sys/cons.h>
70 #include <sys/bio.h>
71 #include <sys/bus.h>
72 #include <sys/buf.h>
73 #include <sys/exec.h>
74 #include <sys/kdb.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 <machine/pmap.h>
85 #include <machine/vmparam.h>
86 #include <machine/pcb.h>
87 #include <machine/undefined.h>
88 #include <machine/machdep.h>
89 #include <machine/metadata.h>
90 #include <machine/armreg.h>
91 #include <machine/bus.h>
92 #include <sys/reboot.h>
93
94 #include <arm/sa11x0/sa11x0_reg.h>
95
96 #define MDROOT_ADDR 0xd0400000
97
98 #define KERNEL_PT_VMEM          0       /* Page table for mapping video memory */
99 #define KERNEL_PT_SYS           0       /* Page table for mapping proc0 zero page */
100 #define KERNEL_PT_IO            3       /* Page table for mapping IO */
101 #define KERNEL_PT_IRQ           2       /* Page table for mapping irq handler */
102 #define KERNEL_PT_KERNEL        1       /* Page table for mapping kernel */
103 #define KERNEL_PT_L1            4       /* Page table for mapping l1pt */
104 #define KERNEL_PT_VMDATA        5       /* Page tables for mapping kernel VM */
105 #define KERNEL_PT_VMDATA_NUM    7       /* start with 16MB of KVM */
106 #define NUM_KERNEL_PTS          (KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
107
108 /* Define various stack sizes in pages */
109 #define IRQ_STACK_SIZE  1
110 #define ABT_STACK_SIZE  1
111 #define UND_STACK_SIZE  1
112
113 #define KERNEL_VM_BASE          (KERNBASE + 0x00100000)
114 #define KERNEL_VM_SIZE          0x05000000
115
116 extern u_int data_abort_handler_address;
117 extern u_int prefetch_abort_handler_address;
118 extern u_int undefined_handler_address;
119
120 struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
121
122 extern void *_end;
123
124 extern vm_offset_t sa1110_uart_vaddr;
125
126 extern vm_offset_t sa1_cache_clean_addr;
127
128 extern int *end;
129
130 struct pcpu __pcpu;
131 struct pcpu *pcpup = &__pcpu;
132
133 #ifndef MD_ROOT_SIZE
134 #define MD_ROOT_SIZE 65535
135 #endif
136 /* Physical and virtual addresses for some global pages */
137
138 vm_paddr_t phys_avail[10];
139 vm_paddr_t dump_avail[4];
140 vm_paddr_t physical_start;
141 vm_paddr_t physical_end;
142 vm_paddr_t physical_freestart;
143 vm_offset_t physical_pages;
144
145 struct pv_addr systempage;
146 struct pv_addr irqstack;
147 struct pv_addr undstack;
148 struct pv_addr abtstack;
149 struct pv_addr kernelstack;
150 static struct trapframe proc0_tf;
151
152 /* Static device mappings. */
153 static const struct pmap_devmap assabet_devmap[] = {
154         /*
155          * Map the on-board devices VA == PA so that we can access them
156          * with the MMU on or off.
157          */
158         {
159                 SACOM1_VBASE,
160                 SACOM1_BASE,
161                 SACOM1_SIZE,
162                 VM_PROT_READ|VM_PROT_WRITE,
163                 PTE_NOCACHE,
164         },
165         {
166                 SAIPIC_BASE,
167                 SAIPIC_BASE,
168                 SAIPIC_SIZE,
169                 VM_PROT_READ|VM_PROT_WRITE,
170                 PTE_NOCACHE,
171         },
172         {
173                 0,
174                 0,
175                 0,
176                 0,
177                 0,
178         }
179 };
180
181 struct arm32_dma_range *
182 bus_dma_get_range(void)
183 {
184
185         return (NULL);
186 }
187
188 int
189 bus_dma_get_range_nb(void)
190 {
191         return (0);
192 }
193
194 void
195 cpu_reset()
196 {
197         cpu_halt();
198         while (1);
199 }
200
201 #define CPU_SA110_CACHE_CLEAN_SIZE (0x4000 * 2)
202
203 void *
204 initarm(void *arg, void *arg2)
205 {
206         struct pcpu *pc;
207         struct pv_addr  kernel_l1pt;
208         struct pv_addr  md_addr;
209         struct pv_addr  md_bla;
210         struct pv_addr  dpcpu;
211         int loop;
212         u_int l1pagetable;
213         vm_offset_t freemempos;
214         vm_offset_t lastalloced;
215         vm_offset_t lastaddr;
216         uint32_t memsize = 32 * 1024 * 1024;
217         sa1110_uart_vaddr = SACOM1_VBASE;
218
219         boothowto = RB_VERBOSE | RB_SINGLE;
220         cninit();
221         set_cpufuncs();
222         lastaddr = fake_preload_metadata();
223         physmem = memsize / PAGE_SIZE;
224         pc = &__pcpu;
225         pcpu_init(pc, 0, sizeof(struct pcpu));
226         PCPU_SET(curthread, &thread0);
227
228         /* Do basic tuning, hz etc */
229         init_param1();
230                 
231         physical_start = (vm_offset_t) KERNBASE;
232         physical_end =  lastaddr;
233         physical_freestart = (((vm_offset_t)physical_end) + PAGE_MASK) & ~PAGE_MASK;
234         md_addr.pv_va = md_addr.pv_pa = MDROOT_ADDR;
235         freemempos = (vm_offset_t)round_page(physical_freestart);
236         memset((void *)freemempos, 0, 256*1024);
237                 /* Define a macro to simplify memory allocation */
238 #define valloc_pages(var, np)                   \
239         alloc_pages((var).pv_pa, (np));         \
240         (var).pv_va = (var).pv_pa;
241
242 #define alloc_pages(var, np)                    \
243         (var) = freemempos;             \
244         freemempos += ((np) * PAGE_SIZE);\
245         memset((char *)(var), 0, ((np) * PAGE_SIZE));
246
247         while ((freemempos & (L1_TABLE_SIZE - 1)) != 0)
248                 freemempos += PAGE_SIZE;
249         valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
250         valloc_pages(md_bla, L2_TABLE_SIZE / PAGE_SIZE);
251         alloc_pages(sa1_cache_clean_addr, CPU_SA110_CACHE_CLEAN_SIZE / PAGE_SIZE);
252
253         for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
254                 if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
255                         valloc_pages(kernel_pt_table[loop],
256                             L2_TABLE_SIZE / PAGE_SIZE);
257                 } else {
258                         kernel_pt_table[loop].pv_pa = freemempos +
259                             (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
260                             L2_TABLE_SIZE_REAL;
261                         kernel_pt_table[loop].pv_va = 
262                             kernel_pt_table[loop].pv_pa;
263                 }
264         }
265
266         /*
267          * Allocate a page for the system page mapped to V0x00000000
268          * This page will just contain the system vectors and can be
269          * shared by all processes.
270          */
271         valloc_pages(systempage, 1);
272
273         /* Allocate dynamic per-cpu area. */
274         valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
275         dpcpu_init((void *)dpcpu.pv_va, 0);
276
277         /* Allocate stacks for all modes */
278         valloc_pages(irqstack, IRQ_STACK_SIZE);
279         valloc_pages(abtstack, ABT_STACK_SIZE);
280         valloc_pages(undstack, UND_STACK_SIZE);
281         valloc_pages(kernelstack, KSTACK_PAGES);
282         lastalloced = kernelstack.pv_va;
283
284         /*
285          * Allocate memory for the l1 and l2 page tables. The scheme to avoid
286          * wasting memory by allocating the l1pt on the first 16k memory was
287          * taken from NetBSD rpc_machdep.c. NKPT should be greater than 12 for
288          * this to work (which is supposed to be the case).
289          */
290
291         /*
292          * Now we start construction of the L1 page table
293          * We start by mapping the L2 page tables into the L1.
294          * This means that we can replace L1 mappings later on if necessary
295          */
296         l1pagetable = kernel_l1pt.pv_pa;
297
298
299         /* Map the L2 pages tables in the L1 page table */
300         pmap_link_l2pt(l1pagetable, 0x00000000,
301             &kernel_pt_table[KERNEL_PT_SYS]);
302         pmap_link_l2pt(l1pagetable, KERNBASE,
303             &kernel_pt_table[KERNEL_PT_KERNEL]);
304         pmap_link_l2pt(l1pagetable, 0xd0000000,
305             &kernel_pt_table[KERNEL_PT_IO]);
306         pmap_link_l2pt(l1pagetable, lastalloced & ~((L1_S_SIZE * 4) - 1),
307             &kernel_pt_table[KERNEL_PT_L1]);
308         pmap_link_l2pt(l1pagetable, 0x90000000, &kernel_pt_table[KERNEL_PT_IRQ]);
309         pmap_link_l2pt(l1pagetable, MDROOT_ADDR,
310             &md_bla);
311         for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
312                 pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00100000,
313                     &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
314         pmap_map_chunk(l1pagetable, KERNBASE, KERNBASE,
315             ((uint32_t)lastaddr - KERNBASE), VM_PROT_READ|VM_PROT_WRITE,
316             PTE_CACHE);
317         /* Map the DPCPU pages */
318         pmap_map_chunk(l1pagetable, dpcpu.pv_va, dpcpu.pv_pa, DPCPU_SIZE,
319             VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
320         /* Map the stack pages */
321         pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
322             IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
323         pmap_map_chunk(l1pagetable, md_addr.pv_va, md_addr.pv_pa,
324             MD_ROOT_SIZE * 1024, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
325         pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
326             ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
327         pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
328             UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
329         pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
330             KSTACK_PAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
331
332         pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
333             L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
334
335         for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
336                 pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
337                     kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
338                     VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
339         }
340         pmap_map_chunk(l1pagetable, md_bla.pv_va, md_bla.pv_pa, L2_TABLE_SIZE,
341             VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
342         /* Map the vector page. */
343         pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
344             VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
345         /* Map the statically mapped devices. */
346         pmap_devmap_bootstrap(l1pagetable, assabet_devmap);
347         pmap_map_chunk(l1pagetable, sa1_cache_clean_addr, 0xf0000000, 
348             CPU_SA110_CACHE_CLEAN_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
349
350         data_abort_handler_address = (u_int)data_abort_handler;
351         prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
352         undefined_handler_address = (u_int)undefinedinstruction_bounce;
353         undefined_init();
354         cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
355         setttb(kernel_l1pt.pv_pa);
356         cpu_tlb_flushID();
357         cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
358
359         /*
360          * Pages were allocated during the secondary bootstrap for the
361          * stacks for different CPU modes.
362          * We must now set the r13 registers in the different CPU modes to
363          * point to these stacks.
364          * Since the ARM stacks use STMFD etc. we must set r13 to the top end
365          * of the stack memory.
366          */
367         set_stackptr(PSR_IRQ32_MODE,
368             irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
369         set_stackptr(PSR_ABT32_MODE,
370             abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
371         set_stackptr(PSR_UND32_MODE,
372             undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
373
374         /*
375          * We must now clean the cache again....
376          * Cleaning may be done by reading new data to displace any
377          * dirty data in the cache. This will have happened in setttb()
378          * but since we are boot strapping the addresses used for the read
379          * may have just been remapped and thus the cache could be out
380          * of sync. A re-clean after the switch will cure this.
381          * After booting there are no gross relocations of the kernel thus
382          * this problem will not occur after initarm().
383          */
384         cpu_idcache_wbinv_all();
385
386         bootverbose = 1;
387
388         /* Set stack for exception handlers */
389         
390         proc_linkup0(&proc0, &thread0);
391         thread0.td_kstack = kernelstack.pv_va;
392         thread0.td_pcb = (struct pcb *)
393                 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
394         thread0.td_pcb->pcb_flags = 0;
395         thread0.td_frame = &proc0_tf;
396         
397         
398         /* Enable MMU, I-cache, D-cache, write buffer. */
399
400         cpufunc_control(0x337f, 0x107d);
401         arm_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
402
403         pmap_curmaxkvaddr = freemempos + KERNEL_PT_VMDATA_NUM * 0x400000;
404
405         dump_avail[0] = phys_avail[0] = round_page(virtual_avail);
406         dump_avail[1] = phys_avail[1] = 0xc0000000 + 0x02000000 - 1;
407         dump_avail[2] = phys_avail[2] = 0;
408         dump_avail[3] = phys_avail[3] = 0;
409                                         
410         mutex_init();
411         pmap_bootstrap(freemempos, 0xd0000000, &kernel_l1pt);
412
413         init_param2(physmem);
414         kdb_init();
415         return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
416             sizeof(struct pcb)));
417 }