cpu.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifdef CONFIG_HOTPLUG_CPU
  16. int __attribute__((weak)) smp_prepare_cpu (int cpu)
  17. {
  18. return 0;
  19. }
  20. static ssize_t show_online(struct sys_device *dev, char *buf)
  21. {
  22. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  23. return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
  24. }
  25. static ssize_t store_online(struct sys_device *dev, const char *buf,
  26. size_t count)
  27. {
  28. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  29. ssize_t ret;
  30. switch (buf[0]) {
  31. case '0':
  32. ret = cpu_down(cpu->sysdev.id);
  33. if (!ret)
  34. kobject_hotplug(&dev->kobj, KOBJ_OFFLINE);
  35. break;
  36. case '1':
  37. ret = smp_prepare_cpu(cpu->sysdev.id);
  38. if (!ret)
  39. ret = cpu_up(cpu->sysdev.id);
  40. if (!ret)
  41. kobject_hotplug(&dev->kobj, KOBJ_ONLINE);
  42. break;
  43. default:
  44. ret = -EINVAL;
  45. }
  46. if (ret >= 0)
  47. ret = count;
  48. return ret;
  49. }
  50. static SYSDEV_ATTR(online, 0600, show_online, store_online);
  51. static void __devinit register_cpu_control(struct cpu *cpu)
  52. {
  53. sysdev_create_file(&cpu->sysdev, &attr_online);
  54. }
  55. void unregister_cpu(struct cpu *cpu, struct node *root)
  56. {
  57. if (root)
  58. sysfs_remove_link(&root->sysdev.kobj,
  59. kobject_name(&cpu->sysdev.kobj));
  60. sysdev_remove_file(&cpu->sysdev, &attr_online);
  61. sysdev_unregister(&cpu->sysdev);
  62. return;
  63. }
  64. #else /* ... !CONFIG_HOTPLUG_CPU */
  65. static inline void register_cpu_control(struct cpu *cpu)
  66. {
  67. }
  68. #endif /* CONFIG_HOTPLUG_CPU */
  69. /*
  70. * register_cpu - Setup a driverfs device for a CPU.
  71. * @cpu - Callers can set the cpu->no_control field to 1, to indicate not to
  72. * generate a control file in sysfs for this CPU.
  73. * @num - CPU number to use when creating the device.
  74. *
  75. * Initialize and register the CPU device.
  76. */
  77. int __devinit register_cpu(struct cpu *cpu, int num, struct node *root)
  78. {
  79. int error;
  80. cpu->node_id = cpu_to_node(num);
  81. cpu->sysdev.id = num;
  82. cpu->sysdev.cls = &cpu_sysdev_class;
  83. error = sysdev_register(&cpu->sysdev);
  84. if (!error && root)
  85. error = sysfs_create_link(&root->sysdev.kobj,
  86. &cpu->sysdev.kobj,
  87. kobject_name(&cpu->sysdev.kobj));
  88. if (!error && !cpu->no_control)
  89. register_cpu_control(cpu);
  90. return error;
  91. }
  92. int __init cpu_dev_init(void)
  93. {
  94. return sysdev_class_register(&cpu_sysdev_class);
  95. }