cpu.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * drivers/base/cpu.c - basic CPU class support
  3. */
  4. #include <linux/sysdev.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/cpu.h>
  8. #include <linux/topology.h>
  9. #include <linux/device.h>
  10. #include "base.h"
  11. struct sysdev_class cpu_sysdev_class = {
  12. set_kset_name("cpu"),
  13. };
  14. EXPORT_SYMBOL(cpu_sysdev_class);
  15. static struct sys_device *cpu_sys_devices[NR_CPUS];
  16. #ifdef CONFIG_HOTPLUG_CPU
  17. int __attribute__((weak)) smp_prepare_cpu (int cpu)
  18. {
  19. return 0;
  20. }
  21. static ssize_t show_online(struct sys_device *dev, char *buf)
  22. {
  23. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  24. return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
  25. }
  26. static ssize_t store_online(struct sys_device *dev, const char *buf,
  27. size_t count)
  28. {
  29. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  30. ssize_t ret;
  31. switch (buf[0]) {
  32. case '0':
  33. ret = cpu_down(cpu->sysdev.id);
  34. if (!ret)
  35. kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  36. break;
  37. case '1':
  38. ret = smp_prepare_cpu(cpu->sysdev.id);
  39. if (!ret)
  40. ret = cpu_up(cpu->sysdev.id);
  41. if (!ret)
  42. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  43. break;
  44. default:
  45. ret = -EINVAL;
  46. }
  47. if (ret >= 0)
  48. ret = count;
  49. return ret;
  50. }
  51. static SYSDEV_ATTR(online, 0600, show_online, store_online);
  52. static void __devinit register_cpu_control(struct cpu *cpu)
  53. {
  54. sysdev_create_file(&cpu->sysdev, &attr_online);
  55. }
  56. void unregister_cpu(struct cpu *cpu, struct node *root)
  57. {
  58. int logical_cpu = cpu->sysdev.id;
  59. if (root)
  60. sysfs_remove_link(&root->sysdev.kobj,
  61. kobject_name(&cpu->sysdev.kobj));
  62. sysdev_remove_file(&cpu->sysdev, &attr_online);
  63. sysdev_unregister(&cpu->sysdev);
  64. cpu_sys_devices[logical_cpu] = NULL;
  65. return;
  66. }
  67. #else /* ... !CONFIG_HOTPLUG_CPU */
  68. static inline void register_cpu_control(struct cpu *cpu)
  69. {
  70. }
  71. #endif /* CONFIG_HOTPLUG_CPU */
  72. #ifdef CONFIG_KEXEC
  73. #include <linux/kexec.h>
  74. static ssize_t show_crash_notes(struct sys_device *dev, char *buf)
  75. {
  76. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  77. ssize_t rc;
  78. unsigned long long addr;
  79. int cpunum;
  80. cpunum = cpu->sysdev.id;
  81. /*
  82. * Might be reading other cpu's data based on which cpu read thread
  83. * has been scheduled. But cpu data (memory) is allocated once during
  84. * boot up and this data does not change there after. Hence this
  85. * operation should be safe. No locking required.
  86. */
  87. addr = __pa(per_cpu_ptr(crash_notes, cpunum));
  88. rc = sprintf(buf, "%Lx\n", addr);
  89. return rc;
  90. }
  91. static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
  92. #endif
  93. /*
  94. * register_cpu - Setup a driverfs device for a CPU.
  95. * @cpu - Callers can set the cpu->no_control field to 1, to indicate not to
  96. * generate a control file in sysfs for this CPU.
  97. * @num - CPU number to use when creating the device.
  98. *
  99. * Initialize and register the CPU device.
  100. */
  101. int __devinit register_cpu(struct cpu *cpu, int num, struct node *root)
  102. {
  103. int error;
  104. cpu->node_id = cpu_to_node(num);
  105. cpu->sysdev.id = num;
  106. cpu->sysdev.cls = &cpu_sysdev_class;
  107. error = sysdev_register(&cpu->sysdev);
  108. if (!error && root)
  109. error = sysfs_create_link(&root->sysdev.kobj,
  110. &cpu->sysdev.kobj,
  111. kobject_name(&cpu->sysdev.kobj));
  112. if (!error && !cpu->no_control)
  113. register_cpu_control(cpu);
  114. if (!error)
  115. cpu_sys_devices[num] = &cpu->sysdev;
  116. #ifdef CONFIG_KEXEC
  117. if (!error)
  118. error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
  119. #endif
  120. return error;
  121. }
  122. struct sys_device *get_cpu_sysdev(int cpu)
  123. {
  124. if (cpu < NR_CPUS)
  125. return cpu_sys_devices[cpu];
  126. else
  127. return NULL;
  128. }
  129. EXPORT_SYMBOL_GPL(get_cpu_sysdev);
  130. int __init cpu_dev_init(void)
  131. {
  132. return sysdev_class_register(&cpu_sysdev_class);
  133. }