cache_init.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Cache-handling routined for MIPS CPUs
  4. *
  5. * Copyright (c) 2003 Wolfgang Denk <wd@denx.de>
  6. */
  7. #include <asm-offsets.h>
  8. #include <config.h>
  9. #include <asm/asm.h>
  10. #include <asm/regdef.h>
  11. #include <asm/mipsregs.h>
  12. #include <asm/addrspace.h>
  13. #include <asm/cacheops.h>
  14. #include <asm/cm.h>
  15. #ifndef CONFIG_SYS_MIPS_CACHE_MODE
  16. #define CONFIG_SYS_MIPS_CACHE_MODE CONF_CM_CACHABLE_NONCOHERENT
  17. #endif
  18. #define INDEX_BASE CKSEG0
  19. .macro f_fill64 dst, offset, val
  20. LONG_S \val, (\offset + 0 * LONGSIZE)(\dst)
  21. LONG_S \val, (\offset + 1 * LONGSIZE)(\dst)
  22. LONG_S \val, (\offset + 2 * LONGSIZE)(\dst)
  23. LONG_S \val, (\offset + 3 * LONGSIZE)(\dst)
  24. LONG_S \val, (\offset + 4 * LONGSIZE)(\dst)
  25. LONG_S \val, (\offset + 5 * LONGSIZE)(\dst)
  26. LONG_S \val, (\offset + 6 * LONGSIZE)(\dst)
  27. LONG_S \val, (\offset + 7 * LONGSIZE)(\dst)
  28. #if LONGSIZE == 4
  29. LONG_S \val, (\offset + 8 * LONGSIZE)(\dst)
  30. LONG_S \val, (\offset + 9 * LONGSIZE)(\dst)
  31. LONG_S \val, (\offset + 10 * LONGSIZE)(\dst)
  32. LONG_S \val, (\offset + 11 * LONGSIZE)(\dst)
  33. LONG_S \val, (\offset + 12 * LONGSIZE)(\dst)
  34. LONG_S \val, (\offset + 13 * LONGSIZE)(\dst)
  35. LONG_S \val, (\offset + 14 * LONGSIZE)(\dst)
  36. LONG_S \val, (\offset + 15 * LONGSIZE)(\dst)
  37. #endif
  38. .endm
  39. .macro cache_loop curr, end, line_sz, op
  40. 10: cache \op, 0(\curr)
  41. PTR_ADDU \curr, \curr, \line_sz
  42. bne \curr, \end, 10b
  43. .endm
  44. .macro l1_info sz, line_sz, off
  45. .set push
  46. .set noat
  47. mfc0 $1, CP0_CONFIG, 1
  48. /* detect line size */
  49. srl \line_sz, $1, \off + MIPS_CONF1_DL_SHF - MIPS_CONF1_DA_SHF
  50. andi \line_sz, \line_sz, (MIPS_CONF1_DL >> MIPS_CONF1_DL_SHF)
  51. move \sz, zero
  52. beqz \line_sz, 10f
  53. li \sz, 2
  54. sllv \line_sz, \sz, \line_sz
  55. /* detect associativity */
  56. srl \sz, $1, \off + MIPS_CONF1_DA_SHF - MIPS_CONF1_DA_SHF
  57. andi \sz, \sz, (MIPS_CONF1_DA >> MIPS_CONF1_DA_SHF)
  58. addiu \sz, \sz, 1
  59. /* sz *= line_sz */
  60. mul \sz, \sz, \line_sz
  61. /* detect log32(sets) */
  62. srl $1, $1, \off + MIPS_CONF1_DS_SHF - MIPS_CONF1_DA_SHF
  63. andi $1, $1, (MIPS_CONF1_DS >> MIPS_CONF1_DS_SHF)
  64. addiu $1, $1, 1
  65. andi $1, $1, 0x7
  66. /* sz <<= log32(sets) */
  67. sllv \sz, \sz, $1
  68. /* sz *= 32 */
  69. li $1, 32
  70. mul \sz, \sz, $1
  71. 10:
  72. .set pop
  73. .endm
  74. /*
  75. * mips_cache_reset - low level initialisation of the primary caches
  76. *
  77. * This routine initialises the primary caches to ensure that they have good
  78. * parity. It must be called by the ROM before any cached locations are used
  79. * to prevent the possibility of data with bad parity being written to memory.
  80. *
  81. * To initialise the instruction cache it is essential that a source of data
  82. * with good parity is available. This routine will initialise an area of
  83. * memory starting at location zero to be used as a source of parity.
  84. *
  85. * Note that this function does not follow the standard calling convention &
  86. * may clobber typically callee-saved registers.
  87. *
  88. * RETURNS: N/A
  89. *
  90. */
  91. #define R_RETURN s0
  92. #define R_IC_SIZE s1
  93. #define R_IC_LINE s2
  94. #define R_DC_SIZE s3
  95. #define R_DC_LINE s4
  96. #define R_L2_SIZE s5
  97. #define R_L2_LINE s6
  98. #define R_L2_BYPASSED s7
  99. #define R_L2_L2C t8
  100. LEAF(mips_cache_reset)
  101. move R_RETURN, ra
  102. #ifdef CONFIG_MIPS_L2_CACHE
  103. /*
  104. * For there to be an L2 present, Config2 must be present. If it isn't
  105. * then we proceed knowing there's no L2 cache.
  106. */
  107. move R_L2_SIZE, zero
  108. move R_L2_LINE, zero
  109. move R_L2_BYPASSED, zero
  110. move R_L2_L2C, zero
  111. mfc0 t0, CP0_CONFIG, 1
  112. bgez t0, l2_probe_done
  113. /*
  114. * From MIPSr6 onwards the L2 cache configuration might not be reported
  115. * by Config2. The Config5.L2C bit indicates whether this is the case,
  116. * and if it is then we need knowledge of where else to look. For cores
  117. * from Imagination Technologies this is a CM GCR.
  118. */
  119. # if __mips_isa_rev >= 6
  120. /* Check that Config5 exists */
  121. mfc0 t0, CP0_CONFIG, 2
  122. bgez t0, l2_probe_cop0
  123. mfc0 t0, CP0_CONFIG, 3
  124. bgez t0, l2_probe_cop0
  125. mfc0 t0, CP0_CONFIG, 4
  126. bgez t0, l2_probe_cop0
  127. /* Check Config5.L2C is set */
  128. mfc0 t0, CP0_CONFIG, 5
  129. and R_L2_L2C, t0, MIPS_CONF5_L2C
  130. beqz R_L2_L2C, l2_probe_cop0
  131. /* Config5.L2C is set */
  132. # ifdef CONFIG_MIPS_CM
  133. /* The CM will provide L2 configuration */
  134. PTR_LI t0, CKSEG1ADDR(CONFIG_MIPS_CM_BASE)
  135. lw t1, GCR_L2_CONFIG(t0)
  136. bgez t1, l2_probe_done
  137. ext R_L2_LINE, t1, \
  138. GCR_L2_CONFIG_LINESZ_SHIFT, GCR_L2_CONFIG_LINESZ_BITS
  139. beqz R_L2_LINE, l2_probe_done
  140. li t2, 2
  141. sllv R_L2_LINE, t2, R_L2_LINE
  142. ext t2, t1, GCR_L2_CONFIG_ASSOC_SHIFT, GCR_L2_CONFIG_ASSOC_BITS
  143. addiu t2, t2, 1
  144. mul R_L2_SIZE, R_L2_LINE, t2
  145. ext t2, t1, GCR_L2_CONFIG_SETSZ_SHIFT, GCR_L2_CONFIG_SETSZ_BITS
  146. sllv R_L2_SIZE, R_L2_SIZE, t2
  147. li t2, 64
  148. mul R_L2_SIZE, R_L2_SIZE, t2
  149. /* Bypass the L2 cache so that we can init the L1s early */
  150. or t1, t1, GCR_L2_CONFIG_BYPASS
  151. sw t1, GCR_L2_CONFIG(t0)
  152. sync
  153. li R_L2_BYPASSED, 1
  154. /* Zero the L2 tag registers */
  155. sw zero, GCR_L2_TAG_ADDR(t0)
  156. sw zero, GCR_L2_TAG_ADDR_UPPER(t0)
  157. sw zero, GCR_L2_TAG_STATE(t0)
  158. sw zero, GCR_L2_TAG_STATE_UPPER(t0)
  159. sw zero, GCR_L2_DATA(t0)
  160. sw zero, GCR_L2_DATA_UPPER(t0)
  161. sync
  162. # else
  163. /* We don't know how to retrieve L2 configuration on this system */
  164. # endif
  165. b l2_probe_done
  166. # endif
  167. /*
  168. * For pre-r6 systems, or r6 systems with Config5.L2C==0, probe the L2
  169. * cache configuration from the cop0 Config2 register.
  170. */
  171. l2_probe_cop0:
  172. mfc0 t0, CP0_CONFIG, 2
  173. srl R_L2_LINE, t0, MIPS_CONF2_SL_SHF
  174. andi R_L2_LINE, R_L2_LINE, MIPS_CONF2_SL >> MIPS_CONF2_SL_SHF
  175. beqz R_L2_LINE, l2_probe_done
  176. li t1, 2
  177. sllv R_L2_LINE, t1, R_L2_LINE
  178. srl t1, t0, MIPS_CONF2_SA_SHF
  179. andi t1, t1, MIPS_CONF2_SA >> MIPS_CONF2_SA_SHF
  180. addiu t1, t1, 1
  181. mul R_L2_SIZE, R_L2_LINE, t1
  182. srl t1, t0, MIPS_CONF2_SS_SHF
  183. andi t1, t1, MIPS_CONF2_SS >> MIPS_CONF2_SS_SHF
  184. sllv R_L2_SIZE, R_L2_SIZE, t1
  185. li t1, 64
  186. mul R_L2_SIZE, R_L2_SIZE, t1
  187. /* Attempt to bypass the L2 so that we can init the L1s early */
  188. or t0, t0, MIPS_CONF2_L2B
  189. mtc0 t0, CP0_CONFIG, 2
  190. ehb
  191. mfc0 t0, CP0_CONFIG, 2
  192. and R_L2_BYPASSED, t0, MIPS_CONF2_L2B
  193. /* Zero the L2 tag registers */
  194. mtc0 zero, CP0_TAGLO, 4
  195. ehb
  196. l2_probe_done:
  197. #endif
  198. #ifndef CONFIG_SYS_CACHE_SIZE_AUTO
  199. li R_IC_SIZE, CONFIG_SYS_ICACHE_SIZE
  200. li R_IC_LINE, CONFIG_SYS_ICACHE_LINE_SIZE
  201. #else
  202. l1_info R_IC_SIZE, R_IC_LINE, MIPS_CONF1_IA_SHF
  203. #endif
  204. #ifndef CONFIG_SYS_CACHE_SIZE_AUTO
  205. li R_DC_SIZE, CONFIG_SYS_DCACHE_SIZE
  206. li R_DC_LINE, CONFIG_SYS_DCACHE_LINE_SIZE
  207. #else
  208. l1_info R_DC_SIZE, R_DC_LINE, MIPS_CONF1_DA_SHF
  209. #endif
  210. #ifdef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD
  211. /* Determine the largest L1 cache size */
  212. #ifndef CONFIG_SYS_CACHE_SIZE_AUTO
  213. #if CONFIG_SYS_ICACHE_SIZE > CONFIG_SYS_DCACHE_SIZE
  214. li v0, CONFIG_SYS_ICACHE_SIZE
  215. #else
  216. li v0, CONFIG_SYS_DCACHE_SIZE
  217. #endif
  218. #else
  219. move v0, R_IC_SIZE
  220. sltu t1, R_IC_SIZE, R_DC_SIZE
  221. movn v0, R_DC_SIZE, t1
  222. #endif
  223. /*
  224. * Now clear that much memory starting from zero.
  225. */
  226. PTR_LI a0, CKSEG1
  227. PTR_ADDU a1, a0, v0
  228. 2: PTR_ADDIU a0, 64
  229. f_fill64 a0, -64, zero
  230. bne a0, a1, 2b
  231. #endif /* CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD */
  232. #ifdef CONFIG_MIPS_L2_CACHE
  233. /*
  234. * If the L2 is bypassed, init the L1 first so that we can execute the
  235. * rest of the cache initialisation using the L1 instruction cache.
  236. */
  237. bnez R_L2_BYPASSED, l1_init
  238. l2_init:
  239. PTR_LI t0, INDEX_BASE
  240. PTR_ADDU t1, t0, R_L2_SIZE
  241. 1: cache INDEX_STORE_TAG_SD, 0(t0)
  242. PTR_ADDU t0, t0, R_L2_LINE
  243. bne t0, t1, 1b
  244. /*
  245. * If the L2 was bypassed then we already initialised the L1s before
  246. * the L2, so we are now done.
  247. */
  248. bnez R_L2_BYPASSED, l2_unbypass
  249. #endif
  250. /*
  251. * The TagLo registers used depend upon the CPU implementation, but the
  252. * architecture requires that it is safe for software to write to both
  253. * TagLo selects 0 & 2 covering supported cases.
  254. */
  255. l1_init:
  256. mtc0 zero, CP0_TAGLO
  257. mtc0 zero, CP0_TAGLO, 2
  258. ehb
  259. /*
  260. * The caches are probably in an indeterminate state, so we force good
  261. * parity into them by doing an invalidate for each line. If
  262. * CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD is set then we'll proceed to
  263. * perform a load/fill & a further invalidate for each line, assuming
  264. * that the bottom of RAM (having just been cleared) will generate good
  265. * parity for the cache.
  266. */
  267. /*
  268. * Initialize the I-cache first,
  269. */
  270. blez R_IC_SIZE, 1f
  271. PTR_LI t0, INDEX_BASE
  272. PTR_ADDU t1, t0, R_IC_SIZE
  273. /* clear tag to invalidate */
  274. cache_loop t0, t1, R_IC_LINE, INDEX_STORE_TAG_I
  275. #ifdef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD
  276. /* fill once, so data field parity is correct */
  277. PTR_LI t0, INDEX_BASE
  278. cache_loop t0, t1, R_IC_LINE, FILL
  279. /* invalidate again - prudent but not strictly neccessary */
  280. PTR_LI t0, INDEX_BASE
  281. cache_loop t0, t1, R_IC_LINE, INDEX_STORE_TAG_I
  282. #endif
  283. /* Enable use of the I-cache by setting Config.K0 */
  284. sync
  285. mfc0 t0, CP0_CONFIG
  286. li t1, CONFIG_SYS_MIPS_CACHE_MODE
  287. #if __mips_isa_rev >= 2
  288. ins t0, t1, 0, 3
  289. #else
  290. ori t0, t0, CONF_CM_CMASK
  291. xori t0, t0, CONF_CM_CMASK
  292. or t0, t0, t1
  293. #endif
  294. mtc0 t0, CP0_CONFIG
  295. /*
  296. * then initialize D-cache.
  297. */
  298. 1: blez R_DC_SIZE, 3f
  299. PTR_LI t0, INDEX_BASE
  300. PTR_ADDU t1, t0, R_DC_SIZE
  301. /* clear all tags */
  302. cache_loop t0, t1, R_DC_LINE, INDEX_STORE_TAG_D
  303. #ifdef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD
  304. /* load from each line (in cached space) */
  305. PTR_LI t0, INDEX_BASE
  306. 2: LONG_L zero, 0(t0)
  307. PTR_ADDU t0, R_DC_LINE
  308. bne t0, t1, 2b
  309. /* clear all tags */
  310. PTR_LI t0, INDEX_BASE
  311. cache_loop t0, t1, R_DC_LINE, INDEX_STORE_TAG_D
  312. #endif
  313. 3:
  314. #ifdef CONFIG_MIPS_L2_CACHE
  315. /* If the L2 isn't bypassed then we're done */
  316. beqz R_L2_BYPASSED, return
  317. /* The L2 is bypassed - go initialise it */
  318. b l2_init
  319. l2_unbypass:
  320. # if __mips_isa_rev >= 6
  321. beqz R_L2_L2C, 1f
  322. li t0, CKSEG1ADDR(CONFIG_MIPS_CM_BASE)
  323. lw t1, GCR_L2_CONFIG(t0)
  324. xor t1, t1, GCR_L2_CONFIG_BYPASS
  325. sw t1, GCR_L2_CONFIG(t0)
  326. sync
  327. ehb
  328. b 2f
  329. # endif
  330. 1: mfc0 t0, CP0_CONFIG, 2
  331. xor t0, t0, MIPS_CONF2_L2B
  332. mtc0 t0, CP0_CONFIG, 2
  333. ehb
  334. 2:
  335. # ifdef CONFIG_MIPS_CM
  336. /* Config3 must exist for a CM to be present */
  337. mfc0 t0, CP0_CONFIG, 1
  338. bgez t0, 2f
  339. mfc0 t0, CP0_CONFIG, 2
  340. bgez t0, 2f
  341. /* Check Config3.CMGCR to determine CM presence */
  342. mfc0 t0, CP0_CONFIG, 3
  343. and t0, t0, MIPS_CONF3_CMGCR
  344. beqz t0, 2f
  345. /* Change Config.K0 to a coherent CCA */
  346. mfc0 t0, CP0_CONFIG
  347. li t1, CONF_CM_CACHABLE_COW
  348. #if __mips_isa_rev >= 2
  349. ins t0, t1, 0, 3
  350. #else
  351. ori t0, t0, CONF_CM_CMASK
  352. xori t0, t0, CONF_CM_CMASK
  353. or t0, t0, t1
  354. #endif
  355. mtc0 t0, CP0_CONFIG
  356. /*
  357. * Join the coherent domain such that the caches of this core are kept
  358. * coherent with those of other cores.
  359. */
  360. PTR_LI t0, CKSEG1ADDR(CONFIG_MIPS_CM_BASE)
  361. lw t1, GCR_REV(t0)
  362. li t2, GCR_REV_CM3
  363. li t3, GCR_Cx_COHERENCE_EN
  364. bge t1, t2, 1f
  365. li t3, GCR_Cx_COHERENCE_DOM_EN
  366. 1: sw t3, GCR_Cx_COHERENCE(t0)
  367. ehb
  368. 2:
  369. # endif
  370. #endif
  371. return:
  372. /* Ensure all cache operations complete before returning */
  373. sync
  374. jr ra
  375. END(mips_cache_reset)