msr.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <linux/export.h>
  2. #include <linux/percpu.h>
  3. #include <linux/preempt.h>
  4. #include <asm/msr.h>
  5. #define CREATE_TRACE_POINTS
  6. #include <asm/msr-trace.h>
  7. struct msr *msrs_alloc(void)
  8. {
  9. struct msr *msrs = NULL;
  10. msrs = alloc_percpu(struct msr);
  11. if (!msrs) {
  12. pr_warn("%s: error allocating msrs\n", __func__);
  13. return NULL;
  14. }
  15. return msrs;
  16. }
  17. EXPORT_SYMBOL(msrs_alloc);
  18. void msrs_free(struct msr *msrs)
  19. {
  20. free_percpu(msrs);
  21. }
  22. EXPORT_SYMBOL(msrs_free);
  23. /**
  24. * Read an MSR with error handling
  25. *
  26. * @msr: MSR to read
  27. * @m: value to read into
  28. *
  29. * It returns read data only on success, otherwise it doesn't change the output
  30. * argument @m.
  31. *
  32. */
  33. int msr_read(u32 msr, struct msr *m)
  34. {
  35. int err;
  36. u64 val;
  37. err = rdmsrl_safe(msr, &val);
  38. if (!err)
  39. m->q = val;
  40. return err;
  41. }
  42. /**
  43. * Write an MSR with error handling
  44. *
  45. * @msr: MSR to write
  46. * @m: value to write
  47. */
  48. int msr_write(u32 msr, struct msr *m)
  49. {
  50. return wrmsrl_safe(msr, m->q);
  51. }
  52. static inline int __flip_bit(u32 msr, u8 bit, bool set)
  53. {
  54. struct msr m, m1;
  55. int err = -EINVAL;
  56. if (bit > 63)
  57. return err;
  58. err = msr_read(msr, &m);
  59. if (err)
  60. return err;
  61. m1 = m;
  62. if (set)
  63. m1.q |= BIT_64(bit);
  64. else
  65. m1.q &= ~BIT_64(bit);
  66. if (m1.q == m.q)
  67. return 0;
  68. err = msr_write(msr, &m1);
  69. if (err)
  70. return err;
  71. return 1;
  72. }
  73. /**
  74. * Set @bit in a MSR @msr.
  75. *
  76. * Retval:
  77. * < 0: An error was encountered.
  78. * = 0: Bit was already set.
  79. * > 0: Hardware accepted the MSR write.
  80. */
  81. int msr_set_bit(u32 msr, u8 bit)
  82. {
  83. return __flip_bit(msr, bit, true);
  84. }
  85. /**
  86. * Clear @bit in a MSR @msr.
  87. *
  88. * Retval:
  89. * < 0: An error was encountered.
  90. * = 0: Bit was already cleared.
  91. * > 0: Hardware accepted the MSR write.
  92. */
  93. int msr_clear_bit(u32 msr, u8 bit)
  94. {
  95. return __flip_bit(msr, bit, false);
  96. }
  97. #ifdef CONFIG_TRACEPOINTS
  98. void do_trace_write_msr(unsigned msr, u64 val, int failed)
  99. {
  100. trace_write_msr(msr, val, failed);
  101. }
  102. EXPORT_SYMBOL(do_trace_write_msr);
  103. EXPORT_TRACEPOINT_SYMBOL(write_msr);
  104. void do_trace_read_msr(unsigned msr, u64 val, int failed)
  105. {
  106. trace_read_msr(msr, val, failed);
  107. }
  108. EXPORT_SYMBOL(do_trace_read_msr);
  109. EXPORT_TRACEPOINT_SYMBOL(read_msr);
  110. void do_trace_rdpmc(unsigned counter, u64 val, int failed)
  111. {
  112. trace_rdpmc(counter, val, failed);
  113. }
  114. EXPORT_SYMBOL(do_trace_rdpmc);
  115. EXPORT_TRACEPOINT_SYMBOL(rdpmc);
  116. #endif