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