cache-v7.S 12 KB

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