/drivers/base/cpu.c

https://bitbucket.org/evzijst/gittest · C · 104 lines · 75 code · 18 blank · 11 comment · 8 complexity · a36bd21c6731e80abe0200dc78883946 MD5 · raw file

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