ppc970-pmu.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Performance counter support for PPC970-family 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/string.h>
  12. #include <linux/perf_counter.h>
  13. #include <asm/reg.h>
  14. /*
  15. * Bits in event code for PPC970
  16. */
  17. #define PM_PMC_SH 12 /* PMC number (1-based) for direct events */
  18. #define PM_PMC_MSK 0xf
  19. #define PM_UNIT_SH 8 /* TTMMUX number and setting - unit select */
  20. #define PM_UNIT_MSK 0xf
  21. #define PM_BYTE_SH 4 /* Byte number of event bus to use */
  22. #define PM_BYTE_MSK 3
  23. #define PM_PMCSEL_MSK 0xf
  24. /* Values in PM_UNIT field */
  25. #define PM_NONE 0
  26. #define PM_FPU 1
  27. #define PM_VPU 2
  28. #define PM_ISU 3
  29. #define PM_IFU 4
  30. #define PM_IDU 5
  31. #define PM_STS 6
  32. #define PM_LSU0 7
  33. #define PM_LSU1U 8
  34. #define PM_LSU1L 9
  35. #define PM_LASTUNIT 9
  36. /*
  37. * Bits in MMCR0 for PPC970
  38. */
  39. #define MMCR0_PMC1SEL_SH 8
  40. #define MMCR0_PMC2SEL_SH 1
  41. #define MMCR_PMCSEL_MSK 0x1f
  42. /*
  43. * Bits in MMCR1 for PPC970
  44. */
  45. #define MMCR1_TTM0SEL_SH 62
  46. #define MMCR1_TTM1SEL_SH 59
  47. #define MMCR1_TTM3SEL_SH 53
  48. #define MMCR1_TTMSEL_MSK 3
  49. #define MMCR1_TD_CP_DBG0SEL_SH 50
  50. #define MMCR1_TD_CP_DBG1SEL_SH 48
  51. #define MMCR1_TD_CP_DBG2SEL_SH 46
  52. #define MMCR1_TD_CP_DBG3SEL_SH 44
  53. #define MMCR1_PMC1_ADDER_SEL_SH 39
  54. #define MMCR1_PMC2_ADDER_SEL_SH 38
  55. #define MMCR1_PMC6_ADDER_SEL_SH 37
  56. #define MMCR1_PMC5_ADDER_SEL_SH 36
  57. #define MMCR1_PMC8_ADDER_SEL_SH 35
  58. #define MMCR1_PMC7_ADDER_SEL_SH 34
  59. #define MMCR1_PMC3_ADDER_SEL_SH 33
  60. #define MMCR1_PMC4_ADDER_SEL_SH 32
  61. #define MMCR1_PMC3SEL_SH 27
  62. #define MMCR1_PMC4SEL_SH 22
  63. #define MMCR1_PMC5SEL_SH 17
  64. #define MMCR1_PMC6SEL_SH 12
  65. #define MMCR1_PMC7SEL_SH 7
  66. #define MMCR1_PMC8SEL_SH 2
  67. static short mmcr1_adder_bits[8] = {
  68. MMCR1_PMC1_ADDER_SEL_SH,
  69. MMCR1_PMC2_ADDER_SEL_SH,
  70. MMCR1_PMC3_ADDER_SEL_SH,
  71. MMCR1_PMC4_ADDER_SEL_SH,
  72. MMCR1_PMC5_ADDER_SEL_SH,
  73. MMCR1_PMC6_ADDER_SEL_SH,
  74. MMCR1_PMC7_ADDER_SEL_SH,
  75. MMCR1_PMC8_ADDER_SEL_SH
  76. };
  77. /*
  78. * Bits in MMCRA
  79. */
  80. /*
  81. * Layout of constraint bits:
  82. * 6666555555555544444444443333333333222222222211111111110000000000
  83. * 3210987654321098765432109876543210987654321098765432109876543210
  84. * <><>[ >[ >[ >< >< >< >< ><><><><><><><><>
  85. * T0T1 UC PS1 PS2 B0 B1 B2 B3 P1P2P3P4P5P6P7P8
  86. *
  87. * T0 - TTM0 constraint
  88. * 46-47: TTM0SEL value (0=FPU, 2=IFU, 3=VPU) 0xC000_0000_0000
  89. *
  90. * T1 - TTM1 constraint
  91. * 44-45: TTM1SEL value (0=IDU, 3=STS) 0x3000_0000_0000
  92. *
  93. * UC - unit constraint: can't have all three of FPU|IFU|VPU, ISU, IDU|STS
  94. * 43: UC3 error 0x0800_0000_0000
  95. * 42: FPU|IFU|VPU events needed 0x0400_0000_0000
  96. * 41: ISU events needed 0x0200_0000_0000
  97. * 40: IDU|STS events needed 0x0100_0000_0000
  98. *
  99. * PS1
  100. * 39: PS1 error 0x0080_0000_0000
  101. * 36-38: count of events needing PMC1/2/5/6 0x0070_0000_0000
  102. *
  103. * PS2
  104. * 35: PS2 error 0x0008_0000_0000
  105. * 32-34: count of events needing PMC3/4/7/8 0x0007_0000_0000
  106. *
  107. * B0
  108. * 28-31: Byte 0 event source 0xf000_0000
  109. * Encoding as for the event code
  110. *
  111. * B1, B2, B3
  112. * 24-27, 20-23, 16-19: Byte 1, 2, 3 event sources
  113. *
  114. * P1
  115. * 15: P1 error 0x8000
  116. * 14-15: Count of events needing PMC1
  117. *
  118. * P2..P8
  119. * 0-13: Count of events needing PMC2..PMC8
  120. */
  121. /* Masks and values for using events from the various units */
  122. static u64 unit_cons[PM_LASTUNIT+1][2] = {
  123. [PM_FPU] = { 0xc80000000000ull, 0x040000000000ull },
  124. [PM_VPU] = { 0xc80000000000ull, 0xc40000000000ull },
  125. [PM_ISU] = { 0x080000000000ull, 0x020000000000ull },
  126. [PM_IFU] = { 0xc80000000000ull, 0x840000000000ull },
  127. [PM_IDU] = { 0x380000000000ull, 0x010000000000ull },
  128. [PM_STS] = { 0x380000000000ull, 0x310000000000ull },
  129. };
  130. static int p970_get_constraint(unsigned int event, u64 *maskp, u64 *valp)
  131. {
  132. int pmc, byte, unit, sh;
  133. u64 mask = 0, value = 0;
  134. int grp = -1;
  135. pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
  136. if (pmc) {
  137. if (pmc > 8)
  138. return -1;
  139. sh = (pmc - 1) * 2;
  140. mask |= 2 << sh;
  141. value |= 1 << sh;
  142. grp = ((pmc - 1) >> 1) & 1;
  143. }
  144. unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
  145. if (unit) {
  146. if (unit > PM_LASTUNIT)
  147. return -1;
  148. mask |= unit_cons[unit][0];
  149. value |= unit_cons[unit][1];
  150. byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
  151. /*
  152. * Bus events on bytes 0 and 2 can be counted
  153. * on PMC1/2/5/6; bytes 1 and 3 on PMC3/4/7/8.
  154. */
  155. if (!pmc)
  156. grp = byte & 1;
  157. /* Set byte lane select field */
  158. mask |= 0xfULL << (28 - 4 * byte);
  159. value |= (u64)unit << (28 - 4 * byte);
  160. }
  161. if (grp == 0) {
  162. /* increment PMC1/2/5/6 field */
  163. mask |= 0x8000000000ull;
  164. value |= 0x1000000000ull;
  165. } else if (grp == 1) {
  166. /* increment PMC3/4/7/8 field */
  167. mask |= 0x800000000ull;
  168. value |= 0x100000000ull;
  169. }
  170. *maskp = mask;
  171. *valp = value;
  172. return 0;
  173. }
  174. static int p970_get_alternatives(unsigned int event, unsigned int alt[])
  175. {
  176. alt[0] = event;
  177. /* 2 alternatives for LSU empty */
  178. if (event == 0x2002 || event == 0x3002) {
  179. alt[1] = event ^ 0x1000;
  180. return 2;
  181. }
  182. return 1;
  183. }
  184. static int p970_compute_mmcr(unsigned int event[], int n_ev,
  185. unsigned int hwc[], u64 mmcr[])
  186. {
  187. u64 mmcr0 = 0, mmcr1 = 0, mmcra = 0;
  188. unsigned int pmc, unit, byte, psel;
  189. unsigned int ttm, grp;
  190. unsigned int pmc_inuse = 0;
  191. unsigned int pmc_grp_use[2];
  192. unsigned char busbyte[4];
  193. unsigned char unituse[16];
  194. unsigned char unitmap[] = { 0, 0<<3, 3<<3, 1<<3, 2<<3, 0|4, 3|4 };
  195. unsigned char ttmuse[2];
  196. unsigned char pmcsel[8];
  197. int i;
  198. if (n_ev > 8)
  199. return -1;
  200. /* First pass to count resource use */
  201. pmc_grp_use[0] = pmc_grp_use[1] = 0;
  202. memset(busbyte, 0, sizeof(busbyte));
  203. memset(unituse, 0, sizeof(unituse));
  204. for (i = 0; i < n_ev; ++i) {
  205. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  206. if (pmc) {
  207. if (pmc_inuse & (1 << (pmc - 1)))
  208. return -1;
  209. pmc_inuse |= 1 << (pmc - 1);
  210. /* count 1/2/5/6 vs 3/4/7/8 use */
  211. ++pmc_grp_use[((pmc - 1) >> 1) & 1];
  212. }
  213. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  214. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  215. if (unit) {
  216. if (unit > PM_LASTUNIT)
  217. return -1;
  218. if (!pmc)
  219. ++pmc_grp_use[byte & 1];
  220. if (busbyte[byte] && busbyte[byte] != unit)
  221. return -1;
  222. busbyte[byte] = unit;
  223. unituse[unit] = 1;
  224. }
  225. }
  226. if (pmc_grp_use[0] > 4 || pmc_grp_use[1] > 4)
  227. return -1;
  228. /*
  229. * Assign resources and set multiplexer selects.
  230. *
  231. * PM_ISU can go either on TTM0 or TTM1, but that's the only
  232. * choice we have to deal with.
  233. */
  234. if (unituse[PM_ISU] &
  235. (unituse[PM_FPU] | unituse[PM_IFU] | unituse[PM_VPU]))
  236. unitmap[PM_ISU] = 2 | 4; /* move ISU to TTM1 */
  237. /* Set TTM[01]SEL fields. */
  238. ttmuse[0] = ttmuse[1] = 0;
  239. for (i = PM_FPU; i <= PM_STS; ++i) {
  240. if (!unituse[i])
  241. continue;
  242. ttm = unitmap[i];
  243. ++ttmuse[(ttm >> 2) & 1];
  244. mmcr1 |= (u64)(ttm & ~4) << MMCR1_TTM1SEL_SH;
  245. }
  246. /* Check only one unit per TTMx */
  247. if (ttmuse[0] > 1 || ttmuse[1] > 1)
  248. return -1;
  249. /* Set byte lane select fields and TTM3SEL. */
  250. for (byte = 0; byte < 4; ++byte) {
  251. unit = busbyte[byte];
  252. if (!unit)
  253. continue;
  254. if (unit <= PM_STS)
  255. ttm = (unitmap[unit] >> 2) & 1;
  256. else if (unit == PM_LSU0)
  257. ttm = 2;
  258. else {
  259. ttm = 3;
  260. if (unit == PM_LSU1L && byte >= 2)
  261. mmcr1 |= 1ull << (MMCR1_TTM3SEL_SH + 3 - byte);
  262. }
  263. mmcr1 |= (u64)ttm << (MMCR1_TD_CP_DBG0SEL_SH - 2 * byte);
  264. }
  265. /* Second pass: assign PMCs, set PMCxSEL and PMCx_ADDER_SEL fields */
  266. memset(pmcsel, 0x8, sizeof(pmcsel)); /* 8 means don't count */
  267. for (i = 0; i < n_ev; ++i) {
  268. pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
  269. unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
  270. byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
  271. psel = event[i] & PM_PMCSEL_MSK;
  272. if (!pmc) {
  273. /* Bus event or any-PMC direct event */
  274. if (unit)
  275. psel |= 0x10 | ((byte & 2) << 2);
  276. else
  277. psel |= 8;
  278. for (pmc = 0; pmc < 8; ++pmc) {
  279. if (pmc_inuse & (1 << pmc))
  280. continue;
  281. grp = (pmc >> 1) & 1;
  282. if (unit) {
  283. if (grp == (byte & 1))
  284. break;
  285. } else if (pmc_grp_use[grp] < 4) {
  286. ++pmc_grp_use[grp];
  287. break;
  288. }
  289. }
  290. pmc_inuse |= 1 << pmc;
  291. } else {
  292. /* Direct event */
  293. --pmc;
  294. if (psel == 0 && (byte & 2))
  295. /* add events on higher-numbered bus */
  296. mmcr1 |= 1ull << mmcr1_adder_bits[pmc];
  297. }
  298. pmcsel[pmc] = psel;
  299. hwc[i] = pmc;
  300. }
  301. for (pmc = 0; pmc < 2; ++pmc)
  302. mmcr0 |= pmcsel[pmc] << (MMCR0_PMC1SEL_SH - 7 * pmc);
  303. for (; pmc < 8; ++pmc)
  304. mmcr1 |= (u64)pmcsel[pmc] << (MMCR1_PMC3SEL_SH - 5 * (pmc - 2));
  305. if (pmc_inuse & 1)
  306. mmcr0 |= MMCR0_PMC1CE;
  307. if (pmc_inuse & 0xfe)
  308. mmcr0 |= MMCR0_PMCjCE;
  309. mmcra |= 0x2000; /* mark only one IOP per PPC instruction */
  310. /* Return MMCRx values */
  311. mmcr[0] = mmcr0;
  312. mmcr[1] = mmcr1;
  313. mmcr[2] = mmcra;
  314. return 0;
  315. }
  316. static void p970_disable_pmc(unsigned int pmc, u64 mmcr[])
  317. {
  318. int shift, i;
  319. if (pmc <= 1) {
  320. shift = MMCR0_PMC1SEL_SH - 7 * pmc;
  321. i = 0;
  322. } else {
  323. shift = MMCR1_PMC3SEL_SH - 5 * (pmc - 2);
  324. i = 1;
  325. }
  326. /*
  327. * Setting the PMCxSEL field to 0x08 disables PMC x.
  328. */
  329. mmcr[i] = (mmcr[i] & ~(0x1fUL << shift)) | (0x08UL << shift);
  330. }
  331. static int ppc970_generic_events[] = {
  332. [PERF_COUNT_CPU_CYCLES] = 7,
  333. [PERF_COUNT_INSTRUCTIONS] = 1,
  334. [PERF_COUNT_CACHE_REFERENCES] = 0x8810, /* PM_LD_REF_L1 */
  335. [PERF_COUNT_CACHE_MISSES] = 0x3810, /* PM_LD_MISS_L1 */
  336. [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x431, /* PM_BR_ISSUED */
  337. [PERF_COUNT_BRANCH_MISSES] = 0x327, /* PM_GRP_BR_MPRED */
  338. };
  339. struct power_pmu ppc970_pmu = {
  340. .n_counter = 8,
  341. .max_alternatives = 2,
  342. .add_fields = 0x001100005555ull,
  343. .test_adder = 0x013300000000ull,
  344. .compute_mmcr = p970_compute_mmcr,
  345. .get_constraint = p970_get_constraint,
  346. .get_alternatives = p970_get_alternatives,
  347. .disable_pmc = p970_disable_pmc,
  348. .n_generic = ARRAY_SIZE(ppc970_generic_events),
  349. .generic_events = ppc970_generic_events,
  350. };