head_64.S 21.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  PowerPC version
 *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
 *
 *  Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
 *    Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
 *  Adapted for Power Macintosh by Paul Mackerras.
 *  Low-level exception handlers and MMU support
 *  rewritten by Paul Mackerras.
 *    Copyright (C) 1996 Paul Mackerras.
 *
 *  Adapted for 64bit PowerPC by Dave Engebretsen, Peter Bergner, and
 *    Mike Corrigan {engebret|bergner|mikejc}@us.ibm.com
 *
15 16 17
 *  This file contains the entry point for the 64-bit kernel along
 *  with some early initialization code common to all 64-bit powerpc
 *  variants.
18 19 20 21 22 23 24 25
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version
 *  2 of the License, or (at your option) any later version.
 */

#include <linux/threads.h>
26
#include <linux/init.h>
27
#include <asm/reg.h>
28 29 30 31 32 33 34 35
#include <asm/page.h>
#include <asm/mmu.h>
#include <asm/ppc_asm.h>
#include <asm/asm-offsets.h>
#include <asm/bug.h>
#include <asm/cputable.h>
#include <asm/setup.h>
#include <asm/hvcall.h>
36
#include <asm/thread_info.h>
37
#include <asm/firmware.h>
38
#include <asm/page_64.h>
39
#include <asm/irqflags.h>
40
#include <asm/kvm_book3s_asm.h>
41
#include <asm/ptrace.h>
42
#include <asm/hw_irq.h>
43

L
Lucas De Marchi 已提交
44
/* The physical memory is laid out such that the secondary processor
45 46
 * spin code sits at 0x0000...0x00ff. On server, the vectors follow
 * using the layout described in exceptions-64s.S
47 48 49 50
 */

/*
 * Entering into this code we make the following assumptions:
51 52
 *
 *  For pSeries or server processors:
53 54
 *   1. The MMU is off & open firmware is running in real mode.
 *   2. The kernel is entered at __start
55 56
 * -or- For OPAL entry:
 *   1. The MMU is off, processor in HV mode, primary CPU enters at 0
57 58
 *      with device-tree in gpr3. We also get OPAL base in r8 and
 *	entry in r9 for debugging purposes
59
 *   2. Secondary processors enter at 0x60 with PIR in gpr3
60
 *
61 62 63
 *  For Book3E processors:
 *   1. The MMU is on running in AS0 in a state defined in ePAPR
 *   2. The kernel is entered at __start
64 65 66 67 68 69 70 71
 */

	.text
	.globl  _stext
_stext:
_GLOBAL(__start)
	/* NOP this out unconditionally */
BEGIN_FTR_SECTION
72
	FIXUP_ENDIAN
73
	b	__start_initialization_multiplatform
74 75 76 77 78
END_FTR_SECTION(0, 1)

	/* Catch branch to 0 in real mode */
	trap

79 80 81
	/* Secondary processors spin on this value until it becomes non-zero.
	 * When non-zero, it contains the real address of the function the cpu
	 * should jump to.
82
	 */
83
	.balign 8
84 85 86 87 88 89 90 91 92 93
	.globl  __secondary_hold_spinloop
__secondary_hold_spinloop:
	.llong	0x0

	/* Secondary processors write this value with their cpu # */
	/* after they enter the spin loop immediately below.	  */
	.globl	__secondary_hold_acknowledge
__secondary_hold_acknowledge:
	.llong	0x0

94
#ifdef CONFIG_RELOCATABLE
95 96 97 98 99 100 101 102 103 104 105 106 107
	/* This flag is set to 1 by a loader if the kernel should run
	 * at the loaded address instead of the linked address.  This
	 * is used by kexec-tools to keep the the kdump kernel in the
	 * crash_kernel region.  The loader is responsible for
	 * observing the alignment requirement.
	 */
	/* Do not move this variable as kexec-tools knows about it. */
	. = 0x5c
	.globl	__run_at_load
__run_at_load:
	.long	0x72756e30	/* "run0" -- relocate to 0 by default */
#endif

108 109
	. = 0x60
