Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
| 2 | * |
| 3 | * Copyright 2000 H. Peter Anvin - All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139, |
| 8 | * USA; either version 2 of the License, or (at your option) any later |
| 9 | * version; incorporated herein by reference. |
| 10 | * |
| 11 | * ----------------------------------------------------------------------- */ |
| 12 | |
| 13 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * x86 CPUID access device |
| 15 | * |
| 16 | * This device is accessed by lseek() to the appropriate CPUID level |
| 17 | * and then read in chunks of 16 bytes. A larger size means multiple |
| 18 | * reads of consecutive levels. |
| 19 | * |
| 20 | * This driver uses /dev/cpu/%d/cpuid where %d is the minor number, and on |
| 21 | * an SMP box will direct the access to CPU %d. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/fcntl.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/poll.h> |
| 31 | #include <linux/smp.h> |
| 32 | #include <linux/major.h> |
| 33 | #include <linux/fs.h> |
| 34 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/device.h> |
| 36 | #include <linux/cpu.h> |
| 37 | #include <linux/notifier.h> |
| 38 | |
| 39 | #include <asm/processor.h> |
| 40 | #include <asm/msr.h> |
| 41 | #include <asm/uaccess.h> |
| 42 | #include <asm/system.h> |
| 43 | |
gregkh@suse.de | 8874b41 | 2005-03-23 09:56:34 -0800 | [diff] [blame] | 44 | static struct class *cpuid_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | #ifdef CONFIG_SMP |
| 47 | |
| 48 | struct cpuid_command { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | u32 reg; |
| 50 | u32 *data; |
| 51 | }; |
| 52 | |
| 53 | static void cpuid_smp_cpuid(void *cmd_block) |
| 54 | { |
| 55 | struct cpuid_command *cmd = (struct cpuid_command *)cmd_block; |
| 56 | |
Alexey Dobriyan | ad4e680 | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 57 | cpuid(cmd->reg, &cmd->data[0], &cmd->data[1], &cmd->data[2], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | &cmd->data[3]); |
| 59 | } |
| 60 | |
| 61 | static inline void do_cpuid(int cpu, u32 reg, u32 * data) |
| 62 | { |
| 63 | struct cpuid_command cmd; |
| 64 | |
| 65 | preempt_disable(); |
| 66 | if (cpu == smp_processor_id()) { |
| 67 | cpuid(reg, &data[0], &data[1], &data[2], &data[3]); |
| 68 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | cmd.reg = reg; |
| 70 | cmd.data = data; |
| 71 | |
Alexey Dobriyan | ad4e680 | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 72 | smp_call_function_single(cpu, cpuid_smp_cpuid, &cmd, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | preempt_enable(); |
| 75 | } |
| 76 | #else /* ! CONFIG_SMP */ |
| 77 | |
| 78 | static inline void do_cpuid(int cpu, u32 reg, u32 * data) |
| 79 | { |
| 80 | cpuid(reg, &data[0], &data[1], &data[2], &data[3]); |
| 81 | } |
| 82 | |
| 83 | #endif /* ! CONFIG_SMP */ |
| 84 | |
| 85 | static loff_t cpuid_seek(struct file *file, loff_t offset, int orig) |
| 86 | { |
| 87 | loff_t ret; |
| 88 | |
| 89 | lock_kernel(); |
| 90 | |
| 91 | switch (orig) { |
| 92 | case 0: |
| 93 | file->f_pos = offset; |
| 94 | ret = file->f_pos; |
| 95 | break; |
| 96 | case 1: |
| 97 | file->f_pos += offset; |
| 98 | ret = file->f_pos; |
| 99 | break; |
| 100 | default: |
| 101 | ret = -EINVAL; |
| 102 | } |
| 103 | |
| 104 | unlock_kernel(); |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | static ssize_t cpuid_read(struct file *file, char __user *buf, |
| 109 | size_t count, loff_t * ppos) |
| 110 | { |
| 111 | char __user *tmp = buf; |
| 112 | u32 data[4]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | u32 reg = *ppos; |
Josef "Jeff" Sipek | aab4c5a | 2006-12-08 02:36:42 -0800 | [diff] [blame] | 114 | int cpu = iminor(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | if (count % 16) |
| 117 | return -EINVAL; /* Invalid chunk size */ |
| 118 | |
Daniel Marjamaki | 6b7f430 | 2006-01-06 00:12:13 -0800 | [diff] [blame] | 119 | for (; count; count -= 16) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | do_cpuid(cpu, reg, data); |
| 121 | if (copy_to_user(tmp, &data, 16)) |
| 122 | return -EFAULT; |
| 123 | tmp += 16; |
| 124 | *ppos = reg++; |
| 125 | } |
| 126 | |
| 127 | return tmp - buf; |
| 128 | } |
| 129 | |
| 130 | static int cpuid_open(struct inode *inode, struct file *file) |
| 131 | { |
Josef "Jeff" Sipek | aab4c5a | 2006-12-08 02:36:42 -0800 | [diff] [blame] | 132 | unsigned int cpu = iminor(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | struct cpuinfo_x86 *c = &(cpu_data)[cpu]; |
| 134 | |
| 135 | if (cpu >= NR_CPUS || !cpu_online(cpu)) |
| 136 | return -ENXIO; /* No such CPU */ |
| 137 | if (c->cpuid_level < 0) |
| 138 | return -EIO; /* CPUID not supported */ |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * File operations we support |
| 145 | */ |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 146 | static const struct file_operations cpuid_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | .owner = THIS_MODULE, |
| 148 | .llseek = cpuid_seek, |
| 149 | .read = cpuid_read, |
| 150 | .open = cpuid_open, |
| 151 | }; |
| 152 | |
Satyam Sharma | 1e32b07 | 2007-10-17 18:04:36 +0200 | [diff] [blame^] | 153 | static int __cpuinit cpuid_device_create(int i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
| 155 | int err = 0; |
Greg Kroah-Hartman | 07accdc | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 156 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Greg Kroah-Hartman | 07accdc | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 158 | dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, i), "cpu%d",i); |
| 159 | if (IS_ERR(dev)) |
| 160 | err = PTR_ERR(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return err; |
| 162 | } |
| 163 | |
Satyam Sharma | 1e32b07 | 2007-10-17 18:04:36 +0200 | [diff] [blame^] | 164 | static int __cpuinit cpuid_class_cpu_callback(struct notifier_block *nfb, |
| 165 | unsigned long action, |
| 166 | void *hcpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
| 168 | unsigned int cpu = (unsigned long)hcpu; |
| 169 | |
| 170 | switch (action) { |
| 171 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 172 | case CPU_ONLINE_FROZEN: |
Greg Kroah-Hartman | 07accdc | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 173 | cpuid_device_create(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | break; |
| 175 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 176 | case CPU_DEAD_FROZEN: |
Greg Kroah-Hartman | 07accdc | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 177 | device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | return NOTIFY_OK; |
| 181 | } |
| 182 | |
Chandra Seetharaman | 74b85f3 | 2006-06-27 02:54:09 -0700 | [diff] [blame] | 183 | static struct notifier_block __cpuinitdata cpuid_class_cpu_notifier = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
| 185 | .notifier_call = cpuid_class_cpu_callback, |
| 186 | }; |
| 187 | |
| 188 | static int __init cpuid_init(void) |
| 189 | { |
| 190 | int i, err = 0; |
| 191 | i = 0; |
| 192 | |
| 193 | if (register_chrdev(CPUID_MAJOR, "cpu/cpuid", &cpuid_fops)) { |
| 194 | printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n", |
| 195 | CPUID_MAJOR); |
| 196 | err = -EBUSY; |
| 197 | goto out; |
| 198 | } |
gregkh@suse.de | 8874b41 | 2005-03-23 09:56:34 -0800 | [diff] [blame] | 199 | cpuid_class = class_create(THIS_MODULE, "cpuid"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | if (IS_ERR(cpuid_class)) { |
| 201 | err = PTR_ERR(cpuid_class); |
| 202 | goto out_chrdev; |
| 203 | } |
| 204 | for_each_online_cpu(i) { |
Greg Kroah-Hartman | 07accdc | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 205 | err = cpuid_device_create(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (err != 0) |
| 207 | goto out_class; |
| 208 | } |
OGAWA Hirofumi | b6a7c79 | 2006-07-03 17:32:20 -0700 | [diff] [blame] | 209 | register_hotcpu_notifier(&cpuid_class_cpu_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | err = 0; |
| 212 | goto out; |
| 213 | |
| 214 | out_class: |
| 215 | i = 0; |
| 216 | for_each_online_cpu(i) { |
Greg Kroah-Hartman | 07accdc | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 217 | device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
gregkh@suse.de | 8874b41 | 2005-03-23 09:56:34 -0800 | [diff] [blame] | 219 | class_destroy(cpuid_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | out_chrdev: |
| 221 | unregister_chrdev(CPUID_MAJOR, "cpu/cpuid"); |
| 222 | out: |
| 223 | return err; |
| 224 | } |
| 225 | |
| 226 | static void __exit cpuid_exit(void) |
| 227 | { |
| 228 | int cpu = 0; |
| 229 | |
| 230 | for_each_online_cpu(cpu) |
Greg Kroah-Hartman | 07accdc | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 231 | device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu)); |
gregkh@suse.de | 8874b41 | 2005-03-23 09:56:34 -0800 | [diff] [blame] | 232 | class_destroy(cpuid_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | unregister_chrdev(CPUID_MAJOR, "cpu/cpuid"); |
OGAWA Hirofumi | b6a7c79 | 2006-07-03 17:32:20 -0700 | [diff] [blame] | 234 | unregister_hotcpu_notifier(&cpuid_class_cpu_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | module_init(cpuid_init); |
| 238 | module_exit(cpuid_exit); |
| 239 | |
| 240 | MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>"); |
| 241 | MODULE_DESCRIPTION("x86 generic CPUID driver"); |
| 242 | MODULE_LICENSE("GPL"); |