knc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* Driver for Intel Xeon Phi "Knights Corner" PMU */
  2. #include <linux/perf_event.h>
  3. #include <linux/types.h>
  4. #include <asm/hardirq.h>
  5. #include "../perf_event.h"
  6. static const u64 knc_perfmon_event_map[] =
  7. {
  8. [PERF_COUNT_HW_CPU_CYCLES] = 0x002a,
  9. [PERF_COUNT_HW_INSTRUCTIONS] = 0x0016,
  10. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0028,
  11. [PERF_COUNT_HW_CACHE_MISSES] = 0x0029,
  12. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x0012,
  13. [PERF_COUNT_HW_BRANCH_MISSES] = 0x002b,
  14. };
  15. static const u64 __initconst knc_hw_cache_event_ids
  16. [PERF_COUNT_HW_CACHE_MAX]
  17. [PERF_COUNT_HW_CACHE_OP_MAX]
  18. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  19. {
  20. [ C(L1D) ] = {
  21. [ C(OP_READ) ] = {
  22. /* On Xeon Phi event "0" is a valid DATA_READ */
  23. /* (L1 Data Cache Reads) Instruction. */
  24. /* We code this as ARCH_PERFMON_EVENTSEL_INT as this */
  25. /* bit will always be set in x86_pmu_hw_config(). */
  26. [ C(RESULT_ACCESS) ] = ARCH_PERFMON_EVENTSEL_INT,
  27. /* DATA_READ */
  28. [ C(RESULT_MISS) ] = 0x0003, /* DATA_READ_MISS */
  29. },
  30. [ C(OP_WRITE) ] = {
  31. [ C(RESULT_ACCESS) ] = 0x0001, /* DATA_WRITE */
  32. [ C(RESULT_MISS) ] = 0x0004, /* DATA_WRITE_MISS */
  33. },
  34. [ C(OP_PREFETCH) ] = {
  35. [ C(RESULT_ACCESS) ] = 0x0011, /* L1_DATA_PF1 */
  36. [ C(RESULT_MISS) ] = 0x001c, /* L1_DATA_PF1_MISS */
  37. },
  38. },
  39. [ C(L1I ) ] = {
  40. [ C(OP_READ) ] = {
  41. [ C(RESULT_ACCESS) ] = 0x000c, /* CODE_READ */
  42. [ C(RESULT_MISS) ] = 0x000e, /* CODE_CACHE_MISS */
  43. },
  44. [ C(OP_WRITE) ] = {
  45. [ C(RESULT_ACCESS) ] = -1,
  46. [ C(RESULT_MISS) ] = -1,
  47. },
  48. [ C(OP_PREFETCH) ] = {
  49. [ C(RESULT_ACCESS) ] = 0x0,
  50. [ C(RESULT_MISS) ] = 0x0,
  51. },
  52. },
  53. [ C(LL ) ] = {
  54. [ C(OP_READ) ] = {
  55. [ C(RESULT_ACCESS) ] = 0,
  56. [ C(RESULT_MISS) ] = 0x10cb, /* L2_READ_MISS */
  57. },
  58. [ C(OP_WRITE) ] = {
  59. [ C(RESULT_ACCESS) ] = 0x10cc, /* L2_WRITE_HIT */
  60. [ C(RESULT_MISS) ] = 0,
  61. },
  62. [ C(OP_PREFETCH) ] = {
  63. [ C(RESULT_ACCESS) ] = 0x10fc, /* L2_DATA_PF2 */
  64. [ C(RESULT_MISS) ] = 0x10fe, /* L2_DATA_PF2_MISS */
  65. },
  66. },
  67. [ C(DTLB) ] = {
  68. [ C(OP_READ) ] = {
  69. [ C(RESULT_ACCESS) ] = ARCH_PERFMON_EVENTSEL_INT,
  70. /* DATA_READ */
  71. /* see note on L1 OP_READ */
  72. [ C(RESULT_MISS) ] = 0x0002, /* DATA_PAGE_WALK */
  73. },
  74. [ C(OP_WRITE) ] = {
  75. [ C(RESULT_ACCESS) ] = 0x0001, /* DATA_WRITE */
  76. [ C(RESULT_MISS) ] = 0x0002, /* DATA_PAGE_WALK */
  77. },
  78. [ C(OP_PREFETCH) ] = {
  79. [ C(RESULT_ACCESS) ] = 0x0,
  80. [ C(RESULT_MISS) ] = 0x0,
  81. },
  82. },
  83. [ C(ITLB) ] = {
  84. [ C(OP_READ) ] = {
  85. [ C(RESULT_ACCESS) ] = 0x000c, /* CODE_READ */
  86. [ C(RESULT_MISS) ] = 0x000d, /* CODE_PAGE_WALK */
  87. },
  88. [ C(OP_WRITE) ] = {
  89. [ C(RESULT_ACCESS) ] = -1,
  90. [ C(RESULT_MISS) ] = -1,
  91. },
  92. [ C(OP_PREFETCH) ] = {
  93. [ C(RESULT_ACCESS) ] = -1,
  94. [ C(RESULT_MISS) ] = -1,
  95. },
  96. },
  97. [ C(BPU ) ] = {
  98. [ C(OP_READ) ] = {
  99. [ C(RESULT_ACCESS) ] = 0x0012, /* BRANCHES */
  100. [ C(RESULT_MISS) ] = 0x002b, /* BRANCHES_MISPREDICTED */
  101. },
  102. [ C(OP_WRITE) ] = {
  103. [ C(RESULT_ACCESS) ] = -1,
  104. [ C(RESULT_MISS) ] = -1,
  105. },
  106. [ C(OP_PREFETCH) ] = {
  107. [ C(RESULT_ACCESS) ] = -1,
  108. [ C(RESULT_MISS) ] = -1,
  109. },
  110. },
  111. };
  112. static u64 knc_pmu_event_map(int hw_event)
  113. {
  114. return knc_perfmon_event_map[hw_event];
  115. }
  116. static struct event_constraint knc_event_constraints[] =
  117. {
  118. INTEL_EVENT_CONSTRAINT(0xc3, 0x1), /* HWP_L2HIT */
  119. INTEL_EVENT_CONSTRAINT(0xc4, 0x1), /* HWP_L2MISS */
  120. INTEL_EVENT_CONSTRAINT(0xc8, 0x1), /* L2_READ_HIT_E */
  121. INTEL_EVENT_CONSTRAINT(0xc9, 0x1), /* L2_READ_HIT_M */
  122. INTEL_EVENT_CONSTRAINT(0xca, 0x1), /* L2_READ_HIT_S */
  123. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* L2_READ_MISS */
  124. INTEL_EVENT_CONSTRAINT(0xcc, 0x1), /* L2_WRITE_HIT */
  125. INTEL_EVENT_CONSTRAINT(0xce, 0x1), /* L2_STRONGLY_ORDERED_STREAMING_VSTORES_MISS */
  126. INTEL_EVENT_CONSTRAINT(0xcf, 0x1), /* L2_WEAKLY_ORDERED_STREAMING_VSTORE_MISS */
  127. INTEL_EVENT_CONSTRAINT(0xd7, 0x1), /* L2_VICTIM_REQ_WITH_DATA */
  128. INTEL_EVENT_CONSTRAINT(0xe3, 0x1), /* SNP_HITM_BUNIT */
  129. INTEL_EVENT_CONSTRAINT(0xe6, 0x1), /* SNP_HIT_L2 */
  130. INTEL_EVENT_CONSTRAINT(0xe7, 0x1), /* SNP_HITM_L2 */
  131. INTEL_EVENT_CONSTRAINT(0xf1, 0x1), /* L2_DATA_READ_MISS_CACHE_FILL */
  132. INTEL_EVENT_CONSTRAINT(0xf2, 0x1), /* L2_DATA_WRITE_MISS_CACHE_FILL */
  133. INTEL_EVENT_CONSTRAINT(0xf6, 0x1), /* L2_DATA_READ_MISS_MEM_FILL */
  134. INTEL_EVENT_CONSTRAINT(0xf7, 0x1), /* L2_DATA_WRITE_MISS_MEM_FILL */
  135. INTEL_EVENT_CONSTRAINT(0xfc, 0x1), /* L2_DATA_PF2 */
  136. INTEL_EVENT_CONSTRAINT(0xfd, 0x1), /* L2_DATA_PF2_DROP */
  137. INTEL_EVENT_CONSTRAINT(0xfe, 0x1), /* L2_DATA_PF2_MISS */
  138. INTEL_EVENT_CONSTRAINT(0xff, 0x1), /* L2_DATA_HIT_INFLIGHT_PF2 */
  139. EVENT_CONSTRAINT_END
  140. };
  141. #define MSR_KNC_IA32_PERF_GLOBAL_STATUS 0x0000002d
  142. #define MSR_KNC_IA32_PERF_GLOBAL_OVF_CONTROL 0x0000002e
  143. #define MSR_KNC_IA32_PERF_GLOBAL_CTRL 0x0000002f
  144. #define KNC_ENABLE_COUNTER0 0x00000001
  145. #define KNC_ENABLE_COUNTER1 0x00000002
  146. static void knc_pmu_disable_all(void)
  147. {
  148. u64 val;
  149. rdmsrl(MSR_KNC_IA32_PERF_GLOBAL_CTRL, val);
  150. val &= ~(KNC_ENABLE_COUNTER0|KNC_ENABLE_COUNTER1);
  151. wrmsrl(MSR_KNC_IA32_PERF_GLOBAL_CTRL, val);
  152. }
  153. static void knc_pmu_enable_all(int added)
  154. {
  155. u64 val;
  156. rdmsrl(MSR_KNC_IA32_PERF_GLOBAL_CTRL, val);
  157. val |= (KNC_ENABLE_COUNTER0|KNC_ENABLE_COUNTER1);
  158. wrmsrl(MSR_KNC_IA32_PERF_GLOBAL_CTRL, val);
  159. }
  160. static inline void
  161. knc_pmu_disable_event(struct perf_event *event)
  162. {
  163. struct hw_perf_event *hwc = &event->hw;
  164. u64 val;
  165. val = hwc->config;
  166. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  167. (void)wrmsrl_safe(hwc->config_base + hwc->idx, val);
  168. }
  169. static void knc_pmu_enable_event(struct perf_event *event)
  170. {
  171. struct hw_perf_event *hwc = &event->hw;
  172. u64 val;
  173. val = hwc->config;
  174. val |= ARCH_PERFMON_EVENTSEL_ENABLE;
  175. (void)wrmsrl_safe(hwc->config_base + hwc->idx, val);
  176. }
  177. static inline u64 knc_pmu_get_status(void)
  178. {
  179. u64 status;
  180. rdmsrl(MSR_KNC_IA32_PERF_GLOBAL_STATUS, status);
  181. return status;
  182. }
  183. static inline void knc_pmu_ack_status(u64 ack)
  184. {
  185. wrmsrl(MSR_KNC_IA32_PERF_GLOBAL_OVF_CONTROL, ack);
  186. }
  187. static int knc_pmu_handle_irq(struct pt_regs *regs)
  188. {
  189. struct perf_sample_data data;
  190. struct cpu_hw_events *cpuc;
  191. int handled = 0;
  192. int bit, loops;
  193. u64 status;
  194. cpuc = this_cpu_ptr(&cpu_hw_events);
  195. knc_pmu_disable_all();
  196. status = knc_pmu_get_status();
  197. if (!status) {
  198. knc_pmu_enable_all(0);
  199. return handled;
  200. }
  201. loops = 0;
  202. again:
  203. knc_pmu_ack_status(status);
  204. if (++loops > 100) {
  205. WARN_ONCE(1, "perf: irq loop stuck!\n");
  206. perf_event_print_debug();
  207. goto done;
  208. }
  209. inc_irq_stat(apic_perf_irqs);
  210. for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  211. struct perf_event *event = cpuc->events[bit];
  212. handled++;
  213. if (!test_bit(bit, cpuc->active_mask))
  214. continue;
  215. if (!intel_pmu_save_and_restart(event))
  216. continue;
  217. perf_sample_data_init(&data, 0, event->hw.last_period);
  218. if (perf_event_overflow(event, &data, regs))
  219. x86_pmu_stop(event, 0);
  220. }
  221. /*
  222. * Repeat if there is more work to be done:
  223. */
  224. status = knc_pmu_get_status();
  225. if (status)
  226. goto again;
  227. done:
  228. /* Only restore PMU state when it's active. See x86_pmu_disable(). */
  229. if (cpuc->enabled)
  230. knc_pmu_enable_all(0);
  231. return handled;
  232. }
  233. PMU_FORMAT_ATTR(event, "config:0-7" );
  234. PMU_FORMAT_ATTR(umask, "config:8-15" );
  235. PMU_FORMAT_ATTR(edge, "config:18" );
  236. PMU_FORMAT_ATTR(inv, "config:23" );
  237. PMU_FORMAT_ATTR(cmask, "config:24-31" );
  238. static struct attribute *intel_knc_formats_attr[] = {
  239. &format_attr_event.attr,
  240. &format_attr_umask.attr,
  241. &format_attr_edge.attr,
  242. &format_attr_inv.attr,
  243. &format_attr_cmask.attr,
  244. NULL,
  245. };
  246. static const struct x86_pmu knc_pmu __initconst = {
  247. .name = "knc",
  248. .handle_irq = knc_pmu_handle_irq,
  249. .disable_all = knc_pmu_disable_all,
  250. .enable_all = knc_pmu_enable_all,
  251. .enable = knc_pmu_enable_event,
  252. .disable = knc_pmu_disable_event,
  253. .hw_config = x86_pmu_hw_config,
  254. .schedule_events = x86_schedule_events,
  255. .eventsel = MSR_KNC_EVNTSEL0,
  256. .perfctr = MSR_KNC_PERFCTR0,
  257. .event_map = knc_pmu_event_map,
  258. .max_events = ARRAY_SIZE(knc_perfmon_event_map),
  259. .apic = 1,
  260. .max_period = (1ULL << 39) - 1,
  261. .version = 0,
  262. .num_counters = 2,
  263. .cntval_bits = 40,
  264. .cntval_mask = (1ULL << 40) - 1,
  265. .get_event_constraints = x86_get_event_constraints,
  266. .event_constraints = knc_event_constraints,
  267. .format_attrs = intel_knc_formats_attr,
  268. };
  269. __init int knc_pmu_init(void)
  270. {
  271. x86_pmu = knc_pmu;
  272. memcpy(hw_cache_event_ids, knc_hw_cache_event_ids,
  273. sizeof(hw_cache_event_ids));
  274. return 0;
  275. }