cacheinfo.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cacheinfo support - processor cache information via sysfs
  4. *
  5. * Based on arch/x86/kernel/cpu/intel_cacheinfo.c
  6. * Author: Sudeep Holla <sudeep.holla@arm.com>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/acpi.h>
  10. #include <linux/bitops.h>
  11. #include <linux/cacheinfo.h>
  12. #include <linux/compiler.h>
  13. #include <linux/cpu.h>
  14. #include <linux/device.h>
  15. #include <linux/init.h>
  16. #include <linux/of.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/smp.h>
  20. #include <linux/sysfs.h>
  21. /* pointer to per cpu cacheinfo */
  22. static DEFINE_PER_CPU(struct cpu_cacheinfo, ci_cpu_cacheinfo);
  23. #define ci_cacheinfo(cpu) (&per_cpu(ci_cpu_cacheinfo, cpu))
  24. #define cache_leaves(cpu) (ci_cacheinfo(cpu)->num_leaves)
  25. #define per_cpu_cacheinfo(cpu) (ci_cacheinfo(cpu)->info_list)
  26. struct cpu_cacheinfo *get_cpu_cacheinfo(unsigned int cpu)
  27. {
  28. return ci_cacheinfo(cpu);
  29. }
  30. #ifdef CONFIG_OF
  31. static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
  32. struct cacheinfo *sib_leaf)
  33. {
  34. return sib_leaf->fw_token == this_leaf->fw_token;
  35. }
  36. /* OF properties to query for a given cache type */
  37. struct cache_type_info {
  38. const char *size_prop;
  39. const char *line_size_props[2];
  40. const char *nr_sets_prop;
  41. };
  42. static const struct cache_type_info cache_type_info[] = {
  43. {
  44. .size_prop = "cache-size",
  45. .line_size_props = { "cache-line-size",
  46. "cache-block-size", },
  47. .nr_sets_prop = "cache-sets",
  48. }, {
  49. .size_prop = "i-cache-size",
  50. .line_size_props = { "i-cache-line-size",
  51. "i-cache-block-size", },
  52. .nr_sets_prop = "i-cache-sets",
  53. }, {
  54. .size_prop = "d-cache-size",
  55. .line_size_props = { "d-cache-line-size",
  56. "d-cache-block-size", },
  57. .nr_sets_prop = "d-cache-sets",
  58. },
  59. };
  60. static inline int get_cacheinfo_idx(enum cache_type type)
  61. {
  62. if (type == CACHE_TYPE_UNIFIED)
  63. return 0;
  64. return type;
  65. }
  66. static void cache_size(struct cacheinfo *this_leaf, struct device_node *np)
  67. {
  68. const char *propname;
  69. int ct_idx;
  70. ct_idx = get_cacheinfo_idx(this_leaf->type);
  71. propname = cache_type_info[ct_idx].size_prop;
  72. of_property_read_u32(np, propname, &this_leaf->size);
  73. }
  74. /* not cache_line_size() because that's a macro in include/linux/cache.h */
  75. static void cache_get_line_size(struct cacheinfo *this_leaf,
  76. struct device_node *np)
  77. {
  78. int i, lim, ct_idx;
  79. ct_idx = get_cacheinfo_idx(this_leaf->type);
  80. lim = ARRAY_SIZE(cache_type_info[ct_idx].line_size_props);
  81. for (i = 0; i < lim; i++) {
  82. int ret;
  83. u32 line_size;
  84. const char *propname;
  85. propname = cache_type_info[ct_idx].line_size_props[i];
  86. ret = of_property_read_u32(np, propname, &line_size);
  87. if (!ret) {
  88. this_leaf->coherency_line_size = line_size;
  89. break;
  90. }
  91. }
  92. }
  93. static void cache_nr_sets(struct cacheinfo *this_leaf, struct device_node *np)
  94. {
  95. const char *propname;
  96. int ct_idx;
  97. ct_idx = get_cacheinfo_idx(this_leaf->type);
  98. propname = cache_type_info[ct_idx].nr_sets_prop;
  99. of_property_read_u32(np, propname, &this_leaf->number_of_sets);
  100. }
  101. static void cache_associativity(struct cacheinfo *this_leaf)
  102. {
  103. unsigned int line_size = this_leaf->coherency_line_size;
  104. unsigned int nr_sets = this_leaf->number_of_sets;
  105. unsigned int size = this_leaf->size;
  106. /*
  107. * If the cache is fully associative, there is no need to
  108. * check the other properties.
  109. */
  110. if (!(nr_sets == 1) && (nr_sets > 0 && size > 0 && line_size > 0))
  111. this_leaf->ways_of_associativity = (size / nr_sets) / line_size;
  112. }
  113. static bool cache_node_is_unified(struct cacheinfo *this_leaf,
  114. struct device_node *np)
  115. {
  116. return of_property_read_bool(np, "cache-unified");
  117. }
  118. static void cache_of_set_props(struct cacheinfo *this_leaf,
  119. struct device_node *np)
  120. {
  121. /*
  122. * init_cache_level must setup the cache level correctly
  123. * overriding the architecturally specified levels, so
  124. * if type is NONE at this stage, it should be unified
  125. */
  126. if (this_leaf->type == CACHE_TYPE_NOCACHE &&
  127. cache_node_is_unified(this_leaf, np))
  128. this_leaf->type = CACHE_TYPE_UNIFIED;
  129. cache_size(this_leaf, np);
  130. cache_get_line_size(this_leaf, np);
  131. cache_nr_sets(this_leaf, np);
  132. cache_associativity(this_leaf);
  133. }
  134. static int cache_setup_of_node(unsigned int cpu)
  135. {
  136. struct device_node *np;
  137. struct cacheinfo *this_leaf;
  138. struct device *cpu_dev = get_cpu_device(cpu);
  139. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  140. unsigned int index = 0;
  141. /* skip if fw_token is already populated */
  142. if (this_cpu_ci->info_list->fw_token) {
  143. return 0;
  144. }
  145. if (!cpu_dev) {
  146. pr_err("No cpu device for CPU %d\n", cpu);
  147. return -ENODEV;
  148. }
  149. np = cpu_dev->of_node;
  150. if (!np) {
  151. pr_err("Failed to find cpu%d device node\n", cpu);
  152. return -ENOENT;
  153. }
  154. while (index < cache_leaves(cpu)) {
  155. this_leaf = this_cpu_ci->info_list + index;
  156. if (this_leaf->level != 1)
  157. np = of_find_next_cache_node(np);
  158. else
  159. np = of_node_get(np);/* cpu node itself */
  160. if (!np)
  161. break;
  162. cache_of_set_props(this_leaf, np);
  163. this_leaf->fw_token = np;
  164. index++;
  165. }
  166. if (index != cache_leaves(cpu)) /* not all OF nodes populated */
  167. return -ENOENT;
  168. return 0;
  169. }
  170. #else
  171. static inline int cache_setup_of_node(unsigned int cpu) { return 0; }
  172. static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
  173. struct cacheinfo *sib_leaf)
  174. {
  175. /*
  176. * For non-DT/ACPI systems, assume unique level 1 caches, system-wide
  177. * shared caches for all other levels. This will be used only if
  178. * arch specific code has not populated shared_cpu_map
  179. */
  180. return !(this_leaf->level == 1);
  181. }
  182. #endif
  183. int __weak cache_setup_acpi(unsigned int cpu)
  184. {
  185. return -ENOTSUPP;
  186. }
  187. static int cache_shared_cpu_map_setup(unsigned int cpu)
  188. {
  189. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  190. struct cacheinfo *this_leaf, *sib_leaf;
  191. unsigned int index;
  192. int ret = 0;
  193. if (this_cpu_ci->cpu_map_populated)
  194. return 0;
  195. if (of_have_populated_dt())
  196. ret = cache_setup_of_node(cpu);
  197. else if (!acpi_disabled)
  198. ret = cache_setup_acpi(cpu);
  199. if (ret)
  200. return ret;
  201. for (index = 0; index < cache_leaves(cpu); index++) {
  202. unsigned int i;
  203. this_leaf = this_cpu_ci->info_list + index;
  204. /* skip if shared_cpu_map is already populated */
  205. if (!cpumask_empty(&this_leaf->shared_cpu_map))
  206. continue;
  207. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  208. for_each_online_cpu(i) {
  209. struct cpu_cacheinfo *sib_cpu_ci = get_cpu_cacheinfo(i);
  210. if (i == cpu || !sib_cpu_ci->info_list)
  211. continue;/* skip if itself or no cacheinfo */
  212. sib_leaf = sib_cpu_ci->info_list + index;
  213. if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
  214. cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map);
  215. cpumask_set_cpu(i, &this_leaf->shared_cpu_map);
  216. }
  217. }
  218. }
  219. return 0;
  220. }
  221. static void cache_shared_cpu_map_remove(unsigned int cpu)
  222. {
  223. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  224. struct cacheinfo *this_leaf, *sib_leaf;
  225. unsigned int sibling, index;
  226. for (index = 0; index < cache_leaves(cpu); index++) {
  227. this_leaf = this_cpu_ci->info_list + index;
  228. for_each_cpu(sibling, &this_leaf->shared_cpu_map) {
  229. struct cpu_cacheinfo *sib_cpu_ci;
  230. if (sibling == cpu) /* skip itself */
  231. continue;
  232. sib_cpu_ci = get_cpu_cacheinfo(sibling);
  233. if (!sib_cpu_ci->info_list)
  234. continue;
  235. sib_leaf = sib_cpu_ci->info_list + index;
  236. cpumask_clear_cpu(cpu, &sib_leaf->shared_cpu_map);
  237. cpumask_clear_cpu(sibling, &this_leaf->shared_cpu_map);
  238. }
  239. if (of_have_populated_dt())
  240. of_node_put(this_leaf->fw_token);
  241. }
  242. }
  243. static void free_cache_attributes(unsigned int cpu)
  244. {
  245. if (!per_cpu_cacheinfo(cpu))
  246. return;
  247. cache_shared_cpu_map_remove(cpu);
  248. kfree(per_cpu_cacheinfo(cpu));
  249. per_cpu_cacheinfo(cpu) = NULL;
  250. }
  251. int __weak init_cache_level(unsigned int cpu)
  252. {
  253. return -ENOENT;
  254. }
  255. int __weak populate_cache_leaves(unsigned int cpu)
  256. {
  257. return -ENOENT;
  258. }
  259. static int detect_cache_attributes(unsigned int cpu)
  260. {
  261. int ret;
  262. if (init_cache_level(cpu) || !cache_leaves(cpu))
  263. return -ENOENT;
  264. per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu),
  265. sizeof(struct cacheinfo), GFP_KERNEL);
  266. if (per_cpu_cacheinfo(cpu) == NULL)
  267. return -ENOMEM;
  268. /*
  269. * populate_cache_leaves() may completely setup the cache leaves and
  270. * shared_cpu_map or it may leave it partially setup.
  271. */
  272. ret = populate_cache_leaves(cpu);
  273. if (ret)
  274. goto free_ci;
  275. /*
  276. * For systems using DT for cache hierarchy, fw_token
  277. * and shared_cpu_map will be set up here only if they are
  278. * not populated already
  279. */
  280. ret = cache_shared_cpu_map_setup(cpu);
  281. if (ret) {
  282. pr_warn("Unable to detect cache hierarchy for CPU %d\n", cpu);
  283. goto free_ci;
  284. }
  285. return 0;
  286. free_ci:
  287. free_cache_attributes(cpu);
  288. return ret;
  289. }
  290. /* pointer to cpuX/cache device */
  291. static DEFINE_PER_CPU(struct device *, ci_cache_dev);
  292. #define per_cpu_cache_dev(cpu) (per_cpu(ci_cache_dev, cpu))
  293. static cpumask_t cache_dev_map;
  294. /* pointer to array of devices for cpuX/cache/indexY */
  295. static DEFINE_PER_CPU(struct device **, ci_index_dev);
  296. #define per_cpu_index_dev(cpu) (per_cpu(ci_index_dev, cpu))
  297. #define per_cache_index_dev(cpu, idx) ((per_cpu_index_dev(cpu))[idx])
  298. #define show_one(file_name, object) \
  299. static ssize_t file_name##_show(struct device *dev, \
  300. struct device_attribute *attr, char *buf) \
  301. { \
  302. struct cacheinfo *this_leaf = dev_get_drvdata(dev); \
  303. return sprintf(buf, "%u\n", this_leaf->object); \
  304. }
  305. show_one(id, id);
  306. show_one(level, level);
  307. show_one(coherency_line_size, coherency_line_size);
  308. show_one(number_of_sets, number_of_sets);
  309. show_one(physical_line_partition, physical_line_partition);
  310. show_one(ways_of_associativity, ways_of_associativity);
  311. static ssize_t size_show(struct device *dev,
  312. struct device_attribute *attr, char *buf)
  313. {
  314. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  315. return sprintf(buf, "%uK\n", this_leaf->size >> 10);
  316. }
  317. static ssize_t shared_cpumap_show_func(struct device *dev, bool list, char *buf)
  318. {
  319. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  320. const struct cpumask *mask = &this_leaf->shared_cpu_map;
  321. return cpumap_print_to_pagebuf(list, buf, mask);
  322. }
  323. static ssize_t shared_cpu_map_show(struct device *dev,
  324. struct device_attribute *attr, char *buf)
  325. {
  326. return shared_cpumap_show_func(dev, false, buf);
  327. }
  328. static ssize_t shared_cpu_list_show(struct device *dev,
  329. struct device_attribute *attr, char *buf)
  330. {
  331. return shared_cpumap_show_func(dev, true, buf);
  332. }
  333. static ssize_t type_show(struct device *dev,
  334. struct device_attribute *attr, char *buf)
  335. {
  336. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  337. switch (this_leaf->type) {
  338. case CACHE_TYPE_DATA:
  339. return sprintf(buf, "Data\n");
  340. case CACHE_TYPE_INST:
  341. return sprintf(buf, "Instruction\n");
  342. case CACHE_TYPE_UNIFIED:
  343. return sprintf(buf, "Unified\n");
  344. default:
  345. return -EINVAL;
  346. }
  347. }
  348. static ssize_t allocation_policy_show(struct device *dev,
  349. struct device_attribute *attr, char *buf)
  350. {
  351. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  352. unsigned int ci_attr = this_leaf->attributes;
  353. int n = 0;
  354. if ((ci_attr & CACHE_READ_ALLOCATE) && (ci_attr & CACHE_WRITE_ALLOCATE))
  355. n = sprintf(buf, "ReadWriteAllocate\n");
  356. else if (ci_attr & CACHE_READ_ALLOCATE)
  357. n = sprintf(buf, "ReadAllocate\n");
  358. else if (ci_attr & CACHE_WRITE_ALLOCATE)
  359. n = sprintf(buf, "WriteAllocate\n");
  360. return n;
  361. }
  362. static ssize_t write_policy_show(struct device *dev,
  363. struct device_attribute *attr, char *buf)
  364. {
  365. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  366. unsigned int ci_attr = this_leaf->attributes;
  367. int n = 0;
  368. if (ci_attr & CACHE_WRITE_THROUGH)
  369. n = sprintf(buf, "WriteThrough\n");
  370. else if (ci_attr & CACHE_WRITE_BACK)
  371. n = sprintf(buf, "WriteBack\n");
  372. return n;
  373. }
  374. static DEVICE_ATTR_RO(id);
  375. static DEVICE_ATTR_RO(level);
  376. static DEVICE_ATTR_RO(type);
  377. static DEVICE_ATTR_RO(coherency_line_size);
  378. static DEVICE_ATTR_RO(ways_of_associativity);
  379. static DEVICE_ATTR_RO(number_of_sets);
  380. static DEVICE_ATTR_RO(size);
  381. static DEVICE_ATTR_RO(allocation_policy);
  382. static DEVICE_ATTR_RO(write_policy);
  383. static DEVICE_ATTR_RO(shared_cpu_map);
  384. static DEVICE_ATTR_RO(shared_cpu_list);
  385. static DEVICE_ATTR_RO(physical_line_partition);
  386. static struct attribute *cache_default_attrs[] = {
  387. &dev_attr_id.attr,
  388. &dev_attr_type.attr,
  389. &dev_attr_level.attr,
  390. &dev_attr_shared_cpu_map.attr,
  391. &dev_attr_shared_cpu_list.attr,
  392. &dev_attr_coherency_line_size.attr,
  393. &dev_attr_ways_of_associativity.attr,
  394. &dev_attr_number_of_sets.attr,
  395. &dev_attr_size.attr,
  396. &dev_attr_allocation_policy.attr,
  397. &dev_attr_write_policy.attr,
  398. &dev_attr_physical_line_partition.attr,
  399. NULL
  400. };
  401. static umode_t
  402. cache_default_attrs_is_visible(struct kobject *kobj,
  403. struct attribute *attr, int unused)
  404. {
  405. struct device *dev = kobj_to_dev(kobj);
  406. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  407. const struct cpumask *mask = &this_leaf->shared_cpu_map;
  408. umode_t mode = attr->mode;
  409. if ((attr == &dev_attr_id.attr) && (this_leaf->attributes & CACHE_ID))
  410. return mode;
  411. if ((attr == &dev_attr_type.attr) && this_leaf->type)
  412. return mode;
  413. if ((attr == &dev_attr_level.attr) && this_leaf->level)
  414. return mode;
  415. if ((attr == &dev_attr_shared_cpu_map.attr) && !cpumask_empty(mask))
  416. return mode;
  417. if ((attr == &dev_attr_shared_cpu_list.attr) && !cpumask_empty(mask))
  418. return mode;
  419. if ((attr == &dev_attr_coherency_line_size.attr) &&
  420. this_leaf->coherency_line_size)
  421. return mode;
  422. if ((attr == &dev_attr_ways_of_associativity.attr) &&
  423. this_leaf->size) /* allow 0 = full associativity */
  424. return mode;
  425. if ((attr == &dev_attr_number_of_sets.attr) &&
  426. this_leaf->number_of_sets)
  427. return mode;
  428. if ((attr == &dev_attr_size.attr) && this_leaf->size)
  429. return mode;
  430. if ((attr == &dev_attr_write_policy.attr) &&
  431. (this_leaf->attributes & CACHE_WRITE_POLICY_MASK))
  432. return mode;
  433. if ((attr == &dev_attr_allocation_policy.attr) &&
  434. (this_leaf->attributes & CACHE_ALLOCATE_POLICY_MASK))
  435. return mode;
  436. if ((attr == &dev_attr_physical_line_partition.attr) &&
  437. this_leaf->physical_line_partition)
  438. return mode;
  439. return 0;
  440. }
  441. static const struct attribute_group cache_default_group = {
  442. .attrs = cache_default_attrs,
  443. .is_visible = cache_default_attrs_is_visible,
  444. };
  445. static const struct attribute_group *cache_default_groups[] = {
  446. &cache_default_group,
  447. NULL,
  448. };
  449. static const struct attribute_group *cache_private_groups[] = {
  450. &cache_default_group,
  451. NULL, /* Place holder for private group */
  452. NULL,
  453. };
  454. const struct attribute_group *
  455. __weak cache_get_priv_group(struct cacheinfo *this_leaf)
  456. {
  457. return NULL;
  458. }
  459. static const struct attribute_group **
  460. cache_get_attribute_groups(struct cacheinfo *this_leaf)
  461. {
  462. const struct attribute_group *priv_group =
  463. cache_get_priv_group(this_leaf);
  464. if (!priv_group)
  465. return cache_default_groups;
  466. if (!cache_private_groups[1])
  467. cache_private_groups[1] = priv_group;
  468. return cache_private_groups;
  469. }
  470. /* Add/Remove cache interface for CPU device */
  471. static void cpu_cache_sysfs_exit(unsigned int cpu)
  472. {
  473. int i;
  474. struct device *ci_dev;
  475. if (per_cpu_index_dev(cpu)) {
  476. for (i = 0; i < cache_leaves(cpu); i++) {
  477. ci_dev = per_cache_index_dev(cpu, i);
  478. if (!ci_dev)
  479. continue;
  480. device_unregister(ci_dev);
  481. }
  482. kfree(per_cpu_index_dev(cpu));
  483. per_cpu_index_dev(cpu) = NULL;
  484. }
  485. device_unregister(per_cpu_cache_dev(cpu));
  486. per_cpu_cache_dev(cpu) = NULL;
  487. }
  488. static int cpu_cache_sysfs_init(unsigned int cpu)
  489. {
  490. struct device *dev = get_cpu_device(cpu);
  491. if (per_cpu_cacheinfo(cpu) == NULL)
  492. return -ENOENT;
  493. per_cpu_cache_dev(cpu) = cpu_device_create(dev, NULL, NULL, "cache");
  494. if (IS_ERR(per_cpu_cache_dev(cpu)))
  495. return PTR_ERR(per_cpu_cache_dev(cpu));
  496. /* Allocate all required memory */
  497. per_cpu_index_dev(cpu) = kcalloc(cache_leaves(cpu),
  498. sizeof(struct device *), GFP_KERNEL);
  499. if (unlikely(per_cpu_index_dev(cpu) == NULL))
  500. goto err_out;
  501. return 0;
  502. err_out:
  503. cpu_cache_sysfs_exit(cpu);
  504. return -ENOMEM;
  505. }
  506. static int cache_add_dev(unsigned int cpu)
  507. {
  508. unsigned int i;
  509. int rc;
  510. struct device *ci_dev, *parent;
  511. struct cacheinfo *this_leaf;
  512. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  513. const struct attribute_group **cache_groups;
  514. rc = cpu_cache_sysfs_init(cpu);
  515. if (unlikely(rc < 0))
  516. return rc;
  517. parent = per_cpu_cache_dev(cpu);
  518. for (i = 0; i < cache_leaves(cpu); i++) {
  519. this_leaf = this_cpu_ci->info_list + i;
  520. if (this_leaf->disable_sysfs)
  521. continue;
  522. cache_groups = cache_get_attribute_groups(this_leaf);
  523. ci_dev = cpu_device_create(parent, this_leaf, cache_groups,
  524. "index%1u", i);
  525. if (IS_ERR(ci_dev)) {
  526. rc = PTR_ERR(ci_dev);
  527. goto err;
  528. }
  529. per_cache_index_dev(cpu, i) = ci_dev;
  530. }
  531. cpumask_set_cpu(cpu, &cache_dev_map);
  532. return 0;
  533. err:
  534. cpu_cache_sysfs_exit(cpu);
  535. return rc;
  536. }
  537. static int cacheinfo_cpu_online(unsigned int cpu)
  538. {
  539. int rc = detect_cache_attributes(cpu);
  540. if (rc)
  541. return rc;
  542. rc = cache_add_dev(cpu);
  543. if (rc)
  544. free_cache_attributes(cpu);
  545. return rc;
  546. }
  547. static int cacheinfo_cpu_pre_down(unsigned int cpu)
  548. {
  549. if (cpumask_test_and_clear_cpu(cpu, &cache_dev_map))
  550. cpu_cache_sysfs_exit(cpu);
  551. free_cache_attributes(cpu);
  552. return 0;
  553. }
  554. static int __init cacheinfo_sysfs_init(void)
  555. {
  556. return cpuhp_setup_state(CPUHP_AP_BASE_CACHEINFO_ONLINE,
  557. "base/cacheinfo:online",
  558. cacheinfo_cpu_online, cacheinfo_cpu_pre_down);
  559. }
  560. device_initcall(cacheinfo_sysfs_init);