xref: /linux/arch/arm/include/asm/arm_pmuv3.h (revision 403edfa436286b21f5ffe6856ae5b36396e8966c)
1009d6dc8SMarc Zyngier /* SPDX-License-Identifier: GPL-2.0 */
2009d6dc8SMarc Zyngier /*
3009d6dc8SMarc Zyngier  * Copyright (C) 2012 ARM Ltd.
4009d6dc8SMarc Zyngier  */
5009d6dc8SMarc Zyngier 
6009d6dc8SMarc Zyngier #ifndef __ASM_PMUV3_H
7009d6dc8SMarc Zyngier #define __ASM_PMUV3_H
8009d6dc8SMarc Zyngier 
9009d6dc8SMarc Zyngier #include <asm/cp15.h>
10009d6dc8SMarc Zyngier #include <asm/cputype.h>
11009d6dc8SMarc Zyngier 
12009d6dc8SMarc Zyngier #define PMCCNTR			__ACCESS_CP15_64(0, c9)
13009d6dc8SMarc Zyngier 
14009d6dc8SMarc Zyngier #define PMCR			__ACCESS_CP15(c9,  0, c12, 0)
15009d6dc8SMarc Zyngier #define PMCNTENSET		__ACCESS_CP15(c9,  0, c12, 1)
16009d6dc8SMarc Zyngier #define PMCNTENCLR		__ACCESS_CP15(c9,  0, c12, 2)
17009d6dc8SMarc Zyngier #define PMOVSR			__ACCESS_CP15(c9,  0, c12, 3)
18009d6dc8SMarc Zyngier #define PMSELR			__ACCESS_CP15(c9,  0, c12, 5)
19009d6dc8SMarc Zyngier #define PMCEID0			__ACCESS_CP15(c9,  0, c12, 6)
20009d6dc8SMarc Zyngier #define PMCEID1			__ACCESS_CP15(c9,  0, c12, 7)
21009d6dc8SMarc Zyngier #define PMXEVTYPER		__ACCESS_CP15(c9,  0, c13, 1)
22009d6dc8SMarc Zyngier #define PMXEVCNTR		__ACCESS_CP15(c9,  0, c13, 2)
23009d6dc8SMarc Zyngier #define PMUSERENR		__ACCESS_CP15(c9,  0, c14, 0)
24009d6dc8SMarc Zyngier #define PMINTENSET		__ACCESS_CP15(c9,  0, c14, 1)
25009d6dc8SMarc Zyngier #define PMINTENCLR		__ACCESS_CP15(c9,  0, c14, 2)
26*403edfa4SIlkka Koskinen #define PMCEID2			__ACCESS_CP15(c9,  0, c14, 4)
27*403edfa4SIlkka Koskinen #define PMCEID3			__ACCESS_CP15(c9,  0, c14, 5)
28009d6dc8SMarc Zyngier #define PMMIR			__ACCESS_CP15(c9,  0, c14, 6)
29009d6dc8SMarc Zyngier #define PMCCFILTR		__ACCESS_CP15(c14, 0, c15, 7)
30009d6dc8SMarc Zyngier 
31009d6dc8SMarc Zyngier #define PMEVCNTR0		__ACCESS_CP15(c14, 0, c8, 0)
32009d6dc8SMarc Zyngier #define PMEVCNTR1		__ACCESS_CP15(c14, 0, c8, 1)
33009d6dc8SMarc Zyngier #define PMEVCNTR2		__ACCESS_CP15(c14, 0, c8, 2)
34009d6dc8SMarc Zyngier #define PMEVCNTR3		__ACCESS_CP15(c14, 0, c8, 3)
35009d6dc8SMarc Zyngier #define PMEVCNTR4		__ACCESS_CP15(c14, 0, c8, 4)
36009d6dc8SMarc Zyngier #define PMEVCNTR5		__ACCESS_CP15(c14, 0, c8, 5)
37009d6dc8SMarc Zyngier #define PMEVCNTR6		__ACCESS_CP15(c14, 0, c8, 6)
38009d6dc8SMarc Zyngier #define PMEVCNTR7		__ACCESS_CP15(c14, 0, c8, 7)
39009d6dc8SMarc Zyngier #define PMEVCNTR8		__ACCESS_CP15(c14, 0, c9, 0)
40009d6dc8SMarc Zyngier #define PMEVCNTR9		__ACCESS_CP15(c14, 0, c9, 1)
41009d6dc8SMarc Zyngier #define PMEVCNTR10		__ACCESS_CP15(c14, 0, c9, 2)
42009d6dc8SMarc Zyngier #define PMEVCNTR11		__ACCESS_CP15(c14, 0, c9, 3)
43009d6dc8SMarc Zyngier #define PMEVCNTR12		__ACCESS_CP15(c14, 0, c9, 4)
44009d6dc8SMarc Zyngier #define PMEVCNTR13		__ACCESS_CP15(c14, 0, c9, 5)
45009d6dc8SMarc Zyngier #define PMEVCNTR14		__ACCESS_CP15(c14, 0, c9, 6)
46009d6dc8SMarc Zyngier #define PMEVCNTR15		__ACCESS_CP15(c14, 0, c9, 7)
47009d6dc8SMarc Zyngier #define PMEVCNTR16		__ACCESS_CP15(c14, 0, c10, 0)
48009d6dc8SMarc Zyngier #define PMEVCNTR17		__ACCESS_CP15(c14, 0, c10, 1)
49009d6dc8SMarc Zyngier #define PMEVCNTR18		__ACCESS_CP15(c14, 0, c10, 2)
50009d6dc8SMarc Zyngier #define PMEVCNTR19		__ACCESS_CP15(c14, 0, c10, 3)
51009d6dc8SMarc Zyngier #define PMEVCNTR20		__ACCESS_CP15(c14, 0, c10, 4)
52009d6dc8SMarc Zyngier #define PMEVCNTR21		__ACCESS_CP15(c14, 0, c10, 5)
53009d6dc8SMarc Zyngier #define PMEVCNTR22		__ACCESS_CP15(c14, 0, c10, 6)
54009d6dc8SMarc Zyngier #define PMEVCNTR23		__ACCESS_CP15(c14, 0, c10, 7)
55009d6dc8SMarc Zyngier #define PMEVCNTR24		__ACCESS_CP15(c14, 0, c11, 0)
56009d6dc8SMarc Zyngier #define PMEVCNTR25		__ACCESS_CP15(c14, 0, c11, 1)
57009d6dc8SMarc Zyngier #define PMEVCNTR26		__ACCESS_CP15(c14, 0, c11, 2)
58009d6dc8SMarc Zyngier #define PMEVCNTR27		__ACCESS_CP15(c14, 0, c11, 3)
59009d6dc8SMarc Zyngier #define PMEVCNTR28		__ACCESS_CP15(c14, 0, c11, 4)
60009d6dc8SMarc Zyngier #define PMEVCNTR29		__ACCESS_CP15(c14, 0, c11, 5)
61009d6dc8SMarc Zyngier #define PMEVCNTR30		__ACCESS_CP15(c14, 0, c11, 6)
62009d6dc8SMarc Zyngier 
63009d6dc8SMarc Zyngier #define PMEVTYPER0		__ACCESS_CP15(c14, 0, c12, 0)
64009d6dc8SMarc Zyngier #define PMEVTYPER1		__ACCESS_CP15(c14, 0, c12, 1)
65009d6dc8SMarc Zyngier #define PMEVTYPER2		__ACCESS_CP15(c14, 0, c12, 2)
66009d6dc8SMarc Zyngier #define PMEVTYPER3		__ACCESS_CP15(c14, 0, c12, 3)
67009d6dc8SMarc Zyngier #define PMEVTYPER4		__ACCESS_CP15(c14, 0, c12, 4)
68009d6dc8SMarc Zyngier #define PMEVTYPER5		__ACCESS_CP15(c14, 0, c12, 5)
69009d6dc8SMarc Zyngier #define PMEVTYPER6		__ACCESS_CP15(c14, 0, c12, 6)
70009d6dc8SMarc Zyngier #define PMEVTYPER7		__ACCESS_CP15(c14, 0, c12, 7)
71009d6dc8SMarc Zyngier #define PMEVTYPER8		__ACCESS_CP15(c14, 0, c13, 0)
72009d6dc8SMarc Zyngier #define PMEVTYPER9		__ACCESS_CP15(c14, 0, c13, 1)
73009d6dc8SMarc Zyngier #define PMEVTYPER10		__ACCESS_CP15(c14, 0, c13, 2)
74009d6dc8SMarc Zyngier #define PMEVTYPER11		__ACCESS_CP15(c14, 0, c13, 3)
75009d6dc8SMarc Zyngier #define PMEVTYPER12		__ACCESS_CP15(c14, 0, c13, 4)
76009d6dc8SMarc Zyngier #define PMEVTYPER13		__ACCESS_CP15(c14, 0, c13, 5)
77009d6dc8SMarc Zyngier #define PMEVTYPER14		__ACCESS_CP15(c14, 0, c13, 6)
78009d6dc8SMarc Zyngier #define PMEVTYPER15		__ACCESS_CP15(c14, 0, c13, 7)
79009d6dc8SMarc Zyngier #define PMEVTYPER16		__ACCESS_CP15(c14, 0, c14, 0)
80009d6dc8SMarc Zyngier #define PMEVTYPER17		__ACCESS_CP15(c14, 0, c14, 1)
81009d6dc8SMarc Zyngier #define PMEVTYPER18		__ACCESS_CP15(c14, 0, c14, 2)
82009d6dc8SMarc Zyngier #define PMEVTYPER19		__ACCESS_CP15(c14, 0, c14, 3)
83009d6dc8SMarc Zyngier #define PMEVTYPER20		__ACCESS_CP15(c14, 0, c14, 4)
84009d6dc8SMarc Zyngier #define PMEVTYPER21		__ACCESS_CP15(c14, 0, c14, 5)
85009d6dc8SMarc Zyngier #define PMEVTYPER22		__ACCESS_CP15(c14, 0, c14, 6)
86009d6dc8SMarc Zyngier #define PMEVTYPER23		__ACCESS_CP15(c14, 0, c14, 7)
87009d6dc8SMarc Zyngier #define PMEVTYPER24		__ACCESS_CP15(c14, 0, c15, 0)
88009d6dc8SMarc Zyngier #define PMEVTYPER25		__ACCESS_CP15(c14, 0, c15, 1)
89009d6dc8SMarc Zyngier #define PMEVTYPER26		__ACCESS_CP15(c14, 0, c15, 2)
90009d6dc8SMarc Zyngier #define PMEVTYPER27		__ACCESS_CP15(c14, 0, c15, 3)
91009d6dc8SMarc Zyngier #define PMEVTYPER28		__ACCESS_CP15(c14, 0, c15, 4)
92009d6dc8SMarc Zyngier #define PMEVTYPER29		__ACCESS_CP15(c14, 0, c15, 5)
93009d6dc8SMarc Zyngier #define PMEVTYPER30		__ACCESS_CP15(c14, 0, c15, 6)
94009d6dc8SMarc Zyngier 
95009d6dc8SMarc Zyngier #define RETURN_READ_PMEVCNTRN(n) \
96009d6dc8SMarc Zyngier 	return read_sysreg(PMEVCNTR##n)
9768e3f61eSGeert Uytterhoeven static inline unsigned long read_pmevcntrn(int n)
98009d6dc8SMarc Zyngier {
99009d6dc8SMarc Zyngier 	PMEVN_SWITCH(n, RETURN_READ_PMEVCNTRN);
100009d6dc8SMarc Zyngier 	return 0;
101009d6dc8SMarc Zyngier }
102009d6dc8SMarc Zyngier 
103009d6dc8SMarc Zyngier #define WRITE_PMEVCNTRN(n) \
104009d6dc8SMarc Zyngier 	write_sysreg(val, PMEVCNTR##n)
10568e3f61eSGeert Uytterhoeven static inline void write_pmevcntrn(int n, unsigned long val)
106009d6dc8SMarc Zyngier {
107009d6dc8SMarc Zyngier 	PMEVN_SWITCH(n, WRITE_PMEVCNTRN);
108009d6dc8SMarc Zyngier }
109009d6dc8SMarc Zyngier 
110009d6dc8SMarc Zyngier #define WRITE_PMEVTYPERN(n) \
111009d6dc8SMarc Zyngier 	write_sysreg(val, PMEVTYPER##n)
11268e3f61eSGeert Uytterhoeven static inline void write_pmevtypern(int n, unsigned long val)
113009d6dc8SMarc Zyngier {
114009d6dc8SMarc Zyngier 	PMEVN_SWITCH(n, WRITE_PMEVTYPERN);
115009d6dc8SMarc Zyngier }
116009d6dc8SMarc Zyngier 
117009d6dc8SMarc Zyngier static inline unsigned long read_pmmir(void)
118009d6dc8SMarc Zyngier {
119009d6dc8SMarc Zyngier 	return read_sysreg(PMMIR);
120009d6dc8SMarc Zyngier }
121009d6dc8SMarc Zyngier 
122009d6dc8SMarc Zyngier static inline u32 read_pmuver(void)
123009d6dc8SMarc Zyngier {
124009d6dc8SMarc Zyngier 	/* PMUVers is not a signed field */
125009d6dc8SMarc Zyngier 	u32 dfr0 = read_cpuid_ext(CPUID_EXT_DFR0);
126009d6dc8SMarc Zyngier 
127009d6dc8SMarc Zyngier 	return (dfr0 >> 24) & 0xf;
128009d6dc8SMarc Zyngier }
129009d6dc8SMarc Zyngier 
130009d6dc8SMarc Zyngier static inline void write_pmcr(u32 val)
131009d6dc8SMarc Zyngier {
132009d6dc8SMarc Zyngier 	write_sysreg(val, PMCR);
133009d6dc8SMarc Zyngier }
134009d6dc8SMarc Zyngier 
135009d6dc8SMarc Zyngier static inline u32 read_pmcr(void)
136009d6dc8SMarc Zyngier {
137009d6dc8SMarc Zyngier 	return read_sysreg(PMCR);
138009d6dc8SMarc Zyngier }
139009d6dc8SMarc Zyngier 
140009d6dc8SMarc Zyngier static inline void write_pmselr(u32 val)
141009d6dc8SMarc Zyngier {
142009d6dc8SMarc Zyngier 	write_sysreg(val, PMSELR);
143009d6dc8SMarc Zyngier }
144009d6dc8SMarc Zyngier 
145009d6dc8SMarc Zyngier static inline void write_pmccntr(u64 val)
146009d6dc8SMarc Zyngier {
147009d6dc8SMarc Zyngier 	write_sysreg(val, PMCCNTR);
148009d6dc8SMarc Zyngier }
149009d6dc8SMarc Zyngier 
150009d6dc8SMarc Zyngier static inline u64 read_pmccntr(void)
151009d6dc8SMarc Zyngier {
152009d6dc8SMarc Zyngier 	return read_sysreg(PMCCNTR);
153009d6dc8SMarc Zyngier }
154009d6dc8SMarc Zyngier 
155009d6dc8SMarc Zyngier static inline void write_pmcntenset(u32 val)
156009d6dc8SMarc Zyngier {
157009d6dc8SMarc Zyngier 	write_sysreg(val, PMCNTENSET);
158009d6dc8SMarc Zyngier }
159009d6dc8SMarc Zyngier 
160009d6dc8SMarc Zyngier static inline void write_pmcntenclr(u32 val)
161009d6dc8SMarc Zyngier {
162009d6dc8SMarc Zyngier 	write_sysreg(val, PMCNTENCLR);
163009d6dc8SMarc Zyngier }
164009d6dc8SMarc Zyngier 
165009d6dc8SMarc Zyngier static inline void write_pmintenset(u32 val)
166009d6dc8SMarc Zyngier {
167009d6dc8SMarc Zyngier 	write_sysreg(val, PMINTENSET);
168009d6dc8SMarc Zyngier }
169009d6dc8SMarc Zyngier 
170009d6dc8SMarc Zyngier static inline void write_pmintenclr(u32 val)
171009d6dc8SMarc Zyngier {
172009d6dc8SMarc Zyngier 	write_sysreg(val, PMINTENCLR);
173009d6dc8SMarc Zyngier }
174009d6dc8SMarc Zyngier 
175009d6dc8SMarc Zyngier static inline void write_pmccfiltr(u32 val)
176009d6dc8SMarc Zyngier {
177009d6dc8SMarc Zyngier 	write_sysreg(val, PMCCFILTR);
178009d6dc8SMarc Zyngier }
179009d6dc8SMarc Zyngier 
180009d6dc8SMarc Zyngier static inline void write_pmovsclr(u32 val)
181009d6dc8SMarc Zyngier {
182009d6dc8SMarc Zyngier 	write_sysreg(val, PMOVSR);
183009d6dc8SMarc Zyngier }
184009d6dc8SMarc Zyngier 
185009d6dc8SMarc Zyngier static inline u32 read_pmovsclr(void)
186009d6dc8SMarc Zyngier {
187009d6dc8SMarc Zyngier 	return read_sysreg(PMOVSR);
188009d6dc8SMarc Zyngier }
189009d6dc8SMarc Zyngier 
190009d6dc8SMarc Zyngier static inline void write_pmuserenr(u32 val)
191009d6dc8SMarc Zyngier {
192009d6dc8SMarc Zyngier 	write_sysreg(val, PMUSERENR);
193009d6dc8SMarc Zyngier }
194009d6dc8SMarc Zyngier 
195009d6dc8SMarc Zyngier static inline void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr) {}
196009d6dc8SMarc Zyngier static inline void kvm_clr_pmu_events(u32 clr) {}
197009d6dc8SMarc Zyngier static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr)
198009d6dc8SMarc Zyngier {
199009d6dc8SMarc Zyngier 	return false;
200009d6dc8SMarc Zyngier }
201009d6dc8SMarc Zyngier 
2020c2f9acfSReiji Watanabe static inline bool kvm_set_pmuserenr(u64 val)
2030c2f9acfSReiji Watanabe {
2040c2f9acfSReiji Watanabe 	return false;
2050c2f9acfSReiji Watanabe }
2060c2f9acfSReiji Watanabe 
207b1f778a2SMarc Zyngier static inline void kvm_vcpu_pmu_resync_el0(void) {}
208b1f778a2SMarc Zyngier 
209009d6dc8SMarc Zyngier /* PMU Version in DFR Register */
210009d6dc8SMarc Zyngier #define ARMV8_PMU_DFR_VER_NI        0
211*403edfa4SIlkka Koskinen #define ARMV8_PMU_DFR_VER_V3P1      0x4
212009d6dc8SMarc Zyngier #define ARMV8_PMU_DFR_VER_V3P4      0x5
213009d6dc8SMarc Zyngier #define ARMV8_PMU_DFR_VER_V3P5      0x6
214009d6dc8SMarc Zyngier #define ARMV8_PMU_DFR_VER_IMP_DEF   0xF
215009d6dc8SMarc Zyngier 
216009d6dc8SMarc Zyngier static inline bool pmuv3_implemented(int pmuver)
217009d6dc8SMarc Zyngier {
218009d6dc8SMarc Zyngier 	return !(pmuver == ARMV8_PMU_DFR_VER_IMP_DEF ||
219009d6dc8SMarc Zyngier 		 pmuver == ARMV8_PMU_DFR_VER_NI);
220009d6dc8SMarc Zyngier }
221009d6dc8SMarc Zyngier 
222009d6dc8SMarc Zyngier static inline bool is_pmuv3p4(int pmuver)
223009d6dc8SMarc Zyngier {
224009d6dc8SMarc Zyngier 	return pmuver >= ARMV8_PMU_DFR_VER_V3P4;
225009d6dc8SMarc Zyngier }
226009d6dc8SMarc Zyngier 
227009d6dc8SMarc Zyngier static inline bool is_pmuv3p5(int pmuver)
228009d6dc8SMarc Zyngier {
229009d6dc8SMarc Zyngier 	return pmuver >= ARMV8_PMU_DFR_VER_V3P5;
230009d6dc8SMarc Zyngier }
231009d6dc8SMarc Zyngier 
232*403edfa4SIlkka Koskinen static inline u64 read_pmceid0(void)
233*403edfa4SIlkka Koskinen {
234*403edfa4SIlkka Koskinen 	u64 val = read_sysreg(PMCEID0);
235*403edfa4SIlkka Koskinen 
236*403edfa4SIlkka Koskinen 	if (read_pmuver() >= ARMV8_PMU_DFR_VER_V3P1)
237*403edfa4SIlkka Koskinen 		val |= (u64)read_sysreg(PMCEID2) << 32;
238*403edfa4SIlkka Koskinen 
239*403edfa4SIlkka Koskinen 	return val;
240*403edfa4SIlkka Koskinen }
241*403edfa4SIlkka Koskinen 
242*403edfa4SIlkka Koskinen static inline u64 read_pmceid1(void)
243*403edfa4SIlkka Koskinen {
244*403edfa4SIlkka Koskinen 	u64 val = read_sysreg(PMCEID1);
245*403edfa4SIlkka Koskinen 
246*403edfa4SIlkka Koskinen 	if (read_pmuver() >= ARMV8_PMU_DFR_VER_V3P1)
247*403edfa4SIlkka Koskinen 		val |= (u64)read_sysreg(PMCEID3) << 32;
248*403edfa4SIlkka Koskinen 
249*403edfa4SIlkka Koskinen 	return val;
250*403edfa4SIlkka Koskinen }
251*403edfa4SIlkka Koskinen 
252009d6dc8SMarc Zyngier #endif
253