blob: b96682253d418deee1e7717d439c9bba1a76f93a [file] [log] [blame]
Soby Mathew991d42c2015-06-29 16:30:12 +01001/*
2 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
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>
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 Mathew6b8b3022015-06-30 11:00:24 +010040 * It's assumed that along with turning the cpu off, higher affinity levels
41 * will be turned off as far as possible. It finds the highest level to be
42 * powered off by traversing the node information and then performs generic,
43 * architectural, platform setup and state management required to turn OFF
44 * that affinity level and affinity levels below it. e.g. For a cpu that's to
45 * be powered OFF, it could mean programming the power controller whereas for
46 * a cluster that's to be powered off, it will call the platform specific code
47 * which will disable coherency at the interconnect level if the cpu is the
48 * last in the cluster and also the program the power controller.
Soby Mathew991d42c2015-06-29 16:30:12 +010049 ******************************************************************************/
Soby Mathew6b8b3022015-06-30 11:00:24 +010050int psci_afflvl_off(int end_afflvl)
Soby Mathew991d42c2015-06-29 16:30:12 +010051{
52 int rc;
53 mpidr_aff_map_nodes_t mpidr_nodes;
54 unsigned int max_phys_off_afflvl;
55
56 /*
57 * This function must only be called on platforms where the
58 * CPU_OFF platform hooks have been implemented.
59 */
60 assert(psci_plat_pm_ops->affinst_off);
61
62 /*
63 * Collect the pointers to the nodes in the topology tree for
64 * each affinity instance in the mpidr. If this function does
65 * not return successfully then either the mpidr or the affinity
66 * levels are incorrect. Either way, this an internal TF error
67 * therefore assert.
68 */
69 rc = psci_get_aff_map_nodes(read_mpidr_el1() & MPIDR_AFFINITY_MASK,
Soby Mathew6b8b3022015-06-30 11:00:24 +010070 MPIDR_AFFLVL0,
Soby Mathew991d42c2015-06-29 16:30:12 +010071 end_afflvl,
72 mpidr_nodes);
73 assert(rc == PSCI_E_SUCCESS);
74
75 /*
76 * This function acquires the lock corresponding to each affinity
77 * level so that by the time all locks are taken, the system topology
78 * is snapshot and state management can be done safely.
79 */
Soby Mathew6b8b3022015-06-30 11:00:24 +010080 psci_acquire_afflvl_locks(MPIDR_AFFLVL0,
Soby Mathew991d42c2015-06-29 16:30:12 +010081 end_afflvl,
82 mpidr_nodes);
83
84
85 /*
86 * Call the cpu off handler registered by the Secure Payload Dispatcher
87 * to let it do any bookkeeping. Assume that the SPD always reports an
88 * E_DENIED error if SP refuse to power down
89 */
90 if (psci_spd_pm && psci_spd_pm->svc_off) {
91 rc = psci_spd_pm->svc_off(0);
92 if (rc)
93 goto exit;
94 }
95
96 /*
97 * This function updates the state of each affinity instance
98 * corresponding to the mpidr in the range of affinity levels
99 * specified.
100 */
Soby Mathew6b8b3022015-06-30 11:00:24 +0100101 psci_do_afflvl_state_mgmt(MPIDR_AFFLVL0,
Soby Mathew991d42c2015-06-29 16:30:12 +0100102 end_afflvl,
103 mpidr_nodes,
104 PSCI_STATE_OFF);
105
Soby Mathew6b8b3022015-06-30 11:00:24 +0100106 max_phys_off_afflvl = psci_find_max_phys_off_afflvl(MPIDR_AFFLVL0,
Soby Mathew991d42c2015-06-29 16:30:12 +0100107 end_afflvl,
108 mpidr_nodes);
109 assert(max_phys_off_afflvl != PSCI_INVALID_DATA);
110
Soby Mathew6b8b3022015-06-30 11:00:24 +0100111 /*
112 * Arch. management. Perform the necessary steps to flush all
113 * cpu caches.
114 */
115 psci_do_pwrdown_cache_maintenance(max_phys_off_afflvl);
Soby Mathew991d42c2015-06-29 16:30:12 +0100116
117 /*
Soby Mathew6b8b3022015-06-30 11:00:24 +0100118 * Plat. management: Perform platform specific actions to turn this
119 * cpu off e.g. exit cpu coherency, program the power controller etc.
Soby Mathew991d42c2015-06-29 16:30:12 +0100120 */
Soby Mathew6b8b3022015-06-30 11:00:24 +0100121 psci_plat_pm_ops->affinst_off(max_phys_off_afflvl);
Soby Mathew991d42c2015-06-29 16:30:12 +0100122
123exit:
124 /*
125 * Release the locks corresponding to each affinity level in the
126 * reverse order to which they were acquired.
127 */
Soby Mathew6b8b3022015-06-30 11:00:24 +0100128 psci_release_afflvl_locks(MPIDR_AFFLVL0,
Soby Mathew991d42c2015-06-29 16:30:12 +0100129 end_afflvl,
130 mpidr_nodes);
131
132 /*
133 * Check if all actions needed to safely power down this cpu have
134 * successfully completed. Enter a wfi loop which will allow the
135 * power controller to physically power down this cpu.
136 */
137 if (rc == PSCI_E_SUCCESS)
138 psci_power_down_wfi();
139
140 return rc;
141}