Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 1 | /* |
| 2 | * ARC700 VIPT Cache Management |
| 3 | * |
| 4 | * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * vineetg: May 2011: for Non-aliasing VIPT D-cache following can be NOPs |
| 11 | * -flush_cache_dup_mm (fork) |
| 12 | * -likewise for flush_cache_mm (exit/execve) |
| 13 | * -likewise for flush_cache_range,flush_cache_page (munmap, exit, COW-break) |
| 14 | * |
| 15 | * vineetg: Apr 2011 |
| 16 | * -Now that MMU can support larger pg sz (16K), the determiniation of |
| 17 | * aliasing shd not be based on assumption of 8k pg |
| 18 | * |
| 19 | * vineetg: Mar 2011 |
| 20 | * -optimised version of flush_icache_range( ) for making I/D coherent |
| 21 | * when vaddr is available (agnostic of num of aliases) |
| 22 | * |
| 23 | * vineetg: Mar 2011 |
| 24 | * -Added documentation about I-cache aliasing on ARC700 and the way it |
| 25 | * was handled up until MMU V2. |
| 26 | * -Spotted a three year old bug when killing the 4 aliases, which needs |
| 27 | * bottom 2 bits, so we need to do paddr | {0x00, 0x01, 0x02, 0x03} |
| 28 | * instead of paddr | {0x00, 0x01, 0x10, 0x11} |
| 29 | * (Rajesh you owe me one now) |
| 30 | * |
| 31 | * vineetg: Dec 2010 |
| 32 | * -Off-by-one error when computing num_of_lines to flush |
| 33 | * This broke signal handling with bionic which uses synthetic sigret stub |
| 34 | * |
| 35 | * vineetg: Mar 2010 |
| 36 | * -GCC can't generate ZOL for core cache flush loops. |
| 37 | * Conv them into iterations based as opposed to while (start < end) types |
| 38 | * |
| 39 | * Vineetg: July 2009 |
| 40 | * -In I-cache flush routine we used to chk for aliasing for every line INV. |
| 41 | * Instead now we setup routines per cache geometry and invoke them |
| 42 | * via function pointers. |
| 43 | * |
| 44 | * Vineetg: Jan 2009 |
| 45 | * -Cache Line flush routines used to flush an extra line beyond end addr |
| 46 | * because check was while (end >= start) instead of (end > start) |
| 47 | * =Some call sites had to work around by doing -1, -4 etc to end param |
| 48 | * =Some callers didnt care. This was spec bad in case of INV routines |
| 49 | * which would discard valid data (cause of the horrible ext2 bug |
| 50 | * in ARC IDE driver) |
| 51 | * |
| 52 | * vineetg: June 11th 2008: Fixed flush_icache_range( ) |
| 53 | * -Since ARC700 caches are not coherent (I$ doesnt snoop D$) both need |
| 54 | * to be flushed, which it was not doing. |
| 55 | * -load_module( ) passes vmalloc addr (Kernel Virtual Addr) to the API, |
| 56 | * however ARC cache maintenance OPs require PHY addr. Thus need to do |
| 57 | * vmalloc_to_phy. |
| 58 | * -Also added optimisation there, that for range > PAGE SIZE we flush the |
| 59 | * entire cache in one shot rather than line by line. For e.g. a module |
| 60 | * with Code sz 600k, old code flushed 600k worth of cache (line-by-line), |
| 61 | * while cache is only 16 or 32k. |
| 62 | */ |
| 63 | |
| 64 | #include <linux/module.h> |
| 65 | #include <linux/mm.h> |
| 66 | #include <linux/sched.h> |
| 67 | #include <linux/cache.h> |
| 68 | #include <linux/mmu_context.h> |
| 69 | #include <linux/syscalls.h> |
| 70 | #include <linux/uaccess.h> |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 71 | #include <linux/pagemap.h> |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 72 | #include <asm/cacheflush.h> |
| 73 | #include <asm/cachectl.h> |
| 74 | #include <asm/setup.h> |
| 75 | |
Vineet Gupta | c3441ed | 2014-02-24 11:42:50 +0800 | [diff] [blame] | 76 | char *arc_cache_mumbojumbo(int c, char *buf, int len) |
Vineet Gupta | af61742 | 2013-01-18 15:12:24 +0530 | [diff] [blame] | 77 | { |
| 78 | int n = 0; |
Vineet Gupta | af61742 | 2013-01-18 15:12:24 +0530 | [diff] [blame] | 79 | |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 80 | #define PR_CACHE(p, cfg, str) \ |
Vineet Gupta | af61742 | 2013-01-18 15:12:24 +0530 | [diff] [blame] | 81 | if (!(p)->ver) \ |
| 82 | n += scnprintf(buf + n, len - n, str"\t\t: N/A\n"); \ |
| 83 | else \ |
| 84 | n += scnprintf(buf + n, len - n, \ |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 85 | str"\t\t: %uK, %dway/set, %uB Line, %s%s%s\n", \ |
| 86 | (p)->sz_k, (p)->assoc, (p)->line_len, \ |
| 87 | (p)->vipt ? "VIPT" : "PIPT", \ |
| 88 | (p)->alias ? " aliasing" : "", \ |
| 89 | IS_ENABLED(cfg) ? "" : " (not used)"); |
Vineet Gupta | af61742 | 2013-01-18 15:12:24 +0530 | [diff] [blame] | 90 | |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 91 | PR_CACHE(&cpuinfo_arc700[c].icache, CONFIG_ARC_HAS_ICACHE, "I-Cache"); |
| 92 | PR_CACHE(&cpuinfo_arc700[c].dcache, CONFIG_ARC_HAS_DCACHE, "D-Cache"); |
Vineet Gupta | af61742 | 2013-01-18 15:12:24 +0530 | [diff] [blame] | 93 | |
| 94 | return buf; |
| 95 | } |
| 96 | |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 97 | /* |
| 98 | * Read the Cache Build Confuration Registers, Decode them and save into |
| 99 | * the cpuinfo structure for later use. |
| 100 | * No Validation done here, simply read/convert the BCRs |
| 101 | */ |
Paul Gortmaker | ce75995 | 2013-06-24 15:30:15 -0400 | [diff] [blame] | 102 | void read_decode_cache_bcr(void) |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 103 | { |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 104 | struct cpuinfo_arc_cache *p_ic, *p_dc; |
| 105 | unsigned int cpu = smp_processor_id(); |
Vineet Gupta | da1677b | 2013-05-14 13:28:17 +0530 | [diff] [blame] | 106 | struct bcr_cache { |
| 107 | #ifdef CONFIG_CPU_BIG_ENDIAN |
| 108 | unsigned int pad:12, line_len:4, sz:4, config:4, ver:8; |
| 109 | #else |
| 110 | unsigned int ver:8, config:4, sz:4, line_len:4, pad:12; |
| 111 | #endif |
| 112 | } ibcr, dbcr; |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 113 | |
| 114 | p_ic = &cpuinfo_arc700[cpu].icache; |
| 115 | READ_BCR(ARC_REG_IC_BCR, ibcr); |
| 116 | |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 117 | if (!ibcr.ver) |
| 118 | goto dc_chk; |
| 119 | |
Vineet Gupta | 3049918 | 2013-06-15 10:21:51 +0530 | [diff] [blame] | 120 | BUG_ON(ibcr.config != 3); |
| 121 | p_ic->assoc = 2; /* Fixed to 2w set assoc */ |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 122 | p_ic->line_len = 8 << ibcr.line_len; |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 123 | p_ic->sz_k = 1 << (ibcr.sz - 1); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 124 | p_ic->ver = ibcr.ver; |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 125 | p_ic->vipt = 1; |
| 126 | p_ic->alias = p_ic->sz_k/p_ic->assoc/TO_KB(PAGE_SIZE) > 1; |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 127 | |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 128 | dc_chk: |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 129 | p_dc = &cpuinfo_arc700[cpu].dcache; |
| 130 | READ_BCR(ARC_REG_DC_BCR, dbcr); |
| 131 | |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 132 | if (!dbcr.ver) |
| 133 | return; |
| 134 | |
Vineet Gupta | 3049918 | 2013-06-15 10:21:51 +0530 | [diff] [blame] | 135 | BUG_ON(dbcr.config != 2); |
| 136 | p_dc->assoc = 4; /* Fixed to 4w set assoc */ |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 137 | p_dc->line_len = 16 << dbcr.line_len; |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 138 | p_dc->sz_k = 1 << (dbcr.sz - 1); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 139 | p_dc->ver = dbcr.ver; |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 140 | p_dc->vipt = 1; |
| 141 | p_dc->alias = p_dc->sz_k/p_dc->assoc/TO_KB(PAGE_SIZE) > 1; |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* |
| 145 | * 1. Validate the Cache Geomtery (compile time config matches hardware) |
| 146 | * 2. If I-cache suffers from aliasing, setup work arounds (difft flush rtn) |
| 147 | * (aliasing D-cache configurations are not supported YET) |
| 148 | * 3. Enable the Caches, setup default flush mode for D-Cache |
| 149 | * 3. Calculate the SHMLBA used by user space |
| 150 | */ |
Paul Gortmaker | ce75995 | 2013-06-24 15:30:15 -0400 | [diff] [blame] | 151 | void arc_cache_init(void) |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 152 | { |
Vineet Gupta | ef680cd | 2014-03-07 18:08:11 +0530 | [diff] [blame] | 153 | unsigned int __maybe_unused cpu = smp_processor_id(); |
Vineet Gupta | af61742 | 2013-01-18 15:12:24 +0530 | [diff] [blame] | 154 | char str[256]; |
| 155 | |
| 156 | printk(arc_cache_mumbojumbo(0, str, sizeof(str))); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 157 | |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 158 | if (IS_ENABLED(CONFIG_ARC_HAS_ICACHE)) { |
| 159 | struct cpuinfo_arc_cache *ic = &cpuinfo_arc700[cpu].icache; |
| 160 | |
| 161 | if (!ic->ver) |
| 162 | panic("cache support enabled but non-existent cache\n"); |
| 163 | |
Vineet Gupta | ef680cd | 2014-03-07 18:08:11 +0530 | [diff] [blame] | 164 | if (ic->line_len != L1_CACHE_BYTES) |
| 165 | panic("ICache line [%d] != kernel Config [%d]", |
| 166 | ic->line_len, L1_CACHE_BYTES); |
Vineet Gupta | af61742 | 2013-01-18 15:12:24 +0530 | [diff] [blame] | 167 | |
Vineet Gupta | ef680cd | 2014-03-07 18:08:11 +0530 | [diff] [blame] | 168 | if (ic->ver != CONFIG_ARC_MMU_VER) |
| 169 | panic("Cache ver [%d] doesn't match MMU ver [%d]\n", |
| 170 | ic->ver, CONFIG_ARC_MMU_VER); |
| 171 | } |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 172 | |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 173 | if (IS_ENABLED(CONFIG_ARC_HAS_DCACHE)) { |
| 174 | struct cpuinfo_arc_cache *dc = &cpuinfo_arc700[cpu].dcache; |
| 175 | int handled; |
| 176 | |
| 177 | if (!dc->ver) |
| 178 | panic("cache support enabled but non-existent cache\n"); |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 179 | |
Vineet Gupta | ef680cd | 2014-03-07 18:08:11 +0530 | [diff] [blame] | 180 | if (dc->line_len != L1_CACHE_BYTES) |
| 181 | panic("DCache line [%d] != kernel Config [%d]", |
| 182 | dc->line_len, L1_CACHE_BYTES); |
Vineet Gupta | 3049918 | 2013-06-15 10:21:51 +0530 | [diff] [blame] | 183 | |
Vineet Gupta | ef680cd | 2014-03-07 18:08:11 +0530 | [diff] [blame] | 184 | /* check for D-Cache aliasing */ |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 185 | handled = IS_ENABLED(CONFIG_ARC_CACHE_VIPT_ALIASING); |
Vineet Gupta | ef680cd | 2014-03-07 18:08:11 +0530 | [diff] [blame] | 186 | |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 187 | if (dc->alias && !handled) |
Vineet Gupta | ef680cd | 2014-03-07 18:08:11 +0530 | [diff] [blame] | 188 | panic("Enable CONFIG_ARC_CACHE_VIPT_ALIASING\n"); |
Vineet Gupta | da40ff4 | 2014-06-27 15:49:47 +0530 | [diff] [blame] | 189 | else if (!dc->alias && handled) |
Vineet Gupta | ef680cd | 2014-03-07 18:08:11 +0530 | [diff] [blame] | 190 | panic("Don't need CONFIG_ARC_CACHE_VIPT_ALIASING\n"); |
| 191 | } |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | #define OP_INV 0x1 |
| 195 | #define OP_FLUSH 0x2 |
| 196 | #define OP_FLUSH_N_INV 0x3 |
Vineet Gupta | bd12976 | 2013-09-05 13:43:03 +0530 | [diff] [blame] | 197 | #define OP_INV_IC 0x4 |
| 198 | |
| 199 | /* |
| 200 | * Common Helper for Line Operations on {I,D}-Cache |
| 201 | */ |
| 202 | static inline void __cache_line_loop(unsigned long paddr, unsigned long vaddr, |
| 203 | unsigned long sz, const int cacheop) |
| 204 | { |
| 205 | unsigned int aux_cmd, aux_tag; |
| 206 | int num_lines; |
Vineet Gupta | d4599ba | 2013-09-05 14:45:51 +0530 | [diff] [blame] | 207 | const int full_page_op = __builtin_constant_p(sz) && sz == PAGE_SIZE; |
Vineet Gupta | bd12976 | 2013-09-05 13:43:03 +0530 | [diff] [blame] | 208 | |
| 209 | if (cacheop == OP_INV_IC) { |
| 210 | aux_cmd = ARC_REG_IC_IVIL; |
Vineet Gupta | d753863 | 2014-04-06 06:59:51 +0530 | [diff] [blame] | 211 | #if (CONFIG_ARC_MMU_VER > 2) |
Vineet Gupta | bd12976 | 2013-09-05 13:43:03 +0530 | [diff] [blame] | 212 | aux_tag = ARC_REG_IC_PTAG; |
Vineet Gupta | d753863 | 2014-04-06 06:59:51 +0530 | [diff] [blame] | 213 | #endif |
Vineet Gupta | bd12976 | 2013-09-05 13:43:03 +0530 | [diff] [blame] | 214 | } |
| 215 | else { |
| 216 | /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */ |
| 217 | aux_cmd = cacheop & OP_INV ? ARC_REG_DC_IVDL : ARC_REG_DC_FLDL; |
Vineet Gupta | d753863 | 2014-04-06 06:59:51 +0530 | [diff] [blame] | 218 | #if (CONFIG_ARC_MMU_VER > 2) |
Vineet Gupta | bd12976 | 2013-09-05 13:43:03 +0530 | [diff] [blame] | 219 | aux_tag = ARC_REG_DC_PTAG; |
Vineet Gupta | d753863 | 2014-04-06 06:59:51 +0530 | [diff] [blame] | 220 | #endif |
Vineet Gupta | bd12976 | 2013-09-05 13:43:03 +0530 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* Ensure we properly floor/ceil the non-line aligned/sized requests |
| 224 | * and have @paddr - aligned to cache line and integral @num_lines. |
| 225 | * This however can be avoided for page sized since: |
| 226 | * -@paddr will be cache-line aligned already (being page aligned) |
| 227 | * -@sz will be integral multiple of line size (being page sized). |
| 228 | */ |
Vineet Gupta | d4599ba | 2013-09-05 14:45:51 +0530 | [diff] [blame] | 229 | if (!full_page_op) { |
Vineet Gupta | bd12976 | 2013-09-05 13:43:03 +0530 | [diff] [blame] | 230 | sz += paddr & ~CACHE_LINE_MASK; |
| 231 | paddr &= CACHE_LINE_MASK; |
| 232 | vaddr &= CACHE_LINE_MASK; |
| 233 | } |
| 234 | |
| 235 | num_lines = DIV_ROUND_UP(sz, L1_CACHE_BYTES); |
| 236 | |
| 237 | #if (CONFIG_ARC_MMU_VER <= 2) |
| 238 | /* MMUv2 and before: paddr contains stuffed vaddrs bits */ |
| 239 | paddr |= (vaddr >> PAGE_SHIFT) & 0x1F; |
Vineet Gupta | d4599ba | 2013-09-05 14:45:51 +0530 | [diff] [blame] | 240 | #else |
| 241 | /* if V-P const for loop, PTAG can be written once outside loop */ |
| 242 | if (full_page_op) |
Vineet Gupta | b053940 | 2014-03-07 13:22:22 +0530 | [diff] [blame] | 243 | write_aux_reg(aux_tag, paddr); |
Vineet Gupta | bd12976 | 2013-09-05 13:43:03 +0530 | [diff] [blame] | 244 | #endif |
| 245 | |
| 246 | while (num_lines-- > 0) { |
| 247 | #if (CONFIG_ARC_MMU_VER > 2) |
| 248 | /* MMUv3, cache ops require paddr seperately */ |
Vineet Gupta | d4599ba | 2013-09-05 14:45:51 +0530 | [diff] [blame] | 249 | if (!full_page_op) { |
| 250 | write_aux_reg(aux_tag, paddr); |
| 251 | paddr += L1_CACHE_BYTES; |
| 252 | } |
Vineet Gupta | bd12976 | 2013-09-05 13:43:03 +0530 | [diff] [blame] | 253 | |
| 254 | write_aux_reg(aux_cmd, vaddr); |
| 255 | vaddr += L1_CACHE_BYTES; |
| 256 | #else |
Vineet Gupta | b053940 | 2014-03-07 13:22:22 +0530 | [diff] [blame] | 257 | write_aux_reg(aux_cmd, paddr); |
Vineet Gupta | bd12976 | 2013-09-05 13:43:03 +0530 | [diff] [blame] | 258 | paddr += L1_CACHE_BYTES; |
Vineet Gupta | d4599ba | 2013-09-05 14:45:51 +0530 | [diff] [blame] | 259 | #endif |
Vineet Gupta | bd12976 | 2013-09-05 13:43:03 +0530 | [diff] [blame] | 260 | } |
| 261 | } |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 262 | |
| 263 | #ifdef CONFIG_ARC_HAS_DCACHE |
| 264 | |
| 265 | /*************************************************************** |
| 266 | * Machine specific helpers for Entire D-Cache or Per Line ops |
| 267 | */ |
| 268 | |
Vineet Gupta | 1b1a22b | 2014-06-29 19:03:58 +0530 | [diff] [blame] | 269 | static unsigned int __before_dc_op(const int op) |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 270 | { |
Vineet Gupta | 1b1a22b | 2014-06-29 19:03:58 +0530 | [diff] [blame] | 271 | unsigned int reg = reg; |
| 272 | |
| 273 | if (op == OP_FLUSH_N_INV) { |
| 274 | /* Dcache provides 2 cmd: FLUSH or INV |
| 275 | * INV inturn has sub-modes: DISCARD or FLUSH-BEFORE |
| 276 | * flush-n-inv is achieved by INV cmd but with IM=1 |
| 277 | * So toggle INV sub-mode depending on op request and default |
| 278 | */ |
| 279 | reg = read_aux_reg(ARC_REG_DC_CTRL); |
| 280 | write_aux_reg(ARC_REG_DC_CTRL, reg | DC_CTRL_INV_MODE_FLUSH) |
| 281 | ; |
| 282 | } |
| 283 | |
| 284 | return reg; |
| 285 | } |
| 286 | |
| 287 | static void __after_dc_op(const int op, unsigned int reg) |
| 288 | { |
| 289 | if (op & OP_FLUSH) /* flush / flush-n-inv both wait */ |
| 290 | while (read_aux_reg(ARC_REG_DC_CTRL) & DC_CTRL_FLUSH_STATUS); |
| 291 | |
| 292 | /* Switch back to default Invalidate mode */ |
| 293 | if (op == OP_FLUSH_N_INV) |
| 294 | write_aux_reg(ARC_REG_DC_CTRL, reg & ~DC_CTRL_INV_MODE_FLUSH); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Operation on Entire D-Cache |
| 299 | * @cacheop = {OP_INV, OP_FLUSH, OP_FLUSH_N_INV} |
| 300 | * Note that constant propagation ensures all the checks are gone |
| 301 | * in generated code |
| 302 | */ |
| 303 | static inline void __dc_entire_op(const int cacheop) |
| 304 | { |
Vineet Gupta | 1b1a22b | 2014-06-29 19:03:58 +0530 | [diff] [blame] | 305 | unsigned int ctrl_reg; |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 306 | int aux; |
| 307 | |
Vineet Gupta | 1b1a22b | 2014-06-29 19:03:58 +0530 | [diff] [blame] | 308 | ctrl_reg = __before_dc_op(cacheop); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 309 | |
| 310 | if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */ |
| 311 | aux = ARC_REG_DC_IVDC; |
| 312 | else |
| 313 | aux = ARC_REG_DC_FLSH; |
| 314 | |
| 315 | write_aux_reg(aux, 0x1); |
| 316 | |
Vineet Gupta | 1b1a22b | 2014-06-29 19:03:58 +0530 | [diff] [blame] | 317 | __after_dc_op(cacheop, ctrl_reg); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 318 | } |
| 319 | |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 320 | /* For kernel mappings cache operation: index is same as paddr */ |
Vineet Gupta | 6ec18a8 | 2013-05-09 15:10:18 +0530 | [diff] [blame] | 321 | #define __dc_line_op_k(p, sz, op) __dc_line_op(p, p, sz, op) |
| 322 | |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 323 | /* |
| 324 | * D-Cache : Per Line INV (discard or wback+discard) or FLUSH (wback) |
| 325 | */ |
Vineet Gupta | 6ec18a8 | 2013-05-09 15:10:18 +0530 | [diff] [blame] | 326 | static inline void __dc_line_op(unsigned long paddr, unsigned long vaddr, |
| 327 | unsigned long sz, const int cacheop) |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 328 | { |
Vineet Gupta | 1b1a22b | 2014-06-29 19:03:58 +0530 | [diff] [blame] | 329 | unsigned long flags; |
| 330 | unsigned int ctrl_reg; |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 331 | |
| 332 | local_irq_save(flags); |
| 333 | |
Vineet Gupta | 1b1a22b | 2014-06-29 19:03:58 +0530 | [diff] [blame] | 334 | ctrl_reg = __before_dc_op(cacheop); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 335 | |
Vineet Gupta | bd12976 | 2013-09-05 13:43:03 +0530 | [diff] [blame] | 336 | __cache_line_loop(paddr, vaddr, sz, cacheop); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 337 | |
Vineet Gupta | 1b1a22b | 2014-06-29 19:03:58 +0530 | [diff] [blame] | 338 | __after_dc_op(cacheop, ctrl_reg); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 339 | |
| 340 | local_irq_restore(flags); |
| 341 | } |
| 342 | |
| 343 | #else |
| 344 | |
| 345 | #define __dc_entire_op(cacheop) |
Vineet Gupta | 6ec18a8 | 2013-05-09 15:10:18 +0530 | [diff] [blame] | 346 | #define __dc_line_op(paddr, vaddr, sz, cacheop) |
| 347 | #define __dc_line_op_k(paddr, sz, cacheop) |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 348 | |
| 349 | #endif /* CONFIG_ARC_HAS_DCACHE */ |
| 350 | |
| 351 | |
| 352 | #ifdef CONFIG_ARC_HAS_ICACHE |
| 353 | |
| 354 | /* |
| 355 | * I-Cache Aliasing in ARC700 VIPT caches |
| 356 | * |
Vineet Gupta | 7f250a0 | 2013-04-12 13:08:06 +0530 | [diff] [blame] | 357 | * ARC VIPT I-cache uses vaddr to index into cache and paddr to match the tag. |
| 358 | * The orig Cache Management Module "CDU" only required paddr to invalidate a |
| 359 | * certain line since it sufficed as index in Non-Aliasing VIPT cache-geometry. |
| 360 | * Infact for distinct V1,V2,P: all of {V1-P},{V2-P},{P-P} would end up fetching |
| 361 | * the exact same line. |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 362 | * |
Vineet Gupta | 7f250a0 | 2013-04-12 13:08:06 +0530 | [diff] [blame] | 363 | * However for larger Caches (way-size > page-size) - i.e. in Aliasing config, |
| 364 | * paddr alone could not be used to correctly index the cache. |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 365 | * |
| 366 | * ------------------ |
| 367 | * MMU v1/v2 (Fixed Page Size 8k) |
| 368 | * ------------------ |
| 369 | * The solution was to provide CDU with these additonal vaddr bits. These |
Vineet Gupta | 7f250a0 | 2013-04-12 13:08:06 +0530 | [diff] [blame] | 370 | * would be bits [x:13], x would depend on cache-geometry, 13 comes from |
| 371 | * standard page size of 8k. |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 372 | * H/w folks chose [17:13] to be a future safe range, and moreso these 5 bits |
| 373 | * of vaddr could easily be "stuffed" in the paddr as bits [4:0] since the |
| 374 | * orig 5 bits of paddr were anyways ignored by CDU line ops, as they |
| 375 | * represent the offset within cache-line. The adv of using this "clumsy" |
Vineet Gupta | 7f250a0 | 2013-04-12 13:08:06 +0530 | [diff] [blame] | 376 | * interface for additional info was no new reg was needed in CDU programming |
| 377 | * model. |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 378 | * |
| 379 | * 17:13 represented the max num of bits passable, actual bits needed were |
| 380 | * fewer, based on the num-of-aliases possible. |
| 381 | * -for 2 alias possibility, only bit 13 needed (32K cache) |
| 382 | * -for 4 alias possibility, bits 14:13 needed (64K cache) |
| 383 | * |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 384 | * ------------------ |
| 385 | * MMU v3 |
| 386 | * ------------------ |
Vineet Gupta | 7f250a0 | 2013-04-12 13:08:06 +0530 | [diff] [blame] | 387 | * This ver of MMU supports variable page sizes (1k-16k): although Linux will |
| 388 | * only support 8k (default), 16k and 4k. |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 389 | * However from hardware perspective, smaller page sizes aggrevate aliasing |
| 390 | * meaning more vaddr bits needed to disambiguate the cache-line-op ; |
| 391 | * the existing scheme of piggybacking won't work for certain configurations. |
| 392 | * Two new registers IC_PTAG and DC_PTAG inttoduced. |
| 393 | * "tag" bits are provided in PTAG, index bits in existing IVIL/IVDL/FLDL regs |
| 394 | */ |
| 395 | |
| 396 | /*********************************************************** |
Vineet Gupta | 7f250a0 | 2013-04-12 13:08:06 +0530 | [diff] [blame] | 397 | * Machine specific helper for per line I-Cache invalidate. |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 398 | */ |
Vineet Gupta | af5abf1 | 2014-07-09 14:59:47 +0530 | [diff] [blame] | 399 | |
| 400 | static inline void __ic_entire_inv(void) |
| 401 | { |
| 402 | write_aux_reg(ARC_REG_IC_IVIC, 1); |
| 403 | read_aux_reg(ARC_REG_IC_CTRL); /* blocks */ |
| 404 | } |
| 405 | |
| 406 | static inline void |
| 407 | __ic_line_inv_vaddr_local(unsigned long paddr, unsigned long vaddr, |
| 408 | unsigned long sz) |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 409 | { |
| 410 | unsigned long flags; |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 411 | |
| 412 | local_irq_save(flags); |
Vineet Gupta | bd12976 | 2013-09-05 13:43:03 +0530 | [diff] [blame] | 413 | __cache_line_loop(paddr, vaddr, sz, OP_INV_IC); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 414 | local_irq_restore(flags); |
| 415 | } |
| 416 | |
Vineet Gupta | af5abf1 | 2014-07-09 14:59:47 +0530 | [diff] [blame] | 417 | #ifndef CONFIG_SMP |
Vineet Gupta | 336e199 | 2013-06-22 19:22:42 +0530 | [diff] [blame] | 418 | |
Vineet Gupta | af5abf1 | 2014-07-09 14:59:47 +0530 | [diff] [blame] | 419 | #define __ic_line_inv_vaddr(p, v, s) __ic_line_inv_vaddr_local(p, v, s) |
| 420 | |
| 421 | #else |
| 422 | |
| 423 | struct ic_inv_args { |
Vineet Gupta | 2328af0 | 2013-02-17 12:51:42 +0200 | [diff] [blame] | 424 | unsigned long paddr, vaddr; |
| 425 | int sz; |
| 426 | }; |
| 427 | |
| 428 | static void __ic_line_inv_vaddr_helper(void *info) |
| 429 | { |
Noam Camus | 014018e | 2014-09-03 14:41:11 +0300 | [diff] [blame] | 430 | struct ic_inv_args *ic_inv = info; |
Vineet Gupta | af5abf1 | 2014-07-09 14:59:47 +0530 | [diff] [blame] | 431 | |
Vineet Gupta | 2328af0 | 2013-02-17 12:51:42 +0200 | [diff] [blame] | 432 | __ic_line_inv_vaddr_local(ic_inv->paddr, ic_inv->vaddr, ic_inv->sz); |
| 433 | } |
| 434 | |
| 435 | static void __ic_line_inv_vaddr(unsigned long paddr, unsigned long vaddr, |
| 436 | unsigned long sz) |
| 437 | { |
Vineet Gupta | af5abf1 | 2014-07-09 14:59:47 +0530 | [diff] [blame] | 438 | struct ic_inv_args ic_inv = { |
| 439 | .paddr = paddr, |
| 440 | .vaddr = vaddr, |
| 441 | .sz = sz |
| 442 | }; |
| 443 | |
Vineet Gupta | 2328af0 | 2013-02-17 12:51:42 +0200 | [diff] [blame] | 444 | on_each_cpu(__ic_line_inv_vaddr_helper, &ic_inv, 1); |
| 445 | } |
Vineet Gupta | af5abf1 | 2014-07-09 14:59:47 +0530 | [diff] [blame] | 446 | |
| 447 | #endif /* CONFIG_SMP */ |
| 448 | |
| 449 | #else /* !CONFIG_ARC_HAS_ICACHE */ |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 450 | |
Vineet Gupta | 336e199 | 2013-06-22 19:22:42 +0530 | [diff] [blame] | 451 | #define __ic_entire_inv() |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 452 | #define __ic_line_inv_vaddr(pstart, vstart, sz) |
| 453 | |
| 454 | #endif /* CONFIG_ARC_HAS_ICACHE */ |
| 455 | |
| 456 | |
| 457 | /*********************************************************** |
| 458 | * Exported APIs |
| 459 | */ |
| 460 | |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 461 | /* |
| 462 | * Handle cache congruency of kernel and userspace mappings of page when kernel |
| 463 | * writes-to/reads-from |
| 464 | * |
| 465 | * The idea is to defer flushing of kernel mapping after a WRITE, possible if: |
| 466 | * -dcache is NOT aliasing, hence any U/K-mappings of page are congruent |
| 467 | * -U-mapping doesn't exist yet for page (finalised in update_mmu_cache) |
| 468 | * -In SMP, if hardware caches are coherent |
| 469 | * |
| 470 | * There's a corollary case, where kernel READs from a userspace mapped page. |
| 471 | * If the U-mapping is not congruent to to K-mapping, former needs flushing. |
| 472 | */ |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 473 | void flush_dcache_page(struct page *page) |
| 474 | { |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 475 | struct address_space *mapping; |
| 476 | |
| 477 | if (!cache_is_vipt_aliasing()) { |
Vineet Gupta | 2ed21da | 2013-05-13 17:23:58 +0530 | [diff] [blame] | 478 | clear_bit(PG_dc_clean, &page->flags); |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 479 | return; |
| 480 | } |
| 481 | |
| 482 | /* don't handle anon pages here */ |
| 483 | mapping = page_mapping(page); |
| 484 | if (!mapping) |
| 485 | return; |
| 486 | |
| 487 | /* |
| 488 | * pagecache page, file not yet mapped to userspace |
| 489 | * Make a note that K-mapping is dirty |
| 490 | */ |
| 491 | if (!mapping_mapped(mapping)) { |
Vineet Gupta | 2ed21da | 2013-05-13 17:23:58 +0530 | [diff] [blame] | 492 | clear_bit(PG_dc_clean, &page->flags); |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 493 | } else if (page_mapped(page)) { |
| 494 | |
| 495 | /* kernel reading from page with U-mapping */ |
| 496 | void *paddr = page_address(page); |
| 497 | unsigned long vaddr = page->index << PAGE_CACHE_SHIFT; |
| 498 | |
| 499 | if (addr_not_cache_congruent(paddr, vaddr)) |
| 500 | __flush_dcache_page(paddr, vaddr); |
| 501 | } |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 502 | } |
| 503 | EXPORT_SYMBOL(flush_dcache_page); |
| 504 | |
| 505 | |
| 506 | void dma_cache_wback_inv(unsigned long start, unsigned long sz) |
| 507 | { |
Vineet Gupta | 6ec18a8 | 2013-05-09 15:10:18 +0530 | [diff] [blame] | 508 | __dc_line_op_k(start, sz, OP_FLUSH_N_INV); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 509 | } |
| 510 | EXPORT_SYMBOL(dma_cache_wback_inv); |
| 511 | |
| 512 | void dma_cache_inv(unsigned long start, unsigned long sz) |
| 513 | { |
Vineet Gupta | 6ec18a8 | 2013-05-09 15:10:18 +0530 | [diff] [blame] | 514 | __dc_line_op_k(start, sz, OP_INV); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 515 | } |
| 516 | EXPORT_SYMBOL(dma_cache_inv); |
| 517 | |
| 518 | void dma_cache_wback(unsigned long start, unsigned long sz) |
| 519 | { |
Vineet Gupta | 6ec18a8 | 2013-05-09 15:10:18 +0530 | [diff] [blame] | 520 | __dc_line_op_k(start, sz, OP_FLUSH); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 521 | } |
| 522 | EXPORT_SYMBOL(dma_cache_wback); |
| 523 | |
| 524 | /* |
Vineet Gupta | 7586bf72 | 2013-04-12 12:18:25 +0530 | [diff] [blame] | 525 | * This is API for making I/D Caches consistent when modifying |
| 526 | * kernel code (loadable modules, kprobes, kgdb...) |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 527 | * This is called on insmod, with kernel virtual address for CODE of |
| 528 | * the module. ARC cache maintenance ops require PHY address thus we |
| 529 | * need to convert vmalloc addr to PHY addr |
| 530 | */ |
| 531 | void flush_icache_range(unsigned long kstart, unsigned long kend) |
| 532 | { |
| 533 | unsigned int tot_sz, off, sz; |
| 534 | unsigned long phy, pfn; |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 535 | |
| 536 | /* printk("Kernel Cache Cohenercy: %lx to %lx\n",kstart, kend); */ |
| 537 | |
| 538 | /* This is not the right API for user virtual address */ |
| 539 | if (kstart < TASK_SIZE) { |
| 540 | BUG_ON("Flush icache range for user virtual addr space"); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | /* Shortcut for bigger flush ranges. |
| 545 | * Here we don't care if this was kernel virtual or phy addr |
| 546 | */ |
| 547 | tot_sz = kend - kstart; |
| 548 | if (tot_sz > PAGE_SIZE) { |
| 549 | flush_cache_all(); |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | /* Case: Kernel Phy addr (0x8000_0000 onwards) */ |
| 554 | if (likely(kstart > PAGE_OFFSET)) { |
Vineet Gupta | 7586bf72 | 2013-04-12 12:18:25 +0530 | [diff] [blame] | 555 | /* |
| 556 | * The 2nd arg despite being paddr will be used to index icache |
| 557 | * This is OK since no alternate virtual mappings will exist |
| 558 | * given the callers for this case: kprobe/kgdb in built-in |
| 559 | * kernel code only. |
| 560 | */ |
Vineet Gupta | 94bad1a | 2013-04-12 12:20:23 +0530 | [diff] [blame] | 561 | __sync_icache_dcache(kstart, kstart, kend - kstart); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 562 | return; |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * Case: Kernel Vaddr (0x7000_0000 to 0x7fff_ffff) |
| 567 | * (1) ARC Cache Maintenance ops only take Phy addr, hence special |
| 568 | * handling of kernel vaddr. |
| 569 | * |
| 570 | * (2) Despite @tot_sz being < PAGE_SIZE (bigger cases handled already), |
| 571 | * it still needs to handle a 2 page scenario, where the range |
| 572 | * straddles across 2 virtual pages and hence need for loop |
| 573 | */ |
| 574 | while (tot_sz > 0) { |
| 575 | off = kstart % PAGE_SIZE; |
| 576 | pfn = vmalloc_to_pfn((void *)kstart); |
| 577 | phy = (pfn << PAGE_SHIFT) + off; |
| 578 | sz = min_t(unsigned int, tot_sz, PAGE_SIZE - off); |
Vineet Gupta | 94bad1a | 2013-04-12 12:20:23 +0530 | [diff] [blame] | 579 | __sync_icache_dcache(phy, kstart, sz); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 580 | kstart += sz; |
| 581 | tot_sz -= sz; |
| 582 | } |
| 583 | } |
Pranith Kumar | e356030 | 2014-08-29 15:19:09 -0700 | [diff] [blame] | 584 | EXPORT_SYMBOL(flush_icache_range); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 585 | |
| 586 | /* |
Vineet Gupta | 94bad1a | 2013-04-12 12:20:23 +0530 | [diff] [blame] | 587 | * General purpose helper to make I and D cache lines consistent. |
| 588 | * @paddr is phy addr of region |
Vineet Gupta | 4b06ff3 | 2013-07-10 11:40:27 +0530 | [diff] [blame] | 589 | * @vaddr is typically user vaddr (breakpoint) or kernel vaddr (vmalloc) |
| 590 | * However in one instance, when called by kprobe (for a breakpt in |
Vineet Gupta | 94bad1a | 2013-04-12 12:20:23 +0530 | [diff] [blame] | 591 | * builtin kernel code) @vaddr will be paddr only, meaning CDU operation will |
| 592 | * use a paddr to index the cache (despite VIPT). This is fine since since a |
Vineet Gupta | 4b06ff3 | 2013-07-10 11:40:27 +0530 | [diff] [blame] | 593 | * builtin kernel page will not have any virtual mappings. |
| 594 | * kprobe on loadable module will be kernel vaddr. |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 595 | */ |
Vineet Gupta | 94bad1a | 2013-04-12 12:20:23 +0530 | [diff] [blame] | 596 | void __sync_icache_dcache(unsigned long paddr, unsigned long vaddr, int len) |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 597 | { |
Vineet Gupta | f538881 | 2013-05-16 12:19:29 +0530 | [diff] [blame] | 598 | __dc_line_op(paddr, vaddr, len, OP_FLUSH_N_INV); |
Vineet Gupta | 2328af0 | 2013-02-17 12:51:42 +0200 | [diff] [blame] | 599 | __ic_line_inv_vaddr(paddr, vaddr, len); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 600 | } |
| 601 | |
Vineet Gupta | 24603fd | 2013-04-11 18:36:35 +0530 | [diff] [blame] | 602 | /* wrapper to compile time eliminate alignment checks in flush loop */ |
| 603 | void __inv_icache_page(unsigned long paddr, unsigned long vaddr) |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 604 | { |
Vineet Gupta | 24603fd | 2013-04-11 18:36:35 +0530 | [diff] [blame] | 605 | __ic_line_inv_vaddr(paddr, vaddr, PAGE_SIZE); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 606 | } |
| 607 | |
Vineet Gupta | 6ec18a8 | 2013-05-09 15:10:18 +0530 | [diff] [blame] | 608 | /* |
| 609 | * wrapper to clearout kernel or userspace mappings of a page |
| 610 | * For kernel mappings @vaddr == @paddr |
| 611 | */ |
Vineet Gupta | de2a852 | 2013-05-09 21:55:27 +0530 | [diff] [blame] | 612 | void ___flush_dcache_page(unsigned long paddr, unsigned long vaddr) |
Vineet Gupta | eacd0e95 | 2013-04-16 14:10:48 +0530 | [diff] [blame] | 613 | { |
Vineet Gupta | 6ec18a8 | 2013-05-09 15:10:18 +0530 | [diff] [blame] | 614 | __dc_line_op(paddr, vaddr & PAGE_MASK, PAGE_SIZE, OP_FLUSH_N_INV); |
Vineet Gupta | eacd0e95 | 2013-04-16 14:10:48 +0530 | [diff] [blame] | 615 | } |
| 616 | |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 617 | noinline void flush_cache_all(void) |
| 618 | { |
| 619 | unsigned long flags; |
| 620 | |
| 621 | local_irq_save(flags); |
| 622 | |
Vineet Gupta | 336e199 | 2013-06-22 19:22:42 +0530 | [diff] [blame] | 623 | __ic_entire_inv(); |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 624 | __dc_entire_op(OP_FLUSH_N_INV); |
| 625 | |
| 626 | local_irq_restore(flags); |
| 627 | |
| 628 | } |
| 629 | |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 630 | #ifdef CONFIG_ARC_CACHE_VIPT_ALIASING |
| 631 | |
| 632 | void flush_cache_mm(struct mm_struct *mm) |
| 633 | { |
| 634 | flush_cache_all(); |
| 635 | } |
| 636 | |
| 637 | void flush_cache_page(struct vm_area_struct *vma, unsigned long u_vaddr, |
| 638 | unsigned long pfn) |
| 639 | { |
| 640 | unsigned int paddr = pfn << PAGE_SHIFT; |
| 641 | |
Vineet Gupta | 5971bc7 | 2013-05-16 12:23:31 +0530 | [diff] [blame] | 642 | u_vaddr &= PAGE_MASK; |
| 643 | |
| 644 | ___flush_dcache_page(paddr, u_vaddr); |
| 645 | |
| 646 | if (vma->vm_flags & VM_EXEC) |
| 647 | __inv_icache_page(paddr, u_vaddr); |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void flush_cache_range(struct vm_area_struct *vma, unsigned long start, |
| 651 | unsigned long end) |
| 652 | { |
| 653 | flush_cache_all(); |
| 654 | } |
| 655 | |
Vineet Gupta | 7bb66f6 | 2013-05-25 14:04:25 +0530 | [diff] [blame] | 656 | void flush_anon_page(struct vm_area_struct *vma, struct page *page, |
| 657 | unsigned long u_vaddr) |
| 658 | { |
| 659 | /* TBD: do we really need to clear the kernel mapping */ |
| 660 | __flush_dcache_page(page_address(page), u_vaddr); |
| 661 | __flush_dcache_page(page_address(page), page_address(page)); |
| 662 | |
| 663 | } |
| 664 | |
| 665 | #endif |
| 666 | |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 667 | void copy_user_highpage(struct page *to, struct page *from, |
| 668 | unsigned long u_vaddr, struct vm_area_struct *vma) |
| 669 | { |
| 670 | void *kfrom = page_address(from); |
| 671 | void *kto = page_address(to); |
| 672 | int clean_src_k_mappings = 0; |
| 673 | |
| 674 | /* |
| 675 | * If SRC page was already mapped in userspace AND it's U-mapping is |
| 676 | * not congruent with K-mapping, sync former to physical page so that |
| 677 | * K-mapping in memcpy below, sees the right data |
| 678 | * |
| 679 | * Note that while @u_vaddr refers to DST page's userspace vaddr, it is |
| 680 | * equally valid for SRC page as well |
| 681 | */ |
| 682 | if (page_mapped(from) && addr_not_cache_congruent(kfrom, u_vaddr)) { |
| 683 | __flush_dcache_page(kfrom, u_vaddr); |
| 684 | clean_src_k_mappings = 1; |
| 685 | } |
| 686 | |
| 687 | copy_page(kto, kfrom); |
| 688 | |
| 689 | /* |
| 690 | * Mark DST page K-mapping as dirty for a later finalization by |
| 691 | * update_mmu_cache(). Although the finalization could have been done |
| 692 | * here as well (given that both vaddr/paddr are available). |
| 693 | * But update_mmu_cache() already has code to do that for other |
| 694 | * non copied user pages (e.g. read faults which wire in pagecache page |
| 695 | * directly). |
| 696 | */ |
Vineet Gupta | 2ed21da | 2013-05-13 17:23:58 +0530 | [diff] [blame] | 697 | clear_bit(PG_dc_clean, &to->flags); |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 698 | |
| 699 | /* |
| 700 | * if SRC was already usermapped and non-congruent to kernel mapping |
| 701 | * sync the kernel mapping back to physical page |
| 702 | */ |
| 703 | if (clean_src_k_mappings) { |
| 704 | __flush_dcache_page(kfrom, kfrom); |
Vineet Gupta | 2ed21da | 2013-05-13 17:23:58 +0530 | [diff] [blame] | 705 | set_bit(PG_dc_clean, &from->flags); |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 706 | } else { |
Vineet Gupta | 2ed21da | 2013-05-13 17:23:58 +0530 | [diff] [blame] | 707 | clear_bit(PG_dc_clean, &from->flags); |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
| 711 | void clear_user_page(void *to, unsigned long u_vaddr, struct page *page) |
| 712 | { |
| 713 | clear_page(to); |
Vineet Gupta | 2ed21da | 2013-05-13 17:23:58 +0530 | [diff] [blame] | 714 | clear_bit(PG_dc_clean, &page->flags); |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 715 | } |
| 716 | |
Vineet Gupta | 4102b53 | 2013-05-09 21:54:51 +0530 | [diff] [blame] | 717 | |
Vineet Gupta | 95d6976 | 2013-01-18 15:12:19 +0530 | [diff] [blame] | 718 | /********************************************************************** |
| 719 | * Explicit Cache flush request from user space via syscall |
| 720 | * Needed for JITs which generate code on the fly |
| 721 | */ |
| 722 | SYSCALL_DEFINE3(cacheflush, uint32_t, start, uint32_t, sz, uint32_t, flags) |
| 723 | { |
| 724 | /* TBD: optimize this */ |
| 725 | flush_cache_all(); |
| 726 | return 0; |
| 727 | } |