cache_init.S 11 KB

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