msr.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. case INTEL_FAM6_ICELAKE_MOBILE:
  76. if (idx == PERF_MSR_SMI || idx == PERF_MSR_PPERF)
  77. return true;
  78. break;
  79. }
  80. return false;
  81. }
  82. struct perf_msr {
  83. u64 msr;
  84. struct perf_pmu_events_attr *attr;
  85. bool (*test)(int idx);
  86. };
  87. PMU_EVENT_ATTR_STRING(tsc, evattr_tsc, "event=0x00" );
  88. PMU_EVENT_ATTR_STRING(aperf, evattr_aperf, "event=0x01" );
  89. PMU_EVENT_ATTR_STRING(mperf, evattr_mperf, "event=0x02" );
  90. PMU_EVENT_ATTR_STRING(pperf, evattr_pperf, "event=0x03" );
  91. PMU_EVENT_ATTR_STRING(smi, evattr_smi, "event=0x04" );
  92. PMU_EVENT_ATTR_STRING(ptsc, evattr_ptsc, "event=0x05" );
  93. PMU_EVENT_ATTR_STRING(irperf, evattr_irperf, "event=0x06" );
  94. PMU_EVENT_ATTR_STRING(cpu_thermal_margin, evattr_therm, "event=0x07" );
  95. PMU_EVENT_ATTR_STRING(cpu_thermal_margin.snapshot, evattr_therm_snap, "1" );
  96. PMU_EVENT_ATTR_STRING(cpu_thermal_margin.unit, evattr_therm_unit, "C" );
  97. static struct perf_msr msr[] = {
  98. [PERF_MSR_TSC] = { 0, &evattr_tsc, NULL, },
  99. [PERF_MSR_APERF] = { MSR_IA32_APERF, &evattr_aperf, test_aperfmperf, },
  100. [PERF_MSR_MPERF] = { MSR_IA32_MPERF, &evattr_mperf, test_aperfmperf, },
  101. [PERF_MSR_PPERF] = { MSR_PPERF, &evattr_pperf, test_intel, },
  102. [PERF_MSR_SMI] = { MSR_SMI_COUNT, &evattr_smi, test_intel, },
  103. [PERF_MSR_PTSC] = { MSR_F15H_PTSC, &evattr_ptsc, test_ptsc, },
  104. [PERF_MSR_IRPERF] = { MSR_F17H_IRPERF, &evattr_irperf, test_irperf, },
  105. [PERF_MSR_THERM] = { MSR_IA32_THERM_STATUS, &evattr_therm, test_therm_status, },
  106. [PERF_MSR_THERM_SNAP] = { MSR_IA32_THERM_STATUS, &evattr_therm_snap, test_therm_status, },
  107. [PERF_MSR_THERM_UNIT] = { MSR_IA32_THERM_STATUS, &evattr_therm_unit, test_therm_status, },
  108. };
  109. static struct attribute *events_attrs[PERF_MSR_EVENT_MAX + 1] = {
  110. NULL,
  111. };
  112. static struct attribute_group events_attr_group = {
  113. .name = "events",
  114. .attrs = events_attrs,
  115. };
  116. PMU_FORMAT_ATTR(event, "config:0-63");
  117. static struct attribute *format_attrs[] = {
  118. &format_attr_event.attr,
  119. NULL,
  120. };
  121. static struct attribute_group format_attr_group = {
  122. .name = "format",
  123. .attrs = format_attrs,
  124. };
  125. static const struct attribute_group *attr_groups[] = {
  126. &events_attr_group,
  127. &format_attr_group,
  128. NULL,
  129. };
  130. static int msr_event_init(struct perf_event *event)
  131. {
  132. u64 cfg = event->attr.config;
  133. if (event->attr.type != event->pmu->type)
  134. return -ENOENT;
  135. /* unsupported modes and filters */
  136. if (event->attr.exclude_user ||
  137. event->attr.exclude_kernel ||
  138. event->attr.exclude_hv ||
  139. event->attr.exclude_idle ||
  140. event->attr.exclude_host ||
  141. event->attr.exclude_guest ||
  142. event->attr.sample_period) /* no sampling */
  143. return -EINVAL;
  144. if (cfg >= PERF_MSR_EVENT_MAX)
  145. return -EINVAL;
  146. cfg = array_index_nospec((unsigned long)cfg, PERF_MSR_EVENT_MAX);
  147. if (!msr[cfg].attr)
  148. return -EINVAL;
  149. event->hw.idx = -1;
  150. event->hw.event_base = msr[cfg].msr;
  151. event->hw.config = cfg;
  152. return 0;
  153. }
  154. static inline u64 msr_read_counter(struct perf_event *event)
  155. {
  156. u64 now;
  157. if (event->hw.event_base)
  158. rdmsrl(event->hw.event_base, now);
  159. else
  160. now = rdtsc_ordered();
  161. return now;
  162. }
  163. static void msr_event_update(struct perf_event *event)
  164. {
  165. u64 prev, now;
  166. s64 delta;
  167. /* Careful, an NMI might modify the previous event value: */
  168. again:
  169. prev = local64_read(&event->hw.prev_count);
  170. now = msr_read_counter(event);
  171. if (local64_cmpxchg(&event->hw.prev_count, prev, now) != prev)
  172. goto again;
  173. delta = now - prev;
  174. if (unlikely(event->hw.event_base == MSR_SMI_COUNT)) {
  175. delta = sign_extend64(delta, 31);
  176. local64_add(delta, &event->count);
  177. } else if (unlikely(event->hw.event_base == MSR_IA32_THERM_STATUS)) {
  178. /* If valid, extract digital readout, otherwise set to -1: */
  179. now = now & (1ULL << 31) ? (now >> 16) & 0x3f : -1;
  180. local64_set(&event->count, now);
  181. } else {
  182. local64_add(delta, &event->count);
  183. }
  184. }
  185. static void msr_event_start(struct perf_event *event, int flags)
  186. {
  187. u64 now = msr_read_counter(event);
  188. local64_set(&event->hw.prev_count, now);
  189. }
  190. static void msr_event_stop(struct perf_event *event, int flags)
  191. {
  192. msr_event_update(event);
  193. }
  194. static void msr_event_del(struct perf_event *event, int flags)
  195. {
  196. msr_event_stop(event, PERF_EF_UPDATE);
  197. }
  198. static int msr_event_add(struct perf_event *event, int flags)
  199. {
  200. if (flags & PERF_EF_START)
  201. msr_event_start(event, flags);
  202. return 0;
  203. }
  204. static struct pmu pmu_msr = {
  205. .task_ctx_nr = perf_sw_context,
  206. .attr_groups = attr_groups,
  207. .event_init = msr_event_init,
  208. .add = msr_event_add,
  209. .del = msr_event_del,
  210. .start = msr_event_start,
  211. .stop = msr_event_stop,
  212. .read = msr_event_update,
  213. .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
  214. };
  215. static int __init msr_init(void)
  216. {
  217. int i, j = 0;
  218. if (!boot_cpu_has(X86_FEATURE_TSC)) {
  219. pr_cont("no MSR PMU driver.\n");
  220. return 0;
  221. }
  222. /* Probe the MSRs. */
  223. for (i = PERF_MSR_TSC + 1; i < PERF_MSR_EVENT_MAX; i++) {
  224. u64 val;
  225. /* Virt sucks; you cannot tell if a R/O MSR is present :/ */
  226. if (!msr[i].test(i) || rdmsrl_safe(msr[i].msr, &val))
  227. msr[i].attr = NULL;
  228. }
  229. /* List remaining MSRs in the sysfs attrs. */
  230. for (i = 0; i < PERF_MSR_EVENT_MAX; i++) {
  231. if (msr[i].attr)
  232. events_attrs[j++] = &msr[i].attr->attr.attr;
  233. }
  234. events_attrs[j] = NULL;
  235. perf_pmu_register(&pmu_msr, "msr", -1);
  236. return 0;
  237. }
  238. device_initcall(msr_init);