Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Al Viro | f6a5703 | 2006-10-18 01:47:25 -0400 | [diff] [blame] | 8 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/cpu.h> |
| 10 | #include <linux/topology.h> |
| 11 | #include <linux/device.h> |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 12 | #include <linux/node.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 14 | #include "base.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | struct sysdev_class cpu_sysdev_class = { |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 17 | .name = "cpu", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | }; |
| 19 | EXPORT_SYMBOL(cpu_sysdev_class); |
| 20 | |
Mike Travis | e37d05d | 2008-05-01 04:35:16 -0700 | [diff] [blame] | 21 | static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices); |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #ifdef CONFIG_HOTPLUG_CPU |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 24 | static ssize_t show_online(struct sys_device *dev, struct sysdev_attribute *attr, |
| 25 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
| 27 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); |
| 28 | |
| 29 | return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id)); |
| 30 | } |
| 31 | |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 32 | static ssize_t __ref store_online(struct sys_device *dev, struct sysdev_attribute *attr, |
| 33 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
| 35 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); |
| 36 | ssize_t ret; |
| 37 | |
Gautham R Shenoy | 51badeb | 2009-11-26 09:59:05 +0000 | [diff] [blame] | 38 | cpu_hotplug_driver_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | switch (buf[0]) { |
| 40 | case '0': |
| 41 | ret = cpu_down(cpu->sysdev.id); |
| 42 | if (!ret) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 43 | kobject_uevent(&dev->kobj, KOBJ_OFFLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | break; |
| 45 | case '1': |
Ashok Raj | 34f361a | 2006-03-25 03:08:18 -0800 | [diff] [blame] | 46 | ret = cpu_up(cpu->sysdev.id); |
Nathan Lynch | fb69c39 | 2005-06-25 14:55:05 -0700 | [diff] [blame] | 47 | if (!ret) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 48 | kobject_uevent(&dev->kobj, KOBJ_ONLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | break; |
| 50 | default: |
| 51 | ret = -EINVAL; |
| 52 | } |
Gautham R Shenoy | 51badeb | 2009-11-26 09:59:05 +0000 | [diff] [blame] | 53 | cpu_hotplug_driver_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | if (ret >= 0) |
| 56 | ret = count; |
| 57 | return ret; |
| 58 | } |
Ulrich Drepper | 9eb3ff4 | 2007-07-31 00:38:16 -0700 | [diff] [blame] | 59 | static SYSDEV_ATTR(online, 0644, show_online, store_online); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Sam Ravnborg | 6c84740 | 2008-03-04 15:09:05 -0800 | [diff] [blame] | 61 | static void __cpuinit register_cpu_control(struct cpu *cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
| 63 | sysdev_create_file(&cpu->sysdev, &attr_online); |
| 64 | } |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 65 | void unregister_cpu(struct cpu *cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 67 | int logical_cpu = cpu->sysdev.id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 69 | unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu)); |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | sysdev_remove_file(&cpu->sysdev, &attr_online); |
| 72 | |
| 73 | sysdev_unregister(&cpu->sysdev); |
Mike Travis | e37d05d | 2008-05-01 04:35:16 -0700 | [diff] [blame] | 74 | per_cpu(cpu_sys_devices, logical_cpu) = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | return; |
| 76 | } |
Nathan Fontenot | 12633e8 | 2009-11-25 17:23:25 +0000 | [diff] [blame] | 77 | |
| 78 | #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE |
| 79 | static ssize_t cpu_probe_store(struct class *class, const char *buf, |
| 80 | size_t count) |
| 81 | { |
| 82 | return arch_cpu_probe(buf, count); |
| 83 | } |
| 84 | |
| 85 | static ssize_t cpu_release_store(struct class *class, const char *buf, |
| 86 | size_t count) |
| 87 | { |
| 88 | return arch_cpu_release(buf, count); |
| 89 | } |
| 90 | |
| 91 | static CLASS_ATTR(probe, S_IWUSR, NULL, cpu_probe_store); |
| 92 | static CLASS_ATTR(release, S_IWUSR, NULL, cpu_release_store); |
| 93 | |
| 94 | int __init cpu_probe_release_init(void) |
| 95 | { |
| 96 | int rc; |
| 97 | |
| 98 | rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj, |
| 99 | &class_attr_probe.attr); |
| 100 | if (!rc) |
| 101 | rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj, |
| 102 | &class_attr_release.attr); |
| 103 | |
| 104 | return rc; |
| 105 | } |
| 106 | device_initcall(cpu_probe_release_init); |
| 107 | #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */ |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | #else /* ... !CONFIG_HOTPLUG_CPU */ |
| 110 | static inline void register_cpu_control(struct cpu *cpu) |
| 111 | { |
| 112 | } |
| 113 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 114 | |
Vivek Goyal | 51be560 | 2006-01-09 20:51:42 -0800 | [diff] [blame] | 115 | #ifdef CONFIG_KEXEC |
| 116 | #include <linux/kexec.h> |
| 117 | |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 118 | static ssize_t show_crash_notes(struct sys_device *dev, struct sysdev_attribute *attr, |
| 119 | char *buf) |
Vivek Goyal | 51be560 | 2006-01-09 20:51:42 -0800 | [diff] [blame] | 120 | { |
| 121 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); |
| 122 | ssize_t rc; |
| 123 | unsigned long long addr; |
| 124 | int cpunum; |
| 125 | |
| 126 | cpunum = cpu->sysdev.id; |
| 127 | |
| 128 | /* |
| 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. |
| 133 | */ |
Vivek Goyal | 3b034b0 | 2009-11-24 15:50:03 +0900 | [diff] [blame] | 134 | addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum)); |
Vivek Goyal | 51be560 | 2006-01-09 20:51:42 -0800 | [diff] [blame] | 135 | rc = sprintf(buf, "%Lx\n", addr); |
Vivek Goyal | 51be560 | 2006-01-09 20:51:42 -0800 | [diff] [blame] | 136 | return rc; |
| 137 | } |
| 138 | static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL); |
| 139 | #endif |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | /* |
Mike Travis | 9d1fe32 | 2008-04-08 11:43:04 -0700 | [diff] [blame] | 142 | * Print cpu online, possible, present, and system maps |
| 143 | */ |
Rusty Russell | f7df8ed | 2009-01-10 21:58:09 -0800 | [diff] [blame] | 144 | static ssize_t print_cpus_map(char *buf, const struct cpumask *map) |
Mike Travis | 9d1fe32 | 2008-04-08 11:43:04 -0700 | [diff] [blame] | 145 | { |
Rusty Russell | 29c0177 | 2008-12-13 21:20:25 +1030 | [diff] [blame] | 146 | int n = cpulist_scnprintf(buf, PAGE_SIZE-2, map); |
Mike Travis | 9d1fe32 | 2008-04-08 11:43:04 -0700 | [diff] [blame] | 147 | |
| 148 | buf[n++] = '\n'; |
| 149 | buf[n] = '\0'; |
| 150 | return n; |
| 151 | } |
| 152 | |
| 153 | #define print_cpus_func(type) \ |
| 154 | static ssize_t print_cpus_##type(struct sysdev_class *class, char *buf) \ |
| 155 | { \ |
Rusty Russell | aa85ea5 | 2009-03-30 22:05:15 -0600 | [diff] [blame] | 156 | return print_cpus_map(buf, cpu_##type##_mask); \ |
Mike Travis | 9d1fe32 | 2008-04-08 11:43:04 -0700 | [diff] [blame] | 157 | } \ |
Mike Travis | 143aa5c | 2008-05-12 21:21:13 +0200 | [diff] [blame] | 158 | static struct sysdev_class_attribute attr_##type##_map = \ |
Mike Travis | 9d1fe32 | 2008-04-08 11:43:04 -0700 | [diff] [blame] | 159 | _SYSDEV_CLASS_ATTR(type, 0444, print_cpus_##type, NULL) |
| 160 | |
| 161 | print_cpus_func(online); |
| 162 | print_cpus_func(possible); |
| 163 | print_cpus_func(present); |
| 164 | |
Mike Travis | e057d7a | 2008-12-15 20:26:48 -0800 | [diff] [blame] | 165 | /* |
| 166 | * Print values for NR_CPUS and offlined cpus |
| 167 | */ |
| 168 | static ssize_t print_cpus_kernel_max(struct sysdev_class *class, char *buf) |
| 169 | { |
Mike Travis | 8fd2d2d | 2008-12-31 18:08:48 -0800 | [diff] [blame] | 170 | int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1); |
Mike Travis | e057d7a | 2008-12-15 20:26:48 -0800 | [diff] [blame] | 171 | return n; |
| 172 | } |
| 173 | static SYSDEV_CLASS_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL); |
| 174 | |
| 175 | /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */ |
| 176 | unsigned int total_cpus; |
| 177 | |
| 178 | static ssize_t print_cpus_offline(struct sysdev_class *class, char *buf) |
| 179 | { |
| 180 | int n = 0, len = PAGE_SIZE-2; |
| 181 | cpumask_var_t offline; |
| 182 | |
| 183 | /* display offline cpus < nr_cpu_ids */ |
| 184 | if (!alloc_cpumask_var(&offline, GFP_KERNEL)) |
| 185 | return -ENOMEM; |
| 186 | cpumask_complement(offline, cpu_online_mask); |
| 187 | n = cpulist_scnprintf(buf, len, offline); |
| 188 | free_cpumask_var(offline); |
| 189 | |
| 190 | /* display offline cpus >= nr_cpu_ids */ |
| 191 | if (total_cpus && nr_cpu_ids < total_cpus) { |
| 192 | if (n && n < len) |
| 193 | buf[n++] = ','; |
| 194 | |
| 195 | if (nr_cpu_ids == total_cpus-1) |
| 196 | n += snprintf(&buf[n], len - n, "%d", nr_cpu_ids); |
| 197 | else |
| 198 | n += snprintf(&buf[n], len - n, "%d-%d", |
| 199 | nr_cpu_ids, total_cpus-1); |
| 200 | } |
| 201 | |
| 202 | n += snprintf(&buf[n], len - n, "\n"); |
| 203 | return n; |
| 204 | } |
| 205 | static SYSDEV_CLASS_ATTR(offline, 0444, print_cpus_offline, NULL); |
| 206 | |
Mike Travis | 143aa5c | 2008-05-12 21:21:13 +0200 | [diff] [blame] | 207 | static struct sysdev_class_attribute *cpu_state_attr[] = { |
Mike Travis | 9d1fe32 | 2008-04-08 11:43:04 -0700 | [diff] [blame] | 208 | &attr_online_map, |
| 209 | &attr_possible_map, |
| 210 | &attr_present_map, |
Mike Travis | e057d7a | 2008-12-15 20:26:48 -0800 | [diff] [blame] | 211 | &attr_kernel_max, |
| 212 | &attr_offline, |
Mike Travis | 9d1fe32 | 2008-04-08 11:43:04 -0700 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | static int cpu_states_init(void) |
| 216 | { |
| 217 | int i; |
| 218 | int err = 0; |
| 219 | |
| 220 | for (i = 0; i < ARRAY_SIZE(cpu_state_attr); i++) { |
| 221 | int ret; |
| 222 | ret = sysdev_class_create_file(&cpu_sysdev_class, |
| 223 | cpu_state_attr[i]); |
| 224 | if (!err) |
| 225 | err = ret; |
| 226 | } |
| 227 | return err; |
| 228 | } |
| 229 | |
| 230 | /* |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 231 | * register_cpu - Setup a sysfs device for a CPU. |
Siddha, Suresh B | 72486f1 | 2006-12-07 02:14:10 +0100 | [diff] [blame] | 232 | * @cpu - cpu->hotpluggable field set to 1 will generate a control file in |
| 233 | * sysfs for this CPU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | * @num - CPU number to use when creating the device. |
| 235 | * |
| 236 | * Initialize and register the CPU device. |
| 237 | */ |
Randy Dunlap | 33b5f31 | 2008-02-06 01:36:28 -0800 | [diff] [blame] | 238 | int __cpuinit register_cpu(struct cpu *cpu, int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
| 240 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | cpu->node_id = cpu_to_node(num); |
| 242 | cpu->sysdev.id = num; |
| 243 | cpu->sysdev.cls = &cpu_sysdev_class; |
| 244 | |
| 245 | error = sysdev_register(&cpu->sysdev); |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 246 | |
Siddha, Suresh B | 72486f1 | 2006-12-07 02:14:10 +0100 | [diff] [blame] | 247 | if (!error && cpu->hotpluggable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | register_cpu_control(cpu); |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 249 | if (!error) |
Mike Travis | e37d05d | 2008-05-01 04:35:16 -0700 | [diff] [blame] | 250 | per_cpu(cpu_sys_devices, num) = &cpu->sysdev; |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 251 | if (!error) |
| 252 | register_cpu_under_node(num, cpu_to_node(num)); |
Vivek Goyal | 51be560 | 2006-01-09 20:51:42 -0800 | [diff] [blame] | 253 | |
| 254 | #ifdef CONFIG_KEXEC |
| 255 | if (!error) |
| 256 | error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes); |
| 257 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | return error; |
| 259 | } |
| 260 | |
Andrew Morton | a29d642 | 2006-03-07 23:53:25 -0800 | [diff] [blame] | 261 | struct sys_device *get_cpu_sysdev(unsigned cpu) |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 262 | { |
Mike Travis | e37d05d | 2008-05-01 04:35:16 -0700 | [diff] [blame] | 263 | if (cpu < nr_cpu_ids && cpu_possible(cpu)) |
| 264 | return per_cpu(cpu_sys_devices, cpu); |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 265 | else |
| 266 | return NULL; |
| 267 | } |
| 268 | EXPORT_SYMBOL_GPL(get_cpu_sysdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
| 270 | int __init cpu_dev_init(void) |
| 271 | { |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 272 | int err; |
| 273 | |
| 274 | err = sysdev_class_register(&cpu_sysdev_class); |
Mike Travis | 9d1fe32 | 2008-04-08 11:43:04 -0700 | [diff] [blame] | 275 | if (!err) |
| 276 | err = cpu_states_init(); |
| 277 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 278 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 279 | if (!err) |
| 280 | err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class); |
| 281 | #endif |
| 282 | |
| 283 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |