msr.c 4.9 KB

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