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;
176 int cpu = iminor(file->f_dentry->d_inode);
180 return -EINVAL; /* Invalid chunk size */
182 for (; count; count -= 8) {
183 err = do_rdmsr(cpu, reg, &data[0], &data[1]);
186 if (copy_to_user(tmp, &data, 8))
191 return ((char __user *)tmp) - buf;
194 static ssize_t msr_write(struct file *file, const char __user *buf,
195 size_t count, loff_t *ppos)
197 const u32 __user *tmp = (const u32 __user *)buf;
201 int cpu = iminor(file->f_dentry->d_inode);
205 return -EINVAL; /* Invalid chunk size */
207 for (rv = 0; count; count -= 8) {
208 if (copy_from_user(&data, tmp, 8))
210 err = do_wrmsr(cpu, reg, data[0], data[1]);
216 return ((char __user *)tmp) - buf;
219 static int msr_open(struct inode *inode, struct file *file)
221 unsigned int cpu = iminor(file->f_dentry->d_inode);
222 struct cpuinfo_x86 *c = &(cpu_data)[cpu];
224 if (cpu >= NR_CPUS || !cpu_online(cpu))
225 return -ENXIO; /* No such CPU */
226 if (!cpu_has(c, X86_FEATURE_MSR))
227 return -EIO; /* MSR not supported */
233 * File operations we support
235 static struct file_operations msr_fops = {
236 .owner = THIS_MODULE,
243 static int msr_class_device_create(int i)
246 struct class_device *class_err;
248 class_err = class_device_create(msr_class, NULL, MKDEV(MSR_MAJOR, i), NULL, "msr%d",i);
249 if (IS_ERR(class_err))
250 err = PTR_ERR(class_err);
254 static int __devinit msr_class_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
256 unsigned int cpu = (unsigned long)hcpu;
260 msr_class_device_create(cpu);
263 class_device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
269 static struct notifier_block msr_class_cpu_notifier =
271 .notifier_call = msr_class_cpu_callback,
274 static int __init msr_init(void)
279 if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
280 printk(KERN_ERR "msr: unable to get major %d for msr\n",
285 msr_class = class_create(THIS_MODULE, "msr");
286 if (IS_ERR(msr_class)) {
287 err = PTR_ERR(msr_class);
290 for_each_online_cpu(i) {
291 err = msr_class_device_create(i);
295 register_cpu_notifier(&msr_class_cpu_notifier);
302 for_each_online_cpu(i)
303 class_device_destroy(msr_class, MKDEV(MSR_MAJOR, i));
304 class_destroy(msr_class);
306 unregister_chrdev(MSR_MAJOR, "cpu/msr");
311 static void __exit msr_exit(void)
314 for_each_online_cpu(cpu)
315 class_device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
316 class_destroy(msr_class);
317 unregister_chrdev(MSR_MAJOR, "cpu/msr");
318 unregister_cpu_notifier(&msr_class_cpu_notifier);
321 module_init(msr_init);
322 module_exit(msr_exit)
324 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
325 MODULE_DESCRIPTION("x86 generic MSR driver");
326 MODULE_LICENSE("GPL");