Merge tag 'kbuild-v4.21-3' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy...
[linux-2.6-block.git] / arch / powerpc / kernel / head_64.S
CommitLineData
14cf11af 1/*
14cf11af
PM
2 * 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 *
0ebc4cda
BH
15 * 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.
14cf11af
PM
18 *
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
14cf11af 25#include <linux/threads.h>
c141611f 26#include <linux/init.h>
b5bbeb23 27#include <asm/reg.h>
14cf11af
PM
28#include <asm/page.h>
29#include <asm/mmu.h>
14cf11af 30#include <asm/ppc_asm.h>
57f26649 31#include <asm/head-64.h>
14cf11af
PM
32#include <asm/asm-offsets.h>
33#include <asm/bug.h>
34#include <asm/cputable.h>
35#include <asm/setup.h>
36#include <asm/hvcall.h>
6cb7bfeb 37#include <asm/thread_info.h>
3f639ee8 38#include <asm/firmware.h>
16a15a30 39#include <asm/page_64.h>
945feb17 40#include <asm/irqflags.h>
2191d657 41#include <asm/kvm_book3s_asm.h>
46f52210 42#include <asm/ptrace.h>
7230c564 43#include <asm/hw_irq.h>
6becef7e 44#include <asm/cputhreads.h>
7a25d912 45#include <asm/ppc-opcode.h>
9445aa1a 46#include <asm/export.h>
2c86cd18 47#include <asm/feature-fixups.h>
14cf11af 48
25985edc 49/* The physical memory is laid out such that the secondary processor
0ebc4cda
BH
50 * spin code sits at 0x0000...0x00ff. On server, the vectors follow
51 * using the layout described in exceptions-64s.S
14cf11af
PM
52 */
53
54/*
55 * Entering into this code we make the following assumptions:
0ebc4cda
BH
56 *
57 * For pSeries or server processors:
14cf11af 58 * 1. The MMU is off & open firmware is running in real mode.
339a3293
NP
59 * 2. The primary CPU enters at __start.
60 * 3. If the RTAS supports "query-cpu-stopped-state", then secondary
61 * CPUs will enter as directed by "start-cpu" RTAS call, which is
62 * generic_secondary_smp_init, with PIR in r3.
63 * 4. Else the secondary CPUs will enter at secondary_hold (0x60) as
64 * directed by the "start-cpu" RTS call, with PIR in r3.
27f44888 65 * -or- For OPAL entry:
339a3293
NP
66 * 1. The MMU is off, processor in HV mode.
67 * 2. The primary CPU enters at 0 with device-tree in r3, OPAL base
68 * in r8, and entry in r9 for debugging purposes.
69 * 3. Secondary CPUs enter as directed by OPAL_START_CPU call, which
70 * is at generic_secondary_smp_init, with PIR in r3.
14cf11af 71 *
0ebc4cda
BH
72 * For Book3E processors:
73 * 1. The MMU is on running in AS0 in a state defined in ePAPR
74 * 2. The kernel is entered at __start
14cf11af
PM
75 */
76
57f26649
NP
77OPEN_FIXED_SECTION(first_256B, 0x0, 0x100)
78USE_FIXED_SECTION(first_256B)
79 /*
80 * Offsets are relative from the start of fixed section, and
81 * first_256B starts at 0. Offsets are a bit easier to use here
82 * than the fixed section entry macros.
83 */
84 . = 0x0
14cf11af
PM
85_GLOBAL(__start)
86 /* NOP this out unconditionally */
87BEGIN_FTR_SECTION
5c0484e2 88 FIXUP_ENDIAN
b1576fec 89 b __start_initialization_multiplatform
14cf11af 90END_FTR_SECTION(0, 1)
14cf11af
PM
91
92 /* Catch branch to 0 in real mode */
93 trap
94
2751b628
AB
95 /* Secondary processors spin on this value until it becomes non-zero.
96 * When non-zero, it contains the real address of the function the cpu
97 * should jump to.
1f6a93e4 98 */
7d4151b5 99 .balign 8
14cf11af
PM
100 .globl __secondary_hold_spinloop
101__secondary_hold_spinloop:
eb039161 102 .8byte 0x0
14cf11af
PM
103
104 /* Secondary processors write this value with their cpu # */
105 /* after they enter the spin loop immediately below. */
106 .globl __secondary_hold_acknowledge
107__secondary_hold_acknowledge:
eb039161 108 .8byte 0x0
14cf11af 109
928a3197 110#ifdef CONFIG_RELOCATABLE
8b8b0cc1
MM
111 /* This flag is set to 1 by a loader if the kernel should run
112 * at the loaded address instead of the linked address. This
113 * is used by kexec-tools to keep the the kdump kernel in the
114 * crash_kernel region. The loader is responsible for
115 * observing the alignment requirement.
116 */
70839d20
NP
117
118#ifdef CONFIG_RELOCATABLE_TEST
119#define RUN_AT_LOAD_DEFAULT 1 /* Test relocation, do not copy to 0 */
120#else
121#define RUN_AT_LOAD_DEFAULT 0x72756e30 /* "run0" -- relocate to 0 by default */
122#endif
123
8b8b0cc1
MM
124 /* Do not move this variable as kexec-tools knows about it. */
125 . = 0x5c
126 .globl __run_at_load
127__run_at_load:
57f26649 128DEFINE_FIXED_SYMBOL(__run_at_load)
70839d20 129 .long RUN_AT_LOAD_DEFAULT
8b8b0cc1
MM
130#endif
131
14cf11af
PM
132 . = 0x60
133/*
75423b7b
GL
134 * The following code is used to hold secondary processors
135 * in a spin loop after they have entered the kernel, but
14cf11af
PM
136 * before the bulk of the kernel has been relocated. This code
137 * is relocated to physical address 0x60 before prom_init is run.
138 * All of it must fit below the first exception vector at 0x100.
1f6a93e4
PM
139 * Use .globl here not _GLOBAL because we want __secondary_hold
140 * to be the actual text address, not a descriptor.
14cf11af 141 */
1f6a93e4
PM
142 .globl __secondary_hold
143__secondary_hold:
5c0484e2 144 FIXUP_ENDIAN
2d27cfd3 145#ifndef CONFIG_PPC_BOOK3E
14cf11af
PM
146 mfmsr r24
147 ori r24,r24,MSR_RI
148 mtmsrd r24 /* RI on */
2d27cfd3 149#endif
f1870f77 150 /* Grab our physical cpu number */
14cf11af 151 mr r24,r3
96f013fe
JX
152 /* stash r4 for book3e */
153 mr r25,r4
14cf11af
PM
154
155 /* Tell the master cpu we're here */
156 /* Relocation is off & we are located at an address less */
157 /* than 0x100, so only need to grab low order offset. */
57f26649 158 std r24,(ABS_ADDR(__secondary_hold_acknowledge))(0)
14cf11af
PM
159 sync
160
96f013fe
JX
161 li r26,0
162#ifdef CONFIG_PPC_BOOK3E
163 tovirt(r26,r26)
164#endif
14cf11af 165 /* All secondary cpus wait here until told to start. */
57f26649 166100: ld r12,(ABS_ADDR(__secondary_hold_spinloop))(r26)
cc7efbf9 167 cmpdi 0,r12,0
1f6a93e4 168 beq 100b
14cf11af 169
da665885 170#if defined(CONFIG_SMP) || defined(CONFIG_KEXEC_CORE)
96f013fe 171#ifdef CONFIG_PPC_BOOK3E
cc7efbf9 172 tovirt(r12,r12)
cc7efbf9
AB
173#endif
174 mtctr r12
14cf11af 175 mr r3,r24
96f013fe
JX
176 /*
177 * it may be the case that other platforms have r4 right to
178 * begin with, this gives us some safety in case it is not
179 */
180#ifdef CONFIG_PPC_BOOK3E
181 mr r4,r25
182#else
2d27cfd3 183 li r4,0
96f013fe 184#endif
dd797738
BH
185 /* Make sure that patched code is visible */
186 isync
758438a7 187 bctr
14cf11af
PM
188#else
189 BUG_OPCODE
190#endif
57f26649 191CLOSE_FIXED_SECTION(first_256B)
14cf11af
PM
192
193/* This value is used to mark exception frames on the stack. */
194 .section ".toc","aw"
195exception_marker:
196 .tc ID_72656773_68657265[TC],0x7265677368657265
57f26649 197 .previous
14cf11af 198
14cf11af 199/*
0ebc4cda
BH
200 * On server, we include the exception vectors code here as it
201 * relies on absolute addressing which is only possible within
202 * this compilation unit
3c726f8d 203 */
0ebc4cda
BH
204#ifdef CONFIG_PPC_BOOK3S
205#include "exceptions-64s.S"
57f26649
NP
206#else
207OPEN_TEXT_SECTION(0x100)
1f6a93e4 208#endif
3c726f8d 209
57f26649
NP
210USE_TEXT_SECTION()
211
e16c8765 212#ifdef CONFIG_PPC_BOOK3E
6becef7e 213/*
214 * The booting_thread_hwid holds the thread id we want to boot in cpu
215 * hotplug case. It is set by cpu hotplug code, and is invalid by default.
216 * The thread id is the same as the initial value of SPRN_PIR[THREAD_ID]
217 * bit field.
218 */
219 .globl booting_thread_hwid
220booting_thread_hwid:
221 .long INVALID_THREAD_HWID
222 .align 3
223/*
224 * start a thread in the same core
225 * input parameters:
226 * r3 = the thread physical id
227 * r4 = the entry point where thread starts
228 */
229_GLOBAL(book3e_start_thread)
230 LOAD_REG_IMMEDIATE(r5, MSR_KERNEL)
f87f253b 231 cmpwi r3, 0
6becef7e 232 beq 10f
f87f253b 233 cmpwi r3, 1
6becef7e 234 beq 11f
235 /* If the thread id is invalid, just exit. */
236 b 13f
23710:
7a25d912
SW
238 MTTMR(TMRN_IMSR0, 5)
239 MTTMR(TMRN_INIA0, 4)
6becef7e 240 b 12f
24111:
7a25d912
SW
242 MTTMR(TMRN_IMSR1, 5)
243 MTTMR(TMRN_INIA1, 4)
6becef7e 24412:
245 isync
246 li r6, 1
247 sld r6, r6, r3
248 mtspr SPRN_TENS, r6
24913:
250 blr
251
d17799f9 252/*
253 * stop a thread in the same core
254 * input parameter:
255 * r3 = the thread physical id
256 */
257_GLOBAL(book3e_stop_thread)
f87f253b 258 cmpwi r3, 0
d17799f9 259 beq 10f
f87f253b 260 cmpwi r3, 1
d17799f9 261 beq 10f
262 /* If the thread id is invalid, just exit. */
263 b 13f
26410:
265 li r4, 1
266 sld r4, r4, r3
267 mtspr SPRN_TENC, r4
26813:
269 blr
270
e16c8765 271_GLOBAL(fsl_secondary_thread_init)
f34b3e19
SW
272 mfspr r4,SPRN_BUCSR
273
e16c8765
AF
274 /* Enable branch prediction */
275 lis r3,BUCSR_INIT@h
276 ori r3,r3,BUCSR_INIT@l
277 mtspr SPRN_BUCSR,r3
278 isync
279
280 /*
281 * Fix PIR to match the linear numbering in the device tree.
282 *
283 * On e6500, the reset value of PIR uses the low three bits for
284 * the thread within a core, and the upper bits for the core
285 * number. There are two threads per core, so shift everything
286 * but the low bit right by two bits so that the cpu numbering is
287 * continuous.
f34b3e19
SW
288 *
289 * If the old value of BUCSR is non-zero, this thread has run
290 * before. Thus, we assume we are coming from kexec or a similar
291 * scenario, and PIR is already set to the correct value. This
292 * is a bit of a hack, but there are limited opportunities for
293 * getting information into the thread and the alternatives
294 * seemed like they'd be overkill. We can't tell just by looking
295 * at the old PIR value which state it's in, since the same value
296 * could be valid for one thread out of reset and for a different
297 * thread in Linux.
e16c8765 298 */
f34b3e19 299
e16c8765 300 mfspr r3, SPRN_PIR
f34b3e19
SW
301 cmpwi r4,0
302 bne 1f
e16c8765
AF
303 rlwimi r3, r3, 30, 2, 30
304 mtspr SPRN_PIR, r3
f34b3e19 3051:
e16c8765
AF
306#endif
307
2d27cfd3
BH
308_GLOBAL(generic_secondary_thread_init)
309 mr r24,r3
310
311 /* turn on 64-bit mode */
b1576fec 312 bl enable_64b_mode
2d27cfd3
BH
313
314 /* get a valid TOC pointer, wherever we're mapped at */
b1576fec 315 bl relative_toc
1fbe9cf2 316 tovirt(r2,r2)
2d27cfd3
BH
317
318#ifdef CONFIG_PPC_BOOK3E
319 /* Book3E initialization */
320 mr r3,r24
b1576fec 321 bl book3e_secondary_thread_init
2d27cfd3
BH
322#endif
323 b generic_secondary_common_init
14cf11af
PM
324
325/*
f39b7a55
OJ
326 * On pSeries and most other platforms, secondary processors spin
327 * in the following code.
14cf11af 328 * At entry, r3 = this processor's number (physical cpu id)
2d27cfd3
BH
329 *
330 * On Book3E, r4 = 1 to indicate that the initial TLB entry for
331 * this core already exists (setup via some other mechanism such
332 * as SCOM before entry).
14cf11af 333 */
f39b7a55 334_GLOBAL(generic_secondary_smp_init)
5c0484e2 335 FIXUP_ENDIAN
14cf11af 336 mr r24,r3
2d27cfd3
BH
337 mr r25,r4
338
14cf11af 339 /* turn on 64-bit mode */
b1576fec 340 bl enable_64b_mode
14cf11af 341
2d27cfd3 342 /* get a valid TOC pointer, wherever we're mapped at */
b1576fec 343 bl relative_toc
1fbe9cf2 344 tovirt(r2,r2)
e31aa453 345
2d27cfd3
BH
346#ifdef CONFIG_PPC_BOOK3E
347 /* Book3E initialization */
348 mr r3,r24
349 mr r4,r25
b1576fec 350 bl book3e_secondary_core_init
6becef7e 351
352/*
353 * After common core init has finished, check if the current thread is the
354 * one we wanted to boot. If not, start the specified thread and stop the
355 * current thread.
356 */
357 LOAD_REG_ADDR(r4, booting_thread_hwid)
358 lwz r3, 0(r4)
359 li r5, INVALID_THREAD_HWID
360 cmpw r3, r5
361 beq 20f
362
363 /*
364 * The value of booting_thread_hwid has been stored in r3,
365 * so make it invalid.
366 */
367 stw r5, 0(r4)
368
369 /*
370 * Get the current thread id and check if it is the one we wanted.
371 * If not, start the one specified in booting_thread_hwid and stop
372 * the current thread.
373 */
374 mfspr r8, SPRN_TIR
375 cmpw r3, r8
376 beq 20f
377
378 /* start the specified thread */
379 LOAD_REG_ADDR(r5, fsl_secondary_thread_init)
380 ld r4, 0(r5)
381 bl book3e_start_thread
382
383 /* stop the current thread */
384 mr r3, r8
385 bl book3e_stop_thread
38610:
387 b 10b
38820:
2d27cfd3
BH
389#endif
390
391generic_secondary_common_init:
14cf11af
PM
392 /* Set up a paca value for this processor. Since we have the
393 * physical cpu id in r24, we need to search the pacas to find
394 * which logical id maps to our physical one.
395 */
768d18ad 396#ifndef CONFIG_SMP
b1576fec 397 b kexec_wait /* wait for next kernel if !SMP */
768d18ad 398#else
d2e60075
NP
399 LOAD_REG_ADDR(r8, paca_ptrs) /* Load paca_ptrs pointe */
400 ld r8,0(r8) /* Get base vaddr of array */
768d18ad
MM
401 LOAD_REG_ADDR(r7, nr_cpu_ids) /* Load nr_cpu_ids address */
402 lwz r7,0(r7) /* also the max paca allocated */
14cf11af 403 li r5,0 /* logical cpu id */
d2e60075
NP
4041:
405 sldi r9,r5,3 /* get paca_ptrs[] index from cpu id */
406 ldx r13,r9,r8 /* r13 = paca_ptrs[cpu id] */
407 lhz r6,PACAHWCPUID(r13) /* Load HW procid from paca */
14cf11af
PM
408 cmpw r6,r24 /* Compare to our id */
409 beq 2f
14cf11af 410 addi r5,r5,1
768d18ad 411 cmpw r5,r7 /* Check if more pacas exist */
14cf11af
PM
412 blt 1b
413
414 mr r3,r24 /* not found, copy phys to r3 */
b1576fec 415 b kexec_wait /* next kernel might do better */
14cf11af 416
2dd60d79 4172: SET_PACA(r13)
2d27cfd3
BH
418#ifdef CONFIG_PPC_BOOK3E
419 addi r12,r13,PACA_EXTLB /* and TLB exc frame in another */
420 mtspr SPRN_SPRG_TLB_EXFRAME,r12
421#endif
422
14cf11af
PM
423 /* From now on, r24 is expected to be logical cpuid */
424 mr r24,r5
b6f6b98a 425
f39b7a55 426 /* See if we need to call a cpu state restore handler */
e31aa453 427 LOAD_REG_ADDR(r23, cur_cpu_spec)
f39b7a55 428 ld r23,0(r23)
2751b628
AB
429 ld r12,CPU_SPEC_RESTORE(r23)
430 cmpdi 0,r12,0
9d07bc84 431 beq 3f
f55d9665 432#ifdef PPC64_ELF_ABI_v1
2751b628
AB
433 ld r12,0(r12)
434#endif
cc7efbf9 435 mtctr r12
f39b7a55
OJ
436 bctrl
437
7ac87abb 4383: LOAD_REG_ADDR(r3, spinning_secondaries) /* Decrement spinning_secondaries */
9d07bc84
BH
439 lwarx r4,0,r3
440 subi r4,r4,1
441 stwcx. r4,0,r3
442 bne 3b
443 isync
444
4454: HMT_LOW
ad0693ee
BH
446 lbz r23,PACAPROCSTART(r13) /* Test if this processor should */
447 /* start. */
ad0693ee 448 cmpwi 0,r23,0
9d07bc84 449 beq 4b /* Loop until told to go */
ad0693ee
BH
450
451 sync /* order paca.run and cur_cpu_spec */
9d07bc84 452 isync /* In case code patching happened */
ad0693ee 453
9d07bc84 454 /* Create a temp kernel stack for use before relocation is on. */
14cf11af
PM
455 ld r1,PACAEMERGSP(r13)
456 subi r1,r1,STACK_FRAME_OVERHEAD
457
c705677e 458 b __secondary_start
768d18ad 459#endif /* SMP */
14cf11af 460
e31aa453
PM
461/*
462 * Turn the MMU off.
463 * Assumes we're mapped EA == RA if the MMU is on.
464 */
2d27cfd3 465#ifdef CONFIG_PPC_BOOK3S
6a3bab90 466__mmu_off:
14cf11af
PM
467 mfmsr r3
468 andi. r0,r3,MSR_IR|MSR_DR
469 beqlr
e31aa453 470 mflr r4
14cf11af
PM
471 andc r3,r3,r0
472 mtspr SPRN_SRR0,r4
473 mtspr SPRN_SRR1,r3
474 sync
475 rfid
476 b . /* prevent speculative execution */
2d27cfd3 477#endif
14cf11af
PM
478
479
480/*
481 * Here is our main kernel entry point. We support currently 2 kind of entries
482 * depending on the value of r5.
483 *
484 * r5 != NULL -> OF entry, we go to prom_init, "legacy" parameter content
485 * in r3...r7
486 *
487 * r5 == NULL -> kexec style entry. r3 is a physical pointer to the
488 * DT block, r4 is a physical pointer to the kernel itself
489 *
490 */
6a3bab90 491__start_initialization_multiplatform:
e31aa453 492 /* Make sure we are running in 64 bits mode */
b1576fec 493 bl enable_64b_mode
e31aa453
PM
494
495 /* Get TOC pointer (current runtime address) */
b1576fec 496 bl relative_toc
e31aa453
PM
497
498 /* find out where we are now */
499 bcl 20,31,$+4
5000: mflr r26 /* r26 = runtime addr here */
501 addis r26,r26,(_stext - 0b)@ha
502 addi r26,r26,(_stext - 0b)@l /* current runtime base addr */
503
14cf11af
PM
504 /*
505 * Are we booted from a PROM Of-type client-interface ?
506 */
507 cmpldi cr0,r5,0
939e60f6 508 beq 1f
b1576fec 509 b __boot_from_prom /* yes -> prom */
939e60f6 5101:
14cf11af
PM
511 /* Save parameters */
512 mr r31,r3
513 mr r30,r4
daea1175
BH
514#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
515 /* Save OPAL entry */
516 mr r28,r8
517 mr r29,r9
518#endif
14cf11af 519
2d27cfd3 520#ifdef CONFIG_PPC_BOOK3E
b1576fec
AB
521 bl start_initialization_book3e
522 b __after_prom_start
2d27cfd3 523#else
14cf11af 524 /* Setup some critical 970 SPRs before switching MMU off */
f39b7a55
OJ
525 mfspr r0,SPRN_PVR
526 srwi r0,r0,16
527 cmpwi r0,0x39 /* 970 */
528 beq 1f
529 cmpwi r0,0x3c /* 970FX */
530 beq 1f
531 cmpwi r0,0x44 /* 970MP */
190a24f5
OJ
532 beq 1f
533 cmpwi r0,0x45 /* 970GX */
f39b7a55 534 bne 2f
b1576fec 5351: bl __cpu_preinit_ppc970
f39b7a55 5362:
14cf11af 537
e31aa453 538 /* Switch off MMU if not already off */
b1576fec
AB
539 bl __mmu_off
540 b __after_prom_start
2d27cfd3 541#endif /* CONFIG_PPC_BOOK3E */
14cf11af 542
6a3bab90 543__boot_from_prom:
28794d34 544#ifdef CONFIG_PPC_OF_BOOT_TRAMPOLINE
14cf11af
PM
545 /* Save parameters */
546 mr r31,r3
547 mr r30,r4
548 mr r29,r5
549 mr r28,r6
550 mr r27,r7
551
6088857b
OH
552 /*
553 * Align the stack to 16-byte boundary
554 * Depending on the size and layout of the ELF sections in the initial
e31aa453 555 * boot binary, the stack pointer may be unaligned on PowerMac
6088857b 556 */
c05b4770
LT
557 rldicr r1,r1,0,59
558
549e8152
PM
559#ifdef CONFIG_RELOCATABLE
560 /* Relocate code for where we are now */
561 mr r3,r26
b1576fec 562 bl relocate
549e8152
PM
563#endif
564
14cf11af
PM
565 /* Restore parameters */
566 mr r3,r31
567 mr r4,r30
568 mr r5,r29
569 mr r6,r28
570 mr r7,r27
571
572 /* Do all of the interaction with OF client interface */
549e8152 573 mr r8,r26
b1576fec 574 bl prom_init
28794d34
BH
575#endif /* #CONFIG_PPC_OF_BOOT_TRAMPOLINE */
576
577 /* We never return. We also hit that trap if trying to boot
578 * from OF while CONFIG_PPC_OF_BOOT_TRAMPOLINE isn't selected */
14cf11af
PM
579 trap
580
6a3bab90 581__after_prom_start:
549e8152
PM
582#ifdef CONFIG_RELOCATABLE
583 /* process relocations for the final address of the kernel */
584 lis r25,PAGE_OFFSET@highest /* compute virtual base of kernel */
585 sldi r25,r25,32
1cb6e064
TC
586#if defined(CONFIG_PPC_BOOK3E)
587 tovirt(r26,r26) /* on booke, we already run at PAGE_OFFSET */
588#endif
57f26649 589 lwz r7,(FIXED_SYMBOL_ABS_ADDR(__run_at_load))(r26)
1cb6e064
TC
590#if defined(CONFIG_PPC_BOOK3E)
591 tophys(r26,r26)
592#endif
928a3197 593 cmplwi cr0,r7,1 /* flagged to stay where we are ? */
54622f10
MK
594 bne 1f
595 add r25,r25,r26
54622f10 5961: mr r3,r25
b1576fec 597 bl relocate
1cb6e064
TC
598#if defined(CONFIG_PPC_BOOK3E)
599 /* IVPR needs to be set after relocation. */
600 bl init_core_book3e
601#endif
549e8152 602#endif
14cf11af
PM
603
604/*
e31aa453 605 * We need to run with _stext at physical address PHYSICAL_START.
14cf11af
PM
606 * This will leave some code in the first 256B of
607 * real memory, which are reserved for software use.
14cf11af
PM
608 *
609 * Note: This process overwrites the OF exception vectors.
14cf11af 610 */
549e8152 611 li r3,0 /* target addr */
2d27cfd3 612#ifdef CONFIG_PPC_BOOK3E
835c031c 613 tovirt(r3,r3) /* on booke, we already run at PAGE_OFFSET */
2d27cfd3 614#endif
549e8152 615 mr. r4,r26 /* In some cases the loader may */
835c031c
TC
616#if defined(CONFIG_PPC_BOOK3E)
617 tovirt(r4,r4)
618#endif
e31aa453 619 beq 9f /* have already put us at zero */
14cf11af
PM
620 li r6,0x100 /* Start offset, the first 0x100 */
621 /* bytes were copied earlier. */
622
11ee7e99 623#ifdef CONFIG_RELOCATABLE
54622f10
MK
624/*
625 * Check if the kernel has to be running as relocatable kernel based on the
8b8b0cc1 626 * variable __run_at_load, if it is set the kernel is treated as relocatable
54622f10
MK
627 * kernel, otherwise it will be moved to PHYSICAL_START
628 */
1cb6e064
TC
629#if defined(CONFIG_PPC_BOOK3E)
630 tovirt(r26,r26) /* on booke, we already run at PAGE_OFFSET */
631#endif
57f26649 632 lwz r7,(FIXED_SYMBOL_ABS_ADDR(__run_at_load))(r26)
8b8b0cc1 633 cmplwi cr0,r7,1
54622f10
MK
634 bne 3f
635
1cb6e064
TC
636#ifdef CONFIG_PPC_BOOK3E
637 LOAD_REG_ADDR(r5, __end_interrupts)
638 LOAD_REG_ADDR(r11, _stext)
639 sub r5,r5,r11
640#else
c1fb6816 641 /* just copy interrupts */
57f26649 642 LOAD_REG_IMMEDIATE(r5, FIXED_SYMBOL_ABS_ADDR(__end_interrupts))
1cb6e064 643#endif
54622f10
MK
644 b 5f
6453:
646#endif
57f26649
NP
647 /* # bytes of memory to copy */
648 lis r5,(ABS_ADDR(copy_to_here))@ha
649 addi r5,r5,(ABS_ADDR(copy_to_here))@l
54622f10 650
b1576fec 651 bl copy_and_flush /* copy the first n bytes */
14cf11af
PM
652 /* this includes the code being */
653 /* executed here. */
57f26649
NP
654 /* Jump to the copy of this code that we just made */
655 addis r8,r3,(ABS_ADDR(4f))@ha
656 addi r12,r8,(ABS_ADDR(4f))@l
cc7efbf9 657 mtctr r12
14cf11af
PM
658 bctr
659
286e4f90 660.balign 8
eb039161 661p_end: .8byte _end - copy_to_here
54622f10 662
573819e3
NP
6634:
664 /*
665 * Now copy the rest of the kernel up to _end, add
666 * _end - copy_to_here to the copy limit and run again.
667 */
57f26649
NP
668 addis r8,r26,(ABS_ADDR(p_end))@ha
669 ld r8,(ABS_ADDR(p_end))@l(r8)
573819e3 670 add r5,r5,r8
b1576fec 6715: bl copy_and_flush /* copy the rest */
e31aa453 672
b1576fec 6739: b start_here_multiplatform
e31aa453 674
14cf11af
PM
675/*
676 * Copy routine used to copy the kernel to start at physical address 0
677 * and flush and invalidate the caches as needed.
678 * r3 = dest addr, r4 = source addr, r5 = copy limit, r6 = start offset
679 * on exit, r3, r4, r5 are unchanged, r6 is updated to be >= r5.
680 *
681 * Note: this routine *only* clobbers r0, r6 and lr
682 */
683_GLOBAL(copy_and_flush)
684 addi r5,r5,-8
685 addi r6,r6,-8
5a2fe38d 6864: li r0,8 /* Use the smallest common */
14cf11af
PM
687 /* denominator cache line */
688 /* size. This results in */
689 /* extra cache line flushes */
690 /* but operation is correct. */
691 /* Can't get cache line size */
692 /* from NACA as it is being */
693 /* moved too. */
694
695 mtctr r0 /* put # words/line in ctr */
6963: addi r6,r6,8 /* copy a cache line */
697 ldx r0,r6,r4
698 stdx r0,r6,r3
699 bdnz 3b
700 dcbst r6,r3 /* write it to memory */
701 sync
702 icbi r6,r3 /* flush the icache line */
703 cmpld 0,r6,r5
704 blt 4b
705 sync
706 addi r5,r5,8
707 addi r6,r6,8
29ce3c50 708 isync
14cf11af
PM
709 blr
710
711.align 8
712copy_to_here:
713
714#ifdef CONFIG_SMP
715#ifdef CONFIG_PPC_PMAC
716/*
717 * On PowerMac, secondary processors starts from the reset vector, which
718 * is temporarily turned into a call to one of the functions below.
719 */
720 .section ".text";
721 .align 2 ;
722
35499c01
PM
723 .globl __secondary_start_pmac_0
724__secondary_start_pmac_0:
725 /* NB the entries for cpus 0, 1, 2 must each occupy 8 bytes. */
726 li r24,0
727 b 1f
728 li r24,1
729 b 1f
730 li r24,2
731 b 1f
732 li r24,3
7331:
14cf11af
PM
734
735_GLOBAL(pmac_secondary_start)
736 /* turn on 64-bit mode */
b1576fec 737 bl enable_64b_mode
14cf11af 738
c478b581
BH
739 li r0,0
740 mfspr r3,SPRN_HID4
741 rldimi r3,r0,40,23 /* clear bit 23 (rm_ci) */
742 sync
743 mtspr SPRN_HID4,r3
744 isync
745 sync
746 slbia
747
e31aa453 748 /* get TOC pointer (real address) */
b1576fec 749 bl relative_toc
1fbe9cf2 750 tovirt(r2,r2)
e31aa453 751
14cf11af 752 /* Copy some CPU settings from CPU 0 */
b1576fec 753 bl __restore_cpu_ppc970
14cf11af
PM
754
755 /* pSeries do that early though I don't think we really need it */
756 mfmsr r3
757 ori r3,r3,MSR_RI
758 mtmsrd r3 /* RI on */
759
760 /* Set up a paca value for this processor. */
d2e60075
NP
761 LOAD_REG_ADDR(r4,paca_ptrs) /* Load paca pointer */
762 ld r4,0(r4) /* Get base vaddr of paca_ptrs array */
763 sldi r5,r24,3 /* get paca_ptrs[] index from cpu id */
764 ldx r13,r5,r4 /* r13 = paca_ptrs[cpu id] */
2dd60d79 765 SET_PACA(r13) /* Save vaddr of paca in an SPRG*/
14cf11af 766
62cc67b9
BH
767 /* Mark interrupts soft and hard disabled (they might be enabled
768 * in the PACA when doing hotplug)
769 */
c2e480ba 770 li r0,IRQS_DISABLED
4e26bc4a 771 stb r0,PACAIRQSOFTMASK(r13)
7230c564
BH
772 li r0,PACA_IRQ_HARD_DIS
773 stb r0,PACAIRQHAPPENED(r13)
62cc67b9 774
14cf11af
PM
775 /* Create a temp kernel stack for use before relocation is on. */
776 ld r1,PACAEMERGSP(r13)
777 subi r1,r1,STACK_FRAME_OVERHEAD
778
c705677e 779 b __secondary_start
14cf11af
PM
780
781#endif /* CONFIG_PPC_PMAC */
782
783/*
784 * This function is called after the master CPU has released the
785 * secondary processors. The execution environment is relocation off.
786 * The paca for this processor has the following fields initialized at
787 * this point:
788 * 1. Processor number
789 * 2. Segment table pointer (virtual address)
790 * On entry the following are set:
4f8cf36f 791 * r1 = stack pointer (real addr of temp stack)
ee43eb78
BH
792 * r24 = cpu# (in Linux terms)
793 * r13 = paca virtual address
794 * SPRG_PACA = paca virtual address
14cf11af 795 */
2d27cfd3
BH
796 .section ".text";
797 .align 2 ;
798
fc68e869 799 .globl __secondary_start
c705677e 800__secondary_start:
799d6046
PM
801 /* Set thread priority to MEDIUM */
802 HMT_MEDIUM
14cf11af 803
4f8cf36f 804 /* Initialize the kernel stack */
e58c3495 805 LOAD_REG_ADDR(r3, current_set)
14cf11af 806 sldi r28,r24,3 /* get current_set[cpu#] */
54a83404
MN
807 ldx r14,r3,r28
808 addi r14,r14,THREAD_SIZE-STACK_FRAME_OVERHEAD
809 std r14,PACAKSAVE(r13)
14cf11af 810
376af594 811 /* Do early setup for that CPU (SLB and hash table pointer) */
b1576fec 812 bl early_setup_secondary
f761622e 813
54a83404
MN
814 /*
815 * setup the new stack pointer, but *don't* use this until
816 * translation is on.
817 */
818 mr r1, r14
819
799d6046 820 /* Clear backchain so we get nice backtraces */
14cf11af
PM
821 li r7,0
822 mtlr r7
823
7230c564
BH
824 /* Mark interrupts soft and hard disabled (they might be enabled
825 * in the PACA when doing hotplug)
826 */
c2e480ba 827 li r7,IRQS_DISABLED
4e26bc4a 828 stb r7,PACAIRQSOFTMASK(r13)
7230c564
BH
829 li r0,PACA_IRQ_HARD_DIS
830 stb r0,PACAIRQHAPPENED(r13)
4f8cf36f 831
14cf11af 832 /* enable MMU and jump to start_secondary */
ad0289e4 833 LOAD_REG_ADDR(r3, start_secondary_prolog)
e58c3495 834 LOAD_REG_IMMEDIATE(r4, MSR_KERNEL)
d04c56f7 835
b5bbeb23
PM
836 mtspr SPRN_SRR0,r3
837 mtspr SPRN_SRR1,r4
2d27cfd3 838 RFI
14cf11af
PM
839 b . /* prevent speculative execution */
840
841/*
842 * Running with relocation on at this point. All we want to do is
e31aa453
PM
843 * zero the stack back-chain pointer and get the TOC virtual address
844 * before going into C code.
14cf11af 845 */
ad0289e4 846start_secondary_prolog:
e31aa453 847 ld r2,PACATOC(r13)
14cf11af
PM
848 li r3,0
849 std r3,0(r1) /* Zero the stack frame pointer */
b1576fec 850 bl start_secondary
799d6046 851 b .
8dbce53c
VS
852/*
853 * Reset stack pointer and call start_secondary
854 * to continue with online operation when woken up
855 * from cede in cpu offline.
856 */
857_GLOBAL(start_secondary_resume)
858 ld r1,PACAKSAVE(r13) /* Reload kernel stack pointer */
859 li r3,0
860 std r3,0(r1) /* Zero the stack frame pointer */
b1576fec 861 bl start_secondary
8dbce53c 862 b .
14cf11af
PM
863#endif
864
865/*
866 * This subroutine clobbers r11 and r12
867 */
6a3bab90 868enable_64b_mode:
14cf11af 869 mfmsr r11 /* grab the current MSR */
2d27cfd3
BH
870#ifdef CONFIG_PPC_BOOK3E
871 oris r11,r11,0x8000 /* CM bit set, we'll set ICM later */
872 mtmsr r11
873#else /* CONFIG_PPC_BOOK3E */
9f0b0793 874 li r12,(MSR_64BIT | MSR_ISF)@highest
e31aa453 875 sldi r12,r12,48
14cf11af
PM
876 or r11,r11,r12
877 mtmsrd r11
878 isync
2d27cfd3 879#endif
14cf11af
PM
880 blr
881
e31aa453
PM
882/*
883 * This puts the TOC pointer into r2, offset by 0x8000 (as expected
884 * by the toolchain). It computes the correct value for wherever we
885 * are running at the moment, using position-independent code.
1fbe9cf2
AB
886 *
887 * Note: The compiler constructs pointers using offsets from the
888 * TOC in -mcmodel=medium mode. After we relocate to 0 but before
889 * the MMU is on we need our TOC to be a virtual address otherwise
890 * these pointers will be real addresses which may get stored and
891 * accessed later with the MMU on. We use tovirt() at the call
892 * sites to handle this.
e31aa453
PM
893 */
894_GLOBAL(relative_toc)
895 mflr r0
896 bcl 20,31,$+4
e550592e
BH
8970: mflr r11
898 ld r2,(p_toc - 0b)(r11)
899 add r2,r2,r11
e31aa453
PM
900 mtlr r0
901 blr
902
5b63fee1 903.balign 8
eb039161 904p_toc: .8byte __toc_start + 0x8000 - 0b
e31aa453 905
14cf11af
PM
906/*
907 * This is where the main kernel code starts.
908 */
6a3bab90 909start_here_multiplatform:
1fbe9cf2 910 /* set up the TOC */
b1576fec 911 bl relative_toc
1fbe9cf2 912 tovirt(r2,r2)
14cf11af
PM
913
914 /* Clear out the BSS. It may have been done in prom_init,
915 * already but that's irrelevant since prom_init will soon
916 * be detached from the kernel completely. Besides, we need
917 * to clear it now for kexec-style entry.
918 */
e31aa453
PM
919 LOAD_REG_ADDR(r11,__bss_stop)
920 LOAD_REG_ADDR(r8,__bss_start)
14cf11af
PM
921 sub r11,r11,r8 /* bss size */
922 addi r11,r11,7 /* round up to an even double word */
e31aa453 923 srdi. r11,r11,3 /* shift right by 3 */
14cf11af
PM
924 beq 4f
925 addi r8,r8,-8
926 li r0,0
927 mtctr r11 /* zero this many doublewords */
9283: stdu r0,8(r8)
929 bdnz 3b
9304:
931
daea1175
BH
932#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
933 /* Setup OPAL entry */
ab7f961a 934 LOAD_REG_ADDR(r11, opal)
daea1175
BH
935 std r28,0(r11);
936 std r29,8(r11);
937#endif
938
2d27cfd3 939#ifndef CONFIG_PPC_BOOK3E
14cf11af
PM
940 mfmsr r6
941 ori r6,r6,MSR_RI
942 mtmsrd r6 /* RI on */
2d27cfd3 943#endif
14cf11af 944
549e8152
PM
945#ifdef CONFIG_RELOCATABLE
946 /* Save the physical address we're running at in kernstart_addr */
947 LOAD_REG_ADDR(r4, kernstart_addr)
948 clrldi r0,r25,2
949 std r0,0(r4)
950#endif
951
e31aa453 952 /* The following gets the stack set up with the regs */
14cf11af
PM
953 /* pointing to the real addr of the kernel stack. This is */
954 /* all done to support the C function call below which sets */
955 /* up the htab. This is done because we have relocated the */
956 /* kernel but are still running in real mode. */
957
e31aa453 958 LOAD_REG_ADDR(r3,init_thread_union)
14cf11af 959
e31aa453 960 /* set up a stack pointer */
cabed148
HM
961 LOAD_REG_IMMEDIATE(r1,THREAD_SIZE)
962 add r1,r3,r1
14cf11af
PM
963 li r0,0
964 stdu r0,-STACK_FRAME_OVERHEAD(r1)
965
376af594
ME
966 /*
967 * Do very early kernel initializations, including initial hash table
968 * and SLB setup before we turn on relocation.
969 */
14cf11af
PM
970
971 /* Restore parameters passed from prom_init/kexec */
972 mr r3,r31
b1576fec 973 bl early_setup /* also sets r13 and SPRG_PACA */
14cf11af 974
ad0289e4 975 LOAD_REG_ADDR(r3, start_here_common)
e31aa453 976 ld r4,PACAKMSR(r13)
b5bbeb23
PM
977 mtspr SPRN_SRR0,r3
978 mtspr SPRN_SRR1,r4
2d27cfd3 979 RFI
14cf11af 980 b . /* prevent speculative execution */
fa745a12 981
14cf11af 982 /* This is where all platforms converge execution */
ad0289e4
AB
983
984start_here_common:
14cf11af 985 /* relocation is on at this point */
e31aa453 986 std r1,PACAKSAVE(r13)
14cf11af 987
e31aa453 988 /* Load the TOC (virtual address) */
14cf11af 989 ld r2,PACATOC(r13)
14cf11af 990
7230c564
BH
991 /* Mark interrupts soft and hard disabled (they might be enabled
992 * in the PACA when doing hotplug)
993 */
c2e480ba 994 li r0,IRQS_DISABLED
4e26bc4a 995 stb r0,PACAIRQSOFTMASK(r13)
7230c564
BH
996 li r0,PACA_IRQ_HARD_DIS
997 stb r0,PACAIRQHAPPENED(r13)
14cf11af 998
7230c564 999 /* Generic kernel entry */
b1576fec 1000 bl start_kernel
14cf11af 1001
f1870f77
AB
1002 /* Not reached */
1003 BUG_OPCODE
14cf11af 1004
14cf11af
PM
1005/*
1006 * We put a few things here that have to be page-aligned.
1007 * This stuff goes at the beginning of the bss, which is page-aligned.
1008 */
1009 .section ".bss"
43a5c684
AK
1010/*
1011 * pgd dir should be aligned to PGD_TABLE_SIZE which is 64K.
1012 * We will need to find a better way to fix this
1013 */
1014 .align 16
14cf11af 1015
43a5c684
AK
1016 .globl swapper_pg_dir
1017swapper_pg_dir:
1018 .space PGD_TABLE_SIZE
14cf11af
PM
1019
1020 .globl empty_zero_page
1021empty_zero_page:
1022 .space PAGE_SIZE
9445aa1a 1023EXPORT_SYMBOL(empty_zero_page)