114cf11afSPaul Mackerras/* 214cf11afSPaul Mackerras * PowerPC version 314cf11afSPaul Mackerras * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) 414cf11afSPaul Mackerras * 514cf11afSPaul Mackerras * Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP 614cf11afSPaul Mackerras * Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu> 714cf11afSPaul Mackerras * Adapted for Power Macintosh by Paul Mackerras. 814cf11afSPaul Mackerras * Low-level exception handlers and MMU support 914cf11afSPaul Mackerras * rewritten by Paul Mackerras. 1014cf11afSPaul Mackerras * Copyright (C) 1996 Paul Mackerras. 1114cf11afSPaul Mackerras * 1214cf11afSPaul Mackerras * Adapted for 64bit PowerPC by Dave Engebretsen, Peter Bergner, and 1314cf11afSPaul Mackerras * Mike Corrigan {engebret|bergner|mikejc}@us.ibm.com 1414cf11afSPaul Mackerras * 15*0ebc4cdaSBenjamin Herrenschmidt * This file contains the entry point for the 64-bit kernel along 16*0ebc4cdaSBenjamin Herrenschmidt * with some early initialization code common to all 64-bit powerpc 17*0ebc4cdaSBenjamin Herrenschmidt * variants. 1814cf11afSPaul Mackerras * 1914cf11afSPaul Mackerras * This program is free software; you can redistribute it and/or 2014cf11afSPaul Mackerras * modify it under the terms of the GNU General Public License 2114cf11afSPaul Mackerras * as published by the Free Software Foundation; either version 2214cf11afSPaul Mackerras * 2 of the License, or (at your option) any later version. 2314cf11afSPaul Mackerras */ 2414cf11afSPaul Mackerras 2514cf11afSPaul Mackerras#include <linux/threads.h> 26b5bbeb23SPaul Mackerras#include <asm/reg.h> 2714cf11afSPaul Mackerras#include <asm/page.h> 2814cf11afSPaul Mackerras#include <asm/mmu.h> 2914cf11afSPaul Mackerras#include <asm/ppc_asm.h> 3014cf11afSPaul Mackerras#include <asm/asm-offsets.h> 3114cf11afSPaul Mackerras#include <asm/bug.h> 3214cf11afSPaul Mackerras#include <asm/cputable.h> 3314cf11afSPaul Mackerras#include <asm/setup.h> 3414cf11afSPaul Mackerras#include <asm/hvcall.h> 35c43a55ffSKelly Daly#include <asm/iseries/lpar_map.h> 366cb7bfebSDavid Gibson#include <asm/thread_info.h> 373f639ee8SStephen Rothwell#include <asm/firmware.h> 3816a15a30SStephen Rothwell#include <asm/page_64.h> 39f9ff0f30SStephen Rothwell#include <asm/exception.h> 40945feb17SBenjamin Herrenschmidt#include <asm/irqflags.h> 4114cf11afSPaul Mackerras 42*0ebc4cdaSBenjamin Herrenschmidt/* The physical memory is layed out such that the secondary processor 43*0ebc4cdaSBenjamin Herrenschmidt * spin code sits at 0x0000...0x00ff. On server, the vectors follow 44*0ebc4cdaSBenjamin Herrenschmidt * using the layout described in exceptions-64s.S 4514cf11afSPaul Mackerras */ 4614cf11afSPaul Mackerras 4714cf11afSPaul Mackerras/* 4814cf11afSPaul Mackerras * Entering into this code we make the following assumptions: 49*0ebc4cdaSBenjamin Herrenschmidt * 50*0ebc4cdaSBenjamin Herrenschmidt * For pSeries or server processors: 5114cf11afSPaul Mackerras * 1. The MMU is off & open firmware is running in real mode. 5214cf11afSPaul Mackerras * 2. The kernel is entered at __start 5314cf11afSPaul Mackerras * 5414cf11afSPaul Mackerras * For iSeries: 5514cf11afSPaul Mackerras * 1. The MMU is on (as it always is for iSeries) 5614cf11afSPaul Mackerras * 2. The kernel is entered at system_reset_iSeries 57*0ebc4cdaSBenjamin Herrenschmidt * 58*0ebc4cdaSBenjamin Herrenschmidt * For Book3E processors: 59*0ebc4cdaSBenjamin Herrenschmidt * 1. The MMU is on running in AS0 in a state defined in ePAPR 60*0ebc4cdaSBenjamin Herrenschmidt * 2. The kernel is entered at __start 6114cf11afSPaul Mackerras */ 6214cf11afSPaul Mackerras 6314cf11afSPaul Mackerras .text 6414cf11afSPaul Mackerras .globl _stext 6514cf11afSPaul Mackerras_stext: 6614cf11afSPaul Mackerras_GLOBAL(__start) 6714cf11afSPaul Mackerras /* NOP this out unconditionally */ 6814cf11afSPaul MackerrasBEGIN_FTR_SECTION 6914cf11afSPaul Mackerras b .__start_initialization_multiplatform 7014cf11afSPaul MackerrasEND_FTR_SECTION(0, 1) 7114cf11afSPaul Mackerras 7214cf11afSPaul Mackerras /* Catch branch to 0 in real mode */ 7314cf11afSPaul Mackerras trap 7414cf11afSPaul Mackerras 751f6a93e4SPaul Mackerras /* Secondary processors spin on this value until it becomes nonzero. 761f6a93e4SPaul Mackerras * When it does it contains the real address of the descriptor 771f6a93e4SPaul Mackerras * of the function that the cpu should jump to to continue 781f6a93e4SPaul Mackerras * initialization. 791f6a93e4SPaul Mackerras */ 8014cf11afSPaul Mackerras .globl __secondary_hold_spinloop 8114cf11afSPaul Mackerras__secondary_hold_spinloop: 8214cf11afSPaul Mackerras .llong 0x0 8314cf11afSPaul Mackerras 8414cf11afSPaul Mackerras /* Secondary processors write this value with their cpu # */ 8514cf11afSPaul Mackerras /* after they enter the spin loop immediately below. */ 8614cf11afSPaul Mackerras .globl __secondary_hold_acknowledge 8714cf11afSPaul Mackerras__secondary_hold_acknowledge: 8814cf11afSPaul Mackerras .llong 0x0 8914cf11afSPaul Mackerras 901dce0e30SMichael Ellerman#ifdef CONFIG_PPC_ISERIES 911dce0e30SMichael Ellerman /* 921dce0e30SMichael Ellerman * At offset 0x20, there is a pointer to iSeries LPAR data. 931dce0e30SMichael Ellerman * This is required by the hypervisor 941dce0e30SMichael Ellerman */ 951dce0e30SMichael Ellerman . = 0x20 961dce0e30SMichael Ellerman .llong hvReleaseData-KERNELBASE 971dce0e30SMichael Ellerman#endif /* CONFIG_PPC_ISERIES */ 981dce0e30SMichael Ellerman 998b8b0cc1SMilton Miller#ifdef CONFIG_CRASH_DUMP 1008b8b0cc1SMilton Miller /* This flag is set to 1 by a loader if the kernel should run 1018b8b0cc1SMilton Miller * at the loaded address instead of the linked address. This 1028b8b0cc1SMilton Miller * is used by kexec-tools to keep the the kdump kernel in the 1038b8b0cc1SMilton Miller * crash_kernel region. The loader is responsible for 1048b8b0cc1SMilton Miller * observing the alignment requirement. 1058b8b0cc1SMilton Miller */ 1068b8b0cc1SMilton Miller /* Do not move this variable as kexec-tools knows about it. */ 1078b8b0cc1SMilton Miller . = 0x5c 1088b8b0cc1SMilton Miller .globl __run_at_load 1098b8b0cc1SMilton Miller__run_at_load: 1108b8b0cc1SMilton Miller .long 0x72756e30 /* "run0" -- relocate to 0 by default */ 1118b8b0cc1SMilton Miller#endif 1128b8b0cc1SMilton Miller 11314cf11afSPaul Mackerras . = 0x60 11414cf11afSPaul Mackerras/* 11575423b7bSGeoff Levand * The following code is used to hold secondary processors 11675423b7bSGeoff Levand * in a spin loop after they have entered the kernel, but 11714cf11afSPaul Mackerras * before the bulk of the kernel has been relocated. This code 11814cf11afSPaul Mackerras * is relocated to physical address 0x60 before prom_init is run. 11914cf11afSPaul Mackerras * All of it must fit below the first exception vector at 0x100. 1201f6a93e4SPaul Mackerras * Use .globl here not _GLOBAL because we want __secondary_hold 1211f6a93e4SPaul Mackerras * to be the actual text address, not a descriptor. 12214cf11afSPaul Mackerras */ 1231f6a93e4SPaul Mackerras .globl __secondary_hold 1241f6a93e4SPaul Mackerras__secondary_hold: 12514cf11afSPaul Mackerras mfmsr r24 12614cf11afSPaul Mackerras ori r24,r24,MSR_RI 12714cf11afSPaul Mackerras mtmsrd r24 /* RI on */ 12814cf11afSPaul Mackerras 129f1870f77SAnton Blanchard /* Grab our physical cpu number */ 13014cf11afSPaul Mackerras mr r24,r3 13114cf11afSPaul Mackerras 13214cf11afSPaul Mackerras /* Tell the master cpu we're here */ 13314cf11afSPaul Mackerras /* Relocation is off & we are located at an address less */ 13414cf11afSPaul Mackerras /* than 0x100, so only need to grab low order offset. */ 135e31aa453SPaul Mackerras std r24,__secondary_hold_acknowledge-_stext(0) 13614cf11afSPaul Mackerras sync 13714cf11afSPaul Mackerras 13814cf11afSPaul Mackerras /* All secondary cpus wait here until told to start. */ 139e31aa453SPaul Mackerras100: ld r4,__secondary_hold_spinloop-_stext(0) 1401f6a93e4SPaul Mackerras cmpdi 0,r4,0 1411f6a93e4SPaul Mackerras beq 100b 14214cf11afSPaul Mackerras 143f1870f77SAnton Blanchard#if defined(CONFIG_SMP) || defined(CONFIG_KEXEC) 1441f6a93e4SPaul Mackerras ld r4,0(r4) /* deref function descriptor */ 145758438a7SMichael Ellerman mtctr r4 14614cf11afSPaul Mackerras mr r3,r24 147758438a7SMichael Ellerman bctr 14814cf11afSPaul Mackerras#else 14914cf11afSPaul Mackerras BUG_OPCODE 15014cf11afSPaul Mackerras#endif 15114cf11afSPaul Mackerras 15214cf11afSPaul Mackerras/* This value is used to mark exception frames on the stack. */ 15314cf11afSPaul Mackerras .section ".toc","aw" 15414cf11afSPaul Mackerrasexception_marker: 15514cf11afSPaul Mackerras .tc ID_72656773_68657265[TC],0x7265677368657265 15614cf11afSPaul Mackerras .text 15714cf11afSPaul Mackerras 15814cf11afSPaul Mackerras/* 159*0ebc4cdaSBenjamin Herrenschmidt * On server, we include the exception vectors code here as it 160*0ebc4cdaSBenjamin Herrenschmidt * relies on absolute addressing which is only possible within 161*0ebc4cdaSBenjamin Herrenschmidt * this compilation unit 16214cf11afSPaul Mackerras */ 163*0ebc4cdaSBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3S 164*0ebc4cdaSBenjamin Herrenschmidt#include "exceptions-64s.S" 1651f6a93e4SPaul Mackerras#endif 16614cf11afSPaul Mackerras 16714cf11afSPaul Mackerras 16814cf11afSPaul Mackerras/* 169f39b7a55SOlof Johansson * On pSeries and most other platforms, secondary processors spin 170f39b7a55SOlof Johansson * in the following code. 17114cf11afSPaul Mackerras * At entry, r3 = this processor's number (physical cpu id) 17214cf11afSPaul Mackerras */ 173f39b7a55SOlof Johansson_GLOBAL(generic_secondary_smp_init) 17414cf11afSPaul Mackerras mr r24,r3 17514cf11afSPaul Mackerras 17614cf11afSPaul Mackerras /* turn on 64-bit mode */ 17714cf11afSPaul Mackerras bl .enable_64b_mode 17814cf11afSPaul Mackerras 179e31aa453SPaul Mackerras /* get the TOC pointer (real address) */ 180e31aa453SPaul Mackerras bl .relative_toc 181e31aa453SPaul Mackerras 18214cf11afSPaul Mackerras /* Set up a paca value for this processor. Since we have the 18314cf11afSPaul Mackerras * physical cpu id in r24, we need to search the pacas to find 18414cf11afSPaul Mackerras * which logical id maps to our physical one. 18514cf11afSPaul Mackerras */ 186e31aa453SPaul Mackerras LOAD_REG_ADDR(r13, paca) /* Get base vaddr of paca array */ 18714cf11afSPaul Mackerras li r5,0 /* logical cpu id */ 18814cf11afSPaul Mackerras1: lhz r6,PACAHWCPUID(r13) /* Load HW procid from paca */ 18914cf11afSPaul Mackerras cmpw r6,r24 /* Compare to our id */ 19014cf11afSPaul Mackerras beq 2f 19114cf11afSPaul Mackerras addi r13,r13,PACA_SIZE /* Loop to next PACA on miss */ 19214cf11afSPaul Mackerras addi r5,r5,1 19314cf11afSPaul Mackerras cmpwi r5,NR_CPUS 19414cf11afSPaul Mackerras blt 1b 19514cf11afSPaul Mackerras 19614cf11afSPaul Mackerras mr r3,r24 /* not found, copy phys to r3 */ 19714cf11afSPaul Mackerras b .kexec_wait /* next kernel might do better */ 19814cf11afSPaul Mackerras 199b5bbeb23SPaul Mackerras2: mtspr SPRN_SPRG3,r13 /* Save vaddr of paca in SPRG3 */ 20014cf11afSPaul Mackerras /* From now on, r24 is expected to be logical cpuid */ 20114cf11afSPaul Mackerras mr r24,r5 20214cf11afSPaul Mackerras3: HMT_LOW 20314cf11afSPaul Mackerras lbz r23,PACAPROCSTART(r13) /* Test if this processor should */ 20414cf11afSPaul Mackerras /* start. */ 20514cf11afSPaul Mackerras 206f39b7a55SOlof Johansson#ifndef CONFIG_SMP 207f39b7a55SOlof Johansson b 3b /* Never go on non-SMP */ 208f39b7a55SOlof Johansson#else 209f39b7a55SOlof Johansson cmpwi 0,r23,0 210f39b7a55SOlof Johansson beq 3b /* Loop until told to go */ 211f39b7a55SOlof Johansson 212b6f6b98aSSonny Rao sync /* order paca.run and cur_cpu_spec */ 213b6f6b98aSSonny Rao 214f39b7a55SOlof Johansson /* See if we need to call a cpu state restore handler */ 215e31aa453SPaul Mackerras LOAD_REG_ADDR(r23, cur_cpu_spec) 216f39b7a55SOlof Johansson ld r23,0(r23) 217f39b7a55SOlof Johansson ld r23,CPU_SPEC_RESTORE(r23) 218f39b7a55SOlof Johansson cmpdi 0,r23,0 219f39b7a55SOlof Johansson beq 4f 220f39b7a55SOlof Johansson ld r23,0(r23) 221f39b7a55SOlof Johansson mtctr r23 222f39b7a55SOlof Johansson bctrl 223f39b7a55SOlof Johansson 224f39b7a55SOlof Johansson4: /* Create a temp kernel stack for use before relocation is on. */ 22514cf11afSPaul Mackerras ld r1,PACAEMERGSP(r13) 22614cf11afSPaul Mackerras subi r1,r1,STACK_FRAME_OVERHEAD 22714cf11afSPaul Mackerras 228c705677eSStephen Rothwell b __secondary_start 22914cf11afSPaul Mackerras#endif 23014cf11afSPaul Mackerras 231e31aa453SPaul Mackerras/* 232e31aa453SPaul Mackerras * Turn the MMU off. 233e31aa453SPaul Mackerras * Assumes we're mapped EA == RA if the MMU is on. 234e31aa453SPaul Mackerras */ 23514cf11afSPaul Mackerras_STATIC(__mmu_off) 23614cf11afSPaul Mackerras mfmsr r3 23714cf11afSPaul Mackerras andi. r0,r3,MSR_IR|MSR_DR 23814cf11afSPaul Mackerras beqlr 239e31aa453SPaul Mackerras mflr r4 24014cf11afSPaul Mackerras andc r3,r3,r0 24114cf11afSPaul Mackerras mtspr SPRN_SRR0,r4 24214cf11afSPaul Mackerras mtspr SPRN_SRR1,r3 24314cf11afSPaul Mackerras sync 24414cf11afSPaul Mackerras rfid 24514cf11afSPaul Mackerras b . /* prevent speculative execution */ 24614cf11afSPaul Mackerras 24714cf11afSPaul Mackerras 24814cf11afSPaul Mackerras/* 24914cf11afSPaul Mackerras * Here is our main kernel entry point. We support currently 2 kind of entries 25014cf11afSPaul Mackerras * depending on the value of r5. 25114cf11afSPaul Mackerras * 25214cf11afSPaul Mackerras * r5 != NULL -> OF entry, we go to prom_init, "legacy" parameter content 25314cf11afSPaul Mackerras * in r3...r7 25414cf11afSPaul Mackerras * 25514cf11afSPaul Mackerras * r5 == NULL -> kexec style entry. r3 is a physical pointer to the 25614cf11afSPaul Mackerras * DT block, r4 is a physical pointer to the kernel itself 25714cf11afSPaul Mackerras * 25814cf11afSPaul Mackerras */ 25914cf11afSPaul Mackerras_GLOBAL(__start_initialization_multiplatform) 260e31aa453SPaul Mackerras /* Make sure we are running in 64 bits mode */ 261e31aa453SPaul Mackerras bl .enable_64b_mode 262e31aa453SPaul Mackerras 263e31aa453SPaul Mackerras /* Get TOC pointer (current runtime address) */ 264e31aa453SPaul Mackerras bl .relative_toc 265e31aa453SPaul Mackerras 266e31aa453SPaul Mackerras /* find out where we are now */ 267e31aa453SPaul Mackerras bcl 20,31,$+4 268e31aa453SPaul Mackerras0: mflr r26 /* r26 = runtime addr here */ 269e31aa453SPaul Mackerras addis r26,r26,(_stext - 0b)@ha 270e31aa453SPaul Mackerras addi r26,r26,(_stext - 0b)@l /* current runtime base addr */ 271e31aa453SPaul Mackerras 27214cf11afSPaul Mackerras /* 27314cf11afSPaul Mackerras * Are we booted from a PROM Of-type client-interface ? 27414cf11afSPaul Mackerras */ 27514cf11afSPaul Mackerras cmpldi cr0,r5,0 276939e60f6SStephen Rothwell beq 1f 277939e60f6SStephen Rothwell b .__boot_from_prom /* yes -> prom */ 278939e60f6SStephen Rothwell1: 27914cf11afSPaul Mackerras /* Save parameters */ 28014cf11afSPaul Mackerras mr r31,r3 28114cf11afSPaul Mackerras mr r30,r4 28214cf11afSPaul Mackerras 28314cf11afSPaul Mackerras /* Setup some critical 970 SPRs before switching MMU off */ 284f39b7a55SOlof Johansson mfspr r0,SPRN_PVR 285f39b7a55SOlof Johansson srwi r0,r0,16 286f39b7a55SOlof Johansson cmpwi r0,0x39 /* 970 */ 287f39b7a55SOlof Johansson beq 1f 288f39b7a55SOlof Johansson cmpwi r0,0x3c /* 970FX */ 289f39b7a55SOlof Johansson beq 1f 290f39b7a55SOlof Johansson cmpwi r0,0x44 /* 970MP */ 291190a24f5SOlof Johansson beq 1f 292190a24f5SOlof Johansson cmpwi r0,0x45 /* 970GX */ 293f39b7a55SOlof Johansson bne 2f 294f39b7a55SOlof Johansson1: bl .__cpu_preinit_ppc970 295f39b7a55SOlof Johansson2: 29614cf11afSPaul Mackerras 297e31aa453SPaul Mackerras /* Switch off MMU if not already off */ 29814cf11afSPaul Mackerras bl .__mmu_off 29914cf11afSPaul Mackerras b .__after_prom_start 30014cf11afSPaul Mackerras 301939e60f6SStephen Rothwell_INIT_STATIC(__boot_from_prom) 30228794d34SBenjamin Herrenschmidt#ifdef CONFIG_PPC_OF_BOOT_TRAMPOLINE 30314cf11afSPaul Mackerras /* Save parameters */ 30414cf11afSPaul Mackerras mr r31,r3 30514cf11afSPaul Mackerras mr r30,r4 30614cf11afSPaul Mackerras mr r29,r5 30714cf11afSPaul Mackerras mr r28,r6 30814cf11afSPaul Mackerras mr r27,r7 30914cf11afSPaul Mackerras 3106088857bSOlaf Hering /* 3116088857bSOlaf Hering * Align the stack to 16-byte boundary 3126088857bSOlaf Hering * Depending on the size and layout of the ELF sections in the initial 313e31aa453SPaul Mackerras * boot binary, the stack pointer may be unaligned on PowerMac 3146088857bSOlaf Hering */ 315c05b4770SLinus Torvalds rldicr r1,r1,0,59 316c05b4770SLinus Torvalds 317549e8152SPaul Mackerras#ifdef CONFIG_RELOCATABLE 318549e8152SPaul Mackerras /* Relocate code for where we are now */ 319549e8152SPaul Mackerras mr r3,r26 320549e8152SPaul Mackerras bl .relocate 321549e8152SPaul Mackerras#endif 322549e8152SPaul Mackerras 32314cf11afSPaul Mackerras /* Restore parameters */ 32414cf11afSPaul Mackerras mr r3,r31 32514cf11afSPaul Mackerras mr r4,r30 32614cf11afSPaul Mackerras mr r5,r29 32714cf11afSPaul Mackerras mr r6,r28 32814cf11afSPaul Mackerras mr r7,r27 32914cf11afSPaul Mackerras 33014cf11afSPaul Mackerras /* Do all of the interaction with OF client interface */ 331549e8152SPaul Mackerras mr r8,r26 33214cf11afSPaul Mackerras bl .prom_init 33328794d34SBenjamin Herrenschmidt#endif /* #CONFIG_PPC_OF_BOOT_TRAMPOLINE */ 33428794d34SBenjamin Herrenschmidt 33528794d34SBenjamin Herrenschmidt /* We never return. We also hit that trap if trying to boot 33628794d34SBenjamin Herrenschmidt * from OF while CONFIG_PPC_OF_BOOT_TRAMPOLINE isn't selected */ 33714cf11afSPaul Mackerras trap 33814cf11afSPaul Mackerras 33914cf11afSPaul Mackerras_STATIC(__after_prom_start) 340549e8152SPaul Mackerras#ifdef CONFIG_RELOCATABLE 341549e8152SPaul Mackerras /* process relocations for the final address of the kernel */ 342549e8152SPaul Mackerras lis r25,PAGE_OFFSET@highest /* compute virtual base of kernel */ 343549e8152SPaul Mackerras sldi r25,r25,32 34454622f10SMohan Kumar M#ifdef CONFIG_CRASH_DUMP 3458b8b0cc1SMilton Miller lwz r7,__run_at_load-_stext(r26) 3468b8b0cc1SMilton Miller cmplwi cr0,r7,1 /* kdump kernel ? - stay where we are */ 34754622f10SMohan Kumar M bne 1f 34854622f10SMohan Kumar M add r25,r25,r26 34954622f10SMohan Kumar M#endif 35054622f10SMohan Kumar M1: mr r3,r25 351549e8152SPaul Mackerras bl .relocate 352549e8152SPaul Mackerras#endif 35314cf11afSPaul Mackerras 35414cf11afSPaul Mackerras/* 355e31aa453SPaul Mackerras * We need to run with _stext at physical address PHYSICAL_START. 35614cf11afSPaul Mackerras * This will leave some code in the first 256B of 35714cf11afSPaul Mackerras * real memory, which are reserved for software use. 35814cf11afSPaul Mackerras * 35914cf11afSPaul Mackerras * Note: This process overwrites the OF exception vectors. 36014cf11afSPaul Mackerras */ 361549e8152SPaul Mackerras li r3,0 /* target addr */ 362549e8152SPaul Mackerras mr. r4,r26 /* In some cases the loader may */ 363e31aa453SPaul Mackerras beq 9f /* have already put us at zero */ 36414cf11afSPaul Mackerras li r6,0x100 /* Start offset, the first 0x100 */ 36514cf11afSPaul Mackerras /* bytes were copied earlier. */ 36614cf11afSPaul Mackerras 36754622f10SMohan Kumar M#ifdef CONFIG_CRASH_DUMP 36854622f10SMohan Kumar M/* 36954622f10SMohan Kumar M * Check if the kernel has to be running as relocatable kernel based on the 3708b8b0cc1SMilton Miller * variable __run_at_load, if it is set the kernel is treated as relocatable 37154622f10SMohan Kumar M * kernel, otherwise it will be moved to PHYSICAL_START 37254622f10SMohan Kumar M */ 3738b8b0cc1SMilton Miller lwz r7,__run_at_load-_stext(r26) 3748b8b0cc1SMilton Miller cmplwi cr0,r7,1 37554622f10SMohan Kumar M bne 3f 37654622f10SMohan Kumar M 37754622f10SMohan Kumar M li r5,__end_interrupts - _stext /* just copy interrupts */ 37854622f10SMohan Kumar M b 5f 37954622f10SMohan Kumar M3: 38054622f10SMohan Kumar M#endif 38154622f10SMohan Kumar M lis r5,(copy_to_here - _stext)@ha 38254622f10SMohan Kumar M addi r5,r5,(copy_to_here - _stext)@l /* # bytes of memory to copy */ 38354622f10SMohan Kumar M 38414cf11afSPaul Mackerras bl .copy_and_flush /* copy the first n bytes */ 38514cf11afSPaul Mackerras /* this includes the code being */ 38614cf11afSPaul Mackerras /* executed here. */ 387e31aa453SPaul Mackerras addis r8,r3,(4f - _stext)@ha /* Jump to the copy of this code */ 388e31aa453SPaul Mackerras addi r8,r8,(4f - _stext)@l /* that we just made */ 389e31aa453SPaul Mackerras mtctr r8 39014cf11afSPaul Mackerras bctr 39114cf11afSPaul Mackerras 39254622f10SMohan Kumar Mp_end: .llong _end - _stext 39354622f10SMohan Kumar M 394e31aa453SPaul Mackerras4: /* Now copy the rest of the kernel up to _end */ 395e31aa453SPaul Mackerras addis r5,r26,(p_end - _stext)@ha 396e31aa453SPaul Mackerras ld r5,(p_end - _stext)@l(r5) /* get _end */ 39754622f10SMohan Kumar M5: bl .copy_and_flush /* copy the rest */ 398e31aa453SPaul Mackerras 399e31aa453SPaul Mackerras9: b .start_here_multiplatform 400e31aa453SPaul Mackerras 40114cf11afSPaul Mackerras/* 40214cf11afSPaul Mackerras * Copy routine used to copy the kernel to start at physical address 0 40314cf11afSPaul Mackerras * and flush and invalidate the caches as needed. 40414cf11afSPaul Mackerras * r3 = dest addr, r4 = source addr, r5 = copy limit, r6 = start offset 40514cf11afSPaul Mackerras * on exit, r3, r4, r5 are unchanged, r6 is updated to be >= r5. 40614cf11afSPaul Mackerras * 40714cf11afSPaul Mackerras * Note: this routine *only* clobbers r0, r6 and lr 40814cf11afSPaul Mackerras */ 40914cf11afSPaul Mackerras_GLOBAL(copy_and_flush) 41014cf11afSPaul Mackerras addi r5,r5,-8 41114cf11afSPaul Mackerras addi r6,r6,-8 4125a2fe38dSOlof Johansson4: li r0,8 /* Use the smallest common */ 41314cf11afSPaul Mackerras /* denominator cache line */ 41414cf11afSPaul Mackerras /* size. This results in */ 41514cf11afSPaul Mackerras /* extra cache line flushes */ 41614cf11afSPaul Mackerras /* but operation is correct. */ 41714cf11afSPaul Mackerras /* Can't get cache line size */ 41814cf11afSPaul Mackerras /* from NACA as it is being */ 41914cf11afSPaul Mackerras /* moved too. */ 42014cf11afSPaul Mackerras 42114cf11afSPaul Mackerras mtctr r0 /* put # words/line in ctr */ 42214cf11afSPaul Mackerras3: addi r6,r6,8 /* copy a cache line */ 42314cf11afSPaul Mackerras ldx r0,r6,r4 42414cf11afSPaul Mackerras stdx r0,r6,r3 42514cf11afSPaul Mackerras bdnz 3b 42614cf11afSPaul Mackerras dcbst r6,r3 /* write it to memory */ 42714cf11afSPaul Mackerras sync 42814cf11afSPaul Mackerras icbi r6,r3 /* flush the icache line */ 42914cf11afSPaul Mackerras cmpld 0,r6,r5 43014cf11afSPaul Mackerras blt 4b 43114cf11afSPaul Mackerras sync 43214cf11afSPaul Mackerras addi r5,r5,8 43314cf11afSPaul Mackerras addi r6,r6,8 43414cf11afSPaul Mackerras blr 43514cf11afSPaul Mackerras 43614cf11afSPaul Mackerras.align 8 43714cf11afSPaul Mackerrascopy_to_here: 43814cf11afSPaul Mackerras 43914cf11afSPaul Mackerras#ifdef CONFIG_SMP 44014cf11afSPaul Mackerras#ifdef CONFIG_PPC_PMAC 44114cf11afSPaul Mackerras/* 44214cf11afSPaul Mackerras * On PowerMac, secondary processors starts from the reset vector, which 44314cf11afSPaul Mackerras * is temporarily turned into a call to one of the functions below. 44414cf11afSPaul Mackerras */ 44514cf11afSPaul Mackerras .section ".text"; 44614cf11afSPaul Mackerras .align 2 ; 44714cf11afSPaul Mackerras 44835499c01SPaul Mackerras .globl __secondary_start_pmac_0 44935499c01SPaul Mackerras__secondary_start_pmac_0: 45035499c01SPaul Mackerras /* NB the entries for cpus 0, 1, 2 must each occupy 8 bytes. */ 45135499c01SPaul Mackerras li r24,0 45235499c01SPaul Mackerras b 1f 45314cf11afSPaul Mackerras li r24,1 45435499c01SPaul Mackerras b 1f 45514cf11afSPaul Mackerras li r24,2 45635499c01SPaul Mackerras b 1f 45714cf11afSPaul Mackerras li r24,3 45835499c01SPaul Mackerras1: 45914cf11afSPaul Mackerras 46014cf11afSPaul Mackerras_GLOBAL(pmac_secondary_start) 46114cf11afSPaul Mackerras /* turn on 64-bit mode */ 46214cf11afSPaul Mackerras bl .enable_64b_mode 46314cf11afSPaul Mackerras 464c478b581SBenjamin Herrenschmidt li r0,0 465c478b581SBenjamin Herrenschmidt mfspr r3,SPRN_HID4 466c478b581SBenjamin Herrenschmidt rldimi r3,r0,40,23 /* clear bit 23 (rm_ci) */ 467c478b581SBenjamin Herrenschmidt sync 468c478b581SBenjamin Herrenschmidt mtspr SPRN_HID4,r3 469c478b581SBenjamin Herrenschmidt isync 470c478b581SBenjamin Herrenschmidt sync 471c478b581SBenjamin Herrenschmidt slbia 472c478b581SBenjamin Herrenschmidt 473e31aa453SPaul Mackerras /* get TOC pointer (real address) */ 474e31aa453SPaul Mackerras bl .relative_toc 475e31aa453SPaul Mackerras 47614cf11afSPaul Mackerras /* Copy some CPU settings from CPU 0 */ 477f39b7a55SOlof Johansson bl .__restore_cpu_ppc970 47814cf11afSPaul Mackerras 47914cf11afSPaul Mackerras /* pSeries do that early though I don't think we really need it */ 48014cf11afSPaul Mackerras mfmsr r3 48114cf11afSPaul Mackerras ori r3,r3,MSR_RI 48214cf11afSPaul Mackerras mtmsrd r3 /* RI on */ 48314cf11afSPaul Mackerras 48414cf11afSPaul Mackerras /* Set up a paca value for this processor. */ 485e31aa453SPaul Mackerras LOAD_REG_ADDR(r4,paca) /* Get base vaddr of paca array */ 48614cf11afSPaul Mackerras mulli r13,r24,PACA_SIZE /* Calculate vaddr of right paca */ 48714cf11afSPaul Mackerras add r13,r13,r4 /* for this processor. */ 488b5bbeb23SPaul Mackerras mtspr SPRN_SPRG3,r13 /* Save vaddr of paca in SPRG3 */ 48914cf11afSPaul Mackerras 49014cf11afSPaul Mackerras /* Create a temp kernel stack for use before relocation is on. */ 49114cf11afSPaul Mackerras ld r1,PACAEMERGSP(r13) 49214cf11afSPaul Mackerras subi r1,r1,STACK_FRAME_OVERHEAD 49314cf11afSPaul Mackerras 494c705677eSStephen Rothwell b __secondary_start 49514cf11afSPaul Mackerras 49614cf11afSPaul Mackerras#endif /* CONFIG_PPC_PMAC */ 49714cf11afSPaul Mackerras 49814cf11afSPaul Mackerras/* 49914cf11afSPaul Mackerras * This function is called after the master CPU has released the 50014cf11afSPaul Mackerras * secondary processors. The execution environment is relocation off. 50114cf11afSPaul Mackerras * The paca for this processor has the following fields initialized at 50214cf11afSPaul Mackerras * this point: 50314cf11afSPaul Mackerras * 1. Processor number 50414cf11afSPaul Mackerras * 2. Segment table pointer (virtual address) 50514cf11afSPaul Mackerras * On entry the following are set: 50614cf11afSPaul Mackerras * r1 = stack pointer. vaddr for iSeries, raddr (temp stack) for pSeries 50714cf11afSPaul Mackerras * r24 = cpu# (in Linux terms) 50814cf11afSPaul Mackerras * r13 = paca virtual address 50914cf11afSPaul Mackerras * SPRG3 = paca virtual address 51014cf11afSPaul Mackerras */ 511fc68e869SStephen Rothwell .globl __secondary_start 512c705677eSStephen Rothwell__secondary_start: 513799d6046SPaul Mackerras /* Set thread priority to MEDIUM */ 514799d6046SPaul Mackerras HMT_MEDIUM 51514cf11afSPaul Mackerras 516799d6046SPaul Mackerras /* Do early setup for that CPU (stab, slb, hash table pointer) */ 517799d6046SPaul Mackerras bl .early_setup_secondary 51814cf11afSPaul Mackerras 51914cf11afSPaul Mackerras /* Initialize the kernel stack. Just a repeat for iSeries. */ 520e58c3495SDavid Gibson LOAD_REG_ADDR(r3, current_set) 52114cf11afSPaul Mackerras sldi r28,r24,3 /* get current_set[cpu#] */ 52214cf11afSPaul Mackerras ldx r1,r3,r28 52314cf11afSPaul Mackerras addi r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD 52414cf11afSPaul Mackerras std r1,PACAKSAVE(r13) 52514cf11afSPaul Mackerras 526799d6046SPaul Mackerras /* Clear backchain so we get nice backtraces */ 52714cf11afSPaul Mackerras li r7,0 52814cf11afSPaul Mackerras mtlr r7 52914cf11afSPaul Mackerras 53014cf11afSPaul Mackerras /* enable MMU and jump to start_secondary */ 531e58c3495SDavid Gibson LOAD_REG_ADDR(r3, .start_secondary_prolog) 532e58c3495SDavid Gibson LOAD_REG_IMMEDIATE(r4, MSR_KERNEL) 533d04c56f7SPaul Mackerras#ifdef CONFIG_PPC_ISERIES 5343f639ee8SStephen RothwellBEGIN_FW_FTR_SECTION 53514cf11afSPaul Mackerras ori r4,r4,MSR_EE 536ff3da2e0SBenjamin Herrenschmidt li r8,1 537ff3da2e0SBenjamin Herrenschmidt stb r8,PACAHARDIRQEN(r13) 5383f639ee8SStephen RothwellEND_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES) 53914cf11afSPaul Mackerras#endif 540d04c56f7SPaul MackerrasBEGIN_FW_FTR_SECTION 541d04c56f7SPaul Mackerras stb r7,PACAHARDIRQEN(r13) 542d04c56f7SPaul MackerrasEND_FW_FTR_SECTION_IFCLR(FW_FEATURE_ISERIES) 543ff3da2e0SBenjamin Herrenschmidt stb r7,PACASOFTIRQEN(r13) 544d04c56f7SPaul Mackerras 545b5bbeb23SPaul Mackerras mtspr SPRN_SRR0,r3 546b5bbeb23SPaul Mackerras mtspr SPRN_SRR1,r4 54714cf11afSPaul Mackerras rfid 54814cf11afSPaul Mackerras b . /* prevent speculative execution */ 54914cf11afSPaul Mackerras 55014cf11afSPaul Mackerras/* 55114cf11afSPaul Mackerras * Running with relocation on at this point. All we want to do is 552e31aa453SPaul Mackerras * zero the stack back-chain pointer and get the TOC virtual address 553e31aa453SPaul Mackerras * before going into C code. 55414cf11afSPaul Mackerras */ 55514cf11afSPaul Mackerras_GLOBAL(start_secondary_prolog) 556e31aa453SPaul Mackerras ld r2,PACATOC(r13) 55714cf11afSPaul Mackerras li r3,0 55814cf11afSPaul Mackerras std r3,0(r1) /* Zero the stack frame pointer */ 55914cf11afSPaul Mackerras bl .start_secondary 560799d6046SPaul Mackerras b . 56114cf11afSPaul Mackerras#endif 56214cf11afSPaul Mackerras 56314cf11afSPaul Mackerras/* 56414cf11afSPaul Mackerras * This subroutine clobbers r11 and r12 56514cf11afSPaul Mackerras */ 56614cf11afSPaul Mackerras_GLOBAL(enable_64b_mode) 56714cf11afSPaul Mackerras mfmsr r11 /* grab the current MSR */ 568e31aa453SPaul Mackerras li r12,(MSR_SF | MSR_ISF)@highest 569e31aa453SPaul Mackerras sldi r12,r12,48 57014cf11afSPaul Mackerras or r11,r11,r12 57114cf11afSPaul Mackerras mtmsrd r11 57214cf11afSPaul Mackerras isync 57314cf11afSPaul Mackerras blr 57414cf11afSPaul Mackerras 57514cf11afSPaul Mackerras/* 576e31aa453SPaul Mackerras * This puts the TOC pointer into r2, offset by 0x8000 (as expected 577e31aa453SPaul Mackerras * by the toolchain). It computes the correct value for wherever we 578e31aa453SPaul Mackerras * are running at the moment, using position-independent code. 579e31aa453SPaul Mackerras */ 580e31aa453SPaul Mackerras_GLOBAL(relative_toc) 581e31aa453SPaul Mackerras mflr r0 582e31aa453SPaul Mackerras bcl 20,31,$+4 583e31aa453SPaul Mackerras0: mflr r9 584e31aa453SPaul Mackerras ld r2,(p_toc - 0b)(r9) 585e31aa453SPaul Mackerras add r2,r2,r9 586e31aa453SPaul Mackerras mtlr r0 587e31aa453SPaul Mackerras blr 588e31aa453SPaul Mackerras 589e31aa453SPaul Mackerrasp_toc: .llong __toc_start + 0x8000 - 0b 590e31aa453SPaul Mackerras 591e31aa453SPaul Mackerras/* 59214cf11afSPaul Mackerras * This is where the main kernel code starts. 59314cf11afSPaul Mackerras */ 594939e60f6SStephen Rothwell_INIT_STATIC(start_here_multiplatform) 595e31aa453SPaul Mackerras /* set up the TOC (real address) */ 596e31aa453SPaul Mackerras bl .relative_toc 59714cf11afSPaul Mackerras 59814cf11afSPaul Mackerras /* Clear out the BSS. It may have been done in prom_init, 59914cf11afSPaul Mackerras * already but that's irrelevant since prom_init will soon 60014cf11afSPaul Mackerras * be detached from the kernel completely. Besides, we need 60114cf11afSPaul Mackerras * to clear it now for kexec-style entry. 60214cf11afSPaul Mackerras */ 603e31aa453SPaul Mackerras LOAD_REG_ADDR(r11,__bss_stop) 604e31aa453SPaul Mackerras LOAD_REG_ADDR(r8,__bss_start) 60514cf11afSPaul Mackerras sub r11,r11,r8 /* bss size */ 60614cf11afSPaul Mackerras addi r11,r11,7 /* round up to an even double word */ 607e31aa453SPaul Mackerras srdi. r11,r11,3 /* shift right by 3 */ 60814cf11afSPaul Mackerras beq 4f 60914cf11afSPaul Mackerras addi r8,r8,-8 61014cf11afSPaul Mackerras li r0,0 61114cf11afSPaul Mackerras mtctr r11 /* zero this many doublewords */ 61214cf11afSPaul Mackerras3: stdu r0,8(r8) 61314cf11afSPaul Mackerras bdnz 3b 61414cf11afSPaul Mackerras4: 61514cf11afSPaul Mackerras 61614cf11afSPaul Mackerras mfmsr r6 61714cf11afSPaul Mackerras ori r6,r6,MSR_RI 61814cf11afSPaul Mackerras mtmsrd r6 /* RI on */ 61914cf11afSPaul Mackerras 620549e8152SPaul Mackerras#ifdef CONFIG_RELOCATABLE 621549e8152SPaul Mackerras /* Save the physical address we're running at in kernstart_addr */ 622549e8152SPaul Mackerras LOAD_REG_ADDR(r4, kernstart_addr) 623549e8152SPaul Mackerras clrldi r0,r25,2 624549e8152SPaul Mackerras std r0,0(r4) 625549e8152SPaul Mackerras#endif 626549e8152SPaul Mackerras 627e31aa453SPaul Mackerras /* The following gets the stack set up with the regs */ 62814cf11afSPaul Mackerras /* pointing to the real addr of the kernel stack. This is */ 62914cf11afSPaul Mackerras /* all done to support the C function call below which sets */ 63014cf11afSPaul Mackerras /* up the htab. This is done because we have relocated the */ 63114cf11afSPaul Mackerras /* kernel but are still running in real mode. */ 63214cf11afSPaul Mackerras 633e31aa453SPaul Mackerras LOAD_REG_ADDR(r3,init_thread_union) 63414cf11afSPaul Mackerras 635e31aa453SPaul Mackerras /* set up a stack pointer */ 63614cf11afSPaul Mackerras addi r1,r3,THREAD_SIZE 63714cf11afSPaul Mackerras li r0,0 63814cf11afSPaul Mackerras stdu r0,-STACK_FRAME_OVERHEAD(r1) 63914cf11afSPaul Mackerras 64014cf11afSPaul Mackerras /* Do very early kernel initializations, including initial hash table, 64114cf11afSPaul Mackerras * stab and slb setup before we turn on relocation. */ 64214cf11afSPaul Mackerras 64314cf11afSPaul Mackerras /* Restore parameters passed from prom_init/kexec */ 64414cf11afSPaul Mackerras mr r3,r31 645e31aa453SPaul Mackerras bl .early_setup /* also sets r13 and SPRG3 */ 64614cf11afSPaul Mackerras 647e31aa453SPaul Mackerras LOAD_REG_ADDR(r3, .start_here_common) 648e31aa453SPaul Mackerras ld r4,PACAKMSR(r13) 649b5bbeb23SPaul Mackerras mtspr SPRN_SRR0,r3 650b5bbeb23SPaul Mackerras mtspr SPRN_SRR1,r4 65114cf11afSPaul Mackerras rfid 65214cf11afSPaul Mackerras b . /* prevent speculative execution */ 65314cf11afSPaul Mackerras 65414cf11afSPaul Mackerras /* This is where all platforms converge execution */ 655fc68e869SStephen Rothwell_INIT_GLOBAL(start_here_common) 65614cf11afSPaul Mackerras /* relocation is on at this point */ 65714cf11afSPaul Mackerras std r1,PACAKSAVE(r13) 65814cf11afSPaul Mackerras 659e31aa453SPaul Mackerras /* Load the TOC (virtual address) */ 660e31aa453SPaul Mackerras ld r2,PACATOC(r13) 661e31aa453SPaul Mackerras 66214cf11afSPaul Mackerras bl .setup_system 66314cf11afSPaul Mackerras 66414cf11afSPaul Mackerras /* Load up the kernel context */ 66514cf11afSPaul Mackerras5: 66614cf11afSPaul Mackerras li r5,0 667d04c56f7SPaul Mackerras stb r5,PACASOFTIRQEN(r13) /* Soft Disabled */ 668d04c56f7SPaul Mackerras#ifdef CONFIG_PPC_ISERIES 669d04c56f7SPaul MackerrasBEGIN_FW_FTR_SECTION 67014cf11afSPaul Mackerras mfmsr r5 671ff3da2e0SBenjamin Herrenschmidt ori r5,r5,MSR_EE /* Hard Enabled on iSeries*/ 67214cf11afSPaul Mackerras mtmsrd r5 673ff3da2e0SBenjamin Herrenschmidt li r5,1 6743f639ee8SStephen RothwellEND_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES) 67514cf11afSPaul Mackerras#endif 676ff3da2e0SBenjamin Herrenschmidt stb r5,PACAHARDIRQEN(r13) /* Hard Disabled on others */ 67714cf11afSPaul Mackerras 67814cf11afSPaul Mackerras bl .start_kernel 67914cf11afSPaul Mackerras 680f1870f77SAnton Blanchard /* Not reached */ 681f1870f77SAnton Blanchard BUG_OPCODE 68214cf11afSPaul Mackerras 68314cf11afSPaul Mackerras/* 68414cf11afSPaul Mackerras * We put a few things here that have to be page-aligned. 68514cf11afSPaul Mackerras * This stuff goes at the beginning of the bss, which is page-aligned. 68614cf11afSPaul Mackerras */ 68714cf11afSPaul Mackerras .section ".bss" 68814cf11afSPaul Mackerras 68914cf11afSPaul Mackerras .align PAGE_SHIFT 69014cf11afSPaul Mackerras 69114cf11afSPaul Mackerras .globl empty_zero_page 69214cf11afSPaul Mackerrasempty_zero_page: 69314cf11afSPaul Mackerras .space PAGE_SIZE 69414cf11afSPaul Mackerras 69514cf11afSPaul Mackerras .globl swapper_pg_dir 69614cf11afSPaul Mackerrasswapper_pg_dir: 697ee7a76daSStephen Rothwell .space PGD_TABLE_SIZE 698