2 /* ----------------------------------------------------------------------- *
4 * Copyright 2000 H. Peter Anvin - All Rights Reserved
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
9 * USA; either version 2 of the License, or (at your option) any later
10 * version; incorporated herein by reference.
12 * ----------------------------------------------------------------------- */
17 * x86 MSR access device
19 * This device is accessed by lseek() to the appropriate register number
20 * and then read/write in chunks of 8 bytes. A larger size means multiple
21 * reads or writes of the same register.
23 * This driver uses /dev/cpu/%d/msr where %d is the minor number, and on
24 * an SMP box will direct the access to CPU %d.
27 #include <linux/module.h>
28 #include <linux/config.h>
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/smp_lock.h>
37 #include <linux/major.h>
40 #include <asm/processor.h>
42 #include <asm/uaccess.h>
43 #include <asm/system.h>
45 /* Note: "err" is handled in a funny way below. Otherwise one version
46 of gcc or another breaks. */
48 static inline int wrmsr_eio(u32 reg, u32 eax, u32 edx)
55 ".section .fixup,\"ax\"\n"
59 ".section __ex_table,\"a\"\n"
64 : "a" (eax), "d" (edx), "c" (reg), "i" (-EIO), "0" (0));
69 static inline int rdmsr_eio(u32 reg, u32 *eax, u32 *edx)
76 ".section .fixup,\"ax\"\n"
80 ".section __ex_table,\"a\"\n"
84 : "=&bDS" (err), "=a" (*eax), "=d" (*edx)
85 : "c" (reg), "i" (-EIO), "0" (0));
99 static void msr_smp_wrmsr(void *cmd_block)
101 struct msr_command *cmd = (struct msr_command *) cmd_block;
103 if ( cmd->cpu == smp_processor_id() )
104 cmd->err = wrmsr_eio(cmd->reg, cmd->data[0], cmd->data[1]);
107 static void msr_smp_rdmsr(void *cmd_block)
109 struct msr_command *cmd = (struct msr_command *) cmd_block;
111 if ( cmd->cpu == smp_processor_id() )
112 cmd->err = rdmsr_eio(cmd->reg, &cmd->data[0], &cmd->data[1]);
115 static inline int do_wrmsr(int cpu, u32 reg, u32 eax, u32 edx)
117 struct msr_command cmd;
121 if ( cpu == smp_processor_id() ) {
122 ret = wrmsr_eio(reg, eax, edx);
129 smp_call_function(msr_smp_wrmsr, &cmd, 1, 1);
136 static inline int do_rdmsr(int cpu, u32 reg, u32 *eax, u32 *edx)
138 struct msr_command cmd;
142 if ( cpu == smp_processor_id() ) {
143 ret = rdmsr_eio(reg, eax, edx);
148 smp_call_function(msr_smp_rdmsr, &cmd, 1, 1);
159 #else /* ! CONFIG_SMP */
161 static inline int do_wrmsr(int cpu, u32 reg, u32 eax, u32 edx)
163 return wrmsr_eio(reg, eax, edx);
166 static inline int do_rdmsr(int cpu, u32 reg, u32 *eax, u32 *edx)
168 return rdmsr_eio(reg, eax, edx);
171 #endif /* ! CONFIG_SMP */
173 static loff_t msr_seek(struct file *file, loff_t offset, int orig)
175 loff_t ret = -EINVAL;
179 file->f_pos = offset;
183 file->f_pos += offset;
190 static ssize_t msr_read(struct file * file, char * buf,
191 size_t count, loff_t *ppos)
193 u32 *tmp = (u32 *)buf;
197 int cpu = iminor(file->f_dentry->d_inode);
201 return -EINVAL; /* Invalid chunk size */
203 for ( rv = 0 ; count ; count -= 8 ) {
204 err = do_rdmsr(cpu, reg, &data[0], &data[1]);
207 if ( copy_to_user(tmp,&data,8) )
212 return ((char *)tmp) - buf;
215 static ssize_t msr_write(struct file * file, const char * buf,
216 size_t count, loff_t *ppos)
218 const u32 *tmp = (const u32 *)buf;
222 int cpu = iminor(file->f_dentry->d_inode);
226 return -EINVAL; /* Invalid chunk size */
228 for ( rv = 0 ; count ; count -= 8 ) {
229 if ( copy_from_user(&data,tmp,8) )
231 err = do_wrmsr(cpu, reg, data[0], data[1]);
237 return ((char *)tmp) - buf;
240 static int msr_open(struct inode *inode, struct file *file)
242 int cpu = iminor(file->f_dentry->d_inode);
243 struct cpuinfo_x86 *c = &(cpu_data)[cpu];
245 if (!cpu_online(cpu))
246 return -ENXIO; /* No such CPU */
247 if ( !cpu_has(c, X86_FEATURE_MSR) )
248 return -EIO; /* MSR not supported */
254 * File operations we support
256 static struct file_operations msr_fops = {
257 .owner = THIS_MODULE,
264 int __init msr_init(void)
266 if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
267 printk(KERN_ERR "msr: unable to get major %d for msr\n",
275 void __exit msr_exit(void)
277 unregister_chrdev(MSR_MAJOR, "cpu/msr");
280 module_init(msr_init);
281 module_exit(msr_exit)
283 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
284 MODULE_DESCRIPTION("x86 generic MSR driver");
285 MODULE_LICENSE("GPL");