cpu.c 9.2 KB

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