msr.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include <linux/module.h>
  2. #include <linux/preempt.h>
  3. #include <linux/smp.h>
  4. #include <asm/msr.h>
  5. struct msr_info {
  6. u32 msr_no;
  7. struct msr reg;
  8. struct msr *msrs;
  9. int off;
  10. int err;
  11. };
  12. static void __rdmsr_on_cpu(void *info)
  13. {
  14. struct msr_info *rv = info;
  15. struct msr *reg;
  16. int this_cpu = raw_smp_processor_id();
  17. if (rv->msrs)
  18. reg = &rv->msrs[this_cpu - rv->off];
  19. else
  20. reg = &rv->reg;
  21. rdmsr(rv->msr_no, reg->l, reg->h);
  22. }
  23. static void __wrmsr_on_cpu(void *info)
  24. {
  25. struct msr_info *rv = info;
  26. struct msr *reg;
  27. int this_cpu = raw_smp_processor_id();
  28. if (rv->msrs)
  29. reg = &rv->msrs[this_cpu - rv->off];
  30. else
  31. reg = &rv->reg;
  32. wrmsr(rv->msr_no, reg->l, reg->h);
  33. }
  34. int rdmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  35. {
  36. int err;
  37. struct msr_info rv;
  38. memset(&rv, 0, sizeof(rv));
  39. rv.msr_no = msr_no;
  40. err = smp_call_function_single(cpu, __rdmsr_on_cpu, &rv, 1);
  41. *l = rv.reg.l;
  42. *h = rv.reg.h;
  43. return err;
  44. }
  45. EXPORT_SYMBOL(rdmsr_on_cpu);
  46. int wrmsr_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  47. {
  48. int err;
  49. struct msr_info rv;
  50. memset(&rv, 0, sizeof(rv));
  51. rv.msr_no = msr_no;
  52. rv.reg.l = l;
  53. rv.reg.h = h;
  54. err = smp_call_function_single(cpu, __wrmsr_on_cpu, &rv, 1);
  55. return err;
  56. }
  57. EXPORT_SYMBOL(wrmsr_on_cpu);
  58. /* rdmsr on a bunch of CPUs
  59. *
  60. * @mask: which CPUs
  61. * @msr_no: which MSR
  62. * @msrs: array of MSR values
  63. *
  64. */
  65. void rdmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs)
  66. {
  67. struct msr_info rv;
  68. int this_cpu;
  69. memset(&rv, 0, sizeof(rv));
  70. rv.off = cpumask_first(mask);
  71. rv.msrs = msrs;
  72. rv.msr_no = msr_no;
  73. preempt_disable();
  74. /*
  75. * FIXME: handle the CPU we're executing on separately for now until
  76. * smp_call_function_many has been fixed to not skip it.
  77. */
  78. this_cpu = raw_smp_processor_id();
  79. smp_call_function_single(this_cpu, __rdmsr_on_cpu, &rv, 1);
  80. smp_call_function_many(mask, __rdmsr_on_cpu, &rv, 1);
  81. preempt_enable();
  82. }
  83. EXPORT_SYMBOL(rdmsr_on_cpus);
  84. /*
  85. * wrmsr on a bunch of CPUs
  86. *
  87. * @mask: which CPUs
  88. * @msr_no: which MSR
  89. * @msrs: array of MSR values
  90. *
  91. */
  92. void wrmsr_on_cpus(const cpumask_t *mask, u32 msr_no, struct msr *msrs)
  93. {
  94. struct msr_info rv;
  95. int this_cpu;
  96. memset(&rv, 0, sizeof(rv));
  97. rv.off = cpumask_first(mask);
  98. rv.msrs = msrs;
  99. rv.msr_no = msr_no;
  100. preempt_disable();
  101. /*
  102. * FIXME: handle the CPU we're executing on separately for now until
  103. * smp_call_function_many has been fixed to not skip it.
  104. */
  105. this_cpu = raw_smp_processor_id();
  106. smp_call_function_single(this_cpu, __wrmsr_on_cpu, &rv, 1);
  107. smp_call_function_many(mask, __wrmsr_on_cpu, &rv, 1);
  108. preempt_enable();
  109. }
  110. EXPORT_SYMBOL(wrmsr_on_cpus);
  111. /* These "safe" variants are slower and should be used when the target MSR
  112. may not actually exist. */
  113. static void __rdmsr_safe_on_cpu(void *info)
  114. {
  115. struct msr_info *rv = info;
  116. rv->err = rdmsr_safe(rv->msr_no, &rv->reg.l, &rv->reg.h);
  117. }
  118. static void __wrmsr_safe_on_cpu(void *info)
  119. {
  120. struct msr_info *rv = info;
  121. rv->err = wrmsr_safe(rv->msr_no, rv->reg.l, rv->reg.h);
  122. }
  123. int rdmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 *l, u32 *h)
  124. {
  125. int err;
  126. struct msr_info rv;
  127. memset(&rv, 0, sizeof(rv));
  128. rv.msr_no = msr_no;
  129. err = smp_call_function_single(cpu, __rdmsr_safe_on_cpu, &rv, 1);
  130. *l = rv.reg.l;
  131. *h = rv.reg.h;
  132. return err ? err : rv.err;
  133. }
  134. EXPORT_SYMBOL(rdmsr_safe_on_cpu);
  135. int wrmsr_safe_on_cpu(unsigned int cpu, u32 msr_no, u32 l, u32 h)
  136. {
  137. int err;
  138. struct msr_info rv;
  139. memset(&rv, 0, sizeof(rv));
  140. rv.msr_no = msr_no;
  141. rv.reg.l = l;
  142. rv.reg.h = h;
  143. err = smp_call_function_single(cpu, __wrmsr_safe_on_cpu, &rv, 1);
  144. return err ? err : rv.err;
  145. }
  146. EXPORT_SYMBOL(wrmsr_safe_on_cpu);