perf_event_msr.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include <linux/perf_event.h>
  2. enum perf_msr_id {
  3. PERF_MSR_TSC = 0,
  4. PERF_MSR_APERF = 1,
  5. PERF_MSR_MPERF = 2,
  6. PERF_MSR_PPERF = 3,
  7. PERF_MSR_SMI = 4,
  8. PERF_MSR_EVENT_MAX,
  9. };
  10. static bool test_aperfmperf(int idx)
  11. {
  12. return boot_cpu_has(X86_FEATURE_APERFMPERF);
  13. }
  14. static bool test_intel(int idx)
  15. {
  16. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
  17. boot_cpu_data.x86 != 6)
  18. return false;
  19. switch (boot_cpu_data.x86_model) {
  20. case 30: /* 45nm Nehalem */
  21. case 26: /* 45nm Nehalem-EP */
  22. case 46: /* 45nm Nehalem-EX */
  23. case 37: /* 32nm Westmere */
  24. case 44: /* 32nm Westmere-EP */
  25. case 47: /* 32nm Westmere-EX */
  26. case 42: /* 32nm SandyBridge */
  27. case 45: /* 32nm SandyBridge-E/EN/EP */
  28. case 58: /* 22nm IvyBridge */
  29. case 62: /* 22nm IvyBridge-EP/EX */
  30. case 60: /* 22nm Haswell Core */
  31. case 63: /* 22nm Haswell Server */
  32. case 69: /* 22nm Haswell ULT */
  33. case 70: /* 22nm Haswell + GT3e (Intel Iris Pro graphics) */
  34. case 61: /* 14nm Broadwell Core-M */
  35. case 86: /* 14nm Broadwell Xeon D */
  36. case 71: /* 14nm Broadwell + GT3e (Intel Iris Pro graphics) */
  37. case 79: /* 14nm Broadwell Server */
  38. case 55: /* 22nm Atom "Silvermont" */
  39. case 77: /* 22nm Atom "Silvermont Avoton/Rangely" */
  40. case 76: /* 14nm Atom "Airmont" */
  41. if (idx == PERF_MSR_SMI)
  42. return true;
  43. break;
  44. case 78: /* 14nm Skylake Mobile */
  45. case 94: /* 14nm Skylake Desktop */
  46. if (idx == PERF_MSR_SMI || idx == PERF_MSR_PPERF)
  47. return true;
  48. break;
  49. }
  50. return false;
  51. }
  52. struct perf_msr {
  53. u64 msr;
  54. struct perf_pmu_events_attr *attr;
  55. bool (*test)(int idx);
  56. };
  57. PMU_EVENT_ATTR_STRING(tsc, evattr_tsc, "event=0x00");
  58. PMU_EVENT_ATTR_STRING(aperf, evattr_aperf, "event=0x01");
  59. PMU_EVENT_ATTR_STRING(mperf, evattr_mperf, "event=0x02");
  60. PMU_EVENT_ATTR_STRING(pperf, evattr_pperf, "event=0x03");
  61. PMU_EVENT_ATTR_STRING(smi, evattr_smi, "event=0x04");
  62. static struct perf_msr msr[] = {
  63. [PERF_MSR_TSC] = { 0, &evattr_tsc, NULL, },
  64. [PERF_MSR_APERF] = { MSR_IA32_APERF, &evattr_aperf, test_aperfmperf, },
  65. [PERF_MSR_MPERF] = { MSR_IA32_MPERF, &evattr_mperf, test_aperfmperf, },
  66. [PERF_MSR_PPERF] = { MSR_PPERF, &evattr_pperf, test_intel, },
  67. [PERF_MSR_SMI] = { MSR_SMI_COUNT, &evattr_smi, test_intel, },
  68. };
  69. static struct attribute *events_attrs[PERF_MSR_EVENT_MAX + 1] = {
  70. NULL,
  71. };
  72. static struct attribute_group events_attr_group = {
  73. .name = "events",
  74. .attrs = events_attrs,
  75. };
  76. PMU_FORMAT_ATTR(event, "config:0-63");
  77. static struct attribute *format_attrs[] = {
  78. &format_attr_event.attr,
  79. NULL,
  80. };
  81. static struct attribute_group format_attr_group = {
  82. .name = "format",
  83. .attrs = format_attrs,
  84. };
  85. static const struct attribute_group *attr_groups[] = {
  86. &events_attr_group,
  87. &format_attr_group,
  88. NULL,
  89. };
  90. static int msr_event_init(struct perf_event *event)
  91. {
  92. u64 cfg = event->attr.config;
  93. if (event->attr.type != event->pmu->type)
  94. return -ENOENT;
  95. if (cfg >= PERF_MSR_EVENT_MAX)
  96. return -EINVAL;
  97. /* unsupported modes and filters */
  98. if (event->attr.exclude_user ||
  99. event->attr.exclude_kernel ||
  100. event->attr.exclude_hv ||
  101. event->attr.exclude_idle ||
  102. event->attr.exclude_host ||
  103. event->attr.exclude_guest ||
  104. event->attr.sample_period) /* no sampling */
  105. return -EINVAL;
  106. if (!msr[cfg].attr)
  107. return -EINVAL;
  108. event->hw.idx = -1;
  109. event->hw.event_base = msr[cfg].msr;
  110. event->hw.config = cfg;
  111. return 0;
  112. }
  113. static inline u64 msr_read_counter(struct perf_event *event)
  114. {
  115. u64 now;
  116. if (event->hw.event_base)
  117. rdmsrl(event->hw.event_base, now);
  118. else
  119. rdtscll(now);
  120. return now;
  121. }
  122. static void msr_event_update(struct perf_event *event)
  123. {
  124. u64 prev, now;
  125. s64 delta;
  126. /* Careful, an NMI might modify the previous event value. */
  127. again:
  128. prev = local64_read(&event->hw.prev_count);
  129. now = msr_read_counter(event);
  130. if (local64_cmpxchg(&event->hw.prev_count, prev, now) != prev)
  131. goto again;
  132. delta = now - prev;
  133. if (unlikely(event->hw.event_base == MSR_SMI_COUNT)) {
  134. delta <<= 32;
  135. delta >>= 32; /* sign extend */
  136. }
  137. local64_add(now - prev, &event->count);
  138. }
  139. static void msr_event_start(struct perf_event *event, int flags)
  140. {
  141. u64 now;
  142. now = msr_read_counter(event);
  143. local64_set(&event->hw.prev_count, now);
  144. }
  145. static void msr_event_stop(struct perf_event *event, int flags)
  146. {
  147. msr_event_update(event);
  148. }
  149. static void msr_event_del(struct perf_event *event, int flags)
  150. {
  151. msr_event_stop(event, PERF_EF_UPDATE);
  152. }
  153. static int msr_event_add(struct perf_event *event, int flags)
  154. {
  155. if (flags & PERF_EF_START)
  156. msr_event_start(event, flags);
  157. return 0;
  158. }
  159. static struct pmu pmu_msr = {
  160. .task_ctx_nr = perf_sw_context,
  161. .attr_groups = attr_groups,
  162. .event_init = msr_event_init,
  163. .add = msr_event_add,
  164. .del = msr_event_del,
  165. .start = msr_event_start,
  166. .stop = msr_event_stop,
  167. .read = msr_event_update,
  168. .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
  169. };
  170. static int __init msr_init(void)
  171. {
  172. int i, j = 0;
  173. if (!boot_cpu_has(X86_FEATURE_TSC)) {
  174. pr_cont("no MSR PMU driver.\n");
  175. return 0;
  176. }
  177. /* Probe the MSRs. */
  178. for (i = PERF_MSR_TSC + 1; i < PERF_MSR_EVENT_MAX; i++) {
  179. u64 val;
  180. /*
  181. * Virt sucks arse; you cannot tell if a R/O MSR is present :/
  182. */
  183. if (!msr[i].test(i) || rdmsrl_safe(msr[i].msr, &val))
  184. msr[i].attr = NULL;
  185. }
  186. /* List remaining MSRs in the sysfs attrs. */
  187. for (i = 0; i < PERF_MSR_EVENT_MAX; i++) {
  188. if (msr[i].attr)
  189. events_attrs[j++] = &msr[i].attr->attr.attr;
  190. }
  191. events_attrs[j] = NULL;
  192. perf_pmu_register(&pmu_msr, "msr", -1);
  193. return 0;
  194. }
  195. device_initcall(msr_init);