amu.c 6.59 KB
Newer Older
1
/*
2
 * Copyright (c) 2017-2021, ARM Limited and Contributors. All rights reserved.
3
4
5
6
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

7
#include <assert.h>
8
9
#include <stdbool.h>

10
11
#include <arch.h>
#include <arch_helpers.h>
12

13
14
15
#include <lib/el3_runtime/pubsub_events.h>
#include <lib/extensions/amu.h>
#include <lib/extensions/amu_private.h>
16

17
#include <plat/common/platform.h>
18
19

static struct amu_ctx amu_ctxs[PLATFORM_CORE_COUNT];
20

21
22
23
24
25
26
27
28
/*
 * Get AMU version value from pfr0.
 * Return values
 *   ID_PFR0_AMU_V1: FEAT_AMUv1 supported (introduced in ARM v8.4)
 *   ID_PFR0_AMU_V1P1: FEAT_AMUv1p1 supported (introduced in ARM v8.6)
 *   ID_PFR0_AMU_NOT_SUPPORTED: not supported
 */
unsigned int amu_get_version(void)
29
{
30
31
	return (unsigned int)(read_id_pfr0() >> ID_PFR0_AMU_SHIFT) &
		ID_PFR0_AMU_MASK;
32
33
}

34
35
36
37
38
39
40
41
42
43
44
45
46
47
#if AMU_GROUP1_NR_COUNTERS
/* Check if group 1 counters is implemented */
bool amu_group1_supported(void)
{
	uint32_t features = read_amcfgr() >> AMCFGR_NCG_SHIFT;

	return (features & AMCFGR_NCG_MASK) == 1U;
}
#endif

/*
 * Enable counters. This function is meant to be invoked
 * by the context management library before exiting from EL3.
 */
48
void amu_enable(bool el2_unused)
49
{
50
	if (amu_get_version() == ID_PFR0_AMU_NOT_SUPPORTED) {
51
		return;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	}

#if AMU_GROUP1_NR_COUNTERS
	/* Check and set presence of group 1 counters */
	if (!amu_group1_supported()) {
		ERROR("AMU Counter Group 1 is not implemented\n");
		panic();
	}

	/* Check number of group 1 counters */
	uint32_t cnt_num = (read_amcgcr() >> AMCGCR_CG1NC_SHIFT) &
				AMCGCR_CG1NC_MASK;
	VERBOSE("%s%u. %s%u\n",
		"Number of AMU Group 1 Counters ", cnt_num,
		"Requested number ", AMU_GROUP1_NR_COUNTERS);

	if (cnt_num < AMU_GROUP1_NR_COUNTERS) {
		ERROR("%s%u is less than %s%u\n",
		"Number of AMU Group 1 Counters ", cnt_num,
		"Requested number ", AMU_GROUP1_NR_COUNTERS);
		panic();
	}
#endif
75

76
77
78
79
80
81
82
83
84
	if (el2_unused) {
		uint64_t v;
		/*
		 * Non-secure access from EL0 or EL1 to the Activity Monitor
		 * registers do not trap to EL2.
		 */
		v = read_hcptr();
		v &= ~TAM_BIT;
		write_hcptr(v);
85
	}
86
87
88

	/* Enable group 0 counters */
	write_amcntenset0(AMU_GROUP0_COUNTERS_MASK);
89

90
#if AMU_GROUP1_NR_COUNTERS
91
92
	/* Enable group 1 counters */
	write_amcntenset1(AMU_GROUP1_COUNTERS_MASK);
93
#endif
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

	/* Initialize FEAT_AMUv1p1 features if present. */
	if (amu_get_version() < ID_PFR0_AMU_V1P1) {
		return;
	}

#if AMU_RESTRICT_COUNTERS
	/*
	 * FEAT_AMUv1p1 adds a register field to restrict access to group 1
	 * counters at all but the highest implemented EL.  This is controlled
	 * with the AMU_RESTRICT_COUNTERS compile time flag, when set, system
	 * register reads at lower ELs return zero.  Reads from the memory
	 * mapped view are unaffected.
	 */
	VERBOSE("AMU group 1 counter access restricted.\n");
	write_amcr(read_amcr() | AMCR_CG1RZ_BIT);
#else
	write_amcr(read_amcr() & ~AMCR_CG1RZ_BIT);
#endif
113
114
115
}

/* Read the group 0 counter identified by the given `idx`. */
116
uint64_t amu_group0_cnt_read(unsigned int idx)
117
{
118
	assert(amu_get_version() != ID_PFR0_AMU_NOT_SUPPORTED);
119
	assert(idx < AMU_GROUP0_NR_COUNTERS);
120
121
122
123

	return amu_group0_cnt_read_internal(idx);
}

124
125
/* Write the group 0 counter identified by the given `idx` with `val` */
void amu_group0_cnt_write(unsigned  int idx, uint64_t val)
126
{
127
	assert(amu_get_version() != ID_PFR0_AMU_NOT_SUPPORTED);
128
	assert(idx < AMU_GROUP0_NR_COUNTERS);
129
130
131
132
133

	amu_group0_cnt_write_internal(idx, val);
	isb();
}

