blob: b04ecfc63b514c64345fc36ab13bd8e786ff85ee [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>
Al Virof6a57032006-10-18 01:47:25 -04008#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/cpu.h>
10#include <linux/topology.h>
11#include <linux/device.h>
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070012#include <linux/node.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010014#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16struct sysdev_class cpu_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +010017 .name = "cpu",
Linus Torvalds1da177e2005-04-16 15:20:36 -070018};
19EXPORT_SYMBOL(cpu_sysdev_class);
20
Mike Travise37d05d2008-05-01 04:35:16 -070021static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices);
Ashok Rajad745572005-10-30 14:59:49 -080022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#ifdef CONFIG_HOTPLUG_CPU
Andi Kleen4a0b2b42008-07-01 18:48:41 +020024static ssize_t show_online(struct sys_device *dev, struct sysdev_attribute *attr,
25 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
27 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
28
29 return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
30}
31
Andi Kleen4a0b2b42008-07-01 18:48:41 +020032static ssize_t __ref store_online(struct sys_device *dev, struct sysdev_attribute *attr,
33 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
36 ssize_t ret;
37
Gautham R Shenoy51badeb2009-11-26 09:59:05 +000038 cpu_hotplug_driver_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 switch (buf[0]) {
40 case '0':
41 ret = cpu_down(cpu->sysdev.id);
42 if (!ret)
Kay Sievers312c0042005-11-16 09:00:00 +010043 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 break;
45 case '1':
Ashok Raj34f361a2006-03-25 03:08:18 -080046 ret = cpu_up(cpu->sysdev.id);
Nathan Lynchfb69c392005-06-25 14:55:05 -070047 if (!ret)
Kay Sievers312c0042005-11-16 09:00:00 +010048 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 break;
50 default:
51 ret = -EINVAL;
52 }
Gautham R Shenoy51badeb2009-11-26 09:59:05 +000053 cpu_hotplug_driver_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 if (ret >= 0)
56 ret = count;
57 return ret;
58}
Ulrich Drepper9eb3ff42007-07-31 00:38:16 -070059static SYSDEV_ATTR(online, 0644, show_online, store_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Sam Ravnborg6c847402008-03-04 15:09:05 -080061static void __cpuinit register_cpu_control(struct cpu *cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 sysdev_create_file(&cpu->sysdev, &attr_online);
64}
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070065void unregister_cpu(struct cpu *cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Ashok Rajad745572005-10-30 14:59:49 -080067 int logical_cpu = cpu->sysdev.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070069 unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 sysdev_remove_file(&cpu->sysdev, &attr_online);
72
73 sysdev_unregister(&cpu->sysdev);
Mike Travise37d05d2008-05-01 04:35:16 -070074 per_cpu(cpu_sys_devices, logical_cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return;
76}
Nathan Fontenot12633e82009-11-25 17:23:25 +000077
78#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
79static ssize_t cpu_probe_store(struct class *class, const char *buf,
80 size_t count)
81{
82 return arch_cpu_probe(buf, count);
83}
84
85static ssize_t cpu_release_store(struct class *class, const char *buf,
86 size_t count)
87{
88 return arch_cpu_release(buf, count);
89}
90
91static CLASS_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
92static CLASS_ATTR(release, S_IWUSR, NULL, cpu_release_store);
93
94int __init cpu_probe_release_init(void)
95{
96 int rc;
97
98 rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
99 &class_attr_probe.attr);
100 if (!rc)
101 rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
102 &class_attr_release.attr);
103
104 return rc;
105}
106device_initcall(cpu_probe_release_init);
107#endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#else /* ... !CONFIG_HOTPLUG_CPU */
110static inline void register_cpu_control(struct cpu *cpu)
111{
112}
113#endif /* CONFIG_HOTPLUG_CPU */
114
Vivek Goyal51be5602006-01-09 20:51:42 -0800115#ifdef CONFIG_KEXEC
116#include <linux/kexec.h>
117
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200118static ssize_t show_crash_notes(struct sys_device *dev, struct sysdev_attribute *attr,
119 char *buf)
Vivek Goyal51be5602006-01-09 20:51:42 -0800120{
121 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
122 ssize_t rc;
123 unsigned long long addr;
124 int cpunum;
125
126 cpunum = cpu->sysdev.id;
127
128 /*
129 * Might be reading other cpu's data based on which cpu read thread
130 * has been scheduled. But cpu data (memory) is allocated once during
131 * boot up and this data does not change there after. Hence this
132 * operation should be safe. No locking required.
133 */
Vivek Goyal3b034b02009-11-24 15:50:03 +0900134 addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
Vivek Goyal51be5602006-01-09 20:51:42 -0800135 rc = sprintf(buf, "%Lx\n", addr);
Vivek Goyal51be5602006-01-09 20:51:42 -0800136 return rc;
137}
138static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
139#endif
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
Mike Travis9d1fe3232008-04-08 11:43:04 -0700142 * Print cpu online, possible, present, and system maps
143 */
Andi Kleen265d2e22010-01-05 12:48:00 +0100144
145struct cpu_attr {
146 struct sysdev_class_attribute attr;
147 const struct cpumask *const * const map;
148};
149
150static ssize_t show_cpus_attr(struct sysdev_class *class,
151 struct sysdev_class_attribute *attr,
152 char *buf)
Mike Travis9d1fe3232008-04-08 11:43:04 -0700153{
Andi Kleen265d2e22010-01-05 12:48:00 +0100154 struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
155 int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *(ca->map));
Mike Travis9d1fe3232008-04-08 11:43:04 -0700156
157 buf[n++] = '\n';
158 buf[n] = '\0';
159 return n;
160}
161
Andi Kleen265d2e22010-01-05 12:48:00 +0100162#define _CPU_ATTR(name, map) \
163 { _SYSDEV_CLASS_ATTR(name, 0444, show_cpus_attr, NULL), map }
Mike Travis9d1fe3232008-04-08 11:43:04 -0700164
Andi Kleen265d2e22010-01-05 12:48:00 +0100165static struct cpu_attr cpu_attrs[] = {
166 _CPU_ATTR(online, &cpu_online_mask),
167 _CPU_ATTR(possible, &cpu_possible_mask),
168 _CPU_ATTR(present, &cpu_present_mask),
169};
Mike Travis9d1fe3232008-04-08 11:43:04 -0700170
Mike Travise057d7a2008-12-15 20:26:48 -0800171/*
172 * Print values for NR_CPUS and offlined cpus
173 */
Andi Kleenc9be0a32010-01-05 12:47:58 +0100174static ssize_t print_cpus_kernel_max(struct sysdev_class *class,
175 struct sysdev_class_attribute *attr, char *buf)
Mike Travise057d7a2008-12-15 20:26:48 -0800176{
Mike Travis8fd2d2d2008-12-31 18:08:48 -0800177 int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1);
Mike Travise057d7a2008-12-15 20:26:48 -0800178 return n;
179}
180static SYSDEV_CLASS_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
181
182/* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
183unsigned int total_cpus;
184
Andi Kleenc9be0a32010-01-05 12:47:58 +0100185static ssize_t print_cpus_offline(struct sysdev_class *class,
186 struct sysdev_class_attribute *attr, char *buf)
Mike Travise057d7a2008-12-15 20:26:48 -0800187{
188 int n = 0, len = PAGE_SIZE-2;
189 cpumask_var_t offline;
190
191 /* display offline cpus < nr_cpu_ids */
192 if (!alloc_cpumask_var(&offline, GFP_KERNEL))
193 return -ENOMEM;
194 cpumask_complement(offline, cpu_online_mask);
195 n = cpulist_scnprintf(buf, len, offline);
196 free_cpumask_var(offline);
197
198 /* display offline cpus >= nr_cpu_ids */
199 if (total_cpus && nr_cpu_ids < total_cpus) {
200 if (n && n < len)
201 buf[n++] = ',';
202
203 if (nr_cpu_ids == total_cpus-1)
204 n += snprintf(&buf[n], len - n, "%d", nr_cpu_ids);
205 else
206 n += snprintf(&buf[n], len - n, "%d-%d",
207 nr_cpu_ids, total_cpus-1);
208 }
209
210 n += snprintf(&buf[n], len - n, "\n");
211 return n;
212}
213static SYSDEV_CLASS_ATTR(offline, 0444, print_cpus_offline, NULL);
214
Mike Travis143aa5c2008-05-12 21:21:13 +0200215static struct sysdev_class_attribute *cpu_state_attr[] = {
Andi Kleen265d2e22010-01-05 12:48:00 +0100216 &cpu_attrs[0].attr,
217 &cpu_attrs[1].attr,
218 &cpu_attrs[2].attr,
Mike Travise057d7a2008-12-15 20:26:48 -0800219 &attr_kernel_max,
220 &attr_offline,
Mike Travis9d1fe3232008-04-08 11:43:04 -0700221};
222
223static int cpu_states_init(void)
224{
225 int i;
226 int err = 0;
227
228 for (i = 0; i < ARRAY_SIZE(cpu_state_attr); i++) {
229 int ret;
230 ret = sysdev_class_create_file(&cpu_sysdev_class,
231 cpu_state_attr[i]);
232 if (!err)
233 err = ret;
234 }
235 return err;
236}
237
238/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100239 * register_cpu - Setup a sysfs device for a CPU.
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100240 * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
241 * sysfs for this CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * @num - CPU number to use when creating the device.
243 *
244 * Initialize and register the CPU device.
245 */
Randy Dunlap33b5f312008-02-06 01:36:28 -0800246int __cpuinit register_cpu(struct cpu *cpu, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 cpu->node_id = cpu_to_node(num);
250 cpu->sysdev.id = num;
251 cpu->sysdev.cls = &cpu_sysdev_class;
252
253 error = sysdev_register(&cpu->sysdev);
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700254
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100255 if (!error && cpu->hotpluggable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 register_cpu_control(cpu);
Ashok Rajad745572005-10-30 14:59:49 -0800257 if (!error)
Mike Travise37d05d2008-05-01 04:35:16 -0700258 per_cpu(cpu_sys_devices, num) = &cpu->sysdev;
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700259 if (!error)
260 register_cpu_under_node(num, cpu_to_node(num));
Vivek Goyal51be5602006-01-09 20:51:42 -0800261
262#ifdef CONFIG_KEXEC
263 if (!error)
264 error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
265#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return error;
267}
268
Andrew Mortona29d6422006-03-07 23:53:25 -0800269struct sys_device *get_cpu_sysdev(unsigned cpu)
Ashok Rajad745572005-10-30 14:59:49 -0800270{
Mike Travise37d05d2008-05-01 04:35:16 -0700271 if (cpu < nr_cpu_ids && cpu_possible(cpu))
272 return per_cpu(cpu_sys_devices, cpu);
Ashok Rajad745572005-10-30 14:59:49 -0800273 else
274 return NULL;
275}
276EXPORT_SYMBOL_GPL(get_cpu_sysdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278int __init cpu_dev_init(void)
279{
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700280 int err;
281
282 err = sysdev_class_register(&cpu_sysdev_class);
Mike Travis9d1fe3232008-04-08 11:43:04 -0700283 if (!err)
284 err = cpu_states_init();
285
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700286#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
287 if (!err)
288 err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class);
289#endif
290
291 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}