Dimitris Papastamos | e08005a | 2017-10-12 13:02:29 +0100 | [diff] [blame] | 1 | /* |
Dimitris Papastamos | 7c4a6e6 | 2018-01-15 14:52:57 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. |
Dimitris Papastamos | e08005a | 2017-10-12 13:02:29 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 8 | #include <stdbool.h> |
Dimitris Papastamos | e08005a | 2017-10-12 13:02:29 +0100 | [diff] [blame] | 9 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <arch.h> |
| 11 | #include <arch_helpers.h> |
| 12 | #include <lib/el3_runtime/pubsub_events.h> |
| 13 | #include <lib/extensions/amu.h> |
| 14 | #include <lib/extensions/amu_private.h> |
| 15 | #include <plat/common/platform.h> |
| 16 | |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 17 | #define AMU_GROUP0_NR_COUNTERS 4 |
| 18 | |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 19 | struct amu_ctx { |
| 20 | uint64_t group0_cnts[AMU_GROUP0_NR_COUNTERS]; |
| 21 | uint64_t group1_cnts[AMU_GROUP1_NR_COUNTERS]; |
| 22 | }; |
| 23 | |
| 24 | static struct amu_ctx amu_ctxs[PLATFORM_CORE_COUNT]; |
| 25 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 26 | bool amu_supported(void) |
Dimitris Papastamos | e08005a | 2017-10-12 13:02:29 +0100 | [diff] [blame] | 27 | { |
| 28 | uint64_t features; |
| 29 | |
| 30 | features = read_id_aa64pfr0_el1() >> ID_AA64PFR0_AMU_SHIFT; |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 31 | return (features & ID_AA64PFR0_AMU_MASK) == 1U; |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* |
| 35 | * Enable counters. This function is meant to be invoked |
| 36 | * by the context management library before exiting from EL3. |
| 37 | */ |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 38 | void amu_enable(bool el2_unused) |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 39 | { |
| 40 | uint64_t v; |
Dimitris Papastamos | e08005a | 2017-10-12 13:02:29 +0100 | [diff] [blame] | 41 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 42 | if (!amu_supported()) |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 43 | return; |
Dimitris Papastamos | e08005a | 2017-10-12 13:02:29 +0100 | [diff] [blame] | 44 | |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 45 | if (el2_unused) { |
Dimitris Papastamos | e08005a | 2017-10-12 13:02:29 +0100 | [diff] [blame] | 46 | /* |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 47 | * CPTR_EL2.TAM: Set to zero so any accesses to |
| 48 | * the Activity Monitor registers do not trap to EL2. |
Dimitris Papastamos | e08005a | 2017-10-12 13:02:29 +0100 | [diff] [blame] | 49 | */ |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 50 | v = read_cptr_el2(); |
| 51 | v &= ~CPTR_EL2_TAM_BIT; |
| 52 | write_cptr_el2(v); |
Dimitris Papastamos | e08005a | 2017-10-12 13:02:29 +0100 | [diff] [blame] | 53 | } |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * CPTR_EL3.TAM: Set to zero so that any accesses to |
| 57 | * the Activity Monitor registers do not trap to EL3. |
| 58 | */ |
| 59 | v = read_cptr_el3(); |
| 60 | v &= ~TAM_BIT; |
| 61 | write_cptr_el3(v); |
| 62 | |
| 63 | /* Enable group 0 counters */ |
| 64 | write_amcntenset0_el0(AMU_GROUP0_COUNTERS_MASK); |
| 65 | /* Enable group 1 counters */ |
| 66 | write_amcntenset1_el0(AMU_GROUP1_COUNTERS_MASK); |
| 67 | } |
| 68 | |
| 69 | /* Read the group 0 counter identified by the given `idx`. */ |
| 70 | uint64_t amu_group0_cnt_read(int idx) |
| 71 | { |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 72 | assert(amu_supported()); |
| 73 | assert((idx >= 0) && (idx < AMU_GROUP0_NR_COUNTERS)); |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 74 | |
| 75 | return amu_group0_cnt_read_internal(idx); |
| 76 | } |
| 77 | |
| 78 | /* Write the group 0 counter identified by the given `idx` with `val`. */ |
| 79 | void amu_group0_cnt_write(int idx, uint64_t val) |
| 80 | { |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 81 | assert(amu_supported()); |
| 82 | assert((idx >= 0) && (idx < AMU_GROUP0_NR_COUNTERS)); |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 83 | |
| 84 | amu_group0_cnt_write_internal(idx, val); |
| 85 | isb(); |
| 86 | } |
| 87 | |
| 88 | /* Read the group 1 counter identified by the given `idx`. */ |
| 89 | uint64_t amu_group1_cnt_read(int idx) |
| 90 | { |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 91 | assert(amu_supported()); |
| 92 | assert((idx >= 0) && (idx < AMU_GROUP1_NR_COUNTERS)); |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 93 | |
| 94 | return amu_group1_cnt_read_internal(idx); |
| 95 | } |
| 96 | |
| 97 | /* Write the group 1 counter identified by the given `idx` with `val`. */ |
| 98 | void amu_group1_cnt_write(int idx, uint64_t val) |
| 99 | { |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 100 | assert(amu_supported()); |
| 101 | assert((idx >= 0) && (idx < AMU_GROUP1_NR_COUNTERS)); |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 102 | |
| 103 | amu_group1_cnt_write_internal(idx, val); |
| 104 | isb(); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Program the event type register for the given `idx` with |
| 109 | * the event number `val`. |
| 110 | */ |
| 111 | void amu_group1_set_evtype(int idx, unsigned int val) |
| 112 | { |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 113 | assert(amu_supported()); |
| 114 | assert((idx >= 0) && (idx < AMU_GROUP1_NR_COUNTERS)); |
Dimitris Papastamos | 525c37a | 2017-11-13 09:49:45 +0000 | [diff] [blame] | 115 | |
| 116 | amu_group1_set_evtype_internal(idx, val); |
| 117 | isb(); |
Dimitris Papastamos | e08005a | 2017-10-12 13:02:29 +0100 | [diff] [blame] | 118 | } |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 119 | |
| 120 | static void *amu_context_save(const void *arg) |
| 121 | { |
| 122 | struct amu_ctx *ctx = &amu_ctxs[plat_my_core_pos()]; |
| 123 | int i; |
| 124 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 125 | if (!amu_supported()) |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 126 | return (void *)-1; |
| 127 | |
| 128 | /* Assert that group 0/1 counter configuration is what we expect */ |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 129 | assert((read_amcntenset0_el0() == AMU_GROUP0_COUNTERS_MASK) && |
| 130 | (read_amcntenset1_el0() == AMU_GROUP1_COUNTERS_MASK)); |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 131 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 132 | assert(((sizeof(int) * 8) - __builtin_clz(AMU_GROUP1_COUNTERS_MASK)) |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 133 | <= AMU_GROUP1_NR_COUNTERS); |
| 134 | |
| 135 | /* |
| 136 | * Disable group 0/1 counters to avoid other observers like SCP sampling |
| 137 | * counter values from the future via the memory mapped view. |
| 138 | */ |
| 139 | write_amcntenclr0_el0(AMU_GROUP0_COUNTERS_MASK); |
| 140 | write_amcntenclr1_el0(AMU_GROUP1_COUNTERS_MASK); |
| 141 | isb(); |
| 142 | |
| 143 | /* Save group 0 counters */ |
| 144 | for (i = 0; i < AMU_GROUP0_NR_COUNTERS; i++) |
| 145 | ctx->group0_cnts[i] = amu_group0_cnt_read(i); |
| 146 | |
| 147 | /* Save group 1 counters */ |
| 148 | for (i = 0; i < AMU_GROUP1_NR_COUNTERS; i++) |
| 149 | ctx->group1_cnts[i] = amu_group1_cnt_read(i); |
| 150 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 151 | return (void *)0; |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static void *amu_context_restore(const void *arg) |
| 155 | { |
| 156 | struct amu_ctx *ctx = &amu_ctxs[plat_my_core_pos()]; |
| 157 | int i; |
| 158 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 159 | if (!amu_supported()) |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 160 | return (void *)-1; |
| 161 | |
| 162 | /* Counters were disabled in `amu_context_save()` */ |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 163 | assert((read_amcntenset0_el0() == 0U) && (read_amcntenset1_el0() == 0U)); |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 164 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 165 | assert(((sizeof(int) * 8U) - __builtin_clz(AMU_GROUP1_COUNTERS_MASK)) |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 166 | <= AMU_GROUP1_NR_COUNTERS); |
| 167 | |
| 168 | /* Restore group 0 counters */ |
| 169 | for (i = 0; i < AMU_GROUP0_NR_COUNTERS; i++) |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 170 | if ((AMU_GROUP0_COUNTERS_MASK & (1U << i)) != 0U) |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 171 | amu_group0_cnt_write(i, ctx->group0_cnts[i]); |
| 172 | |
| 173 | /* Restore group 1 counters */ |
| 174 | for (i = 0; i < AMU_GROUP1_NR_COUNTERS; i++) |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 175 | if ((AMU_GROUP1_COUNTERS_MASK & (1U << i)) != 0U) |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 176 | amu_group1_cnt_write(i, ctx->group1_cnts[i]); |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 177 | |
| 178 | /* Restore group 0/1 counter configuration */ |
| 179 | write_amcntenset0_el0(AMU_GROUP0_COUNTERS_MASK); |
| 180 | write_amcntenset1_el0(AMU_GROUP1_COUNTERS_MASK); |
| 181 | |
Antonio Nino Diaz | 033b4bb | 2018-10-25 16:52:26 +0100 | [diff] [blame] | 182 | return (void *)0; |
Dimitris Papastamos | eaf3e6d | 2017-11-28 13:47:06 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_start, amu_context_save); |
| 186 | SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_finish, amu_context_restore); |