isa207-common.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. static inline bool event_is_fab_match(u64 event)
  15. {
  16. /* Only check pmc, unit and pmcxsel, ignore the edge bit (0) */
  17. event &= 0xff0fe;
  18. /* PM_MRK_FAB_RSP_MATCH & PM_MRK_FAB_RSP_MATCH_CYC */
  19. return (event == 0x30056 || event == 0x4f052);
  20. }
  21. int isa207_get_constraint(u64 event, unsigned long *maskp, unsigned long *valp)
  22. {
  23. unsigned int unit, pmc, cache, ebb;
  24. unsigned long mask, value;
  25. mask = value = 0;
  26. if (event & ~EVENT_VALID_MASK)
  27. return -1;
  28. pmc = (event >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  29. unit = (event >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
  30. cache = (event >> EVENT_CACHE_SEL_SHIFT) & EVENT_CACHE_SEL_MASK;
  31. ebb = (event >> EVENT_EBB_SHIFT) & EVENT_EBB_MASK;
  32. if (pmc) {
  33. u64 base_event;
  34. if (pmc > 6)
  35. return -1;
  36. /* Ignore Linux defined bits when checking event below */
  37. base_event = event & ~EVENT_LINUX_MASK;
  38. if (pmc >= 5 && base_event != 0x500fa &&
  39. base_event != 0x600f4)
  40. return -1;
  41. mask |= CNST_PMC_MASK(pmc);
  42. value |= CNST_PMC_VAL(pmc);
  43. }
  44. if (pmc <= 4) {
  45. /*
  46. * Add to number of counters in use. Note this includes events with
  47. * a PMC of 0 - they still need a PMC, it's just assigned later.
  48. * Don't count events on PMC 5 & 6, there is only one valid event
  49. * on each of those counters, and they are handled above.
  50. */
  51. mask |= CNST_NC_MASK;
  52. value |= CNST_NC_VAL;
  53. }
  54. if (unit >= 6 && unit <= 9) {
  55. /*
  56. * L2/L3 events contain a cache selector field, which is
  57. * supposed to be programmed into MMCRC. However MMCRC is only
  58. * HV writable, and there is no API for guest kernels to modify
  59. * it. The solution is for the hypervisor to initialise the
  60. * field to zeroes, and for us to only ever allow events that
  61. * have a cache selector of zero. The bank selector (bit 3) is
  62. * irrelevant, as long as the rest of the value is 0.
  63. */
  64. if (cache & 0x7)
  65. return -1;
  66. } else if (event & EVENT_IS_L1) {
  67. mask |= CNST_L1_QUAL_MASK;
  68. value |= CNST_L1_QUAL_VAL(cache);
  69. }
  70. if (event & EVENT_IS_MARKED) {
  71. mask |= CNST_SAMPLE_MASK;
  72. value |= CNST_SAMPLE_VAL(event >> EVENT_SAMPLE_SHIFT);
  73. }
  74. /*
  75. * Special case for PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
  76. * the threshold control bits are used for the match value.
  77. */
  78. if (event_is_fab_match(event)) {
  79. mask |= CNST_FAB_MATCH_MASK;
  80. value |= CNST_FAB_MATCH_VAL(event >> EVENT_THR_CTL_SHIFT);
  81. } else {
  82. /*
  83. * Check the mantissa upper two bits are not zero, unless the
  84. * exponent is also zero. See the THRESH_CMP_MANTISSA doc.
  85. */
  86. unsigned int cmp, exp;
  87. cmp = (event >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
  88. exp = cmp >> 7;
  89. if (exp && (cmp & 0x60) == 0)
  90. return -1;
  91. mask |= CNST_THRESH_MASK;
  92. value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
  93. }
  94. if (!pmc && ebb)
  95. /* EBB events must specify the PMC */
  96. return -1;
  97. if (event & EVENT_WANTS_BHRB) {
  98. if (!ebb)
  99. /* Only EBB events can request BHRB */
  100. return -1;
  101. mask |= CNST_IFM_MASK;
  102. value |= CNST_IFM_VAL(event >> EVENT_IFM_SHIFT);
  103. }
  104. /*
  105. * All events must agree on EBB, either all request it or none.
  106. * EBB events are pinned & exclusive, so this should never actually
  107. * hit, but we leave it as a fallback in case.
  108. */
  109. mask |= CNST_EBB_VAL(ebb);
  110. value |= CNST_EBB_MASK;
  111. *maskp = mask;
  112. *valp = value;
  113. return 0;
  114. }
  115. int isa207_compute_mmcr(u64 event[], int n_ev,
  116. unsigned int hwc[], unsigned long mmcr[],
  117. struct perf_event *pevents[])
  118. {
  119. unsigned long mmcra, mmcr1, mmcr2, unit, combine, psel, cache, val;
  120. unsigned int pmc, pmc_inuse;
  121. int i;
  122. pmc_inuse = 0;
  123. /* First pass to count resource use */
  124. for (i = 0; i < n_ev; ++i) {
  125. pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  126. if (pmc)
  127. pmc_inuse |= 1 << pmc;
  128. }
  129. /* In continuous sampling mode, update SDAR on TLB miss */
  130. mmcra = MMCRA_SDAR_MODE_TLB;
  131. mmcr1 = mmcr2 = 0;
  132. /* Second pass: assign PMCs, set all MMCR1 fields */
  133. for (i = 0; i < n_ev; ++i) {
  134. pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  135. unit = (event[i] >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
  136. combine = (event[i] >> EVENT_COMBINE_SHIFT) & EVENT_COMBINE_MASK;
  137. psel = event[i] & EVENT_PSEL_MASK;
  138. if (!pmc) {
  139. for (pmc = 1; pmc <= 4; ++pmc) {
  140. if (!(pmc_inuse & (1 << pmc)))
  141. break;
  142. }
  143. pmc_inuse |= 1 << pmc;
  144. }
  145. if (pmc <= 4) {
  146. mmcr1 |= unit << MMCR1_UNIT_SHIFT(pmc);
  147. mmcr1 |= combine << MMCR1_COMBINE_SHIFT(pmc);
  148. mmcr1 |= psel << MMCR1_PMCSEL_SHIFT(pmc);
  149. }
  150. if (event[i] & EVENT_IS_L1) {
  151. cache = event[i] >> EVENT_CACHE_SEL_SHIFT;
  152. mmcr1 |= (cache & 1) << MMCR1_IC_QUAL_SHIFT;
  153. cache >>= 1;
  154. mmcr1 |= (cache & 1) << MMCR1_DC_QUAL_SHIFT;
  155. }
  156. if (event[i] & EVENT_IS_MARKED) {
  157. mmcra |= MMCRA_SAMPLE_ENABLE;
  158. val = (event[i] >> EVENT_SAMPLE_SHIFT) & EVENT_SAMPLE_MASK;
  159. if (val) {
  160. mmcra |= (val & 3) << MMCRA_SAMP_MODE_SHIFT;
  161. mmcra |= (val >> 2) << MMCRA_SAMP_ELIG_SHIFT;
  162. }
  163. }
  164. /*
  165. * PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
  166. * the threshold bits are used for the match value.
  167. */
  168. if (event_is_fab_match(event[i])) {
  169. mmcr1 |= ((event[i] >> EVENT_THR_CTL_SHIFT) &
  170. EVENT_THR_CTL_MASK) << MMCR1_FAB_SHIFT;
  171. } else {
  172. val = (event[i] >> EVENT_THR_CTL_SHIFT) & EVENT_THR_CTL_MASK;
  173. mmcra |= val << MMCRA_THR_CTL_SHIFT;
  174. val = (event[i] >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
  175. mmcra |= val << MMCRA_THR_SEL_SHIFT;
  176. val = (event[i] >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
  177. mmcra |= val << MMCRA_THR_CMP_SHIFT;
  178. }
  179. if (event[i] & EVENT_WANTS_BHRB) {
  180. val = (event[i] >> EVENT_IFM_SHIFT) & EVENT_IFM_MASK;
  181. mmcra |= val << MMCRA_IFM_SHIFT;
  182. }
  183. if (pevents[i]->attr.exclude_user)
  184. mmcr2 |= MMCR2_FCP(pmc);
  185. if (pevents[i]->attr.exclude_hv)
  186. mmcr2 |= MMCR2_FCH(pmc);
  187. if (pevents[i]->attr.exclude_kernel) {
  188. if (cpu_has_feature(CPU_FTR_HVMODE))
  189. mmcr2 |= MMCR2_FCH(pmc);
  190. else
  191. mmcr2 |= MMCR2_FCS(pmc);
  192. }
  193. hwc[i] = pmc - 1;
  194. }
  195. /* Return MMCRx values */
  196. mmcr[0] = 0;
  197. /* pmc_inuse is 1-based */
  198. if (pmc_inuse & 2)
  199. mmcr[0] = MMCR0_PMC1CE;
  200. if (pmc_inuse & 0x7c)
  201. mmcr[0] |= MMCR0_PMCjCE;
  202. /* If we're not using PMC 5 or 6, freeze them */
  203. if (!(pmc_inuse & 0x60))
  204. mmcr[0] |= MMCR0_FC56;
  205. mmcr[1] = mmcr1;
  206. mmcr[2] = mmcra;
  207. mmcr[3] = mmcr2;
  208. return 0;
  209. }
  210. void isa207_disable_pmc(unsigned int pmc, unsigned long mmcr[])
  211. {
  212. if (pmc <= 3)
  213. mmcr[1] &= ~(0xffUL << MMCR1_PMCSEL_SHIFT(pmc + 1));
  214. }