misc_64.S 13 KB

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