cpu.c 2.4 KB

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