cacheinfo.c 20 KB

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