msr.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. if (cpu >= NR_CPUS || !cpu_online(cpu))
  105. return -ENXIO; /* No such CPU */
  106. if (!cpu_has(c, X86_FEATURE_MSR))
  107. return -EIO; /* MSR not supported */
  108. return 0;
  109. }
  110. /*
  111. * File operations we support
  112. */
  113. static const struct file_operations msr_fops = {
  114. .owner = THIS_MODULE,
  115. .llseek = msr_seek,
  116. .read = msr_read,
  117. .write = msr_write,
  118. .open = msr_open,
  119. };
  120. static int __cpuinit msr_device_create(int cpu)
  121. {
  122. struct device *dev;
  123. dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, cpu),
  124. "msr%d", cpu);
  125. return IS_ERR(dev) ? PTR_ERR(dev) : 0;
  126. }
  127. static void msr_device_destroy(int cpu)
  128. {
  129. device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
  130. }
  131. static int __cpuinit msr_class_cpu_callback(struct notifier_block *nfb,
  132. unsigned long action, void *hcpu)
  133. {
  134. unsigned int cpu = (unsigned long)hcpu;
  135. int err = 0;
  136. switch (action) {
  137. case CPU_UP_PREPARE:
  138. err = msr_device_create(cpu);
  139. break;
  140. case CPU_UP_CANCELED:
  141. case CPU_UP_CANCELED_FROZEN:
  142. case CPU_DEAD:
  143. msr_device_destroy(cpu);
  144. break;
  145. }
  146. return err ? NOTIFY_BAD : NOTIFY_OK;
  147. }
  148. static struct notifier_block __refdata msr_class_cpu_notifier = {
  149. .notifier_call = msr_class_cpu_callback,
  150. };
  151. static int __init msr_init(void)
  152. {
  153. int i, err = 0;
  154. i = 0;
  155. if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
  156. printk(KERN_ERR "msr: unable to get major %d for msr\n",
  157. MSR_MAJOR);
  158. err = -EBUSY;
  159. goto out;
  160. }
  161. msr_class = class_create(THIS_MODULE, "msr");
  162. if (IS_ERR(msr_class)) {
  163. err = PTR_ERR(msr_class);
  164. goto out_chrdev;
  165. }
  166. for_each_online_cpu(i) {
  167. err = msr_device_create(i);
  168. if (err != 0)
  169. goto out_class;
  170. }
  171. register_hotcpu_notifier(&msr_class_cpu_notifier);
  172. err = 0;
  173. goto out;
  174. out_class:
  175. i = 0;
  176. for_each_online_cpu(i)
  177. msr_device_destroy(i);
  178. class_destroy(msr_class);
  179. out_chrdev:
  180. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  181. out:
  182. return err;
  183. }
  184. static void __exit msr_exit(void)
  185. {
  186. int cpu = 0;
  187. for_each_online_cpu(cpu)
  188. msr_device_destroy(cpu);
  189. class_destroy(msr_class);
  190. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  191. unregister_hotcpu_notifier(&msr_class_cpu_notifier);
  192. }
  193. module_init(msr_init);
  194. module_exit(msr_exit)
  195. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  196. MODULE_DESCRIPTION("x86 generic MSR driver");
  197. MODULE_LICENSE("GPL");