Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 1 | /* |
Soby Mathew | 3a9e8bf | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 2 | * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +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 | |
| 31 | #include <arch.h> |
| 32 | #include <arch_helpers.h> |
| 33 | #include <assert.h> |
| 34 | #include <debug.h> |
Soby Mathew | 9d754f6 | 2015-04-08 17:42:06 +0100 | [diff] [blame] | 35 | #include <platform.h> |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 36 | #include <string.h> |
| 37 | #include "psci_private.h" |
| 38 | |
Soby Mathew | 6b8b302 | 2015-06-30 11:00:24 +0100 | [diff] [blame] | 39 | /****************************************************************************** |
Soby Mathew | 85dbf5a | 2015-04-07 12:16:56 +0100 | [diff] [blame] | 40 | * Construct the psci_power_state to request power OFF at all power levels. |
| 41 | ******************************************************************************/ |
| 42 | static void psci_set_power_off_state(psci_power_state_t *state_info) |
| 43 | { |
| 44 | int lvl; |
| 45 | |
| 46 | for (lvl = PSCI_CPU_PWR_LVL; lvl <= PLAT_MAX_PWR_LVL; lvl++) |
| 47 | state_info->pwr_domain_state[lvl] = PLAT_MAX_OFF_STATE; |
| 48 | } |
| 49 | |
| 50 | /****************************************************************************** |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 51 | * Top level handler which is called when a cpu wants to power itself down. |
Soby Mathew | 3a9e8bf | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 52 | * It's assumed that along with turning the cpu power domain off, power |
| 53 | * domains at higher levels will be turned off as far as possible. It finds |
| 54 | * the highest level where a domain has to be powered off by traversing the |
| 55 | * node information and then performs generic, architectural, platform setup |
| 56 | * and state management required to turn OFF that power domain and domains |
| 57 | * below it. e.g. For a cpu that's to be powered OFF, it could mean programming |
| 58 | * the power controller whereas for a cluster that's to be powered off, it will |
| 59 | * call the platform specific code which will disable coherency at the |
| 60 | * interconnect level if the cpu is the last in the cluster and also the |
| 61 | * program the power controller. |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 62 | ******************************************************************************/ |
Soby Mathew | 011ca18 | 2015-07-29 17:05:03 +0100 | [diff] [blame] | 63 | int psci_do_cpu_off(unsigned int end_pwrlvl) |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 64 | { |
Soby Mathew | d50e7d9 | 2015-10-01 16:46:06 +0100 | [diff] [blame] | 65 | int rc = PSCI_E_SUCCESS, idx = plat_my_core_pos(); |
Soby Mathew | 85dbf5a | 2015-04-07 12:16:56 +0100 | [diff] [blame] | 66 | psci_power_state_t state_info; |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * This function must only be called on platforms where the |
| 70 | * CPU_OFF platform hooks have been implemented. |
| 71 | */ |
Soby Mathew | 3a9e8bf | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 72 | assert(psci_plat_pm_ops->pwr_domain_off); |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 73 | |
| 74 | /* |
Soby Mathew | 3a9e8bf | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 75 | * This function acquires the lock corresponding to each power |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 76 | * level so that by the time all locks are taken, the system topology |
| 77 | * is snapshot and state management can be done safely. |
| 78 | */ |
Soby Mathew | 9d754f6 | 2015-04-08 17:42:06 +0100 | [diff] [blame] | 79 | psci_acquire_pwr_domain_locks(end_pwrlvl, |
| 80 | idx); |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * Call the cpu off handler registered by the Secure Payload Dispatcher |
| 84 | * to let it do any bookkeeping. Assume that the SPD always reports an |
| 85 | * E_DENIED error if SP refuse to power down |
| 86 | */ |
| 87 | if (psci_spd_pm && psci_spd_pm->svc_off) { |
| 88 | rc = psci_spd_pm->svc_off(0); |
| 89 | if (rc) |
| 90 | goto exit; |
| 91 | } |
| 92 | |
Soby Mathew | 85dbf5a | 2015-04-07 12:16:56 +0100 | [diff] [blame] | 93 | /* Construct the psci_power_state for CPU_OFF */ |
| 94 | psci_set_power_off_state(&state_info); |
| 95 | |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 96 | /* |
Soby Mathew | 85dbf5a | 2015-04-07 12:16:56 +0100 | [diff] [blame] | 97 | * This function is passed the requested state info and |
| 98 | * it returns the negotiated state info for each power level upto |
| 99 | * the end level specified. |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 100 | */ |
Soby Mathew | 85dbf5a | 2015-04-07 12:16:56 +0100 | [diff] [blame] | 101 | psci_do_state_coordination(end_pwrlvl, &state_info); |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 102 | |
Soby Mathew | 6b8b302 | 2015-06-30 11:00:24 +0100 | [diff] [blame] | 103 | /* |
| 104 | * Arch. management. Perform the necessary steps to flush all |
| 105 | * cpu caches. |
| 106 | */ |
Soby Mathew | 85dbf5a | 2015-04-07 12:16:56 +0100 | [diff] [blame] | 107 | psci_do_pwrdown_cache_maintenance(psci_find_max_off_lvl(&state_info)); |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 108 | |
| 109 | /* |
Soby Mathew | 6b8b302 | 2015-06-30 11:00:24 +0100 | [diff] [blame] | 110 | * Plat. management: Perform platform specific actions to turn this |
| 111 | * cpu off e.g. exit cpu coherency, program the power controller etc. |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 112 | */ |
Soby Mathew | 85dbf5a | 2015-04-07 12:16:56 +0100 | [diff] [blame] | 113 | psci_plat_pm_ops->pwr_domain_off(&state_info); |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 114 | |
| 115 | exit: |
| 116 | /* |
Soby Mathew | 3a9e8bf | 2015-05-05 16:33:16 +0100 | [diff] [blame] | 117 | * Release the locks corresponding to each power level in the |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 118 | * reverse order to which they were acquired. |
| 119 | */ |
Soby Mathew | 9d754f6 | 2015-04-08 17:42:06 +0100 | [diff] [blame] | 120 | psci_release_pwr_domain_locks(end_pwrlvl, |
| 121 | idx); |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 122 | |
| 123 | /* |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 124 | * Check if all actions needed to safely power down this cpu have |
Soby Mathew | d50e7d9 | 2015-10-01 16:46:06 +0100 | [diff] [blame] | 125 | * successfully completed. |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 126 | */ |
Soby Mathew | d50e7d9 | 2015-10-01 16:46:06 +0100 | [diff] [blame] | 127 | if (rc == PSCI_E_SUCCESS) { |
| 128 | /* |
| 129 | * Set the affinity info state to OFF. This writes directly to |
| 130 | * main memory as caches are disabled, so cache maintenance is |
| 131 | * required to ensure that later cached reads of aff_info_state |
Soby Mathew | ca37050 | 2016-01-26 11:47:53 +0000 | [diff] [blame] | 132 | * return AFF_STATE_OFF. A dsbish() ensures ordering of the |
| 133 | * update to the affinity info state prior to cache line |
| 134 | * invalidation. |
Soby Mathew | d50e7d9 | 2015-10-01 16:46:06 +0100 | [diff] [blame] | 135 | */ |
| 136 | flush_cpu_data(psci_svc_cpu_data.aff_info_state); |
| 137 | psci_set_aff_info_state(AFF_STATE_OFF); |
Soby Mathew | ca37050 | 2016-01-26 11:47:53 +0000 | [diff] [blame] | 138 | dsbish(); |
Soby Mathew | d50e7d9 | 2015-10-01 16:46:06 +0100 | [diff] [blame] | 139 | inv_cpu_data(psci_svc_cpu_data.aff_info_state); |
| 140 | |
| 141 | /* |
| 142 | * Enter a wfi loop which will allow the power controller to |
| 143 | * physically power down this cpu. |
| 144 | */ |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 145 | psci_power_down_wfi(); |
Soby Mathew | d50e7d9 | 2015-10-01 16:46:06 +0100 | [diff] [blame] | 146 | } |
Soby Mathew | 991d42c | 2015-06-29 16:30:12 +0100 | [diff] [blame] | 147 | |
| 148 | return rc; |
| 149 | } |