sysdev: Convert node driver
[safe/jmp/linux-2.6] / drivers / base / cpu.c
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/sched.h>
9 #include <linux/cpu.h>
10 #include <linux/topology.h>
11 #include <linux/device.h>
12 #include <linux/node.h>
13
14 #include "base.h"
15
16 static struct sysdev_class_attribute *cpu_sysdev_class_attrs[];
17
18 struct sysdev_class cpu_sysdev_class = {
19         .name = "cpu",
20         .attrs = cpu_sysdev_class_attrs,
21 };
22 EXPORT_SYMBOL(cpu_sysdev_class);
23
24 static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices);
25
26 #ifdef CONFIG_HOTPLUG_CPU
27 static ssize_t show_online(struct sys_device *dev, struct sysdev_attribute *attr,
28                            char *buf)
29 {
30         struct cpu *cpu = container_of(dev, struct cpu, sysdev);
31
32         return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
33 }
34
35 static ssize_t __ref store_online(struct sys_device *dev, struct sysdev_attribute *attr,
36                                  const char *buf, size_t count)
37 {
38         struct cpu *cpu = container_of(dev, struct cpu, sysdev);
39         ssize_t ret;
40
41         cpu_hotplug_driver_lock();
42         switch (buf[0]) {
43         case '0':
44                 ret = cpu_down(cpu->sysdev.id);
45                 if (!ret)
46                         kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
47                 break;
48         case '1':
49                 ret = cpu_up(cpu->sysdev.id);
50                 if (!ret)
51                         kobject_uevent(&dev->kobj, KOBJ_ONLINE);
52                 break;
53         default:
54                 ret = -EINVAL;
55         }
56         cpu_hotplug_driver_unlock();
57
58         if (ret >= 0)
59                 ret = count;
60         return ret;
61 }
62 static SYSDEV_ATTR(online, 0644, show_online, store_online);
63
64 static void __cpuinit register_cpu_control(struct cpu *cpu)
65 {
66         sysdev_create_file(&cpu->sysdev, &attr_online);
67 }
68 void unregister_cpu(struct cpu *cpu)
69 {
70         int logical_cpu = cpu->sysdev.id;
71
72         unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
73
74         sysdev_remove_file(&cpu->sysdev, &attr_online);
75
76         sysdev_unregister(&cpu->sysdev);
77         per_cpu(cpu_sys_devices, logical_cpu) = NULL;
78         return;
79 }
80
81 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
82 static ssize_t cpu_probe_store(struct class *class, const char *buf,
83                                size_t count)
84 {
85         return arch_cpu_probe(buf, count);
86 }
87
88 static ssize_t cpu_release_store(struct class *class, const char *buf,
89                                  size_t count)
90 {
91         return arch_cpu_release(buf, count);
92 }
93
94 static CLASS_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
95 static CLASS_ATTR(release, S_IWUSR, NULL, cpu_release_store);
96 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
97
98 #else /* ... !CONFIG_HOTPLUG_CPU */
99 static inline void register_cpu_control(struct cpu *cpu)
100 {
101 }
102 #endif /* CONFIG_HOTPLUG_CPU */
103
104 #ifdef CONFIG_KEXEC
105 #include <linux/kexec.h>
106
107 static ssize_t show_crash_notes(struct sys_device *dev, struct sysdev_attribute *attr,
108                                 char *buf)
109 {
110         struct cpu *cpu = container_of(dev, struct cpu, sysdev);
111         ssize_t rc;
112         unsigned long long addr;
113         int cpunum;
114
115         cpunum = cpu->sysdev.id;
116
117         /*
118          * Might be reading other cpu's data based on which cpu read thread
119          * has been scheduled. But cpu data (memory) is allocated once during
120          * boot up and this data does not change there after. Hence this
121          * operation should be safe. No locking required.
122          */
123         addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
124         rc = sprintf(buf, "%Lx\n", addr);
125         return rc;
126 }
127 static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
128 #endif
129
130 /*
131  * Print cpu online, possible, present, and system maps
132  */
133
134 struct cpu_attr {
135         struct sysdev_class_attribute attr;
136         const struct cpumask *const * const map;
137 };
138
139 static ssize_t show_cpus_attr(struct sysdev_class *class,
140                               struct sysdev_class_attribute *attr,
141                               char *buf)
142 {
143         struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
144         int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *(ca->map));
145
146         buf[n++] = '\n';
147         buf[n] = '\0';
148         return n;
149 }
150
151 #define _CPU_ATTR(name, map)                                            \
152         { _SYSDEV_CLASS_ATTR(name, 0444, show_cpus_attr, NULL), map }
153
154 /* Keep in sync with cpu_sysdev_class_attrs */
155 static struct cpu_attr cpu_attrs[] = {
156         _CPU_ATTR(online, &cpu_online_mask),
157         _CPU_ATTR(possible, &cpu_possible_mask),
158         _CPU_ATTR(present, &cpu_present_mask),
159 };
160
161 /*
162  * Print values for NR_CPUS and offlined cpus
163  */
164 static ssize_t print_cpus_kernel_max(struct sysdev_class *class,
165                                      struct sysdev_class_attribute *attr, char *buf)
166 {
167         int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1);
168         return n;
169 }
170 static SYSDEV_CLASS_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
171
172 /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
173 unsigned int total_cpus;
174
175 static ssize_t print_cpus_offline(struct sysdev_class *class,
176                                   struct sysdev_class_attribute *attr, char *buf)
177 {
178         int n = 0, len = PAGE_SIZE-2;
179         cpumask_var_t offline;
180
181         /* display offline cpus < nr_cpu_ids */
182         if (!alloc_cpumask_var(&offline, GFP_KERNEL))
183                 return -ENOMEM;
184         cpumask_complement(offline, cpu_online_mask);
185         n = cpulist_scnprintf(buf, len, offline);
186         free_cpumask_var(offline);
187
188         /* display offline cpus >= nr_cpu_ids */
189         if (total_cpus && nr_cpu_ids < total_cpus) {
190                 if (n && n < len)
191                         buf[n++] = ',';
192
193                 if (nr_cpu_ids == total_cpus-1)
194                         n += snprintf(&buf[n], len - n, "%d", nr_cpu_ids);
195                 else
196                         n += snprintf(&buf[n], len - n, "%d-%d",
197                                                       nr_cpu_ids, total_cpus-1);
198         }
199
200         n += snprintf(&buf[n], len - n, "\n");
201         return n;
202 }
203 static SYSDEV_CLASS_ATTR(offline, 0444, print_cpus_offline, NULL);
204
205 /*
206  * register_cpu - Setup a sysfs device for a CPU.
207  * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
208  *        sysfs for this CPU.
209  * @num - CPU number to use when creating the device.
210  *
211  * Initialize and register the CPU device.
212  */
213 int __cpuinit register_cpu(struct cpu *cpu, int num)
214 {
215         int error;
216         cpu->node_id = cpu_to_node(num);
217         cpu->sysdev.id = num;
218         cpu->sysdev.cls = &cpu_sysdev_class;
219
220         error = sysdev_register(&cpu->sysdev);
221
222         if (!error && cpu->hotpluggable)
223                 register_cpu_control(cpu);
224         if (!error)
225                 per_cpu(cpu_sys_devices, num) = &cpu->sysdev;
226         if (!error)
227                 register_cpu_under_node(num, cpu_to_node(num));
228
229 #ifdef CONFIG_KEXEC
230         if (!error)
231                 error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
232 #endif
233         return error;
234 }
235
236 struct sys_device *get_cpu_sysdev(unsigned cpu)
237 {
238         if (cpu < nr_cpu_ids && cpu_possible(cpu))
239                 return per_cpu(cpu_sys_devices, cpu);
240         else
241                 return NULL;
242 }
243 EXPORT_SYMBOL_GPL(get_cpu_sysdev);
244
245 int __init cpu_dev_init(void)
246 {
247         int err;
248
249         err = sysdev_class_register(&cpu_sysdev_class);
250 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
251         if (!err)
252                 err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class);
253 #endif
254
255         return err;
256 }
257
258 static struct sysdev_class_attribute *cpu_sysdev_class_attrs[] = {
259 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
260         &class_attr_probe.attr,
261         &class_attr_release.attr,
262 #endif
263         &cpu_attrs[0].attr,
264         &cpu_attrs[1].attr,
265         &cpu_attrs[2].attr,
266         &attr_kernel_max,
267         &attr_offline,
268         NULL
269 };