pmu_amd.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * KVM PMU support for AMD
  3. *
  4. * Copyright 2015, Red Hat, Inc. and/or its affiliates.
  5. *
  6. * Author:
  7. * Wei Huang <wei@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Implementation is based on pmu_intel.c file
  13. */
  14. #include <linux/types.h>
  15. #include <linux/kvm_host.h>
  16. #include <linux/perf_event.h>
  17. #include "x86.h"
  18. #include "cpuid.h"
  19. #include "lapic.h"
  20. #include "pmu.h"
  21. /* duplicated from amd_perfmon_event_map, K7 and above should work. */
  22. static struct kvm_event_hw_type_mapping amd_event_mapping[] = {
  23. [0] = { 0x76, 0x00, PERF_COUNT_HW_CPU_CYCLES },
  24. [1] = { 0xc0, 0x00, PERF_COUNT_HW_INSTRUCTIONS },
  25. [2] = { 0x80, 0x00, PERF_COUNT_HW_CACHE_REFERENCES },
  26. [3] = { 0x81, 0x00, PERF_COUNT_HW_CACHE_MISSES },
  27. [4] = { 0xc2, 0x00, PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
  28. [5] = { 0xc3, 0x00, PERF_COUNT_HW_BRANCH_MISSES },
  29. [6] = { 0xd0, 0x00, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
  30. [7] = { 0xd1, 0x00, PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
  31. };
  32. static unsigned amd_find_arch_event(struct kvm_pmu *pmu,
  33. u8 event_select,
  34. u8 unit_mask)
  35. {
  36. int i;
  37. for (i = 0; i < ARRAY_SIZE(amd_event_mapping); i++)
  38. if (amd_event_mapping[i].eventsel == event_select
  39. && amd_event_mapping[i].unit_mask == unit_mask)
  40. break;
  41. if (i == ARRAY_SIZE(amd_event_mapping))
  42. return PERF_COUNT_HW_MAX;
  43. return amd_event_mapping[i].event_type;
  44. }
  45. /* return PERF_COUNT_HW_MAX as AMD doesn't have fixed events */
  46. static unsigned amd_find_fixed_event(int idx)
  47. {
  48. return PERF_COUNT_HW_MAX;
  49. }
  50. /* check if a PMC is enabled by comparing it against global_ctrl bits. Because
  51. * AMD CPU doesn't have global_ctrl MSR, all PMCs are enabled (return TRUE).
  52. */
  53. static bool amd_pmc_is_enabled(struct kvm_pmc *pmc)
  54. {
  55. return true;
  56. }
  57. static struct kvm_pmc *amd_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx)
  58. {
  59. return get_gp_pmc(pmu, MSR_K7_EVNTSEL0 + pmc_idx, MSR_K7_EVNTSEL0);
  60. }
  61. /* returns 0 if idx's corresponding MSR exists; otherwise returns 1. */
  62. static int amd_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned idx)
  63. {
  64. struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
  65. idx &= ~(3u << 30);
  66. return (idx >= pmu->nr_arch_gp_counters);
  67. }
  68. /* idx is the ECX register of RDPMC instruction */
  69. static struct kvm_pmc *amd_msr_idx_to_pmc(struct kvm_vcpu *vcpu, unsigned idx)
  70. {
  71. struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
  72. struct kvm_pmc *counters;
  73. idx &= ~(3u << 30);
  74. if (idx >= pmu->nr_arch_gp_counters)
  75. return NULL;
  76. counters = pmu->gp_counters;
  77. return &counters[idx];
  78. }
  79. static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
  80. {
  81. struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
  82. int ret = false;
  83. ret = get_gp_pmc(pmu, msr, MSR_K7_PERFCTR0) ||
  84. get_gp_pmc(pmu, msr, MSR_K7_EVNTSEL0);
  85. return ret;
  86. }
  87. static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
  88. {
  89. struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
  90. struct kvm_pmc *pmc;
  91. /* MSR_K7_PERFCTRn */
  92. pmc = get_gp_pmc(pmu, msr, MSR_K7_PERFCTR0);
  93. if (pmc) {
  94. *data = pmc_read_counter(pmc);
  95. return 0;
  96. }
  97. /* MSR_K7_EVNTSELn */
  98. pmc = get_gp_pmc(pmu, msr, MSR_K7_EVNTSEL0);
  99. if (pmc) {
  100. *data = pmc->eventsel;
  101. return 0;
  102. }
  103. return 1;
  104. }
  105. static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  106. {
  107. struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
  108. struct kvm_pmc *pmc;
  109. u32 msr = msr_info->index;
  110. u64 data = msr_info->data;
  111. /* MSR_K7_PERFCTRn */
  112. pmc = get_gp_pmc(pmu, msr, MSR_K7_PERFCTR0);
  113. if (pmc) {
  114. pmc->counter += data - pmc_read_counter(pmc);
  115. return 0;
  116. }
  117. /* MSR_K7_EVNTSELn */
  118. pmc = get_gp_pmc(pmu, msr, MSR_K7_EVNTSEL0);
  119. if (pmc) {
  120. if (data == pmc->eventsel)
  121. return 0;
  122. if (!(data & pmu->reserved_bits)) {
  123. reprogram_gp_counter(pmc, data);
  124. return 0;
  125. }
  126. }
  127. return 1;
  128. }
  129. static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
  130. {
  131. struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
  132. pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS;
  133. pmu->counter_bitmask[KVM_PMC_GP] = ((u64)1 << 48) - 1;
  134. pmu->reserved_bits = 0xffffffff00200000ull;
  135. /* not applicable to AMD; but clean them to prevent any fall out */
  136. pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
  137. pmu->nr_arch_fixed_counters = 0;
  138. pmu->version = 0;
  139. pmu->global_status = 0;
  140. }
  141. static void amd_pmu_init(struct kvm_vcpu *vcpu)
  142. {
  143. struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
  144. int i;
  145. for (i = 0; i < AMD64_NUM_COUNTERS ; i++) {
  146. pmu->gp_counters[i].type = KVM_PMC_GP;
  147. pmu->gp_counters[i].vcpu = vcpu;
  148. pmu->gp_counters[i].idx = i;
  149. }
  150. }
  151. static void amd_pmu_reset(struct kvm_vcpu *vcpu)
  152. {
  153. struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
  154. int i;
  155. for (i = 0; i < AMD64_NUM_COUNTERS; i++) {
  156. struct kvm_pmc *pmc = &pmu->gp_counters[i];
  157. pmc_stop_counter(pmc);
  158. pmc->counter = pmc->eventsel = 0;
  159. }
  160. }
  161. struct kvm_pmu_ops amd_pmu_ops = {
  162. .find_arch_event = amd_find_arch_event,
  163. .find_fixed_event = amd_find_fixed_event,
  164. .pmc_is_enabled = amd_pmc_is_enabled,
  165. .pmc_idx_to_pmc = amd_pmc_idx_to_pmc,
  166. .msr_idx_to_pmc = amd_msr_idx_to_pmc,
  167. .is_valid_msr_idx = amd_is_valid_msr_idx,
  168. .is_valid_msr = amd_is_valid_msr,
  169. .get_msr = amd_pmu_get_msr,
  170. .set_msr = amd_pmu_set_msr,
  171. .refresh = amd_pmu_refresh,
  172. .init = amd_pmu_init,
  173. .reset = amd_pmu_reset,
  174. };