Soby Mathew | 0d78607 | 2016-03-24 16:56:29 +0000 | [diff] [blame] | 1 | /* |
Boyan Karatotev | 02d9d83 | 2024-11-06 16:23:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2025, Arm Limited and Contributors. All rights reserved. |
Varun Wadekar | 0a176e3 | 2020-02-13 13:07:12 -0800 | [diff] [blame] | 3 | * Copyright (c) 2020, NVIDIA Corporation. All rights reserved. |
Soby Mathew | 0d78607 | 2016-03-24 16:56:29 +0000 | [diff] [blame] | 4 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 5 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | 0d78607 | 2016-03-24 16:56:29 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Soby Mathew | 0d78607 | 2016-03-24 16:56:29 +0000 | [diff] [blame] | 8 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | |
| 10 | #include <arch.h> |
| 11 | #include <lib/pmf/pmf.h> |
| 12 | #include <lib/psci/psci.h> |
Varun Wadekar | 0a176e3 | 2020-02-13 13:07:12 -0800 | [diff] [blame] | 13 | #include <lib/utils_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | #include <plat/common/platform.h> |
Soby Mathew | 0d78607 | 2016-03-24 16:56:29 +0000 | [diff] [blame] | 15 | |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 16 | #if ENABLE_PSCI_STAT && ENABLE_PMF |
| 17 | #pragma weak plat_psci_stat_accounting_start |
| 18 | #pragma weak plat_psci_stat_accounting_stop |
| 19 | #pragma weak plat_psci_stat_get_residency |
| 20 | |
Soby Mathew | 8336f68 | 2017-10-16 15:19:31 +0100 | [diff] [blame] | 21 | /* Maximum time-stamp value read from architectural counters */ |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 22 | #ifdef __aarch64__ |
Soby Mathew | 8336f68 | 2017-10-16 15:19:31 +0100 | [diff] [blame] | 23 | #define MAX_TS UINT64_MAX |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 24 | #else |
| 25 | #define MAX_TS UINT32_MAX |
Soby Mathew | 8336f68 | 2017-10-16 15:19:31 +0100 | [diff] [blame] | 26 | #endif |
| 27 | |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 28 | /* Following are used as ID's to capture time-stamp */ |
| 29 | #define PSCI_STAT_ID_ENTER_LOW_PWR 0 |
| 30 | #define PSCI_STAT_ID_EXIT_LOW_PWR 1 |
| 31 | #define PSCI_STAT_TOTAL_IDS 2 |
| 32 | |
Boyan Karatotev | 02d9d83 | 2024-11-06 16:23:07 +0000 | [diff] [blame] | 33 | #if HW_ASSISTED_COHERENCY |
| 34 | #define CACHE_MAINTENANCE_ATTR PMF_NO_CACHE_MAINT |
| 35 | #else |
| 36 | #define CACHE_MAINTENANCE_ATTR PMF_CACHE_MAINT |
| 37 | #endif |
| 38 | |
Madhukar Pappireddy | a091d04 | 2020-01-06 14:42:30 -0600 | [diff] [blame] | 39 | PMF_DECLARE_CAPTURE_TIMESTAMP(psci_svc) |
| 40 | PMF_DECLARE_GET_TIMESTAMP(psci_svc) |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 41 | PMF_REGISTER_SERVICE(psci_svc, PMF_PSCI_STAT_SVC_ID, PSCI_STAT_TOTAL_IDS, |
| 42 | PMF_STORE_ENABLE) |
| 43 | |
| 44 | /* |
| 45 | * This function calculates the stats residency in microseconds, |
| 46 | * taking in account the wrap around condition. |
| 47 | */ |
| 48 | static u_register_t calc_stat_residency(unsigned long long pwrupts, |
| 49 | unsigned long long pwrdnts) |
| 50 | { |
| 51 | /* The divisor to use to convert raw timestamp into microseconds. */ |
| 52 | u_register_t residency_div; |
| 53 | u_register_t res; |
| 54 | |
| 55 | /* |
| 56 | * Calculate divisor so that it can be directly used to |
| 57 | * convert time-stamp into microseconds. |
| 58 | */ |
| 59 | residency_div = read_cntfrq_el0() / MHZ_TICKS_PER_SEC; |
Antonio Nino Diaz | 6f3ccc5 | 2018-07-20 09:17:26 +0100 | [diff] [blame] | 60 | assert(residency_div > 0U); |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 61 | |
Maheedhar Bollapalli | 9bf4184 | 2024-04-25 10:34:02 +0530 | [diff] [blame^] | 62 | if (pwrupts < pwrdnts) { |
Soby Mathew | 8336f68 | 2017-10-16 15:19:31 +0100 | [diff] [blame] | 63 | res = MAX_TS - pwrdnts + pwrupts; |
Maheedhar Bollapalli | 9bf4184 | 2024-04-25 10:34:02 +0530 | [diff] [blame^] | 64 | } else { |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 65 | res = pwrupts - pwrdnts; |
Maheedhar Bollapalli | 9bf4184 | 2024-04-25 10:34:02 +0530 | [diff] [blame^] | 66 | } |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 67 | |
| 68 | return res / residency_div; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Capture timestamp before entering a low power state. |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 73 | * Cache maintenance may be needed when reading these timestamps. |
| 74 | */ |
| 75 | void plat_psci_stat_accounting_start( |
| 76 | __unused const psci_power_state_t *state_info) |
| 77 | { |
Antonio Nino Diaz | fec756f | 2018-07-18 16:24:16 +0100 | [diff] [blame] | 78 | assert(state_info != NULL); |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 79 | PMF_CAPTURE_TIMESTAMP(psci_svc, PSCI_STAT_ID_ENTER_LOW_PWR, |
Boyan Karatotev | 02d9d83 | 2024-11-06 16:23:07 +0000 | [diff] [blame] | 80 | CACHE_MAINTENANCE_ATTR); |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Capture timestamp after exiting a low power state. |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 85 | * Cache maintenance may be needed when reading these timestamps. |
| 86 | */ |
| 87 | void plat_psci_stat_accounting_stop( |
| 88 | __unused const psci_power_state_t *state_info) |
| 89 | { |
Antonio Nino Diaz | fec756f | 2018-07-18 16:24:16 +0100 | [diff] [blame] | 90 | assert(state_info != NULL); |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 91 | PMF_CAPTURE_TIMESTAMP(psci_svc, PSCI_STAT_ID_EXIT_LOW_PWR, |
Boyan Karatotev | 02d9d83 | 2024-11-06 16:23:07 +0000 | [diff] [blame] | 92 | CACHE_MAINTENANCE_ATTR); |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Calculate the residency for the given level and power state |
| 97 | * information. |
| 98 | */ |
| 99 | u_register_t plat_psci_stat_get_residency(unsigned int lvl, |
| 100 | const psci_power_state_t *state_info, |
Deepika Bhavnani | 4287c0c | 2019-12-13 10:23:18 -0600 | [diff] [blame] | 101 | unsigned int last_cpu_idx) |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 102 | { |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 103 | unsigned long long pwrup_ts = 0, pwrdn_ts = 0; |
| 104 | unsigned int pmf_flags; |
| 105 | |
Antonio Nino Diaz | fec756f | 2018-07-18 16:24:16 +0100 | [diff] [blame] | 106 | assert((lvl >= PSCI_CPU_PWR_LVL) && (lvl <= PLAT_MAX_PWR_LVL)); |
Antonio Nino Diaz | fec756f | 2018-07-18 16:24:16 +0100 | [diff] [blame] | 107 | assert(last_cpu_idx <= PLATFORM_CORE_COUNT); |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 108 | |
| 109 | if (lvl == PSCI_CPU_PWR_LVL) |
Deepika Bhavnani | 4287c0c | 2019-12-13 10:23:18 -0600 | [diff] [blame] | 110 | assert(last_cpu_idx == plat_my_core_pos()); |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 111 | |
Boyan Karatotev | 02d9d83 | 2024-11-06 16:23:07 +0000 | [diff] [blame] | 112 | #if HW_ASSISTED_COHERENCY |
| 113 | /* HW coherency allows for the capture and access to happen with caches |
| 114 | * ON. So these timestamps don't need cache maintenance */ |
| 115 | pmf_flags = PMF_NO_CACHE_MAINT; |
| 116 | #else |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 117 | /* |
| 118 | * If power down is requested, then timestamp capture will |
| 119 | * be with caches OFF. Hence we have to do cache maintenance |
| 120 | * when reading the timestamp. |
| 121 | */ |
Boyan Karatotev | 02d9d83 | 2024-11-06 16:23:07 +0000 | [diff] [blame] | 122 | plat_local_state_t state; |
| 123 | assert(state_info != NULL); |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 124 | state = state_info->pwr_domain_state[PSCI_CPU_PWR_LVL]; |
Antonio Nino Diaz | fec756f | 2018-07-18 16:24:16 +0100 | [diff] [blame] | 125 | if (is_local_state_off(state) != 0) { |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 126 | pmf_flags = PMF_CACHE_MAINT; |
| 127 | } else { |
Antonio Nino Diaz | fec756f | 2018-07-18 16:24:16 +0100 | [diff] [blame] | 128 | assert(is_local_state_retn(state) == 1); |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 129 | pmf_flags = PMF_NO_CACHE_MAINT; |
| 130 | } |
Boyan Karatotev | 02d9d83 | 2024-11-06 16:23:07 +0000 | [diff] [blame] | 131 | #endif |
dp-arm | 66abfbe | 2017-01-31 13:01:04 +0000 | [diff] [blame] | 132 | |
| 133 | PMF_GET_TIMESTAMP_BY_INDEX(psci_svc, |
| 134 | PSCI_STAT_ID_ENTER_LOW_PWR, |
| 135 | last_cpu_idx, |
| 136 | pmf_flags, |
| 137 | pwrdn_ts); |
| 138 | |
| 139 | PMF_GET_TIMESTAMP_BY_INDEX(psci_svc, |
| 140 | PSCI_STAT_ID_EXIT_LOW_PWR, |
| 141 | plat_my_core_pos(), |
| 142 | pmf_flags, |
| 143 | pwrup_ts); |
| 144 | |
| 145 | return calc_stat_residency(pwrup_ts, pwrdn_ts); |
| 146 | } |
| 147 | #endif /* ENABLE_PSCI_STAT && ENABLE_PMF */ |
| 148 | |
Soby Mathew | 0d78607 | 2016-03-24 16:56:29 +0000 | [diff] [blame] | 149 | /* |
| 150 | * The PSCI generic code uses this API to let the platform participate in state |
| 151 | * coordination during a power management operation. It compares the platform |
| 152 | * specific local power states requested by each cpu for a given power domain |
| 153 | * and returns the coordinated target power state that the domain should |
| 154 | * enter. A platform assigns a number to a local power state. This default |
| 155 | * implementation assumes that the platform assigns these numbers in order of |
| 156 | * increasing depth of the power state i.e. for two power states X & Y, if X < Y |
| 157 | * then X represents a shallower power state than Y. As a result, the |
| 158 | * coordinated target local power state for a power domain will be the minimum |
| 159 | * of the requested local power states. |
| 160 | */ |
| 161 | plat_local_state_t plat_get_target_pwr_state(unsigned int lvl, |
| 162 | const plat_local_state_t *states, |
| 163 | unsigned int ncpu) |
| 164 | { |
| 165 | plat_local_state_t target = PLAT_MAX_OFF_STATE, temp; |
Antonio Nino Diaz | fec756f | 2018-07-18 16:24:16 +0100 | [diff] [blame] | 166 | const plat_local_state_t *st = states; |
| 167 | unsigned int n = ncpu; |
Soby Mathew | 0d78607 | 2016-03-24 16:56:29 +0000 | [diff] [blame] | 168 | |
Antonio Nino Diaz | fec756f | 2018-07-18 16:24:16 +0100 | [diff] [blame] | 169 | assert(ncpu > 0U); |
Soby Mathew | 0d78607 | 2016-03-24 16:56:29 +0000 | [diff] [blame] | 170 | |
| 171 | do { |
Antonio Nino Diaz | fec756f | 2018-07-18 16:24:16 +0100 | [diff] [blame] | 172 | temp = *st; |
| 173 | st++; |
Maheedhar Bollapalli | 9bf4184 | 2024-04-25 10:34:02 +0530 | [diff] [blame^] | 174 | if (temp < target) { |
Soby Mathew | 0d78607 | 2016-03-24 16:56:29 +0000 | [diff] [blame] | 175 | target = temp; |
Maheedhar Bollapalli | 9bf4184 | 2024-04-25 10:34:02 +0530 | [diff] [blame^] | 176 | } |
Antonio Nino Diaz | fec756f | 2018-07-18 16:24:16 +0100 | [diff] [blame] | 177 | n--; |
| 178 | } while (n > 0U); |
Soby Mathew | 0d78607 | 2016-03-24 16:56:29 +0000 | [diff] [blame] | 179 | |
| 180 | return target; |
| 181 | } |