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