isa207-common.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Common Performance counter support functions for PowerISA v2.07 processors.
  4. *
  5. * Copyright 2009 Paul Mackerras, IBM Corporation.
  6. * Copyright 2013 Michael Ellerman, IBM Corporation.
  7. * Copyright 2016 Madhavan Srinivasan, IBM Corporation.
  8. */
  9. #include "isa207-common.h"
  10. PMU_FORMAT_ATTR(event, "config:0-49");
  11. PMU_FORMAT_ATTR(pmcxsel, "config:0-7");
  12. PMU_FORMAT_ATTR(mark, "config:8");
  13. PMU_FORMAT_ATTR(combine, "config:11");
  14. PMU_FORMAT_ATTR(unit, "config:12-15");
  15. PMU_FORMAT_ATTR(pmc, "config:16-19");
  16. PMU_FORMAT_ATTR(cache_sel, "config:20-23");
  17. PMU_FORMAT_ATTR(sample_mode, "config:24-28");
  18. PMU_FORMAT_ATTR(thresh_sel, "config:29-31");
  19. PMU_FORMAT_ATTR(thresh_stop, "config:32-35");
  20. PMU_FORMAT_ATTR(thresh_start, "config:36-39");
  21. PMU_FORMAT_ATTR(thresh_cmp, "config:40-49");
  22. static struct attribute *isa207_pmu_format_attr[] = {
  23. &format_attr_event.attr,
  24. &format_attr_pmcxsel.attr,
  25. &format_attr_mark.attr,
  26. &format_attr_combine.attr,
  27. &format_attr_unit.attr,
  28. &format_attr_pmc.attr,
  29. &format_attr_cache_sel.attr,
  30. &format_attr_sample_mode.attr,
  31. &format_attr_thresh_sel.attr,
  32. &format_attr_thresh_stop.attr,
  33. &format_attr_thresh_start.attr,
  34. &format_attr_thresh_cmp.attr,
  35. NULL,
  36. };
  37. const struct attribute_group isa207_pmu_format_group = {
  38. .name = "format",
  39. .attrs = isa207_pmu_format_attr,
  40. };
  41. static inline bool event_is_fab_match(u64 event)
  42. {
  43. /* Only check pmc, unit and pmcxsel, ignore the edge bit (0) */
  44. event &= 0xff0fe;
  45. /* PM_MRK_FAB_RSP_MATCH & PM_MRK_FAB_RSP_MATCH_CYC */
  46. return (event == 0x30056 || event == 0x4f052);
  47. }
  48. static bool is_event_valid(u64 event)
  49. {
  50. u64 valid_mask = EVENT_VALID_MASK;
  51. if (cpu_has_feature(CPU_FTR_ARCH_31))
  52. valid_mask = p10_EVENT_VALID_MASK;
  53. else if (cpu_has_feature(CPU_FTR_ARCH_300))
  54. valid_mask = p9_EVENT_VALID_MASK;
  55. return !(event & ~valid_mask);
  56. }
  57. static inline bool is_event_marked(u64 event)
  58. {
  59. if (event & EVENT_IS_MARKED)
  60. return true;
  61. return false;
  62. }
  63. static unsigned long sdar_mod_val(u64 event)
  64. {
  65. if (cpu_has_feature(CPU_FTR_ARCH_31))
  66. return p10_SDAR_MODE(event);
  67. return p9_SDAR_MODE(event);
  68. }
  69. static void mmcra_sdar_mode(u64 event, unsigned long *mmcra)
  70. {
  71. /*
  72. * MMCRA[SDAR_MODE] specifies how the SDAR should be updated in
  73. * continuous sampling mode.
  74. *
  75. * Incase of Power8:
  76. * MMCRA[SDAR_MODE] will be programmed as "0b01" for continuous sampling
  77. * mode and will be un-changed when setting MMCRA[63] (Marked events).
  78. *
  79. * Incase of Power9/power10:
  80. * Marked event: MMCRA[SDAR_MODE] will be set to 0b00 ('No Updates'),
  81. * or if group already have any marked events.
  82. * For rest
  83. * MMCRA[SDAR_MODE] will be set from event code.
  84. * If sdar_mode from event is zero, default to 0b01. Hardware
  85. * requires that we set a non-zero value.
  86. */
  87. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  88. if (is_event_marked(event) || (*mmcra & MMCRA_SAMPLE_ENABLE))
  89. *mmcra &= MMCRA_SDAR_MODE_NO_UPDATES;
  90. else if (sdar_mod_val(event))
  91. *mmcra |= sdar_mod_val(event) << MMCRA_SDAR_MODE_SHIFT;
  92. else
  93. *mmcra |= MMCRA_SDAR_MODE_DCACHE;
  94. } else
  95. *mmcra |= MMCRA_SDAR_MODE_TLB;
  96. }
  97. static int p10_thresh_cmp_val(u64 value)
  98. {
  99. int exp = 0;
  100. u64 result = value;
  101. if (!value)
  102. return value;
  103. /*
  104. * Incase of P10, thresh_cmp value is not part of raw event code
  105. * and provided via attr.config1 parameter. To program threshold in MMCRA,
  106. * take a 18 bit number N and shift right 2 places and increment
  107. * the exponent E by 1 until the upper 10 bits of N are zero.
  108. * Write E to the threshold exponent and write the lower 8 bits of N
  109. * to the threshold mantissa.
  110. * The max threshold that can be written is 261120.
  111. */
  112. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  113. if (value > 261120)
  114. value = 261120;
  115. while ((64 - __builtin_clzl(value)) > 8) {
  116. exp++;
  117. value >>= 2;
  118. }
  119. /*
  120. * Note that it is invalid to write a mantissa with the
  121. * upper 2 bits of mantissa being zero, unless the
  122. * exponent is also zero.
  123. */
  124. if (!(value & 0xC0) && exp)
  125. result = -1;
  126. else
  127. result = (exp << 8) | value;
  128. }
  129. return result;
  130. }
  131. static u64 thresh_cmp_val(u64 value)
  132. {
  133. if (cpu_has_feature(CPU_FTR_ARCH_31))
  134. value = p10_thresh_cmp_val(value);
  135. /*
  136. * Since location of threshold compare bits in MMCRA
  137. * is different for p8, using different shift value.
  138. */
  139. if (cpu_has_feature(CPU_FTR_ARCH_300))
  140. return value << p9_MMCRA_THR_CMP_SHIFT;
  141. else
  142. return value << MMCRA_THR_CMP_SHIFT;
  143. }
  144. static unsigned long combine_from_event(u64 event)
  145. {
  146. if (cpu_has_feature(CPU_FTR_ARCH_300))
  147. return p9_EVENT_COMBINE(event);
  148. return EVENT_COMBINE(event);
  149. }
  150. static unsigned long combine_shift(unsigned long pmc)
  151. {
  152. if (cpu_has_feature(CPU_FTR_ARCH_300))
  153. return p9_MMCR1_COMBINE_SHIFT(pmc);
  154. return MMCR1_COMBINE_SHIFT(pmc);
  155. }
  156. static inline bool event_is_threshold(u64 event)
  157. {
  158. return (event >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
  159. }
  160. static bool is_thresh_cmp_valid(u64 event)
  161. {
  162. unsigned int cmp, exp;
  163. if (cpu_has_feature(CPU_FTR_ARCH_31))
  164. return p10_thresh_cmp_val(event) >= 0;
  165. /*
  166. * Check the mantissa upper two bits are not zero, unless the
  167. * exponent is also zero. See the THRESH_CMP_MANTISSA doc.
  168. */
  169. cmp = (event >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
  170. exp = cmp >> 7;
  171. if (exp && (cmp & 0x60) == 0)
  172. return false;
  173. return true;
  174. }
  175. static unsigned int dc_ic_rld_quad_l1_sel(u64 event)
  176. {
  177. unsigned int cache;
  178. cache = (event >> EVENT_CACHE_SEL_SHIFT) & MMCR1_DC_IC_QUAL_MASK;
  179. return cache;
  180. }
  181. static inline u64 isa207_find_source(u64 idx, u32 sub_idx)
  182. {
  183. u64 ret = PERF_MEM_NA;
  184. switch(idx) {
  185. case 0:
  186. /* Nothing to do */
  187. break;
  188. case 1:
  189. ret = PH(LVL, L1) | LEVEL(L1) | P(SNOOP, HIT);
  190. break;
  191. case 2:
  192. ret = PH(LVL, L2) | LEVEL(L2) | P(SNOOP, HIT);
  193. break;
  194. case 3:
  195. ret = PH(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
  196. break;
  197. case 4:
  198. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  199. ret = P(SNOOP, HIT);
  200. if (sub_idx == 1)
  201. ret |= PH(LVL, LOC_RAM) | LEVEL(RAM);
  202. else if (sub_idx == 2 || sub_idx == 3)
  203. ret |= P(LVL, HIT) | LEVEL(PMEM);
  204. else if (sub_idx == 4)
  205. ret |= PH(LVL, REM_RAM1) | REM | LEVEL(RAM) | P(HOPS, 2);
  206. else if (sub_idx == 5 || sub_idx == 7)
  207. ret |= P(LVL, HIT) | LEVEL(PMEM) | REM;
  208. else if (sub_idx == 6)
  209. ret |= PH(LVL, REM_RAM2) | REM | LEVEL(RAM) | P(HOPS, 3);
  210. } else {
  211. if (sub_idx <= 1)
  212. ret = PH(LVL, LOC_RAM);
  213. else if (sub_idx > 1 && sub_idx <= 2)
  214. ret = PH(LVL, REM_RAM1);
  215. else
  216. ret = PH(LVL, REM_RAM2);
  217. ret |= P(SNOOP, HIT);
  218. }
  219. break;
  220. case 5:
  221. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  222. ret = REM | P(HOPS, 0);
  223. if (sub_idx == 0 || sub_idx == 4)
  224. ret |= PH(LVL, L2) | LEVEL(L2) | P(SNOOP, HIT);
  225. else if (sub_idx == 1 || sub_idx == 5)
  226. ret |= PH(LVL, L2) | LEVEL(L2) | P(SNOOP, HITM);
  227. else if (sub_idx == 2 || sub_idx == 6)
  228. ret |= PH(LVL, L3) | LEVEL(L3) | P(SNOOP, HIT);
  229. else if (sub_idx == 3 || sub_idx == 7)
  230. ret |= PH(LVL, L3) | LEVEL(L3) | P(SNOOP, HITM);
  231. } else {
  232. if (sub_idx == 0)
  233. ret = PH(LVL, L2) | LEVEL(L2) | REM | P(SNOOP, HIT) | P(HOPS, 0);
  234. else if (sub_idx == 1)
  235. ret = PH(LVL, L2) | LEVEL(L2) | REM | P(SNOOP, HITM) | P(HOPS, 0);
  236. else if (sub_idx == 2 || sub_idx == 4)
  237. ret = PH(LVL, L3) | LEVEL(L3) | REM | P(SNOOP, HIT) | P(HOPS, 0);
  238. else if (sub_idx == 3 || sub_idx == 5)
  239. ret = PH(LVL, L3) | LEVEL(L3) | REM | P(SNOOP, HITM) | P(HOPS, 0);
  240. }
  241. break;
  242. case 6:
  243. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  244. if (sub_idx == 0)
  245. ret = PH(LVL, REM_CCE1) | LEVEL(ANY_CACHE) | REM |
  246. P(SNOOP, HIT) | P(HOPS, 2);
  247. else if (sub_idx == 1)
  248. ret = PH(LVL, REM_CCE1) | LEVEL(ANY_CACHE) | REM |
  249. P(SNOOP, HITM) | P(HOPS, 2);
  250. else if (sub_idx == 2)
  251. ret = PH(LVL, REM_CCE2) | LEVEL(ANY_CACHE) | REM |
  252. P(SNOOP, HIT) | P(HOPS, 3);
  253. else if (sub_idx == 3)
  254. ret = PH(LVL, REM_CCE2) | LEVEL(ANY_CACHE) | REM |
  255. P(SNOOP, HITM) | P(HOPS, 3);
  256. } else {
  257. ret = PH(LVL, REM_CCE2);
  258. if (sub_idx == 0 || sub_idx == 2)
  259. ret |= P(SNOOP, HIT);
  260. else if (sub_idx == 1 || sub_idx == 3)
  261. ret |= P(SNOOP, HITM);
  262. }
  263. break;
  264. case 7:
  265. ret = PM(LVL, L1);
  266. break;
  267. }
  268. return ret;
  269. }
  270. void isa207_get_mem_data_src(union perf_mem_data_src *dsrc, u32 flags,
  271. struct pt_regs *regs)
  272. {
  273. u64 idx;
  274. u32 sub_idx;
  275. u64 sier;
  276. u64 val;
  277. /* Skip if no SIER support */
  278. if (!(flags & PPMU_HAS_SIER)) {
  279. dsrc->val = 0;
  280. return;
  281. }
  282. /*
  283. * Use regs-dar for SPRN_SIER which is saved
  284. * during perf_read_regs at the beginning
  285. * of the PMU interrupt handler to avoid multiple
  286. * reads of SPRN_SIER
  287. */
  288. sier = regs->dar;
  289. val = (sier & ISA207_SIER_TYPE_MASK) >> ISA207_SIER_TYPE_SHIFT;
  290. if (val != 1 && val != 2 && !(val == 7 && cpu_has_feature(CPU_FTR_ARCH_31))) {
  291. dsrc->val = 0;
  292. return;
  293. }
  294. idx = (sier & ISA207_SIER_LDST_MASK) >> ISA207_SIER_LDST_SHIFT;
  295. sub_idx = (sier & ISA207_SIER_DATA_SRC_MASK) >> ISA207_SIER_DATA_SRC_SHIFT;
  296. dsrc->val = isa207_find_source(idx, sub_idx);
  297. if (val == 7) {
  298. u64 mmcra;
  299. u32 op_type;
  300. /*
  301. * Type 0b111 denotes either larx or stcx instruction. Use the
  302. * MMCRA sampling bits [57:59] along with the type value
  303. * to determine the exact instruction type. If the sampling
  304. * criteria is neither load or store, set the type as default
  305. * to NA.
  306. *
  307. * Use regs->dsisr for MMCRA which is saved during perf_read_regs
  308. * at the beginning of the PMU interrupt handler to avoid
  309. * multiple reads of SPRN_MMCRA
  310. */
  311. mmcra = regs->dsisr;
  312. op_type = (mmcra >> MMCRA_SAMP_ELIG_SHIFT) & MMCRA_SAMP_ELIG_MASK;
  313. switch (op_type) {
  314. case 5:
  315. dsrc->val |= P(OP, LOAD);
  316. break;
  317. case 7:
  318. dsrc->val |= P(OP, STORE);
  319. break;
  320. default:
  321. dsrc->val |= P(OP, NA);
  322. break;
  323. }
  324. } else {
  325. dsrc->val |= (val == 1) ? P(OP, LOAD) : P(OP, STORE);
  326. }
  327. }
  328. void isa207_get_mem_weight(u64 *weight, u64 type)
  329. {
  330. union perf_sample_weight *weight_fields;
  331. u64 weight_lat;
  332. u64 mmcra = mfspr(SPRN_MMCRA);
  333. u64 exp = MMCRA_THR_CTR_EXP(mmcra);
  334. u64 mantissa = MMCRA_THR_CTR_MANT(mmcra);
  335. u64 sier = mfspr(SPRN_SIER);
  336. u64 val = (sier & ISA207_SIER_TYPE_MASK) >> ISA207_SIER_TYPE_SHIFT;
  337. if (cpu_has_feature(CPU_FTR_ARCH_31))
  338. mantissa = P10_MMCRA_THR_CTR_MANT(mmcra);
  339. if (val == 0 || (val == 7 && !cpu_has_feature(CPU_FTR_ARCH_31)))
  340. weight_lat = 0;
  341. else
  342. weight_lat = mantissa << (2 * exp);
  343. /*
  344. * Use 64 bit weight field (full) if sample type is
  345. * WEIGHT.
  346. *
  347. * if sample type is WEIGHT_STRUCT:
  348. * - store memory latency in the lower 32 bits.
  349. * - For ISA v3.1, use remaining two 16 bit fields of
  350. * perf_sample_weight to store cycle counter values
  351. * from sier2.
  352. */
  353. weight_fields = (union perf_sample_weight *)weight;
  354. if (type & PERF_SAMPLE_WEIGHT)
  355. weight_fields->full = weight_lat;
  356. else {
  357. weight_fields->var1_dw = (u32)weight_lat;
  358. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  359. weight_fields->var2_w = P10_SIER2_FINISH_CYC(mfspr(SPRN_SIER2));
  360. weight_fields->var3_w = P10_SIER2_DISPATCH_CYC(mfspr(SPRN_SIER2));
  361. }
  362. }
  363. }
  364. int isa207_get_constraint(u64 event, unsigned long *maskp, unsigned long *valp, u64 event_config1)
  365. {
  366. unsigned int unit, pmc, cache, ebb;
  367. unsigned long mask, value;
  368. mask = value = 0;
  369. if (!is_event_valid(event))
  370. return -1;
  371. pmc = (event >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  372. unit = (event >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
  373. if (cpu_has_feature(CPU_FTR_ARCH_31))
  374. cache = (event >> EVENT_CACHE_SEL_SHIFT) &
  375. p10_EVENT_CACHE_SEL_MASK;
  376. else
  377. cache = (event >> EVENT_CACHE_SEL_SHIFT) &
  378. EVENT_CACHE_SEL_MASK;
  379. ebb = (event >> EVENT_EBB_SHIFT) & EVENT_EBB_MASK;
  380. if (pmc) {
  381. u64 base_event;
  382. if (pmc > 6)
  383. return -1;
  384. /* Ignore Linux defined bits when checking event below */
  385. base_event = event & ~EVENT_LINUX_MASK;
  386. if (pmc >= 5 && base_event != 0x500fa &&
  387. base_event != 0x600f4)
  388. return -1;
  389. mask |= CNST_PMC_MASK(pmc);
  390. value |= CNST_PMC_VAL(pmc);
  391. /*
  392. * PMC5 and PMC6 are used to count cycles and instructions and
  393. * they do not support most of the constraint bits. Add a check
  394. * to exclude PMC5/6 from most of the constraints except for
  395. * EBB/BHRB.
  396. */
  397. if (pmc >= 5)
  398. goto ebb_bhrb;
  399. }
  400. if (pmc <= 4) {
  401. /*
  402. * Add to number of counters in use. Note this includes events with
  403. * a PMC of 0 - they still need a PMC, it's just assigned later.
  404. * Don't count events on PMC 5 & 6, there is only one valid event
  405. * on each of those counters, and they are handled above.
  406. */
  407. mask |= CNST_NC_MASK;
  408. value |= CNST_NC_VAL;
  409. }
  410. if (unit >= 6 && unit <= 9) {
  411. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  412. if (unit == 6) {
  413. mask |= CNST_L2L3_GROUP_MASK;
  414. value |= CNST_L2L3_GROUP_VAL(event >> p10_L2L3_EVENT_SHIFT);
  415. }
  416. } else if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  417. mask |= CNST_CACHE_GROUP_MASK;
  418. value |= CNST_CACHE_GROUP_VAL(event & 0xff);
  419. mask |= CNST_CACHE_PMC4_MASK;
  420. if (pmc == 4)
  421. value |= CNST_CACHE_PMC4_VAL;
  422. } else if (cache & 0x7) {
  423. /*
  424. * L2/L3 events contain a cache selector field, which is
  425. * supposed to be programmed into MMCRC. However MMCRC is only
  426. * HV writable, and there is no API for guest kernels to modify
  427. * it. The solution is for the hypervisor to initialise the
  428. * field to zeroes, and for us to only ever allow events that
  429. * have a cache selector of zero. The bank selector (bit 3) is
  430. * irrelevant, as long as the rest of the value is 0.
  431. */
  432. return -1;
  433. }
  434. } else if (cpu_has_feature(CPU_FTR_ARCH_300) || (event & EVENT_IS_L1)) {
  435. mask |= CNST_L1_QUAL_MASK;
  436. value |= CNST_L1_QUAL_VAL(cache);
  437. }
  438. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  439. mask |= CNST_RADIX_SCOPE_GROUP_MASK;
  440. value |= CNST_RADIX_SCOPE_GROUP_VAL(event >> p10_EVENT_RADIX_SCOPE_QUAL_SHIFT);
  441. }
  442. if (is_event_marked(event)) {
  443. mask |= CNST_SAMPLE_MASK;
  444. value |= CNST_SAMPLE_VAL(event >> EVENT_SAMPLE_SHIFT);
  445. }
  446. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  447. if (event_is_threshold(event) && is_thresh_cmp_valid(event_config1)) {
  448. mask |= CNST_THRESH_CTL_SEL_MASK;
  449. value |= CNST_THRESH_CTL_SEL_VAL(event >> EVENT_THRESH_SHIFT);
  450. mask |= p10_CNST_THRESH_CMP_MASK;
  451. value |= p10_CNST_THRESH_CMP_VAL(p10_thresh_cmp_val(event_config1));
  452. } else if (event_is_threshold(event))
  453. return -1;
  454. } else if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  455. if (event_is_threshold(event) && is_thresh_cmp_valid(event)) {
  456. mask |= CNST_THRESH_MASK;
  457. value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
  458. } else if (event_is_threshold(event))
  459. return -1;
  460. } else {
  461. /*
  462. * Special case for PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
  463. * the threshold control bits are used for the match value.
  464. */
  465. if (event_is_fab_match(event)) {
  466. mask |= CNST_FAB_MATCH_MASK;
  467. value |= CNST_FAB_MATCH_VAL(event >> EVENT_THR_CTL_SHIFT);
  468. } else {
  469. if (!is_thresh_cmp_valid(event))
  470. return -1;
  471. mask |= CNST_THRESH_MASK;
  472. value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
  473. }
  474. }
  475. ebb_bhrb:
  476. if (!pmc && ebb)
  477. /* EBB events must specify the PMC */
  478. return -1;
  479. if (event & EVENT_WANTS_BHRB) {
  480. if (!ebb)
  481. /* Only EBB events can request BHRB */
  482. return -1;
  483. mask |= CNST_IFM_MASK;
  484. value |= CNST_IFM_VAL(event >> EVENT_IFM_SHIFT);
  485. }
  486. /*
  487. * All events must agree on EBB, either all request it or none.
  488. * EBB events are pinned & exclusive, so this should never actually
  489. * hit, but we leave it as a fallback in case.
  490. */
  491. mask |= CNST_EBB_MASK;
  492. value |= CNST_EBB_VAL(ebb);
  493. *maskp = mask;
  494. *valp = value;
  495. return 0;
  496. }
  497. int isa207_compute_mmcr(u64 event[], int n_ev,
  498. unsigned int hwc[], struct mmcr_regs *mmcr,
  499. struct perf_event *pevents[], u32 flags)
  500. {
  501. unsigned long mmcra, mmcr1, mmcr2, unit, combine, psel, cache, val;
  502. unsigned long mmcr3;
  503. unsigned int pmc, pmc_inuse;
  504. int i;
  505. pmc_inuse = 0;
  506. /* First pass to count resource use */
  507. for (i = 0; i < n_ev; ++i) {
  508. pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  509. if (pmc)
  510. pmc_inuse |= 1 << pmc;
  511. }
  512. mmcra = mmcr1 = mmcr2 = mmcr3 = 0;
  513. /*
  514. * Disable bhrb unless explicitly requested
  515. * by setting MMCRA (BHRBRD) bit.
  516. */
  517. if (cpu_has_feature(CPU_FTR_ARCH_31))
  518. mmcra |= MMCRA_BHRB_DISABLE;
  519. /* Second pass: assign PMCs, set all MMCR1 fields */
  520. for (i = 0; i < n_ev; ++i) {
  521. pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
  522. unit = (event[i] >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
  523. combine = combine_from_event(event[i]);
  524. psel = event[i] & EVENT_PSEL_MASK;
  525. if (!pmc) {
  526. for (pmc = 1; pmc <= 4; ++pmc) {
  527. if (!(pmc_inuse & (1 << pmc)))
  528. break;
  529. }
  530. pmc_inuse |= 1 << pmc;
  531. }
  532. if (pmc <= 4) {
  533. mmcr1 |= unit << MMCR1_UNIT_SHIFT(pmc);
  534. mmcr1 |= combine << combine_shift(pmc);
  535. mmcr1 |= psel << MMCR1_PMCSEL_SHIFT(pmc);
  536. }
  537. /* In continuous sampling mode, update SDAR on TLB miss */
  538. mmcra_sdar_mode(event[i], &mmcra);
  539. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  540. cache = dc_ic_rld_quad_l1_sel(event[i]);
  541. mmcr1 |= (cache) << MMCR1_DC_IC_QUAL_SHIFT;
  542. } else {
  543. if (event[i] & EVENT_IS_L1) {
  544. cache = dc_ic_rld_quad_l1_sel(event[i]);
  545. mmcr1 |= (cache) << MMCR1_DC_IC_QUAL_SHIFT;
  546. }
  547. }
  548. /* Set RADIX_SCOPE_QUAL bit */
  549. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  550. val = (event[i] >> p10_EVENT_RADIX_SCOPE_QUAL_SHIFT) &
  551. p10_EVENT_RADIX_SCOPE_QUAL_MASK;
  552. mmcr1 |= val << p10_MMCR1_RADIX_SCOPE_QUAL_SHIFT;
  553. }
  554. if (is_event_marked(event[i])) {
  555. mmcra |= MMCRA_SAMPLE_ENABLE;
  556. val = (event[i] >> EVENT_SAMPLE_SHIFT) & EVENT_SAMPLE_MASK;
  557. if (val) {
  558. mmcra |= (val & 3) << MMCRA_SAMP_MODE_SHIFT;
  559. mmcra |= (val >> 2) << MMCRA_SAMP_ELIG_SHIFT;
  560. }
  561. }
  562. /*
  563. * PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
  564. * the threshold bits are used for the match value.
  565. */
  566. if (!cpu_has_feature(CPU_FTR_ARCH_300) && event_is_fab_match(event[i])) {
  567. mmcr1 |= ((event[i] >> EVENT_THR_CTL_SHIFT) &
  568. EVENT_THR_CTL_MASK) << MMCR1_FAB_SHIFT;
  569. } else {
  570. val = (event[i] >> EVENT_THR_CTL_SHIFT) & EVENT_THR_CTL_MASK;
  571. mmcra |= val << MMCRA_THR_CTL_SHIFT;
  572. val = (event[i] >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
  573. mmcra |= val << MMCRA_THR_SEL_SHIFT;
  574. if (!cpu_has_feature(CPU_FTR_ARCH_31)) {
  575. val = (event[i] >> EVENT_THR_CMP_SHIFT) &
  576. EVENT_THR_CMP_MASK;
  577. mmcra |= thresh_cmp_val(val);
  578. } else if (flags & PPMU_HAS_ATTR_CONFIG1) {
  579. val = (pevents[i]->attr.config1 >> p10_EVENT_THR_CMP_SHIFT) &
  580. p10_EVENT_THR_CMP_MASK;
  581. mmcra |= thresh_cmp_val(val);
  582. }
  583. }
  584. if (cpu_has_feature(CPU_FTR_ARCH_31) && (unit == 6)) {
  585. val = (event[i] >> p10_L2L3_EVENT_SHIFT) &
  586. p10_EVENT_L2L3_SEL_MASK;
  587. mmcr2 |= val << p10_L2L3_SEL_SHIFT;
  588. }
  589. if (event[i] & EVENT_WANTS_BHRB) {
  590. val = (event[i] >> EVENT_IFM_SHIFT) & EVENT_IFM_MASK;
  591. mmcra |= val << MMCRA_IFM_SHIFT;
  592. }
  593. /* set MMCRA (BHRBRD) to 0 if there is user request for BHRB */
  594. if (cpu_has_feature(CPU_FTR_ARCH_31) &&
  595. (has_branch_stack(pevents[i]) || (event[i] & EVENT_WANTS_BHRB)))
  596. mmcra &= ~MMCRA_BHRB_DISABLE;
  597. if (pevents[i]->attr.exclude_user)
  598. mmcr2 |= MMCR2_FCP(pmc);
  599. if (pevents[i]->attr.exclude_hv)
  600. mmcr2 |= MMCR2_FCH(pmc);
  601. if (pevents[i]->attr.exclude_kernel) {
  602. if (cpu_has_feature(CPU_FTR_HVMODE))
  603. mmcr2 |= MMCR2_FCH(pmc);
  604. else
  605. mmcr2 |= MMCR2_FCS(pmc);
  606. }
  607. if (pevents[i]->attr.exclude_idle)
  608. mmcr2 |= MMCR2_FCWAIT(pmc);
  609. if (cpu_has_feature(CPU_FTR_ARCH_31)) {
  610. if (pmc <= 4) {
  611. val = (event[i] >> p10_EVENT_MMCR3_SHIFT) &
  612. p10_EVENT_MMCR3_MASK;
  613. mmcr3 |= val << MMCR3_SHIFT(pmc);
  614. }
  615. }
  616. hwc[i] = pmc - 1;
  617. }
  618. /* Return MMCRx values */
  619. mmcr->mmcr0 = 0;
  620. /* pmc_inuse is 1-based */
  621. if (pmc_inuse & 2)
  622. mmcr->mmcr0 = MMCR0_PMC1CE;
  623. if (pmc_inuse & 0x7c)
  624. mmcr->mmcr0 |= MMCR0_PMCjCE;
  625. /* If we're not using PMC 5 or 6, freeze them */
  626. if (!(pmc_inuse & 0x60))
  627. mmcr->mmcr0 |= MMCR0_FC56;
  628. /*
  629. * Set mmcr0 (PMCCEXT) for p10 which
  630. * will restrict access to group B registers
  631. * when MMCR0 PMCC=0b00.
  632. */
  633. if (cpu_has_feature(CPU_FTR_ARCH_31))
  634. mmcr->mmcr0 |= MMCR0_PMCCEXT;
  635. mmcr->mmcr1 = mmcr1;
  636. mmcr->mmcra = mmcra;
  637. mmcr->mmcr2 = mmcr2;
  638. mmcr->mmcr3 = mmcr3;
  639. return 0;
  640. }
  641. void isa207_disable_pmc(unsigned int pmc, struct mmcr_regs *mmcr)
  642. {
  643. if (pmc <= 3)
  644. mmcr->mmcr1 &= ~(0xffUL << MMCR1_PMCSEL_SHIFT(pmc + 1));
  645. }
  646. static int find_alternative(u64 event, const unsigned int ev_alt[][MAX_ALT], int size)
  647. {
  648. int i, j;
  649. for (i = 0; i < size; ++i) {
  650. if (event < ev_alt[i][0])
  651. break;
  652. for (j = 0; j < MAX_ALT && ev_alt[i][j]; ++j)
  653. if (event == ev_alt[i][j])
  654. return i;
  655. }
  656. return -1;
  657. }
  658. int isa207_get_alternatives(u64 event, u64 alt[], int size, unsigned int flags,
  659. const unsigned int ev_alt[][MAX_ALT])
  660. {
  661. int i, j, num_alt = 0;
  662. u64 alt_event;
  663. alt[num_alt++] = event;
  664. i = find_alternative(event, ev_alt, size);
  665. if (i >= 0) {
  666. /* Filter out the original event, it's already in alt[0] */
  667. for (j = 0; j < MAX_ALT; ++j) {
  668. alt_event = ev_alt[i][j];
  669. if (alt_event && alt_event != event)
  670. alt[num_alt++] = alt_event;
  671. }
  672. }
  673. if (flags & PPMU_ONLY_COUNT_RUN) {
  674. /*
  675. * We're only counting in RUN state, so PM_CYC is equivalent to
  676. * PM_RUN_CYC and PM_INST_CMPL === PM_RUN_INST_CMPL.
  677. */
  678. j = num_alt;
  679. for (i = 0; i < num_alt; ++i) {
  680. switch (alt[i]) {
  681. case 0x1e: /* PMC_CYC */
  682. alt[j++] = 0x600f4; /* PM_RUN_CYC */
  683. break;
  684. case 0x600f4:
  685. alt[j++] = 0x1e;
  686. break;
  687. case 0x2: /* PM_INST_CMPL */
  688. alt[j++] = 0x500fa; /* PM_RUN_INST_CMPL */
  689. break;
  690. case 0x500fa:
  691. alt[j++] = 0x2;
  692. break;
  693. }
  694. }
  695. num_alt = j;
  696. }
  697. return num_alt;
  698. }
  699. int isa3XX_check_attr_config(struct perf_event *ev)
  700. {
  701. u64 val, sample_mode;
  702. u64 event = ev->attr.config;
  703. val = (event >> EVENT_SAMPLE_SHIFT) & EVENT_SAMPLE_MASK;
  704. sample_mode = val & 0x3;
  705. /*
  706. * MMCRA[61:62] is Random Sampling Mode (SM).
  707. * value of 0b11 is reserved.
  708. */
  709. if (sample_mode == 0x3)
  710. return -EINVAL;
  711. /*
  712. * Check for all reserved value
  713. * Source: Performance Monitoring Unit User Guide
  714. */
  715. switch (val) {
  716. case 0x5:
  717. case 0x9:
  718. case 0xD:
  719. case 0x19:
  720. case 0x1D:
  721. case 0x1A:
  722. case 0x1E:
  723. return -EINVAL;
  724. }
  725. /*
  726. * MMCRA[48:51]/[52:55]) Threshold Start/Stop
  727. * Events Selection.
  728. * 0b11110000/0b00001111 is reserved.
  729. */
  730. val = (event >> EVENT_THR_CTL_SHIFT) & EVENT_THR_CTL_MASK;
  731. if (((val & 0xF0) == 0xF0) || ((val & 0xF) == 0xF))
  732. return -EINVAL;
  733. return 0;
  734. }