blob: 0bf2dc11cdb8aa56c8728933ab61329120795280 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/cpu.c - basic CPU class support
3 */
4
5#include <linux/sysdev.h>
6#include <linux/module.h>
7#include <linux/init.h>
8#include <linux/cpu.h>
9#include <linux/topology.h>
10#include <linux/device.h>
11
12
13struct sysdev_class cpu_sysdev_class = {
14 set_kset_name("cpu"),
15};
16EXPORT_SYMBOL(cpu_sysdev_class);
17
18#ifdef CONFIG_HOTPLUG_CPU
Ashok Raj52a119f2005-06-25 14:54:57 -070019int __attribute__((weak)) smp_prepare_cpu (int cpu)
20{
21 return 0;
22}
Li Shaohuae1367da2005-06-25 14:54:56 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static ssize_t show_online(struct sys_device *dev, char *buf)
25{
26 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
27
28 return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
29}
30
31static ssize_t store_online(struct sys_device *dev, const char *buf,
32 size_t count)
33{
34 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
35 ssize_t ret;
36
37 switch (buf[0]) {
38 case '0':
39 ret = cpu_down(cpu->sysdev.id);
40 if (!ret)
41 kobject_hotplug(&dev->kobj, KOBJ_OFFLINE);
42 break;
43 case '1':
Li Shaohuae1367da2005-06-25 14:54:56 -070044 ret = smp_prepare_cpu(cpu->sysdev.id);
Ashok Raj52a119f2005-06-25 14:54:57 -070045 if (!ret)
Li Shaohuae1367da2005-06-25 14:54:56 -070046 ret = cpu_up(cpu->sysdev.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 break;
48 default:
49 ret = -EINVAL;
50 }
51
52 if (ret >= 0)
53 ret = count;
54 return ret;
55}
56static SYSDEV_ATTR(online, 0600, show_online, store_online);
57
58static void __devinit register_cpu_control(struct cpu *cpu)
59{
60 sysdev_create_file(&cpu->sysdev, &attr_online);
61}
62void unregister_cpu(struct cpu *cpu, struct node *root)
63{
64
65 if (root)
66 sysfs_remove_link(&root->sysdev.kobj,
67 kobject_name(&cpu->sysdev.kobj));
68 sysdev_remove_file(&cpu->sysdev, &attr_online);
69
70 sysdev_unregister(&cpu->sysdev);
71
72 return;
73}
74#else /* ... !CONFIG_HOTPLUG_CPU */
75static inline void register_cpu_control(struct cpu *cpu)
76{
77}
78#endif /* CONFIG_HOTPLUG_CPU */
79
80/*
81 * register_cpu - Setup a driverfs device for a CPU.
82 * @cpu - Callers can set the cpu->no_control field to 1, to indicate not to
83 * generate a control file in sysfs for this CPU.
84 * @num - CPU number to use when creating the device.
85 *
86 * Initialize and register the CPU device.
87 */
88int __devinit register_cpu(struct cpu *cpu, int num, struct node *root)
89{
90 int error;
91
92 cpu->node_id = cpu_to_node(num);
93 cpu->sysdev.id = num;
94 cpu->sysdev.cls = &cpu_sysdev_class;
95
96 error = sysdev_register(&cpu->sysdev);
97 if (!error && root)
98 error = sysfs_create_link(&root->sysdev.kobj,
99 &cpu->sysdev.kobj,
100 kobject_name(&cpu->sysdev.kobj));
101 if (!error && !cpu->no_control)
102 register_cpu_control(cpu);
103 return error;
104}
105
106
107
108int __init cpu_dev_init(void)
109{
110 return sysdev_class_register(&cpu_sysdev_class);
111}