cacheinfo.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * cacheinfo support - processor cache information via sysfs
  3. *
  4. * Based on arch/x86/kernel/cpu/intel_cacheinfo.c
  5. * Author: Sudeep Holla <sudeep.holla@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/acpi.h>
  20. #include <linux/bitops.h>
  21. #include <linux/cacheinfo.h>
  22. #include <linux/compiler.h>
  23. #include <linux/cpu.h>
  24. #include <linux/device.h>
  25. #include <linux/init.h>
  26. #include <linux/of.h>
  27. #include <linux/sched.h>
  28. #include <linux/slab.h>
  29. #include <linux/smp.h>
  30. #include <linux/sysfs.h>
  31. /* pointer to per cpu cacheinfo */
  32. static DEFINE_PER_CPU(struct cpu_cacheinfo, ci_cpu_cacheinfo);
  33. #define ci_cacheinfo(cpu) (&per_cpu(ci_cpu_cacheinfo, cpu))
  34. #define cache_leaves(cpu) (ci_cacheinfo(cpu)->num_leaves)
  35. #define per_cpu_cacheinfo(cpu) (ci_cacheinfo(cpu)->info_list)
  36. struct cpu_cacheinfo *get_cpu_cacheinfo(unsigned int cpu)
  37. {
  38. return ci_cacheinfo(cpu);
  39. }
  40. #ifdef CONFIG_OF
  41. static int cache_setup_of_node(unsigned int cpu)
  42. {
  43. struct device_node *np;
  44. struct cacheinfo *this_leaf;
  45. struct device *cpu_dev = get_cpu_device(cpu);
  46. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  47. unsigned int index = 0;
  48. /* skip if of_node is already populated */
  49. if (this_cpu_ci->info_list->of_node)
  50. return 0;
  51. if (!cpu_dev) {
  52. pr_err("No cpu device for CPU %d\n", cpu);
  53. return -ENODEV;
  54. }
  55. np = cpu_dev->of_node;
  56. if (!np) {
  57. pr_err("Failed to find cpu%d device node\n", cpu);
  58. return -ENOENT;
  59. }
  60. while (index < cache_leaves(cpu)) {
  61. this_leaf = this_cpu_ci->info_list + index;
  62. if (this_leaf->level != 1)
  63. np = of_find_next_cache_node(np);
  64. else
  65. np = of_node_get(np);/* cpu node itself */
  66. if (!np)
  67. break;
  68. this_leaf->of_node = np;
  69. index++;
  70. }
  71. if (index != cache_leaves(cpu)) /* not all OF nodes populated */
  72. return -ENOENT;
  73. return 0;
  74. }
  75. static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
  76. struct cacheinfo *sib_leaf)
  77. {
  78. return sib_leaf->of_node == this_leaf->of_node;
  79. }
  80. #else
  81. static inline int cache_setup_of_node(unsigned int cpu) { return 0; }
  82. static inline bool cache_leaves_are_shared(struct cacheinfo *this_leaf,
  83. struct cacheinfo *sib_leaf)
  84. {
  85. /*
  86. * For non-DT systems, assume unique level 1 cache, system-wide
  87. * shared caches for all other levels. This will be used only if
  88. * arch specific code has not populated shared_cpu_map
  89. */
  90. return !(this_leaf->level == 1);
  91. }
  92. #endif
  93. static int cache_shared_cpu_map_setup(unsigned int cpu)
  94. {
  95. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  96. struct cacheinfo *this_leaf, *sib_leaf;
  97. unsigned int index;
  98. int ret = 0;
  99. if (this_cpu_ci->cpu_map_populated)
  100. return 0;
  101. if (of_have_populated_dt())
  102. ret = cache_setup_of_node(cpu);
  103. else if (!acpi_disabled)
  104. /* No cache property/hierarchy support yet in ACPI */
  105. ret = -ENOTSUPP;
  106. if (ret)
  107. return ret;
  108. for (index = 0; index < cache_leaves(cpu); index++) {
  109. unsigned int i;
  110. this_leaf = this_cpu_ci->info_list + index;
  111. /* skip if shared_cpu_map is already populated */
  112. if (!cpumask_empty(&this_leaf->shared_cpu_map))
  113. continue;
  114. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  115. for_each_online_cpu(i) {
  116. struct cpu_cacheinfo *sib_cpu_ci = get_cpu_cacheinfo(i);
  117. if (i == cpu || !sib_cpu_ci->info_list)
  118. continue;/* skip if itself or no cacheinfo */
  119. sib_leaf = sib_cpu_ci->info_list + index;
  120. if (cache_leaves_are_shared(this_leaf, sib_leaf)) {
  121. cpumask_set_cpu(cpu, &sib_leaf->shared_cpu_map);
  122. cpumask_set_cpu(i, &this_leaf->shared_cpu_map);
  123. }
  124. }
  125. }
  126. return 0;
  127. }
  128. static void cache_shared_cpu_map_remove(unsigned int cpu)
  129. {
  130. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  131. struct cacheinfo *this_leaf, *sib_leaf;
  132. unsigned int sibling, index;
  133. for (index = 0; index < cache_leaves(cpu); index++) {
  134. this_leaf = this_cpu_ci->info_list + index;
  135. for_each_cpu(sibling, &this_leaf->shared_cpu_map) {
  136. struct cpu_cacheinfo *sib_cpu_ci;
  137. if (sibling == cpu) /* skip itself */
  138. continue;
  139. sib_cpu_ci = get_cpu_cacheinfo(sibling);
  140. if (!sib_cpu_ci->info_list)
  141. continue;
  142. sib_leaf = sib_cpu_ci->info_list + index;
  143. cpumask_clear_cpu(cpu, &sib_leaf->shared_cpu_map);
  144. cpumask_clear_cpu(sibling, &this_leaf->shared_cpu_map);
  145. }
  146. of_node_put(this_leaf->of_node);
  147. }
  148. }
  149. static void free_cache_attributes(unsigned int cpu)
  150. {
  151. if (!per_cpu_cacheinfo(cpu))
  152. return;
  153. cache_shared_cpu_map_remove(cpu);
  154. kfree(per_cpu_cacheinfo(cpu));
  155. per_cpu_cacheinfo(cpu) = NULL;
  156. }
  157. int __weak init_cache_level(unsigned int cpu)
  158. {
  159. return -ENOENT;
  160. }
  161. int __weak populate_cache_leaves(unsigned int cpu)
  162. {
  163. return -ENOENT;
  164. }
  165. static int detect_cache_attributes(unsigned int cpu)
  166. {
  167. int ret;
  168. if (init_cache_level(cpu) || !cache_leaves(cpu))
  169. return -ENOENT;
  170. per_cpu_cacheinfo(cpu) = kcalloc(cache_leaves(cpu),
  171. sizeof(struct cacheinfo), GFP_KERNEL);
  172. if (per_cpu_cacheinfo(cpu) == NULL)
  173. return -ENOMEM;
  174. ret = populate_cache_leaves(cpu);
  175. if (ret)
  176. goto free_ci;
  177. /*
  178. * For systems using DT for cache hierarchy, of_node and shared_cpu_map
  179. * will be set up here only if they are not populated already
  180. */
  181. ret = cache_shared_cpu_map_setup(cpu);
  182. if (ret) {
  183. pr_warn("Unable to detect cache hierarchy for CPU %d\n", cpu);
  184. goto free_ci;
  185. }
  186. return 0;
  187. free_ci:
  188. free_cache_attributes(cpu);
  189. return ret;
  190. }
  191. /* pointer to cpuX/cache device */
  192. static DEFINE_PER_CPU(struct device *, ci_cache_dev);
  193. #define per_cpu_cache_dev(cpu) (per_cpu(ci_cache_dev, cpu))
  194. static cpumask_t cache_dev_map;
  195. /* pointer to array of devices for cpuX/cache/indexY */
  196. static DEFINE_PER_CPU(struct device **, ci_index_dev);
  197. #define per_cpu_index_dev(cpu) (per_cpu(ci_index_dev, cpu))
  198. #define per_cache_index_dev(cpu, idx) ((per_cpu_index_dev(cpu))[idx])
  199. #define show_one(file_name, object) \
  200. static ssize_t file_name##_show(struct device *dev, \
  201. struct device_attribute *attr, char *buf) \
  202. { \
  203. struct cacheinfo *this_leaf = dev_get_drvdata(dev); \
  204. return sprintf(buf, "%u\n", this_leaf->object); \
  205. }
  206. show_one(level, level);
  207. show_one(coherency_line_size, coherency_line_size);
  208. show_one(number_of_sets, number_of_sets);
  209. show_one(physical_line_partition, physical_line_partition);
  210. show_one(ways_of_associativity, ways_of_associativity);
  211. static ssize_t size_show(struct device *dev,
  212. struct device_attribute *attr, char *buf)
  213. {
  214. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  215. return sprintf(buf, "%uK\n", this_leaf->size >> 10);
  216. }
  217. static ssize_t shared_cpumap_show_func(struct device *dev, bool list, char *buf)
  218. {
  219. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  220. const struct cpumask *mask = &this_leaf->shared_cpu_map;
  221. return cpumap_print_to_pagebuf(list, buf, mask);
  222. }
  223. static ssize_t shared_cpu_map_show(struct device *dev,
  224. struct device_attribute *attr, char *buf)
  225. {
  226. return shared_cpumap_show_func(dev, false, buf);
  227. }
  228. static ssize_t shared_cpu_list_show(struct device *dev,
  229. struct device_attribute *attr, char *buf)
  230. {
  231. return shared_cpumap_show_func(dev, true, buf);
  232. }
  233. static ssize_t type_show(struct device *dev,
  234. struct device_attribute *attr, char *buf)
  235. {
  236. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  237. switch (this_leaf->type) {
  238. case CACHE_TYPE_DATA:
  239. return sprintf(buf, "Data\n");
  240. case CACHE_TYPE_INST:
  241. return sprintf(buf, "Instruction\n");
  242. case CACHE_TYPE_UNIFIED:
  243. return sprintf(buf, "Unified\n");
  244. default:
  245. return -EINVAL;
  246. }
  247. }
  248. static ssize_t allocation_policy_show(struct device *dev,
  249. struct device_attribute *attr, char *buf)
  250. {
  251. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  252. unsigned int ci_attr = this_leaf->attributes;
  253. int n = 0;
  254. if ((ci_attr & CACHE_READ_ALLOCATE) && (ci_attr & CACHE_WRITE_ALLOCATE))
  255. n = sprintf(buf, "ReadWriteAllocate\n");
  256. else if (ci_attr & CACHE_READ_ALLOCATE)
  257. n = sprintf(buf, "ReadAllocate\n");
  258. else if (ci_attr & CACHE_WRITE_ALLOCATE)
  259. n = sprintf(buf, "WriteAllocate\n");
  260. return n;
  261. }
  262. static ssize_t write_policy_show(struct device *dev,
  263. struct device_attribute *attr, char *buf)
  264. {
  265. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  266. unsigned int ci_attr = this_leaf->attributes;
  267. int n = 0;
  268. if (ci_attr & CACHE_WRITE_THROUGH)
  269. n = sprintf(buf, "WriteThrough\n");
  270. else if (ci_attr & CACHE_WRITE_BACK)
  271. n = sprintf(buf, "WriteBack\n");
  272. return n;
  273. }
  274. static DEVICE_ATTR_RO(level);
  275. static DEVICE_ATTR_RO(type);
  276. static DEVICE_ATTR_RO(coherency_line_size);
  277. static DEVICE_ATTR_RO(ways_of_associativity);
  278. static DEVICE_ATTR_RO(number_of_sets);
  279. static DEVICE_ATTR_RO(size);
  280. static DEVICE_ATTR_RO(allocation_policy);
  281. static DEVICE_ATTR_RO(write_policy);
  282. static DEVICE_ATTR_RO(shared_cpu_map);
  283. static DEVICE_ATTR_RO(shared_cpu_list);
  284. static DEVICE_ATTR_RO(physical_line_partition);
  285. static struct attribute *cache_default_attrs[] = {
  286. &dev_attr_type.attr,
  287. &dev_attr_level.attr,
  288. &dev_attr_shared_cpu_map.attr,
  289. &dev_attr_shared_cpu_list.attr,
  290. &dev_attr_coherency_line_size.attr,
  291. &dev_attr_ways_of_associativity.attr,
  292. &dev_attr_number_of_sets.attr,
  293. &dev_attr_size.attr,
  294. &dev_attr_allocation_policy.attr,
  295. &dev_attr_write_policy.attr,
  296. &dev_attr_physical_line_partition.attr,
  297. NULL
  298. };
  299. static umode_t
  300. cache_default_attrs_is_visible(struct kobject *kobj,
  301. struct attribute *attr, int unused)
  302. {
  303. struct device *dev = kobj_to_dev(kobj);
  304. struct cacheinfo *this_leaf = dev_get_drvdata(dev);
  305. const struct cpumask *mask = &this_leaf->shared_cpu_map;
  306. umode_t mode = attr->mode;
  307. if ((attr == &dev_attr_type.attr) && this_leaf->type)
  308. return mode;
  309. if ((attr == &dev_attr_level.attr) && this_leaf->level)
  310. return mode;
  311. if ((attr == &dev_attr_shared_cpu_map.attr) && !cpumask_empty(mask))
  312. return mode;
  313. if ((attr == &dev_attr_shared_cpu_list.attr) && !cpumask_empty(mask))
  314. return mode;
  315. if ((attr == &dev_attr_coherency_line_size.attr) &&
  316. this_leaf->coherency_line_size)
  317. return mode;
  318. if ((attr == &dev_attr_ways_of_associativity.attr) &&
  319. this_leaf->size) /* allow 0 = full associativity */
  320. return mode;
  321. if ((attr == &dev_attr_number_of_sets.attr) &&
  322. this_leaf->number_of_sets)
  323. return mode;
  324. if ((attr == &dev_attr_size.attr) && this_leaf->size)
  325. return mode;
  326. if ((attr == &dev_attr_write_policy.attr) &&
  327. (this_leaf->attributes & CACHE_WRITE_POLICY_MASK))
  328. return mode;
  329. if ((attr == &dev_attr_allocation_policy.attr) &&
  330. (this_leaf->attributes & CACHE_ALLOCATE_POLICY_MASK))
  331. return mode;
  332. if ((attr == &dev_attr_physical_line_partition.attr) &&
  333. this_leaf->physical_line_partition)
  334. return mode;
  335. return 0;
  336. }
  337. static const struct attribute_group cache_default_group = {
  338. .attrs = cache_default_attrs,
  339. .is_visible = cache_default_attrs_is_visible,
  340. };
  341. static const struct attribute_group *cache_default_groups[] = {
  342. &cache_default_group,
  343. NULL,
  344. };
  345. static const struct attribute_group *cache_private_groups[] = {
  346. &cache_default_group,
  347. NULL, /* Place holder for private group */
  348. NULL,
  349. };
  350. const struct attribute_group *
  351. __weak cache_get_priv_group(struct cacheinfo *this_leaf)
  352. {
  353. return NULL;
  354. }
  355. static const struct attribute_group **
  356. cache_get_attribute_groups(struct cacheinfo *this_leaf)
  357. {
  358. const struct attribute_group *priv_group =
  359. cache_get_priv_group(this_leaf);
  360. if (!priv_group)
  361. return cache_default_groups;
  362. if (!cache_private_groups[1])
  363. cache_private_groups[1] = priv_group;
  364. return cache_private_groups;
  365. }
  366. /* Add/Remove cache interface for CPU device */
  367. static void cpu_cache_sysfs_exit(unsigned int cpu)
  368. {
  369. int i;
  370. struct device *ci_dev;
  371. if (per_cpu_index_dev(cpu)) {
  372. for (i = 0; i < cache_leaves(cpu); i++) {
  373. ci_dev = per_cache_index_dev(cpu, i);
  374. if (!ci_dev)
  375. continue;
  376. device_unregister(ci_dev);
  377. }
  378. kfree(per_cpu_index_dev(cpu));
  379. per_cpu_index_dev(cpu) = NULL;
  380. }
  381. device_unregister(per_cpu_cache_dev(cpu));
  382. per_cpu_cache_dev(cpu) = NULL;
  383. }
  384. static int cpu_cache_sysfs_init(unsigned int cpu)
  385. {
  386. struct device *dev = get_cpu_device(cpu);
  387. if (per_cpu_cacheinfo(cpu) == NULL)
  388. return -ENOENT;
  389. per_cpu_cache_dev(cpu) = cpu_device_create(dev, NULL, NULL, "cache");
  390. if (IS_ERR(per_cpu_cache_dev(cpu)))
  391. return PTR_ERR(per_cpu_cache_dev(cpu));
  392. /* Allocate all required memory */
  393. per_cpu_index_dev(cpu) = kcalloc(cache_leaves(cpu),
  394. sizeof(struct device *), GFP_KERNEL);
  395. if (unlikely(per_cpu_index_dev(cpu) == NULL))
  396. goto err_out;
  397. return 0;
  398. err_out:
  399. cpu_cache_sysfs_exit(cpu);
  400. return -ENOMEM;
  401. }
  402. static int cache_add_dev(unsigned int cpu)
  403. {
  404. unsigned int i;
  405. int rc;
  406. struct device *ci_dev, *parent;
  407. struct cacheinfo *this_leaf;
  408. struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
  409. const struct attribute_group **cache_groups;
  410. rc = cpu_cache_sysfs_init(cpu);
  411. if (unlikely(rc < 0))
  412. return rc;
  413. parent = per_cpu_cache_dev(cpu);
  414. for (i = 0; i < cache_leaves(cpu); i++) {
  415. this_leaf = this_cpu_ci->info_list + i;
  416. if (this_leaf->disable_sysfs)
  417. continue;
  418. cache_groups = cache_get_attribute_groups(this_leaf);
  419. ci_dev = cpu_device_create(parent, this_leaf, cache_groups,
  420. "index%1u", i);
  421. if (IS_ERR(ci_dev)) {
  422. rc = PTR_ERR(ci_dev);
  423. goto err;
  424. }
  425. per_cache_index_dev(cpu, i) = ci_dev;
  426. }
  427. cpumask_set_cpu(cpu, &cache_dev_map);
  428. return 0;
  429. err:
  430. cpu_cache_sysfs_exit(cpu);
  431. return rc;
  432. }
  433. static void cache_remove_dev(unsigned int cpu)
  434. {
  435. if (!cpumask_test_cpu(cpu, &cache_dev_map))
  436. return;
  437. cpumask_clear_cpu(cpu, &cache_dev_map);
  438. cpu_cache_sysfs_exit(cpu);
  439. }
  440. static int cacheinfo_cpu_callback(struct notifier_block *nfb,
  441. unsigned long action, void *hcpu)
  442. {
  443. unsigned int cpu = (unsigned long)hcpu;
  444. int rc = 0;
  445. switch (action & ~CPU_TASKS_FROZEN) {
  446. case CPU_ONLINE:
  447. rc = detect_cache_attributes(cpu);
  448. if (!rc)
  449. rc = cache_add_dev(cpu);
  450. break;
  451. case CPU_DEAD:
  452. cache_remove_dev(cpu);
  453. free_cache_attributes(cpu);
  454. break;
  455. }
  456. return notifier_from_errno(rc);
  457. }
  458. static int __init cacheinfo_sysfs_init(void)
  459. {
  460. int cpu, rc = 0;
  461. cpu_notifier_register_begin();
  462. for_each_online_cpu(cpu) {
  463. rc = detect_cache_attributes(cpu);
  464. if (rc)
  465. goto out;
  466. rc = cache_add_dev(cpu);
  467. if (rc) {
  468. free_cache_attributes(cpu);
  469. pr_err("error populating cacheinfo..cpu%d\n", cpu);
  470. goto out;
  471. }
  472. }
  473. __hotcpu_notifier(cacheinfo_cpu_callback, 0);
  474. out:
  475. cpu_notifier_register_done();
  476. return rc;
  477. }
  478. device_initcall(cacheinfo_sysfs_init);