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>
38 #include <linux/device.h>
39 #include <linux/cpu.h>
40 #include <linux/notifier.h>
42 #include <asm/processor.h>
44 #include <asm/uaccess.h>
45 #include <asm/system.h>
47 static struct class *msr_class;
49 static inline int wrmsr_eio(u32 reg, u32 eax, u32 edx)
53 err = wrmsr_safe(reg, eax, edx);
59 static inline int rdmsr_eio(u32 reg, u32 *eax, u32 *edx)
63 err = rdmsr_safe(reg, eax, edx);
78 static void msr_smp_wrmsr(void *cmd_block)
80 struct msr_command *cmd = (struct msr_command *)cmd_block;
82 if (cmd->cpu == smp_processor_id())
83 cmd->err = wrmsr_eio(cmd->reg, cmd->data[0], cmd->data[1]);
86 static void msr_smp_rdmsr(void *cmd_block)
88 struct msr_command *cmd = (struct msr_command *)cmd_block;
90 if (cmd->cpu == smp_processor_id())
91 cmd->err = rdmsr_eio(cmd->reg, &cmd->data[0], &cmd->data[1]);
94 static inline int do_wrmsr(int cpu, u32 reg, u32 eax, u32 edx)
96 struct msr_command cmd;
100 if (cpu == smp_processor_id()) {
101 ret = wrmsr_eio(reg, eax, edx);
108 smp_call_function(msr_smp_wrmsr, &cmd, 1, 1);
115 static inline int do_rdmsr(int cpu, u32 reg, u32 * eax, u32 * edx)
117 struct msr_command cmd;
121 if (cpu == smp_processor_id()) {
122 ret = rdmsr_eio(reg, eax, edx);
127 smp_call_function(msr_smp_rdmsr, &cmd, 1, 1);
138 #else /* ! CONFIG_SMP */
140 static inline int do_wrmsr(int cpu, u32 reg, u32 eax, u32 edx)
142 return wrmsr_eio(reg, eax, edx);
145 static inline int do_rdmsr(int cpu, u32 reg, u32 *eax, u32 *edx)
147 return rdmsr_eio(reg, eax, edx);
150 #endif /* ! CONFIG_SMP */
152 static loff_t msr_seek(struct file *file, loff_t offset, int orig)
154 loff_t ret = -EINVAL;
159 file->f_pos = offset;
163 file->f_pos += offset;
170 static ssize_t msr_read(struct file *file, char __user * buf,
171 size_t count, loff_t * ppos)
173 u32 __user *tmp = (u32 __user *) buf;
177 int cpu = iminor(file->f_dentry->d_inode);
181 return -EINVAL; /* Invalid chunk size */
183 for (rv = 0; count; count -= 8) {
184 err = do_rdmsr(cpu, reg, &data[0], &data[1]);
187 if (copy_to_user(tmp, &data, 8))
192 return ((char __user *)tmp) - buf;
195 static ssize_t msr_write(struct file *file, const char __user *buf,
196 size_t count, loff_t *ppos)
198 const u32 __user *tmp = (const u32 __user *)buf;
202 int cpu = iminor(file->f_dentry->d_inode);
206 return -EINVAL; /* Invalid chunk size */
208 for (rv = 0; count; count -= 8) {
209 if (copy_from_user(&data, tmp, 8))
211 err = do_wrmsr(cpu, reg, data[0], data[1]);
217 return ((char __user *)tmp) - buf;
220 static int msr_open(struct inode *inode, struct file *file)
222 unsigned int cpu = iminor(file->f_dentry->d_inode);
223 struct cpuinfo_x86 *c = &(cpu_data)[cpu];
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 */
234 * File operations we support
236 static struct file_operations msr_fops = {
237 .owner = THIS_MODULE,
244 static int msr_class_device_create(int i)
247 struct class_device *class_err;
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);
255 static int __devinit msr_class_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
257 unsigned int cpu = (unsigned long)hcpu;
261 msr_class_device_create(cpu);
264 class_device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
270 static struct notifier_block msr_class_cpu_notifier =
272 .notifier_call = msr_class_cpu_callback,
275 static int __init msr_init(void)
280 if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
281 printk(KERN_ERR "msr: unable to get major %d for msr\n",
286 msr_class = class_create(THIS_MODULE, "msr");
287 if (IS_ERR(msr_class)) {
288 err = PTR_ERR(msr_class);
291 for_each_online_cpu(i) {
292 err = msr_class_device_create(i);
296 register_cpu_notifier(&msr_class_cpu_notifier);
303 for_each_online_cpu(i)
304 class_device_destroy(msr_class, MKDEV(MSR_MAJOR, i));
305 class_destroy(msr_class);
307 unregister_chrdev(MSR_MAJOR, "cpu/msr");
312 static void __exit msr_exit(void)
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);
322 module_init(msr_init);
323 module_exit(msr_exit)
325 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
326 MODULE_DESCRIPTION("x86 generic MSR driver");
327 MODULE_LICENSE("GPL");