cpu.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/sched.h>
  8. #include <linux/cpu.h>
  9. #include <linux/topology.h>
  10. #include <linux/device.h>
  11. #include <linux/node.h>
  12. #include "base.h"
  13. struct sysdev_class cpu_sysdev_class = {
  14. set_kset_name("cpu"),
  15. };
  16. EXPORT_SYMBOL(cpu_sysdev_class);
  17. static struct sys_device *cpu_sys_devices[NR_CPUS];
  18. #ifdef CONFIG_HOTPLUG_CPU
  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_uevent(&dev->kobj, KOBJ_OFFLINE);
  34. break;
  35. case '1':
  36. ret = cpu_up(cpu->sysdev.id);
  37. if (!ret)
  38. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  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)
  53. {
  54. int logical_cpu = cpu->sysdev.id;
  55. unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
  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 sysfs device for a CPU.
  89. * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
  90. * 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)
  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 && cpu->hotpluggable)
  103. register_cpu_control(cpu);
  104. if (!error)
  105. cpu_sys_devices[num] = &cpu->sysdev;
  106. if (!error)
  107. register_cpu_under_node(num, cpu_to_node(num));
  108. #ifdef CONFIG_KEXEC
  109. if (!error)
  110. error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
  111. #endif
  112. return error;
  113. }
  114. struct sys_device *get_cpu_sysdev(unsigned cpu)
  115. {
  116. if (cpu < NR_CPUS)
  117. return cpu_sys_devices[cpu];
  118. else
  119. return NULL;
  120. }
  121. EXPORT_SYMBOL_GPL(get_cpu_sysdev);
  122. int __init cpu_dev_init(void)
  123. {
  124. int err;
  125. err = sysdev_class_register(&cpu_sysdev_class);
  126. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  127. if (!err)
  128. err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class);
  129. #endif
  130. return err;
  131. }