cpu.c 6.5 KB

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