/*
110 111
 * The following code is used to hold secondary processors
 * in a spin loop after they have entered the kernel, but
112 113 114
 * before the bulk of the kernel has been relocated.  This code
 * is relocated to physical address 0x60 before prom_init is run.
 * All of it must fit below the first exception vector at 0x100.
115 116
 * Use .globl here not _GLOBAL because we want __secondary_hold
 * to be the actual text address, not a descriptor.
117
 */
118 119
	.globl	__secondary_hold
__secondary_hold:
120
	FIXUP_ENDIAN
121
#ifndef CONFIG_PPC_BOOK3E
122 123 124
	mfmsr	r24
	ori	r24,r24,MSR_RI
	mtmsrd	r24			/* RI on */
125
#endif
126
	/* Grab our physical cpu number */
127
	mr	r24,r3
128 129
	/* stash r4 for book3e */
	mr	r25,r4
130 131 132 133

	/* Tell the master cpu we're here */
	/* Relocation is off & we are located at an address less */
	/* than 0x100, so only need to grab low order offset.    */
134
	std	r24,__secondary_hold_acknowledge-_stext(0)
135 136
	sync

137 138 139 140
	li	r26,0
#ifdef CONFIG_PPC_BOOK3E
	tovirt(r26,r26)
#endif
141
	/* All secondary cpus wait here until told to start. */
142 143
100:	ld	r12,__secondary_hold_spinloop-_stext(r26)
	cmpdi	0,r12,0
144
	beq	100b
145

146
#if defined(CONFIG_SMP) || defined(CONFIG_KEXEC)
147
#ifdef CONFIG_PPC_BOOK3E
148 149 150
	tovirt(r12,r12)
#endif
	mtctr	r12
151
	mr	r3,r24
152 153 154 155 156 157 158
	/*
	 * it may be the case that other platforms have r4 right to
	 * begin with, this gives us some safety in case it is not
	 */
#ifdef CONFIG_PPC_BOOK3E
	mr	r4,r25
#else
159
	li	r4,0
160
#endif
161 162
	/* Make sure that patched code is visible */
	isync
163
	bctr
164 165 166 167 168 169 170 171 172 173 174
#else
	BUG_OPCODE
#endif

/* This value is used to mark exception frames on the stack. */
	.section ".toc","aw"
exception_marker:
	.tc	ID_72656773_68657265[TC],0x7265677368657265
	.text

/*
175 176 177
 * On server, we include the exception vectors code here as it
 * relies on absolute addressing which is only possible within
 * this compilation unit
178
 */
179 180
#ifdef CONFIG_PPC_BOOK3S
#include "exceptions-64s.S"
181
#endif
182

183 184
#ifdef CONFIG_PPC_BOOK3E
_GLOBAL(fsl_secondary_thread_init)
185 186
	mfspr	r4,SPRN_BUCSR

187 188 189 190 191 192 193 194 195 196 197 198 199 200
	/* Enable branch prediction */
	lis     r3,BUCSR_INIT@h
	ori     r3,r3,BUCSR_INIT@l
	mtspr   SPRN_BUCSR,r3
	isync

	/*
	 * Fix PIR to match the linear numbering in the device tree.
	 *
	 * On e6500, the reset value of PIR uses the low three bits for
	 * the thread within a core, and the upper bits for the core
	 * number.  There are two threads per core, so shift everything
	 * but the low bit right by two bits so that the cpu numbering is
	 * continuous.
201 202 203 204 205 206 207 208 209 210
	 *
	 * If the old value of BUCSR is non-zero, this thread has run
	 * before.  Thus, we assume we are coming from kexec or a similar
	 * scenario, and PIR is already set to the correct value.  This
	 * is a bit of a hack, but there are limited opportunities for
	 * getting information into the thread and the alternatives
	 * seemed like they'd be overkill.  We can't tell just by looking
	 * at the old PIR value which state it's in, since the same value
	 * could be valid for one thread out of reset and for a different
	 * thread in Linux.
211
	 */
212

213
	mfspr	r3, SPRN_PIR
214 215
	cmpwi	r4,0
	bne	1f
216 217
	rlwimi	r3, r3, 30, 2, 30
	mtspr	SPRN_PIR, r3
218
1:
219 220
#endif

221 222 223 224
_GLOBAL(generic_secondary_thread_init)
	mr	r24,r3

	/* turn on 64-bit mode */
