blob: 06c7251c1bf7df7a617627e45f285ee9562a8f47 [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002 * PowerPC version
3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4 *
5 * Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
6 * Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
7 * Adapted for Power Macintosh by Paul Mackerras.
8 * Low-level exception handlers and MMU support
9 * rewritten by Paul Mackerras.
10 * Copyright (C) 1996 Paul Mackerras.
11 *
12 * Adapted for 64bit PowerPC by Dave Engebretsen, Peter Bergner, and
13 * Mike Corrigan {engebret|bergner|mikejc}@us.ibm.com
14 *
Benjamin Herrenschmidt0ebc4cd2009-06-02 21:17:38 +000015 * This file contains the entry point for the 64-bit kernel along
16 * with some early initialization code common to all 64-bit powerpc
17 * variants.
Paul Mackerras14cf11a2005-09-26 16:04:21 +100018 *
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version
22 * 2 of the License, or (at your option) any later version.
23 */
24
Paul Mackerras14cf11a2005-09-26 16:04:21 +100025#include <linux/threads.h>
Paul Mackerrasb5bbeb22005-10-10 14:01:07 +100026#include <asm/reg.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100027#include <asm/page.h>
28#include <asm/mmu.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100029#include <asm/ppc_asm.h>
30#include <asm/asm-offsets.h>
31#include <asm/bug.h>
32#include <asm/cputable.h>
33#include <asm/setup.h>
34#include <asm/hvcall.h>
Kelly Dalyc43a55f2005-11-02 15:02:47 +110035#include <asm/iseries/lpar_map.h>
David Gibson6cb7bfe2005-10-21 15:45:50 +100036#include <asm/thread_info.h>
Stephen Rothwell3f639ee2006-09-25 18:19:00 +100037#include <asm/firmware.h>
Stephen Rothwell16a15a32007-08-20 14:58:36 +100038#include <asm/page_64.h>
Benjamin Herrenschmidt945feb12008-04-17 14:35:01 +100039#include <asm/irqflags.h>
Alexander Graf2191d652010-04-16 00:11:32 +020040#include <asm/kvm_book3s_asm.h>
Stephen Rothwell46f52212010-11-18 15:06:17 +000041#include <asm/ptrace.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100042
Lucas De Marchi25985ed2011-03-30 22:57:33 -030043/* The physical memory is laid out such that the secondary processor
Benjamin Herrenschmidt0ebc4cd2009-06-02 21:17:38 +000044 * spin code sits at 0x0000...0x00ff. On server, the vectors follow
45 * using the layout described in exceptions-64s.S
Paul Mackerras14cf11a2005-09-26 16:04:21 +100046 */
47
48/*
49 * Entering into this code we make the following assumptions:
Benjamin Herrenschmidt0ebc4cd2009-06-02 21:17:38 +000050 *
51 * For pSeries or server processors:
Paul Mackerras14cf11a2005-09-26 16:04:21 +100052 * 1. The MMU is off & open firmware is running in real mode.
53 * 2. The kernel is entered at __start
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +000054 * -or- For OPAL entry:
55 * 1. The MMU is off, processor in HV mode, primary CPU enters at 0
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +000056 * with device-tree in gpr3. We also get OPAL base in r8 and
57 * entry in r9 for debugging purposes
Benjamin Herrenschmidt27f44882011-09-19 18:27:58 +000058 * 2. Secondary processors enter at 0x60 with PIR in gpr3
Paul Mackerras14cf11a2005-09-26 16:04:21 +100059 *
60 * For iSeries:
61 * 1. The MMU is on (as it always is for iSeries)
62 * 2. The kernel is entered at system_reset_iSeries
Benjamin Herrenschmidt0ebc4cd2009-06-02 21:17:38 +000063 *
64 * For Book3E processors:
65 * 1. The MMU is on running in AS0 in a state defined in ePAPR
66 * 2. The kernel is entered at __start
Paul Mackerras14cf11a2005-09-26 16:04:21 +100067 */
68
69 .text
70 .globl _stext
71_stext:
Paul Mackerras14cf11a2005-09-26 16:04:21 +100072_GLOBAL(__start)
73 /* NOP this out unconditionally */
74BEGIN_FTR_SECTION
Paul Mackerrasb85a0462005-10-06 10:59:19 +100075 b .__start_initialization_multiplatform
Paul Mackerras14cf11a2005-09-26 16:04:21 +100076END_FTR_SECTION(0, 1)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100077
78 /* Catch branch to 0 in real mode */
79 trap
80
Paul Mackerras1f6a93e2008-08-30 11:40:24 +100081 /* Secondary processors spin on this value until it becomes nonzero.
82 * When it does it contains the real address of the descriptor
83 * of the function that the cpu should jump to to continue
84 * initialization.
85 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +100086 .globl __secondary_hold_spinloop
87__secondary_hold_spinloop:
88 .llong 0x0
89
90 /* Secondary processors write this value with their cpu # */
91 /* after they enter the spin loop immediately below. */
92 .globl __secondary_hold_acknowledge
93__secondary_hold_acknowledge:
94 .llong 0x0
95
Michael Ellerman1dce0e302006-06-23 18:15:37 +100096#ifdef CONFIG_PPC_ISERIES
97 /*
98 * At offset 0x20, there is a pointer to iSeries LPAR data.
99 * This is required by the hypervisor
100 */
101 . = 0x20
102 .llong hvReleaseData-KERNELBASE
103#endif /* CONFIG_PPC_ISERIES */
104
Sonny Rao928a3192010-11-18 00:35:07 +0000105#ifdef CONFIG_RELOCATABLE
Milton Miller8b8b0cc2008-10-23 18:41:09 +0000106 /* This flag is set to 1 by a loader if the kernel should run
107 * at the loaded address instead of the linked address. This
108 * is used by kexec-tools to keep the the kdump kernel in the
109 * crash_kernel region. The loader is responsible for
110 * observing the alignment requirement.
111 */
112 /* Do not move this variable as kexec-tools knows about it. */
113 . = 0x5c
114 .globl __run_at_load
115__run_at_load:
116 .long 0x72756e30 /* "run0" -- relocate to 0 by default */
117#endif
118
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000119 . = 0x60
120/*
Geoff Levand75423b72007-06-16 08:06:23 +1000121 * The following code is used to hold secondary processors
122 * in a spin loop after they have entered the kernel, but
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000123 * before the bulk of the kernel has been relocated. This code
124 * is relocated to physical address 0x60 before prom_init is run.
125 * All of it must fit below the first exception vector at 0x100.
Paul Mackerras1f6a93e2008-08-30 11:40:24 +1000126 * Use .globl here not _GLOBAL because we want __secondary_hold
127 * to be the actual text address, not a descriptor.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000128 */
Paul Mackerras1f6a93e2008-08-30 11:40:24 +1000129 .globl __secondary_hold
130__secondary_hold:
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000131#ifndef CONFIG_PPC_BOOK3E
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000132 mfmsr r24
133 ori r24,r24,MSR_RI
134 mtmsrd r24 /* RI on */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000135#endif
Anton Blanchardf1870f72006-02-13 18:11:13 +1100136 /* Grab our physical cpu number */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000137 mr r24,r3
138
139 /* Tell the master cpu we're here */
140 /* Relocation is off & we are located at an address less */
141 /* than 0x100, so only need to grab low order offset. */
Paul Mackerrase31aa452008-08-30 11:41:12 +1000142 std r24,__secondary_hold_acknowledge-_stext(0)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000143 sync
144
145 /* All secondary cpus wait here until told to start. */
Paul Mackerrase31aa452008-08-30 11:41:12 +1000146100: ld r4,__secondary_hold_spinloop-_stext(0)
Paul Mackerras1f6a93e2008-08-30 11:40:24 +1000147 cmpdi 0,r4,0
148 beq 100b
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000149
Anton Blanchardf1870f72006-02-13 18:11:13 +1100150#if defined(CONFIG_SMP) || defined(CONFIG_KEXEC)
Paul Mackerras1f6a93e2008-08-30 11:40:24 +1000151 ld r4,0(r4) /* deref function descriptor */
Michael Ellerman758438a2005-12-05 15:49:00 -0600152 mtctr r4
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000153 mr r3,r24
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000154 li r4,0
Benjamin Herrenschmidtdd797732011-04-05 14:34:58 +1000155 /* Make sure that patched code is visible */
156 isync
Michael Ellerman758438a2005-12-05 15:49:00 -0600157 bctr
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000158#else
159 BUG_OPCODE
160#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000161
162/* This value is used to mark exception frames on the stack. */
163 .section ".toc","aw"
164exception_marker:
165 .tc ID_72656773_68657265[TC],0x7265677368657265
166 .text
167
168/*
Benjamin Herrenschmidt0ebc4cd2009-06-02 21:17:38 +0000169 * On server, we include the exception vectors code here as it
170 * relies on absolute addressing which is only possible within
171 * this compilation unit
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000172 */
Benjamin Herrenschmidt0ebc4cd2009-06-02 21:17:38 +0000173#ifdef CONFIG_PPC_BOOK3S
174#include "exceptions-64s.S"
Paul Mackerras1f6a93e2008-08-30 11:40:24 +1000175#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000176
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000177_GLOBAL(generic_secondary_thread_init)
178 mr r24,r3
179
180 /* turn on 64-bit mode */
181 bl .enable_64b_mode
182
183 /* get a valid TOC pointer, wherever we're mapped at */
184 bl .relative_toc
185
186#ifdef CONFIG_PPC_BOOK3E
187 /* Book3E initialization */
188 mr r3,r24
189 bl .book3e_secondary_thread_init
190#endif
191 b generic_secondary_common_init
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000192
193/*
Olof Johanssonf39b7a52006-08-11 00:07:08 -0500194 * On pSeries and most other platforms, secondary processors spin
195 * in the following code.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000196 * At entry, r3 = this processor's number (physical cpu id)
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000197 *
198 * On Book3E, r4 = 1 to indicate that the initial TLB entry for
199 * this core already exists (setup via some other mechanism such
200 * as SCOM before entry).
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000201 */
Olof Johanssonf39b7a52006-08-11 00:07:08 -0500202_GLOBAL(generic_secondary_smp_init)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000203 mr r24,r3
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000204 mr r25,r4
205
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000206 /* turn on 64-bit mode */
207 bl .enable_64b_mode
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000208
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000209 /* get a valid TOC pointer, wherever we're mapped at */
Paul Mackerrase31aa452008-08-30 11:41:12 +1000210 bl .relative_toc
211
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000212#ifdef CONFIG_PPC_BOOK3E
213 /* Book3E initialization */
214 mr r3,r24
215 mr r4,r25
216 bl .book3e_secondary_core_init
217#endif
218
219generic_secondary_common_init:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000220 /* Set up a paca value for this processor. Since we have the
221 * physical cpu id in r24, we need to search the pacas to find
222 * which logical id maps to our physical one.
223 */
Michael Ellerman1426d5a2010-01-28 13:23:22 +0000224 LOAD_REG_ADDR(r13, paca) /* Load paca pointer */
225 ld r13,0(r13) /* Get base vaddr of paca array */
Milton Miller768d18a2011-05-10 19:28:37 +0000226#ifndef CONFIG_SMP
227 addi r13,r13,PACA_SIZE /* know r13 if used accidentally */
228 b .kexec_wait /* wait for next kernel if !SMP */
229#else
230 LOAD_REG_ADDR(r7, nr_cpu_ids) /* Load nr_cpu_ids address */
231 lwz r7,0(r7) /* also the max paca allocated */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000232 li r5,0 /* logical cpu id */
2331: lhz r6,PACAHWCPUID(r13) /* Load HW procid from paca */
234 cmpw r6,r24 /* Compare to our id */
235 beq 2f
236 addi r13,r13,PACA_SIZE /* Loop to next PACA on miss */
237 addi r5,r5,1
Milton Miller768d18a2011-05-10 19:28:37 +0000238 cmpw r5,r7 /* Check if more pacas exist */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000239 blt 1b
240
241 mr r3,r24 /* not found, copy phys to r3 */
242 b .kexec_wait /* next kernel might do better */
243
Benjamin Herrenschmidt2dd60d72011-01-20 17:50:21 +11002442: SET_PACA(r13)
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000245#ifdef CONFIG_PPC_BOOK3E
246 addi r12,r13,PACA_EXTLB /* and TLB exc frame in another */
247 mtspr SPRN_SPRG_TLB_EXFRAME,r12
248#endif
249
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000250 /* From now on, r24 is expected to be logical cpuid */
251 mr r24,r5
Sonny Raob6f6b982008-07-12 09:00:26 +1000252
Olof Johanssonf39b7a52006-08-11 00:07:08 -0500253 /* See if we need to call a cpu state restore handler */
Paul Mackerrase31aa452008-08-30 11:41:12 +1000254 LOAD_REG_ADDR(r23, cur_cpu_spec)
Olof Johanssonf39b7a52006-08-11 00:07:08 -0500255 ld r23,0(r23)
256 ld r23,CPU_SPEC_RESTORE(r23)
257 cmpdi 0,r23,0
Benjamin Herrenschmidt9d07bc82011-03-16 14:54:35 +1100258 beq 3f
Olof Johanssonf39b7a52006-08-11 00:07:08 -0500259 ld r23,0(r23)
260 mtctr r23
261 bctrl
262
Matt Evans7ac87ab2011-05-25 18:09:12 +00002633: LOAD_REG_ADDR(r3, spinning_secondaries) /* Decrement spinning_secondaries */
Benjamin Herrenschmidt9d07bc82011-03-16 14:54:35 +1100264 lwarx r4,0,r3
265 subi r4,r4,1
266 stwcx. r4,0,r3
267 bne 3b
268 isync
269
2704: HMT_LOW
Benjamin Herrenschmidtad0693e2011-02-01 12:13:09 +1100271 lbz r23,PACAPROCSTART(r13) /* Test if this processor should */
272 /* start. */
Benjamin Herrenschmidtad0693e2011-02-01 12:13:09 +1100273 cmpwi 0,r23,0
Benjamin Herrenschmidt9d07bc82011-03-16 14:54:35 +1100274 beq 4b /* Loop until told to go */
Benjamin Herrenschmidtad0693e2011-02-01 12:13:09 +1100275
276 sync /* order paca.run and cur_cpu_spec */
Benjamin Herrenschmidt9d07bc82011-03-16 14:54:35 +1100277 isync /* In case code patching happened */
Benjamin Herrenschmidtad0693e2011-02-01 12:13:09 +1100278
Benjamin Herrenschmidt9d07bc82011-03-16 14:54:35 +1100279 /* Create a temp kernel stack for use before relocation is on. */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000280 ld r1,PACAEMERGSP(r13)
281 subi r1,r1,STACK_FRAME_OVERHEAD
282
Stephen Rothwellc7056772006-11-27 14:59:50 +1100283 b __secondary_start
Milton Miller768d18a2011-05-10 19:28:37 +0000284#endif /* SMP */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000285
Paul Mackerrase31aa452008-08-30 11:41:12 +1000286/*
287 * Turn the MMU off.
288 * Assumes we're mapped EA == RA if the MMU is on.
289 */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000290#ifdef CONFIG_PPC_BOOK3S
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000291_STATIC(__mmu_off)
292 mfmsr r3
293 andi. r0,r3,MSR_IR|MSR_DR
294 beqlr
Paul Mackerrase31aa452008-08-30 11:41:12 +1000295 mflr r4
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000296 andc r3,r3,r0
297 mtspr SPRN_SRR0,r4
298 mtspr SPRN_SRR1,r3
299 sync
300 rfid
301 b . /* prevent speculative execution */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000302#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000303
304
305/*
306 * Here is our main kernel entry point. We support currently 2 kind of entries
307 * depending on the value of r5.
308 *
309 * r5 != NULL -> OF entry, we go to prom_init, "legacy" parameter content
310 * in r3...r7
311 *
312 * r5 == NULL -> kexec style entry. r3 is a physical pointer to the
313 * DT block, r4 is a physical pointer to the kernel itself
314 *
315 */
316_GLOBAL(__start_initialization_multiplatform)
Paul Mackerrase31aa452008-08-30 11:41:12 +1000317 /* Make sure we are running in 64 bits mode */
318 bl .enable_64b_mode
319
320 /* Get TOC pointer (current runtime address) */
321 bl .relative_toc
322
323 /* find out where we are now */
324 bcl 20,31,$+4
3250: mflr r26 /* r26 = runtime addr here */
326 addis r26,r26,(_stext - 0b)@ha
327 addi r26,r26,(_stext - 0b)@l /* current runtime base addr */
328
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000329 /*
330 * Are we booted from a PROM Of-type client-interface ?
331 */
332 cmpldi cr0,r5,0
Stephen Rothwell939e60f62007-07-31 16:44:13 +1000333 beq 1f
334 b .__boot_from_prom /* yes -> prom */
3351:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000336 /* Save parameters */
337 mr r31,r3
338 mr r30,r4
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000339#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
340 /* Save OPAL entry */
341 mr r28,r8
342 mr r29,r9
343#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000344
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000345#ifdef CONFIG_PPC_BOOK3E
346 bl .start_initialization_book3e
347 b .__after_prom_start
348#else
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000349 /* Setup some critical 970 SPRs before switching MMU off */
Olof Johanssonf39b7a52006-08-11 00:07:08 -0500350 mfspr r0,SPRN_PVR
351 srwi r0,r0,16
352 cmpwi r0,0x39 /* 970 */
353 beq 1f
354 cmpwi r0,0x3c /* 970FX */
355 beq 1f
356 cmpwi r0,0x44 /* 970MP */
Olof Johansson190a24f2006-10-25 17:32:40 -0500357 beq 1f
358 cmpwi r0,0x45 /* 970GX */
Olof Johanssonf39b7a52006-08-11 00:07:08 -0500359 bne 2f
3601: bl .__cpu_preinit_ppc970
3612:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000362
Paul Mackerrase31aa452008-08-30 11:41:12 +1000363 /* Switch off MMU if not already off */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000364 bl .__mmu_off
365 b .__after_prom_start
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000366#endif /* CONFIG_PPC_BOOK3E */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000367
Stephen Rothwell939e60f62007-07-31 16:44:13 +1000368_INIT_STATIC(__boot_from_prom)
Benjamin Herrenschmidt28794d32009-03-10 17:53:27 +0000369#ifdef CONFIG_PPC_OF_BOOT_TRAMPOLINE
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000370 /* Save parameters */
371 mr r31,r3
372 mr r30,r4
373 mr r29,r5
374 mr r28,r6
375 mr r27,r7
376
Olaf Hering60888572006-03-23 21:50:59 +0100377 /*
378 * Align the stack to 16-byte boundary
379 * Depending on the size and layout of the ELF sections in the initial
Paul Mackerrase31aa452008-08-30 11:41:12 +1000380 * boot binary, the stack pointer may be unaligned on PowerMac
Olaf Hering60888572006-03-23 21:50:59 +0100381 */
Linus Torvaldsc05b4772006-03-04 15:00:45 -0800382 rldicr r1,r1,0,59
383
Paul Mackerras549e8152008-08-30 11:43:47 +1000384#ifdef CONFIG_RELOCATABLE
385 /* Relocate code for where we are now */
386 mr r3,r26
387 bl .relocate
388#endif
389
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000390 /* Restore parameters */
391 mr r3,r31
392 mr r4,r30
393 mr r5,r29
394 mr r6,r28
395 mr r7,r27
396
397 /* Do all of the interaction with OF client interface */
Paul Mackerras549e8152008-08-30 11:43:47 +1000398 mr r8,r26
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000399 bl .prom_init
Benjamin Herrenschmidt28794d32009-03-10 17:53:27 +0000400#endif /* #CONFIG_PPC_OF_BOOT_TRAMPOLINE */
401
402 /* We never return. We also hit that trap if trying to boot
403 * from OF while CONFIG_PPC_OF_BOOT_TRAMPOLINE isn't selected */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000404 trap
405
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000406_STATIC(__after_prom_start)
Paul Mackerras549e8152008-08-30 11:43:47 +1000407#ifdef CONFIG_RELOCATABLE
408 /* process relocations for the final address of the kernel */
409 lis r25,PAGE_OFFSET@highest /* compute virtual base of kernel */
410 sldi r25,r25,32
Milton Miller8b8b0cc2008-10-23 18:41:09 +0000411 lwz r7,__run_at_load-_stext(r26)
Sonny Rao928a3192010-11-18 00:35:07 +0000412 cmplwi cr0,r7,1 /* flagged to stay where we are ? */
Mohan Kumar M54622f12008-10-21 17:38:10 +0000413 bne 1f
414 add r25,r25,r26
Mohan Kumar M54622f12008-10-21 17:38:10 +00004151: mr r3,r25
Paul Mackerras549e8152008-08-30 11:43:47 +1000416 bl .relocate
417#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000418
419/*
Paul Mackerrase31aa452008-08-30 11:41:12 +1000420 * We need to run with _stext at physical address PHYSICAL_START.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000421 * This will leave some code in the first 256B of
422 * real memory, which are reserved for software use.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000423 *
424 * Note: This process overwrites the OF exception vectors.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000425 */
Paul Mackerras549e8152008-08-30 11:43:47 +1000426 li r3,0 /* target addr */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000427#ifdef CONFIG_PPC_BOOK3E
428 tovirt(r3,r3) /* on booke, we already run at PAGE_OFFSET */
429#endif
Paul Mackerras549e8152008-08-30 11:43:47 +1000430 mr. r4,r26 /* In some cases the loader may */
Paul Mackerrase31aa452008-08-30 11:41:12 +1000431 beq 9f /* have already put us at zero */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000432 li r6,0x100 /* Start offset, the first 0x100 */
433 /* bytes were copied earlier. */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000434#ifdef CONFIG_PPC_BOOK3E
435 tovirt(r6,r6) /* on booke, we already run at PAGE_OFFSET */
436#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000437
Mohan Kumar M54622f12008-10-21 17:38:10 +0000438#ifdef CONFIG_CRASH_DUMP
439/*
440 * Check if the kernel has to be running as relocatable kernel based on the
Milton Miller8b8b0cc2008-10-23 18:41:09 +0000441 * variable __run_at_load, if it is set the kernel is treated as relocatable
Mohan Kumar M54622f12008-10-21 17:38:10 +0000442 * kernel, otherwise it will be moved to PHYSICAL_START
443 */
Milton Miller8b8b0cc2008-10-23 18:41:09 +0000444 lwz r7,__run_at_load-_stext(r26)
445 cmplwi cr0,r7,1
Mohan Kumar M54622f12008-10-21 17:38:10 +0000446 bne 3f
447
448 li r5,__end_interrupts - _stext /* just copy interrupts */
449 b 5f
4503:
451#endif
452 lis r5,(copy_to_here - _stext)@ha
453 addi r5,r5,(copy_to_here - _stext)@l /* # bytes of memory to copy */
454
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000455 bl .copy_and_flush /* copy the first n bytes */
456 /* this includes the code being */
457 /* executed here. */
Paul Mackerrase31aa452008-08-30 11:41:12 +1000458 addis r8,r3,(4f - _stext)@ha /* Jump to the copy of this code */
459 addi r8,r8,(4f - _stext)@l /* that we just made */
460 mtctr r8
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000461 bctr
462
Mohan Kumar M54622f12008-10-21 17:38:10 +0000463p_end: .llong _end - _stext
464
Paul Mackerrase31aa452008-08-30 11:41:12 +10004654: /* Now copy the rest of the kernel up to _end */
466 addis r5,r26,(p_end - _stext)@ha
467 ld r5,(p_end - _stext)@l(r5) /* get _end */
Mohan Kumar M54622f12008-10-21 17:38:10 +00004685: bl .copy_and_flush /* copy the rest */
Paul Mackerrase31aa452008-08-30 11:41:12 +1000469
4709: b .start_here_multiplatform
471
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000472/*
473 * Copy routine used to copy the kernel to start at physical address 0
474 * and flush and invalidate the caches as needed.
475 * r3 = dest addr, r4 = source addr, r5 = copy limit, r6 = start offset
476 * on exit, r3, r4, r5 are unchanged, r6 is updated to be >= r5.
477 *
478 * Note: this routine *only* clobbers r0, r6 and lr
479 */
480_GLOBAL(copy_and_flush)
481 addi r5,r5,-8
482 addi r6,r6,-8
Olof Johansson5a2fe382006-09-06 14:34:41 -05004834: li r0,8 /* Use the smallest common */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000484 /* denominator cache line */
485 /* size. This results in */
486 /* extra cache line flushes */
487 /* but operation is correct. */
488 /* Can't get cache line size */
489 /* from NACA as it is being */
490 /* moved too. */
491
492 mtctr r0 /* put # words/line in ctr */
4933: addi r6,r6,8 /* copy a cache line */
494 ldx r0,r6,r4
495 stdx r0,r6,r3
496 bdnz 3b
497 dcbst r6,r3 /* write it to memory */
498 sync
499 icbi r6,r3 /* flush the icache line */
500 cmpld 0,r6,r5
501 blt 4b
502 sync
503 addi r5,r5,8
504 addi r6,r6,8
505 blr
506
507.align 8
508copy_to_here:
509
510#ifdef CONFIG_SMP
511#ifdef CONFIG_PPC_PMAC
512/*
513 * On PowerMac, secondary processors starts from the reset vector, which
514 * is temporarily turned into a call to one of the functions below.
515 */
516 .section ".text";
517 .align 2 ;
518
Paul Mackerras35499c02005-10-22 16:02:39 +1000519 .globl __secondary_start_pmac_0
520__secondary_start_pmac_0:
521 /* NB the entries for cpus 0, 1, 2 must each occupy 8 bytes. */
522 li r24,0
523 b 1f
524 li r24,1
525 b 1f
526 li r24,2
527 b 1f
528 li r24,3
5291:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000530
531_GLOBAL(pmac_secondary_start)
532 /* turn on 64-bit mode */
533 bl .enable_64b_mode
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000534
Benjamin Herrenschmidtc478b582009-01-11 19:03:45 +0000535 li r0,0
536 mfspr r3,SPRN_HID4
537 rldimi r3,r0,40,23 /* clear bit 23 (rm_ci) */
538 sync
539 mtspr SPRN_HID4,r3
540 isync
541 sync
542 slbia
543
Paul Mackerrase31aa452008-08-30 11:41:12 +1000544 /* get TOC pointer (real address) */
545 bl .relative_toc
546
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000547 /* Copy some CPU settings from CPU 0 */
Olof Johanssonf39b7a52006-08-11 00:07:08 -0500548 bl .__restore_cpu_ppc970
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000549
550 /* pSeries do that early though I don't think we really need it */
551 mfmsr r3
552 ori r3,r3,MSR_RI
553 mtmsrd r3 /* RI on */
554
555 /* Set up a paca value for this processor. */
Michael Ellerman1426d5a2010-01-28 13:23:22 +0000556 LOAD_REG_ADDR(r4,paca) /* Load paca pointer */
557 ld r4,0(r4) /* Get base vaddr of paca array */
Paul Mackerrase31aa452008-08-30 11:41:12 +1000558 mulli r13,r24,PACA_SIZE /* Calculate vaddr of right paca */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000559 add r13,r13,r4 /* for this processor. */
Benjamin Herrenschmidt2dd60d72011-01-20 17:50:21 +1100560 SET_PACA(r13) /* Save vaddr of paca in an SPRG*/
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000561
Benjamin Herrenschmidt62cc67b2011-02-21 16:49:58 +1100562 /* Mark interrupts soft and hard disabled (they might be enabled
563 * in the PACA when doing hotplug)
564 */
565 li r0,0
566 stb r0,PACASOFTIRQEN(r13)
567 stb r0,PACAHARDIRQEN(r13)
568
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000569 /* Create a temp kernel stack for use before relocation is on. */
570 ld r1,PACAEMERGSP(r13)
571 subi r1,r1,STACK_FRAME_OVERHEAD
572
Stephen Rothwellc7056772006-11-27 14:59:50 +1100573 b __secondary_start
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000574
575#endif /* CONFIG_PPC_PMAC */
576
577/*
578 * This function is called after the master CPU has released the
579 * secondary processors. The execution environment is relocation off.
580 * The paca for this processor has the following fields initialized at
581 * this point:
582 * 1. Processor number
583 * 2. Segment table pointer (virtual address)
584 * On entry the following are set:
Benjamin Herrenschmidtee43eb72009-07-14 20:52:54 +0000585 * r1 = stack pointer. vaddr for iSeries, raddr (temp stack) for pSeries
586 * r24 = cpu# (in Linux terms)
587 * r13 = paca virtual address
588 * SPRG_PACA = paca virtual address
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000589 */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000590 .section ".text";
591 .align 2 ;
592
Stephen Rothwellfc68e862007-08-22 13:44:58 +1000593 .globl __secondary_start
Stephen Rothwellc7056772006-11-27 14:59:50 +1100594__secondary_start:
Paul Mackerras799d6042005-11-10 13:37:51 +1100595 /* Set thread priority to MEDIUM */
596 HMT_MEDIUM
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000597
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000598 /* Initialize the kernel stack. Just a repeat for iSeries. */
David Gibsone58c3492006-01-13 14:56:25 +1100599 LOAD_REG_ADDR(r3, current_set)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000600 sldi r28,r24,3 /* get current_set[cpu#] */
Michael Neuling54a83402010-08-25 21:04:25 +0000601 ldx r14,r3,r28
602 addi r14,r14,THREAD_SIZE-STACK_FRAME_OVERHEAD
603 std r14,PACAKSAVE(r13)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000604
Matt Evansf7616222010-08-12 20:58:28 +0000605 /* Do early setup for that CPU (stab, slb, hash table pointer) */
606 bl .early_setup_secondary
607
Michael Neuling54a83402010-08-25 21:04:25 +0000608 /*
609 * setup the new stack pointer, but *don't* use this until
610 * translation is on.
611 */
612 mr r1, r14
613
Paul Mackerras799d6042005-11-10 13:37:51 +1100614 /* Clear backchain so we get nice backtraces */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000615 li r7,0
616 mtlr r7
617
618 /* enable MMU and jump to start_secondary */
David Gibsone58c3492006-01-13 14:56:25 +1100619 LOAD_REG_ADDR(r3, .start_secondary_prolog)
620 LOAD_REG_IMMEDIATE(r4, MSR_KERNEL)
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000621#ifdef CONFIG_PPC_ISERIES
Stephen Rothwell3f639ee2006-09-25 18:19:00 +1000622BEGIN_FW_FTR_SECTION
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000623 ori r4,r4,MSR_EE
Benjamin Herrenschmidtff3da2e2008-04-02 15:58:40 +1100624 li r8,1
625 stb r8,PACAHARDIRQEN(r13)
Stephen Rothwell3f639ee2006-09-25 18:19:00 +1000626END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000627#endif
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000628BEGIN_FW_FTR_SECTION
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000629 stb r7,PACAHARDIRQEN(r13)
630END_FW_FTR_SECTION_IFCLR(FW_FEATURE_ISERIES)
Benjamin Herrenschmidtff3da2e2008-04-02 15:58:40 +1100631 stb r7,PACASOFTIRQEN(r13)
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000632
Paul Mackerrasb5bbeb22005-10-10 14:01:07 +1000633 mtspr SPRN_SRR0,r3
634 mtspr SPRN_SRR1,r4
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000635 RFI
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000636 b . /* prevent speculative execution */
637
638/*
639 * Running with relocation on at this point. All we want to do is
Paul Mackerrase31aa452008-08-30 11:41:12 +1000640 * zero the stack back-chain pointer and get the TOC virtual address
641 * before going into C code.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000642 */
643_GLOBAL(start_secondary_prolog)
Paul Mackerrase31aa452008-08-30 11:41:12 +1000644 ld r2,PACATOC(r13)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000645 li r3,0
646 std r3,0(r1) /* Zero the stack frame pointer */
647 bl .start_secondary
Paul Mackerras799d6042005-11-10 13:37:51 +1100648 b .
Vaidyanathan Srinivasan8dbce532010-03-01 02:58:09 +0000649/*
650 * Reset stack pointer and call start_secondary
651 * to continue with online operation when woken up
652 * from cede in cpu offline.
653 */
654_GLOBAL(start_secondary_resume)
655 ld r1,PACAKSAVE(r13) /* Reload kernel stack pointer */
656 li r3,0
657 std r3,0(r1) /* Zero the stack frame pointer */
658 bl .start_secondary
659 b .
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000660#endif
661
662/*
663 * This subroutine clobbers r11 and r12
664 */
665_GLOBAL(enable_64b_mode)
666 mfmsr r11 /* grab the current MSR */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000667#ifdef CONFIG_PPC_BOOK3E
668 oris r11,r11,0x8000 /* CM bit set, we'll set ICM later */
669 mtmsr r11
670#else /* CONFIG_PPC_BOOK3E */
Michael Ellerman9f0b0792011-04-07 21:56:03 +0000671 li r12,(MSR_64BIT | MSR_ISF)@highest
Paul Mackerrase31aa452008-08-30 11:41:12 +1000672 sldi r12,r12,48
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000673 or r11,r11,r12
674 mtmsrd r11
675 isync
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000676#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000677 blr
678
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000679/*
Paul Mackerrase31aa452008-08-30 11:41:12 +1000680 * This puts the TOC pointer into r2, offset by 0x8000 (as expected
681 * by the toolchain). It computes the correct value for wherever we
682 * are running at the moment, using position-independent code.
683 */
684_GLOBAL(relative_toc)
685 mflr r0
686 bcl 20,31,$+4
Benjamin Herrenschmidte5505922011-09-19 17:44:51 +00006870: mflr r11
688 ld r2,(p_toc - 0b)(r11)
689 add r2,r2,r11
Paul Mackerrase31aa452008-08-30 11:41:12 +1000690 mtlr r0
691 blr
692
693p_toc: .llong __toc_start + 0x8000 - 0b
694
695/*
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000696 * This is where the main kernel code starts.
697 */
Stephen Rothwell939e60f62007-07-31 16:44:13 +1000698_INIT_STATIC(start_here_multiplatform)
Paul Mackerrase31aa452008-08-30 11:41:12 +1000699 /* set up the TOC (real address) */
700 bl .relative_toc
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000701
702 /* Clear out the BSS. It may have been done in prom_init,
703 * already but that's irrelevant since prom_init will soon
704 * be detached from the kernel completely. Besides, we need
705 * to clear it now for kexec-style entry.
706 */
Paul Mackerrase31aa452008-08-30 11:41:12 +1000707 LOAD_REG_ADDR(r11,__bss_stop)
708 LOAD_REG_ADDR(r8,__bss_start)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000709 sub r11,r11,r8 /* bss size */
710 addi r11,r11,7 /* round up to an even double word */
Paul Mackerrase31aa452008-08-30 11:41:12 +1000711 srdi. r11,r11,3 /* shift right by 3 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000712 beq 4f
713 addi r8,r8,-8
714 li r0,0
715 mtctr r11 /* zero this many doublewords */
7163: stdu r0,8(r8)
717 bdnz 3b
7184:
719
Benjamin Herrenschmidtdaea1172011-09-19 17:44:59 +0000720#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
721 /* Setup OPAL entry */
722 std r28,0(r11);
723 std r29,8(r11);
724#endif
725
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000726#ifndef CONFIG_PPC_BOOK3E
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000727 mfmsr r6
728 ori r6,r6,MSR_RI
729 mtmsrd r6 /* RI on */
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000730#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000731
Paul Mackerras549e8152008-08-30 11:43:47 +1000732#ifdef CONFIG_RELOCATABLE
733 /* Save the physical address we're running at in kernstart_addr */
734 LOAD_REG_ADDR(r4, kernstart_addr)
735 clrldi r0,r25,2
736 std r0,0(r4)
737#endif
738
Paul Mackerrase31aa452008-08-30 11:41:12 +1000739 /* The following gets the stack set up with the regs */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000740 /* pointing to the real addr of the kernel stack. This is */
741 /* all done to support the C function call below which sets */
742 /* up the htab. This is done because we have relocated the */
743 /* kernel but are still running in real mode. */
744
Paul Mackerrase31aa452008-08-30 11:41:12 +1000745 LOAD_REG_ADDR(r3,init_thread_union)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000746
Paul Mackerrase31aa452008-08-30 11:41:12 +1000747 /* set up a stack pointer */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000748 addi r1,r3,THREAD_SIZE
749 li r0,0
750 stdu r0,-STACK_FRAME_OVERHEAD(r1)
751
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000752 /* Do very early kernel initializations, including initial hash table,
753 * stab and slb setup before we turn on relocation. */
754
755 /* Restore parameters passed from prom_init/kexec */
756 mr r3,r31
Benjamin Herrenschmidtee43eb72009-07-14 20:52:54 +0000757 bl .early_setup /* also sets r13 and SPRG_PACA */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000758
Paul Mackerrase31aa452008-08-30 11:41:12 +1000759 LOAD_REG_ADDR(r3, .start_here_common)
760 ld r4,PACAKMSR(r13)
Paul Mackerrasb5bbeb22005-10-10 14:01:07 +1000761 mtspr SPRN_SRR0,r3
762 mtspr SPRN_SRR1,r4
Benjamin Herrenschmidt2d27cfd2009-07-23 23:15:59 +0000763 RFI
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000764 b . /* prevent speculative execution */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000765
766 /* This is where all platforms converge execution */
Stephen Rothwellfc68e862007-08-22 13:44:58 +1000767_INIT_GLOBAL(start_here_common)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000768 /* relocation is on at this point */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000769 std r1,PACAKSAVE(r13)
770
Paul Mackerrase31aa452008-08-30 11:41:12 +1000771 /* Load the TOC (virtual address) */
772 ld r2,PACATOC(r13)
773
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000774 bl .setup_system
775
776 /* Load up the kernel context */
7775:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000778 li r5,0
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000779 stb r5,PACASOFTIRQEN(r13) /* Soft Disabled */
780#ifdef CONFIG_PPC_ISERIES
781BEGIN_FW_FTR_SECTION
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000782 mfmsr r5
Benjamin Herrenschmidtff3da2e2008-04-02 15:58:40 +1100783 ori r5,r5,MSR_EE /* Hard Enabled on iSeries*/
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000784 mtmsrd r5
Benjamin Herrenschmidtff3da2e2008-04-02 15:58:40 +1100785 li r5,1
Stephen Rothwell3f639ee2006-09-25 18:19:00 +1000786END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000787#endif
Benjamin Herrenschmidtff3da2e2008-04-02 15:58:40 +1100788 stb r5,PACAHARDIRQEN(r13) /* Hard Disabled on others */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000789
Benjamin Herrenschmidtff3da2e2008-04-02 15:58:40 +1100790 bl .start_kernel
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000791
Anton Blanchardf1870f72006-02-13 18:11:13 +1100792 /* Not reached */
793 BUG_OPCODE
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000794
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000795/*
796 * We put a few things here that have to be page-aligned.
797 * This stuff goes at the beginning of the bss, which is page-aligned.
798 */
799 .section ".bss"
800
801 .align PAGE_SHIFT
802
803 .globl empty_zero_page
804empty_zero_page:
805 .space PAGE_SIZE
806
807 .globl swapper_pg_dir
808swapper_pg_dir:
Stephen Rothwellee7a76d2007-09-18 17:22:59 +1000809 .space PGD_TABLE_SIZE