cpu.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /*
  73. * register_cpu - Setup a driverfs device for a CPU.
  74. * @cpu - Callers can set the cpu->no_control field to 1, to indicate not to
  75. * generate a control file in sysfs for this CPU.
  76. * @num - CPU number to use when creating the device.
  77. *
  78. * Initialize and register the CPU device.
  79. */
  80. int __devinit register_cpu(struct cpu *cpu, int num, struct node *root)
  81. {
  82. int error;
  83. cpu->node_id = cpu_to_node(num);
  84. cpu->sysdev.id = num;
  85. cpu->sysdev.cls = &cpu_sysdev_class;
  86. error = sysdev_register(&cpu->sysdev);
  87. if (!error && root)
  88. error = sysfs_create_link(&root->sysdev.kobj,
  89. &cpu->sysdev.kobj,
  90. kobject_name(&cpu->sysdev.kobj));
  91. if (!error && !cpu->no_control)
  92. register_cpu_control(cpu);
  93. if (!error)
  94. cpu_sys_devices[num] = &cpu->sysdev;
  95. return error;
  96. }
  97. struct sys_device *get_cpu_sysdev(int cpu)
  98. {
  99. if (cpu < NR_CPUS)
  100. return cpu_sys_devices[cpu];
  101. else
  102. return NULL;
  103. }
  104. EXPORT_SYMBOL_GPL(get_cpu_sysdev);
  105. int __init cpu_dev_init(void)
  106. {
  107. return sysdev_class_register(&cpu_sysdev_class);
  108. }