225
	bl	enable_64b_mode
226 227

	/* get a valid TOC pointer, wherever we're mapped at */
228
	bl	relative_toc
229
	tovirt(r2,r2)
230 231 232 233

#ifdef CONFIG_PPC_BOOK3E
	/* Book3E initialization */
	mr	r3,r24
234
	bl	book3e_secondary_thread_init
235 236
#endif
	b	generic_secondary_common_init
237 238

/*
O
Olof Johansson 已提交
239 240
 * On pSeries and most other platforms, secondary processors spin
 * in the following code.
241
 * At entry, r3 = this processor's number (physical cpu id)
242 243 244 245
 *
 * On Book3E, r4 = 1 to indicate that the initial TLB entry for
 * this core already exists (setup via some other mechanism such
 * as SCOM before entry).
246
 */
O
Olof Johansson 已提交
247
_GLOBAL(generic_secondary_smp_init)
248
	FIXUP_ENDIAN
249
	mr	r24,r3
250 251
	mr	r25,r4

252
	/* turn on 64-bit mode */
253
	bl	enable_64b_mode
254

255
	/* get a valid TOC pointer, wherever we're mapped at */
256
	bl	relative_toc
257
	tovirt(r2,r2)
258

259 260 261 262
#ifdef CONFIG_PPC_BOOK3E
	/* Book3E initialization */
	mr	r3,r24
	mr	r4,r25
263
	bl	book3e_secondary_core_init
264 265 266
#endif

generic_secondary_common_init:
267 268 269 270
	/* Set up a paca value for this processor. Since we have the
	 * physical cpu id in r24, we need to search the pacas to find
	 * which logical id maps to our physical one.
	 */
271 272
	LOAD_REG_ADDR(r13, paca)	/* Load paca pointer		 */
	ld	r13,0(r13)		/* Get base vaddr of paca array	 */
273 274
#ifndef CONFIG_SMP
	addi	r13,r13,PACA_SIZE	/* know r13 if used accidentally */
275
	b	kexec_wait		/* wait for next kernel if !SMP	 */
276 277 278
#else
	LOAD_REG_ADDR(r7, nr_cpu_ids)	/* Load nr_cpu_ids address       */
	lwz	r7,0(r7)		/* also the max paca allocated 	 */
279 280 281 282 283 284
	li	r5,0			/* logical cpu id                */
1:	lhz	r6,PACAHWCPUID(r13)	/* Load HW procid from paca      */
	cmpw	r6,r24			/* Compare to our id             */
	beq	2f
	addi	r13,r13,PACA_SIZE	/* Loop to next PACA on miss     */
	addi	r5,r5,1
285
	cmpw	r5,r7			/* Check if more pacas exist     */
286 287 288
	blt	1b

	mr	r3,r24			/* not found, copy phys to r3	 */
289
	b	kexec_wait		/* next kernel might do better	 */
290

291
2:	SET_PACA(r13)
292 293 294 295 296
#ifdef CONFIG_PPC_BOOK3E
	addi	r12,r13,PACA_EXTLB	/* and TLB exc frame in another  */
	mtspr	SPRN_SPRG_TLB_EXFRAME,r12
#endif

297 298
	/* From now on, r24 is expected to be logical cpuid */
	mr	r24,r5
299

O
Olof Johansson 已提交
300
	/* See if we need to call a cpu state restore handler */
301
	LOAD_REG_ADDR(r23, cur_cpu_spec)
O
Olof Johansson 已提交
302
	ld	r23,0(r23)
303 304
	ld	r12,CPU_SPEC_RESTORE(r23)
	cmpdi	0,r12,0
305
	beq	3f
306 307 308
#if !defined(_CALL_ELF) || _CALL_ELF != 2
	ld	r12,0(r12)
#endif
309
	mtctr	r12
O
Olof Johansson 已提交
310 311
	bctrl

312
3:	LOAD_REG_ADDR(r3, spinning_secondaries) /* Decrement spinning_secondaries */
313 314 315 316 317 318 319
	lwarx	r4,0,r3
	subi	r4,r4,1
	stwcx.	r4,0,r3
	bne	3b
	isync

