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