]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/i386/kernel/msr.c
[PATCH] x86: more asm cleanups
[net-next-2.6.git] / arch / i386 / kernel / msr.c
CommitLineData
1da177e4
LT
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
8874b414 47static struct class *msr_class;
1da177e4 48
1da177e4
LT
49static inline int wrmsr_eio(u32 reg, u32 eax, u32 edx)
50{
51 int err;
52
f2ab4461
ZA
53 err = wrmsr_safe(reg, eax, edx);
54 if (err)
55 err = -EIO;
1da177e4
LT
56 return err;
57}
58
59static inline int rdmsr_eio(u32 reg, u32 *eax, u32 *edx)
60{
61 int err;
62
f2ab4461
ZA
63 err = rdmsr_safe(reg, eax, edx);
64 if (err)
65 err = -EIO;
1da177e4
LT
66 return err;
67}
68
69#ifdef CONFIG_SMP
70
71struct msr_command {
72 int cpu;
73 int err;
74 u32 reg;
75 u32 data[2];
76};
77
78static 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
86static 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
94static 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
115static 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
140static inline int do_wrmsr(int cpu, u32 reg, u32 eax, u32 edx)
141{
142 return wrmsr_eio(reg, eax, edx);
143}
144
145static 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
152static 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
170static 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
195static 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
220static 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 */
236static 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
8874b414 244static int msr_class_device_create(int i)
1da177e4
LT
245{
246 int err = 0;
247 struct class_device *class_err;
248
8874b414 249 class_err = class_device_create(msr_class, MKDEV(MSR_MAJOR, i), NULL, "msr%d",i);
1da177e4
LT
250 if (IS_ERR(class_err))
251 err = PTR_ERR(class_err);
252 return err;
253}
254
255static 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:
8874b414 261 msr_class_device_create(cpu);
1da177e4
LT
262 break;
263 case CPU_DEAD:
8874b414 264 class_device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
1da177e4
LT
265 break;
266 }
267 return NOTIFY_OK;
268}
269
270static struct notifier_block msr_class_cpu_notifier =
271{
272 .notifier_call = msr_class_cpu_callback,
273};
274
275static 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 }
8874b414 286 msr_class = class_create(THIS_MODULE, "msr");
1da177e4
LT
287 if (IS_ERR(msr_class)) {
288 err = PTR_ERR(msr_class);
289 goto out_chrdev;
290 }
291 for_each_online_cpu(i) {
8874b414 292 err = msr_class_device_create(i);
1da177e4
LT
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
301out_class:
302 i = 0;
303 for_each_online_cpu(i)
8874b414
GKH
304 class_device_destroy(msr_class, MKDEV(MSR_MAJOR, i));
305 class_destroy(msr_class);
1da177e4
LT
306out_chrdev:
307 unregister_chrdev(MSR_MAJOR, "cpu/msr");
308out:
309 return err;
310}
311
312static void __exit msr_exit(void)
313{
314 int cpu = 0;
315 for_each_online_cpu(cpu)
8874b414
GKH
316 class_device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
317 class_destroy(msr_class);
1da177e4
LT
318 unregister_chrdev(MSR_MAJOR, "cpu/msr");
319 unregister_cpu_notifier(&msr_class_cpu_notifier);
320}
321
322module_init(msr_init);
323module_exit(msr_exit)
324
325MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
326MODULE_DESCRIPTION("x86 generic MSR driver");
327MODULE_LICENSE("GPL");