4:	HMT_LOW
320 321 322
	lbz	r23,PACAPROCSTART(r13)	/* Test if this processor should */
					/* start.			 */
	cmpwi	0,r23,0
323
	beq	4b			/* Loop until told to go	 */
324 325

	sync				/* order paca.run and cur_cpu_spec */
326
	isync				/* In case code patching happened */
327

328
	/* Create a temp kernel stack for use before relocation is on.	*/
329 330 331
	ld	r1,PACAEMERGSP(r13)
	subi	r1,r1,STACK_FRAME_OVERHEAD

332
	b	__secondary_start
333
#endif /* SMP */
334

335 336 337 338
/*
 * Turn the MMU off.
 * Assumes we're mapped EA == RA if the MMU is on.
 */
339
#ifdef CONFIG_PPC_BOOK3S
340
__mmu_off:
341 342 343
	mfmsr	r3
	andi.	r0,r3,MSR_IR|MSR_DR
	beqlr
344
	mflr	r4
345 346 347 348 349 350
	andc	r3,r3,r0
	mtspr	SPRN_SRR0,r4
	mtspr	SPRN_SRR1,r3
	sync
	rfid
	b	.	/* prevent speculative execution */
351
#endif
352 353 354 355 356 357 358 359 360 361 362 363 364


/*
 * Here is our main kernel entry point. We support currently 2 kind of entries
 * depending on the value of r5.
 *
 *   r5 != NULL -> OF entry, we go to prom_init, "legacy" parameter content
 *                 in r3...r7
 *   
 *   r5 == NULL -> kexec style entry. r3 is a physical pointer to the
 *                 DT block, r4 is a physical pointer to the kernel itself
 *
 */
365
__start_initialization_multiplatform:
366
	/* Make sure we are running in 64 bits mode */
367
	bl	enable_64b_mode
368 369

	/* Get TOC pointer (current runtime address) */
370
	bl	relative_toc
371 372 373 374 375 376 377

	/* find out where we are now */
	bcl	20,31,$+4
0:	mflr	r26			/* r26 = runtime addr here */
	addis	r26,r26,(_stext - 0b)@ha
	addi	r26,r26,(_stext - 0b)@l	/* current runtime base addr */

378 379 380 381
	/*
	 * Are we booted from a PROM Of-type client-interface ?
	 */
	cmpldi	cr0,r5,0
382
	beq	1f
383
	b	__boot_from_prom		/* yes -> prom */
384
1:
385 386 387
	/* Save parameters */
	mr	r31,r3
	mr	r30,r4
388 389 390 391 392
#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
	/* Save OPAL entry */
	mr	r28,r8
	mr	r29,r9
#endif
393

394
#ifdef CONFIG_PPC_BOOK3E
395 396
	bl	start_initialization_book3e
	b	__after_prom_start
397
#else
398
	/* Setup some critical 970 SPRs before switching MMU off */
O
Olof Johansson 已提交
399 400 401 402 403 404 405
	mfspr	r0,SPRN_PVR
	srwi	r0,r0,16
	cmpwi	r0,0x39		/* 970 */
	beq	1f
	cmpwi	r0,0x3c		/* 970FX */
	beq	1f
	cmpwi	r0,0x44		/* 970MP */
406 407
	beq	1f
	cmpwi	r0,0x45		/* 970GX */
O
Olof Johansson 已提交
408
	bne	2f
409
1:	bl	__cpu_preinit_ppc970
O
Olof Johansson 已提交
410
2:
411

412
	/* Switch off MMU if not already off */
413 414
	bl	__mmu_off
	b	__after_prom_start
415
#endif /* CONFIG_PPC_BOOK3E */
416

417
__boot_from_prom:
418
#ifdef CONFIG_PPC_OF_BOOT_TRAMPOLINE
419 420 421 422 423 424 425
	/* Save parameters */
	mr	r31,r3
	mr	r30,r4
	mr	r29,r5
	mr	r28,r6
	mr	r27,r7

426 427 428
	/*
	 * Align the stack to 16-byte boundary
	 * Depending on the size and layout of the ELF sections in the initial
429
	 * boot binary, the stack pointer may be unaligned on PowerMac
430
	 */
431 432
	rldicr	r1,r1,0,59

