linux/arch/powerpc/perf/power9-pmu.c
<<
>>
Prefs
   1/*
   2 * Performance counter support for POWER9 processors.
   3 *
   4 * Copyright 2009 Paul Mackerras, IBM Corporation.
   5 * Copyright 2013 Michael Ellerman, IBM Corporation.
   6 * Copyright 2016 Madhavan Srinivasan, IBM Corporation.
   7 *
   8 * This program is free software; you can redistribute it and/or
   9 * modify it under the terms of the GNU General Public License
  10 * as published by the Free Software Foundation; either version
  11 * 2 of the License, or later version.
  12 */
  13
  14#define pr_fmt(fmt)     "power9-pmu: " fmt
  15
  16#include "isa207-common.h"
  17
  18/*
  19 * Raw event encoding for Power9:
  20 *
  21 *        60        56        52        48        44        40        36        32
  22 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  23 *   | | [ ]                       [ ] [      thresh_cmp     ]   [  thresh_ctl   ]
  24 *   | |  |                         |                                     |
  25 *   | |  *- IFM (Linux)            |                  thresh start/stop -*
  26 *   | *- BHRB (Linux)              *sm
  27 *   *- EBB (Linux)
  28 *
  29 *        28        24        20        16        12         8         4         0
  30 * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
  31 *   [   ] [  sample ]   [cache]   [ pmc ]   [unit ]   []    m   [    pmcxsel    ]
  32 *     |        |           |                          |     |
  33 *     |        |           |                          |     *- mark
  34 *     |        |           *- L1/L2/L3 cache_sel      |
  35 *     |        |                                      |
  36 *     |        *- sampling mode for marked events     *- combine
  37 *     |
  38 *     *- thresh_sel
  39 *
  40 * Below uses IBM bit numbering.
  41 *
  42 * MMCR1[x:y] = unit    (PMCxUNIT)
  43 * MMCR1[24]   = pmc1combine[0]
  44 * MMCR1[25]   = pmc1combine[1]
  45 * MMCR1[26]   = pmc2combine[0]
  46 * MMCR1[27]   = pmc2combine[1]
  47 * MMCR1[28]   = pmc3combine[0]
  48 * MMCR1[29]   = pmc3combine[1]
  49 * MMCR1[30]   = pmc4combine[0]
  50 * MMCR1[31]   = pmc4combine[1]
  51 *
  52 * if pmc == 3 and unit == 0 and pmcxsel[0:6] == 0b0101011
  53 *      MMCR1[20:27] = thresh_ctl
  54 * else if pmc == 4 and unit == 0xf and pmcxsel[0:6] == 0b0101001
  55 *      MMCR1[20:27] = thresh_ctl
  56 * else
  57 *      MMCRA[48:55] = thresh_ctl   (THRESH START/END)
  58 *
  59 * if thresh_sel:
  60 *      MMCRA[45:47] = thresh_sel
  61 *
  62 * if thresh_cmp:
  63 *      MMCRA[9:11] = thresh_cmp[0:2]
  64 *      MMCRA[12:18] = thresh_cmp[3:9]
  65 *
  66 * MMCR1[16] = cache_sel[2]
  67 * MMCR1[17] = cache_sel[3]
  68 *
  69 * if mark:
  70 *      MMCRA[63]    = 1                (SAMPLE_ENABLE)
  71 *      MMCRA[57:59] = sample[0:2]      (RAND_SAMP_ELIG)
  72 *      MMCRA[61:62] = sample[3:4]      (RAND_SAMP_MODE)
  73 *
  74 * if EBB and BHRB:
  75 *      MMCRA[32:33] = IFM
  76 *
  77 * MMCRA[SDAR_MODE]  = sm
  78 */
  79
  80/*
  81 * Some power9 event codes.
  82 */
  83#define EVENT(_name, _code)     _name = _code,
  84
  85enum {
  86#include "power9-events-list.h"
  87};
  88
  89#undef EVENT
  90
  91/* MMCRA IFM bits - POWER9 */
  92#define POWER9_MMCRA_IFM1               0x0000000040000000UL
  93#define POWER9_MMCRA_IFM2               0x0000000080000000UL
  94#define POWER9_MMCRA_IFM3               0x00000000C0000000UL
  95#define POWER9_MMCRA_BHRB_MASK          0x00000000C0000000UL
  96
  97extern u64 PERF_REG_EXTENDED_MASK;
  98
  99/* Nasty Power9 specific hack */
 100#define PVR_POWER9_CUMULUS              0x00002000
 101
 102/* PowerISA v2.07 format attribute structure*/
 103extern struct attribute_group isa207_pmu_format_group;
 104
 105int p9_dd21_bl_ev[] = {
 106        PM_MRK_ST_DONE_L2,
 107        PM_RADIX_PWC_L1_HIT,
 108        PM_FLOP_CMPL,
 109        PM_MRK_NTF_FIN,
 110        PM_RADIX_PWC_L2_HIT,
 111        PM_IFETCH_THROTTLE,
 112        PM_MRK_L2_TM_ST_ABORT_SISTER,
 113        PM_RADIX_PWC_L3_HIT,
 114        PM_RUN_CYC_SMT2_MODE,
 115        PM_TM_TX_PASS_RUN_INST,
 116        PM_DISP_HELD_SYNC_HOLD,
 117};
 118
 119int p9_dd22_bl_ev[] = {
 120        PM_DTLB_MISS_16G,
 121        PM_DERAT_MISS_2M,
 122        PM_DTLB_MISS_2M,
 123        PM_MRK_DTLB_MISS_1G,
 124        PM_DTLB_MISS_4K,
 125        PM_DERAT_MISS_1G,
 126        PM_MRK_DERAT_MISS_2M,
 127        PM_MRK_DTLB_MISS_4K,
 128        PM_MRK_DTLB_MISS_16G,
 129        PM_DTLB_MISS_64K,
 130        PM_MRK_DERAT_MISS_1G,
 131        PM_MRK_DTLB_MISS_64K,
 132        PM_DISP_HELD_SYNC_HOLD,
 133        PM_DTLB_MISS_16M,
 134        PM_DTLB_MISS_1G,
 135        PM_MRK_DTLB_MISS_16M,
 136};
 137
 138/* Table of alternatives, sorted by column 0 */
 139static const unsigned int power9_event_alternatives[][MAX_ALT] = {
 140        { PM_INST_DISP,                 PM_INST_DISP_ALT },
 141        { PM_RUN_CYC_ALT,               PM_RUN_CYC },
 142        { PM_RUN_INST_CMPL_ALT,         PM_RUN_INST_CMPL },
 143        { PM_LD_MISS_L1,                PM_LD_MISS_L1_ALT },
 144        { PM_BR_2PATH,                  PM_BR_2PATH_ALT },
 145};
 146
 147static int power9_get_alternatives(u64 event, unsigned int flags, u64 alt[])
 148{
 149        int num_alt = 0;
 150
 151        num_alt = isa207_get_alternatives(event, alt,
 152                                          ARRAY_SIZE(power9_event_alternatives), flags,
 153                                          power9_event_alternatives);
 154
 155        return num_alt;
 156}
 157
 158GENERIC_EVENT_ATTR(cpu-cycles,                  PM_CYC);
 159GENERIC_EVENT_ATTR(stalled-cycles-frontend,     PM_ICT_NOSLOT_CYC);
 160GENERIC_EVENT_ATTR(stalled-cycles-backend,      PM_CMPLU_STALL);
 161GENERIC_EVENT_ATTR(instructions,                PM_INST_CMPL);
 162GENERIC_EVENT_ATTR(branch-instructions,         PM_BR_CMPL);
 163GENERIC_EVENT_ATTR(branch-misses,               PM_BR_MPRED_CMPL);
 164GENERIC_EVENT_ATTR(cache-references,            PM_LD_REF_L1);
 165GENERIC_EVENT_ATTR(cache-misses,                PM_LD_MISS_L1_FIN);
 166GENERIC_EVENT_ATTR(mem-loads,                   MEM_LOADS);
 167GENERIC_EVENT_ATTR(mem-stores,                  MEM_STORES);
 168
 169CACHE_EVENT_ATTR(L1-dcache-load-misses,         PM_LD_MISS_L1_FIN);
 170CACHE_EVENT_ATTR(L1-dcache-loads,               PM_LD_REF_L1);
 171CACHE_EVENT_ATTR(L1-dcache-prefetches,          PM_L1_PREF);
 172CACHE_EVENT_ATTR(L1-dcache-store-misses,        PM_ST_MISS_L1);
 173CACHE_EVENT_ATTR(L1-icache-load-misses,         PM_L1_ICACHE_MISS);
 174CACHE_EVENT_ATTR(L1-icache-loads,               PM_INST_FROM_L1);
 175CACHE_EVENT_ATTR(L1-icache-prefetches,          PM_IC_PREF_WRITE);
 176CACHE_EVENT_ATTR(LLC-load-misses,               PM_DATA_FROM_L3MISS);
 177CACHE_EVENT_ATTR(LLC-loads,                     PM_DATA_FROM_L3);
 178CACHE_EVENT_ATTR(LLC-prefetches,                PM_L3_PREF_ALL);
 179CACHE_EVENT_ATTR(branch-load-misses,            PM_BR_MPRED_CMPL);
 180CACHE_EVENT_ATTR(branch-loads,                  PM_BR_CMPL);
 181CACHE_EVENT_ATTR(dTLB-load-misses,              PM_DTLB_MISS);
 182CACHE_EVENT_ATTR(iTLB-load-misses,              PM_ITLB_MISS);
 183
 184static struct attribute *power9_events_attr[] = {
 185        GENERIC_EVENT_PTR(PM_CYC),
 186        GENERIC_EVENT_PTR(PM_ICT_NOSLOT_CYC),
 187        GENERIC_EVENT_PTR(PM_CMPLU_STALL),
 188        GENERIC_EVENT_PTR(PM_INST_CMPL),
 189        GENERIC_EVENT_PTR(PM_BR_CMPL),
 190        GENERIC_EVENT_PTR(PM_BR_MPRED_CMPL),
 191        GENERIC_EVENT_PTR(PM_LD_REF_L1),
 192        GENERIC_EVENT_PTR(PM_LD_MISS_L1_FIN),
 193        GENERIC_EVENT_PTR(MEM_LOADS),
 194        GENERIC_EVENT_PTR(MEM_STORES),
 195        CACHE_EVENT_PTR(PM_LD_MISS_L1_FIN),
 196        CACHE_EVENT_PTR(PM_LD_REF_L1),
 197        CACHE_EVENT_PTR(PM_L1_PREF),
 198        CACHE_EVENT_PTR(PM_ST_MISS_L1),
 199        CACHE_EVENT_PTR(PM_L1_ICACHE_MISS),
 200        CACHE_EVENT_PTR(PM_INST_FROM_L1),
 201        CACHE_EVENT_PTR(PM_IC_PREF_WRITE),
 202        CACHE_EVENT_PTR(PM_DATA_FROM_L3MISS),
 203        CACHE_EVENT_PTR(PM_DATA_FROM_L3),
 204        CACHE_EVENT_PTR(PM_L3_PREF_ALL),
 205        CACHE_EVENT_PTR(PM_BR_MPRED_CMPL),
 206        CACHE_EVENT_PTR(PM_BR_CMPL),
 207        CACHE_EVENT_PTR(PM_DTLB_MISS),
 208        CACHE_EVENT_PTR(PM_ITLB_MISS),
 209        NULL
 210};
 211
 212static struct attribute_group power9_pmu_events_group = {
 213        .name = "events",
 214        .attrs = power9_events_attr,
 215};
 216
 217PMU_FORMAT_ATTR(event,          "config:0-51");
 218PMU_FORMAT_ATTR(pmcxsel,        "config:0-7");
 219PMU_FORMAT_ATTR(mark,           "config:8");
 220PMU_FORMAT_ATTR(combine,        "config:10-11");
 221PMU_FORMAT_ATTR(unit,           "config:12-15");
 222PMU_FORMAT_ATTR(pmc,            "config:16-19");
 223PMU_FORMAT_ATTR(cache_sel,      "config:20-23");
 224PMU_FORMAT_ATTR(sample_mode,    "config:24-28");
 225PMU_FORMAT_ATTR(thresh_sel,     "config:29-31");
 226PMU_FORMAT_ATTR(thresh_stop,    "config:32-35");
 227PMU_FORMAT_ATTR(thresh_start,   "config:36-39");
 228PMU_FORMAT_ATTR(thresh_cmp,     "config:40-49");
 229PMU_FORMAT_ATTR(sdar_mode,      "config:50-51");
 230
 231static struct attribute *power9_pmu_format_attr[] = {
 232        &format_attr_event.attr,
 233        &format_attr_pmcxsel.attr,
 234        &format_attr_mark.attr,
 235        &format_attr_combine.attr,
 236        &format_attr_unit.attr,
 237        &format_attr_pmc.attr,
 238        &format_attr_cache_sel.attr,
 239        &format_attr_sample_mode.attr,
 240        &format_attr_thresh_sel.attr,
 241        &format_attr_thresh_stop.attr,
 242        &format_attr_thresh_start.attr,
 243        &format_attr_thresh_cmp.attr,
 244        &format_attr_sdar_mode.attr,
 245        NULL,
 246};
 247
 248static struct attribute_group power9_pmu_format_group = {
 249        .name = "format",
 250        .attrs = power9_pmu_format_attr,
 251};
 252
 253static const struct attribute_group *power9_pmu_attr_groups[] = {
 254        &power9_pmu_format_group,
 255        &power9_pmu_events_group,
 256        NULL,
 257};
 258
 259static int power9_generic_events[] = {
 260        [PERF_COUNT_HW_CPU_CYCLES] =                    PM_CYC,
 261        [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =       PM_ICT_NOSLOT_CYC,
 262        [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =        PM_CMPLU_STALL,
 263        [PERF_COUNT_HW_INSTRUCTIONS] =                  PM_INST_CMPL,
 264        [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] =           PM_BR_CMPL,
 265        [PERF_COUNT_HW_BRANCH_MISSES] =                 PM_BR_MPRED_CMPL,
 266        [PERF_COUNT_HW_CACHE_REFERENCES] =              PM_LD_REF_L1,
 267        [PERF_COUNT_HW_CACHE_MISSES] =                  PM_LD_MISS_L1_FIN,
 268};
 269
 270static u64 power9_bhrb_filter_map(u64 branch_sample_type)
 271{
 272        u64 pmu_bhrb_filter = 0;
 273
 274        /* BHRB and regular PMU events share the same privilege state
 275         * filter configuration. BHRB is always recorded along with a
 276         * regular PMU event. As the privilege state filter is handled
 277         * in the basic PMC configuration of the accompanying regular
 278         * PMU event, we ignore any separate BHRB specific request.
 279         */
 280
 281        /* No branch filter requested */
 282        if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY)
 283                return pmu_bhrb_filter;
 284
 285        /* Invalid branch filter options - HW does not support */
 286        if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
 287                return -1;
 288
 289        if (branch_sample_type & PERF_SAMPLE_BRANCH_IND_CALL)
 290                return -1;
 291
 292        if (branch_sample_type & PERF_SAMPLE_BRANCH_CALL)
 293                return -1;
 294
 295        if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_CALL) {
 296                pmu_bhrb_filter |= POWER9_MMCRA_IFM1;
 297                return pmu_bhrb_filter;
 298        }
 299
 300        /* Every thing else is unsupported */
 301        return -1;
 302}
 303
 304static void power9_config_bhrb(u64 pmu_bhrb_filter)
 305{
 306        pmu_bhrb_filter &= POWER9_MMCRA_BHRB_MASK;
 307
 308        /* Enable BHRB filter in PMU */
 309        mtspr(SPRN_MMCRA, (mfspr(SPRN_MMCRA) | pmu_bhrb_filter));
 310}
 311
 312#define C(x)    PERF_COUNT_HW_CACHE_##x
 313
 314/*
 315 * Table of generalized cache-related events.
 316 * 0 means not supported, -1 means nonsensical, other values
 317 * are event codes.
 318 */
 319static u64 power9_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
 320        [ C(L1D) ] = {
 321                [ C(OP_READ) ] = {
 322                        [ C(RESULT_ACCESS) ] = PM_LD_REF_L1,
 323                        [ C(RESULT_MISS)   ] = PM_LD_MISS_L1_FIN,
 324                },
 325                [ C(OP_WRITE) ] = {
 326                        [ C(RESULT_ACCESS) ] = 0,
 327                        [ C(RESULT_MISS)   ] = PM_ST_MISS_L1,
 328                },
 329                [ C(OP_PREFETCH) ] = {
 330                        [ C(RESULT_ACCESS) ] = PM_L1_PREF,
 331                        [ C(RESULT_MISS)   ] = 0,
 332                },
 333        },
 334        [ C(L1I) ] = {
 335                [ C(OP_READ) ] = {
 336                        [ C(RESULT_ACCESS) ] = PM_INST_FROM_L1,
 337                        [ C(RESULT_MISS)   ] = PM_L1_ICACHE_MISS,
 338                },
 339                [ C(OP_WRITE) ] = {
 340                        [ C(RESULT_ACCESS) ] = PM_L1_DEMAND_WRITE,
 341                        [ C(RESULT_MISS)   ] = -1,
 342                },
 343                [ C(OP_PREFETCH) ] = {
 344                        [ C(RESULT_ACCESS) ] = PM_IC_PREF_WRITE,
 345                        [ C(RESULT_MISS)   ] = 0,
 346                },
 347        },
 348        [ C(LL) ] = {
 349                [ C(OP_READ) ] = {
 350                        [ C(RESULT_ACCESS) ] = PM_DATA_FROM_L3,
 351                        [ C(RESULT_MISS)   ] = PM_DATA_FROM_L3MISS,
 352                },
 353                [ C(OP_WRITE) ] = {
 354                        [ C(RESULT_ACCESS) ] = 0,
 355                        [ C(RESULT_MISS)   ] = 0,
 356                },
 357                [ C(OP_PREFETCH) ] = {
 358                        [ C(RESULT_ACCESS) ] = PM_L3_PREF_ALL,
 359                        [ C(RESULT_MISS)   ] = 0,
 360                },
 361        },
 362        [ C(DTLB) ] = {
 363                [ C(OP_READ) ] = {
 364                        [ C(RESULT_ACCESS) ] = 0,
 365                        [ C(RESULT_MISS)   ] = PM_DTLB_MISS,
 366                },
 367                [ C(OP_WRITE) ] = {
 368                        [ C(RESULT_ACCESS) ] = -1,
 369                        [ C(RESULT_MISS)   ] = -1,
 370                },
 371                [ C(OP_PREFETCH) ] = {
 372                        [ C(RESULT_ACCESS) ] = -1,
 373                        [ C(RESULT_MISS)   ] = -1,
 374                },
 375        },
 376        [ C(ITLB) ] = {
 377                [ C(OP_READ) ] = {
 378                        [ C(RESULT_ACCESS) ] = 0,
 379                        [ C(RESULT_MISS)   ] = PM_ITLB_MISS,
 380                },
 381                [ C(OP_WRITE) ] = {
 382                        [ C(RESULT_ACCESS) ] = -1,
 383                        [ C(RESULT_MISS)   ] = -1,
 384                },
 385                [ C(OP_PREFETCH) ] = {
 386                        [ C(RESULT_ACCESS) ] = -1,
 387                        [ C(RESULT_MISS)   ] = -1,
 388                },
 389        },
 390        [ C(BPU) ] = {
 391                [ C(OP_READ) ] = {
 392                        [ C(RESULT_ACCESS) ] = PM_BR_CMPL,
 393                        [ C(RESULT_MISS)   ] = PM_BR_MPRED_CMPL,
 394                },
 395                [ C(OP_WRITE) ] = {
 396                        [ C(RESULT_ACCESS) ] = -1,
 397                        [ C(RESULT_MISS)   ] = -1,
 398                },
 399                [ C(OP_PREFETCH) ] = {
 400                        [ C(RESULT_ACCESS) ] = -1,
 401                        [ C(RESULT_MISS)   ] = -1,
 402                },
 403        },
 404        [ C(NODE) ] = {
 405                [ C(OP_READ) ] = {
 406                        [ C(RESULT_ACCESS) ] = -1,
 407                        [ C(RESULT_MISS)   ] = -1,
 408                },
 409                [ C(OP_WRITE) ] = {
 410                        [ C(RESULT_ACCESS) ] = -1,
 411                        [ C(RESULT_MISS)   ] = -1,
 412                },
 413                [ C(OP_PREFETCH) ] = {
 414                        [ C(RESULT_ACCESS) ] = -1,
 415                        [ C(RESULT_MISS)   ] = -1,
 416                },
 417        },
 418};
 419
 420#undef C
 421
 422static struct power_pmu power9_pmu = {
 423        .name                   = "POWER9",
 424        .n_counter              = MAX_PMU_COUNTERS,
 425        .add_fields             = ISA207_ADD_FIELDS,
 426        .test_adder             = ISA207_TEST_ADDER,
 427        .group_constraint_mask  = CNST_CACHE_PMC4_MASK,
 428        .group_constraint_val   = CNST_CACHE_PMC4_VAL,
 429        .compute_mmcr           = isa207_compute_mmcr,
 430        .config_bhrb            = power9_config_bhrb,
 431        .bhrb_filter_map        = power9_bhrb_filter_map,
 432        .get_constraint         = isa207_get_constraint,
 433        .get_alternatives       = power9_get_alternatives,
 434        .get_mem_data_src       = isa207_get_mem_data_src,
 435        .get_mem_weight         = isa207_get_mem_weight,
 436        .disable_pmc            = isa207_disable_pmc,
 437        .flags                  = PPMU_HAS_SIER | PPMU_ARCH_207S,
 438        .n_generic              = ARRAY_SIZE(power9_generic_events),
 439        .generic_events         = power9_generic_events,
 440        .cache_events           = &power9_cache_events,
 441        .attr_groups            = power9_pmu_attr_groups,
 442        .bhrb_nr                = 32,
 443        .capabilities           = PERF_PMU_CAP_EXTENDED_REGS,
 444};
 445
 446int init_power9_pmu(void)
 447{
 448        int rc = 0;
 449        unsigned int pvr = mfspr(SPRN_PVR);
 450
 451        /* Comes from cpu_specs[] */
 452        if (!cur_cpu_spec->oprofile_cpu_type ||
 453            strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power9"))
 454                return -ENODEV;
 455
 456        /* Blacklist events */
 457        if (!(pvr & PVR_POWER9_CUMULUS)) {
 458                if ((PVR_CFG(pvr) == 2) && (PVR_MIN(pvr) == 1)) {
 459                        power9_pmu.blacklist_ev = p9_dd21_bl_ev;
 460                        power9_pmu.n_blacklist_ev = ARRAY_SIZE(p9_dd21_bl_ev);
 461                } else if ((PVR_CFG(pvr) == 2) && (PVR_MIN(pvr) == 2)) {
 462                        power9_pmu.blacklist_ev = p9_dd22_bl_ev;
 463                        power9_pmu.n_blacklist_ev = ARRAY_SIZE(p9_dd22_bl_ev);
 464                }
 465        }
 466
 467        /* Set the PERF_REG_EXTENDED_MASK here */
 468        PERF_REG_EXTENDED_MASK = PERF_REG_PMU_MASK_300;
 469
 470        rc = register_power_pmu(&power9_pmu);
 471        if (rc)
 472                return rc;
 473
 474        /* Tell userspace that EBB is supported */
 475        cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_EBB;
 476
 477        return 0;
 478}
 479