cacheinfo.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. unsigned int coherency_max_size;
  188. static int cache_shared_cpu_map_setup(unsigned int cpu)
  189. {
  190. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  191. struct cacheinfo *this_leaf, *sib_leaf;
  192. unsigned int index;
  193. int ret = 0;
  194. if (this_cpu_ci->cpu_map_populated)
  195. return 0;
  196. if (of_have_populated_dt())
  197. ret = cache_setup_of_node(cpu);
  198. else if (!acpi_disabled)
  199. ret = cache_setup_acpi(cpu);
  200. if (ret)
  201. return ret;
  202. for (index = 0; index < cache_leaves(cpu); index++) {
  203. unsigned int i;
  204. this_leaf = this_cpu_ci->info_list + index;
  205. /* skip if shared_cpu_map is already populated */
  206. if (!cpumask_empty(&this_leaf->shared_cpu_map))
  207. continue;
  208. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  209. for_each_online_cpu(i) {
  210. struct cpu_cacheinfo *sib_cpu_ci = get_cpu_cacheinfo(i);
  211. if (i == cpu || !sib_cpu_ci->info_list)
  212. continue;/* skip if itself or no cacheinfo */
  213. sib_leaf = sib_cpu_ci->info_list + index;
  214. if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
  215. cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map);
  216. cpumask_set_cpu(i, &this_leaf->shared_cpu_map);
  217. }
  218. }
  219. /* record the maximum cache line size */
  220. if (this_leaf->coherency_line_size > coherency_max_size)
  221. coherency_max_size = this_leaf->coherency_line_size;
  222. }
  223. return 0;
  224. }
  225. static void cache_shared_cpu_map_remove(unsigned int cpu)
  226. {
  227. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  228. struct cacheinfo *this_leaf, *sib_leaf;
  229. unsigned int sibling, index;
  230. for (index = 0; index < cache_leaves(cpu); index++) {
  231. this_leaf = this_cpu_ci->info_list + index;
  232. for_each_cpu(sibling, &this_leaf->shared_cpu_map) {
  233. struct cpu_cacheinfo *sib_cpu_ci;
  234. if (sibling == cpu) /* skip itself */
  235. continue;
  236. sib_cpu_ci = get_cpu_cacheinfo(sibling);
  237. if (!sib_cpu_ci->info_list)
  238. continue;
  239. sib_leaf = sib_cpu_ci->info_list + index;
  240. cpumask_clear_cpu(cpu, &sib_leaf->shared_cpu_map);
  241. cpumask_clear_cpu(sibling, &this_leaf->shared_cpu_map);
  242. }
  243. if (of_have_populated_dt())
  244. of_node_put(this_leaf->fw_token);
  245. }
  246. }
  247. static void free_cache_attributes(unsigned int cpu)
  248. {
  249. if (!per_cpu_cacheinfo(cpu))
  250. return;
  251. cache_shared_cpu_map_remove(cpu);
  252. kfree(per_cpu_cacheinfo(cpu));
  253. per_cpu_cacheinfo(cpu) = NULL;
  254. }
  255. int __weak init_cache_level(unsigned int cpu)
  256. {
  257. return -ENOENT;
  258. }
  259. int __weak populate_cache_leaves(unsigned int cpu)
  260. {
  261. return -ENOENT;
  262. }
  263. static int detect_cache_attributes(unsigned int cpu)
  264. {
  265. int ret;
  266. if (init_cache_level(cpu) || !cache_leaves(cpu))
  267. return -ENOENT;
  268. per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu),
  269. sizeof(struct cacheinfo), GFP_KERNEL);
  270. if (per_cpu_cacheinfo(cpu) == NULL)
  271. return -ENOMEM;
  272. /*
  273. * populate_cache_leaves() may completely setup the cache leaves and
  274. * shared_cpu_map or it may leave it partially setup.
  275. */
  276. ret = populate_cache_leaves(cpu);
  277. if (ret)
  278. goto free_ci;
  279. /*
  280. * For systems using DT for cache hierarchy, fw_token
  281. * and shared_cpu_map will be set up here only if they are
  282. * not populated already
  283. */
  284. ret = cache_shared_cpu_map_setup(cpu);
  285. if (ret) {
  286. pr_warn("Unable to detect cache hierarchy for CPU %d\n", cpu);
  287. goto free_ci;
  288. }
  289. return 0;
  290. free_ci:
  291. free_cache_attributes(cpu);
  292. return ret;
  293. }
  294. /* pointer to cpuX/cache device */
  295. static DEFINE_PER_CPU(struct device *, ci_cache_dev);
  296. #define per_cpu_cache_dev(cpu) (per_cpu(ci_cache_dev, cpu))
  297. static cpumask_t cache_dev_map;
  298. /* pointer to array of devices for cpuX/cache/indexY */
  299. static DEFINE_PER_CPU(struct device **, ci_index_dev);
  300. #define per_cpu_index_dev(cpu) (per_cpu(ci_index_dev, cpu))
  301. #define per_cache_index_dev(cpu, idx) ((per_cpu_index_dev(cpu))[idx])
  302. #define show_one(file_name, object) \
  303. static ssize_t file_name##_show(struct device *dev, \
  304. struct device_attribute *attr, char *buf) \
  305. { \
  306. struct cacheinfo *this_leaf = dev_get_drvdata(dev); \
  307. return sysfs_emit(buf, "%u\n", this_leaf->object); \
  308. }
  309. show_one(id, id);
  310. show_one(level, level);
  311. show_one(coherency_line_size, coherency_line_size);
  312. show_one(number_of_sets, number_of_sets);
  313. show_one(physical_line_partition, physical_line_partition);
  314. show_one(ways_of_associativity, ways_of_associativity);
  315. static ssize_t size_show(struct device *dev,
  316. struct device_attribute *attr, char *buf)
  317. {
  318. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  319. return sysfs_emit(buf, "%uK\n", this_leaf->size >> 10);
  320. }
  321. static ssize_t shared_cpu_map_show(struct device *dev,
  322. struct device_attribute *attr, char *buf)
  323. {
  324. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  325. const struct cpumask *mask = &this_leaf->shared_cpu_map;
  326. return sysfs_emit(buf, "%*pb\n", nr_cpu_ids, mask);
  327. }
  328. static ssize_t shared_cpu_list_show(struct device *dev,
  329. struct device_attribute *attr, char *buf)
  330. {
  331. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  332. const struct cpumask *mask = &this_leaf->shared_cpu_map;
  333. return sysfs_emit(buf, "%*pbl\n", nr_cpu_ids, mask);
  334. }
  335. static ssize_t type_show(struct device *dev,
  336. struct device_attribute *attr, char *buf)
  337. {
  338. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  339. const char *output;
  340. switch (this_leaf->type) {
  341. case CACHE_TYPE_DATA:
  342. output = "Data";
  343. break;
  344. case CACHE_TYPE_INST:
  345. output = "Instruction";
  346. break;
  347. case CACHE_TYPE_UNIFIED:
  348. output = "Unified";
  349. break;
  350. default:
  351. return -EINVAL;
  352. }
  353. return sysfs_emit(buf, "%s\n", output);
  354. }
  355. static ssize_t allocation_policy_show(struct device *dev,
  356. struct device_attribute *attr, char *buf)
  357. {
  358. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  359. unsigned int ci_attr = this_leaf->attributes;
  360. const char *output;
  361. if ((ci_attr & CACHE_READ_ALLOCATE) && (ci_attr & CACHE_WRITE_ALLOCATE))
  362. output = "ReadWriteAllocate";
  363. else if (ci_attr & CACHE_READ_ALLOCATE)
  364. output = "ReadAllocate";
  365. else if (ci_attr & CACHE_WRITE_ALLOCATE)
  366. output = "WriteAllocate";
  367. else
  368. return 0;
  369. return sysfs_emit(buf, "%s\n", output);
  370. }
  371. static ssize_t write_policy_show(struct device *dev,
  372. struct device_attribute *attr, char *buf)
  373. {
  374. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  375. unsigned int ci_attr = this_leaf->attributes;
  376. int n = 0;
  377. if (ci_attr & CACHE_WRITE_THROUGH)
  378. n = sysfs_emit(buf, "WriteThrough\n");
  379. else if (ci_attr & CACHE_WRITE_BACK)
  380. n = sysfs_emit(buf, "WriteBack\n");
  381. return n;
  382. }
  383. static DEVICE_ATTR_RO(id);
  384. static DEVICE_ATTR_RO(level);
  385. static DEVICE_ATTR_RO(type);
  386. static DEVICE_ATTR_RO(coherency_line_size);
  387. static DEVICE_ATTR_RO(ways_of_associativity);
  388. static DEVICE_ATTR_RO(number_of_sets);
  389. static DEVICE_ATTR_RO(size);
  390. static DEVICE_ATTR_RO(allocation_policy);
  391. static DEVICE_ATTR_RO(write_policy);
  392. static DEVICE_ATTR_RO(shared_cpu_map);
  393. static DEVICE_ATTR_RO(shared_cpu_list);
  394. static DEVICE_ATTR_RO(physical_line_partition);
  395. static struct attribute *cache_default_attrs[] = {
  396. &dev_attr_id.attr,
  397. &dev_attr_type.attr,
  398. &dev_attr_level.attr,
  399. &dev_attr_shared_cpu_map.attr,
  400. &dev_attr_shared_cpu_list.attr,
  401. &dev_attr_coherency_line_size.attr,
  402. &dev_attr_ways_of_associativity.attr,
  403. &dev_attr_number_of_sets.attr,
  404. &dev_attr_size.attr,
  405. &dev_attr_allocation_policy.attr,
  406. &dev_attr_write_policy.attr,
  407. &dev_attr_physical_line_partition.attr,
  408. NULL
  409. };
  410. static umode_t
  411. cache_default_attrs_is_visible(struct kobject *kobj,
  412. struct attribute *attr, int unused)
  413. {
  414. struct device *dev = kobj_to_dev(kobj);
  415. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  416. const struct cpumask *mask = &this_leaf->shared_cpu_map;
  417. umode_t mode = attr->mode;
  418. if ((attr == &dev_attr_id.attr) && (this_leaf->attributes & CACHE_ID))
  419. return mode;
  420. if ((attr == &dev_attr_type.attr) && this_leaf->type)
  421. return mode;
  422. if ((attr == &dev_attr_level.attr) && this_leaf->level)
  423. return mode;
  424. if ((attr == &dev_attr_shared_cpu_map.attr) && !cpumask_empty(mask))
  425. return mode;
  426. if ((attr == &dev_attr_shared_cpu_list.attr) && !cpumask_empty(mask))
  427. return mode;
  428. if ((attr == &dev_attr_coherency_line_size.attr) &&
  429. this_leaf->coherency_line_size)
  430. return mode;
  431. if ((attr == &dev_attr_ways_of_associativity.attr) &&
  432. this_leaf->size) /* allow 0 = full associativity */
  433. return mode;
  434. if ((attr == &dev_attr_number_of_sets.attr) &&
  435. this_leaf->number_of_sets)
  436. return mode;
  437. if ((attr == &dev_attr_size.attr) && this_leaf->size)
  438. return mode;
  439. if ((attr == &dev_attr_write_policy.attr) &&
  440. (this_leaf->attributes & CACHE_WRITE_POLICY_MASK))
  441. return mode;
  442. if ((attr == &dev_attr_allocation_policy.attr) &&
  443. (this_leaf->attributes & CACHE_ALLOCATE_POLICY_MASK))
  444. return mode;
  445. if ((attr == &dev_attr_physical_line_partition.attr) &&
  446. this_leaf->physical_line_partition)
  447. return mode;
  448. return 0;
  449. }
  450. static const struct attribute_group cache_default_group = {
  451. .attrs = cache_default_attrs,
  452. .is_visible = cache_default_attrs_is_visible,
  453. };
  454. static const struct attribute_group *cache_default_groups[] = {
  455. &cache_default_group,
  456. NULL,
  457. };
  458. static const struct attribute_group *cache_private_groups[] = {
  459. &cache_default_group,
  460. NULL, /* Place holder for private group */
  461. NULL,
  462. };
  463. const struct attribute_group *
  464. __weak cache_get_priv_group(struct cacheinfo *this_leaf)
  465. {
  466. return NULL;
  467. }
  468. static const struct attribute_group **
  469. cache_get_attribute_groups(struct cacheinfo *this_leaf)
  470. {
  471. const struct attribute_group *priv_group =
  472. cache_get_priv_group(this_leaf);
  473. if (!priv_group)
  474. return cache_default_groups;
  475. if (!cache_private_groups[1])
  476. cache_private_groups[1] = priv_group;
  477. return cache_private_groups;
  478. }
  479. /* Add/Remove cache interface for CPU device */
  480. static void cpu_cache_sysfs_exit(unsigned int cpu)
  481. {
  482. int i;
  483. struct device *ci_dev;
  484. if (per_cpu_index_dev(cpu)) {
  485. for (i = 0; i < cache_leaves(cpu); i++) {
  486. ci_dev = per_cache_index_dev(cpu, i);
  487. if (!ci_dev)
  488. continue;
  489. device_unregister(ci_dev);
  490. }
  491. kfree(per_cpu_index_dev(cpu));
  492. per_cpu_index_dev(cpu) = NULL;
  493. }
  494. device_unregister(per_cpu_cache_dev(cpu));
  495. per_cpu_cache_dev(cpu) = NULL;
  496. }
  497. static int cpu_cache_sysfs_init(unsigned int cpu)
  498. {
  499. struct device *dev = get_cpu_device(cpu);
  500. if (per_cpu_cacheinfo(cpu) == NULL)
  501. return -ENOENT;
  502. per_cpu_cache_dev(cpu) = cpu_device_create(dev, NULL, NULL, "cache");
  503. if (IS_ERR(per_cpu_cache_dev(cpu)))
  504. return PTR_ERR(per_cpu_cache_dev(cpu));
  505. /* Allocate all required memory */
  506. per_cpu_index_dev(cpu) = kcalloc(cache_leaves(cpu),
  507. sizeof(struct device *), GFP_KERNEL);
  508. if (unlikely(per_cpu_index_dev(cpu) == NULL))
  509. goto err_out;
  510. return 0;
  511. err_out:
  512. cpu_cache_sysfs_exit(cpu);
  513. return -ENOMEM;
  514. }
  515. static int cache_add_dev(unsigned int cpu)
  516. {
  517. unsigned int i;
  518. int rc;
  519. struct device *ci_dev, *parent;
  520. struct cacheinfo *this_leaf;
  521. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  522. const struct attribute_group **cache_groups;
  523. rc = cpu_cache_sysfs_init(cpu);
  524. if (unlikely(rc < 0))
  525. return rc;
  526. parent = per_cpu_cache_dev(cpu);
  527. for (i = 0; i < cache_leaves(cpu); i++) {
  528. this_leaf = this_cpu_ci->info_list + i;
  529. if (this_leaf->disable_sysfs)
  530. continue;
  531. if (this_leaf->type == CACHE_TYPE_NOCACHE)
  532. break;
  533. cache_groups = cache_get_attribute_groups(this_leaf);
  534. ci_dev = cpu_device_create(parent, this_leaf, cache_groups,
  535. "index%1u", i);
  536. if (IS_ERR(ci_dev)) {
  537. rc = PTR_ERR(ci_dev);
  538. goto err;
  539. }
  540. per_cache_index_dev(cpu, i) = ci_dev;
  541. }
  542. cpumask_set_cpu(cpu, &cache_dev_map);
  543. return 0;
  544. err:
  545. cpu_cache_sysfs_exit(cpu);
  546. return rc;
  547. }
  548. static int cacheinfo_cpu_online(unsigned int cpu)
  549. {
  550. int rc = detect_cache_attributes(cpu);
  551. if (rc)
  552. return rc;
  553. rc = cache_add_dev(cpu);
  554. if (rc)
  555. free_cache_attributes(cpu);
  556. return rc;
  557. }
  558. static int cacheinfo_cpu_pre_down(unsigned int cpu)
  559. {
  560. if (cpumask_test_and_clear_cpu(cpu, &cache_dev_map))
  561. cpu_cache_sysfs_exit(cpu);
  562. free_cache_attributes(cpu);
  563. return 0;
  564. }
  565. static int __init cacheinfo_sysfs_init(void)
  566. {
  567. return cpuhp_setup_state(CPUHP_AP_BASE_CACHEINFO_ONLINE,
  568. "base/cacheinfo:online",
  569. cacheinfo_cpu_online, cacheinfo_cpu_pre_down);
  570. }
  571. device_initcall(cacheinfo_sysfs_init);