power8-pmu.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Performance counter support for POWER8 processors.
  3. *
  4. * Copyright 2009 Paul Mackerras, IBM Corporation.
  5. * Copyright 2013 Michael Ellerman, IBM Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #define pr_fmt(fmt) "power8-pmu: " fmt
  13. #include "isa207-common.h"
  14. /*
  15. * Some power8 event codes.
  16. */
  17. #define EVENT(_name, _code) _name = _code,
  18. enum {
  19. #include "power8-events-list.h"
  20. };
  21. #undef EVENT
  22. /* MMCRA IFM bits - POWER8 */
  23. #define POWER8_MMCRA_IFM1 0x0000000040000000UL
  24. #define POWER8_MMCRA_IFM2 0x0000000080000000UL
  25. #define POWER8_MMCRA_IFM3 0x00000000C0000000UL
  26. /* PowerISA v2.07 format attribute structure*/
  27. extern struct attribute_group isa207_pmu_format_group;
  28. /* Table of alternatives, sorted by column 0 */
  29. static const unsigned int event_alternatives[][MAX_ALT] = {
  30. { PM_MRK_ST_CMPL, PM_MRK_ST_CMPL_ALT },
  31. { PM_BR_MRK_2PATH, PM_BR_MRK_2PATH_ALT },
  32. { PM_L3_CO_MEPF, PM_L3_CO_MEPF_ALT },
  33. { PM_MRK_DATA_FROM_L2MISS, PM_MRK_DATA_FROM_L2MISS_ALT },
  34. { PM_CMPLU_STALL_ALT, PM_CMPLU_STALL },
  35. { PM_BR_2PATH, PM_BR_2PATH_ALT },
  36. { PM_INST_DISP, PM_INST_DISP_ALT },
  37. { PM_RUN_CYC_ALT, PM_RUN_CYC },
  38. { PM_MRK_FILT_MATCH, PM_MRK_FILT_MATCH_ALT },
  39. { PM_LD_MISS_L1, PM_LD_MISS_L1_ALT },
  40. { PM_RUN_INST_CMPL_ALT, PM_RUN_INST_CMPL },
  41. };
  42. static int power8_get_alternatives(u64 event, unsigned int flags, u64 alt[])
  43. {
  44. int i, j, num_alt = 0;
  45. num_alt = isa207_get_alternatives(event, alt, event_alternatives,
  46. (int)ARRAY_SIZE(event_alternatives));
  47. if (flags & PPMU_ONLY_COUNT_RUN) {
  48. /*
  49. * We're only counting in RUN state, so PM_CYC is equivalent to
  50. * PM_RUN_CYC and PM_INST_CMPL === PM_RUN_INST_CMPL.
  51. */
  52. j = num_alt;
  53. for (i = 0; i < num_alt; ++i) {
  54. switch (alt[i]) {
  55. case PM_CYC:
  56. alt[j++] = PM_RUN_CYC;
  57. break;
  58. case PM_RUN_CYC:
  59. alt[j++] = PM_CYC;
  60. break;
  61. case PM_INST_CMPL:
  62. alt[j++] = PM_RUN_INST_CMPL;
  63. break;
  64. case PM_RUN_INST_CMPL:
  65. alt[j++] = PM_INST_CMPL;
  66. break;
  67. }
  68. }
  69. num_alt = j;
  70. }
  71. return num_alt;
  72. }
  73. GENERIC_EVENT_ATTR(cpu-cycles, PM_CYC);
  74. GENERIC_EVENT_ATTR(stalled-cycles-frontend, PM_GCT_NOSLOT_CYC);
  75. GENERIC_EVENT_ATTR(stalled-cycles-backend, PM_CMPLU_STALL);
  76. GENERIC_EVENT_ATTR(instructions, PM_INST_CMPL);
  77. GENERIC_EVENT_ATTR(branch-instructions, PM_BRU_FIN);
  78. GENERIC_EVENT_ATTR(branch-misses, PM_BR_MPRED_CMPL);
  79. GENERIC_EVENT_ATTR(cache-references, PM_LD_REF_L1);
  80. GENERIC_EVENT_ATTR(cache-misses, PM_LD_MISS_L1);
  81. CACHE_EVENT_ATTR(L1-dcache-load-misses, PM_LD_MISS_L1);
  82. CACHE_EVENT_ATTR(L1-dcache-loads, PM_LD_REF_L1);
  83. CACHE_EVENT_ATTR(L1-dcache-prefetches, PM_L1_PREF);
  84. CACHE_EVENT_ATTR(L1-dcache-store-misses, PM_ST_MISS_L1);
  85. CACHE_EVENT_ATTR(L1-icache-load-misses, PM_L1_ICACHE_MISS);
  86. CACHE_EVENT_ATTR(L1-icache-loads, PM_INST_FROM_L1);
  87. CACHE_EVENT_ATTR(L1-icache-prefetches, PM_IC_PREF_WRITE);
  88. CACHE_EVENT_ATTR(LLC-load-misses, PM_DATA_FROM_L3MISS);
  89. CACHE_EVENT_ATTR(LLC-loads, PM_DATA_FROM_L3);
  90. CACHE_EVENT_ATTR(LLC-prefetches, PM_L3_PREF_ALL);
  91. CACHE_EVENT_ATTR(LLC-store-misses, PM_L2_ST_MISS);
  92. CACHE_EVENT_ATTR(LLC-stores, PM_L2_ST);
  93. CACHE_EVENT_ATTR(branch-load-misses, PM_BR_MPRED_CMPL);
  94. CACHE_EVENT_ATTR(branch-loads, PM_BRU_FIN);
  95. CACHE_EVENT_ATTR(dTLB-load-misses, PM_DTLB_MISS);
  96. CACHE_EVENT_ATTR(iTLB-load-misses, PM_ITLB_MISS);
  97. static struct attribute *power8_events_attr[] = {
  98. GENERIC_EVENT_PTR(PM_CYC),
  99. GENERIC_EVENT_PTR(PM_GCT_NOSLOT_CYC),
  100. GENERIC_EVENT_PTR(PM_CMPLU_STALL),
  101. GENERIC_EVENT_PTR(PM_INST_CMPL),
  102. GENERIC_EVENT_PTR(PM_BRU_FIN),
  103. GENERIC_EVENT_PTR(PM_BR_MPRED_CMPL),
  104. GENERIC_EVENT_PTR(PM_LD_REF_L1),
  105. GENERIC_EVENT_PTR(PM_LD_MISS_L1),
  106. CACHE_EVENT_PTR(PM_LD_MISS_L1),
  107. CACHE_EVENT_PTR(PM_LD_REF_L1),
  108. CACHE_EVENT_PTR(PM_L1_PREF),
  109. CACHE_EVENT_PTR(PM_ST_MISS_L1),
  110. CACHE_EVENT_PTR(PM_L1_ICACHE_MISS),
  111. CACHE_EVENT_PTR(PM_INST_FROM_L1),
  112. CACHE_EVENT_PTR(PM_IC_PREF_WRITE),
  113. CACHE_EVENT_PTR(PM_DATA_FROM_L3MISS),
  114. CACHE_EVENT_PTR(PM_DATA_FROM_L3),
  115. CACHE_EVENT_PTR(PM_L3_PREF_ALL),
  116. CACHE_EVENT_PTR(PM_L2_ST_MISS),
  117. CACHE_EVENT_PTR(PM_L2_ST),
  118. CACHE_EVENT_PTR(PM_BR_MPRED_CMPL),
  119. CACHE_EVENT_PTR(PM_BRU_FIN),
  120. CACHE_EVENT_PTR(PM_DTLB_MISS),
  121. CACHE_EVENT_PTR(PM_ITLB_MISS),
  122. NULL
  123. };
  124. static struct attribute_group power8_pmu_events_group = {
  125. .name = "events",
  126. .attrs = power8_events_attr,
  127. };
  128. static const struct attribute_group *power8_pmu_attr_groups[] = {
  129. &isa207_pmu_format_group,
  130. &power8_pmu_events_group,
  131. NULL,
  132. };
  133. static int power8_generic_events[] = {
  134. [PERF_COUNT_HW_CPU_CYCLES] = PM_CYC,
  135. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = PM_GCT_NOSLOT_CYC,
  136. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = PM_CMPLU_STALL,
  137. [PERF_COUNT_HW_INSTRUCTIONS] = PM_INST_CMPL,
  138. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = PM_BRU_FIN,
  139. [PERF_COUNT_HW_BRANCH_MISSES] = PM_BR_MPRED_CMPL,
  140. [PERF_COUNT_HW_CACHE_REFERENCES] = PM_LD_REF_L1,
  141. [PERF_COUNT_HW_CACHE_MISSES] = PM_LD_MISS_L1,
  142. };
  143. static u64 power8_bhrb_filter_map(u64 branch_sample_type)
  144. {
  145. u64 pmu_bhrb_filter = 0;
  146. /* BHRB and regular PMU events share the same privilege state
  147. * filter configuration. BHRB is always recorded along with a
  148. * regular PMU event. As the privilege state filter is handled
  149. * in the basic PMC configuration of the accompanying regular
  150. * PMU event, we ignore any separate BHRB specific request.
  151. */
  152. /* No branch filter requested */
  153. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY)
  154. return pmu_bhrb_filter;
  155. /* Invalid branch filter options - HW does not support */
  156. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
  157. return -1;
  158. if (branch_sample_type & PERF_SAMPLE_BRANCH_IND_CALL)
  159. return -1;
  160. if (branch_sample_type & PERF_SAMPLE_BRANCH_CALL)
  161. return -1;
  162. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_CALL) {
  163. pmu_bhrb_filter |= POWER8_MMCRA_IFM1;
  164. return pmu_bhrb_filter;
  165. }
  166. /* Every thing else is unsupported */
  167. return -1;
  168. }
  169. static void power8_config_bhrb(u64 pmu_bhrb_filter)
  170. {
  171. /* Enable BHRB filter in PMU */
  172. mtspr(SPRN_MMCRA, (mfspr(SPRN_MMCRA) | pmu_bhrb_filter));
  173. }
  174. #define C(x) PERF_COUNT_HW_CACHE_##x
  175. /*
  176. * Table of generalized cache-related events.
  177. * 0 means not supported, -1 means nonsensical, other values
  178. * are event codes.
  179. */
  180. static int power8_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
  181. [ C(L1D) ] = {
  182. [ C(OP_READ) ] = {
  183. [ C(RESULT_ACCESS) ] = PM_LD_REF_L1,
  184. [ C(RESULT_MISS) ] = PM_LD_MISS_L1,
  185. },
  186. [ C(OP_WRITE) ] = {
  187. [ C(RESULT_ACCESS) ] = 0,
  188. [ C(RESULT_MISS) ] = PM_ST_MISS_L1,
  189. },
  190. [ C(OP_PREFETCH) ] = {
  191. [ C(RESULT_ACCESS) ] = PM_L1_PREF,
  192. [ C(RESULT_MISS) ] = 0,
  193. },
  194. },
  195. [ C(L1I) ] = {
  196. [ C(OP_READ) ] = {
  197. [ C(RESULT_ACCESS) ] = PM_INST_FROM_L1,
  198. [ C(RESULT_MISS) ] = PM_L1_ICACHE_MISS,
  199. },
  200. [ C(OP_WRITE) ] = {
  201. [ C(RESULT_ACCESS) ] = PM_L1_DEMAND_WRITE,
  202. [ C(RESULT_MISS) ] = -1,
  203. },
  204. [ C(OP_PREFETCH) ] = {
  205. [ C(RESULT_ACCESS) ] = PM_IC_PREF_WRITE,
  206. [ C(RESULT_MISS) ] = 0,
  207. },
  208. },
  209. [ C(LL) ] = {
  210. [ C(OP_READ) ] = {
  211. [ C(RESULT_ACCESS) ] = PM_DATA_FROM_L3,
  212. [ C(RESULT_MISS) ] = PM_DATA_FROM_L3MISS,
  213. },
  214. [ C(OP_WRITE) ] = {
  215. [ C(RESULT_ACCESS) ] = PM_L2_ST,
  216. [ C(RESULT_MISS) ] = PM_L2_ST_MISS,
  217. },
  218. [ C(OP_PREFETCH) ] = {
  219. [ C(RESULT_ACCESS) ] = PM_L3_PREF_ALL,
  220. [ C(RESULT_MISS) ] = 0,
  221. },
  222. },
  223. [ C(DTLB) ] = {
  224. [ C(OP_READ) ] = {
  225. [ C(RESULT_ACCESS) ] = 0,
  226. [ C(RESULT_MISS) ] = PM_DTLB_MISS,
  227. },
  228. [ C(OP_WRITE) ] = {
  229. [ C(RESULT_ACCESS) ] = -1,
  230. [ C(RESULT_MISS) ] = -1,
  231. },
  232. [ C(OP_PREFETCH) ] = {
  233. [ C(RESULT_ACCESS) ] = -1,
  234. [ C(RESULT_MISS) ] = -1,
  235. },
  236. },
  237. [ C(ITLB) ] = {
  238. [ C(OP_READ) ] = {
  239. [ C(RESULT_ACCESS) ] = 0,
  240. [ C(RESULT_MISS) ] = PM_ITLB_MISS,
  241. },
  242. [ C(OP_WRITE) ] = {
  243. [ C(RESULT_ACCESS) ] = -1,
  244. [ C(RESULT_MISS) ] = -1,
  245. },
  246. [ C(OP_PREFETCH) ] = {
  247. [ C(RESULT_ACCESS) ] = -1,
  248. [ C(RESULT_MISS) ] = -1,
  249. },
  250. },
  251. [ C(BPU) ] = {
  252. [ C(OP_READ) ] = {
  253. [ C(RESULT_ACCESS) ] = PM_BRU_FIN,
  254. [ C(RESULT_MISS) ] = PM_BR_MPRED_CMPL,
  255. },
  256. [ C(OP_WRITE) ] = {
  257. [ C(RESULT_ACCESS) ] = -1,
  258. [ C(RESULT_MISS) ] = -1,
  259. },
  260. [ C(OP_PREFETCH) ] = {
  261. [ C(RESULT_ACCESS) ] = -1,
  262. [ C(RESULT_MISS) ] = -1,
  263. },
  264. },
  265. [ C(NODE) ] = {
  266. [ C(OP_READ) ] = {
  267. [ C(RESULT_ACCESS) ] = -1,
  268. [ C(RESULT_MISS) ] = -1,
  269. },
  270. [ C(OP_WRITE) ] = {
  271. [ C(RESULT_ACCESS) ] = -1,
  272. [ C(RESULT_MISS) ] = -1,
  273. },
  274. [ C(OP_PREFETCH) ] = {
  275. [ C(RESULT_ACCESS) ] = -1,
  276. [ C(RESULT_MISS) ] = -1,
  277. },
  278. },
  279. };
  280. #undef C
  281. static struct power_pmu power8_pmu = {
  282. .name = "POWER8",
  283. .n_counter = MAX_PMU_COUNTERS,
  284. .max_alternatives = MAX_ALT + 1,
  285. .add_fields = ISA207_ADD_FIELDS,
  286. .test_adder = ISA207_TEST_ADDER,
  287. .compute_mmcr = isa207_compute_mmcr,
  288. .config_bhrb = power8_config_bhrb,
  289. .bhrb_filter_map = power8_bhrb_filter_map,
  290. .get_constraint = isa207_get_constraint,
  291. .get_alternatives = power8_get_alternatives,
  292. .disable_pmc = isa207_disable_pmc,
  293. .flags = PPMU_HAS_SIER | PPMU_ARCH_207S,
  294. .n_generic = ARRAY_SIZE(power8_generic_events),
  295. .generic_events = power8_generic_events,
  296. .cache_events = &power8_cache_events,
  297. .attr_groups = power8_pmu_attr_groups,
  298. .bhrb_nr = 32,
  299. };
  300. static int __init init_power8_pmu(void)
  301. {
  302. int rc;
  303. if (!cur_cpu_spec->oprofile_cpu_type ||
  304. strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power8"))
  305. return -ENODEV;
  306. rc = register_power_pmu(&power8_pmu);
  307. if (rc)
  308. return rc;
  309. /* Tell userspace that EBB is supported */
  310. cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_EBB;
  311. if (cpu_has_feature(CPU_FTR_PMAO_BUG))
  312. pr_info("PMAO restore workaround active.\n");
  313. return 0;
  314. }
  315. early_initcall(init_power8_pmu);