cpu.c 3.5 KB

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