cacheinfo.c 21 KB

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