cpu.c 6.6 KB

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