cpu.c 7.2 KB

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