[PATCH] pci/search: cleanups, add to kernel-api.tmpl
[linux-2.6/next.git] / drivers / base / cpu.c
blob4bef76a2f3f2a7611ff0dcbca79c8d774d7a7255
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/cpu.h>
9 #include <linux/topology.h>
10 #include <linux/device.h>
11 #include <linux/node.h>
13 #include "base.h"
15 struct sysdev_class cpu_sysdev_class = {
16 set_kset_name("cpu"),
18 EXPORT_SYMBOL(cpu_sysdev_class);
20 static struct sys_device *cpu_sys_devices[NR_CPUS];
22 #ifdef CONFIG_HOTPLUG_CPU
23 static ssize_t show_online(struct sys_device *dev, char *buf)
25 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
27 return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id));
30 static ssize_t store_online(struct sys_device *dev, const char *buf,
31 size_t count)
33 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
34 ssize_t ret;
36 switch (buf[0]) {
37 case '0':
38 ret = cpu_down(cpu->sysdev.id);
39 if (!ret)
40 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
41 break;
42 case '1':
43 ret = cpu_up(cpu->sysdev.id);
44 if (!ret)
45 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
46 break;
47 default:
48 ret = -EINVAL;
51 if (ret >= 0)
52 ret = count;
53 return ret;
55 static SYSDEV_ATTR(online, 0600, show_online, store_online);
57 static void __devinit register_cpu_control(struct cpu *cpu)
59 sysdev_create_file(&cpu->sysdev, &attr_online);
61 void unregister_cpu(struct cpu *cpu)
63 int logical_cpu = cpu->sysdev.id;
65 unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
67 sysdev_remove_file(&cpu->sysdev, &attr_online);
69 sysdev_unregister(&cpu->sysdev);
70 cpu_sys_devices[logical_cpu] = NULL;
71 return;
73 #else /* ... !CONFIG_HOTPLUG_CPU */
74 static inline void register_cpu_control(struct cpu *cpu)
77 #endif /* CONFIG_HOTPLUG_CPU */
79 #ifdef CONFIG_KEXEC
80 #include <linux/kexec.h>
82 static ssize_t show_crash_notes(struct sys_device *dev, char *buf)
84 struct cpu *cpu = container_of(dev, struct cpu, sysdev);
85 ssize_t rc;
86 unsigned long long addr;
87 int cpunum;
89 cpunum = cpu->sysdev.id;
92 * Might be reading other cpu's data based on which cpu read thread
93 * has been scheduled. But cpu data (memory) is allocated once during
94 * boot up and this data does not change there after. Hence this
95 * operation should be safe. No locking required.
97 addr = __pa(per_cpu_ptr(crash_notes, cpunum));
98 rc = sprintf(buf, "%Lx\n", addr);
99 return rc;
101 static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL);
102 #endif
105 * register_cpu - Setup a driverfs device for a CPU.
106 * @cpu - Callers can set the cpu->no_control field to 1, to indicate not to
107 * generate a control file in sysfs for this CPU.
108 * @num - CPU number to use when creating the device.
110 * Initialize and register the CPU device.
112 int __devinit register_cpu(struct cpu *cpu, int num)
114 int error;
115 cpu->node_id = cpu_to_node(num);
116 cpu->sysdev.id = num;
117 cpu->sysdev.cls = &cpu_sysdev_class;
119 error = sysdev_register(&cpu->sysdev);
121 if (!error && !cpu->no_control)
122 register_cpu_control(cpu);
123 if (!error)
124 cpu_sys_devices[num] = &cpu->sysdev;
125 if (!error)
126 register_cpu_under_node(num, cpu_to_node(num));
128 #ifdef CONFIG_KEXEC
129 if (!error)
130 error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
131 #endif
132 return error;
135 struct sys_device *get_cpu_sysdev(unsigned cpu)
137 if (cpu < NR_CPUS)
138 return cpu_sys_devices[cpu];
139 else
140 return NULL;
142 EXPORT_SYMBOL_GPL(get_cpu_sysdev);
144 int __init cpu_dev_init(void)
146 int err;
148 err = sysdev_class_register(&cpu_sysdev_class);
149 #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
150 if (!err)
151 err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class);
152 #endif
154 return err;