power5+-pmu.c 18 KB

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