topology.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_one_ro_named(_name, _func) \
  32. static DEVICE_ATTR(_name, 0444, _func, NULL)
  33. #define define_one_ro(_name) \
  34. static DEVICE_ATTR(_name, 0444, show_##_name, NULL)
  35. #define define_id_show_func(name) \
  36. static ssize_t show_##name(struct device *dev, \
  37. struct device_attribute *attr, char *buf) \
  38. { \
  39. return sprintf(buf, "%d\n", topology_##name(dev->id)); \
  40. }
  41. #if defined(topology_thread_cpumask) || defined(topology_core_cpumask) || \
  42. defined(topology_book_cpumask)
  43. static ssize_t show_cpumap(int type, const struct cpumask *mask, char *buf)
  44. {
  45. ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf;
  46. int n = 0;
  47. if (len > 1) {
  48. n = type?
  49. cpulist_scnprintf(buf, len-2, mask) :
  50. cpumask_scnprintf(buf, len-2, mask);
  51. buf[n++] = '\n';
  52. buf[n] = '\0';
  53. }
  54. return n;
  55. }
  56. #endif
  57. #define define_siblings_show_map(name) \
  58. static ssize_t show_##name(struct device *dev, \
  59. struct device_attribute *attr, char *buf) \
  60. { \
  61. return show_cpumap(0, topology_##name(dev->id), buf); \
  62. }
  63. #define define_siblings_show_list(name) \
  64. static ssize_t show_##name##_list(struct device *dev, \
  65. struct device_attribute *attr, \
  66. char *buf) \
  67. { \
  68. return show_cpumap(1, topology_##name(dev->id), buf); \
  69. }
  70. #define define_siblings_show_func(name) \
  71. define_siblings_show_map(name); define_siblings_show_list(name)
  72. define_id_show_func(physical_package_id);
  73. define_one_ro(physical_package_id);
  74. define_id_show_func(core_id);
  75. define_one_ro(core_id);
  76. define_siblings_show_func(thread_cpumask);
  77. define_one_ro_named(thread_siblings, show_thread_cpumask);
  78. define_one_ro_named(thread_siblings_list, show_thread_cpumask_list);
  79. define_siblings_show_func(core_cpumask);
  80. define_one_ro_named(core_siblings, show_core_cpumask);
  81. define_one_ro_named(core_siblings_list, show_core_cpumask_list);
  82. #ifdef CONFIG_SCHED_BOOK
  83. define_id_show_func(book_id);
  84. define_one_ro(book_id);
  85. define_siblings_show_func(book_cpumask);
  86. define_one_ro_named(book_siblings, show_book_cpumask);
  87. define_one_ro_named(book_siblings_list, show_book_cpumask_list);
  88. #endif
  89. static struct attribute *default_attrs[] = {
  90. &dev_attr_physical_package_id.attr,
  91. &dev_attr_core_id.attr,
  92. &dev_attr_thread_siblings.attr,
  93. &dev_attr_thread_siblings_list.attr,
  94. &dev_attr_core_siblings.attr,
  95. &dev_attr_core_siblings_list.attr,
  96. #ifdef CONFIG_SCHED_BOOK
  97. &dev_attr_book_id.attr,
  98. &dev_attr_book_siblings.attr,
  99. &dev_attr_book_siblings_list.attr,
  100. #endif
  101. NULL
  102. };
  103. static struct attribute_group topology_attr_group = {
  104. .attrs = default_attrs,
  105. .name = "topology"
  106. };
  107. /* Add/Remove cpu_topology interface for CPU device */
  108. static int topology_add_dev(unsigned int cpu)
  109. {
  110. struct device *dev = get_cpu_device(cpu);
  111. return sysfs_create_group(&dev->kobj, &topology_attr_group);
  112. }
  113. static void topology_remove_dev(unsigned int cpu)
  114. {
  115. struct device *dev = get_cpu_device(cpu);
  116. sysfs_remove_group(&dev->kobj, &topology_attr_group);
  117. }
  118. static int topology_cpu_callback(struct notifier_block *nfb,
  119. unsigned long action, void *hcpu)
  120. {
  121. unsigned int cpu = (unsigned long)hcpu;
  122. int rc = 0;
  123. switch (action) {
  124. case CPU_UP_PREPARE:
  125. case CPU_UP_PREPARE_FROZEN:
  126. rc = topology_add_dev(cpu);
  127. break;
  128. case CPU_UP_CANCELED:
  129. case CPU_UP_CANCELED_FROZEN:
  130. case CPU_DEAD:
  131. case CPU_DEAD_FROZEN:
  132. topology_remove_dev(cpu);
  133. break;
  134. }
  135. return notifier_from_errno(rc);
  136. }
  137. static int topology_sysfs_init(void)
  138. {
  139. int cpu;
  140. int rc = 0;
  141. cpu_notifier_register_begin();
  142. for_each_online_cpu(cpu) {
  143. rc = topology_add_dev(cpu);
  144. if (rc)
  145. goto out;
  146. }
  147. __hotcpu_notifier(topology_cpu_callback, 0);
  148. out:
  149. cpu_notifier_register_done();
  150. return rc;
  151. }
  152. device_initcall(topology_sysfs_init);