vfs: remove unused wrapper block_page_mkwrite()
[linux/fpc-iii.git] / arch / x86 / kernel / msr.c
blob113e70784854fb55bdc0eb08fe5e38bee4946126
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
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.
12 * ----------------------------------------------------------------------- */
15 * x86 MSR access device
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.
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.
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/module.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/major.h>
36 #include <linux/fs.h>
37 #include <linux/device.h>
38 #include <linux/cpu.h>
39 #include <linux/notifier.h>
40 #include <linux/uaccess.h>
41 #include <linux/gfp.h>
43 #include <asm/processor.h>
44 #include <asm/msr.h>
46 static struct class *msr_class;
48 static loff_t msr_seek(struct file *file, loff_t offset, int orig)
50 loff_t ret;
51 struct inode *inode = file_inode(file);
53 mutex_lock(&inode->i_mutex);
54 switch (orig) {
55 case SEEK_SET:
56 file->f_pos = offset;
57 ret = file->f_pos;
58 break;
59 case SEEK_CUR:
60 file->f_pos += offset;
61 ret = file->f_pos;
62 break;
63 default:
64 ret = -EINVAL;
66 mutex_unlock(&inode->i_mutex);
67 return ret;
70 static ssize_t msr_read(struct file *file, char __user *buf,
71 size_t count, loff_t *ppos)
73 u32 __user *tmp = (u32 __user *) buf;
74 u32 data[2];
75 u32 reg = *ppos;
76 int cpu = iminor(file_inode(file));
77 int err = 0;
78 ssize_t bytes = 0;
80 if (count % 8)
81 return -EINVAL; /* Invalid chunk size */
83 for (; count; count -= 8) {
84 err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
85 if (err)
86 break;
87 if (copy_to_user(tmp, &data, 8)) {
88 err = -EFAULT;
89 break;
91 tmp += 2;
92 bytes += 8;
95 return bytes ? bytes : err;
98 static ssize_t msr_write(struct file *file, const char __user *buf,
99 size_t count, loff_t *ppos)
101 const u32 __user *tmp = (const u32 __user *)buf;
102 u32 data[2];
103 u32 reg = *ppos;
104 int cpu = iminor(file_inode(file));
105 int err = 0;
106 ssize_t bytes = 0;
108 if (count % 8)
109 return -EINVAL; /* Invalid chunk size */
111 for (; count; count -= 8) {
112 if (copy_from_user(&data, tmp, 8)) {
113 err = -EFAULT;
114 break;
116 err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
117 if (err)
118 break;
119 tmp += 2;
120 bytes += 8;
123 return bytes ? bytes : err;
126 static long msr_ioctl(struct file *file, unsigned int ioc, unsigned long arg)
128 u32 __user *uregs = (u32 __user *)arg;
129 u32 regs[8];
130 int cpu = iminor(file_inode(file));
131 int err;
133 switch (ioc) {
134 case X86_IOC_RDMSR_REGS:
135 if (!(file->f_mode & FMODE_READ)) {
136 err = -EBADF;
137 break;
139 if (copy_from_user(&regs, uregs, sizeof regs)) {
140 err = -EFAULT;
141 break;
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;
150 case X86_IOC_WRMSR_REGS:
151 if (!(file->f_mode & FMODE_WRITE)) {
152 err = -EBADF;
153 break;
155 if (copy_from_user(&regs, uregs, sizeof regs)) {
156 err = -EFAULT;
157 break;
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;
166 default:
167 err = -ENOTTY;
168 break;
171 return err;
174 static int msr_open(struct inode *inode, struct file *file)
176 unsigned int cpu = iminor(file_inode(file));
177 struct cpuinfo_x86 *c;
179 if (!capable(CAP_SYS_RAWIO))
180 return -EPERM;
182 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
183 return -ENXIO; /* No such CPU */
185 c = &cpu_data(cpu);
186 if (!cpu_has(c, X86_FEATURE_MSR))
187 return -EIO; /* MSR not supported */
189 return 0;
193 * File operations we support
195 static const struct file_operations msr_fops = {
196 .owner = THIS_MODULE,
197 .llseek = msr_seek,
198 .read = msr_read,
199 .write = msr_write,
200 .open = msr_open,
201 .unlocked_ioctl = msr_ioctl,
202 .compat_ioctl = msr_ioctl,
205 static int msr_device_create(int cpu)
207 struct device *dev;
209 dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL,
210 "msr%d", cpu);
211 return PTR_ERR_OR_ZERO(dev);
214 static void msr_device_destroy(int cpu)
216 device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
219 static int msr_class_cpu_callback(struct notifier_block *nfb,
220 unsigned long action, void *hcpu)
222 unsigned int cpu = (unsigned long)hcpu;
223 int err = 0;
225 switch (action) {
226 case CPU_UP_PREPARE:
227 err = msr_device_create(cpu);
228 break;
229 case CPU_UP_CANCELED:
230 case CPU_UP_CANCELED_FROZEN:
231 case CPU_DEAD:
232 msr_device_destroy(cpu);
233 break;
235 return notifier_from_errno(err);
238 static struct notifier_block __refdata msr_class_cpu_notifier = {
239 .notifier_call = msr_class_cpu_callback,
242 static char *msr_devnode(struct device *dev, umode_t *mode)
244 return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt));
247 static int __init msr_init(void)
249 int i, err = 0;
250 i = 0;
252 if (__register_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr", &msr_fops)) {
253 pr_err("unable to get major %d for msr\n", MSR_MAJOR);
254 err = -EBUSY;
255 goto out;
257 msr_class = class_create(THIS_MODULE, "msr");
258 if (IS_ERR(msr_class)) {
259 err = PTR_ERR(msr_class);
260 goto out_chrdev;
262 msr_class->devnode = msr_devnode;
264 cpu_notifier_register_begin();
265 for_each_online_cpu(i) {
266 err = msr_device_create(i);
267 if (err != 0)
268 goto out_class;
270 __register_hotcpu_notifier(&msr_class_cpu_notifier);
271 cpu_notifier_register_done();
273 err = 0;
274 goto out;
276 out_class:
277 i = 0;
278 for_each_online_cpu(i)
279 msr_device_destroy(i);
280 cpu_notifier_register_done();
281 class_destroy(msr_class);
282 out_chrdev:
283 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
284 out:
285 return err;
288 static void __exit msr_exit(void)
290 int cpu = 0;
292 cpu_notifier_register_begin();
293 for_each_online_cpu(cpu)
294 msr_device_destroy(cpu);
295 class_destroy(msr_class);
296 __unregister_chrdev(MSR_MAJOR, 0, NR_CPUS, "cpu/msr");
297 __unregister_hotcpu_notifier(&msr_class_cpu_notifier);
298 cpu_notifier_register_done();
301 module_init(msr_init);
302 module_exit(msr_exit)
304 MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
305 MODULE_DESCRIPTION("x86 generic MSR driver");
306 MODULE_LICENSE("GPL");