msr.c 5.0 KB

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