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