cpu: Do not return errors from cpu_dev_init() which will be ignored
[deliverable/linux.git] / drivers / base / cpu.c
CommitLineData
1da177e4 1/*
8a25a2fd 2 * CPU subsystem support
1da177e4
LT
3 */
4
024f7846 5#include <linux/kernel.h>
1da177e4
LT
6#include <linux/module.h>
7#include <linux/init.h>
f6a57033 8#include <linux/sched.h>
1da177e4
LT
9#include <linux/cpu.h>
10#include <linux/topology.h>
11#include <linux/device.h>
76b67ed9 12#include <linux/node.h>
5a0e3ad6 13#include <linux/gfp.h>
1da177e4 14
a1bdc7aa 15#include "base.h"
1da177e4 16
8a25a2fd 17struct bus_type cpu_subsys = {
af5ca3f4 18 .name = "cpu",
8a25a2fd 19 .dev_name = "cpu",
1da177e4 20};
8a25a2fd 21EXPORT_SYMBOL_GPL(cpu_subsys);
1da177e4 22
8a25a2fd 23static DEFINE_PER_CPU(struct device *, cpu_sys_devices);
ad74557a 24
1da177e4 25#ifdef CONFIG_HOTPLUG_CPU
8a25a2fd
KS
26static ssize_t show_online(struct device *dev,
27 struct device_attribute *attr,
4a0b2b4d 28 char *buf)
1da177e4 29{
8a25a2fd 30 struct cpu *cpu = container_of(dev, struct cpu, dev);
1da177e4 31
8a25a2fd 32 return sprintf(buf, "%u\n", !!cpu_online(cpu->dev.id));
1da177e4
LT
33}
34
8a25a2fd
KS
35static ssize_t __ref store_online(struct device *dev,
36 struct device_attribute *attr,
37 const char *buf, size_t count)
1da177e4 38{
8a25a2fd 39 struct cpu *cpu = container_of(dev, struct cpu, dev);
1da177e4
LT
40 ssize_t ret;
41
51badebd 42 cpu_hotplug_driver_lock();
1da177e4
LT
43 switch (buf[0]) {
44 case '0':
8a25a2fd 45 ret = cpu_down(cpu->dev.id);
1da177e4 46 if (!ret)
312c004d 47 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1da177e4
LT
48 break;
49 case '1':
8a25a2fd 50 ret = cpu_up(cpu->dev.id);
fb69c390 51 if (!ret)
312c004d 52 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1da177e4
LT
53 break;
54 default:
55 ret = -EINVAL;
56 }
51badebd 57 cpu_hotplug_driver_unlock();
1da177e4
LT
58
59 if (ret >= 0)
60 ret = count;
61 return ret;
62}
8a25a2fd 63static DEVICE_ATTR(online, 0644, show_online, store_online);
1da177e4 64
6c847402 65static void __cpuinit register_cpu_control(struct cpu *cpu)
1da177e4 66{
8a25a2fd 67 device_create_file(&cpu->dev, &dev_attr_online);
1da177e4 68}
76b67ed9 69void unregister_cpu(struct cpu *cpu)
1da177e4 70{
8a25a2fd 71 int logical_cpu = cpu->dev.id;
1da177e4 72
76b67ed9
KH
73 unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
74
8a25a2fd 75 device_remove_file(&cpu->dev, &dev_attr_online);
1da177e4 76
8a25a2fd 77 device_unregister(&cpu->dev);
e37d05da 78 per_cpu(cpu_sys_devices, logical_cpu) = NULL;
1da177e4
LT
79 return;
80}
12633e80
NF
81
82#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
8a25a2fd
KS
83static ssize_t cpu_probe_store(struct device *dev,
84 struct device_attribute *attr,
67fc233f 85 const char *buf,
12633e80
NF
86 size_t count)
87{
88 return arch_cpu_probe(buf, count);
89}
90
8a25a2fd
KS
91static ssize_t cpu_release_store(struct device *dev,
92 struct device_attribute *attr,
67fc233f 93 const char *buf,
12633e80
NF
94 size_t count)
95{
96 return arch_cpu_release(buf, count);
97}
98
8a25a2fd
KS
99static DEVICE_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
100static DEVICE_ATTR(release, S_IWUSR, NULL, cpu_release_store);
12633e80
NF
101#endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
102
1da177e4
LT
103#else /* ... !CONFIG_HOTPLUG_CPU */
104static inline void register_cpu_control(struct cpu *cpu)
105{
106}
107#endif /* CONFIG_HOTPLUG_CPU */
108
51be5606
VG
109#ifdef CONFIG_KEXEC
110#include <linux/kexec.h>
111
8a25a2fd 112static ssize_t show_crash_notes(struct device *dev, struct device_attribute *attr,
4a0b2b4d 113 char *buf)
51be5606 114{
8a25a2fd 115 struct cpu *cpu = container_of(dev, struct cpu, dev);
51be5606
VG
116 ssize_t rc;
117 unsigned long long addr;
118 int cpunum;
119
8a25a2fd 120 cpunum = cpu->dev.id;
51be5606
VG
121
122 /*
123 * Might be reading other cpu's data based on which cpu read thread
124 * has been scheduled. But cpu data (memory) is allocated once during
125 * boot up and this data does not change there after. Hence this
126 * operation should be safe. No locking required.
127 */
3b034b0d 128 addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
51be5606 129 rc = sprintf(buf, "%Lx\n", addr);
51be5606
VG
130 return rc;
131}
8a25a2fd 132static DEVICE_ATTR(crash_notes, 0400, show_crash_notes, NULL);
51be5606
VG
133#endif
134
9d1fe323
MT
135/*
136 * Print cpu online, possible, present, and system maps
137 */
265d2e2e
AK
138
139struct cpu_attr {
8a25a2fd 140 struct device_attribute attr;
265d2e2e
AK
141 const struct cpumask *const * const map;
142};
143
8a25a2fd
KS
144static ssize_t show_cpus_attr(struct device *dev,
145 struct device_attribute *attr,
265d2e2e 146 char *buf)
9d1fe323 147{
265d2e2e
AK
148 struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
149 int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *(ca->map));
9d1fe323
MT
150
151 buf[n++] = '\n';
152 buf[n] = '\0';
153 return n;
154}
155
8a25a2fd
KS
156#define _CPU_ATTR(name, map) \
157 { __ATTR(name, 0444, show_cpus_attr, NULL), map }
9d1fe323 158
8a25a2fd 159/* Keep in sync with cpu_subsys_attrs */
265d2e2e
AK
160static struct cpu_attr cpu_attrs[] = {
161 _CPU_ATTR(online, &cpu_online_mask),
162 _CPU_ATTR(possible, &cpu_possible_mask),
163 _CPU_ATTR(present, &cpu_present_mask),
164};
9d1fe323 165
e057d7ae
MT
166/*
167 * Print values for NR_CPUS and offlined cpus
168 */
8a25a2fd
KS
169static ssize_t print_cpus_kernel_max(struct device *dev,
170 struct device_attribute *attr, char *buf)
e057d7ae 171{
8fd2d2d5 172 int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1);
e057d7ae
MT
173 return n;
174}
8a25a2fd 175static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
e057d7ae
MT
176
177/* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
178unsigned int total_cpus;
179
8a25a2fd
KS
180static ssize_t print_cpus_offline(struct device *dev,
181 struct device_attribute *attr, char *buf)
e057d7ae
MT
182{
183 int n = 0, len = PAGE_SIZE-2;
184 cpumask_var_t offline;
185
186 /* display offline cpus < nr_cpu_ids */
187 if (!alloc_cpumask_var(&offline, GFP_KERNEL))
188 return -ENOMEM;
cdc6e3d3 189 cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask);
e057d7ae
MT
190 n = cpulist_scnprintf(buf, len, offline);
191 free_cpumask_var(offline);
192
193 /* display offline cpus >= nr_cpu_ids */
194 if (total_cpus && nr_cpu_ids < total_cpus) {
195 if (n && n < len)
196 buf[n++] = ',';
197
198 if (nr_cpu_ids == total_cpus-1)
199 n += snprintf(&buf[n], len - n, "%d", nr_cpu_ids);
200 else
201 n += snprintf(&buf[n], len - n, "%d-%d",
202 nr_cpu_ids, total_cpus-1);
203 }
204
205 n += snprintf(&buf[n], len - n, "\n");
206 return n;
207}
8a25a2fd 208static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
e057d7ae 209
1da177e4 210/*
405ae7d3 211 * register_cpu - Setup a sysfs device for a CPU.
72486f1f
SS
212 * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
213 * sysfs for this CPU.
1da177e4
LT
214 * @num - CPU number to use when creating the device.
215 *
216 * Initialize and register the CPU device.
217 */
33b5f31b 218int __cpuinit register_cpu(struct cpu *cpu, int num)
1da177e4
LT
219{
220 int error;
76b67ed9 221
8a25a2fd
KS
222 cpu->node_id = cpu_to_node(num);
223 cpu->dev.id = num;
224 cpu->dev.bus = &cpu_subsys;
225 error = device_register(&cpu->dev);
72486f1f 226 if (!error && cpu->hotpluggable)
1da177e4 227 register_cpu_control(cpu);
ad74557a 228 if (!error)
8a25a2fd 229 per_cpu(cpu_sys_devices, num) = &cpu->dev;
76b67ed9
KH
230 if (!error)
231 register_cpu_under_node(num, cpu_to_node(num));
51be5606
VG
232
233#ifdef CONFIG_KEXEC
234 if (!error)
8a25a2fd 235 error = device_create_file(&cpu->dev, &dev_attr_crash_notes);
51be5606 236#endif
1da177e4
LT
237 return error;
238}
239
8a25a2fd 240struct device *get_cpu_device(unsigned cpu)
ad74557a 241{
e37d05da
MT
242 if (cpu < nr_cpu_ids && cpu_possible(cpu))
243 return per_cpu(cpu_sys_devices, cpu);
ad74557a
AR
244 else
245 return NULL;
246}
8a25a2fd
KS
247EXPORT_SYMBOL_GPL(get_cpu_device);
248
249static struct attribute *cpu_root_attrs[] = {
250#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
251 &dev_attr_probe.attr,
252 &dev_attr_release.attr,
253#endif
254 &cpu_attrs[0].attr.attr,
255 &cpu_attrs[1].attr.attr,
256 &cpu_attrs[2].attr.attr,
257 &dev_attr_kernel_max.attr,
258 &dev_attr_offline.attr,
259 NULL
260};
261
262static struct attribute_group cpu_root_attr_group = {
263 .attrs = cpu_root_attrs,
264};
265
266static const struct attribute_group *cpu_root_attr_groups[] = {
267 &cpu_root_attr_group,
268 NULL,
269};
1da177e4 270
2987557f
JT
271bool cpu_is_hotpluggable(unsigned cpu)
272{
7affca35
LT
273 struct device *dev = get_cpu_device(cpu);
274 return dev && container_of(dev, struct cpu, dev)->hotpluggable;
2987557f
JT
275}
276EXPORT_SYMBOL_GPL(cpu_is_hotpluggable);
277
024f7846 278void __init cpu_dev_init(void)
1da177e4 279{
024f7846
BH
280 if (subsys_system_register(&cpu_subsys, cpu_root_attr_groups))
281 panic("Failed to register CPU subsystem");
8a25a2fd 282
5c45bf27 283#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
024f7846 284 sched_create_sysfs_power_savings_entries(cpu_subsys.dev_root);
5c45bf27 285#endif
1da177e4 286}
This page took 0.662857 seconds and 5 git commands to generate.