1 /* 2 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <amu.h> 8 #include <amu_private.h> 9 #include <arch.h> 10 #include <arch_helpers.h> 11 #include <platform.h> 12 #include <pubsub_events.h> 13 14 #define AMU_GROUP0_NR_COUNTERS 4 15 16 struct amu_ctx { 17 uint64_t group0_cnts[AMU_GROUP0_NR_COUNTERS]; 18 uint64_t group1_cnts[AMU_GROUP1_NR_COUNTERS]; 19 }; 20 21 static struct amu_ctx amu_ctxs[PLATFORM_CORE_COUNT]; 22 23 int amu_supported(void) 24 { 25 uint64_t features; 26 27 features = read_id_pfr0() >> ID_PFR0_AMU_SHIFT; 28 return (features & ID_PFR0_AMU_MASK) == 1; 29 } 30 31 void amu_enable(int el2_unused) 32 { 33 if (!amu_supported()) 34 return; 35 36 if (el2_unused) { 37 uint64_t v; 38 /* 39 * Non-secure access from EL0 or EL1 to the Activity Monitor 40 * registers do not trap to EL2. 41 */ 42 v = read_hcptr(); 43 v &= ~TAM_BIT; 44 write_hcptr(v); 45 } 46 47 /* Enable group 0 counters */ 48 write_amcntenset0(AMU_GROUP0_COUNTERS_MASK); 49 50 /* Enable group 1 counters */ 51 write_amcntenset1(AMU_GROUP1_COUNTERS_MASK); 52 } 53 54 /* Read the group 0 counter identified by the given `idx`. */ 55 uint64_t amu_group0_cnt_read(int idx) 56 { 57 assert(amu_supported()); 58 assert(idx >= 0 && idx < AMU_GROUP0_NR_COUNTERS); 59 60 return amu_group0_cnt_read_internal(idx); 61 } 62 63 /* Write the group 0 counter identified by the given `idx` with `val`. */ 64 void amu_group0_cnt_write(int idx, uint64_t val) 65 { 66 assert(amu_supported()); 67 assert(idx >= 0 && idx < AMU_GROUP0_NR_COUNTERS); 68 69 amu_group0_cnt_write_internal(idx, val); 70 isb(); 71 } 72 73 /* Read the group 1 counter identified by the given `idx`. */ 74 uint64_t amu_group1_cnt_read(int idx) 75 { 76 assert(amu_supported()); 77 assert(idx >= 0 && idx < AMU_GROUP1_NR_COUNTERS); 78 79 return amu_group1_cnt_read_internal(idx); 80 } 81 82 /* Write the group 1 counter identified by the given `idx` with `val`. */ 83 void amu_group1_cnt_write(int idx, uint64_t val) 84 { 85 assert(amu_supported()); 86 assert(idx >= 0 && idx < AMU_GROUP1_NR_COUNTERS); 87 88 amu_group1_cnt_write_internal(idx, val); 89 isb(); 90 } 91 92 void amu_group1_set_evtype(int idx, unsigned int val) 93 { 94 assert(amu_supported()); 95 assert(idx >= 0 && idx < AMU_GROUP1_NR_COUNTERS); 96 97 amu_group1_set_evtype_internal(idx, val); 98 isb(); 99 } 100 101 static void *amu_context_save(const void *arg) 102 { 103 struct amu_ctx *ctx; 104 int i; 105 106 if (!amu_supported()) 107 return (void *)-1; 108 109 ctx = &amu_ctxs[plat_my_core_pos()]; 110 111 /* Assert that group 0 counter configuration is what we expect */ 112 assert(read_amcntenset0() == AMU_GROUP0_COUNTERS_MASK); 113 114 /* 115 * Disable group 0 counters to avoid other observers like SCP sampling 116 * counter values from the future via the memory mapped view. 117 */ 118 write_amcntenclr0(AMU_GROUP0_COUNTERS_MASK); 119 write_amcntenclr1(AMU_GROUP1_COUNTERS_MASK); 120 isb(); 121 122 for (i = 0; i < AMU_GROUP0_NR_COUNTERS; i++) 123 ctx->group0_cnts[i] = amu_group0_cnt_read(i); 124 125 for (i = 0; i < AMU_GROUP1_NR_COUNTERS; i++) 126 ctx->group1_cnts[i] = amu_group1_cnt_read(i); 127 128 return 0; 129 } 130 131 static void *amu_context_restore(const void *arg) 132 { 133 struct amu_ctx *ctx; 134 uint64_t features; 135 int i; 136 137 features = read_id_pfr0() >> ID_PFR0_AMU_SHIFT; 138 if ((features & ID_PFR0_AMU_MASK) != 1) 139 return (void *)-1; 140 141 ctx = &amu_ctxs[plat_my_core_pos()]; 142 143 /* Counters were disabled in `amu_context_save()` */ 144 assert(read_amcntenset0() == 0); 145 146 /* Restore group 0 counters */ 147 for (i = 0; i < AMU_GROUP0_NR_COUNTERS; i++) 148 amu_group0_cnt_write(i, ctx->group0_cnts[i]); 149 for (i = 0; i < AMU_GROUP1_NR_COUNTERS; i++) 150 amu_group1_cnt_write(i, ctx->group1_cnts[i]); 151 152 /* Enable group 0 counters */ 153 write_amcntenset0(AMU_GROUP0_COUNTERS_MASK); 154 155 /* Enable group 1 counters */ 156 write_amcntenset1(AMU_GROUP1_COUNTERS_MASK); 157 return 0; 158 } 159 160 SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_start, amu_context_save); 161 SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_finish, amu_context_restore); 162