arm_pmu.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2015 Linaro Ltd.
  4. * Author: Shannon Zhao <shannon.zhao@linaro.org>
  5. */
  6. #ifndef __ASM_ARM_KVM_PMU_H
  7. #define __ASM_ARM_KVM_PMU_H
  8. #include <linux/perf_event.h>
  9. #include <linux/perf/arm_pmuv3.h>
  10. #define KVM_ARMV8_PMU_MAX_COUNTERS 32
  11. #if IS_ENABLED(CONFIG_HW_PERF_EVENTS) && IS_ENABLED(CONFIG_KVM)
  12. struct kvm_pmc {
  13. u8 idx; /* index into the pmu->pmc array */
  14. struct perf_event *perf_event;
  15. };
  16. struct kvm_pmu_events {
  17. u64 events_host;
  18. u64 events_guest;
  19. };
  20. struct kvm_pmu {
  21. struct irq_work overflow_work;
  22. struct kvm_pmu_events events;
  23. struct kvm_pmc pmc[KVM_ARMV8_PMU_MAX_COUNTERS];
  24. int irq_num;
  25. bool created;
  26. bool irq_level;
  27. };
  28. struct arm_pmu_entry {
  29. struct list_head entry;
  30. struct arm_pmu *arm_pmu;
  31. };
  32. bool kvm_supports_guest_pmuv3(void);
  33. #define kvm_arm_pmu_irq_initialized(v) ((v)->arch.pmu.irq_num >= VGIC_NR_SGIS)
  34. u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx);
  35. void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val);
  36. void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu, u64 select_idx, u64 val);
  37. u64 kvm_pmu_implemented_counter_mask(struct kvm_vcpu *vcpu);
  38. u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu);
  39. u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1);
  40. void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu);
  41. void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu);
  42. void kvm_pmu_reprogram_counter_mask(struct kvm_vcpu *vcpu, u64 val);
  43. void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu);
  44. void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu);
  45. bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu);
  46. void kvm_pmu_update_run(struct kvm_vcpu *vcpu);
  47. void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val);
  48. void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val);
  49. void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
  50. u64 select_idx);
  51. void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu);
  52. int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu,
  53. struct kvm_device_attr *attr);
  54. int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu,
  55. struct kvm_device_attr *attr);
  56. int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu,
  57. struct kvm_device_attr *attr);
  58. int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu);
  59. struct kvm_pmu_events *kvm_get_pmu_events(void);
  60. void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu);
  61. void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
  62. void kvm_vcpu_pmu_resync_el0(void);
  63. #define kvm_vcpu_has_pmu(vcpu) \
  64. (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3))
  65. /*
  66. * Updates the vcpu's view of the pmu events for this cpu.
  67. * Must be called before every vcpu run after disabling interrupts, to ensure
  68. * that an interrupt cannot fire and update the structure.
  69. */
  70. #define kvm_pmu_update_vcpu_events(vcpu) \
  71. do { \
  72. if (!has_vhe() && system_supports_pmuv3()) \
  73. vcpu->arch.pmu.events = *kvm_get_pmu_events(); \
  74. } while (0)
  75. u8 kvm_arm_pmu_get_pmuver_limit(void);
  76. u64 kvm_pmu_evtyper_mask(struct kvm *kvm);
  77. int kvm_arm_set_default_pmu(struct kvm *kvm);
  78. u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm);
  79. u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu);
  80. bool kvm_pmu_counter_is_hyp(struct kvm_vcpu *vcpu, unsigned int idx);
  81. void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu);
  82. #else
  83. struct kvm_pmu {
  84. };
  85. static inline bool kvm_supports_guest_pmuv3(void)
  86. {
  87. return false;
  88. }
  89. #define kvm_arm_pmu_irq_initialized(v) (false)
  90. static inline u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu,
  91. u64 select_idx)
  92. {
  93. return 0;
  94. }
  95. static inline void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu,
  96. u64 select_idx, u64 val) {}
  97. static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu,
  98. u64 select_idx, u64 val) {}
  99. static inline u64 kvm_pmu_implemented_counter_mask(struct kvm_vcpu *vcpu)
  100. {
  101. return 0;
  102. }
  103. static inline u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu)
  104. {
  105. return 0;
  106. }
  107. static inline void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu) {}
  108. static inline void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu) {}
  109. static inline void kvm_pmu_reprogram_counter_mask(struct kvm_vcpu *vcpu, u64 val) {}
  110. static inline void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu) {}
  111. static inline void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu) {}
  112. static inline bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
  113. {
  114. return false;
  115. }
  116. static inline void kvm_pmu_update_run(struct kvm_vcpu *vcpu) {}
  117. static inline void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val) {}
  118. static inline void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val) {}
  119. static inline void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu,
  120. u64 data, u64 select_idx) {}
  121. static inline int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu,
  122. struct kvm_device_attr *attr)
  123. {
  124. return -ENXIO;
  125. }
  126. static inline int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu,
  127. struct kvm_device_attr *attr)
  128. {
  129. return -ENXIO;
  130. }
  131. static inline int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu,
  132. struct kvm_device_attr *attr)
  133. {
  134. return -ENXIO;
  135. }
  136. static inline int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)
  137. {
  138. return 0;
  139. }
  140. static inline u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
  141. {
  142. return 0;
  143. }
  144. #define kvm_vcpu_has_pmu(vcpu) ({ false; })
  145. static inline void kvm_pmu_update_vcpu_events(struct kvm_vcpu *vcpu) {}
  146. static inline void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu) {}
  147. static inline void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu) {}
  148. static inline void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu) {}
  149. static inline u8 kvm_arm_pmu_get_pmuver_limit(void)
  150. {
  151. return 0;
  152. }
  153. static inline u64 kvm_pmu_evtyper_mask(struct kvm *kvm)
  154. {
  155. return 0;
  156. }
  157. static inline void kvm_vcpu_pmu_resync_el0(void) {}
  158. static inline int kvm_arm_set_default_pmu(struct kvm *kvm)
  159. {
  160. return -ENODEV;
  161. }
  162. static inline u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm)
  163. {
  164. return 0;
  165. }
  166. static inline u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)
  167. {
  168. return 0;
  169. }
  170. static inline bool kvm_pmu_counter_is_hyp(struct kvm_vcpu *vcpu, unsigned int idx)
  171. {
  172. return false;
  173. }
  174. static inline void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu) {}
  175. #endif
  176. #endif