blob: 58094ae405dad5650b0acf1326cf5d66692de6f7 [file] [log] [blame]
Dimitris Papastamose08005a2017-10-12 13:02:29 +01001/*
johpow01fa59c6f2020-10-02 13:41:11 -05002 * Copyright (c) 2017-2021, ARM Limited and Contributors. All rights reserved.
Dimitris Papastamose08005a2017-10-12 13:02:29 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Dimitris Papastamos525c37a2017-11-13 09:49:45 +00007#include <assert.h>
Chris Kaya5fde282021-05-26 11:58:23 +01008#include <cdefs.h>
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +01009#include <stdbool.h>
Dimitris Papastamose08005a2017-10-12 13:02:29 +010010
Chris Kay26a79612021-05-24 20:35:26 +010011#include "../amu_private.h"
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <arch.h>
johpow01fa59c6f2020-10-02 13:41:11 -050013#include <arch_features.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <arch_helpers.h>
15#include <lib/el3_runtime/pubsub_events.h>
16#include <lib/extensions/amu.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017
Alexei Fedorov7e6306b2020-07-14 08:17:56 +010018#include <plat/common/platform.h>
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +000019
Chris Kay26a79612021-05-24 20:35:26 +010020struct amu_ctx {
21 uint64_t group0_cnts[AMU_GROUP0_MAX_COUNTERS];
22#if ENABLE_AMU_AUXILIARY_COUNTERS
23 uint64_t group1_cnts[AMU_GROUP1_MAX_COUNTERS];
24#endif
25
26 /* Architected event counter 1 does not have an offset register */
27 uint64_t group0_voffsets[AMU_GROUP0_MAX_COUNTERS - 1U];
28#if ENABLE_AMU_AUXILIARY_COUNTERS
29 uint64_t group1_voffsets[AMU_GROUP1_MAX_COUNTERS];
30#endif
31
32 uint16_t group0_enable;
33#if ENABLE_AMU_AUXILIARY_COUNTERS
34 uint16_t group1_enable;
35#endif
36};
37
38static struct amu_ctx amu_ctxs_[PLATFORM_CORE_COUNT];
39
40CASSERT((sizeof(amu_ctxs_[0].group0_enable) * CHAR_BIT) <= AMU_GROUP0_MAX_COUNTERS,
41 amu_ctx_group0_enable_cannot_represent_all_group0_counters);
42
43#if ENABLE_AMU_AUXILIARY_COUNTERS
44CASSERT((sizeof(amu_ctxs_[0].group1_enable) * CHAR_BIT) <= AMU_GROUP1_MAX_COUNTERS,
45 amu_ctx_group1_enable_cannot_represent_all_group1_counters);
46#endif
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +000047
Chris Kaya5fde282021-05-26 11:58:23 +010048static inline __unused uint64_t read_id_aa64pfr0_el1_amu(void)
Dimitris Papastamose08005a2017-10-12 13:02:29 +010049{
Chris Kaya5fde282021-05-26 11:58:23 +010050 return (read_id_aa64pfr0_el1() >> ID_AA64PFR0_AMU_SHIFT) &
johpow01fa59c6f2020-10-02 13:41:11 -050051 ID_AA64PFR0_AMU_MASK;
Alexei Fedorov7e6306b2020-07-14 08:17:56 +010052}
53
Chris Kaya5fde282021-05-26 11:58:23 +010054static inline __unused uint64_t read_hcr_el2_amvoffen(void)
55{
56 return (read_hcr_el2() & HCR_AMVOFFEN_BIT) >>
57 HCR_AMVOFFEN_SHIFT;
58}
59
60static inline __unused void write_cptr_el2_tam(uint64_t value)
61{
62 write_cptr_el2((read_cptr_el2() & ~CPTR_EL2_TAM_BIT) |
63 ((value << CPTR_EL2_TAM_SHIFT) & CPTR_EL2_TAM_BIT));
64}
65
66static inline __unused void write_cptr_el3_tam(cpu_context_t *ctx, uint64_t tam)
67{
68 uint64_t value = read_ctx_reg(get_el3state_ctx(ctx), CTX_CPTR_EL3);
69
70 value &= ~TAM_BIT;
71 value |= (tam << TAM_SHIFT) & TAM_BIT;
72
73 write_ctx_reg(get_el3state_ctx(ctx), CTX_CPTR_EL3, value);
74}
75
76static inline __unused void write_hcr_el2_amvoffen(uint64_t value)
77{
78 write_hcr_el2((read_hcr_el2() & ~HCR_AMVOFFEN_BIT) |
79 ((value << HCR_AMVOFFEN_SHIFT) & HCR_AMVOFFEN_BIT));
80}
81
82static inline __unused void write_amcr_el0_cg1rz(uint64_t value)
83{
84 write_amcr_el0((read_amcr_el0() & ~AMCR_CG1RZ_BIT) |
85 ((value << AMCR_CG1RZ_SHIFT) & AMCR_CG1RZ_BIT));
86}
87
88static inline __unused uint64_t read_amcfgr_el0_ncg(void)
89{
90 return (read_amcfgr_el0() >> AMCFGR_EL0_NCG_SHIFT) &
91 AMCFGR_EL0_NCG_MASK;
92}
93
Chris Kay26a79612021-05-24 20:35:26 +010094static inline __unused uint64_t read_amcgcr_el0_cg0nc(void)
Chris Kaya40141d2021-05-25 12:33:18 +010095{
96 return (read_amcgcr_el0() >> AMCGCR_EL0_CG0NC_SHIFT) &
97 AMCGCR_EL0_CG0NC_MASK;
98}
99
Chris Kaya5fde282021-05-26 11:58:23 +0100100static inline __unused uint64_t read_amcg1idr_el0_voff(void)
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100101{
Chris Kaya5fde282021-05-26 11:58:23 +0100102 return (read_amcg1idr_el0() >> AMCG1IDR_VOFF_SHIFT) &
103 AMCG1IDR_VOFF_MASK;
104}
105
106static inline __unused uint64_t read_amcgcr_el0_cg1nc(void)
107{
108 return (read_amcgcr_el0() >> AMCGCR_EL0_CG1NC_SHIFT) &
109 AMCGCR_EL0_CG1NC_MASK;
110}
Dimitris Papastamose08005a2017-10-12 13:02:29 +0100111
Chris Kaya5fde282021-05-26 11:58:23 +0100112static inline __unused uint64_t read_amcntenset0_el0_px(void)
113{
114 return (read_amcntenset0_el0() >> AMCNTENSET0_EL0_Pn_SHIFT) &
115 AMCNTENSET0_EL0_Pn_MASK;
116}
117
118static inline __unused uint64_t read_amcntenset1_el0_px(void)
119{
120 return (read_amcntenset1_el0() >> AMCNTENSET1_EL0_Pn_SHIFT) &
121 AMCNTENSET1_EL0_Pn_MASK;
122}
123
124static inline __unused void write_amcntenset0_el0_px(uint64_t px)
125{
126 uint64_t value = read_amcntenset0_el0();
127
128 value &= ~AMCNTENSET0_EL0_Pn_MASK;
129 value |= (px << AMCNTENSET0_EL0_Pn_SHIFT) & AMCNTENSET0_EL0_Pn_MASK;
130
131 write_amcntenset0_el0(value);
132}
133
134static inline __unused void write_amcntenset1_el0_px(uint64_t px)
135{
136 uint64_t value = read_amcntenset1_el0();
137
138 value &= ~AMCNTENSET1_EL0_Pn_MASK;
139 value |= (px << AMCNTENSET1_EL0_Pn_SHIFT) & AMCNTENSET1_EL0_Pn_MASK;
140
141 write_amcntenset1_el0(value);
142}
143
144static inline __unused void write_amcntenclr0_el0_px(uint64_t px)
145{
146 uint64_t value = read_amcntenclr0_el0();
147
148 value &= ~AMCNTENCLR0_EL0_Pn_MASK;
149 value |= (px << AMCNTENCLR0_EL0_Pn_SHIFT) & AMCNTENCLR0_EL0_Pn_MASK;
150
151 write_amcntenclr0_el0(value);
152}
153
154static inline __unused void write_amcntenclr1_el0_px(uint64_t px)
155{
156 uint64_t value = read_amcntenclr1_el0();
157
158 value &= ~AMCNTENCLR1_EL0_Pn_MASK;
159 value |= (px << AMCNTENCLR1_EL0_Pn_SHIFT) & AMCNTENCLR1_EL0_Pn_MASK;
160
161 write_amcntenclr1_el0(value);
162}
163
Chris Kay26a79612021-05-24 20:35:26 +0100164static __unused bool amu_supported(void)
Chris Kaya5fde282021-05-26 11:58:23 +0100165{
166 return read_id_aa64pfr0_el1_amu() >= ID_AA64PFR0_AMU_V1;
167}
168
Chris Kay26a79612021-05-24 20:35:26 +0100169static __unused bool amu_v1p1_supported(void)
Chris Kaya5fde282021-05-26 11:58:23 +0100170{
171 return read_id_aa64pfr0_el1_amu() >= ID_AA64PFR0_AMU_V1P1;
172}
173
174#if ENABLE_AMU_AUXILIARY_COUNTERS
Chris Kay26a79612021-05-24 20:35:26 +0100175static __unused bool amu_group1_supported(void)
Chris Kaya5fde282021-05-26 11:58:23 +0100176{
177 return read_amcfgr_el0_ncg() > 0U;
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000178}
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100179#endif
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000180
181/*
Chris Kay26a79612021-05-24 20:35:26 +0100182 * Enable counters. This function is meant to be invoked by the context
183 * management library before exiting from EL3.
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000184 */
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +0100185void amu_enable(bool el2_unused, cpu_context_t *ctx)
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000186{
Chris Kay26a79612021-05-24 20:35:26 +0100187 uint64_t id_aa64pfr0_el1_amu; /* AMU version */
188
189 uint64_t amcfgr_el0_ncg; /* Number of counter groups */
190 uint64_t amcgcr_el0_cg0nc; /* Number of group 0 counters */
191
192 uint64_t amcntenset0_el0_px = 0x0; /* Group 0 enable mask */
193 uint64_t amcntenset1_el0_px = 0x0; /* Group 1 enable mask */
194
195 id_aa64pfr0_el1_amu = read_id_aa64pfr0_el1_amu();
196 if (id_aa64pfr0_el1_amu == ID_AA64PFR0_AMU_NOT_SUPPORTED) {
197 /*
198 * If the AMU is unsupported, nothing needs to be done.
199 */
200
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000201 return;
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100202 }
203
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000204 if (el2_unused) {
Dimitris Papastamose08005a2017-10-12 13:02:29 +0100205 /*
Chris Kay26a79612021-05-24 20:35:26 +0100206 * CPTR_EL2.TAM: Set to zero so any accesses to the Activity
207 * Monitor registers do not trap to EL2.
Dimitris Papastamose08005a2017-10-12 13:02:29 +0100208 */
Chris Kaya5fde282021-05-26 11:58:23 +0100209 write_cptr_el2_tam(0U);
Dimitris Papastamose08005a2017-10-12 13:02:29 +0100210 }
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000211
212 /*
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +0100213 * Retrieve and update the CPTR_EL3 value from the context mentioned
214 * in 'ctx'. Set CPTR_EL3.TAM to zero so that any accesses to
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000215 * the Activity Monitor registers do not trap to EL3.
216 */
Chris Kaya5fde282021-05-26 11:58:23 +0100217 write_cptr_el3_tam(ctx, 0U);
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000218
Chris Kay26a79612021-05-24 20:35:26 +0100219 /*
220 * Retrieve the number of architected counters. All of these counters
221 * are enabled by default.
222 */
223
224 amcgcr_el0_cg0nc = read_amcgcr_el0_cg0nc();
225 amcntenset0_el0_px = (UINT64_C(1) << (amcgcr_el0_cg0nc)) - 1U;
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100226
Chris Kay26a79612021-05-24 20:35:26 +0100227 assert(amcgcr_el0_cg0nc <= AMU_AMCGCR_CG0NC_MAX);
228
229 /*
230 * Enable the requested counters.
231 */
232
233 write_amcntenset0_el0_px(amcntenset0_el0_px);
234
235 amcfgr_el0_ncg = read_amcfgr_el0_ncg();
236 if (amcfgr_el0_ncg > 0U) {
237 write_amcntenset1_el0_px(amcntenset1_el0_px);
Chris Kay925fda42021-05-25 10:42:56 +0100238 }
johpow01fa59c6f2020-10-02 13:41:11 -0500239
240 /* Initialize FEAT_AMUv1p1 features if present. */
Chris Kay26a79612021-05-24 20:35:26 +0100241 if (id_aa64pfr0_el1_amu >= ID_AA64PFR0_AMU_V1P1) {
johpow01fa59c6f2020-10-02 13:41:11 -0500242 return;
243 }
244
245 if (el2_unused) {
246 /* Make sure virtual offsets are disabled if EL2 not used. */
Chris Kaya5fde282021-05-26 11:58:23 +0100247 write_hcr_el2_amvoffen(0U);
johpow01fa59c6f2020-10-02 13:41:11 -0500248 }
249
250#if AMU_RESTRICT_COUNTERS
251 /*
252 * FEAT_AMUv1p1 adds a register field to restrict access to group 1
253 * counters at all but the highest implemented EL. This is controlled
254 * with the AMU_RESTRICT_COUNTERS compile time flag, when set, system
255 * register reads at lower ELs return zero. Reads from the memory
256 * mapped view are unaffected.
257 */
258 VERBOSE("AMU group 1 counter access restricted.\n");
Chris Kaya5fde282021-05-26 11:58:23 +0100259 write_amcr_el0_cg1rz(1U);
johpow01fa59c6f2020-10-02 13:41:11 -0500260#else
Chris Kaya5fde282021-05-26 11:58:23 +0100261 write_amcr_el0_cg1rz(0U);
johpow01fa59c6f2020-10-02 13:41:11 -0500262#endif
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000263}
264
265/* Read the group 0 counter identified by the given `idx`. */
Chris Kayf13c6b52021-05-24 21:00:07 +0100266static uint64_t amu_group0_cnt_read(unsigned int idx)
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000267{
Chris Kaya5fde282021-05-26 11:58:23 +0100268 assert(amu_supported());
Chris Kaya40141d2021-05-25 12:33:18 +0100269 assert(idx < read_amcgcr_el0_cg0nc());
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000270
271 return amu_group0_cnt_read_internal(idx);
272}
273
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100274/* Write the group 0 counter identified by the given `idx` with `val` */
Chris Kayf13c6b52021-05-24 21:00:07 +0100275static void amu_group0_cnt_write(unsigned int idx, uint64_t val)
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000276{
Chris Kaya5fde282021-05-26 11:58:23 +0100277 assert(amu_supported());
Chris Kaya40141d2021-05-25 12:33:18 +0100278 assert(idx < read_amcgcr_el0_cg0nc());
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000279
280 amu_group0_cnt_write_internal(idx, val);
281 isb();
282}
283
johpow01fa59c6f2020-10-02 13:41:11 -0500284/*
Chris Kay26a79612021-05-24 20:35:26 +0100285 * Unlike with auxiliary counters, we cannot detect at runtime whether an
286 * architected counter supports a virtual offset. These are instead fixed
287 * according to FEAT_AMUv1p1, but this switch will need to be updated if later
288 * revisions of FEAT_AMU add additional architected counters.
289 */
290static bool amu_group0_voffset_supported(uint64_t idx)
291{
292 switch (idx) {
293 case 0U:
294 case 2U:
295 case 3U:
296 return true;
297
298 case 1U:
299 return false;
300
301 default:
302 ERROR("AMU: can't set up virtual offset for unknown "
303 "architected counter %llu!\n", idx);
304
305 panic();
306 }
307}
308
309/*
johpow01fa59c6f2020-10-02 13:41:11 -0500310 * Read the group 0 offset register for a given index. Index must be 0, 2,
311 * or 3, the register for 1 does not exist.
312 *
313 * Using this function requires FEAT_AMUv1p1 support.
314 */
Chris Kayf13c6b52021-05-24 21:00:07 +0100315static uint64_t amu_group0_voffset_read(unsigned int idx)
johpow01fa59c6f2020-10-02 13:41:11 -0500316{
Chris Kaya5fde282021-05-26 11:58:23 +0100317 assert(amu_v1p1_supported());
Chris Kaya40141d2021-05-25 12:33:18 +0100318 assert(idx < read_amcgcr_el0_cg0nc());
johpow01fa59c6f2020-10-02 13:41:11 -0500319 assert(idx != 1U);
320
321 return amu_group0_voffset_read_internal(idx);
322}
323
324/*
325 * Write the group 0 offset register for a given index. Index must be 0, 2, or
326 * 3, the register for 1 does not exist.
327 *
328 * Using this function requires FEAT_AMUv1p1 support.
329 */
Chris Kayf13c6b52021-05-24 21:00:07 +0100330static void amu_group0_voffset_write(unsigned int idx, uint64_t val)
johpow01fa59c6f2020-10-02 13:41:11 -0500331{
Chris Kaya5fde282021-05-26 11:58:23 +0100332 assert(amu_v1p1_supported());
Chris Kaya40141d2021-05-25 12:33:18 +0100333 assert(idx < read_amcgcr_el0_cg0nc());
johpow01fa59c6f2020-10-02 13:41:11 -0500334 assert(idx != 1U);
335
336 amu_group0_voffset_write_internal(idx, val);
337 isb();
338}
339
Chris Kay925fda42021-05-25 10:42:56 +0100340#if ENABLE_AMU_AUXILIARY_COUNTERS
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100341/* Read the group 1 counter identified by the given `idx` */
Chris Kayf13c6b52021-05-24 21:00:07 +0100342static uint64_t amu_group1_cnt_read(unsigned int idx)
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000343{
Chris Kaya5fde282021-05-26 11:58:23 +0100344 assert(amu_supported());
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100345 assert(amu_group1_supported());
Chris Kayda819142021-05-25 15:24:18 +0100346 assert(idx < read_amcgcr_el0_cg1nc());
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000347
348 return amu_group1_cnt_read_internal(idx);
349}
350
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100351/* Write the group 1 counter identified by the given `idx` with `val` */
Chris Kayf13c6b52021-05-24 21:00:07 +0100352static void amu_group1_cnt_write(unsigned int idx, uint64_t val)
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000353{
Chris Kaya5fde282021-05-26 11:58:23 +0100354 assert(amu_supported());
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100355 assert(amu_group1_supported());
Chris Kayda819142021-05-25 15:24:18 +0100356 assert(idx < read_amcgcr_el0_cg1nc());
Dimitris Papastamos525c37a2017-11-13 09:49:45 +0000357
358 amu_group1_cnt_write_internal(idx, val);
359 isb();
360}
361
362/*
johpow01fa59c6f2020-10-02 13:41:11 -0500363 * Read the group 1 offset register for a given index.
364 *
365 * Using this function requires FEAT_AMUv1p1 support.
366 */
Chris Kayf13c6b52021-05-24 21:00:07 +0100367static uint64_t amu_group1_voffset_read(unsigned int idx)
johpow01fa59c6f2020-10-02 13:41:11 -0500368{
Chris Kaya5fde282021-05-26 11:58:23 +0100369 assert(amu_v1p1_supported());
johpow01fa59c6f2020-10-02 13:41:11 -0500370 assert(amu_group1_supported());
Chris Kayda819142021-05-25 15:24:18 +0100371 assert(idx < read_amcgcr_el0_cg1nc());
Chris Kaya5fde282021-05-26 11:58:23 +0100372 assert((read_amcg1idr_el0_voff() & (UINT64_C(1) << idx)) != 0U);
johpow01fa59c6f2020-10-02 13:41:11 -0500373
374 return amu_group1_voffset_read_internal(idx);
375}
376
377/*
378 * Write the group 1 offset register for a given index.
379 *
380 * Using this function requires FEAT_AMUv1p1 support.
381 */
Chris Kayf13c6b52021-05-24 21:00:07 +0100382static void amu_group1_voffset_write(unsigned int idx, uint64_t val)
johpow01fa59c6f2020-10-02 13:41:11 -0500383{
Chris Kaya5fde282021-05-26 11:58:23 +0100384 assert(amu_v1p1_supported());
johpow01fa59c6f2020-10-02 13:41:11 -0500385 assert(amu_group1_supported());
Chris Kayda819142021-05-25 15:24:18 +0100386 assert(idx < read_amcgcr_el0_cg1nc());
Chris Kaya5fde282021-05-26 11:58:23 +0100387 assert((read_amcg1idr_el0_voff() & (UINT64_C(1) << idx)) != 0U);
johpow01fa59c6f2020-10-02 13:41:11 -0500388
389 amu_group1_voffset_write_internal(idx, val);
390 isb();
391}
Chris Kay925fda42021-05-25 10:42:56 +0100392#endif
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000393
394static void *amu_context_save(const void *arg)
395{
Chris Kay26a79612021-05-24 20:35:26 +0100396 uint64_t i, j;
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000397
Chris Kay26a79612021-05-24 20:35:26 +0100398 unsigned int core_pos;
399 struct amu_ctx *ctx;
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000400
Chris Kay26a79612021-05-24 20:35:26 +0100401 uint64_t id_aa64pfr0_el1_amu; /* AMU version */
402 uint64_t hcr_el2_amvoffen; /* AMU virtual offsets enabled */
403 uint64_t amcgcr_el0_cg0nc; /* Number of group 0 counters */
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000404
Chris Kay925fda42021-05-25 10:42:56 +0100405#if ENABLE_AMU_AUXILIARY_COUNTERS
Chris Kay26a79612021-05-24 20:35:26 +0100406 uint64_t amcg1idr_el0_voff; /* Auxiliary counters with virtual offsets */
407 uint64_t amcfgr_el0_ncg; /* Number of counter groups */
408 uint64_t amcgcr_el0_cg1nc; /* Number of group 1 counters */
409#endif
410
411 id_aa64pfr0_el1_amu = read_id_aa64pfr0_el1_amu();
412 if (id_aa64pfr0_el1_amu == ID_AA64PFR0_AMU_NOT_SUPPORTED) {
413 return (void *)0;
Chris Kay925fda42021-05-25 10:42:56 +0100414 }
Chris Kay26a79612021-05-24 20:35:26 +0100415
416 core_pos = plat_my_core_pos();
417 ctx = &amu_ctxs_[core_pos];
418
419 amcgcr_el0_cg0nc = read_amcgcr_el0_cg0nc();
420 hcr_el2_amvoffen = (id_aa64pfr0_el1_amu >= ID_AA64PFR0_AMU_V1P1) ?
421 read_hcr_el2_amvoffen() : 0U;
422
423#if ENABLE_AMU_AUXILIARY_COUNTERS
424 amcfgr_el0_ncg = read_amcfgr_el0_ncg();
425 amcgcr_el0_cg1nc = (amcfgr_el0_ncg > 0U) ? read_amcgcr_el0_cg1nc() : 0U;
426 amcg1idr_el0_voff = (hcr_el2_amvoffen != 0U) ? read_amcg1idr_el0_voff() : 0U;
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100427#endif
Chris Kay925fda42021-05-25 10:42:56 +0100428
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000429 /*
Chris Kay26a79612021-05-24 20:35:26 +0100430 * Disable all AMU counters.
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000431 */
Chris Kay26a79612021-05-24 20:35:26 +0100432
433 ctx->group0_enable = read_amcntenset0_el0_px();
434 write_amcntenclr0_el0_px(ctx->group0_enable);
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100435
Chris Kay925fda42021-05-25 10:42:56 +0100436#if ENABLE_AMU_AUXILIARY_COUNTERS
Chris Kay26a79612021-05-24 20:35:26 +0100437 if (amcfgr_el0_ncg > 0U) {
438 ctx->group1_enable = read_amcntenset1_el0_px();
439 write_amcntenclr1_el0_px(ctx->group1_enable);
Chris Kay925fda42021-05-25 10:42:56 +0100440 }
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100441#endif
Chris Kay925fda42021-05-25 10:42:56 +0100442
Chris Kay26a79612021-05-24 20:35:26 +0100443 /*
444 * Save the counters to the local context.
445 */
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000446
Chris Kay26a79612021-05-24 20:35:26 +0100447 isb(); /* Ensure counters have been stopped */
448
449 for (i = 0U; i < amcgcr_el0_cg0nc; i++) {
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000450 ctx->group0_cnts[i] = amu_group0_cnt_read(i);
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100451 }
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000452
Chris Kay26a79612021-05-24 20:35:26 +0100453#if ENABLE_AMU_AUXILIARY_COUNTERS
454 for (i = 0U; i < amcgcr_el0_cg1nc; i++) {
455 ctx->group1_cnts[i] = amu_group1_cnt_read(i);
johpow01fa59c6f2020-10-02 13:41:11 -0500456 }
Chris Kay26a79612021-05-24 20:35:26 +0100457#endif
johpow01fa59c6f2020-10-02 13:41:11 -0500458
Chris Kay26a79612021-05-24 20:35:26 +0100459 /*
460 * Save virtual offsets for counters that offer them.
461 */
462
463 if (hcr_el2_amvoffen != 0U) {
464 for (i = 0U, j = 0U; i < amcgcr_el0_cg0nc; i++) {
465 if (!amu_group0_voffset_supported(i)) {
466 continue; /* No virtual offset */
Chris Kay925fda42021-05-25 10:42:56 +0100467 }
johpow01fa59c6f2020-10-02 13:41:11 -0500468
Chris Kay26a79612021-05-24 20:35:26 +0100469 ctx->group0_voffsets[j++] = amu_group0_voffset_read(i);
470 }
johpow01fa59c6f2020-10-02 13:41:11 -0500471
Chris Kay26a79612021-05-24 20:35:26 +0100472#if ENABLE_AMU_AUXILIARY_COUNTERS
473 for (i = 0U, j = 0U; i < amcgcr_el0_cg1nc; i++) {
474 if ((amcg1idr_el0_voff >> i) & 1U) {
475 continue; /* No virtual offset */
johpow01fa59c6f2020-10-02 13:41:11 -0500476 }
Chris Kay26a79612021-05-24 20:35:26 +0100477
478 ctx->group1_voffsets[j++] = amu_group1_voffset_read(i);
johpow01fa59c6f2020-10-02 13:41:11 -0500479 }
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100480#endif
Chris Kay26a79612021-05-24 20:35:26 +0100481 }
Chris Kay925fda42021-05-25 10:42:56 +0100482
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +0100483 return (void *)0;
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000484}
485
486static void *amu_context_restore(const void *arg)
487{
Chris Kay26a79612021-05-24 20:35:26 +0100488 uint64_t i, j;
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000489
Chris Kay26a79612021-05-24 20:35:26 +0100490 unsigned int core_pos;
491 struct amu_ctx *ctx;
492
493 uint64_t id_aa64pfr0_el1_amu; /* AMU version */
494
495 uint64_t hcr_el2_amvoffen; /* AMU virtual offsets enabled */
496
497 uint64_t amcfgr_el0_ncg; /* Number of counter groups */
498 uint64_t amcgcr_el0_cg0nc; /* Number of group 0 counters */
499
500#if ENABLE_AMU_AUXILIARY_COUNTERS
501 uint64_t amcgcr_el0_cg1nc; /* Number of group 1 counters */
502 uint64_t amcg1idr_el0_voff; /* Auxiliary counters with virtual offsets */
503#endif
504
505 id_aa64pfr0_el1_amu = read_id_aa64pfr0_el1_amu();
506 if (id_aa64pfr0_el1_amu == ID_AA64PFR0_AMU_NOT_SUPPORTED) {
507 return (void *)0;
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100508 }
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000509
Chris Kay26a79612021-05-24 20:35:26 +0100510 core_pos = plat_my_core_pos();
511 ctx = &amu_ctxs_[core_pos];
512
513 amcfgr_el0_ncg = read_amcfgr_el0_ncg();
514 amcgcr_el0_cg0nc = read_amcgcr_el0_cg0nc();
515
516 hcr_el2_amvoffen = (id_aa64pfr0_el1_amu >= ID_AA64PFR0_AMU_V1P1) ?
517 read_hcr_el2_amvoffen() : 0U;
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000518
Chris Kay925fda42021-05-25 10:42:56 +0100519#if ENABLE_AMU_AUXILIARY_COUNTERS
Chris Kay26a79612021-05-24 20:35:26 +0100520 amcgcr_el0_cg1nc = (amcfgr_el0_ncg > 0U) ? read_amcgcr_el0_cg1nc() : 0U;
521 amcg1idr_el0_voff = (hcr_el2_amvoffen != 0U) ? read_amcg1idr_el0_voff() : 0U;
522#endif
523
524 /*
525 * Sanity check that all counters were disabled when the context was
526 * previously saved.
527 */
528
529 assert(read_amcntenset0_el0_px() == 0U);
530
531 if (amcfgr_el0_ncg > 0U) {
Chris Kay925fda42021-05-25 10:42:56 +0100532 assert(read_amcntenset1_el0_px() == 0U);
533 }
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000534
Chris Kay26a79612021-05-24 20:35:26 +0100535 /*
536 * Restore the counter values from the local context.
537 */
538
539 for (i = 0U; i < amcgcr_el0_cg0nc; i++) {
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100540 amu_group0_cnt_write(i, ctx->group0_cnts[i]);
541 }
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000542
Chris Kay26a79612021-05-24 20:35:26 +0100543#if ENABLE_AMU_AUXILIARY_COUNTERS
544 for (i = 0U; i < amcgcr_el0_cg1nc; i++) {
545 amu_group1_cnt_write(i, ctx->group1_cnts[i]);
johpow01fa59c6f2020-10-02 13:41:11 -0500546 }
Chris Kay26a79612021-05-24 20:35:26 +0100547#endif
johpow01fa59c6f2020-10-02 13:41:11 -0500548
Chris Kay26a79612021-05-24 20:35:26 +0100549 /*
550 * Restore virtual offsets for counters that offer them.
551 */
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100552
Chris Kay26a79612021-05-24 20:35:26 +0100553 if (hcr_el2_amvoffen != 0U) {
554 for (i = 0U, j = 0U; i < amcgcr_el0_cg0nc; i++) {
555 if (!amu_group0_voffset_supported(i)) {
556 continue; /* No virtual offset */
Chris Kay925fda42021-05-25 10:42:56 +0100557 }
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000558
Chris Kay26a79612021-05-24 20:35:26 +0100559 amu_group0_voffset_write(i, ctx->group0_voffsets[j++]);
560 }
johpow01fa59c6f2020-10-02 13:41:11 -0500561
Chris Kay26a79612021-05-24 20:35:26 +0100562#if ENABLE_AMU_AUXILIARY_COUNTERS
563 for (i = 0U, j = 0U; i < amcgcr_el0_cg1nc; i++) {
564 if ((amcg1idr_el0_voff >> i) & 1U) {
565 continue; /* No virtual offset */
johpow01fa59c6f2020-10-02 13:41:11 -0500566 }
Chris Kay26a79612021-05-24 20:35:26 +0100567
568 amu_group1_voffset_write(i, ctx->group1_voffsets[j++]);
johpow01fa59c6f2020-10-02 13:41:11 -0500569 }
Chris Kay26a79612021-05-24 20:35:26 +0100570#endif
571 }
572
573 /*
574 * Re-enable counters that were disabled during context save.
575 */
576
577 write_amcntenset0_el0_px(ctx->group0_enable);
johpow01fa59c6f2020-10-02 13:41:11 -0500578
Chris Kay26a79612021-05-24 20:35:26 +0100579#if ENABLE_AMU_AUXILIARY_COUNTERS
580 if (amcfgr_el0_ncg > 0) {
581 write_amcntenset1_el0_px(ctx->group1_enable);
Chris Kay925fda42021-05-25 10:42:56 +0100582 }
Alexei Fedorov7e6306b2020-07-14 08:17:56 +0100583#endif
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000584
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +0100585 return (void *)0;
Dimitris Papastamoseaf3e6d2017-11-28 13:47:06 +0000586}
587
588SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_start, amu_context_save);
589SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_finish, amu_context_restore);