topology.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * driver/base/topology.c - Populate sysfs with cpu topology information
  4. *
  5. * Written by: Zhang Yanmin, Intel Corporation
  6. *
  7. * Copyright (C) 2006, Intel Corp.
  8. *
  9. * All rights reserved.
  10. */
  11. #include <linux/mm.h>
  12. #include <linux/cpu.h>
  13. #include <linux/module.h>
  14. #include <linux/hardirq.h>
  15. #include <linux/topology.h>
  16. #define define_id_show_func(name, fmt) \
  17. static ssize_t name##_show(struct device *dev, \
  18. struct device_attribute *attr, char *buf) \
  19. { \
  20. return sysfs_emit(buf, fmt "\n", topology_##name(dev->id)); \
  21. }
  22. #define define_siblings_read_func(name, mask) \
  23. static ssize_t name##_read(struct file *file, struct kobject *kobj, \
  24. struct bin_attribute *attr, char *buf, \
  25. loff_t off, size_t count) \
  26. { \
  27. struct device *dev = kobj_to_dev(kobj); \
  28. \
  29. return cpumap_print_bitmask_to_buf(buf, topology_##mask(dev->id), \
  30. off, count); \
  31. } \
  32. \
  33. static ssize_t name##_list_read(struct file *file, struct kobject *kobj, \
  34. struct bin_attribute *attr, char *buf, \
  35. loff_t off, size_t count) \
  36. { \
  37. struct device *dev = kobj_to_dev(kobj); \
  38. \
  39. return cpumap_print_list_to_buf(buf, topology_##mask(dev->id), \
  40. off, count); \
  41. }
  42. define_id_show_func(physical_package_id, "%d");
  43. static DEVICE_ATTR_RO(physical_package_id);
  44. #ifdef TOPOLOGY_DIE_SYSFS
  45. define_id_show_func(die_id, "%d");
  46. static DEVICE_ATTR_RO(die_id);
  47. #endif
  48. #ifdef TOPOLOGY_CLUSTER_SYSFS
  49. define_id_show_func(cluster_id, "%d");
  50. static DEVICE_ATTR_RO(cluster_id);
  51. #endif
  52. define_id_show_func(core_id, "%d");
  53. static DEVICE_ATTR_RO(core_id);
  54. define_id_show_func(ppin, "0x%llx");
  55. static DEVICE_ATTR_ADMIN_RO(ppin);
  56. define_siblings_read_func(thread_siblings, sibling_cpumask);
  57. static BIN_ATTR_RO(thread_siblings, CPUMAP_FILE_MAX_BYTES);
  58. static BIN_ATTR_RO(thread_siblings_list, CPULIST_FILE_MAX_BYTES);
  59. define_siblings_read_func(core_cpus, sibling_cpumask);
  60. static BIN_ATTR_RO(core_cpus, CPUMAP_FILE_MAX_BYTES);
  61. static BIN_ATTR_RO(core_cpus_list, CPULIST_FILE_MAX_BYTES);
  62. define_siblings_read_func(core_siblings, core_cpumask);
  63. static BIN_ATTR_RO(core_siblings, CPUMAP_FILE_MAX_BYTES);
  64. static BIN_ATTR_RO(core_siblings_list, CPULIST_FILE_MAX_BYTES);
  65. #ifdef TOPOLOGY_CLUSTER_SYSFS
  66. define_siblings_read_func(cluster_cpus, cluster_cpumask);
  67. static BIN_ATTR_RO(cluster_cpus, CPUMAP_FILE_MAX_BYTES);
  68. static BIN_ATTR_RO(cluster_cpus_list, CPULIST_FILE_MAX_BYTES);
  69. #endif
  70. #ifdef TOPOLOGY_DIE_SYSFS
  71. define_siblings_read_func(die_cpus, die_cpumask);
  72. static BIN_ATTR_RO(die_cpus, CPUMAP_FILE_MAX_BYTES);
  73. static BIN_ATTR_RO(die_cpus_list, CPULIST_FILE_MAX_BYTES);
  74. #endif
  75. define_siblings_read_func(package_cpus, core_cpumask);
  76. static BIN_ATTR_RO(package_cpus, CPUMAP_FILE_MAX_BYTES);
  77. static BIN_ATTR_RO(package_cpus_list, CPULIST_FILE_MAX_BYTES);
  78. #ifdef TOPOLOGY_BOOK_SYSFS
  79. define_id_show_func(book_id, "%d");
  80. static DEVICE_ATTR_RO(book_id);
  81. define_siblings_read_func(book_siblings, book_cpumask);
  82. static BIN_ATTR_RO(book_siblings, CPUMAP_FILE_MAX_BYTES);
  83. static BIN_ATTR_RO(book_siblings_list, CPULIST_FILE_MAX_BYTES);
  84. #endif
  85. #ifdef TOPOLOGY_DRAWER_SYSFS
  86. define_id_show_func(drawer_id, "%d");
  87. static DEVICE_ATTR_RO(drawer_id);
  88. define_siblings_read_func(drawer_siblings, drawer_cpumask);
  89. static BIN_ATTR_RO(drawer_siblings, CPUMAP_FILE_MAX_BYTES);
  90. static BIN_ATTR_RO(drawer_siblings_list, CPULIST_FILE_MAX_BYTES);
  91. #endif
  92. static struct bin_attribute *bin_attrs[] = {
  93. &bin_attr_core_cpus,
  94. &bin_attr_core_cpus_list,
  95. &bin_attr_thread_siblings,
  96. &bin_attr_thread_siblings_list,
  97. &bin_attr_core_siblings,
  98. &bin_attr_core_siblings_list,
  99. #ifdef TOPOLOGY_CLUSTER_SYSFS
  100. &bin_attr_cluster_cpus,
  101. &bin_attr_cluster_cpus_list,
  102. #endif
  103. #ifdef TOPOLOGY_DIE_SYSFS
  104. &bin_attr_die_cpus,
  105. &bin_attr_die_cpus_list,
  106. #endif
  107. &bin_attr_package_cpus,
  108. &bin_attr_package_cpus_list,
  109. #ifdef TOPOLOGY_BOOK_SYSFS
  110. &bin_attr_book_siblings,
  111. &bin_attr_book_siblings_list,
  112. #endif
  113. #ifdef TOPOLOGY_DRAWER_SYSFS
  114. &bin_attr_drawer_siblings,
  115. &bin_attr_drawer_siblings_list,
  116. #endif
  117. NULL
  118. };
  119. static struct attribute *default_attrs[] = {
  120. &dev_attr_physical_package_id.attr,
  121. #ifdef TOPOLOGY_DIE_SYSFS
  122. &dev_attr_die_id.attr,
  123. #endif
  124. #ifdef TOPOLOGY_CLUSTER_SYSFS
  125. &dev_attr_cluster_id.attr,
  126. #endif
  127. &dev_attr_core_id.attr,
  128. #ifdef TOPOLOGY_BOOK_SYSFS
  129. &dev_attr_book_id.attr,
  130. #endif
  131. #ifdef TOPOLOGY_DRAWER_SYSFS
  132. &dev_attr_drawer_id.attr,
  133. #endif
  134. &dev_attr_ppin.attr,
  135. NULL
  136. };
  137. static umode_t topology_is_visible(struct kobject *kobj,
  138. struct attribute *attr, int unused)
  139. {
  140. if (attr == &dev_attr_ppin.attr && !topology_ppin(kobj_to_dev(kobj)->id))
  141. return 0;
  142. return attr->mode;
  143. }
  144. static const struct attribute_group topology_attr_group = {
  145. .attrs = default_attrs,
  146. .bin_attrs = bin_attrs,
  147. .is_visible = topology_is_visible,
  148. .name = "topology"
  149. };
  150. /* Add/Remove cpu_topology interface for CPU device */
  151. static int topology_add_dev(unsigned int cpu)
  152. {
  153. struct device *dev = get_cpu_device(cpu);
  154. return sysfs_create_group(&dev->kobj, &topology_attr_group);
  155. }
  156. static int topology_remove_dev(unsigned int cpu)
  157. {
  158. struct device *dev = get_cpu_device(cpu);
  159. sysfs_remove_group(&dev->kobj, &topology_attr_group);
  160. return 0;
  161. }
  162. static int __init topology_sysfs_init(void)
  163. {
  164. return cpuhp_setup_state(CPUHP_TOPOLOGY_PREPARE,
  165. "base/topology:prepare", topology_add_dev,
  166. topology_remove_dev);
  167. }
  168. device_initcall(topology_sysfs_init);