From: Madhavan Srinivasan <ma...@linux.ibm.com>

Each platform has raw event encoding format which specifies
the bit positions for different fields. The fields from event
code gets translated into performance monitoring mode control
register (MMCRx) settings. Patch add macros to extract individual
fields from the event code.

Patch adds function for sanity checks, since testcases currently
are only supported in power9 and power10.

Signed-off-by: Madhavan Srinivasan <ma...@linux.ibm.com>
---
 .../powerpc/pmu/sampling_tests/misc.c         | 227 ++++++++++++++++++
 .../powerpc/pmu/sampling_tests/misc.h         |  50 ++++
 2 files changed, 277 insertions(+)

diff --git a/tools/testing/selftests/powerpc/pmu/sampling_tests/misc.c 
b/tools/testing/selftests/powerpc/pmu/sampling_tests/misc.c
index 4779b107f43b..fcd139c95971 100644
--- a/tools/testing/selftests/powerpc/pmu/sampling_tests/misc.c
+++ b/tools/testing/selftests/powerpc/pmu/sampling_tests/misc.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
  * Copyright 2022, Athira Rajeev, IBM Corp.
+ * Copyright 2022, Madhavan Srinivasan, IBM Corp.
  */
 
 #include <unistd.h>
@@ -16,6 +17,232 @@
 
 #define PAGE_SIZE               sysconf(_SC_PAGESIZE)
 
