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