power8-pmu.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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 <linux/kernel.h>
  14. #include <linux/perf_event.h>
  15. #include <asm/firmware.h>
  16. #include <asm/cputable.h>
  17. /*
  18. * Some power8 event codes.
  19. */
  20. #define PM_CYC 0x0001e
  21. #define PM_GCT_NOSLOT_CYC 0x100f8
  22. #define PM_CMPLU_STALL 0x4000a
  23. #define PM_INST_CMPL 0x00002
  24. #define PM_BRU_FIN 0x10068
  25. #define PM_BR_MPRED_CMPL 0x400f6
  26. /* All L1 D cache load references counted at finish, gated by reject */
  27. #define PM_LD_REF_L1 0x100ee
  28. /* Load Missed L1 */
  29. #define PM_LD_MISS_L1 0x3e054
  30. /* Store Missed L1 */
  31. #define PM_ST_MISS_L1 0x300f0
  32. /* L1 cache data prefetches */
  33. #define PM_L1_PREF 0x0d8b8
  34. /* Instruction fetches from L1 */
  35. #define PM_INST_FROM_L1 0x04080
  36. /* Demand iCache Miss */
  37. #define PM_L1_ICACHE_MISS 0x200fd
  38. /* Instruction Demand sectors wriittent into IL1 */
  39. #define PM_L1_DEMAND_WRITE 0x0408c
  40. /* Instruction prefetch written into IL1 */
  41. #define PM_IC_PREF_WRITE 0x0408e
  42. /* The data cache was reloaded from local core's L3 due to a demand load */
  43. #define PM_DATA_FROM_L3 0x4c042
  44. /* Demand LD - L3 Miss (not L2 hit and not L3 hit) */
  45. #define PM_DATA_FROM_L3MISS 0x300fe
  46. /* All successful D-side store dispatches for this thread */
  47. #define PM_L2_ST 0x17080
  48. /* All successful D-side store dispatches for this thread that were L2 Miss */
  49. #define PM_L2_ST_MISS 0x17082
  50. /* Total HW L3 prefetches(Load+store) */
  51. #define PM_L3_PREF_ALL 0x4e052
  52. /* Data PTEG reload */
  53. #define PM_DTLB_MISS 0x300fc
  54. /* ITLB Reloaded */
  55. #define PM_ITLB_MISS 0x400fc
  56. /*
  57. * Raw event encoding for POWER8:
  58. *
  59. * 60 56 52 48 44 40 36 32
  60. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  61. * | | [ ] [ thresh_cmp ] [ thresh_ctl ]
  62. * | | | |
  63. * | | *- IFM (Linux) thresh start/stop OR FAB match -*
  64. * | *- BHRB (Linux)
  65. * *- EBB (Linux)
  66. *
  67. * 28 24 20 16 12 8 4 0
  68. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  69. * [ ] [ sample ] [cache] [ pmc ] [unit ] c m [ pmcxsel ]
  70. * | | | | |
  71. * | | | | *- mark
  72. * | | *- L1/L2/L3 cache_sel |
  73. * | | |
  74. * | *- sampling mode for marked events *- combine
  75. * |
  76. * *- thresh_sel
  77. *
  78. * Below uses IBM bit numbering.
  79. *
  80. * MMCR1[x:y] = unit (PMCxUNIT)
  81. * MMCR1[x] = combine (PMCxCOMB)
  82. *
  83. * if pmc == 3 and unit == 0 and pmcxsel[0:6] == 0b0101011
  84. * # PM_MRK_FAB_RSP_MATCH
  85. * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
  86. * else if pmc == 4 and unit == 0xf and pmcxsel[0:6] == 0b0101001
  87. * # PM_MRK_FAB_RSP_MATCH_CYC
  88. * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
  89. * else
  90. * MMCRA[48:55] = thresh_ctl (THRESH START/END)
  91. *
  92. * if thresh_sel:
  93. * MMCRA[45:47] = thresh_sel
  94. *
  95. * if thresh_cmp:
  96. * MMCRA[22:24] = thresh_cmp[0:2]
  97. * MMCRA[25:31] = thresh_cmp[3:9]
  98. *
  99. * if unit == 6 or unit == 7
  100. * MMCRC[53:55] = cache_sel[1:3] (L2EVENT_SEL)
  101. * else if unit == 8 or unit == 9:
  102. * if cache_sel[0] == 0: # L3 bank
  103. * MMCRC[47:49] = cache_sel[1:3] (L3EVENT_SEL0)
  104. * else if cache_sel[0] == 1:
  105. * MMCRC[50:51] = cache_sel[2:3] (L3EVENT_SEL1)
  106. * else if cache_sel[1]: # L1 event
  107. * MMCR1[16] = cache_sel[2]
  108.  * MMCR1[17] = cache_sel[3]
  109. *
  110. * if mark:
  111. * MMCRA[63] = 1 (SAMPLE_ENABLE)
  112. * MMCRA[57:59] = sample[0:2] (RAND_SAMP_ELIG)
  113.  * MMCRA[61:62] = sample[3:4] (RAND_SAMP_MODE)
  114. *
  115. * if EBB and BHRB:
  116. * MMCRA[32:33] = IFM
  117. *
  118. */
  119. #define EVENT_EBB_MASK 1ull
  120. #define EVENT_EBB_SHIFT PERF_EVENT_CONFIG_EBB_SHIFT
  121. #define EVENT_BHRB_MASK 1ull
  122. #define EVENT_BHRB_SHIFT 62
  123. #define EVENT_WANTS_BHRB (EVENT_BHRB_MASK << EVENT_BHRB_SHIFT)
  124. #define EVENT_IFM_MASK 3ull
  125. #define EVENT_IFM_SHIFT 60
  126. #define EVENT_THR_CMP_SHIFT 40 /* Threshold CMP value */
  127. #define EVENT_THR_CMP_MASK 0x3ff
  128. #define EVENT_THR_CTL_SHIFT 32 /* Threshold control value (start/stop) */
  129. #define EVENT_THR_CTL_MASK 0xffull
  130. #define EVENT_THR_SEL_SHIFT 29 /* Threshold select value */
  131. #define EVENT_THR_SEL_MASK 0x7
  132. #define EVENT_THRESH_SHIFT 29 /* All threshold bits */
  133. #define EVENT_THRESH_MASK 0x1fffffull
  134. #define EVENT_SAMPLE_SHIFT 24 /* Sampling mode & eligibility */
  135. #define EVENT_SAMPLE_MASK 0x1f
  136. #define EVENT_CACHE_SEL_SHIFT 20 /* L2/L3 cache select */
  137. #define EVENT_CACHE_SEL_MASK 0xf
  138. #define EVENT_IS_L1 (4 << EVENT_CACHE_SEL_SHIFT)
  139. #define EVENT_PMC_SHIFT 16 /* PMC number (1-based) */
  140. #define EVENT_PMC_MASK 0xf
  141. #define EVENT_UNIT_SHIFT 12 /* Unit */
  142. #define EVENT_UNIT_MASK 0xf
  143. #define EVENT_COMBINE_SHIFT 11 /* Combine bit */
  144. #define EVENT_COMBINE_MASK 0x1
  145. #define EVENT_MARKED_SHIFT 8 /* Marked bit */
  146. #define EVENT_MARKED_MASK 0x1
  147. #define EVENT_IS_MARKED (EVENT_MARKED_MASK << EVENT_MARKED_SHIFT)
  148. #define EVENT_PSEL_MASK 0xff /* PMCxSEL value */
  149. /* Bits defined by Linux */
  150. #define EVENT_LINUX_MASK \
  151. ((EVENT_EBB_MASK << EVENT_EBB_SHIFT) | \
  152. (EVENT_BHRB_MASK << EVENT_BHRB_SHIFT) | \
  153. (EVENT_IFM_MASK << EVENT_IFM_SHIFT))
  154. #define EVENT_VALID_MASK \
  155. ((EVENT_THRESH_MASK << EVENT_THRESH_SHIFT) | \
  156. (EVENT_SAMPLE_MASK << EVENT_SAMPLE_SHIFT) | \
  157. (EVENT_CACHE_SEL_MASK << EVENT_CACHE_SEL_SHIFT) | \
  158. (EVENT_PMC_MASK << EVENT_PMC_SHIFT) | \
  159. (EVENT_UNIT_MASK << EVENT_UNIT_SHIFT) | \
  160. (EVENT_COMBINE_MASK << EVENT_COMBINE_SHIFT) | \
  161. (EVENT_MARKED_MASK << EVENT_MARKED_SHIFT) | \
  162. EVENT_LINUX_MASK | \
  163. EVENT_PSEL_MASK)
  164. /* MMCRA IFM bits - POWER8 */
  165. #define POWER8_MMCRA_IFM1 0x0000000040000000UL
  166. #define POWER8_MMCRA_IFM2 0x0000000080000000UL
  167. #define POWER8_MMCRA_IFM3 0x00000000C0000000UL
  168. #define ONLY_PLM \
  169. (PERF_SAMPLE_BRANCH_USER |\
  170. PERF_SAMPLE_BRANCH_KERNEL |\
  171. PERF_SAMPLE_BRANCH_HV)
  172. /*
  173. * Layout of constraint bits:
  174. *
  175. * 60 56 52 48 44 40 36 32
  176. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  177. * [ fab_match ] [ thresh_cmp ] [ thresh_ctl ] [ ]
  178. * |
  179. * thresh_sel -*
  180. *
  181. * 28 24 20 16 12 8 4 0
  182. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  183. * [ ] | [ ] [ sample ] [ ] [6] [5] [4] [3] [2] [1]
  184. * | | | |
  185. * BHRB IFM -* | | | Count of events for each PMC.
  186. * EBB -* | | p1, p2, p3, p4, p5, p6.
  187. * L1 I/D qualifier -* |
  188. * nc - number of counters -*
  189. *
  190. * The PMC fields P1..P6, and NC, are adder fields. As we accumulate constraints
  191. * we want the low bit of each field to be added to any existing value.
  192. *
  193. * Everything else is a value field.
  194. */
  195. #define CNST_FAB_MATCH_VAL(v) (((v) & EVENT_THR_CTL_MASK) << 56)
  196. #define CNST_FAB_MATCH_MASK CNST_FAB_MATCH_VAL(EVENT_THR_CTL_MASK)
  197. /* We just throw all the threshold bits into the constraint */
  198. #define CNST_THRESH_VAL(v) (((v) & EVENT_THRESH_MASK) << 32)
  199. #define CNST_THRESH_MASK CNST_THRESH_VAL(EVENT_THRESH_MASK)
  200. #define CNST_EBB_VAL(v) (((v) & EVENT_EBB_MASK) << 24)
  201. #define CNST_EBB_MASK CNST_EBB_VAL(EVENT_EBB_MASK)
  202. #define CNST_IFM_VAL(v) (((v) & EVENT_IFM_MASK) << 25)
  203. #define CNST_IFM_MASK CNST_IFM_VAL(EVENT_IFM_MASK)
  204. #define CNST_L1_QUAL_VAL(v) (((v) & 3) << 22)
  205. #define CNST_L1_QUAL_MASK CNST_L1_QUAL_VAL(3)
  206. #define CNST_SAMPLE_VAL(v) (((v) & EVENT_SAMPLE_MASK) << 16)
  207. #define CNST_SAMPLE_MASK CNST_SAMPLE_VAL(EVENT_SAMPLE_MASK)
  208. /*
  209. * For NC we are counting up to 4 events. This requires three bits, and we need
  210. * the fifth event to overflow and set the 4th bit. To achieve that we bias the
  211. * fields by 3 in test_adder.
  212. */
  213. #define CNST_NC_SHIFT 12
  214. #define CNST_NC_VAL (1 << CNST_NC_SHIFT)
  215. #define CNST_NC_MASK (8 << CNST_NC_SHIFT)
  216. #define POWER8_TEST_ADDER (3 << CNST_NC_SHIFT)
  217. /*
  218. * For the per-PMC fields we have two bits. The low bit is added, so if two
  219. * events ask for the same PMC the sum will overflow, setting the high bit,
  220. * indicating an error. So our mask sets the high bit.
  221. */
  222. #define CNST_PMC_SHIFT(pmc) ((pmc - 1) * 2)
  223. #define CNST_PMC_VAL(pmc) (1 << CNST_PMC_SHIFT(pmc))
  224. #define CNST_PMC_MASK(pmc) (2 << CNST_PMC_SHIFT(pmc))
  225. /* Our add_fields is defined as: */
  226. #define POWER8_ADD_FIELDS \
  227. CNST_PMC_VAL(1) | CNST_PMC_VAL(2) | CNST_PMC_VAL(3) | \
  228. CNST_PMC_VAL(4) | CNST_PMC_VAL(5) | CNST_PMC_VAL(6) | CNST_NC_VAL
  229. /* Bits in MMCR1 for POWER8 */
  230. #define MMCR1_UNIT_SHIFT(pmc) (60 - (4 * ((pmc) - 1)))
  231. #define MMCR1_COMBINE_SHIFT(pmc) (35 - ((pmc) - 1))
  232. #define MMCR1_PMCSEL_SHIFT(pmc) (24 - (((pmc) - 1)) * 8)
  233. #define MMCR1_FAB_SHIFT 36
  234. #define MMCR1_DC_QUAL_SHIFT 47
  235. #define MMCR1_IC_QUAL_SHIFT 46
  236. /* Bits in MMCRA for POWER8 */
  237. #define MMCRA_SAMP_MODE_SHIFT 1
  238. #define MMCRA_SAMP_ELIG_SHIFT 4
  239. #define MMCRA_THR_CTL_SHIFT 8
  240. #define MMCRA_THR_SEL_SHIFT 16
  241. #define MMCRA_THR_CMP_SHIFT 32
  242. #define MMCRA_SDAR_MODE_TLB (1ull << 42)
  243. #define MMCRA_IFM_SHIFT 30
  244. /* Bits in MMCR2 for POWER8 */
  245. #define MMCR2_FCS(pmc) (1ull << (63 - (((pmc) - 1) * 9)))
  246. #define MMCR2_FCP(pmc) (1ull << (62 - (((pmc) - 1) * 9)))
  247. #define MMCR2_FCH(pmc) (1ull << (57 - (((pmc) - 1) * 9)))
  248. static inline bool event_is_fab_match(u64 event)
  249. {
  250. /* Only check pmc, unit and pmcxsel, ignore the edge bit (0) */
  251. event &= 0xff0fe;
  252. /* PM_MRK_FAB_RSP_MATCH & PM_MRK_FAB_RSP_MATCH_CYC */
  253. return (event == 0x30056 || event == 0x4f052);
  254. }
  255. static int power8_get_constraint(u64 event, unsigned long *maskp, unsigned long *valp)
  256. {
  257. unsigned int unit, pmc, cache, ebb;
  258. unsigned long mask, value;
  259. mask = value = 0;
  260. if (event & ~EVENT_VALID_MASK)
  261. return -1;
  262. pmc = (event >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  263. unit = (event >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
  264. cache = (event >> EVENT_CACHE_SEL_SHIFT) & EVENT_CACHE_SEL_MASK;
  265. ebb = (event >> EVENT_EBB_SHIFT) & EVENT_EBB_MASK;
  266. if (pmc) {
  267. u64 base_event;
  268. if (pmc > 6)
  269. return -1;
  270. /* Ignore Linux defined bits when checking event below */
  271. base_event = event & ~EVENT_LINUX_MASK;
  272. if (pmc >= 5 && base_event != 0x500fa && base_event != 0x600f4)
  273. return -1;
  274. mask |= CNST_PMC_MASK(pmc);
  275. value |= CNST_PMC_VAL(pmc);
  276. }
  277. if (pmc <= 4) {
  278. /*
  279. * Add to number of counters in use. Note this includes events with
  280. * a PMC of 0 - they still need a PMC, it's just assigned later.
  281. * Don't count events on PMC 5 & 6, there is only one valid event
  282. * on each of those counters, and they are handled above.
  283. */
  284. mask |= CNST_NC_MASK;
  285. value |= CNST_NC_VAL;
  286. }
  287. if (unit >= 6 && unit <= 9) {
  288. /*
  289. * L2/L3 events contain a cache selector field, which is
  290. * supposed to be programmed into MMCRC. However MMCRC is only
  291. * HV writable, and there is no API for guest kernels to modify
  292. * it. The solution is for the hypervisor to initialise the
  293. * field to zeroes, and for us to only ever allow events that
  294. * have a cache selector of zero. The bank selector (bit 3) is
  295. * irrelevant, as long as the rest of the value is 0.
  296. */
  297. if (cache & 0x7)
  298. return -1;
  299. } else if (event & EVENT_IS_L1) {
  300. mask |= CNST_L1_QUAL_MASK;
  301. value |= CNST_L1_QUAL_VAL(cache);
  302. }
  303. if (event & EVENT_IS_MARKED) {
  304. mask |= CNST_SAMPLE_MASK;
  305. value |= CNST_SAMPLE_VAL(event >> EVENT_SAMPLE_SHIFT);
  306. }
  307. /*
  308. * Special case for PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
  309. * the threshold control bits are used for the match value.
  310. */
  311. if (event_is_fab_match(event)) {
  312. mask |= CNST_FAB_MATCH_MASK;
  313. value |= CNST_FAB_MATCH_VAL(event >> EVENT_THR_CTL_SHIFT);
  314. } else {
  315. /*
  316. * Check the mantissa upper two bits are not zero, unless the
  317. * exponent is also zero. See the THRESH_CMP_MANTISSA doc.
  318. */
  319. unsigned int cmp, exp;
  320. cmp = (event >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
  321. exp = cmp >> 7;
  322. if (exp && (cmp & 0x60) == 0)
  323. return -1;
  324. mask |= CNST_THRESH_MASK;
  325. value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
  326. }
  327. if (!pmc && ebb)
  328. /* EBB events must specify the PMC */
  329. return -1;
  330. if (event & EVENT_WANTS_BHRB) {
  331. if (!ebb)
  332. /* Only EBB events can request BHRB */
  333. return -1;
  334. mask |= CNST_IFM_MASK;
  335. value |= CNST_IFM_VAL(event >> EVENT_IFM_SHIFT);
  336. }
  337. /*
  338. * All events must agree on EBB, either all request it or none.
  339. * EBB events are pinned & exclusive, so this should never actually
  340. * hit, but we leave it as a fallback in case.
  341. */
  342. mask |= CNST_EBB_VAL(ebb);
  343. value |= CNST_EBB_MASK;
  344. *maskp = mask;
  345. *valp = value;
  346. return 0;
  347. }
  348. static int power8_compute_mmcr(u64 event[], int n_ev,
  349. unsigned int hwc[], unsigned long mmcr[],
  350. struct perf_event *pevents[])
  351. {
  352. unsigned long mmcra, mmcr1, mmcr2, unit, combine, psel, cache, val;
  353. unsigned int pmc, pmc_inuse;
  354. int i;
  355. pmc_inuse = 0;
  356. /* First pass to count resource use */
  357. for (i = 0; i < n_ev; ++i) {
  358. pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  359. if (pmc)
  360. pmc_inuse |= 1 << pmc;
  361. }
  362. /* In continous sampling mode, update SDAR on TLB miss */
  363. mmcra = MMCRA_SDAR_MODE_TLB;
  364. mmcr1 = mmcr2 = 0;
  365. /* Second pass: assign PMCs, set all MMCR1 fields */
  366. for (i = 0; i < n_ev; ++i) {
  367. pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  368. unit = (event[i] >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
  369. combine = (event[i] >> EVENT_COMBINE_SHIFT) & EVENT_COMBINE_MASK;
  370. psel = event[i] & EVENT_PSEL_MASK;
  371. if (!pmc) {
  372. for (pmc = 1; pmc <= 4; ++pmc) {
  373. if (!(pmc_inuse & (1 << pmc)))
  374. break;
  375. }
  376. pmc_inuse |= 1 << pmc;
  377. }
  378. if (pmc <= 4) {
  379. mmcr1 |= unit << MMCR1_UNIT_SHIFT(pmc);
  380. mmcr1 |= combine << MMCR1_COMBINE_SHIFT(pmc);
  381. mmcr1 |= psel << MMCR1_PMCSEL_SHIFT(pmc);
  382. }
  383. if (event[i] & EVENT_IS_L1) {
  384. cache = event[i] >> EVENT_CACHE_SEL_SHIFT;
  385. mmcr1 |= (cache & 1) << MMCR1_IC_QUAL_SHIFT;
  386. cache >>= 1;
  387. mmcr1 |= (cache & 1) << MMCR1_DC_QUAL_SHIFT;
  388. }
  389. if (event[i] & EVENT_IS_MARKED) {
  390. mmcra |= MMCRA_SAMPLE_ENABLE;
  391. val = (event[i] >> EVENT_SAMPLE_SHIFT) & EVENT_SAMPLE_MASK;
  392. if (val) {
  393. mmcra |= (val & 3) << MMCRA_SAMP_MODE_SHIFT;
  394. mmcra |= (val >> 2) << MMCRA_SAMP_ELIG_SHIFT;
  395. }
  396. }
  397. /*
  398. * PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
  399. * the threshold bits are used for the match value.
  400. */
  401. if (event_is_fab_match(event[i])) {
  402. mmcr1 |= ((event[i] >> EVENT_THR_CTL_SHIFT) &
  403. EVENT_THR_CTL_MASK) << MMCR1_FAB_SHIFT;
  404. } else {
  405. val = (event[i] >> EVENT_THR_CTL_SHIFT) & EVENT_THR_CTL_MASK;
  406. mmcra |= val << MMCRA_THR_CTL_SHIFT;
  407. val = (event[i] >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
  408. mmcra |= val << MMCRA_THR_SEL_SHIFT;
  409. val = (event[i] >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
  410. mmcra |= val << MMCRA_THR_CMP_SHIFT;
  411. }
  412. if (event[i] & EVENT_WANTS_BHRB) {
  413. val = (event[i] >> EVENT_IFM_SHIFT) & EVENT_IFM_MASK;
  414. mmcra |= val << MMCRA_IFM_SHIFT;
  415. }
  416. if (pevents[i]->attr.exclude_user)
  417. mmcr2 |= MMCR2_FCP(pmc);
  418. if (pevents[i]->attr.exclude_hv)
  419. mmcr2 |= MMCR2_FCH(pmc);
  420. if (pevents[i]->attr.exclude_kernel) {
  421. if (cpu_has_feature(CPU_FTR_HVMODE))
  422. mmcr2 |= MMCR2_FCH(pmc);
  423. else
  424. mmcr2 |= MMCR2_FCS(pmc);
  425. }
  426. hwc[i] = pmc - 1;
  427. }
  428. /* Return MMCRx values */
  429. mmcr[0] = 0;
  430. /* pmc_inuse is 1-based */
  431. if (pmc_inuse & 2)
  432. mmcr[0] = MMCR0_PMC1CE;
  433. if (pmc_inuse & 0x7c)
  434. mmcr[0] |= MMCR0_PMCjCE;
  435. /* If we're not using PMC 5 or 6, freeze them */
  436. if (!(pmc_inuse & 0x60))
  437. mmcr[0] |= MMCR0_FC56;
  438. mmcr[1] = mmcr1;
  439. mmcr[2] = mmcra;
  440. mmcr[3] = mmcr2;
  441. return 0;
  442. }
  443. #define MAX_ALT 2
  444. /* Table of alternatives, sorted by column 0 */
  445. static const unsigned int event_alternatives[][MAX_ALT] = {
  446. { 0x10134, 0x301e2 }, /* PM_MRK_ST_CMPL */
  447. { 0x10138, 0x40138 }, /* PM_BR_MRK_2PATH */
  448. { 0x18082, 0x3e05e }, /* PM_L3_CO_MEPF */
  449. { 0x1d14e, 0x401e8 }, /* PM_MRK_DATA_FROM_L2MISS */
  450. { 0x1e054, 0x4000a }, /* PM_CMPLU_STALL */
  451. { 0x20036, 0x40036 }, /* PM_BR_2PATH */
  452. { 0x200f2, 0x300f2 }, /* PM_INST_DISP */
  453. { 0x200f4, 0x600f4 }, /* PM_RUN_CYC */
  454. { 0x2013c, 0x3012e }, /* PM_MRK_FILT_MATCH */
  455. { 0x3e054, 0x400f0 }, /* PM_LD_MISS_L1 */
  456. { 0x400fa, 0x500fa }, /* PM_RUN_INST_CMPL */
  457. };
  458. /*
  459. * Scan the alternatives table for a match and return the
  460. * index into the alternatives table if found, else -1.
  461. */
  462. static int find_alternative(u64 event)
  463. {
  464. int i, j;
  465. for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
  466. if (event < event_alternatives[i][0])
  467. break;
  468. for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
  469. if (event == event_alternatives[i][j])
  470. return i;
  471. }
  472. return -1;
  473. }
  474. static int power8_get_alternatives(u64 event, unsigned int flags, u64 alt[])
  475. {
  476. int i, j, num_alt = 0;
  477. u64 alt_event;
  478. alt[num_alt++] = event;
  479. i = find_alternative(event);
  480. if (i >= 0) {
  481. /* Filter out the original event, it's already in alt[0] */
  482. for (j = 0; j < MAX_ALT; ++j) {
  483. alt_event = event_alternatives[i][j];
  484. if (alt_event && alt_event != event)
  485. alt[num_alt++] = alt_event;
  486. }
  487. }
  488. if (flags & PPMU_ONLY_COUNT_RUN) {
  489. /*
  490. * We're only counting in RUN state, so PM_CYC is equivalent to
  491. * PM_RUN_CYC and PM_INST_CMPL === PM_RUN_INST_CMPL.
  492. */
  493. j = num_alt;
  494. for (i = 0; i < num_alt; ++i) {
  495. switch (alt[i]) {
  496. case 0x1e: /* PM_CYC */
  497. alt[j++] = 0x600f4; /* PM_RUN_CYC */
  498. break;
  499. case 0x600f4: /* PM_RUN_CYC */
  500. alt[j++] = 0x1e;
  501. break;
  502. case 0x2: /* PM_PPC_CMPL */
  503. alt[j++] = 0x500fa; /* PM_RUN_INST_CMPL */
  504. break;
  505. case 0x500fa: /* PM_RUN_INST_CMPL */
  506. alt[j++] = 0x2; /* PM_PPC_CMPL */
  507. break;
  508. }
  509. }
  510. num_alt = j;
  511. }
  512. return num_alt;
  513. }
  514. static void power8_disable_pmc(unsigned int pmc, unsigned long mmcr[])
  515. {
  516. if (pmc <= 3)
  517. mmcr[1] &= ~(0xffUL << MMCR1_PMCSEL_SHIFT(pmc + 1));
  518. }
  519. PMU_FORMAT_ATTR(event, "config:0-49");
  520. PMU_FORMAT_ATTR(pmcxsel, "config:0-7");
  521. PMU_FORMAT_ATTR(mark, "config:8");
  522. PMU_FORMAT_ATTR(combine, "config:11");
  523. PMU_FORMAT_ATTR(unit, "config:12-15");
  524. PMU_FORMAT_ATTR(pmc, "config:16-19");
  525. PMU_FORMAT_ATTR(cache_sel, "config:20-23");
  526. PMU_FORMAT_ATTR(sample_mode, "config:24-28");
  527. PMU_FORMAT_ATTR(thresh_sel, "config:29-31");
  528. PMU_FORMAT_ATTR(thresh_stop, "config:32-35");
  529. PMU_FORMAT_ATTR(thresh_start, "config:36-39");
  530. PMU_FORMAT_ATTR(thresh_cmp, "config:40-49");
  531. static struct attribute *power8_pmu_format_attr[] = {
  532. &format_attr_event.attr,
  533. &format_attr_pmcxsel.attr,
  534. &format_attr_mark.attr,
  535. &format_attr_combine.attr,
  536. &format_attr_unit.attr,
  537. &format_attr_pmc.attr,
  538. &format_attr_cache_sel.attr,
  539. &format_attr_sample_mode.attr,
  540. &format_attr_thresh_sel.attr,
  541. &format_attr_thresh_stop.attr,
  542. &format_attr_thresh_start.attr,
  543. &format_attr_thresh_cmp.attr,
  544. NULL,
  545. };
  546. struct attribute_group power8_pmu_format_group = {
  547. .name = "format",
  548. .attrs = power8_pmu_format_attr,
  549. };
  550. static const struct attribute_group *power8_pmu_attr_groups[] = {
  551. &power8_pmu_format_group,
  552. NULL,
  553. };
  554. static int power8_generic_events[] = {
  555. [PERF_COUNT_HW_CPU_CYCLES] = PM_CYC,
  556. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = PM_GCT_NOSLOT_CYC,
  557. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = PM_CMPLU_STALL,
  558. [PERF_COUNT_HW_INSTRUCTIONS] = PM_INST_CMPL,
  559. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = PM_BRU_FIN,
  560. [PERF_COUNT_HW_BRANCH_MISSES] = PM_BR_MPRED_CMPL,
  561. [PERF_COUNT_HW_CACHE_REFERENCES] = PM_LD_REF_L1,
  562. [PERF_COUNT_HW_CACHE_MISSES] = PM_LD_MISS_L1,
  563. };
  564. static u64 power8_bhrb_filter_map(u64 branch_sample_type)
  565. {
  566. u64 pmu_bhrb_filter = 0;
  567. /* BHRB and regular PMU events share the same privilege state
  568. * filter configuration. BHRB is always recorded along with a
  569. * regular PMU event. As the privilege state filter is handled
  570. * in the basic PMC configuration of the accompanying regular
  571. * PMU event, we ignore any separate BHRB specific request.
  572. */
  573. /* No branch filter requested */
  574. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY)
  575. return pmu_bhrb_filter;
  576. /* Invalid branch filter options - HW does not support */
  577. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
  578. return -1;
  579. if (branch_sample_type & PERF_SAMPLE_BRANCH_IND_CALL)
  580. return -1;
  581. if (branch_sample_type & PERF_SAMPLE_BRANCH_CALL)
  582. return -1;
  583. if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_CALL) {
  584. pmu_bhrb_filter |= POWER8_MMCRA_IFM1;
  585. return pmu_bhrb_filter;
  586. }
  587. /* Every thing else is unsupported */
  588. return -1;
  589. }
  590. static void power8_config_bhrb(u64 pmu_bhrb_filter)
  591. {
  592. /* Enable BHRB filter in PMU */
  593. mtspr(SPRN_MMCRA, (mfspr(SPRN_MMCRA) | pmu_bhrb_filter));
  594. }
  595. #define C(x) PERF_COUNT_HW_CACHE_##x
  596. /*
  597. * Table of generalized cache-related events.
  598. * 0 means not supported, -1 means nonsensical, other values
  599. * are event codes.
  600. */
  601. static int power8_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
  602. [ C(L1D) ] = {
  603. [ C(OP_READ) ] = {
  604. [ C(RESULT_ACCESS) ] = PM_LD_REF_L1,
  605. [ C(RESULT_MISS) ] = PM_LD_MISS_L1,
  606. },
  607. [ C(OP_WRITE) ] = {
  608. [ C(RESULT_ACCESS) ] = 0,
  609. [ C(RESULT_MISS) ] = PM_ST_MISS_L1,
  610. },
  611. [ C(OP_PREFETCH) ] = {
  612. [ C(RESULT_ACCESS) ] = PM_L1_PREF,
  613. [ C(RESULT_MISS) ] = 0,
  614. },
  615. },
  616. [ C(L1I) ] = {
  617. [ C(OP_READ) ] = {
  618. [ C(RESULT_ACCESS) ] = PM_INST_FROM_L1,
  619. [ C(RESULT_MISS) ] = PM_L1_ICACHE_MISS,
  620. },
  621. [ C(OP_WRITE) ] = {
  622. [ C(RESULT_ACCESS) ] = PM_L1_DEMAND_WRITE,
  623. [ C(RESULT_MISS) ] = -1,
  624. },
  625. [ C(OP_PREFETCH) ] = {
  626. [ C(RESULT_ACCESS) ] = PM_IC_PREF_WRITE,
  627. [ C(RESULT_MISS) ] = 0,
  628. },
  629. },
  630. [ C(LL) ] = {
  631. [ C(OP_READ) ] = {
  632. [ C(RESULT_ACCESS) ] = PM_DATA_FROM_L3,
  633. [ C(RESULT_MISS) ] = PM_DATA_FROM_L3MISS,
  634. },
  635. [ C(OP_WRITE) ] = {
  636. [ C(RESULT_ACCESS) ] = PM_L2_ST,
  637. [ C(RESULT_MISS) ] = PM_L2_ST_MISS,
  638. },
  639. [ C(OP_PREFETCH) ] = {
  640. [ C(RESULT_ACCESS) ] = PM_L3_PREF_ALL,
  641. [ C(RESULT_MISS) ] = 0,
  642. },
  643. },
  644. [ C(DTLB) ] = {
  645. [ C(OP_READ) ] = {
  646. [ C(RESULT_ACCESS) ] = 0,
  647. [ C(RESULT_MISS) ] = PM_DTLB_MISS,
  648. },
  649. [ C(OP_WRITE) ] = {
  650. [ C(RESULT_ACCESS) ] = -1,
  651. [ C(RESULT_MISS) ] = -1,
  652. },
  653. [ C(OP_PREFETCH) ] = {
  654. [ C(RESULT_ACCESS) ] = -1,
  655. [ C(RESULT_MISS) ] = -1,
  656. },
  657. },
  658. [ C(ITLB) ] = {
  659. [ C(OP_READ) ] = {
  660. [ C(RESULT_ACCESS) ] = 0,
  661. [ C(RESULT_MISS) ] = PM_ITLB_MISS,
  662. },
  663. [ C(OP_WRITE) ] = {
  664. [ C(RESULT_ACCESS) ] = -1,
  665. [ C(RESULT_MISS) ] = -1,
  666. },
  667. [ C(OP_PREFETCH) ] = {
  668. [ C(RESULT_ACCESS) ] = -1,
  669. [ C(RESULT_MISS) ] = -1,
  670. },
  671. },
  672. [ C(BPU) ] = {
  673. [ C(OP_READ) ] = {
  674. [ C(RESULT_ACCESS) ] = PM_BRU_FIN,
  675. [ C(RESULT_MISS) ] = PM_BR_MPRED_CMPL,
  676. },
  677. [ C(OP_WRITE) ] = {
  678. [ C(RESULT_ACCESS) ] = -1,
  679. [ C(RESULT_MISS) ] = -1,
  680. },
  681. [ C(OP_PREFETCH) ] = {
  682. [ C(RESULT_ACCESS) ] = -1,
  683. [ C(RESULT_MISS) ] = -1,
  684. },
  685. },
  686. [ C(NODE) ] = {
  687. [ C(OP_READ) ] = {
  688. [ C(RESULT_ACCESS) ] = -1,
  689. [ C(RESULT_MISS) ] = -1,
  690. },
  691. [ C(OP_WRITE) ] = {
  692. [ C(RESULT_ACCESS) ] = -1,
  693. [ C(RESULT_MISS) ] = -1,
  694. },
  695. [ C(OP_PREFETCH) ] = {
  696. [ C(RESULT_ACCESS) ] = -1,
  697. [ C(RESULT_MISS) ] = -1,
  698. },
  699. },
  700. };
  701. #undef C
  702. static struct power_pmu power8_pmu = {
  703. .name = "POWER8",
  704. .n_counter = 6,
  705. .max_alternatives = MAX_ALT + 1,
  706. .add_fields = POWER8_ADD_FIELDS,
  707. .test_adder = POWER8_TEST_ADDER,
  708. .compute_mmcr = power8_compute_mmcr,
  709. .config_bhrb = power8_config_bhrb,
  710. .bhrb_filter_map = power8_bhrb_filter_map,
  711. .get_constraint = power8_get_constraint,
  712. .get_alternatives = power8_get_alternatives,
  713. .disable_pmc = power8_disable_pmc,
  714. .flags = PPMU_HAS_SSLOT | PPMU_HAS_SIER | PPMU_ARCH_207S,
  715. .n_generic = ARRAY_SIZE(power8_generic_events),
  716. .generic_events = power8_generic_events,
  717. .cache_events = &power8_cache_events,
  718. .attr_groups = power8_pmu_attr_groups,
  719. .bhrb_nr = 32,
  720. };
  721. static int __init init_power8_pmu(void)
  722. {
  723. int rc;
  724. if (!cur_cpu_spec->oprofile_cpu_type ||
  725. strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power8"))
  726. return -ENODEV;
  727. rc = register_power_pmu(&power8_pmu);
  728. if (rc)
  729. return rc;
  730. /* Tell userspace that EBB is supported */
  731. cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_EBB;
  732. if (cpu_has_feature(CPU_FTR_PMAO_BUG))
  733. pr_info("PMAO restore workaround active.\n");
  734. return 0;
  735. }
  736. early_initcall(init_power8_pmu);