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. * 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. msr_device_create(cpu);
  134. break;
  135. case CPU_DEAD:
  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");