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 static struct sysdev_class_attribute *cpu_sysdev_class_attrs[];
18 struct sysdev_class cpu_sysdev_class = {
20 .attrs = cpu_sysdev_class_attrs,
22 EXPORT_SYMBOL(cpu_sysdev_class);
24 static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices);
26 #ifdef CONFIG_HOTPLUG_CPU
27 static ssize_t show_online(struct sys_device *dev, struct sysdev_attribute *attr,
30 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
32 return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
35 static ssize_t __ref store_online(struct sys_device *dev, struct sysdev_attribute *attr,
36 const char *buf, size_t count)
38 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
41 cpu_hotplug_driver_lock();
44 ret = cpu_down(cpu->sysdev.id);
46 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
49 ret = cpu_up(cpu->sysdev.id);
51 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
56 cpu_hotplug_driver_unlock();
62 static SYSDEV_ATTR(online, 0644, show_online, store_online);
64 static void __cpuinit register_cpu_control(struct cpu *cpu)
66 sysdev_create_file(&cpu->sysdev, &attr_online);
68 void unregister_cpu(struct cpu *cpu)
70 int logical_cpu = cpu->sysdev.id;
72 unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
74 sysdev_remove_file(&cpu->sysdev, &attr_online);
76 sysdev_unregister(&cpu->sysdev);
77 per_cpu(cpu_sys_devices, logical_cpu) = NULL;
81 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
82 static ssize_t cpu_probe_store(struct class *class, const char *buf,
85 return arch_cpu_probe(buf, count);
88 static ssize_t cpu_release_store(struct class *class, const char *buf,
91 return arch_cpu_release(buf, count);
94 static CLASS_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
95 static CLASS_ATTR(release, S_IWUSR, NULL, cpu_release_store);
96 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
98 #else /* ... !CONFIG_HOTPLUG_CPU */
99 static inline void register_cpu_control(struct cpu *cpu)
102 #endif /* CONFIG_HOTPLUG_CPU */
105 #include <linux/kexec.h>
107 static ssize_t show_crash_notes(struct sys_device *dev, struct sysdev_attribute *attr,
110 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
112 unsigned long long addr;
115 cpunum = cpu->sysdev.id;
118 * Might be reading other cpu's data based on which cpu read thread
119 * has been scheduled. But cpu data (memory) is allocated once during
120 * boot up and this data does not change there after. Hence this
121 * operation should be safe. No locking required.
123 addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
124 rc = sprintf(buf, "%Lx\n", addr);
127 static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
131 * Print cpu online, possible, present, and system maps
135 struct sysdev_class_attribute attr;
136 const struct cpumask *const * const map;
139 static ssize_t show_cpus_attr(struct sysdev_class *class,
140 struct sysdev_class_attribute *attr,
143 struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
144 int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *(ca->map));
151 #define _CPU_ATTR(name, map) \
152 { _SYSDEV_CLASS_ATTR(name, 0444, show_cpus_attr, NULL), map }
154 /* Keep in sync with cpu_sysdev_class_attrs */
155 static struct cpu_attr cpu_attrs[] = {
156 _CPU_ATTR(online, &cpu_online_mask),
157 _CPU_ATTR(possible, &cpu_possible_mask),
158 _CPU_ATTR(present, &cpu_present_mask),
162 * Print values for NR_CPUS and offlined cpus
164 static ssize_t print_cpus_kernel_max(struct sysdev_class *class,
165 struct sysdev_class_attribute *attr, char *buf)
167 int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1);
170 static SYSDEV_CLASS_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
172 /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
173 unsigned int total_cpus;
175 static ssize_t print_cpus_offline(struct sysdev_class *class,
176 struct sysdev_class_attribute *attr, char *buf)
178 int n = 0, len = PAGE_SIZE-2;
179 cpumask_var_t offline;
181 /* display offline cpus < nr_cpu_ids */
182 if (!alloc_cpumask_var(&offline, GFP_KERNEL))
184 cpumask_complement(offline, cpu_online_mask);
185 n = cpulist_scnprintf(buf, len, offline);
186 free_cpumask_var(offline);
188 /* display offline cpus >= nr_cpu_ids */
189 if (total_cpus && nr_cpu_ids < total_cpus) {
193 if (nr_cpu_ids == total_cpus-1)
194 n += snprintf(&buf[n], len - n, "%d", nr_cpu_ids);
196 n += snprintf(&buf[n], len - n, "%d-%d",
197 nr_cpu_ids, total_cpus-1);
200 n += snprintf(&buf[n], len - n, "\n");
203 static SYSDEV_CLASS_ATTR(offline, 0444, print_cpus_offline, NULL);
206 * register_cpu - Setup a sysfs device for a CPU.
207 * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
208 * sysfs for this CPU.
209 * @num - CPU number to use when creating the device.
211 * Initialize and register the CPU device.
213 int __cpuinit register_cpu(struct cpu *cpu, int num)
216 cpu->node_id = cpu_to_node(num);
217 cpu->sysdev.id = num;
218 cpu->sysdev.cls = &cpu_sysdev_class;
220 error = sysdev_register(&cpu->sysdev);
222 if (!error && cpu->hotpluggable)
223 register_cpu_control(cpu);
225 per_cpu(cpu_sys_devices, num) = &cpu->sysdev;
227 register_cpu_under_node(num, cpu_to_node(num));
231 error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
236 struct sys_device *get_cpu_sysdev(unsigned cpu)
238 if (cpu < nr_cpu_ids && cpu_possible(cpu))
239 return per_cpu(cpu_sys_devices, cpu);
243 EXPORT_SYMBOL_GPL(get_cpu_sysdev);
245 int __init cpu_dev_init(void)
249 err = sysdev_class_register(&cpu_sysdev_class);
250 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
252 err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class);
258 static struct sysdev_class_attribute *cpu_sysdev_class_attrs[] = {
259 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
260 &class_attr_probe.attr,
261 &class_attr_release.attr,