topology.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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) \
  17. static ssize_t name##_show(struct device *dev, \
  18. struct device_attribute *attr, char *buf) \
  19. { \
  20. return sprintf(buf, "%d\n", topology_##name(dev->id)); \
  21. }
  22. #define define_siblings_show_map(name, mask) \
  23. static ssize_t name##_show(struct device *dev, \
  24. struct device_attribute *attr, char *buf) \
  25. { \
  26. return cpumap_print_to_pagebuf(false, buf, topology_##mask(dev->id));\
  27. }
  28. #define define_siblings_show_list(name, mask) \
  29. static ssize_t name##_list_show(struct device *dev, \
  30. struct device_attribute *attr, \
  31. char *buf) \
  32. { \
  33. return cpumap_print_to_pagebuf(true, buf, topology_##mask(dev->id));\
  34. }
  35. #define define_siblings_show_func(name, mask) \
  36. define_siblings_show_map(name, mask); \
  37. define_siblings_show_list(name, mask)
  38. define_id_show_func(physical_package_id);
  39. static DEVICE_ATTR_RO(physical_package_id);
  40. define_id_show_func(core_id);
  41. static DEVICE_ATTR_RO(core_id);
  42. define_siblings_show_func(thread_siblings, sibling_cpumask);
  43. static DEVICE_ATTR_RO(thread_siblings);
  44. static DEVICE_ATTR_RO(thread_siblings_list);
  45. define_siblings_show_func(core_siblings, core_cpumask);
  46. static DEVICE_ATTR_RO(core_siblings);
  47. static DEVICE_ATTR_RO(core_siblings_list);
  48. #ifdef CONFIG_SCHED_BOOK
  49. define_id_show_func(book_id);
  50. static DEVICE_ATTR_RO(book_id);
  51. define_siblings_show_func(book_siblings, book_cpumask);
  52. static DEVICE_ATTR_RO(book_siblings);
  53. static DEVICE_ATTR_RO(book_siblings_list);
  54. #endif
  55. #ifdef CONFIG_SCHED_DRAWER
  56. define_id_show_func(drawer_id);
  57. static DEVICE_ATTR_RO(drawer_id);
  58. define_siblings_show_func(drawer_siblings, drawer_cpumask);
  59. static DEVICE_ATTR_RO(drawer_siblings);
  60. static DEVICE_ATTR_RO(drawer_siblings_list);
  61. #endif
  62. static struct attribute *default_attrs[] = {
  63. &dev_attr_physical_package_id.attr,
  64. &dev_attr_core_id.attr,
  65. &dev_attr_thread_siblings.attr,
  66. &dev_attr_thread_siblings_list.attr,
  67. &dev_attr_core_siblings.attr,
  68. &dev_attr_core_siblings_list.attr,
  69. #ifdef CONFIG_SCHED_BOOK
  70. &dev_attr_book_id.attr,
  71. &dev_attr_book_siblings.attr,
  72. &dev_attr_book_siblings_list.attr,
  73. #endif
  74. #ifdef CONFIG_SCHED_DRAWER
  75. &dev_attr_drawer_id.attr,
  76. &dev_attr_drawer_siblings.attr,
  77. &dev_attr_drawer_siblings_list.attr,
  78. #endif
  79. NULL
  80. };
  81. static const struct attribute_group topology_attr_group = {
  82. .attrs = default_attrs,
  83. .name = "topology"
  84. };
  85. /* Add/Remove cpu_topology interface for CPU device */
  86. static int topology_add_dev(unsigned int cpu)
  87. {
  88. struct device *dev = get_cpu_device(cpu);
  89. return sysfs_create_group(&dev->kobj, &topology_attr_group);
  90. }
  91. static int topology_remove_dev(unsigned int cpu)
  92. {
  93. struct device *dev = get_cpu_device(cpu);
  94. sysfs_remove_group(&dev->kobj, &topology_attr_group);
  95. return 0;
  96. }
  97. static int topology_sysfs_init(void)
  98. {
  99. return cpuhp_setup_state(CPUHP_TOPOLOGY_PREPARE,
  100. "base/topology:prepare", topology_add_dev,
  101. topology_remove_dev);
  102. }
  103. device_initcall(topology_sysfs_init);