/*1* PowerPC version2* Copyright (C) 1995-1996 Gary Thomas ([email protected])3*4* Rewritten by Cort Dougan ([email protected]) for PReP5* Copyright (C) 1996 Cort Dougan <[email protected]>6* Adapted for Power Macintosh by Paul Mackerras.7* Low-level exception handlers and MMU support8* rewritten by Paul Mackerras.9* Copyright (C) 1996 Paul Mackerras.10*11* Adapted for 64bit PowerPC by Dave Engebretsen, Peter Bergner, and12* Mike Corrigan {engebret|bergner|mikejc}@us.ibm.com13*14* This file contains the entry point for the 64-bit kernel along15* with some early initialization code common to all 64-bit powerpc16* variants.17*18* This program is free software; you can redistribute it and/or19* modify it under the terms of the GNU General Public License20* as published by the Free Software Foundation; either version21* 2 of the License, or (at your option) any later version.22*/2324#include <linux/threads.h>25#include <asm/reg.h>26#include <asm/page.h>27#include <asm/mmu.h>28#include <asm/ppc_asm.h>29#include <asm/asm-offsets.h>30#include <asm/bug.h>31#include <asm/cputable.h>32#include <asm/setup.h>33#include <asm/hvcall.h>34#include <asm/iseries/lpar_map.h>35#include <asm/thread_info.h>36#include <asm/firmware.h>37#include <asm/page_64.h>38#include <asm/irqflags.h>39#include <asm/kvm_book3s_asm.h>40#include <asm/ptrace.h>4142/* The physical memory is laid out such that the secondary processor43* spin code sits at 0x0000...0x00ff. On server, the vectors follow44* using the layout described in exceptions-64s.S45*/4647/*48* Entering into this code we make the following assumptions:49*50* For pSeries or server processors:51* 1. The MMU is off & open firmware is running in real mode.52* 2. The kernel is entered at __start53*54* For iSeries:55* 1. The MMU is on (as it always is for iSeries)56* 2. The kernel is entered at system_reset_iSeries57*58* For Book3E processors:59* 1. The MMU is on running in AS0 in a state defined in ePAPR60* 2. The kernel is entered at __start61*/6263.text64.globl _stext65_stext:66_GLOBAL(__start)67/* NOP this out unconditionally */68BEGIN_FTR_SECTION69b .__start_initialization_multiplatform70END_FTR_SECTION(0, 1)7172/* Catch branch to 0 in real mode */73trap7475/* Secondary processors spin on this value until it becomes nonzero.76* When it does it contains the real address of the descriptor77* of the function that the cpu should jump to to continue78* initialization.79*/80.globl __secondary_hold_spinloop81__secondary_hold_spinloop:82.llong 0x08384/* Secondary processors write this value with their cpu # */85/* after they enter the spin loop immediately below. */86.globl __secondary_hold_acknowledge87__secondary_hold_acknowledge:88.llong 0x08990#ifdef CONFIG_PPC_ISERIES91/*92* At offset 0x20, there is a pointer to iSeries LPAR data.93* This is required by the hypervisor94*/95. = 0x2096.llong hvReleaseData-KERNELBASE97#endif /* CONFIG_PPC_ISERIES */9899#ifdef CONFIG_RELOCATABLE100/* This flag is set to 1 by a loader if the kernel should run101* at the loaded address instead of the linked address. This102* is used by kexec-tools to keep the the kdump kernel in the103* crash_kernel region. The loader is responsible for104* observing the alignment requirement.105*/106/* Do not move this variable as kexec-tools knows about it. */107. = 0x5c108.globl __run_at_load109__run_at_load:110.long 0x72756e30 /* "run0" -- relocate to 0 by default */111#endif112113. = 0x60114/*115* The following code is used to hold secondary processors116* in a spin loop after they have entered the kernel, but117* before the bulk of the kernel has been relocated. This code118* is relocated to physical address 0x60 before prom_init is run.119* All of it must fit below the first exception vector at 0x100.120* Use .globl here not _GLOBAL because we want __secondary_hold121* to be the actual text address, not a descriptor.122*/123.globl __secondary_hold124__secondary_hold:125#ifndef CONFIG_PPC_BOOK3E126mfmsr r24127ori r24,r24,MSR_RI128mtmsrd r24 /* RI on */129#endif130/* Grab our physical cpu number */131mr r24,r3132133/* Tell the master cpu we're here */134/* Relocation is off & we are located at an address less */135/* than 0x100, so only need to grab low order offset. */136std r24,__secondary_hold_acknowledge-_stext(0)137sync138139/* All secondary cpus wait here until told to start. */140100: ld r4,__secondary_hold_spinloop-_stext(0)141cmpdi 0,r4,0142beq 100b143144#if defined(CONFIG_SMP) || defined(CONFIG_KEXEC)145ld r4,0(r4) /* deref function descriptor */146mtctr r4147mr r3,r24148li r4,0149/* Make sure that patched code is visible */150isync151bctr152#else153BUG_OPCODE154#endif155156/* This value is used to mark exception frames on the stack. */157.section ".toc","aw"158exception_marker:159.tc ID_72656773_68657265[TC],0x7265677368657265160.text161162/*163* On server, we include the exception vectors code here as it164* relies on absolute addressing which is only possible within165* this compilation unit166*/167#ifdef CONFIG_PPC_BOOK3S168#include "exceptions-64s.S"169#endif170171_GLOBAL(generic_secondary_thread_init)172mr r24,r3173174/* turn on 64-bit mode */175bl .enable_64b_mode176177/* get a valid TOC pointer, wherever we're mapped at */178bl .relative_toc179180#ifdef CONFIG_PPC_BOOK3E181/* Book3E initialization */182mr r3,r24183bl .book3e_secondary_thread_init184#endif185b generic_secondary_common_init186187/*188* On pSeries and most other platforms, secondary processors spin189* in the following code.190* At entry, r3 = this processor's number (physical cpu id)191*192* On Book3E, r4 = 1 to indicate that the initial TLB entry for193* this core already exists (setup via some other mechanism such194* as SCOM before entry).195*/196_GLOBAL(generic_secondary_smp_init)197mr r24,r3198mr r25,r4199200/* turn on 64-bit mode */201bl .enable_64b_mode202203/* get a valid TOC pointer, wherever we're mapped at */204bl .relative_toc205206#ifdef CONFIG_PPC_BOOK3E207/* Book3E initialization */208mr r3,r24209mr r4,r25210bl .book3e_secondary_core_init211#endif212213generic_secondary_common_init:214/* Set up a paca value for this processor. Since we have the215* physical cpu id in r24, we need to search the pacas to find216* which logical id maps to our physical one.217*/218LOAD_REG_ADDR(r13, paca) /* Load paca pointer */219ld r13,0(r13) /* Get base vaddr of paca array */220#ifndef CONFIG_SMP221addi r13,r13,PACA_SIZE /* know r13 if used accidentally */222b .kexec_wait /* wait for next kernel if !SMP */223#else224LOAD_REG_ADDR(r7, nr_cpu_ids) /* Load nr_cpu_ids address */225lwz r7,0(r7) /* also the max paca allocated */226li r5,0 /* logical cpu id */2271: lhz r6,PACAHWCPUID(r13) /* Load HW procid from paca */228cmpw r6,r24 /* Compare to our id */229beq 2f230addi r13,r13,PACA_SIZE /* Loop to next PACA on miss */231addi r5,r5,1232cmpw r5,r7 /* Check if more pacas exist */233blt 1b234235mr r3,r24 /* not found, copy phys to r3 */236b .kexec_wait /* next kernel might do better */2372382: SET_PACA(r13)239#ifdef CONFIG_PPC_BOOK3E240addi r12,r13,PACA_EXTLB /* and TLB exc frame in another */241mtspr SPRN_SPRG_TLB_EXFRAME,r12242#endif243244/* From now on, r24 is expected to be logical cpuid */245mr r24,r5246247/* See if we need to call a cpu state restore handler */248LOAD_REG_ADDR(r23, cur_cpu_spec)249ld r23,0(r23)250ld r23,CPU_SPEC_RESTORE(r23)251cmpdi 0,r23,0252beq 3f253ld r23,0(r23)254mtctr r23255bctrl2562573: LOAD_REG_ADDR(r3, boot_cpu_count) /* Decrement boot_cpu_count */258lwarx r4,0,r3259subi r4,r4,1260stwcx. r4,0,r3261bne 3b262isync2632644: HMT_LOW265lbz r23,PACAPROCSTART(r13) /* Test if this processor should */266/* start. */267cmpwi 0,r23,0268beq 4b /* Loop until told to go */269270sync /* order paca.run and cur_cpu_spec */271isync /* In case code patching happened */272273/* Create a temp kernel stack for use before relocation is on. */274ld r1,PACAEMERGSP(r13)275subi r1,r1,STACK_FRAME_OVERHEAD276277b __secondary_start278#endif /* SMP */279280/*281* Turn the MMU off.282* Assumes we're mapped EA == RA if the MMU is on.283*/284#ifdef CONFIG_PPC_BOOK3S285_STATIC(__mmu_off)286mfmsr r3287andi. r0,r3,MSR_IR|MSR_DR288beqlr289mflr r4290andc r3,r3,r0291mtspr SPRN_SRR0,r4292mtspr SPRN_SRR1,r3293sync294rfid295b . /* prevent speculative execution */296#endif297298299/*300* Here is our main kernel entry point. We support currently 2 kind of entries301* depending on the value of r5.302*303* r5 != NULL -> OF entry, we go to prom_init, "legacy" parameter content304* in r3...r7305*306* r5 == NULL -> kexec style entry. r3 is a physical pointer to the307* DT block, r4 is a physical pointer to the kernel itself308*309*/310_GLOBAL(__start_initialization_multiplatform)311/* Make sure we are running in 64 bits mode */312bl .enable_64b_mode313314/* Get TOC pointer (current runtime address) */315bl .relative_toc316317/* find out where we are now */318bcl 20,31,$+43190: mflr r26 /* r26 = runtime addr here */320addis r26,r26,(_stext - 0b)@ha321addi r26,r26,(_stext - 0b)@l /* current runtime base addr */322323/*324* Are we booted from a PROM Of-type client-interface ?325*/326cmpldi cr0,r5,0327beq 1f328b .__boot_from_prom /* yes -> prom */3291:330/* Save parameters */331mr r31,r3332mr r30,r4333334#ifdef CONFIG_PPC_BOOK3E335bl .start_initialization_book3e336b .__after_prom_start337#else338/* Setup some critical 970 SPRs before switching MMU off */339mfspr r0,SPRN_PVR340srwi r0,r0,16341cmpwi r0,0x39 /* 970 */342beq 1f343cmpwi r0,0x3c /* 970FX */344beq 1f345cmpwi r0,0x44 /* 970MP */346beq 1f347cmpwi r0,0x45 /* 970GX */348bne 2f3491: bl .__cpu_preinit_ppc9703502:351352/* Switch off MMU if not already off */353bl .__mmu_off354b .__after_prom_start355#endif /* CONFIG_PPC_BOOK3E */356357_INIT_STATIC(__boot_from_prom)358#ifdef CONFIG_PPC_OF_BOOT_TRAMPOLINE359/* Save parameters */360mr r31,r3361mr r30,r4362mr r29,r5363mr r28,r6364mr r27,r7365366/*367* Align the stack to 16-byte boundary368* Depending on the size and layout of the ELF sections in the initial369* boot binary, the stack pointer may be unaligned on PowerMac370*/371rldicr r1,r1,0,59372373#ifdef CONFIG_RELOCATABLE374/* Relocate code for where we are now */375mr r3,r26376bl .relocate377#endif378379/* Restore parameters */380mr r3,r31381mr r4,r30382mr r5,r29383mr r6,r28384mr r7,r27385386/* Do all of the interaction with OF client interface */387mr r8,r26388bl .prom_init389#endif /* #CONFIG_PPC_OF_BOOT_TRAMPOLINE */390391/* We never return. We also hit that trap if trying to boot392* from OF while CONFIG_PPC_OF_BOOT_TRAMPOLINE isn't selected */393trap394395_STATIC(__after_prom_start)396#ifdef CONFIG_RELOCATABLE397/* process relocations for the final address of the kernel */398lis r25,PAGE_OFFSET@highest /* compute virtual base of kernel */399sldi r25,r25,32400lwz r7,__run_at_load-_stext(r26)401cmplwi cr0,r7,1 /* flagged to stay where we are ? */402bne 1f403add r25,r25,r264041: mr r3,r25405bl .relocate406#endif407408/*409* We need to run with _stext at physical address PHYSICAL_START.410* This will leave some code in the first 256B of411* real memory, which are reserved for software use.412*413* Note: This process overwrites the OF exception vectors.414*/415li r3,0 /* target addr */416#ifdef CONFIG_PPC_BOOK3E417tovirt(r3,r3) /* on booke, we already run at PAGE_OFFSET */418#endif419mr. r4,r26 /* In some cases the loader may */420beq 9f /* have already put us at zero */421li r6,0x100 /* Start offset, the first 0x100 */422/* bytes were copied earlier. */423#ifdef CONFIG_PPC_BOOK3E424tovirt(r6,r6) /* on booke, we already run at PAGE_OFFSET */425#endif426427#ifdef CONFIG_CRASH_DUMP428/*429* Check if the kernel has to be running as relocatable kernel based on the430* variable __run_at_load, if it is set the kernel is treated as relocatable431* kernel, otherwise it will be moved to PHYSICAL_START432*/433lwz r7,__run_at_load-_stext(r26)434cmplwi cr0,r7,1435bne 3f436437li r5,__end_interrupts - _stext /* just copy interrupts */438b 5f4393:440#endif441lis r5,(copy_to_here - _stext)@ha442addi r5,r5,(copy_to_here - _stext)@l /* # bytes of memory to copy */443444bl .copy_and_flush /* copy the first n bytes */445/* this includes the code being */446/* executed here. */447addis r8,r3,(4f - _stext)@ha /* Jump to the copy of this code */448addi r8,r8,(4f - _stext)@l /* that we just made */449mtctr r8450bctr451452p_end: .llong _end - _stext4534544: /* Now copy the rest of the kernel up to _end */455addis r5,r26,(p_end - _stext)@ha456ld r5,(p_end - _stext)@l(r5) /* get _end */4575: bl .copy_and_flush /* copy the rest */4584599: b .start_here_multiplatform460461/*462* Copy routine used to copy the kernel to start at physical address 0463* and flush and invalidate the caches as needed.464* r3 = dest addr, r4 = source addr, r5 = copy limit, r6 = start offset465* on exit, r3, r4, r5 are unchanged, r6 is updated to be >= r5.466*467* Note: this routine *only* clobbers r0, r6 and lr468*/469_GLOBAL(copy_and_flush)470addi r5,r5,-8471addi r6,r6,-84724: li r0,8 /* Use the smallest common */473/* denominator cache line */474/* size. This results in */475/* extra cache line flushes */476/* but operation is correct. */477/* Can't get cache line size */478/* from NACA as it is being */479/* moved too. */480481mtctr r0 /* put # words/line in ctr */4823: addi r6,r6,8 /* copy a cache line */483ldx r0,r6,r4484stdx r0,r6,r3485bdnz 3b486dcbst r6,r3 /* write it to memory */487sync488icbi r6,r3 /* flush the icache line */489cmpld 0,r6,r5490blt 4b491sync492addi r5,r5,8493addi r6,r6,8494blr495496.align 8497copy_to_here:498499#ifdef CONFIG_SMP500#ifdef CONFIG_PPC_PMAC501/*502* On PowerMac, secondary processors starts from the reset vector, which503* is temporarily turned into a call to one of the functions below.504*/505.section ".text";506.align 2 ;507508.globl __secondary_start_pmac_0509__secondary_start_pmac_0:510/* NB the entries for cpus 0, 1, 2 must each occupy 8 bytes. */511li r24,0512b 1f513li r24,1514b 1f515li r24,2516b 1f517li r24,35181:519520_GLOBAL(pmac_secondary_start)521/* turn on 64-bit mode */522bl .enable_64b_mode523524li r0,0525mfspr r3,SPRN_HID4526rldimi r3,r0,40,23 /* clear bit 23 (rm_ci) */527sync528mtspr SPRN_HID4,r3529isync530sync531slbia532533/* get TOC pointer (real address) */534bl .relative_toc535536/* Copy some CPU settings from CPU 0 */537bl .__restore_cpu_ppc970538539/* pSeries do that early though I don't think we really need it */540mfmsr r3541ori r3,r3,MSR_RI542mtmsrd r3 /* RI on */543544/* Set up a paca value for this processor. */545LOAD_REG_ADDR(r4,paca) /* Load paca pointer */546ld r4,0(r4) /* Get base vaddr of paca array */547mulli r13,r24,PACA_SIZE /* Calculate vaddr of right paca */548add r13,r13,r4 /* for this processor. */549SET_PACA(r13) /* Save vaddr of paca in an SPRG*/550551/* Mark interrupts soft and hard disabled (they might be enabled552* in the PACA when doing hotplug)553*/554li r0,0555stb r0,PACASOFTIRQEN(r13)556stb r0,PACAHARDIRQEN(r13)557558/* Create a temp kernel stack for use before relocation is on. */559ld r1,PACAEMERGSP(r13)560subi r1,r1,STACK_FRAME_OVERHEAD561562b __secondary_start563564#endif /* CONFIG_PPC_PMAC */565566/*567* This function is called after the master CPU has released the568* secondary processors. The execution environment is relocation off.569* The paca for this processor has the following fields initialized at570* this point:571* 1. Processor number572* 2. Segment table pointer (virtual address)573* On entry the following are set:574* r1 = stack pointer. vaddr for iSeries, raddr (temp stack) for pSeries575* r24 = cpu# (in Linux terms)576* r13 = paca virtual address577* SPRG_PACA = paca virtual address578*/579.section ".text";580.align 2 ;581582.globl __secondary_start583__secondary_start:584/* Set thread priority to MEDIUM */585HMT_MEDIUM586587/* Initialize the kernel stack. Just a repeat for iSeries. */588LOAD_REG_ADDR(r3, current_set)589sldi r28,r24,3 /* get current_set[cpu#] */590ldx r14,r3,r28591addi r14,r14,THREAD_SIZE-STACK_FRAME_OVERHEAD592std r14,PACAKSAVE(r13)593594/* Do early setup for that CPU (stab, slb, hash table pointer) */595bl .early_setup_secondary596597/*598* setup the new stack pointer, but *don't* use this until599* translation is on.600*/601mr r1, r14602603/* Clear backchain so we get nice backtraces */604li r7,0605mtlr r7606607/* enable MMU and jump to start_secondary */608LOAD_REG_ADDR(r3, .start_secondary_prolog)609LOAD_REG_IMMEDIATE(r4, MSR_KERNEL)610#ifdef CONFIG_PPC_ISERIES611BEGIN_FW_FTR_SECTION612ori r4,r4,MSR_EE613li r8,1614stb r8,PACAHARDIRQEN(r13)615END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)616#endif617BEGIN_FW_FTR_SECTION618stb r7,PACAHARDIRQEN(r13)619END_FW_FTR_SECTION_IFCLR(FW_FEATURE_ISERIES)620stb r7,PACASOFTIRQEN(r13)621622mtspr SPRN_SRR0,r3623mtspr SPRN_SRR1,r4624RFI625b . /* prevent speculative execution */626627/*628* Running with relocation on at this point. All we want to do is629* zero the stack back-chain pointer and get the TOC virtual address630* before going into C code.631*/632_GLOBAL(start_secondary_prolog)633ld r2,PACATOC(r13)634li r3,0635std r3,0(r1) /* Zero the stack frame pointer */636bl .start_secondary637b .638/*639* Reset stack pointer and call start_secondary640* to continue with online operation when woken up641* from cede in cpu offline.642*/643_GLOBAL(start_secondary_resume)644ld r1,PACAKSAVE(r13) /* Reload kernel stack pointer */645li r3,0646std r3,0(r1) /* Zero the stack frame pointer */647bl .start_secondary648b .649#endif650651/*652* This subroutine clobbers r11 and r12653*/654_GLOBAL(enable_64b_mode)655mfmsr r11 /* grab the current MSR */656#ifdef CONFIG_PPC_BOOK3E657oris r11,r11,0x8000 /* CM bit set, we'll set ICM later */658mtmsr r11659#else /* CONFIG_PPC_BOOK3E */660li r12,(MSR_64BIT | MSR_ISF)@highest661sldi r12,r12,48662or r11,r11,r12663mtmsrd r11664isync665#endif666blr667668/*669* This puts the TOC pointer into r2, offset by 0x8000 (as expected670* by the toolchain). It computes the correct value for wherever we671* are running at the moment, using position-independent code.672*/673_GLOBAL(relative_toc)674mflr r0675bcl 20,31,$+46760: mflr r9677ld r2,(p_toc - 0b)(r9)678add r2,r2,r9679mtlr r0680blr681682p_toc: .llong __toc_start + 0x8000 - 0b683684/*685* This is where the main kernel code starts.686*/687_INIT_STATIC(start_here_multiplatform)688/* set up the TOC (real address) */689bl .relative_toc690691/* Clear out the BSS. It may have been done in prom_init,692* already but that's irrelevant since prom_init will soon693* be detached from the kernel completely. Besides, we need694* to clear it now for kexec-style entry.695*/696LOAD_REG_ADDR(r11,__bss_stop)697LOAD_REG_ADDR(r8,__bss_start)698sub r11,r11,r8 /* bss size */699addi r11,r11,7 /* round up to an even double word */700srdi. r11,r11,3 /* shift right by 3 */701beq 4f702addi r8,r8,-8703li r0,0704mtctr r11 /* zero this many doublewords */7053: stdu r0,8(r8)706bdnz 3b7074:708709#ifndef CONFIG_PPC_BOOK3E710mfmsr r6711ori r6,r6,MSR_RI712mtmsrd r6 /* RI on */713#endif714715#ifdef CONFIG_RELOCATABLE716/* Save the physical address we're running at in kernstart_addr */717LOAD_REG_ADDR(r4, kernstart_addr)718clrldi r0,r25,2719std r0,0(r4)720#endif721722/* The following gets the stack set up with the regs */723/* pointing to the real addr of the kernel stack. This is */724/* all done to support the C function call below which sets */725/* up the htab. This is done because we have relocated the */726/* kernel but are still running in real mode. */727728LOAD_REG_ADDR(r3,init_thread_union)729730/* set up a stack pointer */731addi r1,r3,THREAD_SIZE732li r0,0733stdu r0,-STACK_FRAME_OVERHEAD(r1)734735/* Do very early kernel initializations, including initial hash table,736* stab and slb setup before we turn on relocation. */737738/* Restore parameters passed from prom_init/kexec */739mr r3,r31740bl .early_setup /* also sets r13 and SPRG_PACA */741742LOAD_REG_ADDR(r3, .start_here_common)743ld r4,PACAKMSR(r13)744mtspr SPRN_SRR0,r3745mtspr SPRN_SRR1,r4746RFI747b . /* prevent speculative execution */748749/* This is where all platforms converge execution */750_INIT_GLOBAL(start_here_common)751/* relocation is on at this point */752std r1,PACAKSAVE(r13)753754/* Load the TOC (virtual address) */755ld r2,PACATOC(r13)756757bl .setup_system758759/* Load up the kernel context */7605:761li r5,0762stb r5,PACASOFTIRQEN(r13) /* Soft Disabled */763#ifdef CONFIG_PPC_ISERIES764BEGIN_FW_FTR_SECTION765mfmsr r5766ori r5,r5,MSR_EE /* Hard Enabled on iSeries*/767mtmsrd r5768li r5,1769END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)770#endif771stb r5,PACAHARDIRQEN(r13) /* Hard Disabled on others */772773bl .start_kernel774775/* Not reached */776BUG_OPCODE777778/*779* We put a few things here that have to be page-aligned.780* This stuff goes at the beginning of the bss, which is page-aligned.781*/782.section ".bss"783784.align PAGE_SHIFT785786.globl empty_zero_page787empty_zero_page:788.space PAGE_SIZE789790.globl swapper_pg_dir791swapper_pg_dir:792.space PGD_TABLE_SIZE793794795