433 434 435
#ifdef CONFIG_RELOCATABLE
	/* Relocate code for where we are now */
	mr	r3,r26
436
	bl	relocate
437 438
#endif

439 440 441 442 443 444 445 446
	/* Restore parameters */
	mr	r3,r31
	mr	r4,r30
	mr	r5,r29
	mr	r6,r28
	mr	r7,r27

	/* Do all of the interaction with OF client interface */
447
	mr	r8,r26
448
	bl	prom_init
449 450 451 452
#endif /* #CONFIG_PPC_OF_BOOT_TRAMPOLINE */

	/* We never return. We also hit that trap if trying to boot
	 * from OF while CONFIG_PPC_OF_BOOT_TRAMPOLINE isn't selected */
453 454
	trap

455
__after_prom_start:
456 457 458 459
#ifdef CONFIG_RELOCATABLE
	/* process relocations for the final address of the kernel */
	lis	r25,PAGE_OFFSET@highest	/* compute virtual base of kernel */
	sldi	r25,r25,32
460
	lwz	r7,__run_at_load-_stext(r26)
461
	cmplwi	cr0,r7,1	/* flagged to stay where we are ? */
462 463 464
	bne	1f
	add	r25,r25,r26
1:	mr	r3,r25
465
	bl	relocate
466
#endif
467 468

/*
469
 * We need to run with _stext at physical address PHYSICAL_START.
470 471 472 473 474
 * This will leave some code in the first 256B of
 * real memory, which are reserved for software use.
 *
 * Note: This process overwrites the OF exception vectors.
 */
475
	li	r3,0			/* target addr */
476
#ifdef CONFIG_PPC_BOOK3E
477
	tovirt(r3,r3)		/* on booke, we already run at PAGE_OFFSET */
478
#endif
479
	mr.	r4,r26			/* In some cases the loader may  */
480 481 482
#if defined(CONFIG_PPC_BOOK3E)
	tovirt(r4,r4)
#endif
483
	beq	9f			/* have already put us at zero */
484 485 486
	li	r6,0x100		/* Start offset, the first 0x100 */
					/* bytes were copied earlier.	 */

487
#ifdef CONFIG_RELOCATABLE
488 489
/*
 * Check if the kernel has to be running as relocatable kernel based on the
490
 * variable __run_at_load, if it is set the kernel is treated as relocatable
491 492
 * kernel, otherwise it will be moved to PHYSICAL_START
 */
493 494
	lwz	r7,__run_at_load-_stext(r26)
	cmplwi	cr0,r7,1
495 496
	bne	3f

497 498
	/* just copy interrupts */
	LOAD_REG_IMMEDIATE(r5, __end_interrupts - _stext)
499 500 501 502 503 504
	b	5f
3:
#endif
	lis	r5,(copy_to_here - _stext)@ha
	addi	r5,r5,(copy_to_here - _stext)@l /* # bytes of memory to copy */

505
	bl	copy_and_flush		/* copy the first n bytes	 */
506 507
					/* this includes the code being	 */
					/* executed here.		 */
508
	addis	r8,r3,(4f - _stext)@ha	/* Jump to the copy of this code */
509 510
	addi	r12,r8,(4f - _stext)@l	/* that we just made */
	mtctr	r12
511 512
	bctr

A
Anton Blanchard 已提交
513
.balign 8
514 515
p_end:	.llong	_end - _stext

516
4:	/* Now copy the rest of the kernel up to _end */
517 518 519
#if defined(CONFIG_PPC_BOOK3E)
	tovirt(r26,r26)
#endif
520 521
	addis	r5,r26,(p_end - _stext)@ha
	ld	r5,(p_end - _stext)@l(r5)	/* get _end */
522
5:	bl	copy_and_flush		/* copy the rest */
523

524
9:	b	start_here_multiplatform
525

526 527 528 529 530 531 532 533 534 535 536
/*
 * Copy routine used to copy the kernel to start at physical address 0
 * and flush and invalidate the caches as needed.
 * r3 = dest addr, r4 = source addr, r5 = copy limit, r6 = start offset
 * on exit, r3, r4, r5 are unchanged, r6 is updated to be >= r5.
 *
 * Note: this routine *only* clobbers r0, r6 and lr
 */
