Pull release into acpica branch
[pandora-kernel.git] / arch / i386 / kernel / msr.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  * msr.c
15  *
16  * x86 MSR access device
17  *
18  * This device is accessed by lseek() to the appropriate register number
19  * and then read/write in chunks of 8 bytes.  A larger size means multiple
20  * reads or writes of the same register.
21  *
22  * This driver uses /dev/cpu/%d/msr 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 #include <linux/config.h>
28
29 #include <linux/types.h>
30 #include <linux/errno.h>
31 #include <linux/fcntl.h>
32 #include <linux/init.h>
33 #include <linux/poll.h>
34 #include <linux/smp.h>
35 #include <linux/smp_lock.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
42 #include <asm/processor.h>
43 #include <asm/msr.h>
44 #include <asm/uaccess.h>
45 #include <asm/system.h>
46
47 static struct class *msr_class;
48
49 static inline int wrmsr_eio(u32 reg, u32 eax, u32 edx)
50 {
51         int err;
52
53         err = wrmsr_safe(reg, eax, edx);
54         if (err)
55                 err = -EIO;
56         return err;
57 }
58
59 static inline int rdmsr_eio(u32 reg, u32 *eax, u32 *edx)
60 {
61         int err;
62
63         err = rdmsr_safe(reg, eax, edx);
64         if (err)
65                 err = -EIO;
66         return err;
67 }
68
69 #ifdef CONFIG_SMP
70
71 struct msr_command {
72         int cpu;
73         int err;
74         u32 reg;
75         u32 data[2];
76 };
77
78 static void msr_smp_wrmsr(void *cmd_block)
79 {
80         struct msr_command *cmd = (struct msr_command *)cmd_block;
81
82         if (cmd->cpu == smp_processor_id())
83                 cmd->err = wrmsr_eio(cmd->reg, cmd->data[0], cmd->data[1]);
84 }
85
86 static void msr_smp_rdmsr(void *cmd_block)
87 {
88         struct msr_command *cmd = (struct msr_command *)cmd_block;
89
90         if (cmd->cpu == smp_processor_id())
91                 cmd->err = rdmsr_eio(cmd->reg, &cmd->data[0], &cmd->data[1]);
92 }
93
94 static inline int do_wrmsr(int cpu, u32 reg, u32 eax, u32 edx)
95 {
96         struct msr_command cmd;
97         int ret;
98
99         preempt_disable();
100         if (cpu == smp_processor_id()) {
101                 ret = wrmsr_eio(reg, eax, edx);
102         } else {
103                 cmd.cpu = cpu;
104                 cmd.reg = reg;
105                 cmd.data[0] = eax;
106                 cmd.data[1] = edx;
107
108                 smp_call_function(msr_smp_wrmsr, &cmd, 1, 1);
109                 ret = cmd.err;
110         }
111         preempt_enable();
112         return ret;
113 }
114
115 static inline int do_rdmsr(int cpu, u32 reg, u32 * eax, u32 * edx)
116 {
117         struct msr_command cmd;
118         int ret;
119
120         preempt_disable();
121         if (cpu == smp_processor_id()) {
122                 ret = rdmsr_eio(reg, eax, edx);
123         } else {
124                 cmd.cpu = cpu;
125                 cmd.reg = reg;
126
127                 smp_call_function(msr_smp_rdmsr, &cmd, 1, 1);
128
129                 *eax = cmd.data[0];
130                 *edx = cmd.data[1];
131
132                 ret = cmd.err;
133         }
134         preempt_enable();
135         return ret;
136 }
137
138 #else                           /* ! CONFIG_SMP */
139
140 static inline int do_wrmsr(int cpu, u32 reg, u32 eax, u32 edx)
141 {
142         return wrmsr_eio(reg, eax, edx);
143 }
144
145 static inline int do_rdmsr(int cpu, u32 reg, u32 *eax, u32 *edx)
146 {
147         return rdmsr_eio(reg, eax, edx);
148 }
149
150 #endif                          /* ! CONFIG_SMP */
151
152 static loff_t msr_seek(struct file *file, loff_t offset, int orig)
153 {
154         loff_t ret = -EINVAL;
155
156         lock_kernel();
157         switch (orig) {
158         case 0:
159                 file->f_pos = offset;
160                 ret = file->f_pos;
161                 break;
162         case 1:
163                 file->f_pos += offset;
164                 ret = file->f_pos;
165         }
166         unlock_kernel();
167         return ret;
168 }
169
170 static ssize_t msr_read(struct file *file, char __user * buf,
171                         size_t count, loff_t * ppos)
172 {
173         u32 __user *tmp = (u32 __user *) buf;
174         u32 data[2];
175         size_t rv;
176         u32 reg = *ppos;
177         int cpu = iminor(file->f_dentry->d_inode);
178         int err;
179
180         if (count % 8)
181                 return -EINVAL; /* Invalid chunk size */
182
183         for (rv = 0; count; count -= 8) {
184                 err = do_rdmsr(cpu, reg, &data[0], &data[1]);
185                 if (err)
186                         return err;
187                 if (copy_to_user(tmp, &data, 8))
188                         return -EFAULT;
189                 tmp += 2;
190         }
191
192         return ((char __user *)tmp) - buf;
193 }
194
195 static ssize_t msr_write(struct file *file, const char __user *buf,
196                          size_t count, loff_t *ppos)
197 {
198         const u32 __user *tmp = (const u32 __user *)buf;
199         u32 data[2];
200         size_t rv;
201         u32 reg = *ppos;
202         int cpu = iminor(file->f_dentry->d_inode);
203         int err;
204
205         if (count % 8)
206                 return -EINVAL; /* Invalid chunk size */
207
208         for (rv = 0; count; count -= 8) {
209                 if (copy_from_user(&data, tmp, 8))
210                         return -EFAULT;
211                 err = do_wrmsr(cpu, reg, data[0], data[1]);
212                 if (err)
213                         return err;
214                 tmp += 2;
215         }
216
217         return ((char __user *)tmp) - buf;
218 }
219
220 static int msr_open(struct inode *inode, struct file *file)
221 {
222         unsigned int cpu = iminor(file->f_dentry->d_inode);
223         struct cpuinfo_x86 *c = &(cpu_data)[cpu];
224
225         if (cpu >= NR_CPUS || !cpu_online(cpu))
226                 return -ENXIO;  /* No such CPU */
227         if (!cpu_has(c, X86_FEATURE_MSR))
228                 return -EIO;    /* MSR not supported */
229
230         return 0;
231 }
232
233 /*
234  * File operations we support
235  */
236 static struct file_operations msr_fops = {
237         .owner = THIS_MODULE,
238         .llseek = msr_seek,
239         .read = msr_read,
240         .write = msr_write,
241         .open = msr_open,
242 };
243
244 static int msr_class_device_create(int i)
245 {
246         int err = 0;
247         struct class_device *class_err;
248
249         class_err = class_device_create(msr_class, NULL, MKDEV(MSR_MAJOR, i), NULL, "msr%d",i);
250         if (IS_ERR(class_err)) 
251                 err = PTR_ERR(class_err);
252         return err;
253 }
254
255 static int __devinit msr_class_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
256 {
257         unsigned int cpu = (unsigned long)hcpu;
258
259         switch (action) {
260         case CPU_ONLINE:
261                 msr_class_device_create(cpu);
262                 break;
263         case CPU_DEAD:
264                 class_device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
265                 break;
266         }
267         return NOTIFY_OK;
268 }
269
270 static struct notifier_block msr_class_cpu_notifier =
271 {
272         .notifier_call = msr_class_cpu_callback,
273 };
274
275 static int __init msr_init(void)
276 {
277         int i, err = 0;
278         i = 0;
279
280         if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
281                 printk(KERN_ERR "msr: unable to get major %d for msr\n",
282                        MSR_MAJOR);
283                 err = -EBUSY;
284                 goto out;
285         }
286         msr_class = class_create(THIS_MODULE, "msr");
287         if (IS_ERR(msr_class)) {
288                 err = PTR_ERR(msr_class);
289                 goto out_chrdev;
290         }
291         for_each_online_cpu(i) {
292                 err = msr_class_device_create(i);
293                 if (err != 0)
294                         goto out_class;
295         }
296         register_cpu_notifier(&msr_class_cpu_notifier);
297
298         err = 0;
299         goto out;
300
301 out_class:
302         i = 0;
303         for_each_online_cpu(i)
304                 class_device_destroy(msr_class, MKDEV(MSR_MAJOR, i));
305         class_destroy(msr_class);
306 out_chrdev:
307         unregister_chrdev(MSR_MAJOR, "cpu/msr");
308 out:
309         return err;
310 }
311
312 static void __exit msr_exit(void)
313 {
314         int cpu = 0;
315         for_each_online_cpu(cpu)
316                 class_device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
317         class_destroy(msr_class);
318         unregister_chrdev(MSR_MAJOR, "cpu/msr");
319         unregister_cpu_notifier(&msr_class_cpu_notifier);
320 }
321
322 module_init(msr_init);
323 module_exit(msr_exit)
324
325 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
326 MODULE_DESCRIPTION("x86 generic MSR driver");
327 MODULE_LICENSE("GPL");