Dimitris Papastamos | dda48b0 | 2017-10-17 14:03:14 +0100 | [diff] [blame] | 1 | /* |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2025, 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 | |
Dimitris Papastamos | dda48b0 | 2017-10-17 14:03:14 +0100 | [diff] [blame] | 11 | #include <arch.h> |
Andre Przywara | 906776e | 2023-03-03 10:30:06 +0000 | [diff] [blame] | 12 | #include <arch_features.h> |
Dimitris Papastamos | dda48b0 | 2017-10-17 14:03:14 +0100 | [diff] [blame] | 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 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 20 | amu_regs_t amu_ctx[PLATFORM_CORE_COUNT]; |
Chris Kay | a40141d | 2021-05-25 12:33:18 +0100 | [diff] [blame] | 21 | |
Chris Kay | a5fde28 | 2021-05-26 11:58:23 +0100 | [diff] [blame] | 22 | static inline __unused uint32_t read_amcgcr_cg1nc(void) |
| 23 | { |
| 24 | return (read_amcgcr() >> AMCGCR_CG1NC_SHIFT) & |
| 25 | AMCGCR_CG1NC_MASK; |
| 26 | } |
| 27 | |
Alexei Fedorov | 7e6306b | 2020-07-14 08:17:56 +0100 | [diff] [blame] | 28 | /* |
Chris Kay | 26a7961 | 2021-05-24 20:35:26 +0100 | [diff] [blame] | 29 | * Enable counters. This function is meant to be invoked by the context |
| 30 | * management library before exiting from EL3. |
Alexei Fedorov | 7e6306b | 2020-07-14 08:17:56 +0100 | [diff] [blame] | 31 | */ |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 32 | void amu_enable(bool el2_unused) |
Joel Hutton | 0dcdd8d | 2017-12-21 15:21:20 +0000 | [diff] [blame] | 33 | { |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 34 | if (el2_unused) { |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 35 | /* |
Chris Kay | 26a7961 | 2021-05-24 20:35:26 +0100 | [diff] [blame] | 36 | * HCPTR.TAM: Set to zero so any accesses to the Activity |
| 37 | * Monitor registers do not trap to EL2. |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 38 | */ |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 39 | write_hcptr(read_hcptr() & ~TAM_BIT); |
Dimitris Papastamos | dda48b0 | 2017-10-17 14:03:14 +0100 | [diff] [blame] | 40 | } |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 41 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 42 | /* Architecture is currently pinned to 4 */ |
| 43 | assert((read_amcgcr() & AMCGCR_CG0NC_MASK) == CTX_AMU_GRP0_ALL); |
Chris Kay | f11909f | 2021-08-19 11:21:52 +0100 | [diff] [blame] | 44 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 45 | /* Enable all architected counters by default */ |
| 46 | write_amcntenset0(AMCNTENSET0_Pn_MASK); |
| 47 | if (is_feat_amu_aux_supported()) { |
Chris Kay | f11909f | 2021-08-19 11:21:52 +0100 | [diff] [blame] | 48 | unsigned int core_pos = plat_my_core_pos(); |
| 49 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 50 | /* Something went wrong if we're trying to write higher bits */ |
| 51 | assert((get_amu_aux_enables(core_pos) & ~AMCNTENSET1_Pn_MASK) == 0); |
| 52 | write_amcntenset1(get_amu_aux_enables(core_pos)); |
Chris Kay | 925fda4 | 2021-05-25 10:42:56 +0100 | [diff] [blame] | 53 | } |
johpow01 | fa59c6f | 2020-10-02 13:41:11 -0500 | [diff] [blame] | 54 | |
Andre Przywara | 906776e | 2023-03-03 10:30:06 +0000 | [diff] [blame] | 55 | /* Bail out if FEAT_AMUv1p1 features are not present. */ |
| 56 | if (!is_feat_amuv1p1_supported()) { |
johpow01 | fa59c6f | 2020-10-02 13:41:11 -0500 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
| 60 | #if AMU_RESTRICT_COUNTERS |
| 61 | /* |
| 62 | * FEAT_AMUv1p1 adds a register field to restrict access to group 1 |
| 63 | * counters at all but the highest implemented EL. This is controlled |
| 64 | * with the AMU_RESTRICT_COUNTERS compile time flag, when set, system |
| 65 | * register reads at lower ELs return zero. Reads from the memory |
| 66 | * mapped view are unaffected. |
| 67 | */ |
| 68 | VERBOSE("AMU group 1 counter access restricted.\n"); |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 69 | write_amcr(read_amcr() | 1U); |
johpow01 | fa59c6f | 2020-10-02 13:41:11 -0500 | [diff] [blame] | 70 | #else |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 71 | write_amcr(0); |
johpow01 | fa59c6f | 2020-10-02 13:41:11 -0500 | [diff] [blame] | 72 | #endif |
Joel Hutton | 0dcdd8d | 2017-12-21 15:21:20 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 75 | static void *amu_context_save(const void *arg) |
| 76 | { |
Andre Przywara | 906776e | 2023-03-03 10:30:06 +0000 | [diff] [blame] | 77 | if (!is_feat_amu_supported()) { |
Chris Kay | 26a7961 | 2021-05-24 20:35:26 +0100 | [diff] [blame] | 78 | return (void *)0; |
Chris Kay | 925fda4 | 2021-05-25 10:42:56 +0100 | [diff] [blame] | 79 | } |
Chris Kay | 26a7961 | 2021-05-24 20:35:26 +0100 | [diff] [blame] | 80 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 81 | unsigned int core_pos = *(unsigned int *)arg; |
| 82 | amu_regs_t *ctx = &amu_ctx[core_pos]; |
Chris Kay | 26a7961 | 2021-05-24 20:35:26 +0100 | [diff] [blame] | 83 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 84 | /* Disable all counters so we can write to them safely later */ |
| 85 | write_amcntenclr0(AMCNTENCLR0_Pn_MASK); |
| 86 | if (is_feat_amu_aux_supported()) { |
| 87 | write_amcntenclr1(get_amu_aux_enables(core_pos)); |
Chris Kay | 925fda4 | 2021-05-25 10:42:56 +0100 | [diff] [blame] | 88 | } |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 89 | |
Chris Kay | 26a7961 | 2021-05-24 20:35:26 +0100 | [diff] [blame] | 90 | isb(); /* Ensure counters have been stopped */ |
| 91 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 92 | write_amu_grp0_ctx_reg(ctx, 0, read64_amevcntr00()); |
| 93 | write_amu_grp0_ctx_reg(ctx, 1, read64_amevcntr01()); |
| 94 | write_amu_grp0_ctx_reg(ctx, 2, read64_amevcntr02()); |
| 95 | write_amu_grp0_ctx_reg(ctx, 3, read64_amevcntr03()); |
| 96 | |
| 97 | if (is_feat_amu_aux_supported()) { |
| 98 | uint8_t num_counters = read_amcgcr_cg1nc(); |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 99 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 100 | switch (num_counters) { |
| 101 | case 0x10: |
| 102 | write_amu_grp1_ctx_reg(ctx, 0xf, read64_amevcntr1f()); |
| 103 | __fallthrough; |
| 104 | case 0x0f: |
| 105 | write_amu_grp1_ctx_reg(ctx, 0xe, read64_amevcntr1e()); |
| 106 | __fallthrough; |
| 107 | case 0x0e: |
| 108 | write_amu_grp1_ctx_reg(ctx, 0xd, read64_amevcntr1d()); |
| 109 | __fallthrough; |
| 110 | case 0x0d: |
| 111 | write_amu_grp1_ctx_reg(ctx, 0xc, read64_amevcntr1c()); |
| 112 | __fallthrough; |
| 113 | case 0x0c: |
| 114 | write_amu_grp1_ctx_reg(ctx, 0xb, read64_amevcntr1b()); |
| 115 | __fallthrough; |
| 116 | case 0x0b: |
| 117 | write_amu_grp1_ctx_reg(ctx, 0xa, read64_amevcntr1a()); |
| 118 | __fallthrough; |
| 119 | case 0x0a: |
| 120 | write_amu_grp1_ctx_reg(ctx, 0x9, read64_amevcntr19()); |
| 121 | __fallthrough; |
| 122 | case 0x09: |
| 123 | write_amu_grp1_ctx_reg(ctx, 0x8, read64_amevcntr18()); |
| 124 | __fallthrough; |
| 125 | case 0x08: |
| 126 | write_amu_grp1_ctx_reg(ctx, 0x7, read64_amevcntr17()); |
| 127 | __fallthrough; |
| 128 | case 0x07: |
| 129 | write_amu_grp1_ctx_reg(ctx, 0x6, read64_amevcntr16()); |
| 130 | __fallthrough; |
| 131 | case 0x06: |
| 132 | write_amu_grp1_ctx_reg(ctx, 0x5, read64_amevcntr15()); |
| 133 | __fallthrough; |
| 134 | case 0x05: |
| 135 | write_amu_grp1_ctx_reg(ctx, 0x4, read64_amevcntr14()); |
| 136 | __fallthrough; |
| 137 | case 0x04: |
| 138 | write_amu_grp1_ctx_reg(ctx, 0x3, read64_amevcntr13()); |
| 139 | __fallthrough; |
| 140 | case 0x03: |
| 141 | write_amu_grp1_ctx_reg(ctx, 0x2, read64_amevcntr12()); |
| 142 | __fallthrough; |
| 143 | case 0x02: |
| 144 | write_amu_grp1_ctx_reg(ctx, 0x1, read64_amevcntr11()); |
| 145 | __fallthrough; |
| 146 | case 0x01: |
| 147 | write_amu_grp1_ctx_reg(ctx, 0x0, read64_amevcntr10()); |
| 148 | __fallthrough; |
| 149 | case 0x00: |
| 150 | break; |
| 151 | default: |
| 152 | assert(0); /* something is wrong */ |
| 153 | } |
Alexei Fedorov | 7e6306b | 2020-07-14 08:17:56 +0100 | [diff] [blame] | 154 | } |
Chris Kay | 925fda4 | 2021-05-25 10:42:56 +0100 | [diff] [blame] | 155 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 156 | return (void *)0; |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void *amu_context_restore(const void *arg) |
| 160 | { |
Andre Przywara | 906776e | 2023-03-03 10:30:06 +0000 | [diff] [blame] | 161 | if (!is_feat_amu_supported()) { |
Chris Kay | 26a7961 | 2021-05-24 20:35:26 +0100 | [diff] [blame] | 162 | return (void *)0; |
Alexei Fedorov | 7e6306b | 2020-07-14 08:17:56 +0100 | [diff] [blame] | 163 | } |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 164 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 165 | unsigned int core_pos = *(unsigned int *)arg; |
| 166 | amu_regs_t *ctx = &amu_ctx[core_pos]; |
Chris Kay | 26a7961 | 2021-05-24 20:35:26 +0100 | [diff] [blame] | 167 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 168 | write64_amevcntr00(read_amu_grp0_ctx_reg(ctx, 0)); |
| 169 | write64_amevcntr01(read_amu_grp0_ctx_reg(ctx, 1)); |
| 170 | write64_amevcntr02(read_amu_grp0_ctx_reg(ctx, 2)); |
| 171 | write64_amevcntr03(read_amu_grp0_ctx_reg(ctx, 3)); |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 172 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 173 | if (is_feat_amu_aux_supported()) { |
| 174 | uint8_t num_counters = read_amcgcr_cg1nc(); |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 175 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 176 | switch (num_counters) { |
| 177 | case 0x10: |
| 178 | write64_amevcntr1f(read_amu_grp1_ctx_reg(ctx, 0xf)); |
| 179 | __fallthrough; |
| 180 | case 0x0f: |
| 181 | write64_amevcntr1e(read_amu_grp1_ctx_reg(ctx, 0xe)); |
| 182 | __fallthrough; |
| 183 | case 0x0e: |
| 184 | write64_amevcntr1d(read_amu_grp1_ctx_reg(ctx, 0xd)); |
| 185 | __fallthrough; |
| 186 | case 0x0d: |
| 187 | write64_amevcntr1c(read_amu_grp1_ctx_reg(ctx, 0xc)); |
| 188 | __fallthrough; |
| 189 | case 0x0c: |
| 190 | write64_amevcntr1b(read_amu_grp1_ctx_reg(ctx, 0xb)); |
| 191 | __fallthrough; |
| 192 | case 0x0b: |
| 193 | write64_amevcntr1a(read_amu_grp1_ctx_reg(ctx, 0xa)); |
| 194 | __fallthrough; |
| 195 | case 0x0a: |
| 196 | write64_amevcntr19(read_amu_grp1_ctx_reg(ctx, 0x9)); |
| 197 | __fallthrough; |
| 198 | case 0x09: |
| 199 | write64_amevcntr18(read_amu_grp1_ctx_reg(ctx, 0x8)); |
| 200 | __fallthrough; |
| 201 | case 0x08: |
| 202 | write64_amevcntr17(read_amu_grp1_ctx_reg(ctx, 0x7)); |
| 203 | __fallthrough; |
| 204 | case 0x07: |
| 205 | write64_amevcntr16(read_amu_grp1_ctx_reg(ctx, 0x6)); |
| 206 | __fallthrough; |
| 207 | case 0x06: |
| 208 | write64_amevcntr15(read_amu_grp1_ctx_reg(ctx, 0x5)); |
| 209 | __fallthrough; |
| 210 | case 0x05: |
| 211 | write64_amevcntr14(read_amu_grp1_ctx_reg(ctx, 0x4)); |
| 212 | __fallthrough; |
| 213 | case 0x04: |
| 214 | write64_amevcntr13(read_amu_grp1_ctx_reg(ctx, 0x3)); |
| 215 | __fallthrough; |
| 216 | case 0x03: |
| 217 | write64_amevcntr12(read_amu_grp1_ctx_reg(ctx, 0x2)); |
| 218 | __fallthrough; |
| 219 | case 0x02: |
| 220 | write64_amevcntr11(read_amu_grp1_ctx_reg(ctx, 0x1)); |
| 221 | __fallthrough; |
| 222 | case 0x01: |
| 223 | write64_amevcntr10(read_amu_grp1_ctx_reg(ctx, 0x0)); |
| 224 | __fallthrough; |
| 225 | case 0x00: |
| 226 | break; |
| 227 | default: |
| 228 | assert(0); /* something is wrong */ |
| 229 | } |
Chris Kay | 26a7961 | 2021-05-24 20:35:26 +0100 | [diff] [blame] | 230 | } |
Alexei Fedorov | 7e6306b | 2020-07-14 08:17:56 +0100 | [diff] [blame] | 231 | |
Chris Kay | 26a7961 | 2021-05-24 20:35:26 +0100 | [diff] [blame] | 232 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 233 | /* now enable them again */ |
| 234 | write_amcntenset0(AMCNTENSET0_Pn_MASK); |
| 235 | if (is_feat_amu_aux_supported()) { |
| 236 | write_amcntenset1(get_amu_aux_enables(core_pos)); |
Chris Kay | 925fda4 | 2021-05-25 10:42:56 +0100 | [diff] [blame] | 237 | } |
Alexei Fedorov | 7e6306b | 2020-07-14 08:17:56 +0100 | [diff] [blame] | 238 | |
Boyan Karatotev | b295347 | 2024-11-06 14:55:35 +0000 | [diff] [blame] | 239 | isb(); |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 240 | return (void *)0; |
Dimitris Papastamos | dda48b0 | 2017-10-17 14:03:14 +0100 | [diff] [blame] | 241 | } |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 242 | |
| 243 | SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_start, amu_context_save); |
| 244 | SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_finish, amu_context_restore); |