1 /* ----------------------------------------------------------------------- *
3 * Copyright 2000 H. Peter Anvin - All Rights Reserved
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.
11 * ----------------------------------------------------------------------- */
16 * x86 MSR access device
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.
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.
26 #include <linux/module.h>
27 #include <linux/config.h>
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>
39 #include <asm/processor.h>
41 #include <asm/uaccess.h>
42 #include <asm/system.h>
44 /* Note: "err" is handled in a funny way below. Otherwise one version
45 of gcc or another breaks. */
47 static inline int wrmsr_eio(u32 reg, u32 eax, u32 edx)
51 asm volatile ("1: wrmsr\n"
53 ".section .fixup,\"ax\"\n"
57 ".section __ex_table,\"a\"\n"
58 " .align 8\n" " .quad 1b,3b\n" ".previous":"=&bDS" (err)
59 :"a"(eax), "d"(edx), "c"(reg), "i"(-EIO), "0"(0));
64 static inline int rdmsr_eio(u32 reg, u32 *eax, u32 *edx)
68 asm volatile ("1: rdmsr\n"
70 ".section .fixup,\"ax\"\n"
74 ".section __ex_table,\"a\"\n"
77 ".previous":"=&bDS" (err), "=a"(*eax), "=d"(*edx)
78 :"c"(reg), "i"(-EIO), "0"(0));
92 static void msr_smp_wrmsr(void *cmd_block)
94 struct msr_command *cmd = (struct msr_command *)cmd_block;
96 if (cmd->cpu == smp_processor_id())
97 cmd->err = wrmsr_eio(cmd->reg, cmd->data[0], cmd->data[1]);
100 static void msr_smp_rdmsr(void *cmd_block)
102 struct msr_command *cmd = (struct msr_command *)cmd_block;
104 if (cmd->cpu == smp_processor_id())
105 cmd->err = rdmsr_eio(cmd->reg, &cmd->data[0], &cmd->data[1]);
108 static inline int do_wrmsr(int cpu, u32 reg, u32 eax, u32 edx)
110 struct msr_command cmd;
114 if (cpu == smp_processor_id()) {
115 ret = wrmsr_eio(reg, eax, edx);
122 smp_call_function(msr_smp_wrmsr, &cmd, 1, 1);
129 static inline int do_rdmsr(int cpu, u32 reg, u32 * eax, u32 * edx)
131 struct msr_command cmd;
135 if (cpu == smp_processor_id()) {
136 ret = rdmsr_eio(reg, eax, edx);
141 smp_call_function(msr_smp_rdmsr, &cmd, 1, 1);
152 #else /* ! CONFIG_SMP */
154 static inline int do_wrmsr(int cpu, u32 reg, u32 eax, u32 edx)
156 return wrmsr_eio(reg, eax, edx);
159 static inline int do_rdmsr(int cpu, u32 reg, u32 *eax, u32 *edx)
161 return rdmsr_eio(reg, eax, edx);
164 #endif /* ! CONFIG_SMP */
166 static loff_t msr_seek(struct file *file, loff_t offset, int orig)
168 loff_t ret = -EINVAL;
173 file->f_pos = offset;
177 file->f_pos += offset;
184 static ssize_t msr_read(struct file *file, char __user * buf,
185 size_t count, loff_t * ppos)
187 u32 __user *tmp = (u32 __user *) buf;
191 int cpu = iminor(file->f_dentry->d_inode);
195 return -EINVAL; /* Invalid chunk size */
197 for (rv = 0; count; count -= 8) {
198 err = do_rdmsr(cpu, reg, &data[0], &data[1]);
201 if (copy_to_user(tmp, &data, 8))
206 return ((char __user *)tmp) - buf;
209 static ssize_t msr_write(struct file *file, const char __user *buf,
210 size_t count, loff_t *ppos)
212 const u32 __user *tmp = (const u32 __user *)buf;
216 int cpu = iminor(file->f_dentry->d_inode);
220 return -EINVAL; /* Invalid chunk size */
222 for (rv = 0; count; count -= 8) {
223 if (copy_from_user(&data, tmp, 8))
225 err = do_wrmsr(cpu, reg, data[0], data[1]);
231 return ((char __user *)tmp) - buf;
234 static int msr_open(struct inode *inode, struct file *file)
236 unsigned int cpu = iminor(file->f_dentry->d_inode);
237 struct cpuinfo_x86 *c = &(cpu_data)[cpu];
239 if (cpu >= NR_CPUS || !cpu_online(cpu))
240 return -ENXIO; /* No such CPU */
241 if (!cpu_has(c, X86_FEATURE_MSR))
242 return -EIO; /* MSR not supported */
248 * File operations we support
250 static struct file_operations msr_fops = {
251 .owner = THIS_MODULE,
258 static int __init msr_init(void)
260 if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
261 printk(KERN_ERR "msr: unable to get major %d for msr\n",
269 static void __exit msr_exit(void)
271 unregister_chrdev(MSR_MAJOR, "cpu/msr");
274 module_init(msr_init);
275 module_exit(msr_exit)
277 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
278 MODULE_DESCRIPTION("x86 generic MSR driver");
279 MODULE_LICENSE("GPL");