Path: blob/main/sys/powerpc/booke/booke_machdep.c
101200 views
/*-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 HWPMC_HOOKS190extern void *int_performance_counter;191#endif192193#define SET_TRAP(ivor, handler) \194KASSERT(((uintptr_t)(&handler) & ~0xffffUL) == \195((uintptr_t)(&interrupt_vector_base) & ~0xffffUL), \196("Handler " #handler " too far from interrupt vector base")); \197mtspr(ivor, (uintptr_t)(&handler) & 0xffffUL);198199uintptr_t powerpc_init(vm_offset_t fdt, vm_offset_t, vm_offset_t, void *mdp,200uint32_t mdp_cookie);201void booke_cpu_init(void);202203void204booke_cpu_init(void)205{206207cpu_features |= PPC_FEATURE_BOOKE;208209psl_kernset = PSL_CE | PSL_ME | PSL_EE;210#ifdef __powerpc64__211psl_kernset |= PSL_CM;212#endif213psl_userset = psl_kernset | PSL_PR;214#ifdef __powerpc64__215psl_userset32 = psl_userset & ~PSL_CM;216#endif217/*218* Zeroed bits in this variable signify that the value of the bit219* in its position is allowed to vary between userspace contexts.220*221* All other bits are required to be identical for every userspace222* context. The actual *value* of the bit is determined by223* psl_userset and/or psl_userset32, and is not allowed to change.224*225* Remember to update this set when implementing support for226* *conditionally* enabling a processor facility. Failing to do227* this will cause swapcontext() in userspace to break when a228* process uses a conditionally-enabled facility.229*230* When *unconditionally* implementing support for a processor231* facility, update psl_userset / psl_userset32 instead.232*233* See the access control check in set_mcontext().234*/235psl_userstatic = ~(PSL_VEC | PSL_FP | PSL_FE0 | PSL_FE1);236237pmap_mmu_install(MMU_TYPE_BOOKE, BUS_PROBE_GENERIC);238}239240void241ivor_setup(void)242{243244mtspr(SPR_IVPR, ((uintptr_t)&interrupt_vector_base) & ~0xffffUL);245246SET_TRAP(SPR_IVOR0, int_critical_input);247SET_TRAP(SPR_IVOR1, int_machine_check);248SET_TRAP(SPR_IVOR2, int_data_storage);249SET_TRAP(SPR_IVOR3, int_instr_storage);250SET_TRAP(SPR_IVOR4, int_external_input);251SET_TRAP(SPR_IVOR5, int_alignment);252SET_TRAP(SPR_IVOR6, int_program);253SET_TRAP(SPR_IVOR8, int_syscall);254SET_TRAP(SPR_IVOR10, int_decrementer);255SET_TRAP(SPR_IVOR11, int_fixed_interval_timer);256SET_TRAP(SPR_IVOR12, int_watchdog);257SET_TRAP(SPR_IVOR13, int_data_tlb_error);258SET_TRAP(SPR_IVOR14, int_inst_tlb_error);259SET_TRAP(SPR_IVOR15, int_debug);260#ifdef HWPMC_HOOKS261SET_TRAP(SPR_IVOR35, int_performance_counter);262#endif263switch ((mfpvr() >> 16) & 0xffff) {264case FSL_E6500:265SET_TRAP(SPR_IVOR32, int_vec);266SET_TRAP(SPR_IVOR33, int_vecast);267/* FALLTHROUGH */268case FSL_E500mc:269case FSL_E5500:270SET_TRAP(SPR_IVOR7, int_fpu);271SET_TRAP(SPR_IVOR15, int_debug_ed);272break;273case FSL_E500v1:274case FSL_E500v2:275SET_TRAP(SPR_IVOR32, int_vec);276break;277}278279#ifdef __powerpc64__280/* Set 64-bit interrupt mode. */281mtspr(SPR_EPCR, mfspr(SPR_EPCR) | EPCR_ICM);282#endif283}284285static int286booke_check_for_fdt(uint32_t arg1, vm_offset_t *dtbp)287{288void *ptr;289int fdt_size;290291if (arg1 % 8 != 0)292return (-1);293294ptr = (void *)pmap_early_io_map(arg1, PAGE_SIZE);295if (fdt_check_header(ptr) != 0)296return (-1);297298/*299* Read FDT total size from the header of FDT.300* This for sure hits within first page which is301* already mapped.302*/303fdt_size = fdt_totalsize((void *)ptr);304305/*306* Ok, arg1 points to FDT, so we need to map it in.307* First, unmap this page and then map FDT again with full size308*/309pmap_early_io_unmap((vm_offset_t)ptr, PAGE_SIZE);310ptr = (void *)pmap_early_io_map(arg1, fdt_size);311*dtbp = (vm_offset_t)ptr;312313return (0);314}315316uintptr_t317booke_init(u_long arg1, u_long arg2)318{319uintptr_t ret;320void *mdp;321vm_offset_t dtbp, end;322323end = (uintptr_t)_end;324dtbp = (vm_offset_t)NULL;325326/* Set up TLB initially */327bootinfo = NULL;328bzero(__sbss_start, __sbss_end - __sbss_start);329bzero(__bss_start, _end - __bss_start);330tlb1_init();331332/*333* Handle the various ways we can get loaded and started:334* - FreeBSD's loader passes the pointer to the metadata335* in arg1, with arg2 undefined. arg1 has a value that's336* relative to the kernel's link address (i.e. larger337* than 0xc0000000).338* - Juniper's loader passes the metadata pointer in arg2339* and sets arg1 to zero. This is to signal that the340* loader maps the kernel and starts it at its link341* address (unlike the FreeBSD loader).342* - U-Boot passes the standard argc and argv parameters343* in arg1 and arg2 (resp). arg1 is between 1 and some344* relatively small number, such as 64K. arg2 is the345* physical address of the argv vector.346* - ePAPR loaders pass an FDT blob in r3 (arg1) and the magic hex347* string 0x45504150 ('EPAP') in r6 (which has been lost by now).348* r4 (arg2) is supposed to be set to zero, but is not always.349*/350351if (arg1 == 0) /* Juniper loader */352mdp = (void *)arg2;353else if (booke_check_for_fdt(arg1, &dtbp) == 0) { /* ePAPR */354end = roundup(end, 8);355memmove((void *)end, (void *)dtbp, fdt_totalsize((void *)dtbp));356dtbp = end;357end += fdt_totalsize((void *)dtbp);358__endkernel = end;359mdp = NULL;360} else if (arg1 > (uintptr_t)kernload) /* FreeBSD loader */361mdp = (void *)arg1;362else /* U-Boot */363mdp = NULL;364365/* Default to 32 byte cache line size. */366switch ((mfpvr()) >> 16) {367case FSL_E500mc:368case FSL_E5500:369case FSL_E6500:370cacheline_size = 64;371break;372}373374/*375* Last element is a magic cookie that indicates that the metadata376* pointer is meaningful.377*/378ret = powerpc_init(dtbp, 0, 0, mdp, (mdp == NULL) ? 0 : 0xfb5d104d);379380/* Enable caches */381booke_enable_l1_cache();382booke_enable_l2_cache();383384booke_enable_bpred();385386return (ret);387}388389#define RES_GRANULE cacheline_size390extern uintptr_t tlb0_miss_locks[];391392/* Initialise a struct pcpu. */393void394cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)395{396397pcpu->pc_booke.tid_next = TID_MIN;398399#ifdef SMP400uintptr_t *ptr;401int words_per_gran = RES_GRANULE / sizeof(uintptr_t);402403ptr = &tlb0_miss_locks[cpuid * words_per_gran];404pcpu->pc_booke.tlb_lock = ptr;405*ptr = TLB_UNLOCKED;406*(ptr + 1) = 0; /* recurse counter */407#endif408}409410/* Shutdown the CPU as much as possible. */411void412cpu_halt(void)413{414415mtmsr(mfmsr() & ~(PSL_CE | PSL_EE | PSL_ME | PSL_DE));416while (1)417;418}419420int421ptrace_single_step(struct thread *td)422{423struct trapframe *tf;424425tf = td->td_frame;426tf->srr1 |= PSL_DE;427tf->cpu.booke.dbcr0 |= (DBCR0_IDM | DBCR0_IC);428return (0);429}430431int432ptrace_clear_single_step(struct thread *td)433{434struct trapframe *tf;435436tf = td->td_frame;437tf->srr1 &= ~PSL_DE;438tf->cpu.booke.dbcr0 &= ~(DBCR0_IDM | DBCR0_IC);439return (0);440}441442void443kdb_cpu_clear_singlestep(void)444{445register_t r;446447r = mfspr(SPR_DBCR0);448mtspr(SPR_DBCR0, r & ~DBCR0_IC);449kdb_frame->srr1 &= ~PSL_DE;450}451452void453kdb_cpu_set_singlestep(void)454{455register_t r;456457r = mfspr(SPR_DBCR0);458mtspr(SPR_DBCR0, r | DBCR0_IC | DBCR0_IDM);459kdb_frame->srr1 |= PSL_DE;460}461462463