cpu.c 6.8 KB

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