Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6/linux-mips/linux-dm7025.git] / drivers / base / cpu.c
blobe38dfed41d80b8e6dff45379ee1fdfb8e8d3c750
1 /*
2 * drivers/base/cpu.c - basic CPU class support
3 */
5 #include <linux/sysdev.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/sched.h>
9 #include <linux/cpu.h>
10 #include <linux/topology.h>
11 #include <linux/device.h>
12 #include <linux/node.h>
14 #include "base.h"
16 struct sysdev_class cpu_sysdev_class = {
17 .name = "cpu",
19 EXPORT_SYMBOL(cpu_sysdev_class);
21 static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices);
23 #ifdef CONFIG_HOTPLUG_CPU
24 static ssize_t show_online(struct sys_device *dev, char *buf)
26 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
28 return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
31 static ssize_t __ref store_online(struct sys_device *dev, const char *buf,
32 size_t count)
34 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
35 ssize_t ret;
37 switch (buf[0]) {
38 case '0':
39 ret = cpu_down(cpu->sysdev.id);
40 if (!ret)
41 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
42 break;
43 case '1':
44 ret = cpu_up(cpu->sysdev.id);
45 if (!ret)
46 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
47 break;
48 default:
49 ret = -EINVAL;
52 if (ret >= 0)
53 ret = count;
54 return ret;
56 static SYSDEV_ATTR(online, 0644, show_online, store_online);
58 static void __cpuinit register_cpu_control(struct cpu *cpu)
60 sysdev_create_file(&cpu->sysdev, &attr_online);
62 void unregister_cpu(struct cpu *cpu)
64 int logical_cpu = cpu->sysdev.id;
66 unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
68 sysdev_remove_file(&cpu->sysdev, &attr_online);
70 sysdev_unregister(&cpu->sysdev);
71 per_cpu(cpu_sys_devices, logical_cpu) = NULL;
72 return;
74 #else /* ... !CONFIG_HOTPLUG_CPU */
75 static inline void register_cpu_control(struct cpu *cpu)
78 #endif /* CONFIG_HOTPLUG_CPU */
80 #ifdef CONFIG_KEXEC
81 #include <linux/kexec.h>
83 static ssize_t show_crash_notes(struct sys_device *dev, char *buf)
85 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
86 ssize_t rc;
87 unsigned long long addr;
88 int cpunum;
90 cpunum = cpu->sysdev.id;
93 * Might be reading other cpu's data based on which cpu read thread
94 * has been scheduled. But cpu data (memory) is allocated once during
95 * boot up and this data does not change there after. Hence this
96 * operation should be safe. No locking required.
98 addr = __pa(per_cpu_ptr(crash_notes, cpunum));
99 rc = sprintf(buf, "%Lx\n", addr);
100 return rc;
102 static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
103 #endif
106 * Print cpu online, possible, present, and system maps
108 static ssize_t print_cpus_map(char *buf, cpumask_t *map)
110 int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *map);
112 buf[n++] = '\n';
113 buf[n] = '\0';
114 return n;
117 #define print_cpus_func(type) \
118 static ssize_t print_cpus_##type(struct sysdev_class *class, char *buf) \
120 return print_cpus_map(buf, &cpu_##type##_map); \
122 struct sysdev_class_attribute attr_##type##_map = \
123 _SYSDEV_CLASS_ATTR(type, 0444, print_cpus_##type, NULL)
125 print_cpus_func(online);
126 print_cpus_func(possible);
127 print_cpus_func(present);
129 struct sysdev_class_attribute *cpu_state_attr[] = {
130 &attr_online_map,
131 &attr_possible_map,
132 &attr_present_map,
135 static int cpu_states_init(void)
137 int i;
138 int err = 0;
140 for (i = 0; i < ARRAY_SIZE(cpu_state_attr); i++) {
141 int ret;
142 ret = sysdev_class_create_file(&cpu_sysdev_class,
143 cpu_state_attr[i]);
144 if (!err)
145 err = ret;
147 return err;
151 * register_cpu - Setup a sysfs device for a CPU.
152 * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
153 * sysfs for this CPU.
154 * @num - CPU number to use when creating the device.
156 * Initialize and register the CPU device.
158 int __cpuinit register_cpu(struct cpu *cpu, int num)
160 int error;
161 cpu->node_id = cpu_to_node(num);
162 cpu->sysdev.id = num;
163 cpu->sysdev.cls = &cpu_sysdev_class;
165 error = sysdev_register(&cpu->sysdev);
167 if (!error && cpu->hotpluggable)
168 register_cpu_control(cpu);
169 if (!error)
170 per_cpu(cpu_sys_devices, num) = &cpu->sysdev;
171 if (!error)
172 register_cpu_under_node(num, cpu_to_node(num));
174 #ifdef CONFIG_KEXEC
175 if (!error)
176 error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
177 #endif
178 return error;
181 struct sys_device *get_cpu_sysdev(unsigned cpu)
183 if (cpu < nr_cpu_ids && cpu_possible(cpu))
184 return per_cpu(cpu_sys_devices, cpu);
185 else
186 return NULL;
188 EXPORT_SYMBOL_GPL(get_cpu_sysdev);
190 int __init cpu_dev_init(void)
192 int err;
194 err = sysdev_class_register(&cpu_sysdev_class);
195 if (!err)
196 err = cpu_states_init();
198 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
199 if (!err)
200 err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class);
201 #endif
203 return err;