blob: 64f5d54f7edcee095ae8ead0a2f29d032ce546cd [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 Hiroyuki76b67ed2006-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
38 switch (buf[0]) {
39 case '0':
40 ret = cpu_down(cpu->sysdev.id);
41 if (!ret)
Kay Sievers312c0042005-11-16 09:00:00 +010042 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 break;
44 case '1':
Ashok Raj34f361a2006-03-25 03:08:18 -080045 ret = cpu_up(cpu->sysdev.id);
Nathan Lynchfb69c392005-06-25 14:55:05 -070046 if (!ret)
Kay Sievers312c0042005-11-16 09:00:00 +010047 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 break;
49 default:
50 ret = -EINVAL;
51 }
52
53 if (ret >= 0)
54 ret = count;
55 return ret;
56}
Ulrich Drepper9eb3ff42007-07-31 00:38:16 -070057static SYSDEV_ATTR(online, 0644, show_online, store_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Sam Ravnborg6c847402008-03-04 15:09:05 -080059static void __cpuinit register_cpu_control(struct cpu *cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 sysdev_create_file(&cpu->sysdev, &attr_online);
62}
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -070063void unregister_cpu(struct cpu *cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Ashok Rajad745572005-10-30 14:59:49 -080065 int logical_cpu = cpu->sysdev.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -070067 unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 sysdev_remove_file(&cpu->sysdev, &attr_online);
70
71 sysdev_unregister(&cpu->sysdev);
Mike Travise37d05d2008-05-01 04:35:16 -070072 per_cpu(cpu_sys_devices, logical_cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return;
74}
75#else /* ... !CONFIG_HOTPLUG_CPU */
76static inline void register_cpu_control(struct cpu *cpu)
77{
78}
79#endif /* CONFIG_HOTPLUG_CPU */
80
Vivek Goyal51be5602006-01-09 20:51:42 -080081#ifdef CONFIG_KEXEC
82#include <linux/kexec.h>
83
Andi Kleen4a0b2b42008-07-01 18:48:41 +020084static ssize_t show_crash_notes(struct sys_device *dev, struct sysdev_attribute *attr,
85 char *buf)
Vivek Goyal51be5602006-01-09 20:51:42 -080086{
87 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
88 ssize_t rc;
89 unsigned long long addr;
90 int cpunum;
91
92 cpunum = cpu->sysdev.id;
93
94 /*
95 * Might be reading other cpu's data based on which cpu read thread
96 * has been scheduled. But cpu data (memory) is allocated once during
97 * boot up and this data does not change there after. Hence this
98 * operation should be safe. No locking required.
99 */
Vivek Goyal51be5602006-01-09 20:51:42 -0800100 addr = __pa(per_cpu_ptr(crash_notes, cpunum));
101 rc = sprintf(buf, "%Lx\n", addr);
Vivek Goyal51be5602006-01-09 20:51:42 -0800102 return rc;
103}
104static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
105#endif
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*
Mike Travis9d1fe322008-04-08 11:43:04 -0700108 * Print cpu online, possible, present, and system maps
109 */
110static ssize_t print_cpus_map(char *buf, cpumask_t *map)
111{
112 int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *map);
113
114 buf[n++] = '\n';
115 buf[n] = '\0';
116 return n;
117}
118
119#define print_cpus_func(type) \
120static ssize_t print_cpus_##type(struct sysdev_class *class, char *buf) \
121{ \
122 return print_cpus_map(buf, &cpu_##type##_map); \
123} \
Mike Travis143aa5c2008-05-12 21:21:13 +0200124static struct sysdev_class_attribute attr_##type##_map = \
Mike Travis9d1fe322008-04-08 11:43:04 -0700125 _SYSDEV_CLASS_ATTR(type, 0444, print_cpus_##type, NULL)
126
127print_cpus_func(online);
128print_cpus_func(possible);
129print_cpus_func(present);
130
Mike Travis143aa5c2008-05-12 21:21:13 +0200131static struct sysdev_class_attribute *cpu_state_attr[] = {
Mike Travis9d1fe322008-04-08 11:43:04 -0700132 &attr_online_map,
133 &attr_possible_map,
134 &attr_present_map,
135};
136
137static int cpu_states_init(void)
138{
139 int i;
140 int err = 0;
141
142 for (i = 0; i < ARRAY_SIZE(cpu_state_attr); i++) {
143 int ret;
144 ret = sysdev_class_create_file(&cpu_sysdev_class,
145 cpu_state_attr[i]);
146 if (!err)
147 err = ret;
148 }
149 return err;
150}
151
152/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100153 * register_cpu - Setup a sysfs device for a CPU.
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100154 * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
155 * sysfs for this CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * @num - CPU number to use when creating the device.
157 *
158 * Initialize and register the CPU device.
159 */
Randy Dunlap33b5f312008-02-06 01:36:28 -0800160int __cpuinit register_cpu(struct cpu *cpu, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 cpu->node_id = cpu_to_node(num);
164 cpu->sysdev.id = num;
165 cpu->sysdev.cls = &cpu_sysdev_class;
166
167 error = sysdev_register(&cpu->sysdev);
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700168
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100169 if (!error && cpu->hotpluggable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 register_cpu_control(cpu);
Ashok Rajad745572005-10-30 14:59:49 -0800171 if (!error)
Mike Travise37d05d2008-05-01 04:35:16 -0700172 per_cpu(cpu_sys_devices, num) = &cpu->sysdev;
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700173 if (!error)
174 register_cpu_under_node(num, cpu_to_node(num));
Vivek Goyal51be5602006-01-09 20:51:42 -0800175
176#ifdef CONFIG_KEXEC
177 if (!error)
178 error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
179#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return error;
181}
182
Andrew Mortona29d6422006-03-07 23:53:25 -0800183struct sys_device *get_cpu_sysdev(unsigned cpu)
Ashok Rajad745572005-10-30 14:59:49 -0800184{
Mike Travise37d05d2008-05-01 04:35:16 -0700185 if (cpu < nr_cpu_ids && cpu_possible(cpu))
186 return per_cpu(cpu_sys_devices, cpu);
Ashok Rajad745572005-10-30 14:59:49 -0800187 else
188 return NULL;
189}
190EXPORT_SYMBOL_GPL(get_cpu_sysdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192int __init cpu_dev_init(void)
193{
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700194 int err;
195
196 err = sysdev_class_register(&cpu_sysdev_class);
Mike Travis9d1fe322008-04-08 11:43:04 -0700197 if (!err)
198 err = cpu_states_init();
199
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -0700200#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
201 if (!err)
202 err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class);
203#endif
204
205 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}