2 * drivers/base/cpu.c - basic CPU class support
5 #include <linux/sysdev.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
9 #include <linux/topology.h>
10 #include <linux/device.h>
14 struct sysdev_class cpu_sysdev_class = {
17 EXPORT_SYMBOL(cpu_sysdev_class);
19 static struct sys_device *cpu_sys_devices[NR_CPUS];
21 #ifdef CONFIG_HOTPLUG_CPU
22 int __attribute__((weak)) smp_prepare_cpu (int cpu)
27 static ssize_t show_online(struct sys_device *dev, char *buf)
29 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
31 return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
34 static ssize_t store_online(struct sys_device *dev, const char *buf,
37 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
42 ret = cpu_down(cpu->sysdev.id);
44 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
47 ret = smp_prepare_cpu(cpu->sysdev.id);
49 ret = cpu_up(cpu->sysdev.id);
51 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
61 static SYSDEV_ATTR(online, 0600, show_online, store_online);
63 static void __devinit register_cpu_control(struct cpu *cpu)
65 sysdev_create_file(&cpu->sysdev, &attr_online);
67 void unregister_cpu(struct cpu *cpu, struct node *root)
69 int logical_cpu = cpu->sysdev.id;
72 sysfs_remove_link(&root->sysdev.kobj,
73 kobject_name(&cpu->sysdev.kobj));
74 sysdev_remove_file(&cpu->sysdev, &attr_online);
76 sysdev_unregister(&cpu->sysdev);
77 cpu_sys_devices[logical_cpu] = NULL;
80 #else /* ... !CONFIG_HOTPLUG_CPU */
81 static inline void register_cpu_control(struct cpu *cpu)
84 #endif /* CONFIG_HOTPLUG_CPU */
87 #include <linux/kexec.h>
89 static ssize_t show_crash_notes(struct sys_device *dev, char *buf)
91 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
93 unsigned long long addr;
96 cpunum = cpu->sysdev.id;
99 * Might be reading other cpu's data based on which cpu read thread
100 * has been scheduled. But cpu data (memory) is allocated once during
101 * boot up and this data does not change there after. Hence this
102 * operation should be safe. No locking required.
104 addr = __pa(per_cpu_ptr(crash_notes, cpunum));
105 rc = sprintf(buf, "%Lx\n", addr);
108 static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
112 * register_cpu - Setup a driverfs device for a CPU.
113 * @cpu - Callers can set the cpu->no_control field to 1, to indicate not to
114 * generate a control file in sysfs for this CPU.
115 * @num - CPU number to use when creating the device.
117 * Initialize and register the CPU device.
119 int __devinit register_cpu(struct cpu *cpu, int num, struct node *root)
123 cpu->node_id = cpu_to_node(num);
124 cpu->sysdev.id = num;
125 cpu->sysdev.cls = &cpu_sysdev_class;
127 error = sysdev_register(&cpu->sysdev);
129 error = sysfs_create_link(&root->sysdev.kobj,
131 kobject_name(&cpu->sysdev.kobj));
132 if (!error && !cpu->no_control)
133 register_cpu_control(cpu);
135 cpu_sys_devices[num] = &cpu->sysdev;
139 error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
144 struct sys_device *get_cpu_sysdev(int cpu)
147 return cpu_sys_devices[cpu];
151 EXPORT_SYMBOL_GPL(get_cpu_sysdev);
153 int __init cpu_dev_init(void)
155 return sysdev_class_register(&cpu_sysdev_class);