sysfs/cpu: Add probe/release files
[linux-2.6.git] / drivers / base / cpu.c
blob7c03af7b84a93c8822a8d75ef02c9cd5b0f3993f
1 /*
2 * drivers/base/cpu.c - basic CPU class support
3 */
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>
14 #include "base.h"
16 struct sysdev_class cpu_sysdev_class = {
17 .name = "cpu",
19 EXPORT_SYMBOL(cpu_sysdev_class);
21 static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices);
23 #ifdef CONFIG_HOTPLUG_CPU
24 static ssize_t show_online(struct sys_device *dev, struct sysdev_attribute *attr,
25 char *buf)
27 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
29 return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
32 static ssize_t __ref store_online(struct sys_device *dev, struct sysdev_attribute *attr,
33 const char *buf, size_t count)
35 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
36 ssize_t ret;
38 switch (buf[0]) {
39 case '0':
40 ret = cpu_down(cpu->sysdev.id);
41 if (!ret)
42 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
43 break;
44 case '1':
45 ret = cpu_up(cpu->sysdev.id);
46 if (!ret)
47 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
48 break;
49 default:
50 ret = -EINVAL;
53 if (ret >= 0)
54 ret = count;
55 return ret;
57 static SYSDEV_ATTR(online, 0644, show_online, store_online);
59 static void __cpuinit register_cpu_control(struct cpu *cpu)
61 sysdev_create_file(&cpu->sysdev, &attr_online);
63 void unregister_cpu(struct cpu *cpu)
65 int logical_cpu = cpu->sysdev.id;
67 unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
69 sysdev_remove_file(&cpu->sysdev, &attr_online);
71 sysdev_unregister(&cpu->sysdev);
72 per_cpu(cpu_sys_devices, logical_cpu) = NULL;
73 return;
76 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
77 static ssize_t cpu_probe_store(struct class *class, const char *buf,
78 size_t count)
80 return arch_cpu_probe(buf, count);
83 static ssize_t cpu_release_store(struct class *class, const char *buf,
84 size_t count)
86 return arch_cpu_release(buf, count);
89 static CLASS_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
90 static CLASS_ATTR(release, S_IWUSR, NULL, cpu_release_store);
92 int __init cpu_probe_release_init(void)
94 int rc;
96 rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
97 &class_attr_probe.attr);
98 if (!rc)
99 rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
100 &class_attr_release.attr);
102 return rc;
104 device_initcall(cpu_probe_release_init);
105 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
107 #else /* ... !CONFIG_HOTPLUG_CPU */
108 static inline void register_cpu_control(struct cpu *cpu)
111 #endif /* CONFIG_HOTPLUG_CPU */
113 #ifdef CONFIG_KEXEC
114 #include <linux/kexec.h>
116 static ssize_t show_crash_notes(struct sys_device *dev, struct sysdev_attribute *attr,
117 char *buf)
119 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
120 ssize_t rc;
121 unsigned long long addr;
122 int cpunum;
124 cpunum = cpu->sysdev.id;
127 * Might be reading other cpu's data based on which cpu read thread
128 * has been scheduled. But cpu data (memory) is allocated once during
129 * boot up and this data does not change there after. Hence this
130 * operation should be safe. No locking required.
132 addr = __pa(per_cpu_ptr(crash_notes, cpunum));
133 rc = sprintf(buf, "%Lx\n", addr);
134 return rc;
136 static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
137 #endif
140 * Print cpu online, possible, present, and system maps
142 static ssize_t print_cpus_map(char *buf, const struct cpumask *map)
144 int n = cpulist_scnprintf(buf, PAGE_SIZE-2, map);
146 buf[n++] = '\n';
147 buf[n] = '\0';
148 return n;
151 #define print_cpus_func(type) \
152 static ssize_t print_cpus_##type(struct sysdev_class *class, char *buf) \
154 return print_cpus_map(buf, cpu_##type##_mask); \
156 static struct sysdev_class_attribute attr_##type##_map = \
157 _SYSDEV_CLASS_ATTR(type, 0444, print_cpus_##type, NULL)
159 print_cpus_func(online);
160 print_cpus_func(possible);
161 print_cpus_func(present);
164 * Print values for NR_CPUS and offlined cpus
166 static ssize_t print_cpus_kernel_max(struct sysdev_class *class, char *buf)
168 int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1);
169 return n;
171 static SYSDEV_CLASS_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
173 /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
174 unsigned int total_cpus;
176 static ssize_t print_cpus_offline(struct sysdev_class *class, char *buf)
178 int n = 0, len = PAGE_SIZE-2;
179 cpumask_var_t offline;
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);
188 /* display offline cpus >= nr_cpu_ids */
189 if (total_cpus && nr_cpu_ids < total_cpus) {
190 if (n && n < len)
191 buf[n++] = ',';
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);
200 n += snprintf(&buf[n], len - n, "\n");
201 return n;
203 static SYSDEV_CLASS_ATTR(offline, 0444, print_cpus_offline, NULL);
205 static struct sysdev_class_attribute *cpu_state_attr[] = {
206 &attr_online_map,
207 &attr_possible_map,
208 &attr_present_map,
209 &attr_kernel_max,
210 &attr_offline,
213 static int cpu_states_init(void)
215 int i;
216 int err = 0;
218 for (i = 0; i < ARRAY_SIZE(cpu_state_attr); i++) {
219 int ret;
220 ret = sysdev_class_create_file(&cpu_sysdev_class,
221 cpu_state_attr[i]);
222 if (!err)
223 err = ret;
225 return err;
229 * register_cpu - Setup a sysfs device for a CPU.
230 * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
231 * sysfs for this CPU.
232 * @num - CPU number to use when creating the device.
234 * Initialize and register the CPU device.
236 int __cpuinit register_cpu(struct cpu *cpu, int num)
238 int error;
239 cpu->node_id = cpu_to_node(num);
240 cpu->sysdev.id = num;
241 cpu->sysdev.cls = &cpu_sysdev_class;
243 error = sysdev_register(&cpu->sysdev);
245 if (!error && cpu->hotpluggable)
246 register_cpu_control(cpu);
247 if (!error)
248 per_cpu(cpu_sys_devices, num) = &cpu->sysdev;
249 if (!error)
250 register_cpu_under_node(num, cpu_to_node(num));
252 #ifdef CONFIG_KEXEC
253 if (!error)
254 error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
255 #endif
256 return error;
259 struct sys_device *get_cpu_sysdev(unsigned cpu)
261 if (cpu < nr_cpu_ids && cpu_possible(cpu))
262 return per_cpu(cpu_sys_devices, cpu);
263 else
264 return NULL;
266 EXPORT_SYMBOL_GPL(get_cpu_sysdev);
268 int __init cpu_dev_init(void)
270 int err;
272 err = sysdev_class_register(&cpu_sysdev_class);
273 if (!err)
274 err = cpu_states_init();
276 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
277 if (!err)
278 err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class);
279 #endif
281 return err;