blob: f4fb9809ee64199ad82b8ffd2439de45de87b2a6 [file] [log] [blame]
Soby Mathew991d42c2015-06-29 16:30:12 +01001/*
Soby Mathew3a9e8bf2015-05-05 16:33:16 +01002 * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved.
Soby Mathew991d42c2015-06-29 16:30:12 +01003 *
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>
35#include <string.h>
36#include "psci_private.h"
37
Soby Mathew6b8b3022015-06-30 11:00:24 +010038/******************************************************************************
Soby Mathew991d42c2015-06-29 16:30:12 +010039 * Top level handler which is called when a cpu wants to power itself down.
Soby Mathew3a9e8bf2015-05-05 16:33:16 +010040 * It's assumed that along with turning the cpu power domain off, power
41 * domains at higher levels will be turned off as far as possible. It finds
42 * the highest level where a domain has to be powered off by traversing the
43 * node information and then performs generic, architectural, platform setup
44 * and state management required to turn OFF that power domain and domains
45 * below it. e.g. For a cpu that's to be powered OFF, it could mean programming
46 * the power controller whereas for a cluster that's to be powered off, it will
47 * call the platform specific code which will disable coherency at the
48 * interconnect level if the cpu is the last in the cluster and also the
49 * program the power controller.
Soby Mathew991d42c2015-06-29 16:30:12 +010050 ******************************************************************************/
Soby Mathew3a9e8bf2015-05-05 16:33:16 +010051int psci_do_cpu_off(int end_pwrlvl)
Soby Mathew991d42c2015-06-29 16:30:12 +010052{
53 int rc;
Soby Mathew3a9e8bf2015-05-05 16:33:16 +010054 mpidr_pwr_map_nodes_t mpidr_nodes;
55 unsigned int max_phys_off_pwrlvl;
Soby Mathew991d42c2015-06-29 16:30:12 +010056
57 /*
58 * This function must only be called on platforms where the
59 * CPU_OFF platform hooks have been implemented.
60 */
Soby Mathew3a9e8bf2015-05-05 16:33:16 +010061 assert(psci_plat_pm_ops->pwr_domain_off);
Soby Mathew991d42c2015-06-29 16:30:12 +010062
63 /*
64 * Collect the pointers to the nodes in the topology tree for
Soby Mathew3a9e8bf2015-05-05 16:33:16 +010065 * each power domain instance in the mpidr. If this function does
66 * not return successfully then either the mpidr or the power
Soby Mathew991d42c2015-06-29 16:30:12 +010067 * levels are incorrect. Either way, this an internal TF error
68 * therefore assert.
69 */
Soby Mathew3a9e8bf2015-05-05 16:33:16 +010070 rc = psci_get_pwr_map_nodes(read_mpidr_el1() & MPIDR_AFFINITY_MASK,
Soby Mathew6b8b3022015-06-30 11:00:24 +010071 MPIDR_AFFLVL0,
Soby Mathew3a9e8bf2015-05-05 16:33:16 +010072 end_pwrlvl,
Soby Mathew991d42c2015-06-29 16:30:12 +010073 mpidr_nodes);
74 assert(rc == PSCI_E_SUCCESS);
75
76 /*
Soby Mathew3a9e8bf2015-05-05 16:33:16 +010077 * This function acquires the lock corresponding to each power
Soby Mathew991d42c2015-06-29 16:30:12 +010078 * level so that by the time all locks are taken, the system topology
79 * is snapshot and state management can be done safely.
80 */
Soby Mathew3a9e8bf2015-05-05 16:33:16 +010081 psci_acquire_pwr_domain_locks(MPIDR_AFFLVL0,
82 end_pwrlvl,
Soby Mathew991d42c2015-06-29 16:30:12 +010083 mpidr_nodes);
84
85
86 /*
87 * Call the cpu off handler registered by the Secure Payload Dispatcher
88 * to let it do any bookkeeping. Assume that the SPD always reports an
89 * E_DENIED error if SP refuse to power down
90 */
91 if (psci_spd_pm && psci_spd_pm->svc_off) {
92 rc = psci_spd_pm->svc_off(0);
93 if (rc)
94 goto exit;
95 }
96
97 /*
Soby Mathew3a9e8bf2015-05-05 16:33:16 +010098 * This function updates the state of each power domain instance
99 * corresponding to the mpidr in the range of power levels
Soby Mathew991d42c2015-06-29 16:30:12 +0100100 * specified.
101 */
Soby Mathew3a9e8bf2015-05-05 16:33:16 +0100102 psci_do_state_coordination(MPIDR_AFFLVL0,
103 end_pwrlvl,
Soby Mathew991d42c2015-06-29 16:30:12 +0100104 mpidr_nodes,
105 PSCI_STATE_OFF);
106
Soby Mathew3a9e8bf2015-05-05 16:33:16 +0100107 max_phys_off_pwrlvl = psci_find_max_phys_off_pwrlvl(MPIDR_AFFLVL0,
108 end_pwrlvl,
Soby Mathew991d42c2015-06-29 16:30:12 +0100109 mpidr_nodes);
Soby Mathew3a9e8bf2015-05-05 16:33:16 +0100110 assert(max_phys_off_pwrlvl != PSCI_INVALID_DATA);
Soby Mathew991d42c2015-06-29 16:30:12 +0100111
Soby Mathew6b8b3022015-06-30 11:00:24 +0100112 /*
113 * Arch. management. Perform the necessary steps to flush all
114 * cpu caches.
115 */
Soby Mathew3a9e8bf2015-05-05 16:33:16 +0100116 psci_do_pwrdown_cache_maintenance(max_phys_off_pwrlvl);
Soby Mathew991d42c2015-06-29 16:30:12 +0100117
118 /*
Soby Mathew6b8b3022015-06-30 11:00:24 +0100119 * Plat. management: Perform platform specific actions to turn this
120 * cpu off e.g. exit cpu coherency, program the power controller etc.
Soby Mathew991d42c2015-06-29 16:30:12 +0100121 */
Soby Mathew3a9e8bf2015-05-05 16:33:16 +0100122 psci_plat_pm_ops->pwr_domain_off(max_phys_off_pwrlvl);
Soby Mathew991d42c2015-06-29 16:30:12 +0100123
124exit:
125 /*
Soby Mathew3a9e8bf2015-05-05 16:33:16 +0100126 * Release the locks corresponding to each power level in the
Soby Mathew991d42c2015-06-29 16:30:12 +0100127 * reverse order to which they were acquired.
128 */
Soby Mathew3a9e8bf2015-05-05 16:33:16 +0100129 psci_release_pwr_domain_locks(MPIDR_AFFLVL0,
130 end_pwrlvl,
Soby Mathew991d42c2015-06-29 16:30:12 +0100131 mpidr_nodes);
132
133 /*
134 * Check if all actions needed to safely power down this cpu have
135 * successfully completed. Enter a wfi loop which will allow the
136 * power controller to physically power down this cpu.
137 */
138 if (rc == PSCI_E_SUCCESS)
139 psci_power_down_wfi();
140
141 return rc;
142}