cacheinfo.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * Processor cache information made available to userspace via sysfs;
  3. * intended to be compatible with x86 intel_cacheinfo implementation.
  4. *
  5. * Copyright 2008 IBM Corporation
  6. * Author: Nathan Lynch
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. */
  12. #include <linux/cpu.h>
  13. #include <linux/cpumask.h>
  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>
  20. #include <linux/slab.h>
  21. #include <asm/prom.h>
  22. #include <asm/cputhreads.h>
  23. #include <asm/smp.h>
  24. #include "cacheinfo.h"
  25. /* per-cpu object for tracking:
  26. * - a "cache" kobject for the top-level directory
  27. * - a list of "index" objects representing the cpu's local cache hierarchy
  28. */
  29. struct cache_dir {
  30. struct kobject *kobj; /* bare (not embedded) kobject for cache
  31. * directory */
  32. struct cache_index_dir *index; /* list of index objects */
  33. };
  34. /* "index" object: each cpu's cache directory has an index
  35. * subdirectory corresponding to a cache object associated with the
  36. * cpu. This object's lifetime is managed via the embedded kobject.
  37. */
  38. struct cache_index_dir {
  39. struct kobject kobj;
  40. struct cache_index_dir *next; /* next index in parent directory */
  41. struct cache *cache;
  42. };
  43. /* Template for determining which OF properties to query for a given
  44. * cache type */
  45. struct cache_type_info {
  46. const char *name;
  47. const char *size_prop;
  48. /* Allow for both [di]-cache-line-size and
  49. * [di]-cache-block-size properties. According to the PowerPC
  50. * Processor binding, -line-size should be provided if it
  51. * differs from the cache block size (that which is operated
  52. * on by cache instructions), so we look for -line-size first.
  53. * See cache_get_line_size(). */
  54. const char *line_size_props[2];
  55. const char *nr_sets_prop;
  56. };
  57. /* These are used to index the cache_type_info array. */
  58. #define CACHE_TYPE_UNIFIED 0 /* cache-size, cache-block-size, etc. */
  59. #define CACHE_TYPE_UNIFIED_D 1 /* d-cache-size, d-cache-block-size, etc */
  60. #define CACHE_TYPE_INSTRUCTION 2
  61. #define CACHE_TYPE_DATA 3
  62. static const struct cache_type_info cache_type_info[] = {
  63. {
  64. /* Embedded systems that use cache-size, cache-block-size,
  65. * etc. for the Unified (typically L2) cache. */
  66. .name = "Unified",
  67. .size_prop = "cache-size",
  68. .line_size_props = { "cache-line-size",
  69. "cache-block-size", },
  70. .nr_sets_prop = "cache-sets",
  71. },
  72. {
  73. /* PowerPC Processor binding says the [di]-cache-*
  74. * must be equal on unified caches, so just use
  75. * d-cache properties. */
  76. .name = "Unified",
  77. .size_prop = "d-cache-size",
  78. .line_size_props = { "d-cache-line-size",
  79. "d-cache-block-size", },
  80. .nr_sets_prop = "d-cache-sets",
  81. },
  82. {
  83. .name = "Instruction",
  84. .size_prop = "i-cache-size",
  85. .line_size_props = { "i-cache-line-size",
  86. "i-cache-block-size", },
  87. .nr_sets_prop = "i-cache-sets",
  88. },
  89. {
  90. .name = "Data",
  91. .size_prop = "d-cache-size",
  92. .line_size_props = { "d-cache-line-size",
  93. "d-cache-block-size", },
  94. .nr_sets_prop = "d-cache-sets",
  95. },
  96. };
  97. /* Cache object: each instance of this corresponds to a distinct cache
  98. * in the system. There are separate objects for Harvard caches: one
  99. * each for instruction and data, and each refers to the same OF node.
  100. * The refcount of the OF node is elevated for the lifetime of the
  101. * cache object. A cache object is released when its shared_cpu_map
  102. * is cleared (see cache_cpu_clear).
  103. *
  104. * A cache object is on two lists: an unsorted global list
  105. * (cache_list) of cache objects; and a singly-linked list
  106. * representing the local cache hierarchy, which is ordered by level
  107. * (e.g. L1d -> L1i -> L2 -> L3).
  108. */
  109. struct cache {
  110. struct device_node *ofnode; /* OF node for this cache, may be cpu */
  111. struct cpumask shared_cpu_map; /* online CPUs using this cache */
  112. int type; /* split cache disambiguation */
  113. int level; /* level not explicit in device tree */
  114. struct list_head list; /* global list of cache objects */
  115. struct cache *next_local; /* next cache of >= level */
  116. };
  117. static DEFINE_PER_CPU(struct cache_dir *, cache_dir_pcpu);
  118. /* traversal/modification of this list occurs only at cpu hotplug time;
  119. * access is serialized by cpu hotplug locking
  120. */
  121. static LIST_HEAD(cache_list);
  122. static struct cache_index_dir *kobj_to_cache_index_dir(struct kobject *k)
  123. {
  124. return container_of(k, struct cache_index_dir, kobj);
  125. }
  126. static const char *cache_type_string(const struct cache *cache)
  127. {
  128. return cache_type_info[cache->type].name;
  129. }
  130. static void cache_init(struct cache *cache, int type, int level,
  131. struct device_node *ofnode)
  132. {
  133. cache->type = type;
  134. cache->level = level;
  135. cache->ofnode = of_node_get(ofnode);
  136. INIT_LIST_HEAD(&cache->list);
  137. list_add(&cache->list, &cache_list);
  138. }
  139. static struct cache *new_cache(int type, int level, struct device_node *ofnode)
  140. {
  141. struct cache *cache;
  142. cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  143. if (cache)
  144. cache_init(cache, type, level, ofnode);
  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 %pOF(%s) refers to cache for %pOF(%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 %pOF\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 %pOF(%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 && iter->next_local == cache)
  263. return iter;
  264. return cache;
  265. }
  266. /* return the first cache on a local list matching node */
  267. static struct cache *cache_lookup_by_node(const struct device_node *node)
  268. {
  269. struct cache *cache = NULL;
  270. struct cache *iter;
  271. list_for_each_entry(iter, &cache_list, list) {
  272. if (iter->ofnode != node)
  273. continue;
  274. cache = cache_find_first_sibling(iter);
  275. break;
  276. }
  277. return cache;
  278. }
  279. static bool cache_node_is_unified(const struct device_node *np)
  280. {
  281. return of_get_property(np, "cache-unified", NULL);
  282. }
  283. /*
  284. * Unified caches can have two different sets of tags. Most embedded
  285. * use cache-size, etc. for the unified cache size, but open firmware systems
  286. * use d-cache-size, etc. Check on initialization for which type we have, and
  287. * return the appropriate structure type. Assume it's embedded if it isn't
  288. * open firmware. If it's yet a 3rd type, then there will be missing entries
  289. * in /sys/devices/system/cpu/cpu0/cache/index2/, and this code will need
  290. * to be extended further.
  291. */
  292. static int cache_is_unified_d(const struct device_node *np)
  293. {
  294. return of_get_property(np,
  295. cache_type_info[CACHE_TYPE_UNIFIED_D].size_prop, NULL) ?
  296. CACHE_TYPE_UNIFIED_D : CACHE_TYPE_UNIFIED;
  297. }
  298. /*
  299. */
  300. static struct cache *cache_do_one_devnode_unified(struct device_node *node, int level)
  301. {
  302. pr_debug("creating L%d ucache for %pOF\n", level, node);
  303. return new_cache(cache_is_unified_d(node), level, node);
  304. }
  305. static struct cache *cache_do_one_devnode_split(struct device_node *node,
  306. int level)
  307. {
  308. struct cache *dcache, *icache;
  309. pr_debug("creating L%d dcache and icache for %pOF\n", level,
  310. node);
  311. dcache = new_cache(CACHE_TYPE_DATA, level, node);
  312. icache = new_cache(CACHE_TYPE_INSTRUCTION, level, node);
  313. if (!dcache || !icache)
  314. goto err;
  315. dcache->next_local = icache;
  316. return dcache;
  317. err:
  318. release_cache(dcache);
  319. release_cache(icache);
  320. return NULL;
  321. }
  322. static struct cache *cache_do_one_devnode(struct device_node *node, int level)
  323. {
  324. struct cache *cache;
  325. if (cache_node_is_unified(node))
  326. cache = cache_do_one_devnode_unified(node, level);
  327. else
  328. cache = cache_do_one_devnode_split(node, level);
  329. return cache;
  330. }
  331. static struct cache *cache_lookup_or_instantiate(struct device_node *node,
  332. int level)
  333. {
  334. struct cache *cache;
  335. cache = cache_lookup_by_node(node);
  336. WARN_ONCE(cache && cache->level != level,
  337. "cache level mismatch on lookup (got %d, expected %d)\n",
  338. cache->level, level);
  339. if (!cache)
  340. cache = cache_do_one_devnode(node, level);
  341. return cache;
  342. }
  343. static void link_cache_lists(struct cache *smaller, struct cache *bigger)
  344. {
  345. while (smaller->next_local) {
  346. if (smaller->next_local == bigger)
  347. return; /* already linked */
  348. smaller = smaller->next_local;
  349. }
  350. smaller->next_local = bigger;
  351. }
  352. static void do_subsidiary_caches_debugcheck(struct cache *cache)
  353. {
  354. WARN_ON_ONCE(cache->level != 1);
  355. WARN_ON_ONCE(strcmp(cache->ofnode->type, "cpu"));
  356. }
  357. static void do_subsidiary_caches(struct cache *cache)
  358. {
  359. struct device_node *subcache_node;
  360. int level = cache->level;
  361. do_subsidiary_caches_debugcheck(cache);
  362. while ((subcache_node = of_find_next_cache_node(cache->ofnode))) {
  363. struct cache *subcache;
  364. level++;
  365. subcache = cache_lookup_or_instantiate(subcache_node, level);
  366. of_node_put(subcache_node);
  367. if (!subcache)
  368. break;
  369. link_cache_lists(cache, subcache);
  370. cache = subcache;
  371. }
  372. }
  373. static struct cache *cache_chain_instantiate(unsigned int cpu_id)
  374. {
  375. struct device_node *cpu_node;
  376. struct cache *cpu_cache = NULL;
  377. pr_debug("creating cache object(s) for CPU %i\n", cpu_id);
  378. cpu_node = of_get_cpu_node(cpu_id, NULL);
  379. WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
  380. if (!cpu_node)
  381. goto out;
  382. cpu_cache = cache_lookup_or_instantiate(cpu_node, 1);
  383. if (!cpu_cache)
  384. goto out;
  385. do_subsidiary_caches(cpu_cache);
  386. cache_cpu_set(cpu_cache, cpu_id);
  387. out:
  388. of_node_put(cpu_node);
  389. return cpu_cache;
  390. }
  391. static struct cache_dir *cacheinfo_create_cache_dir(unsigned int cpu_id)
  392. {
  393. struct cache_dir *cache_dir;
  394. struct device *dev;
  395. struct kobject *kobj = NULL;
  396. dev = get_cpu_device(cpu_id);
  397. WARN_ONCE(!dev, "no dev for CPU %i\n", cpu_id);
  398. if (!dev)
  399. goto err;
  400. kobj = kobject_create_and_add("cache", &dev->kobj);
  401. if (!kobj)
  402. goto err;
  403. cache_dir = kzalloc(sizeof(*cache_dir), GFP_KERNEL);
  404. if (!cache_dir)
  405. goto err;
  406. cache_dir->kobj = kobj;
  407. WARN_ON_ONCE(per_cpu(cache_dir_pcpu, cpu_id) != NULL);
  408. per_cpu(cache_dir_pcpu, cpu_id) = cache_dir;
  409. return cache_dir;
  410. err:
  411. kobject_put(kobj);
  412. return NULL;
  413. }
  414. static void cache_index_release(struct kobject *kobj)
  415. {
  416. struct cache_index_dir *index;
  417. index = kobj_to_cache_index_dir(kobj);
  418. pr_debug("freeing index directory for L%d %s cache\n",
  419. index->cache->level, cache_type_string(index->cache));
  420. kfree(index);
  421. }
  422. static ssize_t cache_index_show(struct kobject *k, struct attribute *attr, char *buf)
  423. {
  424. struct kobj_attribute *kobj_attr;
  425. kobj_attr = container_of(attr, struct kobj_attribute, attr);
  426. return kobj_attr->show(k, kobj_attr, buf);
  427. }
  428. static struct cache *index_kobj_to_cache(struct kobject *k)
  429. {
  430. struct cache_index_dir *index;
  431. index = kobj_to_cache_index_dir(k);
  432. return index->cache;
  433. }
  434. static ssize_t size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  435. {
  436. unsigned int size_kb;
  437. struct cache *cache;
  438. cache = index_kobj_to_cache(k);
  439. if (cache_size_kb(cache, &size_kb))
  440. return -ENODEV;
  441. return sprintf(buf, "%uK\n", size_kb);
  442. }
  443. static struct kobj_attribute cache_size_attr =
  444. __ATTR(size, 0444, size_show, NULL);
  445. static ssize_t line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  446. {
  447. unsigned int line_size;
  448. struct cache *cache;
  449. cache = index_kobj_to_cache(k);
  450. if (cache_get_line_size(cache, &line_size))
  451. return -ENODEV;
  452. return sprintf(buf, "%u\n", line_size);
  453. }
  454. static struct kobj_attribute cache_line_size_attr =
  455. __ATTR(coherency_line_size, 0444, line_size_show, NULL);
  456. static ssize_t nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  457. {
  458. unsigned int nr_sets;
  459. struct cache *cache;
  460. cache = index_kobj_to_cache(k);
  461. if (cache_nr_sets(cache, &nr_sets))
  462. return -ENODEV;
  463. return sprintf(buf, "%u\n", nr_sets);
  464. }
  465. static struct kobj_attribute cache_nr_sets_attr =
  466. __ATTR(number_of_sets, 0444, nr_sets_show, NULL);
  467. static ssize_t associativity_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  468. {
  469. unsigned int associativity;
  470. struct cache *cache;
  471. cache = index_kobj_to_cache(k);
  472. if (cache_associativity(cache, &associativity))
  473. return -ENODEV;
  474. return sprintf(buf, "%u\n", associativity);
  475. }
  476. static struct kobj_attribute cache_assoc_attr =
  477. __ATTR(ways_of_associativity, 0444, associativity_show, NULL);
  478. static ssize_t type_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  479. {
  480. struct cache *cache;
  481. cache = index_kobj_to_cache(k);
  482. return sprintf(buf, "%s\n", cache_type_string(cache));
  483. }
  484. static struct kobj_attribute cache_type_attr =
  485. __ATTR(type, 0444, type_show, NULL);
  486. static ssize_t level_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  487. {
  488. struct cache_index_dir *index;
  489. struct cache *cache;
  490. index = kobj_to_cache_index_dir(k);
  491. cache = index->cache;
  492. return sprintf(buf, "%d\n", cache->level);
  493. }
  494. static struct kobj_attribute cache_level_attr =
  495. __ATTR(level, 0444, level_show, NULL);
  496. static unsigned int index_dir_to_cpu(struct cache_index_dir *index)
  497. {
  498. struct kobject *index_dir_kobj = &index->kobj;
  499. struct kobject *cache_dir_kobj = index_dir_kobj->parent;
  500. struct kobject *cpu_dev_kobj = cache_dir_kobj->parent;
  501. struct device *dev = kobj_to_dev(cpu_dev_kobj);
  502. return dev->id;
  503. }
  504. /*
  505. * On big-core systems, each core has two groups of CPUs each of which
  506. * has its own L1-cache. The thread-siblings which share l1-cache with
  507. * @cpu can be obtained via cpu_smallcore_mask().
  508. */
  509. static const struct cpumask *get_big_core_shared_cpu_map(int cpu, struct cache *cache)
  510. {
  511. if (cache->level == 1)
  512. return cpu_smallcore_mask(cpu);
  513. return &cache->shared_cpu_map;
  514. }
  515. static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  516. {
  517. struct cache_index_dir *index;
  518. struct cache *cache;
  519. const struct cpumask *mask;
  520. int ret, cpu;
  521. index = kobj_to_cache_index_dir(k);
  522. cache = index->cache;
  523. if (has_big_cores) {
  524. cpu = index_dir_to_cpu(index);
  525. mask = get_big_core_shared_cpu_map(cpu, cache);
  526. } else {
  527. mask = &cache->shared_cpu_map;
  528. }
  529. ret = scnprintf(buf, PAGE_SIZE - 1, "%*pb\n",
  530. cpumask_pr_args(mask));
  531. buf[ret++] = '\n';
  532. buf[ret] = '\0';
  533. return ret;
  534. }
  535. static struct kobj_attribute cache_shared_cpu_map_attr =
  536. __ATTR(shared_cpu_map, 0444, shared_cpu_map_show, NULL);
  537. /* Attributes which should always be created -- the kobject/sysfs core
  538. * does this automatically via kobj_type->default_attrs. This is the
  539. * minimum data required to uniquely identify a cache.
  540. */
  541. static struct attribute *cache_index_default_attrs[] = {
  542. &cache_type_attr.attr,
  543. &cache_level_attr.attr,
  544. &cache_shared_cpu_map_attr.attr,
  545. NULL,
  546. };
  547. /* Attributes which should be created if the cache device node has the
  548. * right properties -- see cacheinfo_create_index_opt_attrs
  549. */
  550. static struct kobj_attribute *cache_index_opt_attrs[] = {
  551. &cache_size_attr,
  552. &cache_line_size_attr,
  553. &cache_nr_sets_attr,
  554. &cache_assoc_attr,
  555. };
  556. static const struct sysfs_ops cache_index_ops = {
  557. .show = cache_index_show,
  558. };
  559. static struct kobj_type cache_index_type = {
  560. .release = cache_index_release,
  561. .sysfs_ops = &cache_index_ops,
  562. .default_attrs = cache_index_default_attrs,
  563. };
  564. static void cacheinfo_create_index_opt_attrs(struct cache_index_dir *dir)
  565. {
  566. const char *cache_type;
  567. struct cache *cache;
  568. char *buf;
  569. int i;
  570. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  571. if (!buf)
  572. return;
  573. cache = dir->cache;
  574. cache_type = cache_type_string(cache);
  575. /* We don't want to create an attribute that can't provide a
  576. * meaningful value. Check the return value of each optional
  577. * attribute's ->show method before registering the
  578. * attribute.
  579. */
  580. for (i = 0; i < ARRAY_SIZE(cache_index_opt_attrs); i++) {
  581. struct kobj_attribute *attr;
  582. ssize_t rc;
  583. attr = cache_index_opt_attrs[i];
  584. rc = attr->show(&dir->kobj, attr, buf);
  585. if (rc <= 0) {
  586. pr_debug("not creating %s attribute for "
  587. "%pOF(%s) (rc = %zd)\n",
  588. attr->attr.name, cache->ofnode,
  589. cache_type, rc);
  590. continue;
  591. }
  592. if (sysfs_create_file(&dir->kobj, &attr->attr))
  593. pr_debug("could not create %s attribute for %pOF(%s)\n",
  594. attr->attr.name, cache->ofnode, cache_type);
  595. }
  596. kfree(buf);
  597. }
  598. static void cacheinfo_create_index_dir(struct cache *cache, int index,
  599. struct cache_dir *cache_dir)
  600. {
  601. struct cache_index_dir *index_dir;
  602. int rc;
  603. index_dir = kzalloc(sizeof(*index_dir), GFP_KERNEL);
  604. if (!index_dir)
  605. goto err;
  606. index_dir->cache = cache;
  607. rc = kobject_init_and_add(&index_dir->kobj, &cache_index_type,
  608. cache_dir->kobj, "index%d", index);
  609. if (rc)
  610. goto err;
  611. index_dir->next = cache_dir->index;
  612. cache_dir->index = index_dir;
  613. cacheinfo_create_index_opt_attrs(index_dir);
  614. return;
  615. err:
  616. kfree(index_dir);
  617. }
  618. static void cacheinfo_sysfs_populate(unsigned int cpu_id,
  619. struct cache *cache_list)
  620. {
  621. struct cache_dir *cache_dir;
  622. struct cache *cache;
  623. int index = 0;
  624. cache_dir = cacheinfo_create_cache_dir(cpu_id);
  625. if (!cache_dir)
  626. return;
  627. cache = cache_list;
  628. while (cache) {
  629. cacheinfo_create_index_dir(cache, index, cache_dir);
  630. index++;
  631. cache = cache->next_local;
  632. }
  633. }
  634. void cacheinfo_cpu_online(unsigned int cpu_id)
  635. {
  636. struct cache *cache;
  637. cache = cache_chain_instantiate(cpu_id);
  638. if (!cache)
  639. return;
  640. cacheinfo_sysfs_populate(cpu_id, cache);
  641. }
  642. /* functions needed to remove cache entry for cpu offline or suspend/resume */
  643. #if (defined(CONFIG_PPC_PSERIES) && defined(CONFIG_SUSPEND)) || \
  644. defined(CONFIG_HOTPLUG_CPU)
  645. static struct cache *cache_lookup_by_cpu(unsigned int cpu_id)
  646. {
  647. struct device_node *cpu_node;
  648. struct cache *cache;
  649. cpu_node = of_get_cpu_node(cpu_id, NULL);
  650. WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
  651. if (!cpu_node)
  652. return NULL;
  653. cache = cache_lookup_by_node(cpu_node);
  654. of_node_put(cpu_node);
  655. return cache;
  656. }
  657. static void remove_index_dirs(struct cache_dir *cache_dir)
  658. {
  659. struct cache_index_dir *index;
  660. index = cache_dir->index;
  661. while (index) {
  662. struct cache_index_dir *next;
  663. next = index->next;
  664. kobject_put(&index->kobj);
  665. index = next;
  666. }
  667. }
  668. static void remove_cache_dir(struct cache_dir *cache_dir)
  669. {
  670. remove_index_dirs(cache_dir);
  671. /* Remove cache dir from sysfs */
  672. kobject_del(cache_dir->kobj);
  673. kobject_put(cache_dir->kobj);
  674. kfree(cache_dir);
  675. }
  676. static void cache_cpu_clear(struct cache *cache, int cpu)
  677. {
  678. while (cache) {
  679. struct cache *next = cache->next_local;
  680. WARN_ONCE(!cpumask_test_cpu(cpu, &cache->shared_cpu_map),
  681. "CPU %i not accounted in %pOF(%s)\n",
  682. cpu, cache->ofnode,
  683. cache_type_string(cache));
  684. cpumask_clear_cpu(cpu, &cache->shared_cpu_map);
  685. /* Release the cache object if all the cpus using it
  686. * are offline */
  687. if (cpumask_empty(&cache->shared_cpu_map))
  688. release_cache(cache);
  689. cache = next;
  690. }
  691. }
  692. void cacheinfo_cpu_offline(unsigned int cpu_id)
  693. {
  694. struct cache_dir *cache_dir;
  695. struct cache *cache;
  696. /* Prevent userspace from seeing inconsistent state - remove
  697. * the sysfs hierarchy first */
  698. cache_dir = per_cpu(cache_dir_pcpu, cpu_id);
  699. /* careful, sysfs population may have failed */
  700. if (cache_dir)
  701. remove_cache_dir(cache_dir);
  702. per_cpu(cache_dir_pcpu, cpu_id) = NULL;
  703. /* clear the CPU's bit in its cache chain, possibly freeing
  704. * cache objects */
  705. cache = cache_lookup_by_cpu(cpu_id);
  706. if (cache)
  707. cache_cpu_clear(cache, cpu_id);
  708. }
  709. #endif /* (CONFIG_PPC_PSERIES && CONFIG_SUSPEND) || CONFIG_HOTPLUG_CPU */