selftests/powerpc/pmu: Add macros to parse event codes
authorMadhavan Srinivasan <maddy@linux.ibm.com>
Thu, 27 Jan 2022 07:19:55 +0000 (12:49 +0530)
committerMichael Ellerman <mpe@ellerman.id.au>
Tue, 1 Mar 2022 12:38:12 +0000 (23:38 +1100)
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.

Add functions for sanity checks, since testcases currently are only
supported in power9 and power10.

Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com>
[mpe: Read PVR directly rather than using /proc/cpuinfo]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20220127072012.662451-4-kjain@linux.ibm.com
tools/testing/selftests/powerpc/include/reg.h
tools/testing/selftests/powerpc/pmu/sampling_tests/misc.c
tools/testing/selftests/powerpc/pmu/sampling_tests/misc.h
tools/testing/selftests/powerpc/security/spectre_v2.c

index c0f2742..c422be8 100644 (file)
@@ -52,6 +52,9 @@
 #define SPRN_TFHAR      0x80    /* Transaction Failure Handler Addr */
 #define SPRN_TAR        0x32f  /* Target Address Register */
 
+#define PVR_VER(pvr)   (((pvr) >>  16) & 0xFFFF)
+#define SPRN_PVR       0x11F
+
 #define SPRN_DSCR_PRIV 0x11    /* Privilege State DSCR */
 #define SPRN_DSCR      0x03    /* Data Stream Control Register */
 #define SPRN_PPR       896     /* Program Priority Register */
@@ -84,6 +87,7 @@
 #define TEXASR_ROT     0x0000000002000000
 
 /* MSR register bits */
+#define MSR_HV                 (1ul << 60)     /* Hypervisor state */
 #define MSR_TS_S_LG     33              /* Trans Mem state: Suspended */
 #define MSR_TS_T_LG    34              /* Trans Mem state: Active */
 
index 4779b10..a86d7d1 100644 (file)
@@ -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>
 
 #define PAGE_SIZE               sysconf(_SC_PAGESIZE)
 
+/* Storage for platform version */
+int pvr;
+u64 platform_extended_mask;
+
+/* 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);
+       }
+}
+
+/* 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 = PVR_VER(mfspr(SPRN_PVR));
+
+       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;
+
+       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.
index 291f9ad..c8f64e8 100644 (file)
@@ -1,9 +1,45 @@
 /* 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;
+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)
+
 void *event_sample_buf_mmap(int fd, int mmap_pages);
 void *__event_read_samples(void *sample_buff, size_t *size, u64 *sample_count);
index 83647b8..d42ca8c 100644 (file)
@@ -125,8 +125,6 @@ static enum spectre_v2_state get_sysfs_state(void)
 #define PM_BR_PRED_PCACHE      0x048a0 // P9 only
 #define PM_BR_MPRED_PCACHE     0x048b0 // P9 only
 
-#define SPRN_PVR 287
-
 int spectre_v2_test(void)
 {
        enum spectre_v2_state state;