2 * drivers/base/cpu.c - basic CPU class support
5 #include <linux/sysdev.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/sched.h>
10 #include <linux/topology.h>
11 #include <linux/device.h>
12 #include <linux/node.h>
16 struct sysdev_class cpu_sysdev_class
= {
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
, struct sysdev_attribute
*attr
,
27 struct cpu
*cpu
= container_of(dev
, struct cpu
, sysdev
);
29 return sprintf(buf
, "%u\n", !!cpu_online(cpu
->sysdev
.id
));
32 static ssize_t __ref
store_online(struct sys_device
*dev
, struct sysdev_attribute
*attr
,
33 const char *buf
, size_t count
)
35 struct cpu
*cpu
= container_of(dev
, struct cpu
, sysdev
);
38 cpu_hotplug_driver_lock();
41 ret
= cpu_down(cpu
->sysdev
.id
);
43 kobject_uevent(&dev
->kobj
, KOBJ_OFFLINE
);
46 ret
= cpu_up(cpu
->sysdev
.id
);
48 kobject_uevent(&dev
->kobj
, KOBJ_ONLINE
);
53 cpu_hotplug_driver_unlock();
59 static SYSDEV_ATTR(online
, 0644, show_online
, store_online
);
61 static void __cpuinit
register_cpu_control(struct cpu
*cpu
)
63 sysdev_create_file(&cpu
->sysdev
, &attr_online
);
65 void unregister_cpu(struct cpu
*cpu
)
67 int logical_cpu
= cpu
->sysdev
.id
;
69 unregister_cpu_under_node(logical_cpu
, cpu_to_node(logical_cpu
));
71 sysdev_remove_file(&cpu
->sysdev
, &attr_online
);
73 sysdev_unregister(&cpu
->sysdev
);
74 per_cpu(cpu_sys_devices
, logical_cpu
) = NULL
;
78 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
79 static ssize_t
cpu_probe_store(struct class *class, const char *buf
,
82 return arch_cpu_probe(buf
, count
);
85 static ssize_t
cpu_release_store(struct class *class, const char *buf
,
88 return arch_cpu_release(buf
, count
);
91 static CLASS_ATTR(probe
, S_IWUSR
, NULL
, cpu_probe_store
);
92 static CLASS_ATTR(release
, S_IWUSR
, NULL
, cpu_release_store
);
94 int __init
cpu_probe_release_init(void)
98 rc
= sysfs_create_file(&cpu_sysdev_class
.kset
.kobj
,
99 &class_attr_probe
.attr
);
101 rc
= sysfs_create_file(&cpu_sysdev_class
.kset
.kobj
,
102 &class_attr_release
.attr
);
106 device_initcall(cpu_probe_release_init
);
107 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
109 #else /* ... !CONFIG_HOTPLUG_CPU */
110 static inline void register_cpu_control(struct cpu
*cpu
)
113 #endif /* CONFIG_HOTPLUG_CPU */
116 #include <linux/kexec.h>
118 static ssize_t
show_crash_notes(struct sys_device
*dev
, struct sysdev_attribute
*attr
,
121 struct cpu
*cpu
= container_of(dev
, struct cpu
, sysdev
);
123 unsigned long long addr
;
126 cpunum
= cpu
->sysdev
.id
;
129 * Might be reading other cpu's data based on which cpu read thread
130 * has been scheduled. But cpu data (memory) is allocated once during
131 * boot up and this data does not change there after. Hence this
132 * operation should be safe. No locking required.
134 addr
= per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes
, cpunum
));
135 rc
= sprintf(buf
, "%Lx\n", addr
);
138 static SYSDEV_ATTR(crash_notes
, 0400, show_crash_notes
, NULL
);
142 * Print cpu online, possible, present, and system maps
144 static ssize_t
print_cpus_map(char *buf
, const struct cpumask
*map
)
146 int n
= cpulist_scnprintf(buf
, PAGE_SIZE
-2, map
);
153 #define print_cpus_func(type) \
154 static ssize_t print_cpus_##type(struct sysdev_class *class, char *buf) \
156 return print_cpus_map(buf, cpu_##type##_mask); \
158 static struct sysdev_class_attribute attr_##type##_map = \
159 _SYSDEV_CLASS_ATTR(type, 0444, print_cpus_##type, NULL)
161 print_cpus_func(online
);
162 print_cpus_func(possible
);
163 print_cpus_func(present
);
166 * Print values for NR_CPUS and offlined cpus
168 static ssize_t
print_cpus_kernel_max(struct sysdev_class
*class, char *buf
)
170 int n
= snprintf(buf
, PAGE_SIZE
-2, "%d\n", NR_CPUS
- 1);
173 static SYSDEV_CLASS_ATTR(kernel_max
, 0444, print_cpus_kernel_max
, NULL
);
175 /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
176 unsigned int total_cpus
;
178 static ssize_t
print_cpus_offline(struct sysdev_class
*class, char *buf
)
180 int n
= 0, len
= PAGE_SIZE
-2;
181 cpumask_var_t offline
;
183 /* display offline cpus < nr_cpu_ids */
184 if (!alloc_cpumask_var(&offline
, GFP_KERNEL
))
186 cpumask_complement(offline
, cpu_online_mask
);
187 n
= cpulist_scnprintf(buf
, len
, offline
);
188 free_cpumask_var(offline
);
190 /* display offline cpus >= nr_cpu_ids */
191 if (total_cpus
&& nr_cpu_ids
< total_cpus
) {
195 if (nr_cpu_ids
== total_cpus
-1)
196 n
+= snprintf(&buf
[n
], len
- n
, "%d", nr_cpu_ids
);
198 n
+= snprintf(&buf
[n
], len
- n
, "%d-%d",
199 nr_cpu_ids
, total_cpus
-1);
202 n
+= snprintf(&buf
[n
], len
- n
, "\n");
205 static SYSDEV_CLASS_ATTR(offline
, 0444, print_cpus_offline
, NULL
);
207 static struct sysdev_class_attribute
*cpu_state_attr
[] = {
215 static int cpu_states_init(void)
220 for (i
= 0; i
< ARRAY_SIZE(cpu_state_attr
); i
++) {
222 ret
= sysdev_class_create_file(&cpu_sysdev_class
,
231 * register_cpu - Setup a sysfs device for a CPU.
232 * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
233 * sysfs for this CPU.
234 * @num - CPU number to use when creating the device.
236 * Initialize and register the CPU device.
238 int __cpuinit
register_cpu(struct cpu
*cpu
, int num
)
241 cpu
->node_id
= cpu_to_node(num
);
242 cpu
->sysdev
.id
= num
;
243 cpu
->sysdev
.cls
= &cpu_sysdev_class
;
245 error
= sysdev_register(&cpu
->sysdev
);
247 if (!error
&& cpu
->hotpluggable
)
248 register_cpu_control(cpu
);
250 per_cpu(cpu_sys_devices
, num
) = &cpu
->sysdev
;
252 register_cpu_under_node(num
, cpu_to_node(num
));
256 error
= sysdev_create_file(&cpu
->sysdev
, &attr_crash_notes
);
261 struct sys_device
*get_cpu_sysdev(unsigned cpu
)
263 if (cpu
< nr_cpu_ids
&& cpu_possible(cpu
))
264 return per_cpu(cpu_sys_devices
, cpu
);
268 EXPORT_SYMBOL_GPL(get_cpu_sysdev
);
270 int __init
cpu_dev_init(void)
274 err
= sysdev_class_register(&cpu_sysdev_class
);
276 err
= cpu_states_init();
278 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
280 err
= sched_create_sysfs_power_savings_entries(&cpu_sysdev_class
);