l2cr.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. L2CR functions
  3. Copyright © 1997-1998 by PowerLogix R & D, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. /*
  17. Thur, Dec. 12, 1998.
  18. - First public release, contributed by PowerLogix.
  19. ***********
  20. Sat, Aug. 7, 1999.
  21. - Terry: Made sure code disabled interrupts before running. (Previously
  22. it was assumed interrupts were already disabled).
  23. - Terry: Updated for tentative G4 support. 4MB of memory is now flushed
  24. instead of 2MB. (Prob. only 3 is necessary).
  25. - Terry: Updated for workaround to HID0[DPM] processor bug
  26. during global invalidates.
  27. ***********
  28. Thu, July 13, 2000.
  29. - Terry: Added isync to correct for an errata.
  30. 22 August 2001.
  31. - DanM: Finally added the 7450 patch I've had for the past
  32. several months. The L2CR is similar, but I'm going
  33. to assume the user of this functions knows what they
  34. are doing.
  35. Author: Terry Greeniaus (tgree@phys.ualberta.ca)
  36. Please e-mail updates to this file to me, thanks!
  37. */
  38. #include <linux/config.h>
  39. #include <asm/processor.h>
  40. #include <asm/cputable.h>
  41. #include <asm/ppc_asm.h>
  42. #include <asm/cache.h>
  43. #include <asm/page.h>
  44. /* Usage:
  45. When setting the L2CR register, you must do a few special
  46. things. If you are enabling the cache, you must perform a
  47. global invalidate. If you are disabling the cache, you must
  48. flush the cache contents first. This routine takes care of
  49. doing these things. When first enabling the cache, make sure
  50. you pass in the L2CR you want, as well as passing in the
  51. global invalidate bit set. A global invalidate will only be
  52. performed if the L2I bit is set in applyThis. When enabling
  53. the cache, you should also set the L2E bit in applyThis. If
  54. you want to modify the L2CR contents after the cache has been
  55. enabled, the recommended procedure is to first call
  56. __setL2CR(0) to disable the cache and then call it again with
  57. the new values for L2CR. Examples:
  58. _setL2CR(0) - disables the cache
  59. _setL2CR(0xB3A04000) - enables my G3 upgrade card:
  60. - L2E set to turn on the cache
  61. - L2SIZ set to 1MB
  62. - L2CLK set to 1:1
  63. - L2RAM set to pipelined synchronous late-write
  64. - L2I set to perform a global invalidation
  65. - L2OH set to 0.5 nS
  66. - L2DF set because this upgrade card
  67. requires it
  68. A similar call should work for your card. You need to know
  69. the correct setting for your card and then place them in the
  70. fields I have outlined above. Other fields support optional
  71. features, such as L2DO which caches only data, or L2TS which
  72. causes cache pushes from the L1 cache to go to the L2 cache
  73. instead of to main memory.
  74. IMPORTANT:
  75. Starting with the 7450, the bits in this register have moved
  76. or behave differently. The Enable, Parity Enable, Size,
  77. and L2 Invalidate are the only bits that have not moved.
  78. The size is read-only for these processors with internal L2
  79. cache, and the invalidate is a control as well as status.
  80. -- Dan
  81. */
  82. /*
  83. * Summary: this procedure ignores the L2I bit in the value passed in,
  84. * flushes the cache if it was already enabled, always invalidates the
  85. * cache, then enables the cache if the L2E bit is set in the value
  86. * passed in.
  87. * -- paulus.
  88. */
  89. _GLOBAL(_set_L2CR)
  90. /* Make sure this is a 750 or 7400 chip */
  91. BEGIN_FTR_SECTION
  92. li r3,-1
  93. blr
  94. END_FTR_SECTION_IFCLR(CPU_FTR_L2CR)
  95. mflr r9
  96. /* Stop DST streams */
  97. BEGIN_FTR_SECTION
  98. DSSALL
  99. sync
  100. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  101. /* Turn off interrupts and data relocation. */
  102. mfmsr r7 /* Save MSR in r7 */
  103. rlwinm r4,r7,0,17,15
  104. rlwinm r4,r4,0,28,26 /* Turn off DR bit */
  105. sync
  106. mtmsr r4
  107. isync
  108. /* Before we perform the global invalidation, we must disable dynamic
  109. * power management via HID0[DPM] to work around a processor bug where
  110. * DPM can possibly interfere with the state machine in the processor
  111. * that invalidates the L2 cache tags.
  112. */
  113. mfspr r8,SPRN_HID0 /* Save HID0 in r8 */
  114. rlwinm r4,r8,0,12,10 /* Turn off HID0[DPM] */
  115. sync
  116. mtspr SPRN_HID0,r4 /* Disable DPM */
  117. sync
  118. /* Get the current enable bit of the L2CR into r4 */
  119. mfspr r4,SPRN_L2CR
  120. /* Tweak some bits */
  121. rlwinm r5,r3,0,0,0 /* r5 contains the new enable bit */
  122. rlwinm r3,r3,0,11,9 /* Turn off the invalidate bit */
  123. rlwinm r3,r3,0,1,31 /* Turn off the enable bit */
  124. /* Check to see if we need to flush */
  125. rlwinm. r4,r4,0,0,0
  126. beq 2f
  127. /* Flush the cache. First, read the first 4MB of memory (physical) to
  128. * put new data in the cache. (Actually we only need
  129. * the size of the L2 cache plus the size of the L1 cache, but 4MB will
  130. * cover everything just to be safe).
  131. */
  132. /**** Might be a good idea to set L2DO here - to prevent instructions
  133. from getting into the cache. But since we invalidate
  134. the next time we enable the cache it doesn't really matter.
  135. Don't do this unless you accomodate all processor variations.
  136. The bit moved on the 7450.....
  137. ****/
  138. BEGIN_FTR_SECTION
  139. /* Disable L2 prefetch on some 745x and try to ensure
  140. * L2 prefetch engines are idle. As explained by errata
  141. * text, we can't be sure they are, we just hope very hard
  142. * that well be enough (sic !). At least I noticed Apple
  143. * doesn't even bother doing the dcbf's here...
  144. */
  145. mfspr r4,SPRN_MSSCR0
  146. rlwinm r4,r4,0,0,29
  147. sync
  148. mtspr SPRN_MSSCR0,r4
  149. sync
  150. isync
  151. lis r4,KERNELBASE@h
  152. dcbf 0,r4
  153. dcbf 0,r4
  154. dcbf 0,r4
  155. dcbf 0,r4
  156. END_FTR_SECTION_IFSET(CPU_FTR_SPEC7450)
  157. /* TODO: use HW flush assist when available */
  158. lis r4,0x0002
  159. mtctr r4
  160. li r4,0
  161. 1:
  162. lwzx r0,r0,r4
  163. addi r4,r4,32 /* Go to start of next cache line */
  164. bdnz 1b
  165. isync
  166. /* Now, flush the first 4MB of memory */
  167. lis r4,0x0002
  168. mtctr r4
  169. li r4,0
  170. sync
  171. 1:
  172. dcbf 0,r4
  173. addi r4,r4,32 /* Go to start of next cache line */
  174. bdnz 1b
  175. 2:
  176. /* Set up the L2CR configuration bits (and switch L2 off) */
  177. /* CPU errata: Make sure the mtspr below is already in the
  178. * L1 icache
  179. */
  180. b 20f
  181. .balign L1_CACHE_LINE_SIZE
  182. 22:
  183. sync
  184. mtspr SPRN_L2CR,r3
  185. sync
  186. b 23f
  187. 20:
  188. b 21f
  189. 21: sync
  190. isync
  191. b 22b
  192. 23:
  193. /* Perform a global invalidation */
  194. oris r3,r3,0x0020
  195. sync
  196. mtspr SPRN_L2CR,r3
  197. sync
  198. isync /* For errata */
  199. BEGIN_FTR_SECTION
  200. /* On the 7450, we wait for the L2I bit to clear......
  201. */
  202. 10: mfspr r3,SPRN_L2CR
  203. andis. r4,r3,0x0020
  204. bne 10b
  205. b 11f
  206. END_FTR_SECTION_IFSET(CPU_FTR_SPEC7450)
  207. /* Wait for the invalidation to complete */
  208. 3: mfspr r3,SPRN_L2CR
  209. rlwinm. r4,r3,0,31,31
  210. bne 3b
  211. 11: rlwinm r3,r3,0,11,9 /* Turn off the L2I bit */
  212. sync
  213. mtspr SPRN_L2CR,r3
  214. sync
  215. /* See if we need to enable the cache */
  216. cmplwi r5,0
  217. beq 4f
  218. /* Enable the cache */
  219. oris r3,r3,0x8000
  220. mtspr SPRN_L2CR,r3
  221. sync
  222. /* Enable L2 HW prefetch on 744x/745x */
  223. BEGIN_FTR_SECTION
  224. mfspr r3,SPRN_MSSCR0
  225. ori r3,r3,3
  226. sync
  227. mtspr SPRN_MSSCR0,r3
  228. sync
  229. isync
  230. END_FTR_SECTION_IFSET(CPU_FTR_SPEC7450)
  231. 4:
  232. /* Restore HID0[DPM] to whatever it was before */
  233. sync
  234. mtspr 1008,r8
  235. sync
  236. /* Restore MSR (restores EE and DR bits to original state) */
  237. SYNC
  238. mtmsr r7
  239. isync
  240. mtlr r9
  241. blr
  242. _GLOBAL(_get_L2CR)
  243. /* Return the L2CR contents */
  244. li r3,0
  245. BEGIN_FTR_SECTION
  246. mfspr r3,SPRN_L2CR
  247. END_FTR_SECTION_IFSET(CPU_FTR_L2CR)
  248. blr
  249. /*
  250. * Here is a similar routine for dealing with the L3 cache
  251. * on the 745x family of chips
  252. */
  253. _GLOBAL(_set_L3CR)
  254. /* Make sure this is a 745x chip */
  255. BEGIN_FTR_SECTION
  256. li r3,-1
  257. blr
  258. END_FTR_SECTION_IFCLR(CPU_FTR_L3CR)
  259. /* Turn off interrupts and data relocation. */
  260. mfmsr r7 /* Save MSR in r7 */
  261. rlwinm r4,r7,0,17,15
  262. rlwinm r4,r4,0,28,26 /* Turn off DR bit */
  263. sync
  264. mtmsr r4
  265. isync
  266. /* Stop DST streams */
  267. DSSALL
  268. sync
  269. /* Get the current enable bit of the L3CR into r4 */
  270. mfspr r4,SPRN_L3CR
  271. /* Tweak some bits */
  272. rlwinm r5,r3,0,0,0 /* r5 contains the new enable bit */
  273. rlwinm r3,r3,0,22,20 /* Turn off the invalidate bit */
  274. rlwinm r3,r3,0,2,31 /* Turn off the enable & PE bits */
  275. rlwinm r3,r3,0,5,3 /* Turn off the clken bit */
  276. /* Check to see if we need to flush */
  277. rlwinm. r4,r4,0,0,0
  278. beq 2f
  279. /* Flush the cache.
  280. */
  281. /* TODO: use HW flush assist */
  282. lis r4,0x0008
  283. mtctr r4
  284. li r4,0
  285. 1:
  286. lwzx r0,r0,r4
  287. dcbf 0,r4
  288. addi r4,r4,32 /* Go to start of next cache line */
  289. bdnz 1b
  290. 2:
  291. /* Set up the L3CR configuration bits (and switch L3 off) */
  292. sync
  293. mtspr SPRN_L3CR,r3
  294. sync
  295. oris r3,r3,L3CR_L3RES@h /* Set reserved bit 5 */
  296. mtspr SPRN_L3CR,r3
  297. sync
  298. oris r3,r3,L3CR_L3CLKEN@h /* Set clken */
  299. mtspr SPRN_L3CR,r3
  300. sync
  301. /* Wait for stabilize */
  302. li r0,256
  303. mtctr r0
  304. 1: bdnz 1b
  305. /* Perform a global invalidation */
  306. ori r3,r3,0x0400
  307. sync
  308. mtspr SPRN_L3CR,r3
  309. sync
  310. isync
  311. /* We wait for the L3I bit to clear...... */
  312. 10: mfspr r3,SPRN_L3CR
  313. andi. r4,r3,0x0400
  314. bne 10b
  315. /* Clear CLKEN */
  316. rlwinm r3,r3,0,5,3 /* Turn off the clken bit */
  317. mtspr SPRN_L3CR,r3
  318. sync
  319. /* Wait for stabilize */
  320. li r0,256
  321. mtctr r0
  322. 1: bdnz 1b
  323. /* See if we need to enable the cache */
  324. cmplwi r5,0
  325. beq 4f
  326. /* Enable the cache */
  327. oris r3,r3,(L3CR_L3E | L3CR_L3CLKEN)@h
  328. mtspr SPRN_L3CR,r3
  329. sync
  330. /* Wait for stabilize */
  331. li r0,256
  332. mtctr r0
  333. 1: bdnz 1b
  334. /* Restore MSR (restores EE and DR bits to original state) */
  335. 4: SYNC
  336. mtmsr r7
  337. isync
  338. blr
  339. _GLOBAL(_get_L3CR)
  340. /* Return the L3CR contents */
  341. li r3,0
  342. BEGIN_FTR_SECTION
  343. mfspr r3,SPRN_L3CR
  344. END_FTR_SECTION_IFSET(CPU_FTR_L3CR)
  345. blr
  346. /* --- End of PowerLogix code ---
  347. */
  348. /* flush_disable_L1() - Flush and disable L1 cache
  349. *
  350. * clobbers r0, r3, ctr, cr0
  351. * Must be called with interrupts disabled and MMU enabled.
  352. */
  353. _GLOBAL(__flush_disable_L1)
  354. /* Stop pending alitvec streams and memory accesses */
  355. BEGIN_FTR_SECTION
  356. DSSALL
  357. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  358. sync
  359. /* Load counter to 0x4000 cache lines (512k) and
  360. * load cache with datas
  361. */
  362. li r3,0x4000 /* 512kB / 32B */
  363. mtctr r3
  364. lis r3,KERNELBASE@h
  365. 1:
  366. lwz r0,0(r3)
  367. addi r3,r3,0x0020 /* Go to start of next cache line */
  368. bdnz 1b
  369. isync
  370. sync
  371. /* Now flush those cache lines */
  372. li r3,0x4000 /* 512kB / 32B */
  373. mtctr r3
  374. lis r3,KERNELBASE@h
  375. 1:
  376. dcbf 0,r3
  377. addi r3,r3,0x0020 /* Go to start of next cache line */
  378. bdnz 1b
  379. sync
  380. /* We can now disable the L1 cache (HID0:DCE, HID0:ICE) */
  381. mfspr r3,SPRN_HID0
  382. rlwinm r3,r3,0,18,15
  383. mtspr SPRN_HID0,r3
  384. sync
  385. isync
  386. blr
  387. /* inval_enable_L1 - Invalidate and enable L1 cache
  388. *
  389. * Assumes L1 is already disabled and MSR:EE is off
  390. *
  391. * clobbers r3
  392. */
  393. _GLOBAL(__inval_enable_L1)
  394. /* Enable and then Flash inval the instruction & data cache */
  395. mfspr r3,SPRN_HID0
  396. ori r3,r3, HID0_ICE|HID0_ICFI|HID0_DCE|HID0_DCI
  397. sync
  398. isync
  399. mtspr SPRN_HID0,r3
  400. xori r3,r3, HID0_ICFI|HID0_DCI
  401. mtspr SPRN_HID0,r3
  402. sync
  403. blr