cacheinfo.c 23 KB

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