cacheinfo.c 14 KB

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