cpu.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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/slab.h>
  14. #include <linux/percpu.h>
  15. #include <linux/acpi.h>
  16. #include "base.h"
  17. static DEFINE_PER_CPU(struct device *, cpu_sys_devices);
  18. static int cpu_subsys_match(struct device *dev, struct device_driver *drv)
  19. {
  20. /* ACPI style match is the only one that may succeed. */
  21. if (acpi_driver_match_device(dev, drv))
  22. return 1;
  23. return 0;
  24. }
  25. #ifdef CONFIG_HOTPLUG_CPU
  26. static void change_cpu_under_node(struct cpu *cpu,
  27. unsigned int from_nid, unsigned int to_nid)
  28. {
  29. int cpuid = cpu->dev.id;
  30. unregister_cpu_under_node(cpuid, from_nid);
  31. register_cpu_under_node(cpuid, to_nid);
  32. cpu->node_id = to_nid;
  33. }
  34. static int __ref cpu_subsys_online(struct device *dev)
  35. {
  36. struct cpu *cpu = container_of(dev, struct cpu, dev);
  37. int cpuid = dev->id;
  38. int from_nid, to_nid;
  39. int ret;
  40. cpu_hotplug_driver_lock();
  41. from_nid = cpu_to_node(cpuid);
  42. ret = cpu_up(cpuid);
  43. /*
  44. * When hot adding memory to memoryless node and enabling a cpu
  45. * on the node, node number of the cpu may internally change.
  46. */
  47. to_nid = cpu_to_node(cpuid);
  48. if (from_nid != to_nid)
  49. change_cpu_under_node(cpu, from_nid, to_nid);
  50. cpu_hotplug_driver_unlock();
  51. return ret;
  52. }
  53. static int cpu_subsys_offline(struct device *dev)
  54. {
  55. int ret;
  56. cpu_hotplug_driver_lock();
  57. ret = cpu_down(dev->id);
  58. cpu_hotplug_driver_unlock();
  59. return ret;
  60. }
  61. void unregister_cpu(struct cpu *cpu)
  62. {
  63. int logical_cpu = cpu->dev.id;
  64. unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
  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. #endif /* CONFIG_HOTPLUG_CPU */
  88. struct bus_type cpu_subsys = {
  89. .name = "cpu",
  90. .dev_name = "cpu",
  91. .match = cpu_subsys_match,
  92. #ifdef CONFIG_HOTPLUG_CPU
  93. .online = cpu_subsys_online,
  94. .offline = cpu_subsys_offline,
  95. #endif
  96. };
  97. EXPORT_SYMBOL_GPL(cpu_subsys);
  98. #ifdef CONFIG_KEXEC
  99. #include <linux/kexec.h>
  100. static ssize_t show_crash_notes(struct device *dev, struct device_attribute *attr,
  101. char *buf)
  102. {
  103. struct cpu *cpu = container_of(dev, struct cpu, dev);
  104. ssize_t rc;
  105. unsigned long long addr;
  106. int cpunum;
  107. cpunum = cpu->dev.id;
  108. /*
  109. * Might be reading other cpu's data based on which cpu read thread
  110. * has been scheduled. But cpu data (memory) is allocated once during
  111. * boot up and this data does not change there after. Hence this
  112. * operation should be safe. No locking required.
  113. */
  114. addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
  115. rc = sprintf(buf, "%Lx\n", addr);
  116. return rc;
  117. }
  118. static DEVICE_ATTR(crash_notes, 0400, show_crash_notes, NULL);
  119. static ssize_t show_crash_notes_size(struct device *dev,
  120. struct device_attribute *attr,
  121. char *buf)
  122. {
  123. ssize_t rc;
  124. rc = sprintf(buf, "%zu\n", sizeof(note_buf_t));
  125. return rc;
  126. }
  127. static DEVICE_ATTR(crash_notes_size, 0400, show_crash_notes_size, NULL);
  128. #endif
  129. /*
  130. * Print cpu online, possible, present, and system maps
  131. */
  132. struct cpu_attr {
  133. struct device_attribute attr;
  134. const struct cpumask *const * const map;
  135. };
  136. static ssize_t show_cpus_attr(struct device *dev,
  137. struct device_attribute *attr,
  138. char *buf)
  139. {
  140. struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
  141. int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *(ca->map));
  142. buf[n++] = '\n';
  143. buf[n] = '\0';
  144. return n;
  145. }
  146. #define _CPU_ATTR(name, map) \
  147. { __ATTR(name, 0444, show_cpus_attr, NULL), map }
  148. /* Keep in sync with cpu_subsys_attrs */
  149. static struct cpu_attr cpu_attrs[] = {
  150. _CPU_ATTR(online, &cpu_online_mask),
  151. _CPU_ATTR(possible, &cpu_possible_mask),
  152. _CPU_ATTR(present, &cpu_present_mask),
  153. };
  154. /*
  155. * Print values for NR_CPUS and offlined cpus
  156. */
  157. static ssize_t print_cpus_kernel_max(struct device *dev,
  158. struct device_attribute *attr, char *buf)
  159. {
  160. int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1);
  161. return n;
  162. }
  163. static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
  164. /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
  165. unsigned int total_cpus;
  166. static ssize_t print_cpus_offline(struct device *dev,
  167. struct device_attribute *attr, char *buf)
  168. {
  169. int n = 0, len = PAGE_SIZE-2;
  170. cpumask_var_t offline;
  171. /* display offline cpus < nr_cpu_ids */
  172. if (!alloc_cpumask_var(&offline, GFP_KERNEL))
  173. return -ENOMEM;
  174. cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask);
  175. n = cpulist_scnprintf(buf, len, offline);
  176. free_cpumask_var(offline);
  177. /* display offline cpus >= nr_cpu_ids */
  178. if (total_cpus && nr_cpu_ids < total_cpus) {
  179. if (n && n < len)
  180. buf[n++] = ',';
  181. if (nr_cpu_ids == total_cpus-1)
  182. n += snprintf(&buf[n], len - n, "%d", nr_cpu_ids);
  183. else
  184. n += snprintf(&buf[n], len - n, "%d-%d",
  185. nr_cpu_ids, total_cpus-1);
  186. }
  187. n += snprintf(&buf[n], len - n, "\n");
  188. return n;
  189. }
  190. static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
  191. static void cpu_device_release(struct device *dev)
  192. {
  193. /*
  194. * This is an empty function to prevent the driver core from spitting a
  195. * warning at us. Yes, I know this is directly opposite of what the
  196. * documentation for the driver core and kobjects say, and the author
  197. * of this code has already been publically ridiculed for doing
  198. * something as foolish as this. However, at this point in time, it is
  199. * the only way to handle the issue of statically allocated cpu
  200. * devices. The different architectures will have their cpu device
  201. * code reworked to properly handle this in the near future, so this
  202. * function will then be changed to correctly free up the memory held
  203. * by the cpu device.
  204. *
  205. * Never copy this way of doing things, or you too will be made fun of
  206. * on the linux-kernel list, you have been warned.
  207. */
  208. }
  209. /*
  210. * register_cpu - Setup a sysfs device for a CPU.
  211. * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
  212. * sysfs for this CPU.
  213. * @num - CPU number to use when creating the device.
  214. *
  215. * Initialize and register the CPU device.
  216. */
  217. int __cpuinit register_cpu(struct cpu *cpu, int num)
  218. {
  219. int error;
  220. cpu->node_id = cpu_to_node(num);
  221. memset(&cpu->dev, 0x00, sizeof(struct device));
  222. cpu->dev.id = num;
  223. cpu->dev.bus = &cpu_subsys;
  224. cpu->dev.release = cpu_device_release;
  225. cpu->dev.offline_disabled = !cpu->hotpluggable;
  226. cpu->dev.offline = !cpu_online(num);
  227. #ifdef CONFIG_ARCH_HAS_CPU_AUTOPROBE
  228. cpu->dev.bus->uevent = arch_cpu_uevent;
  229. #endif
  230. error = device_register(&cpu->dev);
  231. if (!error)
  232. per_cpu(cpu_sys_devices, num) = &cpu->dev;
  233. if (!error)
  234. register_cpu_under_node(num, cpu_to_node(num));
  235. #ifdef CONFIG_KEXEC
  236. if (!error)
  237. error = device_create_file(&cpu->dev, &dev_attr_crash_notes);
  238. if (!error)
  239. error = device_create_file(&cpu->dev,
  240. &dev_attr_crash_notes_size);
  241. #endif
  242. return error;
  243. }
  244. struct device *get_cpu_device(unsigned cpu)
  245. {
  246. if (cpu < nr_cpu_ids && cpu_possible(cpu))
  247. return per_cpu(cpu_sys_devices, cpu);
  248. else
  249. return NULL;
  250. }
  251. EXPORT_SYMBOL_GPL(get_cpu_device);
  252. #ifdef CONFIG_ARCH_HAS_CPU_AUTOPROBE
  253. static DEVICE_ATTR(modalias, 0444, arch_print_cpu_modalias, NULL);
  254. #endif
  255. static struct attribute *cpu_root_attrs[] = {
  256. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  257. &dev_attr_probe.attr,
  258. &dev_attr_release.attr,
  259. #endif
  260. &cpu_attrs[0].attr.attr,
  261. &cpu_attrs[1].attr.attr,
  262. &cpu_attrs[2].attr.attr,
  263. &dev_attr_kernel_max.attr,
  264. &dev_attr_offline.attr,
  265. #ifdef CONFIG_ARCH_HAS_CPU_AUTOPROBE
  266. &dev_attr_modalias.attr,
  267. #endif
  268. NULL
  269. };
  270. static struct attribute_group cpu_root_attr_group = {
  271. .attrs = cpu_root_attrs,
  272. };
  273. static const struct attribute_group *cpu_root_attr_groups[] = {
  274. &cpu_root_attr_group,
  275. NULL,
  276. };
  277. bool cpu_is_hotpluggable(unsigned cpu)
  278. {
  279. struct device *dev = get_cpu_device(cpu);
  280. return dev && container_of(dev, struct cpu, dev)->hotpluggable;
  281. }
  282. EXPORT_SYMBOL_GPL(cpu_is_hotpluggable);
  283. #ifdef CONFIG_GENERIC_CPU_DEVICES
  284. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  285. #endif
  286. static void __init cpu_dev_register_generic(void)
  287. {
  288. #ifdef CONFIG_GENERIC_CPU_DEVICES
  289. int i;
  290. for_each_possible_cpu(i) {
  291. if (register_cpu(&per_cpu(cpu_devices, i), i))
  292. panic("Failed to register CPU device");
  293. }
  294. #endif
  295. }
  296. void __init cpu_dev_init(void)
  297. {
  298. if (subsys_system_register(&cpu_subsys, cpu_root_attr_groups))
  299. panic("Failed to register CPU subsystem");
  300. cpu_dev_register_generic();
  301. }