power5+-pmu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Performance counter support for POWER5 (not POWER5++) processors.
  3. *
  4. * Copyright 2009 Paul Mackerras, IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/perf_counter.h>
  13. #include <asm/reg.h>
  14. /*
  15. * Bits in event code for POWER5+ (POWER5 GS) and POWER5++ (POWER5 GS DD3)
  16. */
  17. #define PM_PMC_SH 20 /* PMC number (1-based) for direct events */
  18. #define PM_PMC_MSK 0xf
  19. #define PM_PMC_MSKS (PM_PMC_MSK << PM_PMC_SH)
  20. #define PM_UNIT_SH 16 /* TTMMUX number and setting - unit select */
  21. #define PM_UNIT_MSK 0xf
  22. #define PM_BYTE_SH 12 /* Byte number of event bus to use */
  23. #define PM_BYTE_MSK 7
  24. #define PM_GRS_SH 8 /* Storage subsystem mux select */
  25. #define PM_GRS_MSK 7
  26. #define PM_BUSEVENT_MSK 0x80 /* Set if event uses event bus */
  27. #define PM_PMCSEL_MSK 0x7f
  28. /* Values in PM_UNIT field */
  29. #define PM_FPU 0
  30. #define PM_ISU0 1
  31. #define PM_IFU 2
  32. #define PM_ISU1 3
  33. #define PM_IDU 4
  34. #define PM_ISU0_ALT 6
  35. #define PM_GRS 7
  36. #define PM_LSU0 8
  37. #define PM_LSU1 0xc
  38. #define PM_LASTUNIT 0xc
  39. /*
  40. * Bits in MMCR1 for POWER5+
  41. */
  42. #define MMCR1_TTM0SEL_SH 62
  43. #define MMCR1_TTM1SEL_SH 60
  44. #define MMCR1_TTM2SEL_SH 58
  45. #define MMCR1_TTM3SEL_SH 56
  46. #define MMCR1_TTMSEL_MSK 3
  47. #define MMCR1_TD_CP_DBG0SEL_SH 54
  48. #define MMCR1_TD_CP_DBG1SEL_SH 52
  49. #define MMCR1_TD_CP_DBG2SEL_SH 50
  50. #define MMCR1_TD_CP_DBG3SEL_SH 48
  51. #define MMCR1_GRS_L2SEL_SH 46
  52. #define MMCR1_GRS_L2SEL_MSK 3
  53. #define MMCR1_GRS_L3SEL_SH 44
  54. #define MMCR1_GRS_L3SEL_MSK 3
  55. #define MMCR1_GRS_MCSEL_SH 41
  56. #define MMCR1_GRS_MCSEL_MSK 7
  57. #define MMCR1_GRS_FABSEL_SH 39
  58. #define MMCR1_GRS_FABSEL_MSK 3
  59. #define MMCR1_PMC1_ADDER_SEL_SH 35
  60. #define MMCR1_PMC2_ADDER_SEL_SH 34
  61. #define MMCR1_PMC3_ADDER_SEL_SH 33
  62. #define MMCR1_PMC4_ADDER_SEL_SH 32
  63. #define MMCR1_PMC1SEL_SH 25
  64. #define MMCR1_PMC2SEL_SH 17
  65. #define MMCR1_PMC3SEL_SH 9
  66. #define MMCR1_PMC4SEL_SH 1
  67. #define MMCR1_PMCSEL_SH(n) (MMCR1_PMC1SEL_SH - (n) * 8)
  68. #define MMCR1_PMCSEL_MSK 0x7f
  69. /*
  70. * Bits in MMCRA
  71. */
  72. /*
  73. * Layout of constraint bits:
  74. * 6666555555555544444444443333333333222222222211111111110000000000
  75. * 3210987654321098765432109876543210987654321098765432109876543210
  76. * [ ><><>< ><> <><>[ > < >< >< >< ><><><><>
  77. * NC G0G1G2 G3 T0T1 UC B0 B1 B2 B3 P4P3P2P1
  78. *
  79. * NC - number of counters
  80. * 51: NC error 0x0008_0000_0000_0000
  81. * 48-50: number of events needing PMC1-4 0x0007_0000_0000_0000
  82. *
  83. * G0..G3 - GRS mux constraints
  84. * 46-47: GRS_L2SEL value
  85. * 44-45: GRS_L3SEL value
  86. * 41-44: GRS_MCSEL value
  87. * 39-40: GRS_FABSEL value
  88. * Note that these match up with their bit positions in MMCR1
  89. *
  90. * T0 - TTM0 constraint
  91. * 36-37: TTM0SEL value (0=FPU, 2=IFU, 3=ISU1) 0x30_0000_0000
  92. *
  93. * T1 - TTM1 constraint
  94. * 34-35: TTM1SEL value (0=IDU, 3=GRS) 0x0c_0000_0000
  95. *
  96. * UC - unit constraint: can't have all three of FPU|IFU|ISU1, ISU0, IDU|GRS
  97. * 33: UC3 error 0x02_0000_0000
  98. * 32: FPU|IFU|ISU1 events needed 0x01_0000_0000
  99. * 31: ISU0 events needed 0x01_8000_0000
  100. * 30: IDU|GRS events needed 0x00_4000_0000
  101. *
  102. * B0
  103. * 20-23: Byte 0 event source 0x00f0_0000
  104. * Encoding as for the event code
  105. *
  106. * B1, B2, B3
  107. * 16-19, 12-15, 8-11: Byte 1, 2, 3 event sources
  108. *
  109. * P4
  110. * 7: P1 error 0x80
  111. * 6-7: Count of events needing PMC4
  112. *
  113. * P1..P3
  114. * 0-6: Count of events needing PMC1..PMC3
  115. */
  116. static const int grsel_shift[8] = {
  117. MMCR1_GRS_L2SEL_SH, MMCR1_GRS_L2SEL_SH, MMCR1_GRS_L2SEL_SH,
  118. MMCR1_GRS_L3SEL_SH, MMCR1_GRS_L3SEL_SH, MMCR1_GRS_L3SEL_SH,
  119. MMCR1_GRS_MCSEL_SH, MMCR1_GRS_FABSEL_SH
  120. };
  121. /* Masks and values for using events from the various units */
  122. static u64 unit_cons[PM_LASTUNIT+1][2] = {
  123. [PM_FPU] = { 0x3200000000ull, 0x0100000000ull },
  124. [PM_ISU0] = { 0x0200000000ull, 0x0080000000ull },
  125. [PM_ISU1] = { 0x3200000000ull, 0x3100000000ull },
  126. [PM_IFU] = { 0x3200000000ull, 0x2100000000ull },
  127. [PM_IDU] = { 0x0e00000000ull, 0x0040000000ull },
  128. [PM_GRS] = { 0x0e00000000ull, 0x0c40000000ull },
  129. };
  130. static int power5p_get_constraint(unsigned int event, u64 *maskp, u64 *valp)
  131. {
  132. int pmc, byte, unit, sh;
  133. int bit, fmask;
  134. u64 mask = 0, value = 0;
  135. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  136. if (pmc) {
  137. if (pmc > 4)
  138. return -1;
  139. sh = (pmc - 1) * 2;
  140. mask |= 2 << sh;
  141. value |= 1 << sh;
  142. }
  143. if (event & PM_BUSEVENT_MSK) {
  144. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  145. if (unit > PM_LASTUNIT)
  146. return -1;
  147. if (unit == PM_ISU0_ALT)
  148. unit = PM_ISU0;
  149. mask |= unit_cons[unit][0];
  150. value |= unit_cons[unit][1];
  151. byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
  152. if (byte >= 4) {
  153. if (unit != PM_LSU1)
  154. return -1;
  155. /* Map LSU1 low word (bytes 4-7) to unit LSU1+1 */
  156. ++unit;
  157. byte &= 3;
  158. }
  159. if (unit == PM_GRS) {
  160. bit = event & 7;
  161. fmask = (bit == 6)? 7: 3;
  162. sh = grsel_shift[bit];
  163. mask |= (u64)fmask << sh;
  164. value |= (u64)((event >> PM_GRS_SH) & fmask) << sh;
  165. }
  166. /* Set byte lane select field */
  167. mask |= 0xfULL << (20 - 4 * byte);
  168. value |= (u64)unit << (20 - 4 * byte);
  169. }
  170. mask |= 0x8000000000000ull;
  171. value |= 0x1000000000000ull;
  172. *maskp = mask;
  173. *valp = value;
  174. return 0;
  175. }
  176. #define MAX_ALT 3 /* at most 3 alternatives for any event */
  177. static const unsigned int event_alternatives[][MAX_ALT] = {
  178. { 0x100c0, 0x40001f }, /* PM_GCT_FULL_CYC */
  179. { 0x120e4, 0x400002 }, /* PM_GRP_DISP_REJECT */
  180. { 0x230e2, 0x323087 }, /* PM_BR_PRED_CR */
  181. { 0x230e3, 0x223087, 0x3230a0 }, /* PM_BR_PRED_TA */
  182. { 0x410c7, 0x441084 }, /* PM_THRD_L2MISS_BOTH_CYC */
  183. { 0x800c4, 0xc20e0 }, /* PM_DTLB_MISS */
  184. { 0xc50c6, 0xc60e0 }, /* PM_MRK_DTLB_MISS */
  185. { 0x100009, 0x200009 }, /* PM_INST_CMPL */
  186. { 0x200015, 0x300015 }, /* PM_LSU_LMQ_SRQ_EMPTY_CYC */
  187. { 0x300009, 0x400009 }, /* PM_INST_DISP */
  188. };
  189. /*
  190. * Scan the alternatives table for a match and return the
  191. * index into the alternatives table if found, else -1.
  192. */
  193. static int find_alternative(unsigned int event)
  194. {
  195. int i, j;
  196. for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
  197. if (event < event_alternatives[i][0])
  198. break;
  199. for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
  200. if (event == event_alternatives[i][j])
  201. return i;
  202. }
  203. return -1;
  204. }
  205. static const unsigned char bytedecode_alternatives[4][4] = {
  206. /* PMC 1 */ { 0x21, 0x23, 0x25, 0x27 },
  207. /* PMC 2 */ { 0x07, 0x17, 0x0e, 0x1e },
  208. /* PMC 3 */ { 0x20, 0x22, 0x24, 0x26 },
  209. /* PMC 4 */ { 0x07, 0x17, 0x0e, 0x1e }
  210. };
  211. /*
  212. * Some direct events for decodes of event bus byte 3 have alternative
  213. * PMCSEL values on other counters. This returns the alternative
  214. * event code for those that do, or -1 otherwise. This also handles
  215. * alternative PCMSEL values for add events.
  216. */
  217. static int find_alternative_bdecode(unsigned int event)
  218. {
  219. int pmc, altpmc, pp, j;
  220. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  221. if (pmc == 0 || pmc > 4)
  222. return -1;
  223. altpmc = 5 - pmc; /* 1 <-> 4, 2 <-> 3 */
  224. pp = event & PM_PMCSEL_MSK;
  225. for (j = 0; j < 4; ++j) {
  226. if (bytedecode_alternatives[pmc - 1][j] == pp) {
  227. return (event & ~(PM_PMC_MSKS | PM_PMCSEL_MSK)) |
  228. (altpmc << PM_PMC_SH) |
  229. bytedecode_alternatives[altpmc - 1][j];
  230. }
  231. }
  232. /* new decode alternatives for power5+ */
  233. if (pmc == 1 && (pp == 0x0d || pp == 0x0e))
  234. return event + (2 << PM_PMC_SH) + (0x2e - 0x0d);
  235. if (pmc == 3 && (pp == 0x2e || pp == 0x2f))
  236. return event - (2 << PM_PMC_SH) - (0x2e - 0x0d);
  237. /* alternative add event encodings */
  238. if (pp == 0x10 || pp == 0x28)
  239. return ((event ^ (0x10 ^ 0x28)) & ~PM_PMC_MSKS) |
  240. (altpmc << PM_PMC_SH);
  241. return -1;
  242. }
  243. static int power5p_get_alternatives(unsigned int event, unsigned int alt[])
  244. {
  245. int i, j, ae, nalt = 1;
  246. alt[0] = event;
  247. nalt = 1;
  248. i = find_alternative(event);
  249. if (i >= 0) {
  250. for (j = 0; j < MAX_ALT; ++j) {
  251. ae = event_alternatives[i][j];
  252. if (ae && ae != event)
  253. alt[nalt++] = ae;
  254. }
  255. } else {
  256. ae = find_alternative_bdecode(event);
  257. if (ae > 0)
  258. alt[nalt++] = ae;
  259. }
  260. return nalt;
  261. }
  262. static int power5p_compute_mmcr(unsigned int event[], int n_ev,
  263. unsigned int hwc[], u64 mmcr[])
  264. {
  265. u64 mmcr1 = 0;
  266. unsigned int pmc, unit, byte, psel;
  267. unsigned int ttm;
  268. int i, isbus, bit, grsel;
  269. unsigned int pmc_inuse = 0;
  270. unsigned char busbyte[4];
  271. unsigned char unituse[16];
  272. int ttmuse;
  273. if (n_ev > 4)
  274. return -1;
  275. /* First pass to count resource use */
  276. memset(busbyte, 0, sizeof(busbyte));
  277. memset(unituse, 0, sizeof(unituse));
  278. for (i = 0; i < n_ev; ++i) {
  279. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  280. if (pmc) {
  281. if (pmc > 4)
  282. return -1;
  283. if (pmc_inuse & (1 << (pmc - 1)))
  284. return -1;
  285. pmc_inuse |= 1 << (pmc - 1);
  286. }
  287. if (event[i] & PM_BUSEVENT_MSK) {
  288. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  289. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  290. if (unit > PM_LASTUNIT)
  291. return -1;
  292. if (unit == PM_ISU0_ALT)
  293. unit = PM_ISU0;
  294. if (byte >= 4) {
  295. if (unit != PM_LSU1)
  296. return -1;
  297. ++unit;
  298. byte &= 3;
  299. }
  300. if (busbyte[byte] && busbyte[byte] != unit)
  301. return -1;
  302. busbyte[byte] = unit;
  303. unituse[unit] = 1;
  304. }
  305. }
  306. /*
  307. * Assign resources and set multiplexer selects.
  308. *
  309. * PM_ISU0 can go either on TTM0 or TTM1, but that's the only
  310. * choice we have to deal with.
  311. */
  312. if (unituse[PM_ISU0] &
  313. (unituse[PM_FPU] | unituse[PM_IFU] | unituse[PM_ISU1])) {
  314. unituse[PM_ISU0_ALT] = 1; /* move ISU to TTM1 */
  315. unituse[PM_ISU0] = 0;
  316. }
  317. /* Set TTM[01]SEL fields. */
  318. ttmuse = 0;
  319. for (i = PM_FPU; i <= PM_ISU1; ++i) {
  320. if (!unituse[i])
  321. continue;
  322. if (ttmuse++)
  323. return -1;
  324. mmcr1 |= (u64)i << MMCR1_TTM0SEL_SH;
  325. }
  326. ttmuse = 0;
  327. for (; i <= PM_GRS; ++i) {
  328. if (!unituse[i])
  329. continue;
  330. if (ttmuse++)
  331. return -1;
  332. mmcr1 |= (u64)(i & 3) << MMCR1_TTM1SEL_SH;
  333. }
  334. if (ttmuse > 1)
  335. return -1;
  336. /* Set byte lane select fields, TTM[23]SEL and GRS_*SEL. */
  337. for (byte = 0; byte < 4; ++byte) {
  338. unit = busbyte[byte];
  339. if (!unit)
  340. continue;
  341. if (unit == PM_ISU0 && unituse[PM_ISU0_ALT]) {
  342. /* get ISU0 through TTM1 rather than TTM0 */
  343. unit = PM_ISU0_ALT;
  344. } else if (unit == PM_LSU1 + 1) {
  345. /* select lower word of LSU1 for this byte */
  346. mmcr1 |= 1ull << (MMCR1_TTM3SEL_SH + 3 - byte);
  347. }
  348. ttm = unit >> 2;
  349. mmcr1 |= (u64)ttm << (MMCR1_TD_CP_DBG0SEL_SH - 2 * byte);
  350. }
  351. /* Second pass: assign PMCs, set PMCxSEL and PMCx_ADDER_SEL fields */
  352. for (i = 0; i < n_ev; ++i) {
  353. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  354. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  355. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  356. psel = event[i] & PM_PMCSEL_MSK;
  357. isbus = event[i] & PM_BUSEVENT_MSK;
  358. if (!pmc) {
  359. /* Bus event or any-PMC direct event */
  360. for (pmc = 0; pmc < 4; ++pmc) {
  361. if (!(pmc_inuse & (1 << pmc)))
  362. break;
  363. }
  364. if (pmc >= 4)
  365. return -1;
  366. pmc_inuse |= 1 << pmc;
  367. } else {
  368. /* Direct event */
  369. --pmc;
  370. if (isbus && (byte & 2) &&
  371. (psel == 8 || psel == 0x10 || psel == 0x28))
  372. /* add events on higher-numbered bus */
  373. mmcr1 |= 1ull << (MMCR1_PMC1_ADDER_SEL_SH - pmc);
  374. }
  375. if (isbus && unit == PM_GRS) {
  376. bit = psel & 7;
  377. grsel = (event[i] >> PM_GRS_SH) & PM_GRS_MSK;
  378. mmcr1 |= (u64)grsel << grsel_shift[bit];
  379. }
  380. if ((psel & 0x58) == 0x40 && (byte & 1) != ((pmc >> 1) & 1))
  381. /* select alternate byte lane */
  382. psel |= 0x10;
  383. if (pmc <= 3)
  384. mmcr1 |= psel << MMCR1_PMCSEL_SH(pmc);
  385. hwc[i] = pmc;
  386. }
  387. /* Return MMCRx values */
  388. mmcr[0] = 0;
  389. if (pmc_inuse & 1)
  390. mmcr[0] = MMCR0_PMC1CE;
  391. if (pmc_inuse & 0x3e)
  392. mmcr[0] |= MMCR0_PMCjCE;
  393. mmcr[1] = mmcr1;
  394. mmcr[2] = 0;
  395. return 0;
  396. }
  397. static void power5p_disable_pmc(unsigned int pmc, u64 mmcr[])
  398. {
  399. if (pmc <= 3)
  400. mmcr[1] &= ~(0x7fUL << MMCR1_PMCSEL_SH(pmc));
  401. }
  402. static int power5p_generic_events[] = {
  403. [PERF_COUNT_CPU_CYCLES] = 0xf,
  404. [PERF_COUNT_INSTRUCTIONS] = 0x100009,
  405. [PERF_COUNT_CACHE_REFERENCES] = 0x1c10a8, /* LD_REF_L1 */
  406. [PERF_COUNT_CACHE_MISSES] = 0x3c1088, /* LD_MISS_L1 */
  407. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x230e4, /* BR_ISSUED */
  408. [PERF_COUNT_BRANCH_MISSES] = 0x230e5, /* BR_MPRED_CR */
  409. };
  410. struct power_pmu power5p_pmu = {
  411. .n_counter = 4,
  412. .max_alternatives = MAX_ALT,
  413. .add_fields = 0x7000000000055ull,
  414. .test_adder = 0x3000040000000ull,
  415. .compute_mmcr = power5p_compute_mmcr,
  416. .get_constraint = power5p_get_constraint,
  417. .get_alternatives = power5p_get_alternatives,
  418. .disable_pmc = power5p_disable_pmc,
  419. .n_generic = ARRAY_SIZE(power5p_generic_events),
  420. .generic_events = power5p_generic_events,
  421. };