cpu.c 2.4 KB

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