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