12874c5fdSThomas 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 2129a011fcSSathvika Vasireddy#include <linux/linkage.h> 2214cf11afSPaul Mackerras#include <linux/threads.h> 23c141611fSPaul Gortmaker#include <linux/init.h> 24b5bbeb23SPaul Mackerras#include <asm/reg.h> 2514cf11afSPaul Mackerras#include <asm/page.h> 2614cf11afSPaul Mackerras#include <asm/mmu.h> 2714cf11afSPaul Mackerras#include <asm/ppc_asm.h> 2857f26649SNicholas Piggin#include <asm/head-64.h> 2914cf11afSPaul Mackerras#include <asm/asm-offsets.h> 3014cf11afSPaul Mackerras#include <asm/bug.h> 3114cf11afSPaul Mackerras#include <asm/cputable.h> 3214cf11afSPaul Mackerras#include <asm/setup.h> 3314cf11afSPaul Mackerras#include <asm/hvcall.h> 346cb7bfebSDavid Gibson#include <asm/thread_info.h> 353f639ee8SStephen Rothwell#include <asm/firmware.h> 3616a15a30SStephen Rothwell#include <asm/page_64.h> 37945feb17SBenjamin Herrenschmidt#include <asm/irqflags.h> 382191d657SAlexander Graf#include <asm/kvm_book3s_asm.h> 3946f52210SStephen Rothwell#include <asm/ptrace.h> 407230c564SBenjamin Herrenschmidt#include <asm/hw_irq.h> 416becef7eSchenhui zhao#include <asm/cputhreads.h> 427a25d912SScott Wood#include <asm/ppc-opcode.h> 432c86cd18SChristophe Leroy#include <asm/feature-fixups.h> 44879add77SChristophe Leroy#ifdef CONFIG_PPC_BOOK3S 45879add77SChristophe Leroy#include <asm/exception-64s.h> 46879add77SChristophe Leroy#else 47879add77SChristophe Leroy#include <asm/exception-64e.h> 48879add77SChristophe Leroy#endif 4914cf11afSPaul Mackerras 5025985edcSLucas De Marchi/* The physical memory is laid out such that the secondary processor 510ebc4cdaSBenjamin Herrenschmidt * spin code sits at 0x0000...0x00ff. On server, the vectors follow 520ebc4cdaSBenjamin Herrenschmidt * using the layout described in exceptions-64s.S 5314cf11afSPaul Mackerras */ 5414cf11afSPaul Mackerras 5514cf11afSPaul Mackerras/* 5614cf11afSPaul Mackerras * Entering into this code we make the following assumptions: 570ebc4cdaSBenjamin Herrenschmidt * 580ebc4cdaSBenjamin Herrenschmidt * For pSeries or server processors: 5914cf11afSPaul Mackerras * 1. The MMU is off & open firmware is running in real mode. 60339a3293SNicholas Piggin * 2. The primary CPU enters at __start. 61339a3293SNicholas Piggin * 3. If the RTAS supports "query-cpu-stopped-state", then secondary 62339a3293SNicholas Piggin * CPUs will enter as directed by "start-cpu" RTAS call, which is 63339a3293SNicholas Piggin * generic_secondary_smp_init, with PIR in r3. 64339a3293SNicholas Piggin * 4. Else the secondary CPUs will enter at secondary_hold (0x60) as 65339a3293SNicholas Piggin * directed by the "start-cpu" RTS call, with PIR in r3. 6627f44888SBenjamin Herrenschmidt * -or- For OPAL entry: 67339a3293SNicholas Piggin * 1. The MMU is off, processor in HV mode. 68339a3293SNicholas Piggin * 2. The primary CPU enters at 0 with device-tree in r3, OPAL base 69339a3293SNicholas Piggin * in r8, and entry in r9 for debugging purposes. 70339a3293SNicholas Piggin * 3. Secondary CPUs enter as directed by OPAL_START_CPU call, which 71339a3293SNicholas Piggin * is at generic_secondary_smp_init, with PIR in r3. 7214cf11afSPaul Mackerras * 730ebc4cdaSBenjamin Herrenschmidt * For Book3E processors: 740ebc4cdaSBenjamin Herrenschmidt * 1. The MMU is on running in AS0 in a state defined in ePAPR 750ebc4cdaSBenjamin Herrenschmidt * 2. The kernel is entered at __start 7614cf11afSPaul Mackerras */ 7714cf11afSPaul Mackerras 78b270bebdSNicholas Piggin/* 79b270bebdSNicholas Piggin * boot_from_prom and prom_init run at the physical address. Everything 80b270bebdSNicholas Piggin * after prom and kexec entry run at the virtual address (PAGE_OFFSET). 81b270bebdSNicholas Piggin * Secondaries run at the virtual address from generic_secondary_common_init 82b270bebdSNicholas Piggin * onward. 83b270bebdSNicholas Piggin */ 84b270bebdSNicholas Piggin 8557f26649SNicholas PigginOPEN_FIXED_SECTION(first_256B, 0x0, 0x100) 8657f26649SNicholas PigginUSE_FIXED_SECTION(first_256B) 8757f26649SNicholas Piggin /* 8857f26649SNicholas Piggin * Offsets are relative from the start of fixed section, and 8957f26649SNicholas Piggin * first_256B starts at 0. Offsets are a bit easier to use here 9057f26649SNicholas Piggin * than the fixed section entry macros. 9157f26649SNicholas Piggin */ 9257f26649SNicholas Piggin . = 0x0 9314cf11afSPaul Mackerras_GLOBAL(__start) 9414cf11afSPaul Mackerras /* NOP this out unconditionally */ 9514cf11afSPaul MackerrasBEGIN_FTR_SECTION 965c0484e2SBenjamin Herrenschmidt FIXUP_ENDIAN 97b1576fecSAnton Blanchard b __start_initialization_multiplatform 9814cf11afSPaul MackerrasEND_FTR_SECTION(0, 1) 9914cf11afSPaul Mackerras 10014cf11afSPaul Mackerras /* Catch branch to 0 in real mode */ 10114cf11afSPaul Mackerras trap 10214cf11afSPaul Mackerras 1032751b628SAnton Blanchard /* Secondary processors spin on this value until it becomes non-zero. 1042751b628SAnton Blanchard * When non-zero, it contains the real address of the function the cpu 1052751b628SAnton Blanchard * should jump to. 1061f6a93e4SPaul Mackerras */ 1077d4151b5SOlof Johansson .balign 8 10814cf11afSPaul Mackerras .globl __secondary_hold_spinloop 10914cf11afSPaul Mackerras__secondary_hold_spinloop: 110eb039161STobin C. Harding .8byte 0x0 11114cf11afSPaul Mackerras 11214cf11afSPaul Mackerras /* Secondary processors write this value with their cpu # */ 11314cf11afSPaul Mackerras /* after they enter the spin loop immediately below. */ 11414cf11afSPaul Mackerras .globl __secondary_hold_acknowledge 11514cf11afSPaul Mackerras__secondary_hold_acknowledge: 116eb039161STobin C. Harding .8byte 0x0 11714cf11afSPaul Mackerras 118928a3197SSonny Rao#ifdef CONFIG_RELOCATABLE 1198b8b0cc1SMilton Miller /* This flag is set to 1 by a loader if the kernel should run 1208b8b0cc1SMilton Miller * at the loaded address instead of the linked address. This 12187c78b61SMichael Ellerman * is used by kexec-tools to keep the kdump kernel in the 1228b8b0cc1SMilton Miller * crash_kernel region. The loader is responsible for 1238b8b0cc1SMilton Miller * observing the alignment requirement. 1248b8b0cc1SMilton Miller */ 12570839d20SNicholas Piggin 12670839d20SNicholas Piggin#ifdef CONFIG_RELOCATABLE_TEST 12770839d20SNicholas Piggin#define RUN_AT_LOAD_DEFAULT 1 /* Test relocation, do not copy to 0 */ 12870839d20SNicholas Piggin#else 12970839d20SNicholas Piggin#define RUN_AT_LOAD_DEFAULT 0x72756e30 /* "run0" -- relocate to 0 by default */ 13070839d20SNicholas Piggin#endif 13170839d20SNicholas Piggin 1328b8b0cc1SMilton Miller /* Do not move this variable as kexec-tools knows about it. */ 1338b8b0cc1SMilton Miller . = 0x5c 1348b8b0cc1SMilton Miller .globl __run_at_load 1358b8b0cc1SMilton Miller__run_at_load: 136d72c4a36SDaniel AxtensDEFINE_FIXED_SYMBOL(__run_at_load, first_256B) 13770839d20SNicholas Piggin .long RUN_AT_LOAD_DEFAULT 1388b8b0cc1SMilton Miller#endif 1398b8b0cc1SMilton Miller 14014cf11afSPaul Mackerras . = 0x60 14114cf11afSPaul Mackerras/* 14275423b7bSGeoff Levand * The following code is used to hold secondary processors 14375423b7bSGeoff Levand * in a spin loop after they have entered the kernel, but 14414cf11afSPaul Mackerras * before the bulk of the kernel has been relocated. This code 14514cf11afSPaul Mackerras * is relocated to physical address 0x60 before prom_init is run. 14614cf11afSPaul Mackerras * All of it must fit below the first exception vector at 0x100. 1471f6a93e4SPaul Mackerras * Use .globl here not _GLOBAL because we want __secondary_hold 1481f6a93e4SPaul Mackerras * to be the actual text address, not a descriptor. 14914cf11afSPaul Mackerras */ 1501f6a93e4SPaul Mackerras .globl __secondary_hold 1511f6a93e4SPaul Mackerras__secondary_hold: 1525c0484e2SBenjamin Herrenschmidt FIXUP_ENDIAN 153e0d68273SChristophe Leroy#ifndef CONFIG_PPC_BOOK3E_64 15414cf11afSPaul Mackerras mfmsr r24 15514cf11afSPaul Mackerras ori r24,r24,MSR_RI 15614cf11afSPaul Mackerras mtmsrd r24 /* RI on */ 1572d27cfd3SBenjamin Herrenschmidt#endif 158f1870f77SAnton Blanchard /* Grab our physical cpu number */ 15914cf11afSPaul Mackerras mr r24,r3 16096f013feSJimi Xenidis /* stash r4 for book3e */ 16196f013feSJimi Xenidis mr r25,r4 16214cf11afSPaul Mackerras 16314cf11afSPaul Mackerras /* Tell the master cpu we're here */ 16414cf11afSPaul Mackerras /* Relocation is off & we are located at an address less */ 16514cf11afSPaul Mackerras /* than 0x100, so only need to grab low order offset. */ 166d72c4a36SDaniel Axtens std r24,(ABS_ADDR(__secondary_hold_acknowledge, first_256B))(0) 16714cf11afSPaul Mackerras sync 16814cf11afSPaul Mackerras 16914cf11afSPaul Mackerras /* All secondary cpus wait here until told to start. */ 170ffc8e90dSNicholas Piggin100: ld r12,(ABS_ADDR(__secondary_hold_spinloop, first_256B))(0) 171cc7efbf9SAnton Blanchard cmpdi 0,r12,0 1721f6a93e4SPaul Mackerras beq 100b 17314cf11afSPaul Mackerras 174da665885SThiago Jung Bauermann#if defined(CONFIG_SMP) || defined(CONFIG_KEXEC_CORE) 175e0d68273SChristophe Leroy#ifdef CONFIG_PPC_BOOK3E_64 176cc7efbf9SAnton Blanchard tovirt(r12,r12) 17796f013feSJimi Xenidis#endif 178cc7efbf9SAnton Blanchard mtctr r12 17914cf11afSPaul Mackerras mr r3,r24 18096f013feSJimi Xenidis /* 18196f013feSJimi Xenidis * it may be the case that other platforms have r4 right to 18296f013feSJimi Xenidis * begin with, this gives us some safety in case it is not 18396f013feSJimi Xenidis */ 184e0d68273SChristophe Leroy#ifdef CONFIG_PPC_BOOK3E_64 18596f013feSJimi Xenidis mr r4,r25 18696f013feSJimi Xenidis#else 1872d27cfd3SBenjamin Herrenschmidt li r4,0 18896f013feSJimi Xenidis#endif 189dd797738SBenjamin Herrenschmidt /* Make sure that patched code is visible */ 190dd797738SBenjamin Herrenschmidt isync 191758438a7SMichael Ellerman bctr 19214cf11afSPaul Mackerras#else 19363ce271bSChristophe Leroy0: trap 19463ce271bSChristophe Leroy EMIT_BUG_ENTRY 0b, __FILE__, __LINE__, 0 19514cf11afSPaul Mackerras#endif 19657f26649SNicholas PigginCLOSE_FIXED_SECTION(first_256B) 19714cf11afSPaul Mackerras 19814cf11afSPaul Mackerras/* 1990ebc4cdaSBenjamin Herrenschmidt * On server, we include the exception vectors code here as it 2000ebc4cdaSBenjamin Herrenschmidt * relies on absolute addressing which is only possible within 2010ebc4cdaSBenjamin Herrenschmidt * this compilation unit 20214cf11afSPaul Mackerras */ 2030ebc4cdaSBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3S 2040ebc4cdaSBenjamin Herrenschmidt#include "exceptions-64s.S" 20557f26649SNicholas Piggin#else 20657f26649SNicholas PigginOPEN_TEXT_SECTION(0x100) 2071f6a93e4SPaul Mackerras#endif 20814cf11afSPaul Mackerras 20957f26649SNicholas PigginUSE_TEXT_SECTION() 21057f26649SNicholas Piggin 211e754f4d1SNicholas Piggin#include "interrupt_64.S" 212e754f4d1SNicholas Piggin 213e0d68273SChristophe Leroy#ifdef CONFIG_PPC_BOOK3E_64 214d17799f9Schenhui zhao/* 2156becef7eSchenhui zhao * The booting_thread_hwid holds the thread id we want to boot in cpu 2166becef7eSchenhui zhao * hotplug case. It is set by cpu hotplug code, and is invalid by default. 2176becef7eSchenhui zhao * The thread id is the same as the initial value of SPRN_PIR[THREAD_ID] 2186becef7eSchenhui zhao * bit field. 2196becef7eSchenhui zhao */ 2206becef7eSchenhui zhao .globl booting_thread_hwid 2216becef7eSchenhui zhaobooting_thread_hwid: 2226becef7eSchenhui zhao .long INVALID_THREAD_HWID 2236becef7eSchenhui zhao .align 3 2246becef7eSchenhui zhao/* 2256becef7eSchenhui zhao * start a thread in the same core 2266becef7eSchenhui zhao * input parameters: 2276becef7eSchenhui zhao * r3 = the thread physical id 2286becef7eSchenhui zhao * r4 = the entry point where thread starts 2296becef7eSchenhui zhao */ 2306becef7eSchenhui zhao_GLOBAL(book3e_start_thread) 2316becef7eSchenhui zhao LOAD_REG_IMMEDIATE(r5, MSR_KERNEL) 232f87f253bSNicholas Piggin cmpwi r3, 0 2336becef7eSchenhui zhao beq 10f 234f87f253bSNicholas Piggin cmpwi r3, 1 2356becef7eSchenhui zhao beq 11f 2366becef7eSchenhui zhao /* If the thread id is invalid, just exit. */ 2376becef7eSchenhui zhao b 13f 2386becef7eSchenhui zhao10: 2397a25d912SScott Wood MTTMR(TMRN_IMSR0, 5) 2407a25d912SScott Wood MTTMR(TMRN_INIA0, 4) 2416becef7eSchenhui zhao b 12f 2426becef7eSchenhui zhao11: 2437a25d912SScott Wood MTTMR(TMRN_IMSR1, 5) 2447a25d912SScott Wood MTTMR(TMRN_INIA1, 4) 2456becef7eSchenhui zhao12: 2466becef7eSchenhui zhao isync 2476becef7eSchenhui zhao li r6, 1 2486becef7eSchenhui zhao sld r6, r6, r3 2496becef7eSchenhui zhao mtspr SPRN_TENS, r6 2506becef7eSchenhui zhao13: 2516becef7eSchenhui zhao blr 2526becef7eSchenhui zhao 2536becef7eSchenhui zhao/* 254d17799f9Schenhui zhao * stop a thread in the same core 255d17799f9Schenhui zhao * input parameter: 256d17799f9Schenhui zhao * r3 = the thread physical id 257d17799f9Schenhui zhao */ 258d17799f9Schenhui zhao_GLOBAL(book3e_stop_thread) 259f87f253bSNicholas Piggin cmpwi r3, 0 260d17799f9Schenhui zhao beq 10f 261f87f253bSNicholas Piggin cmpwi r3, 1 262d17799f9Schenhui zhao beq 10f 263d17799f9Schenhui zhao /* If the thread id is invalid, just exit. */ 264d17799f9Schenhui zhao b 13f 265d17799f9Schenhui zhao10: 266d17799f9Schenhui zhao li r4, 1 267d17799f9Schenhui zhao sld r4, r4, r3 268d17799f9Schenhui zhao mtspr SPRN_TENC, r4 269d17799f9Schenhui zhao13: 270d17799f9Schenhui zhao blr 271d17799f9Schenhui zhao 272e16c8765SAndy Fleming_GLOBAL(fsl_secondary_thread_init) 273f34b3e19SScott Wood mfspr r4,SPRN_BUCSR 274f34b3e19SScott Wood 275e16c8765SAndy Fleming /* Enable branch prediction */ 276e16c8765SAndy Fleming lis r3,BUCSR_INIT@h 277e16c8765SAndy Fleming ori r3,r3,BUCSR_INIT@l 278e16c8765SAndy Fleming mtspr SPRN_BUCSR,r3 279e16c8765SAndy Fleming isync 280e16c8765SAndy Fleming 281e16c8765SAndy Fleming /* 282e16c8765SAndy Fleming * Fix PIR to match the linear numbering in the device tree. 283e16c8765SAndy Fleming * 284e16c8765SAndy Fleming * On e6500, the reset value of PIR uses the low three bits for 285e16c8765SAndy Fleming * the thread within a core, and the upper bits for the core 286e16c8765SAndy Fleming * number. There are two threads per core, so shift everything 287e16c8765SAndy Fleming * but the low bit right by two bits so that the cpu numbering is 288e16c8765SAndy Fleming * continuous. 289f34b3e19SScott Wood * 290f34b3e19SScott Wood * If the old value of BUCSR is non-zero, this thread has run 291f34b3e19SScott Wood * before. Thus, we assume we are coming from kexec or a similar 292f34b3e19SScott Wood * scenario, and PIR is already set to the correct value. This 293f34b3e19SScott Wood * is a bit of a hack, but there are limited opportunities for 294f34b3e19SScott Wood * getting information into the thread and the alternatives 295f34b3e19SScott Wood * seemed like they'd be overkill. We can't tell just by looking 296f34b3e19SScott Wood * at the old PIR value which state it's in, since the same value 297f34b3e19SScott Wood * could be valid for one thread out of reset and for a different 298f34b3e19SScott Wood * thread in Linux. 299e16c8765SAndy Fleming */ 300f34b3e19SScott Wood 301e16c8765SAndy Fleming mfspr r3, SPRN_PIR 302f34b3e19SScott Wood cmpwi r4,0 303f34b3e19SScott Wood bne 1f 304e16c8765SAndy Fleming rlwimi r3, r3, 30, 2, 30 305e16c8765SAndy Fleming mtspr SPRN_PIR, r3 306f34b3e19SScott Wood1: 30714cf11afSPaul Mackerras mr r24,r3 30814cf11afSPaul Mackerras 30914cf11afSPaul Mackerras /* turn on 64-bit mode */ 310b1576fecSAnton Blanchard bl enable_64b_mode 31114cf11afSPaul Mackerras 3122d27cfd3SBenjamin Herrenschmidt /* Book3E initialization */ 3132d27cfd3SBenjamin Herrenschmidt mr r3,r24 314b1576fecSAnton Blanchard bl book3e_secondary_thread_init 315b270bebdSNicholas Piggin bl relative_toc 316b270bebdSNicholas Piggin 3172d27cfd3SBenjamin Herrenschmidt b generic_secondary_common_init 3182d27cfd3SBenjamin Herrenschmidt 319e0d68273SChristophe Leroy#endif /* CONFIG_PPC_BOOK3E_64 */ 320529d2bd5SMichael Ellerman 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 3327e3a68beSNicholas Piggin 3337e3a68beSNicholas Piggin li r13,0 3347e3a68beSNicholas Piggin 3357e3a68beSNicholas Piggin /* Poison TOC */ 3367e3a68beSNicholas Piggin li r2,-1 3377e3a68beSNicholas Piggin 3382d27cfd3SBenjamin Herrenschmidt mr r24,r3 3392d27cfd3SBenjamin Herrenschmidt mr r25,r4 3402d27cfd3SBenjamin Herrenschmidt 3412d27cfd3SBenjamin Herrenschmidt /* turn on 64-bit mode */ 342b1576fecSAnton Blanchard bl enable_64b_mode 3432d27cfd3SBenjamin Herrenschmidt 344e0d68273SChristophe Leroy#ifdef CONFIG_PPC_BOOK3E_64 3452d27cfd3SBenjamin Herrenschmidt /* Book3E initialization */ 3462d27cfd3SBenjamin Herrenschmidt mr r3,r24 3472d27cfd3SBenjamin Herrenschmidt mr r4,r25 348b1576fecSAnton Blanchard bl book3e_secondary_core_init 349b270bebdSNicholas Piggin /* Now NIA and r2 are relocated to PAGE_OFFSET if not already */ 3506becef7eSchenhui zhao/* 3516becef7eSchenhui zhao * After common core init has finished, check if the current thread is the 3526becef7eSchenhui zhao * one we wanted to boot. If not, start the specified thread and stop the 3536becef7eSchenhui zhao * current thread. 3546becef7eSchenhui zhao */ 3556becef7eSchenhui zhao LOAD_REG_ADDR(r4, booting_thread_hwid) 3566becef7eSchenhui zhao lwz r3, 0(r4) 3576becef7eSchenhui zhao li r5, INVALID_THREAD_HWID 3586becef7eSchenhui zhao cmpw r3, r5 3596becef7eSchenhui zhao beq 20f 3606becef7eSchenhui zhao 3616becef7eSchenhui zhao /* 3626becef7eSchenhui zhao * The value of booting_thread_hwid has been stored in r3, 3636becef7eSchenhui zhao * so make it invalid. 3646becef7eSchenhui zhao */ 3656becef7eSchenhui zhao stw r5, 0(r4) 3666becef7eSchenhui zhao 3676becef7eSchenhui zhao /* 3686becef7eSchenhui zhao * Get the current thread id and check if it is the one we wanted. 3696becef7eSchenhui zhao * If not, start the one specified in booting_thread_hwid and stop 3706becef7eSchenhui zhao * the current thread. 3716becef7eSchenhui zhao */ 3726becef7eSchenhui zhao mfspr r8, SPRN_TIR 3736becef7eSchenhui zhao cmpw r3, r8 3746becef7eSchenhui zhao beq 20f 3756becef7eSchenhui zhao 3766becef7eSchenhui zhao /* start the specified thread */ 3772dc0bc11SMichael Ellerman LOAD_REG_ADDR(r5, DOTSYM(fsl_secondary_thread_init)) 3786becef7eSchenhui zhao bl book3e_start_thread 3796becef7eSchenhui zhao 3806becef7eSchenhui zhao /* stop the current thread */ 3816becef7eSchenhui zhao mr r3, r8 3826becef7eSchenhui zhao bl book3e_stop_thread 3836becef7eSchenhui zhao10: 3846becef7eSchenhui zhao b 10b 3856becef7eSchenhui zhao20: 386b270bebdSNicholas Piggin#else 387b270bebdSNicholas Piggin /* Now the MMU is off, can branch to our PAGE_OFFSET address */ 388b270bebdSNicholas Piggin bcl 20,31,$+4 389b270bebdSNicholas Piggin1: mflr r11 390b270bebdSNicholas Piggin addi r11,r11,(2f - 1b) 391b270bebdSNicholas Piggin tovirt(r11, r11) 392b270bebdSNicholas Piggin mtctr r11 393b270bebdSNicholas Piggin bctr 394b270bebdSNicholas Piggin2: 395b270bebdSNicholas Piggin bl relative_toc 3962d27cfd3SBenjamin Herrenschmidt#endif 3972d27cfd3SBenjamin Herrenschmidt 3982d27cfd3SBenjamin Herrenschmidtgeneric_secondary_common_init: 39914cf11afSPaul Mackerras /* Set up a paca value for this processor. Since we have the 40014cf11afSPaul Mackerras * physical cpu id in r24, we need to search the pacas to find 40114cf11afSPaul Mackerras * which logical id maps to our physical one. 40214cf11afSPaul Mackerras */ 403768d18adSMilton Miller#ifndef CONFIG_SMP 404b1576fecSAnton Blanchard b kexec_wait /* wait for next kernel if !SMP */ 405768d18adSMilton Miller#else 406d2e60075SNicholas Piggin LOAD_REG_ADDR(r8, paca_ptrs) /* Load paca_ptrs pointe */ 407d2e60075SNicholas Piggin ld r8,0(r8) /* Get base vaddr of array */ 408546a073dSYury Norov#if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS) 409546a073dSYury Norov LOAD_REG_IMMEDIATE(r7, NR_CPUS) 410546a073dSYury Norov#else 411768d18adSMilton Miller LOAD_REG_ADDR(r7, nr_cpu_ids) /* Load nr_cpu_ids address */ 412768d18adSMilton Miller lwz r7,0(r7) /* also the max paca allocated */ 413546a073dSYury Norov#endif 41414cf11afSPaul Mackerras li r5,0 /* logical cpu id */ 415d2e60075SNicholas Piggin1: 416d2e60075SNicholas Piggin sldi r9,r5,3 /* get paca_ptrs[] index from cpu id */ 417d2e60075SNicholas Piggin ldx r13,r9,r8 /* r13 = paca_ptrs[cpu id] */ 418d2e60075SNicholas Piggin lhz r6,PACAHWCPUID(r13) /* Load HW procid from paca */ 41914cf11afSPaul Mackerras cmpw r6,r24 /* Compare to our id */ 42014cf11afSPaul Mackerras beq 2f 42114cf11afSPaul Mackerras addi r5,r5,1 422768d18adSMilton Miller cmpw r5,r7 /* Check if more pacas exist */ 42314cf11afSPaul Mackerras blt 1b 42414cf11afSPaul Mackerras 42514cf11afSPaul Mackerras mr r3,r24 /* not found, copy phys to r3 */ 426b1576fecSAnton Blanchard b kexec_wait /* next kernel might do better */ 42714cf11afSPaul Mackerras 4282dd60d79SBenjamin Herrenschmidt2: SET_PACA(r13) 429e0d68273SChristophe Leroy#ifdef CONFIG_PPC_BOOK3E_64 4302d27cfd3SBenjamin Herrenschmidt addi r12,r13,PACA_EXTLB /* and TLB exc frame in another */ 4312d27cfd3SBenjamin Herrenschmidt mtspr SPRN_SPRG_TLB_EXFRAME,r12 4322d27cfd3SBenjamin Herrenschmidt#endif 4332d27cfd3SBenjamin Herrenschmidt 43414cf11afSPaul Mackerras /* From now on, r24 is expected to be logical cpuid */ 43514cf11afSPaul Mackerras mr r24,r5 436b6f6b98aSSonny Rao 4373c0b976bSJordan Niethe /* Create a temp kernel stack for use before relocation is on. */ 4383c0b976bSJordan Niethe ld r1,PACAEMERGSP(r13) 43990f1b431SNicholas Piggin subi r1,r1,STACK_FRAME_MIN_SIZE 4403c0b976bSJordan Niethe 441f39b7a55SOlof Johansson /* See if we need to call a cpu state restore handler */ 442e31aa453SPaul Mackerras LOAD_REG_ADDR(r23, cur_cpu_spec) 443f39b7a55SOlof Johansson ld r23,0(r23) 4442751b628SAnton Blanchard ld r12,CPU_SPEC_RESTORE(r23) 4452751b628SAnton Blanchard cmpdi 0,r12,0 4469d07bc84SBenjamin Herrenschmidt beq 3f 4477d40aff8SChristophe Leroy#ifdef CONFIG_PPC64_ELF_ABI_V1 4482751b628SAnton Blanchard ld r12,0(r12) 4492751b628SAnton Blanchard#endif 450cc7efbf9SAnton Blanchard mtctr r12 451f39b7a55SOlof Johansson bctrl 452f39b7a55SOlof Johansson 4537ac87abbSMatt Evans3: LOAD_REG_ADDR(r3, spinning_secondaries) /* Decrement spinning_secondaries */ 4549d07bc84SBenjamin Herrenschmidt lwarx r4,0,r3 4559d07bc84SBenjamin Herrenschmidt subi r4,r4,1 4569d07bc84SBenjamin Herrenschmidt stwcx. r4,0,r3 4579d07bc84SBenjamin Herrenschmidt bne 3b 4589d07bc84SBenjamin Herrenschmidt isync 4599d07bc84SBenjamin Herrenschmidt 4609d07bc84SBenjamin Herrenschmidt4: HMT_LOW 461ad0693eeSBenjamin Herrenschmidt lbz r23,PACAPROCSTART(r13) /* Test if this processor should */ 462ad0693eeSBenjamin Herrenschmidt /* start. */ 463ad0693eeSBenjamin Herrenschmidt cmpwi 0,r23,0 4649d07bc84SBenjamin Herrenschmidt beq 4b /* Loop until told to go */ 465ad0693eeSBenjamin Herrenschmidt 466ad0693eeSBenjamin Herrenschmidt sync /* order paca.run and cur_cpu_spec */ 4679d07bc84SBenjamin Herrenschmidt isync /* In case code patching happened */ 468ad0693eeSBenjamin Herrenschmidt 469c705677eSStephen Rothwell b __secondary_start 470768d18adSMilton Miller#endif /* SMP */ 47114cf11afSPaul Mackerras 472e31aa453SPaul Mackerras/* 473e31aa453SPaul Mackerras * Turn the MMU off. 474e31aa453SPaul Mackerras * Assumes we're mapped EA == RA if the MMU is on. 475e31aa453SPaul Mackerras */ 4762d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3S 47729a011fcSSathvika VasireddySYM_FUNC_START_LOCAL(__mmu_off) 47814cf11afSPaul Mackerras mfmsr r3 47914cf11afSPaul Mackerras andi. r0,r3,MSR_IR|MSR_DR 48014cf11afSPaul Mackerras beqlr 481e31aa453SPaul Mackerras mflr r4 48214cf11afSPaul Mackerras andc r3,r3,r0 48314cf11afSPaul Mackerras mtspr SPRN_SRR0,r4 48414cf11afSPaul Mackerras mtspr SPRN_SRR1,r3 48514cf11afSPaul Mackerras sync 48614cf11afSPaul Mackerras rfid 48714cf11afSPaul Mackerras b . /* prevent speculative execution */ 48829a011fcSSathvika VasireddySYM_FUNC_END(__mmu_off) 48914cf11afSPaul Mackerras 49038d73b67SSathvika VasireddySYM_FUNC_START_LOCAL(start_initialization_book3s) 49158f24eeaSNicholas Piggin mflr r25 49258f24eeaSNicholas Piggin 49358f24eeaSNicholas Piggin /* Setup some critical 970 SPRs before switching MMU off */ 49458f24eeaSNicholas Piggin mfspr r0,SPRN_PVR 49558f24eeaSNicholas Piggin srwi r0,r0,16 49658f24eeaSNicholas Piggin cmpwi r0,0x39 /* 970 */ 49758f24eeaSNicholas Piggin beq 1f 49858f24eeaSNicholas Piggin cmpwi r0,0x3c /* 970FX */ 49958f24eeaSNicholas Piggin beq 1f 50058f24eeaSNicholas Piggin cmpwi r0,0x44 /* 970MP */ 50158f24eeaSNicholas Piggin beq 1f 50258f24eeaSNicholas Piggin cmpwi r0,0x45 /* 970GX */ 50358f24eeaSNicholas Piggin bne 2f 50458f24eeaSNicholas Piggin1: bl __cpu_preinit_ppc970 50558f24eeaSNicholas Piggin2: 50658f24eeaSNicholas Piggin 50758f24eeaSNicholas Piggin /* Switch off MMU if not already off */ 50858f24eeaSNicholas Piggin bl __mmu_off 50958f24eeaSNicholas Piggin 510b270bebdSNicholas Piggin /* Now the MMU is off, can return to our PAGE_OFFSET address */ 511b270bebdSNicholas Piggin tovirt(r25,r25) 51258f24eeaSNicholas Piggin mtlr r25 51358f24eeaSNicholas Piggin blr 51438d73b67SSathvika VasireddySYM_FUNC_END(start_initialization_book3s) 51558f24eeaSNicholas Piggin#endif 51614cf11afSPaul Mackerras 51714cf11afSPaul Mackerras/* 51814cf11afSPaul Mackerras * Here is our main kernel entry point. We support currently 2 kind of entries 51914cf11afSPaul Mackerras * depending on the value of r5. 52014cf11afSPaul Mackerras * 52114cf11afSPaul Mackerras * r5 != NULL -> OF entry, we go to prom_init, "legacy" parameter content 52214cf11afSPaul Mackerras * in r3...r7 52314cf11afSPaul Mackerras * 52414cf11afSPaul Mackerras * r5 == NULL -> kexec style entry. r3 is a physical pointer to the 52514cf11afSPaul Mackerras * DT block, r4 is a physical pointer to the kernel itself 52614cf11afSPaul Mackerras * 52714cf11afSPaul Mackerras */ 5286a3bab90SAnton Blanchard__start_initialization_multiplatform: 529e31aa453SPaul Mackerras /* Make sure we are running in 64 bits mode */ 530b1576fecSAnton Blanchard bl enable_64b_mode 531e31aa453SPaul Mackerras 532e1100ceeSNicholas Piggin /* Zero r13 (paca) so early program check / mce don't use it */ 533e1100ceeSNicholas Piggin li r13,0 534e1100ceeSNicholas Piggin 5357e3a68beSNicholas Piggin /* Poison TOC */ 5367e3a68beSNicholas Piggin li r2,-1 5377e3a68beSNicholas Piggin 53814cf11afSPaul Mackerras /* 53914cf11afSPaul Mackerras * Are we booted from a PROM Of-type client-interface ? 54014cf11afSPaul Mackerras */ 54114cf11afSPaul Mackerras cmpldi cr0,r5,0 542939e60f6SStephen Rothwell beq 1f 543b1576fecSAnton Blanchard b __boot_from_prom /* yes -> prom */ 544939e60f6SStephen Rothwell1: 54514cf11afSPaul Mackerras /* Save parameters */ 54614cf11afSPaul Mackerras mr r31,r3 54714cf11afSPaul Mackerras mr r30,r4 548daea1175SBenjamin Herrenschmidt#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL 549daea1175SBenjamin Herrenschmidt /* Save OPAL entry */ 550daea1175SBenjamin Herrenschmidt mr r28,r8 551daea1175SBenjamin Herrenschmidt mr r29,r9 552daea1175SBenjamin Herrenschmidt#endif 55314cf11afSPaul Mackerras 5544f18b9e6SNicholas Piggin /* Get TOC pointer (current runtime address) */ 5554f18b9e6SNicholas Piggin bl relative_toc 5564f18b9e6SNicholas Piggin 557b270bebdSNicholas Piggin /* These functions return to the virtual (PAGE_OFFSET) address */ 558e0d68273SChristophe Leroy#ifdef CONFIG_PPC_BOOK3E_64 559b1576fecSAnton Blanchard bl start_initialization_book3e 5602d27cfd3SBenjamin Herrenschmidt#else 56158f24eeaSNicholas Piggin bl start_initialization_book3s 562e0d68273SChristophe Leroy#endif /* CONFIG_PPC_BOOK3E_64 */ 5634f18b9e6SNicholas Piggin 564b270bebdSNicholas Piggin /* Get TOC pointer, virtual */ 5654f18b9e6SNicholas Piggin bl relative_toc 5664f18b9e6SNicholas Piggin 5674f18b9e6SNicholas Piggin /* find out where we are now */ 568b270bebdSNicholas Piggin 569b270bebdSNicholas Piggin /* OPAL doesn't pass base address in r4, have to derive it. */ 5704f18b9e6SNicholas Piggin bcl 20,31,$+4 5714f18b9e6SNicholas Piggin0: mflr r26 /* r26 = runtime addr here */ 5724f18b9e6SNicholas Piggin addis r26,r26,(_stext - 0b)@ha 5734f18b9e6SNicholas Piggin addi r26,r26,(_stext - 0b)@l /* current runtime base addr */ 5744f18b9e6SNicholas Piggin 57558f24eeaSNicholas Piggin b __after_prom_start 57614cf11afSPaul Mackerras 5776eeb9b3bSMichael Ellerman__REF 5786a3bab90SAnton Blanchard__boot_from_prom: 57928794d34SBenjamin Herrenschmidt#ifdef CONFIG_PPC_OF_BOOT_TRAMPOLINE 580b270bebdSNicholas Piggin /* Get TOC pointer, non-virtual */ 5814f18b9e6SNicholas Piggin bl relative_toc 5824f18b9e6SNicholas Piggin 5834f18b9e6SNicholas Piggin /* find out where we are now */ 5844f18b9e6SNicholas Piggin bcl 20,31,$+4 5854f18b9e6SNicholas Piggin0: mflr r26 /* r26 = runtime addr here */ 5864f18b9e6SNicholas Piggin addis r26,r26,(_stext - 0b)@ha 5874f18b9e6SNicholas Piggin addi r26,r26,(_stext - 0b)@l /* current runtime base addr */ 5884f18b9e6SNicholas Piggin 58914cf11afSPaul Mackerras /* Save parameters */ 59014cf11afSPaul Mackerras mr r31,r3 59114cf11afSPaul Mackerras mr r30,r4 59214cf11afSPaul Mackerras mr r29,r5 59314cf11afSPaul Mackerras mr r28,r6 59414cf11afSPaul Mackerras mr r27,r7 59514cf11afSPaul Mackerras 5966088857bSOlaf Hering /* 5976088857bSOlaf Hering * Align the stack to 16-byte boundary 5986088857bSOlaf Hering * Depending on the size and layout of the ELF sections in the initial 599e31aa453SPaul Mackerras * boot binary, the stack pointer may be unaligned on PowerMac 6006088857bSOlaf Hering */ 601c05b4770SLinus Torvalds rldicr r1,r1,0,59 602c05b4770SLinus Torvalds 603549e8152SPaul Mackerras#ifdef CONFIG_RELOCATABLE 604549e8152SPaul Mackerras /* Relocate code for where we are now */ 605549e8152SPaul Mackerras mr r3,r26 606b1576fecSAnton Blanchard bl relocate 607549e8152SPaul Mackerras#endif 608549e8152SPaul Mackerras 60914cf11afSPaul Mackerras /* Restore parameters */ 61014cf11afSPaul Mackerras mr r3,r31 61114cf11afSPaul Mackerras mr r4,r30 61214cf11afSPaul Mackerras mr r5,r29 61314cf11afSPaul Mackerras mr r6,r28 61414cf11afSPaul Mackerras mr r7,r27 61514cf11afSPaul Mackerras 61614cf11afSPaul Mackerras /* Do all of the interaction with OF client interface */ 617549e8152SPaul Mackerras mr r8,r26 6184e991e3cSNicholas Piggin bl CFUNC(prom_init) 61928794d34SBenjamin Herrenschmidt#endif /* #CONFIG_PPC_OF_BOOT_TRAMPOLINE */ 62028794d34SBenjamin Herrenschmidt 62128794d34SBenjamin Herrenschmidt /* We never return. We also hit that trap if trying to boot 62228794d34SBenjamin Herrenschmidt * from OF while CONFIG_PPC_OF_BOOT_TRAMPOLINE isn't selected */ 62314cf11afSPaul Mackerras trap 6246eeb9b3bSMichael Ellerman .previous 62514cf11afSPaul Mackerras 6266a3bab90SAnton Blanchard__after_prom_start: 627549e8152SPaul Mackerras#ifdef CONFIG_RELOCATABLE 628549e8152SPaul Mackerras /* process relocations for the final address of the kernel */ 62957f26649SNicholas Piggin lwz r7,(FIXED_SYMBOL_ABS_ADDR(__run_at_load))(r26) 630928a3197SSonny Rao cmplwi cr0,r7,1 /* flagged to stay where we are ? */ 631b270bebdSNicholas Piggin mr r25,r26 /* then use current kernel base */ 632b270bebdSNicholas Piggin beq 1f 633b270bebdSNicholas Piggin LOAD_REG_IMMEDIATE(r25, PAGE_OFFSET) /* else use static kernel base */ 63454622f10SMohan Kumar M1: mr r3,r25 635b1576fecSAnton Blanchard bl relocate 636e0d68273SChristophe Leroy#if defined(CONFIG_PPC_BOOK3E_64) 6371cb6e064STiejun Chen /* IVPR needs to be set after relocation. */ 6381cb6e064STiejun Chen bl init_core_book3e 6391cb6e064STiejun Chen#endif 640549e8152SPaul Mackerras#endif 64114cf11afSPaul Mackerras 64214cf11afSPaul Mackerras/* 643e31aa453SPaul Mackerras * We need to run with _stext at physical address PHYSICAL_START. 64414cf11afSPaul Mackerras * This will leave some code in the first 256B of 64514cf11afSPaul Mackerras * real memory, which are reserved for software use. 64614cf11afSPaul Mackerras * 64714cf11afSPaul Mackerras * Note: This process overwrites the OF exception vectors. 64814cf11afSPaul Mackerras */ 649b270bebdSNicholas Piggin LOAD_REG_IMMEDIATE(r3, PAGE_OFFSET) 650*c6036baeSJinglin Wen mr r4,r26 /* Load the virtual source address into r4 */ 651*c6036baeSJinglin Wen cmpld r3,r4 /* Check if source == dest */ 652*c6036baeSJinglin Wen beq 9f /* If so skip the copy */ 65314cf11afSPaul Mackerras li r6,0x100 /* Start offset, the first 0x100 */ 65414cf11afSPaul Mackerras /* bytes were copied earlier. */ 65514cf11afSPaul Mackerras 65611ee7e99SAnton Blanchard#ifdef CONFIG_RELOCATABLE 65754622f10SMohan Kumar M/* 65854622f10SMohan Kumar M * Check if the kernel has to be running as relocatable kernel based on the 6598b8b0cc1SMilton Miller * variable __run_at_load, if it is set the kernel is treated as relocatable 66054622f10SMohan Kumar M * kernel, otherwise it will be moved to PHYSICAL_START 66154622f10SMohan Kumar M */ 66257f26649SNicholas Piggin lwz r7,(FIXED_SYMBOL_ABS_ADDR(__run_at_load))(r26) 6638b8b0cc1SMilton Miller cmplwi cr0,r7,1 66454622f10SMohan Kumar M bne 3f 66554622f10SMohan Kumar M 666e0d68273SChristophe Leroy#ifdef CONFIG_PPC_BOOK3E_64 6671cb6e064STiejun Chen LOAD_REG_ADDR(r5, __end_interrupts) 6681cb6e064STiejun Chen LOAD_REG_ADDR(r11, _stext) 6691cb6e064STiejun Chen sub r5,r5,r11 6701cb6e064STiejun Chen#else 671c1fb6816SMichael Neuling /* just copy interrupts */ 672d7fb5b18SChristophe Leroy LOAD_REG_IMMEDIATE_SYM(r5, r11, FIXED_SYMBOL_ABS_ADDR(__end_interrupts)) 6731cb6e064STiejun Chen#endif 67454622f10SMohan Kumar M b 5f 67554622f10SMohan Kumar M3: 67654622f10SMohan Kumar M#endif 67757f26649SNicholas Piggin /* # bytes of memory to copy */ 678d72c4a36SDaniel Axtens lis r5,(ABS_ADDR(copy_to_here, text))@ha 679d72c4a36SDaniel Axtens addi r5,r5,(ABS_ADDR(copy_to_here, text))@l 68054622f10SMohan Kumar M 681b1576fecSAnton Blanchard bl copy_and_flush /* copy the first n bytes */ 68214cf11afSPaul Mackerras /* this includes the code being */ 68314cf11afSPaul Mackerras /* executed here. */ 68457f26649SNicholas Piggin /* Jump to the copy of this code that we just made */ 685d72c4a36SDaniel Axtens addis r8,r3,(ABS_ADDR(4f, text))@ha 686d72c4a36SDaniel Axtens addi r12,r8,(ABS_ADDR(4f, text))@l 687cc7efbf9SAnton Blanchard mtctr r12 68814cf11afSPaul Mackerras bctr 68914cf11afSPaul Mackerras 690286e4f90SAnton Blanchard.balign 8 691eb039161STobin C. Hardingp_end: .8byte _end - copy_to_here 69254622f10SMohan Kumar M 693573819e3SNicholas Piggin4: 694573819e3SNicholas Piggin /* 695573819e3SNicholas Piggin * Now copy the rest of the kernel up to _end, add 696573819e3SNicholas Piggin * _end - copy_to_here to the copy limit and run again. 697573819e3SNicholas Piggin */ 698d72c4a36SDaniel Axtens addis r8,r26,(ABS_ADDR(p_end, text))@ha 699d72c4a36SDaniel Axtens ld r8,(ABS_ADDR(p_end, text))@l(r8) 700573819e3SNicholas Piggin add r5,r5,r8 701b1576fecSAnton Blanchard5: bl copy_and_flush /* copy the rest */ 702e31aa453SPaul Mackerras 703b1576fecSAnton Blanchard9: b start_here_multiplatform 704e31aa453SPaul Mackerras 70514cf11afSPaul Mackerras/* 70614cf11afSPaul Mackerras * Copy routine used to copy the kernel to start at physical address 0 70714cf11afSPaul Mackerras * and flush and invalidate the caches as needed. 70814cf11afSPaul Mackerras * r3 = dest addr, r4 = source addr, r5 = copy limit, r6 = start offset 70914cf11afSPaul Mackerras * on exit, r3, r4, r5 are unchanged, r6 is updated to be >= r5. 71014cf11afSPaul Mackerras * 71114cf11afSPaul Mackerras * Note: this routine *only* clobbers r0, r6 and lr 71214cf11afSPaul Mackerras */ 71314cf11afSPaul Mackerras_GLOBAL(copy_and_flush) 71414cf11afSPaul Mackerras addi r5,r5,-8 71514cf11afSPaul Mackerras addi r6,r6,-8 7165a2fe38dSOlof Johansson4: li r0,8 /* Use the smallest common */ 71714cf11afSPaul Mackerras /* denominator cache line */ 71814cf11afSPaul Mackerras /* size. This results in */ 71914cf11afSPaul Mackerras /* extra cache line flushes */ 72014cf11afSPaul Mackerras /* but operation is correct. */ 72114cf11afSPaul Mackerras /* Can't get cache line size */ 72214cf11afSPaul Mackerras /* from NACA as it is being */ 72314cf11afSPaul Mackerras /* moved too. */ 72414cf11afSPaul Mackerras 72514cf11afSPaul Mackerras mtctr r0 /* put # words/line in ctr */ 72614cf11afSPaul Mackerras3: addi r6,r6,8 /* copy a cache line */ 72714cf11afSPaul Mackerras ldx r0,r6,r4 72814cf11afSPaul Mackerras stdx r0,r6,r3 72914cf11afSPaul Mackerras bdnz 3b 73014cf11afSPaul Mackerras dcbst r6,r3 /* write it to memory */ 73114cf11afSPaul Mackerras sync 73214cf11afSPaul Mackerras icbi r6,r3 /* flush the icache line */ 73314cf11afSPaul Mackerras cmpld 0,r6,r5 73414cf11afSPaul Mackerras blt 4b 73514cf11afSPaul Mackerras sync 73614cf11afSPaul Mackerras addi r5,r5,8 73714cf11afSPaul Mackerras addi r6,r6,8 73829ce3c50SMichael Neuling isync 73914cf11afSPaul Mackerras blr 74014cf11afSPaul Mackerras 7418119cefdSHari Bathini_ASM_NOKPROBE_SYMBOL(copy_and_flush); /* Called in real mode */ 7428119cefdSHari Bathini 74314cf11afSPaul Mackerras.align 8 74414cf11afSPaul Mackerrascopy_to_here: 74514cf11afSPaul Mackerras 74614cf11afSPaul Mackerras#ifdef CONFIG_SMP 74714cf11afSPaul Mackerras#ifdef CONFIG_PPC_PMAC 74814cf11afSPaul Mackerras/* 74914cf11afSPaul Mackerras * On PowerMac, secondary processors starts from the reset vector, which 75014cf11afSPaul Mackerras * is temporarily turned into a call to one of the functions below. 75114cf11afSPaul Mackerras */ 75214cf11afSPaul Mackerras .section ".text"; 75314cf11afSPaul Mackerras .align 2 ; 75414cf11afSPaul Mackerras 75535499c01SPaul Mackerras .globl __secondary_start_pmac_0 75635499c01SPaul Mackerras__secondary_start_pmac_0: 75735499c01SPaul Mackerras /* NB the entries for cpus 0, 1, 2 must each occupy 8 bytes. */ 75835499c01SPaul Mackerras li r24,0 75935499c01SPaul Mackerras b 1f 76014cf11afSPaul Mackerras li r24,1 76135499c01SPaul Mackerras b 1f 76214cf11afSPaul Mackerras li r24,2 76335499c01SPaul Mackerras b 1f 76414cf11afSPaul Mackerras li r24,3 76535499c01SPaul Mackerras1: 76614cf11afSPaul Mackerras 76714cf11afSPaul Mackerras_GLOBAL(pmac_secondary_start) 76814cf11afSPaul Mackerras /* turn on 64-bit mode */ 769b1576fecSAnton Blanchard bl enable_64b_mode 77014cf11afSPaul Mackerras 771c478b581SBenjamin Herrenschmidt li r0,0 772c478b581SBenjamin Herrenschmidt mfspr r3,SPRN_HID4 773c478b581SBenjamin Herrenschmidt rldimi r3,r0,40,23 /* clear bit 23 (rm_ci) */ 774c478b581SBenjamin Herrenschmidt sync 775c478b581SBenjamin Herrenschmidt mtspr SPRN_HID4,r3 776c478b581SBenjamin Herrenschmidt isync 777c478b581SBenjamin Herrenschmidt sync 778c478b581SBenjamin Herrenschmidt slbia 779c478b581SBenjamin Herrenschmidt 780b270bebdSNicholas Piggin /* Branch to our PAGE_OFFSET address */ 781b270bebdSNicholas Piggin bcl 20,31,$+4 782b270bebdSNicholas Piggin1: mflr r11 783b270bebdSNicholas Piggin addi r11,r11,(2f - 1b) 784b270bebdSNicholas Piggin tovirt(r11, r11) 785b270bebdSNicholas Piggin mtctr r11 786b270bebdSNicholas Piggin bctr 787b270bebdSNicholas Piggin2: 788b1576fecSAnton Blanchard bl relative_toc 789e31aa453SPaul Mackerras 79014cf11afSPaul Mackerras /* Copy some CPU settings from CPU 0 */ 791b1576fecSAnton Blanchard bl __restore_cpu_ppc970 79214cf11afSPaul Mackerras 79314cf11afSPaul Mackerras /* pSeries do that early though I don't think we really need it */ 79414cf11afSPaul Mackerras mfmsr r3 79514cf11afSPaul Mackerras ori r3,r3,MSR_RI 79614cf11afSPaul Mackerras mtmsrd r3 /* RI on */ 79714cf11afSPaul Mackerras 79814cf11afSPaul Mackerras /* Set up a paca value for this processor. */ 799d2e60075SNicholas Piggin LOAD_REG_ADDR(r4,paca_ptrs) /* Load paca pointer */ 800d2e60075SNicholas Piggin ld r4,0(r4) /* Get base vaddr of paca_ptrs array */ 801d2e60075SNicholas Piggin sldi r5,r24,3 /* get paca_ptrs[] index from cpu id */ 802d2e60075SNicholas Piggin ldx r13,r5,r4 /* r13 = paca_ptrs[cpu id] */ 8032dd60d79SBenjamin Herrenschmidt SET_PACA(r13) /* Save vaddr of paca in an SPRG*/ 80414cf11afSPaul Mackerras 80562cc67b9SBenjamin Herrenschmidt /* Mark interrupts soft and hard disabled (they might be enabled 80662cc67b9SBenjamin Herrenschmidt * in the PACA when doing hotplug) 80762cc67b9SBenjamin Herrenschmidt */ 808c2e480baSMadhavan Srinivasan li r0,IRQS_DISABLED 8094e26bc4aSMadhavan Srinivasan stb r0,PACAIRQSOFTMASK(r13) 8107230c564SBenjamin Herrenschmidt li r0,PACA_IRQ_HARD_DIS 8117230c564SBenjamin Herrenschmidt stb r0,PACAIRQHAPPENED(r13) 81262cc67b9SBenjamin Herrenschmidt 81314cf11afSPaul Mackerras /* Create a temp kernel stack for use before relocation is on. */ 81414cf11afSPaul Mackerras ld r1,PACAEMERGSP(r13) 81590f1b431SNicholas Piggin subi r1,r1,STACK_FRAME_MIN_SIZE 81614cf11afSPaul Mackerras 817c705677eSStephen Rothwell b __secondary_start 81814cf11afSPaul Mackerras 81914cf11afSPaul Mackerras#endif /* CONFIG_PPC_PMAC */ 82014cf11afSPaul Mackerras 82114cf11afSPaul Mackerras/* 82214cf11afSPaul Mackerras * This function is called after the master CPU has released the 82314cf11afSPaul Mackerras * secondary processors. The execution environment is relocation off. 82414cf11afSPaul Mackerras * The paca for this processor has the following fields initialized at 82514cf11afSPaul Mackerras * this point: 82614cf11afSPaul Mackerras * 1. Processor number 82714cf11afSPaul Mackerras * 2. Segment table pointer (virtual address) 82814cf11afSPaul Mackerras * On entry the following are set: 8294f8cf36fSBenjamin Herrenschmidt * r1 = stack pointer (real addr of temp stack) 83014cf11afSPaul Mackerras * r24 = cpu# (in Linux terms) 83114cf11afSPaul Mackerras * r13 = paca virtual address 832ee43eb78SBenjamin Herrenschmidt * SPRG_PACA = paca virtual address 83314cf11afSPaul Mackerras */ 8342d27cfd3SBenjamin Herrenschmidt .section ".text"; 8352d27cfd3SBenjamin Herrenschmidt .align 2 ; 8362d27cfd3SBenjamin Herrenschmidt 837fc68e869SStephen Rothwell .globl __secondary_start 838c705677eSStephen Rothwell__secondary_start: 839799d6046SPaul Mackerras /* Set thread priority to MEDIUM */ 840799d6046SPaul Mackerras HMT_MEDIUM 84114cf11afSPaul Mackerras 842eafd825eSMichael Ellerman /* 843eafd825eSMichael Ellerman * Do early setup for this CPU, in particular initialising the MMU so we 844eafd825eSMichael Ellerman * can turn it on below. This is a call to C, which is OK, we're still 845eafd825eSMichael Ellerman * running on the emergency stack. 846eafd825eSMichael Ellerman */ 8474e991e3cSNicholas Piggin bl CFUNC(early_setup_secondary) 848f761622eSMatt Evans 84954a83404SMichael Neuling /* 850eafd825eSMichael Ellerman * The primary has initialized our kernel stack for us in the paca, grab 851eafd825eSMichael Ellerman * it and put it in r1. We must *not* use it until we turn on the MMU 852eafd825eSMichael Ellerman * below, because it may not be inside the RMO. 85354a83404SMichael Neuling */ 854eafd825eSMichael Ellerman ld r1, PACAKSAVE(r13) 85554a83404SMichael Neuling 856799d6046SPaul Mackerras /* Clear backchain so we get nice backtraces */ 85714cf11afSPaul Mackerras li r7,0 85814cf11afSPaul Mackerras mtlr r7 85914cf11afSPaul Mackerras 8607230c564SBenjamin Herrenschmidt /* Mark interrupts soft and hard disabled (they might be enabled 8617230c564SBenjamin Herrenschmidt * in the PACA when doing hotplug) 8627230c564SBenjamin Herrenschmidt */ 863c2e480baSMadhavan Srinivasan li r7,IRQS_DISABLED 8644e26bc4aSMadhavan Srinivasan stb r7,PACAIRQSOFTMASK(r13) 8657230c564SBenjamin Herrenschmidt li r0,PACA_IRQ_HARD_DIS 8667230c564SBenjamin Herrenschmidt stb r0,PACAIRQHAPPENED(r13) 8674f8cf36fSBenjamin Herrenschmidt 86814cf11afSPaul Mackerras /* enable MMU and jump to start_secondary */ 869ad0289e4SAnton Blanchard LOAD_REG_ADDR(r3, start_secondary_prolog) 870e58c3495SDavid Gibson LOAD_REG_IMMEDIATE(r4, MSR_KERNEL) 871d04c56f7SPaul Mackerras 872b5bbeb23SPaul Mackerras mtspr SPRN_SRR0,r3 873b5bbeb23SPaul Mackerras mtspr SPRN_SRR1,r4 874879add77SChristophe Leroy RFI_TO_KERNEL 87514cf11afSPaul Mackerras b . /* prevent speculative execution */ 87614cf11afSPaul Mackerras 87714cf11afSPaul Mackerras/* 87814cf11afSPaul Mackerras * Running with relocation on at this point. All we want to do is 879e31aa453SPaul Mackerras * zero the stack back-chain pointer and get the TOC virtual address 880e31aa453SPaul Mackerras * before going into C code. 88114cf11afSPaul Mackerras */ 882ad0289e4SAnton Blanchardstart_secondary_prolog: 8838e93fb33SNicholas Piggin LOAD_PACA_TOC() 88414cf11afSPaul Mackerras li r3,0 88514cf11afSPaul Mackerras std r3,0(r1) /* Zero the stack frame pointer */ 8864e991e3cSNicholas Piggin bl CFUNC(start_secondary) 887799d6046SPaul Mackerras b . 8888dbce53cSVaidyanathan Srinivasan/* 8898dbce53cSVaidyanathan Srinivasan * Reset stack pointer and call start_secondary 8908dbce53cSVaidyanathan Srinivasan * to continue with online operation when woken up 8918dbce53cSVaidyanathan Srinivasan * from cede in cpu offline. 8928dbce53cSVaidyanathan Srinivasan */ 8938dbce53cSVaidyanathan Srinivasan_GLOBAL(start_secondary_resume) 8948dbce53cSVaidyanathan Srinivasan ld r1,PACAKSAVE(r13) /* Reload kernel stack pointer */ 8958dbce53cSVaidyanathan Srinivasan li r3,0 8968dbce53cSVaidyanathan Srinivasan std r3,0(r1) /* Zero the stack frame pointer */ 8974e991e3cSNicholas Piggin bl CFUNC(start_secondary) 8988dbce53cSVaidyanathan Srinivasan b . 89914cf11afSPaul Mackerras#endif 90014cf11afSPaul Mackerras 90114cf11afSPaul Mackerras/* 90214cf11afSPaul Mackerras * This subroutine clobbers r11 and r12 90314cf11afSPaul Mackerras */ 90429a011fcSSathvika VasireddySYM_FUNC_START_LOCAL(enable_64b_mode) 90514cf11afSPaul Mackerras mfmsr r11 /* grab the current MSR */ 906e0d68273SChristophe Leroy#ifdef CONFIG_PPC_BOOK3E_64 9072d27cfd3SBenjamin Herrenschmidt oris r11,r11,0x8000 /* CM bit set, we'll set ICM later */ 9082d27cfd3SBenjamin Herrenschmidt mtmsr r11 909e0d68273SChristophe Leroy#else /* CONFIG_PPC_BOOK3E_64 */ 910e89a8ca9SNicholas Piggin LOAD_REG_IMMEDIATE(r12, MSR_64BIT) 91114cf11afSPaul Mackerras or r11,r11,r12 91214cf11afSPaul Mackerras mtmsrd r11 91314cf11afSPaul Mackerras isync 9142d27cfd3SBenjamin Herrenschmidt#endif 91514cf11afSPaul Mackerras blr 91629a011fcSSathvika VasireddySYM_FUNC_END(enable_64b_mode) 91714cf11afSPaul Mackerras 91814cf11afSPaul Mackerras/* 919e31aa453SPaul Mackerras * This puts the TOC pointer into r2, offset by 0x8000 (as expected 920e31aa453SPaul Mackerras * by the toolchain). It computes the correct value for wherever we 921e31aa453SPaul Mackerras * are running at the moment, using position-independent code. 9221fbe9cf2SAnton Blanchard * 9231fbe9cf2SAnton Blanchard * Note: The compiler constructs pointers using offsets from the 9241fbe9cf2SAnton Blanchard * TOC in -mcmodel=medium mode. After we relocate to 0 but before 9251fbe9cf2SAnton Blanchard * the MMU is on we need our TOC to be a virtual address otherwise 9261fbe9cf2SAnton Blanchard * these pointers will be real addresses which may get stored and 927b270bebdSNicholas Piggin * accessed later with the MMU on. We branch to the virtual address 928b270bebdSNicholas Piggin * while still in real mode then call relative_toc again to handle 929b270bebdSNicholas Piggin * this. 930e31aa453SPaul Mackerras */ 931e31aa453SPaul Mackerras_GLOBAL(relative_toc) 9327e3a68beSNicholas Piggin#ifdef CONFIG_PPC_KERNEL_PCREL 9337e3a68beSNicholas Piggin tdnei r2,-1 9347e3a68beSNicholas Piggin blr 9357e3a68beSNicholas Piggin#else 936e31aa453SPaul Mackerras mflr r0 937e31aa453SPaul Mackerras bcl 20,31,$+4 938e550592eSBenjamin Herrenschmidt0: mflr r11 939e550592eSBenjamin Herrenschmidt ld r2,(p_toc - 0b)(r11) 940e550592eSBenjamin Herrenschmidt add r2,r2,r11 941e31aa453SPaul Mackerras mtlr r0 942e31aa453SPaul Mackerras blr 943e31aa453SPaul Mackerras 9445b63fee1SAnton Blanchard.balign 8 945a3ad84daSAlan Modrap_toc: .8byte .TOC. - 0b 9467e3a68beSNicholas Piggin#endif 947e31aa453SPaul Mackerras 948e31aa453SPaul Mackerras/* 94914cf11afSPaul Mackerras * This is where the main kernel code starts. 95014cf11afSPaul Mackerras */ 9519c4e4c90SChristophe Leroy__REF 9526a3bab90SAnton Blanchardstart_here_multiplatform: 953b270bebdSNicholas Piggin /* Adjust TOC for moved kernel. Could adjust when moving it instead. */ 954b1576fecSAnton Blanchard bl relative_toc 95514cf11afSPaul Mackerras 95614cf11afSPaul Mackerras /* Clear out the BSS. It may have been done in prom_init, 95714cf11afSPaul Mackerras * already but that's irrelevant since prom_init will soon 95814cf11afSPaul Mackerras * be detached from the kernel completely. Besides, we need 95914cf11afSPaul Mackerras * to clear it now for kexec-style entry. 96014cf11afSPaul Mackerras */ 961e31aa453SPaul Mackerras LOAD_REG_ADDR(r11,__bss_stop) 962e31aa453SPaul Mackerras LOAD_REG_ADDR(r8,__bss_start) 96314cf11afSPaul Mackerras sub r11,r11,r8 /* bss size */ 96414cf11afSPaul Mackerras addi r11,r11,7 /* round up to an even double word */ 965e31aa453SPaul Mackerras srdi. r11,r11,3 /* shift right by 3 */ 96614cf11afSPaul Mackerras beq 4f 96714cf11afSPaul Mackerras addi r8,r8,-8 96814cf11afSPaul Mackerras li r0,0 96914cf11afSPaul Mackerras mtctr r11 /* zero this many doublewords */ 97014cf11afSPaul Mackerras3: stdu r0,8(r8) 97114cf11afSPaul Mackerras bdnz 3b 97214cf11afSPaul Mackerras4: 97314cf11afSPaul Mackerras 974daea1175SBenjamin Herrenschmidt#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL 975daea1175SBenjamin Herrenschmidt /* Setup OPAL entry */ 976ab7f961aSBenjamin Herrenschmidt LOAD_REG_ADDR(r11, opal) 977daea1175SBenjamin Herrenschmidt std r28,0(r11); 978daea1175SBenjamin Herrenschmidt std r29,8(r11); 979daea1175SBenjamin Herrenschmidt#endif 980daea1175SBenjamin Herrenschmidt 981e0d68273SChristophe Leroy#ifndef CONFIG_PPC_BOOK3E_64 98214cf11afSPaul Mackerras mfmsr r6 98314cf11afSPaul Mackerras ori r6,r6,MSR_RI 98414cf11afSPaul Mackerras mtmsrd r6 /* RI on */ 9852d27cfd3SBenjamin Herrenschmidt#endif 98614cf11afSPaul Mackerras 987549e8152SPaul Mackerras#ifdef CONFIG_RELOCATABLE 988549e8152SPaul Mackerras /* Save the physical address we're running at in kernstart_addr */ 989549e8152SPaul Mackerras LOAD_REG_ADDR(r4, kernstart_addr) 990549e8152SPaul Mackerras clrldi r0,r25,2 991549e8152SPaul Mackerras std r0,0(r4) 992549e8152SPaul Mackerras#endif 993549e8152SPaul Mackerras 994e31aa453SPaul Mackerras /* set up a stack pointer */ 9957ffa8b7dSMichael Ellerman LOAD_REG_ADDR(r3,init_thread_union) 996cabed148SHamish Martin LOAD_REG_IMMEDIATE(r1,THREAD_SIZE) 997cabed148SHamish Martin add r1,r3,r1 99814cf11afSPaul Mackerras li r0,0 99990f1b431SNicholas Piggin stdu r0,-STACK_FRAME_MIN_SIZE(r1) 100014cf11afSPaul Mackerras 1001376af594SMichael Ellerman /* 1002376af594SMichael Ellerman * Do very early kernel initializations, including initial hash table 1003376af594SMichael Ellerman * and SLB setup before we turn on relocation. 1004376af594SMichael Ellerman */ 100514cf11afSPaul Mackerras 1006c7b9ed7cSChristophe Leroy#ifdef CONFIG_KASAN 10074e991e3cSNicholas Piggin bl CFUNC(kasan_early_init) 1008c7b9ed7cSChristophe Leroy#endif 100914cf11afSPaul Mackerras /* Restore parameters passed from prom_init/kexec */ 101014cf11afSPaul Mackerras mr r3,r31 101156c46bbaSRussell Currey LOAD_REG_ADDR(r12, DOTSYM(early_setup)) 101256c46bbaSRussell Currey mtctr r12 101356c46bbaSRussell Currey bctrl /* also sets r13 and SPRG_PACA */ 101414cf11afSPaul Mackerras 1015ad0289e4SAnton Blanchard LOAD_REG_ADDR(r3, start_here_common) 1016e31aa453SPaul Mackerras ld r4,PACAKMSR(r13) 1017b5bbeb23SPaul Mackerras mtspr SPRN_SRR0,r3 1018b5bbeb23SPaul Mackerras mtspr SPRN_SRR1,r4 1019879add77SChristophe Leroy RFI_TO_KERNEL 102014cf11afSPaul Mackerras b . /* prevent speculative execution */ 102114cf11afSPaul Mackerras 102214cf11afSPaul Mackerras /* This is where all platforms converge execution */ 1023ad0289e4SAnton Blanchard 1024ad0289e4SAnton Blanchardstart_here_common: 102514cf11afSPaul Mackerras /* relocation is on at this point */ 102614cf11afSPaul Mackerras std r1,PACAKSAVE(r13) 102714cf11afSPaul Mackerras 1028e31aa453SPaul Mackerras /* Load the TOC (virtual address) */ 10298e93fb33SNicholas Piggin LOAD_PACA_TOC() 103014cf11afSPaul Mackerras 10317230c564SBenjamin Herrenschmidt /* Mark interrupts soft and hard disabled (they might be enabled 10327230c564SBenjamin Herrenschmidt * in the PACA when doing hotplug) 10337230c564SBenjamin Herrenschmidt */ 1034c2e480baSMadhavan Srinivasan li r0,IRQS_DISABLED 10354e26bc4aSMadhavan Srinivasan stb r0,PACAIRQSOFTMASK(r13) 10367230c564SBenjamin Herrenschmidt li r0,PACA_IRQ_HARD_DIS 10377230c564SBenjamin Herrenschmidt stb r0,PACAIRQHAPPENED(r13) 103814cf11afSPaul Mackerras 10397230c564SBenjamin Herrenschmidt /* Generic kernel entry */ 10404e991e3cSNicholas Piggin bl CFUNC(start_kernel) 104114cf11afSPaul Mackerras 1042f1870f77SAnton Blanchard /* Not reached */ 1043fe18a35eSJordan Niethe0: trap 104463ce271bSChristophe Leroy EMIT_BUG_ENTRY 0b, __FILE__, __LINE__, 0 10456eeb9b3bSMichael Ellerman .previous 1046