_GLOBAL(copy_and_flush)
	addi	r5,r5,-8
	addi	r6,r6,-8
537
4:	li	r0,8			/* Use the smallest common	*/
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
					/* denominator cache line	*/
					/* size.  This results in	*/
					/* extra cache line flushes	*/
					/* but operation is correct.	*/
					/* Can't get cache line size	*/
					/* from NACA as it is being	*/
					/* moved too.			*/

	mtctr	r0			/* put # words/line in ctr	*/
3:	addi	r6,r6,8			/* copy a cache line		*/
	ldx	r0,r6,r4
	stdx	r0,r6,r3
	bdnz	3b
	dcbst	r6,r3			/* write it to memory		*/
	sync
	icbi	r6,r3			/* flush the icache line	*/
	cmpld	0,r6,r5
	blt	4b
	sync
	addi	r5,r5,8
	addi	r6,r6,8
559
	isync
560 561 562 563 564 565 566 567 568 569 570 571 572 573
	blr

.align 8
copy_to_here:

#ifdef CONFIG_SMP
#ifdef CONFIG_PPC_PMAC
/*
 * On PowerMac, secondary processors starts from the reset vector, which
 * is temporarily turned into a call to one of the functions below.
 */
	.section ".text";
	.align 2 ;

574 575 576 577 578 579 580 581 582 583 584
	.globl	__secondary_start_pmac_0
__secondary_start_pmac_0:
	/* NB the entries for cpus 0, 1, 2 must each occupy 8 bytes. */
	li	r24,0
	b	1f
	li	r24,1
	b	1f
	li	r24,2
	b	1f
	li	r24,3
1:
585 586 587
	
_GLOBAL(pmac_secondary_start)
	/* turn on 64-bit mode */
588
	bl	enable_64b_mode
589

590 591 592 593 594 595 596 597 598
	li	r0,0
	mfspr	r3,SPRN_HID4
	rldimi	r3,r0,40,23	/* clear bit 23 (rm_ci) */
	sync
	mtspr	SPRN_HID4,r3
	isync
	sync
	slbia

599
	/* get TOC pointer (real address) */
600
	bl	relative_toc
601
	tovirt(r2,r2)
602

603
	/* Copy some CPU settings from CPU 0 */
604
	bl	__restore_cpu_ppc970
605 606 607 608 609 610 611

	/* pSeries do that early though I don't think we really need it */
	mfmsr	r3
	ori	r3,r3,MSR_RI
	mtmsrd	r3			/* RI on */

	/* Set up a paca value for this processor. */
612 613
	LOAD_REG_ADDR(r4,paca)		/* Load paca pointer		*/
	ld	r4,0(r4)		/* Get base vaddr of paca array	*/
614
	mulli	r13,r24,PACA_SIZE	/* Calculate vaddr of right paca */
615
	add	r13,r13,r4		/* for this processor.		*/
616
	SET_PACA(r13)			/* Save vaddr of paca in an SPRG*/
617

618 619 620 621 622
	/* Mark interrupts soft and hard disabled (they might be enabled
	 * in the PACA when doing hotplug)
	 */
	li	r0,0
	stb	r0,PACASOFTIRQEN(r13)
623 624
	li	r0,PACA_IRQ_HARD_DIS
	stb	r0,PACAIRQHAPPENED(r13)
625

626 627 628 629
	/* Create a temp kernel stack for use before relocation is on.	*/
	ld	r1,PACAEMERGSP(r13)
	subi	r1,r1,STACK_FRAME_OVERHEAD

630
	b	__secondary_start
631 632 633 634 635 636 637 638 639 640 641

#endif /* CONFIG_PPC_PMAC */

/*
 * This function is called after the master CPU has released the
 * secondary processors.  The execution environment is relocation off.
 * The paca for this processor has the following fields initialized at
 * this point:
 *   1. Processor number
 *   2. Segment table pointer (virtual address)
 * On entry the following are set:
642
 *   r1	       = stack pointer (real addr of temp stack)
643 644 645
 *   r24       = cpu# (in Linux terms)
 *   r13       = paca virtual address
 *   SPRG_PACA = paca virtual address
646
 */
647 648 649
	.section ".text";
	.align 2 ;

650
	.globl	__secondary_start
