head_64.S 25 KB

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