fuse: use clamp() rather than nested min/max
[deliverable/linux.git] / drivers / base / cpu.c
CommitLineData
1da177e4
LT
1/*
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>
f6a57033 8#include <linux/sched.h>
1da177e4
LT
9#include <linux/cpu.h>
10#include <linux/topology.h>
11#include <linux/device.h>
76b67ed9 12#include <linux/node.h>
1da177e4 13
a1bdc7aa 14#include "base.h"
1da177e4
LT
15
16struct sysdev_class cpu_sysdev_class = {
af5ca3f4 17 .name = "cpu",
1da177e4
LT
18};
19EXPORT_SYMBOL(cpu_sysdev_class);
20
ad74557a
AR
21static struct sys_device *cpu_sys_devices[NR_CPUS];
22
1da177e4
LT
23#ifdef CONFIG_HOTPLUG_CPU
24static ssize_t show_online(struct sys_device *dev, char *buf)
25{
26 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
27
28 return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
29}
30
6c847402 31static ssize_t __ref store_online(struct sys_device *dev, const char *buf,
1da177e4
LT
32 size_t count)
33{
34 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
35 ssize_t ret;
36
37 switch (buf[0]) {
38 case '0':
39 ret = cpu_down(cpu->sysdev.id);
40 if (!ret)
312c004d 41 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1da177e4
LT
42 break;
43 case '1':
34f361ad 44 ret = cpu_up(cpu->sysdev.id);
fb69c390 45 if (!ret)
312c004d 46 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1da177e4
LT
47 break;
48 default:
49 ret = -EINVAL;
50 }
51
52 if (ret >= 0)
53 ret = count;
54 return ret;
55}
9eb3ff40 56static SYSDEV_ATTR(online, 0644, show_online, store_online);
1da177e4 57
6c847402 58static void __cpuinit register_cpu_control(struct cpu *cpu)
1da177e4
LT
59{
60 sysdev_create_file(&cpu->sysdev, &attr_online);
61}
76b67ed9 62void unregister_cpu(struct cpu *cpu)
1da177e4 63{
ad74557a 64 int logical_cpu = cpu->sysdev.id;
1da177e4 65
76b67ed9
KH
66 unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
67
1da177e4
LT
68 sysdev_remove_file(&cpu->sysdev, &attr_online);
69
70 sysdev_unregister(&cpu->sysdev);
ad74557a 71 cpu_sys_devices[logical_cpu] = NULL;
1da177e4
LT
72 return;
73}
74#else /* ... !CONFIG_HOTPLUG_CPU */
75static inline void register_cpu_control(struct cpu *cpu)
76{
77}
78#endif /* CONFIG_HOTPLUG_CPU */
79
51be5606
VG
80#ifdef CONFIG_KEXEC
81#include <linux/kexec.h>
82
83static ssize_t show_crash_notes(struct sys_device *dev, char *buf)
84{
85 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
86 ssize_t rc;
87 unsigned long long addr;
88 int cpunum;
89
90 cpunum = cpu->sysdev.id;
91
92 /*
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.
97 */
51be5606
VG
98 addr = __pa(per_cpu_ptr(crash_notes, cpunum));
99 rc = sprintf(buf, "%Lx\n", addr);
51be5606
VG
100 return rc;
101}
102static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
103#endif
104
9d1fe323
MT
105/*
106 * Print cpu online, possible, present, and system maps
107 */
108static ssize_t print_cpus_map(char *buf, cpumask_t *map)
109{
110 int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *map);
111
112 buf[n++] = '\n';
113 buf[n] = '\0';
114 return n;
115}
116
117#define print_cpus_func(type) \
118static ssize_t print_cpus_##type(struct sysdev_class *class, char *buf) \
119{ \
120 return print_cpus_map(buf, &cpu_##type##_map); \
121} \
122struct sysdev_class_attribute attr_##type##_map = \
123 _SYSDEV_CLASS_ATTR(type, 0444, print_cpus_##type, NULL)
124
125print_cpus_func(online);
126print_cpus_func(possible);
127print_cpus_func(present);
128
129struct sysdev_class_attribute *cpu_state_attr[] = {
130 &attr_online_map,
131 &attr_possible_map,
132 &attr_present_map,
133};
134
135static int cpu_states_init(void)
136{
137 int i;
138 int err = 0;
139
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;
146 }
147 return err;
148}
149
1da177e4 150/*
405ae7d3 151 * register_cpu - Setup a sysfs device for a CPU.
72486f1f
SS
152 * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
153 * sysfs for this CPU.
1da177e4
LT
154 * @num - CPU number to use when creating the device.
155 *
156 * Initialize and register the CPU device.
157 */
33b5f31b 158int __cpuinit register_cpu(struct cpu *cpu, int num)
1da177e4
LT
159{
160 int error;
1da177e4
LT
161 cpu->node_id = cpu_to_node(num);
162 cpu->sysdev.id = num;
163 cpu->sysdev.cls = &cpu_sysdev_class;
164
165 error = sysdev_register(&cpu->sysdev);
76b67ed9 166
72486f1f 167 if (!error && cpu->hotpluggable)
1da177e4 168 register_cpu_control(cpu);
ad74557a
AR
169 if (!error)
170 cpu_sys_devices[num] = &cpu->sysdev;
76b67ed9
KH
171 if (!error)
172 register_cpu_under_node(num, cpu_to_node(num));
51be5606
VG
173
174#ifdef CONFIG_KEXEC
175 if (!error)
176 error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
177#endif
1da177e4
LT
178 return error;
179}
180
a29d642a 181struct sys_device *get_cpu_sysdev(unsigned cpu)
ad74557a
AR
182{
183 if (cpu < NR_CPUS)
184 return cpu_sys_devices[cpu];
185 else
186 return NULL;
187}
188EXPORT_SYMBOL_GPL(get_cpu_sysdev);
1da177e4
LT
189
190int __init cpu_dev_init(void)
191{
5c45bf27
SS
192 int err;
193
194 err = sysdev_class_register(&cpu_sysdev_class);
9d1fe323
MT
195 if (!err)
196 err = cpu_states_init();
197
5c45bf27
SS
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
202
203 return err;
1da177e4 204}
This page took 0.450428 seconds and 5 git commands to generate.