cacheinfo.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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
  58. #define CACHE_TYPE_INSTRUCTION 1
  59. #define CACHE_TYPE_DATA 2
  60. static const struct cache_type_info cache_type_info[] = {
  61. {
  62. /* PowerPC Processor binding says the [di]-cache-*
  63. * must be equal on unified caches, so just use
  64. * d-cache properties. */
  65. .name = "Unified",
  66. .size_prop = "d-cache-size",
  67. .line_size_props = { "d-cache-line-size",
  68. "d-cache-block-size", },
  69. .nr_sets_prop = "d-cache-sets",
  70. },
  71. {
  72. .name = "Instruction",
  73. .size_prop = "i-cache-size",
  74. .line_size_props = { "i-cache-line-size",
  75. "i-cache-block-size", },
  76. .nr_sets_prop = "i-cache-sets",
  77. },
  78. {
  79. .name = "Data",
  80. .size_prop = "d-cache-size",
  81. .line_size_props = { "d-cache-line-size",
  82. "d-cache-block-size", },
  83. .nr_sets_prop = "d-cache-sets",
  84. },
  85. };
  86. /* Cache object: each instance of this corresponds to a distinct cache
  87. * in the system. There are separate objects for Harvard caches: one
  88. * each for instruction and data, and each refers to the same OF node.
  89. * The refcount of the OF node is elevated for the lifetime of the
  90. * cache object. A cache object is released when its shared_cpu_map
  91. * is cleared (see cache_cpu_clear).
  92. *
  93. * A cache object is on two lists: an unsorted global list
  94. * (cache_list) of cache objects; and a singly-linked list
  95. * representing the local cache hierarchy, which is ordered by level
  96. * (e.g. L1d -> L1i -> L2 -> L3).
  97. */
  98. struct cache {
  99. struct device_node *ofnode; /* OF node for this cache, may be cpu */
  100. struct cpumask shared_cpu_map; /* online CPUs using this cache */
  101. int type; /* split cache disambiguation */
  102. int level; /* level not explicit in device tree */
  103. struct list_head list; /* global list of cache objects */
  104. struct cache *next_local; /* next cache of >= level */
  105. };
  106. static DEFINE_PER_CPU(struct cache_dir *, cache_dir_pcpu);
  107. /* traversal/modification of this list occurs only at cpu hotplug time;
  108. * access is serialized by cpu hotplug locking
  109. */
  110. static LIST_HEAD(cache_list);
  111. static struct cache_index_dir *kobj_to_cache_index_dir(struct kobject *k)
  112. {
  113. return container_of(k, struct cache_index_dir, kobj);
  114. }
  115. static const char *cache_type_string(const struct cache *cache)
  116. {
  117. return cache_type_info[cache->type].name;
  118. }
  119. static void __cpuinit cache_init(struct cache *cache, int type, int level, struct device_node *ofnode)
  120. {
  121. cache->type = type;
  122. cache->level = level;
  123. cache->ofnode = of_node_get(ofnode);
  124. INIT_LIST_HEAD(&cache->list);
  125. list_add(&cache->list, &cache_list);
  126. }
  127. static struct cache *__cpuinit new_cache(int type, int level, struct device_node *ofnode)
  128. {
  129. struct cache *cache;
  130. cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  131. if (cache)
  132. cache_init(cache, type, level, ofnode);
  133. return cache;
  134. }
  135. static void release_cache_debugcheck(struct cache *cache)
  136. {
  137. struct cache *iter;
  138. list_for_each_entry(iter, &cache_list, list)
  139. WARN_ONCE(iter->next_local == cache,
  140. "cache for %s(%s) refers to cache for %s(%s)\n",
  141. iter->ofnode->full_name,
  142. cache_type_string(iter),
  143. cache->ofnode->full_name,
  144. cache_type_string(cache));
  145. }
  146. static void release_cache(struct cache *cache)
  147. {
  148. if (!cache)
  149. return;
  150. pr_debug("freeing L%d %s cache for %s\n", cache->level,
  151. cache_type_string(cache), cache->ofnode->full_name);
  152. release_cache_debugcheck(cache);
  153. list_del(&cache->list);
  154. of_node_put(cache->ofnode);
  155. kfree(cache);
  156. }
  157. static void cache_cpu_set(struct cache *cache, int cpu)
  158. {
  159. struct cache *next = cache;
  160. while (next) {
  161. WARN_ONCE(cpumask_test_cpu(cpu, &next->shared_cpu_map),
  162. "CPU %i already accounted in %s(%s)\n",
  163. cpu, next->ofnode->full_name,
  164. cache_type_string(next));
  165. cpumask_set_cpu(cpu, &next->shared_cpu_map);
  166. next = next->next_local;
  167. }
  168. }
  169. static int cache_size(const struct cache *cache, unsigned int *ret)
  170. {
  171. const char *propname;
  172. const u32 *cache_size;
  173. propname = cache_type_info[cache->type].size_prop;
  174. cache_size = of_get_property(cache->ofnode, propname, NULL);
  175. if (!cache_size)
  176. return -ENODEV;
  177. *ret = *cache_size;
  178. return 0;
  179. }
  180. static int cache_size_kb(const struct cache *cache, unsigned int *ret)
  181. {
  182. unsigned int size;
  183. if (cache_size(cache, &size))
  184. return -ENODEV;
  185. *ret = size / 1024;
  186. return 0;
  187. }
  188. /* not cache_line_size() because that's a macro in include/linux/cache.h */
  189. static int cache_get_line_size(const struct cache *cache, unsigned int *ret)
  190. {
  191. const u32 *line_size;
  192. int i, lim;
  193. lim = ARRAY_SIZE(cache_type_info[cache->type].line_size_props);
  194. for (i = 0; i < lim; i++) {
  195. const char *propname;
  196. propname = cache_type_info[cache->type].line_size_props[i];
  197. line_size = of_get_property(cache->ofnode, propname, NULL);
  198. if (line_size)
  199. break;
  200. }
  201. if (!line_size)
  202. return -ENODEV;
  203. *ret = *line_size;
  204. return 0;
  205. }
  206. static int cache_nr_sets(const struct cache *cache, unsigned int *ret)
  207. {
  208. const char *propname;
  209. const u32 *nr_sets;
  210. propname = cache_type_info[cache->type].nr_sets_prop;
  211. nr_sets = of_get_property(cache->ofnode, propname, NULL);
  212. if (!nr_sets)
  213. return -ENODEV;
  214. *ret = *nr_sets;
  215. return 0;
  216. }
  217. static int cache_associativity(const struct cache *cache, unsigned int *ret)
  218. {
  219. unsigned int line_size;
  220. unsigned int nr_sets;
  221. unsigned int size;
  222. if (cache_nr_sets(cache, &nr_sets))
  223. goto err;
  224. /* If the cache is fully associative, there is no need to
  225. * check the other properties.
  226. */
  227. if (nr_sets == 1) {
  228. *ret = 0;
  229. return 0;
  230. }
  231. if (cache_get_line_size(cache, &line_size))
  232. goto err;
  233. if (cache_size(cache, &size))
  234. goto err;
  235. if (!(nr_sets > 0 && size > 0 && line_size > 0))
  236. goto err;
  237. *ret = (size / nr_sets) / line_size;
  238. return 0;
  239. err:
  240. return -ENODEV;
  241. }
  242. /* helper for dealing with split caches */
  243. static struct cache *cache_find_first_sibling(struct cache *cache)
  244. {
  245. struct cache *iter;
  246. if (cache->type == CACHE_TYPE_UNIFIED)
  247. return cache;
  248. list_for_each_entry(iter, &cache_list, list)
  249. if (iter->ofnode == cache->ofnode && iter->next_local == cache)
  250. return iter;
  251. return cache;
  252. }
  253. /* return the first cache on a local list matching node */
  254. static struct cache *cache_lookup_by_node(const struct device_node *node)
  255. {
  256. struct cache *cache = NULL;
  257. struct cache *iter;
  258. list_for_each_entry(iter, &cache_list, list) {
  259. if (iter->ofnode != node)
  260. continue;
  261. cache = cache_find_first_sibling(iter);
  262. break;
  263. }
  264. return cache;
  265. }
  266. static bool cache_node_is_unified(const struct device_node *np)
  267. {
  268. return of_get_property(np, "cache-unified", NULL);
  269. }
  270. static struct cache *__cpuinit cache_do_one_devnode_unified(struct device_node *node, int level)
  271. {
  272. struct cache *cache;
  273. pr_debug("creating L%d ucache for %s\n", level, node->full_name);
  274. cache = new_cache(CACHE_TYPE_UNIFIED, level, node);
  275. return cache;
  276. }
  277. static struct cache *__cpuinit cache_do_one_devnode_split(struct device_node *node, int level)
  278. {
  279. struct cache *dcache, *icache;
  280. pr_debug("creating L%d dcache and icache for %s\n", level,
  281. node->full_name);
  282. dcache = new_cache(CACHE_TYPE_DATA, level, node);
  283. icache = new_cache(CACHE_TYPE_INSTRUCTION, level, node);
  284. if (!dcache || !icache)
  285. goto err;
  286. dcache->next_local = icache;
  287. return dcache;
  288. err:
  289. release_cache(dcache);
  290. release_cache(icache);
  291. return NULL;
  292. }
  293. static struct cache *__cpuinit cache_do_one_devnode(struct device_node *node, int level)
  294. {
  295. struct cache *cache;
  296. if (cache_node_is_unified(node))
  297. cache = cache_do_one_devnode_unified(node, level);
  298. else
  299. cache = cache_do_one_devnode_split(node, level);
  300. return cache;
  301. }
  302. static struct cache *__cpuinit cache_lookup_or_instantiate(struct device_node *node, int level)
  303. {
  304. struct cache *cache;
  305. cache = cache_lookup_by_node(node);
  306. WARN_ONCE(cache && cache->level != level,
  307. "cache level mismatch on lookup (got %d, expected %d)\n",
  308. cache->level, level);
  309. if (!cache)
  310. cache = cache_do_one_devnode(node, level);
  311. return cache;
  312. }
  313. static void __cpuinit link_cache_lists(struct cache *smaller, struct cache *bigger)
  314. {
  315. while (smaller->next_local) {
  316. if (smaller->next_local == bigger)
  317. return; /* already linked */
  318. smaller = smaller->next_local;
  319. }
  320. smaller->next_local = bigger;
  321. }
  322. static void __cpuinit do_subsidiary_caches_debugcheck(struct cache *cache)
  323. {
  324. WARN_ON_ONCE(cache->level != 1);
  325. WARN_ON_ONCE(strcmp(cache->ofnode->type, "cpu"));
  326. }
  327. static void __cpuinit do_subsidiary_caches(struct cache *cache)
  328. {
  329. struct device_node *subcache_node;
  330. int level = cache->level;
  331. do_subsidiary_caches_debugcheck(cache);
  332. while ((subcache_node = of_find_next_cache_node(cache->ofnode))) {
  333. struct cache *subcache;
  334. level++;
  335. subcache = cache_lookup_or_instantiate(subcache_node, level);
  336. of_node_put(subcache_node);
  337. if (!subcache)
  338. break;
  339. link_cache_lists(cache, subcache);
  340. cache = subcache;
  341. }
  342. }
  343. static struct cache *__cpuinit cache_chain_instantiate(unsigned int cpu_id)
  344. {
  345. struct device_node *cpu_node;
  346. struct cache *cpu_cache = NULL;
  347. pr_debug("creating cache object(s) for CPU %i\n", cpu_id);
  348. cpu_node = of_get_cpu_node(cpu_id, NULL);
  349. WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
  350. if (!cpu_node)
  351. goto out;
  352. cpu_cache = cache_lookup_or_instantiate(cpu_node, 1);
  353. if (!cpu_cache)
  354. goto out;
  355. do_subsidiary_caches(cpu_cache);
  356. cache_cpu_set(cpu_cache, cpu_id);
  357. out:
  358. of_node_put(cpu_node);
  359. return cpu_cache;
  360. }
  361. static struct cache_dir *__cpuinit cacheinfo_create_cache_dir(unsigned int cpu_id)
  362. {
  363. struct cache_dir *cache_dir;
  364. struct sys_device *sysdev;
  365. struct kobject *kobj = NULL;
  366. sysdev = get_cpu_sysdev(cpu_id);
  367. WARN_ONCE(!sysdev, "no sysdev for CPU %i\n", cpu_id);
  368. if (!sysdev)
  369. goto err;
  370. kobj = kobject_create_and_add("cache", &sysdev->kobj);
  371. if (!kobj)
  372. goto err;
  373. cache_dir = kzalloc(sizeof(*cache_dir), GFP_KERNEL);
  374. if (!cache_dir)
  375. goto err;
  376. cache_dir->kobj = kobj;
  377. WARN_ON_ONCE(per_cpu(cache_dir_pcpu, cpu_id) != NULL);
  378. per_cpu(cache_dir_pcpu, cpu_id) = cache_dir;
  379. return cache_dir;
  380. err:
  381. kobject_put(kobj);
  382. return NULL;
  383. }
  384. static void cache_index_release(struct kobject *kobj)
  385. {
  386. struct cache_index_dir *index;
  387. index = kobj_to_cache_index_dir(kobj);
  388. pr_debug("freeing index directory for L%d %s cache\n",
  389. index->cache->level, cache_type_string(index->cache));
  390. kfree(index);
  391. }
  392. static ssize_t cache_index_show(struct kobject *k, struct attribute *attr, char *buf)
  393. {
  394. struct kobj_attribute *kobj_attr;
  395. kobj_attr = container_of(attr, struct kobj_attribute, attr);
  396. return kobj_attr->show(k, kobj_attr, buf);
  397. }
  398. static struct cache *index_kobj_to_cache(struct kobject *k)
  399. {
  400. struct cache_index_dir *index;
  401. index = kobj_to_cache_index_dir(k);
  402. return index->cache;
  403. }
  404. static ssize_t size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  405. {
  406. unsigned int size_kb;
  407. struct cache *cache;
  408. cache = index_kobj_to_cache(k);
  409. if (cache_size_kb(cache, &size_kb))
  410. return -ENODEV;
  411. return sprintf(buf, "%uK\n", size_kb);
  412. }
  413. static struct kobj_attribute cache_size_attr =
  414. __ATTR(size, 0444, size_show, NULL);
  415. static ssize_t line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  416. {
  417. unsigned int line_size;
  418. struct cache *cache;
  419. cache = index_kobj_to_cache(k);
  420. if (cache_get_line_size(cache, &line_size))
  421. return -ENODEV;
  422. return sprintf(buf, "%u\n", line_size);
  423. }
  424. static struct kobj_attribute cache_line_size_attr =
  425. __ATTR(coherency_line_size, 0444, line_size_show, NULL);
  426. static ssize_t nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  427. {
  428. unsigned int nr_sets;
  429. struct cache *cache;
  430. cache = index_kobj_to_cache(k);
  431. if (cache_nr_sets(cache, &nr_sets))
  432. return -ENODEV;
  433. return sprintf(buf, "%u\n", nr_sets);
  434. }
  435. static struct kobj_attribute cache_nr_sets_attr =
  436. __ATTR(number_of_sets, 0444, nr_sets_show, NULL);
  437. static ssize_t associativity_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  438. {
  439. unsigned int associativity;
  440. struct cache *cache;
  441. cache = index_kobj_to_cache(k);
  442. if (cache_associativity(cache, &associativity))
  443. return -ENODEV;
  444. return sprintf(buf, "%u\n", associativity);
  445. }
  446. static struct kobj_attribute cache_assoc_attr =
  447. __ATTR(ways_of_associativity, 0444, associativity_show, NULL);
  448. static ssize_t type_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  449. {
  450. struct cache *cache;
  451. cache = index_kobj_to_cache(k);
  452. return sprintf(buf, "%s\n", cache_type_string(cache));
  453. }
  454. static struct kobj_attribute cache_type_attr =
  455. __ATTR(type, 0444, type_show, NULL);
  456. static ssize_t level_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  457. {
  458. struct cache_index_dir *index;
  459. struct cache *cache;
  460. index = kobj_to_cache_index_dir(k);
  461. cache = index->cache;
  462. return sprintf(buf, "%d\n", cache->level);
  463. }
  464. static struct kobj_attribute cache_level_attr =
  465. __ATTR(level, 0444, level_show, NULL);
  466. static ssize_t shared_cpu_map_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  467. {
  468. struct cache_index_dir *index;
  469. struct cache *cache;
  470. int len;
  471. int n = 0;
  472. index = kobj_to_cache_index_dir(k);
  473. cache = index->cache;
  474. len = PAGE_SIZE - 2;
  475. if (len > 1) {
  476. n = cpumask_scnprintf(buf, len, &cache->shared_cpu_map);
  477. buf[n++] = '\n';
  478. buf[n] = '\0';
  479. }
  480. return n;
  481. }
  482. static struct kobj_attribute cache_shared_cpu_map_attr =
  483. __ATTR(shared_cpu_map, 0444, shared_cpu_map_show, NULL);
  484. /* Attributes which should always be created -- the kobject/sysfs core
  485. * does this automatically via kobj_type->default_attrs. This is the
  486. * minimum data required to uniquely identify a cache.
  487. */
  488. static struct attribute *cache_index_default_attrs[] = {
  489. &cache_type_attr.attr,
  490. &cache_level_attr.attr,
  491. &cache_shared_cpu_map_attr.attr,
  492. NULL,
  493. };
  494. /* Attributes which should be created if the cache device node has the
  495. * right properties -- see cacheinfo_create_index_opt_attrs
  496. */
  497. static struct kobj_attribute *cache_index_opt_attrs[] = {
  498. &cache_size_attr,
  499. &cache_line_size_attr,
  500. &cache_nr_sets_attr,
  501. &cache_assoc_attr,
  502. };
  503. static const struct sysfs_ops cache_index_ops = {
  504. .show = cache_index_show,
  505. };
  506. static struct kobj_type cache_index_type = {
  507. .release = cache_index_release,
  508. .sysfs_ops = &cache_index_ops,
  509. .default_attrs = cache_index_default_attrs,
  510. };
  511. static void __cpuinit cacheinfo_create_index_opt_attrs(struct cache_index_dir *dir)
  512. {
  513. const char *cache_name;
  514. const char *cache_type;
  515. struct cache *cache;
  516. char *buf;
  517. int i;
  518. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  519. if (!buf)
  520. return;
  521. cache = dir->cache;
  522. cache_name = cache->ofnode->full_name;
  523. cache_type = cache_type_string(cache);
  524. /* We don't want to create an attribute that can't provide a
  525. * meaningful value. Check the return value of each optional
  526. * attribute's ->show method before registering the
  527. * attribute.
  528. */
  529. for (i = 0; i < ARRAY_SIZE(cache_index_opt_attrs); i++) {
  530. struct kobj_attribute *attr;
  531. ssize_t rc;
  532. attr = cache_index_opt_attrs[i];
  533. rc = attr->show(&dir->kobj, attr, buf);
  534. if (rc <= 0) {
  535. pr_debug("not creating %s attribute for "
  536. "%s(%s) (rc = %zd)\n",
  537. attr->attr.name, cache_name,
  538. cache_type, rc);
  539. continue;
  540. }
  541. if (sysfs_create_file(&dir->kobj, &attr->attr))
  542. pr_debug("could not create %s attribute for %s(%s)\n",
  543. attr->attr.name, cache_name, cache_type);
  544. }
  545. kfree(buf);
  546. }
  547. static void __cpuinit cacheinfo_create_index_dir(struct cache *cache, int index, struct cache_dir *cache_dir)
  548. {
  549. struct cache_index_dir *index_dir;
  550. int rc;
  551. index_dir = kzalloc(sizeof(*index_dir), GFP_KERNEL);
  552. if (!index_dir)
  553. goto err;
  554. index_dir->cache = cache;
  555. rc = kobject_init_and_add(&index_dir->kobj, &cache_index_type,
  556. cache_dir->kobj, "index%d", index);
  557. if (rc)
  558. goto err;
  559. index_dir->next = cache_dir->index;
  560. cache_dir->index = index_dir;
  561. cacheinfo_create_index_opt_attrs(index_dir);
  562. return;
  563. err:
  564. kfree(index_dir);
  565. }
  566. static void __cpuinit cacheinfo_sysfs_populate(unsigned int cpu_id, struct cache *cache_list)
  567. {
  568. struct cache_dir *cache_dir;
  569. struct cache *cache;
  570. int index = 0;
  571. cache_dir = cacheinfo_create_cache_dir(cpu_id);
  572. if (!cache_dir)
  573. return;
  574. cache = cache_list;
  575. while (cache) {
  576. cacheinfo_create_index_dir(cache, index, cache_dir);
  577. index++;
  578. cache = cache->next_local;
  579. }
  580. }
  581. void __cpuinit cacheinfo_cpu_online(unsigned int cpu_id)
  582. {
  583. struct cache *cache;
  584. cache = cache_chain_instantiate(cpu_id);
  585. if (!cache)
  586. return;
  587. cacheinfo_sysfs_populate(cpu_id, cache);
  588. }
  589. #ifdef CONFIG_HOTPLUG_CPU /* functions needed for cpu offline */
  590. static struct cache *cache_lookup_by_cpu(unsigned int cpu_id)
  591. {
  592. struct device_node *cpu_node;
  593. struct cache *cache;
  594. cpu_node = of_get_cpu_node(cpu_id, NULL);
  595. WARN_ONCE(!cpu_node, "no OF node found for CPU %i\n", cpu_id);
  596. if (!cpu_node)
  597. return NULL;
  598. cache = cache_lookup_by_node(cpu_node);
  599. of_node_put(cpu_node);
  600. return cache;
  601. }
  602. static void remove_index_dirs(struct cache_dir *cache_dir)
  603. {
  604. struct cache_index_dir *index;
  605. index = cache_dir->index;
  606. while (index) {
  607. struct cache_index_dir *next;
  608. next = index->next;
  609. kobject_put(&index->kobj);
  610. index = next;
  611. }
  612. }
  613. static void remove_cache_dir(struct cache_dir *cache_dir)
  614. {
  615. remove_index_dirs(cache_dir);
  616. kobject_put(cache_dir->kobj);
  617. kfree(cache_dir);
  618. }
  619. static void cache_cpu_clear(struct cache *cache, int cpu)
  620. {
  621. while (cache) {
  622. struct cache *next = cache->next_local;
  623. WARN_ONCE(!cpumask_test_cpu(cpu, &cache->shared_cpu_map),
  624. "CPU %i not accounted in %s(%s)\n",
  625. cpu, cache->ofnode->full_name,
  626. cache_type_string(cache));
  627. cpumask_clear_cpu(cpu, &cache->shared_cpu_map);
  628. /* Release the cache object if all the cpus using it
  629. * are offline */
  630. if (cpumask_empty(&cache->shared_cpu_map))
  631. release_cache(cache);
  632. cache = next;
  633. }
  634. }
  635. void cacheinfo_cpu_offline(unsigned int cpu_id)
  636. {
  637. struct cache_dir *cache_dir;
  638. struct cache *cache;
  639. /* Prevent userspace from seeing inconsistent state - remove
  640. * the sysfs hierarchy first */
  641. cache_dir = per_cpu(cache_dir_pcpu, cpu_id);
  642. /* careful, sysfs population may have failed */
  643. if (cache_dir)
  644. remove_cache_dir(cache_dir);
  645. per_cpu(cache_dir_pcpu, cpu_id) = NULL;
  646. /* clear the CPU's bit in its cache chain, possibly freeing
  647. * cache objects */
  648. cache = cache_lookup_by_cpu(cpu_id);
  649. if (cache)
  650. cache_cpu_clear(cache, cpu_id);
  651. }
  652. #endif /* CONFIG_HOTPLUG_CPU */