1*2874c5fdSThomas Gleixner/* SPDX-License-Identifier: GPL-2.0-or-later */ 214cf11afSPaul Mackerras/* 314cf11afSPaul Mackerras * PowerPC version 414cf11afSPaul Mackerras * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) 514cf11afSPaul Mackerras * 614cf11afSPaul Mackerras * Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP 714cf11afSPaul Mackerras * Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu> 814cf11afSPaul Mackerras * Adapted for Power Macintosh by Paul Mackerras. 914cf11afSPaul Mackerras * Low-level exception handlers and MMU support 1014cf11afSPaul Mackerras * rewritten by Paul Mackerras. 1114cf11afSPaul Mackerras * Copyright (C) 1996 Paul Mackerras. 1214cf11afSPaul Mackerras * 1314cf11afSPaul Mackerras * Adapted for 64bit PowerPC by Dave Engebretsen, Peter Bergner, and 1414cf11afSPaul Mackerras * Mike Corrigan {engebret|bergner|mikejc}@us.ibm.com 1514cf11afSPaul Mackerras * 160ebc4cdaSBenjamin Herrenschmidt * This file contains the entry point for the 64-bit kernel along 170ebc4cdaSBenjamin Herrenschmidt * with some early initialization code common to all 64-bit powerpc 180ebc4cdaSBenjamin Herrenschmidt * variants. 1914cf11afSPaul Mackerras */ 2014cf11afSPaul Mackerras 2114cf11afSPaul Mackerras#include <linux/threads.h> 22c141611fSPaul Gortmaker#include <linux/init.h> 23b5bbeb23SPaul Mackerras#include <asm/reg.h> 2414cf11afSPaul Mackerras#include <asm/page.h> 2514cf11afSPaul Mackerras#include <asm/mmu.h> 2614cf11afSPaul Mackerras#include <asm/ppc_asm.h> 2757f26649SNicholas Piggin#include <asm/head-64.h> 2814cf11afSPaul Mackerras#include <asm/asm-offsets.h> 2914cf11afSPaul Mackerras#include <asm/bug.h> 3014cf11afSPaul Mackerras#include <asm/cputable.h> 3114cf11afSPaul Mackerras#include <asm/setup.h> 3214cf11afSPaul Mackerras#include <asm/hvcall.h> 336cb7bfebSDavid Gibson#include <asm/thread_info.h> 343f639ee8SStephen Rothwell#include <asm/firmware.h> 3516a15a30SStephen Rothwell#include <asm/page_64.h> 36945feb17SBenjamin Herrenschmidt#include <asm/irqflags.h> 372191d657SAlexander Graf#include <asm/kvm_book3s_asm.h> 3846f52210SStephen Rothwell#include <asm/ptrace.h> 397230c564SBenjamin Herrenschmidt#include <asm/hw_irq.h> 406becef7eSchenhui zhao#include <asm/cputhreads.h> 417a25d912SScott Wood#include <asm/ppc-opcode.h> 429445aa1aSAl Viro#include <asm/export.h> 432c86cd18SChristophe Leroy#include <asm/feature-fixups.h> 4414cf11afSPaul Mackerras 4525985edcSLucas De Marchi/* The physical memory is laid out such that the secondary processor 460ebc4cdaSBenjamin Herrenschmidt * spin code sits at 0x0000...0x00ff. On server, the vectors follow 470ebc4cdaSBenjamin Herrenschmidt * using the layout described in exceptions-64s.S 4814cf11afSPaul Mackerras */ 4914cf11afSPaul Mackerras 5014cf11afSPaul Mackerras/* 5114cf11afSPaul Mackerras * Entering into this code we make the following assumptions: 520ebc4cdaSBenjamin Herrenschmidt * 530ebc4cdaSBenjamin Herrenschmidt * For pSeries or server processors: 5414cf11afSPaul Mackerras * 1. The MMU is off & open firmware is running in real mode. 55339a3293SNicholas Piggin * 2. The primary CPU enters at __start. 56339a3293SNicholas Piggin * 3. If the RTAS supports "query-cpu-stopped-state", then secondary 57339a3293SNicholas Piggin * CPUs will enter as directed by "start-cpu" RTAS call, which is 58339a3293SNicholas Piggin * generic_secondary_smp_init, with PIR in r3. 59339a3293SNicholas Piggin * 4. Else the secondary CPUs will enter at secondary_hold (0x60) as 60339a3293SNicholas Piggin * directed by the "start-cpu" RTS call, with PIR in r3. 6127f44888SBenjamin Herrenschmidt * -or- For OPAL entry: 62339a3293SNicholas Piggin * 1. The MMU is off, processor in HV mode. 63339a3293SNicholas Piggin * 2. The primary CPU enters at 0 with device-tree in r3, OPAL base 64339a3293SNicholas Piggin * in r8, and entry in r9 for debugging purposes. 65339a3293SNicholas Piggin * 3. Secondary CPUs enter as directed by OPAL_START_CPU call, which 66339a3293SNicholas Piggin * is at generic_secondary_smp_init, with PIR in r3. 6714cf11afSPaul Mackerras * 680ebc4cdaSBenjamin Herrenschmidt * For Book3E processors: 690ebc4cdaSBenjamin Herrenschmidt * 1. The MMU is on running in AS0 in a state defined in ePAPR 700ebc4cdaSBenjamin Herrenschmidt * 2. The kernel is entered at __start 7114cf11afSPaul Mackerras */ 7214cf11afSPaul Mackerras 7357f26649SNicholas PigginOPEN_FIXED_SECTION(first_256B, 0x0, 0x100) 7457f26649SNicholas PigginUSE_FIXED_SECTION(first_256B) 7557f26649SNicholas Piggin /* 7657f26649SNicholas Piggin * Offsets are relative from the start of fixed section, and 7757f26649SNicholas Piggin * first_256B starts at 0. Offsets are a bit easier to use here 7857f26649SNicholas Piggin * than the fixed section entry macros. 7957f26649SNicholas Piggin */ 8057f26649SNicholas Piggin . = 0x0 8114cf11afSPaul Mackerras_GLOBAL(__start) 8214cf11afSPaul Mackerras /* NOP this out unconditionally */ 8314cf11afSPaul MackerrasBEGIN_FTR_SECTION 845c0484e2SBenjamin Herrenschmidt FIXUP_ENDIAN 85b1576fecSAnton Blanchard b __start_initialization_multiplatform 8614cf11afSPaul MackerrasEND_FTR_SECTION(0, 1) 8714cf11afSPaul Mackerras 8814cf11afSPaul Mackerras /* Catch branch to 0 in real mode */ 8914cf11afSPaul Mackerras trap 9014cf11afSPaul Mackerras 912751b628SAnton Blanchard /* Secondary processors spin on this value until it becomes non-zero. 922751b628SAnton Blanchard * When non-zero, it contains the real address of the function the cpu 932751b628SAnton Blanchard * should jump to. 941f6a93e4SPaul Mackerras */ 957d4151b5SOlof Johansson .balign 8 9614cf11afSPaul Mackerras .globl __secondary_hold_spinloop 9714cf11afSPaul Mackerras__secondary_hold_spinloop: 98eb039161STobin C. Harding .8byte 0x0 9914cf11afSPaul Mackerras 10014cf11afSPaul Mackerras /* Secondary processors write this value with their cpu # */ 10114cf11afSPaul Mackerras /* after they enter the spin loop immediately below. */ 10214cf11afSPaul Mackerras .globl __secondary_hold_acknowledge 10314cf11afSPaul Mackerras__secondary_hold_acknowledge: 104eb039161STobin C. Harding .8byte 0x0 10514cf11afSPaul Mackerras 106928a3197SSonny Rao#ifdef CONFIG_RELOCATABLE 1078b8b0cc1SMilton Miller /* This flag is set to 1 by a loader if the kernel should run 1088b8b0cc1SMilton Miller * at the loaded address instead of the linked address. This 1098b8b0cc1SMilton Miller * is used by kexec-tools to keep the the kdump kernel in the 1108b8b0cc1SMilton Miller * crash_kernel region. The loader is responsible for 1118b8b0cc1SMilton Miller * observing the alignment requirement. 1128b8b0cc1SMilton Miller */ 11370839d20SNicholas Piggin 11470839d20SNicholas Piggin#ifdef CONFIG_RELOCATABLE_TEST 11570839d20SNicholas Piggin#define RUN_AT_LOAD_DEFAULT 1 /* Test relocation, do not copy to 0 */ 11670839d20SNicholas Piggin#else 11770839d20SNicholas Piggin#define RUN_AT_LOAD_DEFAULT 0x72756e30 /* "run0" -- relocate to 0 by default */ 11870839d20SNicholas Piggin#endif 11970839d20SNicholas Piggin 1208b8b0cc1SMilton Miller /* Do not move this variable as kexec-tools knows about it. */ 1218b8b0cc1SMilton Miller . = 0x5c 1228b8b0cc1SMilton Miller .globl __run_at_load 1238b8b0cc1SMilton Miller__run_at_load: 12457f26649SNicholas PigginDEFINE_FIXED_SYMBOL(__run_at_load) 12570839d20SNicholas Piggin .long RUN_AT_LOAD_DEFAULT 1268b8b0cc1SMilton Miller#endif 1278b8b0cc1SMilton Miller 12814cf11afSPaul Mackerras . = 0x60 12914cf11afSPaul Mackerras/* 13075423b7bSGeoff Levand * The following code is used to hold secondary processors 13175423b7bSGeoff Levand * in a spin loop after they have entered the kernel, but 13214cf11afSPaul Mackerras * before the bulk of the kernel has been relocated. This code 13314cf11afSPaul Mackerras * is relocated to physical address 0x60 before prom_init is run. 13414cf11afSPaul Mackerras * All of it must fit below the first exception vector at 0x100. 1351f6a93e4SPaul Mackerras * Use .globl here not _GLOBAL because we want __secondary_hold 1361f6a93e4SPaul Mackerras * to be the actual text address, not a descriptor. 13714cf11afSPaul Mackerras */ 1381f6a93e4SPaul Mackerras .globl __secondary_hold 1391f6a93e4SPaul Mackerras__secondary_hold: 1405c0484e2SBenjamin Herrenschmidt FIXUP_ENDIAN 1412d27cfd3SBenjamin Herrenschmidt#ifndef CONFIG_PPC_BOOK3E 14214cf11afSPaul Mackerras mfmsr r24 14314cf11afSPaul Mackerras ori r24,r24,MSR_RI 14414cf11afSPaul Mackerras mtmsrd r24 /* RI on */ 1452d27cfd3SBenjamin Herrenschmidt#endif 146f1870f77SAnton Blanchard /* Grab our physical cpu number */ 14714cf11afSPaul Mackerras mr r24,r3 14896f013feSJimi Xenidis /* stash r4 for book3e */ 14996f013feSJimi Xenidis mr r25,r4 15014cf11afSPaul Mackerras 15114cf11afSPaul Mackerras /* Tell the master cpu we're here */ 15214cf11afSPaul Mackerras /* Relocation is off & we are located at an address less */ 15314cf11afSPaul Mackerras /* than 0x100, so only need to grab low order offset. */ 15457f26649SNicholas Piggin std r24,(ABS_ADDR(__secondary_hold_acknowledge))(0) 15514cf11afSPaul Mackerras sync 15614cf11afSPaul Mackerras 15796f013feSJimi Xenidis li r26,0 15896f013feSJimi Xenidis#ifdef CONFIG_PPC_BOOK3E 15996f013feSJimi Xenidis tovirt(r26,r26) 16096f013feSJimi Xenidis#endif 16114cf11afSPaul Mackerras /* All secondary cpus wait here until told to start. */ 16257f26649SNicholas Piggin100: ld r12,(ABS_ADDR(__secondary_hold_spinloop))(r26) 163cc7efbf9SAnton Blanchard cmpdi 0,r12,0 1641f6a93e4SPaul Mackerras beq 100b 16514cf11afSPaul Mackerras 166da665885SThiago Jung Bauermann#if defined(CONFIG_SMP) || defined(CONFIG_KEXEC_CORE) 16796f013feSJimi Xenidis#ifdef CONFIG_PPC_BOOK3E 168cc7efbf9SAnton Blanchard tovirt(r12,r12) 16996f013feSJimi Xenidis#endif 170cc7efbf9SAnton Blanchard mtctr r12 17114cf11afSPaul Mackerras mr r3,r24 17296f013feSJimi Xenidis /* 17396f013feSJimi Xenidis * it may be the case that other platforms have r4 right to 17496f013feSJimi Xenidis * begin with, this gives us some safety in case it is not 17596f013feSJimi Xenidis */ 17696f013feSJimi Xenidis#ifdef CONFIG_PPC_BOOK3E 17796f013feSJimi Xenidis mr r4,r25 17896f013feSJimi Xenidis#else 1792d27cfd3SBenjamin Herrenschmidt li r4,0 18096f013feSJimi Xenidis#endif 181dd797738SBenjamin Herrenschmidt /* Make sure that patched code is visible */ 182dd797738SBenjamin Herrenschmidt isync 183758438a7SMichael Ellerman bctr 18414cf11afSPaul Mackerras#else 18514cf11afSPaul Mackerras BUG_OPCODE 18614cf11afSPaul Mackerras#endif 18757f26649SNicholas PigginCLOSE_FIXED_SECTION(first_256B) 18814cf11afSPaul Mackerras 18914cf11afSPaul Mackerras/* This value is used to mark exception frames on the stack. */ 19014cf11afSPaul Mackerras .section ".toc","aw" 19114cf11afSPaul Mackerrasexception_marker: 19214cf11afSPaul Mackerras .tc ID_72656773_68657265[TC],0x7265677368657265 19357f26649SNicholas Piggin .previous 19414cf11afSPaul Mackerras 19514cf11afSPaul Mackerras/* 1960ebc4cdaSBenjamin Herrenschmidt * On server, we include the exception vectors code here as it 1970ebc4cdaSBenjamin Herrenschmidt * relies on absolute addressing which is only possible within 1980ebc4cdaSBenjamin Herrenschmidt * this compilation unit 19914cf11afSPaul Mackerras */ 2000ebc4cdaSBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3S 2010ebc4cdaSBenjamin Herrenschmidt#include "exceptions-64s.S" 20257f26649SNicholas Piggin#else 20357f26649SNicholas PigginOPEN_TEXT_SECTION(0x100) 2041f6a93e4SPaul Mackerras#endif 20514cf11afSPaul Mackerras 20657f26649SNicholas PigginUSE_TEXT_SECTION() 20757f26649SNicholas Piggin 208e16c8765SAndy Fleming#ifdef CONFIG_PPC_BOOK3E 209d17799f9Schenhui zhao/* 2106becef7eSchenhui zhao * The booting_thread_hwid holds the thread id we want to boot in cpu 2116becef7eSchenhui zhao * hotplug case. It is set by cpu hotplug code, and is invalid by default. 2126becef7eSchenhui zhao * The thread id is the same as the initial value of SPRN_PIR[THREAD_ID] 2136becef7eSchenhui zhao * bit field. 2146becef7eSchenhui zhao */ 2156becef7eSchenhui zhao .globl booting_thread_hwid 2166becef7eSchenhui zhaobooting_thread_hwid: 2176becef7eSchenhui zhao .long INVALID_THREAD_HWID 2186becef7eSchenhui zhao .align 3 2196becef7eSchenhui zhao/* 2206becef7eSchenhui zhao * start a thread in the same core 2216becef7eSchenhui zhao * input parameters: 2226becef7eSchenhui zhao * r3 = the thread physical id 2236becef7eSchenhui zhao * r4 = the entry point where thread starts 2246becef7eSchenhui zhao */ 2256becef7eSchenhui zhao_GLOBAL(book3e_start_thread) 2266becef7eSchenhui zhao LOAD_REG_IMMEDIATE(r5, MSR_KERNEL) 227f87f253bSNicholas Piggin cmpwi r3, 0 2286becef7eSchenhui zhao beq 10f 229f87f253bSNicholas Piggin cmpwi r3, 1 2306becef7eSchenhui zhao beq 11f 2316becef7eSchenhui zhao /* If the thread id is invalid, just exit. */ 2326becef7eSchenhui zhao b 13f 2336becef7eSchenhui zhao10: 2347a25d912SScott Wood MTTMR(TMRN_IMSR0, 5) 2357a25d912SScott Wood MTTMR(TMRN_INIA0, 4) 2366becef7eSchenhui zhao b 12f 2376becef7eSchenhui zhao11: 2387a25d912SScott Wood MTTMR(TMRN_IMSR1, 5) 2397a25d912SScott Wood MTTMR(TMRN_INIA1, 4) 2406becef7eSchenhui zhao12: 2416becef7eSchenhui zhao isync 2426becef7eSchenhui zhao li r6, 1 2436becef7eSchenhui zhao sld r6, r6, r3 2446becef7eSchenhui zhao mtspr SPRN_TENS, r6 2456becef7eSchenhui zhao13: 2466becef7eSchenhui zhao blr 2476becef7eSchenhui zhao 2486becef7eSchenhui zhao/* 249d17799f9Schenhui zhao * stop a thread in the same core 250d17799f9Schenhui zhao * input parameter: 251d17799f9Schenhui zhao * r3 = the thread physical id 252d17799f9Schenhui zhao */ 253d17799f9Schenhui zhao_GLOBAL(book3e_stop_thread) 254f87f253bSNicholas Piggin cmpwi r3, 0 255d17799f9Schenhui zhao beq 10f 256f87f253bSNicholas Piggin cmpwi r3, 1 257d17799f9Schenhui zhao beq 10f 258d17799f9Schenhui zhao /* If the thread id is invalid, just exit. */ 259d17799f9Schenhui zhao b 13f 260d17799f9Schenhui zhao10: 261d17799f9Schenhui zhao li r4, 1 262d17799f9Schenhui zhao sld r4, r4, r3 263d17799f9Schenhui zhao mtspr SPRN_TENC, r4 264d17799f9Schenhui zhao13: 265d17799f9Schenhui zhao blr 266d17799f9Schenhui zhao 267e16c8765SAndy Fleming_GLOBAL(fsl_secondary_thread_init) 268f34b3e19SScott Wood mfspr r4,SPRN_BUCSR 269f34b3e19SScott Wood 270e16c8765SAndy Fleming /* Enable branch prediction */ 271e16c8765SAndy Fleming lis r3,BUCSR_INIT@h 272e16c8765SAndy Fleming ori r3,r3,BUCSR_INIT@l 273e16c8765SAndy Fleming mtspr SPRN_BUCSR,r3 274e16c8765SAndy Fleming isync 275e16c8765SAndy Fleming 276e16c8765SAndy Fleming /* 277e16c8765SAndy Fleming * Fix PIR to match the linear numbering in the device tree. 278e16c8765SAndy Fleming * 279e16c8765SAndy Fleming * On e6500, the reset value of PIR uses the low three bits for 280e16c8765SAndy Fleming * the thread within a core, and the upper bits for the core 281e16c8765SAndy Fleming * number. There are two threads per core, so shift everything 282e16c8765SAndy Fleming * but the low bit right by two bits so that the cpu numbering is 283e16c8765SAndy Fleming * continuous. 284f34b3e19SScott Wood * 285f34b3e19SScott Wood * If the old value of BUCSR is non-zero, this thread has run 286f34b3e19SScott Wood * before. Thus, we assume we are coming from kexec or a similar 287f34b3e19SScott Wood * scenario, and PIR is already set to the correct value. This 288f34b3e19SScott Wood * is a bit of a hack, but there are limited opportunities for 289f34b3e19SScott Wood * getting information into the thread and the alternatives 290f34b3e19SScott Wood * seemed like they'd be overkill. We can't tell just by looking 291f34b3e19SScott Wood * at the old PIR value which state it's in, since the same value 292f34b3e19SScott Wood * could be valid for one thread out of reset and for a different 293f34b3e19SScott Wood * thread in Linux. 294e16c8765SAndy Fleming */ 295f34b3e19SScott Wood 296e16c8765SAndy Fleming mfspr r3, SPRN_PIR 297f34b3e19SScott Wood cmpwi r4,0 298f34b3e19SScott Wood bne 1f 299e16c8765SAndy Fleming rlwimi r3, r3, 30, 2, 30 300e16c8765SAndy Fleming mtspr SPRN_PIR, r3 301f34b3e19SScott Wood1: 302e16c8765SAndy Fleming#endif 303e16c8765SAndy Fleming 3042d27cfd3SBenjamin Herrenschmidt_GLOBAL(generic_secondary_thread_init) 30514cf11afSPaul Mackerras mr r24,r3 30614cf11afSPaul Mackerras 30714cf11afSPaul Mackerras /* turn on 64-bit mode */ 308b1576fecSAnton Blanchard bl enable_64b_mode 30914cf11afSPaul Mackerras 3102d27cfd3SBenjamin Herrenschmidt /* get a valid TOC pointer, wherever we're mapped at */ 311b1576fecSAnton Blanchard bl relative_toc 3121fbe9cf2SAnton Blanchard tovirt(r2,r2) 313e31aa453SPaul Mackerras 3142d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3E 3152d27cfd3SBenjamin Herrenschmidt /* Book3E initialization */ 3162d27cfd3SBenjamin Herrenschmidt mr r3,r24 317b1576fecSAnton Blanchard bl book3e_secondary_thread_init 3182d27cfd3SBenjamin Herrenschmidt#endif 3192d27cfd3SBenjamin Herrenschmidt b generic_secondary_common_init 3202d27cfd3SBenjamin Herrenschmidt 3212d27cfd3SBenjamin Herrenschmidt/* 3222d27cfd3SBenjamin Herrenschmidt * On pSeries and most other platforms, secondary processors spin 3232d27cfd3SBenjamin Herrenschmidt * in the following code. 3242d27cfd3SBenjamin Herrenschmidt * At entry, r3 = this processor's number (physical cpu id) 3252d27cfd3SBenjamin Herrenschmidt * 3262d27cfd3SBenjamin Herrenschmidt * On Book3E, r4 = 1 to indicate that the initial TLB entry for 3272d27cfd3SBenjamin Herrenschmidt * this core already exists (setup via some other mechanism such 3282d27cfd3SBenjamin Herrenschmidt * as SCOM before entry). 3292d27cfd3SBenjamin Herrenschmidt */ 3302d27cfd3SBenjamin Herrenschmidt_GLOBAL(generic_secondary_smp_init) 3315c0484e2SBenjamin Herrenschmidt FIXUP_ENDIAN 3322d27cfd3SBenjamin Herrenschmidt mr r24,r3 3332d27cfd3SBenjamin Herrenschmidt mr r25,r4 3342d27cfd3SBenjamin Herrenschmidt 3352d27cfd3SBenjamin Herrenschmidt /* turn on 64-bit mode */ 336b1576fecSAnton Blanchard bl enable_64b_mode 3372d27cfd3SBenjamin Herrenschmidt 3382d27cfd3SBenjamin Herrenschmidt /* get a valid TOC pointer, wherever we're mapped at */ 339b1576fecSAnton Blanchard bl relative_toc 3401fbe9cf2SAnton Blanchard tovirt(r2,r2) 3412d27cfd3SBenjamin Herrenschmidt 3422d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3E 3432d27cfd3SBenjamin Herrenschmidt /* Book3E initialization */ 3442d27cfd3SBenjamin Herrenschmidt mr r3,r24 3452d27cfd3SBenjamin Herrenschmidt mr r4,r25 346b1576fecSAnton Blanchard bl book3e_secondary_core_init 3476becef7eSchenhui zhao 3486becef7eSchenhui zhao/* 3496becef7eSchenhui zhao * After common core init has finished, check if the current thread is the 3506becef7eSchenhui zhao * one we wanted to boot. If not, start the specified thread and stop the 3516becef7eSchenhui zhao * current thread. 3526becef7eSchenhui zhao */ 3536becef7eSchenhui zhao LOAD_REG_ADDR(r4, booting_thread_hwid) 3546becef7eSchenhui zhao lwz r3, 0(r4) 3556becef7eSchenhui zhao li r5, INVALID_THREAD_HWID 3566becef7eSchenhui zhao cmpw r3, r5 3576becef7eSchenhui zhao beq 20f 3586becef7eSchenhui zhao 3596becef7eSchenhui zhao /* 3606becef7eSchenhui zhao * The value of booting_thread_hwid has been stored in r3, 3616becef7eSchenhui zhao * so make it invalid. 3626becef7eSchenhui zhao */ 3636becef7eSchenhui zhao stw r5, 0(r4) 3646becef7eSchenhui zhao 3656becef7eSchenhui zhao /* 3666becef7eSchenhui zhao * Get the current thread id and check if it is the one we wanted. 3676becef7eSchenhui zhao * If not, start the one specified in booting_thread_hwid and stop 3686becef7eSchenhui zhao * the current thread. 3696becef7eSchenhui zhao */ 3706becef7eSchenhui zhao mfspr r8, SPRN_TIR 3716becef7eSchenhui zhao cmpw r3, r8 3726becef7eSchenhui zhao beq 20f 3736becef7eSchenhui zhao 3746becef7eSchenhui zhao /* start the specified thread */ 3756becef7eSchenhui zhao LOAD_REG_ADDR(r5, fsl_secondary_thread_init) 3766becef7eSchenhui zhao ld r4, 0(r5) 3776becef7eSchenhui zhao bl book3e_start_thread 3786becef7eSchenhui zhao 3796becef7eSchenhui zhao /* stop the current thread */ 3806becef7eSchenhui zhao mr r3, r8 3816becef7eSchenhui zhao bl book3e_stop_thread 3826becef7eSchenhui zhao10: 3836becef7eSchenhui zhao b 10b 3846becef7eSchenhui zhao20: 3852d27cfd3SBenjamin Herrenschmidt#endif 3862d27cfd3SBenjamin Herrenschmidt 3872d27cfd3SBenjamin Herrenschmidtgeneric_secondary_common_init: 38814cf11afSPaul Mackerras /* Set up a paca value for this processor. Since we have the 38914cf11afSPaul Mackerras * physical cpu id in r24, we need to search the pacas to find 39014cf11afSPaul Mackerras * which logical id maps to our physical one. 39114cf11afSPaul Mackerras */ 392768d18adSMilton Miller#ifndef CONFIG_SMP 393b1576fecSAnton Blanchard b kexec_wait /* wait for next kernel if !SMP */ 394768d18adSMilton Miller#else 395d2e60075SNicholas Piggin LOAD_REG_ADDR(r8, paca_ptrs) /* Load paca_ptrs pointe */ 396d2e60075SNicholas Piggin ld r8,0(r8) /* Get base vaddr of array */ 397768d18adSMilton Miller LOAD_REG_ADDR(r7, nr_cpu_ids) /* Load nr_cpu_ids address */ 398768d18adSMilton Miller lwz r7,0(r7) /* also the max paca allocated */ 39914cf11afSPaul Mackerras li r5,0 /* logical cpu id */ 400d2e60075SNicholas Piggin1: 401d2e60075SNicholas Piggin sldi r9,r5,3 /* get paca_ptrs[] index from cpu id */ 402d2e60075SNicholas Piggin ldx r13,r9,r8 /* r13 = paca_ptrs[cpu id] */ 403d2e60075SNicholas Piggin lhz r6,PACAHWCPUID(r13) /* Load HW procid from paca */ 40414cf11afSPaul Mackerras cmpw r6,r24 /* Compare to our id */ 40514cf11afSPaul Mackerras beq 2f 40614cf11afSPaul Mackerras addi r5,r5,1 407768d18adSMilton Miller cmpw r5,r7 /* Check if more pacas exist */ 40814cf11afSPaul Mackerras blt 1b 40914cf11afSPaul Mackerras 41014cf11afSPaul Mackerras mr r3,r24 /* not found, copy phys to r3 */ 411b1576fecSAnton Blanchard b kexec_wait /* next kernel might do better */ 41214cf11afSPaul Mackerras 4132dd60d79SBenjamin Herrenschmidt2: SET_PACA(r13) 4142d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3E 4152d27cfd3SBenjamin Herrenschmidt addi r12,r13,PACA_EXTLB /* and TLB exc frame in another */ 4162d27cfd3SBenjamin Herrenschmidt mtspr SPRN_SPRG_TLB_EXFRAME,r12 4172d27cfd3SBenjamin Herrenschmidt#endif 4182d27cfd3SBenjamin Herrenschmidt 41914cf11afSPaul Mackerras /* From now on, r24 is expected to be logical cpuid */ 42014cf11afSPaul Mackerras mr r24,r5 421b6f6b98aSSonny Rao 422f39b7a55SOlof Johansson /* See if we need to call a cpu state restore handler */ 423e31aa453SPaul Mackerras LOAD_REG_ADDR(r23, cur_cpu_spec) 424f39b7a55SOlof Johansson ld r23,0(r23) 4252751b628SAnton Blanchard ld r12,CPU_SPEC_RESTORE(r23) 4262751b628SAnton Blanchard cmpdi 0,r12,0 4279d07bc84SBenjamin Herrenschmidt beq 3f 428f55d9665SMichael Ellerman#ifdef PPC64_ELF_ABI_v1 4292751b628SAnton Blanchard ld r12,0(r12) 4302751b628SAnton Blanchard#endif 431cc7efbf9SAnton Blanchard mtctr r12 432f39b7a55SOlof Johansson bctrl 433f39b7a55SOlof Johansson 4347ac87abbSMatt Evans3: LOAD_REG_ADDR(r3, spinning_secondaries) /* Decrement spinning_secondaries */ 4359d07bc84SBenjamin Herrenschmidt lwarx r4,0,r3 4369d07bc84SBenjamin Herrenschmidt subi r4,r4,1 4379d07bc84SBenjamin Herrenschmidt stwcx. r4,0,r3 4389d07bc84SBenjamin Herrenschmidt bne 3b 4399d07bc84SBenjamin Herrenschmidt isync 4409d07bc84SBenjamin Herrenschmidt 4419d07bc84SBenjamin Herrenschmidt4: HMT_LOW 442ad0693eeSBenjamin Herrenschmidt lbz r23,PACAPROCSTART(r13) /* Test if this processor should */ 443ad0693eeSBenjamin Herrenschmidt /* start. */ 444ad0693eeSBenjamin Herrenschmidt cmpwi 0,r23,0 4459d07bc84SBenjamin Herrenschmidt beq 4b /* Loop until told to go */ 446ad0693eeSBenjamin Herrenschmidt 447ad0693eeSBenjamin Herrenschmidt sync /* order paca.run and cur_cpu_spec */ 4489d07bc84SBenjamin Herrenschmidt isync /* In case code patching happened */ 449ad0693eeSBenjamin Herrenschmidt 4509d07bc84SBenjamin Herrenschmidt /* Create a temp kernel stack for use before relocation is on. */ 45114cf11afSPaul Mackerras ld r1,PACAEMERGSP(r13) 45214cf11afSPaul Mackerras subi r1,r1,STACK_FRAME_OVERHEAD 45314cf11afSPaul Mackerras 454c705677eSStephen Rothwell b __secondary_start 455768d18adSMilton Miller#endif /* SMP */ 45614cf11afSPaul Mackerras 457e31aa453SPaul Mackerras/* 458e31aa453SPaul Mackerras * Turn the MMU off. 459e31aa453SPaul Mackerras * Assumes we're mapped EA == RA if the MMU is on. 460e31aa453SPaul Mackerras */ 4612d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3S 4626a3bab90SAnton Blanchard__mmu_off: 46314cf11afSPaul Mackerras mfmsr r3 46414cf11afSPaul Mackerras andi. r0,r3,MSR_IR|MSR_DR 46514cf11afSPaul Mackerras beqlr 466e31aa453SPaul Mackerras mflr r4 46714cf11afSPaul Mackerras andc r3,r3,r0 46814cf11afSPaul Mackerras mtspr SPRN_SRR0,r4 46914cf11afSPaul Mackerras mtspr SPRN_SRR1,r3 47014cf11afSPaul Mackerras sync 47114cf11afSPaul Mackerras rfid 47214cf11afSPaul Mackerras b . /* prevent speculative execution */ 4732d27cfd3SBenjamin Herrenschmidt#endif 47414cf11afSPaul Mackerras 47514cf11afSPaul Mackerras 47614cf11afSPaul Mackerras/* 47714cf11afSPaul Mackerras * Here is our main kernel entry point. We support currently 2 kind of entries 47814cf11afSPaul Mackerras * depending on the value of r5. 47914cf11afSPaul Mackerras * 48014cf11afSPaul Mackerras * r5 != NULL -> OF entry, we go to prom_init, "legacy" parameter content 48114cf11afSPaul Mackerras * in r3...r7 48214cf11afSPaul Mackerras * 48314cf11afSPaul Mackerras * r5 == NULL -> kexec style entry. r3 is a physical pointer to the 48414cf11afSPaul Mackerras * DT block, r4 is a physical pointer to the kernel itself 48514cf11afSPaul Mackerras * 48614cf11afSPaul Mackerras */ 4876a3bab90SAnton Blanchard__start_initialization_multiplatform: 488e31aa453SPaul Mackerras /* Make sure we are running in 64 bits mode */ 489b1576fecSAnton Blanchard bl enable_64b_mode 490e31aa453SPaul Mackerras 491e31aa453SPaul Mackerras /* Get TOC pointer (current runtime address) */ 492b1576fecSAnton Blanchard bl relative_toc 493e31aa453SPaul Mackerras 494e31aa453SPaul Mackerras /* find out where we are now */ 495e31aa453SPaul Mackerras bcl 20,31,$+4 496e31aa453SPaul Mackerras0: mflr r26 /* r26 = runtime addr here */ 497e31aa453SPaul Mackerras addis r26,r26,(_stext - 0b)@ha 498e31aa453SPaul Mackerras addi r26,r26,(_stext - 0b)@l /* current runtime base addr */ 499e31aa453SPaul Mackerras 50014cf11afSPaul Mackerras /* 50114cf11afSPaul Mackerras * Are we booted from a PROM Of-type client-interface ? 50214cf11afSPaul Mackerras */ 50314cf11afSPaul Mackerras cmpldi cr0,r5,0 504939e60f6SStephen Rothwell beq 1f 505b1576fecSAnton Blanchard b __boot_from_prom /* yes -> prom */ 506939e60f6SStephen Rothwell1: 50714cf11afSPaul Mackerras /* Save parameters */ 50814cf11afSPaul Mackerras mr r31,r3 50914cf11afSPaul Mackerras mr r30,r4 510daea1175SBenjamin Herrenschmidt#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL 511daea1175SBenjamin Herrenschmidt /* Save OPAL entry */ 512daea1175SBenjamin Herrenschmidt mr r28,r8 513daea1175SBenjamin Herrenschmidt mr r29,r9 514daea1175SBenjamin Herrenschmidt#endif 51514cf11afSPaul Mackerras 5162d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3E 517b1576fecSAnton Blanchard bl start_initialization_book3e 518b1576fecSAnton Blanchard b __after_prom_start 5192d27cfd3SBenjamin Herrenschmidt#else 52014cf11afSPaul Mackerras /* Setup some critical 970 SPRs before switching MMU off */ 521f39b7a55SOlof Johansson mfspr r0,SPRN_PVR 522f39b7a55SOlof Johansson srwi r0,r0,16 523f39b7a55SOlof Johansson cmpwi r0,0x39 /* 970 */ 524f39b7a55SOlof Johansson beq 1f 525f39b7a55SOlof Johansson cmpwi r0,0x3c /* 970FX */ 526f39b7a55SOlof Johansson beq 1f 527f39b7a55SOlof Johansson cmpwi r0,0x44 /* 970MP */ 528190a24f5SOlof Johansson beq 1f 529190a24f5SOlof Johansson cmpwi r0,0x45 /* 970GX */ 530f39b7a55SOlof Johansson bne 2f 531b1576fecSAnton Blanchard1: bl __cpu_preinit_ppc970 532f39b7a55SOlof Johansson2: 53314cf11afSPaul Mackerras 534e31aa453SPaul Mackerras /* Switch off MMU if not already off */ 535b1576fecSAnton Blanchard bl __mmu_off 536b1576fecSAnton Blanchard b __after_prom_start 5372d27cfd3SBenjamin Herrenschmidt#endif /* CONFIG_PPC_BOOK3E */ 53814cf11afSPaul Mackerras 5396a3bab90SAnton Blanchard__boot_from_prom: 54028794d34SBenjamin Herrenschmidt#ifdef CONFIG_PPC_OF_BOOT_TRAMPOLINE 54114cf11afSPaul Mackerras /* Save parameters */ 54214cf11afSPaul Mackerras mr r31,r3 54314cf11afSPaul Mackerras mr r30,r4 54414cf11afSPaul Mackerras mr r29,r5 54514cf11afSPaul Mackerras mr r28,r6 54614cf11afSPaul Mackerras mr r27,r7 54714cf11afSPaul Mackerras 5486088857bSOlaf Hering /* 5496088857bSOlaf Hering * Align the stack to 16-byte boundary 5506088857bSOlaf Hering * Depending on the size and layout of the ELF sections in the initial 551e31aa453SPaul Mackerras * boot binary, the stack pointer may be unaligned on PowerMac 5526088857bSOlaf Hering */ 553c05b4770SLinus Torvalds rldicr r1,r1,0,59 554c05b4770SLinus Torvalds 555549e8152SPaul Mackerras#ifdef CONFIG_RELOCATABLE 556549e8152SPaul Mackerras /* Relocate code for where we are now */ 557549e8152SPaul Mackerras mr r3,r26 558b1576fecSAnton Blanchard bl relocate 559549e8152SPaul Mackerras#endif 560549e8152SPaul Mackerras 56114cf11afSPaul Mackerras /* Restore parameters */ 56214cf11afSPaul Mackerras mr r3,r31 56314cf11afSPaul Mackerras mr r4,r30 56414cf11afSPaul Mackerras mr r5,r29 56514cf11afSPaul Mackerras mr r6,r28 56614cf11afSPaul Mackerras mr r7,r27 56714cf11afSPaul Mackerras 56814cf11afSPaul Mackerras /* Do all of the interaction with OF client interface */ 569549e8152SPaul Mackerras mr r8,r26 570b1576fecSAnton Blanchard bl prom_init 57128794d34SBenjamin Herrenschmidt#endif /* #CONFIG_PPC_OF_BOOT_TRAMPOLINE */ 57228794d34SBenjamin Herrenschmidt 57328794d34SBenjamin Herrenschmidt /* We never return. We also hit that trap if trying to boot 57428794d34SBenjamin Herrenschmidt * from OF while CONFIG_PPC_OF_BOOT_TRAMPOLINE isn't selected */ 57514cf11afSPaul Mackerras trap 57614cf11afSPaul Mackerras 5776a3bab90SAnton Blanchard__after_prom_start: 578549e8152SPaul Mackerras#ifdef CONFIG_RELOCATABLE 579549e8152SPaul Mackerras /* process relocations for the final address of the kernel */ 580549e8152SPaul Mackerras lis r25,PAGE_OFFSET@highest /* compute virtual base of kernel */ 581549e8152SPaul Mackerras sldi r25,r25,32 5821cb6e064STiejun Chen#if defined(CONFIG_PPC_BOOK3E) 5831cb6e064STiejun Chen tovirt(r26,r26) /* on booke, we already run at PAGE_OFFSET */ 5841cb6e064STiejun Chen#endif 58557f26649SNicholas Piggin lwz r7,(FIXED_SYMBOL_ABS_ADDR(__run_at_load))(r26) 5861cb6e064STiejun Chen#if defined(CONFIG_PPC_BOOK3E) 5871cb6e064STiejun Chen tophys(r26,r26) 5881cb6e064STiejun Chen#endif 589928a3197SSonny Rao cmplwi cr0,r7,1 /* flagged to stay where we are ? */ 59054622f10SMohan Kumar M bne 1f 59154622f10SMohan Kumar M add r25,r25,r26 59254622f10SMohan Kumar M1: mr r3,r25 593b1576fecSAnton Blanchard bl relocate 5941cb6e064STiejun Chen#if defined(CONFIG_PPC_BOOK3E) 5951cb6e064STiejun Chen /* IVPR needs to be set after relocation. */ 5961cb6e064STiejun Chen bl init_core_book3e 5971cb6e064STiejun Chen#endif 598549e8152SPaul Mackerras#endif 59914cf11afSPaul Mackerras 60014cf11afSPaul Mackerras/* 601e31aa453SPaul Mackerras * We need to run with _stext at physical address PHYSICAL_START. 60214cf11afSPaul Mackerras * This will leave some code in the first 256B of 60314cf11afSPaul Mackerras * real memory, which are reserved for software use. 60414cf11afSPaul Mackerras * 60514cf11afSPaul Mackerras * Note: This process overwrites the OF exception vectors. 60614cf11afSPaul Mackerras */ 607549e8152SPaul Mackerras li r3,0 /* target addr */ 6082d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3E 6092d27cfd3SBenjamin Herrenschmidt tovirt(r3,r3) /* on booke, we already run at PAGE_OFFSET */ 6102d27cfd3SBenjamin Herrenschmidt#endif 611549e8152SPaul Mackerras mr. r4,r26 /* In some cases the loader may */ 612835c031cSTiejun Chen#if defined(CONFIG_PPC_BOOK3E) 613835c031cSTiejun Chen tovirt(r4,r4) 614835c031cSTiejun Chen#endif 615e31aa453SPaul Mackerras beq 9f /* have already put us at zero */ 61614cf11afSPaul Mackerras li r6,0x100 /* Start offset, the first 0x100 */ 61714cf11afSPaul Mackerras /* bytes were copied earlier. */ 61814cf11afSPaul Mackerras 61911ee7e99SAnton Blanchard#ifdef CONFIG_RELOCATABLE 62054622f10SMohan Kumar M/* 62154622f10SMohan Kumar M * Check if the kernel has to be running as relocatable kernel based on the 6228b8b0cc1SMilton Miller * variable __run_at_load, if it is set the kernel is treated as relocatable 62354622f10SMohan Kumar M * kernel, otherwise it will be moved to PHYSICAL_START 62454622f10SMohan Kumar M */ 6251cb6e064STiejun Chen#if defined(CONFIG_PPC_BOOK3E) 6261cb6e064STiejun Chen tovirt(r26,r26) /* on booke, we already run at PAGE_OFFSET */ 6271cb6e064STiejun Chen#endif 62857f26649SNicholas Piggin lwz r7,(FIXED_SYMBOL_ABS_ADDR(__run_at_load))(r26) 6298b8b0cc1SMilton Miller cmplwi cr0,r7,1 63054622f10SMohan Kumar M bne 3f 63154622f10SMohan Kumar M 6321cb6e064STiejun Chen#ifdef CONFIG_PPC_BOOK3E 6331cb6e064STiejun Chen LOAD_REG_ADDR(r5, __end_interrupts) 6341cb6e064STiejun Chen LOAD_REG_ADDR(r11, _stext) 6351cb6e064STiejun Chen sub r5,r5,r11 6361cb6e064STiejun Chen#else 637c1fb6816SMichael Neuling /* just copy interrupts */ 63857f26649SNicholas Piggin LOAD_REG_IMMEDIATE(r5, FIXED_SYMBOL_ABS_ADDR(__end_interrupts)) 6391cb6e064STiejun Chen#endif 64054622f10SMohan Kumar M b 5f 64154622f10SMohan Kumar M3: 64254622f10SMohan Kumar M#endif 64357f26649SNicholas Piggin /* # bytes of memory to copy */ 64457f26649SNicholas Piggin lis r5,(ABS_ADDR(copy_to_here))@ha 64557f26649SNicholas Piggin addi r5,r5,(ABS_ADDR(copy_to_here))@l 64654622f10SMohan Kumar M 647b1576fecSAnton Blanchard bl copy_and_flush /* copy the first n bytes */ 64814cf11afSPaul Mackerras /* this includes the code being */ 64914cf11afSPaul Mackerras /* executed here. */ 65057f26649SNicholas Piggin /* Jump to the copy of this code that we just made */ 65157f26649SNicholas Piggin addis r8,r3,(ABS_ADDR(4f))@ha 65257f26649SNicholas Piggin addi r12,r8,(ABS_ADDR(4f))@l 653cc7efbf9SAnton Blanchard mtctr r12 65414cf11afSPaul Mackerras bctr 65514cf11afSPaul Mackerras 656286e4f90SAnton Blanchard.balign 8 657eb039161STobin C. Hardingp_end: .8byte _end - copy_to_here 65854622f10SMohan Kumar M 659573819e3SNicholas Piggin4: 660573819e3SNicholas Piggin /* 661573819e3SNicholas Piggin * Now copy the rest of the kernel up to _end, add 662573819e3SNicholas Piggin * _end - copy_to_here to the copy limit and run again. 663573819e3SNicholas Piggin */ 66457f26649SNicholas Piggin addis r8,r26,(ABS_ADDR(p_end))@ha 66557f26649SNicholas Piggin ld r8,(ABS_ADDR(p_end))@l(r8) 666573819e3SNicholas Piggin add r5,r5,r8 667b1576fecSAnton Blanchard5: bl copy_and_flush /* copy the rest */ 668e31aa453SPaul Mackerras 669b1576fecSAnton Blanchard9: b start_here_multiplatform 670e31aa453SPaul Mackerras 67114cf11afSPaul Mackerras/* 67214cf11afSPaul Mackerras * Copy routine used to copy the kernel to start at physical address 0 67314cf11afSPaul Mackerras * and flush and invalidate the caches as needed. 67414cf11afSPaul Mackerras * r3 = dest addr, r4 = source addr, r5 = copy limit, r6 = start offset 67514cf11afSPaul Mackerras * on exit, r3, r4, r5 are unchanged, r6 is updated to be >= r5. 67614cf11afSPaul Mackerras * 67714cf11afSPaul Mackerras * Note: this routine *only* clobbers r0, r6 and lr 67814cf11afSPaul Mackerras */ 67914cf11afSPaul Mackerras_GLOBAL(copy_and_flush) 68014cf11afSPaul Mackerras addi r5,r5,-8 68114cf11afSPaul Mackerras addi r6,r6,-8 6825a2fe38dSOlof Johansson4: li r0,8 /* Use the smallest common */ 68314cf11afSPaul Mackerras /* denominator cache line */ 68414cf11afSPaul Mackerras /* size. This results in */ 68514cf11afSPaul Mackerras /* extra cache line flushes */ 68614cf11afSPaul Mackerras /* but operation is correct. */ 68714cf11afSPaul Mackerras /* Can't get cache line size */ 68814cf11afSPaul Mackerras /* from NACA as it is being */ 68914cf11afSPaul Mackerras /* moved too. */ 69014cf11afSPaul Mackerras 69114cf11afSPaul Mackerras mtctr r0 /* put # words/line in ctr */ 69214cf11afSPaul Mackerras3: addi r6,r6,8 /* copy a cache line */ 69314cf11afSPaul Mackerras ldx r0,r6,r4 69414cf11afSPaul Mackerras stdx r0,r6,r3 69514cf11afSPaul Mackerras bdnz 3b 69614cf11afSPaul Mackerras dcbst r6,r3 /* write it to memory */ 69714cf11afSPaul Mackerras sync 69814cf11afSPaul Mackerras icbi r6,r3 /* flush the icache line */ 69914cf11afSPaul Mackerras cmpld 0,r6,r5 70014cf11afSPaul Mackerras blt 4b 70114cf11afSPaul Mackerras sync 70214cf11afSPaul Mackerras addi r5,r5,8 70314cf11afSPaul Mackerras addi r6,r6,8 70429ce3c50SMichael Neuling isync 70514cf11afSPaul Mackerras blr 70614cf11afSPaul Mackerras 70714cf11afSPaul Mackerras.align 8 70814cf11afSPaul Mackerrascopy_to_here: 70914cf11afSPaul Mackerras 71014cf11afSPaul Mackerras#ifdef CONFIG_SMP 71114cf11afSPaul Mackerras#ifdef CONFIG_PPC_PMAC 71214cf11afSPaul Mackerras/* 71314cf11afSPaul Mackerras * On PowerMac, secondary processors starts from the reset vector, which 71414cf11afSPaul Mackerras * is temporarily turned into a call to one of the functions below. 71514cf11afSPaul Mackerras */ 71614cf11afSPaul Mackerras .section ".text"; 71714cf11afSPaul Mackerras .align 2 ; 71814cf11afSPaul Mackerras 71935499c01SPaul Mackerras .globl __secondary_start_pmac_0 72035499c01SPaul Mackerras__secondary_start_pmac_0: 72135499c01SPaul Mackerras /* NB the entries for cpus 0, 1, 2 must each occupy 8 bytes. */ 72235499c01SPaul Mackerras li r24,0 72335499c01SPaul Mackerras b 1f 72414cf11afSPaul Mackerras li r24,1 72535499c01SPaul Mackerras b 1f 72614cf11afSPaul Mackerras li r24,2 72735499c01SPaul Mackerras b 1f 72814cf11afSPaul Mackerras li r24,3 72935499c01SPaul Mackerras1: 73014cf11afSPaul Mackerras 73114cf11afSPaul Mackerras_GLOBAL(pmac_secondary_start) 73214cf11afSPaul Mackerras /* turn on 64-bit mode */ 733b1576fecSAnton Blanchard bl enable_64b_mode 73414cf11afSPaul Mackerras 735c478b581SBenjamin Herrenschmidt li r0,0 736c478b581SBenjamin Herrenschmidt mfspr r3,SPRN_HID4 737c478b581SBenjamin Herrenschmidt rldimi r3,r0,40,23 /* clear bit 23 (rm_ci) */ 738c478b581SBenjamin Herrenschmidt sync 739c478b581SBenjamin Herrenschmidt mtspr SPRN_HID4,r3 740c478b581SBenjamin Herrenschmidt isync 741c478b581SBenjamin Herrenschmidt sync 742c478b581SBenjamin Herrenschmidt slbia 743c478b581SBenjamin Herrenschmidt 744e31aa453SPaul Mackerras /* get TOC pointer (real address) */ 745b1576fecSAnton Blanchard bl relative_toc 7461fbe9cf2SAnton Blanchard tovirt(r2,r2) 747e31aa453SPaul Mackerras 74814cf11afSPaul Mackerras /* Copy some CPU settings from CPU 0 */ 749b1576fecSAnton Blanchard bl __restore_cpu_ppc970 75014cf11afSPaul Mackerras 75114cf11afSPaul Mackerras /* pSeries do that early though I don't think we really need it */ 75214cf11afSPaul Mackerras mfmsr r3 75314cf11afSPaul Mackerras ori r3,r3,MSR_RI 75414cf11afSPaul Mackerras mtmsrd r3 /* RI on */ 75514cf11afSPaul Mackerras 75614cf11afSPaul Mackerras /* Set up a paca value for this processor. */ 757d2e60075SNicholas Piggin LOAD_REG_ADDR(r4,paca_ptrs) /* Load paca pointer */ 758d2e60075SNicholas Piggin ld r4,0(r4) /* Get base vaddr of paca_ptrs array */ 759d2e60075SNicholas Piggin sldi r5,r24,3 /* get paca_ptrs[] index from cpu id */ 760d2e60075SNicholas Piggin ldx r13,r5,r4 /* r13 = paca_ptrs[cpu id] */ 7612dd60d79SBenjamin Herrenschmidt SET_PACA(r13) /* Save vaddr of paca in an SPRG*/ 76214cf11afSPaul Mackerras 76362cc67b9SBenjamin Herrenschmidt /* Mark interrupts soft and hard disabled (they might be enabled 76462cc67b9SBenjamin Herrenschmidt * in the PACA when doing hotplug) 76562cc67b9SBenjamin Herrenschmidt */ 766c2e480baSMadhavan Srinivasan li r0,IRQS_DISABLED 7674e26bc4aSMadhavan Srinivasan stb r0,PACAIRQSOFTMASK(r13) 7687230c564SBenjamin Herrenschmidt li r0,PACA_IRQ_HARD_DIS 7697230c564SBenjamin Herrenschmidt stb r0,PACAIRQHAPPENED(r13) 77062cc67b9SBenjamin Herrenschmidt 77114cf11afSPaul Mackerras /* Create a temp kernel stack for use before relocation is on. */ 77214cf11afSPaul Mackerras ld r1,PACAEMERGSP(r13) 77314cf11afSPaul Mackerras subi r1,r1,STACK_FRAME_OVERHEAD 77414cf11afSPaul Mackerras 775c705677eSStephen Rothwell b __secondary_start 77614cf11afSPaul Mackerras 77714cf11afSPaul Mackerras#endif /* CONFIG_PPC_PMAC */ 77814cf11afSPaul Mackerras 77914cf11afSPaul Mackerras/* 78014cf11afSPaul Mackerras * This function is called after the master CPU has released the 78114cf11afSPaul Mackerras * secondary processors. The execution environment is relocation off. 78214cf11afSPaul Mackerras * The paca for this processor has the following fields initialized at 78314cf11afSPaul Mackerras * this point: 78414cf11afSPaul Mackerras * 1. Processor number 78514cf11afSPaul Mackerras * 2. Segment table pointer (virtual address) 78614cf11afSPaul Mackerras * On entry the following are set: 7874f8cf36fSBenjamin Herrenschmidt * r1 = stack pointer (real addr of temp stack) 78814cf11afSPaul Mackerras * r24 = cpu# (in Linux terms) 78914cf11afSPaul Mackerras * r13 = paca virtual address 790ee43eb78SBenjamin Herrenschmidt * SPRG_PACA = paca virtual address 79114cf11afSPaul Mackerras */ 7922d27cfd3SBenjamin Herrenschmidt .section ".text"; 7932d27cfd3SBenjamin Herrenschmidt .align 2 ; 7942d27cfd3SBenjamin Herrenschmidt 795fc68e869SStephen Rothwell .globl __secondary_start 796c705677eSStephen Rothwell__secondary_start: 797799d6046SPaul Mackerras /* Set thread priority to MEDIUM */ 798799d6046SPaul Mackerras HMT_MEDIUM 79914cf11afSPaul Mackerras 800eafd825eSMichael Ellerman /* 801eafd825eSMichael Ellerman * Do early setup for this CPU, in particular initialising the MMU so we 802eafd825eSMichael Ellerman * can turn it on below. This is a call to C, which is OK, we're still 803eafd825eSMichael Ellerman * running on the emergency stack. 804eafd825eSMichael Ellerman */ 805b1576fecSAnton Blanchard bl early_setup_secondary 806f761622eSMatt Evans 80754a83404SMichael Neuling /* 808eafd825eSMichael Ellerman * The primary has initialized our kernel stack for us in the paca, grab 809eafd825eSMichael Ellerman * it and put it in r1. We must *not* use it until we turn on the MMU 810eafd825eSMichael Ellerman * below, because it may not be inside the RMO. 81154a83404SMichael Neuling */ 812eafd825eSMichael Ellerman ld r1, PACAKSAVE(r13) 81354a83404SMichael Neuling 814799d6046SPaul Mackerras /* Clear backchain so we get nice backtraces */ 81514cf11afSPaul Mackerras li r7,0 81614cf11afSPaul Mackerras mtlr r7 81714cf11afSPaul Mackerras 8187230c564SBenjamin Herrenschmidt /* Mark interrupts soft and hard disabled (they might be enabled 8197230c564SBenjamin Herrenschmidt * in the PACA when doing hotplug) 8207230c564SBenjamin Herrenschmidt */ 821c2e480baSMadhavan Srinivasan li r7,IRQS_DISABLED 8224e26bc4aSMadhavan Srinivasan stb r7,PACAIRQSOFTMASK(r13) 8237230c564SBenjamin Herrenschmidt li r0,PACA_IRQ_HARD_DIS 8247230c564SBenjamin Herrenschmidt stb r0,PACAIRQHAPPENED(r13) 8254f8cf36fSBenjamin Herrenschmidt 82614cf11afSPaul Mackerras /* enable MMU and jump to start_secondary */ 827ad0289e4SAnton Blanchard LOAD_REG_ADDR(r3, start_secondary_prolog) 828e58c3495SDavid Gibson LOAD_REG_IMMEDIATE(r4, MSR_KERNEL) 829d04c56f7SPaul Mackerras 830b5bbeb23SPaul Mackerras mtspr SPRN_SRR0,r3 831b5bbeb23SPaul Mackerras mtspr SPRN_SRR1,r4 8322d27cfd3SBenjamin Herrenschmidt RFI 83314cf11afSPaul Mackerras b . /* prevent speculative execution */ 83414cf11afSPaul Mackerras 83514cf11afSPaul Mackerras/* 83614cf11afSPaul Mackerras * Running with relocation on at this point. All we want to do is 837e31aa453SPaul Mackerras * zero the stack back-chain pointer and get the TOC virtual address 838e31aa453SPaul Mackerras * before going into C code. 83914cf11afSPaul Mackerras */ 840ad0289e4SAnton Blanchardstart_secondary_prolog: 841e31aa453SPaul Mackerras ld r2,PACATOC(r13) 84214cf11afSPaul Mackerras li r3,0 84314cf11afSPaul Mackerras std r3,0(r1) /* Zero the stack frame pointer */ 844b1576fecSAnton Blanchard bl start_secondary 845799d6046SPaul Mackerras b . 8468dbce53cSVaidyanathan Srinivasan/* 8478dbce53cSVaidyanathan Srinivasan * Reset stack pointer and call start_secondary 8488dbce53cSVaidyanathan Srinivasan * to continue with online operation when woken up 8498dbce53cSVaidyanathan Srinivasan * from cede in cpu offline. 8508dbce53cSVaidyanathan Srinivasan */ 8518dbce53cSVaidyanathan Srinivasan_GLOBAL(start_secondary_resume) 8528dbce53cSVaidyanathan Srinivasan ld r1,PACAKSAVE(r13) /* Reload kernel stack pointer */ 8538dbce53cSVaidyanathan Srinivasan li r3,0 8548dbce53cSVaidyanathan Srinivasan std r3,0(r1) /* Zero the stack frame pointer */ 855b1576fecSAnton Blanchard bl start_secondary 8568dbce53cSVaidyanathan Srinivasan b . 85714cf11afSPaul Mackerras#endif 85814cf11afSPaul Mackerras 85914cf11afSPaul Mackerras/* 86014cf11afSPaul Mackerras * This subroutine clobbers r11 and r12 86114cf11afSPaul Mackerras */ 8626a3bab90SAnton Blanchardenable_64b_mode: 86314cf11afSPaul Mackerras mfmsr r11 /* grab the current MSR */ 8642d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3E 8652d27cfd3SBenjamin Herrenschmidt oris r11,r11,0x8000 /* CM bit set, we'll set ICM later */ 8662d27cfd3SBenjamin Herrenschmidt mtmsr r11 8672d27cfd3SBenjamin Herrenschmidt#else /* CONFIG_PPC_BOOK3E */ 8689f0b0793SMichael Ellerman li r12,(MSR_64BIT | MSR_ISF)@highest 869e31aa453SPaul Mackerras sldi r12,r12,48 87014cf11afSPaul Mackerras or r11,r11,r12 87114cf11afSPaul Mackerras mtmsrd r11 87214cf11afSPaul Mackerras isync 8732d27cfd3SBenjamin Herrenschmidt#endif 87414cf11afSPaul Mackerras blr 87514cf11afSPaul Mackerras 87614cf11afSPaul Mackerras/* 877e31aa453SPaul Mackerras * This puts the TOC pointer into r2, offset by 0x8000 (as expected 878e31aa453SPaul Mackerras * by the toolchain). It computes the correct value for wherever we 879e31aa453SPaul Mackerras * are running at the moment, using position-independent code. 8801fbe9cf2SAnton Blanchard * 8811fbe9cf2SAnton Blanchard * Note: The compiler constructs pointers using offsets from the 8821fbe9cf2SAnton Blanchard * TOC in -mcmodel=medium mode. After we relocate to 0 but before 8831fbe9cf2SAnton Blanchard * the MMU is on we need our TOC to be a virtual address otherwise 8841fbe9cf2SAnton Blanchard * these pointers will be real addresses which may get stored and 8851fbe9cf2SAnton Blanchard * accessed later with the MMU on. We use tovirt() at the call 8861fbe9cf2SAnton Blanchard * sites to handle this. 887e31aa453SPaul Mackerras */ 888e31aa453SPaul Mackerras_GLOBAL(relative_toc) 889e31aa453SPaul Mackerras mflr r0 890e31aa453SPaul Mackerras bcl 20,31,$+4 891e550592eSBenjamin Herrenschmidt0: mflr r11 892e550592eSBenjamin Herrenschmidt ld r2,(p_toc - 0b)(r11) 893e550592eSBenjamin Herrenschmidt add r2,r2,r11 894e31aa453SPaul Mackerras mtlr r0 895e31aa453SPaul Mackerras blr 896e31aa453SPaul Mackerras 8975b63fee1SAnton Blanchard.balign 8 898eb039161STobin C. Hardingp_toc: .8byte __toc_start + 0x8000 - 0b 899e31aa453SPaul Mackerras 900e31aa453SPaul Mackerras/* 90114cf11afSPaul Mackerras * This is where the main kernel code starts. 90214cf11afSPaul Mackerras */ 9036a3bab90SAnton Blanchardstart_here_multiplatform: 9041fbe9cf2SAnton Blanchard /* set up the TOC */ 905b1576fecSAnton Blanchard bl relative_toc 9061fbe9cf2SAnton Blanchard tovirt(r2,r2) 90714cf11afSPaul Mackerras 90814cf11afSPaul Mackerras /* Clear out the BSS. It may have been done in prom_init, 90914cf11afSPaul Mackerras * already but that's irrelevant since prom_init will soon 91014cf11afSPaul Mackerras * be detached from the kernel completely. Besides, we need 91114cf11afSPaul Mackerras * to clear it now for kexec-style entry. 91214cf11afSPaul Mackerras */ 913e31aa453SPaul Mackerras LOAD_REG_ADDR(r11,__bss_stop) 914e31aa453SPaul Mackerras LOAD_REG_ADDR(r8,__bss_start) 91514cf11afSPaul Mackerras sub r11,r11,r8 /* bss size */ 91614cf11afSPaul Mackerras addi r11,r11,7 /* round up to an even double word */ 917e31aa453SPaul Mackerras srdi. r11,r11,3 /* shift right by 3 */ 91814cf11afSPaul Mackerras beq 4f 91914cf11afSPaul Mackerras addi r8,r8,-8 92014cf11afSPaul Mackerras li r0,0 92114cf11afSPaul Mackerras mtctr r11 /* zero this many doublewords */ 92214cf11afSPaul Mackerras3: stdu r0,8(r8) 92314cf11afSPaul Mackerras bdnz 3b 92414cf11afSPaul Mackerras4: 92514cf11afSPaul Mackerras 926daea1175SBenjamin Herrenschmidt#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL 927daea1175SBenjamin Herrenschmidt /* Setup OPAL entry */ 928ab7f961aSBenjamin Herrenschmidt LOAD_REG_ADDR(r11, opal) 929daea1175SBenjamin Herrenschmidt std r28,0(r11); 930daea1175SBenjamin Herrenschmidt std r29,8(r11); 931daea1175SBenjamin Herrenschmidt#endif 932daea1175SBenjamin Herrenschmidt 9332d27cfd3SBenjamin Herrenschmidt#ifndef CONFIG_PPC_BOOK3E 93414cf11afSPaul Mackerras mfmsr r6 93514cf11afSPaul Mackerras ori r6,r6,MSR_RI 93614cf11afSPaul Mackerras mtmsrd r6 /* RI on */ 9372d27cfd3SBenjamin Herrenschmidt#endif 93814cf11afSPaul Mackerras 939549e8152SPaul Mackerras#ifdef CONFIG_RELOCATABLE 940549e8152SPaul Mackerras /* Save the physical address we're running at in kernstart_addr */ 941549e8152SPaul Mackerras LOAD_REG_ADDR(r4, kernstart_addr) 942549e8152SPaul Mackerras clrldi r0,r25,2 943549e8152SPaul Mackerras std r0,0(r4) 944549e8152SPaul Mackerras#endif 945549e8152SPaul Mackerras 946e31aa453SPaul Mackerras /* The following gets the stack set up with the regs */ 94714cf11afSPaul Mackerras /* pointing to the real addr of the kernel stack. This is */ 94814cf11afSPaul Mackerras /* all done to support the C function call below which sets */ 94914cf11afSPaul Mackerras /* up the htab. This is done because we have relocated the */ 95014cf11afSPaul Mackerras /* kernel but are still running in real mode. */ 95114cf11afSPaul Mackerras 952e31aa453SPaul Mackerras LOAD_REG_ADDR(r3,init_thread_union) 95314cf11afSPaul Mackerras 954e31aa453SPaul Mackerras /* set up a stack pointer */ 955cabed148SHamish Martin LOAD_REG_IMMEDIATE(r1,THREAD_SIZE) 956cabed148SHamish Martin add r1,r3,r1 95714cf11afSPaul Mackerras li r0,0 95814cf11afSPaul Mackerras stdu r0,-STACK_FRAME_OVERHEAD(r1) 95914cf11afSPaul Mackerras 960376af594SMichael Ellerman /* 961376af594SMichael Ellerman * Do very early kernel initializations, including initial hash table 962376af594SMichael Ellerman * and SLB setup before we turn on relocation. 963376af594SMichael Ellerman */ 96414cf11afSPaul Mackerras 96514cf11afSPaul Mackerras /* Restore parameters passed from prom_init/kexec */ 96614cf11afSPaul Mackerras mr r3,r31 96756c46bbaSRussell Currey LOAD_REG_ADDR(r12, DOTSYM(early_setup)) 96856c46bbaSRussell Currey mtctr r12 96956c46bbaSRussell Currey bctrl /* also sets r13 and SPRG_PACA */ 97014cf11afSPaul Mackerras 971ad0289e4SAnton Blanchard LOAD_REG_ADDR(r3, start_here_common) 972e31aa453SPaul Mackerras ld r4,PACAKMSR(r13) 973b5bbeb23SPaul Mackerras mtspr SPRN_SRR0,r3 974b5bbeb23SPaul Mackerras mtspr SPRN_SRR1,r4 9752d27cfd3SBenjamin Herrenschmidt RFI 97614cf11afSPaul Mackerras b . /* prevent speculative execution */ 97714cf11afSPaul Mackerras 97814cf11afSPaul Mackerras /* This is where all platforms converge execution */ 979ad0289e4SAnton Blanchard 980ad0289e4SAnton Blanchardstart_here_common: 98114cf11afSPaul Mackerras /* relocation is on at this point */ 98214cf11afSPaul Mackerras std r1,PACAKSAVE(r13) 98314cf11afSPaul Mackerras 984e31aa453SPaul Mackerras /* Load the TOC (virtual address) */ 985e31aa453SPaul Mackerras ld r2,PACATOC(r13) 98614cf11afSPaul Mackerras 9877230c564SBenjamin Herrenschmidt /* Mark interrupts soft and hard disabled (they might be enabled 9887230c564SBenjamin Herrenschmidt * in the PACA when doing hotplug) 9897230c564SBenjamin Herrenschmidt */ 990c2e480baSMadhavan Srinivasan li r0,IRQS_DISABLED 9914e26bc4aSMadhavan Srinivasan stb r0,PACAIRQSOFTMASK(r13) 9927230c564SBenjamin Herrenschmidt li r0,PACA_IRQ_HARD_DIS 9937230c564SBenjamin Herrenschmidt stb r0,PACAIRQHAPPENED(r13) 99414cf11afSPaul Mackerras 9957230c564SBenjamin Herrenschmidt /* Generic kernel entry */ 996b1576fecSAnton Blanchard bl start_kernel 99714cf11afSPaul Mackerras 998f1870f77SAnton Blanchard /* Not reached */ 999f1870f77SAnton Blanchard BUG_OPCODE 100014cf11afSPaul Mackerras 100114cf11afSPaul Mackerras/* 100214cf11afSPaul Mackerras * We put a few things here that have to be page-aligned. 100314cf11afSPaul Mackerras * This stuff goes at the beginning of the bss, which is page-aligned. 100414cf11afSPaul Mackerras */ 100514cf11afSPaul Mackerras .section ".bss" 100643a5c684SAneesh Kumar K.V/* 100743a5c684SAneesh Kumar K.V * pgd dir should be aligned to PGD_TABLE_SIZE which is 64K. 100843a5c684SAneesh Kumar K.V * We will need to find a better way to fix this 100943a5c684SAneesh Kumar K.V */ 101043a5c684SAneesh Kumar K.V .align 16 101114cf11afSPaul Mackerras 101214cf11afSPaul Mackerras .globl swapper_pg_dir 101314cf11afSPaul Mackerrasswapper_pg_dir: 1014ee7a76daSStephen Rothwell .space PGD_TABLE_SIZE 101543a5c684SAneesh Kumar K.V 101643a5c684SAneesh Kumar K.V .globl empty_zero_page 101743a5c684SAneesh Kumar K.Vempty_zero_page: 101843a5c684SAneesh Kumar K.V .space PAGE_SIZE 10199445aa1aSAl ViroEXPORT_SYMBOL(empty_zero_page) 1020