head_64.S 25 KB

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