cpu.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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, const struct cpumask *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##_mask); \
  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. /*
  110. * Print values for NR_CPUS and offlined cpus
  111. */
  112. static ssize_t print_cpus_kernel_max(struct sysdev_class *class, char *buf)
  113. {
  114. int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1);
  115. return n;
  116. }
  117. static SYSDEV_CLASS_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
  118. /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
  119. unsigned int total_cpus;
  120. static ssize_t print_cpus_offline(struct sysdev_class *class, char *buf)
  121. {
  122. int n = 0, len = PAGE_SIZE-2;
  123. cpumask_var_t offline;
  124. /* display offline cpus < nr_cpu_ids */
  125. if (!alloc_cpumask_var(&offline, GFP_KERNEL))
  126. return -ENOMEM;
  127. cpumask_complement(offline, cpu_online_mask);
  128. n = cpulist_scnprintf(buf, len, offline);
  129. free_cpumask_var(offline);
  130. /* display offline cpus >= nr_cpu_ids */
  131. if (total_cpus && nr_cpu_ids < total_cpus) {
  132. if (n && n < len)
  133. buf[n++] = ',';
  134. if (nr_cpu_ids == total_cpus-1)
  135. n += snprintf(&buf[n], len - n, "%d", nr_cpu_ids);
  136. else
  137. n += snprintf(&buf[n], len - n, "%d-%d",
  138. nr_cpu_ids, total_cpus-1);
  139. }
  140. n += snprintf(&buf[n], len - n, "\n");
  141. return n;
  142. }
  143. static SYSDEV_CLASS_ATTR(offline, 0444, print_cpus_offline, NULL);
  144. static struct sysdev_class_attribute *cpu_state_attr[] = {
  145. &attr_online_map,
  146. &attr_possible_map,
  147. &attr_present_map,
  148. &attr_kernel_max,
  149. &attr_offline,
  150. };
  151. static int cpu_states_init(void)
  152. {
  153. int i;
  154. int err = 0;
  155. for (i = 0; i < ARRAY_SIZE(cpu_state_attr); i++) {
  156. int ret;
  157. ret = sysdev_class_create_file(&cpu_sysdev_class,
  158. cpu_state_attr[i]);
  159. if (!err)
  160. err = ret;
  161. }
  162. return err;
  163. }
  164. /*
  165. * register_cpu - Setup a sysfs device for a CPU.
  166. * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
  167. * sysfs for this CPU.
  168. * @num - CPU number to use when creating the device.
  169. *
  170. * Initialize and register the CPU device.
  171. */
  172. int __cpuinit register_cpu(struct cpu *cpu, int num)
  173. {
  174. int error;
  175. cpu->node_id = cpu_to_node(num);
  176. cpu->sysdev.id = num;
  177. cpu->sysdev.cls = &cpu_sysdev_class;
  178. error = sysdev_register(&cpu->sysdev);
  179. if (!error && cpu->hotpluggable)
  180. register_cpu_control(cpu);
  181. if (!error)
  182. per_cpu(cpu_sys_devices, num) = &cpu->sysdev;
  183. if (!error)
  184. register_cpu_under_node(num, cpu_to_node(num));
  185. #ifdef CONFIG_KEXEC
  186. if (!error)
  187. error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
  188. #endif
  189. return error;
  190. }
  191. struct sys_device *get_cpu_sysdev(unsigned cpu)
  192. {
  193. if (cpu < nr_cpu_ids && cpu_possible(cpu))
  194. return per_cpu(cpu_sys_devices, cpu);
  195. else
  196. return NULL;
  197. }
  198. EXPORT_SYMBOL_GPL(get_cpu_sysdev);
  199. int __init cpu_dev_init(void)
  200. {
  201. int err;
  202. err = sysdev_class_register(&cpu_sysdev_class);
  203. if (!err)
  204. err = cpu_states_init();
  205. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  206. if (!err)
  207. err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class);
  208. #endif
  209. return err;
  210. }