blob: bdd7e9f55c81fac3e19d76ddb376972211d48779 [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
Li Shaohuae1367da2005-06-25 14:54:56 -070019#ifndef __HAVE_ARCH_SMP_PREPARE_CPU
20#define smp_prepare_cpu(cpu) (0)
21#endif
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static ssize_t show_online(struct sys_device *dev, char *buf)
24{
25 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
26
27 return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
28}
29
30static ssize_t store_online(struct sys_device *dev, const char *buf,
31 size_t count)
32{
33 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
34 ssize_t ret;
35
36 switch (buf[0]) {
37 case '0':
38 ret = cpu_down(cpu->sysdev.id);
39 if (!ret)
40 kobject_hotplug(&dev->kobj, KOBJ_OFFLINE);
41 break;
42 case '1':
Li Shaohuae1367da2005-06-25 14:54:56 -070043 ret = smp_prepare_cpu(cpu->sysdev.id);
44 if (ret == 0)
45 ret = cpu_up(cpu->sysdev.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 break;
47 default:
48 ret = -EINVAL;
49 }
50
51 if (ret >= 0)
52 ret = count;
53 return ret;
54}
55static SYSDEV_ATTR(online, 0600, show_online, store_online);
56
57static void __devinit register_cpu_control(struct cpu *cpu)
58{
59 sysdev_create_file(&cpu->sysdev, &attr_online);
60}
61void unregister_cpu(struct cpu *cpu, struct node *root)
62{
63
64 if (root)
65 sysfs_remove_link(&root->sysdev.kobj,
66 kobject_name(&cpu->sysdev.kobj));
67 sysdev_remove_file(&cpu->sysdev, &attr_online);
68
69 sysdev_unregister(&cpu->sysdev);
70
71 return;
72}
73#else /* ... !CONFIG_HOTPLUG_CPU */
74static inline void register_cpu_control(struct cpu *cpu)
75{
76}
77#endif /* CONFIG_HOTPLUG_CPU */
78
79/*
80 * register_cpu - Setup a driverfs device for a CPU.
81 * @cpu - Callers can set the cpu->no_control field to 1, to indicate not to
82 * generate a control file in sysfs for this CPU.
83 * @num - CPU number to use when creating the device.
84 *
85 * Initialize and register the CPU device.
86 */
87int __devinit register_cpu(struct cpu *cpu, int num, struct node *root)
88{
89 int error;
90
91 cpu->node_id = cpu_to_node(num);
92 cpu->sysdev.id = num;
93 cpu->sysdev.cls = &cpu_sysdev_class;
94
95 error = sysdev_register(&cpu->sysdev);
96 if (!error && root)
97 error = sysfs_create_link(&root->sysdev.kobj,
98 &cpu->sysdev.kobj,
99 kobject_name(&cpu->sysdev.kobj));
100 if (!error && !cpu->no_control)
101 register_cpu_control(cpu);
102 return error;
103}
104
105
106
107int __init cpu_dev_init(void)
108{
109 return sysdev_class_register(&cpu_sysdev_class);
110}