xref: /linux/drivers/base/cpu.c (revision d67b569f5f620c0fb95d5212642746b7ba9d29e4)
1 /*
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 
13 struct sysdev_class cpu_sysdev_class = {
14 	set_kset_name("cpu"),
15 };
16 EXPORT_SYMBOL(cpu_sysdev_class);
17 
18 #ifdef CONFIG_HOTPLUG_CPU
19 int __attribute__((weak)) smp_prepare_cpu (int cpu)
20 {
21 	return 0;
22 }
23 
24 static 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 
31 static 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':
44 		ret = smp_prepare_cpu(cpu->sysdev.id);
45 		if (!ret)
46 			ret = cpu_up(cpu->sysdev.id);
47 		if (!ret)
48 			kobject_hotplug(&dev->kobj, KOBJ_ONLINE);
49 		break;
50 	default:
51 		ret = -EINVAL;
52 	}
53 
54 	if (ret >= 0)
55 		ret = count;
56 	return ret;
57 }
58 static SYSDEV_ATTR(online, 0600, show_online, store_online);
59 
60 static void __devinit register_cpu_control(struct cpu *cpu)
61 {
62 	sysdev_create_file(&cpu->sysdev, &attr_online);
63 }
64 void unregister_cpu(struct cpu *cpu, struct node *root)
65 {
66 
67 	if (root)
68 		sysfs_remove_link(&root->sysdev.kobj,
69 				  kobject_name(&cpu->sysdev.kobj));
70 	sysdev_remove_file(&cpu->sysdev, &attr_online);
71 
72 	sysdev_unregister(&cpu->sysdev);
73 
74 	return;
75 }
76 #else /* ... !CONFIG_HOTPLUG_CPU */
77 static inline void register_cpu_control(struct cpu *cpu)
78 {
79 }
80 #endif /* CONFIG_HOTPLUG_CPU */
81 
82 /*
83  * register_cpu - Setup a driverfs device for a CPU.
84  * @cpu - Callers can set the cpu->no_control field to 1, to indicate not to
85  *		  generate a control file in sysfs for this CPU.
86  * @num - CPU number to use when creating the device.
87  *
88  * Initialize and register the CPU device.
89  */
90 int __devinit register_cpu(struct cpu *cpu, int num, struct node *root)
91 {
92 	int error;
93 
94 	cpu->node_id = cpu_to_node(num);
95 	cpu->sysdev.id = num;
96 	cpu->sysdev.cls = &cpu_sysdev_class;
97 
98 	error = sysdev_register(&cpu->sysdev);
99 	if (!error && root)
100 		error = sysfs_create_link(&root->sysdev.kobj,
101 					  &cpu->sysdev.kobj,
102 					  kobject_name(&cpu->sysdev.kobj));
103 	if (!error && !cpu->no_control)
104 		register_cpu_control(cpu);
105 	return error;
106 }
107 
108 
109 
110 int __init cpu_dev_init(void)
111 {
112 	return sysdev_class_register(&cpu_sysdev_class);
113 }
114