Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Processor cache information made available to userspace via sysfs; |
| 4 | * intended to be compatible with x86 intel_cacheinfo implementation. |
| 5 | * |
| 6 | * Copyright 2008 IBM Corporation |
| 7 | * Author: Nathan Lynch |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Nathan Lynch | e2b3c16 | 2019-06-27 00:15:34 -0500 | [diff] [blame] | 10 | #define pr_fmt(fmt) "cacheinfo: " fmt |
| 11 | |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 12 | #include <linux/cpu.h> |
| 13 | #include <linux/cpumask.h> |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/kobject.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/notifier.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/percpu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 21 | #include <asm/prom.h> |
Gautham R. Shenoy | 500fe5f | 2018-10-11 11:03:03 +0530 | [diff] [blame] | 22 | #include <asm/cputhreads.h> |
| 23 | #include <asm/smp.h> |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 24 | |
| 25 | #include "cacheinfo.h" |
| 26 | |
| 27 | /* per-cpu object for tracking: |
| 28 | * - a "cache" kobject for the top-level directory |
| 29 | * - a list of "index" objects representing the cpu's local cache hierarchy |
| 30 | */ |
| 31 | struct cache_dir { |
| 32 | struct kobject *kobj; /* bare (not embedded) kobject for cache |
| 33 | * directory */ |
| 34 | struct cache_index_dir *index; /* list of index objects */ |
| 35 | }; |
| 36 | |
| 37 | /* "index" object: each cpu's cache directory has an index |
| 38 | * subdirectory corresponding to a cache object associated with the |
| 39 | * cpu. This object's lifetime is managed via the embedded kobject. |
| 40 | */ |
| 41 | struct cache_index_dir { |
| 42 | struct kobject kobj; |
| 43 | struct cache_index_dir *next; /* next index in parent directory */ |
| 44 | struct cache *cache; |
| 45 | }; |
| 46 | |
| 47 | /* Template for determining which OF properties to query for a given |
| 48 | * cache type */ |
| 49 | struct cache_type_info { |
| 50 | const char *name; |
| 51 | const char *size_prop; |
| 52 | |
| 53 | /* Allow for both [di]-cache-line-size and |
| 54 | * [di]-cache-block-size properties. According to the PowerPC |
| 55 | * Processor binding, -line-size should be provided if it |
| 56 | * differs from the cache block size (that which is operated |
| 57 | * on by cache instructions), so we look for -line-size first. |
| 58 | * See cache_get_line_size(). */ |
| 59 | |
| 60 | const char *line_size_props[2]; |
| 61 | const char *nr_sets_prop; |
| 62 | }; |
| 63 | |
| 64 | /* These are used to index the cache_type_info array. */ |
Dave Olson | f7e9e35 | 2015-04-02 21:28:45 -0700 | [diff] [blame] | 65 | #define CACHE_TYPE_UNIFIED 0 /* cache-size, cache-block-size, etc. */ |
| 66 | #define CACHE_TYPE_UNIFIED_D 1 /* d-cache-size, d-cache-block-size, etc */ |
| 67 | #define CACHE_TYPE_INSTRUCTION 2 |
| 68 | #define CACHE_TYPE_DATA 3 |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 69 | |
| 70 | static const struct cache_type_info cache_type_info[] = { |
| 71 | { |
Dave Olson | f7e9e35 | 2015-04-02 21:28:45 -0700 | [diff] [blame] | 72 | /* Embedded systems that use cache-size, cache-block-size, |
| 73 | * etc. for the Unified (typically L2) cache. */ |
| 74 | .name = "Unified", |
| 75 | .size_prop = "cache-size", |
| 76 | .line_size_props = { "cache-line-size", |
| 77 | "cache-block-size", }, |
| 78 | .nr_sets_prop = "cache-sets", |
| 79 | }, |
| 80 | { |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 81 | /* PowerPC Processor binding says the [di]-cache-* |
| 82 | * must be equal on unified caches, so just use |
| 83 | * d-cache properties. */ |
| 84 | .name = "Unified", |
| 85 | .size_prop = "d-cache-size", |
| 86 | .line_size_props = { "d-cache-line-size", |
| 87 | "d-cache-block-size", }, |
| 88 | .nr_sets_prop = "d-cache-sets", |
| 89 | }, |
| 90 | { |
| 91 | .name = "Instruction", |
| 92 | .size_prop = "i-cache-size", |
| 93 | .line_size_props = { "i-cache-line-size", |
| 94 | "i-cache-block-size", }, |
| 95 | .nr_sets_prop = "i-cache-sets", |
| 96 | }, |
| 97 | { |
| 98 | .name = "Data", |
| 99 | .size_prop = "d-cache-size", |
| 100 | .line_size_props = { "d-cache-line-size", |
| 101 | "d-cache-block-size", }, |
| 102 | .nr_sets_prop = "d-cache-sets", |
| 103 | }, |
| 104 | }; |
| 105 | |
| 106 | /* Cache object: each instance of this corresponds to a distinct cache |
| 107 | * in the system. There are separate objects for Harvard caches: one |
| 108 | * each for instruction and data, and each refers to the same OF node. |
| 109 | * The refcount of the OF node is elevated for the lifetime of the |
| 110 | * cache object. A cache object is released when its shared_cpu_map |
| 111 | * is cleared (see cache_cpu_clear). |
| 112 | * |
| 113 | * A cache object is on two lists: an unsorted global list |
| 114 | * (cache_list) of cache objects; and a singly-linked list |
| 115 | * representing the local cache hierarchy, which is ordered by level |
| 116 | * (e.g. L1d -> L1i -> L2 -> L3). |
| 117 | */ |
| 118 | struct cache { |
| 119 | struct device_node *ofnode; /* OF node for this cache, may be cpu */ |
| 120 | struct cpumask shared_cpu_map; /* online CPUs using this cache */ |
| 121 | int type; /* split cache disambiguation */ |
| 122 | int level; /* level not explicit in device tree */ |
| 123 | struct list_head list; /* global list of cache objects */ |
| 124 | struct cache *next_local; /* next cache of >= level */ |
| 125 | }; |
| 126 | |
Nathan Lynch | fc7a9fe | 2009-01-09 13:12:44 +0000 | [diff] [blame] | 127 | static DEFINE_PER_CPU(struct cache_dir *, cache_dir_pcpu); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 128 | |
| 129 | /* traversal/modification of this list occurs only at cpu hotplug time; |
| 130 | * access is serialized by cpu hotplug locking |
| 131 | */ |
| 132 | static LIST_HEAD(cache_list); |
| 133 | |
| 134 | static struct cache_index_dir *kobj_to_cache_index_dir(struct kobject *k) |
| 135 | { |
| 136 | return container_of(k, struct cache_index_dir, kobj); |
| 137 | } |
| 138 | |
| 139 | static const char *cache_type_string(const struct cache *cache) |
| 140 | { |
| 141 | return cache_type_info[cache->type].name; |
| 142 | } |
| 143 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 144 | static void cache_init(struct cache *cache, int type, int level, |
| 145 | struct device_node *ofnode) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 146 | { |
| 147 | cache->type = type; |
| 148 | cache->level = level; |
| 149 | cache->ofnode = of_node_get(ofnode); |
| 150 | INIT_LIST_HEAD(&cache->list); |
| 151 | list_add(&cache->list, &cache_list); |
| 152 | } |
| 153 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 154 | static struct cache *new_cache(int type, int level, struct device_node *ofnode) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 155 | { |
| 156 | struct cache *cache; |
| 157 | |
| 158 | cache = kzalloc(sizeof(*cache), GFP_KERNEL); |
| 159 | if (cache) |
| 160 | cache_init(cache, type, level, ofnode); |
| 161 | |
| 162 | return cache; |
| 163 | } |
| 164 | |
| 165 | static void release_cache_debugcheck(struct cache *cache) |
| 166 | { |
| 167 | struct cache *iter; |
| 168 | |
| 169 | list_for_each_entry(iter, &cache_list, list) |
| 170 | WARN_ONCE(iter->next_local == cache, |
Nathan Lynch | be6f885e | 2019-06-27 00:15:35 -0500 | [diff] [blame^] | 171 | "cache for %pOFP(%s) refers to cache for %pOFP(%s)\n", |
Rob Herring | b7c670d | 2017-08-21 10:16:47 -0500 | [diff] [blame] | 172 | iter->ofnode, |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 173 | cache_type_string(iter), |
Rob Herring | b7c670d | 2017-08-21 10:16:47 -0500 | [diff] [blame] | 174 | cache->ofnode, |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 175 | cache_type_string(cache)); |
| 176 | } |
| 177 | |
| 178 | static void release_cache(struct cache *cache) |
| 179 | { |
| 180 | if (!cache) |
| 181 | return; |
| 182 | |
Nathan Lynch | be6f885e | 2019-06-27 00:15:35 -0500 | [diff] [blame^] | 183 | pr_debug("freeing L%d %s cache for %pOFP\n", cache->level, |
Rob Herring | b7c670d | 2017-08-21 10:16:47 -0500 | [diff] [blame] | 184 | cache_type_string(cache), cache->ofnode); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 185 | |
| 186 | release_cache_debugcheck(cache); |
| 187 | list_del(&cache->list); |
| 188 | of_node_put(cache->ofnode); |
| 189 | kfree(cache); |
| 190 | } |
| 191 | |
| 192 | static void cache_cpu_set(struct cache *cache, int cpu) |
| 193 | { |
| 194 | struct cache *next = cache; |
| 195 | |
| 196 | while (next) { |
| 197 | WARN_ONCE(cpumask_test_cpu(cpu, &next->shared_cpu_map), |
Nathan Lynch | be6f885e | 2019-06-27 00:15:35 -0500 | [diff] [blame^] | 198 | "CPU %i already accounted in %pOFP(%s)\n", |
Rob Herring | b7c670d | 2017-08-21 10:16:47 -0500 | [diff] [blame] | 199 | cpu, next->ofnode, |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 200 | cache_type_string(next)); |
| 201 | cpumask_set_cpu(cpu, &next->shared_cpu_map); |
| 202 | next = next->next_local; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static int cache_size(const struct cache *cache, unsigned int *ret) |
| 207 | { |
| 208 | const char *propname; |
Anton Blanchard | d10bd84 | 2013-08-07 02:01:37 +1000 | [diff] [blame] | 209 | const __be32 *cache_size; |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 210 | |
| 211 | propname = cache_type_info[cache->type].size_prop; |
| 212 | |
| 213 | cache_size = of_get_property(cache->ofnode, propname, NULL); |
| 214 | if (!cache_size) |
| 215 | return -ENODEV; |
| 216 | |
Anton Blanchard | d10bd84 | 2013-08-07 02:01:37 +1000 | [diff] [blame] | 217 | *ret = of_read_number(cache_size, 1); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int cache_size_kb(const struct cache *cache, unsigned int *ret) |
| 222 | { |
| 223 | unsigned int size; |
| 224 | |
| 225 | if (cache_size(cache, &size)) |
| 226 | return -ENODEV; |
| 227 | |
| 228 | *ret = size / 1024; |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | /* not cache_line_size() because that's a macro in include/linux/cache.h */ |
| 233 | static int cache_get_line_size(const struct cache *cache, unsigned int *ret) |
| 234 | { |
Anton Blanchard | d10bd84 | 2013-08-07 02:01:37 +1000 | [diff] [blame] | 235 | const __be32 *line_size; |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 236 | int i, lim; |
| 237 | |
| 238 | lim = ARRAY_SIZE(cache_type_info[cache->type].line_size_props); |
| 239 | |
| 240 | for (i = 0; i < lim; i++) { |
| 241 | const char *propname; |
| 242 | |
| 243 | propname = cache_type_info[cache->type].line_size_props[i]; |
| 244 | line_size = of_get_property(cache->ofnode, propname, NULL); |
| 245 | if (line_size) |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | if (!line_size) |
| 250 | return -ENODEV; |
| 251 | |
Anton Blanchard | d10bd84 | 2013-08-07 02:01:37 +1000 | [diff] [blame] | 252 | *ret = of_read_number(line_size, 1); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int cache_nr_sets(const struct cache *cache, unsigned int *ret) |
| 257 | { |
| 258 | const char *propname; |
Anton Blanchard | d10bd84 | 2013-08-07 02:01:37 +1000 | [diff] [blame] | 259 | const __be32 *nr_sets; |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 260 | |
| 261 | propname = cache_type_info[cache->type].nr_sets_prop; |
| 262 | |
| 263 | nr_sets = of_get_property(cache->ofnode, propname, NULL); |
| 264 | if (!nr_sets) |
| 265 | return -ENODEV; |
| 266 | |
Anton Blanchard | d10bd84 | 2013-08-07 02:01:37 +1000 | [diff] [blame] | 267 | *ret = of_read_number(nr_sets, 1); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int cache_associativity(const struct cache *cache, unsigned int *ret) |
| 272 | { |
| 273 | unsigned int line_size; |
| 274 | unsigned int nr_sets; |
| 275 | unsigned int size; |
| 276 | |
| 277 | if (cache_nr_sets(cache, &nr_sets)) |
| 278 | goto err; |
| 279 | |
| 280 | /* If the cache is fully associative, there is no need to |
| 281 | * check the other properties. |
| 282 | */ |
| 283 | if (nr_sets == 1) { |
| 284 | *ret = 0; |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | if (cache_get_line_size(cache, &line_size)) |
| 289 | goto err; |
| 290 | if (cache_size(cache, &size)) |
| 291 | goto err; |
| 292 | |
| 293 | if (!(nr_sets > 0 && size > 0 && line_size > 0)) |
| 294 | goto err; |
| 295 | |
| 296 | *ret = (size / nr_sets) / line_size; |
| 297 | return 0; |
| 298 | err: |
| 299 | return -ENODEV; |
| 300 | } |
| 301 | |
| 302 | /* helper for dealing with split caches */ |
| 303 | static struct cache *cache_find_first_sibling(struct cache *cache) |
| 304 | { |
| 305 | struct cache *iter; |
| 306 | |
Dave Olson | f7e9e35 | 2015-04-02 21:28:45 -0700 | [diff] [blame] | 307 | if (cache->type == CACHE_TYPE_UNIFIED || |
| 308 | cache->type == CACHE_TYPE_UNIFIED_D) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 309 | return cache; |
| 310 | |
| 311 | list_for_each_entry(iter, &cache_list, list) |
| 312 | if (iter->ofnode == cache->ofnode && iter->next_local == cache) |
| 313 | return iter; |
| 314 | |
| 315 | return cache; |
| 316 | } |
| 317 | |
| 318 | /* return the first cache on a local list matching node */ |
| 319 | static struct cache *cache_lookup_by_node(const struct device_node *node) |
| 320 | { |
| 321 | struct cache *cache = NULL; |
| 322 | struct cache *iter; |
| 323 | |
| 324 | list_for_each_entry(iter, &cache_list, list) { |
| 325 | if (iter->ofnode != node) |
| 326 | continue; |
| 327 | cache = cache_find_first_sibling(iter); |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | return cache; |
| 332 | } |
| 333 | |
| 334 | static bool cache_node_is_unified(const struct device_node *np) |
| 335 | { |
| 336 | return of_get_property(np, "cache-unified", NULL); |
| 337 | } |
| 338 | |
Dave Olson | f7e9e35 | 2015-04-02 21:28:45 -0700 | [diff] [blame] | 339 | /* |
| 340 | * Unified caches can have two different sets of tags. Most embedded |
| 341 | * use cache-size, etc. for the unified cache size, but open firmware systems |
| 342 | * use d-cache-size, etc. Check on initialization for which type we have, and |
| 343 | * return the appropriate structure type. Assume it's embedded if it isn't |
| 344 | * open firmware. If it's yet a 3rd type, then there will be missing entries |
| 345 | * in /sys/devices/system/cpu/cpu0/cache/index2/, and this code will need |
| 346 | * to be extended further. |
| 347 | */ |
| 348 | static int cache_is_unified_d(const struct device_node *np) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 349 | { |
Dave Olson | f7e9e35 | 2015-04-02 21:28:45 -0700 | [diff] [blame] | 350 | return of_get_property(np, |
| 351 | cache_type_info[CACHE_TYPE_UNIFIED_D].size_prop, NULL) ? |
| 352 | CACHE_TYPE_UNIFIED_D : CACHE_TYPE_UNIFIED; |
| 353 | } |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 354 | |
Dave Olson | f7e9e35 | 2015-04-02 21:28:45 -0700 | [diff] [blame] | 355 | static struct cache *cache_do_one_devnode_unified(struct device_node *node, int level) |
| 356 | { |
Nathan Lynch | be6f885e | 2019-06-27 00:15:35 -0500 | [diff] [blame^] | 357 | pr_debug("creating L%d ucache for %pOFP\n", level, node); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 358 | |
Dave Olson | f7e9e35 | 2015-04-02 21:28:45 -0700 | [diff] [blame] | 359 | return new_cache(cache_is_unified_d(node), level, node); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 362 | static struct cache *cache_do_one_devnode_split(struct device_node *node, |
| 363 | int level) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 364 | { |
| 365 | struct cache *dcache, *icache; |
| 366 | |
Nathan Lynch | be6f885e | 2019-06-27 00:15:35 -0500 | [diff] [blame^] | 367 | pr_debug("creating L%d dcache and icache for %pOFP\n", level, |
Rob Herring | b7c670d | 2017-08-21 10:16:47 -0500 | [diff] [blame] | 368 | node); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 369 | |
| 370 | dcache = new_cache(CACHE_TYPE_DATA, level, node); |
| 371 | icache = new_cache(CACHE_TYPE_INSTRUCTION, level, node); |
| 372 | |
| 373 | if (!dcache || !icache) |
| 374 | goto err; |
| 375 | |
| 376 | dcache->next_local = icache; |
| 377 | |
| 378 | return dcache; |
| 379 | err: |
| 380 | release_cache(dcache); |
| 381 | release_cache(icache); |
| 382 | return NULL; |
| 383 | } |
| 384 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 385 | static struct cache *cache_do_one_devnode(struct device_node *node, int level) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 386 | { |
| 387 | struct cache *cache; |
| 388 | |
| 389 | if (cache_node_is_unified(node)) |
| 390 | cache = cache_do_one_devnode_unified(node, level); |
| 391 | else |
| 392 | cache = cache_do_one_devnode_split(node, level); |
| 393 | |
| 394 | return cache; |
| 395 | } |
| 396 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 397 | static struct cache *cache_lookup_or_instantiate(struct device_node *node, |
| 398 | int level) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 399 | { |
| 400 | struct cache *cache; |
| 401 | |
| 402 | cache = cache_lookup_by_node(node); |
| 403 | |
| 404 | WARN_ONCE(cache && cache->level != level, |
| 405 | "cache level mismatch on lookup (got %d, expected %d)\n", |
| 406 | cache->level, level); |
| 407 | |
| 408 | if (!cache) |
| 409 | cache = cache_do_one_devnode(node, level); |
| 410 | |
| 411 | return cache; |
| 412 | } |
| 413 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 414 | static void link_cache_lists(struct cache *smaller, struct cache *bigger) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 415 | { |
| 416 | while (smaller->next_local) { |
| 417 | if (smaller->next_local == bigger) |
| 418 | return; /* already linked */ |
| 419 | smaller = smaller->next_local; |
| 420 | } |
| 421 | |
| 422 | smaller->next_local = bigger; |
| 423 | } |
| 424 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 425 | static void do_subsidiary_caches_debugcheck(struct cache *cache) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 426 | { |
| 427 | WARN_ON_ONCE(cache->level != 1); |
Rob Herring | e5480bd | 2018-11-16 16:11:00 -0600 | [diff] [blame] | 428 | WARN_ON_ONCE(!of_node_is_type(cache->ofnode, "cpu")); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 431 | static void do_subsidiary_caches(struct cache *cache) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 432 | { |
| 433 | struct device_node *subcache_node; |
| 434 | int level = cache->level; |
| 435 | |
| 436 | do_subsidiary_caches_debugcheck(cache); |
| 437 | |
| 438 | while ((subcache_node = of_find_next_cache_node(cache->ofnode))) { |
| 439 | struct cache *subcache; |
| 440 | |
| 441 | level++; |
| 442 | subcache = cache_lookup_or_instantiate(subcache_node, level); |
| 443 | of_node_put(subcache_node); |
| 444 | if (!subcache) |
| 445 | break; |
| 446 | |
| 447 | link_cache_lists(cache, subcache); |
| 448 | cache = subcache; |
| 449 | } |
| 450 | } |
| 451 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 452 | static struct cache *cache_chain_instantiate(unsigned int cpu_id) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 453 | { |
| 454 | struct device_node *cpu_node; |
| 455 | struct cache *cpu_cache = NULL; |
| 456 | |
| 457 | pr_debug("creating cache object(s) for CPU %i\n", cpu_id); |
| 458 | |
| 459 | cpu_node = of_get_cpu_node(cpu_id, NULL); |
| 460 | WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id); |
| 461 | if (!cpu_node) |
| 462 | goto out; |
| 463 | |
| 464 | cpu_cache = cache_lookup_or_instantiate(cpu_node, 1); |
| 465 | if (!cpu_cache) |
| 466 | goto out; |
| 467 | |
| 468 | do_subsidiary_caches(cpu_cache); |
| 469 | |
| 470 | cache_cpu_set(cpu_cache, cpu_id); |
| 471 | out: |
| 472 | of_node_put(cpu_node); |
| 473 | |
| 474 | return cpu_cache; |
| 475 | } |
| 476 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 477 | static struct cache_dir *cacheinfo_create_cache_dir(unsigned int cpu_id) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 478 | { |
| 479 | struct cache_dir *cache_dir; |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 480 | struct device *dev; |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 481 | struct kobject *kobj = NULL; |
| 482 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 483 | dev = get_cpu_device(cpu_id); |
| 484 | WARN_ONCE(!dev, "no dev for CPU %i\n", cpu_id); |
| 485 | if (!dev) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 486 | goto err; |
| 487 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 488 | kobj = kobject_create_and_add("cache", &dev->kobj); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 489 | if (!kobj) |
| 490 | goto err; |
| 491 | |
| 492 | cache_dir = kzalloc(sizeof(*cache_dir), GFP_KERNEL); |
| 493 | if (!cache_dir) |
| 494 | goto err; |
| 495 | |
| 496 | cache_dir->kobj = kobj; |
| 497 | |
Nathan Lynch | fc7a9fe | 2009-01-09 13:12:44 +0000 | [diff] [blame] | 498 | WARN_ON_ONCE(per_cpu(cache_dir_pcpu, cpu_id) != NULL); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 499 | |
Nathan Lynch | fc7a9fe | 2009-01-09 13:12:44 +0000 | [diff] [blame] | 500 | per_cpu(cache_dir_pcpu, cpu_id) = cache_dir; |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 501 | |
| 502 | return cache_dir; |
| 503 | err: |
| 504 | kobject_put(kobj); |
| 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | static void cache_index_release(struct kobject *kobj) |
| 509 | { |
| 510 | struct cache_index_dir *index; |
| 511 | |
| 512 | index = kobj_to_cache_index_dir(kobj); |
| 513 | |
| 514 | pr_debug("freeing index directory for L%d %s cache\n", |
| 515 | index->cache->level, cache_type_string(index->cache)); |
| 516 | |
| 517 | kfree(index); |
| 518 | } |
| 519 | |
| 520 | static ssize_t cache_index_show(struct kobject *k, struct attribute *attr, char *buf) |
| 521 | { |
| 522 | struct kobj_attribute *kobj_attr; |
| 523 | |
| 524 | kobj_attr = container_of(attr, struct kobj_attribute, attr); |
| 525 | |
| 526 | return kobj_attr->show(k, kobj_attr, buf); |
| 527 | } |
| 528 | |
| 529 | static struct cache *index_kobj_to_cache(struct kobject *k) |
| 530 | { |
| 531 | struct cache_index_dir *index; |
| 532 | |
| 533 | index = kobj_to_cache_index_dir(k); |
| 534 | |
| 535 | return index->cache; |
| 536 | } |
| 537 | |
| 538 | static ssize_t size_show(struct kobject *k, struct kobj_attribute *attr, char *buf) |
| 539 | { |
| 540 | unsigned int size_kb; |
| 541 | struct cache *cache; |
| 542 | |
| 543 | cache = index_kobj_to_cache(k); |
| 544 | |
| 545 | if (cache_size_kb(cache, &size_kb)) |
| 546 | return -ENODEV; |
| 547 | |
| 548 | return sprintf(buf, "%uK\n", size_kb); |
| 549 | } |
| 550 | |
| 551 | static struct kobj_attribute cache_size_attr = |
| 552 | __ATTR(size, 0444, size_show, NULL); |
| 553 | |
| 554 | |
| 555 | static ssize_t line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf) |
| 556 | { |
| 557 | unsigned int line_size; |
| 558 | struct cache *cache; |
| 559 | |
| 560 | cache = index_kobj_to_cache(k); |
| 561 | |
| 562 | if (cache_get_line_size(cache, &line_size)) |
| 563 | return -ENODEV; |
| 564 | |
| 565 | return sprintf(buf, "%u\n", line_size); |
| 566 | } |
| 567 | |
| 568 | static struct kobj_attribute cache_line_size_attr = |
| 569 | __ATTR(coherency_line_size, 0444, line_size_show, NULL); |
| 570 | |
| 571 | static ssize_t nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf) |
| 572 | { |
| 573 | unsigned int nr_sets; |
| 574 | struct cache *cache; |
| 575 | |
| 576 | cache = index_kobj_to_cache(k); |
| 577 | |
| 578 | if (cache_nr_sets(cache, &nr_sets)) |
| 579 | return -ENODEV; |
| 580 | |
| 581 | return sprintf(buf, "%u\n", nr_sets); |
| 582 | } |
| 583 | |
| 584 | static struct kobj_attribute cache_nr_sets_attr = |
| 585 | __ATTR(number_of_sets, 0444, nr_sets_show, NULL); |
| 586 | |
| 587 | static ssize_t associativity_show(struct kobject *k, struct kobj_attribute *attr, char *buf) |
| 588 | { |
| 589 | unsigned int associativity; |
| 590 | struct cache *cache; |
| 591 | |
| 592 | cache = index_kobj_to_cache(k); |
| 593 | |
| 594 | if (cache_associativity(cache, &associativity)) |
| 595 | return -ENODEV; |
| 596 | |
| 597 | return sprintf(buf, "%u\n", associativity); |
| 598 | } |
| 599 | |
| 600 | static struct kobj_attribute cache_assoc_attr = |
| 601 | __ATTR(ways_of_associativity, 0444, associativity_show, NULL); |
| 602 | |
| 603 | static ssize_t type_show(struct kobject *k, struct kobj_attribute *attr, char *buf) |
| 604 | { |
| 605 | struct cache *cache; |
| 606 | |
| 607 | cache = index_kobj_to_cache(k); |
| 608 | |
| 609 | return sprintf(buf, "%s\n", cache_type_string(cache)); |
| 610 | } |
| 611 | |
| 612 | static struct kobj_attribute cache_type_attr = |
| 613 | __ATTR(type, 0444, type_show, NULL); |
| 614 | |
| 615 | static ssize_t level_show(struct kobject *k, struct kobj_attribute *attr, char *buf) |
| 616 | { |
| 617 | struct cache_index_dir *index; |
| 618 | struct cache *cache; |
| 619 | |
| 620 | index = kobj_to_cache_index_dir(k); |
| 621 | cache = index->cache; |
| 622 | |
| 623 | return sprintf(buf, "%d\n", cache->level); |
| 624 | } |
| 625 | |
| 626 | static struct kobj_attribute cache_level_attr = |
| 627 | __ATTR(level, 0444, level_show, NULL); |
| 628 | |
Gautham R. Shenoy | 500fe5f | 2018-10-11 11:03:03 +0530 | [diff] [blame] | 629 | static unsigned int index_dir_to_cpu(struct cache_index_dir *index) |
| 630 | { |
| 631 | struct kobject *index_dir_kobj = &index->kobj; |
| 632 | struct kobject *cache_dir_kobj = index_dir_kobj->parent; |
| 633 | struct kobject *cpu_dev_kobj = cache_dir_kobj->parent; |
| 634 | struct device *dev = kobj_to_dev(cpu_dev_kobj); |
| 635 | |
| 636 | return dev->id; |
| 637 | } |
| 638 | |
| 639 | /* |
| 640 | * On big-core systems, each core has two groups of CPUs each of which |
| 641 | * has its own L1-cache. The thread-siblings which share l1-cache with |
| 642 | * @cpu can be obtained via cpu_smallcore_mask(). |
| 643 | */ |
| 644 | static const struct cpumask *get_big_core_shared_cpu_map(int cpu, struct cache *cache) |
| 645 | { |
| 646 | if (cache->level == 1) |
| 647 | return cpu_smallcore_mask(cpu); |
| 648 | |
| 649 | return &cache->shared_cpu_map; |
| 650 | } |
| 651 | |
Srikar Dronamraju | 74b7492 | 2020-06-29 16:07:02 +0530 | [diff] [blame] | 652 | static ssize_t |
| 653 | show_shared_cpumap(struct kobject *k, struct kobj_attribute *attr, char *buf, bool list) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 654 | { |
| 655 | struct cache_index_dir *index; |
| 656 | struct cache *cache; |
Gautham R. Shenoy | 500fe5f | 2018-10-11 11:03:03 +0530 | [diff] [blame] | 657 | const struct cpumask *mask; |
Srikar Dronamraju | 5658cf0 | 2020-06-29 16:07:01 +0530 | [diff] [blame] | 658 | int cpu; |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 659 | |
| 660 | index = kobj_to_cache_index_dir(k); |
| 661 | cache = index->cache; |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 662 | |
Gautham R. Shenoy | 500fe5f | 2018-10-11 11:03:03 +0530 | [diff] [blame] | 663 | if (has_big_cores) { |
| 664 | cpu = index_dir_to_cpu(index); |
| 665 | mask = get_big_core_shared_cpu_map(cpu, cache); |
| 666 | } else { |
| 667 | mask = &cache->shared_cpu_map; |
| 668 | } |
| 669 | |
Srikar Dronamraju | 74b7492 | 2020-06-29 16:07:02 +0530 | [diff] [blame] | 670 | return cpumap_print_to_pagebuf(list, buf, mask); |
| 671 | } |
| 672 | |
| 673 | static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *attr, char *buf) |
| 674 | { |
Srikar Dronamraju | a87a77c | 2020-06-29 16:07:03 +0530 | [diff] [blame] | 675 | return show_shared_cpumap(k, attr, buf, false); |
| 676 | } |
| 677 | |
| 678 | static ssize_t shared_cpu_list_show(struct kobject *k, struct kobj_attribute *attr, char *buf) |
| 679 | { |
| 680 | return show_shared_cpumap(k, attr, buf, true); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | static struct kobj_attribute cache_shared_cpu_map_attr = |
| 684 | __ATTR(shared_cpu_map, 0444, shared_cpu_map_show, NULL); |
| 685 | |
Srikar Dronamraju | a87a77c | 2020-06-29 16:07:03 +0530 | [diff] [blame] | 686 | static struct kobj_attribute cache_shared_cpu_list_attr = |
| 687 | __ATTR(shared_cpu_list, 0444, shared_cpu_list_show, NULL); |
| 688 | |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 689 | /* Attributes which should always be created -- the kobject/sysfs core |
| 690 | * does this automatically via kobj_type->default_attrs. This is the |
| 691 | * minimum data required to uniquely identify a cache. |
| 692 | */ |
| 693 | static struct attribute *cache_index_default_attrs[] = { |
| 694 | &cache_type_attr.attr, |
| 695 | &cache_level_attr.attr, |
| 696 | &cache_shared_cpu_map_attr.attr, |
Srikar Dronamraju | a87a77c | 2020-06-29 16:07:03 +0530 | [diff] [blame] | 697 | &cache_shared_cpu_list_attr.attr, |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 698 | NULL, |
| 699 | }; |
| 700 | |
| 701 | /* Attributes which should be created if the cache device node has the |
| 702 | * right properties -- see cacheinfo_create_index_opt_attrs |
| 703 | */ |
| 704 | static struct kobj_attribute *cache_index_opt_attrs[] = { |
| 705 | &cache_size_attr, |
| 706 | &cache_line_size_attr, |
| 707 | &cache_nr_sets_attr, |
| 708 | &cache_assoc_attr, |
| 709 | }; |
| 710 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 711 | static const struct sysfs_ops cache_index_ops = { |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 712 | .show = cache_index_show, |
| 713 | }; |
| 714 | |
| 715 | static struct kobj_type cache_index_type = { |
| 716 | .release = cache_index_release, |
| 717 | .sysfs_ops = &cache_index_ops, |
| 718 | .default_attrs = cache_index_default_attrs, |
| 719 | }; |
| 720 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 721 | static void cacheinfo_create_index_opt_attrs(struct cache_index_dir *dir) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 722 | { |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 723 | const char *cache_type; |
| 724 | struct cache *cache; |
| 725 | char *buf; |
| 726 | int i; |
| 727 | |
| 728 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 729 | if (!buf) |
| 730 | return; |
| 731 | |
| 732 | cache = dir->cache; |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 733 | cache_type = cache_type_string(cache); |
| 734 | |
| 735 | /* We don't want to create an attribute that can't provide a |
| 736 | * meaningful value. Check the return value of each optional |
| 737 | * attribute's ->show method before registering the |
| 738 | * attribute. |
| 739 | */ |
| 740 | for (i = 0; i < ARRAY_SIZE(cache_index_opt_attrs); i++) { |
| 741 | struct kobj_attribute *attr; |
| 742 | ssize_t rc; |
| 743 | |
| 744 | attr = cache_index_opt_attrs[i]; |
| 745 | |
| 746 | rc = attr->show(&dir->kobj, attr, buf); |
| 747 | if (rc <= 0) { |
| 748 | pr_debug("not creating %s attribute for " |
Nathan Lynch | be6f885e | 2019-06-27 00:15:35 -0500 | [diff] [blame^] | 749 | "%pOFP(%s) (rc = %zd)\n", |
Rob Herring | b7c670d | 2017-08-21 10:16:47 -0500 | [diff] [blame] | 750 | attr->attr.name, cache->ofnode, |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 751 | cache_type, rc); |
| 752 | continue; |
| 753 | } |
| 754 | if (sysfs_create_file(&dir->kobj, &attr->attr)) |
Nathan Lynch | be6f885e | 2019-06-27 00:15:35 -0500 | [diff] [blame^] | 755 | pr_debug("could not create %s attribute for %pOFP(%s)\n", |
Rob Herring | b7c670d | 2017-08-21 10:16:47 -0500 | [diff] [blame] | 756 | attr->attr.name, cache->ofnode, cache_type); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | kfree(buf); |
| 760 | } |
| 761 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 762 | static void cacheinfo_create_index_dir(struct cache *cache, int index, |
| 763 | struct cache_dir *cache_dir) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 764 | { |
| 765 | struct cache_index_dir *index_dir; |
| 766 | int rc; |
| 767 | |
| 768 | index_dir = kzalloc(sizeof(*index_dir), GFP_KERNEL); |
| 769 | if (!index_dir) |
Tobin C. Harding | 7e80397 | 2019-04-30 11:09:23 +1000 | [diff] [blame] | 770 | return; |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 771 | |
| 772 | index_dir->cache = cache; |
| 773 | |
| 774 | rc = kobject_init_and_add(&index_dir->kobj, &cache_index_type, |
| 775 | cache_dir->kobj, "index%d", index); |
Tobin C. Harding | 7e80397 | 2019-04-30 11:09:23 +1000 | [diff] [blame] | 776 | if (rc) { |
| 777 | kobject_put(&index_dir->kobj); |
Tobin C. Harding | 7e80397 | 2019-04-30 11:09:23 +1000 | [diff] [blame] | 778 | return; |
| 779 | } |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 780 | |
| 781 | index_dir->next = cache_dir->index; |
| 782 | cache_dir->index = index_dir; |
| 783 | |
| 784 | cacheinfo_create_index_opt_attrs(index_dir); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 787 | static void cacheinfo_sysfs_populate(unsigned int cpu_id, |
| 788 | struct cache *cache_list) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 789 | { |
| 790 | struct cache_dir *cache_dir; |
| 791 | struct cache *cache; |
| 792 | int index = 0; |
| 793 | |
| 794 | cache_dir = cacheinfo_create_cache_dir(cpu_id); |
| 795 | if (!cache_dir) |
| 796 | return; |
| 797 | |
| 798 | cache = cache_list; |
| 799 | while (cache) { |
| 800 | cacheinfo_create_index_dir(cache, index, cache_dir); |
| 801 | index++; |
| 802 | cache = cache->next_local; |
| 803 | } |
| 804 | } |
| 805 | |
Paul Gortmaker | 061d19f | 2013-06-24 15:30:09 -0400 | [diff] [blame] | 806 | void cacheinfo_cpu_online(unsigned int cpu_id) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 807 | { |
| 808 | struct cache *cache; |
| 809 | |
| 810 | cache = cache_chain_instantiate(cpu_id); |
| 811 | if (!cache) |
| 812 | return; |
| 813 | |
| 814 | cacheinfo_sysfs_populate(cpu_id, cache); |
| 815 | } |
| 816 | |
Haren Myneni | 6b36ba8 | 2014-02-25 20:02:18 -0800 | [diff] [blame] | 817 | /* functions needed to remove cache entry for cpu offline or suspend/resume */ |
| 818 | |
| 819 | #if (defined(CONFIG_PPC_PSERIES) && defined(CONFIG_SUSPEND)) || \ |
| 820 | defined(CONFIG_HOTPLUG_CPU) |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 821 | |
| 822 | static struct cache *cache_lookup_by_cpu(unsigned int cpu_id) |
| 823 | { |
| 824 | struct device_node *cpu_node; |
| 825 | struct cache *cache; |
| 826 | |
| 827 | cpu_node = of_get_cpu_node(cpu_id, NULL); |
| 828 | WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id); |
| 829 | if (!cpu_node) |
| 830 | return NULL; |
| 831 | |
| 832 | cache = cache_lookup_by_node(cpu_node); |
| 833 | of_node_put(cpu_node); |
| 834 | |
| 835 | return cache; |
| 836 | } |
| 837 | |
| 838 | static void remove_index_dirs(struct cache_dir *cache_dir) |
| 839 | { |
| 840 | struct cache_index_dir *index; |
| 841 | |
| 842 | index = cache_dir->index; |
| 843 | |
| 844 | while (index) { |
| 845 | struct cache_index_dir *next; |
| 846 | |
| 847 | next = index->next; |
| 848 | kobject_put(&index->kobj); |
| 849 | index = next; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | static void remove_cache_dir(struct cache_dir *cache_dir) |
| 854 | { |
| 855 | remove_index_dirs(cache_dir); |
| 856 | |
Paul Mackerras | 91b973f | 2014-01-18 21:14:47 +1100 | [diff] [blame] | 857 | /* Remove cache dir from sysfs */ |
| 858 | kobject_del(cache_dir->kobj); |
| 859 | |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 860 | kobject_put(cache_dir->kobj); |
| 861 | |
| 862 | kfree(cache_dir); |
| 863 | } |
| 864 | |
| 865 | static void cache_cpu_clear(struct cache *cache, int cpu) |
| 866 | { |
| 867 | while (cache) { |
| 868 | struct cache *next = cache->next_local; |
| 869 | |
| 870 | WARN_ONCE(!cpumask_test_cpu(cpu, &cache->shared_cpu_map), |
Nathan Lynch | be6f885e | 2019-06-27 00:15:35 -0500 | [diff] [blame^] | 871 | "CPU %i not accounted in %pOFP(%s)\n", |
Rob Herring | b7c670d | 2017-08-21 10:16:47 -0500 | [diff] [blame] | 872 | cpu, cache->ofnode, |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 873 | cache_type_string(cache)); |
| 874 | |
| 875 | cpumask_clear_cpu(cpu, &cache->shared_cpu_map); |
| 876 | |
| 877 | /* Release the cache object if all the cpus using it |
| 878 | * are offline */ |
| 879 | if (cpumask_empty(&cache->shared_cpu_map)) |
| 880 | release_cache(cache); |
| 881 | |
| 882 | cache = next; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | void cacheinfo_cpu_offline(unsigned int cpu_id) |
| 887 | { |
| 888 | struct cache_dir *cache_dir; |
| 889 | struct cache *cache; |
| 890 | |
| 891 | /* Prevent userspace from seeing inconsistent state - remove |
| 892 | * the sysfs hierarchy first */ |
Nathan Lynch | fc7a9fe | 2009-01-09 13:12:44 +0000 | [diff] [blame] | 893 | cache_dir = per_cpu(cache_dir_pcpu, cpu_id); |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 894 | |
| 895 | /* careful, sysfs population may have failed */ |
| 896 | if (cache_dir) |
| 897 | remove_cache_dir(cache_dir); |
| 898 | |
Nathan Lynch | fc7a9fe | 2009-01-09 13:12:44 +0000 | [diff] [blame] | 899 | per_cpu(cache_dir_pcpu, cpu_id) = NULL; |
Nathan Lynch | 93197a3 | 2008-12-23 18:55:54 +0000 | [diff] [blame] | 900 | |
| 901 | /* clear the CPU's bit in its cache chain, possibly freeing |
| 902 | * cache objects */ |
| 903 | cache = cache_lookup_by_cpu(cpu_id); |
| 904 | if (cache) |
| 905 | cache_cpu_clear(cache, cpu_id); |
| 906 | } |
Nathan Lynch | d4aa219 | 2019-06-11 23:45:04 -0500 | [diff] [blame] | 907 | |
| 908 | void cacheinfo_teardown(void) |
| 909 | { |
| 910 | unsigned int cpu; |
| 911 | |
| 912 | lockdep_assert_cpus_held(); |
| 913 | |
| 914 | for_each_online_cpu(cpu) |
| 915 | cacheinfo_cpu_offline(cpu); |
| 916 | } |
| 917 | |
| 918 | void cacheinfo_rebuild(void) |
| 919 | { |
| 920 | unsigned int cpu; |
| 921 | |
| 922 | lockdep_assert_cpus_held(); |
| 923 | |
| 924 | for_each_online_cpu(cpu) |
| 925 | cacheinfo_cpu_online(cpu); |
| 926 | } |
| 927 | |
Haren Myneni | 6b36ba8 | 2014-02-25 20:02:18 -0800 | [diff] [blame] | 928 | #endif /* (CONFIG_PPC_PSERIES && CONFIG_SUSPEND) || CONFIG_HOTPLUG_CPU */ |