18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Performance counter support for POWER5+/++ (not POWER5) 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 POWER5+ (POWER5 GS) and POWER5++ (POWER5 GS DD3)
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci#define PM_PMC_SH	20	/* 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	16	/* TTMMUX number and setting - unit select */
228c2ecf20Sopenharmony_ci#define PM_UNIT_MSK	0xf
238c2ecf20Sopenharmony_ci#define PM_BYTE_SH	12	/* Byte number of event bus to use */
248c2ecf20Sopenharmony_ci#define PM_BYTE_MSK	7
258c2ecf20Sopenharmony_ci#define PM_GRS_SH	8	/* Storage subsystem mux select */
268c2ecf20Sopenharmony_ci#define PM_GRS_MSK	7
278c2ecf20Sopenharmony_ci#define PM_BUSEVENT_MSK	0x80	/* Set if event uses event bus */
288c2ecf20Sopenharmony_ci#define PM_PMCSEL_MSK	0x7f
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* Values in PM_UNIT field */
318c2ecf20Sopenharmony_ci#define PM_FPU		0
328c2ecf20Sopenharmony_ci#define PM_ISU0		1
338c2ecf20Sopenharmony_ci#define PM_IFU		2
348c2ecf20Sopenharmony_ci#define PM_ISU1		3
358c2ecf20Sopenharmony_ci#define PM_IDU		4
368c2ecf20Sopenharmony_ci#define PM_ISU0_ALT	6
378c2ecf20Sopenharmony_ci#define PM_GRS		7
388c2ecf20Sopenharmony_ci#define PM_LSU0		8
398c2ecf20Sopenharmony_ci#define PM_LSU1		0xc
408c2ecf20Sopenharmony_ci#define PM_LASTUNIT	0xc
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/*
438c2ecf20Sopenharmony_ci * Bits in MMCR1 for POWER5+
448c2ecf20Sopenharmony_ci */
458c2ecf20Sopenharmony_ci#define MMCR1_TTM0SEL_SH	62
468c2ecf20Sopenharmony_ci#define MMCR1_TTM1SEL_SH	60
478c2ecf20Sopenharmony_ci#define MMCR1_TTM2SEL_SH	58
488c2ecf20Sopenharmony_ci#define MMCR1_TTM3SEL_SH	56
498c2ecf20Sopenharmony_ci#define MMCR1_TTMSEL_MSK	3
508c2ecf20Sopenharmony_ci#define MMCR1_TD_CP_DBG0SEL_SH	54
518c2ecf20Sopenharmony_ci#define MMCR1_TD_CP_DBG1SEL_SH	52
528c2ecf20Sopenharmony_ci#define MMCR1_TD_CP_DBG2SEL_SH	50
538c2ecf20Sopenharmony_ci#define MMCR1_TD_CP_DBG3SEL_SH	48
548c2ecf20Sopenharmony_ci#define MMCR1_GRS_L2SEL_SH	46
558c2ecf20Sopenharmony_ci#define MMCR1_GRS_L2SEL_MSK	3
568c2ecf20Sopenharmony_ci#define MMCR1_GRS_L3SEL_SH	44
578c2ecf20Sopenharmony_ci#define MMCR1_GRS_L3SEL_MSK	3
588c2ecf20Sopenharmony_ci#define MMCR1_GRS_MCSEL_SH	41
598c2ecf20Sopenharmony_ci#define MMCR1_GRS_MCSEL_MSK	7
608c2ecf20Sopenharmony_ci#define MMCR1_GRS_FABSEL_SH	39
618c2ecf20Sopenharmony_ci#define MMCR1_GRS_FABSEL_MSK	3
628c2ecf20Sopenharmony_ci#define MMCR1_PMC1_ADDER_SEL_SH	35
638c2ecf20Sopenharmony_ci#define MMCR1_PMC2_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_PMC1SEL_SH	25
678c2ecf20Sopenharmony_ci#define MMCR1_PMC2SEL_SH	17
688c2ecf20Sopenharmony_ci#define MMCR1_PMC3SEL_SH	9
698c2ecf20Sopenharmony_ci#define MMCR1_PMC4SEL_SH	1
708c2ecf20Sopenharmony_ci#define MMCR1_PMCSEL_SH(n)	(MMCR1_PMC1SEL_SH - (n) * 8)
718c2ecf20Sopenharmony_ci#define MMCR1_PMCSEL_MSK	0x7f
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/*
748c2ecf20Sopenharmony_ci * Layout of constraint bits:
758c2ecf20Sopenharmony_ci * 6666555555555544444444443333333333222222222211111111110000000000
768c2ecf20Sopenharmony_ci * 3210987654321098765432109876543210987654321098765432109876543210
778c2ecf20Sopenharmony_ci *             [  ><><>< ><> <><>[  >  <  ><  ><  ><  ><><><><><><>
788c2ecf20Sopenharmony_ci *             NC  G0G1G2 G3 T0T1 UC    B0  B1  B2  B3 P6P5P4P3P2P1
798c2ecf20Sopenharmony_ci *
808c2ecf20Sopenharmony_ci * NC - number of counters
818c2ecf20Sopenharmony_ci *     51: NC error 0x0008_0000_0000_0000
828c2ecf20Sopenharmony_ci *     48-50: number of events needing PMC1-4 0x0007_0000_0000_0000
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * G0..G3 - GRS mux constraints
858c2ecf20Sopenharmony_ci *     46-47: GRS_L2SEL value
868c2ecf20Sopenharmony_ci *     44-45: GRS_L3SEL value
878c2ecf20Sopenharmony_ci *     41-44: GRS_MCSEL value
888c2ecf20Sopenharmony_ci *     39-40: GRS_FABSEL value
898c2ecf20Sopenharmony_ci *	Note that these match up with their bit positions in MMCR1
908c2ecf20Sopenharmony_ci *
918c2ecf20Sopenharmony_ci * T0 - TTM0 constraint
928c2ecf20Sopenharmony_ci *     36-37: TTM0SEL value (0=FPU, 2=IFU, 3=ISU1) 0x30_0000_0000
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci * T1 - TTM1 constraint
958c2ecf20Sopenharmony_ci *     34-35: TTM1SEL value (0=IDU, 3=GRS) 0x0c_0000_0000
968c2ecf20Sopenharmony_ci *
978c2ecf20Sopenharmony_ci * UC - unit constraint: can't have all three of FPU|IFU|ISU1, ISU0, IDU|GRS
988c2ecf20Sopenharmony_ci *     33: UC3 error 0x02_0000_0000
998c2ecf20Sopenharmony_ci *     32: FPU|IFU|ISU1 events needed 0x01_0000_0000
1008c2ecf20Sopenharmony_ci *     31: ISU0 events needed 0x01_8000_0000
1018c2ecf20Sopenharmony_ci *     30: IDU|GRS events needed 0x00_4000_0000
1028c2ecf20Sopenharmony_ci *
1038c2ecf20Sopenharmony_ci * B0
1048c2ecf20Sopenharmony_ci *     24-27: Byte 0 event source 0x0f00_0000
1058c2ecf20Sopenharmony_ci *	      Encoding as for the event code
1068c2ecf20Sopenharmony_ci *
1078c2ecf20Sopenharmony_ci * B1, B2, B3
1088c2ecf20Sopenharmony_ci *     20-23, 16-19, 12-15: Byte 1, 2, 3 event sources
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci * P6
1118c2ecf20Sopenharmony_ci *     11: P6 error 0x800
1128c2ecf20Sopenharmony_ci *     10-11: Count of events needing PMC6
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * P1..P5
1158c2ecf20Sopenharmony_ci *     0-9: Count of events needing PMC1..PMC5
1168c2ecf20Sopenharmony_ci */
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic const int grsel_shift[8] = {
1198c2ecf20Sopenharmony_ci	MMCR1_GRS_L2SEL_SH, MMCR1_GRS_L2SEL_SH, MMCR1_GRS_L2SEL_SH,
1208c2ecf20Sopenharmony_ci	MMCR1_GRS_L3SEL_SH, MMCR1_GRS_L3SEL_SH, MMCR1_GRS_L3SEL_SH,
1218c2ecf20Sopenharmony_ci	MMCR1_GRS_MCSEL_SH, MMCR1_GRS_FABSEL_SH
1228c2ecf20Sopenharmony_ci};
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci/* Masks and values for using events from the various units */
1258c2ecf20Sopenharmony_cistatic unsigned long unit_cons[PM_LASTUNIT+1][2] = {
1268c2ecf20Sopenharmony_ci	[PM_FPU] =   { 0x3200000000ul, 0x0100000000ul },
1278c2ecf20Sopenharmony_ci	[PM_ISU0] =  { 0x0200000000ul, 0x0080000000ul },
1288c2ecf20Sopenharmony_ci	[PM_ISU1] =  { 0x3200000000ul, 0x3100000000ul },
1298c2ecf20Sopenharmony_ci	[PM_IFU] =   { 0x3200000000ul, 0x2100000000ul },
1308c2ecf20Sopenharmony_ci	[PM_IDU] =   { 0x0e00000000ul, 0x0040000000ul },
1318c2ecf20Sopenharmony_ci	[PM_GRS] =   { 0x0e00000000ul, 0x0c40000000ul },
1328c2ecf20Sopenharmony_ci};
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic int power5p_get_constraint(u64 event, unsigned long *maskp,
1358c2ecf20Sopenharmony_ci				  unsigned long *valp)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	int pmc, byte, unit, sh;
1388c2ecf20Sopenharmony_ci	int bit, fmask;
1398c2ecf20Sopenharmony_ci	unsigned long mask = 0, value = 0;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
1428c2ecf20Sopenharmony_ci	if (pmc) {
1438c2ecf20Sopenharmony_ci		if (pmc > 6)
1448c2ecf20Sopenharmony_ci			return -1;
1458c2ecf20Sopenharmony_ci		sh = (pmc - 1) * 2;
1468c2ecf20Sopenharmony_ci		mask |= 2 << sh;
1478c2ecf20Sopenharmony_ci		value |= 1 << sh;
1488c2ecf20Sopenharmony_ci		if (pmc >= 5 && !(event == 0x500009 || event == 0x600005))
1498c2ecf20Sopenharmony_ci			return -1;
1508c2ecf20Sopenharmony_ci	}
1518c2ecf20Sopenharmony_ci	if (event & PM_BUSEVENT_MSK) {
1528c2ecf20Sopenharmony_ci		unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
1538c2ecf20Sopenharmony_ci		if (unit > PM_LASTUNIT)
1548c2ecf20Sopenharmony_ci			return -1;
1558c2ecf20Sopenharmony_ci		if (unit == PM_ISU0_ALT)
1568c2ecf20Sopenharmony_ci			unit = PM_ISU0;
1578c2ecf20Sopenharmony_ci		mask |= unit_cons[unit][0];
1588c2ecf20Sopenharmony_ci		value |= unit_cons[unit][1];
1598c2ecf20Sopenharmony_ci		byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
1608c2ecf20Sopenharmony_ci		if (byte >= 4) {
1618c2ecf20Sopenharmony_ci			if (unit != PM_LSU1)
1628c2ecf20Sopenharmony_ci				return -1;
1638c2ecf20Sopenharmony_ci			/* Map LSU1 low word (bytes 4-7) to unit LSU1+1 */
1648c2ecf20Sopenharmony_ci			++unit;
1658c2ecf20Sopenharmony_ci			byte &= 3;
1668c2ecf20Sopenharmony_ci		}
1678c2ecf20Sopenharmony_ci		if (unit == PM_GRS) {
1688c2ecf20Sopenharmony_ci			bit = event & 7;
1698c2ecf20Sopenharmony_ci			fmask = (bit == 6)? 7: 3;
1708c2ecf20Sopenharmony_ci			sh = grsel_shift[bit];
1718c2ecf20Sopenharmony_ci			mask |= (unsigned long)fmask << sh;
1728c2ecf20Sopenharmony_ci			value |= (unsigned long)((event >> PM_GRS_SH) & fmask)
1738c2ecf20Sopenharmony_ci				<< sh;
1748c2ecf20Sopenharmony_ci		}
1758c2ecf20Sopenharmony_ci		/* Set byte lane select field */
1768c2ecf20Sopenharmony_ci		mask  |= 0xfUL << (24 - 4 * byte);
1778c2ecf20Sopenharmony_ci		value |= (unsigned long)unit << (24 - 4 * byte);
1788c2ecf20Sopenharmony_ci	}
1798c2ecf20Sopenharmony_ci	if (pmc < 5) {
1808c2ecf20Sopenharmony_ci		/* need a counter from PMC1-4 set */
1818c2ecf20Sopenharmony_ci		mask  |= 0x8000000000000ul;
1828c2ecf20Sopenharmony_ci		value |= 0x1000000000000ul;
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci	*maskp = mask;
1858c2ecf20Sopenharmony_ci	*valp = value;
1868c2ecf20Sopenharmony_ci	return 0;
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic int power5p_limited_pmc_event(u64 event)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	int pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	return pmc == 5 || pmc == 6;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci#define MAX_ALT	3	/* at most 3 alternatives for any event */
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic const unsigned int event_alternatives[][MAX_ALT] = {
1998c2ecf20Sopenharmony_ci	{ 0x100c0,  0x40001f },			/* PM_GCT_FULL_CYC */
2008c2ecf20Sopenharmony_ci	{ 0x120e4,  0x400002 },			/* PM_GRP_DISP_REJECT */
2018c2ecf20Sopenharmony_ci	{ 0x230e2,  0x323087 },			/* PM_BR_PRED_CR */
2028c2ecf20Sopenharmony_ci	{ 0x230e3,  0x223087, 0x3230a0 },	/* PM_BR_PRED_TA */
2038c2ecf20Sopenharmony_ci	{ 0x410c7,  0x441084 },			/* PM_THRD_L2MISS_BOTH_CYC */
2048c2ecf20Sopenharmony_ci	{ 0x800c4,  0xc20e0 },			/* PM_DTLB_MISS */
2058c2ecf20Sopenharmony_ci	{ 0xc50c6,  0xc60e0 },			/* PM_MRK_DTLB_MISS */
2068c2ecf20Sopenharmony_ci	{ 0x100005, 0x600005 },			/* PM_RUN_CYC */
2078c2ecf20Sopenharmony_ci	{ 0x100009, 0x200009 },			/* PM_INST_CMPL */
2088c2ecf20Sopenharmony_ci	{ 0x200015, 0x300015 },			/* PM_LSU_LMQ_SRQ_EMPTY_CYC */
2098c2ecf20Sopenharmony_ci	{ 0x300009, 0x400009 },			/* PM_INST_DISP */
2108c2ecf20Sopenharmony_ci};
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci/*
2138c2ecf20Sopenharmony_ci * Scan the alternatives table for a match and return the
2148c2ecf20Sopenharmony_ci * index into the alternatives table if found, else -1.
2158c2ecf20Sopenharmony_ci */
2168c2ecf20Sopenharmony_cistatic int find_alternative(unsigned int event)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	int i, j;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(event_alternatives); ++i) {
2218c2ecf20Sopenharmony_ci		if (event < event_alternatives[i][0])
2228c2ecf20Sopenharmony_ci			break;
2238c2ecf20Sopenharmony_ci		for (j = 0; j < MAX_ALT && event_alternatives[i][j]; ++j)
2248c2ecf20Sopenharmony_ci			if (event == event_alternatives[i][j])
2258c2ecf20Sopenharmony_ci				return i;
2268c2ecf20Sopenharmony_ci	}
2278c2ecf20Sopenharmony_ci	return -1;
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic const unsigned char bytedecode_alternatives[4][4] = {
2318c2ecf20Sopenharmony_ci	/* PMC 1 */	{ 0x21, 0x23, 0x25, 0x27 },
2328c2ecf20Sopenharmony_ci	/* PMC 2 */	{ 0x07, 0x17, 0x0e, 0x1e },
2338c2ecf20Sopenharmony_ci	/* PMC 3 */	{ 0x20, 0x22, 0x24, 0x26 },
2348c2ecf20Sopenharmony_ci	/* PMC 4 */	{ 0x07, 0x17, 0x0e, 0x1e }
2358c2ecf20Sopenharmony_ci};
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci/*
2388c2ecf20Sopenharmony_ci * Some direct events for decodes of event bus byte 3 have alternative
2398c2ecf20Sopenharmony_ci * PMCSEL values on other counters.  This returns the alternative
2408c2ecf20Sopenharmony_ci * event code for those that do, or -1 otherwise.  This also handles
2418c2ecf20Sopenharmony_ci * alternative PCMSEL values for add events.
2428c2ecf20Sopenharmony_ci */
2438c2ecf20Sopenharmony_cistatic s64 find_alternative_bdecode(u64 event)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	int pmc, altpmc, pp, j;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
2488c2ecf20Sopenharmony_ci	if (pmc == 0 || pmc > 4)
2498c2ecf20Sopenharmony_ci		return -1;
2508c2ecf20Sopenharmony_ci	altpmc = 5 - pmc;	/* 1 <-> 4, 2 <-> 3 */
2518c2ecf20Sopenharmony_ci	pp = event & PM_PMCSEL_MSK;
2528c2ecf20Sopenharmony_ci	for (j = 0; j < 4; ++j) {
2538c2ecf20Sopenharmony_ci		if (bytedecode_alternatives[pmc - 1][j] == pp) {
2548c2ecf20Sopenharmony_ci			return (event & ~(PM_PMC_MSKS | PM_PMCSEL_MSK)) |
2558c2ecf20Sopenharmony_ci				(altpmc << PM_PMC_SH) |
2568c2ecf20Sopenharmony_ci				bytedecode_alternatives[altpmc - 1][j];
2578c2ecf20Sopenharmony_ci		}
2588c2ecf20Sopenharmony_ci	}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	/* new decode alternatives for power5+ */
2618c2ecf20Sopenharmony_ci	if (pmc == 1 && (pp == 0x0d || pp == 0x0e))
2628c2ecf20Sopenharmony_ci		return event + (2 << PM_PMC_SH) + (0x2e - 0x0d);
2638c2ecf20Sopenharmony_ci	if (pmc == 3 && (pp == 0x2e || pp == 0x2f))
2648c2ecf20Sopenharmony_ci		return event - (2 << PM_PMC_SH) - (0x2e - 0x0d);
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	/* alternative add event encodings */
2678c2ecf20Sopenharmony_ci	if (pp == 0x10 || pp == 0x28)
2688c2ecf20Sopenharmony_ci		return ((event ^ (0x10 ^ 0x28)) & ~PM_PMC_MSKS) |
2698c2ecf20Sopenharmony_ci			(altpmc << PM_PMC_SH);
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	return -1;
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistatic int power5p_get_alternatives(u64 event, unsigned int flags, u64 alt[])
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	int i, j, nalt = 1;
2778c2ecf20Sopenharmony_ci	int nlim;
2788c2ecf20Sopenharmony_ci	s64 ae;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	alt[0] = event;
2818c2ecf20Sopenharmony_ci	nalt = 1;
2828c2ecf20Sopenharmony_ci	nlim = power5p_limited_pmc_event(event);
2838c2ecf20Sopenharmony_ci	i = find_alternative(event);
2848c2ecf20Sopenharmony_ci	if (i >= 0) {
2858c2ecf20Sopenharmony_ci		for (j = 0; j < MAX_ALT; ++j) {
2868c2ecf20Sopenharmony_ci			ae = event_alternatives[i][j];
2878c2ecf20Sopenharmony_ci			if (ae && ae != event)
2888c2ecf20Sopenharmony_ci				alt[nalt++] = ae;
2898c2ecf20Sopenharmony_ci			nlim += power5p_limited_pmc_event(ae);
2908c2ecf20Sopenharmony_ci		}
2918c2ecf20Sopenharmony_ci	} else {
2928c2ecf20Sopenharmony_ci		ae = find_alternative_bdecode(event);
2938c2ecf20Sopenharmony_ci		if (ae > 0)
2948c2ecf20Sopenharmony_ci			alt[nalt++] = ae;
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	if (flags & PPMU_ONLY_COUNT_RUN) {
2988c2ecf20Sopenharmony_ci		/*
2998c2ecf20Sopenharmony_ci		 * We're only counting in RUN state,
3008c2ecf20Sopenharmony_ci		 * so PM_CYC is equivalent to PM_RUN_CYC
3018c2ecf20Sopenharmony_ci		 * and PM_INST_CMPL === PM_RUN_INST_CMPL.
3028c2ecf20Sopenharmony_ci		 * This doesn't include alternatives that don't provide
3038c2ecf20Sopenharmony_ci		 * any extra flexibility in assigning PMCs (e.g.
3048c2ecf20Sopenharmony_ci		 * 0x100005 for PM_RUN_CYC vs. 0xf for PM_CYC).
3058c2ecf20Sopenharmony_ci		 * Note that even with these additional alternatives
3068c2ecf20Sopenharmony_ci		 * we never end up with more than 3 alternatives for any event.
3078c2ecf20Sopenharmony_ci		 */
3088c2ecf20Sopenharmony_ci		j = nalt;
3098c2ecf20Sopenharmony_ci		for (i = 0; i < nalt; ++i) {
3108c2ecf20Sopenharmony_ci			switch (alt[i]) {
3118c2ecf20Sopenharmony_ci			case 0xf:	/* PM_CYC */
3128c2ecf20Sopenharmony_ci				alt[j++] = 0x600005;	/* PM_RUN_CYC */
3138c2ecf20Sopenharmony_ci				++nlim;
3148c2ecf20Sopenharmony_ci				break;
3158c2ecf20Sopenharmony_ci			case 0x600005:	/* PM_RUN_CYC */
3168c2ecf20Sopenharmony_ci				alt[j++] = 0xf;
3178c2ecf20Sopenharmony_ci				break;
3188c2ecf20Sopenharmony_ci			case 0x100009:	/* PM_INST_CMPL */
3198c2ecf20Sopenharmony_ci				alt[j++] = 0x500009;	/* PM_RUN_INST_CMPL */
3208c2ecf20Sopenharmony_ci				++nlim;
3218c2ecf20Sopenharmony_ci				break;
3228c2ecf20Sopenharmony_ci			case 0x500009:	/* PM_RUN_INST_CMPL */
3238c2ecf20Sopenharmony_ci				alt[j++] = 0x100009;	/* PM_INST_CMPL */
3248c2ecf20Sopenharmony_ci				alt[j++] = 0x200009;
3258c2ecf20Sopenharmony_ci				break;
3268c2ecf20Sopenharmony_ci			}
3278c2ecf20Sopenharmony_ci		}
3288c2ecf20Sopenharmony_ci		nalt = j;
3298c2ecf20Sopenharmony_ci	}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	if (!(flags & PPMU_LIMITED_PMC_OK) && nlim) {
3328c2ecf20Sopenharmony_ci		/* remove the limited PMC events */
3338c2ecf20Sopenharmony_ci		j = 0;
3348c2ecf20Sopenharmony_ci		for (i = 0; i < nalt; ++i) {
3358c2ecf20Sopenharmony_ci			if (!power5p_limited_pmc_event(alt[i])) {
3368c2ecf20Sopenharmony_ci				alt[j] = alt[i];
3378c2ecf20Sopenharmony_ci				++j;
3388c2ecf20Sopenharmony_ci			}
3398c2ecf20Sopenharmony_ci		}
3408c2ecf20Sopenharmony_ci		nalt = j;
3418c2ecf20Sopenharmony_ci	} else if ((flags & PPMU_LIMITED_PMC_REQD) && nlim < nalt) {
3428c2ecf20Sopenharmony_ci		/* remove all but the limited PMC events */
3438c2ecf20Sopenharmony_ci		j = 0;
3448c2ecf20Sopenharmony_ci		for (i = 0; i < nalt; ++i) {
3458c2ecf20Sopenharmony_ci			if (power5p_limited_pmc_event(alt[i])) {
3468c2ecf20Sopenharmony_ci				alt[j] = alt[i];
3478c2ecf20Sopenharmony_ci				++j;
3488c2ecf20Sopenharmony_ci			}
3498c2ecf20Sopenharmony_ci		}
3508c2ecf20Sopenharmony_ci		nalt = j;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	return nalt;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci/*
3578c2ecf20Sopenharmony_ci * Map of which direct events on which PMCs are marked instruction events.
3588c2ecf20Sopenharmony_ci * Indexed by PMCSEL value, bit i (LE) set if PMC i is a marked event.
3598c2ecf20Sopenharmony_ci * Bit 0 is set if it is marked for all PMCs.
3608c2ecf20Sopenharmony_ci * The 0x80 bit indicates a byte decode PMCSEL value.
3618c2ecf20Sopenharmony_ci */
3628c2ecf20Sopenharmony_cistatic unsigned char direct_event_is_marked[0x28] = {
3638c2ecf20Sopenharmony_ci	0,	/* 00 */
3648c2ecf20Sopenharmony_ci	0x1f,	/* 01 PM_IOPS_CMPL */
3658c2ecf20Sopenharmony_ci	0x2,	/* 02 PM_MRK_GRP_DISP */
3668c2ecf20Sopenharmony_ci	0xe,	/* 03 PM_MRK_ST_CMPL, PM_MRK_ST_GPS, PM_MRK_ST_CMPL_INT */
3678c2ecf20Sopenharmony_ci	0,	/* 04 */
3688c2ecf20Sopenharmony_ci	0x1c,	/* 05 PM_MRK_BRU_FIN, PM_MRK_INST_FIN, PM_MRK_CRU_FIN */
3698c2ecf20Sopenharmony_ci	0x80,	/* 06 */
3708c2ecf20Sopenharmony_ci	0x80,	/* 07 */
3718c2ecf20Sopenharmony_ci	0, 0, 0,/* 08 - 0a */
3728c2ecf20Sopenharmony_ci	0x18,	/* 0b PM_THRESH_TIMEO, PM_MRK_GRP_TIMEO */
3738c2ecf20Sopenharmony_ci	0,	/* 0c */
3748c2ecf20Sopenharmony_ci	0x80,	/* 0d */
3758c2ecf20Sopenharmony_ci	0x80,	/* 0e */
3768c2ecf20Sopenharmony_ci	0,	/* 0f */
3778c2ecf20Sopenharmony_ci	0,	/* 10 */
3788c2ecf20Sopenharmony_ci	0x14,	/* 11 PM_MRK_GRP_BR_REDIR, PM_MRK_GRP_IC_MISS */
3798c2ecf20Sopenharmony_ci	0,	/* 12 */
3808c2ecf20Sopenharmony_ci	0x10,	/* 13 PM_MRK_GRP_CMPL */
3818c2ecf20Sopenharmony_ci	0x1f,	/* 14 PM_GRP_MRK, PM_MRK_{FXU,FPU,LSU}_FIN */
3828c2ecf20Sopenharmony_ci	0x2,	/* 15 PM_MRK_GRP_ISSUED */
3838c2ecf20Sopenharmony_ci	0x80,	/* 16 */
3848c2ecf20Sopenharmony_ci	0x80,	/* 17 */
3858c2ecf20Sopenharmony_ci	0, 0, 0, 0, 0,
3868c2ecf20Sopenharmony_ci	0x80,	/* 1d */
3878c2ecf20Sopenharmony_ci	0x80,	/* 1e */
3888c2ecf20Sopenharmony_ci	0,	/* 1f */
3898c2ecf20Sopenharmony_ci	0x80,	/* 20 */
3908c2ecf20Sopenharmony_ci	0x80,	/* 21 */
3918c2ecf20Sopenharmony_ci	0x80,	/* 22 */
3928c2ecf20Sopenharmony_ci	0x80,	/* 23 */
3938c2ecf20Sopenharmony_ci	0x80,	/* 24 */
3948c2ecf20Sopenharmony_ci	0x80,	/* 25 */
3958c2ecf20Sopenharmony_ci	0x80,	/* 26 */
3968c2ecf20Sopenharmony_ci	0x80,	/* 27 */
3978c2ecf20Sopenharmony_ci};
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci/*
4008c2ecf20Sopenharmony_ci * Returns 1 if event counts things relating to marked instructions
4018c2ecf20Sopenharmony_ci * and thus needs the MMCRA_SAMPLE_ENABLE bit set, or 0 if not.
4028c2ecf20Sopenharmony_ci */
4038c2ecf20Sopenharmony_cistatic int power5p_marked_instr_event(u64 event)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	int pmc, psel;
4068c2ecf20Sopenharmony_ci	int bit, byte, unit;
4078c2ecf20Sopenharmony_ci	u32 mask;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
4108c2ecf20Sopenharmony_ci	psel = event & PM_PMCSEL_MSK;
4118c2ecf20Sopenharmony_ci	if (pmc >= 5)
4128c2ecf20Sopenharmony_ci		return 0;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	bit = -1;
4158c2ecf20Sopenharmony_ci	if (psel < sizeof(direct_event_is_marked)) {
4168c2ecf20Sopenharmony_ci		if (direct_event_is_marked[psel] & (1 << pmc))
4178c2ecf20Sopenharmony_ci			return 1;
4188c2ecf20Sopenharmony_ci		if (direct_event_is_marked[psel] & 0x80)
4198c2ecf20Sopenharmony_ci			bit = 4;
4208c2ecf20Sopenharmony_ci		else if (psel == 0x08)
4218c2ecf20Sopenharmony_ci			bit = pmc - 1;
4228c2ecf20Sopenharmony_ci		else if (psel == 0x10)
4238c2ecf20Sopenharmony_ci			bit = 4 - pmc;
4248c2ecf20Sopenharmony_ci		else if (psel == 0x1b && (pmc == 1 || pmc == 3))
4258c2ecf20Sopenharmony_ci			bit = 4;
4268c2ecf20Sopenharmony_ci	} else if ((psel & 0x48) == 0x40) {
4278c2ecf20Sopenharmony_ci		bit = psel & 7;
4288c2ecf20Sopenharmony_ci	} else if (psel == 0x28) {
4298c2ecf20Sopenharmony_ci		bit = pmc - 1;
4308c2ecf20Sopenharmony_ci	} else if (pmc == 3 && (psel == 0x2e || psel == 0x2f)) {
4318c2ecf20Sopenharmony_ci		bit = 4;
4328c2ecf20Sopenharmony_ci	}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	if (!(event & PM_BUSEVENT_MSK) || bit == -1)
4358c2ecf20Sopenharmony_ci		return 0;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
4388c2ecf20Sopenharmony_ci	unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
4398c2ecf20Sopenharmony_ci	if (unit == PM_LSU0) {
4408c2ecf20Sopenharmony_ci		/* byte 1 bits 0-7, byte 2 bits 0,2-4,6 */
4418c2ecf20Sopenharmony_ci		mask = 0x5dff00;
4428c2ecf20Sopenharmony_ci	} else if (unit == PM_LSU1 && byte >= 4) {
4438c2ecf20Sopenharmony_ci		byte -= 4;
4448c2ecf20Sopenharmony_ci		/* byte 5 bits 6-7, byte 6 bits 0,4, byte 7 bits 0-4,6 */
4458c2ecf20Sopenharmony_ci		mask = 0x5f11c000;
4468c2ecf20Sopenharmony_ci	} else
4478c2ecf20Sopenharmony_ci		return 0;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	return (mask >> (byte * 8 + bit)) & 1;
4508c2ecf20Sopenharmony_ci}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_cistatic int power5p_compute_mmcr(u64 event[], int n_ev,
4538c2ecf20Sopenharmony_ci				unsigned int hwc[], struct mmcr_regs *mmcr,
4548c2ecf20Sopenharmony_ci				struct perf_event *pevents[])
4558c2ecf20Sopenharmony_ci{
4568c2ecf20Sopenharmony_ci	unsigned long mmcr1 = 0;
4578c2ecf20Sopenharmony_ci	unsigned long mmcra = 0;
4588c2ecf20Sopenharmony_ci	unsigned int pmc, unit, byte, psel;
4598c2ecf20Sopenharmony_ci	unsigned int ttm;
4608c2ecf20Sopenharmony_ci	int i, isbus, bit, grsel;
4618c2ecf20Sopenharmony_ci	unsigned int pmc_inuse = 0;
4628c2ecf20Sopenharmony_ci	unsigned char busbyte[4];
4638c2ecf20Sopenharmony_ci	unsigned char unituse[16];
4648c2ecf20Sopenharmony_ci	int ttmuse;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if (n_ev > 6)
4678c2ecf20Sopenharmony_ci		return -1;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	/* First pass to count resource use */
4708c2ecf20Sopenharmony_ci	memset(busbyte, 0, sizeof(busbyte));
4718c2ecf20Sopenharmony_ci	memset(unituse, 0, sizeof(unituse));
4728c2ecf20Sopenharmony_ci	for (i = 0; i < n_ev; ++i) {
4738c2ecf20Sopenharmony_ci		pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
4748c2ecf20Sopenharmony_ci		if (pmc) {
4758c2ecf20Sopenharmony_ci			if (pmc > 6)
4768c2ecf20Sopenharmony_ci				return -1;
4778c2ecf20Sopenharmony_ci			if (pmc_inuse & (1 << (pmc - 1)))
4788c2ecf20Sopenharmony_ci				return -1;
4798c2ecf20Sopenharmony_ci			pmc_inuse |= 1 << (pmc - 1);
4808c2ecf20Sopenharmony_ci		}
4818c2ecf20Sopenharmony_ci		if (event[i] & PM_BUSEVENT_MSK) {
4828c2ecf20Sopenharmony_ci			unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
4838c2ecf20Sopenharmony_ci			byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
4848c2ecf20Sopenharmony_ci			if (unit > PM_LASTUNIT)
4858c2ecf20Sopenharmony_ci				return -1;
4868c2ecf20Sopenharmony_ci			if (unit == PM_ISU0_ALT)
4878c2ecf20Sopenharmony_ci				unit = PM_ISU0;
4888c2ecf20Sopenharmony_ci			if (byte >= 4) {
4898c2ecf20Sopenharmony_ci				if (unit != PM_LSU1)
4908c2ecf20Sopenharmony_ci					return -1;
4918c2ecf20Sopenharmony_ci				++unit;
4928c2ecf20Sopenharmony_ci				byte &= 3;
4938c2ecf20Sopenharmony_ci			}
4948c2ecf20Sopenharmony_ci			if (busbyte[byte] && busbyte[byte] != unit)
4958c2ecf20Sopenharmony_ci				return -1;
4968c2ecf20Sopenharmony_ci			busbyte[byte] = unit;
4978c2ecf20Sopenharmony_ci			unituse[unit] = 1;
4988c2ecf20Sopenharmony_ci		}
4998c2ecf20Sopenharmony_ci	}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	/*
5028c2ecf20Sopenharmony_ci	 * Assign resources and set multiplexer selects.
5038c2ecf20Sopenharmony_ci	 *
5048c2ecf20Sopenharmony_ci	 * PM_ISU0 can go either on TTM0 or TTM1, but that's the only
5058c2ecf20Sopenharmony_ci	 * choice we have to deal with.
5068c2ecf20Sopenharmony_ci	 */
5078c2ecf20Sopenharmony_ci	if (unituse[PM_ISU0] &
5088c2ecf20Sopenharmony_ci	    (unituse[PM_FPU] | unituse[PM_IFU] | unituse[PM_ISU1])) {
5098c2ecf20Sopenharmony_ci		unituse[PM_ISU0_ALT] = 1;	/* move ISU to TTM1 */
5108c2ecf20Sopenharmony_ci		unituse[PM_ISU0] = 0;
5118c2ecf20Sopenharmony_ci	}
5128c2ecf20Sopenharmony_ci	/* Set TTM[01]SEL fields. */
5138c2ecf20Sopenharmony_ci	ttmuse = 0;
5148c2ecf20Sopenharmony_ci	for (i = PM_FPU; i <= PM_ISU1; ++i) {
5158c2ecf20Sopenharmony_ci		if (!unituse[i])
5168c2ecf20Sopenharmony_ci			continue;
5178c2ecf20Sopenharmony_ci		if (ttmuse++)
5188c2ecf20Sopenharmony_ci			return -1;
5198c2ecf20Sopenharmony_ci		mmcr1 |= (unsigned long)i << MMCR1_TTM0SEL_SH;
5208c2ecf20Sopenharmony_ci	}
5218c2ecf20Sopenharmony_ci	ttmuse = 0;
5228c2ecf20Sopenharmony_ci	for (; i <= PM_GRS; ++i) {
5238c2ecf20Sopenharmony_ci		if (!unituse[i])
5248c2ecf20Sopenharmony_ci			continue;
5258c2ecf20Sopenharmony_ci		if (ttmuse++)
5268c2ecf20Sopenharmony_ci			return -1;
5278c2ecf20Sopenharmony_ci		mmcr1 |= (unsigned long)(i & 3) << MMCR1_TTM1SEL_SH;
5288c2ecf20Sopenharmony_ci	}
5298c2ecf20Sopenharmony_ci	if (ttmuse > 1)
5308c2ecf20Sopenharmony_ci		return -1;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	/* Set byte lane select fields, TTM[23]SEL and GRS_*SEL. */
5338c2ecf20Sopenharmony_ci	for (byte = 0; byte < 4; ++byte) {
5348c2ecf20Sopenharmony_ci		unit = busbyte[byte];
5358c2ecf20Sopenharmony_ci		if (!unit)
5368c2ecf20Sopenharmony_ci			continue;
5378c2ecf20Sopenharmony_ci		if (unit == PM_ISU0 && unituse[PM_ISU0_ALT]) {
5388c2ecf20Sopenharmony_ci			/* get ISU0 through TTM1 rather than TTM0 */
5398c2ecf20Sopenharmony_ci			unit = PM_ISU0_ALT;
5408c2ecf20Sopenharmony_ci		} else if (unit == PM_LSU1 + 1) {
5418c2ecf20Sopenharmony_ci			/* select lower word of LSU1 for this byte */
5428c2ecf20Sopenharmony_ci			mmcr1 |= 1ul << (MMCR1_TTM3SEL_SH + 3 - byte);
5438c2ecf20Sopenharmony_ci		}
5448c2ecf20Sopenharmony_ci		ttm = unit >> 2;
5458c2ecf20Sopenharmony_ci		mmcr1 |= (unsigned long)ttm
5468c2ecf20Sopenharmony_ci			<< (MMCR1_TD_CP_DBG0SEL_SH - 2 * byte);
5478c2ecf20Sopenharmony_ci	}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	/* Second pass: assign PMCs, set PMCxSEL and PMCx_ADDER_SEL fields */
5508c2ecf20Sopenharmony_ci	for (i = 0; i < n_ev; ++i) {
5518c2ecf20Sopenharmony_ci		pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
5528c2ecf20Sopenharmony_ci		unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
5538c2ecf20Sopenharmony_ci		byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
5548c2ecf20Sopenharmony_ci		psel = event[i] & PM_PMCSEL_MSK;
5558c2ecf20Sopenharmony_ci		isbus = event[i] & PM_BUSEVENT_MSK;
5568c2ecf20Sopenharmony_ci		if (!pmc) {
5578c2ecf20Sopenharmony_ci			/* Bus event or any-PMC direct event */
5588c2ecf20Sopenharmony_ci			for (pmc = 0; pmc < 4; ++pmc) {
5598c2ecf20Sopenharmony_ci				if (!(pmc_inuse & (1 << pmc)))
5608c2ecf20Sopenharmony_ci					break;
5618c2ecf20Sopenharmony_ci			}
5628c2ecf20Sopenharmony_ci			if (pmc >= 4)
5638c2ecf20Sopenharmony_ci				return -1;
5648c2ecf20Sopenharmony_ci			pmc_inuse |= 1 << pmc;
5658c2ecf20Sopenharmony_ci		} else if (pmc <= 4) {
5668c2ecf20Sopenharmony_ci			/* Direct event */
5678c2ecf20Sopenharmony_ci			--pmc;
5688c2ecf20Sopenharmony_ci			if (isbus && (byte & 2) &&
5698c2ecf20Sopenharmony_ci			    (psel == 8 || psel == 0x10 || psel == 0x28))
5708c2ecf20Sopenharmony_ci				/* add events on higher-numbered bus */
5718c2ecf20Sopenharmony_ci				mmcr1 |= 1ul << (MMCR1_PMC1_ADDER_SEL_SH - pmc);
5728c2ecf20Sopenharmony_ci		} else {
5738c2ecf20Sopenharmony_ci			/* Instructions or run cycles on PMC5/6 */
5748c2ecf20Sopenharmony_ci			--pmc;
5758c2ecf20Sopenharmony_ci		}
5768c2ecf20Sopenharmony_ci		if (isbus && unit == PM_GRS) {
5778c2ecf20Sopenharmony_ci			bit = psel & 7;
5788c2ecf20Sopenharmony_ci			grsel = (event[i] >> PM_GRS_SH) & PM_GRS_MSK;
5798c2ecf20Sopenharmony_ci			mmcr1 |= (unsigned long)grsel << grsel_shift[bit];
5808c2ecf20Sopenharmony_ci		}
5818c2ecf20Sopenharmony_ci		if (power5p_marked_instr_event(event[i]))
5828c2ecf20Sopenharmony_ci			mmcra |= MMCRA_SAMPLE_ENABLE;
5838c2ecf20Sopenharmony_ci		if ((psel & 0x58) == 0x40 && (byte & 1) != ((pmc >> 1) & 1))
5848c2ecf20Sopenharmony_ci			/* select alternate byte lane */
5858c2ecf20Sopenharmony_ci			psel |= 0x10;
5868c2ecf20Sopenharmony_ci		if (pmc <= 3)
5878c2ecf20Sopenharmony_ci			mmcr1 |= psel << MMCR1_PMCSEL_SH(pmc);
5888c2ecf20Sopenharmony_ci		hwc[i] = pmc;
5898c2ecf20Sopenharmony_ci	}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	/* Return MMCRx values */
5928c2ecf20Sopenharmony_ci	mmcr->mmcr0 = 0;
5938c2ecf20Sopenharmony_ci	if (pmc_inuse & 1)
5948c2ecf20Sopenharmony_ci		mmcr->mmcr0 = MMCR0_PMC1CE;
5958c2ecf20Sopenharmony_ci	if (pmc_inuse & 0x3e)
5968c2ecf20Sopenharmony_ci		mmcr->mmcr0 |= MMCR0_PMCjCE;
5978c2ecf20Sopenharmony_ci	mmcr->mmcr1 = mmcr1;
5988c2ecf20Sopenharmony_ci	mmcr->mmcra = mmcra;
5998c2ecf20Sopenharmony_ci	return 0;
6008c2ecf20Sopenharmony_ci}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_cistatic void power5p_disable_pmc(unsigned int pmc, struct mmcr_regs *mmcr)
6038c2ecf20Sopenharmony_ci{
6048c2ecf20Sopenharmony_ci	if (pmc <= 3)
6058c2ecf20Sopenharmony_ci		mmcr->mmcr1 &= ~(0x7fUL << MMCR1_PMCSEL_SH(pmc));
6068c2ecf20Sopenharmony_ci}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_cistatic int power5p_generic_events[] = {
6098c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CPU_CYCLES]		= 0xf,
6108c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_INSTRUCTIONS]		= 0x100009,
6118c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CACHE_REFERENCES]	= 0x1c10a8, /* LD_REF_L1 */
6128c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_CACHE_MISSES]		= 0x3c1088, /* LD_MISS_L1 */
6138c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS]	= 0x230e4,  /* BR_ISSUED */
6148c2ecf20Sopenharmony_ci	[PERF_COUNT_HW_BRANCH_MISSES]		= 0x230e5,  /* BR_MPRED_CR */
6158c2ecf20Sopenharmony_ci};
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci#define C(x)	PERF_COUNT_HW_CACHE_##x
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci/*
6208c2ecf20Sopenharmony_ci * Table of generalized cache-related events.
6218c2ecf20Sopenharmony_ci * 0 means not supported, -1 means nonsensical, other values
6228c2ecf20Sopenharmony_ci * are event codes.
6238c2ecf20Sopenharmony_ci */
6248c2ecf20Sopenharmony_cistatic u64 power5p_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
6258c2ecf20Sopenharmony_ci	[C(L1D)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
6268c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0x1c10a8,	0x3c1088	},
6278c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	0x2c10a8,	0xc10c3		},
6288c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0xc70e7,	-1		},
6298c2ecf20Sopenharmony_ci	},
6308c2ecf20Sopenharmony_ci	[C(L1I)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
6318c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0		},
6328c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1		},
6338c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0,		0		},
6348c2ecf20Sopenharmony_ci	},
6358c2ecf20Sopenharmony_ci	[C(LL)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
6368c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0		},
6378c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	0,		0		},
6388c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	0xc50c3,	0		},
6398c2ecf20Sopenharmony_ci	},
6408c2ecf20Sopenharmony_ci	[C(DTLB)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
6418c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0xc20e4,	0x800c4		},
6428c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1		},
6438c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1		},
6448c2ecf20Sopenharmony_ci	},
6458c2ecf20Sopenharmony_ci	[C(ITLB)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
6468c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0,		0x800c0		},
6478c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1		},
6488c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1		},
6498c2ecf20Sopenharmony_ci	},
6508c2ecf20Sopenharmony_ci	[C(BPU)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
6518c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	0x230e4,	0x230e5		},
6528c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1		},
6538c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1		},
6548c2ecf20Sopenharmony_ci	},
6558c2ecf20Sopenharmony_ci	[C(NODE)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
6568c2ecf20Sopenharmony_ci		[C(OP_READ)] = {	-1,		-1		},
6578c2ecf20Sopenharmony_ci		[C(OP_WRITE)] = {	-1,		-1		},
6588c2ecf20Sopenharmony_ci		[C(OP_PREFETCH)] = {	-1,		-1		},
6598c2ecf20Sopenharmony_ci	},
6608c2ecf20Sopenharmony_ci};
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_cistatic struct power_pmu power5p_pmu = {
6638c2ecf20Sopenharmony_ci	.name			= "POWER5+/++",
6648c2ecf20Sopenharmony_ci	.n_counter		= 6,
6658c2ecf20Sopenharmony_ci	.max_alternatives	= MAX_ALT,
6668c2ecf20Sopenharmony_ci	.add_fields		= 0x7000000000055ul,
6678c2ecf20Sopenharmony_ci	.test_adder		= 0x3000040000000ul,
6688c2ecf20Sopenharmony_ci	.compute_mmcr		= power5p_compute_mmcr,
6698c2ecf20Sopenharmony_ci	.get_constraint		= power5p_get_constraint,
6708c2ecf20Sopenharmony_ci	.get_alternatives	= power5p_get_alternatives,
6718c2ecf20Sopenharmony_ci	.disable_pmc		= power5p_disable_pmc,
6728c2ecf20Sopenharmony_ci	.limited_pmc_event	= power5p_limited_pmc_event,
6738c2ecf20Sopenharmony_ci	.flags			= PPMU_LIMITED_PMC5_6 | PPMU_HAS_SSLOT,
6748c2ecf20Sopenharmony_ci	.n_generic		= ARRAY_SIZE(power5p_generic_events),
6758c2ecf20Sopenharmony_ci	.generic_events		= power5p_generic_events,
6768c2ecf20Sopenharmony_ci	.cache_events		= &power5p_cache_events,
6778c2ecf20Sopenharmony_ci};
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ciint init_power5p_pmu(void)
6808c2ecf20Sopenharmony_ci{
6818c2ecf20Sopenharmony_ci	if (!cur_cpu_spec->oprofile_cpu_type ||
6828c2ecf20Sopenharmony_ci	    (strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power5+")
6838c2ecf20Sopenharmony_ci	     && strcmp(cur_cpu_spec->oprofile_cpu_type, "ppc64/power5++")))
6848c2ecf20Sopenharmony_ci		return -ENODEV;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	return register_power_pmu(&power5p_pmu);
6878c2ecf20Sopenharmony_ci}
688