cache-v7.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 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(ands r3, r0, #(7 << 21)) @ extract LoUIS from clidr
  84. ALT_UP(ands r3, r0, #(7 << 27)) @ extract LoUU from clidr
  85. #ifdef CONFIG_ARM_ERRATA_643719
  86. ALT_SMP(mrceq p15, 0, r2, c0, c0, 0) @ read main ID register
  87. ALT_UP(reteq lr) @ LoUU is zero, so nothing to do
  88. ldreq r1, =0x410fc090 @ ID of ARM Cortex A9 r0p?
  89. biceq r2, r2, #0x0000000f @ clear minor revision number
  90. teqeq r2, r1 @ test for errata affected core and if so...
  91. orreqs r3, #(1 << 21) @ fix LoUIS value (and set flags state to 'ne')
  92. #endif
  93. ALT_SMP(mov r3, r3, lsr #20) @ r3 = LoUIS * 2
  94. ALT_UP(mov r3, r3, lsr #26) @ r3 = LoUU * 2
  95. reteq lr @ return if level == 0
  96. mov r10, #0 @ r10 (starting level) = 0
  97. b flush_levels @ start flushing cache levels
  98. ENDPROC(v7_flush_dcache_louis)
  99. /*
  100. * v7_flush_dcache_all()
  101. *
  102. * Flush the whole D-cache.
  103. *
  104. * Corrupted registers: r0-r7, r9-r11 (r6 only in Thumb mode)
  105. *
  106. * - mm - mm_struct describing address space
  107. */
  108. ENTRY(v7_flush_dcache_all)
  109. dmb @ ensure ordering with previous memory accesses
  110. mrc p15, 1, r0, c0, c0, 1 @ read clidr
  111. ands r3, r0, #0x7000000 @ extract loc from clidr
  112. mov r3, r3, lsr #23 @ left align loc bit field
  113. beq finished @ if loc is 0, then no need to clean
  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. ldr 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. ldr 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. USER( mcr p15, 0, r12, c7, c11, 1 ) @ clean D line to the point of unification
  268. add r12, r12, r2
  269. cmp r12, r1
  270. blo 1b
  271. dsb ishst
  272. icache_line_size r2, r3
  273. sub r3, r2, #1
  274. bic r12, r0, r3
  275. 2:
  276. USER( mcr p15, 0, r12, c7, c5, 1 ) @ invalidate I line
  277. add r12, r12, r2
  278. cmp r12, r1
  279. blo 2b
  280. mov r0, #0
  281. ALT_SMP(mcr p15, 0, r0, c7, c1, 6) @ invalidate BTB Inner Shareable
  282. ALT_UP(mcr p15, 0, r0, c7, c5, 6) @ invalidate BTB
  283. dsb ishst
  284. isb
  285. ret lr
  286. /*
  287. * Fault handling for the cache operation above. If the virtual address in r0
  288. * isn't mapped, fail with -EFAULT.
  289. */
  290. 9001:
  291. #ifdef CONFIG_ARM_ERRATA_775420
  292. dsb
  293. #endif
  294. mov r0, #-EFAULT
  295. ret lr
  296. UNWIND(.fnend )
  297. ENDPROC(v7_coherent_kern_range)
  298. ENDPROC(v7_coherent_user_range)
  299. /*
  300. * v7_flush_kern_dcache_area(void *addr, size_t size)
  301. *
  302. * Ensure that the data held in the page kaddr is written back
  303. * to the page in question.
  304. *
  305. * - addr - kernel address
  306. * - size - region size
  307. */
  308. ENTRY(v7_flush_kern_dcache_area)
  309. dcache_line_size r2, r3
  310. add r1, r0, r1
  311. sub r3, r2, #1
  312. bic r0, r0, r3
  313. #ifdef CONFIG_ARM_ERRATA_764369
  314. ALT_SMP(W(dsb))
  315. ALT_UP(W(nop))
  316. #endif
  317. 1:
  318. mcr p15, 0, r0, c7, c14, 1 @ clean & invalidate D line / unified line
  319. add r0, r0, r2
  320. cmp r0, r1
  321. blo 1b
  322. dsb st
  323. ret lr
  324. ENDPROC(v7_flush_kern_dcache_area)
  325. /*
  326. * v7_dma_inv_range(start,end)
  327. *
  328. * Invalidate the data cache within the specified region; we will
  329. * be performing a DMA operation in this region and we want to
  330. * purge old data in the cache.
  331. *
  332. * - start - virtual start address of region
  333. * - end - virtual end address of region
  334. */
  335. v7_dma_inv_range:
  336. dcache_line_size r2, r3
  337. sub r3, r2, #1
  338. tst r0, r3
  339. bic r0, r0, r3
  340. #ifdef CONFIG_ARM_ERRATA_764369
  341. ALT_SMP(W(dsb))
  342. ALT_UP(W(nop))
  343. #endif
  344. mcrne p15, 0, r0, c7, c14, 1 @ clean & invalidate D / U line
  345. tst r1, r3
  346. bic r1, r1, r3
  347. mcrne p15, 0, r1, c7, c14, 1 @ clean & invalidate D / U line
  348. 1:
  349. mcr p15, 0, r0, c7, c6, 1 @ invalidate D / U line
  350. add r0, r0, r2
  351. cmp r0, r1
  352. blo 1b
  353. dsb st
  354. ret lr
  355. ENDPROC(v7_dma_inv_range)
  356. /*
  357. * v7_dma_clean_range(start,end)
  358. * - start - virtual start address of region
  359. * - end - virtual end address of region
  360. */
  361. v7_dma_clean_range:
  362. dcache_line_size r2, r3
  363. sub r3, r2, #1
  364. bic r0, r0, r3
  365. #ifdef CONFIG_ARM_ERRATA_764369
  366. ALT_SMP(W(dsb))
  367. ALT_UP(W(nop))
  368. #endif
  369. 1:
  370. mcr p15, 0, r0, c7, c10, 1 @ clean D / U line
  371. add r0, r0, r2
  372. cmp r0, r1
  373. blo 1b
  374. dsb st
  375. ret lr
  376. ENDPROC(v7_dma_clean_range)
  377. /*
  378. * v7_dma_flush_range(start,end)
  379. * - start - virtual start address of region
  380. * - end - virtual end address of region
  381. */
  382. ENTRY(v7_dma_flush_range)
  383. dcache_line_size r2, r3
  384. sub r3, r2, #1
  385. bic r0, r0, r3
  386. #ifdef CONFIG_ARM_ERRATA_764369
  387. ALT_SMP(W(dsb))
  388. ALT_UP(W(nop))
  389. #endif
  390. 1:
  391. mcr p15, 0, r0, c7, c14, 1 @ clean & invalidate D / U line
  392. add r0, r0, r2
  393. cmp r0, r1
  394. blo 1b
  395. dsb st
  396. ret lr
  397. ENDPROC(v7_dma_flush_range)
  398. /*
  399. * dma_map_area(start, size, dir)
  400. * - start - kernel virtual start address
  401. * - size - size of region
  402. * - dir - DMA direction
  403. */
  404. ENTRY(v7_dma_map_area)
  405. add r1, r1, r0
  406. teq r2, #DMA_FROM_DEVICE
  407. beq v7_dma_inv_range
  408. b v7_dma_clean_range
  409. ENDPROC(v7_dma_map_area)
  410. /*
  411. * dma_unmap_area(start, size, dir)
  412. * - start - kernel virtual start address
  413. * - size - size of region
  414. * - dir - DMA direction
  415. */
  416. ENTRY(v7_dma_unmap_area)
  417. add r1, r1, r0
  418. teq r2, #DMA_TO_DEVICE
  419. bne v7_dma_inv_range
  420. ret lr
  421. ENDPROC(v7_dma_unmap_area)
  422. __INITDATA
  423. @ define struct cpu_cache_fns (see <asm/cacheflush.h> and proc-macros.S)
  424. define_cache_functions v7