cacheinfo.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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 "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 %s(%s) refers to cache for %s(%s)\n",
  151. iter->ofnode->full_name,
  152. cache_type_string(iter),
  153. cache->ofnode->full_name,
  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 %s\n", cache->level,
  161. cache_type_string(cache), cache->ofnode->full_name);
  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 %s(%s)\n",
  173. cpu, next->ofnode->full_name,
  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. /*
  297. */
  298. static struct cache *cache_do_one_devnode_unified(struct device_node *node, int level)
  299. {
  300. pr_debug("creating L%d ucache for %s\n", level, node->full_name);
  301. return new_cache(cache_is_unified_d(node), level, node);
  302. }
  303. static struct cache *cache_do_one_devnode_split(struct device_node *node,
  304. int level)
  305. {
  306. struct cache *dcache, *icache;
  307. pr_debug("creating L%d dcache and icache for %s\n", level,
  308. node->full_name);
  309. dcache = new_cache(CACHE_TYPE_DATA, level, node);
  310. icache = new_cache(CACHE_TYPE_INSTRUCTION, level, node);
  311. if (!dcache || !icache)
  312. goto err;
  313. dcache->next_local = icache;
  314. return dcache;
  315. err:
  316. release_cache(dcache);
  317. release_cache(icache);
  318. return NULL;
  319. }
  320. static struct cache *cache_do_one_devnode(struct device_node *node, int level)
  321. {
  322. struct cache *cache;
  323. if (cache_node_is_unified(node))
  324. cache = cache_do_one_devnode_unified(node, level);
  325. else
  326. cache = cache_do_one_devnode_split(node, level);
  327. return cache;
  328. }
  329. static struct cache *cache_lookup_or_instantiate(struct device_node *node,
  330. int level)
  331. {
  332. struct cache *cache;
  333. cache = cache_lookup_by_node(node);
  334. WARN_ONCE(cache && cache->level != level,
  335. "cache level mismatch on lookup (got %d, expected %d)\n",
  336. cache->level, level);
  337. if (!cache)
  338. cache = cache_do_one_devnode(node, level);
  339. return cache;
  340. }
  341. static void link_cache_lists(struct cache *smaller, struct cache *bigger)
  342. {
  343. while (smaller->next_local) {
  344. if (smaller->next_local == bigger)
  345. return; /* already linked */
  346. smaller = smaller->next_local;
  347. }
  348. smaller->next_local = bigger;
  349. }
  350. static void do_subsidiary_caches_debugcheck(struct cache *cache)
  351. {
  352. WARN_ON_ONCE(cache->level != 1);
  353. WARN_ON_ONCE(strcmp(cache->ofnode->type, "cpu"));
  354. }
  355. static void do_subsidiary_caches(struct cache *cache)
  356. {
  357. struct device_node *subcache_node;
  358. int level = cache->level;
  359. do_subsidiary_caches_debugcheck(cache);
  360. while ((subcache_node = of_find_next_cache_node(cache->ofnode))) {
  361. struct cache *subcache;
  362. level++;
  363. subcache = cache_lookup_or_instantiate(subcache_node, level);
  364. of_node_put(subcache_node);
  365. if (!subcache)
  366. break;
  367. link_cache_lists(cache, subcache);
  368. cache = subcache;
  369. }
  370. }
  371. static struct cache *cache_chain_instantiate(unsigned int cpu_id)
  372. {
  373. struct device_node *cpu_node;
  374. struct cache *cpu_cache = NULL;
  375. pr_debug("creating cache object(s) for CPU %i\n", cpu_id);
  376. cpu_node = of_get_cpu_node(cpu_id, NULL);
  377. WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
  378. if (!cpu_node)
  379. goto out;
  380. cpu_cache = cache_lookup_or_instantiate(cpu_node, 1);
  381. if (!cpu_cache)
  382. goto out;
  383. do_subsidiary_caches(cpu_cache);
  384. cache_cpu_set(cpu_cache, cpu_id);
  385. out:
  386. of_node_put(cpu_node);
  387. return cpu_cache;
  388. }
  389. static struct cache_dir *cacheinfo_create_cache_dir(unsigned int cpu_id)
  390. {
  391. struct cache_dir *cache_dir;
  392. struct device *dev;
  393. struct kobject *kobj = NULL;
  394. dev = get_cpu_device(cpu_id);
  395. WARN_ONCE(!dev, "no dev for CPU %i\n", cpu_id);
  396. if (!dev)
  397. goto err;
  398. kobj = kobject_create_and_add("cache", &dev->kobj);
  399. if (!kobj)
  400. goto err;
  401. cache_dir = kzalloc(sizeof(*cache_dir), GFP_KERNEL);
  402. if (!cache_dir)
  403. goto err;
  404. cache_dir->kobj = kobj;
  405. WARN_ON_ONCE(per_cpu(cache_dir_pcpu, cpu_id) != NULL);
  406. per_cpu(cache_dir_pcpu, cpu_id) = cache_dir;
  407. return cache_dir;
  408. err:
  409. kobject_put(kobj);
  410. return NULL;
  411. }
  412. static void cache_index_release(struct kobject *kobj)
  413. {
  414. struct cache_index_dir *index;
  415. index = kobj_to_cache_index_dir(kobj);
  416. pr_debug("freeing index directory for L%d %s cache\n",
  417. index->cache->level, cache_type_string(index->cache));
  418. kfree(index);
  419. }
  420. static ssize_t cache_index_show(struct kobject *k, struct attribute *attr, char *buf)
  421. {
  422. struct kobj_attribute *kobj_attr;
  423. kobj_attr = container_of(attr, struct kobj_attribute, attr);
  424. return kobj_attr->show(k, kobj_attr, buf);
  425. }
  426. static struct cache *index_kobj_to_cache(struct kobject *k)
  427. {
  428. struct cache_index_dir *index;
  429. index = kobj_to_cache_index_dir(k);
  430. return index->cache;
  431. }
  432. static ssize_t size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  433. {
  434. unsigned int size_kb;
  435. struct cache *cache;
  436. cache = index_kobj_to_cache(k);
  437. if (cache_size_kb(cache, &size_kb))
  438. return -ENODEV;
  439. return sprintf(buf, "%uK\n", size_kb);
  440. }
  441. static struct kobj_attribute cache_size_attr =
  442. __ATTR(size, 0444, size_show, NULL);
  443. static ssize_t line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  444. {
  445. unsigned int line_size;
  446. struct cache *cache;
  447. cache = index_kobj_to_cache(k);
  448. if (cache_get_line_size(cache, &line_size))
  449. return -ENODEV;
  450. return sprintf(buf, "%u\n", line_size);
  451. }
  452. static struct kobj_attribute cache_line_size_attr =
  453. __ATTR(coherency_line_size, 0444, line_size_show, NULL);
  454. static ssize_t nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  455. {
  456. unsigned int nr_sets;
  457. struct cache *cache;
  458. cache = index_kobj_to_cache(k);
  459. if (cache_nr_sets(cache, &nr_sets))
  460. return -ENODEV;
  461. return sprintf(buf, "%u\n", nr_sets);
  462. }
  463. static struct kobj_attribute cache_nr_sets_attr =
  464. __ATTR(number_of_sets, 0444, nr_sets_show, NULL);
  465. static ssize_t associativity_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  466. {
  467. unsigned int associativity;
  468. struct cache *cache;
  469. cache = index_kobj_to_cache(k);
  470. if (cache_associativity(cache, &associativity))
  471. return -ENODEV;
  472. return sprintf(buf, "%u\n", associativity);
  473. }
  474. static struct kobj_attribute cache_assoc_attr =
  475. __ATTR(ways_of_associativity, 0444, associativity_show, NULL);
  476. static ssize_t type_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  477. {
  478. struct cache *cache;
  479. cache = index_kobj_to_cache(k);
  480. return sprintf(buf, "%s\n", cache_type_string(cache));
  481. }
  482. static struct kobj_attribute cache_type_attr =
  483. __ATTR(type, 0444, type_show, NULL);
  484. static ssize_t level_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  485. {
  486. struct cache_index_dir *index;
  487. struct cache *cache;
  488. index = kobj_to_cache_index_dir(k);
  489. cache = index->cache;
  490. return sprintf(buf, "%d\n", cache->level);
  491. }
  492. static struct kobj_attribute cache_level_attr =
  493. __ATTR(level, 0444, level_show, NULL);
  494. static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  495. {
  496. struct cache_index_dir *index;
  497. struct cache *cache;
  498. int ret;
  499. index = kobj_to_cache_index_dir(k);
  500. cache = index->cache;
  501. ret = scnprintf(buf, PAGE_SIZE - 1, "%*pb\n",
  502. cpumask_pr_args(&cache->shared_cpu_map));
  503. buf[ret++] = '\n';
  504. buf[ret] = '\0';
  505. return ret;
  506. }
  507. static struct kobj_attribute cache_shared_cpu_map_attr =
  508. __ATTR(shared_cpu_map, 0444, shared_cpu_map_show, NULL);
  509. /* Attributes which should always be created -- the kobject/sysfs core
  510. * does this automatically via kobj_type->default_attrs. This is the
  511. * minimum data required to uniquely identify a cache.
  512. */
  513. static struct attribute *cache_index_default_attrs[] = {
  514. &cache_type_attr.attr,
  515. &cache_level_attr.attr,
  516. &cache_shared_cpu_map_attr.attr,
  517. NULL,
  518. };
  519. /* Attributes which should be created if the cache device node has the
  520. * right properties -- see cacheinfo_create_index_opt_attrs
  521. */
  522. static struct kobj_attribute *cache_index_opt_attrs[] = {
  523. &cache_size_attr,
  524. &cache_line_size_attr,
  525. &cache_nr_sets_attr,
  526. &cache_assoc_attr,
  527. };
  528. static const struct sysfs_ops cache_index_ops = {
  529. .show = cache_index_show,
  530. };
  531. static struct kobj_type cache_index_type = {
  532. .release = cache_index_release,
  533. .sysfs_ops = &cache_index_ops,
  534. .default_attrs = cache_index_default_attrs,
  535. };
  536. static void cacheinfo_create_index_opt_attrs(struct cache_index_dir *dir)
  537. {
  538. const char *cache_name;
  539. const char *cache_type;
  540. struct cache *cache;
  541. char *buf;
  542. int i;
  543. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  544. if (!buf)
  545. return;
  546. cache = dir->cache;
  547. cache_name = cache->ofnode->full_name;
  548. cache_type = cache_type_string(cache);
  549. /* We don't want to create an attribute that can't provide a
  550. * meaningful value. Check the return value of each optional
  551. * attribute's ->show method before registering the
  552. * attribute.
  553. */
  554. for (i = 0; i < ARRAY_SIZE(cache_index_opt_attrs); i++) {
  555. struct kobj_attribute *attr;
  556. ssize_t rc;
  557. attr = cache_index_opt_attrs[i];
  558. rc = attr->show(&dir->kobj, attr, buf);
  559. if (rc <= 0) {
  560. pr_debug("not creating %s attribute for "
  561. "%s(%s) (rc = %zd)\n",
  562. attr->attr.name, cache_name,
  563. cache_type, rc);
  564. continue;
  565. }
  566. if (sysfs_create_file(&dir->kobj, &attr->attr))
  567. pr_debug("could not create %s attribute for %s(%s)\n",
  568. attr->attr.name, cache_name, cache_type);
  569. }
  570. kfree(buf);
  571. }
  572. static void cacheinfo_create_index_dir(struct cache *cache, int index,
  573. struct cache_dir *cache_dir)
  574. {
  575. struct cache_index_dir *index_dir;
  576. int rc;
  577. index_dir = kzalloc(sizeof(*index_dir), GFP_KERNEL);
  578. if (!index_dir)
  579. goto err;
  580. index_dir->cache = cache;
  581. rc = kobject_init_and_add(&index_dir->kobj, &cache_index_type,
  582. cache_dir->kobj, "index%d", index);
  583. if (rc)
  584. goto err;
  585. index_dir->next = cache_dir->index;
  586. cache_dir->index = index_dir;
  587. cacheinfo_create_index_opt_attrs(index_dir);
  588. return;
  589. err:
  590. kfree(index_dir);
  591. }
  592. static void cacheinfo_sysfs_populate(unsigned int cpu_id,
  593. struct cache *cache_list)
  594. {
  595. struct cache_dir *cache_dir;
  596. struct cache *cache;
  597. int index = 0;
  598. cache_dir = cacheinfo_create_cache_dir(cpu_id);
  599. if (!cache_dir)
  600. return;
  601. cache = cache_list;
  602. while (cache) {
  603. cacheinfo_create_index_dir(cache, index, cache_dir);
  604. index++;
  605. cache = cache->next_local;
  606. }
  607. }
  608. void cacheinfo_cpu_online(unsigned int cpu_id)
  609. {
  610. struct cache *cache;
  611. cache = cache_chain_instantiate(cpu_id);
  612. if (!cache)
  613. return;
  614. cacheinfo_sysfs_populate(cpu_id, cache);
  615. }
  616. /* functions needed to remove cache entry for cpu offline or suspend/resume */
  617. #if (defined(CONFIG_PPC_PSERIES) && defined(CONFIG_SUSPEND)) || \
  618. defined(CONFIG_HOTPLUG_CPU)
  619. static struct cache *cache_lookup_by_cpu(unsigned int cpu_id)
  620. {
  621. struct device_node *cpu_node;
  622. struct cache *cache;
  623. cpu_node = of_get_cpu_node(cpu_id, NULL);
  624. WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
  625. if (!cpu_node)
  626. return NULL;
  627. cache = cache_lookup_by_node(cpu_node);
  628. of_node_put(cpu_node);
  629. return cache;
  630. }
  631. static void remove_index_dirs(struct cache_dir *cache_dir)
  632. {
  633. struct cache_index_dir *index;
  634. index = cache_dir->index;
  635. while (index) {
  636. struct cache_index_dir *next;
  637. next = index->next;
  638. kobject_put(&index->kobj);
  639. index = next;
  640. }
  641. }
  642. static void remove_cache_dir(struct cache_dir *cache_dir)
  643. {
  644. remove_index_dirs(cache_dir);
  645. /* Remove cache dir from sysfs */
  646. kobject_del(cache_dir->kobj);
  647. kobject_put(cache_dir->kobj);
  648. kfree(cache_dir);
  649. }
  650. static void cache_cpu_clear(struct cache *cache, int cpu)
  651. {
  652. while (cache) {
  653. struct cache *next = cache->next_local;
  654. WARN_ONCE(!cpumask_test_cpu(cpu, &cache->shared_cpu_map),
  655. "CPU %i not accounted in %s(%s)\n",
  656. cpu, cache->ofnode->full_name,
  657. cache_type_string(cache));
  658. cpumask_clear_cpu(cpu, &cache->shared_cpu_map);
  659. /* Release the cache object if all the cpus using it
  660. * are offline */
  661. if (cpumask_empty(&cache->shared_cpu_map))
  662. release_cache(cache);
  663. cache = next;
  664. }
  665. }
  666. void cacheinfo_cpu_offline(unsigned int cpu_id)
  667. {
  668. struct cache_dir *cache_dir;
  669. struct cache *cache;
  670. /* Prevent userspace from seeing inconsistent state - remove
  671. * the sysfs hierarchy first */
  672. cache_dir = per_cpu(cache_dir_pcpu, cpu_id);
  673. /* careful, sysfs population may have failed */
  674. if (cache_dir)
  675. remove_cache_dir(cache_dir);
  676. per_cpu(cache_dir_pcpu, cpu_id) = NULL;
  677. /* clear the CPU's bit in its cache chain, possibly freeing
  678. * cache objects */
  679. cache = cache_lookup_by_cpu(cpu_id);
  680. if (cache)
  681. cache_cpu_clear(cache, cpu_id);
  682. }
  683. #endif /* (CONFIG_PPC_PSERIES && CONFIG_SUSPEND) || CONFIG_HOTPLUG_CPU */