]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/base/cpu.c
pseries/pseries: Add code to online/offline CPUs of a DLPAR node
[mirror_ubuntu-zesty-kernel.git] / drivers / base / cpu.c
CommitLineData
1da177e4
LT
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>
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>
1da177e4 13
a1bdc7aa 14#include "base.h"
1da177e4
LT
15
16struct sysdev_class cpu_sysdev_class = {
af5ca3f4 17 .name = "cpu",
1da177e4
LT
18};
19EXPORT_SYMBOL(cpu_sysdev_class);
20
e37d05da 21static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices);
ad74557a 22
1da177e4 23#ifdef CONFIG_HOTPLUG_CPU
4a0b2b4d
AK
24static ssize_t show_online(struct sys_device *dev, struct sysdev_attribute *attr,
25 char *buf)
1da177e4
LT
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
4a0b2b4d
AK
32static ssize_t __ref store_online(struct sys_device *dev, struct sysdev_attribute *attr,
33 const char *buf, size_t count)
1da177e4
LT
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)
312c004d 42 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1da177e4
LT
43 break;
44 case '1':
34f361ad 45 ret = cpu_up(cpu->sysdev.id);
fb69c390 46 if (!ret)
312c004d 47 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1da177e4
LT
48 break;
49 default:
50 ret = -EINVAL;
51 }
52
53 if (ret >= 0)
54 ret = count;
55 return ret;
56}
9eb3ff40 57static SYSDEV_ATTR(online, 0644, show_online, store_online);
1da177e4 58
6c847402 59static void __cpuinit register_cpu_control(struct cpu *cpu)
1da177e4
LT
60{
61 sysdev_create_file(&cpu->sysdev, &attr_online);
62}
76b67ed9 63void unregister_cpu(struct cpu *cpu)
1da177e4 64{
ad74557a 65 int logical_cpu = cpu->sysdev.id;
1da177e4 66
76b67ed9
KH
67 unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
68
1da177e4
LT
69 sysdev_remove_file(&cpu->sysdev, &attr_online);
70
71 sysdev_unregister(&cpu->sysdev);
e37d05da 72 per_cpu(cpu_sys_devices, logical_cpu) = NULL;
1da177e4
LT
73 return;
74}
12633e80
NF
75
76#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
77static ssize_t cpu_probe_store(struct class *class, const char *buf,
78 size_t count)
79{
80 return arch_cpu_probe(buf, count);
81}
82
83static ssize_t cpu_release_store(struct class *class, const char *buf,
84 size_t count)
85{
86 return arch_cpu_release(buf, count);
87}
88
89static CLASS_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
90static CLASS_ATTR(release, S_IWUSR, NULL, cpu_release_store);
91
92int __init cpu_probe_release_init(void)
93{
94 int rc;
95
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);
101
102 return rc;
103}
104device_initcall(cpu_probe_release_init);
105#endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
106
1da177e4
LT
107#else /* ... !CONFIG_HOTPLUG_CPU */
108static inline void register_cpu_control(struct cpu *cpu)
109{
110}
111#endif /* CONFIG_HOTPLUG_CPU */
112
51be5606
VG
113#ifdef CONFIG_KEXEC
114#include <linux/kexec.h>
115
4a0b2b4d
AK
116static ssize_t show_crash_notes(struct sys_device *dev, struct sysdev_attribute *attr,
117 char *buf)
51be5606
VG
118{
119 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
120 ssize_t rc;
121 unsigned long long addr;
122 int cpunum;
123
124 cpunum = cpu->sysdev.id;
125
126 /*
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.
131 */
51be5606
VG
132 addr = __pa(per_cpu_ptr(crash_notes, cpunum));
133 rc = sprintf(buf, "%Lx\n", addr);
51be5606
VG
134 return rc;
135}
136static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
137#endif
138
9d1fe323
MT
139/*
140 * Print cpu online, possible, present, and system maps
141 */
f7df8ed1 142static ssize_t print_cpus_map(char *buf, const struct cpumask *map)
9d1fe323 143{
29c0177e 144 int n = cpulist_scnprintf(buf, PAGE_SIZE-2, map);
9d1fe323
MT
145
146 buf[n++] = '\n';
147 buf[n] = '\0';
148 return n;
149}
150
151#define print_cpus_func(type) \
152static ssize_t print_cpus_##type(struct sysdev_class *class, char *buf) \
153{ \
aa85ea5b 154 return print_cpus_map(buf, cpu_##type##_mask); \
9d1fe323 155} \
143aa5c5 156static struct sysdev_class_attribute attr_##type##_map = \
9d1fe323
MT
157 _SYSDEV_CLASS_ATTR(type, 0444, print_cpus_##type, NULL)
158
159print_cpus_func(online);
160print_cpus_func(possible);
161print_cpus_func(present);
162
e057d7ae
MT
163/*
164 * Print values for NR_CPUS and offlined cpus
165 */
166static ssize_t print_cpus_kernel_max(struct sysdev_class *class, char *buf)
167{
8fd2d2d5 168 int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1);
e057d7ae
MT
169 return n;
170}
171static SYSDEV_CLASS_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
172
173/* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
174unsigned int total_cpus;
175
176static ssize_t print_cpus_offline(struct sysdev_class *class, 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}
203static SYSDEV_CLASS_ATTR(offline, 0444, print_cpus_offline, NULL);
204
143aa5c5 205static struct sysdev_class_attribute *cpu_state_attr[] = {
9d1fe323
MT
206 &attr_online_map,
207 &attr_possible_map,
208 &attr_present_map,
e057d7ae
MT
209 &attr_kernel_max,
210 &attr_offline,
9d1fe323
MT
211};
212
213static int cpu_states_init(void)
214{
215 int i;
216 int err = 0;
217
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;
224 }
225 return err;
226}
227
1da177e4 228/*
405ae7d3 229 * register_cpu - Setup a sysfs device for a CPU.
72486f1f
SS
230 * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
231 * sysfs for this CPU.
1da177e4
LT
232 * @num - CPU number to use when creating the device.
233 *
234 * Initialize and register the CPU device.
235 */
33b5f31b 236int __cpuinit register_cpu(struct cpu *cpu, int num)
1da177e4
LT
237{
238 int error;
1da177e4
LT
239 cpu->node_id = cpu_to_node(num);
240 cpu->sysdev.id = num;
241 cpu->sysdev.cls = &cpu_sysdev_class;
242
243 error = sysdev_register(&cpu->sysdev);
76b67ed9 244
72486f1f 245 if (!error && cpu->hotpluggable)
1da177e4 246 register_cpu_control(cpu);
ad74557a 247 if (!error)
e37d05da 248 per_cpu(cpu_sys_devices, num) = &cpu->sysdev;
76b67ed9
KH
249 if (!error)
250 register_cpu_under_node(num, cpu_to_node(num));
51be5606
VG
251
252#ifdef CONFIG_KEXEC
253 if (!error)
254 error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
255#endif
1da177e4
LT
256 return error;
257}
258
a29d642a 259struct sys_device *get_cpu_sysdev(unsigned cpu)
ad74557a 260{
e37d05da
MT
261 if (cpu < nr_cpu_ids && cpu_possible(cpu))
262 return per_cpu(cpu_sys_devices, cpu);
ad74557a
AR
263 else
264 return NULL;
265}
266EXPORT_SYMBOL_GPL(get_cpu_sysdev);
1da177e4
LT
267
268int __init cpu_dev_init(void)
269{
5c45bf27
SS
270 int err;
271
272 err = sysdev_class_register(&cpu_sysdev_class);
9d1fe323
MT
273 if (!err)
274 err = cpu_states_init();
275
5c45bf27
SS
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
280
281 return err;
1da177e4 282}