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