l2cr_6xx.S 11 KB

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