cache.S 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <version.h>
  12. #include <asm/macro.h>
  13. #include <linux/linkage.h>
  14. /*
  15. * void __asm_flush_dcache_level(level)
  16. *
  17. * clean and invalidate one level cache.
  18. *
  19. * x0: cache level
  20. * x1: 0 flush & invalidate, 1 invalidate only
  21. * x2~x9: clobbered
  22. */
  23. ENTRY(__asm_flush_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_flush_dcache_level)
  57. /*
  58. * void __asm_flush_dcache_all(int invalidate_only)
  59. *
  60. * x0: 0 flush & invalidate, 1 invalidate only
  61. *
  62. * clean and 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_flush_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 /* resotre 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 x16, lr
  99. mov x0, #0
  100. bl __asm_dcache_all
  101. mov lr, x16
  102. ret
  103. ENDPROC(__asm_flush_dcache_all)
  104. ENTRY(__asm_invalidate_dcache_all)
  105. mov x16, lr
  106. mov x0, #0xffff
  107. bl __asm_dcache_all
  108. mov lr, x16
  109. ret
  110. ENDPROC(__asm_invalidate_dcache_all)
  111. /*
  112. * void __asm_flush_dcache_range(start, end)
  113. *
  114. * clean & invalidate data cache in the range
  115. *
  116. * x0: start address
  117. * x1: end address
  118. */
  119. ENTRY(__asm_flush_dcache_range)
  120. mrs x3, ctr_el0
  121. lsr x3, x3, #16
  122. and x3, x3, #0xf
  123. mov x2, #4
  124. lsl x2, x2, x3 /* cache line size */
  125. /* x2 <- minimal cache line size in cache system */
  126. sub x3, x2, #1
  127. bic x0, x0, x3
  128. 1: dc civac, x0 /* clean & invalidate data or unified cache */
  129. add x0, x0, x2
  130. cmp x0, x1
  131. b.lo 1b
  132. dsb sy
  133. ret
  134. ENDPROC(__asm_flush_dcache_range)
  135. /*
  136. * void __asm_invalidate_icache_all(void)
  137. *
  138. * invalidate all tlb entries.
  139. */
  140. ENTRY(__asm_invalidate_icache_all)
  141. ic ialluis
  142. isb sy
  143. ret
  144. ENDPROC(__asm_invalidate_icache_all)