cpu.c 6.7 KB

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