cpu.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * drivers/base/cpu.c - basic CPU class support
  3. */
  4. #include <linux/sysdev.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/sched.h>
  8. #include <linux/cpu.h>
  9. #include <linux/topology.h>
  10. #include <linux/device.h>
  11. #include <linux/node.h>
  12. #include "base.h"
  13. struct sysdev_class cpu_sysdev_class = {
  14. .name = "cpu",
  15. };
  16. EXPORT_SYMBOL(cpu_sysdev_class);
  17. static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices);
  18. #ifdef CONFIG_HOTPLUG_CPU
  19. static ssize_t show_online(struct sys_device *dev, struct sysdev_attribute *attr,
  20. char *buf)
  21. {
  22. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  23. return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
  24. }
  25. static ssize_t __ref store_online(struct sys_device *dev, struct sysdev_attribute *attr,
  26. const char *buf, size_t count)
  27. {
  28. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  29. ssize_t ret;
  30. switch (buf[0]) {
  31. case '0':
  32. ret = cpu_down(cpu->sysdev.id);
  33. if (!ret)
  34. kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  35. break;
  36. case '1':
  37. ret = cpu_up(cpu->sysdev.id);
  38. if (!ret)
  39. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  40. break;
  41. default:
  42. ret = -EINVAL;
  43. }
  44. if (ret >= 0)
  45. ret = count;
  46. return ret;
  47. }
  48. static SYSDEV_ATTR(online, 0644, show_online, store_online);
  49. static void __cpuinit register_cpu_control(struct cpu *cpu)
  50. {
  51. sysdev_create_file(&cpu->sysdev, &attr_online);
  52. }
  53. void unregister_cpu(struct cpu *cpu)
  54. {
  55. int logical_cpu = cpu->sysdev.id;
  56. unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
  57. sysdev_remove_file(&cpu->sysdev, &attr_online);
  58. sysdev_unregister(&cpu->sysdev);
  59. per_cpu(cpu_sys_devices, logical_cpu) = NULL;
  60. return;
  61. }
  62. #else /* ... !CONFIG_HOTPLUG_CPU */
  63. static inline void register_cpu_control(struct cpu *cpu)
  64. {
  65. }
  66. #endif /* CONFIG_HOTPLUG_CPU */
  67. #ifdef CONFIG_KEXEC
  68. #include <linux/kexec.h>
  69. static ssize_t show_crash_notes(struct sys_device *dev, struct sysdev_attribute *attr,
  70. char *buf)
  71. {
  72. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  73. ssize_t rc;
  74. unsigned long long addr;
  75. int cpunum;
  76. cpunum = cpu->sysdev.id;
  77. /*
  78. * Might be reading other cpu's data based on which cpu read thread
  79. * has been scheduled. But cpu data (memory) is allocated once during
  80. * boot up and this data does not change there after. Hence this
  81. * operation should be safe. No locking required.
  82. */
  83. addr = __pa(per_cpu_ptr(crash_notes, cpunum));
  84. rc = sprintf(buf, "%Lx\n", addr);
  85. return rc;
  86. }
  87. static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
  88. #endif
  89. /*
  90. * Print cpu online, possible, present, and system maps
  91. */
  92. static ssize_t print_cpus_map(char *buf, cpumask_t *map)
  93. {
  94. int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *map);
  95. buf[n++] = '\n';
  96. buf[n] = '\0';
  97. return n;
  98. }
  99. #define print_cpus_func(type) \
  100. static ssize_t print_cpus_##type(struct sysdev_class *class, char *buf) \
  101. { \
  102. return print_cpus_map(buf, &cpu_##type##_map); \
  103. } \
  104. static struct sysdev_class_attribute attr_##type##_map = \
  105. _SYSDEV_CLASS_ATTR(type, 0444, print_cpus_##type, NULL)
  106. print_cpus_func(online);
  107. print_cpus_func(possible);
  108. print_cpus_func(present);
  109. static struct sysdev_class_attribute *cpu_state_attr[] = {
  110. &attr_online_map,
  111. &attr_possible_map,
  112. &attr_present_map,
  113. };
  114. static int cpu_states_init(void)
  115. {
  116. int i;
  117. int err = 0;
  118. for (i = 0; i < ARRAY_SIZE(cpu_state_attr); i++) {
  119. int ret;
  120. ret = sysdev_class_create_file(&cpu_sysdev_class,
  121. cpu_state_attr[i]);
  122. if (!err)
  123. err = ret;
  124. }
  125. return err;
  126. }
  127. /*
  128. * register_cpu - Setup a sysfs device for a CPU.
  129. * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
  130. * sysfs for this CPU.
  131. * @num - CPU number to use when creating the device.
  132. *
  133. * Initialize and register the CPU device.
  134. */
  135. int __cpuinit register_cpu(struct cpu *cpu, int num)
  136. {
  137. int error;
  138. cpu->node_id = cpu_to_node(num);
  139. cpu->sysdev.id = num;
  140. cpu->sysdev.cls = &cpu_sysdev_class;
  141. error = sysdev_register(&cpu->sysdev);
  142. if (!error && cpu->hotpluggable)
  143. register_cpu_control(cpu);
  144. if (!error)
  145. per_cpu(cpu_sys_devices, num) = &cpu->sysdev;
  146. if (!error)
  147. register_cpu_under_node(num, cpu_to_node(num));
  148. #ifdef CONFIG_KEXEC
  149. if (!error)
  150. error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
  151. #endif
  152. return error;
  153. }
  154. struct sys_device *get_cpu_sysdev(unsigned cpu)
  155. {
  156. if (cpu < nr_cpu_ids && cpu_possible(cpu))
  157. return per_cpu(cpu_sys_devices, cpu);
  158. else
  159. return NULL;
  160. }
  161. EXPORT_SYMBOL_GPL(get_cpu_sysdev);
  162. int __init cpu_dev_init(void)
  163. {
  164. int err;
  165. err = sysdev_class_register(&cpu_sysdev_class);
  166. if (!err)
  167. err = cpu_states_init();
  168. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  169. if (!err)
  170. err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class);
  171. #endif
  172. return err;
  173. }