power5+-pmu.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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_event.h>
  13. #include <linux/string.h>
  14. #include <asm/reg.h>
  15. #include <asm/cputable.h>
  16. /*
  17. * Bits in event code for POWER5+ (POWER5 GS) and POWER5++ (POWER5 GS DD3)
  18. */
  19. #define PM_PMC_SH 20 /* PMC number (1-based) for direct events */
  20. #define PM_PMC_MSK 0xf
  21. #define PM_PMC_MSKS (PM_PMC_MSK << PM_PMC_SH)
  22. #define PM_UNIT_SH 16 /* TTMMUX number and setting - unit select */
  23. #define PM_UNIT_MSK 0xf
  24. #define PM_BYTE_SH 12 /* Byte number of event bus to use */
  25. #define PM_BYTE_MSK 7
  26. #define PM_GRS_SH 8 /* Storage subsystem mux select */
  27. #define PM_GRS_MSK 7
  28. #define PM_BUSEVENT_MSK 0x80 /* Set if event uses event bus */
  29. #define PM_PMCSEL_MSK 0x7f
  30. /* Values in PM_UNIT field */
  31. #define PM_FPU 0
  32. #define PM_ISU0 1
  33. #define PM_IFU 2
  34. #define PM_ISU1 3
  35. #define PM_IDU 4
  36. #define PM_ISU0_ALT 6
  37. #define PM_GRS 7
  38. #define PM_LSU0 8
  39. #define PM_LSU1 0xc
  40. #define PM_LASTUNIT 0xc
  41. /*
  42. * Bits in MMCR1 for POWER5+
  43. */
  44. #define MMCR1_TTM0SEL_SH 62
  45. #define MMCR1_TTM1SEL_SH 60
  46. #define MMCR1_TTM2SEL_SH 58
  47. #define MMCR1_TTM3SEL_SH 56
  48. #define MMCR1_TTMSEL_MSK 3
  49. #define MMCR1_TD_CP_DBG0SEL_SH 54
  50. #define MMCR1_TD_CP_DBG1SEL_SH 52
  51. #define MMCR1_TD_CP_DBG2SEL_SH 50
  52. #define MMCR1_TD_CP_DBG3SEL_SH 48
  53. #define MMCR1_GRS_L2SEL_SH 46
  54. #define MMCR1_GRS_L2SEL_MSK 3
  55. #define MMCR1_GRS_L3SEL_SH 44
  56. #define MMCR1_GRS_L3SEL_MSK 3
  57. #define MMCR1_GRS_MCSEL_SH 41
  58. #define MMCR1_GRS_MCSEL_MSK 7
  59. #define MMCR1_GRS_FABSEL_SH 39
  60. #define MMCR1_GRS_FABSEL_MSK 3
  61. #define MMCR1_PMC1_ADDER_SEL_SH 35
  62. #define MMCR1_PMC2_ADDER_SEL_SH 34
  63. #define MMCR1_PMC3_ADDER_SEL_SH 33
  64. #define MMCR1_PMC4_ADDER_SEL_SH 32
  65. #define MMCR1_PMC1SEL_SH 25
  66. #define MMCR1_PMC2SEL_SH 17
  67. #define MMCR1_PMC3SEL_SH 9
  68. #define MMCR1_PMC4SEL_SH 1
  69. #define MMCR1_PMCSEL_SH(n) (MMCR1_PMC1SEL_SH - (n) * 8)
  70. #define MMCR1_PMCSEL_MSK 0x7f
  71. /*
  72. * Bits in MMCRA
  73. */
  74. /*
  75. * Layout of constraint bits:
  76. * 6666555555555544444444443333333333222222222211111111110000000000
  77. * 3210987654321098765432109876543210987654321098765432109876543210
  78. * [ ><><>< ><> <><>[ > < >< >< >< ><><><><><><>
  79. * NC G0G1G2 G3 T0T1 UC B0 B1 B2 B3 P6P5P4P3P2P1
  80. *
  81. * NC - number of counters
  82. * 51: NC error 0x0008_0000_0000_0000
  83. * 48-50: number of events needing PMC1-4 0x0007_0000_0000_0000
  84. *
  85. * G0..G3 - GRS mux constraints
  86. * 46-47: GRS_L2SEL value
  87. * 44-45: GRS_L3SEL value
  88. * 41-44: GRS_MCSEL value
  89. * 39-40: GRS_FABSEL value
  90. * Note that these match up with their bit positions in MMCR1
  91. *
  92. * T0 - TTM0 constraint
  93. * 36-37: TTM0SEL value (0=FPU, 2=IFU, 3=ISU1) 0x30_0000_0000
  94. *
  95. * T1 - TTM1 constraint
  96. * 34-35: TTM1SEL value (0=IDU, 3=GRS) 0x0c_0000_0000
  97. *
  98. * UC - unit constraint: can't have all three of FPU|IFU|ISU1, ISU0, IDU|GRS
  99. * 33: UC3 error 0x02_0000_0000
  100. * 32: FPU|IFU|ISU1 events needed 0x01_0000_0000
  101. * 31: ISU0 events needed 0x01_8000_0000
  102. * 30: IDU|GRS events needed 0x00_4000_0000
  103. *
  104. * B0
  105. * 24-27: Byte 0 event source 0x0f00_0000
  106. * Encoding as for the event code
  107. *
  108. * B1, B2, B3
  109. * 20-23, 16-19, 12-15: Byte 1, 2, 3 event sources
  110. *
  111. * P6
  112. * 11: P6 error 0x800
  113. * 10-11: Count of events needing PMC6
  114. *
  115. * P1..P5
  116. * 0-9: Count of events needing PMC1..PMC5
  117. */
  118. static const int grsel_shift[8] = {
  119. MMCR1_GRS_L2SEL_SH, MMCR1_GRS_L2SEL_SH, MMCR1_GRS_L2SEL_SH,
  120. MMCR1_GRS_L3SEL_SH, MMCR1_GRS_L3SEL_SH, MMCR1_GRS_L3SEL_SH,
  121. MMCR1_GRS_MCSEL_SH, MMCR1_GRS_FABSEL_SH
  122. };
  123. /* Masks and values for using events from the various units */
  124. static unsigned long unit_cons[PM_LASTUNIT+1][2] = {
  125. [PM_FPU] = { 0x3200000000ul, 0x0100000000ul },
  126. [PM_ISU0] = { 0x0200000000ul, 0x0080000000ul },
  127. [PM_ISU1] = { 0x3200000000ul, 0x3100000000ul },
  128. [PM_IFU] = { 0x3200000000ul, 0x2100000000ul },
  129. [PM_IDU] = { 0x0e00000000ul, 0x0040000000ul },
  130. [PM_GRS] = { 0x0e00000000ul, 0x0c40000000ul },
  131. };
  132. static int power5p_get_constraint(u64 event, unsigned long *maskp,
  133. unsigned long *valp)
  134. {
  135. int pmc, byte, unit, sh;
  136. int bit, fmask;
  137. unsigned long mask = 0, value = 0;
  138. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  139. if (pmc) {
  140. if (pmc > 6)
  141. return -1;
  142. sh = (pmc - 1) * 2;
  143. mask |= 2 << sh;
  144. value |= 1 << sh;
  145. if (pmc >= 5 && !(event == 0x500009 || event == 0x600005))
  146. return -1;
  147. }
  148. if (event & PM_BUSEVENT_MSK) {
  149. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  150. if (unit > PM_LASTUNIT)
  151. return -1;
  152. if (unit == PM_ISU0_ALT)
  153. unit = PM_ISU0;
  154. mask |= unit_cons[unit][0];
  155. value |= unit_cons[unit][1];
  156. byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
  157. if (byte >= 4) {
  158. if (unit != PM_LSU1)
  159. return -1;
  160. /* Map LSU1 low word (bytes 4-7) to unit LSU1+1 */
  161. ++unit;
  162. byte &= 3;
  163. }
  164. if (unit == PM_GRS) {
  165. bit = event & 7;
  166. fmask = (bit == 6)? 7: 3;
  167. sh = grsel_shift[bit];
  168. mask |= (unsigned long)fmask << sh;
  169. value |= (unsigned long)((event >> PM_GRS_SH) & fmask)
  170. << sh;
  171. }
  172. /* Set byte lane select field */
  173. mask |= 0xfUL << (24 - 4 * byte);
  174. value |= (unsigned long)unit << (24 - 4 * byte);
  175. }
  176. if (pmc < 5) {
  177. /* need a counter from PMC1-4 set */
  178. mask |= 0x8000000000000ul;
  179. value |= 0x1000000000000ul;
  180. }
  181. *maskp = mask;
  182. *valp = value;
  183. return 0;
  184. }
  185. static int power5p_limited_pmc_event(u64 event)
  186. {
  187. int pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  188. return pmc == 5 || pmc == 6;
  189. }
  190. #define MAX_ALT 3 /* at most 3 alternatives for any event */
  191. static const unsigned int event_alternatives[][MAX_ALT] = {
  192. { 0x100c0, 0x40001f }, /* PM_GCT_FULL_CYC */
  193. { 0x120e4, 0x400002 }, /* PM_GRP_DISP_REJECT */
  194. { 0x230e2, 0x323087 }, /* PM_BR_PRED_CR */
  195. { 0x230e3, 0x223087, 0x3230a0 }, /* PM_BR_PRED_TA */
  196. { 0x410c7, 0x441084 }, /* PM_THRD_L2MISS_BOTH_CYC */
  197. { 0x800c4, 0xc20e0 }, /* PM_DTLB_MISS */
  198. { 0xc50c6, 0xc60e0 }, /* PM_MRK_DTLB_MISS */
  199. { 0x100005, 0x600005 }, /* PM_RUN_CYC */
  200. { 0x100009, 0x200009 }, /* PM_INST_CMPL */
  201. { 0x200015, 0x300015 }, /* PM_LSU_LMQ_SRQ_EMPTY_CYC */
  202. { 0x300009, 0x400009 }, /* PM_INST_DISP */
  203. };
  204. /*
  205. * Scan the alternatives table for a match and return the
  206. * index into the alternatives table if found, else -1.
  207. */
  208. static int find_alternative(unsigned int event)
  209. {
  210. int i, j;
  211. for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
  212. if (event < event_alternatives[i][0])
  213. break;
  214. for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
  215. if (event == event_alternatives[i][j])
  216. return i;
  217. }
  218. return -1;
  219. }
  220. static const unsigned char bytedecode_alternatives[4][4] = {
  221. /* PMC 1 */ { 0x21, 0x23, 0x25, 0x27 },
  222. /* PMC 2 */ { 0x07, 0x17, 0x0e, 0x1e },
  223. /* PMC 3 */ { 0x20, 0x22, 0x24, 0x26 },
  224. /* PMC 4 */ { 0x07, 0x17, 0x0e, 0x1e }
  225. };
  226. /*
  227. * Some direct events for decodes of event bus byte 3 have alternative
  228. * PMCSEL values on other counters. This returns the alternative
  229. * event code for those that do, or -1 otherwise. This also handles
  230. * alternative PCMSEL values for add events.
  231. */
  232. static s64 find_alternative_bdecode(u64 event)
  233. {
  234. int pmc, altpmc, pp, j;
  235. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  236. if (pmc == 0 || pmc > 4)
  237. return -1;
  238. altpmc = 5 - pmc; /* 1 <-> 4, 2 <-> 3 */
  239. pp = event & PM_PMCSEL_MSK;
  240. for (j = 0; j < 4; ++j) {
  241. if (bytedecode_alternatives[pmc - 1][j] == pp) {
  242. return (event & ~(PM_PMC_MSKS | PM_PMCSEL_MSK)) |
  243. (altpmc << PM_PMC_SH) |
  244. bytedecode_alternatives[altpmc - 1][j];
  245. }
  246. }
  247. /* new decode alternatives for power5+ */
  248. if (pmc == 1 && (pp == 0x0d || pp == 0x0e))
  249. return event + (2 << PM_PMC_SH) + (0x2e - 0x0d);
  250. if (pmc == 3 && (pp == 0x2e || pp == 0x2f))
  251. return event - (2 << PM_PMC_SH) - (0x2e - 0x0d);
  252. /* alternative add event encodings */
  253. if (pp == 0x10 || pp == 0x28)
  254. return ((event ^ (0x10 ^ 0x28)) & ~PM_PMC_MSKS) |
  255. (altpmc << PM_PMC_SH);
  256. return -1;
  257. }
  258. static int power5p_get_alternatives(u64 event, unsigned int flags, u64 alt[])
  259. {
  260. int i, j, nalt = 1;
  261. int nlim;
  262. s64 ae;
  263. alt[0] = event;
  264. nalt = 1;
  265. nlim = power5p_limited_pmc_event(event);
  266. i = find_alternative(event);
  267. if (i >= 0) {
  268. for (j = 0; j < MAX_ALT; ++j) {
  269. ae = event_alternatives[i][j];
  270. if (ae && ae != event)
  271. alt[nalt++] = ae;
  272. nlim += power5p_limited_pmc_event(ae);
  273. }
  274. } else {
  275. ae = find_alternative_bdecode(event);
  276. if (ae > 0)
  277. alt[nalt++] = ae;
  278. }
  279. if (flags & PPMU_ONLY_COUNT_RUN) {
  280. /*
  281. * We're only counting in RUN state,
  282. * so PM_CYC is equivalent to PM_RUN_CYC
  283. * and PM_INST_CMPL === PM_RUN_INST_CMPL.
  284. * This doesn't include alternatives that don't provide
  285. * any extra flexibility in assigning PMCs (e.g.
  286. * 0x100005 for PM_RUN_CYC vs. 0xf for PM_CYC).
  287. * Note that even with these additional alternatives
  288. * we never end up with more than 3 alternatives for any event.
  289. */
  290. j = nalt;
  291. for (i = 0; i < nalt; ++i) {
  292. switch (alt[i]) {
  293. case 0xf: /* PM_CYC */
  294. alt[j++] = 0x600005; /* PM_RUN_CYC */
  295. ++nlim;
  296. break;
  297. case 0x600005: /* PM_RUN_CYC */
  298. alt[j++] = 0xf;
  299. break;
  300. case 0x100009: /* PM_INST_CMPL */
  301. alt[j++] = 0x500009; /* PM_RUN_INST_CMPL */
  302. ++nlim;
  303. break;
  304. case 0x500009: /* PM_RUN_INST_CMPL */
  305. alt[j++] = 0x100009; /* PM_INST_CMPL */
  306. alt[j++] = 0x200009;
  307. break;
  308. }
  309. }
  310. nalt = j;
  311. }
  312. if (!(flags & PPMU_LIMITED_PMC_OK) && nlim) {
  313. /* remove the limited PMC events */
  314. j = 0;
  315. for (i = 0; i < nalt; ++i) {
  316. if (!power5p_limited_pmc_event(alt[i])) {
  317. alt[j] = alt[i];
  318. ++j;
  319. }
  320. }
  321. nalt = j;
  322. } else if ((flags & PPMU_LIMITED_PMC_REQD) && nlim < nalt) {
  323. /* remove all but the limited PMC events */
  324. j = 0;
  325. for (i = 0; i < nalt; ++i) {
  326. if (power5p_limited_pmc_event(alt[i])) {
  327. alt[j] = alt[i];
  328. ++j;
  329. }
  330. }
  331. nalt = j;
  332. }
  333. return nalt;
  334. }
  335. /*
  336. * Map of which direct events on which PMCs are marked instruction events.
  337. * Indexed by PMCSEL value, bit i (LE) set if PMC i is a marked event.
  338. * Bit 0 is set if it is marked for all PMCs.
  339. * The 0x80 bit indicates a byte decode PMCSEL value.
  340. */
  341. static unsigned char direct_event_is_marked[0x28] = {
  342. 0, /* 00 */
  343. 0x1f, /* 01 PM_IOPS_CMPL */
  344. 0x2, /* 02 PM_MRK_GRP_DISP */
  345. 0xe, /* 03 PM_MRK_ST_CMPL, PM_MRK_ST_GPS, PM_MRK_ST_CMPL_INT */
  346. 0, /* 04 */
  347. 0x1c, /* 05 PM_MRK_BRU_FIN, PM_MRK_INST_FIN, PM_MRK_CRU_FIN */
  348. 0x80, /* 06 */
  349. 0x80, /* 07 */
  350. 0, 0, 0,/* 08 - 0a */
  351. 0x18, /* 0b PM_THRESH_TIMEO, PM_MRK_GRP_TIMEO */
  352. 0, /* 0c */
  353. 0x80, /* 0d */
  354. 0x80, /* 0e */
  355. 0, /* 0f */
  356. 0, /* 10 */
  357. 0x14, /* 11 PM_MRK_GRP_BR_REDIR, PM_MRK_GRP_IC_MISS */
  358. 0, /* 12 */
  359. 0x10, /* 13 PM_MRK_GRP_CMPL */
  360. 0x1f, /* 14 PM_GRP_MRK, PM_MRK_{FXU,FPU,LSU}_FIN */
  361. 0x2, /* 15 PM_MRK_GRP_ISSUED */
  362. 0x80, /* 16 */
  363. 0x80, /* 17 */
  364. 0, 0, 0, 0, 0,
  365. 0x80, /* 1d */
  366. 0x80, /* 1e */
  367. 0, /* 1f */
  368. 0x80, /* 20 */
  369. 0x80, /* 21 */
  370. 0x80, /* 22 */
  371. 0x80, /* 23 */
  372. 0x80, /* 24 */
  373. 0x80, /* 25 */
  374. 0x80, /* 26 */
  375. 0x80, /* 27 */
  376. };
  377. /*
  378. * Returns 1 if event counts things relating to marked instructions
  379. * and thus needs the MMCRA_SAMPLE_ENABLE bit set, or 0 if not.
  380. */
  381. static int power5p_marked_instr_event(u64 event)
  382. {
  383. int pmc, psel;
  384. int bit, byte, unit;
  385. u32 mask;
  386. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  387. psel = event & PM_PMCSEL_MSK;
  388. if (pmc >= 5)
  389. return 0;
  390. bit = -1;
  391. if (psel < sizeof(direct_event_is_marked)) {
  392. if (direct_event_is_marked[psel] & (1 << pmc))
  393. return 1;
  394. if (direct_event_is_marked[psel] & 0x80)
  395. bit = 4;
  396. else if (psel == 0x08)
  397. bit = pmc - 1;
  398. else if (psel == 0x10)
  399. bit = 4 - pmc;
  400. else if (psel == 0x1b && (pmc == 1 || pmc == 3))
  401. bit = 4;
  402. } else if ((psel & 0x48) == 0x40) {
  403. bit = psel & 7;
  404. } else if (psel == 0x28) {
  405. bit = pmc - 1;
  406. } else if (pmc == 3 && (psel == 0x2e || psel == 0x2f)) {
  407. bit = 4;
  408. }
  409. if (!(event & PM_BUSEVENT_MSK) || bit == -1)
  410. return 0;
  411. byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
  412. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  413. if (unit == PM_LSU0) {
  414. /* byte 1 bits 0-7, byte 2 bits 0,2-4,6 */
  415. mask = 0x5dff00;
  416. } else if (unit == PM_LSU1 && byte >= 4) {
  417. byte -= 4;
  418. /* byte 5 bits 6-7, byte 6 bits 0,4, byte 7 bits 0-4,6 */
  419. mask = 0x5f11c000;
  420. } else
  421. return 0;
  422. return (mask >> (byte * 8 + bit)) & 1;
  423. }
  424. static int power5p_compute_mmcr(u64 event[], int n_ev,
  425. unsigned int hwc[], unsigned long mmcr[])
  426. {
  427. unsigned long mmcr1 = 0;
  428. unsigned long mmcra = 0;
  429. unsigned int pmc, unit, byte, psel;
  430. unsigned int ttm;
  431. int i, isbus, bit, grsel;
  432. unsigned int pmc_inuse = 0;
  433. unsigned char busbyte[4];
  434. unsigned char unituse[16];
  435. int ttmuse;
  436. if (n_ev > 6)
  437. return -1;
  438. /* First pass to count resource use */
  439. memset(busbyte, 0, sizeof(busbyte));
  440. memset(unituse, 0, sizeof(unituse));
  441. for (i = 0; i < n_ev; ++i) {
  442. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  443. if (pmc) {
  444. if (pmc > 6)
  445. return -1;
  446. if (pmc_inuse & (1 << (pmc - 1)))
  447. return -1;
  448. pmc_inuse |= 1 << (pmc - 1);
  449. }
  450. if (event[i] & PM_BUSEVENT_MSK) {
  451. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  452. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  453. if (unit > PM_LASTUNIT)
  454. return -1;
  455. if (unit == PM_ISU0_ALT)
  456. unit = PM_ISU0;
  457. if (byte >= 4) {
  458. if (unit != PM_LSU1)
  459. return -1;
  460. ++unit;
  461. byte &= 3;
  462. }
  463. if (busbyte[byte] && busbyte[byte] != unit)
  464. return -1;
  465. busbyte[byte] = unit;
  466. unituse[unit] = 1;
  467. }
  468. }
  469. /*
  470. * Assign resources and set multiplexer selects.
  471. *
  472. * PM_ISU0 can go either on TTM0 or TTM1, but that's the only
  473. * choice we have to deal with.
  474. */
  475. if (unituse[PM_ISU0] &
  476. (unituse[PM_FPU] | unituse[PM_IFU] | unituse[PM_ISU1])) {
  477. unituse[PM_ISU0_ALT] = 1; /* move ISU to TTM1 */
  478. unituse[PM_ISU0] = 0;
  479. }
  480. /* Set TTM[01]SEL fields. */
  481. ttmuse = 0;
  482. for (i = PM_FPU; i <= PM_ISU1; ++i) {
  483. if (!unituse[i])
  484. continue;
  485. if (ttmuse++)
  486. return -1;
  487. mmcr1 |= (unsigned long)i << MMCR1_TTM0SEL_SH;
  488. }
  489. ttmuse = 0;
  490. for (; i <= PM_GRS; ++i) {
  491. if (!unituse[i])
  492. continue;
  493. if (ttmuse++)
  494. return -1;
  495. mmcr1 |= (unsigned long)(i & 3) << MMCR1_TTM1SEL_SH;
  496. }
  497. if (ttmuse > 1)
  498. return -1;
  499. /* Set byte lane select fields, TTM[23]SEL and GRS_*SEL. */
  500. for (byte = 0; byte < 4; ++byte) {
  501. unit = busbyte[byte];
  502. if (!unit)
  503. continue;
  504. if (unit == PM_ISU0 && unituse[PM_ISU0_ALT]) {
  505. /* get ISU0 through TTM1 rather than TTM0 */
  506. unit = PM_ISU0_ALT;
  507. } else if (unit == PM_LSU1 + 1) {
  508. /* select lower word of LSU1 for this byte */
  509. mmcr1 |= 1ul << (MMCR1_TTM3SEL_SH + 3 - byte);
  510. }
  511. ttm = unit >> 2;
  512. mmcr1 |= (unsigned long)ttm
  513. << (MMCR1_TD_CP_DBG0SEL_SH - 2 * byte);
  514. }
  515. /* Second pass: assign PMCs, set PMCxSEL and PMCx_ADDER_SEL fields */
  516. for (i = 0; i < n_ev; ++i) {
  517. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  518. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  519. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  520. psel = event[i] & PM_PMCSEL_MSK;
  521. isbus = event[i] & PM_BUSEVENT_MSK;
  522. if (!pmc) {
  523. /* Bus event or any-PMC direct event */
  524. for (pmc = 0; pmc < 4; ++pmc) {
  525. if (!(pmc_inuse & (1 << pmc)))
  526. break;
  527. }
  528. if (pmc >= 4)
  529. return -1;
  530. pmc_inuse |= 1 << pmc;
  531. } else if (pmc <= 4) {
  532. /* Direct event */
  533. --pmc;
  534. if (isbus && (byte & 2) &&
  535. (psel == 8 || psel == 0x10 || psel == 0x28))
  536. /* add events on higher-numbered bus */
  537. mmcr1 |= 1ul << (MMCR1_PMC1_ADDER_SEL_SH - pmc);
  538. } else {
  539. /* Instructions or run cycles on PMC5/6 */
  540. --pmc;
  541. }
  542. if (isbus && unit == PM_GRS) {
  543. bit = psel & 7;
  544. grsel = (event[i] >> PM_GRS_SH) & PM_GRS_MSK;
  545. mmcr1 |= (unsigned long)grsel << grsel_shift[bit];
  546. }
  547. if (power5p_marked_instr_event(event[i]))
  548. mmcra |= MMCRA_SAMPLE_ENABLE;
  549. if ((psel & 0x58) == 0x40 && (byte & 1) != ((pmc >> 1) & 1))
  550. /* select alternate byte lane */
  551. psel |= 0x10;
  552. if (pmc <= 3)
  553. mmcr1 |= psel << MMCR1_PMCSEL_SH(pmc);
  554. hwc[i] = pmc;
  555. }
  556. /* Return MMCRx values */
  557. mmcr[0] = 0;
  558. if (pmc_inuse & 1)
  559. mmcr[0] = MMCR0_PMC1CE;
  560. if (pmc_inuse & 0x3e)
  561. mmcr[0] |= MMCR0_PMCjCE;
  562. mmcr[1] = mmcr1;
  563. mmcr[2] = mmcra;
  564. return 0;
  565. }
  566. static void power5p_disable_pmc(unsigned int pmc, unsigned long mmcr[])
  567. {
  568. if (pmc <= 3)
  569. mmcr[1] &= ~(0x7fUL << MMCR1_PMCSEL_SH(pmc));
  570. }
  571. static int power5p_generic_events[] = {
  572. [PERF_COUNT_HW_CPU_CYCLES] = 0xf,
  573. [PERF_COUNT_HW_INSTRUCTIONS] = 0x100009,
  574. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x1c10a8, /* LD_REF_L1 */
  575. [PERF_COUNT_HW_CACHE_MISSES] = 0x3c1088, /* LD_MISS_L1 */
  576. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x230e4, /* BR_ISSUED */
  577. [PERF_COUNT_HW_BRANCH_MISSES] = 0x230e5, /* BR_MPRED_CR */
  578. };
  579. #define C(x) PERF_COUNT_HW_CACHE_##x
  580. /*
  581. * Table of generalized cache-related events.
  582. * 0 means not supported, -1 means nonsensical, other values
  583. * are event codes.
  584. */
  585. static int power5p_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
  586. [C(L1D)] = { /* RESULT_ACCESS RESULT_MISS */
  587. [C(OP_READ)] = { 0x1c10a8, 0x3c1088 },
  588. [C(OP_WRITE)] = { 0x2c10a8, 0xc10c3 },
  589. [C(OP_PREFETCH)] = { 0xc70e7, -1 },
  590. },
  591. [C(L1I)] = { /* RESULT_ACCESS RESULT_MISS */
  592. [C(OP_READ)] = { 0, 0 },
  593. [C(OP_WRITE)] = { -1, -1 },
  594. [C(OP_PREFETCH)] = { 0, 0 },
  595. },
  596. [C(LL)] = { /* RESULT_ACCESS RESULT_MISS */
  597. [C(OP_READ)] = { 0, 0 },
  598. [C(OP_WRITE)] = { 0, 0 },
  599. [C(OP_PREFETCH)] = { 0xc50c3, 0 },
  600. },
  601. [C(DTLB)] = { /* RESULT_ACCESS RESULT_MISS */
  602. [C(OP_READ)] = { 0xc20e4, 0x800c4 },
  603. [C(OP_WRITE)] = { -1, -1 },
  604. [C(OP_PREFETCH)] = { -1, -1 },
  605. },
  606. [C(ITLB)] = { /* RESULT_ACCESS RESULT_MISS */
  607. [C(OP_READ)] = { 0, 0x800c0 },
  608. [C(OP_WRITE)] = { -1, -1 },
  609. [C(OP_PREFETCH)] = { -1, -1 },
  610. },
  611. [C(BPU)] = { /* RESULT_ACCESS RESULT_MISS */
  612. [C(OP_READ)] = { 0x230e4, 0x230e5 },
  613. [C(OP_WRITE)] = { -1, -1 },
  614. [C(OP_PREFETCH)] = { -1, -1 },
  615. },
  616. };
  617. static struct power_pmu power5p_pmu = {
  618. .name = "POWER5+/++",
  619. .n_counter = 6,
  620. .max_alternatives = MAX_ALT,
  621. .add_fields = 0x7000000000055ul,
  622. .test_adder = 0x3000040000000ul,
  623. .compute_mmcr = power5p_compute_mmcr,
  624. .get_constraint = power5p_get_constraint,
  625. .get_alternatives = power5p_get_alternatives,
  626. .disable_pmc = power5p_disable_pmc,
  627. .limited_pmc_event = power5p_limited_pmc_event,
  628. .flags = PPMU_LIMITED_PMC5_6,
  629. .n_generic = ARRAY_SIZE(power5p_generic_events),
  630. .generic_events = power5p_generic_events,
  631. .cache_events = &power5p_cache_events,
  632. };
  633. static int init_power5p_pmu(void)
  634. {
  635. if (!cur_cpu_spec->oprofile_cpu_type ||
  636. (strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power5+")
  637. && strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power5++")))
  638. return -ENODEV;
  639. return register_power_pmu(&power5p_pmu);
  640. }
  641. arch_initcall(init_power5p_pmu);