topology.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * driver/base/topology.c - Populate sysfs with cpu topology information
  3. *
  4. * Written by: Zhang Yanmin, Intel Corporation
  5. *
  6. * Copyright (C) 2006, Intel Corp.
  7. *
  8. * All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  18. * NON INFRINGEMENT. See the GNU General Public License for more
  19. * details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. */
  26. #include <linux/mm.h>
  27. #include <linux/cpu.h>
  28. #include <linux/module.h>
  29. #include <linux/hardirq.h>
  30. #include <linux/topology.h>
  31. #define define_id_show_func(name) \
  32. static ssize_t name##_show(struct device *dev, \
  33. struct device_attribute *attr, char *buf) \
  34. { \
  35. return sprintf(buf, "%d\n", topology_##name(dev->id)); \
  36. }
  37. #define define_siblings_show_map(name, mask) \
  38. static ssize_t name##_show(struct device *dev, \
  39. struct device_attribute *attr, char *buf) \
  40. { \
  41. return cpumap_print_to_pagebuf(false, buf, topology_##mask(dev->id));\
  42. }
  43. #define define_siblings_show_list(name, mask) \
  44. static ssize_t name##_list_show(struct device *dev, \
  45. struct device_attribute *attr, \
  46. char *buf) \
  47. { \
  48. return cpumap_print_to_pagebuf(true, buf, topology_##mask(dev->id));\
  49. }
  50. #define define_siblings_show_func(name, mask) \
  51. define_siblings_show_map(name, mask); \
  52. define_siblings_show_list(name, mask)
  53. define_id_show_func(physical_package_id);
  54. static DEVICE_ATTR_RO(physical_package_id);
  55. define_id_show_func(core_id);
  56. static DEVICE_ATTR_RO(core_id);
  57. define_siblings_show_func(thread_siblings, sibling_cpumask);
  58. static DEVICE_ATTR_RO(thread_siblings);
  59. static DEVICE_ATTR_RO(thread_siblings_list);
  60. define_siblings_show_func(core_siblings, core_cpumask);
  61. static DEVICE_ATTR_RO(core_siblings);
  62. static DEVICE_ATTR_RO(core_siblings_list);
  63. #ifdef CONFIG_SCHED_BOOK
  64. define_id_show_func(book_id);
  65. static DEVICE_ATTR_RO(book_id);
  66. define_siblings_show_func(book_siblings, book_cpumask);
  67. static DEVICE_ATTR_RO(book_siblings);
  68. static DEVICE_ATTR_RO(book_siblings_list);
  69. #endif
  70. #ifdef CONFIG_SCHED_DRAWER
  71. define_id_show_func(drawer_id);
  72. static DEVICE_ATTR_RO(drawer_id);
  73. define_siblings_show_func(drawer_siblings, drawer_cpumask);
  74. static DEVICE_ATTR_RO(drawer_siblings);
  75. static DEVICE_ATTR_RO(drawer_siblings_list);
  76. #endif
  77. static struct attribute *default_attrs[] = {
  78. &dev_attr_physical_package_id.attr,
  79. &dev_attr_core_id.attr,
  80. &dev_attr_thread_siblings.attr,
  81. &dev_attr_thread_siblings_list.attr,
  82. &dev_attr_core_siblings.attr,
  83. &dev_attr_core_siblings_list.attr,
  84. #ifdef CONFIG_SCHED_BOOK
  85. &dev_attr_book_id.attr,
  86. &dev_attr_book_siblings.attr,
  87. &dev_attr_book_siblings_list.attr,
  88. #endif
  89. #ifdef CONFIG_SCHED_DRAWER
  90. &dev_attr_drawer_id.attr,
  91. &dev_attr_drawer_siblings.attr,
  92. &dev_attr_drawer_siblings_list.attr,
  93. #endif
  94. NULL
  95. };
  96. static struct attribute_group topology_attr_group = {
  97. .attrs = default_attrs,
  98. .name = "topology"
  99. };
  100. /* Add/Remove cpu_topology interface for CPU device */
  101. static int topology_add_dev(unsigned int cpu)
  102. {
  103. struct device *dev = get_cpu_device(cpu);
  104. return sysfs_create_group(&dev->kobj, &topology_attr_group);
  105. }
  106. static void topology_remove_dev(unsigned int cpu)
  107. {
  108. struct device *dev = get_cpu_device(cpu);
  109. sysfs_remove_group(&dev->kobj, &topology_attr_group);
  110. }
  111. static int topology_cpu_callback(struct notifier_block *nfb,
  112. unsigned long action, void *hcpu)
  113. {
  114. unsigned int cpu = (unsigned long)hcpu;
  115. int rc = 0;
  116. switch (action) {
  117. case CPU_UP_PREPARE:
  118. case CPU_UP_PREPARE_FROZEN:
  119. rc = topology_add_dev(cpu);
  120. break;
  121. case CPU_UP_CANCELED:
  122. case CPU_UP_CANCELED_FROZEN:
  123. case CPU_DEAD:
  124. case CPU_DEAD_FROZEN:
  125. topology_remove_dev(cpu);
  126. break;
  127. }
  128. return notifier_from_errno(rc);
  129. }
  130. static int topology_sysfs_init(void)
  131. {
  132. int cpu;
  133. int rc = 0;
  134. cpu_notifier_register_begin();
  135. for_each_online_cpu(cpu) {
  136. rc = topology_add_dev(cpu);
  137. if (rc)
  138. goto out;
  139. }
  140. __hotcpu_notifier(topology_cpu_callback, 0);
  141. out:
  142. cpu_notifier_register_done();
  143. return rc;
  144. }
  145. device_initcall(topology_sysfs_init);