misc_64.S 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * This file contains miscellaneous low-level functions.
  4. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  5. *
  6. * Largely rewritten by Cort Dougan (cort@cs.nmt.edu)
  7. * and Paul Mackerras.
  8. * Adapted for iSeries by Mike Corrigan (mikejc@us.ibm.com)
  9. * PPC64 updates by Dave Engebretsen (engebret@us.ibm.com)
  10. */
  11. #include <linux/export.h>
  12. #include <linux/linkage.h>
  13. #include <linux/sys.h>
  14. #include <asm/unistd.h>
  15. #include <asm/errno.h>
  16. #include <asm/processor.h>
  17. #include <asm/page.h>
  18. #include <asm/cache.h>
  19. #include <asm/ppc_asm.h>
  20. #include <asm/asm-offsets.h>
  21. #include <asm/cputable.h>
  22. #include <asm/thread_info.h>
  23. #include <asm/kexec.h>
  24. #include <asm/ptrace.h>
  25. #include <asm/mmu.h>
  26. #include <asm/feature-fixups.h>
  27. .text
  28. _GLOBAL(__bswapdi2)
  29. EXPORT_SYMBOL(__bswapdi2)
  30. srdi r8,r3,32
  31. rlwinm r7,r3,8,0xffffffff
  32. rlwimi r7,r3,24,0,7
  33. rlwinm r9,r8,8,0xffffffff
  34. rlwimi r7,r3,24,16,23
  35. rlwimi r9,r8,24,0,7
  36. rlwimi r9,r8,24,16,23
  37. sldi r7,r7,32
  38. or r3,r7,r9
  39. blr
  40. #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
  41. _GLOBAL(rmci_on)
  42. sync
  43. isync
  44. li r3,0x100
  45. rldicl r3,r3,32,0
  46. mfspr r5,SPRN_HID4
  47. or r5,r5,r3
  48. sync
  49. mtspr SPRN_HID4,r5
  50. isync
  51. slbia
  52. isync
  53. sync
  54. blr
  55. _GLOBAL(rmci_off)
  56. sync
  57. isync
  58. li r3,0x100
  59. rldicl r3,r3,32,0
  60. mfspr r5,SPRN_HID4
  61. andc r5,r5,r3
  62. sync
  63. mtspr SPRN_HID4,r5
  64. isync
  65. slbia
  66. isync
  67. sync
  68. blr
  69. #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
  70. #if defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_MAPLE)
  71. /*
  72. * Do an IO access in real mode
  73. */
  74. _GLOBAL(real_readb)
  75. mfmsr r7
  76. ori r0,r7,MSR_DR
  77. xori r0,r0,MSR_DR
  78. sync
  79. mtmsrd r0
  80. sync
  81. isync
  82. mfspr r6,SPRN_HID4
  83. rldicl r5,r6,32,0
  84. ori r5,r5,0x100
  85. rldicl r5,r5,32,0
  86. sync
  87. mtspr SPRN_HID4,r5
  88. isync
  89. slbia
  90. isync
  91. lbz r3,0(r3)
  92. sync
  93. mtspr SPRN_HID4,r6
  94. isync
  95. slbia
  96. isync
  97. mtmsrd r7
  98. sync
  99. isync
  100. blr
  101. /*
  102. * Do an IO access in real mode
  103. */
  104. _GLOBAL(real_writeb)
  105. mfmsr r7
  106. ori r0,r7,MSR_DR
  107. xori r0,r0,MSR_DR
  108. sync
  109. mtmsrd r0
  110. sync
  111. isync
  112. mfspr r6,SPRN_HID4
  113. rldicl r5,r6,32,0
  114. ori r5,r5,0x100
  115. rldicl r5,r5,32,0
  116. sync
  117. mtspr SPRN_HID4,r5
  118. isync
  119. slbia
  120. isync
  121. stb r3,0(r4)
  122. sync
  123. mtspr SPRN_HID4,r6
  124. isync
  125. slbia
  126. isync
  127. mtmsrd r7
  128. sync
  129. isync
  130. blr
  131. #endif /* defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_MAPLE) */
  132. #ifdef CONFIG_PPC_PASEMI
  133. _GLOBAL(real_205_readb)
  134. mfmsr r7
  135. ori r0,r7,MSR_DR
  136. xori r0,r0,MSR_DR
  137. sync
  138. mtmsrd r0
  139. sync
  140. isync
  141. LBZCIX(R3,R0,R3)
  142. isync
  143. mtmsrd r7
  144. sync
  145. isync
  146. blr
  147. _GLOBAL(real_205_writeb)
  148. mfmsr r7
  149. ori r0,r7,MSR_DR
  150. xori r0,r0,MSR_DR
  151. sync
  152. mtmsrd r0
  153. sync
  154. isync
  155. STBCIX(R3,R0,R4)
  156. isync
  157. mtmsrd r7
  158. sync
  159. isync
  160. blr
  161. #endif /* CONFIG_PPC_PASEMI */
  162. #if defined(CONFIG_CPU_FREQ_PMAC64) || defined(CONFIG_CPU_FREQ_MAPLE)
  163. /*
  164. * SCOM access functions for 970 (FX only for now)
  165. *
  166. * unsigned long scom970_read(unsigned int address);
  167. * void scom970_write(unsigned int address, unsigned long value);
  168. *
  169. * The address passed in is the 24 bits register address. This code
  170. * is 970 specific and will not check the status bits, so you should
  171. * know what you are doing.
  172. */
  173. _GLOBAL(scom970_read)
  174. /* interrupts off */
  175. mfmsr r4
  176. ori r0,r4,MSR_EE
  177. xori r0,r0,MSR_EE
  178. mtmsrd r0,1
  179. /* rotate 24 bits SCOM address 8 bits left and mask out its low 8 bits
  180. * (including parity). On current CPUs they must be 0'd,
  181. * and finally or in RW bit
  182. */
  183. rlwinm r3,r3,8,0,15
  184. ori r3,r3,0x8000
  185. /* do the actual scom read */
  186. sync
  187. mtspr SPRN_SCOMC,r3
  188. isync
  189. mfspr r3,SPRN_SCOMD
  190. isync
  191. mfspr r0,SPRN_SCOMC
  192. isync
  193. /* XXX: fixup result on some buggy 970's (ouch ! we lost a bit, bah
  194. * that's the best we can do). Not implemented yet as we don't use
  195. * the scom on any of the bogus CPUs yet, but may have to be done
  196. * ultimately
  197. */
  198. /* restore interrupts */
  199. mtmsrd r4,1
  200. blr
  201. _GLOBAL(scom970_write)
  202. /* interrupts off */
  203. mfmsr r5
  204. ori r0,r5,MSR_EE
  205. xori r0,r0,MSR_EE
  206. mtmsrd r0,1
  207. /* rotate 24 bits SCOM address 8 bits left and mask out its low 8 bits
  208. * (including parity). On current CPUs they must be 0'd.
  209. */
  210. rlwinm r3,r3,8,0,15
  211. sync
  212. mtspr SPRN_SCOMD,r4 /* write data */
  213. isync
  214. mtspr SPRN_SCOMC,r3 /* write command */
  215. isync
  216. mfspr 3,SPRN_SCOMC
  217. isync
  218. /* restore interrupts */
  219. mtmsrd r5,1
  220. blr
  221. #endif /* CONFIG_CPU_FREQ_PMAC64 || CONFIG_CPU_FREQ_MAPLE */
  222. /* kexec_wait(phys_cpu)
  223. *
  224. * wait for the flag to change, indicating this kernel is going away but
  225. * the slave code for the next one is at addresses 0 to 100.
  226. *
  227. * This is used by all slaves, even those that did not find a matching
  228. * paca in the secondary startup code.
  229. *
  230. * Physical (hardware) cpu id should be in r3.
  231. */
  232. _GLOBAL(kexec_wait)
  233. bcl 20,31,$+4
  234. 1: mflr r5
  235. addi r5,r5,kexec_flag-1b
  236. 99: HMT_LOW
  237. #ifdef CONFIG_KEXEC_CORE /* use no memory without kexec */
  238. lwz r4,0(r5)
  239. cmpwi 0,r4,0
  240. beq 99b
  241. #ifdef CONFIG_PPC_BOOK3S_64
  242. li r10,0x60
  243. mfmsr r11
  244. clrrdi r11,r11,1 /* Clear MSR_LE */
  245. mtsrr0 r10
  246. mtsrr1 r11
  247. rfid
  248. #else
  249. /* Create TLB entry in book3e_secondary_core_init */
  250. li r4,0
  251. ba 0x60
  252. #endif
  253. #endif
  254. /* this can be in text because we won't change it until we are
  255. * running in real anyways
  256. */
  257. kexec_flag:
  258. .long 0
  259. #ifdef CONFIG_KEXEC_CORE
  260. #ifdef CONFIG_PPC_BOOK3E_64
  261. /*
  262. * BOOK3E has no real MMU mode, so we have to setup the initial TLB
  263. * for a core to identity map v:0 to p:0. This current implementation
  264. * assumes that 1G is enough for kexec.
  265. */
  266. kexec_create_tlb:
  267. /*
  268. * Invalidate all non-IPROT TLB entries to avoid any TLB conflict.
  269. * IPROT TLB entries should be >= PAGE_OFFSET and thus not conflict.
  270. */
  271. PPC_TLBILX_ALL(0,R0)
  272. sync
  273. isync
  274. mfspr r10,SPRN_TLB1CFG
  275. andi. r10,r10,TLBnCFG_N_ENTRY /* Extract # entries */
  276. subi r10,r10,1 /* Last entry: no conflict with kernel text */
  277. lis r9,MAS0_TLBSEL(1)@h
  278. rlwimi r9,r10,16,4,15 /* Setup MAS0 = TLBSEL | ESEL(r9) */
  279. /* Set up a temp identity mapping v:0 to p:0 and return to it. */
  280. mtspr SPRN_MAS0,r9
  281. lis r9,(MAS1_VALID|MAS1_IPROT)@h
  282. ori r9,r9,(MAS1_TSIZE(BOOK3E_PAGESZ_1GB))@l
  283. mtspr SPRN_MAS1,r9
  284. LOAD_REG_IMMEDIATE(r9, 0x0 | MAS2_M_IF_NEEDED)
  285. mtspr SPRN_MAS2,r9
  286. LOAD_REG_IMMEDIATE(r9, 0x0 | MAS3_SR | MAS3_SW | MAS3_SX)
  287. mtspr SPRN_MAS3,r9
  288. li r9,0
  289. mtspr SPRN_MAS7,r9
  290. tlbwe
  291. isync
  292. blr
  293. #endif
  294. /* kexec_smp_wait(void)
  295. *
  296. * call with interrupts off
  297. * note: this is a terminal routine, it does not save lr
  298. *
  299. * get phys id from paca
  300. * switch to real mode
  301. * mark the paca as no longer used
  302. * join other cpus in kexec_wait(phys_id)
  303. */
  304. _GLOBAL(kexec_smp_wait)
  305. lhz r3,PACAHWCPUID(r13)
  306. bl real_mode
  307. li r4,KEXEC_STATE_REAL_MODE
  308. stb r4,PACAKEXECSTATE(r13)
  309. b kexec_wait
  310. /*
  311. * switch to real mode (turn mmu off)
  312. * we use the early kernel trick that the hardware ignores bits
  313. * 0 and 1 (big endian) of the effective address in real mode
  314. *
  315. * don't overwrite r3 here, it is live for kexec_wait above.
  316. */
  317. SYM_FUNC_START_LOCAL(real_mode) /* assume normal blr return */
  318. #ifdef CONFIG_PPC_BOOK3E_64
  319. /* Create an identity mapping. */
  320. b kexec_create_tlb
  321. #else
  322. 1: li r9,MSR_RI
  323. li r10,MSR_DR|MSR_IR
  324. mflr r11 /* return address to SRR0 */
  325. mfmsr r12
  326. andc r9,r12,r9
  327. andc r10,r12,r10
  328. mtmsrd r9,1
  329. mtspr SPRN_SRR1,r10
  330. mtspr SPRN_SRR0,r11
  331. rfid
  332. #endif
  333. SYM_FUNC_END(real_mode)
  334. /*
  335. * kexec_sequence(newstack, start, image, control, clear_all(),
  336. copy_with_mmu_off)
  337. *
  338. * does the grungy work with stack switching and real mode switches
  339. * also does simple calls to other code
  340. */
  341. _GLOBAL(kexec_sequence)
  342. mflr r0
  343. std r0,16(r1)
  344. /* switch stacks to newstack -- &kexec_stack.stack */
  345. stdu r1,THREAD_SIZE-STACK_FRAME_MIN_SIZE(r3)
  346. mr r1,r3
  347. li r0,0
  348. std r0,16(r1)
  349. /* save regs for local vars on new stack.
  350. * yes, we won't go back, but ...
  351. */
  352. std r31,-8(r1)
  353. std r30,-16(r1)
  354. std r29,-24(r1)
  355. std r28,-32(r1)
  356. std r27,-40(r1)
  357. std r26,-48(r1)
  358. std r25,-56(r1)
  359. stdu r1,-STACK_FRAME_MIN_SIZE-64(r1)
  360. /* save args into preserved regs */
  361. mr r31,r3 /* newstack (both) */
  362. mr r30,r4 /* start (real) */
  363. mr r29,r5 /* image (virt) */
  364. mr r28,r6 /* control, unused */
  365. mr r27,r7 /* clear_all() fn desc */
  366. mr r26,r8 /* copy_with_mmu_off */
  367. lhz r25,PACAHWCPUID(r13) /* get our phys cpu from paca */
  368. /* disable interrupts, we are overwriting kernel data next */
  369. #ifdef CONFIG_PPC_BOOK3E_64
  370. wrteei 0
  371. #else
  372. mfmsr r3
  373. rlwinm r3,r3,0,17,15
  374. mtmsrd r3,1
  375. #endif
  376. /* We need to turn the MMU off unless we are in hash mode
  377. * under a hypervisor
  378. */
  379. cmpdi r26,0
  380. beq 1f
  381. bl real_mode
  382. 1:
  383. /* copy dest pages, flush whole dest image */
  384. mr r3,r29
  385. bl CFUNC(kexec_copy_flush) /* (image) */
  386. /* turn off mmu now if not done earlier */
  387. cmpdi r26,0
  388. bne 1f
  389. bl real_mode
  390. /* copy 0x100 bytes starting at start to 0 */
  391. 1: li r3,0
  392. mr r4,r30 /* start, aka phys mem offset */
  393. li r5,0x100
  394. li r6,0
  395. bl copy_and_flush /* (dest, src, copy limit, start offset) */
  396. 1: /* assume normal blr return */
  397. /* release other cpus to the new kernel secondary start at 0x60 */
  398. mflr r5
  399. li r6,1
  400. stw r6,kexec_flag-1b(5)
  401. cmpdi r27,0
  402. beq 1f
  403. /* clear out hardware hash page table and tlb */
  404. #ifdef CONFIG_PPC64_ELF_ABI_V1
  405. ld r12,0(r27) /* deref function descriptor */
  406. #else
  407. mr r12,r27
  408. #endif
  409. mtctr r12
  410. bctrl /* mmu_hash_ops.hpte_clear_all(void); */
  411. /*
  412. * kexec image calling is:
  413. * the first 0x100 bytes of the entry point are copied to 0
  414. *
  415. * all slaves branch to slave = 0x60 (absolute)
  416. * slave(phys_cpu_id);
  417. *
  418. * master goes to start = entry point
  419. * start(phys_cpu_id, start, 0);
  420. *
  421. *
  422. * a wrapper is needed to call existing kernels, here is an approximate
  423. * description of one method:
  424. *
  425. * v2: (2.6.10)
  426. * start will be near the boot_block (maybe 0x100 bytes before it?)
  427. * it will have a 0x60, which will b to boot_block, where it will wait
  428. * and 0 will store phys into struct boot-block and load r3 from there,
  429. * copy kernel 0-0x100 and tell slaves to back down to 0x60 again
  430. *
  431. * v1: (2.6.9)
  432. * boot block will have all cpus scanning device tree to see if they
  433. * are the boot cpu ?????
  434. * other device tree differences (prop sizes, va vs pa, etc)...
  435. */
  436. 1: mr r3,r25 # my phys cpu
  437. mr r4,r30 # start, aka phys mem offset
  438. mtlr 4
  439. li r5,0
  440. blr /* image->start(physid, image->start, 0); */
  441. #endif /* CONFIG_KEXEC_CORE */