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