xref: /openbmc/linux/arch/powerpc/kernel/head_64.S (revision f87f253bac3ce4a4eb2a60a1ae604d74e65f9042)
114cf11afSPaul Mackerras/*
214cf11afSPaul Mackerras *  PowerPC version
314cf11afSPaul Mackerras *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
414cf11afSPaul Mackerras *
514cf11afSPaul Mackerras *  Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
614cf11afSPaul Mackerras *    Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
714cf11afSPaul Mackerras *  Adapted for Power Macintosh by Paul Mackerras.
814cf11afSPaul Mackerras *  Low-level exception handlers and MMU support
914cf11afSPaul Mackerras *  rewritten by Paul Mackerras.
1014cf11afSPaul Mackerras *    Copyright (C) 1996 Paul Mackerras.
1114cf11afSPaul Mackerras *
1214cf11afSPaul Mackerras *  Adapted for 64bit PowerPC by Dave Engebretsen, Peter Bergner, and
1314cf11afSPaul Mackerras *    Mike Corrigan {engebret|bergner|mikejc}@us.ibm.com
1414cf11afSPaul Mackerras *
150ebc4cdaSBenjamin Herrenschmidt *  This file contains the entry point for the 64-bit kernel along
160ebc4cdaSBenjamin Herrenschmidt *  with some early initialization code common to all 64-bit powerpc
170ebc4cdaSBenjamin Herrenschmidt *  variants.
1814cf11afSPaul Mackerras *
1914cf11afSPaul Mackerras *  This program is free software; you can redistribute it and/or
2014cf11afSPaul Mackerras *  modify it under the terms of the GNU General Public License
2114cf11afSPaul Mackerras *  as published by the Free Software Foundation; either version
2214cf11afSPaul Mackerras *  2 of the License, or (at your option) any later version.
2314cf11afSPaul Mackerras */
2414cf11afSPaul Mackerras
2514cf11afSPaul Mackerras#include <linux/threads.h>
26c141611fSPaul Gortmaker#include <linux/init.h>
27b5bbeb23SPaul Mackerras#include <asm/reg.h>
2814cf11afSPaul Mackerras#include <asm/page.h>
2914cf11afSPaul Mackerras#include <asm/mmu.h>
3014cf11afSPaul Mackerras#include <asm/ppc_asm.h>
3157f26649SNicholas Piggin#include <asm/head-64.h>
3214cf11afSPaul Mackerras#include <asm/asm-offsets.h>
3314cf11afSPaul Mackerras#include <asm/bug.h>
3414cf11afSPaul Mackerras#include <asm/cputable.h>
3514cf11afSPaul Mackerras#include <asm/setup.h>
3614cf11afSPaul Mackerras#include <asm/hvcall.h>
376cb7bfebSDavid Gibson#include <asm/thread_info.h>
383f639ee8SStephen Rothwell#include <asm/firmware.h>
3916a15a30SStephen Rothwell#include <asm/page_64.h>
40945feb17SBenjamin Herrenschmidt#include <asm/irqflags.h>
412191d657SAlexander Graf#include <asm/kvm_book3s_asm.h>
4246f52210SStephen Rothwell#include <asm/ptrace.h>
437230c564SBenjamin Herrenschmidt#include <asm/hw_irq.h>
446becef7eSchenhui zhao#include <asm/cputhreads.h>
457a25d912SScott Wood#include <asm/ppc-opcode.h>
469445aa1aSAl Viro#include <asm/export.h>
4714cf11afSPaul Mackerras
4825985edcSLucas De Marchi/* The physical memory is laid out such that the secondary processor
490ebc4cdaSBenjamin Herrenschmidt * spin code sits at 0x0000...0x00ff. On server, the vectors follow
500ebc4cdaSBenjamin Herrenschmidt * using the layout described in exceptions-64s.S
5114cf11afSPaul Mackerras */
5214cf11afSPaul Mackerras
5314cf11afSPaul Mackerras/*
5414cf11afSPaul Mackerras * Entering into this code we make the following assumptions:
550ebc4cdaSBenjamin Herrenschmidt *
560ebc4cdaSBenjamin Herrenschmidt *  For pSeries or server processors:
5714cf11afSPaul Mackerras *   1. The MMU is off & open firmware is running in real mode.
5814cf11afSPaul Mackerras *   2. The kernel is entered at __start
5927f44888SBenjamin Herrenschmidt * -or- For OPAL entry:
6027f44888SBenjamin Herrenschmidt *   1. The MMU is off, processor in HV mode, primary CPU enters at 0
61daea1175SBenjamin Herrenschmidt *      with device-tree in gpr3. We also get OPAL base in r8 and
62daea1175SBenjamin Herrenschmidt *	entry in r9 for debugging purposes
6327f44888SBenjamin Herrenschmidt *   2. Secondary processors enter at 0x60 with PIR in gpr3
6414cf11afSPaul Mackerras *
650ebc4cdaSBenjamin Herrenschmidt *  For Book3E processors:
660ebc4cdaSBenjamin Herrenschmidt *   1. The MMU is on running in AS0 in a state defined in ePAPR
670ebc4cdaSBenjamin Herrenschmidt *   2. The kernel is entered at __start
6814cf11afSPaul Mackerras */
6914cf11afSPaul Mackerras
7057f26649SNicholas PigginOPEN_FIXED_SECTION(first_256B, 0x0, 0x100)
7157f26649SNicholas PigginUSE_FIXED_SECTION(first_256B)
7257f26649SNicholas Piggin	/*
7357f26649SNicholas Piggin	 * Offsets are relative from the start of fixed section, and
7457f26649SNicholas Piggin	 * first_256B starts at 0. Offsets are a bit easier to use here
7557f26649SNicholas Piggin	 * than the fixed section entry macros.
7657f26649SNicholas Piggin	 */
7757f26649SNicholas Piggin	. = 0x0
7814cf11afSPaul Mackerras_GLOBAL(__start)
7914cf11afSPaul Mackerras	/* NOP this out unconditionally */
8014cf11afSPaul MackerrasBEGIN_FTR_SECTION
815c0484e2SBenjamin Herrenschmidt	FIXUP_ENDIAN
82b1576fecSAnton Blanchard	b	__start_initialization_multiplatform
8314cf11afSPaul MackerrasEND_FTR_SECTION(0, 1)
8414cf11afSPaul Mackerras
8514cf11afSPaul Mackerras	/* Catch branch to 0 in real mode */
8614cf11afSPaul Mackerras	trap
8714cf11afSPaul Mackerras
882751b628SAnton Blanchard	/* Secondary processors spin on this value until it becomes non-zero.
892751b628SAnton Blanchard	 * When non-zero, it contains the real address of the function the cpu
902751b628SAnton Blanchard	 * should jump to.
911f6a93e4SPaul Mackerras	 */
927d4151b5SOlof Johansson	.balign 8
9314cf11afSPaul Mackerras	.globl  __secondary_hold_spinloop
9414cf11afSPaul Mackerras__secondary_hold_spinloop:
9514cf11afSPaul Mackerras	.llong	0x0
9614cf11afSPaul Mackerras
9714cf11afSPaul Mackerras	/* Secondary processors write this value with their cpu # */
9814cf11afSPaul Mackerras	/* after they enter the spin loop immediately below.	  */
9914cf11afSPaul Mackerras	.globl	__secondary_hold_acknowledge
10014cf11afSPaul Mackerras__secondary_hold_acknowledge:
10114cf11afSPaul Mackerras	.llong	0x0
10214cf11afSPaul Mackerras
103928a3197SSonny Rao#ifdef CONFIG_RELOCATABLE
1048b8b0cc1SMilton Miller	/* This flag is set to 1 by a loader if the kernel should run
1058b8b0cc1SMilton Miller	 * at the loaded address instead of the linked address.  This
1068b8b0cc1SMilton Miller	 * is used by kexec-tools to keep the the kdump kernel in the
1078b8b0cc1SMilton Miller	 * crash_kernel region.  The loader is responsible for
1088b8b0cc1SMilton Miller	 * observing the alignment requirement.
1098b8b0cc1SMilton Miller	 */
11070839d20SNicholas Piggin
11170839d20SNicholas Piggin#ifdef CONFIG_RELOCATABLE_TEST
11270839d20SNicholas Piggin#define RUN_AT_LOAD_DEFAULT 1		/* Test relocation, do not copy to 0 */
11370839d20SNicholas Piggin#else
11470839d20SNicholas Piggin#define RUN_AT_LOAD_DEFAULT 0x72756e30  /* "run0" -- relocate to 0 by default */
11570839d20SNicholas Piggin#endif
11670839d20SNicholas Piggin
1178b8b0cc1SMilton Miller	/* Do not move this variable as kexec-tools knows about it. */
1188b8b0cc1SMilton Miller	. = 0x5c
1198b8b0cc1SMilton Miller	.globl	__run_at_load
1208b8b0cc1SMilton Miller__run_at_load:
12157f26649SNicholas PigginDEFINE_FIXED_SYMBOL(__run_at_load)
12270839d20SNicholas Piggin	.long	RUN_AT_LOAD_DEFAULT
1238b8b0cc1SMilton Miller#endif
1248b8b0cc1SMilton Miller
12514cf11afSPaul Mackerras	. = 0x60
12614cf11afSPaul Mackerras/*
12775423b7bSGeoff Levand * The following code is used to hold secondary processors
12875423b7bSGeoff Levand * in a spin loop after they have entered the kernel, but
12914cf11afSPaul Mackerras * before the bulk of the kernel has been relocated.  This code
13014cf11afSPaul Mackerras * is relocated to physical address 0x60 before prom_init is run.
13114cf11afSPaul Mackerras * All of it must fit below the first exception vector at 0x100.
1321f6a93e4SPaul Mackerras * Use .globl here not _GLOBAL because we want __secondary_hold
1331f6a93e4SPaul Mackerras * to be the actual text address, not a descriptor.
13414cf11afSPaul Mackerras */
1351f6a93e4SPaul Mackerras	.globl	__secondary_hold
1361f6a93e4SPaul Mackerras__secondary_hold:
1375c0484e2SBenjamin Herrenschmidt	FIXUP_ENDIAN
1382d27cfd3SBenjamin Herrenschmidt#ifndef CONFIG_PPC_BOOK3E
13914cf11afSPaul Mackerras	mfmsr	r24
14014cf11afSPaul Mackerras	ori	r24,r24,MSR_RI
14114cf11afSPaul Mackerras	mtmsrd	r24			/* RI on */
1422d27cfd3SBenjamin Herrenschmidt#endif
143f1870f77SAnton Blanchard	/* Grab our physical cpu number */
14414cf11afSPaul Mackerras	mr	r24,r3
14596f013feSJimi Xenidis	/* stash r4 for book3e */
14696f013feSJimi Xenidis	mr	r25,r4
14714cf11afSPaul Mackerras
14814cf11afSPaul Mackerras	/* Tell the master cpu we're here */
14914cf11afSPaul Mackerras	/* Relocation is off & we are located at an address less */
15014cf11afSPaul Mackerras	/* than 0x100, so only need to grab low order offset.    */
15157f26649SNicholas Piggin	std	r24,(ABS_ADDR(__secondary_hold_acknowledge))(0)
15214cf11afSPaul Mackerras	sync
15314cf11afSPaul Mackerras
15496f013feSJimi Xenidis	li	r26,0
15596f013feSJimi Xenidis#ifdef CONFIG_PPC_BOOK3E
15696f013feSJimi Xenidis	tovirt(r26,r26)
15796f013feSJimi Xenidis#endif
15814cf11afSPaul Mackerras	/* All secondary cpus wait here until told to start. */
15957f26649SNicholas Piggin100:	ld	r12,(ABS_ADDR(__secondary_hold_spinloop))(r26)
160cc7efbf9SAnton Blanchard	cmpdi	0,r12,0
1611f6a93e4SPaul Mackerras	beq	100b
16214cf11afSPaul Mackerras
163f1870f77SAnton Blanchard#if defined(CONFIG_SMP) || defined(CONFIG_KEXEC)
16496f013feSJimi Xenidis#ifdef CONFIG_PPC_BOOK3E
165cc7efbf9SAnton Blanchard	tovirt(r12,r12)
16696f013feSJimi Xenidis#endif
167cc7efbf9SAnton Blanchard	mtctr	r12
16814cf11afSPaul Mackerras	mr	r3,r24
16996f013feSJimi Xenidis	/*
17096f013feSJimi Xenidis	 * it may be the case that other platforms have r4 right to
17196f013feSJimi Xenidis	 * begin with, this gives us some safety in case it is not
17296f013feSJimi Xenidis	 */
17396f013feSJimi Xenidis#ifdef CONFIG_PPC_BOOK3E
17496f013feSJimi Xenidis	mr	r4,r25
17596f013feSJimi Xenidis#else
1762d27cfd3SBenjamin Herrenschmidt	li	r4,0
17796f013feSJimi Xenidis#endif
178dd797738SBenjamin Herrenschmidt	/* Make sure that patched code is visible */
179dd797738SBenjamin Herrenschmidt	isync
180758438a7SMichael Ellerman	bctr
18114cf11afSPaul Mackerras#else
18214cf11afSPaul Mackerras	BUG_OPCODE
18314cf11afSPaul Mackerras#endif
18457f26649SNicholas PigginCLOSE_FIXED_SECTION(first_256B)
18514cf11afSPaul Mackerras
18614cf11afSPaul Mackerras/* This value is used to mark exception frames on the stack. */
18714cf11afSPaul Mackerras	.section ".toc","aw"
18814cf11afSPaul Mackerrasexception_marker:
18914cf11afSPaul Mackerras	.tc	ID_72656773_68657265[TC],0x7265677368657265
19057f26649SNicholas Piggin	.previous
19114cf11afSPaul Mackerras
19214cf11afSPaul Mackerras/*
1930ebc4cdaSBenjamin Herrenschmidt * On server, we include the exception vectors code here as it
1940ebc4cdaSBenjamin Herrenschmidt * relies on absolute addressing which is only possible within
1950ebc4cdaSBenjamin Herrenschmidt * this compilation unit
19614cf11afSPaul Mackerras */
1970ebc4cdaSBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3S
1980ebc4cdaSBenjamin Herrenschmidt#include "exceptions-64s.S"
19957f26649SNicholas Piggin#else
20057f26649SNicholas PigginOPEN_TEXT_SECTION(0x100)
2011f6a93e4SPaul Mackerras#endif
20214cf11afSPaul Mackerras
20357f26649SNicholas PigginUSE_TEXT_SECTION()
20457f26649SNicholas Piggin
205e16c8765SAndy Fleming#ifdef CONFIG_PPC_BOOK3E
206d17799f9Schenhui zhao/*
2076becef7eSchenhui zhao * The booting_thread_hwid holds the thread id we want to boot in cpu
2086becef7eSchenhui zhao * hotplug case. It is set by cpu hotplug code, and is invalid by default.
2096becef7eSchenhui zhao * The thread id is the same as the initial value of SPRN_PIR[THREAD_ID]
2106becef7eSchenhui zhao * bit field.
2116becef7eSchenhui zhao */
2126becef7eSchenhui zhao	.globl	booting_thread_hwid
2136becef7eSchenhui zhaobooting_thread_hwid:
2146becef7eSchenhui zhao	.long  INVALID_THREAD_HWID
2156becef7eSchenhui zhao	.align 3
2166becef7eSchenhui zhao/*
2176becef7eSchenhui zhao * start a thread in the same core
2186becef7eSchenhui zhao * input parameters:
2196becef7eSchenhui zhao * r3 = the thread physical id
2206becef7eSchenhui zhao * r4 = the entry point where thread starts
2216becef7eSchenhui zhao */
2226becef7eSchenhui zhao_GLOBAL(book3e_start_thread)
2236becef7eSchenhui zhao	LOAD_REG_IMMEDIATE(r5, MSR_KERNEL)
224*f87f253bSNicholas Piggin	cmpwi	r3, 0
2256becef7eSchenhui zhao	beq	10f
226*f87f253bSNicholas Piggin	cmpwi	r3, 1
2276becef7eSchenhui zhao	beq	11f
2286becef7eSchenhui zhao	/* If the thread id is invalid, just exit. */
2296becef7eSchenhui zhao	b	13f
2306becef7eSchenhui zhao10:
2317a25d912SScott Wood	MTTMR(TMRN_IMSR0, 5)
2327a25d912SScott Wood	MTTMR(TMRN_INIA0, 4)
2336becef7eSchenhui zhao	b	12f
2346becef7eSchenhui zhao11:
2357a25d912SScott Wood	MTTMR(TMRN_IMSR1, 5)
2367a25d912SScott Wood	MTTMR(TMRN_INIA1, 4)
2376becef7eSchenhui zhao12:
2386becef7eSchenhui zhao	isync
2396becef7eSchenhui zhao	li	r6, 1
2406becef7eSchenhui zhao	sld	r6, r6, r3
2416becef7eSchenhui zhao	mtspr	SPRN_TENS, r6
2426becef7eSchenhui zhao13:
2436becef7eSchenhui zhao	blr
2446becef7eSchenhui zhao
2456becef7eSchenhui zhao/*
246d17799f9Schenhui zhao * stop a thread in the same core
247d17799f9Schenhui zhao * input parameter:
248d17799f9Schenhui zhao * r3 = the thread physical id
249d17799f9Schenhui zhao */
250d17799f9Schenhui zhao_GLOBAL(book3e_stop_thread)
251*f87f253bSNicholas Piggin	cmpwi	r3, 0
252d17799f9Schenhui zhao	beq	10f
253*f87f253bSNicholas Piggin	cmpwi	r3, 1
254d17799f9Schenhui zhao	beq	10f
255d17799f9Schenhui zhao	/* If the thread id is invalid, just exit. */
256d17799f9Schenhui zhao	b	13f
257d17799f9Schenhui zhao10:
258d17799f9Schenhui zhao	li	r4, 1
259d17799f9Schenhui zhao	sld	r4, r4, r3
260d17799f9Schenhui zhao	mtspr	SPRN_TENC, r4
261d17799f9Schenhui zhao13:
262d17799f9Schenhui zhao	blr
263d17799f9Schenhui zhao
264e16c8765SAndy Fleming_GLOBAL(fsl_secondary_thread_init)
265f34b3e19SScott Wood	mfspr	r4,SPRN_BUCSR
266f34b3e19SScott Wood
267e16c8765SAndy Fleming	/* Enable branch prediction */
268e16c8765SAndy Fleming	lis     r3,BUCSR_INIT@h
269e16c8765SAndy Fleming	ori     r3,r3,BUCSR_INIT@l
270e16c8765SAndy Fleming	mtspr   SPRN_BUCSR,r3
271e16c8765SAndy Fleming	isync
272e16c8765SAndy Fleming
273e16c8765SAndy Fleming	/*
274e16c8765SAndy Fleming	 * Fix PIR to match the linear numbering in the device tree.
275e16c8765SAndy Fleming	 *
276e16c8765SAndy Fleming	 * On e6500, the reset value of PIR uses the low three bits for
277e16c8765SAndy Fleming	 * the thread within a core, and the upper bits for the core
278e16c8765SAndy Fleming	 * number.  There are two threads per core, so shift everything
279e16c8765SAndy Fleming	 * but the low bit right by two bits so that the cpu numbering is
280e16c8765SAndy Fleming	 * continuous.
281f34b3e19SScott Wood	 *
282f34b3e19SScott Wood	 * If the old value of BUCSR is non-zero, this thread has run
283f34b3e19SScott Wood	 * before.  Thus, we assume we are coming from kexec or a similar
284f34b3e19SScott Wood	 * scenario, and PIR is already set to the correct value.  This
285f34b3e19SScott Wood	 * is a bit of a hack, but there are limited opportunities for
286f34b3e19SScott Wood	 * getting information into the thread and the alternatives
287f34b3e19SScott Wood	 * seemed like they'd be overkill.  We can't tell just by looking
288f34b3e19SScott Wood	 * at the old PIR value which state it's in, since the same value
289f34b3e19SScott Wood	 * could be valid for one thread out of reset and for a different
290f34b3e19SScott Wood	 * thread in Linux.
291e16c8765SAndy Fleming	 */
292f34b3e19SScott Wood
293e16c8765SAndy Fleming	mfspr	r3, SPRN_PIR
294f34b3e19SScott Wood	cmpwi	r4,0
295f34b3e19SScott Wood	bne	1f
296e16c8765SAndy Fleming	rlwimi	r3, r3, 30, 2, 30
297e16c8765SAndy Fleming	mtspr	SPRN_PIR, r3
298f34b3e19SScott Wood1:
299e16c8765SAndy Fleming#endif
300e16c8765SAndy Fleming
3012d27cfd3SBenjamin Herrenschmidt_GLOBAL(generic_secondary_thread_init)
30214cf11afSPaul Mackerras	mr	r24,r3
30314cf11afSPaul Mackerras
30414cf11afSPaul Mackerras	/* turn on 64-bit mode */
305b1576fecSAnton Blanchard	bl	enable_64b_mode
30614cf11afSPaul Mackerras
3072d27cfd3SBenjamin Herrenschmidt	/* get a valid TOC pointer, wherever we're mapped at */
308b1576fecSAnton Blanchard	bl	relative_toc
3091fbe9cf2SAnton Blanchard	tovirt(r2,r2)
310e31aa453SPaul Mackerras
3112d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3E
3122d27cfd3SBenjamin Herrenschmidt	/* Book3E initialization */
3132d27cfd3SBenjamin Herrenschmidt	mr	r3,r24
314b1576fecSAnton Blanchard	bl	book3e_secondary_thread_init
3152d27cfd3SBenjamin Herrenschmidt#endif
3162d27cfd3SBenjamin Herrenschmidt	b	generic_secondary_common_init
3172d27cfd3SBenjamin Herrenschmidt
3182d27cfd3SBenjamin Herrenschmidt/*
3192d27cfd3SBenjamin Herrenschmidt * On pSeries and most other platforms, secondary processors spin
3202d27cfd3SBenjamin Herrenschmidt * in the following code.
3212d27cfd3SBenjamin Herrenschmidt * At entry, r3 = this processor's number (physical cpu id)
3222d27cfd3SBenjamin Herrenschmidt *
3232d27cfd3SBenjamin Herrenschmidt * On Book3E, r4 = 1 to indicate that the initial TLB entry for
3242d27cfd3SBenjamin Herrenschmidt * this core already exists (setup via some other mechanism such
3252d27cfd3SBenjamin Herrenschmidt * as SCOM before entry).
3262d27cfd3SBenjamin Herrenschmidt */
3272d27cfd3SBenjamin Herrenschmidt_GLOBAL(generic_secondary_smp_init)
3285c0484e2SBenjamin Herrenschmidt	FIXUP_ENDIAN
3292d27cfd3SBenjamin Herrenschmidt	mr	r24,r3
3302d27cfd3SBenjamin Herrenschmidt	mr	r25,r4
3312d27cfd3SBenjamin Herrenschmidt
3322d27cfd3SBenjamin Herrenschmidt	/* turn on 64-bit mode */
333b1576fecSAnton Blanchard	bl	enable_64b_mode
3342d27cfd3SBenjamin Herrenschmidt
3352d27cfd3SBenjamin Herrenschmidt	/* get a valid TOC pointer, wherever we're mapped at */
336b1576fecSAnton Blanchard	bl	relative_toc
3371fbe9cf2SAnton Blanchard	tovirt(r2,r2)
3382d27cfd3SBenjamin Herrenschmidt
3392d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3E
3402d27cfd3SBenjamin Herrenschmidt	/* Book3E initialization */
3412d27cfd3SBenjamin Herrenschmidt	mr	r3,r24
3422d27cfd3SBenjamin Herrenschmidt	mr	r4,r25
343b1576fecSAnton Blanchard	bl	book3e_secondary_core_init
3446becef7eSchenhui zhao
3456becef7eSchenhui zhao/*
3466becef7eSchenhui zhao * After common core init has finished, check if the current thread is the
3476becef7eSchenhui zhao * one we wanted to boot. If not, start the specified thread and stop the
3486becef7eSchenhui zhao * current thread.
3496becef7eSchenhui zhao */
3506becef7eSchenhui zhao	LOAD_REG_ADDR(r4, booting_thread_hwid)
3516becef7eSchenhui zhao	lwz     r3, 0(r4)
3526becef7eSchenhui zhao	li	r5, INVALID_THREAD_HWID
3536becef7eSchenhui zhao	cmpw	r3, r5
3546becef7eSchenhui zhao	beq	20f
3556becef7eSchenhui zhao
3566becef7eSchenhui zhao	/*
3576becef7eSchenhui zhao	 * The value of booting_thread_hwid has been stored in r3,
3586becef7eSchenhui zhao	 * so make it invalid.
3596becef7eSchenhui zhao	 */
3606becef7eSchenhui zhao	stw	r5, 0(r4)
3616becef7eSchenhui zhao
3626becef7eSchenhui zhao	/*
3636becef7eSchenhui zhao	 * Get the current thread id and check if it is the one we wanted.
3646becef7eSchenhui zhao	 * If not, start the one specified in booting_thread_hwid and stop
3656becef7eSchenhui zhao	 * the current thread.
3666becef7eSchenhui zhao	 */
3676becef7eSchenhui zhao	mfspr	r8, SPRN_TIR
3686becef7eSchenhui zhao	cmpw	r3, r8
3696becef7eSchenhui zhao	beq	20f
3706becef7eSchenhui zhao
3716becef7eSchenhui zhao	/* start the specified thread */
3726becef7eSchenhui zhao	LOAD_REG_ADDR(r5, fsl_secondary_thread_init)
3736becef7eSchenhui zhao	ld	r4, 0(r5)
3746becef7eSchenhui zhao	bl	book3e_start_thread
3756becef7eSchenhui zhao
3766becef7eSchenhui zhao	/* stop the current thread */
3776becef7eSchenhui zhao	mr	r3, r8
3786becef7eSchenhui zhao	bl	book3e_stop_thread
3796becef7eSchenhui zhao10:
3806becef7eSchenhui zhao	b	10b
3816becef7eSchenhui zhao20:
3822d27cfd3SBenjamin Herrenschmidt#endif
3832d27cfd3SBenjamin Herrenschmidt
3842d27cfd3SBenjamin Herrenschmidtgeneric_secondary_common_init:
38514cf11afSPaul Mackerras	/* Set up a paca value for this processor. Since we have the
38614cf11afSPaul Mackerras	 * physical cpu id in r24, we need to search the pacas to find
38714cf11afSPaul Mackerras	 * which logical id maps to our physical one.
38814cf11afSPaul Mackerras	 */
3891426d5a3SMichael Ellerman	LOAD_REG_ADDR(r13, paca)	/* Load paca pointer		 */
3901426d5a3SMichael Ellerman	ld	r13,0(r13)		/* Get base vaddr of paca array	 */
391768d18adSMilton Miller#ifndef CONFIG_SMP
392768d18adSMilton Miller	addi	r13,r13,PACA_SIZE	/* know r13 if used accidentally */
393b1576fecSAnton Blanchard	b	kexec_wait		/* wait for next kernel if !SMP	 */
394768d18adSMilton Miller#else
395768d18adSMilton Miller	LOAD_REG_ADDR(r7, nr_cpu_ids)	/* Load nr_cpu_ids address       */
396768d18adSMilton Miller	lwz	r7,0(r7)		/* also the max paca allocated 	 */
39714cf11afSPaul Mackerras	li	r5,0			/* logical cpu id                */
39814cf11afSPaul Mackerras1:	lhz	r6,PACAHWCPUID(r13)	/* Load HW procid from paca      */
39914cf11afSPaul Mackerras	cmpw	r6,r24			/* Compare to our id             */
40014cf11afSPaul Mackerras	beq	2f
40114cf11afSPaul Mackerras	addi	r13,r13,PACA_SIZE	/* Loop to next PACA on miss     */
40214cf11afSPaul Mackerras	addi	r5,r5,1
403768d18adSMilton Miller	cmpw	r5,r7			/* Check if more pacas exist     */
40414cf11afSPaul Mackerras	blt	1b
40514cf11afSPaul Mackerras
40614cf11afSPaul Mackerras	mr	r3,r24			/* not found, copy phys to r3	 */
407b1576fecSAnton Blanchard	b	kexec_wait		/* next kernel might do better	 */
40814cf11afSPaul Mackerras
4092dd60d79SBenjamin Herrenschmidt2:	SET_PACA(r13)
4102d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3E
4112d27cfd3SBenjamin Herrenschmidt	addi	r12,r13,PACA_EXTLB	/* and TLB exc frame in another  */
4122d27cfd3SBenjamin Herrenschmidt	mtspr	SPRN_SPRG_TLB_EXFRAME,r12
4132d27cfd3SBenjamin Herrenschmidt#endif
4142d27cfd3SBenjamin Herrenschmidt
41514cf11afSPaul Mackerras	/* From now on, r24 is expected to be logical cpuid */
41614cf11afSPaul Mackerras	mr	r24,r5
417b6f6b98aSSonny Rao
418f39b7a55SOlof Johansson	/* See if we need to call a cpu state restore handler */
419e31aa453SPaul Mackerras	LOAD_REG_ADDR(r23, cur_cpu_spec)
420f39b7a55SOlof Johansson	ld	r23,0(r23)
4212751b628SAnton Blanchard	ld	r12,CPU_SPEC_RESTORE(r23)
4222751b628SAnton Blanchard	cmpdi	0,r12,0
4239d07bc84SBenjamin Herrenschmidt	beq	3f
424f55d9665SMichael Ellerman#ifdef PPC64_ELF_ABI_v1
4252751b628SAnton Blanchard	ld	r12,0(r12)
4262751b628SAnton Blanchard#endif
427cc7efbf9SAnton Blanchard	mtctr	r12
428f39b7a55SOlof Johansson	bctrl
429f39b7a55SOlof Johansson
4307ac87abbSMatt Evans3:	LOAD_REG_ADDR(r3, spinning_secondaries) /* Decrement spinning_secondaries */
4319d07bc84SBenjamin Herrenschmidt	lwarx	r4,0,r3
4329d07bc84SBenjamin Herrenschmidt	subi	r4,r4,1
4339d07bc84SBenjamin Herrenschmidt	stwcx.	r4,0,r3
4349d07bc84SBenjamin Herrenschmidt	bne	3b
4359d07bc84SBenjamin Herrenschmidt	isync
4369d07bc84SBenjamin Herrenschmidt
4379d07bc84SBenjamin Herrenschmidt4:	HMT_LOW
438ad0693eeSBenjamin Herrenschmidt	lbz	r23,PACAPROCSTART(r13)	/* Test if this processor should */
439ad0693eeSBenjamin Herrenschmidt					/* start.			 */
440ad0693eeSBenjamin Herrenschmidt	cmpwi	0,r23,0
4419d07bc84SBenjamin Herrenschmidt	beq	4b			/* Loop until told to go	 */
442ad0693eeSBenjamin Herrenschmidt
443ad0693eeSBenjamin Herrenschmidt	sync				/* order paca.run and cur_cpu_spec */
4449d07bc84SBenjamin Herrenschmidt	isync				/* In case code patching happened */
445ad0693eeSBenjamin Herrenschmidt
4469d07bc84SBenjamin Herrenschmidt	/* Create a temp kernel stack for use before relocation is on.	*/
44714cf11afSPaul Mackerras	ld	r1,PACAEMERGSP(r13)
44814cf11afSPaul Mackerras	subi	r1,r1,STACK_FRAME_OVERHEAD
44914cf11afSPaul Mackerras
450c705677eSStephen Rothwell	b	__secondary_start
451768d18adSMilton Miller#endif /* SMP */
45214cf11afSPaul Mackerras
453e31aa453SPaul Mackerras/*
454e31aa453SPaul Mackerras * Turn the MMU off.
455e31aa453SPaul Mackerras * Assumes we're mapped EA == RA if the MMU is on.
456e31aa453SPaul Mackerras */
4572d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3S
4586a3bab90SAnton Blanchard__mmu_off:
45914cf11afSPaul Mackerras	mfmsr	r3
46014cf11afSPaul Mackerras	andi.	r0,r3,MSR_IR|MSR_DR
46114cf11afSPaul Mackerras	beqlr
462e31aa453SPaul Mackerras	mflr	r4
46314cf11afSPaul Mackerras	andc	r3,r3,r0
46414cf11afSPaul Mackerras	mtspr	SPRN_SRR0,r4
46514cf11afSPaul Mackerras	mtspr	SPRN_SRR1,r3
46614cf11afSPaul Mackerras	sync
46714cf11afSPaul Mackerras	rfid
46814cf11afSPaul Mackerras	b	.	/* prevent speculative execution */
4692d27cfd3SBenjamin Herrenschmidt#endif
47014cf11afSPaul Mackerras
47114cf11afSPaul Mackerras
47214cf11afSPaul Mackerras/*
47314cf11afSPaul Mackerras * Here is our main kernel entry point. We support currently 2 kind of entries
47414cf11afSPaul Mackerras * depending on the value of r5.
47514cf11afSPaul Mackerras *
47614cf11afSPaul Mackerras *   r5 != NULL -> OF entry, we go to prom_init, "legacy" parameter content
47714cf11afSPaul Mackerras *                 in r3...r7
47814cf11afSPaul Mackerras *
47914cf11afSPaul Mackerras *   r5 == NULL -> kexec style entry. r3 is a physical pointer to the
48014cf11afSPaul Mackerras *                 DT block, r4 is a physical pointer to the kernel itself
48114cf11afSPaul Mackerras *
48214cf11afSPaul Mackerras */
4836a3bab90SAnton Blanchard__start_initialization_multiplatform:
484e31aa453SPaul Mackerras	/* Make sure we are running in 64 bits mode */
485b1576fecSAnton Blanchard	bl	enable_64b_mode
486e31aa453SPaul Mackerras
487e31aa453SPaul Mackerras	/* Get TOC pointer (current runtime address) */
488b1576fecSAnton Blanchard	bl	relative_toc
489e31aa453SPaul Mackerras
490e31aa453SPaul Mackerras	/* find out where we are now */
491e31aa453SPaul Mackerras	bcl	20,31,$+4
492e31aa453SPaul Mackerras0:	mflr	r26			/* r26 = runtime addr here */
493e31aa453SPaul Mackerras	addis	r26,r26,(_stext - 0b)@ha
494e31aa453SPaul Mackerras	addi	r26,r26,(_stext - 0b)@l	/* current runtime base addr */
495e31aa453SPaul Mackerras
49614cf11afSPaul Mackerras	/*
49714cf11afSPaul Mackerras	 * Are we booted from a PROM Of-type client-interface ?
49814cf11afSPaul Mackerras	 */
49914cf11afSPaul Mackerras	cmpldi	cr0,r5,0
500939e60f6SStephen Rothwell	beq	1f
501b1576fecSAnton Blanchard	b	__boot_from_prom		/* yes -> prom */
502939e60f6SStephen Rothwell1:
50314cf11afSPaul Mackerras	/* Save parameters */
50414cf11afSPaul Mackerras	mr	r31,r3
50514cf11afSPaul Mackerras	mr	r30,r4
506daea1175SBenjamin Herrenschmidt#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
507daea1175SBenjamin Herrenschmidt	/* Save OPAL entry */
508daea1175SBenjamin Herrenschmidt	mr	r28,r8
509daea1175SBenjamin Herrenschmidt	mr	r29,r9
510daea1175SBenjamin Herrenschmidt#endif
51114cf11afSPaul Mackerras
5122d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3E
513b1576fecSAnton Blanchard	bl	start_initialization_book3e
514b1576fecSAnton Blanchard	b	__after_prom_start
5152d27cfd3SBenjamin Herrenschmidt#else
51614cf11afSPaul Mackerras	/* Setup some critical 970 SPRs before switching MMU off */
517f39b7a55SOlof Johansson	mfspr	r0,SPRN_PVR
518f39b7a55SOlof Johansson	srwi	r0,r0,16
519f39b7a55SOlof Johansson	cmpwi	r0,0x39		/* 970 */
520f39b7a55SOlof Johansson	beq	1f
521f39b7a55SOlof Johansson	cmpwi	r0,0x3c		/* 970FX */
522f39b7a55SOlof Johansson	beq	1f
523f39b7a55SOlof Johansson	cmpwi	r0,0x44		/* 970MP */
524190a24f5SOlof Johansson	beq	1f
525190a24f5SOlof Johansson	cmpwi	r0,0x45		/* 970GX */
526f39b7a55SOlof Johansson	bne	2f
527b1576fecSAnton Blanchard1:	bl	__cpu_preinit_ppc970
528f39b7a55SOlof Johansson2:
52914cf11afSPaul Mackerras
530e31aa453SPaul Mackerras	/* Switch off MMU if not already off */
531b1576fecSAnton Blanchard	bl	__mmu_off
532b1576fecSAnton Blanchard	b	__after_prom_start
5332d27cfd3SBenjamin Herrenschmidt#endif /* CONFIG_PPC_BOOK3E */
53414cf11afSPaul Mackerras
5356a3bab90SAnton Blanchard__boot_from_prom:
53628794d34SBenjamin Herrenschmidt#ifdef CONFIG_PPC_OF_BOOT_TRAMPOLINE
53714cf11afSPaul Mackerras	/* Save parameters */
53814cf11afSPaul Mackerras	mr	r31,r3
53914cf11afSPaul Mackerras	mr	r30,r4
54014cf11afSPaul Mackerras	mr	r29,r5
54114cf11afSPaul Mackerras	mr	r28,r6
54214cf11afSPaul Mackerras	mr	r27,r7
54314cf11afSPaul Mackerras
5446088857bSOlaf Hering	/*
5456088857bSOlaf Hering	 * Align the stack to 16-byte boundary
5466088857bSOlaf Hering	 * Depending on the size and layout of the ELF sections in the initial
547e31aa453SPaul Mackerras	 * boot binary, the stack pointer may be unaligned on PowerMac
5486088857bSOlaf Hering	 */
549c05b4770SLinus Torvalds	rldicr	r1,r1,0,59
550c05b4770SLinus Torvalds
551549e8152SPaul Mackerras#ifdef CONFIG_RELOCATABLE
552549e8152SPaul Mackerras	/* Relocate code for where we are now */
553549e8152SPaul Mackerras	mr	r3,r26
554b1576fecSAnton Blanchard	bl	relocate
555549e8152SPaul Mackerras#endif
556549e8152SPaul Mackerras
55714cf11afSPaul Mackerras	/* Restore parameters */
55814cf11afSPaul Mackerras	mr	r3,r31
55914cf11afSPaul Mackerras	mr	r4,r30
56014cf11afSPaul Mackerras	mr	r5,r29
56114cf11afSPaul Mackerras	mr	r6,r28
56214cf11afSPaul Mackerras	mr	r7,r27
56314cf11afSPaul Mackerras
56414cf11afSPaul Mackerras	/* Do all of the interaction with OF client interface */
565549e8152SPaul Mackerras	mr	r8,r26
566b1576fecSAnton Blanchard	bl	prom_init
56728794d34SBenjamin Herrenschmidt#endif /* #CONFIG_PPC_OF_BOOT_TRAMPOLINE */
56828794d34SBenjamin Herrenschmidt
56928794d34SBenjamin Herrenschmidt	/* We never return. We also hit that trap if trying to boot
57028794d34SBenjamin Herrenschmidt	 * from OF while CONFIG_PPC_OF_BOOT_TRAMPOLINE isn't selected */
57114cf11afSPaul Mackerras	trap
57214cf11afSPaul Mackerras
5736a3bab90SAnton Blanchard__after_prom_start:
574549e8152SPaul Mackerras#ifdef CONFIG_RELOCATABLE
575549e8152SPaul Mackerras	/* process relocations for the final address of the kernel */
576549e8152SPaul Mackerras	lis	r25,PAGE_OFFSET@highest	/* compute virtual base of kernel */
577549e8152SPaul Mackerras	sldi	r25,r25,32
5781cb6e064STiejun Chen#if defined(CONFIG_PPC_BOOK3E)
5791cb6e064STiejun Chen	tovirt(r26,r26)		/* on booke, we already run at PAGE_OFFSET */
5801cb6e064STiejun Chen#endif
58157f26649SNicholas Piggin	lwz	r7,(FIXED_SYMBOL_ABS_ADDR(__run_at_load))(r26)
5821cb6e064STiejun Chen#if defined(CONFIG_PPC_BOOK3E)
5831cb6e064STiejun Chen	tophys(r26,r26)
5841cb6e064STiejun Chen#endif
585928a3197SSonny Rao	cmplwi	cr0,r7,1	/* flagged to stay where we are ? */
58654622f10SMohan Kumar M	bne	1f
58754622f10SMohan Kumar M	add	r25,r25,r26
58854622f10SMohan Kumar M1:	mr	r3,r25
589b1576fecSAnton Blanchard	bl	relocate
5901cb6e064STiejun Chen#if defined(CONFIG_PPC_BOOK3E)
5911cb6e064STiejun Chen	/* IVPR needs to be set after relocation. */
5921cb6e064STiejun Chen	bl	init_core_book3e
5931cb6e064STiejun Chen#endif
594549e8152SPaul Mackerras#endif
59514cf11afSPaul Mackerras
59614cf11afSPaul Mackerras/*
597e31aa453SPaul Mackerras * We need to run with _stext at physical address PHYSICAL_START.
59814cf11afSPaul Mackerras * This will leave some code in the first 256B of
59914cf11afSPaul Mackerras * real memory, which are reserved for software use.
60014cf11afSPaul Mackerras *
60114cf11afSPaul Mackerras * Note: This process overwrites the OF exception vectors.
60214cf11afSPaul Mackerras */
603549e8152SPaul Mackerras	li	r3,0			/* target addr */
6042d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3E
6052d27cfd3SBenjamin Herrenschmidt	tovirt(r3,r3)		/* on booke, we already run at PAGE_OFFSET */
6062d27cfd3SBenjamin Herrenschmidt#endif
607549e8152SPaul Mackerras	mr.	r4,r26			/* In some cases the loader may  */
608835c031cSTiejun Chen#if defined(CONFIG_PPC_BOOK3E)
609835c031cSTiejun Chen	tovirt(r4,r4)
610835c031cSTiejun Chen#endif
611e31aa453SPaul Mackerras	beq	9f			/* have already put us at zero */
61214cf11afSPaul Mackerras	li	r6,0x100		/* Start offset, the first 0x100 */
61314cf11afSPaul Mackerras					/* bytes were copied earlier.	 */
61414cf11afSPaul Mackerras
61511ee7e99SAnton Blanchard#ifdef CONFIG_RELOCATABLE
61654622f10SMohan Kumar M/*
61754622f10SMohan Kumar M * Check if the kernel has to be running as relocatable kernel based on the
6188b8b0cc1SMilton Miller * variable __run_at_load, if it is set the kernel is treated as relocatable
61954622f10SMohan Kumar M * kernel, otherwise it will be moved to PHYSICAL_START
62054622f10SMohan Kumar M */
6211cb6e064STiejun Chen#if defined(CONFIG_PPC_BOOK3E)
6221cb6e064STiejun Chen	tovirt(r26,r26)		/* on booke, we already run at PAGE_OFFSET */
6231cb6e064STiejun Chen#endif
62457f26649SNicholas Piggin	lwz	r7,(FIXED_SYMBOL_ABS_ADDR(__run_at_load))(r26)
6258b8b0cc1SMilton Miller	cmplwi	cr0,r7,1
62654622f10SMohan Kumar M	bne	3f
62754622f10SMohan Kumar M
6281cb6e064STiejun Chen#ifdef CONFIG_PPC_BOOK3E
6291cb6e064STiejun Chen	LOAD_REG_ADDR(r5, __end_interrupts)
6301cb6e064STiejun Chen	LOAD_REG_ADDR(r11, _stext)
6311cb6e064STiejun Chen	sub	r5,r5,r11
6321cb6e064STiejun Chen#else
633c1fb6816SMichael Neuling	/* just copy interrupts */
63457f26649SNicholas Piggin	LOAD_REG_IMMEDIATE(r5, FIXED_SYMBOL_ABS_ADDR(__end_interrupts))
6351cb6e064STiejun Chen#endif
63654622f10SMohan Kumar M	b	5f
63754622f10SMohan Kumar M3:
63854622f10SMohan Kumar M#endif
63957f26649SNicholas Piggin	/* # bytes of memory to copy */
64057f26649SNicholas Piggin	lis	r5,(ABS_ADDR(copy_to_here))@ha
64157f26649SNicholas Piggin	addi	r5,r5,(ABS_ADDR(copy_to_here))@l
64254622f10SMohan Kumar M
643b1576fecSAnton Blanchard	bl	copy_and_flush		/* copy the first n bytes	 */
64414cf11afSPaul Mackerras					/* this includes the code being	 */
64514cf11afSPaul Mackerras					/* executed here.		 */
64657f26649SNicholas Piggin	/* Jump to the copy of this code that we just made */
64757f26649SNicholas Piggin	addis	r8,r3,(ABS_ADDR(4f))@ha
64857f26649SNicholas Piggin	addi	r12,r8,(ABS_ADDR(4f))@l
649cc7efbf9SAnton Blanchard	mtctr	r12
65014cf11afSPaul Mackerras	bctr
65114cf11afSPaul Mackerras
652286e4f90SAnton Blanchard.balign 8
653573819e3SNicholas Pigginp_end: .llong _end - copy_to_here
65454622f10SMohan Kumar M
655573819e3SNicholas Piggin4:
656573819e3SNicholas Piggin	/*
657573819e3SNicholas Piggin	 * Now copy the rest of the kernel up to _end, add
658573819e3SNicholas Piggin	 * _end - copy_to_here to the copy limit and run again.
659573819e3SNicholas Piggin	 */
66057f26649SNicholas Piggin	addis   r8,r26,(ABS_ADDR(p_end))@ha
66157f26649SNicholas Piggin	ld      r8,(ABS_ADDR(p_end))@l(r8)
662573819e3SNicholas Piggin	add	r5,r5,r8
663b1576fecSAnton Blanchard5:	bl	copy_and_flush		/* copy the rest */
664e31aa453SPaul Mackerras
665b1576fecSAnton Blanchard9:	b	start_here_multiplatform
666e31aa453SPaul Mackerras
66714cf11afSPaul Mackerras/*
66814cf11afSPaul Mackerras * Copy routine used to copy the kernel to start at physical address 0
66914cf11afSPaul Mackerras * and flush and invalidate the caches as needed.
67014cf11afSPaul Mackerras * r3 = dest addr, r4 = source addr, r5 = copy limit, r6 = start offset
67114cf11afSPaul Mackerras * on exit, r3, r4, r5 are unchanged, r6 is updated to be >= r5.
67214cf11afSPaul Mackerras *
67314cf11afSPaul Mackerras * Note: this routine *only* clobbers r0, r6 and lr
67414cf11afSPaul Mackerras */
67514cf11afSPaul Mackerras_GLOBAL(copy_and_flush)
67614cf11afSPaul Mackerras	addi	r5,r5,-8
67714cf11afSPaul Mackerras	addi	r6,r6,-8
6785a2fe38dSOlof Johansson4:	li	r0,8			/* Use the smallest common	*/
67914cf11afSPaul Mackerras					/* denominator cache line	*/
68014cf11afSPaul Mackerras					/* size.  This results in	*/
68114cf11afSPaul Mackerras					/* extra cache line flushes	*/
68214cf11afSPaul Mackerras					/* but operation is correct.	*/
68314cf11afSPaul Mackerras					/* Can't get cache line size	*/
68414cf11afSPaul Mackerras					/* from NACA as it is being	*/
68514cf11afSPaul Mackerras					/* moved too.			*/
68614cf11afSPaul Mackerras
68714cf11afSPaul Mackerras	mtctr	r0			/* put # words/line in ctr	*/
68814cf11afSPaul Mackerras3:	addi	r6,r6,8			/* copy a cache line		*/
68914cf11afSPaul Mackerras	ldx	r0,r6,r4
69014cf11afSPaul Mackerras	stdx	r0,r6,r3
69114cf11afSPaul Mackerras	bdnz	3b
69214cf11afSPaul Mackerras	dcbst	r6,r3			/* write it to memory		*/
69314cf11afSPaul Mackerras	sync
69414cf11afSPaul Mackerras	icbi	r6,r3			/* flush the icache line	*/
69514cf11afSPaul Mackerras	cmpld	0,r6,r5
69614cf11afSPaul Mackerras	blt	4b
69714cf11afSPaul Mackerras	sync
69814cf11afSPaul Mackerras	addi	r5,r5,8
69914cf11afSPaul Mackerras	addi	r6,r6,8
70029ce3c50SMichael Neuling	isync
70114cf11afSPaul Mackerras	blr
70214cf11afSPaul Mackerras
70314cf11afSPaul Mackerras.align 8
70414cf11afSPaul Mackerrascopy_to_here:
70514cf11afSPaul Mackerras
70614cf11afSPaul Mackerras#ifdef CONFIG_SMP
70714cf11afSPaul Mackerras#ifdef CONFIG_PPC_PMAC
70814cf11afSPaul Mackerras/*
70914cf11afSPaul Mackerras * On PowerMac, secondary processors starts from the reset vector, which
71014cf11afSPaul Mackerras * is temporarily turned into a call to one of the functions below.
71114cf11afSPaul Mackerras */
71214cf11afSPaul Mackerras	.section ".text";
71314cf11afSPaul Mackerras	.align 2 ;
71414cf11afSPaul Mackerras
71535499c01SPaul Mackerras	.globl	__secondary_start_pmac_0
71635499c01SPaul Mackerras__secondary_start_pmac_0:
71735499c01SPaul Mackerras	/* NB the entries for cpus 0, 1, 2 must each occupy 8 bytes. */
71835499c01SPaul Mackerras	li	r24,0
71935499c01SPaul Mackerras	b	1f
72014cf11afSPaul Mackerras	li	r24,1
72135499c01SPaul Mackerras	b	1f
72214cf11afSPaul Mackerras	li	r24,2
72335499c01SPaul Mackerras	b	1f
72414cf11afSPaul Mackerras	li	r24,3
72535499c01SPaul Mackerras1:
72614cf11afSPaul Mackerras
72714cf11afSPaul Mackerras_GLOBAL(pmac_secondary_start)
72814cf11afSPaul Mackerras	/* turn on 64-bit mode */
729b1576fecSAnton Blanchard	bl	enable_64b_mode
73014cf11afSPaul Mackerras
731c478b581SBenjamin Herrenschmidt	li	r0,0
732c478b581SBenjamin Herrenschmidt	mfspr	r3,SPRN_HID4
733c478b581SBenjamin Herrenschmidt	rldimi	r3,r0,40,23	/* clear bit 23 (rm_ci) */
734c478b581SBenjamin Herrenschmidt	sync
735c478b581SBenjamin Herrenschmidt	mtspr	SPRN_HID4,r3
736c478b581SBenjamin Herrenschmidt	isync
737c478b581SBenjamin Herrenschmidt	sync
738c478b581SBenjamin Herrenschmidt	slbia
739c478b581SBenjamin Herrenschmidt
740e31aa453SPaul Mackerras	/* get TOC pointer (real address) */
741b1576fecSAnton Blanchard	bl	relative_toc
7421fbe9cf2SAnton Blanchard	tovirt(r2,r2)
743e31aa453SPaul Mackerras
74414cf11afSPaul Mackerras	/* Copy some CPU settings from CPU 0 */
745b1576fecSAnton Blanchard	bl	__restore_cpu_ppc970
74614cf11afSPaul Mackerras
74714cf11afSPaul Mackerras	/* pSeries do that early though I don't think we really need it */
74814cf11afSPaul Mackerras	mfmsr	r3
74914cf11afSPaul Mackerras	ori	r3,r3,MSR_RI
75014cf11afSPaul Mackerras	mtmsrd	r3			/* RI on */
75114cf11afSPaul Mackerras
75214cf11afSPaul Mackerras	/* Set up a paca value for this processor. */
7531426d5a3SMichael Ellerman	LOAD_REG_ADDR(r4,paca)		/* Load paca pointer		*/
7541426d5a3SMichael Ellerman	ld	r4,0(r4)		/* Get base vaddr of paca array	*/
75514cf11afSPaul Mackerras	mulli	r13,r24,PACA_SIZE	/* Calculate vaddr of right paca */
75614cf11afSPaul Mackerras	add	r13,r13,r4		/* for this processor.		*/
7572dd60d79SBenjamin Herrenschmidt	SET_PACA(r13)			/* Save vaddr of paca in an SPRG*/
75814cf11afSPaul Mackerras
75962cc67b9SBenjamin Herrenschmidt	/* Mark interrupts soft and hard disabled (they might be enabled
76062cc67b9SBenjamin Herrenschmidt	 * in the PACA when doing hotplug)
76162cc67b9SBenjamin Herrenschmidt	 */
76262cc67b9SBenjamin Herrenschmidt	li	r0,0
76362cc67b9SBenjamin Herrenschmidt	stb	r0,PACASOFTIRQEN(r13)
7647230c564SBenjamin Herrenschmidt	li	r0,PACA_IRQ_HARD_DIS
7657230c564SBenjamin Herrenschmidt	stb	r0,PACAIRQHAPPENED(r13)
76662cc67b9SBenjamin Herrenschmidt
76714cf11afSPaul Mackerras	/* Create a temp kernel stack for use before relocation is on.	*/
76814cf11afSPaul Mackerras	ld	r1,PACAEMERGSP(r13)
76914cf11afSPaul Mackerras	subi	r1,r1,STACK_FRAME_OVERHEAD
77014cf11afSPaul Mackerras
771c705677eSStephen Rothwell	b	__secondary_start
77214cf11afSPaul Mackerras
77314cf11afSPaul Mackerras#endif /* CONFIG_PPC_PMAC */
77414cf11afSPaul Mackerras
77514cf11afSPaul Mackerras/*
77614cf11afSPaul Mackerras * This function is called after the master CPU has released the
77714cf11afSPaul Mackerras * secondary processors.  The execution environment is relocation off.
77814cf11afSPaul Mackerras * The paca for this processor has the following fields initialized at
77914cf11afSPaul Mackerras * this point:
78014cf11afSPaul Mackerras *   1. Processor number
78114cf11afSPaul Mackerras *   2. Segment table pointer (virtual address)
78214cf11afSPaul Mackerras * On entry the following are set:
7834f8cf36fSBenjamin Herrenschmidt *   r1	       = stack pointer (real addr of temp stack)
78414cf11afSPaul Mackerras *   r24       = cpu# (in Linux terms)
78514cf11afSPaul Mackerras *   r13       = paca virtual address
786ee43eb78SBenjamin Herrenschmidt *   SPRG_PACA = paca virtual address
78714cf11afSPaul Mackerras */
7882d27cfd3SBenjamin Herrenschmidt	.section ".text";
7892d27cfd3SBenjamin Herrenschmidt	.align 2 ;
7902d27cfd3SBenjamin Herrenschmidt
791fc68e869SStephen Rothwell	.globl	__secondary_start
792c705677eSStephen Rothwell__secondary_start:
793799d6046SPaul Mackerras	/* Set thread priority to MEDIUM */
794799d6046SPaul Mackerras	HMT_MEDIUM
79514cf11afSPaul Mackerras
7964f8cf36fSBenjamin Herrenschmidt	/* Initialize the kernel stack */
797e58c3495SDavid Gibson	LOAD_REG_ADDR(r3, current_set)
79814cf11afSPaul Mackerras	sldi	r28,r24,3		/* get current_set[cpu#]	 */
79954a83404SMichael Neuling	ldx	r14,r3,r28
80054a83404SMichael Neuling	addi	r14,r14,THREAD_SIZE-STACK_FRAME_OVERHEAD
80154a83404SMichael Neuling	std	r14,PACAKSAVE(r13)
80214cf11afSPaul Mackerras
803376af594SMichael Ellerman	/* Do early setup for that CPU (SLB and hash table pointer) */
804b1576fecSAnton Blanchard	bl	early_setup_secondary
805f761622eSMatt Evans
80654a83404SMichael Neuling	/*
80754a83404SMichael Neuling	 * setup the new stack pointer, but *don't* use this until
80854a83404SMichael Neuling	 * translation is on.
80954a83404SMichael Neuling	 */
81054a83404SMichael Neuling	mr	r1, r14
81154a83404SMichael Neuling
812799d6046SPaul Mackerras	/* Clear backchain so we get nice backtraces */
81314cf11afSPaul Mackerras	li	r7,0
81414cf11afSPaul Mackerras	mtlr	r7
81514cf11afSPaul Mackerras
8167230c564SBenjamin Herrenschmidt	/* Mark interrupts soft and hard disabled (they might be enabled
8177230c564SBenjamin Herrenschmidt	 * in the PACA when doing hotplug)
8187230c564SBenjamin Herrenschmidt	 */
8194f8cf36fSBenjamin Herrenschmidt	stb	r7,PACASOFTIRQEN(r13)
8207230c564SBenjamin Herrenschmidt	li	r0,PACA_IRQ_HARD_DIS
8217230c564SBenjamin Herrenschmidt	stb	r0,PACAIRQHAPPENED(r13)
8224f8cf36fSBenjamin Herrenschmidt
82314cf11afSPaul Mackerras	/* enable MMU and jump to start_secondary */
824ad0289e4SAnton Blanchard	LOAD_REG_ADDR(r3, start_secondary_prolog)
825e58c3495SDavid Gibson	LOAD_REG_IMMEDIATE(r4, MSR_KERNEL)
826d04c56f7SPaul Mackerras
827b5bbeb23SPaul Mackerras	mtspr	SPRN_SRR0,r3
828b5bbeb23SPaul Mackerras	mtspr	SPRN_SRR1,r4
8292d27cfd3SBenjamin Herrenschmidt	RFI
83014cf11afSPaul Mackerras	b	.	/* prevent speculative execution */
83114cf11afSPaul Mackerras
83214cf11afSPaul Mackerras/*
83314cf11afSPaul Mackerras * Running with relocation on at this point.  All we want to do is
834e31aa453SPaul Mackerras * zero the stack back-chain pointer and get the TOC virtual address
835e31aa453SPaul Mackerras * before going into C code.
83614cf11afSPaul Mackerras */
837ad0289e4SAnton Blanchardstart_secondary_prolog:
838e31aa453SPaul Mackerras	ld	r2,PACATOC(r13)
83914cf11afSPaul Mackerras	li	r3,0
84014cf11afSPaul Mackerras	std	r3,0(r1)		/* Zero the stack frame pointer	*/
841b1576fecSAnton Blanchard	bl	start_secondary
842799d6046SPaul Mackerras	b	.
8438dbce53cSVaidyanathan Srinivasan/*
8448dbce53cSVaidyanathan Srinivasan * Reset stack pointer and call start_secondary
8458dbce53cSVaidyanathan Srinivasan * to continue with online operation when woken up
8468dbce53cSVaidyanathan Srinivasan * from cede in cpu offline.
8478dbce53cSVaidyanathan Srinivasan */
8488dbce53cSVaidyanathan Srinivasan_GLOBAL(start_secondary_resume)
8498dbce53cSVaidyanathan Srinivasan	ld	r1,PACAKSAVE(r13)	/* Reload kernel stack pointer */
8508dbce53cSVaidyanathan Srinivasan	li	r3,0
8518dbce53cSVaidyanathan Srinivasan	std	r3,0(r1)		/* Zero the stack frame pointer	*/
852b1576fecSAnton Blanchard	bl	start_secondary
8538dbce53cSVaidyanathan Srinivasan	b	.
85414cf11afSPaul Mackerras#endif
85514cf11afSPaul Mackerras
85614cf11afSPaul Mackerras/*
85714cf11afSPaul Mackerras * This subroutine clobbers r11 and r12
85814cf11afSPaul Mackerras */
8596a3bab90SAnton Blanchardenable_64b_mode:
86014cf11afSPaul Mackerras	mfmsr	r11			/* grab the current MSR */
8612d27cfd3SBenjamin Herrenschmidt#ifdef CONFIG_PPC_BOOK3E
8622d27cfd3SBenjamin Herrenschmidt	oris	r11,r11,0x8000		/* CM bit set, we'll set ICM later */
8632d27cfd3SBenjamin Herrenschmidt	mtmsr	r11
8642d27cfd3SBenjamin Herrenschmidt#else /* CONFIG_PPC_BOOK3E */
8659f0b0793SMichael Ellerman	li	r12,(MSR_64BIT | MSR_ISF)@highest
866e31aa453SPaul Mackerras	sldi	r12,r12,48
86714cf11afSPaul Mackerras	or	r11,r11,r12
86814cf11afSPaul Mackerras	mtmsrd	r11
86914cf11afSPaul Mackerras	isync
8702d27cfd3SBenjamin Herrenschmidt#endif
87114cf11afSPaul Mackerras	blr
87214cf11afSPaul Mackerras
87314cf11afSPaul Mackerras/*
874e31aa453SPaul Mackerras * This puts the TOC pointer into r2, offset by 0x8000 (as expected
875e31aa453SPaul Mackerras * by the toolchain).  It computes the correct value for wherever we
876e31aa453SPaul Mackerras * are running at the moment, using position-independent code.
8771fbe9cf2SAnton Blanchard *
8781fbe9cf2SAnton Blanchard * Note: The compiler constructs pointers using offsets from the
8791fbe9cf2SAnton Blanchard * TOC in -mcmodel=medium mode. After we relocate to 0 but before
8801fbe9cf2SAnton Blanchard * the MMU is on we need our TOC to be a virtual address otherwise
8811fbe9cf2SAnton Blanchard * these pointers will be real addresses which may get stored and
8821fbe9cf2SAnton Blanchard * accessed later with the MMU on. We use tovirt() at the call
8831fbe9cf2SAnton Blanchard * sites to handle this.
884e31aa453SPaul Mackerras */
885e31aa453SPaul Mackerras_GLOBAL(relative_toc)
886e31aa453SPaul Mackerras	mflr	r0
887e31aa453SPaul Mackerras	bcl	20,31,$+4
888e550592eSBenjamin Herrenschmidt0:	mflr	r11
889e550592eSBenjamin Herrenschmidt	ld	r2,(p_toc - 0b)(r11)
890e550592eSBenjamin Herrenschmidt	add	r2,r2,r11
891e31aa453SPaul Mackerras	mtlr	r0
892e31aa453SPaul Mackerras	blr
893e31aa453SPaul Mackerras
8945b63fee1SAnton Blanchard.balign 8
895e31aa453SPaul Mackerrasp_toc:	.llong	__toc_start + 0x8000 - 0b
896e31aa453SPaul Mackerras
897e31aa453SPaul Mackerras/*
89814cf11afSPaul Mackerras * This is where the main kernel code starts.
89914cf11afSPaul Mackerras */
9006a3bab90SAnton Blanchardstart_here_multiplatform:
9011fbe9cf2SAnton Blanchard	/* set up the TOC */
902b1576fecSAnton Blanchard	bl      relative_toc
9031fbe9cf2SAnton Blanchard	tovirt(r2,r2)
90414cf11afSPaul Mackerras
90514cf11afSPaul Mackerras	/* Clear out the BSS. It may have been done in prom_init,
90614cf11afSPaul Mackerras	 * already but that's irrelevant since prom_init will soon
90714cf11afSPaul Mackerras	 * be detached from the kernel completely. Besides, we need
90814cf11afSPaul Mackerras	 * to clear it now for kexec-style entry.
90914cf11afSPaul Mackerras	 */
910e31aa453SPaul Mackerras	LOAD_REG_ADDR(r11,__bss_stop)
911e31aa453SPaul Mackerras	LOAD_REG_ADDR(r8,__bss_start)
91214cf11afSPaul Mackerras	sub	r11,r11,r8		/* bss size			*/
91314cf11afSPaul Mackerras	addi	r11,r11,7		/* round up to an even double word */
914e31aa453SPaul Mackerras	srdi.	r11,r11,3		/* shift right by 3		*/
91514cf11afSPaul Mackerras	beq	4f
91614cf11afSPaul Mackerras	addi	r8,r8,-8
91714cf11afSPaul Mackerras	li	r0,0
91814cf11afSPaul Mackerras	mtctr	r11			/* zero this many doublewords	*/
91914cf11afSPaul Mackerras3:	stdu	r0,8(r8)
92014cf11afSPaul Mackerras	bdnz	3b
92114cf11afSPaul Mackerras4:
92214cf11afSPaul Mackerras
923daea1175SBenjamin Herrenschmidt#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
924daea1175SBenjamin Herrenschmidt	/* Setup OPAL entry */
925ab7f961aSBenjamin Herrenschmidt	LOAD_REG_ADDR(r11, opal)
926daea1175SBenjamin Herrenschmidt	std	r28,0(r11);
927daea1175SBenjamin Herrenschmidt	std	r29,8(r11);
928daea1175SBenjamin Herrenschmidt#endif
929daea1175SBenjamin Herrenschmidt
9302d27cfd3SBenjamin Herrenschmidt#ifndef CONFIG_PPC_BOOK3E
93114cf11afSPaul Mackerras	mfmsr	r6
93214cf11afSPaul Mackerras	ori	r6,r6,MSR_RI
93314cf11afSPaul Mackerras	mtmsrd	r6			/* RI on */
9342d27cfd3SBenjamin Herrenschmidt#endif
93514cf11afSPaul Mackerras
936549e8152SPaul Mackerras#ifdef CONFIG_RELOCATABLE
937549e8152SPaul Mackerras	/* Save the physical address we're running at in kernstart_addr */
938549e8152SPaul Mackerras	LOAD_REG_ADDR(r4, kernstart_addr)
939549e8152SPaul Mackerras	clrldi	r0,r25,2
940549e8152SPaul Mackerras	std	r0,0(r4)
941549e8152SPaul Mackerras#endif
942549e8152SPaul Mackerras
943e31aa453SPaul Mackerras	/* The following gets the stack set up with the regs */
94414cf11afSPaul Mackerras	/* pointing to the real addr of the kernel stack.  This is   */
94514cf11afSPaul Mackerras	/* all done to support the C function call below which sets  */
94614cf11afSPaul Mackerras	/* up the htab.  This is done because we have relocated the  */
94714cf11afSPaul Mackerras	/* kernel but are still running in real mode. */
94814cf11afSPaul Mackerras
949e31aa453SPaul Mackerras	LOAD_REG_ADDR(r3,init_thread_union)
95014cf11afSPaul Mackerras
951e31aa453SPaul Mackerras	/* set up a stack pointer */
95214cf11afSPaul Mackerras	addi	r1,r3,THREAD_SIZE
95314cf11afSPaul Mackerras	li	r0,0
95414cf11afSPaul Mackerras	stdu	r0,-STACK_FRAME_OVERHEAD(r1)
95514cf11afSPaul Mackerras
956376af594SMichael Ellerman	/*
957376af594SMichael Ellerman	 * Do very early kernel initializations, including initial hash table
958376af594SMichael Ellerman	 * and SLB setup before we turn on relocation.
959376af594SMichael Ellerman	 */
96014cf11afSPaul Mackerras
96114cf11afSPaul Mackerras	/* Restore parameters passed from prom_init/kexec */
96214cf11afSPaul Mackerras	mr	r3,r31
963b1576fecSAnton Blanchard	bl	early_setup		/* also sets r13 and SPRG_PACA */
96414cf11afSPaul Mackerras
965ad0289e4SAnton Blanchard	LOAD_REG_ADDR(r3, start_here_common)
966e31aa453SPaul Mackerras	ld	r4,PACAKMSR(r13)
967b5bbeb23SPaul Mackerras	mtspr	SPRN_SRR0,r3
968b5bbeb23SPaul Mackerras	mtspr	SPRN_SRR1,r4
9692d27cfd3SBenjamin Herrenschmidt	RFI
97014cf11afSPaul Mackerras	b	.	/* prevent speculative execution */
97114cf11afSPaul Mackerras
97214cf11afSPaul Mackerras	/* This is where all platforms converge execution */
973ad0289e4SAnton Blanchard
974ad0289e4SAnton Blanchardstart_here_common:
97514cf11afSPaul Mackerras	/* relocation is on at this point */
97614cf11afSPaul Mackerras	std	r1,PACAKSAVE(r13)
97714cf11afSPaul Mackerras
978e31aa453SPaul Mackerras	/* Load the TOC (virtual address) */
979e31aa453SPaul Mackerras	ld	r2,PACATOC(r13)
98014cf11afSPaul Mackerras
9817230c564SBenjamin Herrenschmidt	/* Mark interrupts soft and hard disabled (they might be enabled
9827230c564SBenjamin Herrenschmidt	 * in the PACA when doing hotplug)
9837230c564SBenjamin Herrenschmidt	 */
9847230c564SBenjamin Herrenschmidt	li	r0,0
9857230c564SBenjamin Herrenschmidt	stb	r0,PACASOFTIRQEN(r13)
9867230c564SBenjamin Herrenschmidt	li	r0,PACA_IRQ_HARD_DIS
9877230c564SBenjamin Herrenschmidt	stb	r0,PACAIRQHAPPENED(r13)
98814cf11afSPaul Mackerras
9897230c564SBenjamin Herrenschmidt	/* Generic kernel entry */
990b1576fecSAnton Blanchard	bl	start_kernel
99114cf11afSPaul Mackerras
992f1870f77SAnton Blanchard	/* Not reached */
993f1870f77SAnton Blanchard	BUG_OPCODE
99414cf11afSPaul Mackerras
99514cf11afSPaul Mackerras/*
99614cf11afSPaul Mackerras * We put a few things here that have to be page-aligned.
99714cf11afSPaul Mackerras * This stuff goes at the beginning of the bss, which is page-aligned.
99814cf11afSPaul Mackerras */
99914cf11afSPaul Mackerras	.section ".bss"
100043a5c684SAneesh Kumar K.V/*
100143a5c684SAneesh Kumar K.V * pgd dir should be aligned to PGD_TABLE_SIZE which is 64K.
100243a5c684SAneesh Kumar K.V * We will need to find a better way to fix this
100343a5c684SAneesh Kumar K.V */
100443a5c684SAneesh Kumar K.V	.align	16
100514cf11afSPaul Mackerras
100614cf11afSPaul Mackerras	.globl	swapper_pg_dir
100714cf11afSPaul Mackerrasswapper_pg_dir:
1008ee7a76daSStephen Rothwell	.space	PGD_TABLE_SIZE
100943a5c684SAneesh Kumar K.V
101043a5c684SAneesh Kumar K.V	.globl	empty_zero_page
101143a5c684SAneesh Kumar K.Vempty_zero_page:
101243a5c684SAneesh Kumar K.V	.space	PAGE_SIZE
10139445aa1aSAl ViroEXPORT_SYMBOL(empty_zero_page)
1014