msr.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. return -EIO;
  74. if (copy_to_user(tmp, &data, 8))
  75. return -EFAULT;
  76. tmp += 2;
  77. }
  78. return ((char __user *)tmp) - buf;
  79. }
  80. static ssize_t msr_write(struct file *file, const char __user *buf,
  81. size_t count, loff_t *ppos)
  82. {
  83. const u32 __user *tmp = (const u32 __user *)buf;
  84. u32 data[2];
  85. u32 reg = *ppos;
  86. int cpu = iminor(file->f_path.dentry->d_inode);
  87. int err;
  88. if (count % 8)
  89. return -EINVAL; /* Invalid chunk size */
  90. for (; count; count -= 8) {
  91. if (copy_from_user(&data, tmp, 8))
  92. return -EFAULT;
  93. err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
  94. if (err)
  95. return -EIO;
  96. tmp += 2;
  97. }
  98. return ((char __user *)tmp) - buf;
  99. }
  100. static int msr_open(struct inode *inode, struct file *file)
  101. {
  102. unsigned int cpu = iminor(file->f_path.dentry->d_inode);
  103. struct cpuinfo_x86 *c = &cpu_data(cpu);
  104. int ret = 0;
  105. lock_kernel();
  106. cpu = iminor(file->f_path.dentry->d_inode);
  107. if (cpu >= NR_CPUS || !cpu_online(cpu)) {
  108. ret = -ENXIO; /* No such CPU */
  109. goto out;
  110. }
  111. c = &cpu_data(cpu);
  112. if (!cpu_has(c, X86_FEATURE_MSR))
  113. ret = -EIO; /* MSR not supported */
  114. out:
  115. unlock_kernel();
  116. return 0;
  117. }
  118. /*
  119. * File operations we support
  120. */
  121. static const struct file_operations msr_fops = {
  122. .owner = THIS_MODULE,
  123. .llseek = msr_seek,
  124. .read = msr_read,
  125. .write = msr_write,
  126. .open = msr_open,
  127. };
  128. static int __cpuinit msr_device_create(int cpu)
  129. {
  130. struct device *dev;
  131. dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu),
  132. "msr%d", cpu);
  133. return IS_ERR(dev) ? PTR_ERR(dev) : 0;
  134. }
  135. static void msr_device_destroy(int cpu)
  136. {
  137. device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
  138. }
  139. static int __cpuinit msr_class_cpu_callback(struct notifier_block *nfb,
  140. unsigned long action, void *hcpu)
  141. {
  142. unsigned int cpu = (unsigned long)hcpu;
  143. int err = 0;
  144. switch (action) {
  145. case CPU_UP_PREPARE:
  146. err = msr_device_create(cpu);
  147. break;
  148. case CPU_UP_CANCELED:
  149. case CPU_UP_CANCELED_FROZEN:
  150. case CPU_DEAD:
  151. msr_device_destroy(cpu);
  152. break;
  153. }
  154. return err ? NOTIFY_BAD : NOTIFY_OK;
  155. }
  156. static struct notifier_block __refdata msr_class_cpu_notifier = {
  157. .notifier_call = msr_class_cpu_callback,
  158. };
  159. static int __init msr_init(void)
  160. {
  161. int i, err = 0;
  162. i = 0;
  163. if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
  164. printk(KERN_ERR "msr: unable to get major %d for msr\n",
  165. MSR_MAJOR);
  166. err = -EBUSY;
  167. goto out;
  168. }
  169. msr_class = class_create(THIS_MODULE, "msr");
  170. if (IS_ERR(msr_class)) {
  171. err = PTR_ERR(msr_class);
  172. goto out_chrdev;
  173. }
  174. for_each_online_cpu(i) {
  175. err = msr_device_create(i);
  176. if (err != 0)
  177. goto out_class;
  178. }
  179. register_hotcpu_notifier(&msr_class_cpu_notifier);
  180. err = 0;
  181. goto out;
  182. out_class:
  183. i = 0;
  184. for_each_online_cpu(i)
  185. msr_device_destroy(i);
  186. class_destroy(msr_class);
  187. out_chrdev:
  188. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  189. out:
  190. return err;
  191. }
  192. static void __exit msr_exit(void)
  193. {
  194. int cpu = 0;
  195. for_each_online_cpu(cpu)
  196. msr_device_destroy(cpu);
  197. class_destroy(msr_class);
  198. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  199. unregister_hotcpu_notifier(&msr_class_cpu_notifier);
  200. }
  201. module_init(msr_init);
  202. module_exit(msr_exit)
  203. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  204. MODULE_DESCRIPTION("x86 generic MSR driver");
  205. MODULE_LICENSE("GPL");