134
135
136
#if AMU_GROUP1_NR_COUNTERS
/* Read the group 1 counter identified by the given `idx` */
uint64_t amu_group1_cnt_read(unsigned  int idx)
137
{
138
	assert(amu_get_version() != ID_PFR0_AMU_NOT_SUPPORTED);
139
140
	assert(amu_group1_supported());
	assert(idx < AMU_GROUP1_NR_COUNTERS);
141
142
143
144

	return amu_group1_cnt_read_internal(idx);
}

145
146
/* Write the group 1 counter identified by the given `idx` with `val` */
void amu_group1_cnt_write(unsigned  int idx, uint64_t val)
147
{
148
	assert(amu_get_version() != ID_PFR0_AMU_NOT_SUPPORTED);
149
150
	assert(amu_group1_supported());
	assert(idx < AMU_GROUP1_NR_COUNTERS);
151
152
153
154
155

	amu_group1_cnt_write_internal(idx, val);
	isb();
}

156
157
158
159
160
/*
 * Program the event type register for the given `idx` with
 * the event number `val`
 */
void amu_group1_set_evtype(unsigned int idx, unsigned int val)
161
{
162
	assert(amu_get_version() != ID_PFR0_AMU_NOT_SUPPORTED);
163
164
	assert(amu_group1_supported());
	assert(idx < AMU_GROUP1_NR_COUNTERS);
165
166
167

	amu_group1_set_evtype_internal(idx, val);
	isb();
168
}
169
#endif	/* AMU_GROUP1_NR_COUNTERS */
170
171
172

static void *amu_context_save(const void *arg)
{
173
174
	struct amu_ctx *ctx = &amu_ctxs[plat_my_core_pos()];
	unsigned int i;
175

176
	if (amu_get_version() == ID_PFR0_AMU_NOT_SUPPORTED) {
177
		return (void *)-1;
178
	}
179

180
181
182
183
184
185
186
#if AMU_GROUP1_NR_COUNTERS
	if (!amu_group1_supported()) {
		return (void *)-1;
	}
#endif
	/* Assert that group 0/1 counter configuration is what we expect */
	assert(read_amcntenset0_el0() == AMU_GROUP0_COUNTERS_MASK);
187

188
189
190
#if AMU_GROUP1_NR_COUNTERS
	assert(read_amcntenset1_el0() == AMU_GROUP1_COUNTERS_MASK);
#endif
191
	/*
192
	 * Disable group 0/1 counters to avoid other observers like SCP sampling
193
194
195
	 * counter values from the future via the memory mapped view.
	 */
	write_amcntenclr0(AMU_GROUP0_COUNTERS_MASK);
196
197

#if AMU_GROUP1_NR_COUNTERS
198
	write_amcntenclr1(AMU_GROUP1_COUNTERS_MASK);
199
#endif
200
201
	isb();

202
203
	/* Save all group 0 counters */
	for (i = 0U; i < AMU_GROUP0_NR_COUNTERS; i++) {
204
		ctx->group0_cnts[i] = amu_group0_cnt_read(i);
205
	}
206

207
208
209
210
211
212
213
214
#if AMU_GROUP1_NR_COUNTERS
	/* Save group 1 counters */
	for (i = 0U; i < AMU_GROUP1_NR_COUNTERS; i++) {
		if ((AMU_GROUP1_COUNTERS_MASK & (1U << i)) != 0U) {
			ctx->group1_cnts[i] = amu_group1_cnt_read(i);
		}
	}
#endif
215
	return (void *)0;
216
217
218
219
}

static void *amu_context_restore(const void *arg)
{
220
221
	struct amu_ctx *ctx = &amu_ctxs[plat_my_core_pos()];
	unsigned int i;
222

223
	if (amu_get_version() == ID_PFR0_AMU_NOT_SUPPORTED) {
224
		return (void *)-1;
225
	}
226

227
228
229
230
231
#if AMU_GROUP1_NR_COUNTERS
	if (!amu_group1_supported()) {
		return (void *)-1;
	}
#endif
232
	/* Counters were disabled in `amu_context_save()` */
233
234
235
236
237
	assert(read_amcntenset0_el0() == 0U);

#if AMU_GROUP1_NR_COUNTERS
	assert(read_amcntenset1_el0() == 0U);
#endif
238

239
240
	/* Restore all group 0 counters */
	for (i = 0U; i < AMU_GROUP0_NR_COUNTERS; i++) {
241
		amu_group0_cnt_write(i, ctx->group0_cnts[i]);
242
	}
243

244
	/* Restore group 0 counter configuration */
245
246
	write_amcntenset0(AMU_GROUP0_COUNTERS_MASK);

247
248
249
250
251
252
253
254
255
#if AMU_GROUP1_NR_COUNTERS
	/* Restore group 1 counters */
	for (i = 0U; i < AMU_GROUP1_NR_COUNTERS; i++) {
		if ((AMU_GROUP1_COUNTERS_MASK & (1U << i)) != 0U) {
			amu_group1_cnt_write(i, ctx->group1_cnts[i]);
		}
	}

	/* Restore group 1 counter configuration */
256
	write_amcntenset1(AMU_GROUP1_COUNTERS_MASK);
257
258
#endif

259
	return (void *)0;
260
261
262
263
}

SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_start, amu_context_save);
SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_finish, amu_context_restore);