18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Performance counter support for POWER9 processors.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2009 Paul Mackerras, IBM Corporation.
68c2ecf20Sopenharmony_ci * Copyright 2013 Michael Ellerman, IBM Corporation.
78c2ecf20Sopenharmony_ci * Copyright 2016 Madhavan Srinivasan, IBM Corporation.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#define pr_fmt(fmt)	"power9-pmu: " fmt
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "isa207-common.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * Raw event encoding for Power9:
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci *        60        56        52        48        44        40        36        32
188c2ecf20Sopenharmony_ci * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
198c2ecf20Sopenharmony_ci *   | | [ ]                       [ ] [      thresh_cmp     ]   [  thresh_ctl   ]
208c2ecf20Sopenharmony_ci *   | |  |                         |                                     |
218c2ecf20Sopenharmony_ci *   | |  *- IFM (Linux)            |	               thresh start/stop -*
228c2ecf20Sopenharmony_ci *   | *- BHRB (Linux)              *sm
238c2ecf20Sopenharmony_ci *   *- EBB (Linux)
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci *        28        24        20        16        12         8         4         0
268c2ecf20Sopenharmony_ci * | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - | - - - - |
278c2ecf20Sopenharmony_ci *   [   ] [  sample ]   [cache]   [ pmc ]   [unit ]   []    m   [    pmcxsel    ]
288c2ecf20Sopenharmony_ci *     |        |           |                          |     |
298c2ecf20Sopenharmony_ci *     |        |           |                          |     *- mark
308c2ecf20Sopenharmony_ci *     |        |           *- L1/L2/L3 cache_sel      |
318c2ecf20Sopenharmony_ci *     |        |                                      |
328c2ecf20Sopenharmony_ci *     |        *- sampling mode for marked events     *- combine
338c2ecf20Sopenharmony_ci *     |
348c2ecf20Sopenharmony_ci *     *- thresh_sel
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci * Below uses IBM bit numbering.
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * MMCR1[x:y] = unit    (PMCxUNIT)
398c2ecf20Sopenharmony_ci * MMCR1[24]   = pmc1combine[0]
408c2ecf20Sopenharmony_ci * MMCR1[25]   = pmc1combine[1]
418c2ecf20Sopenharmony_ci * MMCR1[26]   = pmc2combine[0]
428c2ecf20Sopenharmony_ci * MMCR1[27]   = pmc2combine[1]
438c2ecf20Sopenharmony_ci * MMCR1[28]   = pmc3combine[0]
448c2ecf20Sopenharmony_ci * MMCR1[29]   = pmc3combine[1]
458c2ecf20Sopenharmony_ci * MMCR1[30]   = pmc4combine[0]
468c2ecf20Sopenharmony_ci * MMCR1[31]   = pmc4combine[1]
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * if pmc == 3 and unit == 0 and pmcxsel[0:6] == 0b0101011
498c2ecf20Sopenharmony_ci *	MMCR1[20:27] = thresh_ctl
508c2ecf20Sopenharmony_ci * else if pmc == 4 and unit == 0xf and pmcxsel[0:6] == 0b0101001
518c2ecf20Sopenharmony_ci *	MMCR1[20:27] = thresh_ctl
528c2ecf20Sopenharmony_ci * else
538c2ecf20Sopenharmony_ci *	MMCRA[48:55] = thresh_ctl   (THRESH START/END)
548c2ecf20Sopenharmony_ci *
558c2ecf20Sopenharmony_ci * if thresh_sel:
568c2ecf20Sopenharmony_ci *	MMCRA[45:47] = thresh_sel
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * if thresh_cmp:
598c2ecf20Sopenharmony_ci *	MMCRA[9:11] = thresh_cmp[0:2]
608c2ecf20Sopenharmony_ci *	MMCRA[12:18] = thresh_cmp[3:9]
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * MMCR1[16] = cache_sel[2]
638c2ecf20Sopenharmony_ci * MMCR1[17] = cache_sel[3]
648c2ecf20Sopenharmony_ci *
658c2ecf20Sopenharmony_ci * if mark:
668c2ecf20Sopenharmony_ci *	MMCRA[63]    = 1		(SAMPLE_ENABLE)
678c2ecf20Sopenharmony_ci *	MMCRA[57:59] = sample[0:2]	(RAND_SAMP_ELIG)
688c2ecf20Sopenharmony_ci *	MMCRA[61:62] = sample[3:4]	(RAND_SAMP_MODE)
698c2ecf20Sopenharmony_ci *
708c2ecf20Sopenharmony_ci * if EBB and BHRB:
718c2ecf20Sopenharmony_ci *	MMCRA[32:33] = IFM
728c2ecf20Sopenharmony_ci *
738c2ecf20Sopenharmony_ci * MMCRA[SDAR_MODE]  = sm
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/*
778c2ecf20Sopenharmony_ci * Some power9 event codes.
788c2ecf20Sopenharmony_ci */
798c2ecf20Sopenharmony_ci#define EVENT(_name, _code)	_name = _code,
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cienum {
828c2ecf20Sopenharmony_ci#include "power9-events-list.h"
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#undef EVENT
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/* MMCRA IFM bits - POWER9 */
888c2ecf20Sopenharmony_ci#define POWER9_MMCRA_IFM1		0x0000000040000000UL
898c2ecf20Sopenharmony_ci#define POWER9_MMCRA_IFM2		0x0000000080000000UL
908c2ecf20Sopenharmony_ci#define POWER9_MMCRA_IFM3		0x00000000C0000000UL
918c2ecf20Sopenharmony_ci#define POWER9_MMCRA_BHRB_MASK		0x00000000C0000000UL
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ciextern u64 PERF_REG_EXTENDED_MASK;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci/* Nasty Power9 specific hack */
968c2ecf20Sopenharmony_ci#define PVR_POWER9_CUMULUS		0x00002000
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/* PowerISA v2.07 format attribute structure*/
998c2ecf20Sopenharmony_ciextern struct attribute_group isa207_pmu_format_group;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciint p9_dd21_bl_ev[] = {
1028c2ecf20Sopenharmony_ci	PM_MRK_ST_DONE_L2,
1038c2ecf20Sopenharmony_ci	PM_RADIX_PWC_L1_HIT,
1048c2ecf20Sopenharmony_ci	PM_FLOP_CMPL,
1058c2ecf20Sopenharmony_ci	PM_MRK_NTF_FIN,
1068c2ecf20Sopenharmony_ci	PM_RADIX_PWC_L2_HIT,
1078c2ecf20Sopenharmony_ci	PM_IFETCH_THROTTLE,
1088c2ecf20Sopenharmony_ci	PM_MRK_L2_TM_ST_ABORT_SISTER,
1098c2ecf20Sopenharmony_ci	PM_RADIX_PWC_L3_HIT,
1108c2ecf20Sopenharmony_ci	PM_RUN_CYC_SMT2_MODE,
1118c2ecf20Sopenharmony_ci	PM_TM_TX_PASS_RUN_INST,
1128c2ecf20Sopenharmony_ci	PM_DISP_HELD_SYNC_HOLD,
1138c2ecf20Sopenharmony_ci};
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciint p9_dd22_bl_ev[] = {
1168c2ecf20Sopenharmony_ci	PM_DTLB_MISS_16G,
1178c2ecf20Sopenharmony_ci	PM_DERAT_MISS_2M,
1188c2ecf20Sopenharmony_ci	PM_DTLB_MISS_2M,
1198c2ecf20Sopenharmony_ci	PM_MRK_DTLB_MISS_1G,
1208c2ecf20Sopenharmony_ci	PM_DTLB_MISS_4K,
1218c2ecf20Sopenharmony_ci	PM_DERAT_MISS_1G,
1228c2ecf20Sopenharmony_ci	PM_MRK_DERAT_MISS_2M,
1238c2ecf20Sopenharmony_ci	PM_MRK_DTLB_MISS_4K,
1248c2ecf20Sopenharmony_ci	PM_MRK_DTLB_MISS_16G,
1258c2ecf20Sopenharmony_ci	PM_DTLB_MISS_64K,
1268c2ecf20Sopenharmony_ci	PM_MRK_DERAT_MISS_1G,
1278c2ecf20Sopenharmony_ci	PM_MRK_DTLB_MISS_64K,
1288c2ecf20Sopenharmony_ci	PM_DISP_HELD_SYNC_HOLD,
1298c2ecf20Sopenharmony_ci	PM_DTLB_MISS_16M,
1308c2ecf20Sopenharmony_ci	PM_DTLB_MISS_1G,
1318c2ecf20Sopenharmony_ci	PM_MRK_DTLB_MISS_16M,
1328c2ecf20Sopenharmony_ci};
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/* Table of alternatives, sorted by column 0 */
1358c2ecf20Sopenharmony_cistatic const unsigned int power9_event_alternatives[][MAX_ALT] = {
1368c2ecf20Sopenharmony_ci	{ PM_BR_2PATH,			PM_BR_2PATH_ALT },
1378c2ecf20Sopenharmony_ci	{ PM_INST_DISP,			PM_INST_DISP_ALT },
1388c2ecf20Sopenharmony_ci	{ PM_RUN_CYC_ALT,               PM_RUN_CYC },
1398c2ecf20Sopenharmony_ci	{ PM_LD_MISS_L1,                PM_LD_MISS_L1_ALT },
1408c2ecf20Sopenharmony_ci	{ PM_RUN_INST_CMPL_ALT,         PM_RUN_INST_CMPL },
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic int power9_get_alternatives(u64 event, unsigned int flags, u64 alt[])
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	int num_alt = 0;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	num_alt = isa207_get_alternatives(event, alt,
1488c2ecf20Sopenharmony_ci					  ARRAY_SIZE(power9_event_alternatives), flags,
1498c2ecf20Sopenharmony_ci					  power9_event_alternatives);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	return num_alt;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(cpu-cycles,			PM_CYC);
1558c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(stalled-cycles-frontend,	PM_ICT_NOSLOT_CYC);
1568c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(stalled-cycles-backend,	PM_CMPLU_STALL);
1578c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(instructions,		PM_INST_CMPL);
1588c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(branch-instructions,		PM_BR_CMPL);
1598c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(branch-misses,		PM_BR_MPRED_CMPL);
1608c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(cache-references,		PM_LD_REF_L1);
1618c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(cache-misses,		PM_LD_MISS_L1_FIN);
1628c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(mem-loads,			MEM_LOADS);
1638c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(mem-stores,			MEM_STORES);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(L1-dcache-load-misses,		PM_LD_MISS_L1_FIN);
1668c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(L1-dcache-loads,		PM_LD_REF_L1);
1678c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(L1-dcache-prefetches,		PM_L1_PREF);
1688c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(L1-dcache-store-misses,	PM_ST_MISS_L1);
1698c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(L1-icache-load-misses,		PM_L1_ICACHE_MISS);
1708c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(L1-icache-loads,		PM_INST_FROM_L1);
1718c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(L1-icache-prefetches,		PM_IC_PREF_WRITE);
1728c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(LLC-load-misses,		PM_DATA_FROM_L3MISS);
1738c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(LLC-loads,			PM_DATA_FROM_L3);
1748c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(LLC-prefetches,		PM_L3_PREF_ALL);
1758c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(branch-load-misses,		PM_BR_MPRED_CMPL);
1768c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(branch-loads,			PM_BR_CMPL);
1778c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(dTLB-load-misses,		PM_DTLB_MISS);
1788c2ecf20Sopenharmony_ciCACHE_EVENT_ATTR(iTLB-load-misses,		PM_ITLB_MISS);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic struct attribute *power9_events_attr[] = {
1818c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_CYC),
1828c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_ICT_NOSLOT_CYC),
1838c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_CMPLU_STALL),
1848c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_INST_CMPL),
1858c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_BR_CMPL),
1868c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_BR_MPRED_CMPL),
1878c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_LD_REF_L1),
1888c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_LD_MISS_L1_FIN),
1898c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(MEM_LOADS),
1908c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(MEM_STORES),
1918c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_LD_MISS_L1_FIN),
1928c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_LD_REF_L1),
1938c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_L1_PREF),
1948c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_ST_MISS_L1),
1958c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_L1_ICACHE_MISS),
1968c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_INST_FROM_L1),
1978c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_IC_PREF_WRITE),
1988c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_DATA_FROM_L3MISS),
1998c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_DATA_FROM_L3),
2008c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_L3_PREF_ALL),
2018c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_BR_MPRED_CMPL),
2028c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_BR_CMPL),
2038c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_DTLB_MISS),
2048c2ecf20Sopenharmony_ci	CACHE_EVENT_PTR(PM_ITLB_MISS),
2058c2ecf20Sopenharmony_ci	NULL
2068c2ecf20Sopenharmony_ci};
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic struct attribute_group power9_pmu_events_group = {
2098c2ecf20Sopenharmony_ci	.name = "events",
2108c2ecf20Sopenharmony_ci	.attrs = power9_events_attr,
2118c2ecf20Sopenharmony_ci};
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(event,		"config:0-51");
2148c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(pmcxsel,	"config:0-7");
2158c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(mark,		"config:8");
2168c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(combine,	"config:10-11");
2178c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(unit,		"config:12-15");
2188c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(pmc,		"config:16-19");
2198c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(cache_sel,	"config:20-23");
2208c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(sample_mode,	"config:24-28");
2218c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(thresh_sel,	"config:29-31");
2228c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(thresh_stop,	"config:32-35");
2238c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(thresh_start,	"config:36-39");
2248c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(thresh_cmp,	"config:40-49");
2258c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(sdar_mode,	"config:50-51");
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic struct attribute *power9_pmu_format_attr[] = {
2288c2ecf20Sopenharmony_ci	&format_attr_event.attr,
2298c2ecf20Sopenharmony_ci	&format_attr_pmcxsel.attr,
2308c2ecf20Sopenharmony_ci	&format_attr_mark.attr,
2318c2ecf20Sopenharmony_ci	&format_attr_combine.attr,
2328c2ecf20Sopenharmony_ci	&format_attr_unit.attr,
2338c2ecf20Sopenharmony_ci	&format_attr_pmc.attr,
2348c2ecf20Sopenharmony_ci	&format_attr_cache_sel.attr,
2358c2ecf20Sopenharmony_ci	&format_attr_sample_mode.attr,
2368c2ecf20Sopenharmony_ci	&format_attr_thresh_sel.attr,
2378c2ecf20Sopenharmony_ci	&format_attr_thresh_stop.attr,
2388c2ecf20Sopenharmony_ci	&format_attr_thresh_start.attr,
2398c2ecf20Sopenharmony_ci	&format_attr_thresh_cmp.attr,
2408c2ecf20Sopenharmony_ci	&format_attr_sdar_mode.attr,
2418c2ecf20Sopenharmony_ci	NULL,
2428c2ecf20Sopenharmony_ci};
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic struct attribute_group power9_pmu_format_group = {
2458c2ecf20Sopenharmony_ci	.name = "format",
2468c2ecf20Sopenharmony_ci	.attrs = power9_pmu_format_attr,
2478c2ecf20Sopenharmony_ci};
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic const struct attribute_group *power9_pmu_attr_groups[] = {
2508c2ecf20Sopenharmony_ci	&power9_pmu_format_group,
2518c2ecf20Sopenharmony_ci	&power9_pmu_events_group,
2528c2ecf20Sopenharmony_ci	NULL,
2538c2ecf20Sopenharmony_ci};
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic int power9_generic_events[] = {
2568c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CPU_CYCLES] =			PM_CYC,
2578c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =	PM_ICT_NOSLOT_CYC,
2588c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =	PM_CMPLU_STALL,
2598c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_INSTRUCTIONS] =			PM_INST_CMPL,
2608c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] =		PM_BR_CMPL,
2618c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_MISSES] =			PM_BR_MPRED_CMPL,
2628c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CACHE_REFERENCES] =		PM_LD_REF_L1,
2638c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CACHE_MISSES] =			PM_LD_MISS_L1_FIN,
2648c2ecf20Sopenharmony_ci};
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic u64 power9_bhrb_filter_map(u64 branch_sample_type)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	u64 pmu_bhrb_filter = 0;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	/* BHRB and regular PMU events share the same privilege state
2718c2ecf20Sopenharmony_ci	 * filter configuration. BHRB is always recorded along with a
2728c2ecf20Sopenharmony_ci	 * regular PMU event. As the privilege state filter is handled
2738c2ecf20Sopenharmony_ci	 * in the basic PMC configuration of the accompanying regular
2748c2ecf20Sopenharmony_ci	 * PMU event, we ignore any separate BHRB specific request.
2758c2ecf20Sopenharmony_ci	 */
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/* No branch filter requested */
2788c2ecf20Sopenharmony_ci	if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY)
2798c2ecf20Sopenharmony_ci		return pmu_bhrb_filter;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	/* Invalid branch filter options - HW does not support */
2828c2ecf20Sopenharmony_ci	if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_RETURN)
2838c2ecf20Sopenharmony_ci		return -1;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	if (branch_sample_type & PERF_SAMPLE_BRANCH_IND_CALL)
2868c2ecf20Sopenharmony_ci		return -1;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	if (branch_sample_type & PERF_SAMPLE_BRANCH_CALL)
2898c2ecf20Sopenharmony_ci		return -1;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	if (branch_sample_type & PERF_SAMPLE_BRANCH_ANY_CALL) {
2928c2ecf20Sopenharmony_ci		pmu_bhrb_filter |= POWER9_MMCRA_IFM1;
2938c2ecf20Sopenharmony_ci		return pmu_bhrb_filter;
2948c2ecf20Sopenharmony_ci	}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	/* Every thing else is unsupported */
2978c2ecf20Sopenharmony_ci	return -1;
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic void power9_config_bhrb(u64 pmu_bhrb_filter)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	pmu_bhrb_filter &= POWER9_MMCRA_BHRB_MASK;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	/* Enable BHRB filter in PMU */
3058c2ecf20Sopenharmony_ci	mtspr(SPRN_MMCRA, (mfspr(SPRN_MMCRA) | pmu_bhrb_filter));
3068c2ecf20Sopenharmony_ci}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci#define C(x)	PERF_COUNT_HW_CACHE_##x
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci/*
3118c2ecf20Sopenharmony_ci * Table of generalized cache-related events.
3128c2ecf20Sopenharmony_ci * 0 means not supported, -1 means nonsensical, other values
3138c2ecf20Sopenharmony_ci * are event codes.
3148c2ecf20Sopenharmony_ci */
3158c2ecf20Sopenharmony_cistatic u64 power9_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
3168c2ecf20Sopenharmony_ci	[ C(L1D) ] = {
3178c2ecf20Sopenharmony_ci		[ C(OP_READ) ] = {
3188c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = PM_LD_REF_L1,
3198c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = PM_LD_MISS_L1_FIN,
3208c2ecf20Sopenharmony_ci		},
3218c2ecf20Sopenharmony_ci		[ C(OP_WRITE) ] = {
3228c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = 0,
3238c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = PM_ST_MISS_L1,
3248c2ecf20Sopenharmony_ci		},
3258c2ecf20Sopenharmony_ci		[ C(OP_PREFETCH) ] = {
3268c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = PM_L1_PREF,
3278c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = 0,
3288c2ecf20Sopenharmony_ci		},
3298c2ecf20Sopenharmony_ci	},
3308c2ecf20Sopenharmony_ci	[ C(L1I) ] = {
3318c2ecf20Sopenharmony_ci		[ C(OP_READ) ] = {
3328c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = PM_INST_FROM_L1,
3338c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = PM_L1_ICACHE_MISS,
3348c2ecf20Sopenharmony_ci		},
3358c2ecf20Sopenharmony_ci		[ C(OP_WRITE) ] = {
3368c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = PM_L1_DEMAND_WRITE,
3378c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = -1,
3388c2ecf20Sopenharmony_ci		},
3398c2ecf20Sopenharmony_ci		[ C(OP_PREFETCH) ] = {
3408c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = PM_IC_PREF_WRITE,
3418c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = 0,
3428c2ecf20Sopenharmony_ci		},
3438c2ecf20Sopenharmony_ci	},
3448c2ecf20Sopenharmony_ci	[ C(LL) ] = {
3458c2ecf20Sopenharmony_ci		[ C(OP_READ) ] = {
3468c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = PM_DATA_FROM_L3,
3478c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = PM_DATA_FROM_L3MISS,
3488c2ecf20Sopenharmony_ci		},
3498c2ecf20Sopenharmony_ci		[ C(OP_WRITE) ] = {
3508c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = 0,
3518c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = 0,
3528c2ecf20Sopenharmony_ci		},
3538c2ecf20Sopenharmony_ci		[ C(OP_PREFETCH) ] = {
3548c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = PM_L3_PREF_ALL,
3558c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = 0,
3568c2ecf20Sopenharmony_ci		},
3578c2ecf20Sopenharmony_ci	},
3588c2ecf20Sopenharmony_ci	[ C(DTLB) ] = {
3598c2ecf20Sopenharmony_ci		[ C(OP_READ) ] = {
3608c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = 0,
3618c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = PM_DTLB_MISS,
3628c2ecf20Sopenharmony_ci		},
3638c2ecf20Sopenharmony_ci		[ C(OP_WRITE) ] = {
3648c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = -1,
3658c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = -1,
3668c2ecf20Sopenharmony_ci		},
3678c2ecf20Sopenharmony_ci		[ C(OP_PREFETCH) ] = {
3688c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = -1,
3698c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = -1,
3708c2ecf20Sopenharmony_ci		},
3718c2ecf20Sopenharmony_ci	},
3728c2ecf20Sopenharmony_ci	[ C(ITLB) ] = {
3738c2ecf20Sopenharmony_ci		[ C(OP_READ) ] = {
3748c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = 0,
3758c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = PM_ITLB_MISS,
3768c2ecf20Sopenharmony_ci		},
3778c2ecf20Sopenharmony_ci		[ C(OP_WRITE) ] = {
3788c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = -1,
3798c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = -1,
3808c2ecf20Sopenharmony_ci		},
3818c2ecf20Sopenharmony_ci		[ C(OP_PREFETCH) ] = {
3828c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = -1,
3838c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = -1,
3848c2ecf20Sopenharmony_ci		},
3858c2ecf20Sopenharmony_ci	},
3868c2ecf20Sopenharmony_ci	[ C(BPU) ] = {
3878c2ecf20Sopenharmony_ci		[ C(OP_READ) ] = {
3888c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = PM_BR_CMPL,
3898c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = PM_BR_MPRED_CMPL,
3908c2ecf20Sopenharmony_ci		},
3918c2ecf20Sopenharmony_ci		[ C(OP_WRITE) ] = {
3928c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = -1,
3938c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = -1,
3948c2ecf20Sopenharmony_ci		},
3958c2ecf20Sopenharmony_ci		[ C(OP_PREFETCH) ] = {
3968c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = -1,
3978c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = -1,
3988c2ecf20Sopenharmony_ci		},
3998c2ecf20Sopenharmony_ci	},
4008c2ecf20Sopenharmony_ci	[ C(NODE) ] = {
4018c2ecf20Sopenharmony_ci		[ C(OP_READ) ] = {
4028c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = -1,
4038c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = -1,
4048c2ecf20Sopenharmony_ci		},
4058c2ecf20Sopenharmony_ci		[ C(OP_WRITE) ] = {
4068c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = -1,
4078c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = -1,
4088c2ecf20Sopenharmony_ci		},
4098c2ecf20Sopenharmony_ci		[ C(OP_PREFETCH) ] = {
4108c2ecf20Sopenharmony_ci			[ C(RESULT_ACCESS) ] = -1,
4118c2ecf20Sopenharmony_ci			[ C(RESULT_MISS)   ] = -1,
4128c2ecf20Sopenharmony_ci		},
4138c2ecf20Sopenharmony_ci	},
4148c2ecf20Sopenharmony_ci};
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci#undef C
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_cistatic struct power_pmu power9_pmu = {
4198c2ecf20Sopenharmony_ci	.name			= "POWER9",
4208c2ecf20Sopenharmony_ci	.n_counter		= MAX_PMU_COUNTERS,
4218c2ecf20Sopenharmony_ci	.add_fields		= ISA207_ADD_FIELDS,
4228c2ecf20Sopenharmony_ci	.test_adder		= ISA207_TEST_ADDER,
4238c2ecf20Sopenharmony_ci	.group_constraint_mask	= CNST_CACHE_PMC4_MASK,
4248c2ecf20Sopenharmony_ci	.group_constraint_val	= CNST_CACHE_PMC4_VAL,
4258c2ecf20Sopenharmony_ci	.compute_mmcr		= isa207_compute_mmcr,
4268c2ecf20Sopenharmony_ci	.config_bhrb		= power9_config_bhrb,
4278c2ecf20Sopenharmony_ci	.bhrb_filter_map	= power9_bhrb_filter_map,
4288c2ecf20Sopenharmony_ci	.get_constraint		= isa207_get_constraint,
4298c2ecf20Sopenharmony_ci	.get_alternatives	= power9_get_alternatives,
4308c2ecf20Sopenharmony_ci	.get_mem_data_src	= isa207_get_mem_data_src,
4318c2ecf20Sopenharmony_ci	.get_mem_weight		= isa207_get_mem_weight,
4328c2ecf20Sopenharmony_ci	.disable_pmc		= isa207_disable_pmc,
4338c2ecf20Sopenharmony_ci	.flags			= PPMU_HAS_SIER | PPMU_ARCH_207S,
4348c2ecf20Sopenharmony_ci	.n_generic		= ARRAY_SIZE(power9_generic_events),
4358c2ecf20Sopenharmony_ci	.generic_events		= power9_generic_events,
4368c2ecf20Sopenharmony_ci	.cache_events		= &power9_cache_events,
4378c2ecf20Sopenharmony_ci	.attr_groups		= power9_pmu_attr_groups,
4388c2ecf20Sopenharmony_ci	.bhrb_nr		= 32,
4398c2ecf20Sopenharmony_ci	.capabilities           = PERF_PMU_CAP_EXTENDED_REGS,
4408c2ecf20Sopenharmony_ci};
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ciint init_power9_pmu(void)
4438c2ecf20Sopenharmony_ci{
4448c2ecf20Sopenharmony_ci	int rc = 0;
4458c2ecf20Sopenharmony_ci	unsigned int pvr = mfspr(SPRN_PVR);
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	/* Comes from cpu_specs[] */
4488c2ecf20Sopenharmony_ci	if (!cur_cpu_spec->oprofile_cpu_type ||
4498c2ecf20Sopenharmony_ci	    strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power9"))
4508c2ecf20Sopenharmony_ci		return -ENODEV;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	/* Blacklist events */
4538c2ecf20Sopenharmony_ci	if (!(pvr & PVR_POWER9_CUMULUS)) {
4548c2ecf20Sopenharmony_ci		if ((PVR_CFG(pvr) == 2) && (PVR_MIN(pvr) == 1)) {
4558c2ecf20Sopenharmony_ci			power9_pmu.blacklist_ev = p9_dd21_bl_ev;
4568c2ecf20Sopenharmony_ci			power9_pmu.n_blacklist_ev = ARRAY_SIZE(p9_dd21_bl_ev);
4578c2ecf20Sopenharmony_ci		} else if ((PVR_CFG(pvr) == 2) && (PVR_MIN(pvr) == 2)) {
4588c2ecf20Sopenharmony_ci			power9_pmu.blacklist_ev = p9_dd22_bl_ev;
4598c2ecf20Sopenharmony_ci			power9_pmu.n_blacklist_ev = ARRAY_SIZE(p9_dd22_bl_ev);
4608c2ecf20Sopenharmony_ci		}
4618c2ecf20Sopenharmony_ci	}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	/* Set the PERF_REG_EXTENDED_MASK here */
4648c2ecf20Sopenharmony_ci	PERF_REG_EXTENDED_MASK = PERF_REG_PMU_MASK_300;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	rc = register_power_pmu(&power9_pmu);
4678c2ecf20Sopenharmony_ci	if (rc)
4688c2ecf20Sopenharmony_ci		return rc;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	/* Tell userspace that EBB is supported */
4718c2ecf20Sopenharmony_ci	cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_EBB;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	return 0;
4748c2ecf20Sopenharmony_ci}
475