msr.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/types.h>
  26. #include <linux/errno.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/init.h>
  29. #include <linux/poll.h>
  30. #include <linux/smp.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/major.h>
  33. #include <linux/fs.h>
  34. #include <linux/device.h>
  35. #include <linux/cpu.h>
  36. #include <linux/notifier.h>
  37. #include <asm/processor.h>
  38. #include <asm/msr.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/system.h>
  41. static struct class *msr_class;
  42. static loff_t msr_seek(struct file *file, loff_t offset, int orig)
  43. {
  44. loff_t ret = -EINVAL;
  45. lock_kernel();
  46. switch (orig) {
  47. case 0:
  48. file->f_pos = offset;
  49. ret = file->f_pos;
  50. break;
  51. case 1:
  52. file->f_pos += offset;
  53. ret = file->f_pos;
  54. }
  55. unlock_kernel();
  56. return ret;
  57. }
  58. static ssize_t msr_read(struct file *file, char __user * buf,
  59. size_t count, loff_t * ppos)
  60. {
  61. u32 __user *tmp = (u32 __user *) buf;
  62. u32 data[2];
  63. u32 reg = *ppos;
  64. int cpu = iminor(file->f_path.dentry->d_inode);
  65. int err;
  66. if (count % 8)
  67. return -EINVAL; /* Invalid chunk size */
  68. for (; count; count -= 8) {
  69. err = rdmsr_safe_on_cpu(cpu, reg, &data[0], &data[1]);
  70. if (err)
  71. return -EIO;
  72. if (copy_to_user(tmp, &data, 8))
  73. return -EFAULT;
  74. tmp += 2;
  75. }
  76. return ((char __user *)tmp) - buf;
  77. }
  78. static ssize_t msr_write(struct file *file, const char __user *buf,
  79. size_t count, loff_t *ppos)
  80. {
  81. const u32 __user *tmp = (const u32 __user *)buf;
  82. u32 data[2];
  83. u32 reg = *ppos;
  84. int cpu = iminor(file->f_path.dentry->d_inode);
  85. int err;
  86. if (count % 8)
  87. return -EINVAL; /* Invalid chunk size */
  88. for (; count; count -= 8) {
  89. if (copy_from_user(&data, tmp, 8))
  90. return -EFAULT;
  91. err = wrmsr_safe_on_cpu(cpu, reg, data[0], data[1]);
  92. if (err)
  93. return -EIO;
  94. tmp += 2;
  95. }
  96. return ((char __user *)tmp) - buf;
  97. }
  98. static int msr_open(struct inode *inode, struct file *file)
  99. {
  100. unsigned int cpu = iminor(file->f_path.dentry->d_inode);
  101. struct cpuinfo_x86 *c = &(cpu_data)[cpu];
  102. if (cpu >= NR_CPUS || !cpu_online(cpu))
  103. return -ENXIO; /* No such CPU */
  104. if (!cpu_has(c, X86_FEATURE_MSR))
  105. return -EIO; /* MSR not supported */
  106. return 0;
  107. }
  108. /*
  109. * File operations we support
  110. */
  111. static const struct file_operations msr_fops = {
  112. .owner = THIS_MODULE,
  113. .llseek = msr_seek,
  114. .read = msr_read,
  115. .write = msr_write,
  116. .open = msr_open,
  117. };
  118. static int msr_device_create(int i)
  119. {
  120. int err = 0;
  121. struct device *dev;
  122. dev = device_create(msr_class, NULL, MKDEV(MSR_MAJOR, i), "msr%d",i);
  123. if (IS_ERR(dev))
  124. err = PTR_ERR(dev);
  125. return err;
  126. }
  127. static int msr_class_cpu_callback(struct notifier_block *nfb,
  128. unsigned long action, void *hcpu)
  129. {
  130. unsigned int cpu = (unsigned long)hcpu;
  131. switch (action) {
  132. case CPU_ONLINE:
  133. case CPU_ONLINE_FROZEN:
  134. msr_device_create(cpu);
  135. break;
  136. case CPU_DEAD:
  137. case CPU_DEAD_FROZEN:
  138. device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
  139. break;
  140. }
  141. return NOTIFY_OK;
  142. }
  143. static struct notifier_block __cpuinitdata msr_class_cpu_notifier =
  144. {
  145. .notifier_call = msr_class_cpu_callback,
  146. };
  147. static int __init msr_init(void)
  148. {
  149. int i, err = 0;
  150. i = 0;
  151. if (register_chrdev(MSR_MAJOR, "cpu/msr", &msr_fops)) {
  152. printk(KERN_ERR "msr: unable to get major %d for msr\n",
  153. MSR_MAJOR);
  154. err = -EBUSY;
  155. goto out;
  156. }
  157. msr_class = class_create(THIS_MODULE, "msr");
  158. if (IS_ERR(msr_class)) {
  159. err = PTR_ERR(msr_class);
  160. goto out_chrdev;
  161. }
  162. for_each_online_cpu(i) {
  163. err = msr_device_create(i);
  164. if (err != 0)
  165. goto out_class;
  166. }
  167. register_hotcpu_notifier(&msr_class_cpu_notifier);
  168. err = 0;
  169. goto out;
  170. out_class:
  171. i = 0;
  172. for_each_online_cpu(i)
  173. device_destroy(msr_class, MKDEV(MSR_MAJOR, i));
  174. class_destroy(msr_class);
  175. out_chrdev:
  176. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  177. out:
  178. return err;
  179. }
  180. static void __exit msr_exit(void)
  181. {
  182. int cpu = 0;
  183. for_each_online_cpu(cpu)
  184. device_destroy(msr_class, MKDEV(MSR_MAJOR, cpu));
  185. class_destroy(msr_class);
  186. unregister_chrdev(MSR_MAJOR, "cpu/msr");
  187. unregister_hotcpu_notifier(&msr_class_cpu_notifier);
  188. }
  189. module_init(msr_init);
  190. module_exit(msr_exit)
  191. MODULE_AUTHOR("H. Peter Anvin <hpa@zytor.com>");
  192. MODULE_DESCRIPTION("x86 generic MSR driver");
  193. MODULE_LICENSE("GPL");