head_64.S 25 KB

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