cpu.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. cpu_hotplug_driver_lock();
  31. switch (buf[0]) {
  32. case '0':
  33. ret = cpu_down(cpu->sysdev.id);
  34. if (!ret)
  35. kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  36. break;
  37. case '1':
  38. ret = cpu_up(cpu->sysdev.id);
  39. if (!ret)
  40. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  41. break;
  42. default:
  43. ret = -EINVAL;
  44. }
  45. cpu_hotplug_driver_unlock();
  46. if (ret >= 0)
  47. ret = count;
  48. return ret;
  49. }
  50. static SYSDEV_ATTR(online, 0644, show_online, store_online);
  51. static void __cpuinit register_cpu_control(struct cpu *cpu)
  52. {
  53. sysdev_create_file(&cpu->sysdev, &attr_online);
  54. }
  55. void unregister_cpu(struct cpu *cpu)
  56. {
  57. int logical_cpu = cpu->sysdev.id;
  58. unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
  59. sysdev_remove_file(&cpu->sysdev, &attr_online);
  60. sysdev_unregister(&cpu->sysdev);
  61. per_cpu(cpu_sys_devices, logical_cpu) = NULL;
  62. return;
  63. }
  64. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  65. static ssize_t cpu_probe_store(struct class *class, const char *buf,
  66. size_t count)
  67. {
  68. return arch_cpu_probe(buf, count);
  69. }
  70. static ssize_t cpu_release_store(struct class *class, const char *buf,
  71. size_t count)
  72. {
  73. return arch_cpu_release(buf, count);
  74. }
  75. static CLASS_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
  76. static CLASS_ATTR(release, S_IWUSR, NULL, cpu_release_store);
  77. int __init cpu_probe_release_init(void)
  78. {
  79. int rc;
  80. rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
  81. &class_attr_probe.attr);
  82. if (!rc)
  83. rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
  84. &class_attr_release.attr);
  85. return rc;
  86. }
  87. device_initcall(cpu_probe_release_init);
  88. #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
  89. #else /* ... !CONFIG_HOTPLUG_CPU */
  90. static inline void register_cpu_control(struct cpu *cpu)
  91. {
  92. }
  93. #endif /* CONFIG_HOTPLUG_CPU */
  94. #ifdef CONFIG_KEXEC
  95. #include <linux/kexec.h>
  96. static ssize_t show_crash_notes(struct sys_device *dev, struct sysdev_attribute *attr,
  97. char *buf)
  98. {
  99. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  100. ssize_t rc;
  101. unsigned long long addr;
  102. int cpunum;
  103. cpunum = cpu->sysdev.id;
  104. /*
  105. * Might be reading other cpu's data based on which cpu read thread
  106. * has been scheduled. But cpu data (memory) is allocated once during
  107. * boot up and this data does not change there after. Hence this
  108. * operation should be safe. No locking required.
  109. */
  110. addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
  111. rc = sprintf(buf, "%Lx\n", addr);
  112. return rc;
  113. }
  114. static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
  115. #endif
  116. /*
  117. * Print cpu online, possible, present, and system maps
  118. */
  119. static ssize_t print_cpus_map(char *buf, const struct cpumask *map)
  120. {
  121. int n = cpulist_scnprintf(buf, PAGE_SIZE-2, map);
  122. buf[n++] = '\n';
  123. buf[n] = '\0';
  124. return n;
  125. }
  126. #define print_cpus_func(type) \
  127. static ssize_t print_cpus_##type(struct sysdev_class *class, char *buf) \
  128. { \
  129. return print_cpus_map(buf, cpu_##type##_mask); \
  130. } \
  131. static struct sysdev_class_attribute attr_##type##_map = \
  132. _SYSDEV_CLASS_ATTR(type, 0444, print_cpus_##type, NULL)
  133. print_cpus_func(online);
  134. print_cpus_func(possible);
  135. print_cpus_func(present);
  136. /*
  137. * Print values for NR_CPUS and offlined cpus
  138. */
  139. static ssize_t print_cpus_kernel_max(struct sysdev_class *class, char *buf)
  140. {
  141. int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1);
  142. return n;
  143. }
  144. static SYSDEV_CLASS_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
  145. /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
  146. unsigned int total_cpus;
  147. static ssize_t print_cpus_offline(struct sysdev_class *class, char *buf)
  148. {
  149. int n = 0, len = PAGE_SIZE-2;
  150. cpumask_var_t offline;
  151. /* display offline cpus < nr_cpu_ids */
  152. if (!alloc_cpumask_var(&offline, GFP_KERNEL))
  153. return -ENOMEM;
  154. cpumask_complement(offline, cpu_online_mask);
  155. n = cpulist_scnprintf(buf, len, offline);
  156. free_cpumask_var(offline);
  157. /* display offline cpus >= nr_cpu_ids */
  158. if (total_cpus && nr_cpu_ids < total_cpus) {
  159. if (n && n < len)
  160. buf[n++] = ',';
  161. if (nr_cpu_ids == total_cpus-1)
  162. n += snprintf(&buf[n], len - n, "%d", nr_cpu_ids);
  163. else
  164. n += snprintf(&buf[n], len - n, "%d-%d",
  165. nr_cpu_ids, total_cpus-1);
  166. }
  167. n += snprintf(&buf[n], len - n, "\n");
  168. return n;
  169. }
  170. static SYSDEV_CLASS_ATTR(offline, 0444, print_cpus_offline, NULL);
  171. static struct sysdev_class_attribute *cpu_state_attr[] = {
  172. &attr_online_map,
  173. &attr_possible_map,
  174. &attr_present_map,
  175. &attr_kernel_max,
  176. &attr_offline,
  177. };
  178. static int cpu_states_init(void)
  179. {
  180. int i;
  181. int err = 0;
  182. for (i = 0; i < ARRAY_SIZE(cpu_state_attr); i++) {
  183. int ret;
  184. ret = sysdev_class_create_file(&cpu_sysdev_class,
  185. cpu_state_attr[i]);
  186. if (!err)
  187. err = ret;
  188. }
  189. return err;
  190. }
  191. /*
  192. * register_cpu - Setup a sysfs device for a CPU.
  193. * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
  194. * sysfs for this CPU.
  195. * @num - CPU number to use when creating the device.
  196. *
  197. * Initialize and register the CPU device.
  198. */
  199. int __cpuinit register_cpu(struct cpu *cpu, int num)
  200. {
  201. int error;
  202. cpu->node_id = cpu_to_node(num);
  203. cpu->sysdev.id = num;
  204. cpu->sysdev.cls = &cpu_sysdev_class;
  205. error = sysdev_register(&cpu->sysdev);
  206. if (!error && cpu->hotpluggable)
  207. register_cpu_control(cpu);
  208. if (!error)
  209. per_cpu(cpu_sys_devices, num) = &cpu->sysdev;
  210. if (!error)
  211. register_cpu_under_node(num, cpu_to_node(num));
  212. #ifdef CONFIG_KEXEC
  213. if (!error)
  214. error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
  215. #endif
  216. return error;
  217. }
  218. struct sys_device *get_cpu_sysdev(unsigned cpu)
  219. {
  220. if (cpu < nr_cpu_ids && cpu_possible(cpu))
  221. return per_cpu(cpu_sys_devices, cpu);
  222. else
  223. return NULL;
  224. }
  225. EXPORT_SYMBOL_GPL(get_cpu_sysdev);
  226. int __init cpu_dev_init(void)
  227. {
  228. int err;
  229. err = sysdev_class_register(&cpu_sysdev_class);
  230. if (!err)
  231. err = cpu_states_init();
  232. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  233. if (!err)
  234. err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class);
  235. #endif
  236. return err;
  237. }