uncore_nhmex.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Nehalem-EX/Westmere-EX uncore support */
  3. #include "uncore.h"
  4. /* NHM-EX event control */
  5. #define NHMEX_PMON_CTL_EV_SEL_MASK 0x000000ff
  6. #define NHMEX_PMON_CTL_UMASK_MASK 0x0000ff00
  7. #define NHMEX_PMON_CTL_EN_BIT0 (1 << 0)
  8. #define NHMEX_PMON_CTL_EDGE_DET (1 << 18)
  9. #define NHMEX_PMON_CTL_PMI_EN (1 << 20)
  10. #define NHMEX_PMON_CTL_EN_BIT22 (1 << 22)
  11. #define NHMEX_PMON_CTL_INVERT (1 << 23)
  12. #define NHMEX_PMON_CTL_TRESH_MASK 0xff000000
  13. #define NHMEX_PMON_RAW_EVENT_MASK (NHMEX_PMON_CTL_EV_SEL_MASK | \
  14. NHMEX_PMON_CTL_UMASK_MASK | \
  15. NHMEX_PMON_CTL_EDGE_DET | \
  16. NHMEX_PMON_CTL_INVERT | \
  17. NHMEX_PMON_CTL_TRESH_MASK)
  18. /* NHM-EX Ubox */
  19. #define NHMEX_U_MSR_PMON_GLOBAL_CTL 0xc00
  20. #define NHMEX_U_MSR_PMON_CTR 0xc11
  21. #define NHMEX_U_MSR_PMON_EV_SEL 0xc10
  22. #define NHMEX_U_PMON_GLOBAL_EN (1 << 0)
  23. #define NHMEX_U_PMON_GLOBAL_PMI_CORE_SEL 0x0000001e
  24. #define NHMEX_U_PMON_GLOBAL_EN_ALL (1 << 28)
  25. #define NHMEX_U_PMON_GLOBAL_RST_ALL (1 << 29)
  26. #define NHMEX_U_PMON_GLOBAL_FRZ_ALL (1 << 31)
  27. #define NHMEX_U_PMON_RAW_EVENT_MASK \
  28. (NHMEX_PMON_CTL_EV_SEL_MASK | \
  29. NHMEX_PMON_CTL_EDGE_DET)
  30. /* NHM-EX Cbox */
  31. #define NHMEX_C0_MSR_PMON_GLOBAL_CTL 0xd00
  32. #define NHMEX_C0_MSR_PMON_CTR0 0xd11
  33. #define NHMEX_C0_MSR_PMON_EV_SEL0 0xd10
  34. #define NHMEX_C_MSR_OFFSET 0x20
  35. /* NHM-EX Bbox */
  36. #define NHMEX_B0_MSR_PMON_GLOBAL_CTL 0xc20
  37. #define NHMEX_B0_MSR_PMON_CTR0 0xc31
  38. #define NHMEX_B0_MSR_PMON_CTL0 0xc30
  39. #define NHMEX_B_MSR_OFFSET 0x40
  40. #define NHMEX_B0_MSR_MATCH 0xe45
  41. #define NHMEX_B0_MSR_MASK 0xe46
  42. #define NHMEX_B1_MSR_MATCH 0xe4d
  43. #define NHMEX_B1_MSR_MASK 0xe4e
  44. #define NHMEX_B_PMON_CTL_EN (1 << 0)
  45. #define NHMEX_B_PMON_CTL_EV_SEL_SHIFT 1
  46. #define NHMEX_B_PMON_CTL_EV_SEL_MASK \
  47. (0x1f << NHMEX_B_PMON_CTL_EV_SEL_SHIFT)
  48. #define NHMEX_B_PMON_CTR_SHIFT 6
  49. #define NHMEX_B_PMON_CTR_MASK \
  50. (0x3 << NHMEX_B_PMON_CTR_SHIFT)
  51. #define NHMEX_B_PMON_RAW_EVENT_MASK \
  52. (NHMEX_B_PMON_CTL_EV_SEL_MASK | \
  53. NHMEX_B_PMON_CTR_MASK)
  54. /* NHM-EX Sbox */
  55. #define NHMEX_S0_MSR_PMON_GLOBAL_CTL 0xc40
  56. #define NHMEX_S0_MSR_PMON_CTR0 0xc51
  57. #define NHMEX_S0_MSR_PMON_CTL0 0xc50
  58. #define NHMEX_S_MSR_OFFSET 0x80
  59. #define NHMEX_S0_MSR_MM_CFG 0xe48
  60. #define NHMEX_S0_MSR_MATCH 0xe49
  61. #define NHMEX_S0_MSR_MASK 0xe4a
  62. #define NHMEX_S1_MSR_MM_CFG 0xe58
  63. #define NHMEX_S1_MSR_MATCH 0xe59
  64. #define NHMEX_S1_MSR_MASK 0xe5a
  65. #define NHMEX_S_PMON_MM_CFG_EN (0x1ULL << 63)
  66. #define NHMEX_S_EVENT_TO_R_PROG_EV 0
  67. /* NHM-EX Mbox */
  68. #define NHMEX_M0_MSR_GLOBAL_CTL 0xca0
  69. #define NHMEX_M0_MSR_PMU_DSP 0xca5
  70. #define NHMEX_M0_MSR_PMU_ISS 0xca6
  71. #define NHMEX_M0_MSR_PMU_MAP 0xca7
  72. #define NHMEX_M0_MSR_PMU_MSC_THR 0xca8
  73. #define NHMEX_M0_MSR_PMU_PGT 0xca9
  74. #define NHMEX_M0_MSR_PMU_PLD 0xcaa
  75. #define NHMEX_M0_MSR_PMU_ZDP_CTL_FVC 0xcab
  76. #define NHMEX_M0_MSR_PMU_CTL0 0xcb0
  77. #define NHMEX_M0_MSR_PMU_CNT0 0xcb1
  78. #define NHMEX_M_MSR_OFFSET 0x40
  79. #define NHMEX_M0_MSR_PMU_MM_CFG 0xe54
  80. #define NHMEX_M1_MSR_PMU_MM_CFG 0xe5c
  81. #define NHMEX_M_PMON_MM_CFG_EN (1ULL << 63)
  82. #define NHMEX_M_PMON_ADDR_MATCH_MASK 0x3ffffffffULL
  83. #define NHMEX_M_PMON_ADDR_MASK_MASK 0x7ffffffULL
  84. #define NHMEX_M_PMON_ADDR_MASK_SHIFT 34
  85. #define NHMEX_M_PMON_CTL_EN (1 << 0)
  86. #define NHMEX_M_PMON_CTL_PMI_EN (1 << 1)
  87. #define NHMEX_M_PMON_CTL_COUNT_MODE_SHIFT 2
  88. #define NHMEX_M_PMON_CTL_COUNT_MODE_MASK \
  89. (0x3 << NHMEX_M_PMON_CTL_COUNT_MODE_SHIFT)
  90. #define NHMEX_M_PMON_CTL_STORAGE_MODE_SHIFT 4
  91. #define NHMEX_M_PMON_CTL_STORAGE_MODE_MASK \
  92. (0x3 << NHMEX_M_PMON_CTL_STORAGE_MODE_SHIFT)
  93. #define NHMEX_M_PMON_CTL_WRAP_MODE (1 << 6)
  94. #define NHMEX_M_PMON_CTL_FLAG_MODE (1 << 7)
  95. #define NHMEX_M_PMON_CTL_INC_SEL_SHIFT 9
  96. #define NHMEX_M_PMON_CTL_INC_SEL_MASK \
  97. (0x1f << NHMEX_M_PMON_CTL_INC_SEL_SHIFT)
  98. #define NHMEX_M_PMON_CTL_SET_FLAG_SEL_SHIFT 19
  99. #define NHMEX_M_PMON_CTL_SET_FLAG_SEL_MASK \
  100. (0x7 << NHMEX_M_PMON_CTL_SET_FLAG_SEL_SHIFT)
  101. #define NHMEX_M_PMON_RAW_EVENT_MASK \
  102. (NHMEX_M_PMON_CTL_COUNT_MODE_MASK | \
  103. NHMEX_M_PMON_CTL_STORAGE_MODE_MASK | \
  104. NHMEX_M_PMON_CTL_WRAP_MODE | \
  105. NHMEX_M_PMON_CTL_FLAG_MODE | \
  106. NHMEX_M_PMON_CTL_INC_SEL_MASK | \
  107. NHMEX_M_PMON_CTL_SET_FLAG_SEL_MASK)
  108. #define NHMEX_M_PMON_ZDP_CTL_FVC_MASK (((1 << 11) - 1) | (1 << 23))
  109. #define NHMEX_M_PMON_ZDP_CTL_FVC_EVENT_MASK(n) (0x7ULL << (11 + 3 * (n)))
  110. #define WSMEX_M_PMON_ZDP_CTL_FVC_MASK (((1 << 12) - 1) | (1 << 24))
  111. #define WSMEX_M_PMON_ZDP_CTL_FVC_EVENT_MASK(n) (0x7ULL << (12 + 3 * (n)))
  112. /*
  113. * use the 9~13 bits to select event If the 7th bit is not set,
  114. * otherwise use the 19~21 bits to select event.
  115. */
  116. #define MBOX_INC_SEL(x) ((x) << NHMEX_M_PMON_CTL_INC_SEL_SHIFT)
  117. #define MBOX_SET_FLAG_SEL(x) (((x) << NHMEX_M_PMON_CTL_SET_FLAG_SEL_SHIFT) | \
  118. NHMEX_M_PMON_CTL_FLAG_MODE)
  119. #define MBOX_INC_SEL_MASK (NHMEX_M_PMON_CTL_INC_SEL_MASK | \
  120. NHMEX_M_PMON_CTL_FLAG_MODE)
  121. #define MBOX_SET_FLAG_SEL_MASK (NHMEX_M_PMON_CTL_SET_FLAG_SEL_MASK | \
  122. NHMEX_M_PMON_CTL_FLAG_MODE)
  123. #define MBOX_INC_SEL_EXTAR_REG(c, r) \
  124. EVENT_EXTRA_REG(MBOX_INC_SEL(c), NHMEX_M0_MSR_PMU_##r, \
  125. MBOX_INC_SEL_MASK, (u64)-1, NHMEX_M_##r)
  126. #define MBOX_SET_FLAG_SEL_EXTRA_REG(c, r) \
  127. EVENT_EXTRA_REG(MBOX_SET_FLAG_SEL(c), NHMEX_M0_MSR_PMU_##r, \
  128. MBOX_SET_FLAG_SEL_MASK, \
  129. (u64)-1, NHMEX_M_##r)
  130. /* NHM-EX Rbox */
  131. #define NHMEX_R_MSR_GLOBAL_CTL 0xe00
  132. #define NHMEX_R_MSR_PMON_CTL0 0xe10
  133. #define NHMEX_R_MSR_PMON_CNT0 0xe11
  134. #define NHMEX_R_MSR_OFFSET 0x20
  135. #define NHMEX_R_MSR_PORTN_QLX_CFG(n) \
  136. ((n) < 4 ? (0xe0c + (n)) : (0xe2c + (n) - 4))
  137. #define NHMEX_R_MSR_PORTN_IPERF_CFG0(n) (0xe04 + (n))
  138. #define NHMEX_R_MSR_PORTN_IPERF_CFG1(n) (0xe24 + (n))
  139. #define NHMEX_R_MSR_PORTN_XBR_OFFSET(n) \
  140. (((n) < 4 ? 0 : 0x10) + (n) * 4)
  141. #define NHMEX_R_MSR_PORTN_XBR_SET1_MM_CFG(n) \
  142. (0xe60 + NHMEX_R_MSR_PORTN_XBR_OFFSET(n))
  143. #define NHMEX_R_MSR_PORTN_XBR_SET1_MATCH(n) \
  144. (NHMEX_R_MSR_PORTN_XBR_SET1_MM_CFG(n) + 1)
  145. #define NHMEX_R_MSR_PORTN_XBR_SET1_MASK(n) \
  146. (NHMEX_R_MSR_PORTN_XBR_SET1_MM_CFG(n) + 2)
  147. #define NHMEX_R_MSR_PORTN_XBR_SET2_MM_CFG(n) \
  148. (0xe70 + NHMEX_R_MSR_PORTN_XBR_OFFSET(n))
  149. #define NHMEX_R_MSR_PORTN_XBR_SET2_MATCH(n) \
  150. (NHMEX_R_MSR_PORTN_XBR_SET2_MM_CFG(n) + 1)
  151. #define NHMEX_R_MSR_PORTN_XBR_SET2_MASK(n) \
  152. (NHMEX_R_MSR_PORTN_XBR_SET2_MM_CFG(n) + 2)
  153. #define NHMEX_R_PMON_CTL_EN (1 << 0)
  154. #define NHMEX_R_PMON_CTL_EV_SEL_SHIFT 1
  155. #define NHMEX_R_PMON_CTL_EV_SEL_MASK \
  156. (0x1f << NHMEX_R_PMON_CTL_EV_SEL_SHIFT)
  157. #define NHMEX_R_PMON_CTL_PMI_EN (1 << 6)
  158. #define NHMEX_R_PMON_RAW_EVENT_MASK NHMEX_R_PMON_CTL_EV_SEL_MASK
  159. /* NHM-EX Wbox */
  160. #define NHMEX_W_MSR_GLOBAL_CTL 0xc80
  161. #define NHMEX_W_MSR_PMON_CNT0 0xc90
  162. #define NHMEX_W_MSR_PMON_EVT_SEL0 0xc91
  163. #define NHMEX_W_MSR_PMON_FIXED_CTR 0x394
  164. #define NHMEX_W_MSR_PMON_FIXED_CTL 0x395
  165. #define NHMEX_W_PMON_GLOBAL_FIXED_EN (1ULL << 31)
  166. #define __BITS_VALUE(x, i, n) ((typeof(x))(((x) >> ((i) * (n))) & \
  167. ((1ULL << (n)) - 1)))
  168. DEFINE_UNCORE_FORMAT_ATTR(event, event, "config:0-7");
  169. DEFINE_UNCORE_FORMAT_ATTR(event5, event, "config:1-5");
  170. DEFINE_UNCORE_FORMAT_ATTR(umask, umask, "config:8-15");
  171. DEFINE_UNCORE_FORMAT_ATTR(edge, edge, "config:18");
  172. DEFINE_UNCORE_FORMAT_ATTR(inv, inv, "config:23");
  173. DEFINE_UNCORE_FORMAT_ATTR(thresh8, thresh, "config:24-31");
  174. DEFINE_UNCORE_FORMAT_ATTR(counter, counter, "config:6-7");
  175. DEFINE_UNCORE_FORMAT_ATTR(match, match, "config1:0-63");
  176. DEFINE_UNCORE_FORMAT_ATTR(mask, mask, "config2:0-63");
  177. static void nhmex_uncore_msr_init_box(struct intel_uncore_box *box)
  178. {
  179. wrmsrl(NHMEX_U_MSR_PMON_GLOBAL_CTL, NHMEX_U_PMON_GLOBAL_EN_ALL);
  180. }
  181. static void nhmex_uncore_msr_exit_box(struct intel_uncore_box *box)
  182. {
  183. wrmsrl(NHMEX_U_MSR_PMON_GLOBAL_CTL, 0);
  184. }
  185. static void nhmex_uncore_msr_disable_box(struct intel_uncore_box *box)
  186. {
  187. unsigned msr = uncore_msr_box_ctl(box);
  188. u64 config;
  189. if (msr) {
  190. rdmsrl(msr, config);
  191. config &= ~((1ULL << uncore_num_counters(box)) - 1);
  192. /* WBox has a fixed counter */
  193. if (uncore_msr_fixed_ctl(box))
  194. config &= ~NHMEX_W_PMON_GLOBAL_FIXED_EN;
  195. wrmsrl(msr, config);
  196. }
  197. }
  198. static void nhmex_uncore_msr_enable_box(struct intel_uncore_box *box)
  199. {
  200. unsigned msr = uncore_msr_box_ctl(box);
  201. u64 config;
  202. if (msr) {
  203. rdmsrl(msr, config);
  204. config |= (1ULL << uncore_num_counters(box)) - 1;
  205. /* WBox has a fixed counter */
  206. if (uncore_msr_fixed_ctl(box))
  207. config |= NHMEX_W_PMON_GLOBAL_FIXED_EN;
  208. wrmsrl(msr, config);
  209. }
  210. }
  211. static void nhmex_uncore_msr_disable_event(struct intel_uncore_box *box, struct perf_event *event)
  212. {
  213. wrmsrl(event->hw.config_base, 0);
  214. }
  215. static void nhmex_uncore_msr_enable_event(struct intel_uncore_box *box, struct perf_event *event)
  216. {
  217. struct hw_perf_event *hwc = &event->hw;
  218. if (hwc->idx == UNCORE_PMC_IDX_FIXED)
  219. wrmsrl(hwc->config_base, NHMEX_PMON_CTL_EN_BIT0);
  220. else if (box->pmu->type->event_mask & NHMEX_PMON_CTL_EN_BIT0)
  221. wrmsrl(hwc->config_base, hwc->config | NHMEX_PMON_CTL_EN_BIT22);
  222. else
  223. wrmsrl(hwc->config_base, hwc->config | NHMEX_PMON_CTL_EN_BIT0);
  224. }
  225. #define NHMEX_UNCORE_OPS_COMMON_INIT() \
  226. .init_box = nhmex_uncore_msr_init_box, \
  227. .exit_box = nhmex_uncore_msr_exit_box, \
  228. .disable_box = nhmex_uncore_msr_disable_box, \
  229. .enable_box = nhmex_uncore_msr_enable_box, \
  230. .disable_event = nhmex_uncore_msr_disable_event, \
  231. .read_counter = uncore_msr_read_counter
  232. static struct intel_uncore_ops nhmex_uncore_ops = {
  233. NHMEX_UNCORE_OPS_COMMON_INIT(),
  234. .enable_event = nhmex_uncore_msr_enable_event,
  235. };
  236. static struct attribute *nhmex_uncore_ubox_formats_attr[] = {
  237. &format_attr_event.attr,
  238. &format_attr_edge.attr,
  239. NULL,
  240. };
  241. static const struct attribute_group nhmex_uncore_ubox_format_group = {
  242. .name = "format",
  243. .attrs = nhmex_uncore_ubox_formats_attr,
  244. };
  245. static struct intel_uncore_type nhmex_uncore_ubox = {
  246. .name = "ubox",
  247. .num_counters = 1,
  248. .num_boxes = 1,
  249. .perf_ctr_bits = 48,
  250. .event_ctl = NHMEX_U_MSR_PMON_EV_SEL,
  251. .perf_ctr = NHMEX_U_MSR_PMON_CTR,
  252. .event_mask = NHMEX_U_PMON_RAW_EVENT_MASK,
  253. .box_ctl = NHMEX_U_MSR_PMON_GLOBAL_CTL,
  254. .ops = &nhmex_uncore_ops,
  255. .format_group = &nhmex_uncore_ubox_format_group
  256. };
  257. static struct attribute *nhmex_uncore_cbox_formats_attr[] = {
  258. &format_attr_event.attr,
  259. &format_attr_umask.attr,
  260. &format_attr_edge.attr,
  261. &format_attr_inv.attr,
  262. &format_attr_thresh8.attr,
  263. NULL,
  264. };
  265. static const struct attribute_group nhmex_uncore_cbox_format_group = {
  266. .name = "format",
  267. .attrs = nhmex_uncore_cbox_formats_attr,
  268. };
  269. /* msr offset for each instance of cbox */
  270. static unsigned nhmex_cbox_msr_offsets[] = {
  271. 0x0, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x240, 0x2c0,
  272. };
  273. static struct intel_uncore_type nhmex_uncore_cbox = {
  274. .name = "cbox",
  275. .num_counters = 6,
  276. .num_boxes = 10,
  277. .perf_ctr_bits = 48,
  278. .event_ctl = NHMEX_C0_MSR_PMON_EV_SEL0,
  279. .perf_ctr = NHMEX_C0_MSR_PMON_CTR0,
  280. .event_mask = NHMEX_PMON_RAW_EVENT_MASK,
  281. .box_ctl = NHMEX_C0_MSR_PMON_GLOBAL_CTL,
  282. .msr_offsets = nhmex_cbox_msr_offsets,
  283. .pair_ctr_ctl = 1,
  284. .ops = &nhmex_uncore_ops,
  285. .format_group = &nhmex_uncore_cbox_format_group
  286. };
  287. static struct uncore_event_desc nhmex_uncore_wbox_events[] = {
  288. INTEL_UNCORE_EVENT_DESC(clockticks, "event=0xff,umask=0"),
  289. { /* end: all zeroes */ },
  290. };
  291. static struct intel_uncore_type nhmex_uncore_wbox = {
  292. .name = "wbox",
  293. .num_counters = 4,
  294. .num_boxes = 1,
  295. .perf_ctr_bits = 48,
  296. .event_ctl = NHMEX_W_MSR_PMON_CNT0,
  297. .perf_ctr = NHMEX_W_MSR_PMON_EVT_SEL0,
  298. .fixed_ctr = NHMEX_W_MSR_PMON_FIXED_CTR,
  299. .fixed_ctl = NHMEX_W_MSR_PMON_FIXED_CTL,
  300. .event_mask = NHMEX_PMON_RAW_EVENT_MASK,
  301. .box_ctl = NHMEX_W_MSR_GLOBAL_CTL,
  302. .pair_ctr_ctl = 1,
  303. .event_descs = nhmex_uncore_wbox_events,
  304. .ops = &nhmex_uncore_ops,
  305. .format_group = &nhmex_uncore_cbox_format_group
  306. };
  307. static int nhmex_bbox_hw_config(struct intel_uncore_box *box, struct perf_event *event)
  308. {
  309. struct hw_perf_event *hwc = &event->hw;
  310. struct hw_perf_event_extra *reg1 = &hwc->extra_reg;
  311. struct hw_perf_event_extra *reg2 = &hwc->branch_reg;
  312. int ctr, ev_sel;
  313. ctr = (hwc->config & NHMEX_B_PMON_CTR_MASK) >>
  314. NHMEX_B_PMON_CTR_SHIFT;
  315. ev_sel = (hwc->config & NHMEX_B_PMON_CTL_EV_SEL_MASK) >>
  316. NHMEX_B_PMON_CTL_EV_SEL_SHIFT;
  317. /* events that do not use the match/mask registers */
  318. if ((ctr == 0 && ev_sel > 0x3) || (ctr == 1 && ev_sel > 0x6) ||
  319. (ctr == 2 && ev_sel != 0x4) || ctr == 3)
  320. return 0;
  321. if (box->pmu->pmu_idx == 0)
  322. reg1->reg = NHMEX_B0_MSR_MATCH;
  323. else
  324. reg1->reg = NHMEX_B1_MSR_MATCH;
  325. reg1->idx = 0;
  326. reg1->config = event->attr.config1;
  327. reg2->config = event->attr.config2;
  328. return 0;
  329. }
  330. static void nhmex_bbox_msr_enable_event(struct intel_uncore_box *box, struct perf_event *event)
  331. {
  332. struct hw_perf_event *hwc = &event->hw;
  333. struct hw_perf_event_extra *reg1 = &hwc->extra_reg;
  334. struct hw_perf_event_extra *reg2 = &hwc->branch_reg;
  335. if (reg1->idx != EXTRA_REG_NONE) {
  336. wrmsrl(reg1->reg, reg1->config);
  337. wrmsrl(reg1->reg + 1, reg2->config);
  338. }
  339. wrmsrl(hwc->config_base, NHMEX_PMON_CTL_EN_BIT0 |
  340. (hwc->config & NHMEX_B_PMON_CTL_EV_SEL_MASK));
  341. }
  342. /*
  343. * The Bbox has 4 counters, but each counter monitors different events.
  344. * Use bits 6-7 in the event config to select counter.
  345. */
  346. static struct event_constraint nhmex_uncore_bbox_constraints[] = {
  347. EVENT_CONSTRAINT(0 , 1, 0xc0),
  348. EVENT_CONSTRAINT(0x40, 2, 0xc0),
  349. EVENT_CONSTRAINT(0x80, 4, 0xc0),
  350. EVENT_CONSTRAINT(0xc0, 8, 0xc0),
  351. EVENT_CONSTRAINT_END,
  352. };
  353. static struct attribute *nhmex_uncore_bbox_formats_attr[] = {
  354. &format_attr_event5.attr,
  355. &format_attr_counter.attr,
  356. &format_attr_match.attr,
  357. &format_attr_mask.attr,
  358. NULL,
  359. };
  360. static const struct attribute_group nhmex_uncore_bbox_format_group = {
  361. .name = "format",
  362. .attrs = nhmex_uncore_bbox_formats_attr,
  363. };
  364. static struct intel_uncore_ops nhmex_uncore_bbox_ops = {
  365. NHMEX_UNCORE_OPS_COMMON_INIT(),
  366. .enable_event = nhmex_bbox_msr_enable_event,
  367. .hw_config = nhmex_bbox_hw_config,
  368. .get_constraint = uncore_get_constraint,
  369. .put_constraint = uncore_put_constraint,
  370. };
  371. static struct intel_uncore_type nhmex_uncore_bbox = {
  372. .name = "bbox",
  373. .num_counters = 4,
  374. .num_boxes = 2,
  375. .perf_ctr_bits = 48,
  376. .event_ctl = NHMEX_B0_MSR_PMON_CTL0,
  377. .perf_ctr = NHMEX_B0_MSR_PMON_CTR0,
  378. .event_mask = NHMEX_B_PMON_RAW_EVENT_MASK,
  379. .box_ctl = NHMEX_B0_MSR_PMON_GLOBAL_CTL,
  380. .msr_offset = NHMEX_B_MSR_OFFSET,
  381. .pair_ctr_ctl = 1,
  382. .num_shared_regs = 1,
  383. .constraints = nhmex_uncore_bbox_constraints,
  384. .ops = &nhmex_uncore_bbox_ops,
  385. .format_group = &nhmex_uncore_bbox_format_group
  386. };
  387. static int nhmex_sbox_hw_config(struct intel_uncore_box *box, struct perf_event *event)
  388. {
  389. struct hw_perf_event *hwc = &event->hw;
  390. struct hw_perf_event_extra *reg1 = &hwc->extra_reg;
  391. struct hw_perf_event_extra *reg2 = &hwc->branch_reg;
  392. /* only TO_R_PROG_EV event uses the match/mask register */
  393. if ((hwc->config & NHMEX_PMON_CTL_EV_SEL_MASK) !=
  394. NHMEX_S_EVENT_TO_R_PROG_EV)
  395. return 0;
  396. if (box->pmu->pmu_idx == 0)
  397. reg1->reg = NHMEX_S0_MSR_MM_CFG;
  398. else
  399. reg1->reg = NHMEX_S1_MSR_MM_CFG;
  400. reg1->idx = 0;
  401. reg1->config = event->attr.config1;
  402. reg2->config = event->attr.config2;
  403. return 0;
  404. }
  405. static void nhmex_sbox_msr_enable_event(struct intel_uncore_box *box, struct perf_event *event)
  406. {
  407. struct hw_perf_event *hwc = &event->hw;
  408. struct hw_perf_event_extra *reg1 = &hwc->extra_reg;
  409. struct hw_perf_event_extra *reg2 = &hwc->branch_reg;
  410. if (reg1->idx != EXTRA_REG_NONE) {
  411. wrmsrl(reg1->reg, 0);
  412. wrmsrl(reg1->reg + 1, reg1->config);
  413. wrmsrl(reg1->reg + 2, reg2->config);
  414. wrmsrl(reg1->reg, NHMEX_S_PMON_MM_CFG_EN);
  415. }
  416. wrmsrl(hwc->config_base, hwc->config | NHMEX_PMON_CTL_EN_BIT22);
  417. }
  418. static struct attribute *nhmex_uncore_sbox_formats_attr[] = {
  419. &format_attr_event.attr,
  420. &format_attr_umask.attr,
  421. &format_attr_edge.attr,
  422. &format_attr_inv.attr,
  423. &format_attr_thresh8.attr,
  424. &format_attr_match.attr,
  425. &format_attr_mask.attr,
  426. NULL,
  427. };
  428. static const struct attribute_group nhmex_uncore_sbox_format_group = {
  429. .name = "format",
  430. .attrs = nhmex_uncore_sbox_formats_attr,
  431. };
  432. static struct intel_uncore_ops nhmex_uncore_sbox_ops = {
  433. NHMEX_UNCORE_OPS_COMMON_INIT(),
  434. .enable_event = nhmex_sbox_msr_enable_event,
  435. .hw_config = nhmex_sbox_hw_config,
  436. .get_constraint = uncore_get_constraint,
  437. .put_constraint = uncore_put_constraint,
  438. };
  439. static struct intel_uncore_type nhmex_uncore_sbox = {
  440. .name = "sbox",
  441. .num_counters = 4,
  442. .num_boxes = 2,
  443. .perf_ctr_bits = 48,
  444. .event_ctl = NHMEX_S0_MSR_PMON_CTL0,
  445. .perf_ctr = NHMEX_S0_MSR_PMON_CTR0,
  446. .event_mask = NHMEX_PMON_RAW_EVENT_MASK,
  447. .box_ctl = NHMEX_S0_MSR_PMON_GLOBAL_CTL,
  448. .msr_offset = NHMEX_S_MSR_OFFSET,
  449. .pair_ctr_ctl = 1,
  450. .num_shared_regs = 1,
  451. .ops = &nhmex_uncore_sbox_ops,
  452. .format_group = &nhmex_uncore_sbox_format_group
  453. };
  454. enum {
  455. EXTRA_REG_NHMEX_M_FILTER,
  456. EXTRA_REG_NHMEX_M_DSP,
  457. EXTRA_REG_NHMEX_M_ISS,
  458. EXTRA_REG_NHMEX_M_MAP,
  459. EXTRA_REG_NHMEX_M_MSC_THR,
  460. EXTRA_REG_NHMEX_M_PGT,
  461. EXTRA_REG_NHMEX_M_PLD,
  462. EXTRA_REG_NHMEX_M_ZDP_CTL_FVC,
  463. };
  464. static struct extra_reg nhmex_uncore_mbox_extra_regs[] = {
  465. MBOX_INC_SEL_EXTAR_REG(0x0, DSP),
  466. MBOX_INC_SEL_EXTAR_REG(0x4, MSC_THR),
  467. MBOX_INC_SEL_EXTAR_REG(0x5, MSC_THR),
  468. MBOX_INC_SEL_EXTAR_REG(0x9, ISS),
  469. /* event 0xa uses two extra registers */
  470. MBOX_INC_SEL_EXTAR_REG(0xa, ISS),
  471. MBOX_INC_SEL_EXTAR_REG(0xa, PLD),
  472. MBOX_INC_SEL_EXTAR_REG(0xb, PLD),
  473. /* events 0xd ~ 0x10 use the same extra register */
  474. MBOX_INC_SEL_EXTAR_REG(0xd, ZDP_CTL_FVC),
  475. MBOX_INC_SEL_EXTAR_REG(0xe, ZDP_CTL_FVC),
  476. MBOX_INC_SEL_EXTAR_REG(0xf, ZDP_CTL_FVC),
  477. MBOX_INC_SEL_EXTAR_REG(0x10, ZDP_CTL_FVC),
  478. MBOX_INC_SEL_EXTAR_REG(0x16, PGT),
  479. MBOX_SET_FLAG_SEL_EXTRA_REG(0x0, DSP),
  480. MBOX_SET_FLAG_SEL_EXTRA_REG(0x1, ISS),
  481. MBOX_SET_FLAG_SEL_EXTRA_REG(0x5, PGT),
  482. MBOX_SET_FLAG_SEL_EXTRA_REG(0x6, MAP),
  483. EVENT_EXTRA_END
  484. };
  485. /* Nehalem-EX or Westmere-EX ? */
  486. static bool uncore_nhmex;
  487. static bool nhmex_mbox_get_shared_reg(struct intel_uncore_box *box, int idx, u64 config)
  488. {
  489. struct intel_uncore_extra_reg *er;
  490. unsigned long flags;
  491. bool ret = false;
  492. u64 mask;
  493. if (idx < EXTRA_REG_NHMEX_M_ZDP_CTL_FVC) {
  494. er = &box->shared_regs[idx];
  495. raw_spin_lock_irqsave(&er->lock, flags);
  496. if (!atomic_read(&er->ref) || er->config == config) {
  497. atomic_inc(&er->ref);
  498. er->config = config;
  499. ret = true;
  500. }
  501. raw_spin_unlock_irqrestore(&er->lock, flags);
  502. return ret;
  503. }
  504. /*
  505. * The ZDP_CTL_FVC MSR has 4 fields which are used to control
  506. * events 0xd ~ 0x10. Besides these 4 fields, there are additional
  507. * fields which are shared.
  508. */
  509. idx -= EXTRA_REG_NHMEX_M_ZDP_CTL_FVC;
  510. if (WARN_ON_ONCE(idx >= 4))
  511. return false;
  512. /* mask of the shared fields */
  513. if (uncore_nhmex)
  514. mask = NHMEX_M_PMON_ZDP_CTL_FVC_MASK;
  515. else
  516. mask = WSMEX_M_PMON_ZDP_CTL_FVC_MASK;
  517. er = &box->shared_regs[EXTRA_REG_NHMEX_M_ZDP_CTL_FVC];
  518. raw_spin_lock_irqsave(&er->lock, flags);
  519. /* add mask of the non-shared field if it's in use */
  520. if (__BITS_VALUE(atomic_read(&er->ref), idx, 8)) {
  521. if (uncore_nhmex)
  522. mask |= NHMEX_M_PMON_ZDP_CTL_FVC_EVENT_MASK(idx);
  523. else
  524. mask |= WSMEX_M_PMON_ZDP_CTL_FVC_EVENT_MASK(idx);
  525. }
  526. if (!atomic_read(&er->ref) || !((er->config ^ config) & mask)) {
  527. atomic_add(1 << (idx * 8), &er->ref);
  528. if (uncore_nhmex)
  529. mask = NHMEX_M_PMON_ZDP_CTL_FVC_MASK |
  530. NHMEX_M_PMON_ZDP_CTL_FVC_EVENT_MASK(idx);
  531. else
  532. mask = WSMEX_M_PMON_ZDP_CTL_FVC_MASK |
  533. WSMEX_M_PMON_ZDP_CTL_FVC_EVENT_MASK(idx);
  534. er->config &= ~mask;
  535. er->config |= (config & mask);
  536. ret = true;
  537. }
  538. raw_spin_unlock_irqrestore(&er->lock, flags);
  539. return ret;
  540. }
  541. static void nhmex_mbox_put_shared_reg(struct intel_uncore_box *box, int idx)
  542. {
  543. struct intel_uncore_extra_reg *er;
  544. if (idx < EXTRA_REG_NHMEX_M_ZDP_CTL_FVC) {
  545. er = &box->shared_regs[idx];
  546. atomic_dec(&er->ref);
  547. return;
  548. }
  549. idx -= EXTRA_REG_NHMEX_M_ZDP_CTL_FVC;
  550. er = &box->shared_regs[EXTRA_REG_NHMEX_M_ZDP_CTL_FVC];
  551. atomic_sub(1 << (idx * 8), &er->ref);
  552. }
  553. static u64 nhmex_mbox_alter_er(struct perf_event *event, int new_idx, bool modify)
  554. {
  555. struct hw_perf_event *hwc = &event->hw;
  556. struct hw_perf_event_extra *reg1 = &hwc->extra_reg;
  557. u64 idx, orig_idx = __BITS_VALUE(reg1->idx, 0, 8);
  558. u64 config = reg1->config;
  559. /* get the non-shared control bits and shift them */
  560. idx = orig_idx - EXTRA_REG_NHMEX_M_ZDP_CTL_FVC;
  561. if (uncore_nhmex)
  562. config &= NHMEX_M_PMON_ZDP_CTL_FVC_EVENT_MASK(idx);
  563. else
  564. config &= WSMEX_M_PMON_ZDP_CTL_FVC_EVENT_MASK(idx);
  565. if (new_idx > orig_idx) {
  566. idx = new_idx - orig_idx;
  567. config <<= 3 * idx;
  568. } else {
  569. idx = orig_idx - new_idx;
  570. config >>= 3 * idx;
  571. }
  572. /* add the shared control bits back */
  573. if (uncore_nhmex)
  574. config |= NHMEX_M_PMON_ZDP_CTL_FVC_MASK & reg1->config;
  575. else
  576. config |= WSMEX_M_PMON_ZDP_CTL_FVC_MASK & reg1->config;
  577. config |= NHMEX_M_PMON_ZDP_CTL_FVC_MASK & reg1->config;
  578. if (modify) {
  579. /* adjust the main event selector */
  580. if (new_idx > orig_idx)
  581. hwc->config += idx << NHMEX_M_PMON_CTL_INC_SEL_SHIFT;
  582. else
  583. hwc->config -= idx << NHMEX_M_PMON_CTL_INC_SEL_SHIFT;
  584. reg1->config = config;
  585. reg1->idx = ~0xff | new_idx;
  586. }
  587. return config;
  588. }
  589. static struct event_constraint *
  590. nhmex_mbox_get_constraint(struct intel_uncore_box *box, struct perf_event *event)
  591. {
  592. struct hw_perf_event_extra *reg1 = &event->hw.extra_reg;
  593. struct hw_perf_event_extra *reg2 = &event->hw.branch_reg;
  594. int i, idx[2], alloc = 0;
  595. u64 config1 = reg1->config;
  596. idx[0] = __BITS_VALUE(reg1->idx, 0, 8);
  597. idx[1] = __BITS_VALUE(reg1->idx, 1, 8);
  598. again:
  599. for (i = 0; i < 2; i++) {
  600. if (!uncore_box_is_fake(box) && (reg1->alloc & (0x1 << i)))
  601. idx[i] = 0xff;
  602. if (idx[i] == 0xff)
  603. continue;
  604. if (!nhmex_mbox_get_shared_reg(box, idx[i],
  605. __BITS_VALUE(config1, i, 32)))
  606. goto fail;
  607. alloc |= (0x1 << i);
  608. }
  609. /* for the match/mask registers */
  610. if (reg2->idx != EXTRA_REG_NONE &&
  611. (uncore_box_is_fake(box) || !reg2->alloc) &&
  612. !nhmex_mbox_get_shared_reg(box, reg2->idx, reg2->config))
  613. goto fail;
  614. /*
  615. * If it's a fake box -- as per validate_{group,event}() we
  616. * shouldn't touch event state and we can avoid doing so
  617. * since both will only call get_event_constraints() once
  618. * on each event, this avoids the need for reg->alloc.
  619. */
  620. if (!uncore_box_is_fake(box)) {
  621. if (idx[0] != 0xff && idx[0] != __BITS_VALUE(reg1->idx, 0, 8))
  622. nhmex_mbox_alter_er(event, idx[0], true);
  623. reg1->alloc |= alloc;
  624. if (reg2->idx != EXTRA_REG_NONE)
  625. reg2->alloc = 1;
  626. }
  627. return NULL;
  628. fail:
  629. if (idx[0] != 0xff && !(alloc & 0x1) &&
  630. idx[0] >= EXTRA_REG_NHMEX_M_ZDP_CTL_FVC) {
  631. /*
  632. * events 0xd ~ 0x10 are functional identical, but are
  633. * controlled by different fields in the ZDP_CTL_FVC
  634. * register. If we failed to take one field, try the
  635. * rest 3 choices.
  636. */
  637. BUG_ON(__BITS_VALUE(reg1->idx, 1, 8) != 0xff);
  638. idx[0] -= EXTRA_REG_NHMEX_M_ZDP_CTL_FVC;
  639. idx[0] = (idx[0] + 1) % 4;
  640. idx[0] += EXTRA_REG_NHMEX_M_ZDP_CTL_FVC;
  641. if (idx[0] != __BITS_VALUE(reg1->idx, 0, 8)) {
  642. config1 = nhmex_mbox_alter_er(event, idx[0], false);
  643. goto again;
  644. }
  645. }
  646. if (alloc & 0x1)
  647. nhmex_mbox_put_shared_reg(box, idx[0]);
  648. if (alloc & 0x2)
  649. nhmex_mbox_put_shared_reg(box, idx[1]);
  650. return &uncore_constraint_empty;
  651. }
  652. static void nhmex_mbox_put_constraint(struct intel_uncore_box *box, struct perf_event *event)
  653. {
  654. struct hw_perf_event_extra *reg1 = &event->hw.extra_reg;
  655. struct hw_perf_event_extra *reg2 = &event->hw.branch_reg;
  656. if (uncore_box_is_fake(box))
  657. return;
  658. if (reg1->alloc & 0x1)
  659. nhmex_mbox_put_shared_reg(box, __BITS_VALUE(reg1->idx, 0, 8));
  660. if (reg1->alloc & 0x2)
  661. nhmex_mbox_put_shared_reg(box, __BITS_VALUE(reg1->idx, 1, 8));
  662. reg1->alloc = 0;
  663. if (reg2->alloc) {
  664. nhmex_mbox_put_shared_reg(box, reg2->idx);
  665. reg2->alloc = 0;
  666. }
  667. }
  668. static int nhmex_mbox_extra_reg_idx(struct extra_reg *er)
  669. {
  670. if (er->idx < EXTRA_REG_NHMEX_M_ZDP_CTL_FVC)
  671. return er->idx;
  672. return er->idx + (er->event >> NHMEX_M_PMON_CTL_INC_SEL_SHIFT) - 0xd;
  673. }
  674. static int nhmex_mbox_hw_config(struct intel_uncore_box *box, struct perf_event *event)
  675. {
  676. struct intel_uncore_type *type = box->pmu->type;
  677. struct hw_perf_event_extra *reg1 = &event->hw.extra_reg;
  678. struct hw_perf_event_extra *reg2 = &event->hw.branch_reg;
  679. struct extra_reg *er;
  680. unsigned msr;
  681. int reg_idx = 0;
  682. /*
  683. * The mbox events may require 2 extra MSRs at the most. But only
  684. * the lower 32 bits in these MSRs are significant, so we can use
  685. * config1 to pass two MSRs' config.
  686. */
  687. for (er = nhmex_uncore_mbox_extra_regs; er->msr; er++) {
  688. if (er->event != (event->hw.config & er->config_mask))
  689. continue;
  690. if (event->attr.config1 & ~er->valid_mask)
  691. return -EINVAL;
  692. msr = er->msr + type->msr_offset * box->pmu->pmu_idx;
  693. if (WARN_ON_ONCE(msr >= 0xffff || er->idx >= 0xff))
  694. return -EINVAL;
  695. /* always use the 32~63 bits to pass the PLD config */
  696. if (er->idx == EXTRA_REG_NHMEX_M_PLD)
  697. reg_idx = 1;
  698. else if (WARN_ON_ONCE(reg_idx > 0))
  699. return -EINVAL;
  700. reg1->idx &= ~(0xff << (reg_idx * 8));
  701. reg1->reg &= ~(0xffff << (reg_idx * 16));
  702. reg1->idx |= nhmex_mbox_extra_reg_idx(er) << (reg_idx * 8);
  703. reg1->reg |= msr << (reg_idx * 16);
  704. reg1->config = event->attr.config1;
  705. reg_idx++;
  706. }
  707. /*
  708. * The mbox only provides ability to perform address matching
  709. * for the PLD events.
  710. */
  711. if (reg_idx == 2) {
  712. reg2->idx = EXTRA_REG_NHMEX_M_FILTER;
  713. if (event->attr.config2 & NHMEX_M_PMON_MM_CFG_EN)
  714. reg2->config = event->attr.config2;
  715. else
  716. reg2->config = ~0ULL;
  717. if (box->pmu->pmu_idx == 0)
  718. reg2->reg = NHMEX_M0_MSR_PMU_MM_CFG;
  719. else
  720. reg2->reg = NHMEX_M1_MSR_PMU_MM_CFG;
  721. }
  722. return 0;
  723. }
  724. static u64 nhmex_mbox_shared_reg_config(struct intel_uncore_box *box, int idx)
  725. {
  726. struct intel_uncore_extra_reg *er;
  727. unsigned long flags;
  728. u64 config;
  729. if (idx < EXTRA_REG_NHMEX_M_ZDP_CTL_FVC)
  730. return box->shared_regs[idx].config;
  731. er = &box->shared_regs[EXTRA_REG_NHMEX_M_ZDP_CTL_FVC];
  732. raw_spin_lock_irqsave(&er->lock, flags);
  733. config = er->config;
  734. raw_spin_unlock_irqrestore(&er->lock, flags);
  735. return config;
  736. }
  737. static void nhmex_mbox_msr_enable_event(struct intel_uncore_box *box, struct perf_event *event)
  738. {
  739. struct hw_perf_event *hwc = &event->hw;
  740. struct hw_perf_event_extra *reg1 = &hwc->extra_reg;
  741. struct hw_perf_event_extra *reg2 = &hwc->branch_reg;
  742. int idx;
  743. idx = __BITS_VALUE(reg1->idx, 0, 8);
  744. if (idx != 0xff)
  745. wrmsrl(__BITS_VALUE(reg1->reg, 0, 16),
  746. nhmex_mbox_shared_reg_config(box, idx));
  747. idx = __BITS_VALUE(reg1->idx, 1, 8);
  748. if (idx != 0xff)
  749. wrmsrl(__BITS_VALUE(reg1->reg, 1, 16),
  750. nhmex_mbox_shared_reg_config(box, idx));
  751. if (reg2->idx != EXTRA_REG_NONE) {
  752. wrmsrl(reg2->reg, 0);
  753. if (reg2->config != ~0ULL) {
  754. wrmsrl(reg2->reg + 1,
  755. reg2->config & NHMEX_M_PMON_ADDR_MATCH_MASK);
  756. wrmsrl(reg2->reg + 2, NHMEX_M_PMON_ADDR_MASK_MASK &
  757. (reg2->config >> NHMEX_M_PMON_ADDR_MASK_SHIFT));
  758. wrmsrl(reg2->reg, NHMEX_M_PMON_MM_CFG_EN);
  759. }
  760. }
  761. wrmsrl(hwc->config_base, hwc->config | NHMEX_PMON_CTL_EN_BIT0);
  762. }
  763. DEFINE_UNCORE_FORMAT_ATTR(count_mode, count_mode, "config:2-3");
  764. DEFINE_UNCORE_FORMAT_ATTR(storage_mode, storage_mode, "config:4-5");
  765. DEFINE_UNCORE_FORMAT_ATTR(wrap_mode, wrap_mode, "config:6");
  766. DEFINE_UNCORE_FORMAT_ATTR(flag_mode, flag_mode, "config:7");
  767. DEFINE_UNCORE_FORMAT_ATTR(inc_sel, inc_sel, "config:9-13");
  768. DEFINE_UNCORE_FORMAT_ATTR(set_flag_sel, set_flag_sel, "config:19-21");
  769. DEFINE_UNCORE_FORMAT_ATTR(filter_cfg_en, filter_cfg_en, "config2:63");
  770. DEFINE_UNCORE_FORMAT_ATTR(filter_match, filter_match, "config2:0-33");
  771. DEFINE_UNCORE_FORMAT_ATTR(filter_mask, filter_mask, "config2:34-61");
  772. DEFINE_UNCORE_FORMAT_ATTR(dsp, dsp, "config1:0-31");
  773. DEFINE_UNCORE_FORMAT_ATTR(thr, thr, "config1:0-31");
  774. DEFINE_UNCORE_FORMAT_ATTR(fvc, fvc, "config1:0-31");
  775. DEFINE_UNCORE_FORMAT_ATTR(pgt, pgt, "config1:0-31");
  776. DEFINE_UNCORE_FORMAT_ATTR(map, map, "config1:0-31");
  777. DEFINE_UNCORE_FORMAT_ATTR(iss, iss, "config1:0-31");
  778. DEFINE_UNCORE_FORMAT_ATTR(pld, pld, "config1:32-63");
  779. static struct attribute *nhmex_uncore_mbox_formats_attr[] = {
  780. &format_attr_count_mode.attr,
  781. &format_attr_storage_mode.attr,
  782. &format_attr_wrap_mode.attr,
  783. &format_attr_flag_mode.attr,
  784. &format_attr_inc_sel.attr,
  785. &format_attr_set_flag_sel.attr,
  786. &format_attr_filter_cfg_en.attr,
  787. &format_attr_filter_match.attr,
  788. &format_attr_filter_mask.attr,
  789. &format_attr_dsp.attr,
  790. &format_attr_thr.attr,
  791. &format_attr_fvc.attr,
  792. &format_attr_pgt.attr,
  793. &format_attr_map.attr,
  794. &format_attr_iss.attr,
  795. &format_attr_pld.attr,
  796. NULL,
  797. };
  798. static const struct attribute_group nhmex_uncore_mbox_format_group = {
  799. .name = "format",
  800. .attrs = nhmex_uncore_mbox_formats_attr,
  801. };
  802. static struct uncore_event_desc nhmex_uncore_mbox_events[] = {
  803. INTEL_UNCORE_EVENT_DESC(bbox_cmds_read, "inc_sel=0xd,fvc=0x2800"),
  804. INTEL_UNCORE_EVENT_DESC(bbox_cmds_write, "inc_sel=0xd,fvc=0x2820"),
  805. { /* end: all zeroes */ },
  806. };
  807. static struct uncore_event_desc wsmex_uncore_mbox_events[] = {
  808. INTEL_UNCORE_EVENT_DESC(bbox_cmds_read, "inc_sel=0xd,fvc=0x5000"),
  809. INTEL_UNCORE_EVENT_DESC(bbox_cmds_write, "inc_sel=0xd,fvc=0x5040"),
  810. { /* end: all zeroes */ },
  811. };
  812. static struct intel_uncore_ops nhmex_uncore_mbox_ops = {
  813. NHMEX_UNCORE_OPS_COMMON_INIT(),
  814. .enable_event = nhmex_mbox_msr_enable_event,
  815. .hw_config = nhmex_mbox_hw_config,
  816. .get_constraint = nhmex_mbox_get_constraint,
  817. .put_constraint = nhmex_mbox_put_constraint,
  818. };
  819. static struct intel_uncore_type nhmex_uncore_mbox = {
  820. .name = "mbox",
  821. .num_counters = 6,
  822. .num_boxes = 2,
  823. .perf_ctr_bits = 48,
  824. .event_ctl = NHMEX_M0_MSR_PMU_CTL0,
  825. .perf_ctr = NHMEX_M0_MSR_PMU_CNT0,
  826. .event_mask = NHMEX_M_PMON_RAW_EVENT_MASK,
  827. .box_ctl = NHMEX_M0_MSR_GLOBAL_CTL,
  828. .msr_offset = NHMEX_M_MSR_OFFSET,
  829. .pair_ctr_ctl = 1,
  830. .num_shared_regs = 8,
  831. .event_descs = nhmex_uncore_mbox_events,
  832. .ops = &nhmex_uncore_mbox_ops,
  833. .format_group = &nhmex_uncore_mbox_format_group,
  834. };
  835. static void nhmex_rbox_alter_er(struct intel_uncore_box *box, struct perf_event *event)
  836. {
  837. struct hw_perf_event *hwc = &event->hw;
  838. struct hw_perf_event_extra *reg1 = &hwc->extra_reg;
  839. /* adjust the main event selector and extra register index */
  840. if (reg1->idx % 2) {
  841. reg1->idx--;
  842. hwc->config -= 1 << NHMEX_R_PMON_CTL_EV_SEL_SHIFT;
  843. } else {
  844. reg1->idx++;
  845. hwc->config += 1 << NHMEX_R_PMON_CTL_EV_SEL_SHIFT;
  846. }
  847. /* adjust extra register config */
  848. switch (reg1->idx % 6) {
  849. case 2:
  850. /* shift the 8~15 bits to the 0~7 bits */
  851. reg1->config >>= 8;
  852. break;
  853. case 3:
  854. /* shift the 0~7 bits to the 8~15 bits */
  855. reg1->config <<= 8;
  856. break;
  857. }
  858. }
  859. /*
  860. * Each rbox has 4 event set which monitor PQI port 0~3 or 4~7.
  861. * An event set consists of 6 events, the 3rd and 4th events in
  862. * an event set use the same extra register. So an event set uses
  863. * 5 extra registers.
  864. */
  865. static struct event_constraint *
  866. nhmex_rbox_get_constraint(struct intel_uncore_box *box, struct perf_event *event)
  867. {
  868. struct hw_perf_event *hwc = &event->hw;
  869. struct hw_perf_event_extra *reg1 = &hwc->extra_reg;
  870. struct hw_perf_event_extra *reg2 = &hwc->branch_reg;
  871. struct intel_uncore_extra_reg *er;
  872. unsigned long flags;
  873. int idx, er_idx;
  874. u64 config1;
  875. bool ok = false;
  876. if (!uncore_box_is_fake(box) && reg1->alloc)
  877. return NULL;
  878. idx = reg1->idx % 6;
  879. config1 = reg1->config;
  880. again:
  881. er_idx = idx;
  882. /* the 3rd and 4th events use the same extra register */
  883. if (er_idx > 2)
  884. er_idx--;
  885. er_idx += (reg1->idx / 6) * 5;
  886. er = &box->shared_regs[er_idx];
  887. raw_spin_lock_irqsave(&er->lock, flags);
  888. if (idx < 2) {
  889. if (!atomic_read(&er->ref) || er->config == reg1->config) {
  890. atomic_inc(&er->ref);
  891. er->config = reg1->config;
  892. ok = true;
  893. }
  894. } else if (idx == 2 || idx == 3) {
  895. /*
  896. * these two events use different fields in a extra register,
  897. * the 0~7 bits and the 8~15 bits respectively.
  898. */
  899. u64 mask = 0xff << ((idx - 2) * 8);
  900. if (!__BITS_VALUE(atomic_read(&er->ref), idx - 2, 8) ||
  901. !((er->config ^ config1) & mask)) {
  902. atomic_add(1 << ((idx - 2) * 8), &er->ref);
  903. er->config &= ~mask;
  904. er->config |= config1 & mask;
  905. ok = true;
  906. }
  907. } else {
  908. if (!atomic_read(&er->ref) ||
  909. (er->config == (hwc->config >> 32) &&
  910. er->config1 == reg1->config &&
  911. er->config2 == reg2->config)) {
  912. atomic_inc(&er->ref);
  913. er->config = (hwc->config >> 32);
  914. er->config1 = reg1->config;
  915. er->config2 = reg2->config;
  916. ok = true;
  917. }
  918. }
  919. raw_spin_unlock_irqrestore(&er->lock, flags);
  920. if (!ok) {
  921. /*
  922. * The Rbox events are always in pairs. The paired
  923. * events are functional identical, but use different
  924. * extra registers. If we failed to take an extra
  925. * register, try the alternative.
  926. */
  927. idx ^= 1;
  928. if (idx != reg1->idx % 6) {
  929. if (idx == 2)
  930. config1 >>= 8;
  931. else if (idx == 3)
  932. config1 <<= 8;
  933. goto again;
  934. }
  935. } else {
  936. if (!uncore_box_is_fake(box)) {
  937. if (idx != reg1->idx % 6)
  938. nhmex_rbox_alter_er(box, event);
  939. reg1->alloc = 1;
  940. }
  941. return NULL;
  942. }
  943. return &uncore_constraint_empty;
  944. }
  945. static void nhmex_rbox_put_constraint(struct intel_uncore_box *box, struct perf_event *event)
  946. {
  947. struct intel_uncore_extra_reg *er;
  948. struct hw_perf_event_extra *reg1 = &event->hw.extra_reg;
  949. int idx, er_idx;
  950. if (uncore_box_is_fake(box) || !reg1->alloc)
  951. return;
  952. idx = reg1->idx % 6;
  953. er_idx = idx;
  954. if (er_idx > 2)
  955. er_idx--;
  956. er_idx += (reg1->idx / 6) * 5;
  957. er = &box->shared_regs[er_idx];
  958. if (idx == 2 || idx == 3)
  959. atomic_sub(1 << ((idx - 2) * 8), &er->ref);
  960. else
  961. atomic_dec(&er->ref);
  962. reg1->alloc = 0;
  963. }
  964. static int nhmex_rbox_hw_config(struct intel_uncore_box *box, struct perf_event *event)
  965. {
  966. struct hw_perf_event *hwc = &event->hw;
  967. struct hw_perf_event_extra *reg1 = &event->hw.extra_reg;
  968. struct hw_perf_event_extra *reg2 = &event->hw.branch_reg;
  969. int idx;
  970. idx = (event->hw.config & NHMEX_R_PMON_CTL_EV_SEL_MASK) >>
  971. NHMEX_R_PMON_CTL_EV_SEL_SHIFT;
  972. if (idx >= 0x18)
  973. return -EINVAL;
  974. reg1->idx = idx;
  975. reg1->config = event->attr.config1;
  976. switch (idx % 6) {
  977. case 4:
  978. case 5:
  979. hwc->config |= event->attr.config & (~0ULL << 32);
  980. reg2->config = event->attr.config2;
  981. break;
  982. }
  983. return 0;
  984. }
  985. static void nhmex_rbox_msr_enable_event(struct intel_uncore_box *box, struct perf_event *event)
  986. {
  987. struct hw_perf_event *hwc = &event->hw;
  988. struct hw_perf_event_extra *reg1 = &hwc->extra_reg;
  989. struct hw_perf_event_extra *reg2 = &hwc->branch_reg;
  990. int idx, port;
  991. idx = reg1->idx;
  992. port = idx / 6 + box->pmu->pmu_idx * 4;
  993. switch (idx % 6) {
  994. case 0:
  995. wrmsrl(NHMEX_R_MSR_PORTN_IPERF_CFG0(port), reg1->config);
  996. break;
  997. case 1:
  998. wrmsrl(NHMEX_R_MSR_PORTN_IPERF_CFG1(port), reg1->config);
  999. break;
  1000. case 2:
  1001. case 3:
  1002. wrmsrl(NHMEX_R_MSR_PORTN_QLX_CFG(port),
  1003. uncore_shared_reg_config(box, 2 + (idx / 6) * 5));
  1004. break;
  1005. case 4:
  1006. wrmsrl(NHMEX_R_MSR_PORTN_XBR_SET1_MM_CFG(port),
  1007. hwc->config >> 32);
  1008. wrmsrl(NHMEX_R_MSR_PORTN_XBR_SET1_MATCH(port), reg1->config);
  1009. wrmsrl(NHMEX_R_MSR_PORTN_XBR_SET1_MASK(port), reg2->config);
  1010. break;
  1011. case 5:
  1012. wrmsrl(NHMEX_R_MSR_PORTN_XBR_SET2_MM_CFG(port),
  1013. hwc->config >> 32);
  1014. wrmsrl(NHMEX_R_MSR_PORTN_XBR_SET2_MATCH(port), reg1->config);
  1015. wrmsrl(NHMEX_R_MSR_PORTN_XBR_SET2_MASK(port), reg2->config);
  1016. break;
  1017. }
  1018. wrmsrl(hwc->config_base, NHMEX_PMON_CTL_EN_BIT0 |
  1019. (hwc->config & NHMEX_R_PMON_CTL_EV_SEL_MASK));
  1020. }
  1021. DEFINE_UNCORE_FORMAT_ATTR(xbr_mm_cfg, xbr_mm_cfg, "config:32-63");
  1022. DEFINE_UNCORE_FORMAT_ATTR(xbr_match, xbr_match, "config1:0-63");
  1023. DEFINE_UNCORE_FORMAT_ATTR(xbr_mask, xbr_mask, "config2:0-63");
  1024. DEFINE_UNCORE_FORMAT_ATTR(qlx_cfg, qlx_cfg, "config1:0-15");
  1025. DEFINE_UNCORE_FORMAT_ATTR(iperf_cfg, iperf_cfg, "config1:0-31");
  1026. static struct attribute *nhmex_uncore_rbox_formats_attr[] = {
  1027. &format_attr_event5.attr,
  1028. &format_attr_xbr_mm_cfg.attr,
  1029. &format_attr_xbr_match.attr,
  1030. &format_attr_xbr_mask.attr,
  1031. &format_attr_qlx_cfg.attr,
  1032. &format_attr_iperf_cfg.attr,
  1033. NULL,
  1034. };
  1035. static const struct attribute_group nhmex_uncore_rbox_format_group = {
  1036. .name = "format",
  1037. .attrs = nhmex_uncore_rbox_formats_attr,
  1038. };
  1039. static struct uncore_event_desc nhmex_uncore_rbox_events[] = {
  1040. INTEL_UNCORE_EVENT_DESC(qpi0_flit_send, "event=0x0,iperf_cfg=0x80000000"),
  1041. INTEL_UNCORE_EVENT_DESC(qpi1_filt_send, "event=0x6,iperf_cfg=0x80000000"),
  1042. INTEL_UNCORE_EVENT_DESC(qpi0_idle_filt, "event=0x0,iperf_cfg=0x40000000"),
  1043. INTEL_UNCORE_EVENT_DESC(qpi1_idle_filt, "event=0x6,iperf_cfg=0x40000000"),
  1044. INTEL_UNCORE_EVENT_DESC(qpi0_date_response, "event=0x0,iperf_cfg=0xc4"),
  1045. INTEL_UNCORE_EVENT_DESC(qpi1_date_response, "event=0x6,iperf_cfg=0xc4"),
  1046. { /* end: all zeroes */ },
  1047. };
  1048. static struct intel_uncore_ops nhmex_uncore_rbox_ops = {
  1049. NHMEX_UNCORE_OPS_COMMON_INIT(),
  1050. .enable_event = nhmex_rbox_msr_enable_event,
  1051. .hw_config = nhmex_rbox_hw_config,
  1052. .get_constraint = nhmex_rbox_get_constraint,
  1053. .put_constraint = nhmex_rbox_put_constraint,
  1054. };
  1055. static struct intel_uncore_type nhmex_uncore_rbox = {
  1056. .name = "rbox",
  1057. .num_counters = 8,
  1058. .num_boxes = 2,
  1059. .perf_ctr_bits = 48,
  1060. .event_ctl = NHMEX_R_MSR_PMON_CTL0,
  1061. .perf_ctr = NHMEX_R_MSR_PMON_CNT0,
  1062. .event_mask = NHMEX_R_PMON_RAW_EVENT_MASK,
  1063. .box_ctl = NHMEX_R_MSR_GLOBAL_CTL,
  1064. .msr_offset = NHMEX_R_MSR_OFFSET,
  1065. .pair_ctr_ctl = 1,
  1066. .num_shared_regs = 20,
  1067. .event_descs = nhmex_uncore_rbox_events,
  1068. .ops = &nhmex_uncore_rbox_ops,
  1069. .format_group = &nhmex_uncore_rbox_format_group
  1070. };
  1071. static struct intel_uncore_type *nhmex_msr_uncores[] = {
  1072. &nhmex_uncore_ubox,
  1073. &nhmex_uncore_cbox,
  1074. &nhmex_uncore_bbox,
  1075. &nhmex_uncore_sbox,
  1076. &nhmex_uncore_mbox,
  1077. &nhmex_uncore_rbox,
  1078. &nhmex_uncore_wbox,
  1079. NULL,
  1080. };
  1081. void nhmex_uncore_cpu_init(void)
  1082. {
  1083. if (boot_cpu_data.x86_model == 46)
  1084. uncore_nhmex = true;
  1085. else
  1086. nhmex_uncore_mbox.event_descs = wsmex_uncore_mbox_events;
  1087. if (nhmex_uncore_cbox.num_boxes > boot_cpu_data.x86_max_cores)
  1088. nhmex_uncore_cbox.num_boxes = boot_cpu_data.x86_max_cores;
  1089. uncore_msr_uncores = nhmex_msr_uncores;
  1090. }
  1091. /* end of Nehalem-EX uncore support */