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 #include "opt_kstack_pages.h"
43 #define _ARM32_BUS_DMA_PRIVATE
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysproto.h>
47 #include <sys/signalvar.h>
48 #include <sys/imgact.h>
49 #include <sys/kernel.h>
51 #include <sys/linker.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
57 #include <sys/ptrace.h>
64 #include <sys/msgbuf.h>
65 #include <sys/devmap.h>
66 #include <machine/physmem.h>
67 #include <machine/reg.h>
68 #include <machine/cpu.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_map.h>
75 #include <machine/vmparam.h>
76 #include <machine/pcb.h>
77 #include <machine/undefined.h>
78 #include <machine/machdep.h>
79 #include <machine/metadata.h>
80 #include <machine/armreg.h>
81 #include <machine/bus.h>
82 #include <sys/reboot.h>
83 #include "econa_reg.h"
85 /* Page table for mapping proc0 zero page */
86 #define KERNEL_PT_SYS 0
87 #define KERNEL_PT_KERN 1
88 #define KERNEL_PT_KERN_NUM 22
89 /* L2 table for mapping after kernel */
90 #define KERNEL_PT_AFKERNEL KERNEL_PT_KERN + KERNEL_PT_KERN_NUM
91 #define KERNEL_PT_AFKERNEL_NUM 5
93 /* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
94 #define NUM_KERNEL_PTS (KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
96 struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
98 /* Physical and virtual addresses for some global pages */
100 struct pv_addr systempage;
101 struct pv_addr msgbufpv;
102 struct pv_addr irqstack;
103 struct pv_addr undstack;
104 struct pv_addr abtstack;
105 struct pv_addr kernelstack;
107 /* Static device mappings. */
108 static const struct devmap_entry econa_devmap[] = {
111 * This maps DDR SDRAM
113 ECONA_SDRAM_BASE, /*virtual*/
114 ECONA_SDRAM_BASE, /*physical*/
115 ECONA_SDRAM_SIZE, /*size*/
118 * Map the on-board devices VA == PA so that we can access them
119 * with the MMU on or off.
123 * This maps the interrupt controller, the UART
126 ECONA_IO_BASE, /*virtual*/
127 ECONA_IO_BASE, /*physical*/
128 ECONA_IO_SIZE, /*size*/
134 ECONA_OHCI_VBASE, /*virtual*/
135 ECONA_OHCI_PBASE, /*physical*/
136 ECONA_USB_SIZE, /*size*/
142 ECONA_CFI_VBASE, /*virtual*/
143 ECONA_CFI_PBASE, /*physical*/
155 initarm(struct arm_boot_params *abp)
157 struct pv_addr kernel_l1pt;
158 volatile uint32_t * ddr = (uint32_t *)0x4000000C;
161 vm_offset_t afterkern;
162 vm_offset_t freemempos;
163 vm_offset_t lastaddr;
167 boothowto = RB_VERBOSE;
168 lastaddr = parse_boot_param(abp);
169 arm_physmem_kernaddr = abp->abp_physaddr;
173 /* Do basic tuning, hz etc */
177 freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
178 /* Define a macro to simplify memory allocation */
179 #define valloc_pages(var, np) \
180 alloc_pages((var).pv_va, (np)); \
181 (var).pv_pa = (var).pv_va + (abp->abp_physaddr - KERNVIRTADDR);
183 #define alloc_pages(var, np) \
184 (var) = freemempos; \
185 freemempos += (np * PAGE_SIZE); \
186 memset((char *)(var), 0, ((np) * PAGE_SIZE));
188 while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
189 freemempos += PAGE_SIZE;
190 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
191 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
192 if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
193 valloc_pages(kernel_pt_table[loop],
194 L2_TABLE_SIZE / PAGE_SIZE);
196 kernel_pt_table[loop].pv_va = freemempos -
197 (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
199 kernel_pt_table[loop].pv_pa =
200 kernel_pt_table[loop].pv_va - KERNVIRTADDR +
205 * Allocate a page for the system page mapped to V0x00000000
206 * This page will just contain the system vectors and can be
207 * shared by all processes.
209 valloc_pages(systempage, 1);
211 /* Allocate stacks for all modes */
212 valloc_pages(irqstack, IRQ_STACK_SIZE);
213 valloc_pages(abtstack, ABT_STACK_SIZE);
214 valloc_pages(undstack, UND_STACK_SIZE);
215 valloc_pages(kernelstack, kstack_pages);
216 valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
219 * Now we start construction of the L1 page table
220 * We start by mapping the L2 page tables into the L1.
221 * This means that we can replace L1 mappings later on if necessary
223 l1pagetable = kernel_l1pt.pv_va;
225 /* Map the L2 pages tables in the L1 page table */
226 pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
227 &kernel_pt_table[KERNEL_PT_SYS]);
228 for (i = 0; i < KERNEL_PT_KERN_NUM; i++)
229 pmap_link_l2pt(l1pagetable, KERNBASE + i * L1_S_SIZE,
230 &kernel_pt_table[KERNEL_PT_KERN + i]);
231 pmap_map_chunk(l1pagetable, KERNBASE, PHYSADDR,
232 rounddown2(((uint32_t)lastaddr - KERNBASE) + PAGE_SIZE, PAGE_SIZE),
233 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
234 afterkern = round_page(rounddown2(lastaddr + L1_S_SIZE, L1_S_SIZE));
235 for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
236 pmap_link_l2pt(l1pagetable, afterkern + i * L1_S_SIZE,
237 &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
240 /* Map the vector page. */
241 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
242 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
245 /* Map the stack pages */
246 pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
247 IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
248 pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
249 ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
250 pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
251 UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
252 pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
253 kstack_pages * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
255 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
256 L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
257 pmap_map_chunk(l1pagetable, msgbufpv.pv_va, msgbufpv.pv_pa,
258 msgbufsize, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
260 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
261 pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
262 kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
263 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
266 devmap_bootstrap(l1pagetable, econa_devmap);
267 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
268 cpu_setttb(kernel_l1pt.pv_pa);
270 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
272 mem_info = ((*ddr) >> 4) & 0x3;
273 memsize = (8<<mem_info)*1024*1024;
275 /* Enable MMU in system control register (SCTLR). */
276 cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
279 * Pages were allocated during the secondary bootstrap for the
280 * stacks for different CPU modes.
281 * We must now set the r13 registers in the different CPU modes to
282 * point to these stacks.
283 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
284 * of the stack memory.
289 * We must now clean the cache again....
290 * Cleaning may be done by reading new data to displace any
291 * dirty data in the cache. This will have happened in cpu_setttb()
292 * but since we are boot strapping the addresses used for the read
293 * may have just been remapped and thus the cache could be out
294 * of sync. A re-clean after the switch will cure this.
295 * After booting there are no gross relocations of the kernel thus
296 * this problem will not occur after initarm().
298 cpu_idcache_wbinv_all();
303 init_proc0(kernelstack.pv_va);
305 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
307 pmap_curmaxkvaddr = afterkern + L1_S_SIZE * (KERNEL_PT_KERN_NUM - 1);
308 vm_max_kernel_address = KERNVIRTADDR + 3 * memsize;
309 pmap_bootstrap(freemempos, &kernel_l1pt);
311 msgbufp = (void*)msgbufpv.pv_va;
312 msgbufinit(msgbufp, msgbufsize);
317 * Add the physical ram we have available.
319 * Exclude the kernel, and all the things we allocated which immediately
320 * follow the kernel, from the VM allocation pool but not from crash
321 * dumps. virtual_avail is a global variable which tracks the kva we've
322 * "allocated" while setting up pmaps.
324 * Prepare the list of physical memory available to the vm subsystem.
326 arm_physmem_hardware_region(PHYSADDR, memsize);
327 arm_physmem_exclude_region(abp->abp_physaddr,
328 virtual_avail - KERNVIRTADDR, EXFLAG_NOALLOC);
329 arm_physmem_init_kernel_globals();
331 init_param2(physmem);
334 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
335 sizeof(struct pcb)));