cache-v7.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * linux/arch/arm/mm/cache-v7.S
  4. *
  5. * Copyright (C) 2001 Deep Blue Solutions Ltd.
  6. * Copyright (C) 2005 ARM Ltd.
  7. *
  8. * This is the "shell" of the ARMv7 processor support.
  9. */
  10. #include <linux/linkage.h>
  11. #include <linux/init.h>
  12. #include <asm/assembler.h>
  13. #include <asm/errno.h>
  14. #include <asm/unwind.h>
  15. #include <asm/hardware/cache-b15-rac.h>
  16. #include "proc-macros.S"
  17. #ifdef CONFIG_CPU_ICACHE_MISMATCH_WORKAROUND
  18. .globl icache_size
  19. .data
  20. .align 2
  21. icache_size:
  22. .long 64
  23. .text
  24. #endif
  25. /*
  26. * The secondary kernel init calls v7_flush_dcache_all before it enables
  27. * the L1; however, the L1 comes out of reset in an undefined state, so
  28. * the clean + invalidate performed by v7_flush_dcache_all causes a bunch
  29. * of cache lines with uninitialized data and uninitialized tags to get
  30. * written out to memory, which does really unpleasant things to the main
  31. * processor. We fix this by performing an invalidate, rather than a
  32. * clean + invalidate, before jumping into the kernel.
  33. *
  34. * This function needs to be called for both secondary cores startup and
  35. * primary core resume procedures.
  36. */
  37. ENTRY(v7_invalidate_l1)
  38. mov r0, #0
  39. mcr p15, 2, r0, c0, c0, 0 @ select L1 data cache in CSSELR
  40. isb
  41. mrc p15, 1, r0, c0, c0, 0 @ read cache geometry from CCSIDR
  42. movw r3, #0x3ff
  43. and r3, r3, r0, lsr #3 @ 'Associativity' in CCSIDR[12:3]
  44. clz r1, r3 @ WayShift
  45. mov r2, #1
  46. mov r3, r3, lsl r1 @ NumWays-1 shifted into bits [31:...]
  47. movs r1, r2, lsl r1 @ #1 shifted left by same amount
  48. moveq r1, #1 @ r1 needs value > 0 even if only 1 way
  49. and r2, r0, #0x7
  50. add r2, r2, #4 @ SetShift
  51. 1: movw ip, #0x7fff
  52. and r0, ip, r0, lsr #13 @ 'NumSets' in CCSIDR[27:13]
  53. 2: mov ip, r0, lsl r2 @ NumSet << SetShift
  54. orr ip, ip, r3 @ Reg = (Temp<<WayShift)|(NumSets<<SetShift)
  55. mcr p15, 0, ip, c7, c6, 2
  56. subs r0, r0, #1 @ Set--
  57. bpl 2b
  58. subs r3, r3, r1 @ Way--
  59. bcc 3f
  60. mrc p15, 1, r0, c0, c0, 0 @ re-read cache geometry from CCSIDR
  61. b 1b
  62. 3: dsb st
  63. isb
  64. ret lr
  65. ENDPROC(v7_invalidate_l1)
  66. /*
  67. * v7_flush_icache_all()
  68. *
  69. * Flush the whole I-cache.
  70. *
  71. * Registers:
  72. * r0 - set to 0
  73. */
  74. ENTRY(v7_flush_icache_all)
  75. mov r0, #0
  76. ALT_SMP(mcr p15, 0, r0, c7, c1, 0) @ invalidate I-cache inner shareable
  77. ALT_UP(mcr p15, 0, r0, c7, c5, 0) @ I+BTB cache invalidate
  78. ret lr
  79. ENDPROC(v7_flush_icache_all)
  80. /*
  81. * v7_flush_dcache_louis()
  82. *
  83. * Flush the D-cache up to the Level of Unification Inner Shareable
  84. *
  85. * Corrupted registers: r0-r6, r9-r10
  86. */
  87. ENTRY(v7_flush_dcache_louis)
  88. dmb @ ensure ordering with previous memory accesses
  89. mrc p15, 1, r0, c0, c0, 1 @ read clidr, r0 = clidr
  90. ALT_SMP(mov r3, r0, lsr #20) @ move LoUIS into position
  91. ALT_UP( mov r3, r0, lsr #26) @ move LoUU into position
  92. ands r3, r3, #7 << 1 @ extract LoU*2 field from clidr
  93. bne start_flush_levels @ LoU != 0, start flushing
  94. #ifdef CONFIG_ARM_ERRATA_643719
  95. ALT_SMP(mrc p15, 0, r2, c0, c0, 0) @ read main ID register
  96. ALT_UP( ret lr) @ LoUU is zero, so nothing to do
  97. movw r1, #:lower16:(0x410fc090 >> 4) @ ID of ARM Cortex A9 r0p?
  98. movt r1, #:upper16:(0x410fc090 >> 4)
  99. teq r1, r2, lsr #4 @ test for errata affected core and if so...
  100. moveq r3, #1 << 1 @ fix LoUIS value
  101. beq start_flush_levels @ start flushing cache levels
  102. #endif
  103. ret lr
  104. ENDPROC(v7_flush_dcache_louis)
  105. /*
  106. * v7_flush_dcache_all()
  107. *
  108. * Flush the whole D-cache.
  109. *
  110. * Corrupted registers: r0-r6, r9-r10
  111. *
  112. * - mm - mm_struct describing address space
  113. */
  114. ENTRY(v7_flush_dcache_all)
  115. dmb @ ensure ordering with previous memory accesses
  116. mrc p15, 1, r0, c0, c0, 1 @ read clidr
  117. mov r3, r0, lsr #23 @ move LoC into position
  118. ands r3, r3, #7 << 1 @ extract LoC*2 from clidr
  119. beq finished @ if loc is 0, then no need to clean
  120. start_flush_levels:
  121. mov r10, #0 @ start clean at cache level 0
  122. flush_levels:
  123. add r2, r10, r10, lsr #1 @ work out 3x current cache level
  124. mov r1, r0, lsr r2 @ extract cache type bits from clidr
  125. and r1, r1, #7 @ mask of the bits for current cache only
  126. cmp r1, #2 @ see what cache we have at this level
  127. blt skip @ skip if no cache, or just i-cache
  128. #ifdef CONFIG_PREEMPTION
  129. save_and_disable_irqs_notrace r9 @ make cssr&csidr read atomic
  130. #endif
  131. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  132. isb @ isb to sych the new cssr&csidr
  133. mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
  134. #ifdef CONFIG_PREEMPTION
  135. restore_irqs_notrace r9
  136. #endif
  137. and r2, r1, #7 @ extract the length of the cache lines
  138. add r2, r2, #4 @ add 4 (line length offset)
  139. movw r4, #0x3ff
  140. ands r4, r4, r1, lsr #3 @ find maximum number on the way size
  141. clz r5, r4 @ find bit position of way size increment
  142. movw r6, #0x7fff
  143. and r1, r6, r1, lsr #13 @ extract max number of the index size
  144. mov r6, #1
  145. movne r4, r4, lsl r5 @ # of ways shifted into bits [31:...]
  146. movne r6, r6, lsl r5 @ 1 shifted left by same amount
  147. loop1:
  148. mov r9, r1 @ create working copy of max index
  149. loop2:
  150. mov r5, r9, lsl r2 @ factor set number into r5
  151. orr r5, r5, r4 @ factor way number into r5
  152. orr r5, r5, r10 @ factor cache level into r5
  153. mcr p15, 0, r5, c7, c14, 2 @ clean & invalidate by set/way
  154. subs r9, r9, #1 @ decrement the index
  155. bge loop2
  156. subs r4, r4, r6 @ decrement the way
  157. bcs loop1
  158. skip:
  159. add r10, r10, #2 @ increment cache number
  160. cmp r3, r10
  161. #ifdef CONFIG_ARM_ERRATA_814220
  162. dsb
  163. #endif
  164. bgt flush_levels
  165. finished:
  166. mov r10, #0 @ switch back to cache level 0
  167. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  168. dsb st
  169. isb
  170. ret lr
  171. ENDPROC(v7_flush_dcache_all)
  172. /*
  173. * v7_flush_cache_all()
  174. *
  175. * Flush the entire cache system.
  176. * The data cache flush is now achieved using atomic clean / invalidates
  177. * working outwards from L1 cache. This is done using Set/Way based cache
  178. * maintenance instructions.
  179. * The instruction cache can still be invalidated back to the point of
  180. * unification in a single instruction.
  181. *
  182. */
  183. ENTRY(v7_flush_kern_cache_all)
  184. stmfd sp!, {r4-r6, r9-r10, lr}
  185. bl v7_flush_dcache_all
  186. mov r0, #0
  187. ALT_SMP(mcr p15, 0, r0, c7, c1, 0) @ invalidate I-cache inner shareable
  188. ALT_UP(mcr p15, 0, r0, c7, c5, 0) @ I+BTB cache invalidate
  189. ldmfd sp!, {r4-r6, r9-r10, lr}
  190. ret lr
  191. ENDPROC(v7_flush_kern_cache_all)
  192. /*
  193. * v7_flush_kern_cache_louis(void)
  194. *
  195. * Flush the data cache up to Level of Unification Inner Shareable.
  196. * Invalidate the I-cache to the point of unification.
  197. */
  198. ENTRY(v7_flush_kern_cache_louis)
  199. stmfd sp!, {r4-r6, r9-r10, lr}
  200. bl v7_flush_dcache_louis
  201. mov r0, #0
  202. ALT_SMP(mcr p15, 0, r0, c7, c1, 0) @ invalidate I-cache inner shareable
  203. ALT_UP(mcr p15, 0, r0, c7, c5, 0) @ I+BTB cache invalidate
  204. ldmfd sp!, {r4-r6, r9-r10, lr}
  205. ret lr
  206. ENDPROC(v7_flush_kern_cache_louis)
  207. /*
  208. * v7_flush_cache_all()
  209. *
  210. * Flush all TLB entries in a particular address space
  211. *
  212. * - mm - mm_struct describing address space
  213. */
  214. ENTRY(v7_flush_user_cache_all)
  215. /*FALLTHROUGH*/
  216. /*
  217. * v7_flush_cache_range(start, end, flags)
  218. *
  219. * Flush a range of TLB entries in the specified address space.
  220. *
  221. * - start - start address (may not be aligned)
  222. * - end - end address (exclusive, may not be aligned)
  223. * - flags - vm_area_struct flags describing address space
  224. *
  225. * It is assumed that:
  226. * - we have a VIPT cache.
  227. */
  228. ENTRY(v7_flush_user_cache_range)
  229. ret lr
  230. ENDPROC(v7_flush_user_cache_all)
  231. ENDPROC(v7_flush_user_cache_range)
  232. /*
  233. * v7_coherent_kern_range(start,end)
  234. *
  235. * Ensure that the I and D caches are coherent within specified
  236. * region. This is typically used when code has been written to
  237. * a memory region, and will be executed.
  238. *
  239. * - start - virtual start address of region
  240. * - end - virtual end address of region
  241. *
  242. * It is assumed that:
  243. * - the Icache does not read data from the write buffer
  244. */
  245. ENTRY(v7_coherent_kern_range)
  246. /* FALLTHROUGH */
  247. /*
  248. * v7_coherent_user_range(start,end)
  249. *
  250. * Ensure that the I and D caches are coherent within specified
  251. * region. This is typically used when code has been written to
  252. * a memory region, and will be executed.
  253. *
  254. * - start - virtual start address of region
  255. * - end - virtual end address of region
  256. *
  257. * It is assumed that:
  258. * - the Icache does not read data from the write buffer
  259. */
  260. ENTRY(v7_coherent_user_range)
  261. UNWIND(.fnstart )
  262. dcache_line_size r2, r3
  263. sub r3, r2, #1
  264. bic r12, r0, r3
  265. #ifdef CONFIG_ARM_ERRATA_764369
  266. ALT_SMP(W(dsb))
  267. ALT_UP(W(nop))
  268. #endif
  269. 1:
  270. USER( mcr p15, 0, r12, c7, c11, 1 ) @ clean D line to the point of unification
  271. add r12, r12, r2
  272. cmp r12, r1
  273. blo 1b
  274. dsb ishst
  275. #ifdef CONFIG_CPU_ICACHE_MISMATCH_WORKAROUND
  276. ldr r3, =icache_size
  277. ldr r2, [r3, #0]
  278. #else
  279. icache_line_size r2, r3
  280. #endif
  281. sub r3, r2, #1
  282. bic r12, r0, r3
  283. 2:
  284. USER( mcr p15, 0, r12, c7, c5, 1 ) @ invalidate I line
  285. add r12, r12, r2
  286. cmp r12, r1
  287. blo 2b
  288. mov r0, #0
  289. ALT_SMP(mcr p15, 0, r0, c7, c1, 6) @ invalidate BTB Inner Shareable
  290. ALT_UP(mcr p15, 0, r0, c7, c5, 6) @ invalidate BTB
  291. dsb ishst
  292. isb
  293. ret lr
  294. /*
  295. * Fault handling for the cache operation above. If the virtual address in r0
  296. * isn't mapped, fail with -EFAULT.
  297. */
  298. 9001:
  299. #ifdef CONFIG_ARM_ERRATA_775420
  300. dsb
  301. #endif
  302. mov r0, #-EFAULT
  303. ret lr
  304. UNWIND(.fnend )
  305. ENDPROC(v7_coherent_kern_range)
  306. ENDPROC(v7_coherent_user_range)
  307. /*
  308. * v7_flush_kern_dcache_area(void *addr, size_t size)
  309. *
  310. * Ensure that the data held in the page kaddr is written back
  311. * to the page in question.
  312. *
  313. * - addr - kernel address
  314. * - size - region size
  315. */
  316. ENTRY(v7_flush_kern_dcache_area)
  317. dcache_line_size r2, r3
  318. add r1, r0, r1
  319. sub r3, r2, #1
  320. bic r0, r0, r3
  321. #ifdef CONFIG_ARM_ERRATA_764369
  322. ALT_SMP(W(dsb))
  323. ALT_UP(W(nop))
  324. #endif
  325. 1:
  326. mcr p15, 0, r0, c7, c14, 1 @ clean & invalidate D line / unified line
  327. add r0, r0, r2
  328. cmp r0, r1
  329. blo 1b
  330. dsb st
  331. ret lr
  332. ENDPROC(v7_flush_kern_dcache_area)
  333. /*
  334. * v7_dma_inv_range(start,end)
  335. *
  336. * Invalidate the data cache within the specified region; we will
  337. * be performing a DMA operation in this region and we want to
  338. * purge old data in the cache.
  339. *
  340. * - start - virtual start address of region
  341. * - end - virtual end address of region
  342. */
  343. v7_dma_inv_range:
  344. dcache_line_size r2, r3
  345. sub r3, r2, #1
  346. tst r0, r3
  347. bic r0, r0, r3
  348. #ifdef CONFIG_ARM_ERRATA_764369
  349. ALT_SMP(W(dsb))
  350. ALT_UP(W(nop))
  351. #endif
  352. mcrne p15, 0, r0, c7, c14, 1 @ clean & invalidate D / U line
  353. addne r0, r0, r2
  354. tst r1, r3
  355. bic r1, r1, r3
  356. mcrne p15, 0, r1, c7, c14, 1 @ clean & invalidate D / U line
  357. cmp r0, r1
  358. 1:
  359. mcrlo p15, 0, r0, c7, c6, 1 @ invalidate D / U line
  360. addlo r0, r0, r2
  361. cmplo r0, r1
  362. blo 1b
  363. dsb st
  364. ret lr
  365. ENDPROC(v7_dma_inv_range)
  366. /*
  367. * v7_dma_clean_range(start,end)
  368. * - start - virtual start address of region
  369. * - end - virtual end address of region
  370. */
  371. v7_dma_clean_range:
  372. dcache_line_size r2, r3
  373. sub r3, r2, #1
  374. bic r0, r0, r3
  375. #ifdef CONFIG_ARM_ERRATA_764369
  376. ALT_SMP(W(dsb))
  377. ALT_UP(W(nop))
  378. #endif
  379. 1:
  380. mcr p15, 0, r0, c7, c10, 1 @ clean D / U line
  381. add r0, r0, r2
  382. cmp r0, r1
  383. blo 1b
  384. dsb st
  385. ret lr
  386. ENDPROC(v7_dma_clean_range)
  387. /*
  388. * v7_dma_flush_range(start,end)
  389. * - start - virtual start address of region
  390. * - end - virtual end address of region
  391. */
  392. ENTRY(v7_dma_flush_range)
  393. dcache_line_size r2, r3
  394. sub r3, r2, #1
  395. bic r0, r0, r3
  396. #ifdef CONFIG_ARM_ERRATA_764369
  397. ALT_SMP(W(dsb))
  398. ALT_UP(W(nop))
  399. #endif
  400. 1:
  401. mcr p15, 0, r0, c7, c14, 1 @ clean & invalidate D / U line
  402. add r0, r0, r2
  403. cmp r0, r1
  404. blo 1b
  405. dsb st
  406. ret lr
  407. ENDPROC(v7_dma_flush_range)
  408. /*
  409. * dma_map_area(start, size, dir)
  410. * - start - kernel virtual start address
  411. * - size - size of region
  412. * - dir - DMA direction
  413. */
  414. ENTRY(v7_dma_map_area)
  415. add r1, r1, r0
  416. teq r2, #DMA_FROM_DEVICE
  417. beq v7_dma_inv_range
  418. b v7_dma_clean_range
  419. ENDPROC(v7_dma_map_area)
  420. /*
  421. * dma_unmap_area(start, size, dir)
  422. * - start - kernel virtual start address
  423. * - size - size of region
  424. * - dir - DMA direction
  425. */
  426. ENTRY(v7_dma_unmap_area)
  427. add r1, r1, r0
  428. teq r2, #DMA_TO_DEVICE
  429. bne v7_dma_inv_range
  430. ret lr
  431. ENDPROC(v7_dma_unmap_area)
  432. __INITDATA
  433. @ define struct cpu_cache_fns (see <asm/cacheflush.h> and proc-macros.S)
  434. define_cache_functions v7
  435. /* The Broadcom Brahma-B15 read-ahead cache requires some modifications
  436. * to the v7_cache_fns, we only override the ones we need
  437. */
  438. #ifndef CONFIG_CACHE_B15_RAC
  439. globl_equ b15_flush_kern_cache_all, v7_flush_kern_cache_all
  440. #endif
  441. globl_equ b15_flush_icache_all, v7_flush_icache_all
  442. globl_equ b15_flush_kern_cache_louis, v7_flush_kern_cache_louis
  443. globl_equ b15_flush_user_cache_all, v7_flush_user_cache_all
  444. globl_equ b15_flush_user_cache_range, v7_flush_user_cache_range
  445. globl_equ b15_coherent_kern_range, v7_coherent_kern_range
  446. globl_equ b15_coherent_user_range, v7_coherent_user_range
  447. globl_equ b15_flush_kern_dcache_area, v7_flush_kern_dcache_area
  448. globl_equ b15_dma_map_area, v7_dma_map_area
  449. globl_equ b15_dma_unmap_area, v7_dma_unmap_area
  450. globl_equ b15_dma_flush_range, v7_dma_flush_range
  451. define_cache_functions b15