cache-v7.S 12 KB

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