blob: 251acea3d35986dc13f3b5f8423eb803d37508f4 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Ben Dooksa1bdc7a2005-10-13 17:54:41 +010015#include "base.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Andi Kleene1a7e292010-01-05 12:48:03 +010017static struct sysdev_class_attribute *cpu_sysdev_class_attrs[];
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019struct sysdev_class cpu_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +010020 .name = "cpu",
Andi Kleene1a7e292010-01-05 12:48:03 +010021 .attrs = cpu_sysdev_class_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070022};
23EXPORT_SYMBOL(cpu_sysdev_class);
24
Mike Travise37d05d2008-05-01 04:35:16 -070025static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices);
Ashok Rajad745572005-10-30 14:59:49 -080026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#ifdef CONFIG_HOTPLUG_CPU
Andi Kleen4a0b2b42008-07-01 18:48:41 +020028static ssize_t show_online(struct sys_device *dev, struct sysdev_attribute *attr,
29 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
32
33 return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
34}
35
Andi Kleen4a0b2b42008-07-01 18:48:41 +020036static ssize_t __ref store_online(struct sys_device *dev, struct sysdev_attribute *attr,
37 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
40 ssize_t ret;
41
Gautham R Shenoy51badeb2009-11-26 09:59:05 +000042 cpu_hotplug_driver_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 switch (buf[0]) {
44 case '0':
45 ret = cpu_down(cpu->sysdev.id);
46 if (!ret)
Kay Sievers312c0042005-11-16 09:00:00 +010047 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 break;
49 case '1':
Ashok Raj34f361a2006-03-25 03:08:18 -080050 ret = cpu_up(cpu->sysdev.id);
Nathan Lynchfb69c392005-06-25 14:55:05 -070051 if (!ret)
Kay Sievers312c0042005-11-16 09:00:00 +010052 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 break;
54 default:
55 ret = -EINVAL;
56 }
Gautham R Shenoy51badeb2009-11-26 09:59:05 +000057 cpu_hotplug_driver_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 if (ret >= 0)
60 ret = count;
61 return ret;
62}
Ulrich Drepper9eb3ff42007-07-31 00:38:16 -070063static SYSDEV_ATTR(online, 0644, show_online, store_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Sam Ravnborg6c847402008-03-04 15:09:05 -080065static void __cpuinit register_cpu_control(struct cpu *cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 sysdev_create_file(&cpu->sysdev, &attr_online);
68}
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070069void unregister_cpu(struct cpu *cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Ashok Rajad745572005-10-30 14:59:49 -080071 int logical_cpu = cpu->sysdev.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070073 unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 sysdev_remove_file(&cpu->sysdev, &attr_online);
76
77 sysdev_unregister(&cpu->sysdev);
Mike Travise37d05d2008-05-01 04:35:16 -070078 per_cpu(cpu_sys_devices, logical_cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return;
80}
Nathan Fontenot12633e82009-11-25 17:23:25 +000081
82#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
Stephen Rothwell67fc2332010-03-16 10:33:32 +110083static ssize_t cpu_probe_store(struct sysdev_class *class,
84 struct sysdev_class_attribute *attr,
85 const char *buf,
Nathan Fontenot12633e82009-11-25 17:23:25 +000086 size_t count)
87{
88 return arch_cpu_probe(buf, count);
89}
90
Stephen Rothwell67fc2332010-03-16 10:33:32 +110091static ssize_t cpu_release_store(struct sysdev_class *class,
92 struct sysdev_class_attribute *attr,
93 const char *buf,
Nathan Fontenot12633e82009-11-25 17:23:25 +000094 size_t count)
95{
96 return arch_cpu_release(buf, count);
97}
98
Stephen Rothwell67fc2332010-03-16 10:33:32 +110099static SYSDEV_CLASS_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
100static SYSDEV_CLASS_ATTR(release, S_IWUSR, NULL, cpu_release_store);
Nathan Fontenot12633e82009-11-25 17:23:25 +0000101#endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#else /* ... !CONFIG_HOTPLUG_CPU */
104static inline void register_cpu_control(struct cpu *cpu)
105{
106}
107#endif /* CONFIG_HOTPLUG_CPU */
108
Vivek Goyal51be5602006-01-09 20:51:42 -0800109#ifdef CONFIG_KEXEC
110#include <linux/kexec.h>
111
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200112static ssize_t show_crash_notes(struct sys_device *dev, struct sysdev_attribute *attr,
113 char *buf)
Vivek Goyal51be5602006-01-09 20:51:42 -0800114{
115 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
116 ssize_t rc;
117 unsigned long long addr;
118 int cpunum;
119
120 cpunum = cpu->sysdev.id;
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 */
Vivek Goyal3b034b02009-11-24 15:50:03 +0900128 addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
Vivek Goyal51be5602006-01-09 20:51:42 -0800129 rc = sprintf(buf, "%Lx\n", addr);
Vivek Goyal51be5602006-01-09 20:51:42 -0800130 return rc;
131}
132static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
133#endif
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
Mike Travis9d1fe3232008-04-08 11:43:04 -0700136 * Print cpu online, possible, present, and system maps
137 */
Andi Kleen265d2e22010-01-05 12:48:00 +0100138
139struct cpu_attr {
140 struct sysdev_class_attribute attr;
141 const struct cpumask *const * const map;
142};
143
144static ssize_t show_cpus_attr(struct sysdev_class *class,
145 struct sysdev_class_attribute *attr,
146 char *buf)
Mike Travis9d1fe3232008-04-08 11:43:04 -0700147{
Andi Kleen265d2e22010-01-05 12:48:00 +0100148 struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
149 int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *(ca->map));
Mike Travis9d1fe3232008-04-08 11:43:04 -0700150
151 buf[n++] = '\n';
152 buf[n] = '\0';
153 return n;
154}
155
Andi Kleen265d2e22010-01-05 12:48:00 +0100156#define _CPU_ATTR(name, map) \
157 { _SYSDEV_CLASS_ATTR(name, 0444, show_cpus_attr, NULL), map }
Mike Travis9d1fe3232008-04-08 11:43:04 -0700158
Andi Kleene1a7e292010-01-05 12:48:03 +0100159/* Keep in sync with cpu_sysdev_class_attrs */
Andi Kleen265d2e22010-01-05 12:48:00 +0100160static 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};
Mike Travis9d1fe3232008-04-08 11:43:04 -0700165
Mike Travise057d7a2008-12-15 20:26:48 -0800166/*
167 * Print values for NR_CPUS and offlined cpus
168 */
Andi Kleenc9be0a32010-01-05 12:47:58 +0100169static ssize_t print_cpus_kernel_max(struct sysdev_class *class,
170 struct sysdev_class_attribute *attr, char *buf)
Mike Travise057d7a2008-12-15 20:26:48 -0800171{
Mike Travis8fd2d2d2008-12-31 18:08:48 -0800172 int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1);
Mike Travise057d7a2008-12-15 20:26:48 -0800173 return n;
174}
175static SYSDEV_CLASS_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
176
177/* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
178unsigned int total_cpus;
179
Andi Kleenc9be0a32010-01-05 12:47:58 +0100180static ssize_t print_cpus_offline(struct sysdev_class *class,
181 struct sysdev_class_attribute *attr, char *buf)
Mike Travise057d7a2008-12-15 20:26:48 -0800182{
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;
Jan Beulichcdc6e3d2010-04-27 14:01:20 -0700189 cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask);
Mike Travise057d7a2008-12-15 20:26:48 -0800190 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}
208static SYSDEV_CLASS_ATTR(offline, 0444, print_cpus_offline, NULL);
209
Mike Travis9d1fe3232008-04-08 11:43:04 -0700210/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100211 * register_cpu - Setup a sysfs device for a CPU.
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100212 * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
213 * sysfs for this CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * @num - CPU number to use when creating the device.
215 *
216 * Initialize and register the CPU device.
217 */
Randy Dunlap33b5f312008-02-06 01:36:28 -0800218int __cpuinit register_cpu(struct cpu *cpu, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 cpu->node_id = cpu_to_node(num);
222 cpu->sysdev.id = num;
223 cpu->sysdev.cls = &cpu_sysdev_class;
224
225 error = sysdev_register(&cpu->sysdev);
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700226
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100227 if (!error && cpu->hotpluggable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 register_cpu_control(cpu);
Ashok Rajad745572005-10-30 14:59:49 -0800229 if (!error)
Mike Travise37d05d2008-05-01 04:35:16 -0700230 per_cpu(cpu_sys_devices, num) = &cpu->sysdev;
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700231 if (!error)
232 register_cpu_under_node(num, cpu_to_node(num));
Vivek Goyal51be5602006-01-09 20:51:42 -0800233
234#ifdef CONFIG_KEXEC
235 if (!error)
236 error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
237#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return error;
239}
240
Andrew Mortona29d6422006-03-07 23:53:25 -0800241struct sys_device *get_cpu_sysdev(unsigned cpu)
Ashok Rajad745572005-10-30 14:59:49 -0800242{
Mike Travise37d05d2008-05-01 04:35:16 -0700243 if (cpu < nr_cpu_ids && cpu_possible(cpu))
244 return per_cpu(cpu_sys_devices, cpu);
Ashok Rajad745572005-10-30 14:59:49 -0800245 else
246 return NULL;
247}
248EXPORT_SYMBOL_GPL(get_cpu_sysdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250int __init cpu_dev_init(void)
251{
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700252 int err;
253
254 err = sysdev_class_register(&cpu_sysdev_class);
255#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
256 if (!err)
257 err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class);
258#endif
259
260 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
Andi Kleene1a7e292010-01-05 12:48:03 +0100262
263static struct sysdev_class_attribute *cpu_sysdev_class_attrs[] = {
264#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
Greg Kroah-Hartman6c1733a2010-01-21 15:07:59 -0800265 &attr_probe,
266 &attr_release,
Andi Kleene1a7e292010-01-05 12:48:03 +0100267#endif
268 &cpu_attrs[0].attr,
269 &cpu_attrs[1].attr,
270 &cpu_attrs[2].attr,
271 &attr_kernel_max,
272 &attr_offline,
273 NULL
274};