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> |
| 8 | #include <linux/cpu.h> |
| 9 | #include <linux/topology.h> |
| 10 | #include <linux/device.h> |
| 11 | |
Ben Dooks | a1bdc7a | 2005-10-13 17:54:41 +0100 | [diff] [blame] | 12 | #include "base.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | struct sysdev_class cpu_sysdev_class = { |
| 15 | set_kset_name("cpu"), |
| 16 | }; |
| 17 | EXPORT_SYMBOL(cpu_sysdev_class); |
| 18 | |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 19 | static struct sys_device *cpu_sys_devices[NR_CPUS]; |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #ifdef CONFIG_HOTPLUG_CPU |
Ashok Raj | 52a119f | 2005-06-25 14:54:57 -0700 | [diff] [blame] | 22 | int __attribute__((weak)) smp_prepare_cpu (int cpu) |
| 23 | { |
| 24 | return 0; |
| 25 | } |
Li Shaohua | e1367da | 2005-06-25 14:54:56 -0700 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | static ssize_t show_online(struct sys_device *dev, char *buf) |
| 28 | { |
| 29 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); |
| 30 | |
| 31 | return sprintf(buf, "%u\n", !!cpu_online(cpu->sysdev.id)); |
| 32 | } |
| 33 | |
| 34 | static ssize_t store_online(struct sys_device *dev, const char *buf, |
| 35 | size_t count) |
| 36 | { |
| 37 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); |
| 38 | ssize_t ret; |
| 39 | |
| 40 | switch (buf[0]) { |
| 41 | case '0': |
| 42 | ret = cpu_down(cpu->sysdev.id); |
| 43 | if (!ret) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 44 | kobject_uevent(&dev->kobj, KOBJ_OFFLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | break; |
| 46 | case '1': |
Li Shaohua | e1367da | 2005-06-25 14:54:56 -0700 | [diff] [blame] | 47 | ret = smp_prepare_cpu(cpu->sysdev.id); |
Ashok Raj | 52a119f | 2005-06-25 14:54:57 -0700 | [diff] [blame] | 48 | if (!ret) |
Li Shaohua | e1367da | 2005-06-25 14:54:56 -0700 | [diff] [blame] | 49 | ret = cpu_up(cpu->sysdev.id); |
Nathan Lynch | fb69c39 | 2005-06-25 14:55:05 -0700 | [diff] [blame] | 50 | if (!ret) |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 51 | kobject_uevent(&dev->kobj, KOBJ_ONLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | break; |
| 53 | default: |
| 54 | ret = -EINVAL; |
| 55 | } |
| 56 | |
| 57 | if (ret >= 0) |
| 58 | ret = count; |
| 59 | return ret; |
| 60 | } |
| 61 | static SYSDEV_ATTR(online, 0600, show_online, store_online); |
| 62 | |
| 63 | static void __devinit register_cpu_control(struct cpu *cpu) |
| 64 | { |
| 65 | sysdev_create_file(&cpu->sysdev, &attr_online); |
| 66 | } |
| 67 | void unregister_cpu(struct cpu *cpu, struct node *root) |
| 68 | { |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 69 | int logical_cpu = cpu->sysdev.id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | if (root) |
| 72 | sysfs_remove_link(&root->sysdev.kobj, |
| 73 | kobject_name(&cpu->sysdev.kobj)); |
| 74 | sysdev_remove_file(&cpu->sysdev, &attr_online); |
| 75 | |
| 76 | sysdev_unregister(&cpu->sysdev); |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 77 | cpu_sys_devices[logical_cpu] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | #else /* ... !CONFIG_HOTPLUG_CPU */ |
| 81 | static inline void register_cpu_control(struct cpu *cpu) |
| 82 | { |
| 83 | } |
| 84 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 85 | |
Vivek Goyal | 51be560 | 2006-01-09 20:51:42 -0800 | [diff] [blame] | 86 | #ifdef CONFIG_KEXEC |
| 87 | #include <linux/kexec.h> |
| 88 | |
| 89 | static ssize_t show_crash_notes(struct sys_device *dev, char *buf) |
| 90 | { |
| 91 | struct cpu *cpu = container_of(dev, struct cpu, sysdev); |
| 92 | ssize_t rc; |
| 93 | unsigned long long addr; |
| 94 | int cpunum; |
| 95 | |
| 96 | cpunum = cpu->sysdev.id; |
| 97 | |
| 98 | /* |
| 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. |
| 103 | */ |
Vivek Goyal | 51be560 | 2006-01-09 20:51:42 -0800 | [diff] [blame] | 104 | addr = __pa(per_cpu_ptr(crash_notes, cpunum)); |
| 105 | rc = sprintf(buf, "%Lx\n", addr); |
Vivek Goyal | 51be560 | 2006-01-09 20:51:42 -0800 | [diff] [blame] | 106 | return rc; |
| 107 | } |
| 108 | static SYSDEV_ATTR(crash_notes, 0400, show_crash_notes, NULL); |
| 109 | #endif |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | /* |
| 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. |
| 116 | * |
| 117 | * Initialize and register the CPU device. |
| 118 | */ |
| 119 | int __devinit register_cpu(struct cpu *cpu, int num, struct node *root) |
| 120 | { |
| 121 | int error; |
| 122 | |
| 123 | cpu->node_id = cpu_to_node(num); |
| 124 | cpu->sysdev.id = num; |
| 125 | cpu->sysdev.cls = &cpu_sysdev_class; |
| 126 | |
| 127 | error = sysdev_register(&cpu->sysdev); |
| 128 | if (!error && root) |
| 129 | error = sysfs_create_link(&root->sysdev.kobj, |
| 130 | &cpu->sysdev.kobj, |
| 131 | kobject_name(&cpu->sysdev.kobj)); |
| 132 | if (!error && !cpu->no_control) |
| 133 | register_cpu_control(cpu); |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 134 | if (!error) |
| 135 | cpu_sys_devices[num] = &cpu->sysdev; |
Vivek Goyal | 51be560 | 2006-01-09 20:51:42 -0800 | [diff] [blame] | 136 | |
| 137 | #ifdef CONFIG_KEXEC |
| 138 | if (!error) |
| 139 | error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes); |
| 140 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | return error; |
| 142 | } |
| 143 | |
Ashok Raj | ad74557 | 2005-10-30 14:59:49 -0800 | [diff] [blame] | 144 | struct sys_device *get_cpu_sysdev(int cpu) |
| 145 | { |
| 146 | if (cpu < NR_CPUS) |
| 147 | return cpu_sys_devices[cpu]; |
| 148 | else |
| 149 | return NULL; |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(get_cpu_sysdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | int __init cpu_dev_init(void) |
| 154 | { |
| 155 | return sysdev_class_register(&cpu_sysdev_class); |
| 156 | } |