head_64.S 26 KB

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