cache-v7.S 13 KB

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