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