isa207-common.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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_MARKED_SHIFT 8 /* Marked bit */
  107. #define EVENT_MARKED_MASK 0x1
  108. #define EVENT_IS_MARKED (EVENT_MARKED_MASK << EVENT_MARKED_SHIFT)
  109. #define EVENT_PSEL_MASK 0xff /* PMCxSEL value */
  110. /* Bits defined by Linux */
  111. #define EVENT_LINUX_MASK \
  112. ((EVENT_EBB_MASK << EVENT_EBB_SHIFT) | \
  113. (EVENT_BHRB_MASK << EVENT_BHRB_SHIFT) | \
  114. (EVENT_IFM_MASK << EVENT_IFM_SHIFT))
  115. #define EVENT_VALID_MASK \
  116. ((EVENT_THRESH_MASK << EVENT_THRESH_SHIFT) | \
  117. (EVENT_SAMPLE_MASK << EVENT_SAMPLE_SHIFT) | \
  118. (EVENT_CACHE_SEL_MASK << EVENT_CACHE_SEL_SHIFT) | \
  119. (EVENT_PMC_MASK << EVENT_PMC_SHIFT) | \
  120. (EVENT_UNIT_MASK << EVENT_UNIT_SHIFT) | \
  121. (EVENT_COMBINE_MASK << EVENT_COMBINE_SHIFT) | \
  122. (EVENT_MARKED_MASK << EVENT_MARKED_SHIFT) | \
  123. EVENT_LINUX_MASK | \
  124. EVENT_PSEL_MASK)
  125. #define ONLY_PLM \
  126. (PERF_SAMPLE_BRANCH_USER |\
  127. PERF_SAMPLE_BRANCH_KERNEL |\
  128. PERF_SAMPLE_BRANCH_HV)
  129. /*
  130. * Layout of constraint bits:
  131. *
  132. * 60 56 52 48 44 40 36 32
  133. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  134. * [ fab_match ] [ thresh_cmp ] [ thresh_ctl ] [ ]
  135. * |
  136. * thresh_sel -*
  137. *
  138. * 28 24 20 16 12 8 4 0
  139. * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  140. * [ ] | [ ] [ sample ] [ ] [6] [5] [4] [3] [2] [1]
  141. * | | | |
  142. * BHRB IFM -* | | | Count of events for each PMC.
  143. * EBB -* | | p1, p2, p3, p4, p5, p6.
  144. * L1 I/D qualifier -* |
  145. * nc - number of counters -*
  146. *
  147. * The PMC fields P1..P6, and NC, are adder fields. As we accumulate constraints
  148. * we want the low bit of each field to be added to any existing value.
  149. *
  150. * Everything else is a value field.
  151. */
  152. #define CNST_FAB_MATCH_VAL(v) (((v) & EVENT_THR_CTL_MASK) << 56)
  153. #define CNST_FAB_MATCH_MASK CNST_FAB_MATCH_VAL(EVENT_THR_CTL_MASK)
  154. /* We just throw all the threshold bits into the constraint */
  155. #define CNST_THRESH_VAL(v) (((v) & EVENT_THRESH_MASK) << 32)
  156. #define CNST_THRESH_MASK CNST_THRESH_VAL(EVENT_THRESH_MASK)
  157. #define CNST_EBB_VAL(v) (((v) & EVENT_EBB_MASK) << 24)
  158. #define CNST_EBB_MASK CNST_EBB_VAL(EVENT_EBB_MASK)
  159. #define CNST_IFM_VAL(v) (((v) & EVENT_IFM_MASK) << 25)
  160. #define CNST_IFM_MASK CNST_IFM_VAL(EVENT_IFM_MASK)
  161. #define CNST_L1_QUAL_VAL(v) (((v) & 3) << 22)
  162. #define CNST_L1_QUAL_MASK CNST_L1_QUAL_VAL(3)
  163. #define CNST_SAMPLE_VAL(v) (((v) & EVENT_SAMPLE_MASK) << 16)
  164. #define CNST_SAMPLE_MASK CNST_SAMPLE_VAL(EVENT_SAMPLE_MASK)
  165. /*
  166. * For NC we are counting up to 4 events. This requires three bits, and we need
  167. * the fifth event to overflow and set the 4th bit. To achieve that we bias the
  168. * fields by 3 in test_adder.
  169. */
  170. #define CNST_NC_SHIFT 12
  171. #define CNST_NC_VAL (1 << CNST_NC_SHIFT)
  172. #define CNST_NC_MASK (8 << CNST_NC_SHIFT)
  173. #define ISA207_TEST_ADDER (3 << CNST_NC_SHIFT)
  174. /*
  175. * For the per-PMC fields we have two bits. The low bit is added, so if two
  176. * events ask for the same PMC the sum will overflow, setting the high bit,
  177. * indicating an error. So our mask sets the high bit.
  178. */
  179. #define CNST_PMC_SHIFT(pmc) ((pmc - 1) * 2)
  180. #define CNST_PMC_VAL(pmc) (1 << CNST_PMC_SHIFT(pmc))
  181. #define CNST_PMC_MASK(pmc) (2 << CNST_PMC_SHIFT(pmc))
  182. /* Our add_fields is defined as: */
  183. #define ISA207_ADD_FIELDS \
  184. CNST_PMC_VAL(1) | CNST_PMC_VAL(2) | CNST_PMC_VAL(3) | \
  185. CNST_PMC_VAL(4) | CNST_PMC_VAL(5) | CNST_PMC_VAL(6) | CNST_NC_VAL
  186. /*
  187. * Lets restrict use of PMC5 for instruction counting.
  188. */
  189. #define P9_DD1_TEST_ADDER (ISA207_TEST_ADDER | CNST_PMC_VAL(5))
  190. /* Bits in MMCR1 for PowerISA v2.07 */
  191. #define MMCR1_UNIT_SHIFT(pmc) (60 - (4 * ((pmc) - 1)))
  192. #define MMCR1_COMBINE_SHIFT(pmc) (35 - ((pmc) - 1))
  193. #define MMCR1_PMCSEL_SHIFT(pmc) (24 - (((pmc) - 1)) * 8)
  194. #define MMCR1_FAB_SHIFT 36
  195. #define MMCR1_DC_QUAL_SHIFT 47
  196. #define MMCR1_IC_QUAL_SHIFT 46
  197. /* Bits in MMCRA for PowerISA v2.07 */
  198. #define MMCRA_SAMP_MODE_SHIFT 1
  199. #define MMCRA_SAMP_ELIG_SHIFT 4
  200. #define MMCRA_THR_CTL_SHIFT 8
  201. #define MMCRA_THR_SEL_SHIFT 16
  202. #define MMCRA_THR_CMP_SHIFT 32
  203. #define MMCRA_SDAR_MODE_TLB (1ull << 42)
  204. #define MMCRA_IFM_SHIFT 30
  205. /* Bits in MMCR2 for PowerISA v2.07 */
  206. #define MMCR2_FCS(pmc) (1ull << (63 - (((pmc) - 1) * 9)))
  207. #define MMCR2_FCP(pmc) (1ull << (62 - (((pmc) - 1) * 9)))
  208. #define MMCR2_FCH(pmc) (1ull << (57 - (((pmc) - 1) * 9)))
  209. #define MAX_ALT 2
  210. #define MAX_PMU_COUNTERS 6
  211. int isa207_get_constraint(u64 event, unsigned long *maskp, unsigned long *valp);
  212. int isa207_compute_mmcr(u64 event[], int n_ev,
  213. unsigned int hwc[], unsigned long mmcr[],
  214. struct perf_event *pevents[]);
  215. void isa207_disable_pmc(unsigned int pmc, unsigned long mmcr[]);
  216. #endif