cpu.c 4.5 KB

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