cache-v7.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. movw r1, #0x7fff
  37. and r2, r1, r0, lsr #13
  38. movw 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 st
  56. isb
  57. ret 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. ret 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(mov r3, r0, lsr #20) @ move LoUIS into position
  84. ALT_UP( mov r3, r0, lsr #26) @ move LoUU into position
  85. ands r3, r3, #7 << 1 @ extract LoU*2 field from clidr
  86. bne start_flush_levels @ LoU != 0, start flushing
  87. #ifdef CONFIG_ARM_ERRATA_643719
  88. ALT_SMP(mrc p15, 0, r2, c0, c0, 0) @ read main ID register
  89. ALT_UP( ret lr) @ LoUU is zero, so nothing to do
  90. movw r1, #:lower16:(0x410fc090 >> 4) @ ID of ARM Cortex A9 r0p?
  91. movt r1, #:upper16:(0x410fc090 >> 4)
  92. teq r1, r2, lsr #4 @ test for errata affected core and if so...
  93. moveq r3, #1 << 1 @ fix LoUIS value
  94. beq start_flush_levels @ start flushing cache levels
  95. #endif
  96. ret lr
  97. ENDPROC(v7_flush_dcache_louis)
  98. /*
  99. * v7_flush_dcache_all()
  100. *
  101. * Flush the whole D-cache.
  102. *
  103. * Corrupted registers: r0-r7, r9-r11 (r6 only in Thumb mode)
  104. *
  105. * - mm - mm_struct describing address space
  106. */
  107. ENTRY(v7_flush_dcache_all)
  108. dmb @ ensure ordering with previous memory accesses
  109. mrc p15, 1, r0, c0, c0, 1 @ read clidr
  110. mov r3, r0, lsr #23 @ move LoC into position
  111. ands r3, r3, #7 << 1 @ extract LoC*2 from clidr
  112. beq finished @ if loc is 0, then no need to clean
  113. start_flush_levels:
  114. mov r10, #0 @ start clean at cache level 0
  115. flush_levels:
  116. add r2, r10, r10, lsr #1 @ work out 3x current cache level
  117. mov r1, r0, lsr r2 @ extract cache type bits from clidr
  118. and r1, r1, #7 @ mask of the bits for current cache only
  119. cmp r1, #2 @ see what cache we have at this level
  120. blt skip @ skip if no cache, or just i-cache
  121. #ifdef CONFIG_PREEMPT
  122. save_and_disable_irqs_notrace r9 @ make cssr&csidr read atomic
  123. #endif
  124. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  125. isb @ isb to sych the new cssr&csidr
  126. mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
  127. #ifdef CONFIG_PREEMPT
  128. restore_irqs_notrace r9
  129. #endif
  130. and r2, r1, #7 @ extract the length of the cache lines
  131. add r2, r2, #4 @ add 4 (line length offset)
  132. movw r4, #0x3ff
  133. ands r4, r4, r1, lsr #3 @ find maximum number on the way size
  134. clz r5, r4 @ find bit position of way size increment
  135. movw r7, #0x7fff
  136. ands r7, r7, r1, lsr #13 @ extract max number of the index size
  137. loop1:
  138. mov r9, r7 @ create working copy of max index
  139. loop2:
  140. ARM( orr r11, r10, r4, lsl r5 ) @ factor way and cache number into r11
  141. THUMB( lsl r6, r4, r5 )
  142. THUMB( orr r11, r10, r6 ) @ factor way and cache number into r11
  143. ARM( orr r11, r11, r9, lsl r2 ) @ factor index number into r11
  144. THUMB( lsl r6, r9, r2 )
  145. THUMB( orr r11, r11, r6 ) @ factor index number into r11
  146. mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
  147. subs r9, r9, #1 @ decrement the index
  148. bge loop2
  149. subs r4, r4, #1 @ decrement the way
  150. bge loop1
  151. skip:
  152. add r10, r10, #2 @ increment cache number
  153. cmp r3, r10
  154. bgt flush_levels
  155. finished:
  156. mov r10, #0 @ swith back to cache level 0
  157. mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
  158. dsb st
  159. isb
  160. ret lr
  161. ENDPROC(v7_flush_dcache_all)
  162. /*
  163. * v7_flush_cache_all()
  164. *
  165. * Flush the entire cache system.
  166. * The data cache flush is now achieved using atomic clean / invalidates
  167. * working outwards from L1 cache. This is done using Set/Way based cache
  168. * maintenance instructions.
  169. * The instruction cache can still be invalidated back to the point of
  170. * unification in a single instruction.
  171. *
  172. */
  173. ENTRY(v7_flush_kern_cache_all)
  174. ARM( stmfd sp!, {r4-r5, r7, r9-r11, lr} )
  175. THUMB( stmfd sp!, {r4-r7, r9-r11, lr} )
  176. bl v7_flush_dcache_all
  177. mov r0, #0
  178. ALT_SMP(mcr p15, 0, r0, c7, c1, 0) @ invalidate I-cache inner shareable
  179. ALT_UP(mcr p15, 0, r0, c7, c5, 0) @ I+BTB cache invalidate
  180. ARM( ldmfd sp!, {r4-r5, r7, r9-r11, lr} )
  181. THUMB( ldmfd sp!, {r4-r7, r9-r11, lr} )
  182. ret lr
  183. ENDPROC(v7_flush_kern_cache_all)
  184. /*
  185. * v7_flush_kern_cache_louis(void)
  186. *
  187. * Flush the data cache up to Level of Unification Inner Shareable.
  188. * Invalidate the I-cache to the point of unification.
  189. */
  190. ENTRY(v7_flush_kern_cache_louis)
  191. ARM( stmfd sp!, {r4-r5, r7, r9-r11, lr} )
  192. THUMB( stmfd sp!, {r4-r7, r9-r11, lr} )
  193. bl v7_flush_dcache_louis
  194. mov r0, #0
  195. ALT_SMP(mcr p15, 0, r0, c7, c1, 0) @ invalidate I-cache inner shareable
  196. ALT_UP(mcr p15, 0, r0, c7, c5, 0) @ I+BTB cache invalidate
  197. ARM( ldmfd sp!, {r4-r5, r7, r9-r11, lr} )
  198. THUMB( ldmfd sp!, {r4-r7, r9-r11, lr} )
  199. ret lr
  200. ENDPROC(v7_flush_kern_cache_louis)
  201. /*
  202. * v7_flush_cache_all()
  203. *
  204. * Flush all TLB entries in a particular address space
  205. *
  206. * - mm - mm_struct describing address space
  207. */
  208. ENTRY(v7_flush_user_cache_all)
  209. /*FALLTHROUGH*/
  210. /*
  211. * v7_flush_cache_range(start, end, flags)
  212. *
  213. * Flush a range of TLB entries in the specified address space.
  214. *
  215. * - start - start address (may not be aligned)
  216. * - end - end address (exclusive, may not be aligned)
  217. * - flags - vm_area_struct flags describing address space
  218. *
  219. * It is assumed that:
  220. * - we have a VIPT cache.
  221. */
  222. ENTRY(v7_flush_user_cache_range)
  223. ret lr
  224. ENDPROC(v7_flush_user_cache_all)
  225. ENDPROC(v7_flush_user_cache_range)
  226. /*
  227. * v7_coherent_kern_range(start,end)
  228. *
  229. * Ensure that the I and D caches are coherent within specified
  230. * region. This is typically used when code has been written to
  231. * a memory region, and will be executed.
  232. *
  233. * - start - virtual start address of region
  234. * - end - virtual end address of region
  235. *
  236. * It is assumed that:
  237. * - the Icache does not read data from the write buffer
  238. */
  239. ENTRY(v7_coherent_kern_range)
  240. /* FALLTHROUGH */
  241. /*
  242. * v7_coherent_user_range(start,end)
  243. *
  244. * Ensure that the I and D caches are coherent within specified
  245. * region. This is typically used when code has been written to
  246. * a memory region, and will be executed.
  247. *
  248. * - start - virtual start address of region
  249. * - end - virtual end address of region
  250. *
  251. * It is assumed that:
  252. * - the Icache does not read data from the write buffer
  253. */
  254. ENTRY(v7_coherent_user_range)
  255. UNWIND(.fnstart )
  256. dcache_line_size r2, r3
  257. sub r3, r2, #1
  258. bic r12, r0, r3
  259. #ifdef CONFIG_ARM_ERRATA_764369
  260. ALT_SMP(W(dsb))
  261. ALT_UP(W(nop))
  262. #endif
  263. 1:
  264. USER( mcr p15, 0, r12, c7, c11, 1 ) @ clean D line to the point of unification
  265. add r12, r12, r2
  266. cmp r12, r1
  267. blo 1b
  268. dsb ishst
  269. icache_line_size r2, r3
  270. sub r3, r2, #1
  271. bic r12, r0, r3
  272. 2:
  273. USER( mcr p15, 0, r12, c7, c5, 1 ) @ invalidate I line
  274. add r12, r12, r2
  275. cmp r12, r1
  276. blo 2b
  277. mov r0, #0
  278. ALT_SMP(mcr p15, 0, r0, c7, c1, 6) @ invalidate BTB Inner Shareable
  279. ALT_UP(mcr p15, 0, r0, c7, c5, 6) @ invalidate BTB
  280. dsb ishst
  281. isb
  282. ret lr
  283. /*
  284. * Fault handling for the cache operation above. If the virtual address in r0
  285. * isn't mapped, fail with -EFAULT.
  286. */
  287. 9001:
  288. #ifdef CONFIG_ARM_ERRATA_775420
  289. dsb
  290. #endif
  291. mov r0, #-EFAULT
  292. ret lr
  293. UNWIND(.fnend )
  294. ENDPROC(v7_coherent_kern_range)
  295. ENDPROC(v7_coherent_user_range)
  296. /*
  297. * v7_flush_kern_dcache_area(void *addr, size_t size)
  298. *
  299. * Ensure that the data held in the page kaddr is written back
  300. * to the page in question.
  301. *
  302. * - addr - kernel address
  303. * - size - region size
  304. */
  305. ENTRY(v7_flush_kern_dcache_area)
  306. dcache_line_size r2, r3
  307. add r1, r0, r1
  308. sub r3, r2, #1
  309. bic r0, r0, r3
  310. #ifdef CONFIG_ARM_ERRATA_764369
  311. ALT_SMP(W(dsb))
  312. ALT_UP(W(nop))
  313. #endif
  314. 1:
  315. mcr p15, 0, r0, c7, c14, 1 @ clean & invalidate D line / unified line
  316. add r0, r0, r2
  317. cmp r0, r1
  318. blo 1b
  319. dsb st
  320. ret lr
  321. ENDPROC(v7_flush_kern_dcache_area)
  322. /*
  323. * v7_dma_inv_range(start,end)
  324. *
  325. * Invalidate the data cache within the specified region; we will
  326. * be performing a DMA operation in this region and we want to
  327. * purge old data in the cache.
  328. *
  329. * - start - virtual start address of region
  330. * - end - virtual end address of region
  331. */
  332. v7_dma_inv_range:
  333. dcache_line_size r2, r3
  334. sub r3, r2, #1
  335. tst r0, r3
  336. bic r0, r0, r3
  337. #ifdef CONFIG_ARM_ERRATA_764369
  338. ALT_SMP(W(dsb))
  339. ALT_UP(W(nop))
  340. #endif
  341. mcrne p15, 0, r0, c7, c14, 1 @ clean & invalidate D / U line
  342. tst r1, r3
  343. bic r1, r1, r3
  344. mcrne p15, 0, r1, c7, c14, 1 @ clean & invalidate D / U line
  345. 1:
  346. mcr p15, 0, r0, c7, c6, 1 @ invalidate D / U line
  347. add r0, r0, r2
  348. cmp r0, r1
  349. blo 1b
  350. dsb st
  351. ret lr
  352. ENDPROC(v7_dma_inv_range)
  353. /*
  354. * v7_dma_clean_range(start,end)
  355. * - start - virtual start address of region
  356. * - end - virtual end address of region
  357. */
  358. v7_dma_clean_range:
  359. dcache_line_size r2, r3
  360. sub r3, r2, #1
  361. bic r0, r0, r3
  362. #ifdef CONFIG_ARM_ERRATA_764369
  363. ALT_SMP(W(dsb))
  364. ALT_UP(W(nop))
  365. #endif
  366. 1:
  367. mcr p15, 0, r0, c7, c10, 1 @ clean D / U line
  368. add r0, r0, r2
  369. cmp r0, r1
  370. blo 1b
  371. dsb st
  372. ret lr
  373. ENDPROC(v7_dma_clean_range)
  374. /*
  375. * v7_dma_flush_range(start,end)
  376. * - start - virtual start address of region
  377. * - end - virtual end address of region
  378. */
  379. ENTRY(v7_dma_flush_range)
  380. dcache_line_size r2, r3
  381. sub r3, r2, #1
  382. bic r0, r0, r3
  383. #ifdef CONFIG_ARM_ERRATA_764369
  384. ALT_SMP(W(dsb))
  385. ALT_UP(W(nop))
  386. #endif
  387. 1:
  388. mcr p15, 0, r0, c7, c14, 1 @ clean & invalidate D / U line
  389. add r0, r0, r2
  390. cmp r0, r1
  391. blo 1b
  392. dsb st
  393. ret lr
  394. ENDPROC(v7_dma_flush_range)
  395. /*
  396. * dma_map_area(start, size, dir)
  397. * - start - kernel virtual start address
  398. * - size - size of region
  399. * - dir - DMA direction
  400. */
  401. ENTRY(v7_dma_map_area)
  402. add r1, r1, r0
  403. teq r2, #DMA_FROM_DEVICE
  404. beq v7_dma_inv_range
  405. b v7_dma_clean_range
  406. ENDPROC(v7_dma_map_area)
  407. /*
  408. * dma_unmap_area(start, size, dir)
  409. * - start - kernel virtual start address
  410. * - size - size of region
  411. * - dir - DMA direction
  412. */
  413. ENTRY(v7_dma_unmap_area)
  414. add r1, r1, r0
  415. teq r2, #DMA_TO_DEVICE
  416. bne v7_dma_inv_range
  417. ret lr
  418. ENDPROC(v7_dma_unmap_area)
  419. __INITDATA
  420. @ define struct cpu_cache_fns (see <asm/cacheflush.h> and proc-macros.S)
  421. define_cache_functions v7