Merge branch 'for-linus' of git://git390.osdl.marist.edu/pub/scm/linux-2.6
[pandora-kernel.git] / arch / i386 / kernel / cpuid.c
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 /*
14  * cpuid.c
15  *
16  * x86 CPUID access device
17  *
18  * This device is accessed by lseek() to the appropriate CPUID level
19  * and then read in chunks of 16 bytes.  A larger size means multiple
20  * reads of consecutive levels.
21  *
22  * This driver uses /dev/cpu/%d/cpuid where %d is the minor number, and on
23  * an SMP box will direct the access to CPU %d.
24  */
25
26 #include <linux/module.h>
27
28 #include <linux/types.h>
29 #include <linux/errno.h>
30 #include <linux/fcntl.h>
31 #include <linux/init.h>
32 #include <linux/poll.h>
33 #include <linux/smp.h>
34 #include <linux/major.h>
35 #include <linux/fs.h>
36 #include <linux/smp_lock.h>
37 #include <linux/device.h>
38 #include <linux/cpu.h>
39 #include <linux/notifier.h>
40
41 #include <asm/processor.h>
42 #include <asm/msr.h>
43 #include <asm/uaccess.h>
44 #include <asm/system.h>
45
46 static struct class *cpuid_class;
47
48 #ifdef CONFIG_SMP
49
50 struct cpuid_command {
51         int cpu;
52         u32 reg;
53         u32 *data;
54 };
55
56 static void cpuid_smp_cpuid(void *cmd_block)
57 {
58         struct cpuid_command *cmd = (struct cpuid_command *)cmd_block;
59
60         if (cmd->cpu == smp_processor_id())
61                 cpuid(cmd->reg, &cmd->data[0], &cmd->data[1], &cmd->data[2],
62                       &cmd->data[3]);
63 }
64
65 static inline void do_cpuid(int cpu, u32 reg, u32 * data)
66 {
67         struct cpuid_command cmd;
68
69         preempt_disable();
70         if (cpu == smp_processor_id()) {
71                 cpuid(reg, &data[0], &data[1], &data[2], &data[3]);
72         } else {
73                 cmd.cpu = cpu;
74                 cmd.reg = reg;
75                 cmd.data = data;
76
77                 smp_call_function(cpuid_smp_cpuid, &cmd, 1, 1);
78         }
79         preempt_enable();
80 }
81 #else                           /* ! CONFIG_SMP */
82
83 static inline void do_cpuid(int cpu, u32 reg, u32 * data)
84 {
85         cpuid(reg, &data[0], &data[1], &data[2], &data[3]);
86 }
87
88 #endif                          /* ! CONFIG_SMP */
89
90 static loff_t cpuid_seek(struct file *file, loff_t offset, int orig)
91 {
92         loff_t ret;
93
94         lock_kernel();
95
96         switch (orig) {
97         case 0:
98                 file->f_pos = offset;
99                 ret = file->f_pos;
100                 break;
101         case 1:
102                 file->f_pos += offset;
103                 ret = file->f_pos;
104                 break;
105         default:
106                 ret = -EINVAL;
107         }
108
109         unlock_kernel();
110         return ret;
111 }
112
113 static ssize_t cpuid_read(struct file *file, char __user *buf,
114                           size_t count, loff_t * ppos)
115 {
116         char __user *tmp = buf;
117         u32 data[4];
118         u32 reg = *ppos;
119         int cpu = iminor(file->f_path.dentry->d_inode);
120
121         if (count % 16)
122                 return -EINVAL; /* Invalid chunk size */
123
124         for (; count; count -= 16) {
125                 do_cpuid(cpu, reg, data);
126                 if (copy_to_user(tmp, &data, 16))
127                         return -EFAULT;
128                 tmp += 16;
129                 *ppos = reg++;
130         }
131
132         return tmp - buf;
133 }
134
135 static int cpuid_open(struct inode *inode, struct file *file)
136 {
137         unsigned int cpu = iminor(file->f_path.dentry->d_inode);
138         struct cpuinfo_x86 *c = &(cpu_data)[cpu];
139
140         if (cpu >= NR_CPUS || !cpu_online(cpu))
141                 return -ENXIO;  /* No such CPU */
142         if (c->cpuid_level < 0)
143                 return -EIO;    /* CPUID not supported */
144
145         return 0;
146 }
147
148 /*
149  * File operations we support
150  */
151 static struct file_operations cpuid_fops = {
152         .owner = THIS_MODULE,
153         .llseek = cpuid_seek,
154         .read = cpuid_read,
155         .open = cpuid_open,
156 };
157
158 static int cpuid_device_create(int i)
159 {
160         int err = 0;
161         struct device *dev;
162
163         dev = device_create(cpuid_class, NULL, MKDEV(CPUID_MAJOR, i), "cpu%d",i);
164         if (IS_ERR(dev))
165                 err = PTR_ERR(dev);
166         return err;
167 }
168
169 static int cpuid_class_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
170 {
171         unsigned int cpu = (unsigned long)hcpu;
172
173         switch (action) {
174         case CPU_ONLINE:
175                 cpuid_device_create(cpu);
176                 break;
177         case CPU_DEAD:
178                 device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
179                 break;
180         }
181         return NOTIFY_OK;
182 }
183
184 static struct notifier_block __cpuinitdata cpuid_class_cpu_notifier =
185 {
186         .notifier_call = cpuid_class_cpu_callback,
187 };
188
189 static int __init cpuid_init(void)
190 {
191         int i, err = 0;
192         i = 0;
193
194         if (register_chrdev(CPUID_MAJOR, "cpu/cpuid", &cpuid_fops)) {
195                 printk(KERN_ERR "cpuid: unable to get major %d for cpuid\n",
196                        CPUID_MAJOR);
197                 err = -EBUSY;
198                 goto out;
199         }
200         cpuid_class = class_create(THIS_MODULE, "cpuid");
201         if (IS_ERR(cpuid_class)) {
202                 err = PTR_ERR(cpuid_class);
203                 goto out_chrdev;
204         }
205         for_each_online_cpu(i) {
206                 err = cpuid_device_create(i);
207                 if (err != 0) 
208                         goto out_class;
209         }
210         register_hotcpu_notifier(&cpuid_class_cpu_notifier);
211
212         err = 0;
213         goto out;
214
215 out_class:
216         i = 0;
217         for_each_online_cpu(i) {
218                 device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, i));
219         }
220         class_destroy(cpuid_class);
221 out_chrdev:
222         unregister_chrdev(CPUID_MAJOR, "cpu/cpuid");    
223 out:
224         return err;
225 }
226
227 static void __exit cpuid_exit(void)
228 {
229         int cpu = 0;
230
231         for_each_online_cpu(cpu)
232                 device_destroy(cpuid_class, MKDEV(CPUID_MAJOR, cpu));
233         class_destroy(cpuid_class);
234         unregister_chrdev(CPUID_MAJOR, "cpu/cpuid");
235         unregister_hotcpu_notifier(&cpuid_class_cpu_notifier);
236 }
237
238 module_init(cpuid_init);
239 module_exit(cpuid_exit);
240
241 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
242 MODULE_DESCRIPTION("x86 generic CPUID driver");
243 MODULE_LICENSE("GPL");