msr-smp.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include <linux/export.h>
  2. #include <linux/preempt.h>
  3. #include <linux/smp.h>
  4. #include <asm/msr.h>
  5. static void __rdmsr_on_cpu(void *info)
  6. {
  7. struct msr_info *rv = info;
  8. struct msr *reg;
  9. int this_cpu = raw_smp_processor_id();
  10. if (rv->msrs)
  11. reg = per_cpu_ptr(rv->msrs, this_cpu);
  12. else
  13. reg = &rv->reg;
  14. rdmsr(rv->msr_no, reg->l, reg->h);
  15. }
  16. static void __wrmsr_on_cpu(void *info)
  17. {
  18. struct msr_info *rv = info;
  19. struct msr *reg;
  20. int this_cpu = raw_smp_processor_id();
  21. if (rv->msrs)
  22. reg = per_cpu_ptr(rv->msrs, this_cpu);
  23. else
  24. reg = &rv->reg;
  25. wrmsr(rv->msr_no, reg->l, reg->h);
  26. }
  27. int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  28. {
  29. int err;
  30. struct msr_info rv;
  31. memset(&rv, 0, sizeof(rv));
  32. rv.msr_no = msr_no;
  33. err = smp_call_function_single(cpu, __rdmsr_on_cpu, &rv, 1);
  34. *l = rv.reg.l;
  35. *h = rv.reg.h;
  36. return err;
  37. }
  38. EXPORT_SYMBOL(rdmsr_on_cpu);
  39. int rdmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
  40. {
  41. int err;
  42. struct msr_info rv;
  43. memset(&rv, 0, sizeof(rv));
  44. rv.msr_no = msr_no;
  45. err = smp_call_function_single(cpu, __rdmsr_on_cpu, &rv, 1);
  46. *q = rv.reg.q;
  47. return err;
  48. }
  49. EXPORT_SYMBOL(rdmsrl_on_cpu);
  50. int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  51. {
  52. int err;
  53. struct msr_info rv;
  54. memset(&rv, 0, sizeof(rv));
  55. rv.msr_no = msr_no;
  56. rv.reg.l = l;
  57. rv.reg.h = h;
  58. err = smp_call_function_single(cpu, __wrmsr_on_cpu, &rv, 1);
  59. return err;
  60. }
  61. EXPORT_SYMBOL(wrmsr_on_cpu);
  62. int wrmsrl_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
  63. {
  64. int err;
  65. struct msr_info rv;
  66. memset(&rv, 0, sizeof(rv));
  67. rv.msr_no = msr_no;
  68. rv.reg.q = q;
  69. err = smp_call_function_single(cpu, __wrmsr_on_cpu, &rv, 1);
  70. return err;
  71. }
  72. EXPORT_SYMBOL(wrmsrl_on_cpu);
  73. static void __rwmsr_on_cpus(const struct cpumask *mask, u32 msr_no,
  74. struct msr *msrs,
  75. void (*msr_func) (void *info))
  76. {
  77. struct msr_info rv;
  78. int this_cpu;
  79. memset(&rv, 0, sizeof(rv));
  80. rv.msrs = msrs;
  81. rv.msr_no = msr_no;
  82. this_cpu = get_cpu();
  83. if (cpumask_test_cpu(this_cpu, mask))
  84. msr_func(&rv);
  85. smp_call_function_many(mask, msr_func, &rv, 1);
  86. put_cpu();
  87. }
  88. /* rdmsr on a bunch of CPUs
  89. *
  90. * @mask: which CPUs
  91. * @msr_no: which MSR
  92. * @msrs: array of MSR values
  93. *
  94. */
  95. void rdmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs)
  96. {
  97. __rwmsr_on_cpus(mask, msr_no, msrs, __rdmsr_on_cpu);
  98. }
  99. EXPORT_SYMBOL(rdmsr_on_cpus);
  100. /*
  101. * wrmsr on a bunch of CPUs
  102. *
  103. * @mask: which CPUs
  104. * @msr_no: which MSR
  105. * @msrs: array of MSR values
  106. *
  107. */
  108. void wrmsr_on_cpus(const struct cpumask *mask, u32 msr_no, struct msr *msrs)
  109. {
  110. __rwmsr_on_cpus(mask, msr_no, msrs, __wrmsr_on_cpu);
  111. }
  112. EXPORT_SYMBOL(wrmsr_on_cpus);
  113. /* These "safe" variants are slower and should be used when the target MSR
  114. may not actually exist. */
  115. static void __rdmsr_safe_on_cpu(void *info)
  116. {
  117. struct msr_info *rv = info;
  118. rv->err = rdmsr_safe(rv->msr_no, &rv->reg.l, &rv->reg.h);
  119. }
  120. static void __wrmsr_safe_on_cpu(void *info)
  121. {
  122. struct msr_info *rv = info;
  123. rv->err = wrmsr_safe(rv->msr_no, rv->reg.l, rv->reg.h);
  124. }
  125. int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  126. {
  127. int err;
  128. struct msr_info rv;
  129. memset(&rv, 0, sizeof(rv));
  130. rv.msr_no = msr_no;
  131. err = smp_call_function_single(cpu, __rdmsr_safe_on_cpu, &rv, 1);
  132. *l = rv.reg.l;
  133. *h = rv.reg.h;
  134. return err ? err : rv.err;
  135. }
  136. EXPORT_SYMBOL(rdmsr_safe_on_cpu);
  137. int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  138. {
  139. int err;
  140. struct msr_info rv;
  141. memset(&rv, 0, sizeof(rv));
  142. rv.msr_no = msr_no;
  143. rv.reg.l = l;
  144. rv.reg.h = h;
  145. err = smp_call_function_single(cpu, __wrmsr_safe_on_cpu, &rv, 1);
  146. return err ? err : rv.err;
  147. }
  148. EXPORT_SYMBOL(wrmsr_safe_on_cpu);
  149. int wrmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 q)
  150. {
  151. int err;
  152. struct msr_info rv;
  153. memset(&rv, 0, sizeof(rv));
  154. rv.msr_no = msr_no;
  155. rv.reg.q = q;
  156. err = smp_call_function_single(cpu, __wrmsr_safe_on_cpu, &rv, 1);
  157. return err ? err : rv.err;
  158. }
  159. EXPORT_SYMBOL(wrmsrl_safe_on_cpu);
  160. int rdmsrl_safe_on_cpu(unsigned int cpu, u32 msr_no, u64 *q)
  161. {
  162. int err;
  163. struct msr_info rv;
  164. memset(&rv, 0, sizeof(rv));
  165. rv.msr_no = msr_no;
  166. err = smp_call_function_single(cpu, __rdmsr_safe_on_cpu, &rv, 1);
  167. *q = rv.reg.q;
  168. return err ? err : rv.err;
  169. }
  170. EXPORT_SYMBOL(rdmsrl_safe_on_cpu);
  171. /*
  172. * These variants are significantly slower, but allows control over
  173. * the entire 32-bit GPR set.
  174. */
  175. static void __rdmsr_safe_regs_on_cpu(void *info)
  176. {
  177. struct msr_regs_info *rv = info;
  178. rv->err = rdmsr_safe_regs(rv->regs);
  179. }
  180. static void __wrmsr_safe_regs_on_cpu(void *info)
  181. {
  182. struct msr_regs_info *rv = info;
  183. rv->err = wrmsr_safe_regs(rv->regs);
  184. }
  185. int rdmsr_safe_regs_on_cpu(unsigned int cpu, u32 *regs)
  186. {
  187. int err;
  188. struct msr_regs_info rv;
  189. rv.regs = regs;
  190. rv.err = -EIO;
  191. err = smp_call_function_single(cpu, __rdmsr_safe_regs_on_cpu, &rv, 1);
  192. return err ? err : rv.err;
  193. }
  194. EXPORT_SYMBOL(rdmsr_safe_regs_on_cpu);
  195. int wrmsr_safe_regs_on_cpu(unsigned int cpu, u32 *regs)
  196. {
  197. int err;
  198. struct msr_regs_info rv;
  199. rv.regs = regs;
  200. rv.err = -EIO;
  201. err = smp_call_function_single(cpu, __wrmsr_safe_regs_on_cpu, &rv, 1);
  202. return err ? err : rv.err;
  203. }
  204. EXPORT_SYMBOL(wrmsr_safe_regs_on_cpu);