power5-pmu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 (not POWER5++)
  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. * T0T1 NC G0G1G2 G3 UC PS1PS2 B0 B1 B2 B3 P6P5P4P3P2P1
  78. *
  79. * T0 - TTM0 constraint
  80. * 54-55: TTM0SEL value (0=FPU, 2=IFU, 3=ISU1) 0xc0_0000_0000_0000
  81. *
  82. * T1 - TTM1 constraint
  83. * 52-53: TTM1SEL value (0=IDU, 3=GRS) 0x30_0000_0000_0000
  84. *
  85. * NC - number of counters
  86. * 51: NC error 0x0008_0000_0000_0000
  87. * 48-50: number of events needing PMC1-4 0x0007_0000_0000_0000
  88. *
  89. * G0..G3 - GRS mux constraints
  90. * 46-47: GRS_L2SEL value
  91. * 44-45: GRS_L3SEL value
  92. * 41-44: GRS_MCSEL value
  93. * 39-40: GRS_FABSEL value
  94. * Note that these match up with their bit positions in MMCR1
  95. *
  96. * UC - unit constraint: can't have all three of FPU|IFU|ISU1, ISU0, IDU|GRS
  97. * 37: UC3 error 0x20_0000_0000
  98. * 36: FPU|IFU|ISU1 events needed 0x10_0000_0000
  99. * 35: ISU0 events needed 0x08_0000_0000
  100. * 34: IDU|GRS events needed 0x04_0000_0000
  101. *
  102. * PS1
  103. * 33: PS1 error 0x2_0000_0000
  104. * 31-32: count of events needing PMC1/2 0x1_8000_0000
  105. *
  106. * PS2
  107. * 30: PS2 error 0x4000_0000
  108. * 28-29: count of events needing PMC3/4 0x3000_0000
  109. *
  110. * B0
  111. * 24-27: Byte 0 event source 0x0f00_0000
  112. * Encoding as for the event code
  113. *
  114. * B1, B2, B3
  115. * 20-23, 16-19, 12-15: Byte 1, 2, 3 event sources
  116. *
  117. * P1..P6
  118. * 0-11: Count of events needing PMC1..PMC6
  119. */
  120. static const int grsel_shift[8] = {
  121. MMCR1_GRS_L2SEL_SH, MMCR1_GRS_L2SEL_SH, MMCR1_GRS_L2SEL_SH,
  122. MMCR1_GRS_L3SEL_SH, MMCR1_GRS_L3SEL_SH, MMCR1_GRS_L3SEL_SH,
  123. MMCR1_GRS_MCSEL_SH, MMCR1_GRS_FABSEL_SH
  124. };
  125. /* Masks and values for using events from the various units */
  126. static u64 unit_cons[PM_LASTUNIT+1][2] = {
  127. [PM_FPU] = { 0xc0002000000000ull, 0x00001000000000ull },
  128. [PM_ISU0] = { 0x00002000000000ull, 0x00000800000000ull },
  129. [PM_ISU1] = { 0xc0002000000000ull, 0xc0001000000000ull },
  130. [PM_IFU] = { 0xc0002000000000ull, 0x80001000000000ull },
  131. [PM_IDU] = { 0x30002000000000ull, 0x00000400000000ull },
  132. [PM_GRS] = { 0x30002000000000ull, 0x30000400000000ull },
  133. };
  134. static int power5_get_constraint(unsigned int event, u64 *maskp, u64 *valp)
  135. {
  136. int pmc, byte, unit, sh;
  137. int bit, fmask;
  138. u64 mask = 0, value = 0;
  139. int grp = -1;
  140. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  141. if (pmc) {
  142. if (pmc > 6)
  143. return -1;
  144. sh = (pmc - 1) * 2;
  145. mask |= 2 << sh;
  146. value |= 1 << sh;
  147. if (pmc <= 4)
  148. grp = (pmc - 1) >> 1;
  149. else if (event != 0x500009 && event != 0x600005)
  150. return -1;
  151. }
  152. if (event & PM_BUSEVENT_MSK) {
  153. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  154. if (unit > PM_LASTUNIT)
  155. return -1;
  156. if (unit == PM_ISU0_ALT)
  157. unit = PM_ISU0;
  158. mask |= unit_cons[unit][0];
  159. value |= unit_cons[unit][1];
  160. byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
  161. if (byte >= 4) {
  162. if (unit != PM_LSU1)
  163. return -1;
  164. /* Map LSU1 low word (bytes 4-7) to unit LSU1+1 */
  165. ++unit;
  166. byte &= 3;
  167. }
  168. if (unit == PM_GRS) {
  169. bit = event & 7;
  170. fmask = (bit == 6)? 7: 3;
  171. sh = grsel_shift[bit];
  172. mask |= (u64)fmask << sh;
  173. value |= (u64)((event >> PM_GRS_SH) & fmask) << sh;
  174. }
  175. /*
  176. * Bus events on bytes 0 and 2 can be counted
  177. * on PMC1/2; bytes 1 and 3 on PMC3/4.
  178. */
  179. if (!pmc)
  180. grp = byte & 1;
  181. /* Set byte lane select field */
  182. mask |= 0xfULL << (24 - 4 * byte);
  183. value |= (u64)unit << (24 - 4 * byte);
  184. }
  185. if (grp == 0) {
  186. /* increment PMC1/2 field */
  187. mask |= 0x200000000ull;
  188. value |= 0x080000000ull;
  189. } else if (grp == 1) {
  190. /* increment PMC3/4 field */
  191. mask |= 0x40000000ull;
  192. value |= 0x10000000ull;
  193. }
  194. if (pmc < 5) {
  195. /* need a counter from PMC1-4 set */
  196. mask |= 0x8000000000000ull;
  197. value |= 0x1000000000000ull;
  198. }
  199. *maskp = mask;
  200. *valp = value;
  201. return 0;
  202. }
  203. #define MAX_ALT 3 /* at most 3 alternatives for any event */
  204. static const unsigned int event_alternatives[][MAX_ALT] = {
  205. { 0x120e4, 0x400002 }, /* PM_GRP_DISP_REJECT */
  206. { 0x410c7, 0x441084 }, /* PM_THRD_L2MISS_BOTH_CYC */
  207. { 0x100005, 0x600005 }, /* PM_RUN_CYC */
  208. { 0x100009, 0x200009, 0x500009 }, /* PM_INST_CMPL */
  209. { 0x300009, 0x400009 }, /* PM_INST_DISP */
  210. };
  211. /*
  212. * Scan the alternatives table for a match and return the
  213. * index into the alternatives table if found, else -1.
  214. */
  215. static int find_alternative(unsigned int event)
  216. {
  217. int i, j;
  218. for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
  219. if (event < event_alternatives[i][0])
  220. break;
  221. for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
  222. if (event == event_alternatives[i][j])
  223. return i;
  224. }
  225. return -1;
  226. }
  227. static const unsigned char bytedecode_alternatives[4][4] = {
  228. /* PMC 1 */ { 0x21, 0x23, 0x25, 0x27 },
  229. /* PMC 2 */ { 0x07, 0x17, 0x0e, 0x1e },
  230. /* PMC 3 */ { 0x20, 0x22, 0x24, 0x26 },
  231. /* PMC 4 */ { 0x07, 0x17, 0x0e, 0x1e }
  232. };
  233. /*
  234. * Some direct events for decodes of event bus byte 3 have alternative
  235. * PMCSEL values on other counters. This returns the alternative
  236. * event code for those that do, or -1 otherwise.
  237. */
  238. static int find_alternative_bdecode(unsigned int event)
  239. {
  240. int pmc, altpmc, pp, j;
  241. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  242. if (pmc == 0 || pmc > 4)
  243. return -1;
  244. altpmc = 5 - pmc; /* 1 <-> 4, 2 <-> 3 */
  245. pp = event & PM_PMCSEL_MSK;
  246. for (j = 0; j < 4; ++j) {
  247. if (bytedecode_alternatives[pmc - 1][j] == pp) {
  248. return (event & ~(PM_PMC_MSKS | PM_PMCSEL_MSK)) |
  249. (altpmc << PM_PMC_SH) |
  250. bytedecode_alternatives[altpmc - 1][j];
  251. }
  252. }
  253. return -1;
  254. }
  255. static int power5_get_alternatives(unsigned int event, unsigned int alt[])
  256. {
  257. int i, j, ae, nalt = 1;
  258. alt[0] = event;
  259. nalt = 1;
  260. i = find_alternative(event);
  261. if (i >= 0) {
  262. for (j = 0; j < MAX_ALT; ++j) {
  263. ae = event_alternatives[i][j];
  264. if (ae && ae != event)
  265. alt[nalt++] = ae;
  266. }
  267. } else {
  268. ae = find_alternative_bdecode(event);
  269. if (ae > 0)
  270. alt[nalt++] = ae;
  271. }
  272. return nalt;
  273. }
  274. static int power5_compute_mmcr(unsigned int event[], int n_ev,
  275. unsigned int hwc[], u64 mmcr[])
  276. {
  277. u64 mmcr1 = 0;
  278. unsigned int pmc, unit, byte, psel;
  279. unsigned int ttm, grp;
  280. int i, isbus, bit, grsel;
  281. unsigned int pmc_inuse = 0;
  282. unsigned int pmc_grp_use[2];
  283. unsigned char busbyte[4];
  284. unsigned char unituse[16];
  285. int ttmuse;
  286. if (n_ev > 6)
  287. return -1;
  288. /* First pass to count resource use */
  289. pmc_grp_use[0] = pmc_grp_use[1] = 0;
  290. memset(busbyte, 0, sizeof(busbyte));
  291. memset(unituse, 0, sizeof(unituse));
  292. for (i = 0; i < n_ev; ++i) {
  293. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  294. if (pmc) {
  295. if (pmc > 6)
  296. return -1;
  297. if (pmc_inuse & (1 << (pmc - 1)))
  298. return -1;
  299. pmc_inuse |= 1 << (pmc - 1);
  300. /* count 1/2 vs 3/4 use */
  301. if (pmc <= 4)
  302. ++pmc_grp_use[(pmc - 1) >> 1];
  303. }
  304. if (event[i] & PM_BUSEVENT_MSK) {
  305. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  306. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  307. if (unit > PM_LASTUNIT)
  308. return -1;
  309. if (unit == PM_ISU0_ALT)
  310. unit = PM_ISU0;
  311. if (byte >= 4) {
  312. if (unit != PM_LSU1)
  313. return -1;
  314. ++unit;
  315. byte &= 3;
  316. }
  317. if (!pmc)
  318. ++pmc_grp_use[byte & 1];
  319. if (busbyte[byte] && busbyte[byte] != unit)
  320. return -1;
  321. busbyte[byte] = unit;
  322. unituse[unit] = 1;
  323. }
  324. }
  325. if (pmc_grp_use[0] > 2 || pmc_grp_use[1] > 2)
  326. return -1;
  327. /*
  328. * Assign resources and set multiplexer selects.
  329. *
  330. * PM_ISU0 can go either on TTM0 or TTM1, but that's the only
  331. * choice we have to deal with.
  332. */
  333. if (unituse[PM_ISU0] &
  334. (unituse[PM_FPU] | unituse[PM_IFU] | unituse[PM_ISU1])) {
  335. unituse[PM_ISU0_ALT] = 1; /* move ISU to TTM1 */
  336. unituse[PM_ISU0] = 0;
  337. }
  338. /* Set TTM[01]SEL fields. */
  339. ttmuse = 0;
  340. for (i = PM_FPU; i <= PM_ISU1; ++i) {
  341. if (!unituse[i])
  342. continue;
  343. if (ttmuse++)
  344. return -1;
  345. mmcr1 |= (u64)i << MMCR1_TTM0SEL_SH;
  346. }
  347. ttmuse = 0;
  348. for (; i <= PM_GRS; ++i) {
  349. if (!unituse[i])
  350. continue;
  351. if (ttmuse++)
  352. return -1;
  353. mmcr1 |= (u64)(i & 3) << MMCR1_TTM1SEL_SH;
  354. }
  355. if (ttmuse > 1)
  356. return -1;
  357. /* Set byte lane select fields, TTM[23]SEL and GRS_*SEL. */
  358. for (byte = 0; byte < 4; ++byte) {
  359. unit = busbyte[byte];
  360. if (!unit)
  361. continue;
  362. if (unit == PM_ISU0 && unituse[PM_ISU0_ALT]) {
  363. /* get ISU0 through TTM1 rather than TTM0 */
  364. unit = PM_ISU0_ALT;
  365. } else if (unit == PM_LSU1 + 1) {
  366. /* select lower word of LSU1 for this byte */
  367. mmcr1 |= 1ull << (MMCR1_TTM3SEL_SH + 3 - byte);
  368. }
  369. ttm = unit >> 2;
  370. mmcr1 |= (u64)ttm << (MMCR1_TD_CP_DBG0SEL_SH - 2 * byte);
  371. }
  372. /* Second pass: assign PMCs, set PMCxSEL and PMCx_ADDER_SEL fields */
  373. for (i = 0; i < n_ev; ++i) {
  374. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  375. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  376. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  377. psel = event[i] & PM_PMCSEL_MSK;
  378. isbus = event[i] & PM_BUSEVENT_MSK;
  379. if (!pmc) {
  380. /* Bus event or any-PMC direct event */
  381. for (pmc = 0; pmc < 4; ++pmc) {
  382. if (pmc_inuse & (1 << pmc))
  383. continue;
  384. grp = (pmc >> 1) & 1;
  385. if (isbus) {
  386. if (grp == (byte & 1))
  387. break;
  388. } else if (pmc_grp_use[grp] < 2) {
  389. ++pmc_grp_use[grp];
  390. break;
  391. }
  392. }
  393. pmc_inuse |= 1 << pmc;
  394. } else if (pmc <= 4) {
  395. /* Direct event */
  396. --pmc;
  397. if ((psel == 8 || psel == 0x10) && isbus && (byte & 2))
  398. /* add events on higher-numbered bus */
  399. mmcr1 |= 1ull << (MMCR1_PMC1_ADDER_SEL_SH - pmc);
  400. } else {
  401. /* Instructions or run cycles on PMC5/6 */
  402. --pmc;
  403. }
  404. if (isbus && unit == PM_GRS) {
  405. bit = psel & 7;
  406. grsel = (event[i] >> PM_GRS_SH) & PM_GRS_MSK;
  407. mmcr1 |= (u64)grsel << grsel_shift[bit];
  408. }
  409. if (pmc <= 3)
  410. mmcr1 |= psel << MMCR1_PMCSEL_SH(pmc);
  411. hwc[i] = pmc;
  412. }
  413. /* Return MMCRx values */
  414. mmcr[0] = 0;
  415. if (pmc_inuse & 1)
  416. mmcr[0] = MMCR0_PMC1CE;
  417. if (pmc_inuse & 0x3e)
  418. mmcr[0] |= MMCR0_PMCjCE;
  419. mmcr[1] = mmcr1;
  420. mmcr[2] = 0;
  421. return 0;
  422. }
  423. static void power5_disable_pmc(unsigned int pmc, u64 mmcr[])
  424. {
  425. if (pmc <= 3)
  426. mmcr[1] &= ~(0x7fUL << MMCR1_PMCSEL_SH(pmc));
  427. }
  428. static int power5_generic_events[] = {
  429. [PERF_COUNT_CPU_CYCLES] = 0xf,
  430. [PERF_COUNT_INSTRUCTIONS] = 0x100009,
  431. [PERF_COUNT_CACHE_REFERENCES] = 0x4c1090, /* LD_REF_L1 */
  432. [PERF_COUNT_CACHE_MISSES] = 0x3c1088, /* LD_MISS_L1 */
  433. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x230e4, /* BR_ISSUED */
  434. [PERF_COUNT_BRANCH_MISSES] = 0x230e5, /* BR_MPRED_CR */
  435. };
  436. struct power_pmu power5_pmu = {
  437. .n_counter = 6,
  438. .max_alternatives = MAX_ALT,
  439. .add_fields = 0x7000090000555ull,
  440. .test_adder = 0x3000490000000ull,
  441. .compute_mmcr = power5_compute_mmcr,
  442. .get_constraint = power5_get_constraint,
  443. .get_alternatives = power5_get_alternatives,
  444. .disable_pmc = power5_disable_pmc,
  445. .n_generic = ARRAY_SIZE(power5_generic_events),
  446. .generic_events = power5_generic_events,
  447. };