blob: ce130493b8020aa05edf555234ebfd8cac9e9f9a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/major.h>
34#include <linux/fs.h>
35#include <linux/device.h>
36#include <linux/cpu.h>
37#include <linux/notifier.h>
Jaswinder Singh Rajput448dd2f2009-01-12 14:45:14 +053038#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/gfp.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
gregkh@suse.de8874b412005-03-23 09:56:34 -080044static struct class *msr_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static loff_t msr_seek(struct file *file, loff_t offset, int orig)
47{
H. Peter Anvin2b06ac82008-02-04 16:47:59 +010048 loff_t ret;
49 struct inode *inode = file->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
H. Peter Anvin2b06ac82008-02-04 16:47:59 +010051 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 switch (orig) {
53 case 0:
54 file->f_pos = offset;
55 ret = file->f_pos;
56 break;
57 case 1:
58 file->f_pos += offset;
59 ret = file->f_pos;
H. Peter Anvin2b06ac82008-02-04 16:47:59 +010060 break;
61 default:
62 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
H. Peter Anvin2b06ac82008-02-04 16:47:59 +010064 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return ret;
66}
67
Paolo Ciarrocchi94a9fa42008-02-22 23:11:52 +010068static ssize_t msr_read(struct file *file, char __user *buf,
69 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 u32 __user *tmp = (u32 __user *) buf;
72 u32 data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 u32 reg = *ppos;
Al Viro6131ffa2013-02-27 16:59:05 -050074 int cpu = iminor(file_inode(file));
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070075 int err = 0;
76 ssize_t bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 if (count % 8)
79 return -EINVAL; /* Invalid chunk size */
80
Daniel Marjamaki6926d572006-01-06 00:12:12 -080081 for (; count; count -= 8) {
Nicolas Boichat78a62d22007-05-08 17:22:01 +020082 err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
H. Peter Anvin0cc02132009-08-31 14:23:29 -070083 if (err)
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070084 break;
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070085 if (copy_to_user(tmp, &data, 8)) {
86 err = -EFAULT;
87 break;
88 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 tmp += 2;
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070090 bytes += 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
H. Peter Anvin85f1cb62008-08-25 17:34:27 -070093 return bytes ? bytes : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96static ssize_t msr_write(struct file *file, const char __user *buf,
97 size_t count, loff_t *ppos)
98{
99 const u32 __user *tmp = (const u32 __user *)buf;
100 u32 data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 u32 reg = *ppos;
Al Viro6131ffa2013-02-27 16:59:05 -0500102 int cpu = iminor(file_inode(file));
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700103 int err = 0;
104 ssize_t bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 if (count % 8)
107 return -EINVAL; /* Invalid chunk size */
108
David Rientjesf475ff32006-12-07 02:14:13 +0100109 for (; count; count -= 8) {
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700110 if (copy_from_user(&data, tmp, 8)) {
111 err = -EFAULT;
112 break;
113 }
Nicolas Boichat78a62d22007-05-08 17:22:01 +0200114 err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
H. Peter Anvin0cc02132009-08-31 14:23:29 -0700115 if (err)
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700116 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 tmp += 2;
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700118 bytes += 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120
H. Peter Anvin85f1cb62008-08-25 17:34:27 -0700121 return bytes ? bytes : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
H. Peter Anvinff55df52009-08-31 14:16:57 -0700124static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
125{
126 u32 __user *uregs = (u32 __user *)arg;
127 u32 regs[8];
Al Viro6131ffa2013-02-27 16:59:05 -0500128 int cpu = iminor(file_inode(file));
H. Peter Anvinff55df52009-08-31 14:16:57 -0700129 int err;
130
131 switch (ioc) {
132 case X86_IOC_RDMSR_REGS:
133 if (!(file->f_mode & FMODE_READ)) {
134 err = -EBADF;
135 break;
136 }
137 if (copy_from_user(&regs, uregs, sizeof regs)) {
138 err = -EFAULT;
139 break;
140 }
141 err = rdmsr_safe_regs_on_cpu(cpu, regs);
142 if (err)
143 break;
144 if (copy_to_user(uregs, &regs, sizeof regs))
145 err = -EFAULT;
146 break;
147
148 case X86_IOC_WRMSR_REGS:
149 if (!(file->f_mode & FMODE_WRITE)) {
150 err = -EBADF;
151 break;
152 }
153 if (copy_from_user(&regs, uregs, sizeof regs)) {
154 err = -EFAULT;
155 break;
156 }
157 err = wrmsr_safe_regs_on_cpu(cpu, regs);
158 if (err)
159 break;
160 if (copy_to_user(uregs, &regs, sizeof regs))
161 err = -EFAULT;
162 break;
163
164 default:
165 err = -ENOTTY;
166 break;
167 }
168
169 return err;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static int msr_open(struct inode *inode, struct file *file)
173{
Al Viro6131ffa2013-02-27 16:59:05 -0500174 unsigned int cpu = iminor(file_inode(file));
H. Peter Anvin494c2eb2009-12-14 10:02:18 -0800175 struct cpuinfo_x86 *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Alan Coxc903f042012-11-15 13:06:22 +0000177 if (!capable(CAP_SYS_RAWIO))
178 return -EPERM;
179
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
Al Viro2c9ede52011-07-23 20:24:48 -0400240static char *msr_devnode(struct device *dev, umode_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;
Silas Boyd-Wickizera2db6722012-08-03 12:33:27 -0700262 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 for_each_online_cpu(i) {
Greg Kroah-Hartmana271aaf2006-08-07 22:19:37 -0700264 err = msr_device_create(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (err != 0)
266 goto out_class;
267 }
Andrew Mortone09793b2006-06-30 01:55:29 -0700268 register_hotcpu_notifier(&msr_class_cpu_notifier);
Silas Boyd-Wickizera2db6722012-08-03 12:33:27 -0700269 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 err = 0;
272 goto out;
273
274out_class:
275 i = 0;
276 for_each_online_cpu(i)
Akinobu Mita881a8412007-10-18 03:05:14 -0700277 msr_device_destroy(i);
Silas Boyd-Wickizera2db6722012-08-03 12:33:27 -0700278 put_online_cpus();
gregkh@suse.de8874b412005-03-23 09:56:34 -0800279 class_destroy(msr_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280out_chrdev:
H. Peter Anvin0b962d42009-12-15 15:13:07 -0800281 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282out:
283 return err;
284}
285
286static void __exit msr_exit(void)
287{
288 int cpu = 0;
Silas Boyd-Wickizera2db6722012-08-03 12:33:27 -0700289 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 for_each_online_cpu(cpu)
Akinobu Mita881a8412007-10-18 03:05:14 -0700291 msr_device_destroy(cpu);
gregkh@suse.de8874b412005-03-23 09:56:34 -0800292 class_destroy(msr_class);
Russ Andersonda482472010-01-26 20:37:22 -0600293 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
Andrew Mortone09793b2006-06-30 01:55:29 -0700294 unregister_hotcpu_notifier(&msr_class_cpu_notifier);
Silas Boyd-Wickizera2db6722012-08-03 12:33:27 -0700295 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298module_init(msr_init);
299module_exit(msr_exit)
300
301MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
302MODULE_DESCRIPTION("x86 generic MSR driver");
303MODULE_LICENSE("GPL");