xref: /openbmc/linux/arch/powerpc/perf/ppc970-pmu.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f2699491SMichael Ellerman /*
3f2699491SMichael Ellerman  * Performance counter support for PPC970-family processors.
4f2699491SMichael Ellerman  *
5f2699491SMichael Ellerman  * Copyright 2008-2009 Paul Mackerras, IBM Corporation.
6f2699491SMichael Ellerman  */
7f2699491SMichael Ellerman #include <linux/string.h>
8f2699491SMichael Ellerman #include <linux/perf_event.h>
9f2699491SMichael Ellerman #include <asm/reg.h>
10f2699491SMichael Ellerman #include <asm/cputable.h>
11f2699491SMichael Ellerman 
12d10ebe79SMichael Ellerman #include "internal.h"
13d10ebe79SMichael Ellerman 
14f2699491SMichael Ellerman /*
15f2699491SMichael Ellerman  * Bits in event code for PPC970
16f2699491SMichael Ellerman  */
17f2699491SMichael Ellerman #define PM_PMC_SH	12	/* PMC number (1-based) for direct events */
18f2699491SMichael Ellerman #define PM_PMC_MSK	0xf
19f2699491SMichael Ellerman #define PM_UNIT_SH	8	/* TTMMUX number and setting - unit select */
20f2699491SMichael Ellerman #define PM_UNIT_MSK	0xf
21f2699491SMichael Ellerman #define PM_SPCSEL_SH	6
22f2699491SMichael Ellerman #define PM_SPCSEL_MSK	3
23f2699491SMichael Ellerman #define PM_BYTE_SH	4	/* Byte number of event bus to use */
24f2699491SMichael Ellerman #define PM_BYTE_MSK	3
25f2699491SMichael Ellerman #define PM_PMCSEL_MSK	0xf
26f2699491SMichael Ellerman 
27f2699491SMichael Ellerman /* Values in PM_UNIT field */
28f2699491SMichael Ellerman #define PM_NONE		0
29f2699491SMichael Ellerman #define PM_FPU		1
30f2699491SMichael Ellerman #define PM_VPU		2
31f2699491SMichael Ellerman #define PM_ISU		3
32f2699491SMichael Ellerman #define PM_IFU		4
33f2699491SMichael Ellerman #define PM_IDU		5
34f2699491SMichael Ellerman #define PM_STS		6
35f2699491SMichael Ellerman #define PM_LSU0		7
36f2699491SMichael Ellerman #define PM_LSU1U	8
37f2699491SMichael Ellerman #define PM_LSU1L	9
38f2699491SMichael Ellerman #define PM_LASTUNIT	9
39f2699491SMichael Ellerman 
40f2699491SMichael Ellerman /*
41f2699491SMichael Ellerman  * Bits in MMCR0 for PPC970
42f2699491SMichael Ellerman  */
43f2699491SMichael Ellerman #define MMCR0_PMC1SEL_SH	8
44f2699491SMichael Ellerman #define MMCR0_PMC2SEL_SH	1
45f2699491SMichael Ellerman #define MMCR_PMCSEL_MSK		0x1f
46f2699491SMichael Ellerman 
47f2699491SMichael Ellerman /*
48f2699491SMichael Ellerman  * Bits in MMCR1 for PPC970
49f2699491SMichael Ellerman  */
50f2699491SMichael Ellerman #define MMCR1_TTM0SEL_SH	62
51f2699491SMichael Ellerman #define MMCR1_TTM1SEL_SH	59
52f2699491SMichael Ellerman #define MMCR1_TTM3SEL_SH	53
53f2699491SMichael Ellerman #define MMCR1_TTMSEL_MSK	3
54f2699491SMichael Ellerman #define MMCR1_TD_CP_DBG0SEL_SH	50
55f2699491SMichael Ellerman #define MMCR1_TD_CP_DBG1SEL_SH	48
56f2699491SMichael Ellerman #define MMCR1_TD_CP_DBG2SEL_SH	46
57f2699491SMichael Ellerman #define MMCR1_TD_CP_DBG3SEL_SH	44
58f2699491SMichael Ellerman #define MMCR1_PMC1_ADDER_SEL_SH	39
59f2699491SMichael Ellerman #define MMCR1_PMC2_ADDER_SEL_SH	38
60f2699491SMichael Ellerman #define MMCR1_PMC6_ADDER_SEL_SH	37
61f2699491SMichael Ellerman #define MMCR1_PMC5_ADDER_SEL_SH	36
62f2699491SMichael Ellerman #define MMCR1_PMC8_ADDER_SEL_SH	35
63f2699491SMichael Ellerman #define MMCR1_PMC7_ADDER_SEL_SH	34
64f2699491SMichael Ellerman #define MMCR1_PMC3_ADDER_SEL_SH	33
65f2699491SMichael Ellerman #define MMCR1_PMC4_ADDER_SEL_SH	32
66f2699491SMichael Ellerman #define MMCR1_PMC3SEL_SH	27
67f2699491SMichael Ellerman #define MMCR1_PMC4SEL_SH	22
68f2699491SMichael Ellerman #define MMCR1_PMC5SEL_SH	17
69f2699491SMichael Ellerman #define MMCR1_PMC6SEL_SH	12
70f2699491SMichael Ellerman #define MMCR1_PMC7SEL_SH	7
71f2699491SMichael Ellerman #define MMCR1_PMC8SEL_SH	2
72f2699491SMichael Ellerman 
73f2699491SMichael Ellerman static short mmcr1_adder_bits[8] = {
74f2699491SMichael Ellerman 	MMCR1_PMC1_ADDER_SEL_SH,
75f2699491SMichael Ellerman 	MMCR1_PMC2_ADDER_SEL_SH,
76f2699491SMichael Ellerman 	MMCR1_PMC3_ADDER_SEL_SH,
77f2699491SMichael Ellerman 	MMCR1_PMC4_ADDER_SEL_SH,
78f2699491SMichael Ellerman 	MMCR1_PMC5_ADDER_SEL_SH,
79f2699491SMichael Ellerman 	MMCR1_PMC6_ADDER_SEL_SH,
80f2699491SMichael Ellerman 	MMCR1_PMC7_ADDER_SEL_SH,
81f2699491SMichael Ellerman 	MMCR1_PMC8_ADDER_SEL_SH
82f2699491SMichael Ellerman };
83f2699491SMichael Ellerman 
84f2699491SMichael Ellerman /*
85f2699491SMichael Ellerman  * Layout of constraint bits:
86f2699491SMichael Ellerman  * 6666555555555544444444443333333333222222222211111111110000000000
87f2699491SMichael Ellerman  * 3210987654321098765432109876543210987654321098765432109876543210
88f2699491SMichael Ellerman  *               <><><>[  >[  >[  ><  ><  ><  ><  ><><><><><><><><>
89f2699491SMichael Ellerman  *               SPT0T1 UC  PS1 PS2 B0  B1  B2  B3 P1P2P3P4P5P6P7P8
90f2699491SMichael Ellerman  *
91f2699491SMichael Ellerman  * SP - SPCSEL constraint
92f2699491SMichael Ellerman  *     48-49: SPCSEL value 0x3_0000_0000_0000
93f2699491SMichael Ellerman  *
94f2699491SMichael Ellerman  * T0 - TTM0 constraint
95f2699491SMichael Ellerman  *     46-47: TTM0SEL value (0=FPU, 2=IFU, 3=VPU) 0xC000_0000_0000
96f2699491SMichael Ellerman  *
97f2699491SMichael Ellerman  * T1 - TTM1 constraint
98f2699491SMichael Ellerman  *     44-45: TTM1SEL value (0=IDU, 3=STS) 0x3000_0000_0000
99f2699491SMichael Ellerman  *
100f2699491SMichael Ellerman  * UC - unit constraint: can't have all three of FPU|IFU|VPU, ISU, IDU|STS
101f2699491SMichael Ellerman  *     43: UC3 error 0x0800_0000_0000
102f2699491SMichael Ellerman  *     42: FPU|IFU|VPU events needed 0x0400_0000_0000
103f2699491SMichael Ellerman  *     41: ISU events needed 0x0200_0000_0000
104f2699491SMichael Ellerman  *     40: IDU|STS events needed 0x0100_0000_0000
105f2699491SMichael Ellerman  *
106f2699491SMichael Ellerman  * PS1
107f2699491SMichael Ellerman  *     39: PS1 error 0x0080_0000_0000
108f2699491SMichael Ellerman  *     36-38: count of events needing PMC1/2/5/6 0x0070_0000_0000
109f2699491SMichael Ellerman  *
110f2699491SMichael Ellerman  * PS2
111f2699491SMichael Ellerman  *     35: PS2 error 0x0008_0000_0000
112f2699491SMichael Ellerman  *     32-34: count of events needing PMC3/4/7/8 0x0007_0000_0000
113f2699491SMichael Ellerman  *
114f2699491SMichael Ellerman  * B0
115f2699491SMichael Ellerman  *     28-31: Byte 0 event source 0xf000_0000
116f2699491SMichael Ellerman  *	      Encoding as for the event code
117f2699491SMichael Ellerman  *
118f2699491SMichael Ellerman  * B1, B2, B3
119f2699491SMichael Ellerman  *     24-27, 20-23, 16-19: Byte 1, 2, 3 event sources
120f2699491SMichael Ellerman  *
121f2699491SMichael Ellerman  * P1
122f2699491SMichael Ellerman  *     15: P1 error 0x8000
123f2699491SMichael Ellerman  *     14-15: Count of events needing PMC1
124f2699491SMichael Ellerman  *
125f2699491SMichael Ellerman  * P2..P8
126f2699491SMichael Ellerman  *     0-13: Count of events needing PMC2..PMC8
127f2699491SMichael Ellerman  */
128f2699491SMichael Ellerman 
129f2699491SMichael Ellerman static unsigned char direct_marked_event[8] = {
130f2699491SMichael Ellerman 	(1<<2) | (1<<3),	/* PMC1: PM_MRK_GRP_DISP, PM_MRK_ST_CMPL */
131f2699491SMichael Ellerman 	(1<<3) | (1<<5),	/* PMC2: PM_THRESH_TIMEO, PM_MRK_BRU_FIN */
132f2699491SMichael Ellerman 	(1<<3) | (1<<5),	/* PMC3: PM_MRK_ST_CMPL_INT, PM_MRK_VMX_FIN */
133f2699491SMichael Ellerman 	(1<<4) | (1<<5),	/* PMC4: PM_MRK_GRP_CMPL, PM_MRK_CRU_FIN */
134f2699491SMichael Ellerman 	(1<<4) | (1<<5),	/* PMC5: PM_GRP_MRK, PM_MRK_GRP_TIMEO */
135f2699491SMichael Ellerman 	(1<<3) | (1<<4) | (1<<5),
136f2699491SMichael Ellerman 		/* PMC6: PM_MRK_ST_STS, PM_MRK_FXU_FIN, PM_MRK_GRP_ISSUED */
137f2699491SMichael Ellerman 	(1<<4) | (1<<5),	/* PMC7: PM_MRK_FPU_FIN, PM_MRK_INST_FIN */
138f2699491SMichael Ellerman 	(1<<4)			/* PMC8: PM_MRK_LSU_FIN */
139f2699491SMichael Ellerman };
140f2699491SMichael Ellerman 
141f2699491SMichael Ellerman /*
142f2699491SMichael Ellerman  * Returns 1 if event counts things relating to marked instructions
143f2699491SMichael Ellerman  * and thus needs the MMCRA_SAMPLE_ENABLE bit set, or 0 if not.
144f2699491SMichael Ellerman  */
p970_marked_instr_event(u64 event)145f2699491SMichael Ellerman static int p970_marked_instr_event(u64 event)
146f2699491SMichael Ellerman {
147f2699491SMichael Ellerman 	int pmc, psel, unit, byte, bit;
148f2699491SMichael Ellerman 	unsigned int mask;
149f2699491SMichael Ellerman 
150f2699491SMichael Ellerman 	pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
151f2699491SMichael Ellerman 	psel = event & PM_PMCSEL_MSK;
152f2699491SMichael Ellerman 	if (pmc) {
153f2699491SMichael Ellerman 		if (direct_marked_event[pmc - 1] & (1 << psel))
154f2699491SMichael Ellerman 			return 1;
155f2699491SMichael Ellerman 		if (psel == 0)		/* add events */
156f2699491SMichael Ellerman 			bit = (pmc <= 4)? pmc - 1: 8 - pmc;
157f2699491SMichael Ellerman 		else if (psel == 7 || psel == 13)	/* decode events */
158f2699491SMichael Ellerman 			bit = 4;
159f2699491SMichael Ellerman 		else
160f2699491SMichael Ellerman 			return 0;
161f2699491SMichael Ellerman 	} else
162f2699491SMichael Ellerman 		bit = psel;
163f2699491SMichael Ellerman 
164f2699491SMichael Ellerman 	byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
165f2699491SMichael Ellerman 	unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
166f2699491SMichael Ellerman 	mask = 0;
167f2699491SMichael Ellerman 	switch (unit) {
168f2699491SMichael Ellerman 	case PM_VPU:
169f2699491SMichael Ellerman 		mask = 0x4c;		/* byte 0 bits 2,3,6 */
170f2699491SMichael Ellerman 		break;
171f2699491SMichael Ellerman 	case PM_LSU0:
172f2699491SMichael Ellerman 		/* byte 2 bits 0,2,3,4,6; all of byte 1 */
173f2699491SMichael Ellerman 		mask = 0x085dff00;
174f2699491SMichael Ellerman 		break;
175f2699491SMichael Ellerman 	case PM_LSU1L:
176f2699491SMichael Ellerman 		mask = 0x50 << 24;	/* byte 3 bits 4,6 */
177f2699491SMichael Ellerman 		break;
178f2699491SMichael Ellerman 	}
179f2699491SMichael Ellerman 	return (mask >> (byte * 8 + bit)) & 1;
180f2699491SMichael Ellerman }
181f2699491SMichael Ellerman 
182f2699491SMichael Ellerman /* Masks and values for using events from the various units */
183f2699491SMichael Ellerman static unsigned long unit_cons[PM_LASTUNIT+1][2] = {
184f2699491SMichael Ellerman 	[PM_FPU] =   { 0xc80000000000ull, 0x040000000000ull },
185f2699491SMichael Ellerman 	[PM_VPU] =   { 0xc80000000000ull, 0xc40000000000ull },
186f2699491SMichael Ellerman 	[PM_ISU] =   { 0x080000000000ull, 0x020000000000ull },
187f2699491SMichael Ellerman 	[PM_IFU] =   { 0xc80000000000ull, 0x840000000000ull },
188f2699491SMichael Ellerman 	[PM_IDU] =   { 0x380000000000ull, 0x010000000000ull },
189f2699491SMichael Ellerman 	[PM_STS] =   { 0x380000000000ull, 0x310000000000ull },
190f2699491SMichael Ellerman };
191f2699491SMichael Ellerman 
p970_get_constraint(u64 event,unsigned long * maskp,unsigned long * valp,u64 event_config1 __maybe_unused)192f2699491SMichael Ellerman static int p970_get_constraint(u64 event, unsigned long *maskp,
19382d2c16bSKajol Jain 			       unsigned long *valp, u64 event_config1 __maybe_unused)
194f2699491SMichael Ellerman {
195f2699491SMichael Ellerman 	int pmc, byte, unit, sh, spcsel;
196f2699491SMichael Ellerman 	unsigned long mask = 0, value = 0;
197f2699491SMichael Ellerman 	int grp = -1;
198f2699491SMichael Ellerman 
199f2699491SMichael Ellerman 	pmc = (event >> PM_PMC_SH) & PM_PMC_MSK;
200f2699491SMichael Ellerman 	if (pmc) {
201f2699491SMichael Ellerman 		if (pmc > 8)
202f2699491SMichael Ellerman 			return -1;
203f2699491SMichael Ellerman 		sh = (pmc - 1) * 2;
204f2699491SMichael Ellerman 		mask |= 2 << sh;
205f2699491SMichael Ellerman 		value |= 1 << sh;
206f2699491SMichael Ellerman 		grp = ((pmc - 1) >> 1) & 1;
207f2699491SMichael Ellerman 	}
208f2699491SMichael Ellerman 	unit = (event >> PM_UNIT_SH) & PM_UNIT_MSK;
209f2699491SMichael Ellerman 	if (unit) {
210f2699491SMichael Ellerman 		if (unit > PM_LASTUNIT)
211f2699491SMichael Ellerman 			return -1;
212f2699491SMichael Ellerman 		mask |= unit_cons[unit][0];
213f2699491SMichael Ellerman 		value |= unit_cons[unit][1];
214f2699491SMichael Ellerman 		byte = (event >> PM_BYTE_SH) & PM_BYTE_MSK;
215f2699491SMichael Ellerman 		/*
216f2699491SMichael Ellerman 		 * Bus events on bytes 0 and 2 can be counted
217f2699491SMichael Ellerman 		 * on PMC1/2/5/6; bytes 1 and 3 on PMC3/4/7/8.
218f2699491SMichael Ellerman 		 */
219f2699491SMichael Ellerman 		if (!pmc)
220f2699491SMichael Ellerman 			grp = byte & 1;
221f2699491SMichael Ellerman 		/* Set byte lane select field */
222f2699491SMichael Ellerman 		mask  |= 0xfULL << (28 - 4 * byte);
223f2699491SMichael Ellerman 		value |= (unsigned long)unit << (28 - 4 * byte);
224f2699491SMichael Ellerman 	}
225f2699491SMichael Ellerman 	if (grp == 0) {
226f2699491SMichael Ellerman 		/* increment PMC1/2/5/6 field */
227f2699491SMichael Ellerman 		mask  |= 0x8000000000ull;
228f2699491SMichael Ellerman 		value |= 0x1000000000ull;
229f2699491SMichael Ellerman 	} else if (grp == 1) {
230f2699491SMichael Ellerman 		/* increment PMC3/4/7/8 field */
231f2699491SMichael Ellerman 		mask  |= 0x800000000ull;
232f2699491SMichael Ellerman 		value |= 0x100000000ull;
233f2699491SMichael Ellerman 	}
234f2699491SMichael Ellerman 	spcsel = (event >> PM_SPCSEL_SH) & PM_SPCSEL_MSK;
235f2699491SMichael Ellerman 	if (spcsel) {
236f2699491SMichael Ellerman 		mask  |= 3ull << 48;
237f2699491SMichael Ellerman 		value |= (unsigned long)spcsel << 48;
238f2699491SMichael Ellerman 	}
239f2699491SMichael Ellerman 	*maskp = mask;
240f2699491SMichael Ellerman 	*valp = value;
241f2699491SMichael Ellerman 	return 0;
242f2699491SMichael Ellerman }
243f2699491SMichael Ellerman 
p970_get_alternatives(u64 event,unsigned int flags,u64 alt[])244f2699491SMichael Ellerman static int p970_get_alternatives(u64 event, unsigned int flags, u64 alt[])
245f2699491SMichael Ellerman {
246f2699491SMichael Ellerman 	alt[0] = event;
247f2699491SMichael Ellerman 
248f2699491SMichael Ellerman 	/* 2 alternatives for LSU empty */
249f2699491SMichael Ellerman 	if (event == 0x2002 || event == 0x3002) {
250f2699491SMichael Ellerman 		alt[1] = event ^ 0x1000;
251f2699491SMichael Ellerman 		return 2;
252f2699491SMichael Ellerman 	}
253f2699491SMichael Ellerman 
254f2699491SMichael Ellerman 	return 1;
255f2699491SMichael Ellerman }
256f2699491SMichael Ellerman 
p970_compute_mmcr(u64 event[],int n_ev,unsigned int hwc[],struct mmcr_regs * mmcr,struct perf_event * pevents[],u32 flags __maybe_unused)257f2699491SMichael Ellerman static int p970_compute_mmcr(u64 event[], int n_ev,
25878d76819SAthira Rajeev 			     unsigned int hwc[], struct mmcr_regs *mmcr,
25982d2c16bSKajol Jain 			     struct perf_event *pevents[],
26082d2c16bSKajol Jain 			     u32 flags __maybe_unused)
261f2699491SMichael Ellerman {
262f2699491SMichael Ellerman 	unsigned long mmcr0 = 0, mmcr1 = 0, mmcra = 0;
263f2699491SMichael Ellerman 	unsigned int pmc, unit, byte, psel;
264f2699491SMichael Ellerman 	unsigned int ttm, grp;
265f2699491SMichael Ellerman 	unsigned int pmc_inuse = 0;
266f2699491SMichael Ellerman 	unsigned int pmc_grp_use[2];
267f2699491SMichael Ellerman 	unsigned char busbyte[4];
268f2699491SMichael Ellerman 	unsigned char unituse[16];
269f2699491SMichael Ellerman 	unsigned char unitmap[] = { 0, 0<<3, 3<<3, 1<<3, 2<<3, 0|4, 3|4 };
270f2699491SMichael Ellerman 	unsigned char ttmuse[2];
271f2699491SMichael Ellerman 	unsigned char pmcsel[8];
272f2699491SMichael Ellerman 	int i;
273f2699491SMichael Ellerman 	int spcsel;
274f2699491SMichael Ellerman 
275f2699491SMichael Ellerman 	if (n_ev > 8)
276f2699491SMichael Ellerman 		return -1;
277f2699491SMichael Ellerman 
278f2699491SMichael Ellerman 	/* First pass to count resource use */
279f2699491SMichael Ellerman 	pmc_grp_use[0] = pmc_grp_use[1] = 0;
280f2699491SMichael Ellerman 	memset(busbyte, 0, sizeof(busbyte));
281f2699491SMichael Ellerman 	memset(unituse, 0, sizeof(unituse));
282f2699491SMichael Ellerman 	for (i = 0; i < n_ev; ++i) {
283f2699491SMichael Ellerman 		pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
284f2699491SMichael Ellerman 		if (pmc) {
285f2699491SMichael Ellerman 			if (pmc_inuse & (1 << (pmc - 1)))
286f2699491SMichael Ellerman 				return -1;
287f2699491SMichael Ellerman 			pmc_inuse |= 1 << (pmc - 1);
288f2699491SMichael Ellerman 			/* count 1/2/5/6 vs 3/4/7/8 use */
289f2699491SMichael Ellerman 			++pmc_grp_use[((pmc - 1) >> 1) & 1];
290f2699491SMichael Ellerman 		}
291f2699491SMichael Ellerman 		unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
292f2699491SMichael Ellerman 		byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
293f2699491SMichael Ellerman 		if (unit) {
294f2699491SMichael Ellerman 			if (unit > PM_LASTUNIT)
295f2699491SMichael Ellerman 				return -1;
296f2699491SMichael Ellerman 			if (!pmc)
297f2699491SMichael Ellerman 				++pmc_grp_use[byte & 1];
298f2699491SMichael Ellerman 			if (busbyte[byte] && busbyte[byte] != unit)
299f2699491SMichael Ellerman 				return -1;
300f2699491SMichael Ellerman 			busbyte[byte] = unit;
301f2699491SMichael Ellerman 			unituse[unit] = 1;
302f2699491SMichael Ellerman 		}
303f2699491SMichael Ellerman 	}
304f2699491SMichael Ellerman 	if (pmc_grp_use[0] > 4 || pmc_grp_use[1] > 4)
305f2699491SMichael Ellerman 		return -1;
306f2699491SMichael Ellerman 
307f2699491SMichael Ellerman 	/*
308f2699491SMichael Ellerman 	 * Assign resources and set multiplexer selects.
309f2699491SMichael Ellerman 	 *
310f2699491SMichael Ellerman 	 * PM_ISU can go either on TTM0 or TTM1, but that's the only
311f2699491SMichael Ellerman 	 * choice we have to deal with.
312f2699491SMichael Ellerman 	 */
313f2699491SMichael Ellerman 	if (unituse[PM_ISU] &
314f2699491SMichael Ellerman 	    (unituse[PM_FPU] | unituse[PM_IFU] | unituse[PM_VPU]))
315f2699491SMichael Ellerman 		unitmap[PM_ISU] = 2 | 4;	/* move ISU to TTM1 */
316f2699491SMichael Ellerman 	/* Set TTM[01]SEL fields. */
317f2699491SMichael Ellerman 	ttmuse[0] = ttmuse[1] = 0;
318f2699491SMichael Ellerman 	for (i = PM_FPU; i <= PM_STS; ++i) {
319f2699491SMichael Ellerman 		if (!unituse[i])
320f2699491SMichael Ellerman 			continue;
321f2699491SMichael Ellerman 		ttm = unitmap[i];
322f2699491SMichael Ellerman 		++ttmuse[(ttm >> 2) & 1];
323f2699491SMichael Ellerman 		mmcr1 |= (unsigned long)(ttm & ~4) << MMCR1_TTM1SEL_SH;
324f2699491SMichael Ellerman 	}
325f2699491SMichael Ellerman 	/* Check only one unit per TTMx */
326f2699491SMichael Ellerman 	if (ttmuse[0] > 1 || ttmuse[1] > 1)
327f2699491SMichael Ellerman 		return -1;
328f2699491SMichael Ellerman 
329f2699491SMichael Ellerman 	/* Set byte lane select fields and TTM3SEL. */
330f2699491SMichael Ellerman 	for (byte = 0; byte < 4; ++byte) {
331f2699491SMichael Ellerman 		unit = busbyte[byte];
332f2699491SMichael Ellerman 		if (!unit)
333f2699491SMichael Ellerman 			continue;
334f2699491SMichael Ellerman 		if (unit <= PM_STS)
335f2699491SMichael Ellerman 			ttm = (unitmap[unit] >> 2) & 1;
336f2699491SMichael Ellerman 		else if (unit == PM_LSU0)
337f2699491SMichael Ellerman 			ttm = 2;
338f2699491SMichael Ellerman 		else {
339f2699491SMichael Ellerman 			ttm = 3;
340f2699491SMichael Ellerman 			if (unit == PM_LSU1L && byte >= 2)
341f2699491SMichael Ellerman 				mmcr1 |= 1ull << (MMCR1_TTM3SEL_SH + 3 - byte);
342f2699491SMichael Ellerman 		}
343f2699491SMichael Ellerman 		mmcr1 |= (unsigned long)ttm
344f2699491SMichael Ellerman 			<< (MMCR1_TD_CP_DBG0SEL_SH - 2 * byte);
345f2699491SMichael Ellerman 	}
346f2699491SMichael Ellerman 
347f2699491SMichael Ellerman 	/* Second pass: assign PMCs, set PMCxSEL and PMCx_ADDER_SEL fields */
348f2699491SMichael Ellerman 	memset(pmcsel, 0x8, sizeof(pmcsel));	/* 8 means don't count */
349f2699491SMichael Ellerman 	for (i = 0; i < n_ev; ++i) {
350f2699491SMichael Ellerman 		pmc = (event[i] >> PM_PMC_SH) & PM_PMC_MSK;
351f2699491SMichael Ellerman 		unit = (event[i] >> PM_UNIT_SH) & PM_UNIT_MSK;
352f2699491SMichael Ellerman 		byte = (event[i] >> PM_BYTE_SH) & PM_BYTE_MSK;
353f2699491SMichael Ellerman 		psel = event[i] & PM_PMCSEL_MSK;
354f2699491SMichael Ellerman 		if (!pmc) {
355f2699491SMichael Ellerman 			/* Bus event or any-PMC direct event */
356f2699491SMichael Ellerman 			if (unit)
357f2699491SMichael Ellerman 				psel |= 0x10 | ((byte & 2) << 2);
358f2699491SMichael Ellerman 			else
359f2699491SMichael Ellerman 				psel |= 8;
360f2699491SMichael Ellerman 			for (pmc = 0; pmc < 8; ++pmc) {
361f2699491SMichael Ellerman 				if (pmc_inuse & (1 << pmc))
362f2699491SMichael Ellerman 					continue;
363f2699491SMichael Ellerman 				grp = (pmc >> 1) & 1;
364f2699491SMichael Ellerman 				if (unit) {
365f2699491SMichael Ellerman 					if (grp == (byte & 1))
366f2699491SMichael Ellerman 						break;
367f2699491SMichael Ellerman 				} else if (pmc_grp_use[grp] < 4) {
368f2699491SMichael Ellerman 					++pmc_grp_use[grp];
369f2699491SMichael Ellerman 					break;
370f2699491SMichael Ellerman 				}
371f2699491SMichael Ellerman 			}
372f2699491SMichael Ellerman 			pmc_inuse |= 1 << pmc;
373f2699491SMichael Ellerman 		} else {
374f2699491SMichael Ellerman 			/* Direct event */
375f2699491SMichael Ellerman 			--pmc;
376f2699491SMichael Ellerman 			if (psel == 0 && (byte & 2))
377f2699491SMichael Ellerman 				/* add events on higher-numbered bus */
378f2699491SMichael Ellerman 				mmcr1 |= 1ull << mmcr1_adder_bits[pmc];
379f2699491SMichael Ellerman 		}
380f2699491SMichael Ellerman 		pmcsel[pmc] = psel;
381f2699491SMichael Ellerman 		hwc[i] = pmc;
382f2699491SMichael Ellerman 		spcsel = (event[i] >> PM_SPCSEL_SH) & PM_SPCSEL_MSK;
383f2699491SMichael Ellerman 		mmcr1 |= spcsel;
384f2699491SMichael Ellerman 		if (p970_marked_instr_event(event[i]))
385f2699491SMichael Ellerman 			mmcra |= MMCRA_SAMPLE_ENABLE;
386f2699491SMichael Ellerman 	}
387f2699491SMichael Ellerman 	for (pmc = 0; pmc < 2; ++pmc)
388f2699491SMichael Ellerman 		mmcr0 |= pmcsel[pmc] << (MMCR0_PMC1SEL_SH - 7 * pmc);
389f2699491SMichael Ellerman 	for (; pmc < 8; ++pmc)
390f2699491SMichael Ellerman 		mmcr1 |= (unsigned long)pmcsel[pmc]
391f2699491SMichael Ellerman 			<< (MMCR1_PMC3SEL_SH - 5 * (pmc - 2));
392f2699491SMichael Ellerman 	if (pmc_inuse & 1)
393f2699491SMichael Ellerman 		mmcr0 |= MMCR0_PMC1CE;
394f2699491SMichael Ellerman 	if (pmc_inuse & 0xfe)
395f2699491SMichael Ellerman 		mmcr0 |= MMCR0_PMCjCE;
396f2699491SMichael Ellerman 
397f2699491SMichael Ellerman 	mmcra |= 0x2000;	/* mark only one IOP per PPC instruction */
398f2699491SMichael Ellerman 
399f2699491SMichael Ellerman 	/* Return MMCRx values */
40078d76819SAthira Rajeev 	mmcr->mmcr0 = mmcr0;
40178d76819SAthira Rajeev 	mmcr->mmcr1 = mmcr1;
40278d76819SAthira Rajeev 	mmcr->mmcra = mmcra;
403f2699491SMichael Ellerman 	return 0;
404f2699491SMichael Ellerman }
405f2699491SMichael Ellerman 
p970_disable_pmc(unsigned int pmc,struct mmcr_regs * mmcr)40678d76819SAthira Rajeev static void p970_disable_pmc(unsigned int pmc, struct mmcr_regs *mmcr)
407f2699491SMichael Ellerman {
40878d76819SAthira Rajeev 	int shift;
409f2699491SMichael Ellerman 
410f2699491SMichael Ellerman 	/*
411f2699491SMichael Ellerman 	 * Setting the PMCxSEL field to 0x08 disables PMC x.
412f2699491SMichael Ellerman 	 */
41378d76819SAthira Rajeev 	if (pmc <= 1) {
41478d76819SAthira Rajeev 		shift = MMCR0_PMC1SEL_SH - 7 * pmc;
41578d76819SAthira Rajeev 		mmcr->mmcr0 = (mmcr->mmcr0 & ~(0x1fUL << shift)) | (0x08UL << shift);
41678d76819SAthira Rajeev 	} else {
41778d76819SAthira Rajeev 		shift = MMCR1_PMC3SEL_SH - 5 * (pmc - 2);
41878d76819SAthira Rajeev 		mmcr->mmcr1 = (mmcr->mmcr1 & ~(0x1fUL << shift)) | (0x08UL << shift);
41978d76819SAthira Rajeev 	}
420f2699491SMichael Ellerman }
421f2699491SMichael Ellerman 
422f2699491SMichael Ellerman static int ppc970_generic_events[] = {
423f2699491SMichael Ellerman 	[PERF_COUNT_HW_CPU_CYCLES]		= 7,
424f2699491SMichael Ellerman 	[PERF_COUNT_HW_INSTRUCTIONS]		= 1,
425f2699491SMichael Ellerman 	[PERF_COUNT_HW_CACHE_REFERENCES]	= 0x8810, /* PM_LD_REF_L1 */
426f2699491SMichael Ellerman 	[PERF_COUNT_HW_CACHE_MISSES]		= 0x3810, /* PM_LD_MISS_L1 */
427f2699491SMichael Ellerman 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS]	= 0x431,  /* PM_BR_ISSUED */
428f2699491SMichael Ellerman 	[PERF_COUNT_HW_BRANCH_MISSES] 		= 0x327,  /* PM_GRP_BR_MPRED */
429f2699491SMichael Ellerman };
430f2699491SMichael Ellerman 
431f2699491SMichael Ellerman #define C(x)	PERF_COUNT_HW_CACHE_##x
432f2699491SMichael Ellerman 
433f2699491SMichael Ellerman /*
434f2699491SMichael Ellerman  * Table of generalized cache-related events.
435f2699491SMichael Ellerman  * 0 means not supported, -1 means nonsensical, other values
436f2699491SMichael Ellerman  * are event codes.
437f2699491SMichael Ellerman  */
4389d4fc86dSAthira Rajeev static u64 ppc970_cache_events[C(MAX)][C(OP_MAX)][C(RESULT_MAX)] = {
439f2699491SMichael Ellerman 	[C(L1D)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
440f2699491SMichael Ellerman 		[C(OP_READ)] = {	0x8810,		0x3810	},
441f2699491SMichael Ellerman 		[C(OP_WRITE)] = {	0x7810,		0x813	},
442f2699491SMichael Ellerman 		[C(OP_PREFETCH)] = {	0x731,		0	},
443f2699491SMichael Ellerman 	},
444f2699491SMichael Ellerman 	[C(L1I)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
445f2699491SMichael Ellerman 		[C(OP_READ)] = {	0,		0	},
446f2699491SMichael Ellerman 		[C(OP_WRITE)] = {	-1,		-1	},
447f2699491SMichael Ellerman 		[C(OP_PREFETCH)] = {	0,		0	},
448f2699491SMichael Ellerman 	},
449f2699491SMichael Ellerman 	[C(LL)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
450f2699491SMichael Ellerman 		[C(OP_READ)] = {	0,		0	},
451f2699491SMichael Ellerman 		[C(OP_WRITE)] = {	0,		0	},
452f2699491SMichael Ellerman 		[C(OP_PREFETCH)] = {	0x733,		0	},
453f2699491SMichael Ellerman 	},
454f2699491SMichael Ellerman 	[C(DTLB)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
455f2699491SMichael Ellerman 		[C(OP_READ)] = {	0,		0x704	},
456f2699491SMichael Ellerman 		[C(OP_WRITE)] = {	-1,		-1	},
457f2699491SMichael Ellerman 		[C(OP_PREFETCH)] = {	-1,		-1	},
458f2699491SMichael Ellerman 	},
459f2699491SMichael Ellerman 	[C(ITLB)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
460f2699491SMichael Ellerman 		[C(OP_READ)] = {	0,		0x700	},
461f2699491SMichael Ellerman 		[C(OP_WRITE)] = {	-1,		-1	},
462f2699491SMichael Ellerman 		[C(OP_PREFETCH)] = {	-1,		-1	},
463f2699491SMichael Ellerman 	},
464f2699491SMichael Ellerman 	[C(BPU)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
465f2699491SMichael Ellerman 		[C(OP_READ)] = {	0x431,		0x327	},
466f2699491SMichael Ellerman 		[C(OP_WRITE)] = {	-1,		-1	},
467f2699491SMichael Ellerman 		[C(OP_PREFETCH)] = {	-1,		-1	},
468f2699491SMichael Ellerman 	},
469f2699491SMichael Ellerman 	[C(NODE)] = {		/* 	RESULT_ACCESS	RESULT_MISS */
470f2699491SMichael Ellerman 		[C(OP_READ)] = {	-1,		-1	},
471f2699491SMichael Ellerman 		[C(OP_WRITE)] = {	-1,		-1	},
472f2699491SMichael Ellerman 		[C(OP_PREFETCH)] = {	-1,		-1	},
473f2699491SMichael Ellerman 	},
474f2699491SMichael Ellerman };
475f2699491SMichael Ellerman 
476f2699491SMichael Ellerman static struct power_pmu ppc970_pmu = {
477f2699491SMichael Ellerman 	.name			= "PPC970/FX/MP",
478f2699491SMichael Ellerman 	.n_counter		= 8,
479f2699491SMichael Ellerman 	.max_alternatives	= 2,
480f2699491SMichael Ellerman 	.add_fields		= 0x001100005555ull,
481f2699491SMichael Ellerman 	.test_adder		= 0x013300000000ull,
482f2699491SMichael Ellerman 	.compute_mmcr		= p970_compute_mmcr,
483f2699491SMichael Ellerman 	.get_constraint		= p970_get_constraint,
484f2699491SMichael Ellerman 	.get_alternatives	= p970_get_alternatives,
485f2699491SMichael Ellerman 	.disable_pmc		= p970_disable_pmc,
486f2699491SMichael Ellerman 	.n_generic		= ARRAY_SIZE(ppc970_generic_events),
487f2699491SMichael Ellerman 	.generic_events		= ppc970_generic_events,
488f2699491SMichael Ellerman 	.cache_events		= &ppc970_cache_events,
4891ce447b9SBenjamin Herrenschmidt 	.flags			= PPMU_NO_SIPR | PPMU_NO_CONT_SAMPLING,
490f2699491SMichael Ellerman };
491f2699491SMichael Ellerman 
init_ppc970_pmu(void)492c49f5d88SNick Child int __init init_ppc970_pmu(void)
493f2699491SMichael Ellerman {
494*ec3eb9d9SRashmica Gupta 	unsigned int pvr = mfspr(SPRN_PVR);
495*ec3eb9d9SRashmica Gupta 
496*ec3eb9d9SRashmica Gupta 	if (PVR_VER(pvr) != PVR_970 && PVR_VER(pvr) != PVR_970MP &&
497*ec3eb9d9SRashmica Gupta 	    PVR_VER(pvr) != PVR_970FX && PVR_VER(pvr) != PVR_970GX)
498f2699491SMichael Ellerman 		return -ENODEV;
499f2699491SMichael Ellerman 
500f2699491SMichael Ellerman 	return register_power_pmu(&ppc970_pmu);
501f2699491SMichael Ellerman }
502