power6-pmu.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Performance counter support for POWER6 processors.
  3. *
  4. * Copyright 2008-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 POWER6
  16. */
  17. #define PM_PMC_SH 20 /* PMC number (1-based) for direct events */
  18. #define PM_PMC_MSK 0x7
  19. #define PM_PMC_MSKS (PM_PMC_MSK << PM_PMC_SH)
  20. #define PM_UNIT_SH 16 /* Unit event comes (TTMxSEL encoding) */
  21. #define PM_UNIT_MSK 0xf
  22. #define PM_UNIT_MSKS (PM_UNIT_MSK << PM_UNIT_SH)
  23. #define PM_LLAV 0x8000 /* Load lookahead match value */
  24. #define PM_LLA 0x4000 /* Load lookahead match enable */
  25. #define PM_BYTE_SH 12 /* Byte of event bus to use */
  26. #define PM_BYTE_MSK 3
  27. #define PM_SUBUNIT_SH 8 /* Subunit event comes from (NEST_SEL enc.) */
  28. #define PM_SUBUNIT_MSK 7
  29. #define PM_SUBUNIT_MSKS (PM_SUBUNIT_MSK << PM_SUBUNIT_SH)
  30. #define PM_PMCSEL_MSK 0xff /* PMCxSEL value */
  31. #define PM_BUSEVENT_MSK 0xf3700
  32. /*
  33. * Bits in MMCR1 for POWER6
  34. */
  35. #define MMCR1_TTM0SEL_SH 60
  36. #define MMCR1_TTMSEL_SH(n) (MMCR1_TTM0SEL_SH - (n) * 4)
  37. #define MMCR1_TTMSEL_MSK 0xf
  38. #define MMCR1_TTMSEL(m, n) (((m) >> MMCR1_TTMSEL_SH(n)) & MMCR1_TTMSEL_MSK)
  39. #define MMCR1_NESTSEL_SH 45
  40. #define MMCR1_NESTSEL_MSK 0x7
  41. #define MMCR1_NESTSEL(m) (((m) >> MMCR1_NESTSEL_SH) & MMCR1_NESTSEL_MSK)
  42. #define MMCR1_PMC1_LLA ((u64)1 << 44)
  43. #define MMCR1_PMC1_LLA_VALUE ((u64)1 << 39)
  44. #define MMCR1_PMC1_ADDR_SEL ((u64)1 << 35)
  45. #define MMCR1_PMC1SEL_SH 24
  46. #define MMCR1_PMCSEL_SH(n) (MMCR1_PMC1SEL_SH - (n) * 8)
  47. #define MMCR1_PMCSEL_MSK 0xff
  48. /*
  49. * Assign PMC numbers and compute MMCR1 value for a set of events
  50. */
  51. static int p6_compute_mmcr(unsigned int event[], int n_ev,
  52. unsigned int hwc[], u64 mmcr[])
  53. {
  54. u64 mmcr1 = 0;
  55. int i;
  56. unsigned int pmc, ev, b, u, s, psel;
  57. unsigned int ttmset = 0;
  58. unsigned int pmc_inuse = 0;
  59. if (n_ev > 4)
  60. return -1;
  61. for (i = 0; i < n_ev; ++i) {
  62. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  63. if (pmc) {
  64. if (pmc_inuse & (1 << (pmc - 1)))
  65. return -1; /* collision! */
  66. pmc_inuse |= 1 << (pmc - 1);
  67. }
  68. }
  69. for (i = 0; i < n_ev; ++i) {
  70. ev = event[i];
  71. pmc = (ev >> PM_PMC_SH) & PM_PMC_MSK;
  72. if (pmc) {
  73. --pmc;
  74. } else {
  75. /* can go on any PMC; find a free one */
  76. for (pmc = 0; pmc < 4; ++pmc)
  77. if (!(pmc_inuse & (1 << pmc)))
  78. break;
  79. pmc_inuse |= 1 << pmc;
  80. }
  81. hwc[i] = pmc;
  82. psel = ev & PM_PMCSEL_MSK;
  83. if (ev & PM_BUSEVENT_MSK) {
  84. /* this event uses the event bus */
  85. b = (ev >> PM_BYTE_SH) & PM_BYTE_MSK;
  86. u = (ev >> PM_UNIT_SH) & PM_UNIT_MSK;
  87. /* check for conflict on this byte of event bus */
  88. if ((ttmset & (1 << b)) && MMCR1_TTMSEL(mmcr1, b) != u)
  89. return -1;
  90. mmcr1 |= (u64)u << MMCR1_TTMSEL_SH(b);
  91. ttmset |= 1 << b;
  92. if (u == 5) {
  93. /* Nest events have a further mux */
  94. s = (ev >> PM_SUBUNIT_SH) & PM_SUBUNIT_MSK;
  95. if ((ttmset & 0x10) &&
  96. MMCR1_NESTSEL(mmcr1) != s)
  97. return -1;
  98. ttmset |= 0x10;
  99. mmcr1 |= (u64)s << MMCR1_NESTSEL_SH;
  100. }
  101. if (0x30 <= psel && psel <= 0x3d) {
  102. /* these need the PMCx_ADDR_SEL bits */
  103. if (b >= 2)
  104. mmcr1 |= MMCR1_PMC1_ADDR_SEL >> pmc;
  105. }
  106. /* bus select values are different for PMC3/4 */
  107. if (pmc >= 2 && (psel & 0x90) == 0x80)
  108. psel ^= 0x20;
  109. }
  110. if (ev & PM_LLA) {
  111. mmcr1 |= MMCR1_PMC1_LLA >> pmc;
  112. if (ev & PM_LLAV)
  113. mmcr1 |= MMCR1_PMC1_LLA_VALUE >> pmc;
  114. }
  115. mmcr1 |= (u64)psel << MMCR1_PMCSEL_SH(pmc);
  116. }
  117. mmcr[0] = 0;
  118. if (pmc_inuse & 1)
  119. mmcr[0] = MMCR0_PMC1CE;
  120. if (pmc_inuse & 0xe)
  121. mmcr[0] |= MMCR0_PMCjCE;
  122. mmcr[1] = mmcr1;
  123. mmcr[2] = 0;
  124. return 0;
  125. }
  126. /*
  127. * Layout of constraint bits:
  128. *
  129. * 0-1 add field: number of uses of PMC1 (max 1)
  130. * 2-3, 4-5, 6-7: ditto for PMC2, 3, 4
  131. * 8-10 select field: nest (subunit) event selector
  132. * 16-19 select field: unit on byte 0 of event bus
  133. * 20-23, 24-27, 28-31 ditto for bytes 1, 2, 3
  134. */
  135. static int p6_get_constraint(unsigned int event, u64 *maskp, u64 *valp)
  136. {
  137. int pmc, byte, sh;
  138. unsigned int mask = 0, value = 0;
  139. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  140. if (pmc) {
  141. if (pmc > 4)
  142. return -1;
  143. sh = (pmc - 1) * 2;
  144. mask |= 2 << sh;
  145. value |= 1 << sh;
  146. }
  147. if (event & PM_BUSEVENT_MSK) {
  148. byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
  149. sh = byte * 4;
  150. mask |= PM_UNIT_MSKS << sh;
  151. value |= (event & PM_UNIT_MSKS) << sh;
  152. if ((event & PM_UNIT_MSKS) == (5 << PM_UNIT_SH)) {
  153. mask |= PM_SUBUNIT_MSKS;
  154. value |= event & PM_SUBUNIT_MSKS;
  155. }
  156. }
  157. *maskp = mask;
  158. *valp = value;
  159. return 0;
  160. }
  161. #define MAX_ALT 4 /* at most 4 alternatives for any event */
  162. static const unsigned int event_alternatives[][MAX_ALT] = {
  163. { 0x0130e8, 0x2000f6, 0x3000fc }, /* PM_PTEG_RELOAD_VALID */
  164. { 0x080080, 0x10000d, 0x30000c, 0x4000f0 }, /* PM_LD_MISS_L1 */
  165. { 0x080088, 0x200054, 0x3000f0 }, /* PM_ST_MISS_L1 */
  166. { 0x10000a, 0x2000f4 }, /* PM_RUN_CYC */
  167. { 0x10000b, 0x2000f5 }, /* PM_RUN_COUNT */
  168. { 0x10000e, 0x400010 }, /* PM_PURR */
  169. { 0x100010, 0x4000f8 }, /* PM_FLUSH */
  170. { 0x10001a, 0x200010 }, /* PM_MRK_INST_DISP */
  171. { 0x100026, 0x3000f8 }, /* PM_TB_BIT_TRANS */
  172. { 0x100054, 0x2000f0 }, /* PM_ST_FIN */
  173. { 0x100056, 0x2000fc }, /* PM_L1_ICACHE_MISS */
  174. { 0x1000f0, 0x40000a }, /* PM_INST_IMC_MATCH_CMPL */
  175. { 0x1000f8, 0x200008 }, /* PM_GCT_EMPTY_CYC */
  176. { 0x1000fc, 0x400006 }, /* PM_LSU_DERAT_MISS_CYC */
  177. { 0x20000e, 0x400007 }, /* PM_LSU_DERAT_MISS */
  178. { 0x200012, 0x300012 }, /* PM_INST_DISP */
  179. { 0x2000f2, 0x3000f2 }, /* PM_INST_DISP */
  180. { 0x2000f8, 0x300010 }, /* PM_EXT_INT */
  181. { 0x2000fe, 0x300056 }, /* PM_DATA_FROM_L2MISS */
  182. { 0x2d0030, 0x30001a }, /* PM_MRK_FPU_FIN */
  183. { 0x30000a, 0x400018 }, /* PM_MRK_INST_FIN */
  184. { 0x3000f6, 0x40000e }, /* PM_L1_DCACHE_RELOAD_VALID */
  185. { 0x3000fe, 0x400056 }, /* PM_DATA_FROM_L3MISS */
  186. };
  187. /*
  188. * This could be made more efficient with a binary search on
  189. * a presorted list, if necessary
  190. */
  191. static int find_alternatives_list(unsigned int event)
  192. {
  193. int i, j;
  194. unsigned int alt;
  195. for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
  196. if (event < event_alternatives[i][0])
  197. return -1;
  198. for (j = 0; j < MAX_ALT; ++j) {
  199. alt = event_alternatives[i][j];
  200. if (!alt || event < alt)
  201. break;
  202. if (event == alt)
  203. return i;
  204. }
  205. }
  206. return -1;
  207. }
  208. static int p6_get_alternatives(unsigned int event, unsigned int alt[])
  209. {
  210. int i, j;
  211. unsigned int aevent, psel, pmc;
  212. unsigned int nalt = 1;
  213. alt[0] = event;
  214. /* check the alternatives table */
  215. i = find_alternatives_list(event);
  216. if (i >= 0) {
  217. /* copy out alternatives from list */
  218. for (j = 0; j < MAX_ALT; ++j) {
  219. aevent = event_alternatives[i][j];
  220. if (!aevent)
  221. break;
  222. if (aevent != event)
  223. alt[nalt++] = aevent;
  224. }
  225. } else {
  226. /* Check for alternative ways of computing sum events */
  227. /* PMCSEL 0x32 counter N == PMCSEL 0x34 counter 5-N */
  228. psel = event & (PM_PMCSEL_MSK & ~1); /* ignore edge bit */
  229. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  230. if (pmc && (psel == 0x32 || psel == 0x34))
  231. alt[nalt++] = ((event ^ 0x6) & ~PM_PMC_MSKS) |
  232. ((5 - pmc) << PM_PMC_SH);
  233. /* PMCSEL 0x38 counter N == PMCSEL 0x3a counter N+/-2 */
  234. if (pmc && (psel == 0x38 || psel == 0x3a))
  235. alt[nalt++] = ((event ^ 0x2) & ~PM_PMC_MSKS) |
  236. ((pmc > 2? pmc - 2: pmc + 2) << PM_PMC_SH);
  237. }
  238. return nalt;
  239. }
  240. static void p6_disable_pmc(unsigned int pmc, u64 mmcr[])
  241. {
  242. /* Set PMCxSEL to 0 to disable PMCx */
  243. mmcr[1] &= ~(0xffUL << MMCR1_PMCSEL_SH(pmc));
  244. }
  245. static int power6_generic_events[] = {
  246. [PERF_COUNT_CPU_CYCLES] = 0x1e,
  247. [PERF_COUNT_INSTRUCTIONS] = 2,
  248. [PERF_COUNT_CACHE_REFERENCES] = 0x280030, /* LD_REF_L1 */
  249. [PERF_COUNT_CACHE_MISSES] = 0x30000c, /* LD_MISS_L1 */
  250. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x410a0, /* BR_PRED */
  251. [PERF_COUNT_BRANCH_MISSES] = 0x400052, /* BR_MPRED */
  252. };
  253. struct power_pmu power6_pmu = {
  254. .n_counter = 4,
  255. .max_alternatives = MAX_ALT,
  256. .add_fields = 0x55,
  257. .test_adder = 0,
  258. .compute_mmcr = p6_compute_mmcr,
  259. .get_constraint = p6_get_constraint,
  260. .get_alternatives = p6_get_alternatives,
  261. .disable_pmc = p6_disable_pmc,
  262. .n_generic = ARRAY_SIZE(power6_generic_events),
  263. .generic_events = power6_generic_events,
  264. };