cache.S 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * (C) Copyright 2013
  3. * David Feng <fenghua@phytium.com.cn>
  4. *
  5. * This file is based on sample code from ARMv8 ARM.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <asm-offsets.h>
  10. #include <config.h>
  11. #include <asm/macro.h>
  12. #include <asm/system.h>
  13. #include <linux/linkage.h>
  14. /*
  15. * void __asm_dcache_level(level)
  16. *
  17. * flush or invalidate one level cache.
  18. *
  19. * x0: cache level
  20. * x1: 0 clean & invalidate, 1 invalidate only
  21. * x2~x9: clobbered
  22. */
  23. ENTRY(__asm_dcache_level)
  24. lsl x12, x0, #1
  25. msr csselr_el1, x12 /* select cache level */
  26. isb /* sync change of cssidr_el1 */
  27. mrs x6, ccsidr_el1 /* read the new cssidr_el1 */
  28. and x2, x6, #7 /* x2 <- log2(cache line size)-4 */
  29. add x2, x2, #4 /* x2 <- log2(cache line size) */
  30. mov x3, #0x3ff
  31. and x3, x3, x6, lsr #3 /* x3 <- max number of #ways */
  32. clz w5, w3 /* bit position of #ways */
  33. mov x4, #0x7fff
  34. and x4, x4, x6, lsr #13 /* x4 <- max number of #sets */
  35. /* x12 <- cache level << 1 */
  36. /* x2 <- line length offset */
  37. /* x3 <- number of cache ways - 1 */
  38. /* x4 <- number of cache sets - 1 */
  39. /* x5 <- bit position of #ways */
  40. loop_set:
  41. mov x6, x3 /* x6 <- working copy of #ways */
  42. loop_way:
  43. lsl x7, x6, x5
  44. orr x9, x12, x7 /* map way and level to cisw value */
  45. lsl x7, x4, x2
  46. orr x9, x9, x7 /* map set number to cisw value */
  47. tbz w1, #0, 1f
  48. dc isw, x9
  49. b 2f
  50. 1: dc cisw, x9 /* clean & invalidate by set/way */
  51. 2: subs x6, x6, #1 /* decrement the way */
  52. b.ge loop_way
  53. subs x4, x4, #1 /* decrement the set */
  54. b.ge loop_set
  55. ret
  56. ENDPROC(__asm_dcache_level)
  57. /*
  58. * void __asm_flush_dcache_all(int invalidate_only)
  59. *
  60. * x0: 0 clean & invalidate, 1 invalidate only
  61. *
  62. * flush or invalidate all data cache by SET/WAY.
  63. */
  64. ENTRY(__asm_dcache_all)
  65. mov x1, x0
  66. dsb sy
  67. mrs x10, clidr_el1 /* read clidr_el1 */
  68. lsr x11, x10, #24
  69. and x11, x11, #0x7 /* x11 <- loc */
  70. cbz x11, finished /* if loc is 0, exit */
  71. mov x15, lr
  72. mov x0, #0 /* start flush at cache level 0 */
  73. /* x0 <- cache level */
  74. /* x10 <- clidr_el1 */
  75. /* x11 <- loc */
  76. /* x15 <- return address */
  77. loop_level:
  78. lsl x12, x0, #1
  79. add x12, x12, x0 /* x0 <- tripled cache level */
  80. lsr x12, x10, x12
  81. and x12, x12, #7 /* x12 <- cache type */
  82. cmp x12, #2
  83. b.lt skip /* skip if no cache or icache */
  84. bl __asm_dcache_level /* x1 = 0 flush, 1 invalidate */
  85. skip:
  86. add x0, x0, #1 /* increment cache level */
  87. cmp x11, x0
  88. b.gt loop_level
  89. mov x0, #0
  90. msr csselr_el1, x0 /* restore csselr_el1 */
  91. dsb sy
  92. isb
  93. mov lr, x15
  94. finished:
  95. ret
  96. ENDPROC(__asm_dcache_all)
  97. ENTRY(__asm_flush_dcache_all)
  98. mov x0, #0
  99. b __asm_dcache_all
  100. ENDPROC(__asm_flush_dcache_all)
  101. ENTRY(__asm_invalidate_dcache_all)
  102. mov x0, #0x1
  103. b __asm_dcache_all
  104. ENDPROC(__asm_invalidate_dcache_all)
  105. /*
  106. * void __asm_flush_dcache_range(start, end)
  107. *
  108. * clean & invalidate data cache in the range
  109. *
  110. * x0: start address
  111. * x1: end address
  112. */
  113. ENTRY(__asm_flush_dcache_range)
  114. mrs x3, ctr_el0
  115. lsr x3, x3, #16
  116. and x3, x3, #0xf
  117. mov x2, #4
  118. lsl x2, x2, x3 /* cache line size */
  119. /* x2 <- minimal cache line size in cache system */
  120. sub x3, x2, #1
  121. bic x0, x0, x3
  122. 1: dc civac, x0 /* clean & invalidate data or unified cache */
  123. add x0, x0, x2
  124. cmp x0, x1
  125. b.lo 1b
  126. dsb sy
  127. ret
  128. ENDPROC(__asm_flush_dcache_range)
  129. /*
  130. * void __asm_invalidate_icache_all(void)
  131. *
  132. * invalidate all tlb entries.
  133. */
  134. ENTRY(__asm_invalidate_icache_all)
  135. ic ialluis
  136. isb sy
  137. ret
  138. ENDPROC(__asm_invalidate_icache_all)
  139. ENTRY(__asm_invalidate_l3_dcache)
  140. mov x0, #0 /* return status as success */
  141. ret
  142. ENDPROC(__asm_invalidate_l3_dcache)
  143. .weak __asm_invalidate_l3_dcache
  144. ENTRY(__asm_flush_l3_dcache)
  145. mov x0, #0 /* return status as success */
  146. ret
  147. ENDPROC(__asm_flush_l3_dcache)
  148. .weak __asm_flush_l3_dcache
  149. ENTRY(__asm_invalidate_l3_icache)
  150. mov x0, #0 /* return status as success */
  151. ret
  152. ENDPROC(__asm_invalidate_l3_icache)
  153. .weak __asm_invalidate_l3_icache
  154. /*
  155. * void __asm_switch_ttbr(ulong new_ttbr)
  156. *
  157. * Safely switches to a new page table.
  158. */
  159. ENTRY(__asm_switch_ttbr)
  160. /* x2 = SCTLR (alive throghout the function) */
  161. switch_el x4, 3f, 2f, 1f
  162. 3: mrs x2, sctlr_el3
  163. b 0f
  164. 2: mrs x2, sctlr_el2
  165. b 0f
  166. 1: mrs x2, sctlr_el1
  167. 0:
  168. /* Unset CR_M | CR_C | CR_I from SCTLR to disable all caches */
  169. movn x1, #(CR_M | CR_C | CR_I)
  170. and x1, x2, x1
  171. switch_el x4, 3f, 2f, 1f
  172. 3: msr sctlr_el3, x1
  173. b 0f
  174. 2: msr sctlr_el2, x1
  175. b 0f
  176. 1: msr sctlr_el1, x1
  177. 0: isb
  178. /* This call only clobbers x30 (lr) and x9 (unused) */
  179. mov x3, x30
  180. bl __asm_invalidate_tlb_all
  181. /* From here on we're running safely with caches disabled */
  182. /* Set TTBR to our first argument */
  183. switch_el x4, 3f, 2f, 1f
  184. 3: msr ttbr0_el3, x0
  185. b 0f
  186. 2: msr ttbr0_el2, x0
  187. b 0f
  188. 1: msr ttbr0_el1, x0
  189. 0: isb
  190. /* Restore original SCTLR and thus enable caches again */
  191. switch_el x4, 3f, 2f, 1f
  192. 3: msr sctlr_el3, x2
  193. b 0f
  194. 2: msr sctlr_el2, x2
  195. b 0f
  196. 1: msr sctlr_el1, x2
  197. 0: isb
  198. ret x3
  199. ENDPROC(__asm_switch_ttbr)