msr.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/perf_event.h>
  3. #include <linux/nospec.h>
  4. #include <asm/intel-family.h>
  5. enum perf_msr_id {
  6. PERF_MSR_TSC = 0,
  7. PERF_MSR_APERF = 1,
  8. PERF_MSR_MPERF = 2,
  9. PERF_MSR_PPERF = 3,
  10. PERF_MSR_SMI = 4,
  11. PERF_MSR_PTSC = 5,
  12. PERF_MSR_IRPERF = 6,
  13. PERF_MSR_THERM = 7,
  14. PERF_MSR_THERM_SNAP = 8,
  15. PERF_MSR_THERM_UNIT = 9,
  16. PERF_MSR_EVENT_MAX,
  17. };
  18. static bool test_aperfmperf(int idx)
  19. {
  20. return boot_cpu_has(X86_FEATURE_APERFMPERF);
  21. }
  22. static bool test_ptsc(int idx)
  23. {
  24. return boot_cpu_has(X86_FEATURE_PTSC);
  25. }
  26. static bool test_irperf(int idx)
  27. {
  28. return boot_cpu_has(X86_FEATURE_IRPERF);
  29. }
  30. static bool test_therm_status(int idx)
  31. {
  32. return boot_cpu_has(X86_FEATURE_DTHERM);
  33. }
  34. static bool test_intel(int idx)
  35. {
  36. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
  37. boot_cpu_data.x86 != 6)
  38. return false;
  39. switch (boot_cpu_data.x86_model) {
  40. case INTEL_FAM6_NEHALEM:
  41. case INTEL_FAM6_NEHALEM_G:
  42. case INTEL_FAM6_NEHALEM_EP:
  43. case INTEL_FAM6_NEHALEM_EX:
  44. case INTEL_FAM6_WESTMERE:
  45. case INTEL_FAM6_WESTMERE_EP:
  46. case INTEL_FAM6_WESTMERE_EX:
  47. case INTEL_FAM6_SANDYBRIDGE:
  48. case INTEL_FAM6_SANDYBRIDGE_X:
  49. case INTEL_FAM6_IVYBRIDGE:
  50. case INTEL_FAM6_IVYBRIDGE_X:
  51. case INTEL_FAM6_HASWELL_CORE:
  52. case INTEL_FAM6_HASWELL_X:
  53. case INTEL_FAM6_HASWELL_ULT:
  54. case INTEL_FAM6_HASWELL_GT3E:
  55. case INTEL_FAM6_BROADWELL_CORE:
  56. case INTEL_FAM6_BROADWELL_XEON_D:
  57. case INTEL_FAM6_BROADWELL_GT3E:
  58. case INTEL_FAM6_BROADWELL_X:
  59. case INTEL_FAM6_ATOM_SILVERMONT:
  60. case INTEL_FAM6_ATOM_SILVERMONT_X:
  61. case INTEL_FAM6_ATOM_AIRMONT:
  62. case INTEL_FAM6_ATOM_GOLDMONT:
  63. case INTEL_FAM6_ATOM_GOLDMONT_X:
  64. case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
  65. case INTEL_FAM6_XEON_PHI_KNL:
  66. case INTEL_FAM6_XEON_PHI_KNM:
  67. if (idx == PERF_MSR_SMI)
  68. return true;
  69. break;
  70. case INTEL_FAM6_SKYLAKE_MOBILE:
  71. case INTEL_FAM6_SKYLAKE_DESKTOP:
  72. case INTEL_FAM6_SKYLAKE_X:
  73. case INTEL_FAM6_KABYLAKE_MOBILE:
  74. case INTEL_FAM6_KABYLAKE_DESKTOP:
  75. if (idx == PERF_MSR_SMI || idx == PERF_MSR_PPERF)
  76. return true;
  77. break;
  78. }
  79. return false;
  80. }
  81. struct perf_msr {
  82. u64 msr;
  83. struct perf_pmu_events_attr *attr;
  84. bool (*test)(int idx);
  85. };
  86. PMU_EVENT_ATTR_STRING(tsc, evattr_tsc, "event=0x00" );
  87. PMU_EVENT_ATTR_STRING(aperf, evattr_aperf, "event=0x01" );
  88. PMU_EVENT_ATTR_STRING(mperf, evattr_mperf, "event=0x02" );
  89. PMU_EVENT_ATTR_STRING(pperf, evattr_pperf, "event=0x03" );
  90. PMU_EVENT_ATTR_STRING(smi, evattr_smi, "event=0x04" );
  91. PMU_EVENT_ATTR_STRING(ptsc, evattr_ptsc, "event=0x05" );
  92. PMU_EVENT_ATTR_STRING(irperf, evattr_irperf, "event=0x06" );
  93. PMU_EVENT_ATTR_STRING(cpu_thermal_margin, evattr_therm, "event=0x07" );
  94. PMU_EVENT_ATTR_STRING(cpu_thermal_margin.snapshot, evattr_therm_snap, "1" );
  95. PMU_EVENT_ATTR_STRING(cpu_thermal_margin.unit, evattr_therm_unit, "C" );
  96. static struct perf_msr msr[] = {
  97. [PERF_MSR_TSC] = { 0, &evattr_tsc, NULL, },
  98. [PERF_MSR_APERF] = { MSR_IA32_APERF, &evattr_aperf, test_aperfmperf, },
  99. [PERF_MSR_MPERF] = { MSR_IA32_MPERF, &evattr_mperf, test_aperfmperf, },
  100. [PERF_MSR_PPERF] = { MSR_PPERF, &evattr_pperf, test_intel, },
  101. [PERF_MSR_SMI] = { MSR_SMI_COUNT, &evattr_smi, test_intel, },
  102. [PERF_MSR_PTSC] = { MSR_F15H_PTSC, &evattr_ptsc, test_ptsc, },
  103. [PERF_MSR_IRPERF] = { MSR_F17H_IRPERF, &evattr_irperf, test_irperf, },
  104. [PERF_MSR_THERM] = { MSR_IA32_THERM_STATUS, &evattr_therm, test_therm_status, },
  105. [PERF_MSR_THERM_SNAP] = { MSR_IA32_THERM_STATUS, &evattr_therm_snap, test_therm_status, },
  106. [PERF_MSR_THERM_UNIT] = { MSR_IA32_THERM_STATUS, &evattr_therm_unit, test_therm_status, },
  107. };
  108. static struct attribute *events_attrs[PERF_MSR_EVENT_MAX + 1] = {
  109. NULL,
  110. };
  111. static struct attribute_group events_attr_group = {
  112. .name = "events",
  113. .attrs = events_attrs,
  114. };
  115. PMU_FORMAT_ATTR(event, "config:0-63");
  116. static struct attribute *format_attrs[] = {
  117. &format_attr_event.attr,
  118. NULL,
  119. };
  120. static struct attribute_group format_attr_group = {
  121. .name = "format",
  122. .attrs = format_attrs,
  123. };
  124. static const struct attribute_group *attr_groups[] = {
  125. &events_attr_group,
  126. &format_attr_group,
  127. NULL,
  128. };
  129. static int msr_event_init(struct perf_event *event)
  130. {
  131. u64 cfg = event->attr.config;
  132. if (event->attr.type != event->pmu->type)
  133. return -ENOENT;
  134. /* unsupported modes and filters */
  135. if (event->attr.exclude_user ||
  136. event->attr.exclude_kernel ||
  137. event->attr.exclude_hv ||
  138. event->attr.exclude_idle ||
  139. event->attr.exclude_host ||
  140. event->attr.exclude_guest ||
  141. event->attr.sample_period) /* no sampling */
  142. return -EINVAL;
  143. if (cfg >= PERF_MSR_EVENT_MAX)
  144. return -EINVAL;
  145. cfg = array_index_nospec((unsigned long)cfg, PERF_MSR_EVENT_MAX);
  146. if (!msr[cfg].attr)
  147. return -EINVAL;
  148. event->hw.idx = -1;
  149. event->hw.event_base = msr[cfg].msr;
  150. event->hw.config = cfg;
  151. return 0;
  152. }
  153. static inline u64 msr_read_counter(struct perf_event *event)
  154. {
  155. u64 now;
  156. if (event->hw.event_base)
  157. rdmsrl(event->hw.event_base, now);
  158. else
  159. now = rdtsc_ordered();
  160. return now;
  161. }
  162. static void msr_event_update(struct perf_event *event)
  163. {
  164. u64 prev, now;
  165. s64 delta;
  166. /* Careful, an NMI might modify the previous event value: */
  167. again:
  168. prev = local64_read(&event->hw.prev_count);
  169. now = msr_read_counter(event);
  170. if (local64_cmpxchg(&event->hw.prev_count, prev, now) != prev)
  171. goto again;
  172. delta = now - prev;
  173. if (unlikely(event->hw.event_base == MSR_SMI_COUNT)) {
  174. delta = sign_extend64(delta, 31);
  175. local64_add(delta, &event->count);
  176. } else if (unlikely(event->hw.event_base == MSR_IA32_THERM_STATUS)) {
  177. /* If valid, extract digital readout, otherwise set to -1: */
  178. now = now & (1ULL << 31) ? (now >> 16) & 0x3f : -1;
  179. local64_set(&event->count, now);
  180. } else {
  181. local64_add(delta, &event->count);
  182. }
  183. }
  184. static void msr_event_start(struct perf_event *event, int flags)
  185. {
  186. u64 now = msr_read_counter(event);
  187. local64_set(&event->hw.prev_count, now);
  188. }
  189. static void msr_event_stop(struct perf_event *event, int flags)
  190. {
  191. msr_event_update(event);
  192. }
  193. static void msr_event_del(struct perf_event *event, int flags)
  194. {
  195. msr_event_stop(event, PERF_EF_UPDATE);
  196. }
  197. static int msr_event_add(struct perf_event *event, int flags)
  198. {
  199. if (flags & PERF_EF_START)
  200. msr_event_start(event, flags);
  201. return 0;
  202. }
  203. static struct pmu pmu_msr = {
  204. .task_ctx_nr = perf_sw_context,
  205. .attr_groups = attr_groups,
  206. .event_init = msr_event_init,
  207. .add = msr_event_add,
  208. .del = msr_event_del,
  209. .start = msr_event_start,
  210. .stop = msr_event_stop,
  211. .read = msr_event_update,
  212. .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
  213. };
  214. static int __init msr_init(void)
  215. {
  216. int i, j = 0;
  217. if (!boot_cpu_has(X86_FEATURE_TSC)) {
  218. pr_cont("no MSR PMU driver.\n");
  219. return 0;
  220. }
  221. /* Probe the MSRs. */
  222. for (i = PERF_MSR_TSC + 1; i < PERF_MSR_EVENT_MAX; i++) {
  223. u64 val;
  224. /* Virt sucks; you cannot tell if a R/O MSR is present :/ */
  225. if (!msr[i].test(i) || rdmsrl_safe(msr[i].msr, &val))
  226. msr[i].attr = NULL;
  227. }
  228. /* List remaining MSRs in the sysfs attrs. */
  229. for (i = 0; i < PERF_MSR_EVENT_MAX; i++) {
  230. if (msr[i].attr)
  231. events_attrs[j++] = &msr[i].attr->attr.attr;
  232. }
  233. events_attrs[j] = NULL;
  234. perf_pmu_register(&pmu_msr, "msr", -1);
  235. return 0;
  236. }
  237. device_initcall(msr_init);