1/*- 2 * Copyright (c) 2009 Yohanes Nugroho <yohanes@gmail.com> 3 * Copyright (c) 1994-1998 Mark Brinicombe. 4 * Copyright (c) 1994 Brini. 5 * All rights reserved. 6 * 7 * This code is derived from software written for Brini by Mark Brinicombe 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 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. 23 * 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 34 * SUCH DAMAGE. 35 * 36 */ 37 38#include <sys/cdefs.h> 39__FBSDID("$FreeBSD$"); 40 41#include "opt_kstack_pages.h" 42 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> 50#include <sys/ktr.h> 51#include <sys/linker.h> 52#include <sys/lock.h> 53#include <sys/malloc.h> 54#include <sys/mutex.h> 55#include <sys/pcpu.h> 56#include <sys/proc.h> 57#include <sys/ptrace.h> 58#include <sys/cons.h> 59#include <sys/bio.h> 60#include <sys/bus.h> 61#include <sys/buf.h> 62#include <sys/exec.h> 63#include <sys/kdb.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> 69 70#include <vm/vm.h> 71#include <vm/pmap.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" 84 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 92 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) 95 96struct pv_addr kernel_pt_table[NUM_KERNEL_PTS]; 97 98/* Physical and virtual addresses for some global pages */ 99 100struct pv_addr systempage; 101struct pv_addr msgbufpv; 102struct pv_addr irqstack; 103struct pv_addr undstack; 104struct pv_addr abtstack; 105struct pv_addr kernelstack; 106 107/* Static device mappings. */ 108static const struct devmap_entry econa_devmap[] = { 109 { 110 /* 111 * This maps DDR SDRAM 112 */ 113 ECONA_SDRAM_BASE, /*virtual*/ 114 ECONA_SDRAM_BASE, /*physical*/ 115 ECONA_SDRAM_SIZE, /*size*/ 116 }, 117 /* 118 * Map the on-board devices VA == PA so that we can access them 119 * with the MMU on or off. 120 */ 121 { 122 /* 123 * This maps the interrupt controller, the UART 124 * and the timer. 125 */ 126 ECONA_IO_BASE, /*virtual*/ 127 ECONA_IO_BASE, /*physical*/ 128 ECONA_IO_SIZE, /*size*/ 129 }, 130 { 131 /* 132 * OHCI + EHCI 133 */ 134 ECONA_OHCI_VBASE, /*virtual*/ 135 ECONA_OHCI_PBASE, /*physical*/ 136 ECONA_USB_SIZE, /*size*/ 137 }, 138 { 139 /* 140 * CFI 141 */ 142 ECONA_CFI_VBASE, /*virtual*/ 143 ECONA_CFI_PBASE, /*physical*/ 144 ECONA_CFI_SIZE, 145 }, 146 { 147 0, 148 0, 149 0, 150 } 151}; 152 153 154void * 155initarm(struct arm_boot_params *abp) 156{ 157 struct pv_addr kernel_l1pt; 158 volatile uint32_t * ddr = (uint32_t *)0x4000000C; 159 int loop, i; 160 u_int l1pagetable; 161 vm_offset_t afterkern; 162 vm_offset_t freemempos; 163 vm_offset_t lastaddr; 164 uint32_t memsize; 165 int mem_info; 166 167 boothowto = RB_VERBOSE; 168 lastaddr = parse_boot_param(abp); 169 arm_physmem_kernaddr = abp->abp_physaddr; 170 set_cpufuncs(); 171 pcpu0_init(); 172 173 /* Do basic tuning, hz etc */ 174 init_param1(); 175 176 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); 182 183#define alloc_pages(var, np) \ 184 (var) = freemempos; \ 185 freemempos += (np * PAGE_SIZE); \ 186 memset((char *)(var), 0, ((np) * PAGE_SIZE)); 187 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); 195 } else { 196 kernel_pt_table[loop].pv_va = freemempos - 197 (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) * 198 L2_TABLE_SIZE_REAL; 199 kernel_pt_table[loop].pv_pa = 200 kernel_pt_table[loop].pv_va - KERNVIRTADDR + 201 abp->abp_physaddr; 202 } 203 } 204 /* 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. 208 */ 209 valloc_pages(systempage, 1); 210 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); 217 218 /* 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 222 */ 223 l1pagetable = kernel_l1pt.pv_va; 224 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]); 238 } 239 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); 243 244 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); 254 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); 259 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); 264 } 265 266 devmap_bootstrap(l1pagetable, econa_devmap); 267 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT); 268 cpu_setttb(kernel_l1pt.pv_pa); 269 cpu_tlb_flushID(); 270 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)); 271 cninit(); 272 mem_info = ((*ddr) >> 4) & 0x3; 273 memsize = (8<<mem_info)*1024*1024; 274 275 /* Enable MMU in system control register (SCTLR). */ 276 cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE); 277 278 /* 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. 285 */ 286 set_stackptrs(0); 287 288 /* 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(). 297 */ 298 cpu_idcache_wbinv_all(); 299 cpu_setup(); 300 301 undefined_init(); 302 303 init_proc0(kernelstack.pv_va); 304 305 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL); 306 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); 310 311 msgbufp = (void*)msgbufpv.pv_va; 312 msgbufinit(msgbufp, msgbufsize); 313 314 mutex_init(); 315 316 /* 317 * Add the physical ram we have available. 318 * 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. 323 * 324 * Prepare the list of physical memory available to the vm subsystem. 325 */ 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(); 330 331 init_param2(physmem); 332 kdb_init(); 333 334 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP - 335 sizeof(struct pcb))); 336} 337