Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved. |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <config.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 8 | #include <asm/global_data.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 9 | #include <linux/bitops.h> |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 10 | #include <linux/compiler.h> |
| 11 | #include <linux/kernel.h> |
Alexey Brodkin | 982f6bf | 2017-06-26 11:46:47 +0300 | [diff] [blame] | 12 | #include <linux/log2.h> |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 13 | #include <asm/arcregs.h> |
Eugeniy Paltsev | 589ac75 | 2018-03-21 15:58:52 +0300 | [diff] [blame] | 14 | #include <asm/arc-bcr.h> |
Alexey Brodkin | 6b95cca | 2015-02-03 13:58:13 +0300 | [diff] [blame] | 15 | #include <asm/cache.h> |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 16 | |
Eugeniy Paltsev | bcedf4d | 2018-03-21 15:58:50 +0300 | [diff] [blame] | 17 | /* |
| 18 | * [ NOTE 1 ]: |
| 19 | * Data cache (L1 D$ or SL$) entire invalidate operation or data cache disable |
| 20 | * operation may result in unexpected behavior and data loss even if we flush |
| 21 | * data cache right before invalidation. That may happens if we store any context |
| 22 | * on stack (like we store BLINK register on stack before function call). |
| 23 | * BLINK register is the register where return address is automatically saved |
| 24 | * when we do function call with instructions like 'bl'. |
| 25 | * |
| 26 | * There is the real example: |
| 27 | * We may hang in the next code as we store any BLINK register on stack in |
| 28 | * invalidate_dcache_all() function. |
| 29 | * |
| 30 | * void flush_dcache_all() { |
| 31 | * __dc_entire_op(OP_FLUSH); |
| 32 | * // Other code // |
| 33 | * } |
| 34 | * |
| 35 | * void invalidate_dcache_all() { |
| 36 | * __dc_entire_op(OP_INV); |
| 37 | * // Other code // |
| 38 | * } |
| 39 | * |
| 40 | * void foo(void) { |
| 41 | * flush_dcache_all(); |
| 42 | * invalidate_dcache_all(); |
| 43 | * } |
| 44 | * |
| 45 | * Now let's see what really happens during that code execution: |
| 46 | * |
| 47 | * foo() |
| 48 | * |->> call flush_dcache_all |
| 49 | * [return address is saved to BLINK register] |
| 50 | * [push BLINK] (save to stack) ![point 1] |
| 51 | * |->> call __dc_entire_op(OP_FLUSH) |
| 52 | * [return address is saved to BLINK register] |
| 53 | * [flush L1 D$] |
| 54 | * return [jump to BLINK] |
| 55 | * <<------ |
| 56 | * [other flush_dcache_all code] |
| 57 | * [pop BLINK] (get from stack) |
| 58 | * return [jump to BLINK] |
| 59 | * <<------ |
| 60 | * |->> call invalidate_dcache_all |
| 61 | * [return address is saved to BLINK register] |
| 62 | * [push BLINK] (save to stack) ![point 2] |
| 63 | * |->> call __dc_entire_op(OP_FLUSH) |
| 64 | * [return address is saved to BLINK register] |
| 65 | * [invalidate L1 D$] ![point 3] |
| 66 | * // Oops!!! |
| 67 | * // We lose return address from invalidate_dcache_all function: |
| 68 | * // we save it to stack and invalidate L1 D$ after that! |
| 69 | * return [jump to BLINK] |
| 70 | * <<------ |
| 71 | * [other invalidate_dcache_all code] |
| 72 | * [pop BLINK] (get from stack) |
| 73 | * // we don't have this data in L1 dcache as we invalidated it in [point 3] |
| 74 | * // so we get it from next memory level (for example DDR memory) |
| 75 | * // but in the memory we have value which we save in [point 1], which |
| 76 | * // is return address from flush_dcache_all function (instead of |
| 77 | * // address from current invalidate_dcache_all function which we |
| 78 | * // saved in [point 2] !) |
| 79 | * return [jump to BLINK] |
| 80 | * <<------ |
| 81 | * // As BLINK points to invalidate_dcache_all, we call it again and |
| 82 | * // loop forever. |
| 83 | * |
| 84 | * Fortunately we may fix that by using flush & invalidation of D$ with a single |
| 85 | * one instruction (instead of flush and invalidation instructions pair) and |
| 86 | * enabling force function inline with '__attribute__((always_inline))' gcc |
| 87 | * attribute to avoid any function call (and BLINK store) between cache flush |
| 88 | * and disable. |
Eugeniy Paltsev | a22fcc4 | 2018-03-21 15:59:03 +0300 | [diff] [blame] | 89 | * |
| 90 | * |
| 91 | * [ NOTE 2 ]: |
| 92 | * As of today we only support the following cache configurations on ARC. |
Eugeniy Paltsev | 67c3492 | 2020-03-11 15:00:43 +0300 | [diff] [blame] | 93 | * Other configurations may exist in HW but we don't support it in SW. |
Eugeniy Paltsev | a22fcc4 | 2018-03-21 15:59:03 +0300 | [diff] [blame] | 94 | * Configuration 1: |
| 95 | * ______________________ |
| 96 | * | | |
| 97 | * | ARC CPU | |
| 98 | * |______________________| |
| 99 | * ___|___ ___|___ |
| 100 | * | | | | |
| 101 | * | L1 I$ | | L1 D$ | |
| 102 | * |_______| |_______| |
| 103 | * on/off on/off |
| 104 | * ___|______________|____ |
| 105 | * | | |
| 106 | * | main memory | |
| 107 | * |______________________| |
| 108 | * |
| 109 | * Configuration 2: |
| 110 | * ______________________ |
| 111 | * | | |
| 112 | * | ARC CPU | |
| 113 | * |______________________| |
| 114 | * ___|___ ___|___ |
| 115 | * | | | | |
| 116 | * | L1 I$ | | L1 D$ | |
| 117 | * |_______| |_______| |
| 118 | * on/off on/off |
| 119 | * ___|______________|____ |
| 120 | * | | |
| 121 | * | L2 (SL$) | |
| 122 | * |______________________| |
Eugeniy Paltsev | 67c3492 | 2020-03-11 15:00:43 +0300 | [diff] [blame] | 123 | * always on (ARCv2, HS < 3.0) |
| 124 | * on/off (ARCv2, HS >= 3.0) |
Eugeniy Paltsev | a22fcc4 | 2018-03-21 15:59:03 +0300 | [diff] [blame] | 125 | * ___|______________|____ |
| 126 | * | | |
| 127 | * | main memory | |
| 128 | * |______________________| |
| 129 | * |
| 130 | * Configuration 3: |
| 131 | * ______________________ |
| 132 | * | | |
| 133 | * | ARC CPU | |
| 134 | * |______________________| |
| 135 | * ___|___ ___|___ |
| 136 | * | | | | |
| 137 | * | L1 I$ | | L1 D$ | |
| 138 | * |_______| |_______| |
| 139 | * on/off must be on |
| 140 | * ___|______________|____ _______ |
| 141 | * | | | | |
| 142 | * | L2 (SL$) |-----| IOC | |
| 143 | * |______________________| |_______| |
| 144 | * always must be on on/off |
| 145 | * ___|______________|____ |
| 146 | * | | |
| 147 | * | main memory | |
| 148 | * |______________________| |
Eugeniy Paltsev | bcedf4d | 2018-03-21 15:58:50 +0300 | [diff] [blame] | 149 | */ |
| 150 | |
Eugeniy Paltsev | f3de8d6 | 2018-03-21 15:58:57 +0300 | [diff] [blame] | 151 | DECLARE_GLOBAL_DATA_PTR; |
| 152 | |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 153 | /* Bit values in IC_CTRL */ |
Eugeniy Paltsev | 6e626f0 | 2018-01-16 19:20:29 +0300 | [diff] [blame] | 154 | #define IC_CTRL_CACHE_DISABLE BIT(0) |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 155 | |
| 156 | /* Bit values in DC_CTRL */ |
Eugeniy Paltsev | 6e626f0 | 2018-01-16 19:20:29 +0300 | [diff] [blame] | 157 | #define DC_CTRL_CACHE_DISABLE BIT(0) |
| 158 | #define DC_CTRL_INV_MODE_FLUSH BIT(6) |
| 159 | #define DC_CTRL_FLUSH_STATUS BIT(8) |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 160 | |
Eugeniy Paltsev | e256cb0 | 2018-03-21 15:58:48 +0300 | [diff] [blame] | 161 | #define OP_INV BIT(0) |
| 162 | #define OP_FLUSH BIT(1) |
| 163 | #define OP_FLUSH_N_INV (OP_FLUSH | OP_INV) |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 164 | |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 165 | /* Bit val in SLC_CONTROL */ |
| 166 | #define SLC_CTRL_DIS 0x001 |
| 167 | #define SLC_CTRL_IM 0x040 |
| 168 | #define SLC_CTRL_BUSY 0x100 |
| 169 | #define SLC_CTRL_RGN_OP_INV 0x200 |
| 170 | |
Eugeniy Paltsev | f3de8d6 | 2018-03-21 15:58:57 +0300 | [diff] [blame] | 171 | #define CACHE_LINE_MASK (~(gd->arch.l1_line_sz - 1)) |
Alexey Brodkin | dff5df2 | 2015-12-14 17:14:46 +0300 | [diff] [blame] | 172 | |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 173 | /* |
| 174 | * We don't want to use '__always_inline' macro here as it can be redefined |
| 175 | * to simple 'inline' in some cases which breaks stuff. See [ NOTE 1 ] for more |
| 176 | * details about the reasons we need to use always_inline functions. |
| 177 | */ |
| 178 | #define inlined_cachefunc inline __attribute__((always_inline)) |
| 179 | |
| 180 | static inlined_cachefunc void __ic_entire_invalidate(void); |
| 181 | static inlined_cachefunc void __dc_entire_op(const int cacheop); |
Eugeniy Paltsev | 67c3492 | 2020-03-11 15:00:43 +0300 | [diff] [blame] | 182 | static inlined_cachefunc void __slc_entire_op(const int op); |
Eugeniy Paltsev | 637e354 | 2020-03-11 15:00:44 +0300 | [diff] [blame] | 183 | static inlined_cachefunc bool ioc_enabled(void); |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 184 | |
Eugeniy Paltsev | 6d5147b | 2018-03-21 15:58:56 +0300 | [diff] [blame] | 185 | static inline bool pae_exists(void) |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 186 | { |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 187 | /* TODO: should we compare mmu version from BCR and from CONFIG? */ |
| 188 | #if (CONFIG_ARC_MMU_VER >= 4) |
Eugeniy Paltsev | 589ac75 | 2018-03-21 15:58:52 +0300 | [diff] [blame] | 189 | union bcr_mmu_4 mmu4; |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 190 | |
Eugeniy Paltsev | 589ac75 | 2018-03-21 15:58:52 +0300 | [diff] [blame] | 191 | mmu4.word = read_aux_reg(ARC_AUX_MMU_BCR); |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 192 | |
Eugeniy Paltsev | 6d5147b | 2018-03-21 15:58:56 +0300 | [diff] [blame] | 193 | if (mmu4.fields.pae) |
| 194 | return true; |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 195 | #endif /* (CONFIG_ARC_MMU_VER >= 4) */ |
Eugeniy Paltsev | 6d5147b | 2018-03-21 15:58:56 +0300 | [diff] [blame] | 196 | |
| 197 | return false; |
| 198 | } |
| 199 | |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 200 | static inlined_cachefunc bool icache_exists(void) |
Eugeniy Paltsev | 6d5147b | 2018-03-21 15:58:56 +0300 | [diff] [blame] | 201 | { |
| 202 | union bcr_di_cache ibcr; |
| 203 | |
| 204 | ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD); |
| 205 | return !!ibcr.fields.ver; |
| 206 | } |
| 207 | |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 208 | static inlined_cachefunc bool icache_enabled(void) |
Eugeniy Paltsev | 0dec96c | 2018-03-21 15:59:00 +0300 | [diff] [blame] | 209 | { |
| 210 | if (!icache_exists()) |
| 211 | return false; |
| 212 | |
| 213 | return !(read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE); |
| 214 | } |
| 215 | |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 216 | static inlined_cachefunc bool dcache_exists(void) |
Eugeniy Paltsev | 6d5147b | 2018-03-21 15:58:56 +0300 | [diff] [blame] | 217 | { |
| 218 | union bcr_di_cache dbcr; |
| 219 | |
| 220 | dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD); |
| 221 | return !!dbcr.fields.ver; |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 222 | } |
| 223 | |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 224 | static inlined_cachefunc bool dcache_enabled(void) |
Eugeniy Paltsev | 0dec96c | 2018-03-21 15:59:00 +0300 | [diff] [blame] | 225 | { |
| 226 | if (!dcache_exists()) |
| 227 | return false; |
| 228 | |
| 229 | return !(read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE); |
| 230 | } |
| 231 | |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 232 | static inlined_cachefunc bool slc_exists(void) |
Eugeniy Paltsev | 6d5147b | 2018-03-21 15:58:56 +0300 | [diff] [blame] | 233 | { |
| 234 | if (is_isa_arcv2()) { |
| 235 | union bcr_generic sbcr; |
| 236 | |
| 237 | sbcr.word = read_aux_reg(ARC_BCR_SLC); |
| 238 | return !!sbcr.fields.ver; |
| 239 | } |
| 240 | |
| 241 | return false; |
| 242 | } |
| 243 | |
Eugeniy Paltsev | 67c3492 | 2020-03-11 15:00:43 +0300 | [diff] [blame] | 244 | enum slc_dis_status { |
| 245 | ST_SLC_MISSING = 0, |
| 246 | ST_SLC_NO_DISABLE_CTRL, |
| 247 | ST_SLC_DISABLE_CTRL |
| 248 | }; |
| 249 | |
| 250 | /* |
| 251 | * ARCv1 -> ST_SLC_MISSING |
| 252 | * ARCv2 && SLC absent -> ST_SLC_MISSING |
| 253 | * ARCv2 && SLC exists && SLC version <= 2 -> ST_SLC_NO_DISABLE_CTRL |
| 254 | * ARCv2 && SLC exists && SLC version > 2 -> ST_SLC_DISABLE_CTRL |
| 255 | */ |
| 256 | static inlined_cachefunc enum slc_dis_status slc_disable_supported(void) |
| 257 | { |
| 258 | if (is_isa_arcv2()) { |
| 259 | union bcr_generic sbcr; |
| 260 | |
| 261 | sbcr.word = read_aux_reg(ARC_BCR_SLC); |
| 262 | if (sbcr.fields.ver == 0) |
| 263 | return ST_SLC_MISSING; |
| 264 | else if (sbcr.fields.ver <= 2) |
| 265 | return ST_SLC_NO_DISABLE_CTRL; |
| 266 | else |
| 267 | return ST_SLC_DISABLE_CTRL; |
| 268 | } |
| 269 | |
| 270 | return ST_SLC_MISSING; |
| 271 | } |
| 272 | |
| 273 | static inlined_cachefunc bool __slc_enabled(void) |
| 274 | { |
| 275 | return !(read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_DIS); |
| 276 | } |
| 277 | |
| 278 | static inlined_cachefunc void __slc_enable(void) |
| 279 | { |
| 280 | unsigned int ctrl; |
| 281 | |
| 282 | ctrl = read_aux_reg(ARC_AUX_SLC_CTRL); |
| 283 | ctrl &= ~SLC_CTRL_DIS; |
| 284 | write_aux_reg(ARC_AUX_SLC_CTRL, ctrl); |
| 285 | } |
| 286 | |
| 287 | static inlined_cachefunc void __slc_disable(void) |
| 288 | { |
| 289 | unsigned int ctrl; |
| 290 | |
| 291 | ctrl = read_aux_reg(ARC_AUX_SLC_CTRL); |
| 292 | ctrl |= SLC_CTRL_DIS; |
| 293 | write_aux_reg(ARC_AUX_SLC_CTRL, ctrl); |
| 294 | } |
| 295 | |
| 296 | static inlined_cachefunc bool slc_enabled(void) |
| 297 | { |
| 298 | enum slc_dis_status slc_status = slc_disable_supported(); |
| 299 | |
| 300 | if (slc_status == ST_SLC_MISSING) |
| 301 | return false; |
| 302 | else if (slc_status == ST_SLC_NO_DISABLE_CTRL) |
| 303 | return true; |
| 304 | else |
| 305 | return __slc_enabled(); |
| 306 | } |
| 307 | |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 308 | static inlined_cachefunc bool slc_data_bypass(void) |
Eugeniy Paltsev | 767675f | 2018-03-21 15:59:01 +0300 | [diff] [blame] | 309 | { |
| 310 | /* |
| 311 | * If L1 data cache is disabled SL$ is bypassed and all load/store |
| 312 | * requests are sent directly to main memory. |
| 313 | */ |
| 314 | return !dcache_enabled(); |
| 315 | } |
| 316 | |
Eugeniy Paltsev | 67c3492 | 2020-03-11 15:00:43 +0300 | [diff] [blame] | 317 | void slc_enable(void) |
| 318 | { |
| 319 | if (slc_disable_supported() != ST_SLC_DISABLE_CTRL) |
| 320 | return; |
| 321 | |
| 322 | if (__slc_enabled()) |
| 323 | return; |
| 324 | |
| 325 | __slc_enable(); |
| 326 | } |
| 327 | |
| 328 | /* TODO: warn if we are not able to disable SLC */ |
| 329 | void slc_disable(void) |
| 330 | { |
| 331 | if (slc_disable_supported() != ST_SLC_DISABLE_CTRL) |
| 332 | return; |
| 333 | |
| 334 | /* we don't support SLC disabling if we use IOC */ |
| 335 | if (ioc_enabled()) |
| 336 | return; |
| 337 | |
| 338 | if (!__slc_enabled()) |
| 339 | return; |
| 340 | |
| 341 | /* |
| 342 | * We need to flush L1D$ to guarantee that we won't have any |
| 343 | * writeback operations during SLC disabling. |
| 344 | */ |
| 345 | __dc_entire_op(OP_FLUSH); |
| 346 | __slc_entire_op(OP_FLUSH_N_INV); |
| 347 | __slc_disable(); |
| 348 | } |
| 349 | |
Eugeniy Paltsev | 637e354 | 2020-03-11 15:00:44 +0300 | [diff] [blame] | 350 | static inlined_cachefunc bool ioc_exists(void) |
Eugeniy Paltsev | 04011ab | 2018-03-21 15:58:59 +0300 | [diff] [blame] | 351 | { |
| 352 | if (is_isa_arcv2()) { |
| 353 | union bcr_clust_cfg cbcr; |
| 354 | |
| 355 | cbcr.word = read_aux_reg(ARC_BCR_CLUSTER); |
| 356 | return cbcr.fields.c; |
| 357 | } |
| 358 | |
| 359 | return false; |
| 360 | } |
| 361 | |
Eugeniy Paltsev | 637e354 | 2020-03-11 15:00:44 +0300 | [diff] [blame] | 362 | static inlined_cachefunc bool ioc_enabled(void) |
Eugeniy Paltsev | 04011ab | 2018-03-21 15:58:59 +0300 | [diff] [blame] | 363 | { |
| 364 | /* |
| 365 | * We check only CONFIG option instead of IOC HW state check as IOC |
| 366 | * must be disabled by default. |
| 367 | */ |
| 368 | if (is_ioc_enabled()) |
| 369 | return ioc_exists(); |
| 370 | |
| 371 | return false; |
| 372 | } |
| 373 | |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 374 | static inlined_cachefunc void __slc_entire_op(const int op) |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 375 | { |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 376 | unsigned int ctrl; |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 377 | |
Eugeniy Paltsev | 67c3492 | 2020-03-11 15:00:43 +0300 | [diff] [blame] | 378 | if (!slc_enabled()) |
Eugeniy Paltsev | bb8155a | 2018-03-21 15:58:55 +0300 | [diff] [blame] | 379 | return; |
| 380 | |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 381 | ctrl = read_aux_reg(ARC_AUX_SLC_CTRL); |
| 382 | |
| 383 | if (!(op & OP_FLUSH)) /* i.e. OP_INV */ |
| 384 | ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */ |
| 385 | else |
| 386 | ctrl |= SLC_CTRL_IM; |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 387 | |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 388 | write_aux_reg(ARC_AUX_SLC_CTRL, ctrl); |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 389 | |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 390 | if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */ |
| 391 | write_aux_reg(ARC_AUX_SLC_INVALIDATE, 0x1); |
| 392 | else |
| 393 | write_aux_reg(ARC_AUX_SLC_FLUSH, 0x1); |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 394 | |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 395 | /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */ |
| 396 | read_aux_reg(ARC_AUX_SLC_CTRL); |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 397 | |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 398 | /* Important to wait for flush to complete */ |
| 399 | while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY); |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 400 | } |
| 401 | |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 402 | static void slc_upper_region_init(void) |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 403 | { |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 404 | /* |
Eugeniy Paltsev | 0627b3a | 2018-03-21 15:58:58 +0300 | [diff] [blame] | 405 | * ARC_AUX_SLC_RGN_START1 and ARC_AUX_SLC_RGN_END1 register exist |
| 406 | * only if PAE exists in current HW. So we had to check pae_exist |
| 407 | * before using them. |
| 408 | */ |
| 409 | if (!pae_exists()) |
| 410 | return; |
| 411 | |
| 412 | /* |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 413 | * ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1 are always == 0 |
| 414 | * as we don't use PAE40. |
| 415 | */ |
| 416 | write_aux_reg(ARC_AUX_SLC_RGN_END1, 0); |
| 417 | write_aux_reg(ARC_AUX_SLC_RGN_START1, 0); |
| 418 | } |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 419 | |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 420 | static void __slc_rgn_op(unsigned long paddr, unsigned long sz, const int op) |
| 421 | { |
Eugeniy Paltsev | 76caa80 | 2018-03-21 15:58:54 +0300 | [diff] [blame] | 422 | #ifdef CONFIG_ISA_ARCV2 |
| 423 | |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 424 | unsigned int ctrl; |
| 425 | unsigned long end; |
| 426 | |
Eugeniy Paltsev | 67c3492 | 2020-03-11 15:00:43 +0300 | [diff] [blame] | 427 | if (!slc_enabled()) |
Eugeniy Paltsev | bb8155a | 2018-03-21 15:58:55 +0300 | [diff] [blame] | 428 | return; |
| 429 | |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 430 | /* |
| 431 | * The Region Flush operation is specified by CTRL.RGN_OP[11..9] |
| 432 | * - b'000 (default) is Flush, |
| 433 | * - b'001 is Invalidate if CTRL.IM == 0 |
| 434 | * - b'001 is Flush-n-Invalidate if CTRL.IM == 1 |
| 435 | */ |
| 436 | ctrl = read_aux_reg(ARC_AUX_SLC_CTRL); |
| 437 | |
| 438 | /* Don't rely on default value of IM bit */ |
| 439 | if (!(op & OP_FLUSH)) /* i.e. OP_INV */ |
| 440 | ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */ |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 441 | else |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 442 | ctrl |= SLC_CTRL_IM; |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 443 | |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 444 | if (op & OP_INV) |
| 445 | ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */ |
| 446 | else |
| 447 | ctrl &= ~SLC_CTRL_RGN_OP_INV; |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 448 | |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 449 | write_aux_reg(ARC_AUX_SLC_CTRL, ctrl); |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 450 | |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 451 | /* |
| 452 | * Lower bits are ignored, no need to clip |
| 453 | * END needs to be setup before START (latter triggers the operation) |
| 454 | * END can't be same as START, so add (l2_line_sz - 1) to sz |
| 455 | */ |
Eugeniy Paltsev | f3de8d6 | 2018-03-21 15:58:57 +0300 | [diff] [blame] | 456 | end = paddr + sz + gd->arch.slc_line_sz - 1; |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 457 | |
| 458 | /* |
| 459 | * Upper addresses (ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1) |
| 460 | * are always == 0 as we don't use PAE40, so we only setup lower ones |
| 461 | * (ARC_AUX_SLC_RGN_END and ARC_AUX_SLC_RGN_START) |
| 462 | */ |
| 463 | write_aux_reg(ARC_AUX_SLC_RGN_END, end); |
| 464 | write_aux_reg(ARC_AUX_SLC_RGN_START, paddr); |
| 465 | |
| 466 | /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */ |
| 467 | read_aux_reg(ARC_AUX_SLC_CTRL); |
| 468 | |
| 469 | while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY); |
Eugeniy Paltsev | 76caa80 | 2018-03-21 15:58:54 +0300 | [diff] [blame] | 470 | |
| 471 | #endif /* CONFIG_ISA_ARCV2 */ |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 472 | } |
Eugeniy Paltsev | d4c5b2a | 2018-03-21 15:58:51 +0300 | [diff] [blame] | 473 | |
| 474 | static void arc_ioc_setup(void) |
| 475 | { |
| 476 | /* IOC Aperture start is equal to DDR start */ |
Tom Rini | bb4dd96 | 2022-11-16 13:10:37 -0500 | [diff] [blame] | 477 | unsigned int ap_base = CFG_SYS_SDRAM_BASE; |
Eugeniy Paltsev | d4c5b2a | 2018-03-21 15:58:51 +0300 | [diff] [blame] | 478 | /* IOC Aperture size is equal to DDR size */ |
Tom Rini | bb4dd96 | 2022-11-16 13:10:37 -0500 | [diff] [blame] | 479 | long ap_size = CFG_SYS_SDRAM_SIZE; |
Eugeniy Paltsev | d4c5b2a | 2018-03-21 15:58:51 +0300 | [diff] [blame] | 480 | |
Eugeniy Paltsev | a22fcc4 | 2018-03-21 15:59:03 +0300 | [diff] [blame] | 481 | /* Unsupported configuration. See [ NOTE 2 ] for more details. */ |
| 482 | if (!slc_exists()) |
| 483 | panic("Try to enable IOC but SLC is not present"); |
| 484 | |
Eugeniy Paltsev | 67c3492 | 2020-03-11 15:00:43 +0300 | [diff] [blame] | 485 | if (!slc_enabled()) |
| 486 | panic("Try to enable IOC but SLC is disabled"); |
| 487 | |
Eugeniy Paltsev | a22fcc4 | 2018-03-21 15:59:03 +0300 | [diff] [blame] | 488 | /* Unsupported configuration. See [ NOTE 2 ] for more details. */ |
| 489 | if (!dcache_enabled()) |
| 490 | panic("Try to enable IOC but L1 D$ is disabled"); |
| 491 | |
Eugeniy Paltsev | d4c5b2a | 2018-03-21 15:58:51 +0300 | [diff] [blame] | 492 | if (!is_power_of_2(ap_size) || ap_size < 4096) |
| 493 | panic("IOC Aperture size must be power of 2 and bigger 4Kib"); |
| 494 | |
Eugeniy Paltsev | 6305e2b | 2018-03-21 15:59:05 +0300 | [diff] [blame] | 495 | /* IOC Aperture start must be aligned to the size of the aperture */ |
| 496 | if (ap_base % ap_size != 0) |
| 497 | panic("IOC Aperture start must be aligned to the size of the aperture"); |
| 498 | |
| 499 | flush_n_invalidate_dcache_all(); |
| 500 | |
Eugeniy Paltsev | d4c5b2a | 2018-03-21 15:58:51 +0300 | [diff] [blame] | 501 | /* |
| 502 | * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB, |
| 503 | * so setting 0x11 implies 512M, 0x12 implies 1G... |
| 504 | */ |
| 505 | write_aux_reg(ARC_AUX_IO_COH_AP0_SIZE, |
| 506 | order_base_2(ap_size / 1024) - 2); |
| 507 | |
Eugeniy Paltsev | d4c5b2a | 2018-03-21 15:58:51 +0300 | [diff] [blame] | 508 | write_aux_reg(ARC_AUX_IO_COH_AP0_BASE, ap_base >> 12); |
| 509 | write_aux_reg(ARC_AUX_IO_COH_PARTIAL, 1); |
| 510 | write_aux_reg(ARC_AUX_IO_COH_ENABLE, 1); |
| 511 | } |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 512 | |
Alexey Brodkin | dff5df2 | 2015-12-14 17:14:46 +0300 | [diff] [blame] | 513 | static void read_decode_cache_bcr_arcv2(void) |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 514 | { |
Eugeniy Paltsev | 76caa80 | 2018-03-21 15:58:54 +0300 | [diff] [blame] | 515 | #ifdef CONFIG_ISA_ARCV2 |
| 516 | |
Eugeniy Paltsev | 589ac75 | 2018-03-21 15:58:52 +0300 | [diff] [blame] | 517 | union bcr_slc_cfg slc_cfg; |
Alexey Brodkin | dff5df2 | 2015-12-14 17:14:46 +0300 | [diff] [blame] | 518 | |
Eugeniy Paltsev | 6d5147b | 2018-03-21 15:58:56 +0300 | [diff] [blame] | 519 | if (slc_exists()) { |
Alexey Brodkin | dff5df2 | 2015-12-14 17:14:46 +0300 | [diff] [blame] | 520 | slc_cfg.word = read_aux_reg(ARC_AUX_SLC_CONFIG); |
Eugeniy Paltsev | f3de8d6 | 2018-03-21 15:58:57 +0300 | [diff] [blame] | 521 | gd->arch.slc_line_sz = (slc_cfg.fields.lsz == 0) ? 128 : 64; |
Eugeniy Paltsev | a22fcc4 | 2018-03-21 15:59:03 +0300 | [diff] [blame] | 522 | |
| 523 | /* |
| 524 | * We don't support configuration where L1 I$ or L1 D$ is |
| 525 | * absent but SL$ exists. See [ NOTE 2 ] for more details. |
| 526 | */ |
| 527 | if (!icache_exists() || !dcache_exists()) |
| 528 | panic("Unsupported cache configuration: SLC exists but one of L1 caches is absent"); |
Alexey Brodkin | dff5df2 | 2015-12-14 17:14:46 +0300 | [diff] [blame] | 529 | } |
Alexey Brodkin | 4764d26 | 2015-12-14 17:15:13 +0300 | [diff] [blame] | 530 | |
Eugeniy Paltsev | 76caa80 | 2018-03-21 15:58:54 +0300 | [diff] [blame] | 531 | #endif /* CONFIG_ISA_ARCV2 */ |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 532 | } |
| 533 | |
Alexey Brodkin | dff5df2 | 2015-12-14 17:14:46 +0300 | [diff] [blame] | 534 | void read_decode_cache_bcr(void) |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 535 | { |
Alexey Brodkin | dff5df2 | 2015-12-14 17:14:46 +0300 | [diff] [blame] | 536 | int dc_line_sz = 0, ic_line_sz = 0; |
Eugeniy Paltsev | 589ac75 | 2018-03-21 15:58:52 +0300 | [diff] [blame] | 537 | union bcr_di_cache ibcr, dbcr; |
Alexey Brodkin | dff5df2 | 2015-12-14 17:14:46 +0300 | [diff] [blame] | 538 | |
Alexey Brodkin | cf4f323 | 2018-05-25 20:22:23 +0300 | [diff] [blame] | 539 | /* |
| 540 | * We don't care much about I$ line length really as there're |
| 541 | * no per-line ops on I$ instead we only do full invalidation of it |
| 542 | * on occasion of relocation and right before jumping to the OS. |
| 543 | * Still we check insane config with zero-encoded line length in |
| 544 | * presense of version field in I$ BCR. Just in case. |
| 545 | */ |
Alexey Brodkin | dff5df2 | 2015-12-14 17:14:46 +0300 | [diff] [blame] | 546 | ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD); |
| 547 | if (ibcr.fields.ver) { |
Alexey Brodkin | cf4f323 | 2018-05-25 20:22:23 +0300 | [diff] [blame] | 548 | ic_line_sz = 8 << ibcr.fields.line_len; |
Alexey Brodkin | dff5df2 | 2015-12-14 17:14:46 +0300 | [diff] [blame] | 549 | if (!ic_line_sz) |
| 550 | panic("Instruction exists but line length is 0\n"); |
| 551 | } |
| 552 | |
| 553 | dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD); |
Eugeniy Paltsev | 6e626f0 | 2018-01-16 19:20:29 +0300 | [diff] [blame] | 554 | if (dbcr.fields.ver) { |
Eugeniy Paltsev | f3de8d6 | 2018-03-21 15:58:57 +0300 | [diff] [blame] | 555 | gd->arch.l1_line_sz = dc_line_sz = 16 << dbcr.fields.line_len; |
Alexey Brodkin | dff5df2 | 2015-12-14 17:14:46 +0300 | [diff] [blame] | 556 | if (!dc_line_sz) |
| 557 | panic("Data cache exists but line length is 0\n"); |
| 558 | } |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | void cache_init(void) |
| 562 | { |
Alexey Brodkin | dff5df2 | 2015-12-14 17:14:46 +0300 | [diff] [blame] | 563 | read_decode_cache_bcr(); |
| 564 | |
Eugeniy Paltsev | 76caa80 | 2018-03-21 15:58:54 +0300 | [diff] [blame] | 565 | if (is_isa_arcv2()) |
| 566 | read_decode_cache_bcr_arcv2(); |
Alexey Brodkin | 4764d26 | 2015-12-14 17:15:13 +0300 | [diff] [blame] | 567 | |
Eugeniy Paltsev | 04011ab | 2018-03-21 15:58:59 +0300 | [diff] [blame] | 568 | if (is_isa_arcv2() && ioc_enabled()) |
Eugeniy Paltsev | d4c5b2a | 2018-03-21 15:58:51 +0300 | [diff] [blame] | 569 | arc_ioc_setup(); |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 570 | |
Eugeniy Paltsev | 0627b3a | 2018-03-21 15:58:58 +0300 | [diff] [blame] | 571 | if (is_isa_arcv2() && slc_exists()) |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 572 | slc_upper_region_init(); |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 573 | } |
| 574 | |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 575 | int icache_status(void) |
| 576 | { |
Eugeniy Paltsev | 0dec96c | 2018-03-21 15:59:00 +0300 | [diff] [blame] | 577 | return icache_enabled(); |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | void icache_enable(void) |
| 581 | { |
Eugeniy Paltsev | 6d5147b | 2018-03-21 15:58:56 +0300 | [diff] [blame] | 582 | if (icache_exists()) |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 583 | write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) & |
| 584 | ~IC_CTRL_CACHE_DISABLE); |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | void icache_disable(void) |
| 588 | { |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 589 | if (!icache_exists()) |
| 590 | return; |
| 591 | |
| 592 | __ic_entire_invalidate(); |
| 593 | |
| 594 | write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) | |
| 595 | IC_CTRL_CACHE_DISABLE); |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 596 | } |
| 597 | |
Eugeniy Paltsev | ad8aef3 | 2018-03-21 15:58:46 +0300 | [diff] [blame] | 598 | /* IC supports only invalidation */ |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 599 | static inlined_cachefunc void __ic_entire_invalidate(void) |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 600 | { |
Eugeniy Paltsev | 0dec96c | 2018-03-21 15:59:00 +0300 | [diff] [blame] | 601 | if (!icache_enabled()) |
Eugeniy Paltsev | ad8aef3 | 2018-03-21 15:58:46 +0300 | [diff] [blame] | 602 | return; |
| 603 | |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 604 | /* Any write to IC_IVIC register triggers invalidation of entire I$ */ |
Eugeniy Paltsev | ad8aef3 | 2018-03-21 15:58:46 +0300 | [diff] [blame] | 605 | write_aux_reg(ARC_AUX_IC_IVIC, 1); |
| 606 | /* |
| 607 | * As per ARC HS databook (see chapter 5.3.3.2) |
| 608 | * it is required to add 3 NOPs after each write to IC_IVIC. |
| 609 | */ |
| 610 | __builtin_arc_nop(); |
| 611 | __builtin_arc_nop(); |
| 612 | __builtin_arc_nop(); |
| 613 | read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */ |
| 614 | } |
| 615 | |
| 616 | void invalidate_icache_all(void) |
| 617 | { |
| 618 | __ic_entire_invalidate(); |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 619 | |
Eugeniy Paltsev | 767675f | 2018-03-21 15:59:01 +0300 | [diff] [blame] | 620 | /* |
| 621 | * If SL$ is bypassed for data it is used only for instructions, |
| 622 | * so we need to invalidate it too. |
Eugeniy Paltsev | 767675f | 2018-03-21 15:59:01 +0300 | [diff] [blame] | 623 | */ |
| 624 | if (is_isa_arcv2() && slc_data_bypass()) |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 625 | __slc_entire_op(OP_INV); |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 626 | } |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 627 | |
| 628 | int dcache_status(void) |
| 629 | { |
Eugeniy Paltsev | 0dec96c | 2018-03-21 15:59:00 +0300 | [diff] [blame] | 630 | return dcache_enabled(); |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | void dcache_enable(void) |
| 634 | { |
Eugeniy Paltsev | 6d5147b | 2018-03-21 15:58:56 +0300 | [diff] [blame] | 635 | if (!dcache_exists()) |
Igor Guryanov | bd889f9 | 2014-12-24 16:07:07 +0300 | [diff] [blame] | 636 | return; |
| 637 | |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 638 | write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) & |
| 639 | ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE)); |
| 640 | } |
| 641 | |
| 642 | void dcache_disable(void) |
| 643 | { |
Eugeniy Paltsev | 6d5147b | 2018-03-21 15:58:56 +0300 | [diff] [blame] | 644 | if (!dcache_exists()) |
Igor Guryanov | bd889f9 | 2014-12-24 16:07:07 +0300 | [diff] [blame] | 645 | return; |
| 646 | |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 647 | __dc_entire_op(OP_FLUSH_N_INV); |
| 648 | |
| 649 | /* |
| 650 | * As SLC will be bypassed for data after L1 D$ disable we need to |
| 651 | * flush it first before L1 D$ disable. Also we invalidate SLC to |
| 652 | * avoid any inconsistent data problems after enabling L1 D$ again with |
| 653 | * dcache_enable function. |
| 654 | */ |
| 655 | if (is_isa_arcv2()) |
| 656 | __slc_entire_op(OP_FLUSH_N_INV); |
| 657 | |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 658 | write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) | |
| 659 | DC_CTRL_CACHE_DISABLE); |
| 660 | } |
| 661 | |
Eugeniy Paltsev | 988152c | 2018-03-21 15:58:47 +0300 | [diff] [blame] | 662 | /* Common Helper for Line Operations on D-cache */ |
| 663 | static inline void __dcache_line_loop(unsigned long paddr, unsigned long sz, |
| 664 | const int cacheop) |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 665 | { |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 666 | unsigned int aux_cmd; |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 667 | int num_lines; |
| 668 | |
Eugeniy Paltsev | 988152c | 2018-03-21 15:58:47 +0300 | [diff] [blame] | 669 | /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */ |
| 670 | aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL; |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 671 | |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 672 | sz += paddr & ~CACHE_LINE_MASK; |
| 673 | paddr &= CACHE_LINE_MASK; |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 674 | |
Eugeniy Paltsev | f3de8d6 | 2018-03-21 15:58:57 +0300 | [diff] [blame] | 675 | num_lines = DIV_ROUND_UP(sz, gd->arch.l1_line_sz); |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 676 | |
| 677 | while (num_lines-- > 0) { |
Alexey Brodkin | 6da8cfc | 2015-02-03 13:58:12 +0300 | [diff] [blame] | 678 | #if (CONFIG_ARC_MMU_VER == 3) |
Eugeniy Paltsev | 988152c | 2018-03-21 15:58:47 +0300 | [diff] [blame] | 679 | write_aux_reg(ARC_AUX_DC_PTAG, paddr); |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 680 | #endif |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 681 | write_aux_reg(aux_cmd, paddr); |
Eugeniy Paltsev | f3de8d6 | 2018-03-21 15:58:57 +0300 | [diff] [blame] | 682 | paddr += gd->arch.l1_line_sz; |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 683 | } |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 684 | } |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 685 | |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 686 | static inlined_cachefunc void __before_dc_op(const int op) |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 687 | { |
Eugeniy Paltsev | e256cb0 | 2018-03-21 15:58:48 +0300 | [diff] [blame] | 688 | unsigned int ctrl; |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 689 | |
Eugeniy Paltsev | e256cb0 | 2018-03-21 15:58:48 +0300 | [diff] [blame] | 690 | ctrl = read_aux_reg(ARC_AUX_DC_CTRL); |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 691 | |
Eugeniy Paltsev | e256cb0 | 2018-03-21 15:58:48 +0300 | [diff] [blame] | 692 | /* IM bit implies flush-n-inv, instead of vanilla inv */ |
| 693 | if (op == OP_INV) |
| 694 | ctrl &= ~DC_CTRL_INV_MODE_FLUSH; |
| 695 | else |
| 696 | ctrl |= DC_CTRL_INV_MODE_FLUSH; |
| 697 | |
| 698 | write_aux_reg(ARC_AUX_DC_CTRL, ctrl); |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 699 | } |
| 700 | |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 701 | static inlined_cachefunc void __after_dc_op(const int op) |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 702 | { |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 703 | if (op & OP_FLUSH) /* flush / flush-n-inv both wait */ |
Eugeniy Paltsev | 6e626f0 | 2018-01-16 19:20:29 +0300 | [diff] [blame] | 704 | while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS); |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 705 | } |
| 706 | |
Eugeniy Paltsev | dddc886 | 2018-03-21 15:59:04 +0300 | [diff] [blame] | 707 | static inlined_cachefunc void __dc_entire_op(const int cacheop) |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 708 | { |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 709 | int aux; |
Eugeniy Paltsev | e256cb0 | 2018-03-21 15:58:48 +0300 | [diff] [blame] | 710 | |
Eugeniy Paltsev | 0dec96c | 2018-03-21 15:59:00 +0300 | [diff] [blame] | 711 | if (!dcache_enabled()) |
Eugeniy Paltsev | 7fd7e0a | 2018-03-21 15:58:53 +0300 | [diff] [blame] | 712 | return; |
| 713 | |
Eugeniy Paltsev | e256cb0 | 2018-03-21 15:58:48 +0300 | [diff] [blame] | 714 | __before_dc_op(cacheop); |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 715 | |
| 716 | if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */ |
| 717 | aux = ARC_AUX_DC_IVDC; |
| 718 | else |
| 719 | aux = ARC_AUX_DC_FLSH; |
Alexey Brodkin | 35221a6 | 2015-03-27 12:47:29 +0300 | [diff] [blame] | 720 | |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 721 | write_aux_reg(aux, 0x1); |
| 722 | |
Eugeniy Paltsev | e256cb0 | 2018-03-21 15:58:48 +0300 | [diff] [blame] | 723 | __after_dc_op(cacheop); |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 724 | } |
| 725 | |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 726 | static inline void __dc_line_op(unsigned long paddr, unsigned long sz, |
| 727 | const int cacheop) |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 728 | { |
Eugeniy Paltsev | 0dec96c | 2018-03-21 15:59:00 +0300 | [diff] [blame] | 729 | if (!dcache_enabled()) |
Eugeniy Paltsev | 7fd7e0a | 2018-03-21 15:58:53 +0300 | [diff] [blame] | 730 | return; |
| 731 | |
Eugeniy Paltsev | e256cb0 | 2018-03-21 15:58:48 +0300 | [diff] [blame] | 732 | __before_dc_op(cacheop); |
Eugeniy Paltsev | 988152c | 2018-03-21 15:58:47 +0300 | [diff] [blame] | 733 | __dcache_line_loop(paddr, sz, cacheop); |
Eugeniy Paltsev | e256cb0 | 2018-03-21 15:58:48 +0300 | [diff] [blame] | 734 | __after_dc_op(cacheop); |
Alexey Brodkin | 3a59d91 | 2014-02-04 12:56:14 +0400 | [diff] [blame] | 735 | } |
Alexey Brodkin | 275583e | 2015-03-30 13:36:04 +0300 | [diff] [blame] | 736 | |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 737 | void invalidate_dcache_range(unsigned long start, unsigned long end) |
Alexey Brodkin | 275583e | 2015-03-30 13:36:04 +0300 | [diff] [blame] | 738 | { |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 739 | if (start >= end) |
| 740 | return; |
| 741 | |
Eugeniy Paltsev | 76caa80 | 2018-03-21 15:58:54 +0300 | [diff] [blame] | 742 | /* |
Eugeniy Paltsev | 767675f | 2018-03-21 15:59:01 +0300 | [diff] [blame] | 743 | * ARCv1 -> call __dc_line_op |
| 744 | * ARCv2 && L1 D$ disabled -> nothing |
| 745 | * ARCv2 && L1 D$ enabled && IOC enabled -> nothing |
| 746 | * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op |
Eugeniy Paltsev | 76caa80 | 2018-03-21 15:58:54 +0300 | [diff] [blame] | 747 | */ |
Eugeniy Paltsev | 04011ab | 2018-03-21 15:58:59 +0300 | [diff] [blame] | 748 | if (!is_isa_arcv2() || !ioc_enabled()) |
Alexey Brodkin | 4764d26 | 2015-12-14 17:15:13 +0300 | [diff] [blame] | 749 | __dc_line_op(start, end - start, OP_INV); |
| 750 | |
Eugeniy Paltsev | 767675f | 2018-03-21 15:59:01 +0300 | [diff] [blame] | 751 | if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass()) |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 752 | __slc_rgn_op(start, end - start, OP_INV); |
Alexey Brodkin | 275583e | 2015-03-30 13:36:04 +0300 | [diff] [blame] | 753 | } |
| 754 | |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 755 | void flush_dcache_range(unsigned long start, unsigned long end) |
Alexey Brodkin | 275583e | 2015-03-30 13:36:04 +0300 | [diff] [blame] | 756 | { |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 757 | if (start >= end) |
| 758 | return; |
| 759 | |
Eugeniy Paltsev | 76caa80 | 2018-03-21 15:58:54 +0300 | [diff] [blame] | 760 | /* |
Eugeniy Paltsev | 767675f | 2018-03-21 15:59:01 +0300 | [diff] [blame] | 761 | * ARCv1 -> call __dc_line_op |
| 762 | * ARCv2 && L1 D$ disabled -> nothing |
| 763 | * ARCv2 && L1 D$ enabled && IOC enabled -> nothing |
| 764 | * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op |
Eugeniy Paltsev | 76caa80 | 2018-03-21 15:58:54 +0300 | [diff] [blame] | 765 | */ |
Eugeniy Paltsev | 04011ab | 2018-03-21 15:58:59 +0300 | [diff] [blame] | 766 | if (!is_isa_arcv2() || !ioc_enabled()) |
Alexey Brodkin | 4764d26 | 2015-12-14 17:15:13 +0300 | [diff] [blame] | 767 | __dc_line_op(start, end - start, OP_FLUSH); |
| 768 | |
Eugeniy Paltsev | 767675f | 2018-03-21 15:59:01 +0300 | [diff] [blame] | 769 | if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass()) |
Eugeniy Paltsev | 1d0578e | 2018-01-16 19:20:26 +0300 | [diff] [blame] | 770 | __slc_rgn_op(start, end - start, OP_FLUSH); |
Alexey Brodkin | 275583e | 2015-03-30 13:36:04 +0300 | [diff] [blame] | 771 | } |
| 772 | |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 773 | void flush_cache(unsigned long start, unsigned long size) |
Alexey Brodkin | 275583e | 2015-03-30 13:36:04 +0300 | [diff] [blame] | 774 | { |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 775 | flush_dcache_range(start, start + size); |
Alexey Brodkin | 275583e | 2015-03-30 13:36:04 +0300 | [diff] [blame] | 776 | } |
| 777 | |
Eugeniy Paltsev | bcedf4d | 2018-03-21 15:58:50 +0300 | [diff] [blame] | 778 | /* |
| 779 | * As invalidate_dcache_all() is not used in generic U-Boot code and as we |
| 780 | * don't need it in arch/arc code alone (invalidate without flush) we implement |
| 781 | * flush_n_invalidate_dcache_all (flush and invalidate in 1 operation) because |
| 782 | * it's much safer. See [ NOTE 1 ] for more details. |
| 783 | */ |
| 784 | void flush_n_invalidate_dcache_all(void) |
Alexey Brodkin | 275583e | 2015-03-30 13:36:04 +0300 | [diff] [blame] | 785 | { |
Eugeniy Paltsev | bcedf4d | 2018-03-21 15:58:50 +0300 | [diff] [blame] | 786 | __dc_entire_op(OP_FLUSH_N_INV); |
Alexey Brodkin | 4764d26 | 2015-12-14 17:15:13 +0300 | [diff] [blame] | 787 | |
Eugeniy Paltsev | 767675f | 2018-03-21 15:59:01 +0300 | [diff] [blame] | 788 | if (is_isa_arcv2() && !slc_data_bypass()) |
Eugeniy Paltsev | bcedf4d | 2018-03-21 15:58:50 +0300 | [diff] [blame] | 789 | __slc_entire_op(OP_FLUSH_N_INV); |
Alexey Brodkin | 275583e | 2015-03-30 13:36:04 +0300 | [diff] [blame] | 790 | } |
| 791 | |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 792 | void flush_dcache_all(void) |
| 793 | { |
Alexey Brodkin | 5f54169 | 2016-04-16 15:28:30 +0300 | [diff] [blame] | 794 | __dc_entire_op(OP_FLUSH); |
Alexey Brodkin | 4764d26 | 2015-12-14 17:15:13 +0300 | [diff] [blame] | 795 | |
Eugeniy Paltsev | 767675f | 2018-03-21 15:59:01 +0300 | [diff] [blame] | 796 | if (is_isa_arcv2() && !slc_data_bypass()) |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 797 | __slc_entire_op(OP_FLUSH); |
Alexey Brodkin | 9f916ee | 2015-05-18 16:56:26 +0300 | [diff] [blame] | 798 | } |
Eugeniy Paltsev | 67fd56a | 2018-03-21 15:59:02 +0300 | [diff] [blame] | 799 | |
| 800 | /* |
| 801 | * This is function to cleanup all caches (and therefore sync I/D caches) which |
| 802 | * can be used for cleanup before linux launch or to sync caches during |
| 803 | * relocation. |
| 804 | */ |
| 805 | void sync_n_cleanup_cache_all(void) |
| 806 | { |
| 807 | __dc_entire_op(OP_FLUSH_N_INV); |
| 808 | |
| 809 | /* |
| 810 | * If SL$ is bypassed for data it is used only for instructions, |
| 811 | * and we shouldn't flush it. So invalidate it instead of flush_n_inv. |
| 812 | */ |
| 813 | if (is_isa_arcv2()) { |
| 814 | if (slc_data_bypass()) |
| 815 | __slc_entire_op(OP_INV); |
| 816 | else |
| 817 | __slc_entire_op(OP_FLUSH_N_INV); |
| 818 | } |
| 819 | |
| 820 | __ic_entire_invalidate(); |
| 821 | } |