l2cr.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. /* TODO: use HW flush assist when available */
  139. lis r4,0x0002
  140. mtctr r4
  141. li r4,0
  142. 1:
  143. lwzx r0,r0,r4
  144. addi r4,r4,32 /* Go to start of next cache line */
  145. bdnz 1b
  146. isync
  147. /* Now, flush the first 4MB of memory */
  148. lis r4,0x0002
  149. mtctr r4
  150. li r4,0
  151. sync
  152. 1:
  153. dcbf 0,r4
  154. addi r4,r4,32 /* Go to start of next cache line */
  155. bdnz 1b
  156. 2:
  157. /* Set up the L2CR configuration bits (and switch L2 off) */
  158. /* CPU errata: Make sure the mtspr below is already in the
  159. * L1 icache
  160. */
  161. b 20f
  162. .balign L1_CACHE_LINE_SIZE
  163. 22:
  164. sync
  165. mtspr SPRN_L2CR,r3
  166. sync
  167. b 23f
  168. 20:
  169. b 21f
  170. 21: sync
  171. isync
  172. b 22b
  173. 23:
  174. /* Perform a global invalidation */
  175. oris r3,r3,0x0020
  176. sync
  177. mtspr SPRN_L2CR,r3
  178. sync
  179. isync /* For errata */
  180. BEGIN_FTR_SECTION
  181. /* On the 7450, we wait for the L2I bit to clear......
  182. */
  183. 10: mfspr r3,SPRN_L2CR
  184. andis. r4,r3,0x0020
  185. bne 10b
  186. b 11f
  187. END_FTR_SECTION_IFSET(CPU_FTR_SPEC7450)
  188. /* Wait for the invalidation to complete */
  189. 3: mfspr r3,SPRN_L2CR
  190. rlwinm. r4,r3,0,31,31
  191. bne 3b
  192. 11: rlwinm r3,r3,0,11,9 /* Turn off the L2I bit */
  193. sync
  194. mtspr SPRN_L2CR,r3
  195. sync
  196. /* See if we need to enable the cache */
  197. cmplwi r5,0
  198. beq 4f
  199. /* Enable the cache */
  200. oris r3,r3,0x8000
  201. mtspr SPRN_L2CR,r3
  202. sync
  203. 4:
  204. /* Restore HID0[DPM] to whatever it was before */
  205. sync
  206. mtspr 1008,r8
  207. sync
  208. /* Restore MSR (restores EE and DR bits to original state) */
  209. SYNC
  210. mtmsr r7
  211. isync
  212. mtlr r9
  213. blr
  214. _GLOBAL(_get_L2CR)
  215. /* Return the L2CR contents */
  216. li r3,0
  217. BEGIN_FTR_SECTION
  218. mfspr r3,SPRN_L2CR
  219. END_FTR_SECTION_IFSET(CPU_FTR_L2CR)
  220. blr
  221. /*
  222. * Here is a similar routine for dealing with the L3 cache
  223. * on the 745x family of chips
  224. */
  225. _GLOBAL(_set_L3CR)
  226. /* Make sure this is a 745x chip */
  227. BEGIN_FTR_SECTION
  228. li r3,-1
  229. blr
  230. END_FTR_SECTION_IFCLR(CPU_FTR_L3CR)
  231. /* Turn off interrupts and data relocation. */
  232. mfmsr r7 /* Save MSR in r7 */
  233. rlwinm r4,r7,0,17,15
  234. rlwinm r4,r4,0,28,26 /* Turn off DR bit */
  235. sync
  236. mtmsr r4
  237. isync
  238. /* Stop DST streams */
  239. DSSALL
  240. sync
  241. /* Get the current enable bit of the L3CR into r4 */
  242. mfspr r4,SPRN_L3CR
  243. /* Tweak some bits */
  244. rlwinm r5,r3,0,0,0 /* r5 contains the new enable bit */
  245. rlwinm r3,r3,0,22,20 /* Turn off the invalidate bit */
  246. rlwinm r3,r3,0,2,31 /* Turn off the enable & PE bits */
  247. rlwinm r3,r3,0,5,3 /* Turn off the clken bit */
  248. /* Check to see if we need to flush */
  249. rlwinm. r4,r4,0,0,0
  250. beq 2f
  251. /* Flush the cache.
  252. */
  253. /* TODO: use HW flush assist */
  254. lis r4,0x0008
  255. mtctr r4
  256. li r4,0
  257. 1:
  258. lwzx r0,r0,r4
  259. dcbf 0,r4
  260. addi r4,r4,32 /* Go to start of next cache line */
  261. bdnz 1b
  262. 2:
  263. /* Set up the L3CR configuration bits (and switch L3 off) */
  264. sync
  265. mtspr SPRN_L3CR,r3
  266. sync
  267. oris r3,r3,L3CR_L3RES@h /* Set reserved bit 5 */
  268. mtspr SPRN_L3CR,r3
  269. sync
  270. oris r3,r3,L3CR_L3CLKEN@h /* Set clken */
  271. mtspr SPRN_L3CR,r3
  272. sync
  273. /* Wait for stabilize */
  274. li r0,256
  275. mtctr r0
  276. 1: bdnz 1b
  277. /* Perform a global invalidation */
  278. ori r3,r3,0x0400
  279. sync
  280. mtspr SPRN_L3CR,r3
  281. sync
  282. isync
  283. /* We wait for the L3I bit to clear...... */
  284. 10: mfspr r3,SPRN_L3CR
  285. andi. r4,r3,0x0400
  286. bne 10b
  287. /* Clear CLKEN */
  288. rlwinm r3,r3,0,5,3 /* Turn off the clken bit */
  289. mtspr SPRN_L3CR,r3
  290. sync
  291. /* Wait for stabilize */
  292. li r0,256
  293. mtctr r0
  294. 1: bdnz 1b
  295. /* See if we need to enable the cache */
  296. cmplwi r5,0
  297. beq 4f
  298. /* Enable the cache */
  299. oris r3,r3,(L3CR_L3E | L3CR_L3CLKEN)@h
  300. mtspr SPRN_L3CR,r3
  301. sync
  302. /* Wait for stabilize */
  303. li r0,256
  304. mtctr r0
  305. 1: bdnz 1b
  306. /* Restore MSR (restores EE and DR bits to original state) */
  307. 4: SYNC
  308. mtmsr r7
  309. isync
  310. blr
  311. _GLOBAL(_get_L3CR)
  312. /* Return the L3CR contents */
  313. li r3,0
  314. BEGIN_FTR_SECTION
  315. mfspr r3,SPRN_L3CR
  316. END_FTR_SECTION_IFSET(CPU_FTR_L3CR)
  317. blr
  318. /* --- End of PowerLogix code ---
  319. */
  320. /* flush_disable_L1() - Flush and disable L1 cache
  321. *
  322. * clobbers r0, r3, ctr, cr0
  323. * Must be called with interrupts disabled and MMU enabled.
  324. */
  325. _GLOBAL(__flush_disable_L1)
  326. /* Stop pending alitvec streams and memory accesses */
  327. BEGIN_FTR_SECTION
  328. DSSALL
  329. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  330. sync
  331. /* Load counter to 0x4000 cache lines (512k) and
  332. * load cache with datas
  333. */
  334. li r3,0x4000 /* 512kB / 32B */
  335. mtctr r3
  336. lis r3,KERNELBASE@h
  337. 1:
  338. lwz r0,0(r3)
  339. addi r3,r3,0x0020 /* Go to start of next cache line */
  340. bdnz 1b
  341. isync
  342. sync
  343. /* Now flush those cache lines */
  344. li r3,0x4000 /* 512kB / 32B */
  345. mtctr r3
  346. lis r3,KERNELBASE@h
  347. 1:
  348. dcbf 0,r3
  349. addi r3,r3,0x0020 /* Go to start of next cache line */
  350. bdnz 1b
  351. sync
  352. /* We can now disable the L1 cache (HID0:DCE, HID0:ICE) */
  353. mfspr r3,SPRN_HID0
  354. rlwinm r3,r3,0,18,15
  355. mtspr SPRN_HID0,r3
  356. sync
  357. isync
  358. blr
  359. /* inval_enable_L1 - Invalidate and enable L1 cache
  360. *
  361. * Assumes L1 is already disabled and MSR:EE is off
  362. *
  363. * clobbers r3
  364. */
  365. _GLOBAL(__inval_enable_L1)
  366. /* Enable and then Flash inval the instruction & data cache */
  367. mfspr r3,SPRN_HID0
  368. ori r3,r3, HID0_ICE|HID0_ICFI|HID0_DCE|HID0_DCI
  369. sync
  370. isync
  371. mtspr SPRN_HID0,r3
  372. xori r3,r3, HID0_ICFI|HID0_DCI
  373. mtspr SPRN_HID0,r3
  374. sync
  375. blr