msr.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 2000 H. Peter Anvin - All Rights Reserved
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
  8. * USA; either version 2 of the License, or (at your option) any later
  9. * version; incorporated herein by reference.
  10. *
  11. * ----------------------------------------------------------------------- */
  12. /*
  13. * msr.c
  14. *
  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. #include <linux/module.h>
  25. #include <linux/config.h>
  26. #include <linux/types.h>
  27. #include <linux/errno.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/init.h>
  30. #include <linux/poll.h>
  31. #include <linux/smp.h>
  32. #include <linux/smp_lock.h>
  33. #include <linux/major.h>
  34. #include <linux/fs.h>
  35. #include <linux/device.h>
  36. #include <linux/cpu.h>
  37. #include <linux/notifier.h>
  38. #include <asm/processor.h>
  39. #include <asm/msr.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/system.h>
  42. static struct class *msr_class;
  43. /* Note: "err" is handled in a funny way below. Otherwise one version
  44. of gcc or another breaks. */
  45. static inline int wrmsr_eio(u32 reg, u32 eax, u32 edx)
  46. {
  47. int err;
  48. asm volatile ("1: wrmsr\n"
  49. "2:\n"
  50. ".section .fixup,\"ax\"\n"
  51. "3: movl %4,%0\n"
  52. " jmp 2b\n"
  53. ".previous\n"
  54. ".section __ex_table,\"a\"\n"
  55. " .align 4\n" " .long 1b,3b\n" ".previous":"=&bDS" (err)
  56. :"a"(eax), "d"(edx), "c"(reg), "i"(-EIO), "0"(0));
  57. return err;
  58. }
  59. static inline int rdmsr_eio(u32 reg, u32 *eax, u32 *edx)
  60. {
  61. int err;
  62. asm volatile ("1: rdmsr\n"
  63. "2:\n"
  64. ".section .fixup,\"ax\"\n"
  65. "3: movl %4,%0\n"
  66. " jmp 2b\n"
  67. ".previous\n"
  68. ".section __ex_table,\"a\"\n"
  69. " .align 4\n"
  70. " .long 1b,3b\n"
  71. ".previous":"=&bDS" (err), "=a"(*eax), "=d"(*edx)
  72. :"c"(reg), "i"(-EIO), "0"(0));
  73. return err;
  74. }
  75. #ifdef CONFIG_SMP
  76. struct msr_command {
  77. int cpu;
  78. int err;
  79. u32 reg;
  80. u32 data[2];
  81. };
  82. static void msr_smp_wrmsr(void *cmd_block)
  83. {
  84. struct msr_command *cmd = (struct msr_command *)cmd_block;
  85. if (cmd->cpu == smp_processor_id())
  86. cmd->err = wrmsr_eio(cmd->reg, cmd->data[0], cmd->data[1]);
  87. }
  88. static void msr_smp_rdmsr(void *cmd_block)
  89. {
  90. struct msr_command *cmd = (struct msr_command *)cmd_block;
  91. if (cmd->cpu == smp_processor_id())
  92. cmd->err = rdmsr_eio(cmd->reg, &cmd->data[0], &cmd->data[1]);
  93. }
  94. static inline int do_wrmsr(int cpu, u32 reg, u32 eax, u32 edx)
  95. {
  96. struct msr_command cmd;
  97. int ret;
  98. preempt_disable();
  99. if (cpu == smp_processor_id()) {
  100. ret = wrmsr_eio(reg, eax, edx);
  101. } else {
  102. cmd.cpu = cpu;
  103. cmd.reg = reg;
  104. cmd.data[0] = eax;
  105. cmd.data[1] = edx;
  106. smp_call_function(msr_smp_wrmsr, &cmd, 1, 1);
  107. ret = cmd.err;
  108. }
  109. preempt_enable();
  110. return ret;
  111. }
  112. static inline int do_rdmsr(int cpu, u32 reg, u32 * eax, u32 * edx)
  113. {
  114. struct msr_command cmd;
  115. int ret;
  116. preempt_disable();
  117. if (cpu == smp_processor_id()) {
  118. ret = rdmsr_eio(reg, eax, edx);
  119. } else {
  120. cmd.cpu = cpu;
  121. cmd.reg = reg;
  122. smp_call_function(msr_smp_rdmsr, &cmd, 1, 1);
  123. *eax = cmd.data[0];
  124. *edx = cmd.data[1];
  125. ret = cmd.err;
  126. }
  127. preempt_enable();
  128. return ret;
  129. }
  130. #else /* ! CONFIG_SMP */
  131. static inline int do_wrmsr(int cpu, u32 reg, u32 eax, u32 edx)
  132. {
  133. return wrmsr_eio(reg, eax, edx);
  134. }
  135. static inline int do_rdmsr(int cpu, u32 reg, u32 *eax, u32 *edx)
  136. {
  137. return rdmsr_eio(reg, eax, edx);
  138. }
  139. #endif /* ! CONFIG_SMP */
  140. static loff_t msr_seek(struct file *file, loff_t offset, int orig)
  141. {
  142. loff_t ret = -EINVAL;
  143. lock_kernel();
  144. switch (orig) {
  145. case 0:
  146. file->f_pos = offset;
  147. ret = file->f_pos;
  148. break;
  149. case 1:
  150. file->f_pos += offset;
  151. ret = file->f_pos;
  152. }
  153. unlock_kernel();
  154. return ret;
  155. }
  156. static ssize_t msr_read(struct file *file, char __user * buf,
  157. size_t count, loff_t * ppos)
  158. {
  159. u32 __user *tmp = (u32 __user *) buf;
  160. u32 data[2];
  161. size_t rv;
  162. u32 reg = *ppos;
  163. int cpu = iminor(file->f_dentry->d_inode);
  164. int err;
  165. if (count % 8)
  166. return -EINVAL; /* Invalid chunk size */
  167. for (rv = 0; count; count -= 8) {
  168. err = do_rdmsr(cpu, reg, &data[0], &data[1]);
  169. if (err)
  170. return err;
  171. if (copy_to_user(tmp, &data, 8))
  172. return -EFAULT;
  173. tmp += 2;
  174. }
  175. return ((char __user *)tmp) - buf;
  176. }
  177. static ssize_t msr_write(struct file *file, const char __user *buf,
  178. size_t count, loff_t *ppos)
  179. {
  180. const u32 __user *tmp = (const u32 __user *)buf;
  181. u32 data[2];
  182. size_t rv;
  183. u32 reg = *ppos;
  184. int cpu = iminor(file->f_dentry->d_inode);
  185. int err;
  186. if (count % 8)
  187. return -EINVAL; /* Invalid chunk size */
  188. for (rv = 0; count; count -= 8) {
  189. if (copy_from_user(&data, tmp, 8))
  190. return -EFAULT;
  191. err = do_wrmsr(cpu, reg, data[0], data[1]);
  192. if (err)
  193. return err;
  194. tmp += 2;
  195. }
  196. return ((char __user *)tmp) - buf;
  197. }
  198. static int msr_open(struct inode *inode, struct file *file)
  199. {
  200. unsigned int cpu = iminor(file->f_dentry->d_inode);
  201. struct cpuinfo_x86 *c = &(cpu_data)[cpu];
  202. if (cpu >= NR_CPUS || !cpu_online(cpu))
  203. return -ENXIO; /* No such CPU */
  204. if (!cpu_has(c, X86_FEATURE_MSR))
  205. return -EIO; /* MSR not supported */
  206. return 0;
  207. }
  208. /*
  209. * File operations we support
  210. */
  211. static struct file_operations msr_fops = {
  212. .owner = THIS_MODULE,
  213. .llseek = msr_seek,
  214. .read = msr_read,
  215. .write = msr_write,
  216. .open = msr_open,
  217. };
  218. static int msr_class_device_create(int i)
  219. {
  220. int err = 0;
  221. struct class_device *class_err;
  222. class_err = class_device_create(msr_class, MKDEV(MSR_MAJOR, i), NULL, "msr%d",i);
  223. if (IS_ERR(class_err))
  224. err = PTR_ERR(class_err);
  225. return err;
  226. }
  227. static int __devinit msr_class_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  228. {
  229. unsigned int cpu = (unsigned long)hcpu;
  230. switch (action) {
  231. case CPU_ONLINE:
  232. msr_class_device_create(cpu);
  233. break;
  234. case CPU_DEAD:
  235. class_device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
  236. break;
  237. }
  238. return NOTIFY_OK;
  239. }
  240. static struct notifier_block msr_class_cpu_notifier =
  241. {
  242. .notifier_call = msr_class_cpu_callback,
  243. };
  244. static int __init msr_init(void)
  245. {
  246. int i, err = 0;
  247. i = 0;
  248. if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
  249. printk(KERN_ERR "msr: unable to get major %d for msr\n",
  250. MSR_MAJOR);
  251. err = -EBUSY;
  252. goto out;
  253. }
  254. msr_class = class_create(THIS_MODULE, "msr");
  255. if (IS_ERR(msr_class)) {
  256. err = PTR_ERR(msr_class);
  257. goto out_chrdev;
  258. }
  259. for_each_online_cpu(i) {
  260. err = msr_class_device_create(i);
  261. if (err != 0)
  262. goto out_class;
  263. }
  264. register_cpu_notifier(&msr_class_cpu_notifier);
  265. err = 0;
  266. goto out;
  267. out_class:
  268. i = 0;
  269. for_each_online_cpu(i)
  270. class_device_destroy(msr_class, MKDEV(MSR_MAJOR, i));
  271. class_destroy(msr_class);
  272. out_chrdev:
  273. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  274. out:
  275. return err;
  276. }
  277. static void __exit msr_exit(void)
  278. {
  279. int cpu = 0;
  280. for_each_online_cpu(cpu)
  281. class_device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
  282. class_destroy(msr_class);
  283. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  284. unregister_cpu_notifier(&msr_class_cpu_notifier);
  285. }
  286. module_init(msr_init);
  287. module_exit(msr_exit)
  288. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  289. MODULE_DESCRIPTION("x86 generic MSR driver");
  290. MODULE_LICENSE("GPL");