Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
H. Peter Anvin | 2b06ac8 | 2008-02-04 16:47:59 +0100 | [diff] [blame] | 2 | * |
| 3 | * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved |
H. Peter Anvin | ff55df5 | 2009-08-31 14:16:57 -0700 | [diff] [blame] | 4 | * Copyright 2009 Intel Corporation; author: H. Peter Anvin |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 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. |
| 11 | * |
| 12 | * ----------------------------------------------------------------------- */ |
| 13 | |
| 14 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * x86 MSR access device |
| 16 | * |
| 17 | * This device is accessed by lseek() to the appropriate register number |
| 18 | * and then read/write in chunks of 8 bytes. A larger size means multiple |
| 19 | * reads or writes of the same register. |
| 20 | * |
| 21 | * This driver uses /dev/cpu/%d/msr where %d is the minor number, and on |
| 22 | * an SMP box will direct the access to CPU %d. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/errno.h> |
| 29 | #include <linux/fcntl.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/poll.h> |
| 32 | #include <linux/smp.h> |
| 33 | #include <linux/smp_lock.h> |
| 34 | #include <linux/major.h> |
| 35 | #include <linux/fs.h> |
| 36 | #include <linux/device.h> |
| 37 | #include <linux/cpu.h> |
| 38 | #include <linux/notifier.h> |
Jaswinder Singh Rajput | 448dd2f | 2009-01-12 14:45:14 +0530 | [diff] [blame] | 39 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | #include <asm/processor.h> |
| 42 | #include <asm/msr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/system.h> |
| 44 | |
gregkh@suse.de | 8874b41 | 2005-03-23 09:56:34 -0800 | [diff] [blame] | 45 | static struct class *msr_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static loff_t msr_seek(struct file *file, loff_t offset, int orig) |
| 48 | { |
H. Peter Anvin | 2b06ac8 | 2008-02-04 16:47:59 +0100 | [diff] [blame] | 49 | loff_t ret; |
| 50 | struct inode *inode = file->f_mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
H. Peter Anvin | 2b06ac8 | 2008-02-04 16:47:59 +0100 | [diff] [blame] | 52 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | switch (orig) { |
| 54 | case 0: |
| 55 | file->f_pos = offset; |
| 56 | ret = file->f_pos; |
| 57 | break; |
| 58 | case 1: |
| 59 | file->f_pos += offset; |
| 60 | ret = file->f_pos; |
H. Peter Anvin | 2b06ac8 | 2008-02-04 16:47:59 +0100 | [diff] [blame] | 61 | break; |
| 62 | default: |
| 63 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
H. Peter Anvin | 2b06ac8 | 2008-02-04 16:47:59 +0100 | [diff] [blame] | 65 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | return ret; |
| 67 | } |
| 68 | |
Paolo Ciarrocchi | 94a9fa4 | 2008-02-22 23:11:52 +0100 | [diff] [blame] | 69 | static ssize_t msr_read(struct file *file, char __user *buf, |
| 70 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
| 72 | u32 __user *tmp = (u32 __user *) buf; |
| 73 | u32 data[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | u32 reg = *ppos; |
Josef "Jeff" Sipek | aab4c5a | 2006-12-08 02:36:42 -0800 | [diff] [blame] | 75 | int cpu = iminor(file->f_path.dentry->d_inode); |
H. Peter Anvin | 85f1cb6 | 2008-08-25 17:34:27 -0700 | [diff] [blame] | 76 | int err = 0; |
| 77 | ssize_t bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | if (count % 8) |
| 80 | return -EINVAL; /* Invalid chunk size */ |
| 81 | |
Daniel Marjamaki | 6926d57 | 2006-01-06 00:12:12 -0800 | [diff] [blame] | 82 | for (; count; count -= 8) { |
Nicolas Boichat | 78a62d2 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 83 | err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]); |
H. Peter Anvin | 0cc0213 | 2009-08-31 14:23:29 -0700 | [diff] [blame] | 84 | if (err) |
H. Peter Anvin | 85f1cb6 | 2008-08-25 17:34:27 -0700 | [diff] [blame] | 85 | break; |
H. Peter Anvin | 85f1cb6 | 2008-08-25 17:34:27 -0700 | [diff] [blame] | 86 | if (copy_to_user(tmp, &data, 8)) { |
| 87 | err = -EFAULT; |
| 88 | break; |
| 89 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | tmp += 2; |
H. Peter Anvin | 85f1cb6 | 2008-08-25 17:34:27 -0700 | [diff] [blame] | 91 | bytes += 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
H. Peter Anvin | 85f1cb6 | 2008-08-25 17:34:27 -0700 | [diff] [blame] | 94 | return bytes ? bytes : err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static ssize_t msr_write(struct file *file, const char __user *buf, |
| 98 | size_t count, loff_t *ppos) |
| 99 | { |
| 100 | const u32 __user *tmp = (const u32 __user *)buf; |
| 101 | u32 data[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | u32 reg = *ppos; |
Josef "Jeff" Sipek | aab4c5a | 2006-12-08 02:36:42 -0800 | [diff] [blame] | 103 | int cpu = iminor(file->f_path.dentry->d_inode); |
H. Peter Anvin | 85f1cb6 | 2008-08-25 17:34:27 -0700 | [diff] [blame] | 104 | int err = 0; |
| 105 | ssize_t bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | if (count % 8) |
| 108 | return -EINVAL; /* Invalid chunk size */ |
| 109 | |
David Rientjes | f475ff3 | 2006-12-07 02:14:13 +0100 | [diff] [blame] | 110 | for (; count; count -= 8) { |
H. Peter Anvin | 85f1cb6 | 2008-08-25 17:34:27 -0700 | [diff] [blame] | 111 | if (copy_from_user(&data, tmp, 8)) { |
| 112 | err = -EFAULT; |
| 113 | break; |
| 114 | } |
Nicolas Boichat | 78a62d2 | 2007-05-08 17:22:01 +0200 | [diff] [blame] | 115 | err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]); |
H. Peter Anvin | 0cc0213 | 2009-08-31 14:23:29 -0700 | [diff] [blame] | 116 | if (err) |
H. Peter Anvin | 85f1cb6 | 2008-08-25 17:34:27 -0700 | [diff] [blame] | 117 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | tmp += 2; |
H. Peter Anvin | 85f1cb6 | 2008-08-25 17:34:27 -0700 | [diff] [blame] | 119 | bytes += 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
H. Peter Anvin | 85f1cb6 | 2008-08-25 17:34:27 -0700 | [diff] [blame] | 122 | return bytes ? bytes : err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
H. Peter Anvin | ff55df5 | 2009-08-31 14:16:57 -0700 | [diff] [blame] | 125 | static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg) |
| 126 | { |
| 127 | u32 __user *uregs = (u32 __user *)arg; |
| 128 | u32 regs[8]; |
| 129 | int cpu = iminor(file->f_path.dentry->d_inode); |
| 130 | int err; |
| 131 | |
| 132 | switch (ioc) { |
| 133 | case X86_IOC_RDMSR_REGS: |
| 134 | if (!(file->f_mode & FMODE_READ)) { |
| 135 | err = -EBADF; |
| 136 | break; |
| 137 | } |
| 138 | if (copy_from_user(®s, uregs, sizeof regs)) { |
| 139 | err = -EFAULT; |
| 140 | break; |
| 141 | } |
| 142 | err = rdmsr_safe_regs_on_cpu(cpu, regs); |
| 143 | if (err) |
| 144 | break; |
| 145 | if (copy_to_user(uregs, ®s, sizeof regs)) |
| 146 | err = -EFAULT; |
| 147 | break; |
| 148 | |
| 149 | case X86_IOC_WRMSR_REGS: |
| 150 | if (!(file->f_mode & FMODE_WRITE)) { |
| 151 | err = -EBADF; |
| 152 | break; |
| 153 | } |
| 154 | if (copy_from_user(®s, uregs, sizeof regs)) { |
| 155 | err = -EFAULT; |
| 156 | break; |
| 157 | } |
| 158 | err = wrmsr_safe_regs_on_cpu(cpu, regs); |
| 159 | if (err) |
| 160 | break; |
| 161 | if (copy_to_user(uregs, ®s, sizeof regs)) |
| 162 | err = -EFAULT; |
| 163 | break; |
| 164 | |
| 165 | default: |
| 166 | err = -ENOTTY; |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | return err; |
| 171 | } |
| 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | static int msr_open(struct inode *inode, struct file *file) |
| 174 | { |
H. Peter Anvin | 494c2eb | 2009-12-14 10:02:18 -0800 | [diff] [blame] | 175 | unsigned int cpu; |
| 176 | struct cpuinfo_x86 *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Jonathan Corbet | 5119e92 | 2008-05-15 09:12:01 -0600 | [diff] [blame] | 178 | cpu = iminor(file->f_path.dentry->d_inode); |
Frederic Weisbecker | d6c3040 | 2009-10-07 21:43:22 +0200 | [diff] [blame] | 179 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
| 180 | return -ENXIO; /* No such CPU */ |
| 181 | |
Jonathan Corbet | 5119e92 | 2008-05-15 09:12:01 -0600 | [diff] [blame] | 182 | c = &cpu_data(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | if (!cpu_has(c, X86_FEATURE_MSR)) |
Frederic Weisbecker | d6c3040 | 2009-10-07 21:43:22 +0200 | [diff] [blame] | 184 | return -EIO; /* MSR not supported */ |
| 185 | |
| 186 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* |
| 190 | * File operations we support |
| 191 | */ |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 192 | static const struct file_operations msr_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | .owner = THIS_MODULE, |
| 194 | .llseek = msr_seek, |
| 195 | .read = msr_read, |
| 196 | .write = msr_write, |
| 197 | .open = msr_open, |
H. Peter Anvin | ff55df5 | 2009-08-31 14:16:57 -0700 | [diff] [blame] | 198 | .unlocked_ioctl = msr_ioctl, |
| 199 | .compat_ioctl = msr_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
Satyam Sharma | 3804898 | 2007-10-18 03:06:38 -0700 | [diff] [blame] | 202 | static int __cpuinit msr_device_create(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Greg Kroah-Hartman | a271aaf | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 204 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Greg Kroah-Hartman | a9b1261 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 206 | dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL, |
| 207 | "msr%d", cpu); |
Akinobu Mita | 881a841 | 2007-10-18 03:05:14 -0700 | [diff] [blame] | 208 | return IS_ERR(dev) ? PTR_ERR(dev) : 0; |
| 209 | } |
| 210 | |
| 211 | static void msr_device_destroy(int cpu) |
| 212 | { |
| 213 | device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Satyam Sharma | 761c4bf | 2007-10-17 18:04:36 +0200 | [diff] [blame] | 216 | static int __cpuinit msr_class_cpu_callback(struct notifier_block *nfb, |
Andrew Morton | e09793b | 2006-06-30 01:55:29 -0700 | [diff] [blame] | 217 | unsigned long action, void *hcpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
| 219 | unsigned int cpu = (unsigned long)hcpu; |
Akinobu Mita | 881a841 | 2007-10-18 03:05:14 -0700 | [diff] [blame] | 220 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | switch (action) { |
Akinobu Mita | 881a841 | 2007-10-18 03:05:14 -0700 | [diff] [blame] | 223 | case CPU_UP_PREPARE: |
Akinobu Mita | 881a841 | 2007-10-18 03:05:14 -0700 | [diff] [blame] | 224 | err = msr_device_create(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | break; |
Akinobu Mita | 881a841 | 2007-10-18 03:05:14 -0700 | [diff] [blame] | 226 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | b844eba | 2008-03-23 20:28:24 +0100 | [diff] [blame] | 227 | case CPU_UP_CANCELED_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | case CPU_DEAD: |
Akinobu Mita | 881a841 | 2007-10-18 03:05:14 -0700 | [diff] [blame] | 229 | msr_device_destroy(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | break; |
| 231 | } |
Akinobu Mita | 881a841 | 2007-10-18 03:05:14 -0700 | [diff] [blame] | 232 | return err ? NOTIFY_BAD : NOTIFY_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Sam Ravnborg | c72258c | 2008-02-01 17:49:42 +0100 | [diff] [blame] | 235 | static struct notifier_block __refdata msr_class_cpu_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | .notifier_call = msr_class_cpu_callback, |
| 237 | }; |
| 238 | |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 239 | static char *msr_devnode(struct device *dev, mode_t *mode) |
Kay Sievers | 07e9bb8 | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 240 | { |
| 241 | return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt)); |
| 242 | } |
| 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | static int __init msr_init(void) |
| 245 | { |
| 246 | int i, err = 0; |
| 247 | i = 0; |
| 248 | |
H. Peter Anvin | 0b962d4 | 2009-12-15 15:13:07 -0800 | [diff] [blame] | 249 | if (__register_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr", &msr_fops)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | printk(KERN_ERR "msr: unable to get major %d for msr\n", |
| 251 | MSR_MAJOR); |
| 252 | err = -EBUSY; |
| 253 | goto out; |
| 254 | } |
gregkh@suse.de | 8874b41 | 2005-03-23 09:56:34 -0800 | [diff] [blame] | 255 | msr_class = class_create(THIS_MODULE, "msr"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | if (IS_ERR(msr_class)) { |
| 257 | err = PTR_ERR(msr_class); |
| 258 | goto out_chrdev; |
| 259 | } |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 260 | msr_class->devnode = msr_devnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | for_each_online_cpu(i) { |
Greg Kroah-Hartman | a271aaf | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 262 | err = msr_device_create(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (err != 0) |
| 264 | goto out_class; |
| 265 | } |
Andrew Morton | e09793b | 2006-06-30 01:55:29 -0700 | [diff] [blame] | 266 | register_hotcpu_notifier(&msr_class_cpu_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | err = 0; |
| 269 | goto out; |
| 270 | |
| 271 | out_class: |
| 272 | i = 0; |
| 273 | for_each_online_cpu(i) |
Akinobu Mita | 881a841 | 2007-10-18 03:05:14 -0700 | [diff] [blame] | 274 | msr_device_destroy(i); |
gregkh@suse.de | 8874b41 | 2005-03-23 09:56:34 -0800 | [diff] [blame] | 275 | class_destroy(msr_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | out_chrdev: |
H. Peter Anvin | 0b962d4 | 2009-12-15 15:13:07 -0800 | [diff] [blame] | 277 | __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | out: |
| 279 | return err; |
| 280 | } |
| 281 | |
| 282 | static void __exit msr_exit(void) |
| 283 | { |
| 284 | int cpu = 0; |
| 285 | for_each_online_cpu(cpu) |
Akinobu Mita | 881a841 | 2007-10-18 03:05:14 -0700 | [diff] [blame] | 286 | msr_device_destroy(cpu); |
gregkh@suse.de | 8874b41 | 2005-03-23 09:56:34 -0800 | [diff] [blame] | 287 | class_destroy(msr_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | unregister_chrdev(MSR_MAJOR, "cpu/msr"); |
Andrew Morton | e09793b | 2006-06-30 01:55:29 -0700 | [diff] [blame] | 289 | unregister_hotcpu_notifier(&msr_class_cpu_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | module_init(msr_init); |
| 293 | module_exit(msr_exit) |
| 294 | |
| 295 | MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>"); |
| 296 | MODULE_DESCRIPTION("x86 generic MSR driver"); |
| 297 | MODULE_LICENSE("GPL"); |