head_64.S 25 KB

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