isa207-common.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright 2009 Paul Mackerras, IBM Corporation.
  3. * Copyright 2013 Michael Ellerman, IBM Corporation.
  4. * Copyright 2016 Madhavan Srinivasan, 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 any later version.
  10. */
  11. #ifndef _LINUX_POWERPC_PERF_ISA207_COMMON_H_
  12. #define _LINUX_POWERPC_PERF_ISA207_COMMON_H_
  13. #include <linux/kernel.h>
  14. #include <linux/perf_event.h>
  15. #include <asm/firmware.h>
  16. #include <asm/cputable.h>
  17. /*
  18. * Raw event encoding for PowerISA v2.07:
  19. *
  20. * 60 56 52 48 44 40 36 32
  21. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  22. * | | [ ] [ thresh_cmp ] [ thresh_ctl ]
  23. * | | | |
  24. * | | *- IFM (Linux) thresh start/stop OR FAB match -*
  25. * | *- BHRB (Linux)
  26. * *- EBB (Linux)
  27. *
  28. * 28 24 20 16 12 8 4 0
  29. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  30. * [ ] [ sample ] [cache] [ pmc ] [unit ] c m [ pmcxsel ]
  31. * | | | | |
  32. * | | | | *- mark
  33. * | | *- L1/L2/L3 cache_sel |
  34. * | | |
  35. * | *- sampling mode for marked events *- combine
  36. * |
  37. * *- thresh_sel
  38. *
  39. * Below uses IBM bit numbering.
  40. *
  41. * MMCR1[x:y] = unit (PMCxUNIT)
  42. * MMCR1[x] = combine (PMCxCOMB)
  43. *
  44. * if pmc == 3 and unit == 0 and pmcxsel[0:6] == 0b0101011
  45. * # PM_MRK_FAB_RSP_MATCH
  46. * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
  47. * else if pmc == 4 and unit == 0xf and pmcxsel[0:6] == 0b0101001
  48. * # PM_MRK_FAB_RSP_MATCH_CYC
  49. * MMCR1[20:27] = thresh_ctl (FAB_CRESP_MATCH / FAB_TYPE_MATCH)
  50. * else
  51. * MMCRA[48:55] = thresh_ctl (THRESH START/END)
  52. *
  53. * if thresh_sel:
  54. * MMCRA[45:47] = thresh_sel
  55. *
  56. * if thresh_cmp:
  57. * MMCRA[22:24] = thresh_cmp[0:2]
  58. * MMCRA[25:31] = thresh_cmp[3:9]
  59. *
  60. * if unit == 6 or unit == 7
  61. * MMCRC[53:55] = cache_sel[1:3] (L2EVENT_SEL)
  62. * else if unit == 8 or unit == 9:
  63. * if cache_sel[0] == 0: # L3 bank
  64. * MMCRC[47:49] = cache_sel[1:3] (L3EVENT_SEL0)
  65. * else if cache_sel[0] == 1:
  66. * MMCRC[50:51] = cache_sel[2:3] (L3EVENT_SEL1)
  67. * else if cache_sel[1]: # L1 event
  68. * MMCR1[16] = cache_sel[2]
  69.  * MMCR1[17] = cache_sel[3]
  70. *
  71. * if mark:
  72. * MMCRA[63] = 1 (SAMPLE_ENABLE)
  73. * MMCRA[57:59] = sample[0:2] (RAND_SAMP_ELIG)
  74.  * MMCRA[61:62] = sample[3:4] (RAND_SAMP_MODE)
  75. *
  76. * if EBB and BHRB:
  77. * MMCRA[32:33] = IFM
  78. *
  79. */
  80. #define EVENT_EBB_MASK 1ull
  81. #define EVENT_EBB_SHIFT PERF_EVENT_CONFIG_EBB_SHIFT
  82. #define EVENT_BHRB_MASK 1ull
  83. #define EVENT_BHRB_SHIFT 62
  84. #define EVENT_WANTS_BHRB (EVENT_BHRB_MASK << EVENT_BHRB_SHIFT)
  85. #define EVENT_IFM_MASK 3ull
  86. #define EVENT_IFM_SHIFT 60
  87. #define EVENT_THR_CMP_SHIFT 40 /* Threshold CMP value */
  88. #define EVENT_THR_CMP_MASK 0x3ff
  89. #define EVENT_THR_CTL_SHIFT 32 /* Threshold control value (start/stop) */
  90. #define EVENT_THR_CTL_MASK 0xffull
  91. #define EVENT_THR_SEL_SHIFT 29 /* Threshold select value */
  92. #define EVENT_THR_SEL_MASK 0x7
  93. #define EVENT_THRESH_SHIFT 29 /* All threshold bits */
  94. #define EVENT_THRESH_MASK 0x1fffffull
  95. #define EVENT_SAMPLE_SHIFT 24 /* Sampling mode & eligibility */
  96. #define EVENT_SAMPLE_MASK 0x1f
  97. #define EVENT_CACHE_SEL_SHIFT 20 /* L2/L3 cache select */
  98. #define EVENT_CACHE_SEL_MASK 0xf
  99. #define EVENT_IS_L1 (4 << EVENT_CACHE_SEL_SHIFT)
  100. #define EVENT_PMC_SHIFT 16 /* PMC number (1-based) */
  101. #define EVENT_PMC_MASK 0xf
  102. #define EVENT_UNIT_SHIFT 12 /* Unit */
  103. #define EVENT_UNIT_MASK 0xf
  104. #define EVENT_COMBINE_SHIFT 11 /* Combine bit */
  105. #define EVENT_COMBINE_MASK 0x1
  106. #define EVENT_COMBINE(v) (((v) >> EVENT_COMBINE_SHIFT) & EVENT_COMBINE_MASK)
  107. #define EVENT_MARKED_SHIFT 8 /* Marked bit */
  108. #define EVENT_MARKED_MASK 0x1
  109. #define EVENT_IS_MARKED (EVENT_MARKED_MASK << EVENT_MARKED_SHIFT)
  110. #define EVENT_PSEL_MASK 0xff /* PMCxSEL value */
  111. /* Bits defined by Linux */
  112. #define EVENT_LINUX_MASK \
  113. ((EVENT_EBB_MASK << EVENT_EBB_SHIFT) | \
  114. (EVENT_BHRB_MASK << EVENT_BHRB_SHIFT) | \
  115. (EVENT_IFM_MASK << EVENT_IFM_SHIFT))
  116. #define EVENT_VALID_MASK \
  117. ((EVENT_THRESH_MASK << EVENT_THRESH_SHIFT) | \
  118. (EVENT_SAMPLE_MASK << EVENT_SAMPLE_SHIFT) | \
  119. (EVENT_CACHE_SEL_MASK << EVENT_CACHE_SEL_SHIFT) | \
  120. (EVENT_PMC_MASK << EVENT_PMC_SHIFT) | \
  121. (EVENT_UNIT_MASK << EVENT_UNIT_SHIFT) | \
  122. (EVENT_COMBINE_MASK << EVENT_COMBINE_SHIFT) | \
  123. (EVENT_MARKED_MASK << EVENT_MARKED_SHIFT) | \
  124. EVENT_LINUX_MASK | \
  125. EVENT_PSEL_MASK)
  126. #define ONLY_PLM \
  127. (PERF_SAMPLE_BRANCH_USER |\
  128. PERF_SAMPLE_BRANCH_KERNEL |\
  129. PERF_SAMPLE_BRANCH_HV)
  130. /* Contants to support power9 raw encoding format */
  131. #define p9_EVENT_COMBINE_SHIFT 10 /* Combine bit */
  132. #define p9_EVENT_COMBINE_MASK 0x3ull
  133. #define p9_EVENT_COMBINE(v) (((v) >> p9_EVENT_COMBINE_SHIFT) & p9_EVENT_COMBINE_MASK)
  134. #define p9_SDAR_MODE_SHIFT 50
  135. #define p9_SDAR_MODE_MASK 0x3ull
  136. #define p9_SDAR_MODE(v) (((v) >> p9_SDAR_MODE_SHIFT) & p9_SDAR_MODE_MASK)
  137. #define p9_EVENT_VALID_MASK \
  138. ((p9_SDAR_MODE_MASK << p9_SDAR_MODE_SHIFT | \
  139. (EVENT_THRESH_MASK << EVENT_THRESH_SHIFT) | \
  140. (EVENT_SAMPLE_MASK << EVENT_SAMPLE_SHIFT) | \
  141. (EVENT_CACHE_SEL_MASK << EVENT_CACHE_SEL_SHIFT) | \
  142. (EVENT_PMC_MASK << EVENT_PMC_SHIFT) | \
  143. (EVENT_UNIT_MASK << EVENT_UNIT_SHIFT) | \
  144. (p9_EVENT_COMBINE_MASK << p9_EVENT_COMBINE_SHIFT) | \
  145. (EVENT_MARKED_MASK << EVENT_MARKED_SHIFT) | \
  146. EVENT_LINUX_MASK | \
  147. EVENT_PSEL_MASK))
  148. /*
  149. * Layout of constraint bits:
  150. *
  151. * 60 56 52 48 44 40 36 32
  152. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  153. * [ fab_match ] [ thresh_cmp ] [ thresh_ctl ] [ ]
  154. * |
  155. * thresh_sel -*
  156. *
  157. * 28 24 20 16 12 8 4 0
  158. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  159. * [ ] | [ ] [ sample ] [ ] [6] [5] [4] [3] [2] [1]
  160. * | | | |
  161. * BHRB IFM -* | | | Count of events for each PMC.
  162. * EBB -* | | p1, p2, p3, p4, p5, p6.
  163. * L1 I/D qualifier -* |
  164. * nc - number of counters -*
  165. *
  166. * The PMC fields P1..P6, and NC, are adder fields. As we accumulate constraints
  167. * we want the low bit of each field to be added to any existing value.
  168. *
  169. * Everything else is a value field.
  170. */
  171. #define CNST_FAB_MATCH_VAL(v) (((v) & EVENT_THR_CTL_MASK) << 56)
  172. #define CNST_FAB_MATCH_MASK CNST_FAB_MATCH_VAL(EVENT_THR_CTL_MASK)
  173. /* We just throw all the threshold bits into the constraint */
  174. #define CNST_THRESH_VAL(v) (((v) & EVENT_THRESH_MASK) << 32)
  175. #define CNST_THRESH_MASK CNST_THRESH_VAL(EVENT_THRESH_MASK)
  176. #define CNST_EBB_VAL(v) (((v) & EVENT_EBB_MASK) << 24)
  177. #define CNST_EBB_MASK CNST_EBB_VAL(EVENT_EBB_MASK)
  178. #define CNST_IFM_VAL(v) (((v) & EVENT_IFM_MASK) << 25)
  179. #define CNST_IFM_MASK CNST_IFM_VAL(EVENT_IFM_MASK)
  180. #define CNST_L1_QUAL_VAL(v) (((v) & 3) << 22)
  181. #define CNST_L1_QUAL_MASK CNST_L1_QUAL_VAL(3)
  182. #define CNST_SAMPLE_VAL(v) (((v) & EVENT_SAMPLE_MASK) << 16)
  183. #define CNST_SAMPLE_MASK CNST_SAMPLE_VAL(EVENT_SAMPLE_MASK)
  184. /*
  185. * For NC we are counting up to 4 events. This requires three bits, and we need
  186. * the fifth event to overflow and set the 4th bit. To achieve that we bias the
  187. * fields by 3 in test_adder.
  188. */
  189. #define CNST_NC_SHIFT 12
  190. #define CNST_NC_VAL (1 << CNST_NC_SHIFT)
  191. #define CNST_NC_MASK (8 << CNST_NC_SHIFT)
  192. #define ISA207_TEST_ADDER (3 << CNST_NC_SHIFT)
  193. /*
  194. * For the per-PMC fields we have two bits. The low bit is added, so if two
  195. * events ask for the same PMC the sum will overflow, setting the high bit,
  196. * indicating an error. So our mask sets the high bit.
  197. */
  198. #define CNST_PMC_SHIFT(pmc) ((pmc - 1) * 2)
  199. #define CNST_PMC_VAL(pmc) (1 << CNST_PMC_SHIFT(pmc))
  200. #define CNST_PMC_MASK(pmc) (2 << CNST_PMC_SHIFT(pmc))
  201. /* Our add_fields is defined as: */
  202. #define ISA207_ADD_FIELDS \
  203. CNST_PMC_VAL(1) | CNST_PMC_VAL(2) | CNST_PMC_VAL(3) | \
  204. CNST_PMC_VAL(4) | CNST_PMC_VAL(5) | CNST_PMC_VAL(6) | CNST_NC_VAL
  205. /*
  206. * Lets restrict use of PMC5 for instruction counting.
  207. */
  208. #define P9_DD1_TEST_ADDER (ISA207_TEST_ADDER | CNST_PMC_VAL(5))
  209. /* Bits in MMCR1 for PowerISA v2.07 */
  210. #define MMCR1_UNIT_SHIFT(pmc) (60 - (4 * ((pmc) - 1)))
  211. #define MMCR1_COMBINE_SHIFT(pmc) (35 - ((pmc) - 1))
  212. #define MMCR1_PMCSEL_SHIFT(pmc) (24 - (((pmc) - 1)) * 8)
  213. #define MMCR1_FAB_SHIFT 36
  214. #define MMCR1_DC_QUAL_SHIFT 47
  215. #define MMCR1_IC_QUAL_SHIFT 46
  216. /* MMCR1 Combine bits macro for power9 */
  217. #define p9_MMCR1_COMBINE_SHIFT(pmc) (38 - ((pmc - 1) * 2))
  218. /* Bits in MMCRA for PowerISA v2.07 */
  219. #define MMCRA_SAMP_MODE_SHIFT 1
  220. #define MMCRA_SAMP_ELIG_SHIFT 4
  221. #define MMCRA_THR_CTL_SHIFT 8
  222. #define MMCRA_THR_SEL_SHIFT 16
  223. #define MMCRA_THR_CMP_SHIFT 32
  224. #define MMCRA_SDAR_MODE_SHIFT 42
  225. #define MMCRA_SDAR_MODE_TLB (1ull << MMCRA_SDAR_MODE_SHIFT)
  226. #define MMCRA_IFM_SHIFT 30
  227. /* MMCR1 Threshold Compare bit constant for power9 */
  228. #define p9_MMCRA_THR_CMP_SHIFT 45
  229. /* Bits in MMCR2 for PowerISA v2.07 */
  230. #define MMCR2_FCS(pmc) (1ull << (63 - (((pmc) - 1) * 9)))
  231. #define MMCR2_FCP(pmc) (1ull << (62 - (((pmc) - 1) * 9)))
  232. #define MMCR2_FCH(pmc) (1ull << (57 - (((pmc) - 1) * 9)))
  233. #define MAX_ALT 2
  234. #define MAX_PMU_COUNTERS 6
  235. int isa207_get_constraint(u64 event, unsigned long *maskp, unsigned long *valp);
  236. int isa207_compute_mmcr(u64 event[], int n_ev,
  237. unsigned int hwc[], unsigned long mmcr[],
  238. struct perf_event *pevents[]);
  239. void isa207_disable_pmc(unsigned int pmc, unsigned long mmcr[]);
  240. int isa207_get_alternatives(u64 event, u64 alt[],
  241. const unsigned int ev_alt[][MAX_ALT], int size);
  242. #endif