power8-pmu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. #include <linux/kernel.h>
  13. #include <linux/perf_event.h>
  14. #include <asm/firmware.h>
  15. /*
  16. * Some power8 event codes.
  17. */
  18. #define PM_CYC 0x0001e
  19. #define PM_GCT_NOSLOT_CYC 0x100f8
  20. #define PM_CMPLU_STALL 0x4000a
  21. #define PM_INST_CMPL 0x00002
  22. #define PM_BRU_FIN 0x10068
  23. #define PM_BR_MPRED_CMPL 0x400f6
  24. /*
  25. * Raw event encoding for POWER8:
  26. *
  27. * 60 56 52 48 44 40 36 32
  28. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  29. * | [ thresh_cmp ] [ thresh_ctl ]
  30. * | |
  31. * *- EBB (Linux) thresh start/stop OR FAB match -*
  32. *
  33. * 28 24 20 16 12 8 4 0
  34. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  35. * [ ] [ sample ] [cache] [ pmc ] [unit ] c m [ pmcxsel ]
  36. * | | | | |
  37. * | | | | *- mark
  38. * | | *- L1/L2/L3 cache_sel |
  39. * | | |
  40. * | *- sampling mode for marked events *- combine
  41. * |
  42. * *- thresh_sel
  43. *
  44. * Below uses IBM bit numbering.
  45. *
  46. * MMCR1[x:y] = unit (PMCxUNIT)
  47. * MMCR1[x] = combine (PMCxCOMB)
  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[22:24] = thresh_cmp[0:2]
  63. * MMCRA[25:31] = 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. */
  82. #define EVENT_EBB_MASK 1ull
  83. #define EVENT_THR_CMP_SHIFT 40 /* Threshold CMP value */
  84. #define EVENT_THR_CMP_MASK 0x3ff
  85. #define EVENT_THR_CTL_SHIFT 32 /* Threshold control value (start/stop) */
  86. #define EVENT_THR_CTL_MASK 0xffull
  87. #define EVENT_THR_SEL_SHIFT 29 /* Threshold select value */
  88. #define EVENT_THR_SEL_MASK 0x7
  89. #define EVENT_THRESH_SHIFT 29 /* All threshold bits */
  90. #define EVENT_THRESH_MASK 0x1fffffull
  91. #define EVENT_SAMPLE_SHIFT 24 /* Sampling mode & eligibility */
  92. #define EVENT_SAMPLE_MASK 0x1f
  93. #define EVENT_CACHE_SEL_SHIFT 20 /* L2/L3 cache select */
  94. #define EVENT_CACHE_SEL_MASK 0xf
  95. #define EVENT_IS_L1 (4 << EVENT_CACHE_SEL_SHIFT)
  96. #define EVENT_PMC_SHIFT 16 /* PMC number (1-based) */
  97. #define EVENT_PMC_MASK 0xf
  98. #define EVENT_UNIT_SHIFT 12 /* Unit */
  99. #define EVENT_UNIT_MASK 0xf
  100. #define EVENT_COMBINE_SHIFT 11 /* Combine bit */
  101. #define EVENT_COMBINE_MASK 0x1
  102. #define EVENT_MARKED_SHIFT 8 /* Marked bit */
  103. #define EVENT_MARKED_MASK 0x1
  104. #define EVENT_IS_MARKED (EVENT_MARKED_MASK << EVENT_MARKED_SHIFT)
  105. #define EVENT_PSEL_MASK 0xff /* PMCxSEL value */
  106. #define EVENT_VALID_MASK \
  107. ((EVENT_THRESH_MASK << EVENT_THRESH_SHIFT) | \
  108. (EVENT_SAMPLE_MASK << EVENT_SAMPLE_SHIFT) | \
  109. (EVENT_CACHE_SEL_MASK << EVENT_CACHE_SEL_SHIFT) | \
  110. (EVENT_PMC_MASK << EVENT_PMC_SHIFT) | \
  111. (EVENT_UNIT_MASK << EVENT_UNIT_SHIFT) | \
  112. (EVENT_COMBINE_MASK << EVENT_COMBINE_SHIFT) | \
  113. (EVENT_MARKED_MASK << EVENT_MARKED_SHIFT) | \
  114. (EVENT_EBB_MASK << PERF_EVENT_CONFIG_EBB_SHIFT) | \
  115. EVENT_PSEL_MASK)
  116. /* MMCRA IFM bits - POWER8 */
  117. #define POWER8_MMCRA_IFM1 0x0000000040000000UL
  118. #define POWER8_MMCRA_IFM2 0x0000000080000000UL
  119. #define POWER8_MMCRA_IFM3 0x00000000C0000000UL
  120. #define ONLY_PLM \
  121. (PERF_SAMPLE_BRANCH_USER |\
  122. PERF_SAMPLE_BRANCH_KERNEL |\
  123. PERF_SAMPLE_BRANCH_HV)
  124. /*
  125. * Layout of constraint bits:
  126. *
  127. * 60 56 52 48 44 40 36 32
  128. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  129. * [ fab_match ] [ thresh_cmp ] [ thresh_ctl ] [ ]
  130. * |
  131. * thresh_sel -*
  132. *
  133. * 28 24 20 16 12 8 4 0
  134. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  135. * | [ ] [ sample ] [ ] [6] [5] [4] [3] [2] [1]
  136. * EBB -* | |
  137. * | | Count of events for each PMC.
  138. * L1 I/D qualifier -* | p1, p2, p3, p4, p5, p6.
  139. * nc - number of counters -*
  140. *
  141. * The PMC fields P1..P6, and NC, are adder fields. As we accumulate constraints
  142. * we want the low bit of each field to be added to any existing value.
  143. *
  144. * Everything else is a value field.
  145. */
  146. #define CNST_FAB_MATCH_VAL(v) (((v) & EVENT_THR_CTL_MASK) << 56)
  147. #define CNST_FAB_MATCH_MASK CNST_FAB_MATCH_VAL(EVENT_THR_CTL_MASK)
  148. /* We just throw all the threshold bits into the constraint */
  149. #define CNST_THRESH_VAL(v) (((v) & EVENT_THRESH_MASK) << 32)
  150. #define CNST_THRESH_MASK CNST_THRESH_VAL(EVENT_THRESH_MASK)
  151. #define CNST_EBB_VAL(v) (((v) & EVENT_EBB_MASK) << 24)
  152. #define CNST_EBB_MASK CNST_EBB_VAL(EVENT_EBB_MASK)
  153. #define CNST_L1_QUAL_VAL(v) (((v) & 3) << 22)
  154. #define CNST_L1_QUAL_MASK CNST_L1_QUAL_VAL(3)
  155. #define CNST_SAMPLE_VAL(v) (((v) & EVENT_SAMPLE_MASK) << 16)
  156. #define CNST_SAMPLE_MASK CNST_SAMPLE_VAL(EVENT_SAMPLE_MASK)
  157. /*
  158. * For NC we are counting up to 4 events. This requires three bits, and we need
  159. * the fifth event to overflow and set the 4th bit. To achieve that we bias the
  160. * fields by 3 in test_adder.
  161. */
  162. #define CNST_NC_SHIFT 12
  163. #define CNST_NC_VAL (1 << CNST_NC_SHIFT)
  164. #define CNST_NC_MASK (8 << CNST_NC_SHIFT)
  165. #define POWER8_TEST_ADDER (3 << CNST_NC_SHIFT)
  166. /*
  167. * For the per-PMC fields we have two bits. The low bit is added, so if two
  168. * events ask for the same PMC the sum will overflow, setting the high bit,
  169. * indicating an error. So our mask sets the high bit.
  170. */
  171. #define CNST_PMC_SHIFT(pmc) ((pmc - 1) * 2)
  172. #define CNST_PMC_VAL(pmc) (1 << CNST_PMC_SHIFT(pmc))
  173. #define CNST_PMC_MASK(pmc) (2 << CNST_PMC_SHIFT(pmc))
  174. /* Our add_fields is defined as: */
  175. #define POWER8_ADD_FIELDS \
  176. CNST_PMC_VAL(1) | CNST_PMC_VAL(2) | CNST_PMC_VAL(3) | \
  177. CNST_PMC_VAL(4) | CNST_PMC_VAL(5) | CNST_PMC_VAL(6) | CNST_NC_VAL
  178. /* Bits in MMCR1 for POWER8 */
  179. #define MMCR1_UNIT_SHIFT(pmc) (60 - (4 * ((pmc) - 1)))
  180. #define MMCR1_COMBINE_SHIFT(pmc) (35 - ((pmc) - 1))
  181. #define MMCR1_PMCSEL_SHIFT(pmc) (24 - (((pmc) - 1)) * 8)
  182. #define MMCR1_FAB_SHIFT 36
  183. #define MMCR1_DC_QUAL_SHIFT 47
  184. #define MMCR1_IC_QUAL_SHIFT 46
  185. /* Bits in MMCRA for POWER8 */
  186. #define MMCRA_SAMP_MODE_SHIFT 1
  187. #define MMCRA_SAMP_ELIG_SHIFT 4
  188. #define MMCRA_THR_CTL_SHIFT 8
  189. #define MMCRA_THR_SEL_SHIFT 16
  190. #define MMCRA_THR_CMP_SHIFT 32
  191. #define MMCRA_SDAR_MODE_TLB (1ull << 42)
  192. static inline bool event_is_fab_match(u64 event)
  193. {
  194. /* Only check pmc, unit and pmcxsel, ignore the edge bit (0) */
  195. event &= 0xff0fe;
  196. /* PM_MRK_FAB_RSP_MATCH & PM_MRK_FAB_RSP_MATCH_CYC */
  197. return (event == 0x30056 || event == 0x4f052);
  198. }
  199. static int power8_get_constraint(u64 event, unsigned long *maskp, unsigned long *valp)
  200. {
  201. unsigned int unit, pmc, cache, ebb;
  202. unsigned long mask, value;
  203. mask = value = 0;
  204. if (event & ~EVENT_VALID_MASK)
  205. return -1;
  206. pmc = (event >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  207. unit = (event >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
  208. cache = (event >> EVENT_CACHE_SEL_SHIFT) & EVENT_CACHE_SEL_MASK;
  209. ebb = (event >> PERF_EVENT_CONFIG_EBB_SHIFT) & EVENT_EBB_MASK;
  210. /* Clear the EBB bit in the event, so event checks work below */
  211. event &= ~(EVENT_EBB_MASK << PERF_EVENT_CONFIG_EBB_SHIFT);
  212. if (pmc) {
  213. if (pmc > 6)
  214. return -1;
  215. mask |= CNST_PMC_MASK(pmc);
  216. value |= CNST_PMC_VAL(pmc);
  217. if (pmc >= 5 && event != 0x500fa && event != 0x600f4)
  218. return -1;
  219. }
  220. if (pmc <= 4) {
  221. /*
  222. * Add to number of counters in use. Note this includes events with
  223. * a PMC of 0 - they still need a PMC, it's just assigned later.
  224. * Don't count events on PMC 5 & 6, there is only one valid event
  225. * on each of those counters, and they are handled above.
  226. */
  227. mask |= CNST_NC_MASK;
  228. value |= CNST_NC_VAL;
  229. }
  230. if (unit >= 6 && unit <= 9) {
  231. /*
  232. * L2/L3 events contain a cache selector field, which is
  233. * supposed to be programmed into MMCRC. However MMCRC is only
  234. * HV writable, and there is no API for guest kernels to modify
  235. * it. The solution is for the hypervisor to initialise the
  236. * field to zeroes, and for us to only ever allow events that
  237. * have a cache selector of zero.
  238. */
  239. if (cache)
  240. return -1;
  241. } else if (event & EVENT_IS_L1) {
  242. mask |= CNST_L1_QUAL_MASK;
  243. value |= CNST_L1_QUAL_VAL(cache);
  244. }
  245. if (event & EVENT_IS_MARKED) {
  246. mask |= CNST_SAMPLE_MASK;
  247. value |= CNST_SAMPLE_VAL(event >> EVENT_SAMPLE_SHIFT);
  248. }
  249. /*
  250. * Special case for PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
  251. * the threshold control bits are used for the match value.
  252. */
  253. if (event_is_fab_match(event)) {
  254. mask |= CNST_FAB_MATCH_MASK;
  255. value |= CNST_FAB_MATCH_VAL(event >> EVENT_THR_CTL_SHIFT);
  256. } else {
  257. /*
  258. * Check the mantissa upper two bits are not zero, unless the
  259. * exponent is also zero. See the THRESH_CMP_MANTISSA doc.
  260. */
  261. unsigned int cmp, exp;
  262. cmp = (event >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
  263. exp = cmp >> 7;
  264. if (exp && (cmp & 0x60) == 0)
  265. return -1;
  266. mask |= CNST_THRESH_MASK;
  267. value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
  268. }
  269. if (!pmc && ebb)
  270. /* EBB events must specify the PMC */
  271. return -1;
  272. /*
  273. * All events must agree on EBB, either all request it or none.
  274. * EBB events are pinned & exclusive, so this should never actually
  275. * hit, but we leave it as a fallback in case.
  276. */
  277. mask |= CNST_EBB_VAL(ebb);
  278. value |= CNST_EBB_MASK;
  279. *maskp = mask;
  280. *valp = value;
  281. return 0;
  282. }
  283. static int power8_compute_mmcr(u64 event[], int n_ev,
  284. unsigned int hwc[], unsigned long mmcr[])
  285. {
  286. unsigned long mmcra, mmcr1, unit, combine, psel, cache, val;
  287. unsigned int pmc, pmc_inuse;
  288. int i;
  289. pmc_inuse = 0;
  290. /* First pass to count resource use */
  291. for (i = 0; i < n_ev; ++i) {
  292. pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  293. if (pmc)
  294. pmc_inuse |= 1 << pmc;
  295. }
  296. /* In continous sampling mode, update SDAR on TLB miss */
  297. mmcra = MMCRA_SDAR_MODE_TLB;
  298. mmcr1 = 0;
  299. /* Second pass: assign PMCs, set all MMCR1 fields */
  300. for (i = 0; i < n_ev; ++i) {
  301. pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  302. unit = (event[i] >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
  303. combine = (event[i] >> EVENT_COMBINE_SHIFT) & EVENT_COMBINE_MASK;
  304. psel = event[i] & EVENT_PSEL_MASK;
  305. if (!pmc) {
  306. for (pmc = 1; pmc <= 4; ++pmc) {
  307. if (!(pmc_inuse & (1 << pmc)))
  308. break;
  309. }
  310. pmc_inuse |= 1 << pmc;
  311. }
  312. if (pmc <= 4) {
  313. mmcr1 |= unit << MMCR1_UNIT_SHIFT(pmc);
  314. mmcr1 |= combine << MMCR1_COMBINE_SHIFT(pmc);
  315. mmcr1 |= psel << MMCR1_PMCSEL_SHIFT(pmc);
  316. }
  317. if (event[i] & EVENT_IS_L1) {
  318. cache = event[i] >> EVENT_CACHE_SEL_SHIFT;
  319. mmcr1 |= (cache & 1) << MMCR1_IC_QUAL_SHIFT;
  320. cache >>= 1;
  321. mmcr1 |= (cache & 1) << MMCR1_DC_QUAL_SHIFT;
  322. }
  323. if (event[i] & EVENT_IS_MARKED) {
  324. mmcra |= MMCRA_SAMPLE_ENABLE;
  325. val = (event[i] >> EVENT_SAMPLE_SHIFT) & EVENT_SAMPLE_MASK;
  326. if (val) {
  327. mmcra |= (val & 3) << MMCRA_SAMP_MODE_SHIFT;
  328. mmcra |= (val >> 2) << MMCRA_SAMP_ELIG_SHIFT;
  329. }
  330. }
  331. /*
  332. * PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
  333. * the threshold bits are used for the match value.
  334. */
  335. if (event_is_fab_match(event[i])) {
  336. mmcr1 |= ((event[i] >> EVENT_THR_CTL_SHIFT) &
  337. EVENT_THR_CTL_MASK) << MMCR1_FAB_SHIFT;
  338. } else {
  339. val = (event[i] >> EVENT_THR_CTL_SHIFT) & EVENT_THR_CTL_MASK;
  340. mmcra |= val << MMCRA_THR_CTL_SHIFT;
  341. val = (event[i] >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
  342. mmcra |= val << MMCRA_THR_SEL_SHIFT;
  343. val = (event[i] >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
  344. mmcra |= val << MMCRA_THR_CMP_SHIFT;
  345. }
  346. hwc[i] = pmc - 1;
  347. }
  348. /* Return MMCRx values */
  349. mmcr[0] = 0;
  350. /* pmc_inuse is 1-based */
  351. if (pmc_inuse & 2)
  352. mmcr[0] = MMCR0_PMC1CE;
  353. if (pmc_inuse & 0x7c)
  354. mmcr[0] |= MMCR0_PMCjCE;
  355. /* If we're not using PMC 5 or 6, freeze them */
  356. if (!(pmc_inuse & 0x60))
  357. mmcr[0] |= MMCR0_FC56;
  358. mmcr[1] = mmcr1;
  359. mmcr[2] = mmcra;
  360. return 0;
  361. }
  362. #define MAX_ALT 2
  363. /* Table of alternatives, sorted by column 0 */
  364. static const unsigned int event_alternatives[][MAX_ALT] = {
  365. { 0x10134, 0x301e2 }, /* PM_MRK_ST_CMPL */
  366. { 0x10138, 0x40138 }, /* PM_BR_MRK_2PATH */
  367. { 0x18082, 0x3e05e }, /* PM_L3_CO_MEPF */
  368. { 0x1d14e, 0x401e8 }, /* PM_MRK_DATA_FROM_L2MISS */
  369. { 0x1e054, 0x4000a }, /* PM_CMPLU_STALL */
  370. { 0x20036, 0x40036 }, /* PM_BR_2PATH */
  371. { 0x200f2, 0x300f2 }, /* PM_INST_DISP */
  372. { 0x200f4, 0x600f4 }, /* PM_RUN_CYC */
  373. { 0x2013c, 0x3012e }, /* PM_MRK_FILT_MATCH */
  374. { 0x3e054, 0x400f0 }, /* PM_LD_MISS_L1 */
  375. { 0x400fa, 0x500fa }, /* PM_RUN_INST_CMPL */
  376. };
  377. /*
  378. * Scan the alternatives table for a match and return the
  379. * index into the alternatives table if found, else -1.
  380. */
  381. static int find_alternative(u64 event)
  382. {
  383. int i, j;
  384. for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
  385. if (event < event_alternatives[i][0])
  386. break;
  387. for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
  388. if (event == event_alternatives[i][j])
  389. return i;
  390. }
  391. return -1;
  392. }
  393. static int power8_get_alternatives(u64 event, unsigned int flags, u64 alt[])
  394. {
  395. int i, j, num_alt = 0;
  396. u64 alt_event;
  397. alt[num_alt++] = event;
  398. i = find_alternative(event);
  399. if (i >= 0) {
  400. /* Filter out the original event, it's already in alt[0] */
  401. for (j = 0; j < MAX_ALT; ++j) {
  402. alt_event = event_alternatives[i][j];
  403. if (alt_event && alt_event != event)
  404. alt[num_alt++] = alt_event;
  405. }
  406. }
  407. if (flags & PPMU_ONLY_COUNT_RUN) {
  408. /*
  409. * We're only counting in RUN state, so PM_CYC is equivalent to
  410. * PM_RUN_CYC and PM_INST_CMPL === PM_RUN_INST_CMPL.
  411. */
  412. j = num_alt;
  413. for (i = 0; i < num_alt; ++i) {
  414. switch (alt[i]) {
  415. case 0x1e: /* PM_CYC */
  416. alt[j++] = 0x600f4; /* PM_RUN_CYC */
  417. break;
  418. case 0x600f4: /* PM_RUN_CYC */
  419. alt[j++] = 0x1e;
  420. break;
  421. case 0x2: /* PM_PPC_CMPL */
  422. alt[j++] = 0x500fa; /* PM_RUN_INST_CMPL */
  423. break;
  424. case 0x500fa: /* PM_RUN_INST_CMPL */
  425. alt[j++] = 0x2; /* PM_PPC_CMPL */
  426. break;
  427. }
  428. }
  429. num_alt = j;
  430. }
  431. return num_alt;
  432. }
  433. static void power8_disable_pmc(unsigned int pmc, unsigned long mmcr[])
  434. {
  435. if (pmc <= 3)
  436. mmcr[1] &= ~(0xffUL << MMCR1_PMCSEL_SHIFT(pmc + 1));
  437. }
  438. PMU_FORMAT_ATTR(event, "config:0-49");
  439. PMU_FORMAT_ATTR(pmcxsel, "config:0-7");
  440. PMU_FORMAT_ATTR(mark, "config:8");
  441. PMU_FORMAT_ATTR(combine, "config:11");
  442. PMU_FORMAT_ATTR(unit, "config:12-15");
  443. PMU_FORMAT_ATTR(pmc, "config:16-19");
  444. PMU_FORMAT_ATTR(cache_sel, "config:20-23");
  445. PMU_FORMAT_ATTR(sample_mode, "config:24-28");
  446. PMU_FORMAT_ATTR(thresh_sel, "config:29-31");
  447. PMU_FORMAT_ATTR(thresh_stop, "config:32-35");
  448. PMU_FORMAT_ATTR(thresh_start, "config:36-39");
  449. PMU_FORMAT_ATTR(thresh_cmp, "config:40-49");
  450. static struct attribute *power8_pmu_format_attr[] = {
  451. &format_attr_event.attr,
  452. &format_attr_pmcxsel.attr,
  453. &format_attr_mark.attr,
  454. &format_attr_combine.attr,
  455. &format_attr_unit.attr,
  456. &format_attr_pmc.attr,
  457. &format_attr_cache_sel.attr,
  458. &format_attr_sample_mode.attr,
  459. &format_attr_thresh_sel.attr,
  460. &format_attr_thresh_stop.attr,
  461. &format_attr_thresh_start.attr,
  462. &format_attr_thresh_cmp.attr,
  463. NULL,
  464. };
  465. struct attribute_group power8_pmu_format_group = {
  466. .name = "format",
  467. .attrs = power8_pmu_format_attr,
  468. };
  469. static const struct attribute_group *power8_pmu_attr_groups[] = {
  470. &power8_pmu_format_group,
  471. NULL,
  472. };
  473. static int power8_generic_events[] = {
  474. [PERF_COUNT_HW_CPU_CYCLES] = PM_CYC,
  475. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = PM_GCT_NOSLOT_CYC,
  476. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = PM_CMPLU_STALL,
  477. [PERF_COUNT_HW_INSTRUCTIONS] = PM_INST_CMPL,
  478. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = PM_BRU_FIN,
  479. [PERF_COUNT_HW_BRANCH_MISSES] = PM_BR_MPRED_CMPL,
  480. };
  481. static u64 power8_bhrb_filter_map(u64 branch_sample_type)
  482. {
  483. u64 pmu_bhrb_filter = 0;
  484. /* BHRB and regular PMU events share the same privilege state
  485. * filter configuration. BHRB is always recorded along with a
  486. * regular PMU event. As the privilege state filter is handled
  487. * in the basic PMC configuration of the accompanying regular
  488. * PMU event, we ignore any separate BHRB specific request.
  489. */
  490. /* No branch filter requested */
  491. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY)
  492. return pmu_bhrb_filter;
  493. /* Invalid branch filter options - HW does not support */
  494. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
  495. return -1;
  496. if (branch_sample_type & PERF_SAMPLE_BRANCH_IND_CALL)
  497. return -1;
  498. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_CALL) {
  499. pmu_bhrb_filter |= POWER8_MMCRA_IFM1;
  500. return pmu_bhrb_filter;
  501. }
  502. /* Every thing else is unsupported */
  503. return -1;
  504. }
  505. static void power8_config_bhrb(u64 pmu_bhrb_filter)
  506. {
  507. /* Enable BHRB filter in PMU */
  508. mtspr(SPRN_MMCRA, (mfspr(SPRN_MMCRA) | pmu_bhrb_filter));
  509. }
  510. static struct power_pmu power8_pmu = {
  511. .name = "POWER8",
  512. .n_counter = 6,
  513. .max_alternatives = MAX_ALT + 1,
  514. .add_fields = POWER8_ADD_FIELDS,
  515. .test_adder = POWER8_TEST_ADDER,
  516. .compute_mmcr = power8_compute_mmcr,
  517. .config_bhrb = power8_config_bhrb,
  518. .bhrb_filter_map = power8_bhrb_filter_map,
  519. .get_constraint = power8_get_constraint,
  520. .get_alternatives = power8_get_alternatives,
  521. .disable_pmc = power8_disable_pmc,
  522. .flags = PPMU_HAS_SSLOT | PPMU_HAS_SIER | PPMU_BHRB | PPMU_EBB,
  523. .n_generic = ARRAY_SIZE(power8_generic_events),
  524. .generic_events = power8_generic_events,
  525. .attr_groups = power8_pmu_attr_groups,
  526. .bhrb_nr = 32,
  527. };
  528. static int __init init_power8_pmu(void)
  529. {
  530. int rc;
  531. if (!cur_cpu_spec->oprofile_cpu_type ||
  532. strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power8"))
  533. return -ENODEV;
  534. rc = register_power_pmu(&power8_pmu);
  535. if (rc)
  536. return rc;
  537. /* Tell userspace that EBB is supported */
  538. cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_EBB;
  539. return 0;
  540. }
  541. early_initcall(init_power8_pmu);