18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Performance counter support for PPC970-family processors.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/string.h>
88c2ecf20Sopenharmony_ci#include <linux/perf_event.h>
98c2ecf20Sopenharmony_ci#include <asm/reg.h>
108c2ecf20Sopenharmony_ci#include <asm/cputable.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "internal.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * Bits in event code for PPC970
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci#define PM_PMC_SH	12	/* PMC number (1-based) for direct events */
188c2ecf20Sopenharmony_ci#define PM_PMC_MSK	0xf
198c2ecf20Sopenharmony_ci#define PM_UNIT_SH	8	/* TTMMUX number and setting - unit select */
208c2ecf20Sopenharmony_ci#define PM_UNIT_MSK	0xf
218c2ecf20Sopenharmony_ci#define PM_SPCSEL_SH	6
228c2ecf20Sopenharmony_ci#define PM_SPCSEL_MSK	3
238c2ecf20Sopenharmony_ci#define PM_BYTE_SH	4	/* Byte number of event bus to use */
248c2ecf20Sopenharmony_ci#define PM_BYTE_MSK	3
258c2ecf20Sopenharmony_ci#define PM_PMCSEL_MSK	0xf
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* Values in PM_UNIT field */
288c2ecf20Sopenharmony_ci#define PM_NONE		0
298c2ecf20Sopenharmony_ci#define PM_FPU		1
308c2ecf20Sopenharmony_ci#define PM_VPU		2
318c2ecf20Sopenharmony_ci#define PM_ISU		3
328c2ecf20Sopenharmony_ci#define PM_IFU		4
338c2ecf20Sopenharmony_ci#define PM_IDU		5
348c2ecf20Sopenharmony_ci#define PM_STS		6
358c2ecf20Sopenharmony_ci#define PM_LSU0		7
368c2ecf20Sopenharmony_ci#define PM_LSU1U	8
378c2ecf20Sopenharmony_ci#define PM_LSU1L	9
388c2ecf20Sopenharmony_ci#define PM_LASTUNIT	9
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * Bits in MMCR0 for PPC970
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci#define MMCR0_PMC1SEL_SH	8
448c2ecf20Sopenharmony_ci#define MMCR0_PMC2SEL_SH	1
458c2ecf20Sopenharmony_ci#define MMCR_PMCSEL_MSK		0x1f
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/*
488c2ecf20Sopenharmony_ci * Bits in MMCR1 for PPC970
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_ci#define MMCR1_TTM0SEL_SH	62
518c2ecf20Sopenharmony_ci#define MMCR1_TTM1SEL_SH	59
528c2ecf20Sopenharmony_ci#define MMCR1_TTM3SEL_SH	53
538c2ecf20Sopenharmony_ci#define MMCR1_TTMSEL_MSK	3
548c2ecf20Sopenharmony_ci#define MMCR1_TD_CP_DBG0SEL_SH	50
558c2ecf20Sopenharmony_ci#define MMCR1_TD_CP_DBG1SEL_SH	48
568c2ecf20Sopenharmony_ci#define MMCR1_TD_CP_DBG2SEL_SH	46
578c2ecf20Sopenharmony_ci#define MMCR1_TD_CP_DBG3SEL_SH	44
588c2ecf20Sopenharmony_ci#define MMCR1_PMC1_ADDER_SEL_SH	39
598c2ecf20Sopenharmony_ci#define MMCR1_PMC2_ADDER_SEL_SH	38
608c2ecf20Sopenharmony_ci#define MMCR1_PMC6_ADDER_SEL_SH	37
618c2ecf20Sopenharmony_ci#define MMCR1_PMC5_ADDER_SEL_SH	36
628c2ecf20Sopenharmony_ci#define MMCR1_PMC8_ADDER_SEL_SH	35
638c2ecf20Sopenharmony_ci#define MMCR1_PMC7_ADDER_SEL_SH	34
648c2ecf20Sopenharmony_ci#define MMCR1_PMC3_ADDER_SEL_SH	33
658c2ecf20Sopenharmony_ci#define MMCR1_PMC4_ADDER_SEL_SH	32
668c2ecf20Sopenharmony_ci#define MMCR1_PMC3SEL_SH	27
678c2ecf20Sopenharmony_ci#define MMCR1_PMC4SEL_SH	22
688c2ecf20Sopenharmony_ci#define MMCR1_PMC5SEL_SH	17
698c2ecf20Sopenharmony_ci#define MMCR1_PMC6SEL_SH	12
708c2ecf20Sopenharmony_ci#define MMCR1_PMC7SEL_SH	7
718c2ecf20Sopenharmony_ci#define MMCR1_PMC8SEL_SH	2
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic short mmcr1_adder_bits[8] = {
748c2ecf20Sopenharmony_ci	MMCR1_PMC1_ADDER_SEL_SH,
758c2ecf20Sopenharmony_ci	MMCR1_PMC2_ADDER_SEL_SH,
768c2ecf20Sopenharmony_ci	MMCR1_PMC3_ADDER_SEL_SH,
778c2ecf20Sopenharmony_ci	MMCR1_PMC4_ADDER_SEL_SH,
788c2ecf20Sopenharmony_ci	MMCR1_PMC5_ADDER_SEL_SH,
798c2ecf20Sopenharmony_ci	MMCR1_PMC6_ADDER_SEL_SH,
808c2ecf20Sopenharmony_ci	MMCR1_PMC7_ADDER_SEL_SH,
818c2ecf20Sopenharmony_ci	MMCR1_PMC8_ADDER_SEL_SH
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/*
858c2ecf20Sopenharmony_ci * Layout of constraint bits:
868c2ecf20Sopenharmony_ci * 6666555555555544444444443333333333222222222211111111110000000000
878c2ecf20Sopenharmony_ci * 3210987654321098765432109876543210987654321098765432109876543210
888c2ecf20Sopenharmony_ci *               <><><>[  >[  >[  ><  ><  ><  ><  ><><><><><><><><>
898c2ecf20Sopenharmony_ci *               SPT0T1 UC  PS1 PS2 B0  B1  B2  B3 P1P2P3P4P5P6P7P8
908c2ecf20Sopenharmony_ci *
918c2ecf20Sopenharmony_ci * SP - SPCSEL constraint
928c2ecf20Sopenharmony_ci *     48-49: SPCSEL value 0x3_0000_0000_0000
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci * T0 - TTM0 constraint
958c2ecf20Sopenharmony_ci *     46-47: TTM0SEL value (0=FPU, 2=IFU, 3=VPU) 0xC000_0000_0000
968c2ecf20Sopenharmony_ci *
978c2ecf20Sopenharmony_ci * T1 - TTM1 constraint
988c2ecf20Sopenharmony_ci *     44-45: TTM1SEL value (0=IDU, 3=STS) 0x3000_0000_0000
998c2ecf20Sopenharmony_ci *
1008c2ecf20Sopenharmony_ci * UC - unit constraint: can't have all three of FPU|IFU|VPU, ISU, IDU|STS
1018c2ecf20Sopenharmony_ci *     43: UC3 error 0x0800_0000_0000
1028c2ecf20Sopenharmony_ci *     42: FPU|IFU|VPU events needed 0x0400_0000_0000
1038c2ecf20Sopenharmony_ci *     41: ISU events needed 0x0200_0000_0000
1048c2ecf20Sopenharmony_ci *     40: IDU|STS events needed 0x0100_0000_0000
1058c2ecf20Sopenharmony_ci *
1068c2ecf20Sopenharmony_ci * PS1
1078c2ecf20Sopenharmony_ci *     39: PS1 error 0x0080_0000_0000
1088c2ecf20Sopenharmony_ci *     36-38: count of events needing PMC1/2/5/6 0x0070_0000_0000
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci * PS2
1118c2ecf20Sopenharmony_ci *     35: PS2 error 0x0008_0000_0000
1128c2ecf20Sopenharmony_ci *     32-34: count of events needing PMC3/4/7/8 0x0007_0000_0000
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * B0
1158c2ecf20Sopenharmony_ci *     28-31: Byte 0 event source 0xf000_0000
1168c2ecf20Sopenharmony_ci *	      Encoding as for the event code
1178c2ecf20Sopenharmony_ci *
1188c2ecf20Sopenharmony_ci * B1, B2, B3
1198c2ecf20Sopenharmony_ci *     24-27, 20-23, 16-19: Byte 1, 2, 3 event sources
1208c2ecf20Sopenharmony_ci *
1218c2ecf20Sopenharmony_ci * P1
1228c2ecf20Sopenharmony_ci *     15: P1 error 0x8000
1238c2ecf20Sopenharmony_ci *     14-15: Count of events needing PMC1
1248c2ecf20Sopenharmony_ci *
1258c2ecf20Sopenharmony_ci * P2..P8
1268c2ecf20Sopenharmony_ci *     0-13: Count of events needing PMC2..PMC8
1278c2ecf20Sopenharmony_ci */
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic unsigned char direct_marked_event[8] = {
1308c2ecf20Sopenharmony_ci	(1<<2) | (1<<3),	/* PMC1: PM_MRK_GRP_DISP, PM_MRK_ST_CMPL */
1318c2ecf20Sopenharmony_ci	(1<<3) | (1<<5),	/* PMC2: PM_THRESH_TIMEO, PM_MRK_BRU_FIN */
1328c2ecf20Sopenharmony_ci	(1<<3) | (1<<5),	/* PMC3: PM_MRK_ST_CMPL_INT, PM_MRK_VMX_FIN */
1338c2ecf20Sopenharmony_ci	(1<<4) | (1<<5),	/* PMC4: PM_MRK_GRP_CMPL, PM_MRK_CRU_FIN */
1348c2ecf20Sopenharmony_ci	(1<<4) | (1<<5),	/* PMC5: PM_GRP_MRK, PM_MRK_GRP_TIMEO */
1358c2ecf20Sopenharmony_ci	(1<<3) | (1<<4) | (1<<5),
1368c2ecf20Sopenharmony_ci		/* PMC6: PM_MRK_ST_STS, PM_MRK_FXU_FIN, PM_MRK_GRP_ISSUED */
1378c2ecf20Sopenharmony_ci	(1<<4) | (1<<5),	/* PMC7: PM_MRK_FPU_FIN, PM_MRK_INST_FIN */
1388c2ecf20Sopenharmony_ci	(1<<4)			/* PMC8: PM_MRK_LSU_FIN */
1398c2ecf20Sopenharmony_ci};
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci/*
1428c2ecf20Sopenharmony_ci * Returns 1 if event counts things relating to marked instructions
1438c2ecf20Sopenharmony_ci * and thus needs the MMCRA_SAMPLE_ENABLE bit set, or 0 if not.
1448c2ecf20Sopenharmony_ci */
1458c2ecf20Sopenharmony_cistatic int p970_marked_instr_event(u64 event)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	int pmc, psel, unit, byte, bit;
1488c2ecf20Sopenharmony_ci	unsigned int mask;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
1518c2ecf20Sopenharmony_ci	psel = event & PM_PMCSEL_MSK;
1528c2ecf20Sopenharmony_ci	if (pmc) {
1538c2ecf20Sopenharmony_ci		if (direct_marked_event[pmc - 1] & (1 << psel))
1548c2ecf20Sopenharmony_ci			return 1;
1558c2ecf20Sopenharmony_ci		if (psel == 0)		/* add events */
1568c2ecf20Sopenharmony_ci			bit = (pmc <= 4)? pmc - 1: 8 - pmc;
1578c2ecf20Sopenharmony_ci		else if (psel == 7 || psel == 13)	/* decode events */
1588c2ecf20Sopenharmony_ci			bit = 4;
1598c2ecf20Sopenharmony_ci		else
1608c2ecf20Sopenharmony_ci			return 0;
1618c2ecf20Sopenharmony_ci	} else
1628c2ecf20Sopenharmony_ci		bit = psel;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
1658c2ecf20Sopenharmony_ci	unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
1668c2ecf20Sopenharmony_ci	mask = 0;
1678c2ecf20Sopenharmony_ci	switch (unit) {
1688c2ecf20Sopenharmony_ci	case PM_VPU:
1698c2ecf20Sopenharmony_ci		mask = 0x4c;		/* byte 0 bits 2,3,6 */
1708c2ecf20Sopenharmony_ci		break;
1718c2ecf20Sopenharmony_ci	case PM_LSU0:
1728c2ecf20Sopenharmony_ci		/* byte 2 bits 0,2,3,4,6; all of byte 1 */
1738c2ecf20Sopenharmony_ci		mask = 0x085dff00;
1748c2ecf20Sopenharmony_ci		break;
1758c2ecf20Sopenharmony_ci	case PM_LSU1L:
1768c2ecf20Sopenharmony_ci		mask = 0x50 << 24;	/* byte 3 bits 4,6 */
1778c2ecf20Sopenharmony_ci		break;
1788c2ecf20Sopenharmony_ci	}
1798c2ecf20Sopenharmony_ci	return (mask >> (byte * 8 + bit)) & 1;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/* Masks and values for using events from the various units */
1838c2ecf20Sopenharmony_cistatic unsigned long unit_cons[PM_LASTUNIT+1][2] = {
1848c2ecf20Sopenharmony_ci	[PM_FPU] =   { 0xc80000000000ull, 0x040000000000ull },
1858c2ecf20Sopenharmony_ci	[PM_VPU] =   { 0xc80000000000ull, 0xc40000000000ull },
1868c2ecf20Sopenharmony_ci	[PM_ISU] =   { 0x080000000000ull, 0x020000000000ull },
1878c2ecf20Sopenharmony_ci	[PM_IFU] =   { 0xc80000000000ull, 0x840000000000ull },
1888c2ecf20Sopenharmony_ci	[PM_IDU] =   { 0x380000000000ull, 0x010000000000ull },
1898c2ecf20Sopenharmony_ci	[PM_STS] =   { 0x380000000000ull, 0x310000000000ull },
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic int p970_get_constraint(u64 event, unsigned long *maskp,
1938c2ecf20Sopenharmony_ci			       unsigned long *valp)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	int pmc, byte, unit, sh, spcsel;
1968c2ecf20Sopenharmony_ci	unsigned long mask = 0, value = 0;
1978c2ecf20Sopenharmony_ci	int grp = -1;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
2008c2ecf20Sopenharmony_ci	if (pmc) {
2018c2ecf20Sopenharmony_ci		if (pmc > 8)
2028c2ecf20Sopenharmony_ci			return -1;
2038c2ecf20Sopenharmony_ci		sh = (pmc - 1) * 2;
2048c2ecf20Sopenharmony_ci		mask |= 2 << sh;
2058c2ecf20Sopenharmony_ci		value |= 1 << sh;
2068c2ecf20Sopenharmony_ci		grp = ((pmc - 1) >> 1) & 1;
2078c2ecf20Sopenharmony_ci	}
2088c2ecf20Sopenharmony_ci	unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
2098c2ecf20Sopenharmony_ci	if (unit) {
2108c2ecf20Sopenharmony_ci		if (unit > PM_LASTUNIT)
2118c2ecf20Sopenharmony_ci			return -1;
2128c2ecf20Sopenharmony_ci		mask |= unit_cons[unit][0];
2138c2ecf20Sopenharmony_ci		value |= unit_cons[unit][1];
2148c2ecf20Sopenharmony_ci		byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
2158c2ecf20Sopenharmony_ci		/*
2168c2ecf20Sopenharmony_ci		 * Bus events on bytes 0 and 2 can be counted
2178c2ecf20Sopenharmony_ci		 * on PMC1/2/5/6; bytes 1 and 3 on PMC3/4/7/8.
2188c2ecf20Sopenharmony_ci		 */
2198c2ecf20Sopenharmony_ci		if (!pmc)
2208c2ecf20Sopenharmony_ci			grp = byte & 1;
2218c2ecf20Sopenharmony_ci		/* Set byte lane select field */
2228c2ecf20Sopenharmony_ci		mask  |= 0xfULL << (28 - 4 * byte);
2238c2ecf20Sopenharmony_ci		value |= (unsigned long)unit << (28 - 4 * byte);
2248c2ecf20Sopenharmony_ci	}
2258c2ecf20Sopenharmony_ci	if (grp == 0) {
2268c2ecf20Sopenharmony_ci		/* increment PMC1/2/5/6 field */
2278c2ecf20Sopenharmony_ci		mask  |= 0x8000000000ull;
2288c2ecf20Sopenharmony_ci		value |= 0x1000000000ull;
2298c2ecf20Sopenharmony_ci	} else if (grp == 1) {
2308c2ecf20Sopenharmony_ci		/* increment PMC3/4/7/8 field */
2318c2ecf20Sopenharmony_ci		mask  |= 0x800000000ull;
2328c2ecf20Sopenharmony_ci		value |= 0x100000000ull;
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci	spcsel = (event >> PM_SPCSEL_SH) & PM_SPCSEL_MSK;
2358c2ecf20Sopenharmony_ci	if (spcsel) {
2368c2ecf20Sopenharmony_ci		mask  |= 3ull << 48;
2378c2ecf20Sopenharmony_ci		value |= (unsigned long)spcsel << 48;
2388c2ecf20Sopenharmony_ci	}
2398c2ecf20Sopenharmony_ci	*maskp = mask;
2408c2ecf20Sopenharmony_ci	*valp = value;
2418c2ecf20Sopenharmony_ci	return 0;
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic int p970_get_alternatives(u64 event, unsigned int flags, u64 alt[])
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	alt[0] = event;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	/* 2 alternatives for LSU empty */
2498c2ecf20Sopenharmony_ci	if (event == 0x2002 || event == 0x3002) {
2508c2ecf20Sopenharmony_ci		alt[1] = event ^ 0x1000;
2518c2ecf20Sopenharmony_ci		return 2;
2528c2ecf20Sopenharmony_ci	}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	return 1;
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic int p970_compute_mmcr(u64 event[], int n_ev,
2588c2ecf20Sopenharmony_ci			     unsigned int hwc[], struct mmcr_regs *mmcr,
2598c2ecf20Sopenharmony_ci			     struct perf_event *pevents[])
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	unsigned long mmcr0 = 0, mmcr1 = 0, mmcra = 0;
2628c2ecf20Sopenharmony_ci	unsigned int pmc, unit, byte, psel;
2638c2ecf20Sopenharmony_ci	unsigned int ttm, grp;
2648c2ecf20Sopenharmony_ci	unsigned int pmc_inuse = 0;
2658c2ecf20Sopenharmony_ci	unsigned int pmc_grp_use[2];
2668c2ecf20Sopenharmony_ci	unsigned char busbyte[4];
2678c2ecf20Sopenharmony_ci	unsigned char unituse[16];
2688c2ecf20Sopenharmony_ci	unsigned char unitmap[] = { 0, 0<<3, 3<<3, 1<<3, 2<<3, 0|4, 3|4 };
2698c2ecf20Sopenharmony_ci	unsigned char ttmuse[2];
2708c2ecf20Sopenharmony_ci	unsigned char pmcsel[8];
2718c2ecf20Sopenharmony_ci	int i;
2728c2ecf20Sopenharmony_ci	int spcsel;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	if (n_ev > 8)
2758c2ecf20Sopenharmony_ci		return -1;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/* First pass to count resource use */
2788c2ecf20Sopenharmony_ci	pmc_grp_use[0] = pmc_grp_use[1] = 0;
2798c2ecf20Sopenharmony_ci	memset(busbyte, 0, sizeof(busbyte));
2808c2ecf20Sopenharmony_ci	memset(unituse, 0, sizeof(unituse));
2818c2ecf20Sopenharmony_ci	for (i = 0; i < n_ev; ++i) {
2828c2ecf20Sopenharmony_ci		pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
2838c2ecf20Sopenharmony_ci		if (pmc) {
2848c2ecf20Sopenharmony_ci			if (pmc_inuse & (1 << (pmc - 1)))
2858c2ecf20Sopenharmony_ci				return -1;
2868c2ecf20Sopenharmony_ci			pmc_inuse |= 1 << (pmc - 1);
2878c2ecf20Sopenharmony_ci			/* count 1/2/5/6 vs 3/4/7/8 use */
2888c2ecf20Sopenharmony_ci			++pmc_grp_use[((pmc - 1) >> 1) & 1];
2898c2ecf20Sopenharmony_ci		}
2908c2ecf20Sopenharmony_ci		unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
2918c2ecf20Sopenharmony_ci		byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
2928c2ecf20Sopenharmony_ci		if (unit) {
2938c2ecf20Sopenharmony_ci			if (unit > PM_LASTUNIT)
2948c2ecf20Sopenharmony_ci				return -1;
2958c2ecf20Sopenharmony_ci			if (!pmc)
2968c2ecf20Sopenharmony_ci				++pmc_grp_use[byte & 1];
2978c2ecf20Sopenharmony_ci			if (busbyte[byte] && busbyte[byte] != unit)
2988c2ecf20Sopenharmony_ci				return -1;
2998c2ecf20Sopenharmony_ci			busbyte[byte] = unit;
3008c2ecf20Sopenharmony_ci			unituse[unit] = 1;
3018c2ecf20Sopenharmony_ci		}
3028c2ecf20Sopenharmony_ci	}
3038c2ecf20Sopenharmony_ci	if (pmc_grp_use[0] > 4 || pmc_grp_use[1] > 4)
3048c2ecf20Sopenharmony_ci		return -1;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	/*
3078c2ecf20Sopenharmony_ci	 * Assign resources and set multiplexer selects.
3088c2ecf20Sopenharmony_ci	 *
3098c2ecf20Sopenharmony_ci	 * PM_ISU can go either on TTM0 or TTM1, but that's the only
3108c2ecf20Sopenharmony_ci	 * choice we have to deal with.
3118c2ecf20Sopenharmony_ci	 */
3128c2ecf20Sopenharmony_ci	if (unituse[PM_ISU] &
3138c2ecf20Sopenharmony_ci	    (unituse[PM_FPU] | unituse[PM_IFU] | unituse[PM_VPU]))
3148c2ecf20Sopenharmony_ci		unitmap[PM_ISU] = 2 | 4;	/* move ISU to TTM1 */
3158c2ecf20Sopenharmony_ci	/* Set TTM[01]SEL fields. */
3168c2ecf20Sopenharmony_ci	ttmuse[0] = ttmuse[1] = 0;
3178c2ecf20Sopenharmony_ci	for (i = PM_FPU; i <= PM_STS; ++i) {
3188c2ecf20Sopenharmony_ci		if (!unituse[i])
3198c2ecf20Sopenharmony_ci			continue;
3208c2ecf20Sopenharmony_ci		ttm = unitmap[i];
3218c2ecf20Sopenharmony_ci		++ttmuse[(ttm >> 2) & 1];
3228c2ecf20Sopenharmony_ci		mmcr1 |= (unsigned long)(ttm & ~4) << MMCR1_TTM1SEL_SH;
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci	/* Check only one unit per TTMx */
3258c2ecf20Sopenharmony_ci	if (ttmuse[0] > 1 || ttmuse[1] > 1)
3268c2ecf20Sopenharmony_ci		return -1;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	/* Set byte lane select fields and TTM3SEL. */
3298c2ecf20Sopenharmony_ci	for (byte = 0; byte < 4; ++byte) {
3308c2ecf20Sopenharmony_ci		unit = busbyte[byte];
3318c2ecf20Sopenharmony_ci		if (!unit)
3328c2ecf20Sopenharmony_ci			continue;
3338c2ecf20Sopenharmony_ci		if (unit <= PM_STS)
3348c2ecf20Sopenharmony_ci			ttm = (unitmap[unit] >> 2) & 1;
3358c2ecf20Sopenharmony_ci		else if (unit == PM_LSU0)
3368c2ecf20Sopenharmony_ci			ttm = 2;
3378c2ecf20Sopenharmony_ci		else {
3388c2ecf20Sopenharmony_ci			ttm = 3;
3398c2ecf20Sopenharmony_ci			if (unit == PM_LSU1L && byte >= 2)
3408c2ecf20Sopenharmony_ci				mmcr1 |= 1ull << (MMCR1_TTM3SEL_SH + 3 - byte);
3418c2ecf20Sopenharmony_ci		}
3428c2ecf20Sopenharmony_ci		mmcr1 |= (unsigned long)ttm
3438c2ecf20Sopenharmony_ci			<< (MMCR1_TD_CP_DBG0SEL_SH - 2 * byte);
3448c2ecf20Sopenharmony_ci	}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	/* Second pass: assign PMCs, set PMCxSEL and PMCx_ADDER_SEL fields */
3478c2ecf20Sopenharmony_ci	memset(pmcsel, 0x8, sizeof(pmcsel));	/* 8 means don't count */
3488c2ecf20Sopenharmony_ci	for (i = 0; i < n_ev; ++i) {
3498c2ecf20Sopenharmony_ci		pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
3508c2ecf20Sopenharmony_ci		unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
3518c2ecf20Sopenharmony_ci		byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
3528c2ecf20Sopenharmony_ci		psel = event[i] & PM_PMCSEL_MSK;
3538c2ecf20Sopenharmony_ci		if (!pmc) {
3548c2ecf20Sopenharmony_ci			/* Bus event or any-PMC direct event */
3558c2ecf20Sopenharmony_ci			if (unit)
3568c2ecf20Sopenharmony_ci				psel |= 0x10 | ((byte & 2) << 2);
3578c2ecf20Sopenharmony_ci			else
3588c2ecf20Sopenharmony_ci				psel |= 8;
3598c2ecf20Sopenharmony_ci			for (pmc = 0; pmc < 8; ++pmc) {
3608c2ecf20Sopenharmony_ci				if (pmc_inuse & (1 << pmc))
3618c2ecf20Sopenharmony_ci					continue;
3628c2ecf20Sopenharmony_ci				grp = (pmc >> 1) & 1;
3638c2ecf20Sopenharmony_ci				if (unit) {
3648c2ecf20Sopenharmony_ci					if (grp == (byte & 1))
3658c2ecf20Sopenharmony_ci						break;
3668c2ecf20Sopenharmony_ci				} else if (pmc_grp_use[grp] < 4) {
3678c2ecf20Sopenharmony_ci					++pmc_grp_use[grp];
3688c2ecf20Sopenharmony_ci					break;
3698c2ecf20Sopenharmony_ci				}
3708c2ecf20Sopenharmony_ci			}
3718c2ecf20Sopenharmony_ci			pmc_inuse |= 1 << pmc;
3728c2ecf20Sopenharmony_ci		} else {
3738c2ecf20Sopenharmony_ci			/* Direct event */
3748c2ecf20Sopenharmony_ci			--pmc;
3758c2ecf20Sopenharmony_ci			if (psel == 0 && (byte & 2))
3768c2ecf20Sopenharmony_ci				/* add events on higher-numbered bus */
3778c2ecf20Sopenharmony_ci				mmcr1 |= 1ull << mmcr1_adder_bits[pmc];
3788c2ecf20Sopenharmony_ci		}
3798c2ecf20Sopenharmony_ci		pmcsel[pmc] = psel;
3808c2ecf20Sopenharmony_ci		hwc[i] = pmc;
3818c2ecf20Sopenharmony_ci		spcsel = (event[i] >> PM_SPCSEL_SH) & PM_SPCSEL_MSK;
3828c2ecf20Sopenharmony_ci		mmcr1 |= spcsel;
3838c2ecf20Sopenharmony_ci		if (p970_marked_instr_event(event[i]))
3848c2ecf20Sopenharmony_ci			mmcra |= MMCRA_SAMPLE_ENABLE;
3858c2ecf20Sopenharmony_ci	}
3868c2ecf20Sopenharmony_ci	for (pmc = 0; pmc < 2; ++pmc)
3878c2ecf20Sopenharmony_ci		mmcr0 |= pmcsel[pmc] << (MMCR0_PMC1SEL_SH - 7 * pmc);
3888c2ecf20Sopenharmony_ci	for (; pmc < 8; ++pmc)
3898c2ecf20Sopenharmony_ci		mmcr1 |= (unsigned long)pmcsel[pmc]
3908c2ecf20Sopenharmony_ci			<< (MMCR1_PMC3SEL_SH - 5 * (pmc - 2));
3918c2ecf20Sopenharmony_ci	if (pmc_inuse & 1)
3928c2ecf20Sopenharmony_ci		mmcr0 |= MMCR0_PMC1CE;
3938c2ecf20Sopenharmony_ci	if (pmc_inuse & 0xfe)
3948c2ecf20Sopenharmony_ci		mmcr0 |= MMCR0_PMCjCE;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	mmcra |= 0x2000;	/* mark only one IOP per PPC instruction */
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	/* Return MMCRx values */
3998c2ecf20Sopenharmony_ci	mmcr->mmcr0 = mmcr0;
4008c2ecf20Sopenharmony_ci	mmcr->mmcr1 = mmcr1;
4018c2ecf20Sopenharmony_ci	mmcr->mmcra = mmcra;
4028c2ecf20Sopenharmony_ci	return 0;
4038c2ecf20Sopenharmony_ci}
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_cistatic void p970_disable_pmc(unsigned int pmc, struct mmcr_regs *mmcr)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	int shift;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	/*
4108c2ecf20Sopenharmony_ci	 * Setting the PMCxSEL field to 0x08 disables PMC x.
4118c2ecf20Sopenharmony_ci	 */
4128c2ecf20Sopenharmony_ci	if (pmc <= 1) {
4138c2ecf20Sopenharmony_ci		shift = MMCR0_PMC1SEL_SH - 7 * pmc;
4148c2ecf20Sopenharmony_ci		mmcr->mmcr0 = (mmcr->mmcr0 & ~(0x1fUL << shift)) | (0x08UL << shift);
4158c2ecf20Sopenharmony_ci	} else {
4168c2ecf20Sopenharmony_ci		shift = MMCR1_PMC3SEL_SH - 5 * (pmc - 2);
4178c2ecf20Sopenharmony_ci		mmcr->mmcr1 = (mmcr->mmcr1 & ~(0x1fUL << shift)) | (0x08UL << shift);
4188c2ecf20Sopenharmony_ci	}
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_cistatic int ppc970_generic_events[] = {
4228c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CPU_CYCLES]		= 7,
4238c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_INSTRUCTIONS]		= 1,
4248c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CACHE_REFERENCES]	= 0x8810, /* PM_LD_REF_L1 */
4258c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CACHE_MISSES]		= 0x3810, /* PM_LD_MISS_L1 */
4268c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS]	= 0x431,  /* PM_BR_ISSUED */
4278c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_MISSES] 		= 0x327,  /* PM_GRP_BR_MPRED */
4288c2ecf20Sopenharmony_ci};
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci#define C(x)	PERF_COUNT_HW_CACHE_##x
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci/*
4338c2ecf20Sopenharmony_ci * Table of generalized cache-related events.
4348c2ecf20Sopenharmony_ci * 0 means not supported, -1 means nonsensical, other values
4358c2ecf20Sopenharmony_ci * are event codes.
4368c2ecf20Sopenharmony_ci */
4378c2ecf20Sopenharmony_cistatic u64 ppc970_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
4388c2ecf20Sopenharmony_ci	[C(L1D)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
4398c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0x8810,		0x3810	},
4408c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	0x7810,		0x813	},
4418c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0x731,		0	},
4428c2ecf20Sopenharmony_ci	},
4438c2ecf20Sopenharmony_ci	[C(L1I)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
4448c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0	},
4458c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
4468c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0,		0	},
4478c2ecf20Sopenharmony_ci	},
4488c2ecf20Sopenharmony_ci	[C(LL)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
4498c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0	},
4508c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	0,		0	},
4518c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0x733,		0	},
4528c2ecf20Sopenharmony_ci	},
4538c2ecf20Sopenharmony_ci	[C(DTLB)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
4548c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0x704	},
4558c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
4568c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1	},
4578c2ecf20Sopenharmony_ci	},
4588c2ecf20Sopenharmony_ci	[C(ITLB)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
4598c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0x700	},
4608c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
4618c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1	},
4628c2ecf20Sopenharmony_ci	},
4638c2ecf20Sopenharmony_ci	[C(BPU)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
4648c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0x431,		0x327	},
4658c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
4668c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1	},
4678c2ecf20Sopenharmony_ci	},
4688c2ecf20Sopenharmony_ci	[C(NODE)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
4698c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	-1,		-1	},
4708c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1	},
4718c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1	},
4728c2ecf20Sopenharmony_ci	},
4738c2ecf20Sopenharmony_ci};
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_cistatic struct power_pmu ppc970_pmu = {
4768c2ecf20Sopenharmony_ci	.name			= "PPC970/FX/MP",
4778c2ecf20Sopenharmony_ci	.n_counter		= 8,
4788c2ecf20Sopenharmony_ci	.max_alternatives	= 2,
4798c2ecf20Sopenharmony_ci	.add_fields		= 0x001100005555ull,
4808c2ecf20Sopenharmony_ci	.test_adder		= 0x013300000000ull,
4818c2ecf20Sopenharmony_ci	.compute_mmcr		= p970_compute_mmcr,
4828c2ecf20Sopenharmony_ci	.get_constraint		= p970_get_constraint,
4838c2ecf20Sopenharmony_ci	.get_alternatives	= p970_get_alternatives,
4848c2ecf20Sopenharmony_ci	.disable_pmc		= p970_disable_pmc,
4858c2ecf20Sopenharmony_ci	.n_generic		= ARRAY_SIZE(ppc970_generic_events),
4868c2ecf20Sopenharmony_ci	.generic_events		= ppc970_generic_events,
4878c2ecf20Sopenharmony_ci	.cache_events		= &ppc970_cache_events,
4888c2ecf20Sopenharmony_ci	.flags			= PPMU_NO_SIPR | PPMU_NO_CONT_SAMPLING,
4898c2ecf20Sopenharmony_ci};
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ciint init_ppc970_pmu(void)
4928c2ecf20Sopenharmony_ci{
4938c2ecf20Sopenharmony_ci	if (!cur_cpu_spec->oprofile_cpu_type ||
4948c2ecf20Sopenharmony_ci	    (strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/970")
4958c2ecf20Sopenharmony_ci	     && strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/970MP")))
4968c2ecf20Sopenharmony_ci		return -ENODEV;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	return register_power_pmu(&ppc970_pmu);
4998c2ecf20Sopenharmony_ci}
500