Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 |
| 4 | * David Feng <fenghua@phytium.com.cn> |
| 5 | * |
| 6 | * This file is based on sample code from ARMv8 ARM. |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <asm-offsets.h> |
| 10 | #include <config.h> |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 11 | #include <asm/macro.h> |
Alexander Graf | 5e2ec77 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 12 | #include <asm/system.h> |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 13 | #include <linux/linkage.h> |
| 14 | |
Marc Zyngier | 46dc542 | 2023-02-09 04:54:27 +0800 | [diff] [blame] | 15 | #ifndef CONFIG_CMO_BY_VA_ONLY |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 16 | /* |
Masahiro Yamada | ba9eb6c | 2016-05-17 16:38:08 +0900 | [diff] [blame] | 17 | * void __asm_dcache_level(level) |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 18 | * |
Masahiro Yamada | ba9eb6c | 2016-05-17 16:38:08 +0900 | [diff] [blame] | 19 | * flush or invalidate one level cache. |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 20 | * |
| 21 | * x0: cache level |
Masahiro Yamada | 1a02123 | 2016-05-17 16:38:07 +0900 | [diff] [blame] | 22 | * x1: 0 clean & invalidate, 1 invalidate only |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 23 | * x2~x9: clobbered |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 24 | */ |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 25 | .pushsection .text.__asm_dcache_level, "ax" |
Masahiro Yamada | ba9eb6c | 2016-05-17 16:38:08 +0900 | [diff] [blame] | 26 | ENTRY(__asm_dcache_level) |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 27 | lsl x12, x0, #1 |
| 28 | msr csselr_el1, x12 /* select cache level */ |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 29 | isb /* sync change of cssidr_el1 */ |
| 30 | mrs x6, ccsidr_el1 /* read the new cssidr_el1 */ |
Pierre-Clément Tosi | f050bfa | 2021-08-27 18:03:45 +0200 | [diff] [blame] | 31 | ubfx x2, x6, #0, #3 /* x2 <- log2(cache line size)-4 */ |
| 32 | ubfx x3, x6, #3, #10 /* x3 <- number of cache ways - 1 */ |
| 33 | ubfx x4, x6, #13, #15 /* x4 <- number of cache sets - 1 */ |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 34 | add x2, x2, #4 /* x2 <- log2(cache line size) */ |
Leo Yan | 42ddfad | 2014-03-31 09:50:35 +0800 | [diff] [blame] | 35 | clz w5, w3 /* bit position of #ways */ |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 36 | /* x12 <- cache level << 1 */ |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 37 | /* x2 <- line length offset */ |
| 38 | /* x3 <- number of cache ways - 1 */ |
| 39 | /* x4 <- number of cache sets - 1 */ |
| 40 | /* x5 <- bit position of #ways */ |
| 41 | |
| 42 | loop_set: |
| 43 | mov x6, x3 /* x6 <- working copy of #ways */ |
| 44 | loop_way: |
| 45 | lsl x7, x6, x5 |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 46 | orr x9, x12, x7 /* map way and level to cisw value */ |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 47 | lsl x7, x4, x2 |
| 48 | orr x9, x9, x7 /* map set number to cisw value */ |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 49 | tbz w1, #0, 1f |
| 50 | dc isw, x9 |
| 51 | b 2f |
| 52 | 1: dc cisw, x9 /* clean & invalidate by set/way */ |
| 53 | 2: subs x6, x6, #1 /* decrement the way */ |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 54 | b.ge loop_way |
| 55 | subs x4, x4, #1 /* decrement the set */ |
| 56 | b.ge loop_set |
| 57 | |
| 58 | ret |
Masahiro Yamada | ba9eb6c | 2016-05-17 16:38:08 +0900 | [diff] [blame] | 59 | ENDPROC(__asm_dcache_level) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 60 | .popsection |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 61 | |
| 62 | /* |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 63 | * void __asm_flush_dcache_all(int invalidate_only) |
| 64 | * |
Masahiro Yamada | 1a02123 | 2016-05-17 16:38:07 +0900 | [diff] [blame] | 65 | * x0: 0 clean & invalidate, 1 invalidate only |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 66 | * |
Masahiro Yamada | ba9eb6c | 2016-05-17 16:38:08 +0900 | [diff] [blame] | 67 | * flush or invalidate all data cache by SET/WAY. |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 68 | */ |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 69 | .pushsection .text.__asm_dcache_all, "ax" |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 70 | ENTRY(__asm_dcache_all) |
| 71 | mov x1, x0 |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 72 | dsb sy |
| 73 | mrs x10, clidr_el1 /* read clidr_el1 */ |
Pierre-Clément Tosi | f050bfa | 2021-08-27 18:03:45 +0200 | [diff] [blame] | 74 | ubfx x11, x10, #24, #3 /* x11 <- loc */ |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 75 | cbz x11, finished /* if loc is 0, exit */ |
| 76 | mov x15, lr |
| 77 | mov x0, #0 /* start flush at cache level 0 */ |
| 78 | /* x0 <- cache level */ |
| 79 | /* x10 <- clidr_el1 */ |
| 80 | /* x11 <- loc */ |
| 81 | /* x15 <- return address */ |
| 82 | |
| 83 | loop_level: |
Pierre-Clément Tosi | 37479e6 | 2021-08-27 18:04:10 +0200 | [diff] [blame] | 84 | add x12, x0, x0, lsl #1 /* x12 <- tripled cache level */ |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 85 | lsr x12, x10, x12 |
| 86 | and x12, x12, #7 /* x12 <- cache type */ |
| 87 | cmp x12, #2 |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 88 | b.lt skip /* skip if no cache or icache */ |
Masahiro Yamada | ba9eb6c | 2016-05-17 16:38:08 +0900 | [diff] [blame] | 89 | bl __asm_dcache_level /* x1 = 0 flush, 1 invalidate */ |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 90 | skip: |
| 91 | add x0, x0, #1 /* increment cache level */ |
| 92 | cmp x11, x0 |
| 93 | b.gt loop_level |
| 94 | |
| 95 | mov x0, #0 |
Michal Simek | f1075ae | 2015-01-14 15:36:35 +0100 | [diff] [blame] | 96 | msr csselr_el1, x0 /* restore csselr_el1 */ |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 97 | dsb sy |
| 98 | isb |
| 99 | mov lr, x15 |
| 100 | |
| 101 | finished: |
| 102 | ret |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 103 | ENDPROC(__asm_dcache_all) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 104 | .popsection |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 105 | |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 106 | .pushsection .text.__asm_flush_dcache_all, "ax" |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 107 | ENTRY(__asm_flush_dcache_all) |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 108 | mov x0, #0 |
Masahiro Yamada | 2582858 | 2016-05-17 16:38:06 +0900 | [diff] [blame] | 109 | b __asm_dcache_all |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 110 | ENDPROC(__asm_flush_dcache_all) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 111 | .popsection |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 112 | |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 113 | .pushsection .text.__asm_invalidate_dcache_all, "ax" |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 114 | ENTRY(__asm_invalidate_dcache_all) |
Peng Fan | 208bd51 | 2015-08-06 17:54:13 +0800 | [diff] [blame] | 115 | mov x0, #0x1 |
Masahiro Yamada | 2582858 | 2016-05-17 16:38:06 +0900 | [diff] [blame] | 116 | b __asm_dcache_all |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 117 | ENDPROC(__asm_invalidate_dcache_all) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 118 | .popsection |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 119 | |
Marc Zyngier | 46dc542 | 2023-02-09 04:54:27 +0800 | [diff] [blame] | 120 | .pushsection .text.__asm_flush_l3_dcache, "ax" |
| 121 | WEAK(__asm_flush_l3_dcache) |
| 122 | mov x0, #0 /* return status as success */ |
| 123 | ret |
| 124 | ENDPROC(__asm_flush_l3_dcache) |
| 125 | .popsection |
| 126 | |
| 127 | .pushsection .text.__asm_invalidate_l3_icache, "ax" |
| 128 | WEAK(__asm_invalidate_l3_icache) |
| 129 | mov x0, #0 /* return status as success */ |
| 130 | ret |
| 131 | ENDPROC(__asm_invalidate_l3_icache) |
| 132 | .popsection |
| 133 | |
| 134 | #else /* CONFIG_CMO_BY_VA */ |
| 135 | |
| 136 | /* |
| 137 | * Define these so that they actively clash with in implementation |
| 138 | * accidentally selecting CONFIG_CMO_BY_VA |
| 139 | */ |
| 140 | |
| 141 | .pushsection .text.__asm_invalidate_l3_icache, "ax" |
| 142 | ENTRY(__asm_invalidate_l3_icache) |
| 143 | mov x0, xzr |
| 144 | ret |
| 145 | ENDPROC(__asm_invalidate_l3_icache) |
| 146 | .popsection |
| 147 | .pushsection .text.__asm_flush_l3_dcache, "ax" |
| 148 | ENTRY(__asm_flush_l3_dcache) |
| 149 | mov x0, xzr |
| 150 | ret |
| 151 | ENDPROC(__asm_flush_l3_dcache) |
| 152 | .popsection |
| 153 | #endif /* CONFIG_CMO_BY_VA */ |
| 154 | |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 155 | /* |
| 156 | * void __asm_flush_dcache_range(start, end) |
| 157 | * |
| 158 | * clean & invalidate data cache in the range |
| 159 | * |
| 160 | * x0: start address |
| 161 | * x1: end address |
| 162 | */ |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 163 | .pushsection .text.__asm_flush_dcache_range, "ax" |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 164 | ENTRY(__asm_flush_dcache_range) |
| 165 | mrs x3, ctr_el0 |
Pierre-Clément Tosi | f050bfa | 2021-08-27 18:03:45 +0200 | [diff] [blame] | 166 | ubfx x3, x3, #16, #4 |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 167 | mov x2, #4 |
| 168 | lsl x2, x2, x3 /* cache line size */ |
| 169 | |
| 170 | /* x2 <- minimal cache line size in cache system */ |
| 171 | sub x3, x2, #1 |
| 172 | bic x0, x0, x3 |
| 173 | 1: dc civac, x0 /* clean & invalidate data or unified cache */ |
| 174 | add x0, x0, x2 |
| 175 | cmp x0, x1 |
| 176 | b.lo 1b |
| 177 | dsb sy |
| 178 | ret |
| 179 | ENDPROC(__asm_flush_dcache_range) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 180 | .popsection |
Simon Glass | 6775a82 | 2017-04-05 17:53:18 -0600 | [diff] [blame] | 181 | /* |
| 182 | * void __asm_invalidate_dcache_range(start, end) |
| 183 | * |
| 184 | * invalidate data cache in the range |
| 185 | * |
| 186 | * x0: start address |
| 187 | * x1: end address |
| 188 | */ |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 189 | .pushsection .text.__asm_invalidate_dcache_range, "ax" |
Simon Glass | 6775a82 | 2017-04-05 17:53:18 -0600 | [diff] [blame] | 190 | ENTRY(__asm_invalidate_dcache_range) |
| 191 | mrs x3, ctr_el0 |
Pierre-Clément Tosi | f050bfa | 2021-08-27 18:03:45 +0200 | [diff] [blame] | 192 | ubfx x3, x3, #16, #4 |
Simon Glass | 6775a82 | 2017-04-05 17:53:18 -0600 | [diff] [blame] | 193 | mov x2, #4 |
| 194 | lsl x2, x2, x3 /* cache line size */ |
| 195 | |
| 196 | /* x2 <- minimal cache line size in cache system */ |
| 197 | sub x3, x2, #1 |
| 198 | bic x0, x0, x3 |
| 199 | 1: dc ivac, x0 /* invalidate data or unified cache */ |
| 200 | add x0, x0, x2 |
| 201 | cmp x0, x1 |
| 202 | b.lo 1b |
| 203 | dsb sy |
| 204 | ret |
| 205 | ENDPROC(__asm_invalidate_dcache_range) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 206 | .popsection |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * void __asm_invalidate_icache_all(void) |
| 210 | * |
| 211 | * invalidate all tlb entries. |
| 212 | */ |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 213 | .pushsection .text.__asm_invalidate_icache_all, "ax" |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 214 | ENTRY(__asm_invalidate_icache_all) |
| 215 | ic ialluis |
| 216 | isb sy |
| 217 | ret |
| 218 | ENDPROC(__asm_invalidate_icache_all) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 219 | .popsection |
York Sun | dcd468b | 2015-01-06 13:18:42 -0800 | [diff] [blame] | 220 | |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 221 | .pushsection .text.__asm_invalidate_l3_dcache, "ax" |
Tom Rini | f7b845b | 2021-06-29 19:33:04 -0400 | [diff] [blame] | 222 | WEAK(__asm_invalidate_l3_dcache) |
York Sun | dcd468b | 2015-01-06 13:18:42 -0800 | [diff] [blame] | 223 | mov x0, #0 /* return status as success */ |
| 224 | ret |
Stephen Warren | 1ab557a | 2016-10-19 15:18:46 -0600 | [diff] [blame] | 225 | ENDPROC(__asm_invalidate_l3_dcache) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 226 | .popsection |
Stephen Warren | 1ab557a | 2016-10-19 15:18:46 -0600 | [diff] [blame] | 227 | |
Alexander Graf | 5e2ec77 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 228 | /* |
| 229 | * void __asm_switch_ttbr(ulong new_ttbr) |
| 230 | * |
| 231 | * Safely switches to a new page table. |
| 232 | */ |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 233 | .pushsection .text.__asm_switch_ttbr, "ax" |
Alexander Graf | 5e2ec77 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 234 | ENTRY(__asm_switch_ttbr) |
| 235 | /* x2 = SCTLR (alive throghout the function) */ |
| 236 | switch_el x4, 3f, 2f, 1f |
| 237 | 3: mrs x2, sctlr_el3 |
| 238 | b 0f |
| 239 | 2: mrs x2, sctlr_el2 |
| 240 | b 0f |
| 241 | 1: mrs x2, sctlr_el1 |
| 242 | 0: |
| 243 | |
| 244 | /* Unset CR_M | CR_C | CR_I from SCTLR to disable all caches */ |
| 245 | movn x1, #(CR_M | CR_C | CR_I) |
| 246 | and x1, x2, x1 |
| 247 | switch_el x4, 3f, 2f, 1f |
| 248 | 3: msr sctlr_el3, x1 |
| 249 | b 0f |
| 250 | 2: msr sctlr_el2, x1 |
| 251 | b 0f |
| 252 | 1: msr sctlr_el1, x1 |
| 253 | 0: isb |
| 254 | |
| 255 | /* This call only clobbers x30 (lr) and x9 (unused) */ |
| 256 | mov x3, x30 |
| 257 | bl __asm_invalidate_tlb_all |
| 258 | |
| 259 | /* From here on we're running safely with caches disabled */ |
| 260 | |
| 261 | /* Set TTBR to our first argument */ |
| 262 | switch_el x4, 3f, 2f, 1f |
| 263 | 3: msr ttbr0_el3, x0 |
| 264 | b 0f |
| 265 | 2: msr ttbr0_el2, x0 |
| 266 | b 0f |
| 267 | 1: msr ttbr0_el1, x0 |
| 268 | 0: isb |
| 269 | |
| 270 | /* Restore original SCTLR and thus enable caches again */ |
| 271 | switch_el x4, 3f, 2f, 1f |
| 272 | 3: msr sctlr_el3, x2 |
| 273 | b 0f |
| 274 | 2: msr sctlr_el2, x2 |
| 275 | b 0f |
| 276 | 1: msr sctlr_el1, x2 |
| 277 | 0: isb |
| 278 | |
| 279 | ret x3 |
| 280 | ENDPROC(__asm_switch_ttbr) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 281 | .popsection |