crb_machdep.c revision 292525
1171626Scognet/* $NetBSD: hpc_machdep.c,v 1.70 2003/09/16 08:18:22 agc Exp $ */ 2171626Scognet 3171626Scognet/*- 4171626Scognet * Copyright (c) 1994-1998 Mark Brinicombe. 5171626Scognet * Copyright (c) 1994 Brini. 6171626Scognet * All rights reserved. 7171626Scognet * 8171626Scognet * This code is derived from software written for Brini by Mark Brinicombe 9171626Scognet * 10171626Scognet * Redistribution and use in source and binary forms, with or without 11171626Scognet * modification, are permitted provided that the following conditions 12171626Scognet * are met: 13171626Scognet * 1. Redistributions of source code must retain the above copyright 14171626Scognet * notice, this list of conditions and the following disclaimer. 15171626Scognet * 2. Redistributions in binary form must reproduce the above copyright 16171626Scognet * notice, this list of conditions and the following disclaimer in the 17171626Scognet * documentation and/or other materials provided with the distribution. 18171626Scognet * 3. All advertising materials mentioning features or use of this software 19171626Scognet * must display the following acknowledgement: 20171626Scognet * This product includes software developed by Brini. 21171626Scognet * 4. The name of the company nor the name of the author may be used to 22171626Scognet * endorse or promote products derived from this software without specific 23171626Scognet * prior written permission. 24171626Scognet * 25171626Scognet * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED 26171626Scognet * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 27171626Scognet * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28171626Scognet * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 29171626Scognet * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 30171626Scognet * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 31171626Scognet * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32171626Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33171626Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34171626Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35171626Scognet * SUCH DAMAGE. 36171626Scognet * 37171626Scognet * RiscBSD kernel project 38171626Scognet * 39171626Scognet * machdep.c 40171626Scognet * 41171626Scognet * Machine dependant functions for kernel setup 42171626Scognet * 43236987Simp * This file needs a lot of work. 44171626Scognet * 45171626Scognet * Created : 17/09/94 46171626Scognet */ 47171626Scognet 48171626Scognet#include <sys/cdefs.h> 49171626Scognet__FBSDID("$FreeBSD: head/sys/arm/xscale/i8134x/crb_machdep.c 292525 2015-12-21 01:14:54Z ian $"); 50171626Scognet 51285627Szbb#include "opt_kstack_pages.h" 52285627Szbb 53171626Scognet#define _ARM32_BUS_DMA_PRIVATE 54171626Scognet#include <sys/param.h> 55171626Scognet#include <sys/systm.h> 56171626Scognet#include <sys/sysproto.h> 57171626Scognet#include <sys/signalvar.h> 58171626Scognet#include <sys/imgact.h> 59171626Scognet#include <sys/kernel.h> 60171626Scognet#include <sys/ktr.h> 61171626Scognet#include <sys/linker.h> 62171626Scognet#include <sys/lock.h> 63171626Scognet#include <sys/malloc.h> 64171626Scognet#include <sys/mutex.h> 65171626Scognet#include <sys/pcpu.h> 66171626Scognet#include <sys/proc.h> 67171626Scognet#include <sys/ptrace.h> 68171626Scognet#include <sys/cons.h> 69171626Scognet#include <sys/bio.h> 70171626Scognet#include <sys/bus.h> 71171626Scognet#include <sys/buf.h> 72171626Scognet#include <sys/exec.h> 73171626Scognet#include <sys/kdb.h> 74171626Scognet#include <sys/msgbuf.h> 75171626Scognet#include <machine/reg.h> 76171626Scognet#include <machine/cpu.h> 77171626Scognet 78171626Scognet#include <vm/vm.h> 79171626Scognet#include <vm/pmap.h> 80171626Scognet#include <vm/vm_object.h> 81171626Scognet#include <vm/vm_page.h> 82171626Scognet#include <vm/vm_map.h> 83257660Sian#include <machine/devmap.h> 84171626Scognet#include <machine/vmparam.h> 85171626Scognet#include <machine/pcb.h> 86171626Scognet#include <machine/undefined.h> 87171626Scognet#include <machine/machdep.h> 88171626Scognet#include <machine/metadata.h> 89171626Scognet#include <machine/armreg.h> 90171626Scognet#include <machine/bus.h> 91261649Sian#include <machine/physmem.h> 92171626Scognet#include <sys/reboot.h> 93171626Scognet 94171626Scognet 95171626Scognet#include <arm/xscale/i80321/i80321var.h> /* For i80321_calibrate_delay() */ 96171626Scognet 97171626Scognet#include <arm/xscale/i8134x/i81342reg.h> 98171626Scognet#include <arm/xscale/i8134x/i81342var.h> 99171626Scognet#include <arm/xscale/i8134x/obiovar.h> 100171626Scognet 101171626Scognet 102171626Scognet#define KERNEL_PT_SYS 0 /* Page table for mapping proc0 zero page */ 103171626Scognet#define KERNEL_PT_IOPXS 1 104171626Scognet#define KERNEL_PT_BEFOREKERN 2 105171626Scognet#define KERNEL_PT_AFKERNEL 3 /* L2 table for mapping after kernel */ 106171626Scognet#define KERNEL_PT_AFKERNEL_NUM 9 107171626Scognet 108171626Scognet/* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */ 109171626Scognet#define NUM_KERNEL_PTS (KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM) 110171626Scognet 111171626Scognetstruct pv_addr kernel_pt_table[NUM_KERNEL_PTS]; 112171626Scognet 113171626Scognet/* Physical and virtual addresses for some global pages */ 114171626Scognet 115171626Scognetstruct pv_addr systempage; 116171626Scognetstruct pv_addr msgbufpv; 117171626Scognetstruct pv_addr irqstack; 118171626Scognetstruct pv_addr undstack; 119171626Scognetstruct pv_addr abtstack; 120171626Scognetstruct pv_addr kernelstack; 121171626Scognet 122171626Scognet/* Static device mappings. */ 123257660Sianstatic const struct arm_devmap_entry iq81342_devmap[] = { 124171626Scognet { 125171626Scognet IOP34X_VADDR, 126171626Scognet IOP34X_HWADDR, 127171626Scognet IOP34X_SIZE, 128171626Scognet VM_PROT_READ|VM_PROT_WRITE, 129265852Sian PTE_DEVICE, 130171626Scognet }, 131171626Scognet { 132171626Scognet /* 133171626Scognet * Cheat and map a whole section, this will bring 134171626Scognet * both PCI-X and PCI-E outbound I/O 135171626Scognet */ 136171626Scognet IOP34X_PCIX_OIOBAR_VADDR &~ (0x100000 - 1), 137171626Scognet IOP34X_PCIX_OIOBAR &~ (0x100000 - 1), 138171626Scognet 0x100000, 139171626Scognet VM_PROT_READ|VM_PROT_WRITE, 140265852Sian PTE_DEVICE, 141171626Scognet }, 142172297Scognet { 143172297Scognet IOP34X_PCE1_VADDR, 144172297Scognet IOP34X_PCE1, 145172297Scognet IOP34X_PCE1_SIZE, 146172297Scognet VM_PROT_READ|VM_PROT_WRITE, 147265852Sian PTE_DEVICE, 148172297Scognet }, 149236987Simp { 150171626Scognet 0, 151171626Scognet 0, 152171626Scognet 0, 153171626Scognet 0, 154171626Scognet 0, 155171626Scognet } 156171626Scognet}; 157171626Scognet 158171626Scognet#define SDRAM_START 0x00000000 159171626Scognet 160171626Scognetextern vm_offset_t xscale_cache_clean_addr; 161171626Scognet 162171626Scognetvoid * 163236524Simpinitarm(struct arm_boot_params *abp) 164171626Scognet{ 165171626Scognet struct pv_addr kernel_l1pt; 166194784Sjeff struct pv_addr dpcpu; 167177883Simp int loop, i; 168171626Scognet u_int l1pagetable; 169171626Scognet vm_offset_t freemempos; 170171626Scognet vm_offset_t freemem_pt; 171171626Scognet vm_offset_t afterkern; 172171626Scognet vm_offset_t freemem_after; 173171626Scognet vm_offset_t lastaddr; 174171626Scognet uint32_t memsize, memstart; 175171626Scognet 176237040Simp lastaddr = parse_boot_param(abp); 177261649Sian arm_physmem_kernaddr = abp->abp_physaddr; 178171626Scognet set_cpufuncs(); 179171626Scognet pcpu_init(pcpup, 0, sizeof(struct pcpu)); 180171626Scognet PCPU_SET(curthread, &thread0); 181171626Scognet 182220836Spluknet /* Do basic tuning, hz etc */ 183220836Spluknet init_param1(); 184220836Spluknet 185171626Scognet freemempos = 0x00200000; 186171626Scognet /* Define a macro to simplify memory allocation */ 187171626Scognet#define valloc_pages(var, np) \ 188171626Scognet alloc_pages((var).pv_pa, (np)); \ 189171626Scognet (var).pv_va = (var).pv_pa + 0xc0000000; 190171626Scognet 191171626Scognet#define alloc_pages(var, np) \ 192171626Scognet freemempos -= (np * PAGE_SIZE); \ 193171626Scognet (var) = freemempos; \ 194171626Scognet memset((char *)(var), 0, ((np) * PAGE_SIZE)); 195171626Scognet 196171626Scognet while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0) 197171626Scognet freemempos -= PAGE_SIZE; 198171626Scognet valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE); 199171626Scognet for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) { 200171626Scognet if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) { 201171626Scognet valloc_pages(kernel_pt_table[loop], 202171626Scognet L2_TABLE_SIZE / PAGE_SIZE); 203171626Scognet } else { 204171626Scognet kernel_pt_table[loop].pv_pa = freemempos + 205171626Scognet (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) * 206171626Scognet L2_TABLE_SIZE_REAL; 207236987Simp kernel_pt_table[loop].pv_va = 208171626Scognet kernel_pt_table[loop].pv_pa + 0xc0000000; 209171626Scognet } 210171626Scognet } 211171626Scognet freemem_pt = freemempos; 212171626Scognet freemempos = 0x00100000; 213171626Scognet /* 214171626Scognet * Allocate a page for the system page mapped to V0x00000000 215171626Scognet * This page will just contain the system vectors and can be 216171626Scognet * shared by all processes. 217171626Scognet */ 218171626Scognet valloc_pages(systempage, 1); 219171626Scognet 220194784Sjeff /* Allocate dynamic per-cpu area. */ 221194784Sjeff valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE); 222194784Sjeff dpcpu_init((void *)dpcpu.pv_va, 0); 223194784Sjeff 224171626Scognet /* Allocate stacks for all modes */ 225171626Scognet valloc_pages(irqstack, IRQ_STACK_SIZE); 226171626Scognet valloc_pages(abtstack, ABT_STACK_SIZE); 227171626Scognet valloc_pages(undstack, UND_STACK_SIZE); 228286584Skib valloc_pages(kernelstack, kstack_pages); 229217688Spluknet valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE); 230171626Scognet /* 231171626Scognet * Now we start construction of the L1 page table 232171626Scognet * We start by mapping the L2 page tables into the L1. 233171626Scognet * This means that we can replace L1 mappings later on if necessary 234171626Scognet */ 235171626Scognet l1pagetable = kernel_l1pt.pv_va; 236171626Scognet 237171626Scognet /* Map the L2 pages tables in the L1 page table */ 238171626Scognet pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00100000 - 1), 239171626Scognet &kernel_pt_table[KERNEL_PT_SYS]); 240171626Scognet pmap_map_chunk(l1pagetable, KERNBASE, SDRAM_START, 0x100000, 241171626Scognet VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE); 242171626Scognet 243171626Scognet pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, SDRAM_START + 0x100000, 244171626Scognet 0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE); 245171626Scognet 246171626Scognet pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, SDRAM_START + 0x200000, 247171626Scognet (((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE) & ~(L1_S_SIZE - 1), 248171626Scognet VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE); 249171626Scognet freemem_after = ((int)lastaddr + PAGE_SIZE) & ~(PAGE_SIZE - 1); 250236987Simp afterkern = round_page(((vm_offset_t)lastaddr + L1_S_SIZE) & ~(L1_S_SIZE 251171626Scognet - 1)); 252171626Scognet for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) { 253171626Scognet pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000, 254171626Scognet &kernel_pt_table[KERNEL_PT_AFKERNEL + i]); 255171626Scognet } 256171626Scognet 257171626Scognet 258171626Scognet /* Map the vector page. */ 259171626Scognet pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa, 260171626Scognet VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE); 261257660Sian arm_devmap_bootstrap(l1pagetable, iq81342_devmap); 262171626Scognet /* 263171626Scognet * Give the XScale global cache clean code an appropriately 264171626Scognet * sized chunk of unmapped VA space starting at 0xff000000 265171626Scognet * (our device mappings end before this address). 266171626Scognet */ 267171626Scognet xscale_cache_clean_addr = 0xff000000U; 268171626Scognet 269171626Scognet cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT); 270171626Scognet setttb(kernel_l1pt.pv_pa); 271171626Scognet cpu_tlb_flushID(); 272171626Scognet cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)); 273171626Scognet /* 274171626Scognet * Pages were allocated during the secondary bootstrap for the 275171626Scognet * stacks for different CPU modes. 276171626Scognet * We must now set the r13 registers in the different CPU modes to 277171626Scognet * point to these stacks. 278171626Scognet * Since the ARM stacks use STMFD etc. we must set r13 to the top end 279171626Scognet * of the stack memory. 280171626Scognet */ 281171626Scognet 282240802Sandrew set_stackptrs(0); 283171626Scognet 284171626Scognet /* 285171626Scognet * We must now clean the cache again.... 286171626Scognet * Cleaning may be done by reading new data to displace any 287171626Scognet * dirty data in the cache. This will have happened in setttb() 288171626Scognet * but since we are boot strapping the addresses used for the read 289171626Scognet * may have just been remapped and thus the cache could be out 290171626Scognet * of sync. A re-clean after the switch will cure this. 291185513Sstas * After booting there are no gross relocations of the kernel thus 292171626Scognet * this problem will not occur after initarm(). 293171626Scognet */ 294171626Scognet cpu_idcache_wbinv_all(); 295280823Sandrew cpu_setup(); 296258412Sian 297171626Scognet i80321_calibrate_delay(); 298277477Sian i81342_sdram_bounds(obio_bs_tag, IOP34X_VADDR, &memstart, &memsize); 299171626Scognet physmem = memsize / PAGE_SIZE; 300171626Scognet cninit(); 301171626Scognet /* Set stack for exception handlers */ 302171626Scognet 303171626Scognet undefined_init(); 304171626Scognet 305236828Sandrew init_proc0(kernelstack.pv_va); 306171626Scognet 307171626Scognet arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL); 308171626Scognet 309171626Scognet pmap_curmaxkvaddr = afterkern + PAGE_SIZE; 310261642Sian 311256712Scognet vm_max_kernel_address = 0xe0000000; 312247046Salc pmap_bootstrap(pmap_curmaxkvaddr, &kernel_l1pt); 313171626Scognet msgbufp = (void*)msgbufpv.pv_va; 314217688Spluknet msgbufinit(msgbufp, msgbufsize); 315171626Scognet mutex_init(); 316261698Sian 317261698Sian /* 318261698Sian * Add the physical ram we have available. 319261698Sian * 320261698Sian * Exclude the kernel (and all the things we allocated which immediately 321261698Sian * follow the kernel) from the VM allocation pool but not from crash 322261698Sian * dumps. virtual_avail is a global variable which tracks the kva we've 323261698Sian * "allocated" while setting up pmaps. 324261698Sian * 325261698Sian * Prepare the list of physical memory available to the vm subsystem. 326261698Sian */ 327261698Sian arm_physmem_hardware_region(SDRAM_START, memsize); 328292525Sian arm_physmem_exclude_region(freemem_pt, abp->abp_physaddr - 329266850Scognet freemem_pt, EXFLAG_NOALLOC); 330292525Sian arm_physmem_exclude_region(freemempos, abp->abp_physaddr - 0x100000 - 331266850Scognet freemempos, EXFLAG_NOALLOC); 332261698Sian arm_physmem_exclude_region(abp->abp_physaddr, 333261698Sian virtual_avail - KERNVIRTADDR, EXFLAG_NOALLOC); 334261698Sian arm_physmem_init_kernel_globals(); 335261698Sian 336171626Scognet init_param2(physmem); 337171626Scognet kdb_init(); 338171626Scognet return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP - 339171626Scognet sizeof(struct pcb))); 340171626Scognet} 341