+/* Storage for platform version */
+int pvr, pvr_dd;
+u64 platform_extended_mask;
+int pSeries;
+
+/* Mask and Shift for Event code fields */
+int ev_mask_pmcxsel, ev_shift_pmcxsel;         //pmcxsel field
+int ev_mask_marked, ev_shift_marked;           //marked filed
+int ev_mask_comb, ev_shift_comb;               //combine field
+int ev_mask_unit, ev_shift_unit;               //unit field
+int ev_mask_pmc, ev_shift_pmc;                 //pmc field
+int ev_mask_cache, ev_shift_cache;             //Cache sel field
+int ev_mask_sample, ev_shift_sample;           //Random sampling field
+int ev_mask_thd_sel, ev_shift_thd_sel;         //thresh_sel field
+int ev_mask_thd_start, ev_shift_thd_start;     //thresh_start field
+int ev_mask_thd_stop, ev_shift_thd_stop;       //thresh_stop field
+int ev_mask_thd_cmp, ev_shift_thd_cmp;         //thresh cmp field
+int ev_mask_sm, ev_shift_sm;                   //SDAR mode field
+int ev_mask_rsq, ev_shift_rsq;                 //radix scope qual field
+int ev_mask_l2l3, ev_shift_l2l3;               //l2l3 sel field
+int ev_mask_mmcr3_src, ev_shift_mmcr3_src;     //mmcr3 field
+
+static void init_ev_encodes(void)
+{
+       ev_mask_pmcxsel = 0xff;
+       ev_shift_pmcxsel = 0;
+       ev_mask_marked = 1;
+       ev_shift_marked = 8;
+       ev_mask_unit = 0xf;
+       ev_shift_unit = 12;
+       ev_mask_pmc = 0xf;
+       ev_shift_pmc = 16;
+       ev_mask_sample  = 0x1f;
+       ev_shift_sample = 24;
+       ev_mask_thd_sel = 0x7;
+       ev_shift_thd_sel = 29;
+       ev_mask_thd_start = 0xf;
+       ev_shift_thd_start = 36;
+       ev_mask_thd_stop = 0xf;
+       ev_shift_thd_stop = 32;
+
+       switch (pvr) {
+       case POWER10:
+               ev_mask_rsq = 1;
+               ev_shift_rsq = 9;
+               ev_mask_comb = 3;
+               ev_shift_comb = 10;
+               ev_mask_cache = 3;
+               ev_shift_cache = 20;
+               ev_mask_sm = 0x3;
+               ev_shift_sm = 22;
+               ev_mask_l2l3 = 0x1f;
+               ev_shift_l2l3 = 40;
+               ev_mask_mmcr3_src = 0x7fff;
+               ev_shift_mmcr3_src = 45;
+               break;
+       case POWER9:
+               ev_mask_comb = 3;
+               ev_shift_comb = 10;
+               ev_mask_cache = 0xf;
+               ev_shift_cache = 20;
+               ev_mask_thd_cmp = 0x3ff;
+               ev_shift_thd_cmp = 40;
+               ev_mask_sm = 0x3;
+               ev_shift_sm = 50;
+               break;
+       default:
+               FAIL_IF_EXIT(1);
+       }
+}
+
+static int __get_pvr(int flg)
+{
+       FILE *fd;
+       char *buf = NULL, *search_string = "revision", tmp[4];
+       size_t len = 0;
+       int ret = -1;
+
+       fd = fopen("/proc/cpuinfo", "r");
+       if (!fd)
+               return -1;
+
+       /* get to the line that matters */
+       while (getline(&buf, &len, fd) > 0) {
+               ret = strncmp(buf, search_string, strlen(search_string));
+               if (!ret)
+                       break;
+       }
+
+       switch (flg) {
+       case 1: /* get processor version number */
+               /* seek to pvr hex values within the line */
+               while (*buf++ != '(')
+                       ; /* nothing to run */
+               buf += 5;
+               strncpy(tmp, buf, 3);
+
+               fclose(fd);
+               return strtol(tmp, NULL, 16);
+
+       case 2: /* get processor major revision number */
+               /* seek to pvr hex values within the line */
+               while (*buf++ != ':')
+                       ; /* nothing to run */
+               strncpy(tmp, buf, 3);
+
+               fclose(fd);
+               return (int)strtof(tmp, NULL);
+       default:
+               return -1;
+       }
+}
+
+static int init_platform(void)
+{
+       FILE *fd;
+       char *buf = NULL, *search_string = "pSeries", *sim = "Mambo", *ptr;
+       size_t len = 0;
+
+       fd = fopen("/proc/cpuinfo", "r");
+       if (!fd)
+               return -1;
+
+       /* check for sim (like mambo) */
+       while (getline(&buf, &len, fd) > 0) {
+               ptr = strstr(buf, sim);
+               if (ptr)
+                       return -1;
+       }
+
+       fseek(fd, 0, SEEK_SET);
+
+       /* get to the line that matters */
+       while (getline(&buf, &len, fd) > 0) {
+               ptr = strstr(buf, search_string);
+               if (ptr) {
+                       pSeries = 1;
+                       break;
+               }
+       }
+
+       fclose(fd);
+       return 0;
+}
+
+static int get_ver(void)
+{
+       return __get_pvr(1);
+}
+
+static int get_rev_maj(void)
+{
+       return __get_pvr(2);
+}
+
+/* Return the extended regs mask value */
+static u64 perf_get_platform_reg_mask(void)
+{
+       if (have_hwcap2(PPC_FEATURE2_ARCH_3_1))
+               return PERF_POWER10_MASK;
+       if (have_hwcap2(PPC_FEATURE2_ARCH_3_00))
+               return PERF_POWER9_MASK;
+
+       return -1;
+}
+
+int check_extended_regs_support(void)
+{
+       int fd;
+       struct event event;
+
+       event_init(&event, 0x1001e);
+
+       event.attr.type = 4;
+       event.attr.sample_period = 1;
+       event.attr.disabled = 1;
+       event.attr.sample_type = PERF_SAMPLE_REGS_INTR;
+       event.attr.sample_regs_intr = platform_extended_mask;
+
+       fd = event_open(&event);
+       if (fd != -1)
+               return 0;
+
+       return -1;
+}
+
+int check_pvr_for_sampling_tests(void)
+{
+       pvr = get_ver();
+       /* Exit if it fails to fetch pvr */
+       if (pvr < 0) {
+               printf("%s: Failed to fetch pvr\n", __func__);
+               FAIL_IF_EXIT(1);
+       }
+
+       platform_extended_mask = perf_get_platform_reg_mask();
+
+       /*
+        * Check for supported platforms
+        * for sampling test
+        */
+       if ((pvr != POWER10) && (pvr != POWER9))
+               goto out;
+
+       /*
+        * Check PMU driver registered by looking for
+        * PPC_FEATURE2_EBB bit in AT_HWCAP2
+        */
+       if (!have_hwcap2(PPC_FEATURE2_EBB))
+               goto out;
+
+       /* check if platform supports extended regs */
+       if (check_extended_regs_support())
+               goto out;
+
+       pvr_dd = get_rev_maj();
+
+       if (init_platform())
+               goto out;
+
+       init_ev_encodes();
+       return 0;
+out:
+       printf("%s: Sampling tests un-supported\n", __func__);
+       return -1;
+}
 /*
  * Allocate mmap buffer of "mmap_pages" number of
  * pages.
diff --git a/tools/testing/selftests/powerpc/pmu/sampling_tests/misc.h 
b/tools/testing/selftests/powerpc/pmu/sampling_tests/misc.h
index 291f9adba817..8526c15d4c90 100644
--- a/tools/testing/selftests/powerpc/pmu/sampling_tests/misc.h
+++ b/tools/testing/selftests/powerpc/pmu/sampling_tests/misc.h
@@ -1,9 +1,59 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 /*
  * Copyright 2022, Athira Rajeev, IBM Corp.
+ * Copyright 2022, Madhavan Srinivasan, IBM Corp.
  */
 
 #include "../event.h"
 
