blob: bba70d08be6a6b072d3d07545e694d8983bf3aff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002 * CPU subsystem support
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 */
4
Ben Hutchings024f7842012-01-10 02:59:49 +00005#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#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
Kay Sievers8a25a2f2011-12-21 14:29:42 -080017struct bus_type cpu_subsys = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +010018 .name = "cpu",
Kay Sievers8a25a2f2011-12-21 14:29:42 -080019 .dev_name = "cpu",
Linus Torvalds1da177e2005-04-16 15:20:36 -070020};
Kay Sievers8a25a2f2011-12-21 14:29:42 -080021EXPORT_SYMBOL_GPL(cpu_subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Kay Sievers8a25a2f2011-12-21 14:29:42 -080023static DEFINE_PER_CPU(struct device *, cpu_sys_devices);
Ashok Rajad745572005-10-30 14:59:49 -080024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#ifdef CONFIG_HOTPLUG_CPU
Kay Sievers8a25a2f2011-12-21 14:29:42 -080026static ssize_t show_online(struct device *dev,
27 struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +020028 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Kay Sievers8a25a2f2011-12-21 14:29:42 -080030 struct cpu *cpu = container_of(dev, struct cpu, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Kay Sievers8a25a2f2011-12-21 14:29:42 -080032 return sprintf(buf, "%u\n", !!cpu_online(cpu->dev.id));
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
Kay Sievers8a25a2f2011-12-21 14:29:42 -080035static ssize_t __ref store_online(struct device *dev,
36 struct device_attribute *attr,
37 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Kay Sievers8a25a2f2011-12-21 14:29:42 -080039 struct cpu *cpu = container_of(dev, struct cpu, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 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':
Kay Sievers8a25a2f2011-12-21 14:29:42 -080045 ret = cpu_down(cpu->dev.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 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':
Kay Sievers8a25a2f2011-12-21 14:29:42 -080050 ret = cpu_up(cpu->dev.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}
Kay Sievers8a25a2f2011-12-21 14:29:42 -080063static DEVICE_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{
Kay Sievers8a25a2f2011-12-21 14:29:42 -080067 device_create_file(&cpu->dev, &dev_attr_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070069void unregister_cpu(struct cpu *cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Kay Sievers8a25a2f2011-12-21 14:29:42 -080071 int logical_cpu = cpu->dev.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
Kay Sievers8a25a2f2011-12-21 14:29:42 -080075 device_remove_file(&cpu->dev, &dev_attr_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Kay Sievers8a25a2f2011-12-21 14:29:42 -080077 device_unregister(&cpu->dev);
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
Kay Sievers8a25a2f2011-12-21 14:29:42 -080083static ssize_t cpu_probe_store(struct device *dev,
84 struct device_attribute *attr,
Stephen Rothwell67fc2332010-03-16 10:33:32 +110085 const char *buf,
Nathan Fontenot12633e82009-11-25 17:23:25 +000086 size_t count)
87{
88 return arch_cpu_probe(buf, count);
89}
90
Kay Sievers8a25a2f2011-12-21 14:29:42 -080091static ssize_t cpu_release_store(struct device *dev,
92 struct device_attribute *attr,
Stephen Rothwell67fc2332010-03-16 10:33:32 +110093 const char *buf,
Nathan Fontenot12633e82009-11-25 17:23:25 +000094 size_t count)
95{
96 return arch_cpu_release(buf, count);
97}
98
Kay Sievers8a25a2f2011-12-21 14:29:42 -080099static DEVICE_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
100static DEVICE_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
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800112static ssize_t show_crash_notes(struct device *dev, struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200113 char *buf)
Vivek Goyal51be5602006-01-09 20:51:42 -0800114{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800115 struct cpu *cpu = container_of(dev, struct cpu, dev);
Vivek Goyal51be5602006-01-09 20:51:42 -0800116 ssize_t rc;
117 unsigned long long addr;
118 int cpunum;
119
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800120 cpunum = cpu->dev.id;
Vivek Goyal51be5602006-01-09 20:51:42 -0800121
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}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800132static DEVICE_ATTR(crash_notes, 0400, show_crash_notes, NULL);
Vivek Goyal51be5602006-01-09 20:51:42 -0800133#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 {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800140 struct device_attribute attr;
Andi Kleen265d2e22010-01-05 12:48:00 +0100141 const struct cpumask *const * const map;
142};
143
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800144static ssize_t show_cpus_attr(struct device *dev,
145 struct device_attribute *attr,
Andi Kleen265d2e22010-01-05 12:48:00 +0100146 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
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800156#define _CPU_ATTR(name, map) \
157 { __ATTR(name, 0444, show_cpus_attr, NULL), map }
Mike Travis9d1fe3232008-04-08 11:43:04 -0700158
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800159/* Keep in sync with cpu_subsys_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 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800169static ssize_t print_cpus_kernel_max(struct device *dev,
170 struct device_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}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800175static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
Mike Travise057d7a2008-12-15 20:26:48 -0800176
177/* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
178unsigned int total_cpus;
179
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800180static ssize_t print_cpus_offline(struct device *dev,
181 struct device_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}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800208static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
Mike Travise057d7a2008-12-15 20:26:48 -0800209
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;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 cpu->node_id = cpu_to_node(num);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800223 cpu->dev.id = num;
224 cpu->dev.bus = &cpu_subsys;
225 error = device_register(&cpu->dev);
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100226 if (!error && cpu->hotpluggable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 register_cpu_control(cpu);
Ashok Rajad745572005-10-30 14:59:49 -0800228 if (!error)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800229 per_cpu(cpu_sys_devices, num) = &cpu->dev;
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700230 if (!error)
231 register_cpu_under_node(num, cpu_to_node(num));
Vivek Goyal51be5602006-01-09 20:51:42 -0800232
233#ifdef CONFIG_KEXEC
234 if (!error)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800235 error = device_create_file(&cpu->dev, &dev_attr_crash_notes);
Vivek Goyal51be5602006-01-09 20:51:42 -0800236#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return error;
238}
239
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800240struct device *get_cpu_device(unsigned cpu)
Ashok Rajad745572005-10-30 14:59:49 -0800241{
Mike Travise37d05d2008-05-01 04:35:16 -0700242 if (cpu < nr_cpu_ids && cpu_possible(cpu))
243 return per_cpu(cpu_sys_devices, cpu);
Ashok Rajad745572005-10-30 14:59:49 -0800244 else
245 return NULL;
246}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800247EXPORT_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};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Josh Triplett29875572011-12-03 13:06:50 -0800271bool cpu_is_hotpluggable(unsigned cpu)
272{
Linus Torvalds7affca32012-01-07 12:03:30 -0800273 struct device *dev = get_cpu_device(cpu);
274 return dev && container_of(dev, struct cpu, dev)->hotpluggable;
Josh Triplett29875572011-12-03 13:06:50 -0800275}
276EXPORT_SYMBOL_GPL(cpu_is_hotpluggable);
277
Ben Hutchings024f7842012-01-10 02:59:49 +0000278void __init cpu_dev_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Ben Hutchings024f7842012-01-10 02:59:49 +0000280 if (subsys_system_register(&cpu_subsys, cpu_root_attr_groups))
281 panic("Failed to register CPU subsystem");
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700282
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800283#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ben Hutchings024f7842012-01-10 02:59:49 +0000284 sched_create_sysfs_power_savings_entries(cpu_subsys.dev_root);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800285#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}