xref: /aosp_15_r20/external/arm-trusted-firmware/lib/extensions/amu/aarch32/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 <stdbool.h>
10*54fd6939SJiyong Park 
11*54fd6939SJiyong Park #include "../amu_private.h"
12*54fd6939SJiyong Park #include <arch.h>
13*54fd6939SJiyong Park #include <arch_helpers.h>
14*54fd6939SJiyong Park #include <common/debug.h>
15*54fd6939SJiyong Park #include <lib/el3_runtime/pubsub_events.h>
16*54fd6939SJiyong Park #include <lib/extensions/amu.h>
17*54fd6939SJiyong Park 
18*54fd6939SJiyong Park #include <plat/common/platform.h>
19*54fd6939SJiyong Park 
20*54fd6939SJiyong Park struct amu_ctx {
21*54fd6939SJiyong Park 	uint64_t group0_cnts[AMU_GROUP0_MAX_COUNTERS];
22*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
23*54fd6939SJiyong Park 	uint64_t group1_cnts[AMU_GROUP1_MAX_COUNTERS];
24*54fd6939SJiyong Park #endif
25*54fd6939SJiyong Park 
26*54fd6939SJiyong Park 	uint16_t group0_enable;
27*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
28*54fd6939SJiyong Park 	uint16_t group1_enable;
29*54fd6939SJiyong Park #endif
30*54fd6939SJiyong Park };
31*54fd6939SJiyong Park 
32*54fd6939SJiyong Park static struct amu_ctx amu_ctxs_[PLATFORM_CORE_COUNT];
33*54fd6939SJiyong Park 
34*54fd6939SJiyong Park CASSERT((sizeof(amu_ctxs_[0].group0_enable) * CHAR_BIT) <= AMU_GROUP0_MAX_COUNTERS,
35*54fd6939SJiyong Park 	amu_ctx_group0_enable_cannot_represent_all_group0_counters);
36*54fd6939SJiyong Park 
37*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
38*54fd6939SJiyong Park CASSERT((sizeof(amu_ctxs_[0].group1_enable) * CHAR_BIT) <= AMU_GROUP1_MAX_COUNTERS,
39*54fd6939SJiyong Park 	amu_ctx_group1_enable_cannot_represent_all_group1_counters);
40*54fd6939SJiyong Park #endif
41*54fd6939SJiyong Park 
read_id_pfr0_amu(void)42*54fd6939SJiyong Park static inline __unused uint32_t read_id_pfr0_amu(void)
43*54fd6939SJiyong Park {
44*54fd6939SJiyong Park 	return (read_id_pfr0() >> ID_PFR0_AMU_SHIFT) &
45*54fd6939SJiyong Park 		ID_PFR0_AMU_MASK;
46*54fd6939SJiyong Park }
47*54fd6939SJiyong Park 
write_hcptr_tam(uint32_t value)48*54fd6939SJiyong Park static inline __unused void write_hcptr_tam(uint32_t value)
49*54fd6939SJiyong Park {
50*54fd6939SJiyong Park 	write_hcptr((read_hcptr() & ~TAM_BIT) |
51*54fd6939SJiyong Park 		((value << TAM_SHIFT) & TAM_BIT));
52*54fd6939SJiyong Park }
53*54fd6939SJiyong Park 
write_amcr_cg1rz(uint32_t value)54*54fd6939SJiyong Park static inline __unused void write_amcr_cg1rz(uint32_t value)
55*54fd6939SJiyong Park {
56*54fd6939SJiyong Park 	write_amcr((read_amcr() & ~AMCR_CG1RZ_BIT) |
57*54fd6939SJiyong Park 		((value << AMCR_CG1RZ_SHIFT) & AMCR_CG1RZ_BIT));
58*54fd6939SJiyong Park }
59*54fd6939SJiyong Park 
read_amcfgr_ncg(void)60*54fd6939SJiyong Park static inline __unused uint32_t read_amcfgr_ncg(void)
61*54fd6939SJiyong Park {
62*54fd6939SJiyong Park 	return (read_amcfgr() >> AMCFGR_NCG_SHIFT) &
63*54fd6939SJiyong Park 		AMCFGR_NCG_MASK;
64*54fd6939SJiyong Park }
65*54fd6939SJiyong Park 
read_amcgcr_cg0nc(void)66*54fd6939SJiyong Park static inline __unused uint32_t read_amcgcr_cg0nc(void)
67*54fd6939SJiyong Park {
68*54fd6939SJiyong Park 	return (read_amcgcr() >> AMCGCR_CG0NC_SHIFT) &
69*54fd6939SJiyong Park 		AMCGCR_CG0NC_MASK;
70*54fd6939SJiyong Park }
71*54fd6939SJiyong Park 
read_amcgcr_cg1nc(void)72*54fd6939SJiyong Park static inline __unused uint32_t read_amcgcr_cg1nc(void)
73*54fd6939SJiyong Park {
74*54fd6939SJiyong Park 	return (read_amcgcr() >> AMCGCR_CG1NC_SHIFT) &
75*54fd6939SJiyong Park 		AMCGCR_CG1NC_MASK;
76*54fd6939SJiyong Park }
77*54fd6939SJiyong Park 
read_amcntenset0_px(void)78*54fd6939SJiyong Park static inline __unused uint32_t read_amcntenset0_px(void)
79*54fd6939SJiyong Park {
80*54fd6939SJiyong Park 	return (read_amcntenset0() >> AMCNTENSET0_Pn_SHIFT) &
81*54fd6939SJiyong Park 		AMCNTENSET0_Pn_MASK;
82*54fd6939SJiyong Park }
83*54fd6939SJiyong Park 
read_amcntenset1_px(void)84*54fd6939SJiyong Park static inline __unused uint32_t read_amcntenset1_px(void)
85*54fd6939SJiyong Park {
86*54fd6939SJiyong Park 	return (read_amcntenset1() >> AMCNTENSET1_Pn_SHIFT) &
87*54fd6939SJiyong Park 		AMCNTENSET1_Pn_MASK;
88*54fd6939SJiyong Park }
89*54fd6939SJiyong Park 
write_amcntenset0_px(uint32_t px)90*54fd6939SJiyong Park static inline __unused void write_amcntenset0_px(uint32_t px)
91*54fd6939SJiyong Park {
92*54fd6939SJiyong Park 	uint32_t value = read_amcntenset0();
93*54fd6939SJiyong Park 
94*54fd6939SJiyong Park 	value &= ~AMCNTENSET0_Pn_MASK;
95*54fd6939SJiyong Park 	value |= (px << AMCNTENSET0_Pn_SHIFT) &
96*54fd6939SJiyong Park 		AMCNTENSET0_Pn_MASK;
97*54fd6939SJiyong Park 
98*54fd6939SJiyong Park 	write_amcntenset0(value);
99*54fd6939SJiyong Park }
100*54fd6939SJiyong Park 
write_amcntenset1_px(uint32_t px)101*54fd6939SJiyong Park static inline __unused void write_amcntenset1_px(uint32_t px)
102*54fd6939SJiyong Park {
103*54fd6939SJiyong Park 	uint32_t value = read_amcntenset1();
104*54fd6939SJiyong Park 
105*54fd6939SJiyong Park 	value &= ~AMCNTENSET1_Pn_MASK;
106*54fd6939SJiyong Park 	value |= (px << AMCNTENSET1_Pn_SHIFT) &
107*54fd6939SJiyong Park 		AMCNTENSET1_Pn_MASK;
108*54fd6939SJiyong Park 
109*54fd6939SJiyong Park 	write_amcntenset1(value);
110*54fd6939SJiyong Park }
111*54fd6939SJiyong Park 
write_amcntenclr0_px(uint32_t px)112*54fd6939SJiyong Park static inline __unused void write_amcntenclr0_px(uint32_t px)
113*54fd6939SJiyong Park {
114*54fd6939SJiyong Park 	uint32_t value = read_amcntenclr0();
115*54fd6939SJiyong Park 
116*54fd6939SJiyong Park 	value &= ~AMCNTENCLR0_Pn_MASK;
117*54fd6939SJiyong Park 	value |= (px << AMCNTENCLR0_Pn_SHIFT) & AMCNTENCLR0_Pn_MASK;
118*54fd6939SJiyong Park 
119*54fd6939SJiyong Park 	write_amcntenclr0(value);
120*54fd6939SJiyong Park }
121*54fd6939SJiyong Park 
write_amcntenclr1_px(uint32_t px)122*54fd6939SJiyong Park static inline __unused void write_amcntenclr1_px(uint32_t px)
123*54fd6939SJiyong Park {
124*54fd6939SJiyong Park 	uint32_t value = read_amcntenclr1();
125*54fd6939SJiyong Park 
126*54fd6939SJiyong Park 	value &= ~AMCNTENCLR1_Pn_MASK;
127*54fd6939SJiyong Park 	value |= (px << AMCNTENCLR1_Pn_SHIFT) & AMCNTENCLR1_Pn_MASK;
128*54fd6939SJiyong Park 
129*54fd6939SJiyong Park 	write_amcntenclr1(value);
130*54fd6939SJiyong Park }
131*54fd6939SJiyong Park 
amu_supported(void)132*54fd6939SJiyong Park static __unused bool amu_supported(void)
133*54fd6939SJiyong Park {
134*54fd6939SJiyong Park 	return read_id_pfr0_amu() >= ID_PFR0_AMU_V1;
135*54fd6939SJiyong Park }
136*54fd6939SJiyong Park 
137*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
amu_group1_supported(void)138*54fd6939SJiyong Park static __unused bool amu_group1_supported(void)
139*54fd6939SJiyong Park {
140*54fd6939SJiyong Park 	return read_amcfgr_ncg() > 0U;
141*54fd6939SJiyong Park }
142*54fd6939SJiyong Park #endif
143*54fd6939SJiyong Park 
144*54fd6939SJiyong Park /*
145*54fd6939SJiyong Park  * Enable counters. This function is meant to be invoked by the context
146*54fd6939SJiyong Park  * management library before exiting from EL3.
147*54fd6939SJiyong Park  */
amu_enable(bool el2_unused)148*54fd6939SJiyong Park void amu_enable(bool el2_unused)
149*54fd6939SJiyong Park {
150*54fd6939SJiyong Park 	uint32_t id_pfr0_amu;		/* AMU version */
151*54fd6939SJiyong Park 
152*54fd6939SJiyong Park 	uint32_t amcfgr_ncg;		/* Number of counter groups */
153*54fd6939SJiyong Park 	uint32_t amcgcr_cg0nc;		/* Number of group 0 counters */
154*54fd6939SJiyong Park 
155*54fd6939SJiyong Park 	uint32_t amcntenset0_px = 0x0;	/* Group 0 enable mask */
156*54fd6939SJiyong Park 	uint32_t amcntenset1_px = 0x0;	/* Group 1 enable mask */
157*54fd6939SJiyong Park 
158*54fd6939SJiyong Park 	id_pfr0_amu = read_id_pfr0_amu();
159*54fd6939SJiyong Park 	if (id_pfr0_amu == ID_PFR0_AMU_NOT_SUPPORTED) {
160*54fd6939SJiyong Park 		/*
161*54fd6939SJiyong Park 		 * If the AMU is unsupported, nothing needs to be done.
162*54fd6939SJiyong Park 		 */
163*54fd6939SJiyong Park 
164*54fd6939SJiyong Park 		return;
165*54fd6939SJiyong Park 	}
166*54fd6939SJiyong Park 
167*54fd6939SJiyong Park 	if (el2_unused) {
168*54fd6939SJiyong Park 		/*
169*54fd6939SJiyong Park 		 * HCPTR.TAM: Set to zero so any accesses to the Activity
170*54fd6939SJiyong Park 		 * Monitor registers do not trap to EL2.
171*54fd6939SJiyong Park 		 */
172*54fd6939SJiyong Park 		write_hcptr_tam(0U);
173*54fd6939SJiyong Park 	}
174*54fd6939SJiyong Park 
175*54fd6939SJiyong Park 	/*
176*54fd6939SJiyong Park 	 * Retrieve the number of architected counters. All of these counters
177*54fd6939SJiyong Park 	 * are enabled by default.
178*54fd6939SJiyong Park 	 */
179*54fd6939SJiyong Park 
180*54fd6939SJiyong Park 	amcgcr_cg0nc = read_amcgcr_cg0nc();
181*54fd6939SJiyong Park 	amcntenset0_px = (UINT32_C(1) << (amcgcr_cg0nc)) - 1U;
182*54fd6939SJiyong Park 
183*54fd6939SJiyong Park 	assert(amcgcr_cg0nc <= AMU_AMCGCR_CG0NC_MAX);
184*54fd6939SJiyong Park 
185*54fd6939SJiyong Park 	/*
186*54fd6939SJiyong Park 	 * The platform may opt to enable specific auxiliary counters. This can
187*54fd6939SJiyong Park 	 * be done via the common FCONF getter, or via the platform-implemented
188*54fd6939SJiyong Park 	 * function.
189*54fd6939SJiyong Park 	 */
190*54fd6939SJiyong Park 
191*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
192*54fd6939SJiyong Park 	const struct amu_topology *topology;
193*54fd6939SJiyong Park 
194*54fd6939SJiyong Park #if ENABLE_AMU_FCONF
195*54fd6939SJiyong Park 	topology = FCONF_GET_PROPERTY(amu, config, topology);
196*54fd6939SJiyong Park #else
197*54fd6939SJiyong Park 	topology = plat_amu_topology();
198*54fd6939SJiyong Park #endif /* ENABLE_AMU_FCONF */
199*54fd6939SJiyong Park 
200*54fd6939SJiyong Park 	if (topology != NULL) {
201*54fd6939SJiyong Park 		unsigned int core_pos = plat_my_core_pos();
202*54fd6939SJiyong Park 
203*54fd6939SJiyong Park 		amcntenset1_el0_px = topology->cores[core_pos].enable;
204*54fd6939SJiyong Park 	} else {
205*54fd6939SJiyong Park 		ERROR("AMU: failed to generate AMU topology\n");
206*54fd6939SJiyong Park 	}
207*54fd6939SJiyong Park #endif /* ENABLE_AMU_AUXILIARY_COUNTERS */
208*54fd6939SJiyong Park 
209*54fd6939SJiyong Park 	/*
210*54fd6939SJiyong Park 	 * Enable the requested counters.
211*54fd6939SJiyong Park 	 */
212*54fd6939SJiyong Park 
213*54fd6939SJiyong Park 	write_amcntenset0_px(amcntenset0_px);
214*54fd6939SJiyong Park 
215*54fd6939SJiyong Park 	amcfgr_ncg = read_amcfgr_ncg();
216*54fd6939SJiyong Park 	if (amcfgr_ncg > 0U) {
217*54fd6939SJiyong Park 		write_amcntenset1_px(amcntenset1_px);
218*54fd6939SJiyong Park 
219*54fd6939SJiyong Park #if !ENABLE_AMU_AUXILIARY_COUNTERS
220*54fd6939SJiyong Park 		VERBOSE("AMU: auxiliary counters detected but support is disabled\n");
221*54fd6939SJiyong Park #endif
222*54fd6939SJiyong Park 	}
223*54fd6939SJiyong Park 
224*54fd6939SJiyong Park 	/* Initialize FEAT_AMUv1p1 features if present. */
225*54fd6939SJiyong Park 	if (id_pfr0_amu < ID_PFR0_AMU_V1P1) {
226*54fd6939SJiyong Park 		return;
227*54fd6939SJiyong Park 	}
228*54fd6939SJiyong Park 
229*54fd6939SJiyong Park #if AMU_RESTRICT_COUNTERS
230*54fd6939SJiyong Park 	/*
231*54fd6939SJiyong Park 	 * FEAT_AMUv1p1 adds a register field to restrict access to group 1
232*54fd6939SJiyong Park 	 * counters at all but the highest implemented EL.  This is controlled
233*54fd6939SJiyong Park 	 * with the AMU_RESTRICT_COUNTERS compile time flag, when set, system
234*54fd6939SJiyong Park 	 * register reads at lower ELs return zero.  Reads from the memory
235*54fd6939SJiyong Park 	 * mapped view are unaffected.
236*54fd6939SJiyong Park 	 */
237*54fd6939SJiyong Park 	VERBOSE("AMU group 1 counter access restricted.\n");
238*54fd6939SJiyong Park 	write_amcr_cg1rz(1U);
239*54fd6939SJiyong Park #else
240*54fd6939SJiyong Park 	write_amcr_cg1rz(0U);
241*54fd6939SJiyong Park #endif
242*54fd6939SJiyong Park }
243*54fd6939SJiyong Park 
244*54fd6939SJiyong Park /* Read the group 0 counter identified by the given `idx`. */
amu_group0_cnt_read(unsigned int idx)245*54fd6939SJiyong Park static uint64_t amu_group0_cnt_read(unsigned int idx)
246*54fd6939SJiyong Park {
247*54fd6939SJiyong Park 	assert(amu_supported());
248*54fd6939SJiyong Park 	assert(idx < read_amcgcr_cg0nc());
249*54fd6939SJiyong Park 
250*54fd6939SJiyong Park 	return amu_group0_cnt_read_internal(idx);
251*54fd6939SJiyong Park }
252*54fd6939SJiyong Park 
253*54fd6939SJiyong Park /* Write the group 0 counter identified by the given `idx` with `val` */
amu_group0_cnt_write(unsigned int idx,uint64_t val)254*54fd6939SJiyong Park static void amu_group0_cnt_write(unsigned  int idx, uint64_t val)
255*54fd6939SJiyong Park {
256*54fd6939SJiyong Park 	assert(amu_supported());
257*54fd6939SJiyong Park 	assert(idx < read_amcgcr_cg0nc());
258*54fd6939SJiyong Park 
259*54fd6939SJiyong Park 	amu_group0_cnt_write_internal(idx, val);
260*54fd6939SJiyong Park 	isb();
261*54fd6939SJiyong Park }
262*54fd6939SJiyong Park 
263*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
264*54fd6939SJiyong Park /* Read the group 1 counter identified by the given `idx` */
amu_group1_cnt_read(unsigned int idx)265*54fd6939SJiyong Park static uint64_t amu_group1_cnt_read(unsigned  int idx)
266*54fd6939SJiyong Park {
267*54fd6939SJiyong Park 	assert(amu_supported());
268*54fd6939SJiyong Park 	assert(amu_group1_supported());
269*54fd6939SJiyong Park 	assert(idx < read_amcgcr_cg1nc());
270*54fd6939SJiyong Park 
271*54fd6939SJiyong Park 	return amu_group1_cnt_read_internal(idx);
272*54fd6939SJiyong Park }
273*54fd6939SJiyong Park 
274*54fd6939SJiyong Park /* Write the group 1 counter identified by the given `idx` with `val` */
amu_group1_cnt_write(unsigned int idx,uint64_t val)275*54fd6939SJiyong Park static void amu_group1_cnt_write(unsigned  int idx, uint64_t val)
276*54fd6939SJiyong Park {
277*54fd6939SJiyong Park 	assert(amu_supported());
278*54fd6939SJiyong Park 	assert(amu_group1_supported());
279*54fd6939SJiyong Park 	assert(idx < read_amcgcr_cg1nc());
280*54fd6939SJiyong Park 
281*54fd6939SJiyong Park 	amu_group1_cnt_write_internal(idx, val);
282*54fd6939SJiyong Park 	isb();
283*54fd6939SJiyong Park }
284*54fd6939SJiyong Park #endif
285*54fd6939SJiyong Park 
amu_context_save(const void * arg)286*54fd6939SJiyong Park static void *amu_context_save(const void *arg)
287*54fd6939SJiyong Park {
288*54fd6939SJiyong Park 	uint32_t i;
289*54fd6939SJiyong Park 
290*54fd6939SJiyong Park 	unsigned int core_pos;
291*54fd6939SJiyong Park 	struct amu_ctx *ctx;
292*54fd6939SJiyong Park 
293*54fd6939SJiyong Park 	uint32_t id_pfr0_amu;	/* AMU version */
294*54fd6939SJiyong Park 	uint32_t amcgcr_cg0nc;	/* Number of group 0 counters */
295*54fd6939SJiyong Park 
296*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
297*54fd6939SJiyong Park 	uint32_t amcfgr_ncg;	/* Number of counter groups */
298*54fd6939SJiyong Park 	uint32_t amcgcr_cg1nc;	/* Number of group 1 counters */
299*54fd6939SJiyong Park #endif
300*54fd6939SJiyong Park 
301*54fd6939SJiyong Park 	id_pfr0_amu = read_id_pfr0_amu();
302*54fd6939SJiyong Park 	if (id_pfr0_amu == ID_PFR0_AMU_NOT_SUPPORTED) {
303*54fd6939SJiyong Park 		return (void *)0;
304*54fd6939SJiyong Park 	}
305*54fd6939SJiyong Park 
306*54fd6939SJiyong Park 	core_pos = plat_my_core_pos();
307*54fd6939SJiyong Park 	ctx = &amu_ctxs_[core_pos];
308*54fd6939SJiyong Park 
309*54fd6939SJiyong Park 	amcgcr_cg0nc = read_amcgcr_cg0nc();
310*54fd6939SJiyong Park 
311*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
312*54fd6939SJiyong Park 	amcfgr_ncg = read_amcfgr_ncg();
313*54fd6939SJiyong Park 	amcgcr_cg1nc = (amcfgr_ncg > 0U) ? read_amcgcr_cg1nc() : 0U;
314*54fd6939SJiyong Park #endif
315*54fd6939SJiyong Park 
316*54fd6939SJiyong Park 	/*
317*54fd6939SJiyong Park 	 * Disable all AMU counters.
318*54fd6939SJiyong Park 	 */
319*54fd6939SJiyong Park 
320*54fd6939SJiyong Park 	ctx->group0_enable = read_amcntenset0_px();
321*54fd6939SJiyong Park 	write_amcntenclr0_px(ctx->group0_enable);
322*54fd6939SJiyong Park 
323*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
324*54fd6939SJiyong Park 	if (amcfgr_ncg > 0U) {
325*54fd6939SJiyong Park 		ctx->group1_enable = read_amcntenset1_px();
326*54fd6939SJiyong Park 		write_amcntenclr1_px(ctx->group1_enable);
327*54fd6939SJiyong Park 	}
328*54fd6939SJiyong Park #endif
329*54fd6939SJiyong Park 
330*54fd6939SJiyong Park 	/*
331*54fd6939SJiyong Park 	 * Save the counters to the local context.
332*54fd6939SJiyong Park 	 */
333*54fd6939SJiyong Park 
334*54fd6939SJiyong Park 	isb(); /* Ensure counters have been stopped */
335*54fd6939SJiyong Park 
336*54fd6939SJiyong Park 	for (i = 0U; i < amcgcr_cg0nc; i++) {
337*54fd6939SJiyong Park 		ctx->group0_cnts[i] = amu_group0_cnt_read(i);
338*54fd6939SJiyong Park 	}
339*54fd6939SJiyong Park 
340*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
341*54fd6939SJiyong Park 	for (i = 0U; i < amcgcr_cg1nc; i++) {
342*54fd6939SJiyong Park 		ctx->group1_cnts[i] = amu_group1_cnt_read(i);
343*54fd6939SJiyong Park 	}
344*54fd6939SJiyong Park #endif
345*54fd6939SJiyong Park 
346*54fd6939SJiyong Park 	return (void *)0;
347*54fd6939SJiyong Park }
348*54fd6939SJiyong Park 
amu_context_restore(const void * arg)349*54fd6939SJiyong Park static void *amu_context_restore(const void *arg)
350*54fd6939SJiyong Park {
351*54fd6939SJiyong Park 	uint32_t i;
352*54fd6939SJiyong Park 
353*54fd6939SJiyong Park 	unsigned int core_pos;
354*54fd6939SJiyong Park 	struct amu_ctx *ctx;
355*54fd6939SJiyong Park 
356*54fd6939SJiyong Park 	uint32_t id_pfr0_amu;	/* AMU version */
357*54fd6939SJiyong Park 
358*54fd6939SJiyong Park 	uint32_t amcfgr_ncg;	/* Number of counter groups */
359*54fd6939SJiyong Park 	uint32_t amcgcr_cg0nc;	/* Number of group 0 counters */
360*54fd6939SJiyong Park 
361*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
362*54fd6939SJiyong Park 	uint32_t amcgcr_cg1nc;	/* Number of group 1 counters */
363*54fd6939SJiyong Park #endif
364*54fd6939SJiyong Park 
365*54fd6939SJiyong Park 	id_pfr0_amu = read_id_pfr0_amu();
366*54fd6939SJiyong Park 	if (id_pfr0_amu == ID_PFR0_AMU_NOT_SUPPORTED) {
367*54fd6939SJiyong Park 		return (void *)0;
368*54fd6939SJiyong Park 	}
369*54fd6939SJiyong Park 
370*54fd6939SJiyong Park 	core_pos = plat_my_core_pos();
371*54fd6939SJiyong Park 	ctx = &amu_ctxs_[core_pos];
372*54fd6939SJiyong Park 
373*54fd6939SJiyong Park 	amcfgr_ncg = read_amcfgr_ncg();
374*54fd6939SJiyong Park 	amcgcr_cg0nc = read_amcgcr_cg0nc();
375*54fd6939SJiyong Park 
376*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
377*54fd6939SJiyong Park 	amcgcr_cg1nc = (amcfgr_ncg > 0U) ? read_amcgcr_cg1nc() : 0U;
378*54fd6939SJiyong Park #endif
379*54fd6939SJiyong Park 
380*54fd6939SJiyong Park 	/*
381*54fd6939SJiyong Park 	 * Sanity check that all counters were disabled when the context was
382*54fd6939SJiyong Park 	 * previously saved.
383*54fd6939SJiyong Park 	 */
384*54fd6939SJiyong Park 
385*54fd6939SJiyong Park 	assert(read_amcntenset0_px() == 0U);
386*54fd6939SJiyong Park 
387*54fd6939SJiyong Park 	if (amcfgr_ncg > 0U) {
388*54fd6939SJiyong Park 		assert(read_amcntenset1_px() == 0U);
389*54fd6939SJiyong Park 	}
390*54fd6939SJiyong Park 
391*54fd6939SJiyong Park 	/*
392*54fd6939SJiyong Park 	 * Restore the counter values from the local context.
393*54fd6939SJiyong Park 	 */
394*54fd6939SJiyong Park 
395*54fd6939SJiyong Park 	for (i = 0U; i < amcgcr_cg0nc; i++) {
396*54fd6939SJiyong Park 		amu_group0_cnt_write(i, ctx->group0_cnts[i]);
397*54fd6939SJiyong Park 	}
398*54fd6939SJiyong Park 
399*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
400*54fd6939SJiyong Park 	for (i = 0U; i < amcgcr_cg1nc; i++) {
401*54fd6939SJiyong Park 		amu_group1_cnt_write(i, ctx->group1_cnts[i]);
402*54fd6939SJiyong Park 	}
403*54fd6939SJiyong Park #endif
404*54fd6939SJiyong Park 
405*54fd6939SJiyong Park 	/*
406*54fd6939SJiyong Park 	 * Re-enable counters that were disabled during context save.
407*54fd6939SJiyong Park 	 */
408*54fd6939SJiyong Park 
409*54fd6939SJiyong Park 	write_amcntenset0_px(ctx->group0_enable);
410*54fd6939SJiyong Park 
411*54fd6939SJiyong Park #if ENABLE_AMU_AUXILIARY_COUNTERS
412*54fd6939SJiyong Park 	if (amcfgr_ncg > 0U) {
413*54fd6939SJiyong Park 		write_amcntenset1_px(ctx->group1_enable);
414*54fd6939SJiyong Park 	}
415*54fd6939SJiyong Park #endif
416*54fd6939SJiyong Park 
417*54fd6939SJiyong Park 	return (void *)0;
418*54fd6939SJiyong Park }
419*54fd6939SJiyong Park 
420*54fd6939SJiyong Park SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_start, amu_context_save);
421*54fd6939SJiyong Park SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_finish, amu_context_restore);
422