1 /* $NetBSD: hpc_machdep.c,v 1.70 2003/09/16 08:18:22 agc Exp $ */
4 * Copyright (c) 1994-1998 Mark Brinicombe.
5 * Copyright (c) 1994 Brini.
8 * This code is derived from software written for Brini by Mark Brinicombe
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
37 * RiscBSD kernel project
41 * Machine dependant functions for kernel setup
43 * This file needs a lot of work.
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
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>
62 #include <sys/linker.h>
64 #include <sys/malloc.h>
65 #include <sys/mutex.h>
68 #include <sys/ptrace.h>
75 #include <machine/reg.h>
76 #include <machine/cpu.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>
96 #define MDROOT_ADDR 0xd0400000
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 4 /* start with 16MB of KVM */
106 #define NUM_KERNEL_PTS (KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
108 /* Define various stack sizes in pages */
109 #define IRQ_STACK_SIZE 1
110 #define ABT_STACK_SIZE 1
112 #define UND_STACK_SIZE 2
114 #define UND_STACK_SIZE 1
116 #define KERNEL_VM_BASE (KERNBASE + 0x00c00000)
117 #define KERNEL_VM_SIZE 0x05000000
119 extern u_int data_abort_handler_address;
120 extern u_int prefetch_abort_handler_address;
121 extern u_int undefined_handler_address;
123 struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
129 extern vm_offset_t sa1_cache_clean_addr;
134 struct pcpu *pcpup = &__pcpu;
137 #define MD_ROOT_SIZE 65535
139 /* Physical and virtual addresses for some global pages */
141 vm_paddr_t phys_avail[10];
142 vm_paddr_t dump_avail[4];
143 vm_paddr_t physical_start;
144 vm_paddr_t physical_end;
145 vm_paddr_t physical_freestart;
146 vm_offset_t physical_pages;
147 vm_offset_t clean_sva, clean_eva;
149 struct pv_addr systempage;
150 struct pv_addr irqstack;
151 struct pv_addr undstack;
152 struct pv_addr abtstack;
153 struct pv_addr kernelstack;
154 void enable_mmu(vm_offset_t);
155 static struct trapframe proc0_tf;
157 struct arm32_dma_range *
158 bus_dma_get_range(void)
165 bus_dma_get_range_nb(void)
177 #define CPU_SA110_CACHE_CLEAN_SIZE (0x4000 * 2)
180 initarm(void *arg, void *arg2)
183 struct pv_addr kernel_l1pt;
184 struct pv_addr md_addr;
185 struct pv_addr md_bla;
187 u_int kerneldatasize, symbolsize;
189 vm_offset_t freemempos;
190 vm_offset_t lastalloced;
193 uint32_t fake_preload[35];
195 boothowto = RB_VERBOSE | RB_SINGLE;
198 fake_preload[i++] = MODINFO_NAME;
199 fake_preload[i++] = strlen("elf kernel") + 1;
200 strcpy((char*)&fake_preload[i++], "elf kernel");
202 fake_preload[i++] = MODINFO_TYPE;
203 fake_preload[i++] = strlen("elf kernel") + 1;
204 strcpy((char*)&fake_preload[i++], "elf kernel");
206 fake_preload[i++] = MODINFO_ADDR;
207 fake_preload[i++] = sizeof(vm_offset_t);
208 fake_preload[i++] = KERNBASE;
209 fake_preload[i++] = MODINFO_SIZE;
210 fake_preload[i++] = sizeof(uint32_t);
211 fake_preload[i++] = (uint32_t)&end - KERNBASE;
212 fake_preload[i++] = MODINFO_NAME;
213 fake_preload[i++] = strlen("md root") + 1;
214 strcpy((char*)&fake_preload[i++], "md root");
216 fake_preload[i++] = MODINFO_TYPE;
217 fake_preload[i++] = strlen("md_image") + 1;
218 strcpy((char*)&fake_preload[i++], "md_image");
220 fake_preload[i++] = MODINFO_ADDR;
221 fake_preload[i++] = sizeof(uint32_t);
222 fake_preload[i++] = MDROOT_ADDR;
223 fake_preload[i++] = MODINFO_SIZE;
224 fake_preload[i++] = sizeof(uint32_t);
225 fake_preload[i++] = MD_ROOT_SIZE * 1024;
226 fake_preload[i++] = 0;
228 preload_metadata = (void *)fake_preload;
230 physmem =( 16 * 1024 * 1024) / PAGE_SIZE;
232 pcpu_init(pc, 0, sizeof(struct pcpu));
233 PCPU_SET(curthread, &thread0);
235 physical_start = (vm_offset_t) KERNBASE;
236 physical_end = (vm_offset_t) &end;
237 physical_freestart = (((vm_offset_t)physical_end) + PAGE_MASK) & ~PAGE_MASK;
238 md_addr.pv_va = md_addr.pv_pa = MDROOT_ADDR;
239 #define KERNEL_TEXT_BASE (KERNBASE + 0x00040000)
240 kerneldatasize = (u_int32_t)&end - (u_int32_t)KERNEL_TEXT_BASE;
242 freemempos = (vm_offset_t)round_page(physical_freestart);
243 memset((void *)freemempos, 0, 256*1024);
244 /* Define a macro to simplify memory allocation */
245 #define valloc_pages(var, np) \
246 alloc_pages((var).pv_pa, (np)); \
247 (var).pv_va = (var).pv_pa;
249 #define alloc_pages(var, np) \
250 (var) = freemempos; \
251 freemempos += ((np) * PAGE_SIZE);\
252 memset((char *)(var), 0, ((np) * PAGE_SIZE));
254 while ((freemempos & (L1_TABLE_SIZE - 1)) != 0)
255 freemempos += PAGE_SIZE;
256 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
257 valloc_pages(md_bla, L2_TABLE_SIZE / PAGE_SIZE);
258 alloc_pages(sa1_cache_clean_addr, CPU_SA110_CACHE_CLEAN_SIZE / PAGE_SIZE);
259 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
260 valloc_pages(kernel_pt_table[loop],
261 L2_TABLE_SIZE / PAGE_SIZE);
264 valloc_pages(systempage, 1);
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.
271 pt_size = round_page(freemempos) - physical_freestart;
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 lastalloced = kernelstack.pv_va;
281 * Allocate memory for the l1 and l2 page tables. The scheme to avoid
282 * wasting memory by allocating the l1pt on the first 16k memory was
283 * taken from NetBSD rpc_machdep.c. NKPT should be greater than 12 for
284 * this to work (which is supposed to be the case).
288 * Now we start construction of the L1 page table
289 * We start by mapping the L2 page tables into the L1.
290 * This means that we can replace L1 mappings later on if necessary
292 l1pagetable = kernel_l1pt.pv_pa;
295 /* Map the L2 pages tables in the L1 page table */
296 pmap_link_l2pt(l1pagetable, 0x00000000,
297 &kernel_pt_table[KERNEL_PT_SYS]);
298 pmap_link_l2pt(l1pagetable, KERNBASE,
299 &kernel_pt_table[KERNEL_PT_KERNEL]);
300 pmap_link_l2pt(l1pagetable, 0xd0000000,
301 &kernel_pt_table[KERNEL_PT_IO]);
302 pmap_link_l2pt(l1pagetable, lastalloced & ~((L1_S_SIZE * 4) - 1),
303 &kernel_pt_table[KERNEL_PT_L1]);
304 pmap_link_l2pt(l1pagetable, 0x90000000, &kernel_pt_table[KERNEL_PT_IRQ]);
305 pmap_link_l2pt(l1pagetable, MDROOT_ADDR,
307 for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; ++loop)
308 pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
309 &kernel_pt_table[KERNEL_PT_VMDATA + loop]);
310 pmap_map_chunk(l1pagetable, KERNBASE, KERNBASE,
311 (uint32_t)&end - KERNBASE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
312 /* Map the stack pages */
313 pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
314 IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
315 pmap_map_chunk(l1pagetable, md_addr.pv_va, md_addr.pv_pa,
316 MD_ROOT_SIZE * 1024, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
317 pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
318 ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
319 pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
320 UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
321 pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
322 KSTACK_PAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
324 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
325 L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
327 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
328 pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
329 kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
330 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
332 pmap_map_chunk(l1pagetable, md_bla.pv_va, md_bla.pv_pa, L2_TABLE_SIZE,
333 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
334 /* Map the vector page. */
335 pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
336 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
338 pmap_map_entry(l1pagetable, 0xd000d000, 0x80010000,
339 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
340 pmap_map_entry(l1pagetable, 0x90050000, 0x90050000,
341 VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
342 pmap_map_chunk(l1pagetable, sa1_cache_clean_addr, 0xf0000000,
343 CPU_SA110_CACHE_CLEAN_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
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;
349 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
350 setttb(kernel_l1pt.pv_pa);
352 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
355 * Pages were allocated during the secondary bootstrap for the
356 * stacks for different CPU modes.
357 * We must now set the r13 registers in the different CPU modes to
358 * point to these stacks.
359 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
360 * of the stack memory.
362 set_stackptr(PSR_IRQ32_MODE,
363 irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
364 set_stackptr(PSR_ABT32_MODE,
365 abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
366 set_stackptr(PSR_UND32_MODE,
367 undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
372 * We must now clean the cache again....
373 * Cleaning may be done by reading new data to displace any
374 * dirty data in the cache. This will have happened in setttb()
375 * but since we are boot strapping the addresses used for the read
376 * may have just been remapped and thus the cache could be out
377 * of sync. A re-clean after the switch will cure this.
378 * After booting there are no gross reloations of the kernel thus
379 * this problem will not occur after initarm().
381 /* cpu_idcache_wbinv_all();*/
386 /* Set stack for exception handlers */
388 proc_linkup(&proc0, &ksegrp0, &thread0);
389 thread0.td_kstack = kernelstack.pv_va;
390 thread0.td_pcb = (struct pcb *)
391 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
392 thread0.td_pcb->pcb_flags = 0;
393 thread0.td_frame = &proc0_tf;
396 /* Enable MMU, I-cache, D-cache, write buffer. */
398 cpufunc_control(0x337f, 0x107d);
400 arm_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
402 pmap_curmaxkvaddr = freemempos + KERNEL_PT_VMDATA_NUM * 0x400000;
404 pmap_bootstrap(freemempos,
405 0xd0000000, &kernel_l1pt);
411 dump_avail[0] = phys_avail[0] = round_page(virtual_avail);
412 dump_avail[1] = phys_avail[1] = 0xc0000000 + 0x02000000 - 1;
413 dump_avail[2] = phys_avail[2] = 0;
414 dump_avail[3] = phys_avail[3] = 0;
416 /* Do basic tuning, hz etc */
418 init_param2(physmem);
420 avail_end = 0xc0000000 + 0x02000000 - 1;
421 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
422 sizeof(struct pcb)));