misc_64.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * This file contains miscellaneous low-level functions.
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. *
  5. * Largely rewritten by Cort Dougan (cort@cs.nmt.edu)
  6. * and Paul Mackerras.
  7. * Adapted for iSeries by Mike Corrigan (mikejc@us.ibm.com)
  8. * PPC64 updates by Dave Engebretsen (engebret@us.ibm.com)
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. */
  16. #include <linux/sys.h>
  17. #include <asm/unistd.h>
  18. #include <asm/errno.h>
  19. #include <asm/processor.h>
  20. #include <asm/page.h>
  21. #include <asm/cache.h>
  22. #include <asm/ppc_asm.h>
  23. #include <asm/asm-offsets.h>
  24. #include <asm/cputable.h>
  25. #include <asm/thread_info.h>
  26. #include <asm/kexec.h>
  27. .text
  28. #ifdef CONFIG_IRQSTACKS
  29. _GLOBAL(call_do_softirq)
  30. mflr r0
  31. std r0,16(r1)
  32. stdu r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r3)
  33. mr r1,r3
  34. bl .__do_softirq
  35. ld r1,0(r1)
  36. ld r0,16(r1)
  37. mtlr r0
  38. blr
  39. _GLOBAL(call_handle_irq)
  40. ld r8,0(r6)
  41. mflr r0
  42. std r0,16(r1)
  43. mtctr r8
  44. stdu r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r5)
  45. mr r1,r5
  46. bctrl
  47. ld r1,0(r1)
  48. ld r0,16(r1)
  49. mtlr r0
  50. blr
  51. #endif /* CONFIG_IRQSTACKS */
  52. .section ".toc","aw"
  53. PPC64_CACHES:
  54. .tc ppc64_caches[TC],ppc64_caches
  55. .section ".text"
  56. /*
  57. * Write any modified data cache blocks out to memory
  58. * and invalidate the corresponding instruction cache blocks.
  59. *
  60. * flush_icache_range(unsigned long start, unsigned long stop)
  61. *
  62. * flush all bytes from start through stop-1 inclusive
  63. */
  64. _KPROBE(__flush_icache_range)
  65. /*
  66. * Flush the data cache to memory
  67. *
  68. * Different systems have different cache line sizes
  69. * and in some cases i-cache and d-cache line sizes differ from
  70. * each other.
  71. */
  72. ld r10,PPC64_CACHES@toc(r2)
  73. lwz r7,DCACHEL1LINESIZE(r10)/* Get cache line size */
  74. addi r5,r7,-1
  75. andc r6,r3,r5 /* round low to line bdy */
  76. subf r8,r6,r4 /* compute length */
  77. add r8,r8,r5 /* ensure we get enough */
  78. lwz r9,DCACHEL1LOGLINESIZE(r10) /* Get log-2 of cache line size */
  79. srw. r8,r8,r9 /* compute line count */
  80. beqlr /* nothing to do? */
  81. mtctr r8
  82. 1: dcbst 0,r6
  83. add r6,r6,r7
  84. bdnz 1b
  85. sync
  86. /* Now invalidate the instruction cache */
  87. lwz r7,ICACHEL1LINESIZE(r10) /* Get Icache line size */
  88. addi r5,r7,-1
  89. andc r6,r3,r5 /* round low to line bdy */
  90. subf r8,r6,r4 /* compute length */
  91. add r8,r8,r5
  92. lwz r9,ICACHEL1LOGLINESIZE(r10) /* Get log-2 of Icache line size */
  93. srw. r8,r8,r9 /* compute line count */
  94. beqlr /* nothing to do? */
  95. mtctr r8
  96. 2: icbi 0,r6
  97. add r6,r6,r7
  98. bdnz 2b
  99. isync
  100. blr
  101. .previous .text
  102. /*
  103. * Like above, but only do the D-cache.
  104. *
  105. * flush_dcache_range(unsigned long start, unsigned long stop)
  106. *
  107. * flush all bytes from start to stop-1 inclusive
  108. */
  109. _GLOBAL(flush_dcache_range)
  110. /*
  111. * Flush the data cache to memory
  112. *
  113. * Different systems have different cache line sizes
  114. */
  115. ld r10,PPC64_CACHES@toc(r2)
  116. lwz r7,DCACHEL1LINESIZE(r10) /* Get dcache line size */
  117. addi r5,r7,-1
  118. andc r6,r3,r5 /* round low to line bdy */
  119. subf r8,r6,r4 /* compute length */
  120. add r8,r8,r5 /* ensure we get enough */
  121. lwz r9,DCACHEL1LOGLINESIZE(r10) /* Get log-2 of dcache line size */
  122. srw. r8,r8,r9 /* compute line count */
  123. beqlr /* nothing to do? */
  124. mtctr r8
  125. 0: dcbst 0,r6
  126. add r6,r6,r7
  127. bdnz 0b
  128. sync
  129. blr
  130. /*
  131. * Like above, but works on non-mapped physical addresses.
  132. * Use only for non-LPAR setups ! It also assumes real mode
  133. * is cacheable. Used for flushing out the DART before using
  134. * it as uncacheable memory
  135. *
  136. * flush_dcache_phys_range(unsigned long start, unsigned long stop)
  137. *
  138. * flush all bytes from start to stop-1 inclusive
  139. */
  140. _GLOBAL(flush_dcache_phys_range)
  141. ld r10,PPC64_CACHES@toc(r2)
  142. lwz r7,DCACHEL1LINESIZE(r10) /* Get dcache line size */
  143. addi r5,r7,-1
  144. andc r6,r3,r5 /* round low to line bdy */
  145. subf r8,r6,r4 /* compute length */
  146. add r8,r8,r5 /* ensure we get enough */
  147. lwz r9,DCACHEL1LOGLINESIZE(r10) /* Get log-2 of dcache line size */
  148. srw. r8,r8,r9 /* compute line count */
  149. beqlr /* nothing to do? */
  150. mfmsr r5 /* Disable MMU Data Relocation */
  151. ori r0,r5,MSR_DR
  152. xori r0,r0,MSR_DR
  153. sync
  154. mtmsr r0
  155. sync
  156. isync
  157. mtctr r8
  158. 0: dcbst 0,r6
  159. add r6,r6,r7
  160. bdnz 0b
  161. sync
  162. isync
  163. mtmsr r5 /* Re-enable MMU Data Relocation */
  164. sync
  165. isync
  166. blr
  167. _GLOBAL(flush_inval_dcache_range)
  168. ld r10,PPC64_CACHES@toc(r2)
  169. lwz r7,DCACHEL1LINESIZE(r10) /* Get dcache line size */
  170. addi r5,r7,-1
  171. andc r6,r3,r5 /* round low to line bdy */
  172. subf r8,r6,r4 /* compute length */
  173. add r8,r8,r5 /* ensure we get enough */
  174. lwz r9,DCACHEL1LOGLINESIZE(r10)/* Get log-2 of dcache line size */
  175. srw. r8,r8,r9 /* compute line count */
  176. beqlr /* nothing to do? */
  177. sync
  178. isync
  179. mtctr r8
  180. 0: dcbf 0,r6
  181. add r6,r6,r7
  182. bdnz 0b
  183. sync
  184. isync
  185. blr
  186. /*
  187. * Flush a particular page from the data cache to RAM.
  188. * Note: this is necessary because the instruction cache does *not*
  189. * snoop from the data cache.
  190. *
  191. * void __flush_dcache_icache(void *page)
  192. */
  193. _GLOBAL(__flush_dcache_icache)
  194. /*
  195. * Flush the data cache to memory
  196. *
  197. * Different systems have different cache line sizes
  198. */
  199. /* Flush the dcache */
  200. ld r7,PPC64_CACHES@toc(r2)
  201. clrrdi r3,r3,PAGE_SHIFT /* Page align */
  202. lwz r4,DCACHEL1LINESPERPAGE(r7) /* Get # dcache lines per page */
  203. lwz r5,DCACHEL1LINESIZE(r7) /* Get dcache line size */
  204. mr r6,r3
  205. mtctr r4
  206. 0: dcbst 0,r6
  207. add r6,r6,r5
  208. bdnz 0b
  209. sync
  210. /* Now invalidate the icache */
  211. lwz r4,ICACHEL1LINESPERPAGE(r7) /* Get # icache lines per page */
  212. lwz r5,ICACHEL1LINESIZE(r7) /* Get icache line size */
  213. mtctr r4
  214. 1: icbi 0,r3
  215. add r3,r3,r5
  216. bdnz 1b
  217. isync
  218. blr
  219. #if defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_MAPLE)
  220. /*
  221. * Do an IO access in real mode
  222. */
  223. _GLOBAL(real_readb)
  224. mfmsr r7
  225. ori r0,r7,MSR_DR
  226. xori r0,r0,MSR_DR
  227. sync
  228. mtmsrd r0
  229. sync
  230. isync
  231. mfspr r6,SPRN_HID4
  232. rldicl r5,r6,32,0
  233. ori r5,r5,0x100
  234. rldicl r5,r5,32,0
  235. sync
  236. mtspr SPRN_HID4,r5
  237. isync
  238. slbia
  239. isync
  240. lbz r3,0(r3)
  241. sync
  242. mtspr SPRN_HID4,r6
  243. isync
  244. slbia
  245. isync
  246. mtmsrd r7
  247. sync
  248. isync
  249. blr
  250. /*
  251. * Do an IO access in real mode
  252. */
  253. _GLOBAL(real_writeb)
  254. mfmsr r7
  255. ori r0,r7,MSR_DR
  256. xori r0,r0,MSR_DR
  257. sync
  258. mtmsrd r0
  259. sync
  260. isync
  261. mfspr r6,SPRN_HID4
  262. rldicl r5,r6,32,0
  263. ori r5,r5,0x100
  264. rldicl r5,r5,32,0
  265. sync
  266. mtspr SPRN_HID4,r5
  267. isync
  268. slbia
  269. isync
  270. stb r3,0(r4)
  271. sync
  272. mtspr SPRN_HID4,r6
  273. isync
  274. slbia
  275. isync
  276. mtmsrd r7
  277. sync
  278. isync
  279. blr
  280. #endif /* defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_MAPLE) */
  281. #ifdef CONFIG_PPC_PASEMI
  282. /* No support in all binutils for these yet, so use defines */
  283. #define LBZCIX(RT,RA,RB) .long (0x7c0006aa|(RT<<21)|(RA<<16)|(RB << 11))
  284. #define STBCIX(RS,RA,RB) .long (0x7c0007aa|(RS<<21)|(RA<<16)|(RB << 11))
  285. _GLOBAL(real_205_readb)
  286. mfmsr r7
  287. ori r0,r7,MSR_DR
  288. xori r0,r0,MSR_DR
  289. sync
  290. mtmsrd r0
  291. sync
  292. isync
  293. LBZCIX(r3,0,r3)
  294. isync
  295. mtmsrd r7
  296. sync
  297. isync
  298. blr
  299. _GLOBAL(real_205_writeb)
  300. mfmsr r7
  301. ori r0,r7,MSR_DR
  302. xori r0,r0,MSR_DR
  303. sync
  304. mtmsrd r0
  305. sync
  306. isync
  307. STBCIX(r3,0,r4)
  308. isync
  309. mtmsrd r7
  310. sync
  311. isync
  312. blr
  313. #endif /* CONFIG_PPC_PASEMI */
  314. #ifdef CONFIG_CPU_FREQ_PMAC64
  315. /*
  316. * SCOM access functions for 970 (FX only for now)
  317. *
  318. * unsigned long scom970_read(unsigned int address);
  319. * void scom970_write(unsigned int address, unsigned long value);
  320. *
  321. * The address passed in is the 24 bits register address. This code
  322. * is 970 specific and will not check the status bits, so you should
  323. * know what you are doing.
  324. */
  325. _GLOBAL(scom970_read)
  326. /* interrupts off */
  327. mfmsr r4
  328. ori r0,r4,MSR_EE
  329. xori r0,r0,MSR_EE
  330. mtmsrd r0,1
  331. /* rotate 24 bits SCOM address 8 bits left and mask out it's low 8 bits
  332. * (including parity). On current CPUs they must be 0'd,
  333. * and finally or in RW bit
  334. */
  335. rlwinm r3,r3,8,0,15
  336. ori r3,r3,0x8000
  337. /* do the actual scom read */
  338. sync
  339. mtspr SPRN_SCOMC,r3
  340. isync
  341. mfspr r3,SPRN_SCOMD
  342. isync
  343. mfspr r0,SPRN_SCOMC
  344. isync
  345. /* XXX: fixup result on some buggy 970's (ouch ! we lost a bit, bah
  346. * that's the best we can do). Not implemented yet as we don't use
  347. * the scom on any of the bogus CPUs yet, but may have to be done
  348. * ultimately
  349. */
  350. /* restore interrupts */
  351. mtmsrd r4,1
  352. blr
  353. _GLOBAL(scom970_write)
  354. /* interrupts off */
  355. mfmsr r5
  356. ori r0,r5,MSR_EE
  357. xori r0,r0,MSR_EE
  358. mtmsrd r0,1
  359. /* rotate 24 bits SCOM address 8 bits left and mask out it's low 8 bits
  360. * (including parity). On current CPUs they must be 0'd.
  361. */
  362. rlwinm r3,r3,8,0,15
  363. sync
  364. mtspr SPRN_SCOMD,r4 /* write data */
  365. isync
  366. mtspr SPRN_SCOMC,r3 /* write command */
  367. isync
  368. mfspr 3,SPRN_SCOMC
  369. isync
  370. /* restore interrupts */
  371. mtmsrd r5,1
  372. blr
  373. #endif /* CONFIG_CPU_FREQ_PMAC64 */
  374. /*
  375. * Create a kernel thread
  376. * kernel_thread(fn, arg, flags)
  377. */
  378. _GLOBAL(kernel_thread)
  379. std r29,-24(r1)
  380. std r30,-16(r1)
  381. stdu r1,-STACK_FRAME_OVERHEAD(r1)
  382. mr r29,r3
  383. mr r30,r4
  384. ori r3,r5,CLONE_VM /* flags */
  385. oris r3,r3,(CLONE_UNTRACED>>16)
  386. li r4,0 /* new sp (unused) */
  387. li r0,__NR_clone
  388. sc
  389. bns+ 1f /* did system call indicate error? */
  390. neg r3,r3 /* if so, make return code negative */
  391. 1: cmpdi 0,r3,0 /* parent or child? */
  392. bne 2f /* return if parent */
  393. li r0,0
  394. stdu r0,-STACK_FRAME_OVERHEAD(r1)
  395. ld r2,8(r29)
  396. ld r29,0(r29)
  397. mtlr r29 /* fn addr in lr */
  398. mr r3,r30 /* load arg and call fn */
  399. blrl
  400. li r0,__NR_exit /* exit after child exits */
  401. li r3,0
  402. sc
  403. 2: addi r1,r1,STACK_FRAME_OVERHEAD
  404. ld r29,-24(r1)
  405. ld r30,-16(r1)
  406. blr
  407. /*
  408. * disable_kernel_fp()
  409. * Disable the FPU.
  410. */
  411. _GLOBAL(disable_kernel_fp)
  412. mfmsr r3
  413. rldicl r0,r3,(63-MSR_FP_LG),1
  414. rldicl r3,r0,(MSR_FP_LG+1),0
  415. mtmsrd r3 /* disable use of fpu now */
  416. isync
  417. blr
  418. /* kexec_wait(phys_cpu)
  419. *
  420. * wait for the flag to change, indicating this kernel is going away but
  421. * the slave code for the next one is at addresses 0 to 100.
  422. *
  423. * This is used by all slaves.
  424. *
  425. * Physical (hardware) cpu id should be in r3.
  426. */
  427. _GLOBAL(kexec_wait)
  428. bl 1f
  429. 1: mflr r5
  430. addi r5,r5,kexec_flag-1b
  431. li r4,KEXEC_STATE_REAL_MODE
  432. stb r4,PACAKEXECSTATE(r13)
  433. SYNC
  434. 99: HMT_LOW
  435. #ifdef CONFIG_KEXEC /* use no memory without kexec */
  436. lwz r4,0(r5)
  437. cmpwi 0,r4,0
  438. bnea 0x60
  439. #endif
  440. b 99b
  441. /* this can be in text because we won't change it until we are
  442. * running in real anyways
  443. */
  444. kexec_flag:
  445. .long 0
  446. #ifdef CONFIG_KEXEC
  447. /* kexec_smp_wait(void)
  448. *
  449. * call with interrupts off
  450. * note: this is a terminal routine, it does not save lr
  451. *
  452. * get phys id from paca
  453. * switch to real mode
  454. * join other cpus in kexec_wait(phys_id)
  455. */
  456. _GLOBAL(kexec_smp_wait)
  457. lhz r3,PACAHWCPUID(r13)
  458. bl real_mode
  459. b .kexec_wait
  460. /*
  461. * switch to real mode (turn mmu off)
  462. * we use the early kernel trick that the hardware ignores bits
  463. * 0 and 1 (big endian) of the effective address in real mode
  464. *
  465. * don't overwrite r3 here, it is live for kexec_wait above.
  466. */
  467. real_mode: /* assume normal blr return */
  468. 1: li r9,MSR_RI
  469. li r10,MSR_DR|MSR_IR
  470. mflr r11 /* return address to SRR0 */
  471. mfmsr r12
  472. andc r9,r12,r9
  473. andc r10,r12,r10
  474. mtmsrd r9,1
  475. mtspr SPRN_SRR1,r10
  476. mtspr SPRN_SRR0,r11
  477. rfid
  478. /*
  479. * kexec_sequence(newstack, start, image, control, clear_all())
  480. *
  481. * does the grungy work with stack switching and real mode switches
  482. * also does simple calls to other code
  483. */
  484. _GLOBAL(kexec_sequence)
  485. mflr r0
  486. std r0,16(r1)
  487. /* switch stacks to newstack -- &kexec_stack.stack */
  488. stdu r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r3)
  489. mr r1,r3
  490. li r0,0
  491. std r0,16(r1)
  492. /* save regs for local vars on new stack.
  493. * yes, we won't go back, but ...
  494. */
  495. std r31,-8(r1)
  496. std r30,-16(r1)
  497. std r29,-24(r1)
  498. std r28,-32(r1)
  499. std r27,-40(r1)
  500. std r26,-48(r1)
  501. std r25,-56(r1)
  502. stdu r1,-STACK_FRAME_OVERHEAD-64(r1)
  503. /* save args into preserved regs */
  504. mr r31,r3 /* newstack (both) */
  505. mr r30,r4 /* start (real) */
  506. mr r29,r5 /* image (virt) */
  507. mr r28,r6 /* control, unused */
  508. mr r27,r7 /* clear_all() fn desc */
  509. mr r26,r8 /* spare */
  510. lhz r25,PACAHWCPUID(r13) /* get our phys cpu from paca */
  511. /* disable interrupts, we are overwriting kernel data next */
  512. mfmsr r3
  513. rlwinm r3,r3,0,17,15
  514. mtmsrd r3,1
  515. /* copy dest pages, flush whole dest image */
  516. mr r3,r29
  517. bl .kexec_copy_flush /* (image) */
  518. /* turn off mmu */
  519. bl real_mode
  520. /* copy 0x100 bytes starting at start to 0 */
  521. li r3,0
  522. mr r4,r30 /* start, aka phys mem offset */
  523. li r5,0x100
  524. li r6,0
  525. bl .copy_and_flush /* (dest, src, copy limit, start offset) */
  526. 1: /* assume normal blr return */
  527. /* release other cpus to the new kernel secondary start at 0x60 */
  528. mflr r5
  529. li r6,1
  530. stw r6,kexec_flag-1b(5)
  531. /* clear out hardware hash page table and tlb */
  532. ld r5,0(r27) /* deref function descriptor */
  533. mtctr r5
  534. bctrl /* ppc_md.hpte_clear_all(void); */
  535. /*
  536. * kexec image calling is:
  537. * the first 0x100 bytes of the entry point are copied to 0
  538. *
  539. * all slaves branch to slave = 0x60 (absolute)
  540. * slave(phys_cpu_id);
  541. *
  542. * master goes to start = entry point
  543. * start(phys_cpu_id, start, 0);
  544. *
  545. *
  546. * a wrapper is needed to call existing kernels, here is an approximate
  547. * description of one method:
  548. *
  549. * v2: (2.6.10)
  550. * start will be near the boot_block (maybe 0x100 bytes before it?)
  551. * it will have a 0x60, which will b to boot_block, where it will wait
  552. * and 0 will store phys into struct boot-block and load r3 from there,
  553. * copy kernel 0-0x100 and tell slaves to back down to 0x60 again
  554. *
  555. * v1: (2.6.9)
  556. * boot block will have all cpus scanning device tree to see if they
  557. * are the boot cpu ?????
  558. * other device tree differences (prop sizes, va vs pa, etc)...
  559. */
  560. mr r3,r25 # my phys cpu
  561. mr r4,r30 # start, aka phys mem offset
  562. mtlr 4
  563. li r5,0
  564. blr /* image->start(physid, image->start, 0); */
  565. #endif /* CONFIG_KEXEC */