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