pmac_cache.S 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * This file contains low-level cache management functions
  3. * used for sleep and CPU speed changes on Apple machines.
  4. * (In fact the only thing that is Apple-specific is that we assume
  5. * that we can read from ROM at physical address 0xfff00000.)
  6. *
  7. * Copyright (C) 2004 Paul Mackerras (paulus@samba.org) and
  8. * Benjamin Herrenschmidt (benh@kernel.crashing.org)
  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/config.h>
  17. #include <asm/processor.h>
  18. #include <asm/ppc_asm.h>
  19. #include <asm/cputable.h>
  20. /*
  21. * Flush and disable all data caches (dL1, L2, L3). This is used
  22. * when going to sleep, when doing a PMU based cpufreq transition,
  23. * or when "offlining" a CPU on SMP machines. This code is over
  24. * paranoid, but I've had enough issues with various CPU revs and
  25. * bugs that I decided it was worth beeing over cautious
  26. */
  27. _GLOBAL(flush_disable_caches)
  28. #ifndef CONFIG_6xx
  29. blr
  30. #else
  31. BEGIN_FTR_SECTION
  32. b flush_disable_745x
  33. END_FTR_SECTION_IFSET(CPU_FTR_SPEC7450)
  34. BEGIN_FTR_SECTION
  35. b flush_disable_75x
  36. END_FTR_SECTION_IFSET(CPU_FTR_L2CR)
  37. b __flush_disable_L1
  38. /* This is the code for G3 and 74[01]0 */
  39. flush_disable_75x:
  40. mflr r10
  41. /* Turn off EE and DR in MSR */
  42. mfmsr r11
  43. rlwinm r0,r11,0,~MSR_EE
  44. rlwinm r0,r0,0,~MSR_DR
  45. sync
  46. mtmsr r0
  47. isync
  48. /* Stop DST streams */
  49. BEGIN_FTR_SECTION
  50. DSSALL
  51. sync
  52. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  53. /* Stop DPM */
  54. mfspr r8,SPRN_HID0 /* Save SPRN_HID0 in r8 */
  55. rlwinm r4,r8,0,12,10 /* Turn off HID0[DPM] */
  56. sync
  57. mtspr SPRN_HID0,r4 /* Disable DPM */
  58. sync
  59. /* Disp-flush L1. We have a weird problem here that I never
  60. * totally figured out. On 750FX, using the ROM for the flush
  61. * results in a non-working flush. We use that workaround for
  62. * now until I finally understand what's going on. --BenH
  63. */
  64. /* ROM base by default */
  65. lis r4,0xfff0
  66. mfpvr r3
  67. srwi r3,r3,16
  68. cmplwi cr0,r3,0x7000
  69. bne+ 1f
  70. /* RAM base on 750FX */
  71. li r4,0
  72. 1: li r4,0x4000
  73. mtctr r4
  74. 1: lwz r0,0(r4)
  75. addi r4,r4,32
  76. bdnz 1b
  77. sync
  78. isync
  79. /* Disable / invalidate / enable L1 data */
  80. mfspr r3,SPRN_HID0
  81. rlwinm r3,r3,0,~(HID0_DCE | HID0_ICE)
  82. mtspr SPRN_HID0,r3
  83. sync
  84. isync
  85. ori r3,r3,(HID0_DCE|HID0_DCI|HID0_ICE|HID0_ICFI)
  86. sync
  87. isync
  88. mtspr SPRN_HID0,r3
  89. xori r3,r3,(HID0_DCI|HID0_ICFI)
  90. mtspr SPRN_HID0,r3
  91. sync
  92. /* Get the current enable bit of the L2CR into r4 */
  93. mfspr r5,SPRN_L2CR
  94. /* Set to data-only (pre-745x bit) */
  95. oris r3,r5,L2CR_L2DO@h
  96. b 2f
  97. /* When disabling L2, code must be in L1 */
  98. .balign 32
  99. 1: mtspr SPRN_L2CR,r3
  100. 3: sync
  101. isync
  102. b 1f
  103. 2: b 3f
  104. 3: sync
  105. isync
  106. b 1b
  107. 1: /* disp-flush L2. The interesting thing here is that the L2 can be
  108. * up to 2Mb ... so using the ROM, we'll end up wrapping back to memory
  109. * but that is probbaly fine. We disp-flush over 4Mb to be safe
  110. */
  111. lis r4,2
  112. mtctr r4
  113. lis r4,0xfff0
  114. 1: lwz r0,0(r4)
  115. addi r4,r4,32
  116. bdnz 1b
  117. sync
  118. isync
  119. lis r4,2
  120. mtctr r4
  121. lis r4,0xfff0
  122. 1: dcbf 0,r4
  123. addi r4,r4,32
  124. bdnz 1b
  125. sync
  126. isync
  127. /* now disable L2 */
  128. rlwinm r5,r5,0,~L2CR_L2E
  129. b 2f
  130. /* When disabling L2, code must be in L1 */
  131. .balign 32
  132. 1: mtspr SPRN_L2CR,r5
  133. 3: sync
  134. isync
  135. b 1f
  136. 2: b 3f
  137. 3: sync
  138. isync
  139. b 1b
  140. 1: sync
  141. isync
  142. /* Invalidate L2. This is pre-745x, we clear the L2I bit ourselves */
  143. oris r4,r5,L2CR_L2I@h
  144. mtspr SPRN_L2CR,r4
  145. sync
  146. isync
  147. /* Wait for the invalidation to complete */
  148. 1: mfspr r3,SPRN_L2CR
  149. rlwinm. r0,r3,0,31,31
  150. bne 1b
  151. /* Clear L2I */
  152. xoris r4,r4,L2CR_L2I@h
  153. sync
  154. mtspr SPRN_L2CR,r4
  155. sync
  156. /* now disable the L1 data cache */
  157. mfspr r0,SPRN_HID0
  158. rlwinm r0,r0,0,~(HID0_DCE|HID0_ICE)
  159. mtspr SPRN_HID0,r0
  160. sync
  161. isync
  162. /* Restore HID0[DPM] to whatever it was before */
  163. sync
  164. mfspr r0,SPRN_HID0
  165. rlwimi r0,r8,0,11,11 /* Turn back HID0[DPM] */
  166. mtspr SPRN_HID0,r0
  167. sync
  168. /* restore DR and EE */
  169. sync
  170. mtmsr r11
  171. isync
  172. mtlr r10
  173. blr
  174. /* This code is for 745x processors */
  175. flush_disable_745x:
  176. /* Turn off EE and DR in MSR */
  177. mfmsr r11
  178. rlwinm r0,r11,0,~MSR_EE
  179. rlwinm r0,r0,0,~MSR_DR
  180. sync
  181. mtmsr r0
  182. isync
  183. /* Stop prefetch streams */
  184. DSSALL
  185. sync
  186. /* Disable L2 prefetching */
  187. mfspr r0,SPRN_MSSCR0
  188. rlwinm r0,r0,0,0,29
  189. mtspr SPRN_MSSCR0,r0
  190. sync
  191. isync
  192. lis r4,0
  193. dcbf 0,r4
  194. dcbf 0,r4
  195. dcbf 0,r4
  196. dcbf 0,r4
  197. dcbf 0,r4
  198. dcbf 0,r4
  199. dcbf 0,r4
  200. dcbf 0,r4
  201. /* Due to a bug with the HW flush on some CPU revs, we occasionally
  202. * experience data corruption. I'm adding a displacement flush along
  203. * with a dcbf loop over a few Mb to "help". The problem isn't totally
  204. * fixed by this in theory, but at least, in practice, I couldn't reproduce
  205. * it even with a big hammer...
  206. */
  207. lis r4,0x0002
  208. mtctr r4
  209. li r4,0
  210. 1:
  211. lwz r0,0(r4)
  212. addi r4,r4,32 /* Go to start of next cache line */
  213. bdnz 1b
  214. isync
  215. /* Now, flush the first 4MB of memory */
  216. lis r4,0x0002
  217. mtctr r4
  218. li r4,0
  219. sync
  220. 1:
  221. dcbf 0,r4
  222. addi r4,r4,32 /* Go to start of next cache line */
  223. bdnz 1b
  224. /* Flush and disable the L1 data cache */
  225. mfspr r6,SPRN_LDSTCR
  226. lis r3,0xfff0 /* read from ROM for displacement flush */
  227. li r4,0xfe /* start with only way 0 unlocked */
  228. li r5,128 /* 128 lines in each way */
  229. 1: mtctr r5
  230. rlwimi r6,r4,0,24,31
  231. mtspr SPRN_LDSTCR,r6
  232. sync
  233. isync
  234. 2: lwz r0,0(r3) /* touch each cache line */
  235. addi r3,r3,32
  236. bdnz 2b
  237. rlwinm r4,r4,1,24,30 /* move on to the next way */
  238. ori r4,r4,1
  239. cmpwi r4,0xff /* all done? */
  240. bne 1b
  241. /* now unlock the L1 data cache */
  242. li r4,0
  243. rlwimi r6,r4,0,24,31
  244. sync
  245. mtspr SPRN_LDSTCR,r6
  246. sync
  247. isync
  248. /* Flush the L2 cache using the hardware assist */
  249. mfspr r3,SPRN_L2CR
  250. cmpwi r3,0 /* check if it is enabled first */
  251. bge 4f
  252. oris r0,r3,(L2CR_L2IO_745x|L2CR_L2DO_745x)@h
  253. b 2f
  254. /* When disabling/locking L2, code must be in L1 */
  255. .balign 32
  256. 1: mtspr SPRN_L2CR,r0 /* lock the L2 cache */
  257. 3: sync
  258. isync
  259. b 1f
  260. 2: b 3f
  261. 3: sync
  262. isync
  263. b 1b
  264. 1: sync
  265. isync
  266. ori r0,r3,L2CR_L2HWF_745x
  267. sync
  268. mtspr SPRN_L2CR,r0 /* set the hardware flush bit */
  269. 3: mfspr r0,SPRN_L2CR /* wait for it to go to 0 */
  270. andi. r0,r0,L2CR_L2HWF_745x
  271. bne 3b
  272. sync
  273. rlwinm r3,r3,0,~L2CR_L2E
  274. b 2f
  275. /* When disabling L2, code must be in L1 */
  276. .balign 32
  277. 1: mtspr SPRN_L2CR,r3 /* disable the L2 cache */
  278. 3: sync
  279. isync
  280. b 1f
  281. 2: b 3f
  282. 3: sync
  283. isync
  284. b 1b
  285. 1: sync
  286. isync
  287. oris r4,r3,L2CR_L2I@h
  288. mtspr SPRN_L2CR,r4
  289. sync
  290. isync
  291. 1: mfspr r4,SPRN_L2CR
  292. andis. r0,r4,L2CR_L2I@h
  293. bne 1b
  294. sync
  295. BEGIN_FTR_SECTION
  296. /* Flush the L3 cache using the hardware assist */
  297. 4: mfspr r3,SPRN_L3CR
  298. cmpwi r3,0 /* check if it is enabled */
  299. bge 6f
  300. oris r0,r3,L3CR_L3IO@h
  301. ori r0,r0,L3CR_L3DO
  302. sync
  303. mtspr SPRN_L3CR,r0 /* lock the L3 cache */
  304. sync
  305. isync
  306. ori r0,r0,L3CR_L3HWF
  307. sync
  308. mtspr SPRN_L3CR,r0 /* set the hardware flush bit */
  309. 5: mfspr r0,SPRN_L3CR /* wait for it to go to zero */
  310. andi. r0,r0,L3CR_L3HWF
  311. bne 5b
  312. rlwinm r3,r3,0,~L3CR_L3E
  313. sync
  314. mtspr SPRN_L3CR,r3 /* disable the L3 cache */
  315. sync
  316. ori r4,r3,L3CR_L3I
  317. mtspr SPRN_L3CR,r4
  318. 1: mfspr r4,SPRN_L3CR
  319. andi. r0,r4,L3CR_L3I
  320. bne 1b
  321. sync
  322. END_FTR_SECTION_IFSET(CPU_FTR_L3CR)
  323. 6: mfspr r0,SPRN_HID0 /* now disable the L1 data cache */
  324. rlwinm r0,r0,0,~HID0_DCE
  325. mtspr SPRN_HID0,r0
  326. sync
  327. isync
  328. mtmsr r11 /* restore DR and EE */
  329. isync
  330. blr
  331. #endif /* CONFIG_6xx */