18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Performance counter support for POWER7 processors.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2009 Paul Mackerras, IBM Corporation.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/perf_event.h>
98c2ecf20Sopenharmony_ci#include <linux/string.h>
108c2ecf20Sopenharmony_ci#include <asm/reg.h>
118c2ecf20Sopenharmony_ci#include <asm/cputable.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include "internal.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * Bits in event code for POWER7
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci#define PM_PMC_SH	16	/* PMC number (1-based) for direct events */
198c2ecf20Sopenharmony_ci#define PM_PMC_MSK	0xf
208c2ecf20Sopenharmony_ci#define PM_PMC_MSKS	(PM_PMC_MSK << PM_PMC_SH)
218c2ecf20Sopenharmony_ci#define PM_UNIT_SH	12	/* TTMMUX number and setting - unit select */
228c2ecf20Sopenharmony_ci#define PM_UNIT_MSK	0xf
238c2ecf20Sopenharmony_ci#define PM_COMBINE_SH	11	/* Combined event bit */
248c2ecf20Sopenharmony_ci#define PM_COMBINE_MSK	1
258c2ecf20Sopenharmony_ci#define PM_COMBINE_MSKS	0x800
268c2ecf20Sopenharmony_ci#define PM_L2SEL_SH	8	/* L2 event select */
278c2ecf20Sopenharmony_ci#define PM_L2SEL_MSK	7
288c2ecf20Sopenharmony_ci#define PM_PMCSEL_MSK	0xff
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * Bits in MMCR1 for POWER7
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci#define MMCR1_TTM0SEL_SH	60
348c2ecf20Sopenharmony_ci#define MMCR1_TTM1SEL_SH	56
358c2ecf20Sopenharmony_ci#define MMCR1_TTM2SEL_SH	52
368c2ecf20Sopenharmony_ci#define MMCR1_TTM3SEL_SH	48
378c2ecf20Sopenharmony_ci#define MMCR1_TTMSEL_MSK	0xf
388c2ecf20Sopenharmony_ci#define MMCR1_L2SEL_SH		45
398c2ecf20Sopenharmony_ci#define MMCR1_L2SEL_MSK		7
408c2ecf20Sopenharmony_ci#define MMCR1_PMC1_COMBINE_SH	35
418c2ecf20Sopenharmony_ci#define MMCR1_PMC2_COMBINE_SH	34
428c2ecf20Sopenharmony_ci#define MMCR1_PMC3_COMBINE_SH	33
438c2ecf20Sopenharmony_ci#define MMCR1_PMC4_COMBINE_SH	32
448c2ecf20Sopenharmony_ci#define MMCR1_PMC1SEL_SH	24
458c2ecf20Sopenharmony_ci#define MMCR1_PMC2SEL_SH	16
468c2ecf20Sopenharmony_ci#define MMCR1_PMC3SEL_SH	8
478c2ecf20Sopenharmony_ci#define MMCR1_PMC4SEL_SH	0
488c2ecf20Sopenharmony_ci#define MMCR1_PMCSEL_SH(n)	(MMCR1_PMC1SEL_SH - (n) * 8)
498c2ecf20Sopenharmony_ci#define MMCR1_PMCSEL_MSK	0xff
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/*
528c2ecf20Sopenharmony_ci * Power7 event codes.
538c2ecf20Sopenharmony_ci */
548c2ecf20Sopenharmony_ci#define EVENT(_name, _code) \
558c2ecf20Sopenharmony_ci	_name = _code,
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cienum {
588c2ecf20Sopenharmony_ci#include "power7-events-list.h"
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci#undef EVENT
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/*
638c2ecf20Sopenharmony_ci * Layout of constraint bits:
648c2ecf20Sopenharmony_ci * 6666555555555544444444443333333333222222222211111111110000000000
658c2ecf20Sopenharmony_ci * 3210987654321098765432109876543210987654321098765432109876543210
668c2ecf20Sopenharmony_ci *                                              < ><  ><><><><><><>
678c2ecf20Sopenharmony_ci *                                              L2  NC P6P5P4P3P2P1
688c2ecf20Sopenharmony_ci *
698c2ecf20Sopenharmony_ci * L2 - 16-18 - Required L2SEL value (select field)
708c2ecf20Sopenharmony_ci *
718c2ecf20Sopenharmony_ci * NC - number of counters
728c2ecf20Sopenharmony_ci *     15: NC error 0x8000
738c2ecf20Sopenharmony_ci *     12-14: number of events needing PMC1-4 0x7000
748c2ecf20Sopenharmony_ci *
758c2ecf20Sopenharmony_ci * P6
768c2ecf20Sopenharmony_ci *     11: P6 error 0x800
778c2ecf20Sopenharmony_ci *     10-11: Count of events needing PMC6
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci * P1..P5
808c2ecf20Sopenharmony_ci *     0-9: Count of events needing PMC1..PMC5
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic int power7_get_constraint(u64 event, unsigned long *maskp,
848c2ecf20Sopenharmony_ci				 unsigned long *valp)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	int pmc, sh, unit;
878c2ecf20Sopenharmony_ci	unsigned long mask = 0, value = 0;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
908c2ecf20Sopenharmony_ci	if (pmc) {
918c2ecf20Sopenharmony_ci		if (pmc > 6)
928c2ecf20Sopenharmony_ci			return -1;
938c2ecf20Sopenharmony_ci		sh = (pmc - 1) * 2;
948c2ecf20Sopenharmony_ci		mask |= 2 << sh;
958c2ecf20Sopenharmony_ci		value |= 1 << sh;
968c2ecf20Sopenharmony_ci		if (pmc >= 5 && !(event == 0x500fa || event == 0x600f4))
978c2ecf20Sopenharmony_ci			return -1;
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci	if (pmc < 5) {
1008c2ecf20Sopenharmony_ci		/* need a counter from PMC1-4 set */
1018c2ecf20Sopenharmony_ci		mask  |= 0x8000;
1028c2ecf20Sopenharmony_ci		value |= 0x1000;
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
1068c2ecf20Sopenharmony_ci	if (unit == 6) {
1078c2ecf20Sopenharmony_ci		/* L2SEL must be identical across events */
1088c2ecf20Sopenharmony_ci		int l2sel = (event >> PM_L2SEL_SH) & PM_L2SEL_MSK;
1098c2ecf20Sopenharmony_ci		mask  |= 0x7 << 16;
1108c2ecf20Sopenharmony_ci		value |= l2sel << 16;
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	*maskp = mask;
1148c2ecf20Sopenharmony_ci	*valp = value;
1158c2ecf20Sopenharmony_ci	return 0;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci#define MAX_ALT	2	/* at most 2 alternatives for any event */
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic const unsigned int event_alternatives[][MAX_ALT] = {
1218c2ecf20Sopenharmony_ci	{ 0x200f2, 0x300f2 },		/* PM_INST_DISP */
1228c2ecf20Sopenharmony_ci	{ 0x200f4, 0x600f4 },		/* PM_RUN_CYC */
1238c2ecf20Sopenharmony_ci	{ 0x400fa, 0x500fa },		/* PM_RUN_INST_CMPL */
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci/*
1278c2ecf20Sopenharmony_ci * Scan the alternatives table for a match and return the
1288c2ecf20Sopenharmony_ci * index into the alternatives table if found, else -1.
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_cistatic int find_alternative(u64 event)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	int i, j;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
1358c2ecf20Sopenharmony_ci		if (event < event_alternatives[i][0])
1368c2ecf20Sopenharmony_ci			break;
1378c2ecf20Sopenharmony_ci		for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
1388c2ecf20Sopenharmony_ci			if (event == event_alternatives[i][j])
1398c2ecf20Sopenharmony_ci				return i;
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci	return -1;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic s64 find_alternative_decode(u64 event)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	int pmc, psel;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	/* this only handles the 4x decode events */
1498c2ecf20Sopenharmony_ci	pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
1508c2ecf20Sopenharmony_ci	psel = event & PM_PMCSEL_MSK;
1518c2ecf20Sopenharmony_ci	if ((pmc == 2 || pmc == 4) && (psel & ~7) == 0x40)
1528c2ecf20Sopenharmony_ci		return event - (1 << PM_PMC_SH) + 8;
1538c2ecf20Sopenharmony_ci	if ((pmc == 1 || pmc == 3) && (psel & ~7) == 0x48)
1548c2ecf20Sopenharmony_ci		return event + (1 << PM_PMC_SH) - 8;
1558c2ecf20Sopenharmony_ci	return -1;
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic int power7_get_alternatives(u64 event, unsigned int flags, u64 alt[])
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	int i, j, nalt = 1;
1618c2ecf20Sopenharmony_ci	s64 ae;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	alt[0] = event;
1648c2ecf20Sopenharmony_ci	nalt = 1;
1658c2ecf20Sopenharmony_ci	i = find_alternative(event);
1668c2ecf20Sopenharmony_ci	if (i >= 0) {
1678c2ecf20Sopenharmony_ci		for (j = 0; j < MAX_ALT; ++j) {
1688c2ecf20Sopenharmony_ci			ae = event_alternatives[i][j];
1698c2ecf20Sopenharmony_ci			if (ae && ae != event)
1708c2ecf20Sopenharmony_ci				alt[nalt++] = ae;
1718c2ecf20Sopenharmony_ci		}
1728c2ecf20Sopenharmony_ci	} else {
1738c2ecf20Sopenharmony_ci		ae = find_alternative_decode(event);
1748c2ecf20Sopenharmony_ci		if (ae > 0)
1758c2ecf20Sopenharmony_ci			alt[nalt++] = ae;
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	if (flags & PPMU_ONLY_COUNT_RUN) {
1798c2ecf20Sopenharmony_ci		/*
1808c2ecf20Sopenharmony_ci		 * We're only counting in RUN state,
1818c2ecf20Sopenharmony_ci		 * so PM_CYC is equivalent to PM_RUN_CYC
1828c2ecf20Sopenharmony_ci		 * and PM_INST_CMPL === PM_RUN_INST_CMPL.
1838c2ecf20Sopenharmony_ci		 * This doesn't include alternatives that don't provide
1848c2ecf20Sopenharmony_ci		 * any extra flexibility in assigning PMCs.
1858c2ecf20Sopenharmony_ci		 */
1868c2ecf20Sopenharmony_ci		j = nalt;
1878c2ecf20Sopenharmony_ci		for (i = 0; i < nalt; ++i) {
1888c2ecf20Sopenharmony_ci			switch (alt[i]) {
1898c2ecf20Sopenharmony_ci			case 0x1e:	/* PM_CYC */
1908c2ecf20Sopenharmony_ci				alt[j++] = 0x600f4;	/* PM_RUN_CYC */
1918c2ecf20Sopenharmony_ci				break;
1928c2ecf20Sopenharmony_ci			case 0x600f4:	/* PM_RUN_CYC */
1938c2ecf20Sopenharmony_ci				alt[j++] = 0x1e;
1948c2ecf20Sopenharmony_ci				break;
1958c2ecf20Sopenharmony_ci			case 0x2:	/* PM_PPC_CMPL */
1968c2ecf20Sopenharmony_ci				alt[j++] = 0x500fa;	/* PM_RUN_INST_CMPL */
1978c2ecf20Sopenharmony_ci				break;
1988c2ecf20Sopenharmony_ci			case 0x500fa:	/* PM_RUN_INST_CMPL */
1998c2ecf20Sopenharmony_ci				alt[j++] = 0x2;	/* PM_PPC_CMPL */
2008c2ecf20Sopenharmony_ci				break;
2018c2ecf20Sopenharmony_ci			}
2028c2ecf20Sopenharmony_ci		}
2038c2ecf20Sopenharmony_ci		nalt = j;
2048c2ecf20Sopenharmony_ci	}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	return nalt;
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci/*
2108c2ecf20Sopenharmony_ci * Returns 1 if event counts things relating to marked instructions
2118c2ecf20Sopenharmony_ci * and thus needs the MMCRA_SAMPLE_ENABLE bit set, or 0 if not.
2128c2ecf20Sopenharmony_ci */
2138c2ecf20Sopenharmony_cistatic int power7_marked_instr_event(u64 event)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	int pmc, psel;
2168c2ecf20Sopenharmony_ci	int unit;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
2198c2ecf20Sopenharmony_ci	unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
2208c2ecf20Sopenharmony_ci	psel = event & PM_PMCSEL_MSK & ~1;	/* trim off edge/level bit */
2218c2ecf20Sopenharmony_ci	if (pmc >= 5)
2228c2ecf20Sopenharmony_ci		return 0;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	switch (psel >> 4) {
2258c2ecf20Sopenharmony_ci	case 2:
2268c2ecf20Sopenharmony_ci		return pmc == 2 || pmc == 4;
2278c2ecf20Sopenharmony_ci	case 3:
2288c2ecf20Sopenharmony_ci		if (psel == 0x3c)
2298c2ecf20Sopenharmony_ci			return pmc == 1;
2308c2ecf20Sopenharmony_ci		if (psel == 0x3e)
2318c2ecf20Sopenharmony_ci			return pmc != 2;
2328c2ecf20Sopenharmony_ci		return 1;
2338c2ecf20Sopenharmony_ci	case 4:
2348c2ecf20Sopenharmony_ci	case 5:
2358c2ecf20Sopenharmony_ci		return unit == 0xd;
2368c2ecf20Sopenharmony_ci	case 6:
2378c2ecf20Sopenharmony_ci		if (psel == 0x64)
2388c2ecf20Sopenharmony_ci			return pmc >= 3;
2398c2ecf20Sopenharmony_ci		break;
2408c2ecf20Sopenharmony_ci	case 8:
2418c2ecf20Sopenharmony_ci		return unit == 0xd;
2428c2ecf20Sopenharmony_ci	}
2438c2ecf20Sopenharmony_ci	return 0;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic int power7_compute_mmcr(u64 event[], int n_ev,
2478c2ecf20Sopenharmony_ci			       unsigned int hwc[], struct mmcr_regs *mmcr,
2488c2ecf20Sopenharmony_ci			       struct perf_event *pevents[])
2498c2ecf20Sopenharmony_ci{
2508c2ecf20Sopenharmony_ci	unsigned long mmcr1 = 0;
2518c2ecf20Sopenharmony_ci	unsigned long mmcra = MMCRA_SDAR_DCACHE_MISS | MMCRA_SDAR_ERAT_MISS;
2528c2ecf20Sopenharmony_ci	unsigned int pmc, unit, combine, l2sel, psel;
2538c2ecf20Sopenharmony_ci	unsigned int pmc_inuse = 0;
2548c2ecf20Sopenharmony_ci	int i;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	/* First pass to count resource use */
2578c2ecf20Sopenharmony_ci	for (i = 0; i < n_ev; ++i) {
2588c2ecf20Sopenharmony_ci		pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
2598c2ecf20Sopenharmony_ci		if (pmc) {
2608c2ecf20Sopenharmony_ci			if (pmc > 6)
2618c2ecf20Sopenharmony_ci				return -1;
2628c2ecf20Sopenharmony_ci			if (pmc_inuse & (1 << (pmc - 1)))
2638c2ecf20Sopenharmony_ci				return -1;
2648c2ecf20Sopenharmony_ci			pmc_inuse |= 1 << (pmc - 1);
2658c2ecf20Sopenharmony_ci		}
2668c2ecf20Sopenharmony_ci	}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	/* Second pass: assign PMCs, set all MMCR1 fields */
2698c2ecf20Sopenharmony_ci	for (i = 0; i < n_ev; ++i) {
2708c2ecf20Sopenharmony_ci		pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
2718c2ecf20Sopenharmony_ci		unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
2728c2ecf20Sopenharmony_ci		combine = (event[i] >> PM_COMBINE_SH) & PM_COMBINE_MSK;
2738c2ecf20Sopenharmony_ci		l2sel = (event[i] >> PM_L2SEL_SH) & PM_L2SEL_MSK;
2748c2ecf20Sopenharmony_ci		psel = event[i] & PM_PMCSEL_MSK;
2758c2ecf20Sopenharmony_ci		if (!pmc) {
2768c2ecf20Sopenharmony_ci			/* Bus event or any-PMC direct event */
2778c2ecf20Sopenharmony_ci			for (pmc = 0; pmc < 4; ++pmc) {
2788c2ecf20Sopenharmony_ci				if (!(pmc_inuse & (1 << pmc)))
2798c2ecf20Sopenharmony_ci					break;
2808c2ecf20Sopenharmony_ci			}
2818c2ecf20Sopenharmony_ci			if (pmc >= 4)
2828c2ecf20Sopenharmony_ci				return -1;
2838c2ecf20Sopenharmony_ci			pmc_inuse |= 1 << pmc;
2848c2ecf20Sopenharmony_ci		} else {
2858c2ecf20Sopenharmony_ci			/* Direct or decoded event */
2868c2ecf20Sopenharmony_ci			--pmc;
2878c2ecf20Sopenharmony_ci		}
2888c2ecf20Sopenharmony_ci		if (pmc <= 3) {
2898c2ecf20Sopenharmony_ci			mmcr1 |= (unsigned long) unit
2908c2ecf20Sopenharmony_ci				<< (MMCR1_TTM0SEL_SH - 4 * pmc);
2918c2ecf20Sopenharmony_ci			mmcr1 |= (unsigned long) combine
2928c2ecf20Sopenharmony_ci				<< (MMCR1_PMC1_COMBINE_SH - pmc);
2938c2ecf20Sopenharmony_ci			mmcr1 |= psel << MMCR1_PMCSEL_SH(pmc);
2948c2ecf20Sopenharmony_ci			if (unit == 6)	/* L2 events */
2958c2ecf20Sopenharmony_ci				mmcr1 |= (unsigned long) l2sel
2968c2ecf20Sopenharmony_ci					<< MMCR1_L2SEL_SH;
2978c2ecf20Sopenharmony_ci		}
2988c2ecf20Sopenharmony_ci		if (power7_marked_instr_event(event[i]))
2998c2ecf20Sopenharmony_ci			mmcra |= MMCRA_SAMPLE_ENABLE;
3008c2ecf20Sopenharmony_ci		hwc[i] = pmc;
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	/* Return MMCRx values */
3048c2ecf20Sopenharmony_ci	mmcr->mmcr0 = 0;
3058c2ecf20Sopenharmony_ci	if (pmc_inuse & 1)
3068c2ecf20Sopenharmony_ci		mmcr->mmcr0 = MMCR0_PMC1CE;
3078c2ecf20Sopenharmony_ci	if (pmc_inuse & 0x3e)
3088c2ecf20Sopenharmony_ci		mmcr->mmcr0 |= MMCR0_PMCjCE;
3098c2ecf20Sopenharmony_ci	mmcr->mmcr1 = mmcr1;
3108c2ecf20Sopenharmony_ci	mmcr->mmcra = mmcra;
3118c2ecf20Sopenharmony_ci	return 0;
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic void power7_disable_pmc(unsigned int pmc, struct mmcr_regs *mmcr)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	if (pmc <= 3)
3178c2ecf20Sopenharmony_ci		mmcr->mmcr1 &= ~(0xffUL << MMCR1_PMCSEL_SH(pmc));
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_cistatic int power7_generic_events[] = {
3218c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CPU_CYCLES] =			PM_CYC,
3228c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =	PM_GCT_NOSLOT_CYC,
3238c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =	PM_CMPLU_STALL,
3248c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_INSTRUCTIONS] =			PM_INST_CMPL,
3258c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CACHE_REFERENCES] =		PM_LD_REF_L1,
3268c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CACHE_MISSES] =			PM_LD_MISS_L1,
3278c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] =		PM_BRU_FIN,
3288c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_MISSES] =			PM_BR_MPRED,
3298c2ecf20Sopenharmony_ci};
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci#define C(x)	PERF_COUNT_HW_CACHE_##x
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci/*
3348c2ecf20Sopenharmony_ci * Table of generalized cache-related events.
3358c2ecf20Sopenharmony_ci * 0 means not supported, -1 means nonsensical, other values
3368c2ecf20Sopenharmony_ci * are event codes.
3378c2ecf20Sopenharmony_ci */
3388c2ecf20Sopenharmony_cistatic u64 power7_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
3398c2ecf20Sopenharmony_ci	[C(L1D)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
3408c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0xc880,		0x400f0	},
3418c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	0,		0x300f0	},
3428c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0xd8b8,		0	},
3438c2ecf20Sopenharmony_ci	},
3448c2ecf20Sopenharmony_ci	[C(L1I)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
3458c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0x200fc	},
3468c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
3478c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0x408a,		0	},
3488c2ecf20Sopenharmony_ci	},
3498c2ecf20Sopenharmony_ci	[C(LL)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
3508c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0x16080,	0x26080	},
3518c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	0x16082,	0x26082	},
3528c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0,		0	},
3538c2ecf20Sopenharmony_ci	},
3548c2ecf20Sopenharmony_ci	[C(DTLB)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
3558c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0x300fc	},
3568c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
3578c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1	},
3588c2ecf20Sopenharmony_ci	},
3598c2ecf20Sopenharmony_ci	[C(ITLB)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
3608c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0x400fc	},
3618c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
3628c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1	},
3638c2ecf20Sopenharmony_ci	},
3648c2ecf20Sopenharmony_ci	[C(BPU)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
3658c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0x10068,	0x400f6	},
3668c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
3678c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1	},
3688c2ecf20Sopenharmony_ci	},
3698c2ecf20Sopenharmony_ci	[C(NODE)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
3708c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	-1,		-1	},
3718c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
3728c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1	},
3738c2ecf20Sopenharmony_ci	},
3748c2ecf20Sopenharmony_ci};
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(cpu-cycles,			PM_CYC);
3788c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(stalled-cycles-frontend,	PM_GCT_NOSLOT_CYC);
3798c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(stalled-cycles-backend,	PM_CMPLU_STALL);
3808c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(instructions,		PM_INST_CMPL);
3818c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(cache-references,		PM_LD_REF_L1);
3828c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(cache-misses,		PM_LD_MISS_L1);
3838c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(branch-instructions,		PM_BRU_FIN);
3848c2ecf20Sopenharmony_ciGENERIC_EVENT_ATTR(branch-misses,		PM_BR_MPRED);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci#define EVENT(_name, _code)     POWER_EVENT_ATTR(_name, _name);
3878c2ecf20Sopenharmony_ci#include "power7-events-list.h"
3888c2ecf20Sopenharmony_ci#undef EVENT
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci#define EVENT(_name, _code)     POWER_EVENT_PTR(_name),
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistatic struct attribute *power7_events_attr[] = {
3938c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_CYC),
3948c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_GCT_NOSLOT_CYC),
3958c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_CMPLU_STALL),
3968c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_INST_CMPL),
3978c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_LD_REF_L1),
3988c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_LD_MISS_L1),
3998c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_BRU_FIN),
4008c2ecf20Sopenharmony_ci	GENERIC_EVENT_PTR(PM_BR_MPRED),
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	#include "power7-events-list.h"
4038c2ecf20Sopenharmony_ci	#undef EVENT
4048c2ecf20Sopenharmony_ci	NULL
4058c2ecf20Sopenharmony_ci};
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic struct attribute_group power7_pmu_events_group = {
4088c2ecf20Sopenharmony_ci	.name = "events",
4098c2ecf20Sopenharmony_ci	.attrs = power7_events_attr,
4108c2ecf20Sopenharmony_ci};
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ciPMU_FORMAT_ATTR(event, "config:0-19");
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistatic struct attribute *power7_pmu_format_attr[] = {
4158c2ecf20Sopenharmony_ci	&format_attr_event.attr,
4168c2ecf20Sopenharmony_ci	NULL,
4178c2ecf20Sopenharmony_ci};
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic struct attribute_group power7_pmu_format_group = {
4208c2ecf20Sopenharmony_ci	.name = "format",
4218c2ecf20Sopenharmony_ci	.attrs = power7_pmu_format_attr,
4228c2ecf20Sopenharmony_ci};
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic const struct attribute_group *power7_pmu_attr_groups[] = {
4258c2ecf20Sopenharmony_ci	&power7_pmu_format_group,
4268c2ecf20Sopenharmony_ci	&power7_pmu_events_group,
4278c2ecf20Sopenharmony_ci	NULL,
4288c2ecf20Sopenharmony_ci};
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistatic struct power_pmu power7_pmu = {
4318c2ecf20Sopenharmony_ci	.name			= "POWER7",
4328c2ecf20Sopenharmony_ci	.n_counter		= 6,
4338c2ecf20Sopenharmony_ci	.max_alternatives	= MAX_ALT + 1,
4348c2ecf20Sopenharmony_ci	.add_fields		= 0x1555ul,
4358c2ecf20Sopenharmony_ci	.test_adder		= 0x3000ul,
4368c2ecf20Sopenharmony_ci	.compute_mmcr		= power7_compute_mmcr,
4378c2ecf20Sopenharmony_ci	.get_constraint		= power7_get_constraint,
4388c2ecf20Sopenharmony_ci	.get_alternatives	= power7_get_alternatives,
4398c2ecf20Sopenharmony_ci	.disable_pmc		= power7_disable_pmc,
4408c2ecf20Sopenharmony_ci	.flags			= PPMU_ALT_SIPR,
4418c2ecf20Sopenharmony_ci	.attr_groups		= power7_pmu_attr_groups,
4428c2ecf20Sopenharmony_ci	.n_generic		= ARRAY_SIZE(power7_generic_events),
4438c2ecf20Sopenharmony_ci	.generic_events		= power7_generic_events,
4448c2ecf20Sopenharmony_ci	.cache_events		= &power7_cache_events,
4458c2ecf20Sopenharmony_ci};
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ciint init_power7_pmu(void)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	if (!cur_cpu_spec->oprofile_cpu_type ||
4508c2ecf20Sopenharmony_ci	    strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power7"))
4518c2ecf20Sopenharmony_ci		return -ENODEV;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	if (pvr_version_is(PVR_POWER7p))
4548c2ecf20Sopenharmony_ci		power7_pmu.flags |= PPMU_SIAR_VALID;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	return register_power_pmu(&power7_pmu);
4578c2ecf20Sopenharmony_ci}
458