cpu.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. break;
  40. default:
  41. ret = -EINVAL;
  42. }
  43. if (ret >= 0)
  44. ret = count;
  45. return ret;
  46. }
  47. static SYSDEV_ATTR(online, 0600, show_online, store_online);
  48. static void __devinit register_cpu_control(struct cpu *cpu)
  49. {
  50. sysdev_create_file(&cpu->sysdev, &attr_online);
  51. }
  52. void unregister_cpu(struct cpu *cpu, struct node *root)
  53. {
  54. if (root)
  55. sysfs_remove_link(&root->sysdev.kobj,
  56. kobject_name(&cpu->sysdev.kobj));
  57. sysdev_remove_file(&cpu->sysdev, &attr_online);
  58. sysdev_unregister(&cpu->sysdev);
  59. return;
  60. }
  61. #else /* ... !CONFIG_HOTPLUG_CPU */
  62. static inline void register_cpu_control(struct cpu *cpu)
  63. {
  64. }
  65. #endif /* CONFIG_HOTPLUG_CPU */
  66. /*
  67. * register_cpu - Setup a driverfs device for a CPU.
  68. * @cpu - Callers can set the cpu->no_control field to 1, to indicate not to
  69. * generate a control file in sysfs for this CPU.
  70. * @num - CPU number to use when creating the device.
  71. *
  72. * Initialize and register the CPU device.
  73. */
  74. int __devinit register_cpu(struct cpu *cpu, int num, struct node *root)
  75. {
  76. int error;
  77. cpu->node_id = cpu_to_node(num);
  78. cpu->sysdev.id = num;
  79. cpu->sysdev.cls = &cpu_sysdev_class;
  80. error = sysdev_register(&cpu->sysdev);
  81. if (!error && root)
  82. error = sysfs_create_link(&root->sysdev.kobj,
  83. &cpu->sysdev.kobj,
  84. kobject_name(&cpu->sysdev.kobj));
  85. if (!error && !cpu->no_control)
  86. register_cpu_control(cpu);
  87. return error;
  88. }
  89. int __init cpu_dev_init(void)
  90. {
  91. return sysdev_class_register(&cpu_sysdev_class);
  92. }