Add driver auto probing for x86 features v4
[linux-flexiantxendom0-3.2.10.git] / arch / x86 / kernel / cpuid.c
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2000-2008 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 /*
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  * The lower 32 bits of the file position is used as the incoming %eax,
21  * and the upper 32 bits of the file position as the incoming %ecx,
22  * the latter intended for "counting" eax levels like eax=4.
23  *
24  * This driver uses /dev/cpu/%d/cpuid where %d is the minor number, and on
25  * an SMP box will direct the access to CPU %d.
26  */
27
28 #include <linux/module.h>
29
30 #include <linux/types.h>
31 #include <linux/errno.h>
32 #include <linux/fcntl.h>
33 #include <linux/init.h>
34 #include <linux/poll.h>
35 #include <linux/smp.h>
36 #include <linux/major.h>
37 #include <linux/fs.h>
38 #include <linux/device.h>
39 #include <linux/cpu.h>
40 #include <linux/notifier.h>
41 #include <linux/uaccess.h>
42 #include <linux/gfp.h>
43 #include <linux/slab.h>
44
45 #include <asm/processor.h>
46 #include <asm/msr.h>
47 #include <asm/system.h>
48
49 static struct class *cpuid_class;
50
51 struct cpuid_regs {
52         u32 eax, ebx, ecx, edx;
53 };
54
55 static void cpuid_smp_cpuid(void *cmd_block)
56 {
57         struct cpuid_regs *cmd = (struct cpuid_regs *)cmd_block;
58
59         cpuid_count(cmd->eax, cmd->ecx,
60                     &cmd->eax, &cmd->ebx, &cmd->ecx, &cmd->edx);
61 }
62
63 static loff_t cpuid_seek(struct file *file, loff_t offset, int orig)
64 {
65         loff_t ret;
66         struct inode *inode = file->f_mapping->host;
67
68         mutex_lock(&inode->i_mutex);
69         switch (orig) {
70         case 0:
71                 file->f_pos = offset;
72                 ret = file->f_pos;
73                 break;
74         case 1:
75                 file->f_pos += offset;
76                 ret = file->f_pos;
77                 break;
78         default:
79                 ret = -EINVAL;
80         }
81         mutex_unlock(&inode->i_mutex);
82         return ret;
83 }
84
85 static ssize_t cpuid_read(struct file *file, char __user *buf,
86                           size_t count, loff_t *ppos)
87 {
88         char __user *tmp = buf;
89         struct cpuid_regs cmd;
90         int cpu = iminor(file->f_path.dentry->d_inode);
91         u64 pos = *ppos;
92         ssize_t bytes = 0;
93         int err = 0;
94
95         if (count % 16)
96                 return -EINVAL; /* Invalid chunk size */
97
98         for (; count; count -= 16) {
99                 cmd.eax = pos;
100                 cmd.ecx = pos >> 32;
101                 err = smp_call_function_single(cpu, cpuid_smp_cpuid, &cmd, 1);
102                 if (err)
103                         break;
104                 if (copy_to_user(tmp, &cmd, 16)) {
105                         err = -EFAULT;
106                         break;
107                 }
108                 tmp += 16;
109                 bytes += 16;
110                 *ppos = ++pos;
111         }
112
113         return bytes ? bytes : err;
114 }
115
116 static int cpuid_open(struct inode *inode, struct file *file)
117 {
118         unsigned int cpu;
119         struct cpuinfo_x86 *c;
120
121         cpu = iminor(file->f_path.dentry->d_inode);
122         if (cpu >= nr_cpu_ids || !cpu_online(cpu))
123                 return -ENXIO;  /* No such CPU */
124
125         c = &cpu_data(cpu);
126         if (c->cpuid_level < 0)
127                 return -EIO;    /* CPUID not supported */
128
129         return 0;
130 }
131
132 /*
133  * File operations we support
134  */
135 static const struct file_operations cpuid_fops = {
136         .owner = THIS_MODULE,
137         .llseek = cpuid_seek,
138         .read = cpuid_read,
139         .open = cpuid_open,
140 };
141
142 static ssize_t print_cpu_modalias(struct device *dev,
143                                   struct device_attribute *attr,
144                                   char *bufptr)
145 {
146         int size = PAGE_SIZE;
147         int i, n;
148         char *buf = bufptr;
149
150         n = snprintf(buf, size, "x86cpu:vendor:%04X:family:"
151                      "%04X:model:%04X:feature:",
152                 boot_cpu_data.x86_vendor,
153                 boot_cpu_data.x86,
154                 boot_cpu_data.x86_model);
155         size -= n;
156         buf += n;
157         size -= 2;
158         for (i = 0; i < NCAPINTS*32; i++) {
159                 if (boot_cpu_has(i)) {
160                         n = snprintf(buf, size, ",%04X", i);
161                         if (n < 0) {
162                                 WARN(1, "x86 features overflow page\n");
163                                 break;
164                         }
165                         size -= n;
166                         buf += n;
167                 }
168         }
169         *buf++ = ',';
170         *buf++ = '\n';
171         return buf - bufptr;
172 }
173
174 static DEVICE_ATTR(modalias, 0444, print_cpu_modalias, NULL);
175
176 static __cpuinit int cpuid_device_create(int cpu)
177 {
178         struct device *dev;
179         int err;
180
181         dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, cpu), NULL,
182                             "cpu%d", cpu);
183         if (IS_ERR(dev))
184                 return PTR_ERR(dev);
185
186         err = device_create_file(dev, &dev_attr_modalias);
187         if (err) {
188                 /* keep device around on error. attribute is optional. */
189                 err = 0;
190         }
191
192         return 0;
193 }
194
195 static void cpuid_device_destroy(int cpu)
196 {
197         device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
198 }
199
200 static int __cpuinit cpuid_class_cpu_callback(struct notifier_block *nfb,
201                                               unsigned long action,
202                                               void *hcpu)
203 {
204         unsigned int cpu = (unsigned long)hcpu;
205         int err = 0;
206
207         switch (action) {
208         case CPU_UP_PREPARE:
209                 err = cpuid_device_create(cpu);
210                 break;
211         case CPU_UP_CANCELED:
212         case CPU_UP_CANCELED_FROZEN:
213         case CPU_DEAD:
214                 cpuid_device_destroy(cpu);
215                 break;
216         }
217         return notifier_from_errno(err);
218 }
219
220 static struct notifier_block __refdata cpuid_class_cpu_notifier =
221 {
222         .notifier_call = cpuid_class_cpu_callback,
223 };
224
225 static char *cpuid_devnode(struct device *dev, umode_t *mode)
226 {
227         return kasprintf(GFP_KERNEL, "cpu/%u/cpuid", MINOR(dev->devt));
228 }
229
230 static int cpuid_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
231 {
232         char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
233         if (buf) {
234                 print_cpu_modalias(NULL, NULL, buf);
235                 add_uevent_var(env, "MODALIAS=%s", buf);
236                 kfree(buf);
237         }
238         return 0;
239 }
240
241 static int __init cpuid_init(void)
242 {
243         int i, err = 0;
244         i = 0;
245
246         if (__register_chrdev(CPUID_MAJOR, 0, NR_CPUS,
247                               "cpu/cpuid", &cpuid_fops)) {
248                 printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
249                        CPUID_MAJOR);
250                 err = -EBUSY;
251                 goto out;
252         }
253         cpuid_class = class_create(THIS_MODULE, "cpuid");
254         if (IS_ERR(cpuid_class)) {
255                 err = PTR_ERR(cpuid_class);
256                 goto out_chrdev;
257         }
258         cpuid_class->devnode = cpuid_devnode;
259         cpuid_class->dev_uevent = cpuid_dev_uevent;
260         for_each_online_cpu(i) {
261                 err = cpuid_device_create(i);
262                 if (err != 0)
263                         goto out_class;
264         }
265         register_hotcpu_notifier(&cpuid_class_cpu_notifier);
266
267         err = 0;
268         goto out;
269
270 out_class:
271         i = 0;
272         for_each_online_cpu(i) {
273                 cpuid_device_destroy(i);
274         }
275         class_destroy(cpuid_class);
276 out_chrdev:
277         __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
278 out:
279         return err;
280 }
281
282 static void __exit cpuid_exit(void)
283 {
284         int cpu = 0;
285
286         for_each_online_cpu(cpu)
287                 cpuid_device_destroy(cpu);
288         class_destroy(cpuid_class);
289         __unregister_chrdev(CPUID_MAJOR, 0, NR_CPUS, "cpu/cpuid");
290         unregister_hotcpu_notifier(&cpuid_class_cpu_notifier);
291 }
292
293 module_init(cpuid_init);
294 module_exit(cpuid_exit);
295
296 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
297 MODULE_DESCRIPTION("x86 generic CPUID driver");
298 MODULE_LICENSE("GPL");