cpu.c 6.6 KB

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