head_64.S 25 KB

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