/*-1* Copyright (C) 2006-2012 Semihalf2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES15* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN16* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,17* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED18* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR19* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF20* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING21* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS22* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*/24/*-25* Copyright (C) 2001 Benno Rice26* All rights reserved.27*28* Redistribution and use in source and binary forms, with or without29* modification, are permitted provided that the following conditions30* are met:31* 1. Redistributions of source code must retain the above copyright32* notice, this list of conditions and the following disclaimer.33* 2. Redistributions in binary form must reproduce the above copyright34* notice, this list of conditions and the following disclaimer in the35* documentation and/or other materials provided with the distribution.36*37* THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR38* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES39* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.40* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,41* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,42* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;43* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,44* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR45* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF46* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.47* $NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $48*/49/*-50* Copyright (C) 1995, 1996 Wolfgang Solfrank.51* Copyright (C) 1995, 1996 TooLs GmbH.52* All rights reserved.53*54* Redistribution and use in source and binary forms, with or without55* modification, are permitted provided that the following conditions56* are met:57* 1. Redistributions of source code must retain the above copyright58* notice, this list of conditions and the following disclaimer.59* 2. Redistributions in binary form must reproduce the above copyright60* notice, this list of conditions and the following disclaimer in the61* documentation and/or other materials provided with the distribution.62* 3. All advertising materials mentioning features or use of this software63* must display the following acknowledgement:64* This product includes software developed by TooLs GmbH.65* 4. The name of TooLs GmbH may not be used to endorse or promote products66* derived from this software without specific prior written permission.67*68* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR69* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES70* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.71* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,72* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,73* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;74* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,75* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR76* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF77* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.78*/7980#include <sys/cdefs.h>81#include "opt_ddb.h"82#include "opt_hwpmc_hooks.h"83#include "opt_kstack_pages.h"84#include "opt_platform.h"8586#include <sys/types.h>87#include <sys/param.h>88#include <sys/proc.h>89#include <sys/systm.h>90#include <sys/time.h>91#include <sys/bio.h>92#include <sys/buf.h>93#include <sys/bus.h>94#include <sys/cons.h>95#include <sys/cpu.h>96#include <sys/kdb.h>97#include <sys/kernel.h>98#include <sys/lock.h>99#include <sys/mutex.h>100#include <sys/rwlock.h>101#include <sys/sysctl.h>102#include <sys/exec.h>103#include <sys/ktr.h>104#include <sys/syscallsubr.h>105#include <sys/sysproto.h>106#include <sys/signalvar.h>107#include <sys/sysent.h>108#include <sys/imgact.h>109#include <sys/msgbuf.h>110#include <sys/ptrace.h>111112#include <vm/vm.h>113#include <vm/pmap.h>114#include <vm/vm_extern.h>115#include <vm/vm_page.h>116#include <vm/vm_object.h>117#include <vm/vm_pager.h>118119#include <machine/cpu.h>120#include <machine/kdb.h>121#include <machine/vmparam.h>122#include <machine/spr.h>123#include <machine/hid.h>124#include <machine/psl.h>125#include <machine/trap.h>126#include <machine/md_var.h>127#include <machine/mmuvar.h>128#include <machine/sigframe.h>129#include <machine/machdep.h>130#include <machine/metadata.h>131#include <machine/platform.h>132133#include <sys/linker.h>134#include <sys/reboot.h>135136#include <contrib/libfdt/libfdt.h>137#include <dev/fdt/fdt_common.h>138#include <dev/ofw/openfirm.h>139140#ifdef DDB141#include <ddb/ddb.h>142#endif143144#ifdef DEBUG145#define debugf(fmt, args...) printf(fmt, ##args)146#else147#define debugf(fmt, args...)148#endif149150extern unsigned char _etext[];151extern unsigned char _edata[];152extern unsigned char __bss_start[];153extern unsigned char __sbss_start[];154extern unsigned char __sbss_end[];155extern unsigned char _end[];156extern vm_offset_t __endkernel;157extern vm_paddr_t kernload;158159/*160* Bootinfo is passed to us by legacy loaders. Save the address of the161* structure to handle backward compatibility.162*/163uint32_t *bootinfo;164165void print_kernel_section_addr(void);166void print_kenv(void);167uintptr_t booke_init(u_long, u_long);168void ivor_setup(void);169170extern void *interrupt_vector_base;171extern void *int_critical_input;172extern void *int_machine_check;173extern void *int_data_storage;174extern void *int_instr_storage;175extern void *int_external_input;176extern void *int_alignment;177extern void *int_fpu;178extern void *int_program;179extern void *int_syscall;180extern void *int_decrementer;181extern void *int_fixed_interval_timer;182extern void *int_watchdog;183extern void *int_data_tlb_error;184extern void *int_inst_tlb_error;185extern void *int_debug;186extern void *int_debug_ed;187extern void *int_vec;188extern void *int_vecast;189#ifdef __SPE__190extern void *int_spe_fpdata;191extern void *int_spe_fpround;192#endif193#ifdef HWPMC_HOOKS194extern void *int_performance_counter;195#endif196197#define SET_TRAP(ivor, handler) \198KASSERT(((uintptr_t)(&handler) & ~0xffffUL) == \199((uintptr_t)(&interrupt_vector_base) & ~0xffffUL), \200("Handler " #handler " too far from interrupt vector base")); \201mtspr(ivor, (uintptr_t)(&handler) & 0xffffUL);202203uintptr_t powerpc_init(vm_offset_t fdt, vm_offset_t, vm_offset_t, void *mdp,204uint32_t mdp_cookie);205void booke_cpu_init(void);206207void208booke_cpu_init(void)209{210211cpu_features |= PPC_FEATURE_BOOKE;212213psl_kernset = PSL_CE | PSL_ME | PSL_EE;214#ifdef __powerpc64__215psl_kernset |= PSL_CM;216#endif217psl_userset = psl_kernset | PSL_PR;218#ifdef __powerpc64__219psl_userset32 = psl_userset & ~PSL_CM;220#endif221/*222* Zeroed bits in this variable signify that the value of the bit223* in its position is allowed to vary between userspace contexts.224*225* All other bits are required to be identical for every userspace226* context. The actual *value* of the bit is determined by227* psl_userset and/or psl_userset32, and is not allowed to change.228*229* Remember to update this set when implementing support for230* *conditionally* enabling a processor facility. Failing to do231* this will cause swapcontext() in userspace to break when a232* process uses a conditionally-enabled facility.233*234* When *unconditionally* implementing support for a processor235* facility, update psl_userset / psl_userset32 instead.236*237* See the access control check in set_mcontext().238*/239psl_userstatic = ~(PSL_VEC | PSL_FP | PSL_FE0 | PSL_FE1);240241pmap_mmu_install(MMU_TYPE_BOOKE, BUS_PROBE_GENERIC);242}243244void245ivor_setup(void)246{247248mtspr(SPR_IVPR, ((uintptr_t)&interrupt_vector_base) & ~0xffffUL);249250SET_TRAP(SPR_IVOR0, int_critical_input);251SET_TRAP(SPR_IVOR1, int_machine_check);252SET_TRAP(SPR_IVOR2, int_data_storage);253SET_TRAP(SPR_IVOR3, int_instr_storage);254SET_TRAP(SPR_IVOR4, int_external_input);255SET_TRAP(SPR_IVOR5, int_alignment);256SET_TRAP(SPR_IVOR6, int_program);257SET_TRAP(SPR_IVOR8, int_syscall);258SET_TRAP(SPR_IVOR10, int_decrementer);259SET_TRAP(SPR_IVOR11, int_fixed_interval_timer);260SET_TRAP(SPR_IVOR12, int_watchdog);261SET_TRAP(SPR_IVOR13, int_data_tlb_error);262SET_TRAP(SPR_IVOR14, int_inst_tlb_error);263SET_TRAP(SPR_IVOR15, int_debug);264#ifdef HWPMC_HOOKS265SET_TRAP(SPR_IVOR35, int_performance_counter);266#endif267switch ((mfpvr() >> 16) & 0xffff) {268case FSL_E6500:269SET_TRAP(SPR_IVOR32, int_vec);270SET_TRAP(SPR_IVOR33, int_vecast);271/* FALLTHROUGH */272case FSL_E500mc:273case FSL_E5500:274SET_TRAP(SPR_IVOR7, int_fpu);275SET_TRAP(SPR_IVOR15, int_debug_ed);276break;277case FSL_E500v1:278case FSL_E500v2:279SET_TRAP(SPR_IVOR32, int_vec);280#ifdef __SPE__281SET_TRAP(SPR_IVOR33, int_spe_fpdata);282SET_TRAP(SPR_IVOR34, int_spe_fpround);283#endif284break;285}286287#ifdef __powerpc64__288/* Set 64-bit interrupt mode. */289mtspr(SPR_EPCR, mfspr(SPR_EPCR) | EPCR_ICM);290#endif291}292293static int294booke_check_for_fdt(uint32_t arg1, vm_offset_t *dtbp)295{296void *ptr;297int fdt_size;298299if (arg1 % 8 != 0)300return (-1);301302ptr = (void *)pmap_early_io_map(arg1, PAGE_SIZE);303if (fdt_check_header(ptr) != 0)304return (-1);305306/*307* Read FDT total size from the header of FDT.308* This for sure hits within first page which is309* already mapped.310*/311fdt_size = fdt_totalsize((void *)ptr);312313/*314* Ok, arg1 points to FDT, so we need to map it in.315* First, unmap this page and then map FDT again with full size316*/317pmap_early_io_unmap((vm_offset_t)ptr, PAGE_SIZE);318ptr = (void *)pmap_early_io_map(arg1, fdt_size);319*dtbp = (vm_offset_t)ptr;320321return (0);322}323324uintptr_t325booke_init(u_long arg1, u_long arg2)326{327uintptr_t ret;328void *mdp;329vm_offset_t dtbp, end;330331end = (uintptr_t)_end;332dtbp = (vm_offset_t)NULL;333334/* Set up TLB initially */335bootinfo = NULL;336bzero(__sbss_start, __sbss_end - __sbss_start);337bzero(__bss_start, _end - __bss_start);338tlb1_init();339340/*341* Handle the various ways we can get loaded and started:342* - FreeBSD's loader passes the pointer to the metadata343* in arg1, with arg2 undefined. arg1 has a value that's344* relative to the kernel's link address (i.e. larger345* than 0xc0000000).346* - Juniper's loader passes the metadata pointer in arg2347* and sets arg1 to zero. This is to signal that the348* loader maps the kernel and starts it at its link349* address (unlike the FreeBSD loader).350* - U-Boot passes the standard argc and argv parameters351* in arg1 and arg2 (resp). arg1 is between 1 and some352* relatively small number, such as 64K. arg2 is the353* physical address of the argv vector.354* - ePAPR loaders pass an FDT blob in r3 (arg1) and the magic hex355* string 0x45504150 ('EPAP') in r6 (which has been lost by now).356* r4 (arg2) is supposed to be set to zero, but is not always.357*/358359if (arg1 == 0) /* Juniper loader */360mdp = (void *)arg2;361else if (booke_check_for_fdt(arg1, &dtbp) == 0) { /* ePAPR */362end = roundup(end, 8);363memmove((void *)end, (void *)dtbp, fdt_totalsize((void *)dtbp));364dtbp = end;365end += fdt_totalsize((void *)dtbp);366__endkernel = end;367mdp = NULL;368} else if (arg1 > (uintptr_t)kernload) /* FreeBSD loader */369mdp = (void *)arg1;370else /* U-Boot */371mdp = NULL;372373/* Default to 32 byte cache line size. */374switch ((mfpvr()) >> 16) {375case FSL_E500mc:376case FSL_E5500:377case FSL_E6500:378cacheline_size = 64;379break;380}381382/*383* Last element is a magic cookie that indicates that the metadata384* pointer is meaningful.385*/386ret = powerpc_init(dtbp, 0, 0, mdp, (mdp == NULL) ? 0 : 0xfb5d104d);387388/* Enable caches */389booke_enable_l1_cache();390booke_enable_l2_cache();391392booke_enable_bpred();393394return (ret);395}396397#define RES_GRANULE cacheline_size398extern uintptr_t tlb0_miss_locks[];399400/* Initialise a struct pcpu. */401void402cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)403{404405pcpu->pc_booke.tid_next = TID_MIN;406407#ifdef SMP408uintptr_t *ptr;409int words_per_gran = RES_GRANULE / sizeof(uintptr_t);410411ptr = &tlb0_miss_locks[cpuid * words_per_gran];412pcpu->pc_booke.tlb_lock = ptr;413*ptr = TLB_UNLOCKED;414*(ptr + 1) = 0; /* recurse counter */415#endif416}417418/* Shutdown the CPU as much as possible. */419void420cpu_halt(void)421{422423mtmsr(mfmsr() & ~(PSL_CE | PSL_EE | PSL_ME | PSL_DE));424while (1)425;426}427428int429ptrace_single_step(struct thread *td)430{431struct trapframe *tf;432433tf = td->td_frame;434tf->srr1 |= PSL_DE;435tf->cpu.booke.dbcr0 |= (DBCR0_IDM | DBCR0_IC);436return (0);437}438439int440ptrace_clear_single_step(struct thread *td)441{442struct trapframe *tf;443444tf = td->td_frame;445tf->srr1 &= ~PSL_DE;446tf->cpu.booke.dbcr0 &= ~(DBCR0_IDM | DBCR0_IC);447return (0);448}449450void451kdb_cpu_clear_singlestep(void)452{453register_t r;454455r = mfspr(SPR_DBCR0);456mtspr(SPR_DBCR0, r & ~DBCR0_IC);457kdb_frame->srr1 &= ~PSL_DE;458}459460void461kdb_cpu_set_singlestep(void)462{463register_t r;464465r = mfspr(SPR_DBCR0);466mtspr(SPR_DBCR0, r | DBCR0_IC | DBCR0_IDM);467kdb_frame->srr1 |= PSL_DE;468}469470471