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 | |
| 15 | /* |
Masahiro Yamada | ba9eb6c | 2016-05-17 16:38:08 +0900 | [diff] [blame] | 16 | * void __asm_dcache_level(level) |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 17 | * |
Masahiro Yamada | ba9eb6c | 2016-05-17 16:38:08 +0900 | [diff] [blame] | 18 | * flush or invalidate one level cache. |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 19 | * |
| 20 | * x0: cache level |
Masahiro Yamada | 1a02123 | 2016-05-17 16:38:07 +0900 | [diff] [blame] | 21 | * x1: 0 clean & invalidate, 1 invalidate only |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 22 | * x2~x9: clobbered |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 23 | */ |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 24 | .pushsection .text.__asm_dcache_level, "ax" |
Masahiro Yamada | ba9eb6c | 2016-05-17 16:38:08 +0900 | [diff] [blame] | 25 | ENTRY(__asm_dcache_level) |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 26 | lsl x12, x0, #1 |
| 27 | msr csselr_el1, x12 /* select cache level */ |
David Feng | 0ae7653 | 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 | 42ddfad | 2014-03-31 09:50:35 +0800 | [diff] [blame] | 34 | clz w5, w3 /* bit position of #ways */ |
David Feng | 0ae7653 | 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 | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 37 | /* x12 <- cache level << 1 */ |
David Feng | 0ae7653 | 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 | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 47 | orr x9, x12, x7 /* map way and level to cisw value */ |
David Feng | 0ae7653 | 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 | 1e6ad55 | 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 | 0ae7653 | 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 | ba9eb6c | 2016-05-17 16:38:08 +0900 | [diff] [blame] | 60 | ENDPROC(__asm_dcache_level) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 61 | .popsection |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 62 | |
| 63 | /* |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 64 | * void __asm_flush_dcache_all(int invalidate_only) |
| 65 | * |
Masahiro Yamada | 1a02123 | 2016-05-17 16:38:07 +0900 | [diff] [blame] | 66 | * x0: 0 clean & invalidate, 1 invalidate only |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 67 | * |
Masahiro Yamada | ba9eb6c | 2016-05-17 16:38:08 +0900 | [diff] [blame] | 68 | * flush or invalidate all data cache by SET/WAY. |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 69 | */ |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 70 | .pushsection .text.__asm_dcache_all, "ax" |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 71 | ENTRY(__asm_dcache_all) |
| 72 | mov x1, x0 |
David Feng | 0ae7653 | 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 | 1e6ad55 | 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 | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 91 | b.lt skip /* skip if no cache or icache */ |
Masahiro Yamada | ba9eb6c | 2016-05-17 16:38:08 +0900 | [diff] [blame] | 92 | bl __asm_dcache_level /* x1 = 0 flush, 1 invalidate */ |
David Feng | 0ae7653 | 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 | f1075ae | 2015-01-14 15:36:35 +0100 | [diff] [blame] | 99 | msr csselr_el1, x0 /* restore csselr_el1 */ |
David Feng | 0ae7653 | 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 | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 106 | ENDPROC(__asm_dcache_all) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 107 | .popsection |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 108 | |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 109 | .pushsection .text.__asm_flush_dcache_all, "ax" |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 110 | ENTRY(__asm_flush_dcache_all) |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 111 | mov x0, #0 |
Masahiro Yamada | 2582858 | 2016-05-17 16:38:06 +0900 | [diff] [blame] | 112 | b __asm_dcache_all |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 113 | ENDPROC(__asm_flush_dcache_all) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 114 | .popsection |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 115 | |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 116 | .pushsection .text.__asm_invalidate_dcache_all, "ax" |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 117 | ENTRY(__asm_invalidate_dcache_all) |
Peng Fan | 208bd51 | 2015-08-06 17:54:13 +0800 | [diff] [blame] | 118 | mov x0, #0x1 |
Masahiro Yamada | 2582858 | 2016-05-17 16:38:06 +0900 | [diff] [blame] | 119 | b __asm_dcache_all |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 120 | ENDPROC(__asm_invalidate_dcache_all) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 121 | .popsection |
York Sun | 1e6ad55 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 122 | |
David Feng | 0ae7653 | 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 | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 131 | .pushsection .text.__asm_flush_dcache_range, "ax" |
David Feng | 0ae7653 | 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 | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 149 | .popsection |
Simon Glass | 6775a82 | 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 | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 158 | .pushsection .text.__asm_invalidate_dcache_range, "ax" |
Simon Glass | 6775a82 | 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 | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 175 | .popsection |
David Feng | 0ae7653 | 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 | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 182 | .pushsection .text.__asm_invalidate_icache_all, "ax" |
David Feng | 0ae7653 | 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 | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 188 | .popsection |
York Sun | dcd468b | 2015-01-06 13:18:42 -0800 | [diff] [blame] | 189 | |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 190 | .pushsection .text.__asm_invalidate_l3_dcache, "ax" |
Stephen Warren | 1ab557a | 2016-10-19 15:18:46 -0600 | [diff] [blame] | 191 | ENTRY(__asm_invalidate_l3_dcache) |
York Sun | dcd468b | 2015-01-06 13:18:42 -0800 | [diff] [blame] | 192 | mov x0, #0 /* return status as success */ |
| 193 | ret |
Stephen Warren | 1ab557a | 2016-10-19 15:18:46 -0600 | [diff] [blame] | 194 | ENDPROC(__asm_invalidate_l3_dcache) |
| 195 | .weak __asm_invalidate_l3_dcache |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 196 | .popsection |
Stephen Warren | 1ab557a | 2016-10-19 15:18:46 -0600 | [diff] [blame] | 197 | |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 198 | .pushsection .text.__asm_flush_l3_dcache, "ax" |
Stephen Warren | 1ab557a | 2016-10-19 15:18:46 -0600 | [diff] [blame] | 199 | ENTRY(__asm_flush_l3_dcache) |
| 200 | mov x0, #0 /* return status as success */ |
| 201 | ret |
| 202 | ENDPROC(__asm_flush_l3_dcache) |
| 203 | .weak __asm_flush_l3_dcache |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 204 | .popsection |
Stephen Warren | 1ab557a | 2016-10-19 15:18:46 -0600 | [diff] [blame] | 205 | |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 206 | .pushsection .text.__asm_invalidate_l3_icache, "ax" |
Stephen Warren | 1ab557a | 2016-10-19 15:18:46 -0600 | [diff] [blame] | 207 | ENTRY(__asm_invalidate_l3_icache) |
| 208 | mov x0, #0 /* return status as success */ |
| 209 | ret |
| 210 | ENDPROC(__asm_invalidate_l3_icache) |
| 211 | .weak __asm_invalidate_l3_icache |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 212 | .popsection |
Alexander Graf | 5e2ec77 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * void __asm_switch_ttbr(ulong new_ttbr) |
| 216 | * |
| 217 | * Safely switches to a new page table. |
| 218 | */ |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 219 | .pushsection .text.__asm_switch_ttbr, "ax" |
Alexander Graf | 5e2ec77 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 220 | ENTRY(__asm_switch_ttbr) |
| 221 | /* x2 = SCTLR (alive throghout the function) */ |
| 222 | switch_el x4, 3f, 2f, 1f |
| 223 | 3: mrs x2, sctlr_el3 |
| 224 | b 0f |
| 225 | 2: mrs x2, sctlr_el2 |
| 226 | b 0f |
| 227 | 1: mrs x2, sctlr_el1 |
| 228 | 0: |
| 229 | |
| 230 | /* Unset CR_M | CR_C | CR_I from SCTLR to disable all caches */ |
| 231 | movn x1, #(CR_M | CR_C | CR_I) |
| 232 | and x1, x2, x1 |
| 233 | switch_el x4, 3f, 2f, 1f |
| 234 | 3: msr sctlr_el3, x1 |
| 235 | b 0f |
| 236 | 2: msr sctlr_el2, x1 |
| 237 | b 0f |
| 238 | 1: msr sctlr_el1, x1 |
| 239 | 0: isb |
| 240 | |
| 241 | /* This call only clobbers x30 (lr) and x9 (unused) */ |
| 242 | mov x3, x30 |
| 243 | bl __asm_invalidate_tlb_all |
| 244 | |
| 245 | /* From here on we're running safely with caches disabled */ |
| 246 | |
| 247 | /* Set TTBR to our first argument */ |
| 248 | switch_el x4, 3f, 2f, 1f |
| 249 | 3: msr ttbr0_el3, x0 |
| 250 | b 0f |
| 251 | 2: msr ttbr0_el2, x0 |
| 252 | b 0f |
| 253 | 1: msr ttbr0_el1, x0 |
| 254 | 0: isb |
| 255 | |
| 256 | /* Restore original SCTLR and thus enable caches again */ |
| 257 | switch_el x4, 3f, 2f, 1f |
| 258 | 3: msr sctlr_el3, x2 |
| 259 | b 0f |
| 260 | 2: msr sctlr_el2, x2 |
| 261 | b 0f |
| 262 | 1: msr sctlr_el1, x2 |
| 263 | 0: isb |
| 264 | |
| 265 | ret x3 |
| 266 | ENDPROC(__asm_switch_ttbr) |
Philipp Tomsich | e6a0586 | 2017-07-04 10:04:54 +0200 | [diff] [blame] | 267 | .popsection |