topology.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. static struct attribute *default_attrs[] = {
  71. &dev_attr_physical_package_id.attr,
  72. &dev_attr_core_id.attr,
  73. &dev_attr_thread_siblings.attr,
  74. &dev_attr_thread_siblings_list.attr,
  75. &dev_attr_core_siblings.attr,
  76. &dev_attr_core_siblings_list.attr,
  77. #ifdef CONFIG_SCHED_BOOK
  78. &dev_attr_book_id.attr,
  79. &dev_attr_book_siblings.attr,
  80. &dev_attr_book_siblings_list.attr,
  81. #endif
  82. NULL
  83. };
  84. static struct attribute_group topology_attr_group = {
  85. .attrs = default_attrs,
  86. .name = "topology"
  87. };
  88. /* Add/Remove cpu_topology interface for CPU device */
  89. static int topology_add_dev(unsigned int cpu)
  90. {
  91. struct device *dev = get_cpu_device(cpu);
  92. return sysfs_create_group(&dev->kobj, &topology_attr_group);
  93. }
  94. static void topology_remove_dev(unsigned int cpu)
  95. {
  96. struct device *dev = get_cpu_device(cpu);
  97. sysfs_remove_group(&dev->kobj, &topology_attr_group);
  98. }
  99. static int topology_cpu_callback(struct notifier_block *nfb,
  100. unsigned long action, void *hcpu)
  101. {
  102. unsigned int cpu = (unsigned long)hcpu;
  103. int rc = 0;
  104. switch (action) {
  105. case CPU_UP_PREPARE:
  106. case CPU_UP_PREPARE_FROZEN:
  107. rc = topology_add_dev(cpu);
  108. break;
  109. case CPU_UP_CANCELED:
  110. case CPU_UP_CANCELED_FROZEN:
  111. case CPU_DEAD:
  112. case CPU_DEAD_FROZEN:
  113. topology_remove_dev(cpu);
  114. break;
  115. }
  116. return notifier_from_errno(rc);
  117. }
  118. static int topology_sysfs_init(void)
  119. {
  120. int cpu;
  121. int rc = 0;
  122. cpu_notifier_register_begin();
  123. for_each_online_cpu(cpu) {
  124. rc = topology_add_dev(cpu);
  125. if (rc)
  126. goto out;
  127. }
  128. __hotcpu_notifier(topology_cpu_callback, 0);
  129. out:
  130. cpu_notifier_register_done();
  131. return rc;
  132. }
  133. device_initcall(topology_sysfs_init);