isa207-common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * Common Performance counter support functions for PowerISA v2.07 processors.
  3. *
  4. * Copyright 2009 Paul Mackerras, IBM Corporation.
  5. * Copyright 2013 Michael Ellerman, IBM Corporation.
  6. * Copyright 2016 Madhavan Srinivasan, IBM Corporation.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include "isa207-common.h"
  14. PMU_FORMAT_ATTR(event, "config:0-49");
  15. PMU_FORMAT_ATTR(pmcxsel, "config:0-7");
  16. PMU_FORMAT_ATTR(mark, "config:8");
  17. PMU_FORMAT_ATTR(combine, "config:11");
  18. PMU_FORMAT_ATTR(unit, "config:12-15");
  19. PMU_FORMAT_ATTR(pmc, "config:16-19");
  20. PMU_FORMAT_ATTR(cache_sel, "config:20-23");
  21. PMU_FORMAT_ATTR(sample_mode, "config:24-28");
  22. PMU_FORMAT_ATTR(thresh_sel, "config:29-31");
  23. PMU_FORMAT_ATTR(thresh_stop, "config:32-35");
  24. PMU_FORMAT_ATTR(thresh_start, "config:36-39");
  25. PMU_FORMAT_ATTR(thresh_cmp, "config:40-49");
  26. struct attribute *isa207_pmu_format_attr[] = {
  27. &format_attr_event.attr,
  28. &format_attr_pmcxsel.attr,
  29. &format_attr_mark.attr,
  30. &format_attr_combine.attr,
  31. &format_attr_unit.attr,
  32. &format_attr_pmc.attr,
  33. &format_attr_cache_sel.attr,
  34. &format_attr_sample_mode.attr,
  35. &format_attr_thresh_sel.attr,
  36. &format_attr_thresh_stop.attr,
  37. &format_attr_thresh_start.attr,
  38. &format_attr_thresh_cmp.attr,
  39. NULL,
  40. };
  41. struct attribute_group isa207_pmu_format_group = {
  42. .name = "format",
  43. .attrs = isa207_pmu_format_attr,
  44. };
  45. static inline bool event_is_fab_match(u64 event)
  46. {
  47. /* Only check pmc, unit and pmcxsel, ignore the edge bit (0) */
  48. event &= 0xff0fe;
  49. /* PM_MRK_FAB_RSP_MATCH & PM_MRK_FAB_RSP_MATCH_CYC */
  50. return (event == 0x30056 || event == 0x4f052);
  51. }
  52. static bool is_event_valid(u64 event)
  53. {
  54. u64 valid_mask = EVENT_VALID_MASK;
  55. if (cpu_has_feature(CPU_FTR_ARCH_300))
  56. valid_mask = p9_EVENT_VALID_MASK;
  57. return !(event & ~valid_mask);
  58. }
  59. static inline bool is_event_marked(u64 event)
  60. {
  61. if (event & EVENT_IS_MARKED)
  62. return true;
  63. return false;
  64. }
  65. static void mmcra_sdar_mode(u64 event, unsigned long *mmcra)
  66. {
  67. /*
  68. * MMCRA[SDAR_MODE] specifices how the SDAR should be updated in
  69. * continous sampling mode.
  70. *
  71. * Incase of Power8:
  72. * MMCRA[SDAR_MODE] will be programmed as "0b01" for continous sampling
  73. * mode and will be un-changed when setting MMCRA[63] (Marked events).
  74. *
  75. * Incase of Power9:
  76. * Marked event: MMCRA[SDAR_MODE] will be set to 0b00 ('No Updates'),
  77. * or if group already have any marked events.
  78. * For rest
  79. * MMCRA[SDAR_MODE] will be set from event code.
  80. * If sdar_mode from event is zero, default to 0b01. Hardware
  81. * requires that we set a non-zero value.
  82. */
  83. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  84. if (is_event_marked(event) || (*mmcra & MMCRA_SAMPLE_ENABLE))
  85. *mmcra &= MMCRA_SDAR_MODE_NO_UPDATES;
  86. else if (p9_SDAR_MODE(event))
  87. *mmcra |= p9_SDAR_MODE(event) << MMCRA_SDAR_MODE_SHIFT;
  88. else
  89. *mmcra |= MMCRA_SDAR_MODE_DCACHE;
  90. } else
  91. *mmcra |= MMCRA_SDAR_MODE_TLB;
  92. }
  93. static u64 thresh_cmp_val(u64 value)
  94. {
  95. if (cpu_has_feature(CPU_FTR_ARCH_300))
  96. return value << p9_MMCRA_THR_CMP_SHIFT;
  97. return value << MMCRA_THR_CMP_SHIFT;
  98. }
  99. static unsigned long combine_from_event(u64 event)
  100. {
  101. if (cpu_has_feature(CPU_FTR_ARCH_300))
  102. return p9_EVENT_COMBINE(event);
  103. return EVENT_COMBINE(event);
  104. }
  105. static unsigned long combine_shift(unsigned long pmc)
  106. {
  107. if (cpu_has_feature(CPU_FTR_ARCH_300))
  108. return p9_MMCR1_COMBINE_SHIFT(pmc);
  109. return MMCR1_COMBINE_SHIFT(pmc);
  110. }
  111. static inline bool event_is_threshold(u64 event)
  112. {
  113. return (event >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
  114. }
  115. static bool is_thresh_cmp_valid(u64 event)
  116. {
  117. unsigned int cmp, exp;
  118. /*
  119. * Check the mantissa upper two bits are not zero, unless the
  120. * exponent is also zero. See the THRESH_CMP_MANTISSA doc.
  121. */
  122. cmp = (event >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
  123. exp = cmp >> 7;
  124. if (exp && (cmp & 0x60) == 0)
  125. return false;
  126. return true;
  127. }
  128. static unsigned int dc_ic_rld_quad_l1_sel(u64 event)
  129. {
  130. unsigned int cache;
  131. cache = (event >> EVENT_CACHE_SEL_SHIFT) & MMCR1_DC_IC_QUAL_MASK;
  132. return cache;
  133. }
  134. static inline u64 isa207_find_source(u64 idx, u32 sub_idx)
  135. {
  136. u64 ret = PERF_MEM_NA;
  137. switch(idx) {
  138. case 0:
  139. /* Nothing to do */
  140. break;
  141. case 1:
  142. ret = PH(LVL, L1);
  143. break;
  144. case 2:
  145. ret = PH(LVL, L2);
  146. break;
  147. case 3:
  148. ret = PH(LVL, L3);
  149. break;
  150. case 4:
  151. if (sub_idx <= 1)
  152. ret = PH(LVL, LOC_RAM);
  153. else if (sub_idx > 1 && sub_idx <= 2)
  154. ret = PH(LVL, REM_RAM1);
  155. else
  156. ret = PH(LVL, REM_RAM2);
  157. ret |= P(SNOOP, HIT);
  158. break;
  159. case 5:
  160. ret = PH(LVL, REM_CCE1);
  161. if ((sub_idx == 0) || (sub_idx == 2) || (sub_idx == 4))
  162. ret |= P(SNOOP, HIT);
  163. else if ((sub_idx == 1) || (sub_idx == 3) || (sub_idx == 5))
  164. ret |= P(SNOOP, HITM);
  165. break;
  166. case 6:
  167. ret = PH(LVL, REM_CCE2);
  168. if ((sub_idx == 0) || (sub_idx == 2))
  169. ret |= P(SNOOP, HIT);
  170. else if ((sub_idx == 1) || (sub_idx == 3))
  171. ret |= P(SNOOP, HITM);
  172. break;
  173. case 7:
  174. ret = PM(LVL, L1);
  175. break;
  176. }
  177. return ret;
  178. }
  179. void isa207_get_mem_data_src(union perf_mem_data_src *dsrc, u32 flags,
  180. struct pt_regs *regs)
  181. {
  182. u64 idx;
  183. u32 sub_idx;
  184. u64 sier;
  185. u64 val;
  186. /* Skip if no SIER support */
  187. if (!(flags & PPMU_HAS_SIER)) {
  188. dsrc->val = 0;
  189. return;
  190. }
  191. sier = mfspr(SPRN_SIER);
  192. val = (sier & ISA207_SIER_TYPE_MASK) >> ISA207_SIER_TYPE_SHIFT;
  193. if (val == 1 || val == 2) {
  194. idx = (sier & ISA207_SIER_LDST_MASK) >> ISA207_SIER_LDST_SHIFT;
  195. sub_idx = (sier & ISA207_SIER_DATA_SRC_MASK) >> ISA207_SIER_DATA_SRC_SHIFT;
  196. dsrc->val = isa207_find_source(idx, sub_idx);
  197. dsrc->val |= (val == 1) ? P(OP, LOAD) : P(OP, STORE);
  198. }
  199. }
  200. void isa207_get_mem_weight(u64 *weight)
  201. {
  202. u64 mmcra = mfspr(SPRN_MMCRA);
  203. u64 exp = MMCRA_THR_CTR_EXP(mmcra);
  204. u64 mantissa = MMCRA_THR_CTR_MANT(mmcra);
  205. u64 sier = mfspr(SPRN_SIER);
  206. u64 val = (sier & ISA207_SIER_TYPE_MASK) >> ISA207_SIER_TYPE_SHIFT;
  207. if (val == 0 || val == 7)
  208. *weight = 0;
  209. else
  210. *weight = mantissa << (2 * exp);
  211. }
  212. int isa207_get_constraint(u64 event, unsigned long *maskp, unsigned long *valp)
  213. {
  214. unsigned int unit, pmc, cache, ebb;
  215. unsigned long mask, value;
  216. mask = value = 0;
  217. if (!is_event_valid(event))
  218. return -1;
  219. pmc = (event >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  220. unit = (event >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
  221. cache = (event >> EVENT_CACHE_SEL_SHIFT) & EVENT_CACHE_SEL_MASK;
  222. ebb = (event >> EVENT_EBB_SHIFT) & EVENT_EBB_MASK;
  223. if (pmc) {
  224. u64 base_event;
  225. if (pmc > 6)
  226. return -1;
  227. /* Ignore Linux defined bits when checking event below */
  228. base_event = event & ~EVENT_LINUX_MASK;
  229. if (pmc >= 5 && base_event != 0x500fa &&
  230. base_event != 0x600f4)
  231. return -1;
  232. mask |= CNST_PMC_MASK(pmc);
  233. value |= CNST_PMC_VAL(pmc);
  234. /*
  235. * PMC5 and PMC6 are used to count cycles and instructions and
  236. * they do not support most of the constraint bits. Add a check
  237. * to exclude PMC5/6 from most of the constraints except for
  238. * EBB/BHRB.
  239. */
  240. if (pmc >= 5)
  241. goto ebb_bhrb;
  242. }
  243. if (pmc <= 4) {
  244. /*
  245. * Add to number of counters in use. Note this includes events with
  246. * a PMC of 0 - they still need a PMC, it's just assigned later.
  247. * Don't count events on PMC 5 & 6, there is only one valid event
  248. * on each of those counters, and they are handled above.
  249. */
  250. mask |= CNST_NC_MASK;
  251. value |= CNST_NC_VAL;
  252. }
  253. if (unit >= 6 && unit <= 9) {
  254. /*
  255. * L2/L3 events contain a cache selector field, which is
  256. * supposed to be programmed into MMCRC. However MMCRC is only
  257. * HV writable, and there is no API for guest kernels to modify
  258. * it. The solution is for the hypervisor to initialise the
  259. * field to zeroes, and for us to only ever allow events that
  260. * have a cache selector of zero. The bank selector (bit 3) is
  261. * irrelevant, as long as the rest of the value is 0.
  262. */
  263. if (!cpu_has_feature(CPU_FTR_ARCH_300) && (cache & 0x7))
  264. return -1;
  265. } else if (cpu_has_feature(CPU_FTR_ARCH_300) || (event & EVENT_IS_L1)) {
  266. mask |= CNST_L1_QUAL_MASK;
  267. value |= CNST_L1_QUAL_VAL(cache);
  268. }
  269. if (is_event_marked(event)) {
  270. mask |= CNST_SAMPLE_MASK;
  271. value |= CNST_SAMPLE_VAL(event >> EVENT_SAMPLE_SHIFT);
  272. }
  273. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  274. if (event_is_threshold(event) && is_thresh_cmp_valid(event)) {
  275. mask |= CNST_THRESH_MASK;
  276. value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
  277. }
  278. } else {
  279. /*
  280. * Special case for PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
  281. * the threshold control bits are used for the match value.
  282. */
  283. if (event_is_fab_match(event)) {
  284. mask |= CNST_FAB_MATCH_MASK;
  285. value |= CNST_FAB_MATCH_VAL(event >> EVENT_THR_CTL_SHIFT);
  286. } else {
  287. if (!is_thresh_cmp_valid(event))
  288. return -1;
  289. mask |= CNST_THRESH_MASK;
  290. value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
  291. }
  292. }
  293. ebb_bhrb:
  294. if (!pmc && ebb)
  295. /* EBB events must specify the PMC */
  296. return -1;
  297. if (event & EVENT_WANTS_BHRB) {
  298. if (!ebb)
  299. /* Only EBB events can request BHRB */
  300. return -1;
  301. mask |= CNST_IFM_MASK;
  302. value |= CNST_IFM_VAL(event >> EVENT_IFM_SHIFT);
  303. }
  304. /*
  305. * All events must agree on EBB, either all request it or none.
  306. * EBB events are pinned & exclusive, so this should never actually
  307. * hit, but we leave it as a fallback in case.
  308. */
  309. mask |= CNST_EBB_MASK;
  310. value |= CNST_EBB_VAL(ebb);
  311. *maskp = mask;
  312. *valp = value;
  313. return 0;
  314. }
  315. int isa207_compute_mmcr(u64 event[], int n_ev,
  316. unsigned int hwc[], unsigned long mmcr[],
  317. struct perf_event *pevents[])
  318. {
  319. unsigned long mmcra, mmcr1, mmcr2, unit, combine, psel, cache, val;
  320. unsigned int pmc, pmc_inuse;
  321. int i;
  322. pmc_inuse = 0;
  323. /* First pass to count resource use */
  324. for (i = 0; i < n_ev; ++i) {
  325. pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  326. if (pmc)
  327. pmc_inuse |= 1 << pmc;
  328. }
  329. mmcra = mmcr1 = mmcr2 = 0;
  330. /* Second pass: assign PMCs, set all MMCR1 fields */
  331. for (i = 0; i < n_ev; ++i) {
  332. pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  333. unit = (event[i] >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
  334. combine = combine_from_event(event[i]);
  335. psel = event[i] & EVENT_PSEL_MASK;
  336. if (!pmc) {
  337. for (pmc = 1; pmc <= 4; ++pmc) {
  338. if (!(pmc_inuse & (1 << pmc)))
  339. break;
  340. }
  341. pmc_inuse |= 1 << pmc;
  342. }
  343. if (pmc <= 4) {
  344. mmcr1 |= unit << MMCR1_UNIT_SHIFT(pmc);
  345. mmcr1 |= combine << combine_shift(pmc);
  346. mmcr1 |= psel << MMCR1_PMCSEL_SHIFT(pmc);
  347. }
  348. /* In continuous sampling mode, update SDAR on TLB miss */
  349. mmcra_sdar_mode(event[i], &mmcra);
  350. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  351. cache = dc_ic_rld_quad_l1_sel(event[i]);
  352. mmcr1 |= (cache) << MMCR1_DC_IC_QUAL_SHIFT;
  353. } else {
  354. if (event[i] & EVENT_IS_L1) {
  355. cache = dc_ic_rld_quad_l1_sel(event[i]);
  356. mmcr1 |= (cache) << MMCR1_DC_IC_QUAL_SHIFT;
  357. }
  358. }
  359. if (is_event_marked(event[i])) {
  360. mmcra |= MMCRA_SAMPLE_ENABLE;
  361. val = (event[i] >> EVENT_SAMPLE_SHIFT) & EVENT_SAMPLE_MASK;
  362. if (val) {
  363. mmcra |= (val & 3) << MMCRA_SAMP_MODE_SHIFT;
  364. mmcra |= (val >> 2) << MMCRA_SAMP_ELIG_SHIFT;
  365. }
  366. }
  367. /*
  368. * PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
  369. * the threshold bits are used for the match value.
  370. */
  371. if (!cpu_has_feature(CPU_FTR_ARCH_300) && event_is_fab_match(event[i])) {
  372. mmcr1 |= ((event[i] >> EVENT_THR_CTL_SHIFT) &
  373. EVENT_THR_CTL_MASK) << MMCR1_FAB_SHIFT;
  374. } else {
  375. val = (event[i] >> EVENT_THR_CTL_SHIFT) & EVENT_THR_CTL_MASK;
  376. mmcra |= val << MMCRA_THR_CTL_SHIFT;
  377. val = (event[i] >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
  378. mmcra |= val << MMCRA_THR_SEL_SHIFT;
  379. val = (event[i] >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
  380. mmcra |= thresh_cmp_val(val);
  381. }
  382. if (event[i] & EVENT_WANTS_BHRB) {
  383. val = (event[i] >> EVENT_IFM_SHIFT) & EVENT_IFM_MASK;
  384. mmcra |= val << MMCRA_IFM_SHIFT;
  385. }
  386. if (pevents[i]->attr.exclude_user)
  387. mmcr2 |= MMCR2_FCP(pmc);
  388. if (pevents[i]->attr.exclude_hv)
  389. mmcr2 |= MMCR2_FCH(pmc);
  390. if (pevents[i]->attr.exclude_kernel) {
  391. if (cpu_has_feature(CPU_FTR_HVMODE))
  392. mmcr2 |= MMCR2_FCH(pmc);
  393. else
  394. mmcr2 |= MMCR2_FCS(pmc);
  395. }
  396. hwc[i] = pmc - 1;
  397. }
  398. /* Return MMCRx values */
  399. mmcr[0] = 0;
  400. /* pmc_inuse is 1-based */
  401. if (pmc_inuse & 2)
  402. mmcr[0] = MMCR0_PMC1CE;
  403. if (pmc_inuse & 0x7c)
  404. mmcr[0] |= MMCR0_PMCjCE;
  405. /* If we're not using PMC 5 or 6, freeze them */
  406. if (!(pmc_inuse & 0x60))
  407. mmcr[0] |= MMCR0_FC56;
  408. mmcr[1] = mmcr1;
  409. mmcr[2] = mmcra;
  410. mmcr[3] = mmcr2;
  411. return 0;
  412. }
  413. void isa207_disable_pmc(unsigned int pmc, unsigned long mmcr[])
  414. {
  415. if (pmc <= 3)
  416. mmcr[1] &= ~(0xffUL << MMCR1_PMCSEL_SHIFT(pmc + 1));
  417. }
  418. static int find_alternative(u64 event, const unsigned int ev_alt[][MAX_ALT], int size)
  419. {
  420. int i, j;
  421. for (i = 0; i < size; ++i) {
  422. if (event < ev_alt[i][0])
  423. break;
  424. for (j = 0; j < MAX_ALT && ev_alt[i][j]; ++j)
  425. if (event == ev_alt[i][j])
  426. return i;
  427. }
  428. return -1;
  429. }
  430. int isa207_get_alternatives(u64 event, u64 alt[], int size, unsigned int flags,
  431. const unsigned int ev_alt[][MAX_ALT])
  432. {
  433. int i, j, num_alt = 0;
  434. u64 alt_event;
  435. alt[num_alt++] = event;
  436. i = find_alternative(event, ev_alt, size);
  437. if (i >= 0) {
  438. /* Filter out the original event, it's already in alt[0] */
  439. for (j = 0; j < MAX_ALT; ++j) {
  440. alt_event = ev_alt[i][j];
  441. if (alt_event && alt_event != event)
  442. alt[num_alt++] = alt_event;
  443. }
  444. }
  445. if (flags & PPMU_ONLY_COUNT_RUN) {
  446. /*
  447. * We're only counting in RUN state, so PM_CYC is equivalent to
  448. * PM_RUN_CYC and PM_INST_CMPL === PM_RUN_INST_CMPL.
  449. */
  450. j = num_alt;
  451. for (i = 0; i < num_alt; ++i) {
  452. switch (alt[i]) {
  453. case 0x1e: /* PMC_CYC */
  454. alt[j++] = 0x600f4; /* PM_RUN_CYC */
  455. break;
  456. case 0x600f4:
  457. alt[j++] = 0x1e;
  458. break;
  459. case 0x2: /* PM_INST_CMPL */
  460. alt[j++] = 0x500fa; /* PM_RUN_INST_CMPL */
  461. break;
  462. case 0x500fa:
  463. alt[j++] = 0x2;
  464. break;
  465. }
  466. }
  467. num_alt = j;
  468. }
  469. return num_alt;
  470. }