cpu.c 11 KB

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