Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
Dan Handley | e83b0ca | 2014-01-14 18:17:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 31 | #include <assert.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 32 | #include <bl_common.h> |
| 33 | #include <arch.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 34 | #include <arch_helpers.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 35 | #include <context.h> |
Achin Gupta | ef7a28c | 2014-02-01 08:59:56 +0000 | [diff] [blame] | 36 | #include <context_mgmt.h> |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 37 | #include <cpu_data.h> |
Soby Mathew | 2ed46e9 | 2014-07-04 16:02:26 +0100 | [diff] [blame] | 38 | #include <platform.h> |
Dan Handley | bcd60ba | 2014-04-17 18:53:42 +0100 | [diff] [blame] | 39 | #include <runtime_svc.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 40 | #include <stddef.h> |
Dan Handley | 714a0d2 | 2014-04-09 13:13:04 +0100 | [diff] [blame] | 41 | #include "psci_private.h" |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 42 | |
Soby Mathew | ffb4ab1 | 2014-09-26 15:08:52 +0100 | [diff] [blame^] | 43 | typedef int (*afflvl_suspend_handler_t)(aff_map_node_t *node, |
| 44 | unsigned long ns_entrypoint, |
| 45 | unsigned long context_id, |
| 46 | unsigned int power_state); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 47 | |
| 48 | /******************************************************************************* |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 49 | * This function saves the power state parameter passed in the current PSCI |
| 50 | * cpu_suspend call in the per-cpu data array. |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 51 | ******************************************************************************/ |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 52 | void psci_set_suspend_power_state(unsigned int power_state) |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 53 | { |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 54 | set_cpu_data(psci_svc_cpu_data.power_state, power_state); |
| 55 | flush_cpu_data(psci_svc_cpu_data.power_state); |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /******************************************************************************* |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 59 | * This function gets the affinity level till which the current cpu could be |
| 60 | * powered down during a cpu_suspend call. Returns PSCI_INVALID_DATA if the |
| 61 | * power state is invalid. |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 62 | ******************************************************************************/ |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 63 | int psci_get_suspend_afflvl() |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 64 | { |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 65 | unsigned int power_state; |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 66 | |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 67 | power_state = get_cpu_data(psci_svc_cpu_data.power_state); |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 68 | |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 69 | return ((power_state == PSCI_INVALID_DATA) ? |
| 70 | power_state : psci_get_pstate_afflvl(power_state)); |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 73 | /******************************************************************************* |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 74 | * This function gets the state id of the current cpu from the power state |
| 75 | * parameter saved in the per-cpu data array. Returns PSCI_INVALID_DATA if the |
| 76 | * power state saved is invalid. |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 77 | ******************************************************************************/ |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 78 | int psci_get_suspend_stateid() |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 79 | { |
| 80 | unsigned int power_state; |
| 81 | |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 82 | power_state = get_cpu_data(psci_svc_cpu_data.power_state); |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 83 | |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 84 | return ((power_state == PSCI_INVALID_DATA) ? |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 85 | power_state : psci_get_pstate_id(power_state)); |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /******************************************************************************* |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 89 | * This function gets the state id of the cpu specified by the 'mpidr' parameter |
| 90 | * from the power state parameter saved in the per-cpu data array. Returns |
| 91 | * PSCI_INVALID_DATA if the power state saved is invalid. |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 92 | ******************************************************************************/ |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 93 | int psci_get_suspend_stateid_by_mpidr(unsigned long mpidr) |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 94 | { |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 95 | unsigned int power_state; |
| 96 | |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 97 | power_state = get_cpu_data_by_mpidr(mpidr, |
| 98 | psci_svc_cpu_data.power_state); |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 99 | |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 100 | return ((power_state == PSCI_INVALID_DATA) ? |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 101 | power_state : psci_get_pstate_id(power_state)); |
Achin Gupta | a45e397 | 2013-12-05 15:10:48 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /******************************************************************************* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 105 | * The next three functions implement a handler for each supported affinity |
| 106 | * level which is called when that affinity level is about to be suspended. |
| 107 | ******************************************************************************/ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 108 | static int psci_afflvl0_suspend(aff_map_node_t *cpu_node, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 109 | unsigned long ns_entrypoint, |
| 110 | unsigned long context_id, |
| 111 | unsigned int power_state) |
| 112 | { |
Achin Gupta | e1aa516 | 2014-06-26 09:58:52 +0100 | [diff] [blame] | 113 | unsigned long psci_entrypoint; |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 114 | uint32_t ns_scr_el3 = read_scr_el3(); |
| 115 | uint32_t ns_sctlr_el1 = read_sctlr_el1(); |
| 116 | int rc; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 117 | |
| 118 | /* Sanity check to safeguard against data corruption */ |
| 119 | assert(cpu_node->level == MPIDR_AFFLVL0); |
| 120 | |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 121 | /* Save PSCI power state parameter for the core in suspend context */ |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 122 | psci_set_suspend_power_state(power_state); |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 123 | |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 124 | /* |
| 125 | * Generic management: Store the re-entry information for the non-secure |
| 126 | * world and allow the secure world to suspend itself |
| 127 | */ |
| 128 | |
| 129 | /* |
| 130 | * Call the cpu suspend handler registered by the Secure Payload |
| 131 | * Dispatcher to let it do any bookeeping. If the handler encounters an |
| 132 | * error, it's expected to assert within |
| 133 | */ |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 134 | if (psci_spd_pm && psci_spd_pm->svc_suspend) |
| 135 | psci_spd_pm->svc_suspend(power_state); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 136 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 137 | /* |
| 138 | * Generic management: Store the re-entry information for the |
| 139 | * non-secure world |
| 140 | */ |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 141 | rc = psci_save_ns_entry(read_mpidr_el1(), ns_entrypoint, context_id, |
| 142 | ns_scr_el3, ns_sctlr_el1); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 143 | if (rc != PSCI_E_SUCCESS) |
| 144 | return rc; |
| 145 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 146 | /* Set the secure world (EL3) re-entry point after BL1 */ |
| 147 | psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry; |
| 148 | |
| 149 | /* |
| 150 | * Arch. management. Perform the necessary steps to flush all |
| 151 | * cpu caches. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 152 | */ |
Achin Gupta | e1aa516 | 2014-06-26 09:58:52 +0100 | [diff] [blame] | 153 | psci_do_pwrdown_cache_maintenance(MPIDR_AFFLVL0); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 154 | |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 155 | if (!psci_plat_pm_ops->affinst_suspend) |
| 156 | return PSCI_E_SUCCESS; |
| 157 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 158 | /* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 159 | * Plat. management: Allow the platform to perform the |
| 160 | * necessary actions to turn off this cpu e.g. set the |
| 161 | * platform defined mailbox with the psci entrypoint, |
| 162 | * program the power controller etc. |
| 163 | */ |
Soby Mathew | ffb4ab1 | 2014-09-26 15:08:52 +0100 | [diff] [blame^] | 164 | return psci_plat_pm_ops->affinst_suspend(psci_entrypoint, |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 165 | cpu_node->level, |
| 166 | psci_get_phys_state(cpu_node)); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 169 | static int psci_afflvl1_suspend(aff_map_node_t *cluster_node, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 170 | unsigned long ns_entrypoint, |
| 171 | unsigned long context_id, |
| 172 | unsigned int power_state) |
| 173 | { |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 174 | unsigned int plat_state; |
| 175 | unsigned long psci_entrypoint; |
| 176 | |
| 177 | /* Sanity check the cluster level */ |
| 178 | assert(cluster_node->level == MPIDR_AFFLVL1); |
| 179 | |
| 180 | /* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 181 | * Arch. management: Flush all levels of caches to PoC if the |
Achin Gupta | f6b9e99 | 2014-07-31 11:19:11 +0100 | [diff] [blame] | 182 | * cluster is to be shutdown. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 183 | */ |
Achin Gupta | f6b9e99 | 2014-07-31 11:19:11 +0100 | [diff] [blame] | 184 | psci_do_pwrdown_cache_maintenance(MPIDR_AFFLVL1); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 185 | |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 186 | if (!psci_plat_pm_ops->affinst_suspend) |
| 187 | return PSCI_E_SUCCESS; |
| 188 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 189 | /* |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 190 | * Plat. Management. Allow the platform to do its cluster specific |
| 191 | * bookeeping e.g. turn off interconnect coherency, program the power |
| 192 | * controller etc. Sending the psci entrypoint is currently redundant |
| 193 | * beyond affinity level 0 but one never knows what a platform might |
| 194 | * do. Also it allows us to keep the platform handler prototype the |
| 195 | * same. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 196 | */ |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 197 | plat_state = psci_get_phys_state(cluster_node); |
| 198 | psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry; |
Soby Mathew | ffb4ab1 | 2014-09-26 15:08:52 +0100 | [diff] [blame^] | 199 | return psci_plat_pm_ops->affinst_suspend(psci_entrypoint, |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 200 | cluster_node->level, |
| 201 | plat_state); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 205 | static int psci_afflvl2_suspend(aff_map_node_t *system_node, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 206 | unsigned long ns_entrypoint, |
| 207 | unsigned long context_id, |
| 208 | unsigned int power_state) |
| 209 | { |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 210 | unsigned int plat_state; |
| 211 | unsigned long psci_entrypoint; |
| 212 | |
| 213 | /* Cannot go beyond this */ |
| 214 | assert(system_node->level == MPIDR_AFFLVL2); |
| 215 | |
| 216 | /* |
| 217 | * Keep the physical state of the system handy to decide what |
| 218 | * action needs to be taken |
| 219 | */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 220 | plat_state = psci_get_phys_state(system_node); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 221 | |
| 222 | /* |
Achin Gupta | f6b9e99 | 2014-07-31 11:19:11 +0100 | [diff] [blame] | 223 | * Arch. management: Flush all levels of caches to PoC if the |
| 224 | * system is to be shutdown. |
| 225 | */ |
| 226 | psci_do_pwrdown_cache_maintenance(MPIDR_AFFLVL2); |
| 227 | |
| 228 | /* |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 229 | * Plat. Management : Allow the platform to do its bookeeping |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 230 | * at this affinity level |
| 231 | */ |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 232 | if (!psci_plat_pm_ops->affinst_suspend) |
| 233 | return PSCI_E_SUCCESS; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 234 | |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 235 | /* |
| 236 | * Sending the psci entrypoint is currently redundant |
| 237 | * beyond affinity level 0 but one never knows what a |
| 238 | * platform might do. Also it allows us to keep the |
| 239 | * platform handler prototype the same. |
| 240 | */ |
| 241 | plat_state = psci_get_phys_state(system_node); |
| 242 | psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry; |
Soby Mathew | ffb4ab1 | 2014-09-26 15:08:52 +0100 | [diff] [blame^] | 243 | return psci_plat_pm_ops->affinst_suspend(psci_entrypoint, |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 244 | system_node->level, |
| 245 | plat_state); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 248 | static const afflvl_suspend_handler_t psci_afflvl_suspend_handlers[] = { |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 249 | psci_afflvl0_suspend, |
| 250 | psci_afflvl1_suspend, |
| 251 | psci_afflvl2_suspend, |
| 252 | }; |
| 253 | |
| 254 | /******************************************************************************* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 255 | * This function takes an array of pointers to affinity instance nodes in the |
| 256 | * topology tree and calls the suspend handler for the corresponding affinity |
| 257 | * levels |
| 258 | ******************************************************************************/ |
Achin Gupta | 56bcdc2 | 2014-07-28 00:15:23 +0100 | [diff] [blame] | 259 | static int psci_call_suspend_handlers(aff_map_node_t *mpidr_nodes[], |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 260 | int start_afflvl, |
| 261 | int end_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 262 | unsigned long entrypoint, |
| 263 | unsigned long context_id, |
| 264 | unsigned int power_state) |
| 265 | { |
| 266 | int rc = PSCI_E_INVALID_PARAMS, level; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 267 | aff_map_node_t *node; |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 268 | |
| 269 | for (level = start_afflvl; level <= end_afflvl; level++) { |
| 270 | node = mpidr_nodes[level]; |
| 271 | if (node == NULL) |
| 272 | continue; |
| 273 | |
| 274 | /* |
| 275 | * TODO: In case of an error should there be a way |
| 276 | * of restoring what we might have torn down at |
| 277 | * lower affinity levels. |
| 278 | */ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 279 | rc = psci_afflvl_suspend_handlers[level](node, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 280 | entrypoint, |
| 281 | context_id, |
| 282 | power_state); |
| 283 | if (rc != PSCI_E_SUCCESS) |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | return rc; |
| 288 | } |
| 289 | |
| 290 | /******************************************************************************* |
| 291 | * Top level handler which is called when a cpu wants to suspend its execution. |
| 292 | * It is assumed that along with turning the cpu off, higher affinity levels |
| 293 | * until the target affinity level will be turned off as well. It traverses |
| 294 | * through all the affinity levels performing generic, architectural, platform |
| 295 | * setup and state management e.g. for a cluster that's to be suspended, it will |
| 296 | * call the platform specific code which will disable coherency at the |
| 297 | * interconnect level if the cpu is the last in the cluster. For a cpu it could |
| 298 | * mean programming the power controller etc. |
| 299 | * |
| 300 | * The state of all the relevant affinity levels is changed prior to calling the |
| 301 | * affinity level specific handlers as their actions would depend upon the state |
| 302 | * the affinity level is about to enter. |
| 303 | * |
| 304 | * The affinity level specific handlers are called in ascending order i.e. from |
| 305 | * the lowest to the highest affinity level implemented by the platform because |
| 306 | * to turn off affinity level X it is neccesary to turn off affinity level X - 1 |
| 307 | * first. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 308 | ******************************************************************************/ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 309 | int psci_afflvl_suspend(unsigned long entrypoint, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 310 | unsigned long context_id, |
| 311 | unsigned int power_state, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 312 | int start_afflvl, |
| 313 | int end_afflvl) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 314 | { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 315 | int rc = PSCI_E_SUCCESS; |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 316 | mpidr_aff_map_nodes_t mpidr_nodes; |
Achin Gupta | f6b9e99 | 2014-07-31 11:19:11 +0100 | [diff] [blame] | 317 | unsigned int max_phys_off_afflvl; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 318 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 319 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 320 | * Collect the pointers to the nodes in the topology tree for |
| 321 | * each affinity instance in the mpidr. If this function does |
| 322 | * not return successfully then either the mpidr or the affinity |
| 323 | * levels are incorrect. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 324 | */ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 325 | rc = psci_get_aff_map_nodes(read_mpidr_el1() & MPIDR_AFFINITY_MASK, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 326 | start_afflvl, |
| 327 | end_afflvl, |
| 328 | mpidr_nodes); |
| 329 | if (rc != PSCI_E_SUCCESS) |
| 330 | return rc; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 331 | |
| 332 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 333 | * This function acquires the lock corresponding to each affinity |
| 334 | * level so that by the time all locks are taken, the system topology |
| 335 | * is snapshot and state management can be done safely. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 336 | */ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 337 | psci_acquire_afflvl_locks(start_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 338 | end_afflvl, |
| 339 | mpidr_nodes); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 340 | |
Achin Gupta | cab78e4 | 2014-07-28 00:09:01 +0100 | [diff] [blame] | 341 | /* |
| 342 | * This function updates the state of each affinity instance |
| 343 | * corresponding to the mpidr in the range of affinity levels |
| 344 | * specified. |
| 345 | */ |
| 346 | psci_do_afflvl_state_mgmt(start_afflvl, |
| 347 | end_afflvl, |
| 348 | mpidr_nodes, |
| 349 | PSCI_STATE_SUSPEND); |
Achin Gupta | f6b9e99 | 2014-07-31 11:19:11 +0100 | [diff] [blame] | 350 | |
| 351 | max_phys_off_afflvl = psci_find_max_phys_off_afflvl(start_afflvl, |
| 352 | end_afflvl, |
| 353 | mpidr_nodes); |
| 354 | assert(max_phys_off_afflvl != PSCI_INVALID_DATA); |
| 355 | |
| 356 | /* Stash the highest affinity level that will be turned off */ |
| 357 | psci_set_max_phys_off_afflvl(max_phys_off_afflvl); |
| 358 | |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 359 | /* Perform generic, architecture and platform specific handling */ |
| 360 | rc = psci_call_suspend_handlers(mpidr_nodes, |
| 361 | start_afflvl, |
| 362 | end_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 363 | entrypoint, |
| 364 | context_id, |
| 365 | power_state); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 366 | |
| 367 | /* |
Achin Gupta | f6b9e99 | 2014-07-31 11:19:11 +0100 | [diff] [blame] | 368 | * Invalidate the entry for the highest affinity level stashed earlier. |
| 369 | * This ensures that any reads of this variable outside the power |
| 370 | * up/down sequences return PSCI_INVALID_DATA. |
| 371 | */ |
| 372 | psci_set_max_phys_off_afflvl(PSCI_INVALID_DATA); |
| 373 | |
| 374 | /* |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 375 | * Release the locks corresponding to each affinity level in the |
| 376 | * reverse order to which they were acquired. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 377 | */ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 378 | psci_release_afflvl_locks(start_afflvl, |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 379 | end_afflvl, |
| 380 | mpidr_nodes); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 381 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 382 | return rc; |
| 383 | } |
| 384 | |
| 385 | /******************************************************************************* |
| 386 | * The following functions finish an earlier affinity suspend request. They |
| 387 | * are called by the common finisher routine in psci_common.c. |
| 388 | ******************************************************************************/ |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 389 | static unsigned int psci_afflvl0_suspend_finish(aff_map_node_t *cpu_node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 390 | { |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 391 | unsigned int plat_state, state, rc; |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 392 | int32_t suspend_level; |
Soby Mathew | 2ed46e9 | 2014-07-04 16:02:26 +0100 | [diff] [blame] | 393 | uint64_t counter_freq; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 394 | |
| 395 | assert(cpu_node->level == MPIDR_AFFLVL0); |
| 396 | |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 397 | /* Ensure we have been woken up from a suspended state */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 398 | state = psci_get_state(cpu_node); |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 399 | assert(state == PSCI_STATE_SUSPEND); |
| 400 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 401 | /* |
| 402 | * Plat. management: Perform the platform specific actions |
| 403 | * before we change the state of the cpu e.g. enabling the |
| 404 | * gic or zeroing the mailbox register. If anything goes |
| 405 | * wrong then assert as there is no way to recover from this |
| 406 | * situation. |
| 407 | */ |
| 408 | if (psci_plat_pm_ops->affinst_suspend_finish) { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 409 | |
| 410 | /* Get the physical state of this cpu */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 411 | plat_state = get_phys_state(state); |
Soby Mathew | ffb4ab1 | 2014-09-26 15:08:52 +0100 | [diff] [blame^] | 412 | rc = psci_plat_pm_ops->affinst_suspend_finish(cpu_node->level, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 413 | plat_state); |
| 414 | assert(rc == PSCI_E_SUCCESS); |
| 415 | } |
| 416 | |
| 417 | /* Get the index for restoring the re-entry information */ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 418 | /* |
Achin Gupta | e1aa516 | 2014-06-26 09:58:52 +0100 | [diff] [blame] | 419 | * Arch. management: Enable the data cache, manage stack memory and |
| 420 | * restore the stashed EL3 architectural context from the 'cpu_context' |
| 421 | * structure for this cpu. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 422 | */ |
Achin Gupta | e1aa516 | 2014-06-26 09:58:52 +0100 | [diff] [blame] | 423 | psci_do_pwrup_cache_maintenance(); |
Soby Mathew | 2ed46e9 | 2014-07-04 16:02:26 +0100 | [diff] [blame] | 424 | |
| 425 | /* Re-init the cntfrq_el0 register */ |
| 426 | counter_freq = plat_get_syscnt_freq(); |
| 427 | write_cntfrq_el0(counter_freq); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 428 | |
| 429 | /* |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 430 | * Call the cpu suspend finish handler registered by the Secure Payload |
| 431 | * Dispatcher to let it do any bookeeping. If the handler encounters an |
| 432 | * error, it's expected to assert within |
| 433 | */ |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 434 | if (psci_spd_pm && psci_spd_pm->svc_suspend) { |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 435 | suspend_level = psci_get_suspend_afflvl(); |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 436 | assert (suspend_level != PSCI_INVALID_DATA); |
Jeenu Viswambharan | 7f36660 | 2014-02-20 17:11:00 +0000 | [diff] [blame] | 437 | psci_spd_pm->svc_suspend_finish(suspend_level); |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 440 | /* Invalidate the suspend context for the node */ |
Achin Gupta | f3ccbab | 2014-07-25 14:52:47 +0100 | [diff] [blame] | 441 | psci_set_suspend_power_state(PSCI_INVALID_DATA); |
Vikram Kanigiri | f100f41 | 2014-04-01 19:26:26 +0100 | [diff] [blame] | 442 | |
Achin Gupta | 607084e | 2014-02-09 18:24:19 +0000 | [diff] [blame] | 443 | /* |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 444 | * Generic management: Now we just need to retrieve the |
| 445 | * information that we had stashed away during the suspend |
Achin Gupta | 3140a9e | 2013-12-02 16:23:12 +0000 | [diff] [blame] | 446 | * call to set this cpu on its way. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 447 | */ |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 448 | cm_prepare_el3_exit(NON_SECURE); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 449 | |
| 450 | /* Clean caches before re-entering normal world */ |
| 451 | dcsw_op_louis(DCCSW); |
| 452 | |
Andrew Thoelke | 4e12607 | 2014-06-04 21:10:52 +0100 | [diff] [blame] | 453 | rc = PSCI_E_SUCCESS; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 454 | return rc; |
| 455 | } |
| 456 | |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 457 | static unsigned int psci_afflvl1_suspend_finish(aff_map_node_t *cluster_node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 458 | { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 459 | unsigned int plat_state, rc = PSCI_E_SUCCESS; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 460 | |
| 461 | assert(cluster_node->level == MPIDR_AFFLVL1); |
| 462 | |
| 463 | /* |
| 464 | * Plat. management: Perform the platform specific actions |
| 465 | * as per the old state of the cluster e.g. enabling |
| 466 | * coherency at the interconnect depends upon the state with |
| 467 | * which this cluster was powered up. If anything goes wrong |
| 468 | * then assert as there is no way to recover from this |
| 469 | * situation. |
| 470 | */ |
| 471 | if (psci_plat_pm_ops->affinst_suspend_finish) { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 472 | |
| 473 | /* Get the physical state of this cpu */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 474 | plat_state = psci_get_phys_state(cluster_node); |
Soby Mathew | ffb4ab1 | 2014-09-26 15:08:52 +0100 | [diff] [blame^] | 475 | rc = psci_plat_pm_ops->affinst_suspend_finish(cluster_node->level, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 476 | plat_state); |
| 477 | assert(rc == PSCI_E_SUCCESS); |
| 478 | } |
| 479 | |
| 480 | return rc; |
| 481 | } |
| 482 | |
| 483 | |
Andrew Thoelke | 2bc0785 | 2014-06-09 12:44:21 +0100 | [diff] [blame] | 484 | static unsigned int psci_afflvl2_suspend_finish(aff_map_node_t *system_node) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 485 | { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 486 | unsigned int plat_state, rc = PSCI_E_SUCCESS;; |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 487 | |
| 488 | /* Cannot go beyond this affinity level */ |
| 489 | assert(system_node->level == MPIDR_AFFLVL2); |
| 490 | |
| 491 | /* |
| 492 | * Currently, there are no architectural actions to perform |
| 493 | * at the system level. |
| 494 | */ |
| 495 | |
| 496 | /* |
| 497 | * Plat. management: Perform the platform specific actions |
| 498 | * as per the old state of the cluster e.g. enabling |
| 499 | * coherency at the interconnect depends upon the state with |
| 500 | * which this cluster was powered up. If anything goes wrong |
| 501 | * then assert as there is no way to recover from this |
| 502 | * situation. |
| 503 | */ |
| 504 | if (psci_plat_pm_ops->affinst_suspend_finish) { |
Achin Gupta | 0959db5 | 2013-12-02 17:33:04 +0000 | [diff] [blame] | 505 | |
| 506 | /* Get the physical state of the system */ |
Achin Gupta | 75f7367 | 2013-12-05 16:33:10 +0000 | [diff] [blame] | 507 | plat_state = psci_get_phys_state(system_node); |
Soby Mathew | ffb4ab1 | 2014-09-26 15:08:52 +0100 | [diff] [blame^] | 508 | rc = psci_plat_pm_ops->affinst_suspend_finish(system_node->level, |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 509 | plat_state); |
| 510 | assert(rc == PSCI_E_SUCCESS); |
| 511 | } |
| 512 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 513 | return rc; |
| 514 | } |
| 515 | |
Dan Handley | e2712bc | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 516 | const afflvl_power_on_finisher_t psci_afflvl_suspend_finishers[] = { |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 517 | psci_afflvl0_suspend_finish, |
| 518 | psci_afflvl1_suspend_finish, |
| 519 | psci_afflvl2_suspend_finish, |
| 520 | }; |