+#define POWER10 0x80
+#define POWER9  0x4e
+#define PERF_POWER9_MASK        0x7f8ffffffffffff
+#define PERF_POWER10_MASK       0x7ffffffffffffff
+
+extern int ev_mask_pmcxsel, ev_shift_pmcxsel;
+extern int ev_mask_marked, ev_shift_marked;
+extern int ev_mask_comb, ev_shift_comb;
+extern int ev_mask_unit, ev_shift_unit;
+extern int ev_mask_pmc, ev_shift_pmc;
+extern int ev_mask_cache, ev_shift_cache;
+extern int ev_mask_sample, ev_shift_sample;
+extern int ev_mask_thd_sel, ev_shift_thd_sel;
+extern int ev_mask_thd_start, ev_shift_thd_start;
+extern int ev_mask_thd_stop, ev_shift_thd_stop;
+extern int ev_mask_thd_cmp, ev_shift_thd_cmp;
+extern int ev_mask_sm, ev_shift_sm;
+extern int ev_mask_rsq, ev_shift_rsq;
+extern int ev_mask_l2l3, ev_shift_l2l3;
+extern int ev_mask_mmcr3_src, ev_shift_mmcr3_src;
+extern int pvr, pvr_dd, pSeries;
+extern u64 platform_extended_mask;
+extern int check_pvr_for_sampling_tests(void);
+
+/*
+ * Event code field extraction macro.
+ * Raw event code is combination of multiple
+ * fields. Macro to extract individual fields
+ *
+ * x - Raw event code value
+ * y - Field to extract
+ */
+#define EV_CODE_EXTRACT(x, y)   \
+       ((x >> ev_shift_##y) & ev_mask_##y)
+
+/*
+ * Event attribute extraction macro.
+ *
+ * x - struct event
+ * y - attribute field
+ */
+#define GET_ATTR_FIELD(x, y)   \
+       ((x)->attr.y)
+
+static inline int is_pSeries(void)
+{
+       return pSeries;
+}
+
 void *event_sample_buf_mmap(int fd, int mmap_pages);
 void *__event_read_samples(void *sample_buff, size_t *size, u64 *sample_count);
-- 
2.27.0

Reply via email to