head_64.S 25 KB

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