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 | |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 21 | static struct sys_device *cpu_sys_devices[NR_CPUS]; |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #ifdef CONFIG_HOTPLUG_CPU |
| 24 | static ssize_t show_online(struct sys_device *dev, char *buf) |
| 25 | { |
| 26 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); |
| 27 | |
| 28 | return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id)); |
| 29 | } |
| 30 | |
Sam Ravnborg | 6c84740 | 2008-03-04 15:09:05 -0800 | [diff] [blame] | 31 | static ssize_t __ref store_online(struct sys_device *dev, const char *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | size_t count) |
| 33 | { |
| 34 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); |
| 35 | ssize_t ret; |
| 36 | |
| 37 | switch (buf[0]) { |
| 38 | case '0': |
| 39 | ret = cpu_down(cpu->sysdev.id); |
| 40 | if (!ret) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 41 | kobject_uevent(&dev->kobj, KOBJ_OFFLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | break; |
| 43 | case '1': |
Ashok Raj | 34f361a | 2006-03-25 03:08:18 -0800 | [diff] [blame] | 44 | ret = cpu_up(cpu->sysdev.id); |
Nathan Lynch | fb69c39 | 2005-06-25 14:55:05 -0700 | [diff] [blame] | 45 | if (!ret) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 46 | kobject_uevent(&dev->kobj, KOBJ_ONLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | break; |
| 48 | default: |
| 49 | ret = -EINVAL; |
| 50 | } |
| 51 | |
| 52 | if (ret >= 0) |
| 53 | ret = count; |
| 54 | return ret; |
| 55 | } |
Ulrich Drepper | 9eb3ff4 | 2007-07-31 00:38:16 -0700 | [diff] [blame] | 56 | static SYSDEV_ATTR(online, 0644, show_online, store_online); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Sam Ravnborg | 6c84740 | 2008-03-04 15:09:05 -0800 | [diff] [blame] | 58 | static void __cpuinit register_cpu_control(struct cpu *cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
| 60 | sysdev_create_file(&cpu->sysdev, &attr_online); |
| 61 | } |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 62 | void unregister_cpu(struct cpu *cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 64 | int logical_cpu = cpu->sysdev.id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 66 | unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu)); |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | sysdev_remove_file(&cpu->sysdev, &attr_online); |
| 69 | |
| 70 | sysdev_unregister(&cpu->sysdev); |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 71 | cpu_sys_devices[logical_cpu] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | #else /* ... !CONFIG_HOTPLUG_CPU */ |
| 75 | static inline void register_cpu_control(struct cpu *cpu) |
| 76 | { |
| 77 | } |
| 78 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 79 | |
Vivek Goyal | 51be560 | 2006-01-09 20:51:42 -0800 | [diff] [blame] | 80 | #ifdef CONFIG_KEXEC |
| 81 | #include <linux/kexec.h> |
| 82 | |
| 83 | static ssize_t show_crash_notes(struct sys_device *dev, char *buf) |
| 84 | { |
| 85 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); |
| 86 | ssize_t rc; |
| 87 | unsigned long long addr; |
| 88 | int cpunum; |
| 89 | |
| 90 | cpunum = cpu->sysdev.id; |
| 91 | |
| 92 | /* |
| 93 | * Might be reading other cpu's data based on which cpu read thread |
| 94 | * has been scheduled. But cpu data (memory) is allocated once during |
| 95 | * boot up and this data does not change there after. Hence this |
| 96 | * operation should be safe. No locking required. |
| 97 | */ |
Vivek Goyal | 51be560 | 2006-01-09 20:51:42 -0800 | [diff] [blame] | 98 | addr = __pa(per_cpu_ptr(crash_notes, cpunum)); |
| 99 | rc = sprintf(buf, "%Lx\n", addr); |
Vivek Goyal | 51be560 | 2006-01-09 20:51:42 -0800 | [diff] [blame] | 100 | return rc; |
| 101 | } |
| 102 | static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL); |
| 103 | #endif |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | /* |
Mike Travis | 9d1fe32 | 2008-04-08 11:43:04 -0700 | [diff] [blame] | 106 | * Print cpu online, possible, present, and system maps |
| 107 | */ |
| 108 | static ssize_t print_cpus_map(char *buf, cpumask_t *map) |
| 109 | { |
| 110 | int n = cpulist_scnprintf(buf, PAGE_SIZE-2, *map); |
| 111 | |
| 112 | buf[n++] = '\n'; |
| 113 | buf[n] = '\0'; |
| 114 | return n; |
| 115 | } |
| 116 | |
| 117 | #define print_cpus_func(type) \ |
| 118 | static ssize_t print_cpus_##type(struct sysdev_class *class, char *buf) \ |
| 119 | { \ |
| 120 | return print_cpus_map(buf, &cpu_##type##_map); \ |
| 121 | } \ |
| 122 | struct sysdev_class_attribute attr_##type##_map = \ |
| 123 | _SYSDEV_CLASS_ATTR(type, 0444, print_cpus_##type, NULL) |
| 124 | |
| 125 | print_cpus_func(online); |
| 126 | print_cpus_func(possible); |
| 127 | print_cpus_func(present); |
| 128 | |
| 129 | struct sysdev_class_attribute *cpu_state_attr[] = { |
| 130 | &attr_online_map, |
| 131 | &attr_possible_map, |
| 132 | &attr_present_map, |
| 133 | }; |
| 134 | |
| 135 | static int cpu_states_init(void) |
| 136 | { |
| 137 | int i; |
| 138 | int err = 0; |
| 139 | |
| 140 | for (i = 0; i < ARRAY_SIZE(cpu_state_attr); i++) { |
| 141 | int ret; |
| 142 | ret = sysdev_class_create_file(&cpu_sysdev_class, |
| 143 | cpu_state_attr[i]); |
| 144 | if (!err) |
| 145 | err = ret; |
| 146 | } |
| 147 | return err; |
| 148 | } |
| 149 | |
| 150 | /* |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 151 | * register_cpu - Setup a sysfs device for a CPU. |
Siddha, Suresh B | 72486f1 | 2006-12-07 02:14:10 +0100 | [diff] [blame] | 152 | * @cpu - cpu->hotpluggable field set to 1 will generate a control file in |
| 153 | * sysfs for this CPU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | * @num - CPU number to use when creating the device. |
| 155 | * |
| 156 | * Initialize and register the CPU device. |
| 157 | */ |
Randy Dunlap | 33b5f31 | 2008-02-06 01:36:28 -0800 | [diff] [blame] | 158 | int __cpuinit register_cpu(struct cpu *cpu, int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
| 160 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | cpu->node_id = cpu_to_node(num); |
| 162 | cpu->sysdev.id = num; |
| 163 | cpu->sysdev.cls = &cpu_sysdev_class; |
| 164 | |
| 165 | error = sysdev_register(&cpu->sysdev); |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 166 | |
Siddha, Suresh B | 72486f1 | 2006-12-07 02:14:10 +0100 | [diff] [blame] | 167 | if (!error && cpu->hotpluggable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | register_cpu_control(cpu); |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 169 | if (!error) |
| 170 | cpu_sys_devices[num] = &cpu->sysdev; |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 171 | if (!error) |
| 172 | register_cpu_under_node(num, cpu_to_node(num)); |
Vivek Goyal | 51be560 | 2006-01-09 20:51:42 -0800 | [diff] [blame] | 173 | |
| 174 | #ifdef CONFIG_KEXEC |
| 175 | if (!error) |
| 176 | error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes); |
| 177 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | return error; |
| 179 | } |
| 180 | |
Andrew Morton | a29d642 | 2006-03-07 23:53:25 -0800 | [diff] [blame] | 181 | struct sys_device *get_cpu_sysdev(unsigned cpu) |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 182 | { |
| 183 | if (cpu < NR_CPUS) |
| 184 | return cpu_sys_devices[cpu]; |
| 185 | else |
| 186 | return NULL; |
| 187 | } |
| 188 | EXPORT_SYMBOL_GPL(get_cpu_sysdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | int __init cpu_dev_init(void) |
| 191 | { |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 192 | int err; |
| 193 | |
| 194 | err = sysdev_class_register(&cpu_sysdev_class); |
Mike Travis | 9d1fe32 | 2008-04-08 11:43:04 -0700 | [diff] [blame] | 195 | if (!err) |
| 196 | err = cpu_states_init(); |
| 197 | |
Siddha, Suresh B | 5c45bf2 | 2006-06-27 02:54:42 -0700 | [diff] [blame] | 198 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 199 | if (!err) |
| 200 | err = sched_create_sysfs_power_savings_entries(&cpu_sysdev_class); |
| 201 | #endif |
| 202 | |
| 203 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |