power8-pmu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. /*
  27. * Raw event encoding for PowerISA v2.07 (Power8):
  28. *
  29. * 60 56 52 48 44 40 36 32
  30. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  31. * | | [ ] [ thresh_cmp ] [ thresh_ctl ]
  32. * | | | |
  33. * | | *- IFM (Linux) thresh start/stop OR FAB match -*
  34. * | *- BHRB (Linux)
  35. * *- EBB (Linux)
  36. *
  37. * 28 24 20 16 12 8 4 0
  38. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  39. * [ ] [ sample ] [cache] [ pmc ] [unit ] c m [ pmcxsel ]
  40. * | | | | |
  41. * | | | | *- mark
  42. * | | *- L1/L2/L3 cache_sel |
  43. * | | |
  44. * | *- sampling mode for marked events *- combine
  45. * |
  46. * *- thresh_sel
  47. *
  48. * Below uses IBM bit numbering.
  49. *
  50. * MMCR1[x:y] = unit (PMCxUNIT)
  51. * MMCR1[x] = combine (PMCxCOMB)
  52. *
  53. * if pmc == 3 and unit == 0 and pmcxsel[0:6] == 0b0101011
  54. * # PM_MRK_FAB_RSP_MATCH
  55. * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
  56. * else if pmc == 4 and unit == 0xf and pmcxsel[0:6] == 0b0101001
  57. * # PM_MRK_FAB_RSP_MATCH_CYC
  58. * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
  59. * else
  60. * MMCRA[48:55] = thresh_ctl (THRESH START/END)
  61. *
  62. * if thresh_sel:
  63. * MMCRA[45:47] = thresh_sel
  64. *
  65. * if thresh_cmp:
  66. * MMCRA[22:24] = thresh_cmp[0:2]
  67. * MMCRA[25:31] = thresh_cmp[3:9]
  68. *
  69. * if unit == 6 or unit == 7
  70. * MMCRC[53:55] = cache_sel[1:3] (L2EVENT_SEL)
  71. * else if unit == 8 or unit == 9:
  72. * if cache_sel[0] == 0: # L3 bank
  73. * MMCRC[47:49] = cache_sel[1:3] (L3EVENT_SEL0)
  74. * else if cache_sel[0] == 1:
  75. * MMCRC[50:51] = cache_sel[2:3] (L3EVENT_SEL1)
  76. * else if cache_sel[1]: # L1 event
  77. * MMCR1[16] = cache_sel[2]
  78.  * MMCR1[17] = cache_sel[3]
  79. *
  80. * if mark:
  81. * MMCRA[63] = 1 (SAMPLE_ENABLE)
  82. * MMCRA[57:59] = sample[0:2] (RAND_SAMP_ELIG)
  83.  * MMCRA[61:62] = sample[3:4] (RAND_SAMP_MODE)
  84. *
  85. * if EBB and BHRB:
  86. * MMCRA[32:33] = IFM
  87. *
  88. */
  89. /* PowerISA v2.07 format attribute structure*/
  90. extern struct attribute_group isa207_pmu_format_group;
  91. /* Table of alternatives, sorted by column 0 */
  92. static const unsigned int event_alternatives[][MAX_ALT] = {
  93. { PM_MRK_ST_CMPL, PM_MRK_ST_CMPL_ALT },
  94. { PM_BR_MRK_2PATH, PM_BR_MRK_2PATH_ALT },
  95. { PM_L3_CO_MEPF, PM_L3_CO_MEPF_ALT },
  96. { PM_MRK_DATA_FROM_L2MISS, PM_MRK_DATA_FROM_L2MISS_ALT },
  97. { PM_CMPLU_STALL_ALT, PM_CMPLU_STALL },
  98. { PM_BR_2PATH, PM_BR_2PATH_ALT },
  99. { PM_INST_DISP, PM_INST_DISP_ALT },
  100. { PM_RUN_CYC_ALT, PM_RUN_CYC },
  101. { PM_MRK_FILT_MATCH, PM_MRK_FILT_MATCH_ALT },
  102. { PM_LD_MISS_L1, PM_LD_MISS_L1_ALT },
  103. { PM_RUN_INST_CMPL_ALT, PM_RUN_INST_CMPL },
  104. };
  105. static int power8_get_alternatives(u64 event, unsigned int flags, u64 alt[])
  106. {
  107. int num_alt = 0;
  108. num_alt = isa207_get_alternatives(event, alt,
  109. ARRAY_SIZE(event_alternatives), flags,
  110. event_alternatives);
  111. return num_alt;
  112. }
  113. GENERIC_EVENT_ATTR(cpu-cycles, PM_CYC);
  114. GENERIC_EVENT_ATTR(stalled-cycles-frontend, PM_GCT_NOSLOT_CYC);
  115. GENERIC_EVENT_ATTR(stalled-cycles-backend, PM_CMPLU_STALL);
  116. GENERIC_EVENT_ATTR(instructions, PM_INST_CMPL);
  117. GENERIC_EVENT_ATTR(branch-instructions, PM_BRU_FIN);
  118. GENERIC_EVENT_ATTR(branch-misses, PM_BR_MPRED_CMPL);
  119. GENERIC_EVENT_ATTR(cache-references, PM_LD_REF_L1);
  120. GENERIC_EVENT_ATTR(cache-misses, PM_LD_MISS_L1);
  121. GENERIC_EVENT_ATTR(mem_access, MEM_ACCESS);
  122. CACHE_EVENT_ATTR(L1-dcache-load-misses, PM_LD_MISS_L1);
  123. CACHE_EVENT_ATTR(L1-dcache-loads, PM_LD_REF_L1);
  124. CACHE_EVENT_ATTR(L1-dcache-prefetches, PM_L1_PREF);
  125. CACHE_EVENT_ATTR(L1-dcache-store-misses, PM_ST_MISS_L1);
  126. CACHE_EVENT_ATTR(L1-icache-load-misses, PM_L1_ICACHE_MISS);
  127. CACHE_EVENT_ATTR(L1-icache-loads, PM_INST_FROM_L1);
  128. CACHE_EVENT_ATTR(L1-icache-prefetches, PM_IC_PREF_WRITE);
  129. CACHE_EVENT_ATTR(LLC-load-misses, PM_DATA_FROM_L3MISS);
  130. CACHE_EVENT_ATTR(LLC-loads, PM_DATA_FROM_L3);
  131. CACHE_EVENT_ATTR(LLC-prefetches, PM_L3_PREF_ALL);
  132. CACHE_EVENT_ATTR(LLC-store-misses, PM_L2_ST_MISS);
  133. CACHE_EVENT_ATTR(LLC-stores, PM_L2_ST);
  134. CACHE_EVENT_ATTR(branch-load-misses, PM_BR_MPRED_CMPL);
  135. CACHE_EVENT_ATTR(branch-loads, PM_BRU_FIN);
  136. CACHE_EVENT_ATTR(dTLB-load-misses, PM_DTLB_MISS);
  137. CACHE_EVENT_ATTR(iTLB-load-misses, PM_ITLB_MISS);
  138. static struct attribute *power8_events_attr[] = {
  139. GENERIC_EVENT_PTR(PM_CYC),
  140. GENERIC_EVENT_PTR(PM_GCT_NOSLOT_CYC),
  141. GENERIC_EVENT_PTR(PM_CMPLU_STALL),
  142. GENERIC_EVENT_PTR(PM_INST_CMPL),
  143. GENERIC_EVENT_PTR(PM_BRU_FIN),
  144. GENERIC_EVENT_PTR(PM_BR_MPRED_CMPL),
  145. GENERIC_EVENT_PTR(PM_LD_REF_L1),
  146. GENERIC_EVENT_PTR(PM_LD_MISS_L1),
  147. GENERIC_EVENT_PTR(MEM_ACCESS),
  148. CACHE_EVENT_PTR(PM_LD_MISS_L1),
  149. CACHE_EVENT_PTR(PM_LD_REF_L1),
  150. CACHE_EVENT_PTR(PM_L1_PREF),
  151. CACHE_EVENT_PTR(PM_ST_MISS_L1),
  152. CACHE_EVENT_PTR(PM_L1_ICACHE_MISS),
  153. CACHE_EVENT_PTR(PM_INST_FROM_L1),
  154. CACHE_EVENT_PTR(PM_IC_PREF_WRITE),
  155. CACHE_EVENT_PTR(PM_DATA_FROM_L3MISS),
  156. CACHE_EVENT_PTR(PM_DATA_FROM_L3),
  157. CACHE_EVENT_PTR(PM_L3_PREF_ALL),
  158. CACHE_EVENT_PTR(PM_L2_ST_MISS),
  159. CACHE_EVENT_PTR(PM_L2_ST),
  160. CACHE_EVENT_PTR(PM_BR_MPRED_CMPL),
  161. CACHE_EVENT_PTR(PM_BRU_FIN),
  162. CACHE_EVENT_PTR(PM_DTLB_MISS),
  163. CACHE_EVENT_PTR(PM_ITLB_MISS),
  164. NULL
  165. };
  166. static struct attribute_group power8_pmu_events_group = {
  167. .name = "events",
  168. .attrs = power8_events_attr,
  169. };
  170. static const struct attribute_group *power8_pmu_attr_groups[] = {
  171. &isa207_pmu_format_group,
  172. &power8_pmu_events_group,
  173. NULL,
  174. };
  175. static int power8_generic_events[] = {
  176. [PERF_COUNT_HW_CPU_CYCLES] = PM_CYC,
  177. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = PM_GCT_NOSLOT_CYC,
  178. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = PM_CMPLU_STALL,
  179. [PERF_COUNT_HW_INSTRUCTIONS] = PM_INST_CMPL,
  180. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = PM_BRU_FIN,
  181. [PERF_COUNT_HW_BRANCH_MISSES] = PM_BR_MPRED_CMPL,
  182. [PERF_COUNT_HW_CACHE_REFERENCES] = PM_LD_REF_L1,
  183. [PERF_COUNT_HW_CACHE_MISSES] = PM_LD_MISS_L1,
  184. };
  185. static u64 power8_bhrb_filter_map(u64 branch_sample_type)
  186. {
  187. u64 pmu_bhrb_filter = 0;
  188. /* BHRB and regular PMU events share the same privilege state
  189. * filter configuration. BHRB is always recorded along with a
  190. * regular PMU event. As the privilege state filter is handled
  191. * in the basic PMC configuration of the accompanying regular
  192. * PMU event, we ignore any separate BHRB specific request.
  193. */
  194. /* No branch filter requested */
  195. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY)
  196. return pmu_bhrb_filter;
  197. /* Invalid branch filter options - HW does not support */
  198. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
  199. return -1;
  200. if (branch_sample_type & PERF_SAMPLE_BRANCH_IND_CALL)
  201. return -1;
  202. if (branch_sample_type & PERF_SAMPLE_BRANCH_CALL)
  203. return -1;
  204. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_CALL) {
  205. pmu_bhrb_filter |= POWER8_MMCRA_IFM1;
  206. return pmu_bhrb_filter;
  207. }
  208. /* Every thing else is unsupported */
  209. return -1;
  210. }
  211. static void power8_config_bhrb(u64 pmu_bhrb_filter)
  212. {
  213. /* Enable BHRB filter in PMU */
  214. mtspr(SPRN_MMCRA, (mfspr(SPRN_MMCRA) | pmu_bhrb_filter));
  215. }
  216. #define C(x) PERF_COUNT_HW_CACHE_##x
  217. /*
  218. * Table of generalized cache-related events.
  219. * 0 means not supported, -1 means nonsensical, other values
  220. * are event codes.
  221. */
  222. static int power8_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
  223. [ C(L1D) ] = {
  224. [ C(OP_READ) ] = {
  225. [ C(RESULT_ACCESS) ] = PM_LD_REF_L1,
  226. [ C(RESULT_MISS) ] = PM_LD_MISS_L1,
  227. },
  228. [ C(OP_WRITE) ] = {
  229. [ C(RESULT_ACCESS) ] = 0,
  230. [ C(RESULT_MISS) ] = PM_ST_MISS_L1,
  231. },
  232. [ C(OP_PREFETCH) ] = {
  233. [ C(RESULT_ACCESS) ] = PM_L1_PREF,
  234. [ C(RESULT_MISS) ] = 0,
  235. },
  236. },
  237. [ C(L1I) ] = {
  238. [ C(OP_READ) ] = {
  239. [ C(RESULT_ACCESS) ] = PM_INST_FROM_L1,
  240. [ C(RESULT_MISS) ] = PM_L1_ICACHE_MISS,
  241. },
  242. [ C(OP_WRITE) ] = {
  243. [ C(RESULT_ACCESS) ] = PM_L1_DEMAND_WRITE,
  244. [ C(RESULT_MISS) ] = -1,
  245. },
  246. [ C(OP_PREFETCH) ] = {
  247. [ C(RESULT_ACCESS) ] = PM_IC_PREF_WRITE,
  248. [ C(RESULT_MISS) ] = 0,
  249. },
  250. },
  251. [ C(LL) ] = {
  252. [ C(OP_READ) ] = {
  253. [ C(RESULT_ACCESS) ] = PM_DATA_FROM_L3,
  254. [ C(RESULT_MISS) ] = PM_DATA_FROM_L3MISS,
  255. },
  256. [ C(OP_WRITE) ] = {
  257. [ C(RESULT_ACCESS) ] = PM_L2_ST,
  258. [ C(RESULT_MISS) ] = PM_L2_ST_MISS,
  259. },
  260. [ C(OP_PREFETCH) ] = {
  261. [ C(RESULT_ACCESS) ] = PM_L3_PREF_ALL,
  262. [ C(RESULT_MISS) ] = 0,
  263. },
  264. },
  265. [ C(DTLB) ] = {
  266. [ C(OP_READ) ] = {
  267. [ C(RESULT_ACCESS) ] = 0,
  268. [ C(RESULT_MISS) ] = PM_DTLB_MISS,
  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. [ C(ITLB) ] = {
  280. [ C(OP_READ) ] = {
  281. [ C(RESULT_ACCESS) ] = 0,
  282. [ C(RESULT_MISS) ] = PM_ITLB_MISS,
  283. },
  284. [ C(OP_WRITE) ] = {
  285. [ C(RESULT_ACCESS) ] = -1,
  286. [ C(RESULT_MISS) ] = -1,
  287. },
  288. [ C(OP_PREFETCH) ] = {
  289. [ C(RESULT_ACCESS) ] = -1,
  290. [ C(RESULT_MISS) ] = -1,
  291. },
  292. },
  293. [ C(BPU) ] = {
  294. [ C(OP_READ) ] = {
  295. [ C(RESULT_ACCESS) ] = PM_BRU_FIN,
  296. [ C(RESULT_MISS) ] = PM_BR_MPRED_CMPL,
  297. },
  298. [ C(OP_WRITE) ] = {
  299. [ C(RESULT_ACCESS) ] = -1,
  300. [ C(RESULT_MISS) ] = -1,
  301. },
  302. [ C(OP_PREFETCH) ] = {
  303. [ C(RESULT_ACCESS) ] = -1,
  304. [ C(RESULT_MISS) ] = -1,
  305. },
  306. },
  307. [ C(NODE) ] = {
  308. [ C(OP_READ) ] = {
  309. [ C(RESULT_ACCESS) ] = -1,
  310. [ C(RESULT_MISS) ] = -1,
  311. },
  312. [ C(OP_WRITE) ] = {
  313. [ C(RESULT_ACCESS) ] = -1,
  314. [ C(RESULT_MISS) ] = -1,
  315. },
  316. [ C(OP_PREFETCH) ] = {
  317. [ C(RESULT_ACCESS) ] = -1,
  318. [ C(RESULT_MISS) ] = -1,
  319. },
  320. },
  321. };
  322. #undef C
  323. static struct power_pmu power8_pmu = {
  324. .name = "POWER8",
  325. .n_counter = MAX_PMU_COUNTERS,
  326. .max_alternatives = MAX_ALT + 1,
  327. .add_fields = ISA207_ADD_FIELDS,
  328. .test_adder = ISA207_TEST_ADDER,
  329. .compute_mmcr = isa207_compute_mmcr,
  330. .config_bhrb = power8_config_bhrb,
  331. .bhrb_filter_map = power8_bhrb_filter_map,
  332. .get_constraint = isa207_get_constraint,
  333. .get_alternatives = power8_get_alternatives,
  334. .get_mem_data_src = isa207_get_mem_data_src,
  335. .get_mem_weight = isa207_get_mem_weight,
  336. .disable_pmc = isa207_disable_pmc,
  337. .flags = PPMU_HAS_SIER | PPMU_ARCH_207S,
  338. .n_generic = ARRAY_SIZE(power8_generic_events),
  339. .generic_events = power8_generic_events,
  340. .cache_events = &power8_cache_events,
  341. .attr_groups = power8_pmu_attr_groups,
  342. .bhrb_nr = 32,
  343. };
  344. static int __init init_power8_pmu(void)
  345. {
  346. int rc;
  347. if (!cur_cpu_spec->oprofile_cpu_type ||
  348. strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power8"))
  349. return -ENODEV;
  350. rc = register_power_pmu(&power8_pmu);
  351. if (rc)
  352. return rc;
  353. /* Tell userspace that EBB is supported */
  354. cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_EBB;
  355. if (cpu_has_feature(CPU_FTR_PMAO_BUG))
  356. pr_info("PMAO restore workaround active.\n");
  357. return 0;
  358. }
  359. early_initcall(init_power8_pmu);