2 * Copyright (c) 2009 Yohanes Nugroho <yohanes@gmail.com>
3 * Copyright (c) 1994-1998 Mark Brinicombe.
4 * Copyright (c) 1994 Brini.
7 * This code is derived from software written for Brini by Mark Brinicombe
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
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>
49 #include <sys/linker.h>
51 #include <sys/malloc.h>
52 #include <sys/mutex.h>
55 #include <sys/ptrace.h>
62 #include <sys/msgbuf.h>
63 #include <machine/reg.h>
64 #include <machine/cpu.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_map.h>
71 #include <machine/vmparam.h>
72 #include <machine/pcb.h>
73 #include <machine/undefined.h>
74 #include <machine/machdep.h>
75 #include <machine/metadata.h>
76 #include <machine/armreg.h>
77 #include <machine/bus.h>
78 #include <sys/reboot.h>
79 #include "econa_reg.h"
81 /* Page table for mapping proc0 zero page */
82 #define KERNEL_PT_SYS 0
83 #define KERNEL_PT_KERN 1
84 #define KERNEL_PT_KERN_NUM 22
85 /* L2 table for mapping after kernel */
86 #define KERNEL_PT_AFKERNEL KERNEL_PT_KERN + KERNEL_PT_KERN_NUM
87 #define KERNEL_PT_AFKERNEL_NUM 5
89 /* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
90 #define NUM_KERNEL_PTS (KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
92 extern u_int data_abort_handler_address;
93 extern u_int prefetch_abort_handler_address;
94 extern u_int undefined_handler_address;
96 struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
98 /* Physical and virtual addresses for some global pages */
100 vm_paddr_t phys_avail[10];
101 vm_paddr_t dump_avail[4];
103 struct pv_addr systempage;
104 struct pv_addr msgbufpv;
105 struct pv_addr irqstack;
106 struct pv_addr undstack;
107 struct pv_addr abtstack;
108 struct pv_addr kernelstack;
110 /* Static device mappings. */
111 static const struct pmap_devmap econa_devmap[] = {
114 * This maps DDR SDRAM
116 ECONA_SDRAM_BASE, /*virtual*/
117 ECONA_SDRAM_BASE, /*physical*/
118 ECONA_SDRAM_SIZE, /*size*/
119 VM_PROT_READ|VM_PROT_WRITE,
123 * Map the on-board devices VA == PA so that we can access them
124 * with the MMU on or off.
128 * This maps the interrupt controller, the UART
131 ECONA_IO_BASE, /*virtual*/
132 ECONA_IO_BASE, /*physical*/
133 ECONA_IO_SIZE, /*size*/
134 VM_PROT_READ|VM_PROT_WRITE,
141 ECONA_OHCI_VBASE, /*virtual*/
142 ECONA_OHCI_PBASE, /*physical*/
143 ECONA_USB_SIZE, /*size*/
144 VM_PROT_READ|VM_PROT_WRITE,
151 ECONA_CFI_VBASE, /*virtual*/
152 ECONA_CFI_PBASE, /*physical*/
154 VM_PROT_READ|VM_PROT_WRITE,
168 initarm(struct arm_boot_params *abp)
170 struct pv_addr kernel_l1pt;
171 volatile uint32_t * ddr = (uint32_t *)0x4000000C;
174 vm_offset_t afterkern;
175 vm_offset_t freemempos;
176 vm_offset_t lastaddr;
180 boothowto = RB_VERBOSE;
181 lastaddr = parse_boot_param(abp);
185 /* Do basic tuning, hz etc */
189 freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
190 /* Define a macro to simplify memory allocation */
191 #define valloc_pages(var, np) \
192 alloc_pages((var).pv_va, (np)); \
193 (var).pv_pa = (var).pv_va + (KERNPHYSADDR - KERNVIRTADDR);
195 #define alloc_pages(var, np) \
196 (var) = freemempos; \
197 freemempos += (np * PAGE_SIZE); \
198 memset((char *)(var), 0, ((np) * PAGE_SIZE));
200 while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
201 freemempos += PAGE_SIZE;
202 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
203 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
204 if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
205 valloc_pages(kernel_pt_table[loop],
206 L2_TABLE_SIZE / PAGE_SIZE);
208 kernel_pt_table[loop].pv_va = freemempos -
209 (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
211 kernel_pt_table[loop].pv_pa =
212 kernel_pt_table[loop].pv_va - KERNVIRTADDR +
217 * Allocate a page for the system page mapped to V0x00000000
218 * This page will just contain the system vectors and can be
219 * shared by all processes.
221 valloc_pages(systempage, 1);
223 /* Allocate stacks for all modes */
224 valloc_pages(irqstack, IRQ_STACK_SIZE);
225 valloc_pages(abtstack, ABT_STACK_SIZE);
226 valloc_pages(undstack, UND_STACK_SIZE);
227 valloc_pages(kernelstack, KSTACK_PAGES);
228 valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
231 * Now we start construction of the L1 page table
232 * We start by mapping the L2 page tables into the L1.
233 * This means that we can replace L1 mappings later on if necessary
235 l1pagetable = kernel_l1pt.pv_va;
237 /* Map the L2 pages tables in the L1 page table */
238 pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
239 &kernel_pt_table[KERNEL_PT_SYS]);
240 for (i = 0; i < KERNEL_PT_KERN_NUM; i++)
241 pmap_link_l2pt(l1pagetable, KERNBASE + i * L1_S_SIZE,
242 &kernel_pt_table[KERNEL_PT_KERN + i]);
243 pmap_map_chunk(l1pagetable, KERNBASE, PHYSADDR,
244 (((uint32_t)lastaddr - KERNBASE) + PAGE_SIZE) & ~(PAGE_SIZE - 1),
245 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
246 afterkern = round_page((lastaddr + L1_S_SIZE) & ~(L1_S_SIZE - 1));
247 for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
248 pmap_link_l2pt(l1pagetable, afterkern + i * L1_S_SIZE,
249 &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
252 /* Map the vector page. */
253 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
254 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
257 /* Map the stack pages */
258 pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
259 IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
260 pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
261 ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
262 pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
263 UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
264 pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
265 KSTACK_PAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
267 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
268 L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
269 pmap_map_chunk(l1pagetable, msgbufpv.pv_va, msgbufpv.pv_pa,
270 msgbufsize, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
272 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
273 pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
274 kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
275 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
278 pmap_devmap_bootstrap(l1pagetable, econa_devmap);
279 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
280 setttb(kernel_l1pt.pv_pa);
282 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
284 mem_info = ((*ddr) >> 4) & 0x3;
285 memsize = (8<<mem_info)*1024*1024;
286 physmem = memsize / PAGE_SIZE;
289 * Pages were allocated during the secondary bootstrap for the
290 * stacks for different CPU modes.
291 * We must now set the r13 registers in the different CPU modes to
292 * point to these stacks.
293 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
294 * of the stack memory.
296 cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
301 * We must now clean the cache again....
302 * Cleaning may be done by reading new data to displace any
303 * dirty data in the cache. This will have happened in setttb()
304 * but since we are boot strapping the addresses used for the read
305 * may have just been remapped and thus the cache could be out
306 * of sync. A re-clean after the switch will cure this.
307 * After booting there are no gross relocations of the kernel thus
308 * this problem will not occur after initarm().
310 cpu_idcache_wbinv_all();
312 /* Set stack for exception handlers */
313 data_abort_handler_address = (u_int)data_abort_handler;
314 prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
315 undefined_handler_address = (u_int)undefinedinstruction_bounce;
318 init_proc0(kernelstack.pv_va);
320 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
322 pmap_curmaxkvaddr = afterkern + L1_S_SIZE * (KERNEL_PT_KERN_NUM - 1);
323 arm_dump_avail_init(memsize, sizeof(dump_avail) / sizeof(dump_avail[0]));
324 vm_max_kernel_address = KERNVIRTADDR + 3 * memsize;
325 pmap_bootstrap(freemempos, &kernel_l1pt);
327 msgbufp = (void*)msgbufpv.pv_va;
328 msgbufinit(msgbufp, msgbufsize);
333 #if PHYSADDR != KERNPHYSADDR
334 phys_avail[i++] = PHYSADDR;
335 phys_avail[i++] = KERNPHYSADDR;
337 phys_avail[i++] = virtual_avail - KERNVIRTADDR + KERNPHYSADDR;
339 phys_avail[i++] = PHYSADDR + memsize;
342 init_param2(physmem);
345 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
346 sizeof(struct pcb)));