cpu.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. static struct sys_device *cpu_sys_devices[NR_CPUS];
  16. #ifdef CONFIG_HOTPLUG_CPU
  17. static ssize_t show_online(struct sys_device *dev, char *buf)
  18. {
  19. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  20. return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
  21. }
  22. static ssize_t store_online(struct sys_device *dev, const char *buf,
  23. size_t count)
  24. {
  25. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  26. ssize_t ret;
  27. switch (buf[0]) {
  28. case '0':
  29. ret = cpu_down(cpu->sysdev.id);
  30. if (!ret)
  31. kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  32. break;
  33. case '1':
  34. ret = cpu_up(cpu->sysdev.id);
  35. if (!ret)
  36. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  37. break;
  38. default:
  39. ret = -EINVAL;
  40. }
  41. if (ret >= 0)
  42. ret = count;
  43. return ret;
  44. }
  45. static SYSDEV_ATTR(online, 0600, show_online, store_online);
  46. static void __devinit register_cpu_control(struct cpu *cpu)
  47. {
  48. sysdev_create_file(&cpu->sysdev, &attr_online);
  49. }
  50. void unregister_cpu(struct cpu *cpu, struct node *root)
  51. {
  52. int logical_cpu = cpu->sysdev.id;
  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. cpu_sys_devices[logical_cpu] = NULL;
  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. #ifdef CONFIG_KEXEC
  67. #include <linux/kexec.h>
  68. static ssize_t show_crash_notes(struct sys_device *dev, char *buf)
  69. {
  70. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  71. ssize_t rc;
  72. unsigned long long addr;
  73. int cpunum;
  74. cpunum = cpu->sysdev.id;
  75. /*
  76. * Might be reading other cpu's data based on which cpu read thread
  77. * has been scheduled. But cpu data (memory) is allocated once during
  78. * boot up and this data does not change there after. Hence this
  79. * operation should be safe. No locking required.
  80. */
  81. addr = __pa(per_cpu_ptr(crash_notes, cpunum));
  82. rc = sprintf(buf, "%Lx\n", addr);
  83. return rc;
  84. }
  85. static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
  86. #endif
  87. /*
  88. * register_cpu - Setup a driverfs device for a CPU.
  89. * @cpu - Callers can set the cpu->no_control field to 1, to indicate not to
  90. * generate a control file in sysfs for this CPU.
  91. * @num - CPU number to use when creating the device.
  92. *
  93. * Initialize and register the CPU device.
  94. */
  95. int __devinit register_cpu(struct cpu *cpu, int num, struct node *root)
  96. {
  97. int error;
  98. cpu->node_id = cpu_to_node(num);
  99. cpu->sysdev.id = num;
  100. cpu->sysdev.cls = &cpu_sysdev_class;
  101. error = sysdev_register(&cpu->sysdev);
  102. if (!error && root)
  103. error = sysfs_create_link(&root->sysdev.kobj,
  104. &cpu->sysdev.kobj,
  105. kobject_name(&cpu->sysdev.kobj));
  106. if (!error && !cpu->no_control)
  107. register_cpu_control(cpu);
  108. if (!error)
  109. cpu_sys_devices[num] = &cpu->sysdev;
  110. #ifdef CONFIG_KEXEC
  111. if (!error)
  112. error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
  113. #endif
  114. return error;
  115. }
  116. struct sys_device *get_cpu_sysdev(unsigned cpu)
  117. {
  118. if (cpu < NR_CPUS)
  119. return cpu_sys_devices[cpu];
  120. else
  121. return NULL;
  122. }
  123. EXPORT_SYMBOL_GPL(get_cpu_sysdev);
  124. int __init cpu_dev_init(void)
  125. {
  126. return sysdev_class_register(&cpu_sysdev_class);
  127. }