blob: 7bf2dc4c8f701de8965fc1e124126863c7384552 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/processor.h>
43#include <asm/msr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/system.h>
45
gregkh@suse.de8874b412005-03-23 09:56:34 -080046static struct class *msr_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static loff_t msr_seek(struct file *file, loff_t offset, int orig)
49{
H. Peter Anvin2b06ac82008-02-04 16:47:59 +010050 loff_t ret;
51 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
H. Peter Anvin2b06ac82008-02-04 16:47:59 +010053 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 switch (orig) {
55 case 0:
56 file->f_pos = offset;
57 ret = file->f_pos;
58 break;
59 case 1:
60 file->f_pos += offset;
61 ret = file->f_pos;
H. Peter Anvin2b06ac82008-02-04 16:47:59 +010062 break;
63 default:
64 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 }
H. Peter Anvin2b06ac82008-02-04 16:47:59 +010066 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return ret;
68}
69
Paolo Ciarrocchi94a9fa42008-02-22 23:11:52 +010070static ssize_t msr_read(struct file *file, char __user *buf,
71 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 u32 __user *tmp = (u32 __user *) buf;
74 u32 data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 u32 reg = *ppos;
Josef "Jeff" Sipekaab4c5a2006-12-08 02:36:42 -080076 int cpu = iminor(file->f_path.dentry->d_inode);
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070077 int err = 0;
78 ssize_t bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 if (count % 8)
81 return -EINVAL; /* Invalid chunk size */
82
Daniel Marjamaki6926d572006-01-06 00:12:12 -080083 for (; count; count -= 8) {
Nicolas Boichat78a62d22007-05-08 17:22:01 +020084 err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
H. Peter Anvin0cc02132009-08-31 14:23:29 -070085 if (err)
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070086 break;
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070087 if (copy_to_user(tmp, &data, 8)) {
88 err = -EFAULT;
89 break;
90 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 tmp += 2;
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070092 bytes += 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070095 return bytes ? bytes : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98static ssize_t msr_write(struct file *file, const char __user *buf,
99 size_t count, loff_t *ppos)
100{
101 const u32 __user *tmp = (const u32 __user *)buf;
102 u32 data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 u32 reg = *ppos;
Josef "Jeff" Sipekaab4c5a2006-12-08 02:36:42 -0800104 int cpu = iminor(file->f_path.dentry->d_inode);
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700105 int err = 0;
106 ssize_t bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if (count % 8)
109 return -EINVAL; /* Invalid chunk size */
110
David Rientjesf475ff32006-12-07 02:14:13 +0100111 for (; count; count -= 8) {
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700112 if (copy_from_user(&data, tmp, 8)) {
113 err = -EFAULT;
114 break;
115 }
Nicolas Boichat78a62d22007-05-08 17:22:01 +0200116 err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
H. Peter Anvin0cc02132009-08-31 14:23:29 -0700117 if (err)
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700118 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 tmp += 2;
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700120 bytes += 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700123 return bytes ? bytes : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
H. Peter Anvinff55df52009-08-31 14:16:57 -0700126static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
127{
128 u32 __user *uregs = (u32 __user *)arg;
129 u32 regs[8];
130 int cpu = iminor(file->f_path.dentry->d_inode);
131 int err;
132
133 switch (ioc) {
134 case X86_IOC_RDMSR_REGS:
135 if (!(file->f_mode & FMODE_READ)) {
136 err = -EBADF;
137 break;
138 }
139 if (copy_from_user(&regs, uregs, sizeof regs)) {
140 err = -EFAULT;
141 break;
142 }
143 err = rdmsr_safe_regs_on_cpu(cpu, regs);
144 if (err)
145 break;
146 if (copy_to_user(uregs, &regs, sizeof regs))
147 err = -EFAULT;
148 break;
149
150 case X86_IOC_WRMSR_REGS:
151 if (!(file->f_mode & FMODE_WRITE)) {
152 err = -EBADF;
153 break;
154 }
155 if (copy_from_user(&regs, uregs, sizeof regs)) {
156 err = -EFAULT;
157 break;
158 }
159 err = wrmsr_safe_regs_on_cpu(cpu, regs);
160 if (err)
161 break;
162 if (copy_to_user(uregs, &regs, sizeof regs))
163 err = -EFAULT;
164 break;
165
166 default:
167 err = -ENOTTY;
168 break;
169 }
170
171 return err;
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static int msr_open(struct inode *inode, struct file *file)
175{
H. Peter Anvin494c2eb2009-12-14 10:02:18 -0800176 unsigned int cpu;
177 struct cpuinfo_x86 *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Jonathan Corbet5119e922008-05-15 09:12:01 -0600179 cpu = iminor(file->f_path.dentry->d_inode);
Frederic Weisbeckerd6c30402009-10-07 21:43:22 +0200180 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
181 return -ENXIO; /* No such CPU */
182
Jonathan Corbet5119e922008-05-15 09:12:01 -0600183 c = &cpu_data(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (!cpu_has(c, X86_FEATURE_MSR))
Frederic Weisbeckerd6c30402009-10-07 21:43:22 +0200185 return -EIO; /* MSR not supported */
186
187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190/*
191 * File operations we support
192 */
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800193static const struct file_operations msr_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .owner = THIS_MODULE,
195 .llseek = msr_seek,
196 .read = msr_read,
197 .write = msr_write,
198 .open = msr_open,
H. Peter Anvinff55df52009-08-31 14:16:57 -0700199 .unlocked_ioctl = msr_ioctl,
200 .compat_ioctl = msr_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
Satyam Sharma38048982007-10-18 03:06:38 -0700203static int __cpuinit msr_device_create(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Greg Kroah-Hartmana271aaf2006-08-07 22:19:37 -0700205 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700207 dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
208 "msr%d", cpu);
Akinobu Mita881a8412007-10-18 03:05:14 -0700209 return IS_ERR(dev) ? PTR_ERR(dev) : 0;
210}
211
212static void msr_device_destroy(int cpu)
213{
214 device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Satyam Sharma761c4bf2007-10-17 18:04:36 +0200217static int __cpuinit msr_class_cpu_callback(struct notifier_block *nfb,
Andrew Mortone09793b2006-06-30 01:55:29 -0700218 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 unsigned int cpu = (unsigned long)hcpu;
Akinobu Mita881a8412007-10-18 03:05:14 -0700221 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 switch (action) {
Akinobu Mita881a8412007-10-18 03:05:14 -0700224 case CPU_UP_PREPARE:
Akinobu Mita881a8412007-10-18 03:05:14 -0700225 err = msr_device_create(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 break;
Akinobu Mita881a8412007-10-18 03:05:14 -0700227 case CPU_UP_CANCELED:
Rafael J. Wysockib844eba2008-03-23 20:28:24 +0100228 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 case CPU_DEAD:
Akinobu Mita881a8412007-10-18 03:05:14 -0700230 msr_device_destroy(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 break;
232 }
Akinobu Mitaa94247e2010-05-26 14:43:30 -0700233 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Sam Ravnborgc72258c2008-02-01 17:49:42 +0100236static struct notifier_block __refdata msr_class_cpu_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .notifier_call = msr_class_cpu_callback,
238};
239
Kay Sieverse454cea2009-09-18 23:01:12 +0200240static char *msr_devnode(struct device *dev, mode_t *mode)
Kay Sievers07e9bb82009-04-30 15:23:42 +0200241{
242 return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static int __init msr_init(void)
246{
247 int i, err = 0;
248 i = 0;
249
H. Peter Anvin0b962d42009-12-15 15:13:07 -0800250 if (__register_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr", &msr_fops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 printk(KERN_ERR "msr: unable to get major %d for msr\n",
252 MSR_MAJOR);
253 err = -EBUSY;
254 goto out;
255 }
gregkh@suse.de8874b412005-03-23 09:56:34 -0800256 msr_class = class_create(THIS_MODULE, "msr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (IS_ERR(msr_class)) {
258 err = PTR_ERR(msr_class);
259 goto out_chrdev;
260 }
Kay Sieverse454cea2009-09-18 23:01:12 +0200261 msr_class->devnode = msr_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 for_each_online_cpu(i) {
Greg Kroah-Hartmana271aaf2006-08-07 22:19:37 -0700263 err = msr_device_create(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (err != 0)
265 goto out_class;
266 }
Andrew Mortone09793b2006-06-30 01:55:29 -0700267 register_hotcpu_notifier(&msr_class_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 err = 0;
270 goto out;
271
272out_class:
273 i = 0;
274 for_each_online_cpu(i)
Akinobu Mita881a8412007-10-18 03:05:14 -0700275 msr_device_destroy(i);
gregkh@suse.de8874b412005-03-23 09:56:34 -0800276 class_destroy(msr_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277out_chrdev:
H. Peter Anvin0b962d42009-12-15 15:13:07 -0800278 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279out:
280 return err;
281}
282
283static void __exit msr_exit(void)
284{
285 int cpu = 0;
286 for_each_online_cpu(cpu)
Akinobu Mita881a8412007-10-18 03:05:14 -0700287 msr_device_destroy(cpu);
gregkh@suse.de8874b412005-03-23 09:56:34 -0800288 class_destroy(msr_class);
Russ Andersonda482472010-01-26 20:37:22 -0600289 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
Andrew Mortone09793b2006-06-30 01:55:29 -0700290 unregister_hotcpu_notifier(&msr_class_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293module_init(msr_init);
294module_exit(msr_exit)
295
296MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
297MODULE_DESCRIPTION("x86 generic MSR driver");
298MODULE_LICENSE("GPL");