651
__secondary_start:
652 653
	/* Set thread priority to MEDIUM */
	HMT_MEDIUM
654

655
	/* Initialize the kernel stack */
656
	LOAD_REG_ADDR(r3, current_set)
657
	sldi	r28,r24,3		/* get current_set[cpu#]	 */
658 659 660
	ldx	r14,r3,r28
	addi	r14,r14,THREAD_SIZE-STACK_FRAME_OVERHEAD
	std	r14,PACAKSAVE(r13)
661

M
Michael Ellerman 已提交
662
	/* Do early setup for that CPU (SLB and hash table pointer) */
663
	bl	early_setup_secondary
664

665 666 667 668 669 670
	/*
	 * setup the new stack pointer, but *don't* use this until
	 * translation is on.
	 */
	mr	r1, r14

671
	/* Clear backchain so we get nice backtraces */
672 673 674
	li	r7,0
	mtlr	r7

675 676 677
	/* Mark interrupts soft and hard disabled (they might be enabled
	 * in the PACA when doing hotplug)
	 */
678
	stb	r7,PACASOFTIRQEN(r13)
679 680
	li	r0,PACA_IRQ_HARD_DIS
	stb	r0,PACAIRQHAPPENED(r13)
681

682
	/* enable MMU and jump to start_secondary */
683
	LOAD_REG_ADDR(r3, start_secondary_prolog)
684
	LOAD_REG_IMMEDIATE(r4, MSR_KERNEL)
685

686 687
	mtspr	SPRN_SRR0,r3
	mtspr	SPRN_SRR1,r4
688
	RFI
689 690 691 692
	b	.	/* prevent speculative execution */

/* 
 * Running with relocation on at this point.  All we want to do is
693 694
 * zero the stack back-chain pointer and get the TOC virtual address
 * before going into C code.
695
 */
696
start_secondary_prolog:
697
	ld	r2,PACATOC(r13)
698 699
	li	r3,0
	std	r3,0(r1)		/* Zero the stack frame pointer	*/
700
	bl	start_secondary
701
	b	.
702 703 704 705 706 707 708 709 710
/*
 * Reset stack pointer and call start_secondary
 * to continue with online operation when woken up
 * from cede in cpu offline.
 */
_GLOBAL(start_secondary_resume)
	ld	r1,PACAKSAVE(r13)	/* Reload kernel stack pointer */
	li	r3,0
	std	r3,0(r1)		/* Zero the stack frame pointer	*/
711
	bl	start_secondary
712
	b	.
713 714 715 716 717
#endif

/*
 * This subroutine clobbers r11 and r12
 */
718
enable_64b_mode:
719
	mfmsr	r11			/* grab the current MSR */
720 721 722 723
#ifdef CONFIG_PPC_BOOK3E
	oris	r11,r11,0x8000		/* CM bit set, we'll set ICM later */
	mtmsr	r11
#else /* CONFIG_PPC_BOOK3E */
724
	li	r12,(MSR_64BIT | MSR_ISF)@highest
725
	sldi	r12,r12,48
726 727 728
	or	r11,r11,r12
	mtmsrd	r11
	isync
729
#endif
730 731
	blr

732 733 734 735
/*
 * This puts the TOC pointer into r2, offset by 0x8000 (as expected
 * by the toolchain).  It computes the correct value for wherever we
 * are running at the moment, using position-independent code.
736 737 738 739 740 741 742
 *
 * Note: The compiler constructs pointers using offsets from the
 * TOC in -mcmodel=medium mode. After we relocate to 0 but before
 * the MMU is on we need our TOC to be a virtual address otherwise
 * these pointers will be real addresses which may get stored and
 * accessed later with the MMU on. We use tovirt() at the call
 * sites to handle this.
743 744 745 746
 */
_GLOBAL(relative_toc)
	mflr	r0
	bcl	20,31,$+4
747 748 749
0:	mflr	r11
	ld	r2,(p_toc - 0b)(r11)
	add	r2,r2,r11
750 751 752
	mtlr	r0
	blr

A
Anton Blanchard 已提交
753
.balign 8
754 755
p_toc:	.llong	__toc_start + 0x8000 - 0b

