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