18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Performance counter support for POWER6 processors.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2008-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 POWER6
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci#define PM_PMC_SH	20	/* PMC number (1-based) for direct events */
198c2ecf20Sopenharmony_ci#define PM_PMC_MSK	0x7
208c2ecf20Sopenharmony_ci#define PM_PMC_MSKS	(PM_PMC_MSK << PM_PMC_SH)
218c2ecf20Sopenharmony_ci#define PM_UNIT_SH	16	/* Unit event comes (TTMxSEL encoding) */
228c2ecf20Sopenharmony_ci#define PM_UNIT_MSK	0xf
238c2ecf20Sopenharmony_ci#define PM_UNIT_MSKS	(PM_UNIT_MSK << PM_UNIT_SH)
248c2ecf20Sopenharmony_ci#define PM_LLAV		0x8000	/* Load lookahead match value */
258c2ecf20Sopenharmony_ci#define PM_LLA		0x4000	/* Load lookahead match enable */
268c2ecf20Sopenharmony_ci#define PM_BYTE_SH	12	/* Byte of event bus to use */
278c2ecf20Sopenharmony_ci#define PM_BYTE_MSK	3
288c2ecf20Sopenharmony_ci#define PM_SUBUNIT_SH	8	/* Subunit event comes from (NEST_SEL enc.) */
298c2ecf20Sopenharmony_ci#define PM_SUBUNIT_MSK	7
308c2ecf20Sopenharmony_ci#define PM_SUBUNIT_MSKS	(PM_SUBUNIT_MSK << PM_SUBUNIT_SH)
318c2ecf20Sopenharmony_ci#define PM_PMCSEL_MSK	0xff	/* PMCxSEL value */
328c2ecf20Sopenharmony_ci#define PM_BUSEVENT_MSK	0xf3700
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/*
358c2ecf20Sopenharmony_ci * Bits in MMCR1 for POWER6
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci#define MMCR1_TTM0SEL_SH	60
388c2ecf20Sopenharmony_ci#define MMCR1_TTMSEL_SH(n)	(MMCR1_TTM0SEL_SH - (n) * 4)
398c2ecf20Sopenharmony_ci#define MMCR1_TTMSEL_MSK	0xf
408c2ecf20Sopenharmony_ci#define MMCR1_TTMSEL(m, n)	(((m) >> MMCR1_TTMSEL_SH(n)) & MMCR1_TTMSEL_MSK)
418c2ecf20Sopenharmony_ci#define MMCR1_NESTSEL_SH	45
428c2ecf20Sopenharmony_ci#define MMCR1_NESTSEL_MSK	0x7
438c2ecf20Sopenharmony_ci#define MMCR1_NESTSEL(m)	(((m) >> MMCR1_NESTSEL_SH) & MMCR1_NESTSEL_MSK)
448c2ecf20Sopenharmony_ci#define MMCR1_PMC1_LLA		(1ul << 44)
458c2ecf20Sopenharmony_ci#define MMCR1_PMC1_LLA_VALUE	(1ul << 39)
468c2ecf20Sopenharmony_ci#define MMCR1_PMC1_ADDR_SEL	(1ul << 35)
478c2ecf20Sopenharmony_ci#define MMCR1_PMC1SEL_SH	24
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 * Map of which direct events on which PMCs are marked instruction events.
538c2ecf20Sopenharmony_ci * Indexed by PMCSEL value >> 1.
548c2ecf20Sopenharmony_ci * Bottom 4 bits are a map of which PMCs are interesting,
558c2ecf20Sopenharmony_ci * top 4 bits say what sort of event:
568c2ecf20Sopenharmony_ci *   0 = direct marked event,
578c2ecf20Sopenharmony_ci *   1 = byte decode event,
588c2ecf20Sopenharmony_ci *   4 = add/and event (PMC1 -> bits 0 & 4),
598c2ecf20Sopenharmony_ci *   5 = add/and event (PMC1 -> bits 1 & 5),
608c2ecf20Sopenharmony_ci *   6 = add/and event (PMC1 -> bits 2 & 6),
618c2ecf20Sopenharmony_ci *   7 = add/and event (PMC1 -> bits 3 & 7).
628c2ecf20Sopenharmony_ci */
638c2ecf20Sopenharmony_cistatic unsigned char direct_event_is_marked[0x60 >> 1] = {
648c2ecf20Sopenharmony_ci	0,	/* 00 */
658c2ecf20Sopenharmony_ci	0,	/* 02 */
668c2ecf20Sopenharmony_ci	0,	/* 04 */
678c2ecf20Sopenharmony_ci	0x07,	/* 06 PM_MRK_ST_CMPL, PM_MRK_ST_GPS, PM_MRK_ST_CMPL_INT */
688c2ecf20Sopenharmony_ci	0x04,	/* 08 PM_MRK_DFU_FIN */
698c2ecf20Sopenharmony_ci	0x06,	/* 0a PM_MRK_IFU_FIN, PM_MRK_INST_FIN */
708c2ecf20Sopenharmony_ci	0,	/* 0c */
718c2ecf20Sopenharmony_ci	0,	/* 0e */
728c2ecf20Sopenharmony_ci	0x02,	/* 10 PM_MRK_INST_DISP */
738c2ecf20Sopenharmony_ci	0x08,	/* 12 PM_MRK_LSU_DERAT_MISS */
748c2ecf20Sopenharmony_ci	0,	/* 14 */
758c2ecf20Sopenharmony_ci	0,	/* 16 */
768c2ecf20Sopenharmony_ci	0x0c,	/* 18 PM_THRESH_TIMEO, PM_MRK_INST_FIN */
778c2ecf20Sopenharmony_ci	0x0f,	/* 1a PM_MRK_INST_DISP, PM_MRK_{FXU,FPU,LSU}_FIN */
788c2ecf20Sopenharmony_ci	0x01,	/* 1c PM_MRK_INST_ISSUED */
798c2ecf20Sopenharmony_ci	0,	/* 1e */
808c2ecf20Sopenharmony_ci	0,	/* 20 */
818c2ecf20Sopenharmony_ci	0,	/* 22 */
828c2ecf20Sopenharmony_ci	0,	/* 24 */
838c2ecf20Sopenharmony_ci	0,	/* 26 */
848c2ecf20Sopenharmony_ci	0x15,	/* 28 PM_MRK_DATA_FROM_L2MISS, PM_MRK_DATA_FROM_L3MISS */
858c2ecf20Sopenharmony_ci	0,	/* 2a */
868c2ecf20Sopenharmony_ci	0,	/* 2c */
878c2ecf20Sopenharmony_ci	0,	/* 2e */
888c2ecf20Sopenharmony_ci	0x4f,	/* 30 */
898c2ecf20Sopenharmony_ci	0x7f,	/* 32 */
908c2ecf20Sopenharmony_ci	0x4f,	/* 34 */
918c2ecf20Sopenharmony_ci	0x5f,	/* 36 */
928c2ecf20Sopenharmony_ci	0x6f,	/* 38 */
938c2ecf20Sopenharmony_ci	0x4f,	/* 3a */
948c2ecf20Sopenharmony_ci	0,	/* 3c */
958c2ecf20Sopenharmony_ci	0x08,	/* 3e PM_MRK_INST_TIMEO */
968c2ecf20Sopenharmony_ci	0x1f,	/* 40 */
978c2ecf20Sopenharmony_ci	0x1f,	/* 42 */
988c2ecf20Sopenharmony_ci	0x1f,	/* 44 */
998c2ecf20Sopenharmony_ci	0x1f,	/* 46 */
1008c2ecf20Sopenharmony_ci	0x1f,	/* 48 */
1018c2ecf20Sopenharmony_ci	0x1f,	/* 4a */
1028c2ecf20Sopenharmony_ci	0x1f,	/* 4c */
1038c2ecf20Sopenharmony_ci	0x1f,	/* 4e */
1048c2ecf20Sopenharmony_ci	0,	/* 50 */
1058c2ecf20Sopenharmony_ci	0x05,	/* 52 PM_MRK_BR_TAKEN, PM_MRK_BR_MPRED */
1068c2ecf20Sopenharmony_ci	0x1c,	/* 54 PM_MRK_PTEG_FROM_L3MISS, PM_MRK_PTEG_FROM_L2MISS */
1078c2ecf20Sopenharmony_ci	0x02,	/* 56 PM_MRK_LD_MISS_L1 */
1088c2ecf20Sopenharmony_ci	0,	/* 58 */
1098c2ecf20Sopenharmony_ci	0,	/* 5a */
1108c2ecf20Sopenharmony_ci	0,	/* 5c */
1118c2ecf20Sopenharmony_ci	0,	/* 5e */
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/*
1158c2ecf20Sopenharmony_ci * Masks showing for each unit which bits are marked events.
1168c2ecf20Sopenharmony_ci * These masks are in LE order, i.e. 0x00000001 is byte 0, bit 0.
1178c2ecf20Sopenharmony_ci */
1188c2ecf20Sopenharmony_cistatic u32 marked_bus_events[16] = {
1198c2ecf20Sopenharmony_ci	0x01000000,	/* direct events set 1: byte 3 bit 0 */
1208c2ecf20Sopenharmony_ci	0x00010000,	/* direct events set 2: byte 2 bit 0 */
1218c2ecf20Sopenharmony_ci	0, 0, 0, 0,	/* IDU, IFU, nest: nothing */
1228c2ecf20Sopenharmony_ci	0x00000088,	/* VMX set 1: byte 0 bits 3, 7 */
1238c2ecf20Sopenharmony_ci	0x000000c0,	/* VMX set 2: byte 0 bits 4-7 */
1248c2ecf20Sopenharmony_ci	0x04010000,	/* LSU set 1: byte 2 bit 0, byte 3 bit 2 */
1258c2ecf20Sopenharmony_ci	0xff010000u,	/* LSU set 2: byte 2 bit 0, all of byte 3 */
1268c2ecf20Sopenharmony_ci	0,		/* LSU set 3 */
1278c2ecf20Sopenharmony_ci	0x00000010,	/* VMX set 3: byte 0 bit 4 */
1288c2ecf20Sopenharmony_ci	0,		/* BFP set 1 */
1298c2ecf20Sopenharmony_ci	0x00000022,	/* BFP set 2: byte 0 bits 1, 5 */
1308c2ecf20Sopenharmony_ci	0, 0
1318c2ecf20Sopenharmony_ci};
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/*
1348c2ecf20Sopenharmony_ci * Returns 1 if event counts things relating to marked instructions
1358c2ecf20Sopenharmony_ci * and thus needs the MMCRA_SAMPLE_ENABLE bit set, or 0 if not.
1368c2ecf20Sopenharmony_ci */
1378c2ecf20Sopenharmony_cistatic int power6_marked_instr_event(u64 event)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	int pmc, psel, ptype;
1408c2ecf20Sopenharmony_ci	int bit, byte, unit;
1418c2ecf20Sopenharmony_ci	u32 mask;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
1448c2ecf20Sopenharmony_ci	psel = (event & PM_PMCSEL_MSK) >> 1;	/* drop edge/level bit */
1458c2ecf20Sopenharmony_ci	if (pmc >= 5)
1468c2ecf20Sopenharmony_ci		return 0;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	bit = -1;
1498c2ecf20Sopenharmony_ci	if (psel < sizeof(direct_event_is_marked)) {
1508c2ecf20Sopenharmony_ci		ptype = direct_event_is_marked[psel];
1518c2ecf20Sopenharmony_ci		if (pmc == 0 || !(ptype & (1 << (pmc - 1))))
1528c2ecf20Sopenharmony_ci			return 0;
1538c2ecf20Sopenharmony_ci		ptype >>= 4;
1548c2ecf20Sopenharmony_ci		if (ptype == 0)
1558c2ecf20Sopenharmony_ci			return 1;
1568c2ecf20Sopenharmony_ci		if (ptype == 1)
1578c2ecf20Sopenharmony_ci			bit = 0;
1588c2ecf20Sopenharmony_ci		else
1598c2ecf20Sopenharmony_ci			bit = ptype ^ (pmc - 1);
1608c2ecf20Sopenharmony_ci	} else if ((psel & 0x48) == 0x40)
1618c2ecf20Sopenharmony_ci		bit = psel & 7;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	if (!(event & PM_BUSEVENT_MSK) || bit == -1)
1648c2ecf20Sopenharmony_ci		return 0;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
1678c2ecf20Sopenharmony_ci	unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
1688c2ecf20Sopenharmony_ci	mask = marked_bus_events[unit];
1698c2ecf20Sopenharmony_ci	return (mask >> (byte * 8 + bit)) & 1;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci/*
1738c2ecf20Sopenharmony_ci * Assign PMC numbers and compute MMCR1 value for a set of events
1748c2ecf20Sopenharmony_ci */
1758c2ecf20Sopenharmony_cistatic int p6_compute_mmcr(u64 event[], int n_ev,
1768c2ecf20Sopenharmony_ci			   unsigned int hwc[], struct mmcr_regs *mmcr, struct perf_event *pevents[])
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	unsigned long mmcr1 = 0;
1798c2ecf20Sopenharmony_ci	unsigned long mmcra = MMCRA_SDAR_DCACHE_MISS | MMCRA_SDAR_ERAT_MISS;
1808c2ecf20Sopenharmony_ci	int i;
1818c2ecf20Sopenharmony_ci	unsigned int pmc, ev, b, u, s, psel;
1828c2ecf20Sopenharmony_ci	unsigned int ttmset = 0;
1838c2ecf20Sopenharmony_ci	unsigned int pmc_inuse = 0;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	if (n_ev > 6)
1868c2ecf20Sopenharmony_ci		return -1;
1878c2ecf20Sopenharmony_ci	for (i = 0; i < n_ev; ++i) {
1888c2ecf20Sopenharmony_ci		pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
1898c2ecf20Sopenharmony_ci		if (pmc) {
1908c2ecf20Sopenharmony_ci			if (pmc_inuse & (1 << (pmc - 1)))
1918c2ecf20Sopenharmony_ci				return -1;	/* collision! */
1928c2ecf20Sopenharmony_ci			pmc_inuse |= 1 << (pmc - 1);
1938c2ecf20Sopenharmony_ci		}
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci	for (i = 0; i < n_ev; ++i) {
1968c2ecf20Sopenharmony_ci		ev = event[i];
1978c2ecf20Sopenharmony_ci		pmc = (ev >> PM_PMC_SH) & PM_PMC_MSK;
1988c2ecf20Sopenharmony_ci		if (pmc) {
1998c2ecf20Sopenharmony_ci			--pmc;
2008c2ecf20Sopenharmony_ci		} else {
2018c2ecf20Sopenharmony_ci			/* can go on any PMC; find a free one */
2028c2ecf20Sopenharmony_ci			for (pmc = 0; pmc < 4; ++pmc)
2038c2ecf20Sopenharmony_ci				if (!(pmc_inuse & (1 << pmc)))
2048c2ecf20Sopenharmony_ci					break;
2058c2ecf20Sopenharmony_ci			if (pmc >= 4)
2068c2ecf20Sopenharmony_ci				return -1;
2078c2ecf20Sopenharmony_ci			pmc_inuse |= 1 << pmc;
2088c2ecf20Sopenharmony_ci		}
2098c2ecf20Sopenharmony_ci		hwc[i] = pmc;
2108c2ecf20Sopenharmony_ci		psel = ev & PM_PMCSEL_MSK;
2118c2ecf20Sopenharmony_ci		if (ev & PM_BUSEVENT_MSK) {
2128c2ecf20Sopenharmony_ci			/* this event uses the event bus */
2138c2ecf20Sopenharmony_ci			b = (ev >> PM_BYTE_SH) & PM_BYTE_MSK;
2148c2ecf20Sopenharmony_ci			u = (ev >> PM_UNIT_SH) & PM_UNIT_MSK;
2158c2ecf20Sopenharmony_ci			/* check for conflict on this byte of event bus */
2168c2ecf20Sopenharmony_ci			if ((ttmset & (1 << b)) && MMCR1_TTMSEL(mmcr1, b) != u)
2178c2ecf20Sopenharmony_ci				return -1;
2188c2ecf20Sopenharmony_ci			mmcr1 |= (unsigned long)u << MMCR1_TTMSEL_SH(b);
2198c2ecf20Sopenharmony_ci			ttmset |= 1 << b;
2208c2ecf20Sopenharmony_ci			if (u == 5) {
2218c2ecf20Sopenharmony_ci				/* Nest events have a further mux */
2228c2ecf20Sopenharmony_ci				s = (ev >> PM_SUBUNIT_SH) & PM_SUBUNIT_MSK;
2238c2ecf20Sopenharmony_ci				if ((ttmset & 0x10) &&
2248c2ecf20Sopenharmony_ci				    MMCR1_NESTSEL(mmcr1) != s)
2258c2ecf20Sopenharmony_ci					return -1;
2268c2ecf20Sopenharmony_ci				ttmset |= 0x10;
2278c2ecf20Sopenharmony_ci				mmcr1 |= (unsigned long)s << MMCR1_NESTSEL_SH;
2288c2ecf20Sopenharmony_ci			}
2298c2ecf20Sopenharmony_ci			if (0x30 <= psel && psel <= 0x3d) {
2308c2ecf20Sopenharmony_ci				/* these need the PMCx_ADDR_SEL bits */
2318c2ecf20Sopenharmony_ci				if (b >= 2)
2328c2ecf20Sopenharmony_ci					mmcr1 |= MMCR1_PMC1_ADDR_SEL >> pmc;
2338c2ecf20Sopenharmony_ci			}
2348c2ecf20Sopenharmony_ci			/* bus select values are different for PMC3/4 */
2358c2ecf20Sopenharmony_ci			if (pmc >= 2 && (psel & 0x90) == 0x80)
2368c2ecf20Sopenharmony_ci				psel ^= 0x20;
2378c2ecf20Sopenharmony_ci		}
2388c2ecf20Sopenharmony_ci		if (ev & PM_LLA) {
2398c2ecf20Sopenharmony_ci			mmcr1 |= MMCR1_PMC1_LLA >> pmc;
2408c2ecf20Sopenharmony_ci			if (ev & PM_LLAV)
2418c2ecf20Sopenharmony_ci				mmcr1 |= MMCR1_PMC1_LLA_VALUE >> pmc;
2428c2ecf20Sopenharmony_ci		}
2438c2ecf20Sopenharmony_ci		if (power6_marked_instr_event(event[i]))
2448c2ecf20Sopenharmony_ci			mmcra |= MMCRA_SAMPLE_ENABLE;
2458c2ecf20Sopenharmony_ci		if (pmc < 4)
2468c2ecf20Sopenharmony_ci			mmcr1 |= (unsigned long)psel << MMCR1_PMCSEL_SH(pmc);
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci	mmcr->mmcr0 = 0;
2498c2ecf20Sopenharmony_ci	if (pmc_inuse & 1)
2508c2ecf20Sopenharmony_ci		mmcr->mmcr0 = MMCR0_PMC1CE;
2518c2ecf20Sopenharmony_ci	if (pmc_inuse & 0xe)
2528c2ecf20Sopenharmony_ci		mmcr->mmcr0 |= MMCR0_PMCjCE;
2538c2ecf20Sopenharmony_ci	mmcr->mmcr1 = mmcr1;
2548c2ecf20Sopenharmony_ci	mmcr->mmcra = mmcra;
2558c2ecf20Sopenharmony_ci	return 0;
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci/*
2598c2ecf20Sopenharmony_ci * Layout of constraint bits:
2608c2ecf20Sopenharmony_ci *
2618c2ecf20Sopenharmony_ci *	0-1	add field: number of uses of PMC1 (max 1)
2628c2ecf20Sopenharmony_ci *	2-3, 4-5, 6-7, 8-9, 10-11: ditto for PMC2, 3, 4, 5, 6
2638c2ecf20Sopenharmony_ci *	12-15	add field: number of uses of PMC1-4 (max 4)
2648c2ecf20Sopenharmony_ci *	16-19	select field: unit on byte 0 of event bus
2658c2ecf20Sopenharmony_ci *	20-23, 24-27, 28-31 ditto for bytes 1, 2, 3
2668c2ecf20Sopenharmony_ci *	32-34	select field: nest (subunit) event selector
2678c2ecf20Sopenharmony_ci */
2688c2ecf20Sopenharmony_cistatic int p6_get_constraint(u64 event, unsigned long *maskp,
2698c2ecf20Sopenharmony_ci			     unsigned long *valp)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	int pmc, byte, sh, subunit;
2728c2ecf20Sopenharmony_ci	unsigned long mask = 0, value = 0;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
2758c2ecf20Sopenharmony_ci	if (pmc) {
2768c2ecf20Sopenharmony_ci		if (pmc > 4 && !(event == 0x500009 || event == 0x600005))
2778c2ecf20Sopenharmony_ci			return -1;
2788c2ecf20Sopenharmony_ci		sh = (pmc - 1) * 2;
2798c2ecf20Sopenharmony_ci		mask |= 2 << sh;
2808c2ecf20Sopenharmony_ci		value |= 1 << sh;
2818c2ecf20Sopenharmony_ci	}
2828c2ecf20Sopenharmony_ci	if (event & PM_BUSEVENT_MSK) {
2838c2ecf20Sopenharmony_ci		byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
2848c2ecf20Sopenharmony_ci		sh = byte * 4 + (16 - PM_UNIT_SH);
2858c2ecf20Sopenharmony_ci		mask |= PM_UNIT_MSKS << sh;
2868c2ecf20Sopenharmony_ci		value |= (unsigned long)(event & PM_UNIT_MSKS) << sh;
2878c2ecf20Sopenharmony_ci		if ((event & PM_UNIT_MSKS) == (5 << PM_UNIT_SH)) {
2888c2ecf20Sopenharmony_ci			subunit = (event >> PM_SUBUNIT_SH) & PM_SUBUNIT_MSK;
2898c2ecf20Sopenharmony_ci			mask  |= (unsigned long)PM_SUBUNIT_MSK << 32;
2908c2ecf20Sopenharmony_ci			value |= (unsigned long)subunit << 32;
2918c2ecf20Sopenharmony_ci		}
2928c2ecf20Sopenharmony_ci	}
2938c2ecf20Sopenharmony_ci	if (pmc <= 4) {
2948c2ecf20Sopenharmony_ci		mask  |= 0x8000;	/* add field for count of PMC1-4 uses */
2958c2ecf20Sopenharmony_ci		value |= 0x1000;
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci	*maskp = mask;
2988c2ecf20Sopenharmony_ci	*valp = value;
2998c2ecf20Sopenharmony_ci	return 0;
3008c2ecf20Sopenharmony_ci}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic int p6_limited_pmc_event(u64 event)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	int pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	return pmc == 5 || pmc == 6;
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci#define MAX_ALT	4	/* at most 4 alternatives for any event */
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistatic const unsigned int event_alternatives[][MAX_ALT] = {
3128c2ecf20Sopenharmony_ci	{ 0x0130e8, 0x2000f6, 0x3000fc },	/* PM_PTEG_RELOAD_VALID */
3138c2ecf20Sopenharmony_ci	{ 0x080080, 0x10000d, 0x30000c, 0x4000f0 }, /* PM_LD_MISS_L1 */
3148c2ecf20Sopenharmony_ci	{ 0x080088, 0x200054, 0x3000f0 },	/* PM_ST_MISS_L1 */
3158c2ecf20Sopenharmony_ci	{ 0x10000a, 0x2000f4, 0x600005 },	/* PM_RUN_CYC */
3168c2ecf20Sopenharmony_ci	{ 0x10000b, 0x2000f5 },			/* PM_RUN_COUNT */
3178c2ecf20Sopenharmony_ci	{ 0x10000e, 0x400010 },			/* PM_PURR */
3188c2ecf20Sopenharmony_ci	{ 0x100010, 0x4000f8 },			/* PM_FLUSH */
3198c2ecf20Sopenharmony_ci	{ 0x10001a, 0x200010 },			/* PM_MRK_INST_DISP */
3208c2ecf20Sopenharmony_ci	{ 0x100026, 0x3000f8 },			/* PM_TB_BIT_TRANS */
3218c2ecf20Sopenharmony_ci	{ 0x100054, 0x2000f0 },			/* PM_ST_FIN */
3228c2ecf20Sopenharmony_ci	{ 0x100056, 0x2000fc },			/* PM_L1_ICACHE_MISS */
3238c2ecf20Sopenharmony_ci	{ 0x1000f0, 0x40000a },			/* PM_INST_IMC_MATCH_CMPL */
3248c2ecf20Sopenharmony_ci	{ 0x1000f8, 0x200008 },			/* PM_GCT_EMPTY_CYC */
3258c2ecf20Sopenharmony_ci	{ 0x1000fc, 0x400006 },			/* PM_LSU_DERAT_MISS_CYC */
3268c2ecf20Sopenharmony_ci	{ 0x20000e, 0x400007 },			/* PM_LSU_DERAT_MISS */
3278c2ecf20Sopenharmony_ci	{ 0x200012, 0x300012 },			/* PM_INST_DISP */
3288c2ecf20Sopenharmony_ci	{ 0x2000f2, 0x3000f2 },			/* PM_INST_DISP */
3298c2ecf20Sopenharmony_ci	{ 0x2000f8, 0x300010 },			/* PM_EXT_INT */
3308c2ecf20Sopenharmony_ci	{ 0x2000fe, 0x300056 },			/* PM_DATA_FROM_L2MISS */
3318c2ecf20Sopenharmony_ci	{ 0x2d0030, 0x30001a },			/* PM_MRK_FPU_FIN */
3328c2ecf20Sopenharmony_ci	{ 0x30000a, 0x400018 },			/* PM_MRK_INST_FIN */
3338c2ecf20Sopenharmony_ci	{ 0x3000f6, 0x40000e },			/* PM_L1_DCACHE_RELOAD_VALID */
3348c2ecf20Sopenharmony_ci	{ 0x3000fe, 0x400056 },			/* PM_DATA_FROM_L3MISS */
3358c2ecf20Sopenharmony_ci};
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci/*
3388c2ecf20Sopenharmony_ci * This could be made more efficient with a binary search on
3398c2ecf20Sopenharmony_ci * a presorted list, if necessary
3408c2ecf20Sopenharmony_ci */
3418c2ecf20Sopenharmony_cistatic int find_alternatives_list(u64 event)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	int i, j;
3448c2ecf20Sopenharmony_ci	unsigned int alt;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
3478c2ecf20Sopenharmony_ci		if (event < event_alternatives[i][0])
3488c2ecf20Sopenharmony_ci			return -1;
3498c2ecf20Sopenharmony_ci		for (j = 0; j < MAX_ALT; ++j) {
3508c2ecf20Sopenharmony_ci			alt = event_alternatives[i][j];
3518c2ecf20Sopenharmony_ci			if (!alt || event < alt)
3528c2ecf20Sopenharmony_ci				break;
3538c2ecf20Sopenharmony_ci			if (event == alt)
3548c2ecf20Sopenharmony_ci				return i;
3558c2ecf20Sopenharmony_ci		}
3568c2ecf20Sopenharmony_ci	}
3578c2ecf20Sopenharmony_ci	return -1;
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic int p6_get_alternatives(u64 event, unsigned int flags, u64 alt[])
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	int i, j, nlim;
3638c2ecf20Sopenharmony_ci	unsigned int psel, pmc;
3648c2ecf20Sopenharmony_ci	unsigned int nalt = 1;
3658c2ecf20Sopenharmony_ci	u64 aevent;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	alt[0] = event;
3688c2ecf20Sopenharmony_ci	nlim = p6_limited_pmc_event(event);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	/* check the alternatives table */
3718c2ecf20Sopenharmony_ci	i = find_alternatives_list(event);
3728c2ecf20Sopenharmony_ci	if (i >= 0) {
3738c2ecf20Sopenharmony_ci		/* copy out alternatives from list */
3748c2ecf20Sopenharmony_ci		for (j = 0; j < MAX_ALT; ++j) {
3758c2ecf20Sopenharmony_ci			aevent = event_alternatives[i][j];
3768c2ecf20Sopenharmony_ci			if (!aevent)
3778c2ecf20Sopenharmony_ci				break;
3788c2ecf20Sopenharmony_ci			if (aevent != event)
3798c2ecf20Sopenharmony_ci				alt[nalt++] = aevent;
3808c2ecf20Sopenharmony_ci			nlim += p6_limited_pmc_event(aevent);
3818c2ecf20Sopenharmony_ci		}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	} else {
3848c2ecf20Sopenharmony_ci		/* Check for alternative ways of computing sum events */
3858c2ecf20Sopenharmony_ci		/* PMCSEL 0x32 counter N == PMCSEL 0x34 counter 5-N */
3868c2ecf20Sopenharmony_ci		psel = event & (PM_PMCSEL_MSK & ~1);	/* ignore edge bit */
3878c2ecf20Sopenharmony_ci		pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
3888c2ecf20Sopenharmony_ci		if (pmc && (psel == 0x32 || psel == 0x34))
3898c2ecf20Sopenharmony_ci			alt[nalt++] = ((event ^ 0x6) & ~PM_PMC_MSKS) |
3908c2ecf20Sopenharmony_ci				((5 - pmc) << PM_PMC_SH);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci		/* PMCSEL 0x38 counter N == PMCSEL 0x3a counter N+/-2 */
3938c2ecf20Sopenharmony_ci		if (pmc && (psel == 0x38 || psel == 0x3a))
3948c2ecf20Sopenharmony_ci			alt[nalt++] = ((event ^ 0x2) & ~PM_PMC_MSKS) |
3958c2ecf20Sopenharmony_ci				((pmc > 2? pmc - 2: pmc + 2) << PM_PMC_SH);
3968c2ecf20Sopenharmony_ci	}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	if (flags & PPMU_ONLY_COUNT_RUN) {
3998c2ecf20Sopenharmony_ci		/*
4008c2ecf20Sopenharmony_ci		 * We're only counting in RUN state,
4018c2ecf20Sopenharmony_ci		 * so PM_CYC is equivalent to PM_RUN_CYC,
4028c2ecf20Sopenharmony_ci		 * PM_INST_CMPL === PM_RUN_INST_CMPL, PM_PURR === PM_RUN_PURR.
4038c2ecf20Sopenharmony_ci		 * This doesn't include alternatives that don't provide
4048c2ecf20Sopenharmony_ci		 * any extra flexibility in assigning PMCs (e.g.
4058c2ecf20Sopenharmony_ci		 * 0x10000a for PM_RUN_CYC vs. 0x1e for PM_CYC).
4068c2ecf20Sopenharmony_ci		 * Note that even with these additional alternatives
4078c2ecf20Sopenharmony_ci		 * we never end up with more than 4 alternatives for any event.
4088c2ecf20Sopenharmony_ci		 */
4098c2ecf20Sopenharmony_ci		j = nalt;
4108c2ecf20Sopenharmony_ci		for (i = 0; i < nalt; ++i) {
4118c2ecf20Sopenharmony_ci			switch (alt[i]) {
4128c2ecf20Sopenharmony_ci			case 0x1e:	/* PM_CYC */
4138c2ecf20Sopenharmony_ci				alt[j++] = 0x600005;	/* PM_RUN_CYC */
4148c2ecf20Sopenharmony_ci				++nlim;
4158c2ecf20Sopenharmony_ci				break;
4168c2ecf20Sopenharmony_ci			case 0x10000a:	/* PM_RUN_CYC */
4178c2ecf20Sopenharmony_ci				alt[j++] = 0x1e;	/* PM_CYC */
4188c2ecf20Sopenharmony_ci				break;
4198c2ecf20Sopenharmony_ci			case 2:		/* PM_INST_CMPL */
4208c2ecf20Sopenharmony_ci				alt[j++] = 0x500009;	/* PM_RUN_INST_CMPL */
4218c2ecf20Sopenharmony_ci				++nlim;
4228c2ecf20Sopenharmony_ci				break;
4238c2ecf20Sopenharmony_ci			case 0x500009:	/* PM_RUN_INST_CMPL */
4248c2ecf20Sopenharmony_ci				alt[j++] = 2;		/* PM_INST_CMPL */
4258c2ecf20Sopenharmony_ci				break;
4268c2ecf20Sopenharmony_ci			case 0x10000e:	/* PM_PURR */
4278c2ecf20Sopenharmony_ci				alt[j++] = 0x4000f4;	/* PM_RUN_PURR */
4288c2ecf20Sopenharmony_ci				break;
4298c2ecf20Sopenharmony_ci			case 0x4000f4:	/* PM_RUN_PURR */
4308c2ecf20Sopenharmony_ci				alt[j++] = 0x10000e;	/* PM_PURR */
4318c2ecf20Sopenharmony_ci				break;
4328c2ecf20Sopenharmony_ci			}
4338c2ecf20Sopenharmony_ci		}
4348c2ecf20Sopenharmony_ci		nalt = j;
4358c2ecf20Sopenharmony_ci	}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	if (!(flags & PPMU_LIMITED_PMC_OK) && nlim) {
4388c2ecf20Sopenharmony_ci		/* remove the limited PMC events */
4398c2ecf20Sopenharmony_ci		j = 0;
4408c2ecf20Sopenharmony_ci		for (i = 0; i < nalt; ++i) {
4418c2ecf20Sopenharmony_ci			if (!p6_limited_pmc_event(alt[i])) {
4428c2ecf20Sopenharmony_ci				alt[j] = alt[i];
4438c2ecf20Sopenharmony_ci				++j;
4448c2ecf20Sopenharmony_ci			}
4458c2ecf20Sopenharmony_ci		}
4468c2ecf20Sopenharmony_ci		nalt = j;
4478c2ecf20Sopenharmony_ci	} else if ((flags & PPMU_LIMITED_PMC_REQD) && nlim < nalt) {
4488c2ecf20Sopenharmony_ci		/* remove all but the limited PMC events */
4498c2ecf20Sopenharmony_ci		j = 0;
4508c2ecf20Sopenharmony_ci		for (i = 0; i < nalt; ++i) {
4518c2ecf20Sopenharmony_ci			if (p6_limited_pmc_event(alt[i])) {
4528c2ecf20Sopenharmony_ci				alt[j] = alt[i];
4538c2ecf20Sopenharmony_ci				++j;
4548c2ecf20Sopenharmony_ci			}
4558c2ecf20Sopenharmony_ci		}
4568c2ecf20Sopenharmony_ci		nalt = j;
4578c2ecf20Sopenharmony_ci	}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	return nalt;
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cistatic void p6_disable_pmc(unsigned int pmc, struct mmcr_regs *mmcr)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	/* Set PMCxSEL to 0 to disable PMCx */
4658c2ecf20Sopenharmony_ci	if (pmc <= 3)
4668c2ecf20Sopenharmony_ci		mmcr->mmcr1 &= ~(0xffUL << MMCR1_PMCSEL_SH(pmc));
4678c2ecf20Sopenharmony_ci}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_cistatic int power6_generic_events[] = {
4708c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CPU_CYCLES]		= 0x1e,
4718c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_INSTRUCTIONS]		= 2,
4728c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CACHE_REFERENCES]	= 0x280030, /* LD_REF_L1 */
4738c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CACHE_MISSES]		= 0x30000c, /* LD_MISS_L1 */
4748c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS]	= 0x410a0,  /* BR_PRED */
4758c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_MISSES]		= 0x400052, /* BR_MPRED */
4768c2ecf20Sopenharmony_ci};
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci#define C(x)	PERF_COUNT_HW_CACHE_##x
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci/*
4818c2ecf20Sopenharmony_ci * Table of generalized cache-related events.
4828c2ecf20Sopenharmony_ci * 0 means not supported, -1 means nonsensical, other values
4838c2ecf20Sopenharmony_ci * are event codes.
4848c2ecf20Sopenharmony_ci * The "DTLB" and "ITLB" events relate to the DERAT and IERAT.
4858c2ecf20Sopenharmony_ci */
4868c2ecf20Sopenharmony_cistatic u64 power6_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
4878c2ecf20Sopenharmony_ci	[C(L1D)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
4888c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0x280030,	0x80080		},
4898c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	0x180032,	0x80088		},
4908c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0x810a4,	0		},
4918c2ecf20Sopenharmony_ci	},
4928c2ecf20Sopenharmony_ci	[C(L1I)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
4938c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0x100056 	},
4948c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1		},
4958c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0x4008c,	0		},
4968c2ecf20Sopenharmony_ci	},
4978c2ecf20Sopenharmony_ci	[C(LL)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
4988c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0x150730,	0x250532	},
4998c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	0x250432,	0x150432	},
5008c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0x810a6,	0		},
5018c2ecf20Sopenharmony_ci	},
5028c2ecf20Sopenharmony_ci	[C(DTLB)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
5038c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0x20000e	},
5048c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1		},
5058c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1		},
5068c2ecf20Sopenharmony_ci	},
5078c2ecf20Sopenharmony_ci	[C(ITLB)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
5088c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0x420ce		},
5098c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1		},
5108c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1		},
5118c2ecf20Sopenharmony_ci	},
5128c2ecf20Sopenharmony_ci	[C(BPU)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
5138c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0x430e6,	0x400052	},
5148c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1		},
5158c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1		},
5168c2ecf20Sopenharmony_ci	},
5178c2ecf20Sopenharmony_ci	[C(NODE)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
5188c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	-1,		-1		},
5198c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1		},
5208c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1		},
5218c2ecf20Sopenharmony_ci	},
5228c2ecf20Sopenharmony_ci};
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_cistatic struct power_pmu power6_pmu = {
5258c2ecf20Sopenharmony_ci	.name			= "POWER6",
5268c2ecf20Sopenharmony_ci	.n_counter		= 6,
5278c2ecf20Sopenharmony_ci	.max_alternatives	= MAX_ALT,
5288c2ecf20Sopenharmony_ci	.add_fields		= 0x1555,
5298c2ecf20Sopenharmony_ci	.test_adder		= 0x3000,
5308c2ecf20Sopenharmony_ci	.compute_mmcr		= p6_compute_mmcr,
5318c2ecf20Sopenharmony_ci	.get_constraint		= p6_get_constraint,
5328c2ecf20Sopenharmony_ci	.get_alternatives	= p6_get_alternatives,
5338c2ecf20Sopenharmony_ci	.disable_pmc		= p6_disable_pmc,
5348c2ecf20Sopenharmony_ci	.limited_pmc_event	= p6_limited_pmc_event,
5358c2ecf20Sopenharmony_ci	.flags			= PPMU_LIMITED_PMC5_6 | PPMU_ALT_SIPR,
5368c2ecf20Sopenharmony_ci	.n_generic		= ARRAY_SIZE(power6_generic_events),
5378c2ecf20Sopenharmony_ci	.generic_events		= power6_generic_events,
5388c2ecf20Sopenharmony_ci	.cache_events		= &power6_cache_events,
5398c2ecf20Sopenharmony_ci};
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ciint init_power6_pmu(void)
5428c2ecf20Sopenharmony_ci{
5438c2ecf20Sopenharmony_ci	if (!cur_cpu_spec->oprofile_cpu_type ||
5448c2ecf20Sopenharmony_ci	    strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power6"))
5458c2ecf20Sopenharmony_ci		return -ENODEV;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	return register_power_pmu(&power6_pmu);
5488c2ecf20Sopenharmony_ci}
549