msr.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 2000-2008 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. * x86 MSR access device
  14. *
  15. * This device is accessed by lseek() to the appropriate register number
  16. * and then read/write in chunks of 8 bytes. A larger size means multiple
  17. * reads or writes of the same register.
  18. *
  19. * This driver uses /dev/cpu/%d/msr where %d is the minor number, and on
  20. * an SMP box will direct the access to CPU %d.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/errno.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/init.h>
  27. #include <linux/poll.h>
  28. #include <linux/smp.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/major.h>
  31. #include <linux/fs.h>
  32. #include <linux/device.h>
  33. #include <linux/cpu.h>
  34. #include <linux/notifier.h>
  35. #include <asm/processor.h>
  36. #include <asm/msr.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/system.h>
  39. static struct class *msr_class;
  40. static loff_t msr_seek(struct file *file, loff_t offset, int orig)
  41. {
  42. loff_t ret;
  43. struct inode *inode = file->f_mapping->host;
  44. mutex_lock(&inode->i_mutex);
  45. switch (orig) {
  46. case 0:
  47. file->f_pos = offset;
  48. ret = file->f_pos;
  49. break;
  50. case 1:
  51. file->f_pos += offset;
  52. ret = file->f_pos;
  53. break;
  54. default:
  55. ret = -EINVAL;
  56. }
  57. mutex_unlock(&inode->i_mutex);
  58. return ret;
  59. }
  60. static ssize_t msr_read(struct file *file, char __user *buf,
  61. size_t count, loff_t *ppos)
  62. {
  63. u32 __user *tmp = (u32 __user *) buf;
  64. u32 data[2];
  65. u32 reg = *ppos;
  66. int cpu = iminor(file->f_path.dentry->d_inode);
  67. int err;
  68. if (count % 8)
  69. return -EINVAL; /* Invalid chunk size */
  70. for (; count; count -= 8) {
  71. err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
  72. if (err) {
  73. if (err == -EFAULT) /* Fix idiotic error code */
  74. err = -EIO;
  75. return err;
  76. }
  77. if (copy_to_user(tmp, &data, 8))
  78. return -EFAULT;
  79. tmp += 2;
  80. }
  81. return ((char __user *)tmp) - buf;
  82. }
  83. static ssize_t msr_write(struct file *file, const char __user *buf,
  84. size_t count, loff_t *ppos)
  85. {
  86. const u32 __user *tmp = (const u32 __user *)buf;
  87. u32 data[2];
  88. u32 reg = *ppos;
  89. int cpu = iminor(file->f_path.dentry->d_inode);
  90. int err;
  91. if (count % 8)
  92. return -EINVAL; /* Invalid chunk size */
  93. for (; count; count -= 8) {
  94. if (copy_from_user(&data, tmp, 8))
  95. return -EFAULT;
  96. err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
  97. if (err) {
  98. if (err == -EFAULT) /* Fix idiotic error code */
  99. err = -EIO;
  100. return err;
  101. }
  102. tmp += 2;
  103. }
  104. return ((char __user *)tmp) - buf;
  105. }
  106. static int msr_open(struct inode *inode, struct file *file)
  107. {
  108. unsigned int cpu = iminor(file->f_path.dentry->d_inode);
  109. struct cpuinfo_x86 *c = &cpu_data(cpu);
  110. int ret = 0;
  111. lock_kernel();
  112. cpu = iminor(file->f_path.dentry->d_inode);
  113. if (cpu >= NR_CPUS || !cpu_online(cpu)) {
  114. ret = -ENXIO; /* No such CPU */
  115. goto out;
  116. }
  117. c = &cpu_data(cpu);
  118. if (!cpu_has(c, X86_FEATURE_MSR))
  119. ret = -EIO; /* MSR not supported */
  120. out:
  121. unlock_kernel();
  122. return ret;
  123. }
  124. /*
  125. * File operations we support
  126. */
  127. static const struct file_operations msr_fops = {
  128. .owner = THIS_MODULE,
  129. .llseek = msr_seek,
  130. .read = msr_read,
  131. .write = msr_write,
  132. .open = msr_open,
  133. };
  134. static int __cpuinit msr_device_create(int cpu)
  135. {
  136. struct device *dev;
  137. dev = device_create_drvdata(msr_class, NULL, MKDEV(MSR_MAJOR, cpu),
  138. NULL, "msr%d", cpu);
  139. return IS_ERR(dev) ? PTR_ERR(dev) : 0;
  140. }
  141. static void msr_device_destroy(int cpu)
  142. {
  143. device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
  144. }
  145. static int __cpuinit msr_class_cpu_callback(struct notifier_block *nfb,
  146. unsigned long action, void *hcpu)
  147. {
  148. unsigned int cpu = (unsigned long)hcpu;
  149. int err = 0;
  150. switch (action) {
  151. case CPU_UP_PREPARE:
  152. err = msr_device_create(cpu);
  153. break;
  154. case CPU_UP_CANCELED:
  155. case CPU_UP_CANCELED_FROZEN:
  156. case CPU_DEAD:
  157. msr_device_destroy(cpu);
  158. break;
  159. }
  160. return err ? NOTIFY_BAD : NOTIFY_OK;
  161. }
  162. static struct notifier_block __refdata msr_class_cpu_notifier = {
  163. .notifier_call = msr_class_cpu_callback,
  164. };
  165. static int __init msr_init(void)
  166. {
  167. int i, err = 0;
  168. i = 0;
  169. if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
  170. printk(KERN_ERR "msr: unable to get major %d for msr\n",
  171. MSR_MAJOR);
  172. err = -EBUSY;
  173. goto out;
  174. }
  175. msr_class = class_create(THIS_MODULE, "msr");
  176. if (IS_ERR(msr_class)) {
  177. err = PTR_ERR(msr_class);
  178. goto out_chrdev;
  179. }
  180. for_each_online_cpu(i) {
  181. err = msr_device_create(i);
  182. if (err != 0)
  183. goto out_class;
  184. }
  185. register_hotcpu_notifier(&msr_class_cpu_notifier);
  186. err = 0;
  187. goto out;
  188. out_class:
  189. i = 0;
  190. for_each_online_cpu(i)
  191. msr_device_destroy(i);
  192. class_destroy(msr_class);
  193. out_chrdev:
  194. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  195. out:
  196. return err;
  197. }
  198. static void __exit msr_exit(void)
  199. {
  200. int cpu = 0;
  201. for_each_online_cpu(cpu)
  202. msr_device_destroy(cpu);
  203. class_destroy(msr_class);
  204. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  205. unregister_hotcpu_notifier(&msr_class_cpu_notifier);
  206. }
  207. module_init(msr_init);
  208. module_exit(msr_exit)
  209. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  210. MODULE_DESCRIPTION("x86 generic MSR driver");
  211. MODULE_LICENSE("GPL");