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