Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* ----------------------------------------------------------------------- * |
2b06ac86 PA |
2 | * |
3 | * Copyright 2000-2008 H. Peter Anvin - All Rights Reserved | |
ff55df53 | 4 | * Copyright 2009 Intel Corporation; author: H. Peter Anvin |
1da177e4 LT |
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 | /* | |
1da177e4 LT |
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> | |
1da177e4 LT |
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> | |
448dd2fa | 39 | #include <linux/uaccess.h> |
1da177e4 LT |
40 | |
41 | #include <asm/processor.h> | |
42 | #include <asm/msr.h> | |
1da177e4 LT |
43 | #include <asm/system.h> |
44 | ||
8874b414 | 45 | static struct class *msr_class; |
1da177e4 | 46 | |
1da177e4 LT |
47 | static loff_t msr_seek(struct file *file, loff_t offset, int orig) |
48 | { | |
2b06ac86 PA |
49 | loff_t ret; |
50 | struct inode *inode = file->f_mapping->host; | |
1da177e4 | 51 | |
2b06ac86 | 52 | mutex_lock(&inode->i_mutex); |
1da177e4 LT |
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; | |
2b06ac86 PA |
61 | break; |
62 | default: | |
63 | ret = -EINVAL; | |
1da177e4 | 64 | } |
2b06ac86 | 65 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
66 | return ret; |
67 | } | |
68 | ||
94a9fa41 PC |
69 | static ssize_t msr_read(struct file *file, char __user *buf, |
70 | size_t count, loff_t *ppos) | |
1da177e4 LT |
71 | { |
72 | u32 __user *tmp = (u32 __user *) buf; | |
73 | u32 data[2]; | |
1da177e4 | 74 | u32 reg = *ppos; |
aab4c5a5 | 75 | int cpu = iminor(file->f_path.dentry->d_inode); |
85f1cb60 PA |
76 | int err = 0; |
77 | ssize_t bytes = 0; | |
1da177e4 LT |
78 | |
79 | if (count % 8) | |
80 | return -EINVAL; /* Invalid chunk size */ | |
81 | ||
6926d570 | 82 | for (; count; count -= 8) { |
78a62d2c | 83 | err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]); |
0cc0213e | 84 | if (err) |
85f1cb60 | 85 | break; |
85f1cb60 PA |
86 | if (copy_to_user(tmp, &data, 8)) { |
87 | err = -EFAULT; | |
88 | break; | |
c6f31932 | 89 | } |
1da177e4 | 90 | tmp += 2; |
85f1cb60 | 91 | bytes += 8; |
1da177e4 LT |
92 | } |
93 | ||
85f1cb60 | 94 | return bytes ? bytes : err; |
1da177e4 LT |
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]; | |
1da177e4 | 102 | u32 reg = *ppos; |
aab4c5a5 | 103 | int cpu = iminor(file->f_path.dentry->d_inode); |
85f1cb60 PA |
104 | int err = 0; |
105 | ssize_t bytes = 0; | |
1da177e4 LT |
106 | |
107 | if (count % 8) | |
108 | return -EINVAL; /* Invalid chunk size */ | |
109 | ||
f475ff35 | 110 | for (; count; count -= 8) { |
85f1cb60 PA |
111 | if (copy_from_user(&data, tmp, 8)) { |
112 | err = -EFAULT; | |
113 | break; | |
114 | } | |
78a62d2c | 115 | err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]); |
0cc0213e | 116 | if (err) |
85f1cb60 | 117 | break; |
1da177e4 | 118 | tmp += 2; |
85f1cb60 | 119 | bytes += 8; |
1da177e4 LT |
120 | } |
121 | ||
85f1cb60 | 122 | return bytes ? bytes : err; |
1da177e4 LT |
123 | } |
124 | ||
ff55df53 PA |
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 | ||
1da177e4 LT |
173 | static int msr_open(struct inode *inode, struct file *file) |
174 | { | |
aab4c5a5 | 175 | unsigned int cpu = iminor(file->f_path.dentry->d_inode); |
92cb7612 | 176 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
5119e92e | 177 | int ret = 0; |
1da177e4 | 178 | |
5119e92e JC |
179 | lock_kernel(); |
180 | cpu = iminor(file->f_path.dentry->d_inode); | |
1da177e4 | 181 | |
9628937d | 182 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) { |
5119e92e JC |
183 | ret = -ENXIO; /* No such CPU */ |
184 | goto out; | |
185 | } | |
186 | c = &cpu_data(cpu); | |
187 | if (!cpu_has(c, X86_FEATURE_MSR)) | |
188 | ret = -EIO; /* MSR not supported */ | |
189 | out: | |
190 | unlock_kernel(); | |
967060d0 | 191 | return ret; |
1da177e4 LT |
192 | } |
193 | ||
194 | /* | |
195 | * File operations we support | |
196 | */ | |
5dfe4c96 | 197 | static const struct file_operations msr_fops = { |
1da177e4 LT |
198 | .owner = THIS_MODULE, |
199 | .llseek = msr_seek, | |
200 | .read = msr_read, | |
201 | .write = msr_write, | |
202 | .open = msr_open, | |
ff55df53 PA |
203 | .unlocked_ioctl = msr_ioctl, |
204 | .compat_ioctl = msr_ioctl, | |
1da177e4 LT |
205 | }; |
206 | ||
38048983 | 207 | static int __cpuinit msr_device_create(int cpu) |
1da177e4 | 208 | { |
a271aaf1 | 209 | struct device *dev; |
1da177e4 | 210 | |
a9b12619 GKH |
211 | dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu), NULL, |
212 | "msr%d", cpu); | |
881a841f AM |
213 | return IS_ERR(dev) ? PTR_ERR(dev) : 0; |
214 | } | |
215 | ||
216 | static void msr_device_destroy(int cpu) | |
217 | { | |
218 | device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu)); | |
1da177e4 LT |
219 | } |
220 | ||
761c4bf8 | 221 | static int __cpuinit msr_class_cpu_callback(struct notifier_block *nfb, |
e09793bb | 222 | unsigned long action, void *hcpu) |
1da177e4 LT |
223 | { |
224 | unsigned int cpu = (unsigned long)hcpu; | |
881a841f | 225 | int err = 0; |
1da177e4 LT |
226 | |
227 | switch (action) { | |
881a841f | 228 | case CPU_UP_PREPARE: |
881a841f | 229 | err = msr_device_create(cpu); |
1da177e4 | 230 | break; |
881a841f | 231 | case CPU_UP_CANCELED: |
b844eba2 | 232 | case CPU_UP_CANCELED_FROZEN: |
1da177e4 | 233 | case CPU_DEAD: |
881a841f | 234 | msr_device_destroy(cpu); |
1da177e4 LT |
235 | break; |
236 | } | |
881a841f | 237 | return err ? NOTIFY_BAD : NOTIFY_OK; |
1da177e4 LT |
238 | } |
239 | ||
c72258c7 | 240 | static struct notifier_block __refdata msr_class_cpu_notifier = { |
1da177e4 LT |
241 | .notifier_call = msr_class_cpu_callback, |
242 | }; | |
243 | ||
07e9bb8e KS |
244 | static char *msr_nodename(struct device *dev) |
245 | { | |
246 | return kasprintf(GFP_KERNEL, "cpu/%u/msr", MINOR(dev->devt)); | |
247 | } | |
248 | ||
1da177e4 LT |
249 | static int __init msr_init(void) |
250 | { | |
251 | int i, err = 0; | |
252 | i = 0; | |
253 | ||
254 | if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) { | |
255 | printk(KERN_ERR "msr: unable to get major %d for msr\n", | |
256 | MSR_MAJOR); | |
257 | err = -EBUSY; | |
258 | goto out; | |
259 | } | |
8874b414 | 260 | msr_class = class_create(THIS_MODULE, "msr"); |
1da177e4 LT |
261 | if (IS_ERR(msr_class)) { |
262 | err = PTR_ERR(msr_class); | |
263 | goto out_chrdev; | |
264 | } | |
07e9bb8e | 265 | msr_class->nodename = msr_nodename; |
1da177e4 | 266 | for_each_online_cpu(i) { |
a271aaf1 | 267 | err = msr_device_create(i); |
1da177e4 LT |
268 | if (err != 0) |
269 | goto out_class; | |
270 | } | |
e09793bb | 271 | register_hotcpu_notifier(&msr_class_cpu_notifier); |
1da177e4 LT |
272 | |
273 | err = 0; | |
274 | goto out; | |
275 | ||
276 | out_class: | |
277 | i = 0; | |
278 | for_each_online_cpu(i) | |
881a841f | 279 | msr_device_destroy(i); |
8874b414 | 280 | class_destroy(msr_class); |
1da177e4 LT |
281 | out_chrdev: |
282 | unregister_chrdev(MSR_MAJOR, "cpu/msr"); | |
283 | out: | |
284 | return err; | |
285 | } | |
286 | ||
287 | static void __exit msr_exit(void) | |
288 | { | |
289 | int cpu = 0; | |
290 | for_each_online_cpu(cpu) | |
881a841f | 291 | msr_device_destroy(cpu); |
8874b414 | 292 | class_destroy(msr_class); |
1da177e4 | 293 | unregister_chrdev(MSR_MAJOR, "cpu/msr"); |
e09793bb | 294 | unregister_hotcpu_notifier(&msr_class_cpu_notifier); |
1da177e4 LT |
295 | } |
296 | ||
297 | module_init(msr_init); | |
298 | module_exit(msr_exit) | |
299 | ||
300 | MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>"); | |
301 | MODULE_DESCRIPTION("x86 generic MSR driver"); | |
302 | MODULE_LICENSE("GPL"); |