xref: /aosp_15_r20/external/arm-trusted-firmware/lib/extensions/amu/aarch64/amu.c (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park  * Copyright (c) 2017-2021, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park  *
4*54fd6939SJiyong Park  * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park  */
6*54fd6939SJiyong Park 
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park #include <cdefs.h>
9*54fd6939SJiyong Park #include <inttypes.h>
10*54fd6939SJiyong Park #include <stdbool.h>
11*54fd6939SJiyong Park #include <stdint.h>
12*54fd6939SJiyong Park 
13*54fd6939SJiyong Park #include "../amu_private.h"
14*54fd6939SJiyong Park #include <arch.h>
15*54fd6939SJiyong Park #include <arch_features.h>
16*54fd6939SJiyong Park #include <arch_helpers.h>
17*54fd6939SJiyong Park #include <common/debug.h>
18*54fd6939SJiyong Park #include <lib/el3_runtime/pubsub_events.h>
19*54fd6939SJiyong Park #include <lib/extensions/amu.h>
20*54fd6939SJiyong Park 
21*54fd6939SJiyong Park #include <plat/common/platform.h>
22*54fd6939SJiyong Park 
23*54fd6939SJiyong Park #if ENABLE_AMU_FCONF
24*54fd6939SJiyong Park #	include <lib/fconf/fconf.h>
25*54fd6939SJiyong Park #	include <lib/fconf/fconf_amu_getter.h>
26*54fd6939SJiyong Park #endif
27*54fd6939SJiyong Park 
28*54fd6939SJiyong Park #if ENABLE_MPMM
29*54fd6939SJiyong Park #	include <lib/mpmm/mpmm.h>
30*54fd6939SJiyong Park #endif
31*54fd6939SJiyong Park 
32*54fd6939SJiyong Park struct amu_ctx {
33*54fd6939SJiyong Park 	uint64_t group0_cnts[AMU_GROUP0_MAX_COUNTERS];
34*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
35*54fd6939SJiyong Park 	uint64_t group1_cnts[AMU_GROUP1_MAX_COUNTERS];
36*54fd6939SJiyong Park #endif
37*54fd6939SJiyong Park 
38*54fd6939SJiyong Park 	/* Architected event counter 1 does not have an offset register */
39*54fd6939SJiyong Park 	uint64_t group0_voffsets[AMU_GROUP0_MAX_COUNTERS - 1U];
40*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
41*54fd6939SJiyong Park 	uint64_t group1_voffsets[AMU_GROUP1_MAX_COUNTERS];
42*54fd6939SJiyong Park #endif
43*54fd6939SJiyong Park 
44*54fd6939SJiyong Park 	uint16_t group0_enable;
45*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
46*54fd6939SJiyong Park 	uint16_t group1_enable;
47*54fd6939SJiyong Park #endif
48*54fd6939SJiyong Park };
49*54fd6939SJiyong Park 
50*54fd6939SJiyong Park static struct amu_ctx amu_ctxs_[PLATFORM_CORE_COUNT];
51*54fd6939SJiyong Park 
52*54fd6939SJiyong Park CASSERT((sizeof(amu_ctxs_[0].group0_enable) * CHAR_BIT) <= AMU_GROUP0_MAX_COUNTERS,
53*54fd6939SJiyong Park 	amu_ctx_group0_enable_cannot_represent_all_group0_counters);
54*54fd6939SJiyong Park 
55*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
56*54fd6939SJiyong Park CASSERT((sizeof(amu_ctxs_[0].group1_enable) * CHAR_BIT) <= AMU_GROUP1_MAX_COUNTERS,
57*54fd6939SJiyong Park 	amu_ctx_group1_enable_cannot_represent_all_group1_counters);
58*54fd6939SJiyong Park #endif
59*54fd6939SJiyong Park 
read_id_aa64pfr0_el1_amu(void)60*54fd6939SJiyong Park static inline __unused uint64_t read_id_aa64pfr0_el1_amu(void)
61*54fd6939SJiyong Park {
62*54fd6939SJiyong Park 	return (read_id_aa64pfr0_el1() >> ID_AA64PFR0_AMU_SHIFT) &
63*54fd6939SJiyong Park 		ID_AA64PFR0_AMU_MASK;
64*54fd6939SJiyong Park }
65*54fd6939SJiyong Park 
read_hcr_el2_amvoffen(void)66*54fd6939SJiyong Park static inline __unused uint64_t read_hcr_el2_amvoffen(void)
67*54fd6939SJiyong Park {
68*54fd6939SJiyong Park 	return (read_hcr_el2() & HCR_AMVOFFEN_BIT) >>
69*54fd6939SJiyong Park 		HCR_AMVOFFEN_SHIFT;
70*54fd6939SJiyong Park }
71*54fd6939SJiyong Park 
write_cptr_el2_tam(uint64_t value)72*54fd6939SJiyong Park static inline __unused void write_cptr_el2_tam(uint64_t value)
73*54fd6939SJiyong Park {
74*54fd6939SJiyong Park 	write_cptr_el2((read_cptr_el2() & ~CPTR_EL2_TAM_BIT) |
75*54fd6939SJiyong Park 		((value << CPTR_EL2_TAM_SHIFT) & CPTR_EL2_TAM_BIT));
76*54fd6939SJiyong Park }
77*54fd6939SJiyong Park 
write_cptr_el3_tam(cpu_context_t * ctx,uint64_t tam)78*54fd6939SJiyong Park static inline __unused void write_cptr_el3_tam(cpu_context_t *ctx, uint64_t tam)
79*54fd6939SJiyong Park {
80*54fd6939SJiyong Park 	uint64_t value = read_ctx_reg(get_el3state_ctx(ctx), CTX_CPTR_EL3);
81*54fd6939SJiyong Park 
82*54fd6939SJiyong Park 	value &= ~TAM_BIT;
83*54fd6939SJiyong Park 	value |= (tam << TAM_SHIFT) & TAM_BIT;
84*54fd6939SJiyong Park 
85*54fd6939SJiyong Park 	write_ctx_reg(get_el3state_ctx(ctx), CTX_CPTR_EL3, value);
86*54fd6939SJiyong Park }
87*54fd6939SJiyong Park 
write_hcr_el2_amvoffen(uint64_t value)88*54fd6939SJiyong Park static inline __unused void write_hcr_el2_amvoffen(uint64_t value)
89*54fd6939SJiyong Park {
90*54fd6939SJiyong Park 	write_hcr_el2((read_hcr_el2() & ~HCR_AMVOFFEN_BIT) |
91*54fd6939SJiyong Park 		((value << HCR_AMVOFFEN_SHIFT) & HCR_AMVOFFEN_BIT));
92*54fd6939SJiyong Park }
93*54fd6939SJiyong Park 
write_amcr_el0_cg1rz(uint64_t value)94*54fd6939SJiyong Park static inline __unused void write_amcr_el0_cg1rz(uint64_t value)
95*54fd6939SJiyong Park {
96*54fd6939SJiyong Park 	write_amcr_el0((read_amcr_el0() & ~AMCR_CG1RZ_BIT) |
97*54fd6939SJiyong Park 		((value << AMCR_CG1RZ_SHIFT) & AMCR_CG1RZ_BIT));
98*54fd6939SJiyong Park }
99*54fd6939SJiyong Park 
read_amcfgr_el0_ncg(void)100*54fd6939SJiyong Park static inline __unused uint64_t read_amcfgr_el0_ncg(void)
101*54fd6939SJiyong Park {
102*54fd6939SJiyong Park 	return (read_amcfgr_el0() >> AMCFGR_EL0_NCG_SHIFT) &
103*54fd6939SJiyong Park 		AMCFGR_EL0_NCG_MASK;
104*54fd6939SJiyong Park }
105*54fd6939SJiyong Park 
read_amcgcr_el0_cg0nc(void)106*54fd6939SJiyong Park static inline __unused uint64_t read_amcgcr_el0_cg0nc(void)
107*54fd6939SJiyong Park {
108*54fd6939SJiyong Park 	return (read_amcgcr_el0() >> AMCGCR_EL0_CG0NC_SHIFT) &
109*54fd6939SJiyong Park 		AMCGCR_EL0_CG0NC_MASK;
110*54fd6939SJiyong Park }
111*54fd6939SJiyong Park 
read_amcg1idr_el0_voff(void)112*54fd6939SJiyong Park static inline __unused uint64_t read_amcg1idr_el0_voff(void)
113*54fd6939SJiyong Park {
114*54fd6939SJiyong Park 	return (read_amcg1idr_el0() >> AMCG1IDR_VOFF_SHIFT) &
115*54fd6939SJiyong Park 		AMCG1IDR_VOFF_MASK;
116*54fd6939SJiyong Park }
117*54fd6939SJiyong Park 
read_amcgcr_el0_cg1nc(void)118*54fd6939SJiyong Park static inline __unused uint64_t read_amcgcr_el0_cg1nc(void)
119*54fd6939SJiyong Park {
120*54fd6939SJiyong Park 	return (read_amcgcr_el0() >> AMCGCR_EL0_CG1NC_SHIFT) &
121*54fd6939SJiyong Park 		AMCGCR_EL0_CG1NC_MASK;
122*54fd6939SJiyong Park }
123*54fd6939SJiyong Park 
read_amcntenset0_el0_px(void)124*54fd6939SJiyong Park static inline __unused uint64_t read_amcntenset0_el0_px(void)
125*54fd6939SJiyong Park {
126*54fd6939SJiyong Park 	return (read_amcntenset0_el0() >> AMCNTENSET0_EL0_Pn_SHIFT) &
127*54fd6939SJiyong Park 		AMCNTENSET0_EL0_Pn_MASK;
128*54fd6939SJiyong Park }
129*54fd6939SJiyong Park 
read_amcntenset1_el0_px(void)130*54fd6939SJiyong Park static inline __unused uint64_t read_amcntenset1_el0_px(void)
131*54fd6939SJiyong Park {
132*54fd6939SJiyong Park 	return (read_amcntenset1_el0() >> AMCNTENSET1_EL0_Pn_SHIFT) &
133*54fd6939SJiyong Park 		AMCNTENSET1_EL0_Pn_MASK;
134*54fd6939SJiyong Park }
135*54fd6939SJiyong Park 
write_amcntenset0_el0_px(uint64_t px)136*54fd6939SJiyong Park static inline __unused void write_amcntenset0_el0_px(uint64_t px)
137*54fd6939SJiyong Park {
138*54fd6939SJiyong Park 	uint64_t value = read_amcntenset0_el0();
139*54fd6939SJiyong Park 
140*54fd6939SJiyong Park 	value &= ~AMCNTENSET0_EL0_Pn_MASK;
141*54fd6939SJiyong Park 	value |= (px << AMCNTENSET0_EL0_Pn_SHIFT) & AMCNTENSET0_EL0_Pn_MASK;
142*54fd6939SJiyong Park 
143*54fd6939SJiyong Park 	write_amcntenset0_el0(value);
144*54fd6939SJiyong Park }
145*54fd6939SJiyong Park 
write_amcntenset1_el0_px(uint64_t px)146*54fd6939SJiyong Park static inline __unused void write_amcntenset1_el0_px(uint64_t px)
147*54fd6939SJiyong Park {
148*54fd6939SJiyong Park 	uint64_t value = read_amcntenset1_el0();
149*54fd6939SJiyong Park 
150*54fd6939SJiyong Park 	value &= ~AMCNTENSET1_EL0_Pn_MASK;
151*54fd6939SJiyong Park 	value |= (px << AMCNTENSET1_EL0_Pn_SHIFT) & AMCNTENSET1_EL0_Pn_MASK;
152*54fd6939SJiyong Park 
153*54fd6939SJiyong Park 	write_amcntenset1_el0(value);
154*54fd6939SJiyong Park }
155*54fd6939SJiyong Park 
write_amcntenclr0_el0_px(uint64_t px)156*54fd6939SJiyong Park static inline __unused void write_amcntenclr0_el0_px(uint64_t px)
157*54fd6939SJiyong Park {
158*54fd6939SJiyong Park 	uint64_t value = read_amcntenclr0_el0();
159*54fd6939SJiyong Park 
160*54fd6939SJiyong Park 	value &= ~AMCNTENCLR0_EL0_Pn_MASK;
161*54fd6939SJiyong Park 	value |= (px << AMCNTENCLR0_EL0_Pn_SHIFT) & AMCNTENCLR0_EL0_Pn_MASK;
162*54fd6939SJiyong Park 
163*54fd6939SJiyong Park 	write_amcntenclr0_el0(value);
164*54fd6939SJiyong Park }
165*54fd6939SJiyong Park 
write_amcntenclr1_el0_px(uint64_t px)166*54fd6939SJiyong Park static inline __unused void write_amcntenclr1_el0_px(uint64_t px)
167*54fd6939SJiyong Park {
168*54fd6939SJiyong Park 	uint64_t value = read_amcntenclr1_el0();
169*54fd6939SJiyong Park 
170*54fd6939SJiyong Park 	value &= ~AMCNTENCLR1_EL0_Pn_MASK;
171*54fd6939SJiyong Park 	value |= (px << AMCNTENCLR1_EL0_Pn_SHIFT) & AMCNTENCLR1_EL0_Pn_MASK;
172*54fd6939SJiyong Park 
173*54fd6939SJiyong Park 	write_amcntenclr1_el0(value);
174*54fd6939SJiyong Park }
175*54fd6939SJiyong Park 
amu_supported(void)176*54fd6939SJiyong Park static __unused bool amu_supported(void)
177*54fd6939SJiyong Park {
178*54fd6939SJiyong Park 	return read_id_aa64pfr0_el1_amu() >= ID_AA64PFR0_AMU_V1;
179*54fd6939SJiyong Park }
180*54fd6939SJiyong Park 
amu_v1p1_supported(void)181*54fd6939SJiyong Park static __unused bool amu_v1p1_supported(void)
182*54fd6939SJiyong Park {
183*54fd6939SJiyong Park 	return read_id_aa64pfr0_el1_amu() >= ID_AA64PFR0_AMU_V1P1;
184*54fd6939SJiyong Park }
185*54fd6939SJiyong Park 
186*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
amu_group1_supported(void)187*54fd6939SJiyong Park static __unused bool amu_group1_supported(void)
188*54fd6939SJiyong Park {
189*54fd6939SJiyong Park 	return read_amcfgr_el0_ncg() > 0U;
190*54fd6939SJiyong Park }
191*54fd6939SJiyong Park #endif
192*54fd6939SJiyong Park 
193*54fd6939SJiyong Park /*
194*54fd6939SJiyong Park  * Enable counters. This function is meant to be invoked by the context
195*54fd6939SJiyong Park  * management library before exiting from EL3.
196*54fd6939SJiyong Park  */
amu_enable(bool el2_unused,cpu_context_t * ctx)197*54fd6939SJiyong Park void amu_enable(bool el2_unused, cpu_context_t *ctx)
198*54fd6939SJiyong Park {
199*54fd6939SJiyong Park 	uint64_t id_aa64pfr0_el1_amu;		/* AMU version */
200*54fd6939SJiyong Park 
201*54fd6939SJiyong Park 	uint64_t amcfgr_el0_ncg;		/* Number of counter groups */
202*54fd6939SJiyong Park 	uint64_t amcgcr_el0_cg0nc;		/* Number of group 0 counters */
203*54fd6939SJiyong Park 
204*54fd6939SJiyong Park 	uint64_t amcntenset0_el0_px = 0x0;	/* Group 0 enable mask */
205*54fd6939SJiyong Park 	uint64_t amcntenset1_el0_px = 0x0;	/* Group 1 enable mask */
206*54fd6939SJiyong Park 
207*54fd6939SJiyong Park 	id_aa64pfr0_el1_amu = read_id_aa64pfr0_el1_amu();
208*54fd6939SJiyong Park 	if (id_aa64pfr0_el1_amu == ID_AA64PFR0_AMU_NOT_SUPPORTED) {
209*54fd6939SJiyong Park 		/*
210*54fd6939SJiyong Park 		 * If the AMU is unsupported, nothing needs to be done.
211*54fd6939SJiyong Park 		 */
212*54fd6939SJiyong Park 
213*54fd6939SJiyong Park 		return;
214*54fd6939SJiyong Park 	}
215*54fd6939SJiyong Park 
216*54fd6939SJiyong Park 	if (el2_unused) {
217*54fd6939SJiyong Park 		/*
218*54fd6939SJiyong Park 		 * CPTR_EL2.TAM: Set to zero so any accesses to the Activity
219*54fd6939SJiyong Park 		 * Monitor registers do not trap to EL2.
220*54fd6939SJiyong Park 		 */
221*54fd6939SJiyong Park 		write_cptr_el2_tam(0U);
222*54fd6939SJiyong Park 	}
223*54fd6939SJiyong Park 
224*54fd6939SJiyong Park 	/*
225*54fd6939SJiyong Park 	 * Retrieve and update the CPTR_EL3 value from the context mentioned
226*54fd6939SJiyong Park 	 * in 'ctx'. Set CPTR_EL3.TAM to zero so that any accesses to
227*54fd6939SJiyong Park 	 * the Activity Monitor registers do not trap to EL3.
228*54fd6939SJiyong Park 	 */
229*54fd6939SJiyong Park 	write_cptr_el3_tam(ctx, 0U);
230*54fd6939SJiyong Park 
231*54fd6939SJiyong Park 	/*
232*54fd6939SJiyong Park 	 * Retrieve the number of architected counters. All of these counters
233*54fd6939SJiyong Park 	 * are enabled by default.
234*54fd6939SJiyong Park 	 */
235*54fd6939SJiyong Park 
236*54fd6939SJiyong Park 	amcgcr_el0_cg0nc = read_amcgcr_el0_cg0nc();
237*54fd6939SJiyong Park 	amcntenset0_el0_px = (UINT64_C(1) << (amcgcr_el0_cg0nc)) - 1U;
238*54fd6939SJiyong Park 
239*54fd6939SJiyong Park 	assert(amcgcr_el0_cg0nc <= AMU_AMCGCR_CG0NC_MAX);
240*54fd6939SJiyong Park 
241*54fd6939SJiyong Park 	/*
242*54fd6939SJiyong Park 	 * The platform may opt to enable specific auxiliary counters. This can
243*54fd6939SJiyong Park 	 * be done via the common FCONF getter, or via the platform-implemented
244*54fd6939SJiyong Park 	 * function.
245*54fd6939SJiyong Park 	 */
246*54fd6939SJiyong Park 
247*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
248*54fd6939SJiyong Park 	const struct amu_topology *topology;
249*54fd6939SJiyong Park 
250*54fd6939SJiyong Park #if ENABLE_AMU_FCONF
251*54fd6939SJiyong Park 	topology = FCONF_GET_PROPERTY(amu, config, topology);
252*54fd6939SJiyong Park #else
253*54fd6939SJiyong Park 	topology = plat_amu_topology();
254*54fd6939SJiyong Park #endif /* ENABLE_AMU_FCONF */
255*54fd6939SJiyong Park 
256*54fd6939SJiyong Park 	if (topology != NULL) {
257*54fd6939SJiyong Park 		unsigned int core_pos = plat_my_core_pos();
258*54fd6939SJiyong Park 
259*54fd6939SJiyong Park 		amcntenset1_el0_px = topology->cores[core_pos].enable;
260*54fd6939SJiyong Park 	} else {
261*54fd6939SJiyong Park 		ERROR("AMU: failed to generate AMU topology\n");
262*54fd6939SJiyong Park 	}
263*54fd6939SJiyong Park #endif /* ENABLE_AMU_AUXILIARY_COUNTERS */
264*54fd6939SJiyong Park 
265*54fd6939SJiyong Park 	/*
266*54fd6939SJiyong Park 	 * Enable the requested counters.
267*54fd6939SJiyong Park 	 */
268*54fd6939SJiyong Park 
269*54fd6939SJiyong Park 	write_amcntenset0_el0_px(amcntenset0_el0_px);
270*54fd6939SJiyong Park 
271*54fd6939SJiyong Park 	amcfgr_el0_ncg = read_amcfgr_el0_ncg();
272*54fd6939SJiyong Park 	if (amcfgr_el0_ncg > 0U) {
273*54fd6939SJiyong Park 		write_amcntenset1_el0_px(amcntenset1_el0_px);
274*54fd6939SJiyong Park 
275*54fd6939SJiyong Park #if !ENABLE_AMU_AUXILIARY_COUNTERS
276*54fd6939SJiyong Park 		VERBOSE("AMU: auxiliary counters detected but support is disabled\n");
277*54fd6939SJiyong Park #endif
278*54fd6939SJiyong Park 	}
279*54fd6939SJiyong Park 
280*54fd6939SJiyong Park 	/* Initialize FEAT_AMUv1p1 features if present. */
281*54fd6939SJiyong Park 	if (id_aa64pfr0_el1_amu >= ID_AA64PFR0_AMU_V1P1) {
282*54fd6939SJiyong Park 		if (el2_unused) {
283*54fd6939SJiyong Park 			/*
284*54fd6939SJiyong Park 			 * Make sure virtual offsets are disabled if EL2 not
285*54fd6939SJiyong Park 			 * used.
286*54fd6939SJiyong Park 			 */
287*54fd6939SJiyong Park 			write_hcr_el2_amvoffen(0U);
288*54fd6939SJiyong Park 		}
289*54fd6939SJiyong Park 
290*54fd6939SJiyong Park #if AMU_RESTRICT_COUNTERS
291*54fd6939SJiyong Park 		/*
292*54fd6939SJiyong Park 		 * FEAT_AMUv1p1 adds a register field to restrict access to
293*54fd6939SJiyong Park 		 * group 1 counters at all but the highest implemented EL. This
294*54fd6939SJiyong Park 		 * is controlled with the `AMU_RESTRICT_COUNTERS` compile time
295*54fd6939SJiyong Park 		 * flag, when set, system register reads at lower ELs return
296*54fd6939SJiyong Park 		 * zero. Reads from the memory mapped view are unaffected.
297*54fd6939SJiyong Park 		 */
298*54fd6939SJiyong Park 		VERBOSE("AMU group 1 counter access restricted.\n");
299*54fd6939SJiyong Park 		write_amcr_el0_cg1rz(1U);
300*54fd6939SJiyong Park #else
301*54fd6939SJiyong Park 		write_amcr_el0_cg1rz(0U);
302*54fd6939SJiyong Park #endif
303*54fd6939SJiyong Park 	}
304*54fd6939SJiyong Park 
305*54fd6939SJiyong Park #if ENABLE_MPMM
306*54fd6939SJiyong Park 	mpmm_enable();
307*54fd6939SJiyong Park #endif
308*54fd6939SJiyong Park }
309*54fd6939SJiyong Park 
310*54fd6939SJiyong Park /* Read the group 0 counter identified by the given `idx`. */
amu_group0_cnt_read(unsigned int idx)311*54fd6939SJiyong Park static uint64_t amu_group0_cnt_read(unsigned int idx)
312*54fd6939SJiyong Park {
313*54fd6939SJiyong Park 	assert(amu_supported());
314*54fd6939SJiyong Park 	assert(idx < read_amcgcr_el0_cg0nc());
315*54fd6939SJiyong Park 
316*54fd6939SJiyong Park 	return amu_group0_cnt_read_internal(idx);
317*54fd6939SJiyong Park }
318*54fd6939SJiyong Park 
319*54fd6939SJiyong Park /* Write the group 0 counter identified by the given `idx` with `val` */
amu_group0_cnt_write(unsigned int idx,uint64_t val)320*54fd6939SJiyong Park static void amu_group0_cnt_write(unsigned  int idx, uint64_t val)
321*54fd6939SJiyong Park {
322*54fd6939SJiyong Park 	assert(amu_supported());
323*54fd6939SJiyong Park 	assert(idx < read_amcgcr_el0_cg0nc());
324*54fd6939SJiyong Park 
325*54fd6939SJiyong Park 	amu_group0_cnt_write_internal(idx, val);
326*54fd6939SJiyong Park 	isb();
327*54fd6939SJiyong Park }
328*54fd6939SJiyong Park 
329*54fd6939SJiyong Park /*
330*54fd6939SJiyong Park  * Unlike with auxiliary counters, we cannot detect at runtime whether an
331*54fd6939SJiyong Park  * architected counter supports a virtual offset. These are instead fixed
332*54fd6939SJiyong Park  * according to FEAT_AMUv1p1, but this switch will need to be updated if later
333*54fd6939SJiyong Park  * revisions of FEAT_AMU add additional architected counters.
334*54fd6939SJiyong Park  */
amu_group0_voffset_supported(uint64_t idx)335*54fd6939SJiyong Park static bool amu_group0_voffset_supported(uint64_t idx)
336*54fd6939SJiyong Park {
337*54fd6939SJiyong Park 	switch (idx) {
338*54fd6939SJiyong Park 	case 0U:
339*54fd6939SJiyong Park 	case 2U:
340*54fd6939SJiyong Park 	case 3U:
341*54fd6939SJiyong Park 		return true;
342*54fd6939SJiyong Park 
343*54fd6939SJiyong Park 	case 1U:
344*54fd6939SJiyong Park 		return false;
345*54fd6939SJiyong Park 
346*54fd6939SJiyong Park 	default:
347*54fd6939SJiyong Park 		ERROR("AMU: can't set up virtual offset for unknown "
348*54fd6939SJiyong Park 		      "architected counter %" PRIu64 "!\n", idx);
349*54fd6939SJiyong Park 
350*54fd6939SJiyong Park 		panic();
351*54fd6939SJiyong Park 	}
352*54fd6939SJiyong Park }
353*54fd6939SJiyong Park 
354*54fd6939SJiyong Park /*
355*54fd6939SJiyong Park  * Read the group 0 offset register for a given index. Index must be 0, 2,
356*54fd6939SJiyong Park  * or 3, the register for 1 does not exist.
357*54fd6939SJiyong Park  *
358*54fd6939SJiyong Park  * Using this function requires FEAT_AMUv1p1 support.
359*54fd6939SJiyong Park  */
amu_group0_voffset_read(unsigned int idx)360*54fd6939SJiyong Park static uint64_t amu_group0_voffset_read(unsigned int idx)
361*54fd6939SJiyong Park {
362*54fd6939SJiyong Park 	assert(amu_v1p1_supported());
363*54fd6939SJiyong Park 	assert(idx < read_amcgcr_el0_cg0nc());
364*54fd6939SJiyong Park 	assert(idx != 1U);
365*54fd6939SJiyong Park 
366*54fd6939SJiyong Park 	return amu_group0_voffset_read_internal(idx);
367*54fd6939SJiyong Park }
368*54fd6939SJiyong Park 
369*54fd6939SJiyong Park /*
370*54fd6939SJiyong Park  * Write the group 0 offset register for a given index. Index must be 0, 2, or
371*54fd6939SJiyong Park  * 3, the register for 1 does not exist.
372*54fd6939SJiyong Park  *
373*54fd6939SJiyong Park  * Using this function requires FEAT_AMUv1p1 support.
374*54fd6939SJiyong Park  */
amu_group0_voffset_write(unsigned int idx,uint64_t val)375*54fd6939SJiyong Park static void amu_group0_voffset_write(unsigned int idx, uint64_t val)
376*54fd6939SJiyong Park {
377*54fd6939SJiyong Park 	assert(amu_v1p1_supported());
378*54fd6939SJiyong Park 	assert(idx < read_amcgcr_el0_cg0nc());
379*54fd6939SJiyong Park 	assert(idx != 1U);
380*54fd6939SJiyong Park 
381*54fd6939SJiyong Park 	amu_group0_voffset_write_internal(idx, val);
382*54fd6939SJiyong Park 	isb();
383*54fd6939SJiyong Park }
384*54fd6939SJiyong Park 
385*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
386*54fd6939SJiyong Park /* Read the group 1 counter identified by the given `idx` */
amu_group1_cnt_read(unsigned int idx)387*54fd6939SJiyong Park static uint64_t amu_group1_cnt_read(unsigned int idx)
388*54fd6939SJiyong Park {
389*54fd6939SJiyong Park 	assert(amu_supported());
390*54fd6939SJiyong Park 	assert(amu_group1_supported());
391*54fd6939SJiyong Park 	assert(idx < read_amcgcr_el0_cg1nc());
392*54fd6939SJiyong Park 
393*54fd6939SJiyong Park 	return amu_group1_cnt_read_internal(idx);
394*54fd6939SJiyong Park }
395*54fd6939SJiyong Park 
396*54fd6939SJiyong Park /* Write the group 1 counter identified by the given `idx` with `val` */
amu_group1_cnt_write(unsigned int idx,uint64_t val)397*54fd6939SJiyong Park static void amu_group1_cnt_write(unsigned int idx, uint64_t val)
398*54fd6939SJiyong Park {
399*54fd6939SJiyong Park 	assert(amu_supported());
400*54fd6939SJiyong Park 	assert(amu_group1_supported());
401*54fd6939SJiyong Park 	assert(idx < read_amcgcr_el0_cg1nc());
402*54fd6939SJiyong Park 
403*54fd6939SJiyong Park 	amu_group1_cnt_write_internal(idx, val);
404*54fd6939SJiyong Park 	isb();
405*54fd6939SJiyong Park }
406*54fd6939SJiyong Park 
407*54fd6939SJiyong Park /*
408*54fd6939SJiyong Park  * Read the group 1 offset register for a given index.
409*54fd6939SJiyong Park  *
410*54fd6939SJiyong Park  * Using this function requires FEAT_AMUv1p1 support.
411*54fd6939SJiyong Park  */
amu_group1_voffset_read(unsigned int idx)412*54fd6939SJiyong Park static uint64_t amu_group1_voffset_read(unsigned int idx)
413*54fd6939SJiyong Park {
414*54fd6939SJiyong Park 	assert(amu_v1p1_supported());
415*54fd6939SJiyong Park 	assert(amu_group1_supported());
416*54fd6939SJiyong Park 	assert(idx < read_amcgcr_el0_cg1nc());
417*54fd6939SJiyong Park 	assert((read_amcg1idr_el0_voff() & (UINT64_C(1) << idx)) != 0U);
418*54fd6939SJiyong Park 
419*54fd6939SJiyong Park 	return amu_group1_voffset_read_internal(idx);
420*54fd6939SJiyong Park }
421*54fd6939SJiyong Park 
422*54fd6939SJiyong Park /*
423*54fd6939SJiyong Park  * Write the group 1 offset register for a given index.
424*54fd6939SJiyong Park  *
425*54fd6939SJiyong Park  * Using this function requires FEAT_AMUv1p1 support.
426*54fd6939SJiyong Park  */
amu_group1_voffset_write(unsigned int idx,uint64_t val)427*54fd6939SJiyong Park static void amu_group1_voffset_write(unsigned int idx, uint64_t val)
428*54fd6939SJiyong Park {
429*54fd6939SJiyong Park 	assert(amu_v1p1_supported());
430*54fd6939SJiyong Park 	assert(amu_group1_supported());
431*54fd6939SJiyong Park 	assert(idx < read_amcgcr_el0_cg1nc());
432*54fd6939SJiyong Park 	assert((read_amcg1idr_el0_voff() & (UINT64_C(1) << idx)) != 0U);
433*54fd6939SJiyong Park 
434*54fd6939SJiyong Park 	amu_group1_voffset_write_internal(idx, val);
435*54fd6939SJiyong Park 	isb();
436*54fd6939SJiyong Park }
437*54fd6939SJiyong Park #endif
438*54fd6939SJiyong Park 
amu_context_save(const void * arg)439*54fd6939SJiyong Park static void *amu_context_save(const void *arg)
440*54fd6939SJiyong Park {
441*54fd6939SJiyong Park 	uint64_t i, j;
442*54fd6939SJiyong Park 
443*54fd6939SJiyong Park 	unsigned int core_pos;
444*54fd6939SJiyong Park 	struct amu_ctx *ctx;
445*54fd6939SJiyong Park 
446*54fd6939SJiyong Park 	uint64_t id_aa64pfr0_el1_amu;	/* AMU version */
447*54fd6939SJiyong Park 	uint64_t hcr_el2_amvoffen;	/* AMU virtual offsets enabled */
448*54fd6939SJiyong Park 	uint64_t amcgcr_el0_cg0nc;	/* Number of group 0 counters */
449*54fd6939SJiyong Park 
450*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
451*54fd6939SJiyong Park 	uint64_t amcg1idr_el0_voff;	/* Auxiliary counters with virtual offsets */
452*54fd6939SJiyong Park 	uint64_t amcfgr_el0_ncg;	/* Number of counter groups */
453*54fd6939SJiyong Park 	uint64_t amcgcr_el0_cg1nc;	/* Number of group 1 counters */
454*54fd6939SJiyong Park #endif
455*54fd6939SJiyong Park 
456*54fd6939SJiyong Park 	id_aa64pfr0_el1_amu = read_id_aa64pfr0_el1_amu();
457*54fd6939SJiyong Park 	if (id_aa64pfr0_el1_amu == ID_AA64PFR0_AMU_NOT_SUPPORTED) {
458*54fd6939SJiyong Park 		return (void *)0;
459*54fd6939SJiyong Park 	}
460*54fd6939SJiyong Park 
461*54fd6939SJiyong Park 	core_pos = plat_my_core_pos();
462*54fd6939SJiyong Park 	ctx = &amu_ctxs_[core_pos];
463*54fd6939SJiyong Park 
464*54fd6939SJiyong Park 	amcgcr_el0_cg0nc = read_amcgcr_el0_cg0nc();
465*54fd6939SJiyong Park 	hcr_el2_amvoffen = (id_aa64pfr0_el1_amu >= ID_AA64PFR0_AMU_V1P1) ?
466*54fd6939SJiyong Park 		read_hcr_el2_amvoffen() : 0U;
467*54fd6939SJiyong Park 
468*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
469*54fd6939SJiyong Park 	amcfgr_el0_ncg = read_amcfgr_el0_ncg();
470*54fd6939SJiyong Park 	amcgcr_el0_cg1nc = (amcfgr_el0_ncg > 0U) ? read_amcgcr_el0_cg1nc() : 0U;
471*54fd6939SJiyong Park 	amcg1idr_el0_voff = (hcr_el2_amvoffen != 0U) ? read_amcg1idr_el0_voff() : 0U;
472*54fd6939SJiyong Park #endif
473*54fd6939SJiyong Park 
474*54fd6939SJiyong Park 	/*
475*54fd6939SJiyong Park 	 * Disable all AMU counters.
476*54fd6939SJiyong Park 	 */
477*54fd6939SJiyong Park 
478*54fd6939SJiyong Park 	ctx->group0_enable = read_amcntenset0_el0_px();
479*54fd6939SJiyong Park 	write_amcntenclr0_el0_px(ctx->group0_enable);
480*54fd6939SJiyong Park 
481*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
482*54fd6939SJiyong Park 	if (amcfgr_el0_ncg > 0U) {
483*54fd6939SJiyong Park 		ctx->group1_enable = read_amcntenset1_el0_px();
484*54fd6939SJiyong Park 		write_amcntenclr1_el0_px(ctx->group1_enable);
485*54fd6939SJiyong Park 	}
486*54fd6939SJiyong Park #endif
487*54fd6939SJiyong Park 
488*54fd6939SJiyong Park 	/*
489*54fd6939SJiyong Park 	 * Save the counters to the local context.
490*54fd6939SJiyong Park 	 */
491*54fd6939SJiyong Park 
492*54fd6939SJiyong Park 	isb(); /* Ensure counters have been stopped */
493*54fd6939SJiyong Park 
494*54fd6939SJiyong Park 	for (i = 0U; i < amcgcr_el0_cg0nc; i++) {
495*54fd6939SJiyong Park 		ctx->group0_cnts[i] = amu_group0_cnt_read(i);
496*54fd6939SJiyong Park 	}
497*54fd6939SJiyong Park 
498*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
499*54fd6939SJiyong Park 	for (i = 0U; i < amcgcr_el0_cg1nc; i++) {
500*54fd6939SJiyong Park 		ctx->group1_cnts[i] = amu_group1_cnt_read(i);
501*54fd6939SJiyong Park 	}
502*54fd6939SJiyong Park #endif
503*54fd6939SJiyong Park 
504*54fd6939SJiyong Park 	/*
505*54fd6939SJiyong Park 	 * Save virtual offsets for counters that offer them.
506*54fd6939SJiyong Park 	 */
507*54fd6939SJiyong Park 
508*54fd6939SJiyong Park 	if (hcr_el2_amvoffen != 0U) {
509*54fd6939SJiyong Park 		for (i = 0U, j = 0U; i < amcgcr_el0_cg0nc; i++) {
510*54fd6939SJiyong Park 			if (!amu_group0_voffset_supported(i)) {
511*54fd6939SJiyong Park 				continue; /* No virtual offset */
512*54fd6939SJiyong Park 			}
513*54fd6939SJiyong Park 
514*54fd6939SJiyong Park 			ctx->group0_voffsets[j++] = amu_group0_voffset_read(i);
515*54fd6939SJiyong Park 		}
516*54fd6939SJiyong Park 
517*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
518*54fd6939SJiyong Park 		for (i = 0U, j = 0U; i < amcgcr_el0_cg1nc; i++) {
519*54fd6939SJiyong Park 			if ((amcg1idr_el0_voff >> i) & 1U) {
520*54fd6939SJiyong Park 				continue; /* No virtual offset */
521*54fd6939SJiyong Park 			}
522*54fd6939SJiyong Park 
523*54fd6939SJiyong Park 			ctx->group1_voffsets[j++] = amu_group1_voffset_read(i);
524*54fd6939SJiyong Park 		}
525*54fd6939SJiyong Park #endif
526*54fd6939SJiyong Park 	}
527*54fd6939SJiyong Park 
528*54fd6939SJiyong Park 	return (void *)0;
529*54fd6939SJiyong Park }
530*54fd6939SJiyong Park 
amu_context_restore(const void * arg)531*54fd6939SJiyong Park static void *amu_context_restore(const void *arg)
532*54fd6939SJiyong Park {
533*54fd6939SJiyong Park 	uint64_t i, j;
534*54fd6939SJiyong Park 
535*54fd6939SJiyong Park 	unsigned int core_pos;
536*54fd6939SJiyong Park 	struct amu_ctx *ctx;
537*54fd6939SJiyong Park 
538*54fd6939SJiyong Park 	uint64_t id_aa64pfr0_el1_amu;	/* AMU version */
539*54fd6939SJiyong Park 
540*54fd6939SJiyong Park 	uint64_t hcr_el2_amvoffen;	/* AMU virtual offsets enabled */
541*54fd6939SJiyong Park 
542*54fd6939SJiyong Park 	uint64_t amcfgr_el0_ncg;	/* Number of counter groups */
543*54fd6939SJiyong Park 	uint64_t amcgcr_el0_cg0nc;	/* Number of group 0 counters */
544*54fd6939SJiyong Park 
545*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
546*54fd6939SJiyong Park 	uint64_t amcgcr_el0_cg1nc;	/* Number of group 1 counters */
547*54fd6939SJiyong Park 	uint64_t amcg1idr_el0_voff;	/* Auxiliary counters with virtual offsets */
548*54fd6939SJiyong Park #endif
549*54fd6939SJiyong Park 
550*54fd6939SJiyong Park 	id_aa64pfr0_el1_amu = read_id_aa64pfr0_el1_amu();
551*54fd6939SJiyong Park 	if (id_aa64pfr0_el1_amu == ID_AA64PFR0_AMU_NOT_SUPPORTED) {
552*54fd6939SJiyong Park 		return (void *)0;
553*54fd6939SJiyong Park 	}
554*54fd6939SJiyong Park 
555*54fd6939SJiyong Park 	core_pos = plat_my_core_pos();
556*54fd6939SJiyong Park 	ctx = &amu_ctxs_[core_pos];
557*54fd6939SJiyong Park 
558*54fd6939SJiyong Park 	amcfgr_el0_ncg = read_amcfgr_el0_ncg();
559*54fd6939SJiyong Park 	amcgcr_el0_cg0nc = read_amcgcr_el0_cg0nc();
560*54fd6939SJiyong Park 
561*54fd6939SJiyong Park 	hcr_el2_amvoffen = (id_aa64pfr0_el1_amu >= ID_AA64PFR0_AMU_V1P1) ?
562*54fd6939SJiyong Park 		read_hcr_el2_amvoffen() : 0U;
563*54fd6939SJiyong Park 
564*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
565*54fd6939SJiyong Park 	amcgcr_el0_cg1nc = (amcfgr_el0_ncg > 0U) ? read_amcgcr_el0_cg1nc() : 0U;
566*54fd6939SJiyong Park 	amcg1idr_el0_voff = (hcr_el2_amvoffen != 0U) ? read_amcg1idr_el0_voff() : 0U;
567*54fd6939SJiyong Park #endif
568*54fd6939SJiyong Park 
569*54fd6939SJiyong Park 	/*
570*54fd6939SJiyong Park 	 * Sanity check that all counters were disabled when the context was
571*54fd6939SJiyong Park 	 * previously saved.
572*54fd6939SJiyong Park 	 */
573*54fd6939SJiyong Park 
574*54fd6939SJiyong Park 	assert(read_amcntenset0_el0_px() == 0U);
575*54fd6939SJiyong Park 
576*54fd6939SJiyong Park 	if (amcfgr_el0_ncg > 0U) {
577*54fd6939SJiyong Park 		assert(read_amcntenset1_el0_px() == 0U);
578*54fd6939SJiyong Park 	}
579*54fd6939SJiyong Park 
580*54fd6939SJiyong Park 	/*
581*54fd6939SJiyong Park 	 * Restore the counter values from the local context.
582*54fd6939SJiyong Park 	 */
583*54fd6939SJiyong Park 
584*54fd6939SJiyong Park 	for (i = 0U; i < amcgcr_el0_cg0nc; i++) {
585*54fd6939SJiyong Park 		amu_group0_cnt_write(i, ctx->group0_cnts[i]);
586*54fd6939SJiyong Park 	}
587*54fd6939SJiyong Park 
588*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
589*54fd6939SJiyong Park 	for (i = 0U; i < amcgcr_el0_cg1nc; i++) {
590*54fd6939SJiyong Park 		amu_group1_cnt_write(i, ctx->group1_cnts[i]);
591*54fd6939SJiyong Park 	}
592*54fd6939SJiyong Park #endif
593*54fd6939SJiyong Park 
594*54fd6939SJiyong Park 	/*
595*54fd6939SJiyong Park 	 * Restore virtual offsets for counters that offer them.
596*54fd6939SJiyong Park 	 */
597*54fd6939SJiyong Park 
598*54fd6939SJiyong Park 	if (hcr_el2_amvoffen != 0U) {
599*54fd6939SJiyong Park 		for (i = 0U, j = 0U; i < amcgcr_el0_cg0nc; i++) {
600*54fd6939SJiyong Park 			if (!amu_group0_voffset_supported(i)) {
601*54fd6939SJiyong Park 				continue; /* No virtual offset */
602*54fd6939SJiyong Park 			}
603*54fd6939SJiyong Park 
604*54fd6939SJiyong Park 			amu_group0_voffset_write(i, ctx->group0_voffsets[j++]);
605*54fd6939SJiyong Park 		}
606*54fd6939SJiyong Park 
607*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
608*54fd6939SJiyong Park 		for (i = 0U, j = 0U; i < amcgcr_el0_cg1nc; i++) {
609*54fd6939SJiyong Park 			if ((amcg1idr_el0_voff >> i) & 1U) {
610*54fd6939SJiyong Park 				continue; /* No virtual offset */
611*54fd6939SJiyong Park 			}
612*54fd6939SJiyong Park 
613*54fd6939SJiyong Park 			amu_group1_voffset_write(i, ctx->group1_voffsets[j++]);
614*54fd6939SJiyong Park 		}
615*54fd6939SJiyong Park #endif
616*54fd6939SJiyong Park 	}
617*54fd6939SJiyong Park 
618*54fd6939SJiyong Park 	/*
619*54fd6939SJiyong Park 	 * Re-enable counters that were disabled during context save.
620*54fd6939SJiyong Park 	 */
621*54fd6939SJiyong Park 
622*54fd6939SJiyong Park 	write_amcntenset0_el0_px(ctx->group0_enable);
623*54fd6939SJiyong Park 
624*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
625*54fd6939SJiyong Park 	if (amcfgr_el0_ncg > 0) {
626*54fd6939SJiyong Park 		write_amcntenset1_el0_px(ctx->group1_enable);
627*54fd6939SJiyong Park 	}
628*54fd6939SJiyong Park #endif
629*54fd6939SJiyong Park 
630*54fd6939SJiyong Park #if ENABLE_MPMM
631*54fd6939SJiyong Park 	mpmm_enable();
632*54fd6939SJiyong Park #endif
633*54fd6939SJiyong Park 
634*54fd6939SJiyong Park 	return (void *)0;
635*54fd6939SJiyong Park }
636*54fd6939SJiyong Park 
637*54fd6939SJiyong Park SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_start, amu_context_save);
638*54fd6939SJiyong Park SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_finish, amu_context_restore);
639