power9-pmu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Performance counter support for POWER9 processors.
  3. *
  4. * Copyright 2009 Paul Mackerras, IBM Corporation.
  5. * Copyright 2013 Michael Ellerman, IBM Corporation.
  6. * Copyright 2016 Madhavan Srinivasan, IBM Corporation.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or later version.
  12. */
  13. #define pr_fmt(fmt) "power9-pmu: " fmt
  14. #include "isa207-common.h"
  15. /*
  16. * Raw event encoding for Power9:
  17. *
  18. * 60 56 52 48 44 40 36 32
  19. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  20. * | | [ ] [ ] [ thresh_cmp ] [ thresh_ctl ]
  21. * | | | | |
  22. * | | *- IFM (Linux) | thresh start/stop OR FAB match -*
  23. * | *- BHRB (Linux) *sm
  24. * *- EBB (Linux)
  25. *
  26. * 28 24 20 16 12 8 4 0
  27. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  28. * [ ] [ sample ] [cache] [ pmc ] [unit ] [] m [ pmcxsel ]
  29. * | | | | |
  30. * | | | | *- mark
  31. * | | *- L1/L2/L3 cache_sel |
  32. * | | |
  33. * | *- sampling mode for marked events *- combine
  34. * |
  35. * *- thresh_sel
  36. *
  37. * Below uses IBM bit numbering.
  38. *
  39. * MMCR1[x:y] = unit (PMCxUNIT)
  40. * MMCR1[24] = pmc1combine[0]
  41. * MMCR1[25] = pmc1combine[1]
  42. * MMCR1[26] = pmc2combine[0]
  43. * MMCR1[27] = pmc2combine[1]
  44. * MMCR1[28] = pmc3combine[0]
  45. * MMCR1[29] = pmc3combine[1]
  46. * MMCR1[30] = pmc4combine[0]
  47. * MMCR1[31] = pmc4combine[1]
  48. *
  49. * if pmc == 3 and unit == 0 and pmcxsel[0:6] == 0b0101011
  50. * # PM_MRK_FAB_RSP_MATCH
  51. * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
  52. * else if pmc == 4 and unit == 0xf and pmcxsel[0:6] == 0b0101001
  53. * # PM_MRK_FAB_RSP_MATCH_CYC
  54. * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
  55. * else
  56. * MMCRA[48:55] = thresh_ctl (THRESH START/END)
  57. *
  58. * if thresh_sel:
  59. * MMCRA[45:47] = thresh_sel
  60. *
  61. * if thresh_cmp:
  62. * MMCRA[9:11] = thresh_cmp[0:2]
  63. * MMCRA[12:18] = thresh_cmp[3:9]
  64. *
  65. * if unit == 6 or unit == 7
  66. * MMCRC[53:55] = cache_sel[1:3] (L2EVENT_SEL)
  67. * else if unit == 8 or unit == 9:
  68. * if cache_sel[0] == 0: # L3 bank
  69. * MMCRC[47:49] = cache_sel[1:3] (L3EVENT_SEL0)
  70. * else if cache_sel[0] == 1:
  71. * MMCRC[50:51] = cache_sel[2:3] (L3EVENT_SEL1)
  72. * else if cache_sel[1]: # L1 event
  73. * MMCR1[16] = cache_sel[2]
  74.  * MMCR1[17] = cache_sel[3]
  75. *
  76. * if mark:
  77. * MMCRA[63] = 1 (SAMPLE_ENABLE)
  78. * MMCRA[57:59] = sample[0:2] (RAND_SAMP_ELIG)
  79.  * MMCRA[61:62] = sample[3:4] (RAND_SAMP_MODE)
  80. *
  81. * if EBB and BHRB:
  82. * MMCRA[32:33] = IFM
  83. *
  84. * MMCRA[SDAR_MODE] = sm
  85. */
  86. /*
  87. * Some power9 event codes.
  88. */
  89. #define EVENT(_name, _code) _name = _code,
  90. enum {
  91. #include "power9-events-list.h"
  92. };
  93. #undef EVENT
  94. /* MMCRA IFM bits - POWER9 */
  95. #define POWER9_MMCRA_IFM1 0x0000000040000000UL
  96. #define POWER9_MMCRA_IFM2 0x0000000080000000UL
  97. #define POWER9_MMCRA_IFM3 0x00000000C0000000UL
  98. /* PowerISA v2.07 format attribute structure*/
  99. extern struct attribute_group isa207_pmu_format_group;
  100. GENERIC_EVENT_ATTR(cpu-cycles, PM_CYC);
  101. GENERIC_EVENT_ATTR(stalled-cycles-frontend, PM_ICT_NOSLOT_CYC);
  102. GENERIC_EVENT_ATTR(stalled-cycles-backend, PM_CMPLU_STALL);
  103. GENERIC_EVENT_ATTR(instructions, PM_INST_CMPL);
  104. GENERIC_EVENT_ATTR(branch-instructions, PM_BRU_CMPL);
  105. GENERIC_EVENT_ATTR(branch-misses, PM_BR_MPRED_CMPL);
  106. GENERIC_EVENT_ATTR(cache-references, PM_LD_REF_L1);
  107. GENERIC_EVENT_ATTR(cache-misses, PM_LD_MISS_L1_FIN);
  108. CACHE_EVENT_ATTR(L1-dcache-load-misses, PM_LD_MISS_L1_FIN);
  109. CACHE_EVENT_ATTR(L1-dcache-loads, PM_LD_REF_L1);
  110. CACHE_EVENT_ATTR(L1-dcache-prefetches, PM_L1_PREF);
  111. CACHE_EVENT_ATTR(L1-dcache-store-misses, PM_ST_MISS_L1);
  112. CACHE_EVENT_ATTR(L1-icache-load-misses, PM_L1_ICACHE_MISS);
  113. CACHE_EVENT_ATTR(L1-icache-loads, PM_INST_FROM_L1);
  114. CACHE_EVENT_ATTR(L1-icache-prefetches, PM_IC_PREF_WRITE);
  115. CACHE_EVENT_ATTR(LLC-load-misses, PM_DATA_FROM_L3MISS);
  116. CACHE_EVENT_ATTR(LLC-loads, PM_DATA_FROM_L3);
  117. CACHE_EVENT_ATTR(LLC-prefetches, PM_L3_PREF_ALL);
  118. CACHE_EVENT_ATTR(LLC-store-misses, PM_L2_ST_MISS);
  119. CACHE_EVENT_ATTR(LLC-stores, PM_L2_ST);
  120. CACHE_EVENT_ATTR(branch-load-misses, PM_BR_MPRED_CMPL);
  121. CACHE_EVENT_ATTR(branch-loads, PM_BRU_CMPL);
  122. CACHE_EVENT_ATTR(dTLB-load-misses, PM_DTLB_MISS);
  123. CACHE_EVENT_ATTR(iTLB-load-misses, PM_ITLB_MISS);
  124. static struct attribute *power9_events_attr[] = {
  125. GENERIC_EVENT_PTR(PM_CYC),
  126. GENERIC_EVENT_PTR(PM_ICT_NOSLOT_CYC),
  127. GENERIC_EVENT_PTR(PM_CMPLU_STALL),
  128. GENERIC_EVENT_PTR(PM_INST_CMPL),
  129. GENERIC_EVENT_PTR(PM_BRU_CMPL),
  130. GENERIC_EVENT_PTR(PM_BR_MPRED_CMPL),
  131. GENERIC_EVENT_PTR(PM_LD_REF_L1),
  132. GENERIC_EVENT_PTR(PM_LD_MISS_L1_FIN),
  133. CACHE_EVENT_PTR(PM_LD_MISS_L1_FIN),
  134. CACHE_EVENT_PTR(PM_LD_REF_L1),
  135. CACHE_EVENT_PTR(PM_L1_PREF),
  136. CACHE_EVENT_PTR(PM_ST_MISS_L1),
  137. CACHE_EVENT_PTR(PM_L1_ICACHE_MISS),
  138. CACHE_EVENT_PTR(PM_INST_FROM_L1),
  139. CACHE_EVENT_PTR(PM_IC_PREF_WRITE),
  140. CACHE_EVENT_PTR(PM_DATA_FROM_L3MISS),
  141. CACHE_EVENT_PTR(PM_DATA_FROM_L3),
  142. CACHE_EVENT_PTR(PM_L3_PREF_ALL),
  143. CACHE_EVENT_PTR(PM_L2_ST_MISS),
  144. CACHE_EVENT_PTR(PM_L2_ST),
  145. CACHE_EVENT_PTR(PM_BR_MPRED_CMPL),
  146. CACHE_EVENT_PTR(PM_BRU_CMPL),
  147. CACHE_EVENT_PTR(PM_DTLB_MISS),
  148. CACHE_EVENT_PTR(PM_ITLB_MISS),
  149. NULL
  150. };
  151. static struct attribute_group power9_pmu_events_group = {
  152. .name = "events",
  153. .attrs = power9_events_attr,
  154. };
  155. static const struct attribute_group *power9_isa207_pmu_attr_groups[] = {
  156. &isa207_pmu_format_group,
  157. &power9_pmu_events_group,
  158. NULL,
  159. };
  160. PMU_FORMAT_ATTR(event, "config:0-51");
  161. PMU_FORMAT_ATTR(pmcxsel, "config:0-7");
  162. PMU_FORMAT_ATTR(mark, "config:8");
  163. PMU_FORMAT_ATTR(combine, "config:10-11");
  164. PMU_FORMAT_ATTR(unit, "config:12-15");
  165. PMU_FORMAT_ATTR(pmc, "config:16-19");
  166. PMU_FORMAT_ATTR(cache_sel, "config:20-23");
  167. PMU_FORMAT_ATTR(sample_mode, "config:24-28");
  168. PMU_FORMAT_ATTR(thresh_sel, "config:29-31");
  169. PMU_FORMAT_ATTR(thresh_stop, "config:32-35");
  170. PMU_FORMAT_ATTR(thresh_start, "config:36-39");
  171. PMU_FORMAT_ATTR(thresh_cmp, "config:40-49");
  172. PMU_FORMAT_ATTR(sdar_mode, "config:50-51");
  173. static struct attribute *power9_pmu_format_attr[] = {
  174. &format_attr_event.attr,
  175. &format_attr_pmcxsel.attr,
  176. &format_attr_mark.attr,
  177. &format_attr_combine.attr,
  178. &format_attr_unit.attr,
  179. &format_attr_pmc.attr,
  180. &format_attr_cache_sel.attr,
  181. &format_attr_sample_mode.attr,
  182. &format_attr_thresh_sel.attr,
  183. &format_attr_thresh_stop.attr,
  184. &format_attr_thresh_start.attr,
  185. &format_attr_thresh_cmp.attr,
  186. &format_attr_sdar_mode.attr,
  187. NULL,
  188. };
  189. static struct attribute_group power9_pmu_format_group = {
  190. .name = "format",
  191. .attrs = power9_pmu_format_attr,
  192. };
  193. static const struct attribute_group *power9_pmu_attr_groups[] = {
  194. &power9_pmu_format_group,
  195. &power9_pmu_events_group,
  196. NULL,
  197. };
  198. static int power9_generic_events[] = {
  199. [PERF_COUNT_HW_CPU_CYCLES] = PM_CYC,
  200. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = PM_ICT_NOSLOT_CYC,
  201. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = PM_CMPLU_STALL,
  202. [PERF_COUNT_HW_INSTRUCTIONS] = PM_INST_CMPL,
  203. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = PM_BRU_CMPL,
  204. [PERF_COUNT_HW_BRANCH_MISSES] = PM_BR_MPRED_CMPL,
  205. [PERF_COUNT_HW_CACHE_REFERENCES] = PM_LD_REF_L1,
  206. [PERF_COUNT_HW_CACHE_MISSES] = PM_LD_MISS_L1_FIN,
  207. };
  208. static u64 power9_bhrb_filter_map(u64 branch_sample_type)
  209. {
  210. u64 pmu_bhrb_filter = 0;
  211. /* BHRB and regular PMU events share the same privilege state
  212. * filter configuration. BHRB is always recorded along with a
  213. * regular PMU event. As the privilege state filter is handled
  214. * in the basic PMC configuration of the accompanying regular
  215. * PMU event, we ignore any separate BHRB specific request.
  216. */
  217. /* No branch filter requested */
  218. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY)
  219. return pmu_bhrb_filter;
  220. /* Invalid branch filter options - HW does not support */
  221. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
  222. return -1;
  223. if (branch_sample_type & PERF_SAMPLE_BRANCH_IND_CALL)
  224. return -1;
  225. if (branch_sample_type & PERF_SAMPLE_BRANCH_CALL)
  226. return -1;
  227. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_CALL) {
  228. pmu_bhrb_filter |= POWER9_MMCRA_IFM1;
  229. return pmu_bhrb_filter;
  230. }
  231. /* Every thing else is unsupported */
  232. return -1;
  233. }
  234. static void power9_config_bhrb(u64 pmu_bhrb_filter)
  235. {
  236. /* Enable BHRB filter in PMU */
  237. mtspr(SPRN_MMCRA, (mfspr(SPRN_MMCRA) | pmu_bhrb_filter));
  238. }
  239. #define C(x) PERF_COUNT_HW_CACHE_##x
  240. /*
  241. * Table of generalized cache-related events.
  242. * 0 means not supported, -1 means nonsensical, other values
  243. * are event codes.
  244. */
  245. static int power9_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
  246. [ C(L1D) ] = {
  247. [ C(OP_READ) ] = {
  248. [ C(RESULT_ACCESS) ] = PM_LD_REF_L1,
  249. [ C(RESULT_MISS) ] = PM_LD_MISS_L1_FIN,
  250. },
  251. [ C(OP_WRITE) ] = {
  252. [ C(RESULT_ACCESS) ] = 0,
  253. [ C(RESULT_MISS) ] = PM_ST_MISS_L1,
  254. },
  255. [ C(OP_PREFETCH) ] = {
  256. [ C(RESULT_ACCESS) ] = PM_L1_PREF,
  257. [ C(RESULT_MISS) ] = 0,
  258. },
  259. },
  260. [ C(L1I) ] = {
  261. [ C(OP_READ) ] = {
  262. [ C(RESULT_ACCESS) ] = PM_INST_FROM_L1,
  263. [ C(RESULT_MISS) ] = PM_L1_ICACHE_MISS,
  264. },
  265. [ C(OP_WRITE) ] = {
  266. [ C(RESULT_ACCESS) ] = PM_L1_DEMAND_WRITE,
  267. [ C(RESULT_MISS) ] = -1,
  268. },
  269. [ C(OP_PREFETCH) ] = {
  270. [ C(RESULT_ACCESS) ] = PM_IC_PREF_WRITE,
  271. [ C(RESULT_MISS) ] = 0,
  272. },
  273. },
  274. [ C(LL) ] = {
  275. [ C(OP_READ) ] = {
  276. [ C(RESULT_ACCESS) ] = PM_DATA_FROM_L3,
  277. [ C(RESULT_MISS) ] = PM_DATA_FROM_L3MISS,
  278. },
  279. [ C(OP_WRITE) ] = {
  280. [ C(RESULT_ACCESS) ] = PM_L2_ST,
  281. [ C(RESULT_MISS) ] = PM_L2_ST_MISS,
  282. },
  283. [ C(OP_PREFETCH) ] = {
  284. [ C(RESULT_ACCESS) ] = PM_L3_PREF_ALL,
  285. [ C(RESULT_MISS) ] = 0,
  286. },
  287. },
  288. [ C(DTLB) ] = {
  289. [ C(OP_READ) ] = {
  290. [ C(RESULT_ACCESS) ] = 0,
  291. [ C(RESULT_MISS) ] = PM_DTLB_MISS,
  292. },
  293. [ C(OP_WRITE) ] = {
  294. [ C(RESULT_ACCESS) ] = -1,
  295. [ C(RESULT_MISS) ] = -1,
  296. },
  297. [ C(OP_PREFETCH) ] = {
  298. [ C(RESULT_ACCESS) ] = -1,
  299. [ C(RESULT_MISS) ] = -1,
  300. },
  301. },
  302. [ C(ITLB) ] = {
  303. [ C(OP_READ) ] = {
  304. [ C(RESULT_ACCESS) ] = 0,
  305. [ C(RESULT_MISS) ] = PM_ITLB_MISS,
  306. },
  307. [ C(OP_WRITE) ] = {
  308. [ C(RESULT_ACCESS) ] = -1,
  309. [ C(RESULT_MISS) ] = -1,
  310. },
  311. [ C(OP_PREFETCH) ] = {
  312. [ C(RESULT_ACCESS) ] = -1,
  313. [ C(RESULT_MISS) ] = -1,
  314. },
  315. },
  316. [ C(BPU) ] = {
  317. [ C(OP_READ) ] = {
  318. [ C(RESULT_ACCESS) ] = PM_BRU_CMPL,
  319. [ C(RESULT_MISS) ] = PM_BR_MPRED_CMPL,
  320. },
  321. [ C(OP_WRITE) ] = {
  322. [ C(RESULT_ACCESS) ] = -1,
  323. [ C(RESULT_MISS) ] = -1,
  324. },
  325. [ C(OP_PREFETCH) ] = {
  326. [ C(RESULT_ACCESS) ] = -1,
  327. [ C(RESULT_MISS) ] = -1,
  328. },
  329. },
  330. [ C(NODE) ] = {
  331. [ C(OP_READ) ] = {
  332. [ C(RESULT_ACCESS) ] = -1,
  333. [ C(RESULT_MISS) ] = -1,
  334. },
  335. [ C(OP_WRITE) ] = {
  336. [ C(RESULT_ACCESS) ] = -1,
  337. [ C(RESULT_MISS) ] = -1,
  338. },
  339. [ C(OP_PREFETCH) ] = {
  340. [ C(RESULT_ACCESS) ] = -1,
  341. [ C(RESULT_MISS) ] = -1,
  342. },
  343. },
  344. };
  345. #undef C
  346. static struct power_pmu power9_isa207_pmu = {
  347. .name = "POWER9",
  348. .n_counter = MAX_PMU_COUNTERS,
  349. .add_fields = ISA207_ADD_FIELDS,
  350. .test_adder = ISA207_TEST_ADDER,
  351. .compute_mmcr = isa207_compute_mmcr,
  352. .config_bhrb = power9_config_bhrb,
  353. .bhrb_filter_map = power9_bhrb_filter_map,
  354. .get_constraint = isa207_get_constraint,
  355. .disable_pmc = isa207_disable_pmc,
  356. .flags = PPMU_NO_SIAR | PPMU_ARCH_207S,
  357. .n_generic = ARRAY_SIZE(power9_generic_events),
  358. .generic_events = power9_generic_events,
  359. .cache_events = &power9_cache_events,
  360. .attr_groups = power9_isa207_pmu_attr_groups,
  361. .bhrb_nr = 32,
  362. };
  363. static struct power_pmu power9_pmu = {
  364. .name = "POWER9",
  365. .n_counter = MAX_PMU_COUNTERS,
  366. .add_fields = ISA207_ADD_FIELDS,
  367. .test_adder = ISA207_TEST_ADDER,
  368. .compute_mmcr = isa207_compute_mmcr,
  369. .config_bhrb = power9_config_bhrb,
  370. .bhrb_filter_map = power9_bhrb_filter_map,
  371. .get_constraint = isa207_get_constraint,
  372. .disable_pmc = isa207_disable_pmc,
  373. .flags = PPMU_HAS_SIER | PPMU_ARCH_207S,
  374. .n_generic = ARRAY_SIZE(power9_generic_events),
  375. .generic_events = power9_generic_events,
  376. .cache_events = &power9_cache_events,
  377. .attr_groups = power9_pmu_attr_groups,
  378. .bhrb_nr = 32,
  379. };
  380. static int __init init_power9_pmu(void)
  381. {
  382. int rc = 0;
  383. /* Comes from cpu_specs[] */
  384. if (!cur_cpu_spec->oprofile_cpu_type ||
  385. strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power9"))
  386. return -ENODEV;
  387. if (cpu_has_feature(CPU_FTR_POWER9_DD1)) {
  388. rc = register_power_pmu(&power9_isa207_pmu);
  389. } else {
  390. rc = register_power_pmu(&power9_pmu);
  391. }
  392. if (rc)
  393. return rc;
  394. /* Tell userspace that EBB is supported */
  395. cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_EBB;
  396. return 0;
  397. }
  398. early_initcall(init_power9_pmu);