blob: 4bd93c9b2b2716d86367c41631ffa1a0c4625a83 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin2b06ac82008-02-04 16:47:59 +01002 *
3 * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
H. Peter Anvinff55df52009-08-31 14:16:57 -07004 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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 Torvalds1da177e2005-04-16 15:20:36 -070015 * 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 Torvalds1da177e2005-04-16 15:20:36 -070026
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 Rajput448dd2f2009-01-12 14:45:14 +053039#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/processor.h>
42#include <asm/msr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/system.h>
44
gregkh@suse.de8874b412005-03-23 09:56:34 -080045static struct class *msr_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static loff_t msr_seek(struct file *file, loff_t offset, int orig)
48{
H. Peter Anvin2b06ac82008-02-04 16:47:59 +010049 loff_t ret;
50 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
H. Peter Anvin2b06ac82008-02-04 16:47:59 +010052 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 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 Anvin2b06ac82008-02-04 16:47:59 +010061 break;
62 default:
63 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
H. Peter Anvin2b06ac82008-02-04 16:47:59 +010065 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return ret;
67}
68
Paolo Ciarrocchi94a9fa42008-02-22 23:11:52 +010069static ssize_t msr_read(struct file *file, char __user *buf,
70 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 u32 __user *tmp = (u32 __user *) buf;
73 u32 data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 u32 reg = *ppos;
Josef "Jeff" Sipekaab4c5a2006-12-08 02:36:42 -080075 int cpu = iminor(file->f_path.dentry->d_inode);
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070076 int err = 0;
77 ssize_t bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 if (count % 8)
80 return -EINVAL; /* Invalid chunk size */
81
Daniel Marjamaki6926d572006-01-06 00:12:12 -080082 for (; count; count -= 8) {
Nicolas Boichat78a62d22007-05-08 17:22:01 +020083 err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
H. Peter Anvin0cc02132009-08-31 14:23:29 -070084 if (err)
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070085 break;
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070086 if (copy_to_user(tmp, &data, 8)) {
87 err = -EFAULT;
88 break;
89 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 tmp += 2;
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070091 bytes += 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070094 return bytes ? bytes : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97static 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 Torvalds1da177e2005-04-16 15:20:36 -0700102 u32 reg = *ppos;
Josef "Jeff" Sipekaab4c5a2006-12-08 02:36:42 -0800103 int cpu = iminor(file->f_path.dentry->d_inode);
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700104 int err = 0;
105 ssize_t bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 if (count % 8)
108 return -EINVAL; /* Invalid chunk size */
109
David Rientjesf475ff32006-12-07 02:14:13 +0100110 for (; count; count -= 8) {
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700111 if (copy_from_user(&data, tmp, 8)) {
112 err = -EFAULT;
113 break;
114 }
Nicolas Boichat78a62d22007-05-08 17:22:01 +0200115 err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
H. Peter Anvin0cc02132009-08-31 14:23:29 -0700116 if (err)
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700117 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 tmp += 2;
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700119 bytes += 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700122 return bytes ? bytes : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
H. Peter Anvinff55df52009-08-31 14:16:57 -0700125static 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(&regs, 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, &regs, 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(&regs, 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, &regs, sizeof regs))
162 err = -EFAULT;
163 break;
164
165 default:
166 err = -ENOTTY;
167 break;
168 }
169
170 return err;
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static int msr_open(struct inode *inode, struct file *file)
174{
H. Peter Anvin494c2eb2009-12-14 10:02:18 -0800175 unsigned int cpu;
176 struct cpuinfo_x86 *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Jonathan Corbet5119e922008-05-15 09:12:01 -0600178 cpu = iminor(file->f_path.dentry->d_inode);
Frederic Weisbeckerd6c30402009-10-07 21:43:22 +0200179 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
180 return -ENXIO; /* No such CPU */
181
Jonathan Corbet5119e922008-05-15 09:12:01 -0600182 c = &cpu_data(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (!cpu_has(c, X86_FEATURE_MSR))
Frederic Weisbeckerd6c30402009-10-07 21:43:22 +0200184 return -EIO; /* MSR not supported */
185
186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189/*
190 * File operations we support
191 */
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800192static const struct file_operations msr_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .owner = THIS_MODULE,
194 .llseek = msr_seek,
195 .read = msr_read,
196 .write = msr_write,
197 .open = msr_open,
H. Peter Anvinff55df52009-08-31 14:16:57 -0700198 .unlocked_ioctl = msr_ioctl,
199 .compat_ioctl = msr_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
Satyam Sharma38048982007-10-18 03:06:38 -0700202static int __cpuinit msr_device_create(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Greg Kroah-Hartmana271aaf2006-08-07 22:19:37 -0700204 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700206 dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
207 "msr%d", cpu);
Akinobu Mita881a8412007-10-18 03:05:14 -0700208 return IS_ERR(dev) ? PTR_ERR(dev) : 0;
209}
210
211static void msr_device_destroy(int cpu)
212{
213 device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Satyam Sharma761c4bf2007-10-17 18:04:36 +0200216static int __cpuinit msr_class_cpu_callback(struct notifier_block *nfb,
Andrew Mortone09793b2006-06-30 01:55:29 -0700217 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 unsigned int cpu = (unsigned long)hcpu;
Akinobu Mita881a8412007-10-18 03:05:14 -0700220 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 switch (action) {
Akinobu Mita881a8412007-10-18 03:05:14 -0700223 case CPU_UP_PREPARE:
Akinobu Mita881a8412007-10-18 03:05:14 -0700224 err = msr_device_create(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 break;
Akinobu Mita881a8412007-10-18 03:05:14 -0700226 case CPU_UP_CANCELED:
Rafael J. Wysockib844eba2008-03-23 20:28:24 +0100227 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 case CPU_DEAD:
Akinobu Mita881a8412007-10-18 03:05:14 -0700229 msr_device_destroy(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 break;
231 }
Akinobu Mita881a8412007-10-18 03:05:14 -0700232 return err ? NOTIFY_BAD : NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Sam Ravnborgc72258c2008-02-01 17:49:42 +0100235static struct notifier_block __refdata msr_class_cpu_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .notifier_call = msr_class_cpu_callback,
237};
238
Kay Sieverse454cea2009-09-18 23:01:12 +0200239static char *msr_devnode(struct device *dev, mode_t *mode)
Kay Sievers07e9bb82009-04-30 15:23:42 +0200240{
241 return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static int __init msr_init(void)
245{
246 int i, err = 0;
247 i = 0;
248
H. Peter Anvin0b962d42009-12-15 15:13:07 -0800249 if (__register_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr", &msr_fops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 printk(KERN_ERR "msr: unable to get major %d for msr\n",
251 MSR_MAJOR);
252 err = -EBUSY;
253 goto out;
254 }
gregkh@suse.de8874b412005-03-23 09:56:34 -0800255 msr_class = class_create(THIS_MODULE, "msr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (IS_ERR(msr_class)) {
257 err = PTR_ERR(msr_class);
258 goto out_chrdev;
259 }
Kay Sieverse454cea2009-09-18 23:01:12 +0200260 msr_class->devnode = msr_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 for_each_online_cpu(i) {
Greg Kroah-Hartmana271aaf2006-08-07 22:19:37 -0700262 err = msr_device_create(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (err != 0)
264 goto out_class;
265 }
Andrew Mortone09793b2006-06-30 01:55:29 -0700266 register_hotcpu_notifier(&msr_class_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 err = 0;
269 goto out;
270
271out_class:
272 i = 0;
273 for_each_online_cpu(i)
Akinobu Mita881a8412007-10-18 03:05:14 -0700274 msr_device_destroy(i);
gregkh@suse.de8874b412005-03-23 09:56:34 -0800275 class_destroy(msr_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276out_chrdev:
H. Peter Anvin0b962d42009-12-15 15:13:07 -0800277 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278out:
279 return err;
280}
281
282static void __exit msr_exit(void)
283{
284 int cpu = 0;
285 for_each_online_cpu(cpu)
Akinobu Mita881a8412007-10-18 03:05:14 -0700286 msr_device_destroy(cpu);
gregkh@suse.de8874b412005-03-23 09:56:34 -0800287 class_destroy(msr_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 unregister_chrdev(MSR_MAJOR, "cpu/msr");
Andrew Mortone09793b2006-06-30 01:55:29 -0700289 unregister_hotcpu_notifier(&msr_class_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292module_init(msr_init);
293module_exit(msr_exit)
294
295MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
296MODULE_DESCRIPTION("x86 generic MSR driver");
297MODULE_LICENSE("GPL");