cacheinfo.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. // SPDX-License-Identifier: GPL-2.0-only
  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
  8. */
  9. #define pr_fmt(fmt) "cacheinfo: " fmt
  10. #include <linux/cpu.h>
  11. #include <linux/cpumask.h>
  12. #include <linux/kernel.h>
  13. #include <linux/kobject.h>
  14. #include <linux/list.h>
  15. #include <linux/notifier.h>
  16. #include <linux/of.h>
  17. #include <linux/percpu.h>
  18. #include <linux/slab.h>
  19. #include <asm/cputhreads.h>
  20. #include <asm/smp.h>
  21. #include "cacheinfo.h"
  22. /* per-cpu object for tracking:
  23. * - a "cache" kobject for the top-level directory
  24. * - a list of "index" objects representing the cpu's local cache hierarchy
  25. */
  26. struct cache_dir {
  27. struct kobject *kobj; /* bare (not embedded) kobject for cache
  28. * directory */
  29. struct cache_index_dir *index; /* list of index objects */
  30. };
  31. /* "index" object: each cpu's cache directory has an index
  32. * subdirectory corresponding to a cache object associated with the
  33. * cpu. This object's lifetime is managed via the embedded kobject.
  34. */
  35. struct cache_index_dir {
  36. struct kobject kobj;
  37. struct cache_index_dir *next; /* next index in parent directory */
  38. struct cache *cache;
  39. };
  40. /* Template for determining which OF properties to query for a given
  41. * cache type */
  42. struct cache_type_info {
  43. const char *name;
  44. const char *size_prop;
  45. /* Allow for both [di]-cache-line-size and
  46. * [di]-cache-block-size properties. According to the PowerPC
  47. * Processor binding, -line-size should be provided if it
  48. * differs from the cache block size (that which is operated
  49. * on by cache instructions), so we look for -line-size first.
  50. * See cache_get_line_size(). */
  51. const char *line_size_props[2];
  52. const char *nr_sets_prop;
  53. };
  54. /* These are used to index the cache_type_info array. */
  55. #define CACHE_TYPE_UNIFIED 0 /* cache-size, cache-block-size, etc. */
  56. #define CACHE_TYPE_UNIFIED_D 1 /* d-cache-size, d-cache-block-size, etc */
  57. #define CACHE_TYPE_INSTRUCTION 2
  58. #define CACHE_TYPE_DATA 3
  59. static const struct cache_type_info cache_type_info[] = {
  60. {
  61. /* Embedded systems that use cache-size, cache-block-size,
  62. * etc. for the Unified (typically L2) cache. */
  63. .name = "Unified",
  64. .size_prop = "cache-size",
  65. .line_size_props = { "cache-line-size",
  66. "cache-block-size", },
  67. .nr_sets_prop = "cache-sets",
  68. },
  69. {
  70. /* PowerPC Processor binding says the [di]-cache-*
  71. * must be equal on unified caches, so just use
  72. * d-cache properties. */
  73. .name = "Unified",
  74. .size_prop = "d-cache-size",
  75. .line_size_props = { "d-cache-line-size",
  76. "d-cache-block-size", },
  77. .nr_sets_prop = "d-cache-sets",
  78. },
  79. {
  80. .name = "Instruction",
  81. .size_prop = "i-cache-size",
  82. .line_size_props = { "i-cache-line-size",
  83. "i-cache-block-size", },
  84. .nr_sets_prop = "i-cache-sets",
  85. },
  86. {
  87. .name = "Data",
  88. .size_prop = "d-cache-size",
  89. .line_size_props = { "d-cache-line-size",
  90. "d-cache-block-size", },
  91. .nr_sets_prop = "d-cache-sets",
  92. },
  93. };
  94. /* Cache object: each instance of this corresponds to a distinct cache
  95. * in the system. There are separate objects for Harvard caches: one
  96. * each for instruction and data, and each refers to the same OF node.
  97. * The refcount of the OF node is elevated for the lifetime of the
  98. * cache object. A cache object is released when its shared_cpu_map
  99. * is cleared (see cache_cpu_clear).
  100. *
  101. * A cache object is on two lists: an unsorted global list
  102. * (cache_list) of cache objects; and a singly-linked list
  103. * representing the local cache hierarchy, which is ordered by level
  104. * (e.g. L1d -> L1i -> L2 -> L3).
  105. */
  106. struct cache {
  107. struct device_node *ofnode; /* OF node for this cache, may be cpu */
  108. struct cpumask shared_cpu_map; /* online CPUs using this cache */
  109. int type; /* split cache disambiguation */
  110. int level; /* level not explicit in device tree */
  111. int group_id; /* id of the group of threads that share this cache */
  112. struct list_head list; /* global list of cache objects */
  113. struct cache *next_local; /* next cache of >= level */
  114. };
  115. static DEFINE_PER_CPU(struct cache_dir *, cache_dir_pcpu);
  116. /* traversal/modification of this list occurs only at cpu hotplug time;
  117. * access is serialized by cpu hotplug locking
  118. */
  119. static LIST_HEAD(cache_list);
  120. static struct cache_index_dir *kobj_to_cache_index_dir(struct kobject *k)
  121. {
  122. return container_of(k, struct cache_index_dir, kobj);
  123. }
  124. static const char *cache_type_string(const struct cache *cache)
  125. {
  126. return cache_type_info[cache->type].name;
  127. }
  128. static void cache_init(struct cache *cache, int type, int level,
  129. struct device_node *ofnode, int group_id)
  130. {
  131. cache->type = type;
  132. cache->level = level;
  133. cache->ofnode = of_node_get(ofnode);
  134. cache->group_id = group_id;
  135. INIT_LIST_HEAD(&cache->list);
  136. list_add(&cache->list, &cache_list);
  137. }
  138. static struct cache *new_cache(int type, int level,
  139. struct device_node *ofnode, int group_id)
  140. {
  141. struct cache *cache;
  142. cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  143. if (cache)
  144. cache_init(cache, type, level, ofnode, group_id);
  145. return cache;
  146. }
  147. static void release_cache_debugcheck(struct cache *cache)
  148. {
  149. struct cache *iter;
  150. list_for_each_entry(iter, &cache_list, list)
  151. WARN_ONCE(iter->next_local == cache,
  152. "cache for %pOFP(%s) refers to cache for %pOFP(%s)\n",
  153. iter->ofnode,
  154. cache_type_string(iter),
  155. cache->ofnode,
  156. cache_type_string(cache));
  157. }
  158. static void release_cache(struct cache *cache)
  159. {
  160. if (!cache)
  161. return;
  162. pr_debug("freeing L%d %s cache for %pOFP\n", cache->level,
  163. cache_type_string(cache), cache->ofnode);
  164. release_cache_debugcheck(cache);
  165. list_del(&cache->list);
  166. of_node_put(cache->ofnode);
  167. kfree(cache);
  168. }
  169. static void cache_cpu_set(struct cache *cache, int cpu)
  170. {
  171. struct cache *next = cache;
  172. while (next) {
  173. WARN_ONCE(cpumask_test_cpu(cpu, &next->shared_cpu_map),
  174. "CPU %i already accounted in %pOFP(%s)\n",
  175. cpu, next->ofnode,
  176. cache_type_string(next));
  177. cpumask_set_cpu(cpu, &next->shared_cpu_map);
  178. next = next->next_local;
  179. }
  180. }
  181. static int cache_size(const struct cache *cache, unsigned int *ret)
  182. {
  183. const char *propname;
  184. const __be32 *cache_size;
  185. propname = cache_type_info[cache->type].size_prop;
  186. cache_size = of_get_property(cache->ofnode, propname, NULL);
  187. if (!cache_size)
  188. return -ENODEV;
  189. *ret = of_read_number(cache_size, 1);
  190. return 0;
  191. }
  192. static int cache_size_kb(const struct cache *cache, unsigned int *ret)
  193. {
  194. unsigned int size;
  195. if (cache_size(cache, &size))
  196. return -ENODEV;
  197. *ret = size / 1024;
  198. return 0;
  199. }
  200. /* not cache_line_size() because that's a macro in include/linux/cache.h */
  201. static int cache_get_line_size(const struct cache *cache, unsigned int *ret)
  202. {
  203. const __be32 *line_size;
  204. int i, lim;
  205. lim = ARRAY_SIZE(cache_type_info[cache->type].line_size_props);
  206. for (i = 0; i < lim; i++) {
  207. const char *propname;
  208. propname = cache_type_info[cache->type].line_size_props[i];
  209. line_size = of_get_property(cache->ofnode, propname, NULL);
  210. if (line_size)
  211. break;
  212. }
  213. if (!line_size)
  214. return -ENODEV;
  215. *ret = of_read_number(line_size, 1);
  216. return 0;
  217. }
  218. static int cache_nr_sets(const struct cache *cache, unsigned int *ret)
  219. {
  220. const char *propname;
  221. const __be32 *nr_sets;
  222. propname = cache_type_info[cache->type].nr_sets_prop;
  223. nr_sets = of_get_property(cache->ofnode, propname, NULL);
  224. if (!nr_sets)
  225. return -ENODEV;
  226. *ret = of_read_number(nr_sets, 1);
  227. return 0;
  228. }
  229. static int cache_associativity(const struct cache *cache, unsigned int *ret)
  230. {
  231. unsigned int line_size;
  232. unsigned int nr_sets;
  233. unsigned int size;
  234. if (cache_nr_sets(cache, &nr_sets))
  235. goto err;
  236. /* If the cache is fully associative, there is no need to
  237. * check the other properties.
  238. */
  239. if (nr_sets == 1) {
  240. *ret = 0;
  241. return 0;
  242. }
  243. if (cache_get_line_size(cache, &line_size))
  244. goto err;
  245. if (cache_size(cache, &size))
  246. goto err;
  247. if (!(nr_sets > 0 && size > 0 && line_size > 0))
  248. goto err;
  249. *ret = (size / nr_sets) / line_size;
  250. return 0;
  251. err:
  252. return -ENODEV;
  253. }
  254. /* helper for dealing with split caches */
  255. static struct cache *cache_find_first_sibling(struct cache *cache)
  256. {
  257. struct cache *iter;
  258. if (cache->type == CACHE_TYPE_UNIFIED ||
  259. cache->type == CACHE_TYPE_UNIFIED_D)
  260. return cache;
  261. list_for_each_entry(iter, &cache_list, list)
  262. if (iter->ofnode == cache->ofnode &&
  263. iter->group_id == cache->group_id &&
  264. iter->next_local == cache)
  265. return iter;
  266. return cache;
  267. }
  268. /* return the first cache on a local list matching node and thread-group id */
  269. static struct cache *cache_lookup_by_node_group(const struct device_node *node,
  270. int group_id)
  271. {
  272. struct cache *cache = NULL;
  273. struct cache *iter;
  274. list_for_each_entry(iter, &cache_list, list) {
  275. if (iter->ofnode != node ||
  276. iter->group_id != group_id)
  277. continue;
  278. cache = cache_find_first_sibling(iter);
  279. break;
  280. }
  281. return cache;
  282. }
  283. static bool cache_node_is_unified(const struct device_node *np)
  284. {
  285. return of_get_property(np, "cache-unified", NULL);
  286. }
  287. /*
  288. * Unified caches can have two different sets of tags. Most embedded
  289. * use cache-size, etc. for the unified cache size, but open firmware systems
  290. * use d-cache-size, etc. Check on initialization for which type we have, and
  291. * return the appropriate structure type. Assume it's embedded if it isn't
  292. * open firmware. If it's yet a 3rd type, then there will be missing entries
  293. * in /sys/devices/system/cpu/cpu0/cache/index2/, and this code will need
  294. * to be extended further.
  295. */
  296. static int cache_is_unified_d(const struct device_node *np)
  297. {
  298. return of_get_property(np,
  299. cache_type_info[CACHE_TYPE_UNIFIED_D].size_prop, NULL) ?
  300. CACHE_TYPE_UNIFIED_D : CACHE_TYPE_UNIFIED;
  301. }
  302. static struct cache *cache_do_one_devnode_unified(struct device_node *node, int group_id,
  303. int level)
  304. {
  305. pr_debug("creating L%d ucache for %pOFP\n", level, node);
  306. return new_cache(cache_is_unified_d(node), level, node, group_id);
  307. }
  308. static struct cache *cache_do_one_devnode_split(struct device_node *node, int group_id,
  309. int level)
  310. {
  311. struct cache *dcache, *icache;
  312. pr_debug("creating L%d dcache and icache for %pOFP\n", level,
  313. node);
  314. dcache = new_cache(CACHE_TYPE_DATA, level, node, group_id);
  315. icache = new_cache(CACHE_TYPE_INSTRUCTION, level, node, group_id);
  316. if (!dcache || !icache)
  317. goto err;
  318. dcache->next_local = icache;
  319. return dcache;
  320. err:
  321. release_cache(dcache);
  322. release_cache(icache);
  323. return NULL;
  324. }
  325. static struct cache *cache_do_one_devnode(struct device_node *node, int group_id, int level)
  326. {
  327. struct cache *cache;
  328. if (cache_node_is_unified(node))
  329. cache = cache_do_one_devnode_unified(node, group_id, level);
  330. else
  331. cache = cache_do_one_devnode_split(node, group_id, level);
  332. return cache;
  333. }
  334. static struct cache *cache_lookup_or_instantiate(struct device_node *node,
  335. int group_id,
  336. int level)
  337. {
  338. struct cache *cache;
  339. cache = cache_lookup_by_node_group(node, group_id);
  340. WARN_ONCE(cache && cache->level != level,
  341. "cache level mismatch on lookup (got %d, expected %d)\n",
  342. cache->level, level);
  343. if (!cache)
  344. cache = cache_do_one_devnode(node, group_id, level);
  345. return cache;
  346. }
  347. static void link_cache_lists(struct cache *smaller, struct cache *bigger)
  348. {
  349. while (smaller->next_local) {
  350. if (smaller->next_local == bigger)
  351. return; /* already linked */
  352. smaller = smaller->next_local;
  353. }
  354. smaller->next_local = bigger;
  355. /*
  356. * The cache->next_local list sorts by level ascending:
  357. * L1d -> L1i -> L2 -> L3 ...
  358. */
  359. WARN_ONCE((smaller->level == 1 && bigger->level > 2) ||
  360. (smaller->level > 1 && bigger->level != smaller->level + 1),
  361. "linking L%i cache %pOFP to L%i cache %pOFP; skipped a level?\n",
  362. smaller->level, smaller->ofnode, bigger->level, bigger->ofnode);
  363. }
  364. static void do_subsidiary_caches_debugcheck(struct cache *cache)
  365. {
  366. WARN_ONCE(cache->level != 1,
  367. "instantiating cache chain from L%d %s cache for "
  368. "%pOFP instead of an L1\n", cache->level,
  369. cache_type_string(cache), cache->ofnode);
  370. WARN_ONCE(!of_node_is_type(cache->ofnode, "cpu"),
  371. "instantiating cache chain from node %pOFP of type '%s' "
  372. "instead of a cpu node\n", cache->ofnode,
  373. of_node_get_device_type(cache->ofnode));
  374. }
  375. /*
  376. * If sub-groups of threads in a core containing @cpu_id share the
  377. * L@level-cache (information obtained via "ibm,thread-groups"
  378. * device-tree property), then we identify the group by the first
  379. * thread-sibling in the group. We define this to be the group-id.
  380. *
  381. * In the absence of any thread-group information for L@level-cache,
  382. * this function returns -1.
  383. */
  384. static int get_group_id(unsigned int cpu_id, int level)
  385. {
  386. if (has_big_cores && level == 1)
  387. return cpumask_first(per_cpu(thread_group_l1_cache_map,
  388. cpu_id));
  389. else if (thread_group_shares_l2 && level == 2)
  390. return cpumask_first(per_cpu(thread_group_l2_cache_map,
  391. cpu_id));
  392. else if (thread_group_shares_l3 && level == 3)
  393. return cpumask_first(per_cpu(thread_group_l3_cache_map,
  394. cpu_id));
  395. return -1;
  396. }
  397. static void do_subsidiary_caches(struct cache *cache, unsigned int cpu_id)
  398. {
  399. struct device_node *subcache_node;
  400. int level = cache->level;
  401. do_subsidiary_caches_debugcheck(cache);
  402. while ((subcache_node = of_find_next_cache_node(cache->ofnode))) {
  403. struct cache *subcache;
  404. int group_id;
  405. level++;
  406. group_id = get_group_id(cpu_id, level);
  407. subcache = cache_lookup_or_instantiate(subcache_node, group_id, level);
  408. of_node_put(subcache_node);
  409. if (!subcache)
  410. break;
  411. link_cache_lists(cache, subcache);
  412. cache = subcache;
  413. }
  414. }
  415. static struct cache *cache_chain_instantiate(unsigned int cpu_id)
  416. {
  417. struct device_node *cpu_node;
  418. struct cache *cpu_cache = NULL;
  419. int group_id;
  420. pr_debug("creating cache object(s) for CPU %i\n", cpu_id);
  421. cpu_node = of_get_cpu_node(cpu_id, NULL);
  422. WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
  423. if (!cpu_node)
  424. goto out;
  425. group_id = get_group_id(cpu_id, 1);
  426. cpu_cache = cache_lookup_or_instantiate(cpu_node, group_id, 1);
  427. if (!cpu_cache)
  428. goto out;
  429. do_subsidiary_caches(cpu_cache, cpu_id);
  430. cache_cpu_set(cpu_cache, cpu_id);
  431. out:
  432. of_node_put(cpu_node);
  433. return cpu_cache;
  434. }
  435. static struct cache_dir *cacheinfo_create_cache_dir(unsigned int cpu_id)
  436. {
  437. struct cache_dir *cache_dir;
  438. struct device *dev;
  439. struct kobject *kobj = NULL;
  440. dev = get_cpu_device(cpu_id);
  441. WARN_ONCE(!dev, "no dev for CPU %i\n", cpu_id);
  442. if (!dev)
  443. goto err;
  444. kobj = kobject_create_and_add("cache", &dev->kobj);
  445. if (!kobj)
  446. goto err;
  447. cache_dir = kzalloc(sizeof(*cache_dir), GFP_KERNEL);
  448. if (!cache_dir)
  449. goto err;
  450. cache_dir->kobj = kobj;
  451. WARN_ON_ONCE(per_cpu(cache_dir_pcpu, cpu_id) != NULL);
  452. per_cpu(cache_dir_pcpu, cpu_id) = cache_dir;
  453. return cache_dir;
  454. err:
  455. kobject_put(kobj);
  456. return NULL;
  457. }
  458. static void cache_index_release(struct kobject *kobj)
  459. {
  460. struct cache_index_dir *index;
  461. index = kobj_to_cache_index_dir(kobj);
  462. pr_debug("freeing index directory for L%d %s cache\n",
  463. index->cache->level, cache_type_string(index->cache));
  464. kfree(index);
  465. }
  466. static ssize_t cache_index_show(struct kobject *k, struct attribute *attr, char *buf)
  467. {
  468. struct kobj_attribute *kobj_attr;
  469. kobj_attr = container_of(attr, struct kobj_attribute, attr);
  470. return kobj_attr->show(k, kobj_attr, buf);
  471. }
  472. static struct cache *index_kobj_to_cache(struct kobject *k)
  473. {
  474. struct cache_index_dir *index;
  475. index = kobj_to_cache_index_dir(k);
  476. return index->cache;
  477. }
  478. static ssize_t size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  479. {
  480. unsigned int size_kb;
  481. struct cache *cache;
  482. cache = index_kobj_to_cache(k);
  483. if (cache_size_kb(cache, &size_kb))
  484. return -ENODEV;
  485. return sprintf(buf, "%uK\n", size_kb);
  486. }
  487. static struct kobj_attribute cache_size_attr =
  488. __ATTR(size, 0444, size_show, NULL);
  489. static ssize_t line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  490. {
  491. unsigned int line_size;
  492. struct cache *cache;
  493. cache = index_kobj_to_cache(k);
  494. if (cache_get_line_size(cache, &line_size))
  495. return -ENODEV;
  496. return sprintf(buf, "%u\n", line_size);
  497. }
  498. static struct kobj_attribute cache_line_size_attr =
  499. __ATTR(coherency_line_size, 0444, line_size_show, NULL);
  500. static ssize_t nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  501. {
  502. unsigned int nr_sets;
  503. struct cache *cache;
  504. cache = index_kobj_to_cache(k);
  505. if (cache_nr_sets(cache, &nr_sets))
  506. return -ENODEV;
  507. return sprintf(buf, "%u\n", nr_sets);
  508. }
  509. static struct kobj_attribute cache_nr_sets_attr =
  510. __ATTR(number_of_sets, 0444, nr_sets_show, NULL);
  511. static ssize_t associativity_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  512. {
  513. unsigned int associativity;
  514. struct cache *cache;
  515. cache = index_kobj_to_cache(k);
  516. if (cache_associativity(cache, &associativity))
  517. return -ENODEV;
  518. return sprintf(buf, "%u\n", associativity);
  519. }
  520. static struct kobj_attribute cache_assoc_attr =
  521. __ATTR(ways_of_associativity, 0444, associativity_show, NULL);
  522. static ssize_t type_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  523. {
  524. struct cache *cache;
  525. cache = index_kobj_to_cache(k);
  526. return sprintf(buf, "%s\n", cache_type_string(cache));
  527. }
  528. static struct kobj_attribute cache_type_attr =
  529. __ATTR(type, 0444, type_show, NULL);
  530. static ssize_t level_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  531. {
  532. struct cache_index_dir *index;
  533. struct cache *cache;
  534. index = kobj_to_cache_index_dir(k);
  535. cache = index->cache;
  536. return sprintf(buf, "%d\n", cache->level);
  537. }
  538. static struct kobj_attribute cache_level_attr =
  539. __ATTR(level, 0444, level_show, NULL);
  540. static ssize_t
  541. show_shared_cpumap(struct kobject *k, struct kobj_attribute *attr, char *buf, bool list)
  542. {
  543. struct cache_index_dir *index;
  544. struct cache *cache;
  545. const struct cpumask *mask;
  546. index = kobj_to_cache_index_dir(k);
  547. cache = index->cache;
  548. mask = &cache->shared_cpu_map;
  549. return cpumap_print_to_pagebuf(list, buf, mask);
  550. }
  551. static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  552. {
  553. return show_shared_cpumap(k, attr, buf, false);
  554. }
  555. static ssize_t shared_cpu_list_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  556. {
  557. return show_shared_cpumap(k, attr, buf, true);
  558. }
  559. static struct kobj_attribute cache_shared_cpu_map_attr =
  560. __ATTR(shared_cpu_map, 0444, shared_cpu_map_show, NULL);
  561. static struct kobj_attribute cache_shared_cpu_list_attr =
  562. __ATTR(shared_cpu_list, 0444, shared_cpu_list_show, NULL);
  563. /* Attributes which should always be created -- the kobject/sysfs core
  564. * does this automatically via kobj_type->default_groups. This is the
  565. * minimum data required to uniquely identify a cache.
  566. */
  567. static struct attribute *cache_index_default_attrs[] = {
  568. &cache_type_attr.attr,
  569. &cache_level_attr.attr,
  570. &cache_shared_cpu_map_attr.attr,
  571. &cache_shared_cpu_list_attr.attr,
  572. NULL,
  573. };
  574. ATTRIBUTE_GROUPS(cache_index_default);
  575. /* Attributes which should be created if the cache device node has the
  576. * right properties -- see cacheinfo_create_index_opt_attrs
  577. */
  578. static struct kobj_attribute *cache_index_opt_attrs[] = {
  579. &cache_size_attr,
  580. &cache_line_size_attr,
  581. &cache_nr_sets_attr,
  582. &cache_assoc_attr,
  583. };
  584. static const struct sysfs_ops cache_index_ops = {
  585. .show = cache_index_show,
  586. };
  587. static struct kobj_type cache_index_type = {
  588. .release = cache_index_release,
  589. .sysfs_ops = &cache_index_ops,
  590. .default_groups = cache_index_default_groups,
  591. };
  592. static void cacheinfo_create_index_opt_attrs(struct cache_index_dir *dir)
  593. {
  594. const char *cache_type;
  595. struct cache *cache;
  596. char *buf;
  597. int i;
  598. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  599. if (!buf)
  600. return;
  601. cache = dir->cache;
  602. cache_type = cache_type_string(cache);
  603. /* We don't want to create an attribute that can't provide a
  604. * meaningful value. Check the return value of each optional
  605. * attribute's ->show method before registering the
  606. * attribute.
  607. */
  608. for (i = 0; i < ARRAY_SIZE(cache_index_opt_attrs); i++) {
  609. struct kobj_attribute *attr;
  610. ssize_t rc;
  611. attr = cache_index_opt_attrs[i];
  612. rc = attr->show(&dir->kobj, attr, buf);
  613. if (rc <= 0) {
  614. pr_debug("not creating %s attribute for "
  615. "%pOFP(%s) (rc = %zd)\n",
  616. attr->attr.name, cache->ofnode,
  617. cache_type, rc);
  618. continue;
  619. }
  620. if (sysfs_create_file(&dir->kobj, &attr->attr))
  621. pr_debug("could not create %s attribute for %pOFP(%s)\n",
  622. attr->attr.name, cache->ofnode, cache_type);
  623. }
  624. kfree(buf);
  625. }
  626. static void cacheinfo_create_index_dir(struct cache *cache, int index,
  627. struct cache_dir *cache_dir)
  628. {
  629. struct cache_index_dir *index_dir;
  630. int rc;
  631. index_dir = kzalloc(sizeof(*index_dir), GFP_KERNEL);
  632. if (!index_dir)
  633. return;
  634. index_dir->cache = cache;
  635. rc = kobject_init_and_add(&index_dir->kobj, &cache_index_type,
  636. cache_dir->kobj, "index%d", index);
  637. if (rc) {
  638. kobject_put(&index_dir->kobj);
  639. return;
  640. }
  641. index_dir->next = cache_dir->index;
  642. cache_dir->index = index_dir;
  643. cacheinfo_create_index_opt_attrs(index_dir);
  644. }
  645. static void cacheinfo_sysfs_populate(unsigned int cpu_id,
  646. struct cache *cache_list)
  647. {
  648. struct cache_dir *cache_dir;
  649. struct cache *cache;
  650. int index = 0;
  651. cache_dir = cacheinfo_create_cache_dir(cpu_id);
  652. if (!cache_dir)
  653. return;
  654. cache = cache_list;
  655. while (cache) {
  656. cacheinfo_create_index_dir(cache, index, cache_dir);
  657. index++;
  658. cache = cache->next_local;
  659. }
  660. }
  661. void cacheinfo_cpu_online(unsigned int cpu_id)
  662. {
  663. struct cache *cache;
  664. cache = cache_chain_instantiate(cpu_id);
  665. if (!cache)
  666. return;
  667. cacheinfo_sysfs_populate(cpu_id, cache);
  668. }
  669. /* functions needed to remove cache entry for cpu offline or suspend/resume */
  670. #if (defined(CONFIG_PPC_PSERIES) && defined(CONFIG_SUSPEND)) || \
  671. defined(CONFIG_HOTPLUG_CPU)
  672. static struct cache *cache_lookup_by_cpu(unsigned int cpu_id)
  673. {
  674. struct device_node *cpu_node;
  675. struct cache *cache;
  676. int group_id;
  677. cpu_node = of_get_cpu_node(cpu_id, NULL);
  678. WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
  679. if (!cpu_node)
  680. return NULL;
  681. group_id = get_group_id(cpu_id, 1);
  682. cache = cache_lookup_by_node_group(cpu_node, group_id);
  683. of_node_put(cpu_node);
  684. return cache;
  685. }
  686. static void remove_index_dirs(struct cache_dir *cache_dir)
  687. {
  688. struct cache_index_dir *index;
  689. index = cache_dir->index;
  690. while (index) {
  691. struct cache_index_dir *next;
  692. next = index->next;
  693. kobject_put(&index->kobj);
  694. index = next;
  695. }
  696. }
  697. static void remove_cache_dir(struct cache_dir *cache_dir)
  698. {
  699. remove_index_dirs(cache_dir);
  700. /* Remove cache dir from sysfs */
  701. kobject_del(cache_dir->kobj);
  702. kobject_put(cache_dir->kobj);
  703. kfree(cache_dir);
  704. }
  705. static void cache_cpu_clear(struct cache *cache, int cpu)
  706. {
  707. while (cache) {
  708. struct cache *next = cache->next_local;
  709. WARN_ONCE(!cpumask_test_cpu(cpu, &cache->shared_cpu_map),
  710. "CPU %i not accounted in %pOFP(%s)\n",
  711. cpu, cache->ofnode,
  712. cache_type_string(cache));
  713. cpumask_clear_cpu(cpu, &cache->shared_cpu_map);
  714. /* Release the cache object if all the cpus using it
  715. * are offline */
  716. if (cpumask_empty(&cache->shared_cpu_map))
  717. release_cache(cache);
  718. cache = next;
  719. }
  720. }
  721. void cacheinfo_cpu_offline(unsigned int cpu_id)
  722. {
  723. struct cache_dir *cache_dir;
  724. struct cache *cache;
  725. /* Prevent userspace from seeing inconsistent state - remove
  726. * the sysfs hierarchy first */
  727. cache_dir = per_cpu(cache_dir_pcpu, cpu_id);
  728. /* careful, sysfs population may have failed */
  729. if (cache_dir)
  730. remove_cache_dir(cache_dir);
  731. per_cpu(cache_dir_pcpu, cpu_id) = NULL;
  732. /* clear the CPU's bit in its cache chain, possibly freeing
  733. * cache objects */
  734. cache = cache_lookup_by_cpu(cpu_id);
  735. if (cache)
  736. cache_cpu_clear(cache, cpu_id);
  737. }
  738. void cacheinfo_teardown(void)
  739. {
  740. unsigned int cpu;
  741. lockdep_assert_cpus_held();
  742. for_each_online_cpu(cpu)
  743. cacheinfo_cpu_offline(cpu);
  744. }
  745. void cacheinfo_rebuild(void)
  746. {
  747. unsigned int cpu;
  748. lockdep_assert_cpus_held();
  749. for_each_online_cpu(cpu)
  750. cacheinfo_cpu_online(cpu);
  751. }
  752. #endif /* (CONFIG_PPC_PSERIES && CONFIG_SUSPEND) || CONFIG_HOTPLUG_CPU */