cacheinfo.c 20 KB

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