756 757 758
/*
 * This is where the main kernel code starts.
 */
759
start_here_multiplatform:
760
	/* set up the TOC */
761
	bl      relative_toc
762
	tovirt(r2,r2)
763 764 765 766 767 768

	/* Clear out the BSS. It may have been done in prom_init,
	 * already but that's irrelevant since prom_init will soon
	 * be detached from the kernel completely. Besides, we need
	 * to clear it now for kexec-style entry.
	 */
769 770
	LOAD_REG_ADDR(r11,__bss_stop)
	LOAD_REG_ADDR(r8,__bss_start)
771 772
	sub	r11,r11,r8		/* bss size			*/
	addi	r11,r11,7		/* round up to an even double word */
773
	srdi.	r11,r11,3		/* shift right by 3		*/
774 775 776 777 778 779 780 781
	beq	4f
	addi	r8,r8,-8
	li	r0,0
	mtctr	r11			/* zero this many doublewords	*/
3:	stdu	r0,8(r8)
	bdnz	3b
4:

782 783
#ifdef CONFIG_PPC_EARLY_DEBUG_OPAL
	/* Setup OPAL entry */
784
	LOAD_REG_ADDR(r11, opal)
785 786 787 788
	std	r28,0(r11);
	std	r29,8(r11);
#endif

789
#ifndef CONFIG_PPC_BOOK3E
790 791 792
	mfmsr	r6
	ori	r6,r6,MSR_RI
	mtmsrd	r6			/* RI on */
793
#endif
794

795 796 797 798 799 800 801
#ifdef CONFIG_RELOCATABLE
	/* Save the physical address we're running at in kernstart_addr */
	LOAD_REG_ADDR(r4, kernstart_addr)
	clrldi	r0,r25,2
	std	r0,0(r4)
#endif

802
	/* The following gets the stack set up with the regs */
803 804 805 806 807
	/* pointing to the real addr of the kernel stack.  This is   */
	/* all done to support the C function call below which sets  */
	/* up the htab.  This is done because we have relocated the  */
	/* kernel but are still running in real mode. */

808
	LOAD_REG_ADDR(r3,init_thread_union)
809

810
	/* set up a stack pointer */
811 812 813 814
	addi	r1,r3,THREAD_SIZE
	li	r0,0
	stdu	r0,-STACK_FRAME_OVERHEAD(r1)

M
Michael Ellerman 已提交
815 816 817 818
	/*
	 * Do very early kernel initializations, including initial hash table
	 * and SLB setup before we turn on relocation.
	 */
819 820 821

	/* Restore parameters passed from prom_init/kexec */
	mr	r3,r31
822
	bl	early_setup		/* also sets r13 and SPRG_PACA */
823

824
	LOAD_REG_ADDR(r3, start_here_common)
825
	ld	r4,PACAKMSR(r13)
826 827
	mtspr	SPRN_SRR0,r3
	mtspr	SPRN_SRR1,r4
828
	RFI
829 830 831
	b	.	/* prevent speculative execution */
	
	/* This is where all platforms converge execution */
832 833

start_here_common:
834
	/* relocation is on at this point */
835
	std	r1,PACAKSAVE(r13)
836

837
	/* Load the TOC (virtual address) */
838 839
	ld	r2,PACATOC(r13)

840
	/* Do more system initializations in virtual mode */
841
	bl	setup_system
842

843 844 845 846 847 848 849
	/* Mark interrupts soft and hard disabled (they might be enabled
	 * in the PACA when doing hotplug)
	 */
	li	r0,0
	stb	r0,PACASOFTIRQEN(r13)
	li	r0,PACA_IRQ_HARD_DIS
	stb	r0,PACAIRQHAPPENED(r13)
850

851
	/* Generic kernel entry */
852
	bl	start_kernel
853

854 855
	/* Not reached */
	BUG_OPCODE
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

/*
 * We put a few things here that have to be page-aligned.
 * This stuff goes at the beginning of the bss, which is page-aligned.
 */
	.section ".bss"

	.align	PAGE_SHIFT

	.globl	empty_zero_page
empty_zero_page:
	.space	PAGE_SIZE

	.globl	swapper_pg_dir
swapper_pg_dir:
871
	.space	PGD_TABLE_SIZE