power8-pmu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. /*
  43. * Scan the alternatives table for a match and return the
  44. * index into the alternatives table if found, else -1.
  45. */
  46. static int find_alternative(u64 event)
  47. {
  48. int i, j;
  49. for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
  50. if (event < event_alternatives[i][0])
  51. break;
  52. for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
  53. if (event == event_alternatives[i][j])
  54. return i;
  55. }
  56. return -1;
  57. }
  58. static int power8_get_alternatives(u64 event, unsigned int flags, u64 alt[])
  59. {
  60. int i, j, num_alt = 0;
  61. u64 alt_event;
  62. alt[num_alt++] = event;
  63. i = find_alternative(event);
  64. if (i >= 0) {
  65. /* Filter out the original event, it's already in alt[0] */
  66. for (j = 0; j < MAX_ALT; ++j) {
  67. alt_event = event_alternatives[i][j];
  68. if (alt_event && alt_event != event)
  69. alt[num_alt++] = alt_event;
  70. }
  71. }
  72. if (flags & PPMU_ONLY_COUNT_RUN) {
  73. /*
  74. * We're only counting in RUN state, so PM_CYC is equivalent to
  75. * PM_RUN_CYC and PM_INST_CMPL === PM_RUN_INST_CMPL.
  76. */
  77. j = num_alt;
  78. for (i = 0; i < num_alt; ++i) {
  79. switch (alt[i]) {
  80. case PM_CYC:
  81. alt[j++] = PM_RUN_CYC;
  82. break;
  83. case PM_RUN_CYC:
  84. alt[j++] = PM_CYC;
  85. break;
  86. case PM_INST_CMPL:
  87. alt[j++] = PM_RUN_INST_CMPL;
  88. break;
  89. case PM_RUN_INST_CMPL:
  90. alt[j++] = PM_INST_CMPL;
  91. break;
  92. }
  93. }
  94. num_alt = j;
  95. }
  96. return num_alt;
  97. }
  98. GENERIC_EVENT_ATTR(cpu-cycles, PM_CYC);
  99. GENERIC_EVENT_ATTR(stalled-cycles-frontend, PM_GCT_NOSLOT_CYC);
  100. GENERIC_EVENT_ATTR(stalled-cycles-backend, PM_CMPLU_STALL);
  101. GENERIC_EVENT_ATTR(instructions, PM_INST_CMPL);
  102. GENERIC_EVENT_ATTR(branch-instructions, PM_BRU_FIN);
  103. GENERIC_EVENT_ATTR(branch-misses, PM_BR_MPRED_CMPL);
  104. GENERIC_EVENT_ATTR(cache-references, PM_LD_REF_L1);
  105. GENERIC_EVENT_ATTR(cache-misses, PM_LD_MISS_L1);
  106. CACHE_EVENT_ATTR(L1-dcache-load-misses, PM_LD_MISS_L1);
  107. CACHE_EVENT_ATTR(L1-dcache-loads, PM_LD_REF_L1);
  108. CACHE_EVENT_ATTR(L1-dcache-prefetches, PM_L1_PREF);
  109. CACHE_EVENT_ATTR(L1-dcache-store-misses, PM_ST_MISS_L1);
  110. CACHE_EVENT_ATTR(L1-icache-load-misses, PM_L1_ICACHE_MISS);
  111. CACHE_EVENT_ATTR(L1-icache-loads, PM_INST_FROM_L1);
  112. CACHE_EVENT_ATTR(L1-icache-prefetches, PM_IC_PREF_WRITE);
  113. CACHE_EVENT_ATTR(LLC-load-misses, PM_DATA_FROM_L3MISS);
  114. CACHE_EVENT_ATTR(LLC-loads, PM_DATA_FROM_L3);
  115. CACHE_EVENT_ATTR(LLC-prefetches, PM_L3_PREF_ALL);
  116. CACHE_EVENT_ATTR(LLC-store-misses, PM_L2_ST_MISS);
  117. CACHE_EVENT_ATTR(LLC-stores, PM_L2_ST);
  118. CACHE_EVENT_ATTR(branch-load-misses, PM_BR_MPRED_CMPL);
  119. CACHE_EVENT_ATTR(branch-loads, PM_BRU_FIN);
  120. CACHE_EVENT_ATTR(dTLB-load-misses, PM_DTLB_MISS);
  121. CACHE_EVENT_ATTR(iTLB-load-misses, PM_ITLB_MISS);
  122. static struct attribute *power8_events_attr[] = {
  123. GENERIC_EVENT_PTR(PM_CYC),
  124. GENERIC_EVENT_PTR(PM_GCT_NOSLOT_CYC),
  125. GENERIC_EVENT_PTR(PM_CMPLU_STALL),
  126. GENERIC_EVENT_PTR(PM_INST_CMPL),
  127. GENERIC_EVENT_PTR(PM_BRU_FIN),
  128. GENERIC_EVENT_PTR(PM_BR_MPRED_CMPL),
  129. GENERIC_EVENT_PTR(PM_LD_REF_L1),
  130. GENERIC_EVENT_PTR(PM_LD_MISS_L1),
  131. CACHE_EVENT_PTR(PM_LD_MISS_L1),
  132. CACHE_EVENT_PTR(PM_LD_REF_L1),
  133. CACHE_EVENT_PTR(PM_L1_PREF),
  134. CACHE_EVENT_PTR(PM_ST_MISS_L1),
  135. CACHE_EVENT_PTR(PM_L1_ICACHE_MISS),
  136. CACHE_EVENT_PTR(PM_INST_FROM_L1),
  137. CACHE_EVENT_PTR(PM_IC_PREF_WRITE),
  138. CACHE_EVENT_PTR(PM_DATA_FROM_L3MISS),
  139. CACHE_EVENT_PTR(PM_DATA_FROM_L3),
  140. CACHE_EVENT_PTR(PM_L3_PREF_ALL),
  141. CACHE_EVENT_PTR(PM_L2_ST_MISS),
  142. CACHE_EVENT_PTR(PM_L2_ST),
  143. CACHE_EVENT_PTR(PM_BR_MPRED_CMPL),
  144. CACHE_EVENT_PTR(PM_BRU_FIN),
  145. CACHE_EVENT_PTR(PM_DTLB_MISS),
  146. CACHE_EVENT_PTR(PM_ITLB_MISS),
  147. NULL
  148. };
  149. static struct attribute_group power8_pmu_events_group = {
  150. .name = "events",
  151. .attrs = power8_events_attr,
  152. };
  153. static const struct attribute_group *power8_pmu_attr_groups[] = {
  154. &isa207_pmu_format_group,
  155. &power8_pmu_events_group,
  156. NULL,
  157. };
  158. static int power8_generic_events[] = {
  159. [PERF_COUNT_HW_CPU_CYCLES] = PM_CYC,
  160. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = PM_GCT_NOSLOT_CYC,
  161. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = PM_CMPLU_STALL,
  162. [PERF_COUNT_HW_INSTRUCTIONS] = PM_INST_CMPL,
  163. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = PM_BRU_FIN,
  164. [PERF_COUNT_HW_BRANCH_MISSES] = PM_BR_MPRED_CMPL,
  165. [PERF_COUNT_HW_CACHE_REFERENCES] = PM_LD_REF_L1,
  166. [PERF_COUNT_HW_CACHE_MISSES] = PM_LD_MISS_L1,
  167. };
  168. static u64 power8_bhrb_filter_map(u64 branch_sample_type)
  169. {
  170. u64 pmu_bhrb_filter = 0;
  171. /* BHRB and regular PMU events share the same privilege state
  172. * filter configuration. BHRB is always recorded along with a
  173. * regular PMU event. As the privilege state filter is handled
  174. * in the basic PMC configuration of the accompanying regular
  175. * PMU event, we ignore any separate BHRB specific request.
  176. */
  177. /* No branch filter requested */
  178. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY)
  179. return pmu_bhrb_filter;
  180. /* Invalid branch filter options - HW does not support */
  181. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
  182. return -1;
  183. if (branch_sample_type & PERF_SAMPLE_BRANCH_IND_CALL)
  184. return -1;
  185. if (branch_sample_type & PERF_SAMPLE_BRANCH_CALL)
  186. return -1;
  187. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_CALL) {
  188. pmu_bhrb_filter |= POWER8_MMCRA_IFM1;
  189. return pmu_bhrb_filter;
  190. }
  191. /* Every thing else is unsupported */
  192. return -1;
  193. }
  194. static void power8_config_bhrb(u64 pmu_bhrb_filter)
  195. {
  196. /* Enable BHRB filter in PMU */
  197. mtspr(SPRN_MMCRA, (mfspr(SPRN_MMCRA) | pmu_bhrb_filter));
  198. }
  199. #define C(x) PERF_COUNT_HW_CACHE_##x
  200. /*
  201. * Table of generalized cache-related events.
  202. * 0 means not supported, -1 means nonsensical, other values
  203. * are event codes.
  204. */
  205. static int power8_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
  206. [ C(L1D) ] = {
  207. [ C(OP_READ) ] = {
  208. [ C(RESULT_ACCESS) ] = PM_LD_REF_L1,
  209. [ C(RESULT_MISS) ] = PM_LD_MISS_L1,
  210. },
  211. [ C(OP_WRITE) ] = {
  212. [ C(RESULT_ACCESS) ] = 0,
  213. [ C(RESULT_MISS) ] = PM_ST_MISS_L1,
  214. },
  215. [ C(OP_PREFETCH) ] = {
  216. [ C(RESULT_ACCESS) ] = PM_L1_PREF,
  217. [ C(RESULT_MISS) ] = 0,
  218. },
  219. },
  220. [ C(L1I) ] = {
  221. [ C(OP_READ) ] = {
  222. [ C(RESULT_ACCESS) ] = PM_INST_FROM_L1,
  223. [ C(RESULT_MISS) ] = PM_L1_ICACHE_MISS,
  224. },
  225. [ C(OP_WRITE) ] = {
  226. [ C(RESULT_ACCESS) ] = PM_L1_DEMAND_WRITE,
  227. [ C(RESULT_MISS) ] = -1,
  228. },
  229. [ C(OP_PREFETCH) ] = {
  230. [ C(RESULT_ACCESS) ] = PM_IC_PREF_WRITE,
  231. [ C(RESULT_MISS) ] = 0,
  232. },
  233. },
  234. [ C(LL) ] = {
  235. [ C(OP_READ) ] = {
  236. [ C(RESULT_ACCESS) ] = PM_DATA_FROM_L3,
  237. [ C(RESULT_MISS) ] = PM_DATA_FROM_L3MISS,
  238. },
  239. [ C(OP_WRITE) ] = {
  240. [ C(RESULT_ACCESS) ] = PM_L2_ST,
  241. [ C(RESULT_MISS) ] = PM_L2_ST_MISS,
  242. },
  243. [ C(OP_PREFETCH) ] = {
  244. [ C(RESULT_ACCESS) ] = PM_L3_PREF_ALL,
  245. [ C(RESULT_MISS) ] = 0,
  246. },
  247. },
  248. [ C(DTLB) ] = {
  249. [ C(OP_READ) ] = {
  250. [ C(RESULT_ACCESS) ] = 0,
  251. [ C(RESULT_MISS) ] = PM_DTLB_MISS,
  252. },
  253. [ C(OP_WRITE) ] = {
  254. [ C(RESULT_ACCESS) ] = -1,
  255. [ C(RESULT_MISS) ] = -1,
  256. },
  257. [ C(OP_PREFETCH) ] = {
  258. [ C(RESULT_ACCESS) ] = -1,
  259. [ C(RESULT_MISS) ] = -1,
  260. },
  261. },
  262. [ C(ITLB) ] = {
  263. [ C(OP_READ) ] = {
  264. [ C(RESULT_ACCESS) ] = 0,
  265. [ C(RESULT_MISS) ] = PM_ITLB_MISS,
  266. },
  267. [ C(OP_WRITE) ] = {
  268. [ C(RESULT_ACCESS) ] = -1,
  269. [ C(RESULT_MISS) ] = -1,
  270. },
  271. [ C(OP_PREFETCH) ] = {
  272. [ C(RESULT_ACCESS) ] = -1,
  273. [ C(RESULT_MISS) ] = -1,
  274. },
  275. },
  276. [ C(BPU) ] = {
  277. [ C(OP_READ) ] = {
  278. [ C(RESULT_ACCESS) ] = PM_BRU_FIN,
  279. [ C(RESULT_MISS) ] = PM_BR_MPRED_CMPL,
  280. },
  281. [ C(OP_WRITE) ] = {
  282. [ C(RESULT_ACCESS) ] = -1,
  283. [ C(RESULT_MISS) ] = -1,
  284. },
  285. [ C(OP_PREFETCH) ] = {
  286. [ C(RESULT_ACCESS) ] = -1,
  287. [ C(RESULT_MISS) ] = -1,
  288. },
  289. },
  290. [ C(NODE) ] = {
  291. [ C(OP_READ) ] = {
  292. [ C(RESULT_ACCESS) ] = -1,
  293. [ C(RESULT_MISS) ] = -1,
  294. },
  295. [ C(OP_WRITE) ] = {
  296. [ C(RESULT_ACCESS) ] = -1,
  297. [ C(RESULT_MISS) ] = -1,
  298. },
  299. [ C(OP_PREFETCH) ] = {
  300. [ C(RESULT_ACCESS) ] = -1,
  301. [ C(RESULT_MISS) ] = -1,
  302. },
  303. },
  304. };
  305. #undef C
  306. static struct power_pmu power8_pmu = {
  307. .name = "POWER8",
  308. .n_counter = MAX_PMU_COUNTERS,
  309. .max_alternatives = MAX_ALT + 1,
  310. .add_fields = ISA207_ADD_FIELDS,
  311. .test_adder = ISA207_TEST_ADDER,
  312. .compute_mmcr = isa207_compute_mmcr,
  313. .config_bhrb = power8_config_bhrb,
  314. .bhrb_filter_map = power8_bhrb_filter_map,
  315. .get_constraint = isa207_get_constraint,
  316. .get_alternatives = power8_get_alternatives,
  317. .disable_pmc = isa207_disable_pmc,
  318. .flags = PPMU_HAS_SIER | PPMU_ARCH_207S,
  319. .n_generic = ARRAY_SIZE(power8_generic_events),
  320. .generic_events = power8_generic_events,
  321. .cache_events = &power8_cache_events,
  322. .attr_groups = power8_pmu_attr_groups,
  323. .bhrb_nr = 32,
  324. };
  325. static int __init init_power8_pmu(void)
  326. {
  327. int rc;
  328. if (!cur_cpu_spec->oprofile_cpu_type ||
  329. strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power8"))
  330. return -ENODEV;
  331. rc = register_power_pmu(&power8_pmu);
  332. if (rc)
  333. return rc;
  334. /* Tell userspace that EBB is supported */
  335. cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_EBB;
  336. if (cpu_has_feature(CPU_FTR_PMAO_BUG))
  337. pr_info("PMAO restore workaround active.\n");
  338. return 0;
  339. }
  340. early_initcall(init_power8_pmu);