blob: d13d2683c8ad6d03ed892090554dfec46d98db9f [file] [log] [blame]
Dan Handley9df48042015-03-19 18:58:55 +00001/*
2 * Copyright (c) 2015, 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_helpers.h>
Soby Mathew0d9e8522015-07-15 13:36:24 +010032#include <arm_def.h>
Soby Mathew61e8d0b2015-10-12 17:32:29 +010033#include <arm_gic.h>
Dan Handley9df48042015-03-19 18:58:55 +000034#include <assert.h>
Soby Mathew61e8d0b2015-10-12 17:32:29 +010035#include <console.h>
Dan Handley9df48042015-03-19 18:58:55 +000036#include <errno.h>
Soby Mathew7799cf72015-04-16 14:49:09 +010037#include <plat_arm.h>
Soby Mathewfeac8fc2015-09-29 15:47:16 +010038#include <platform_def.h>
Dan Handley9df48042015-03-19 18:58:55 +000039#include <psci.h>
40
Soby Mathewfeac8fc2015-09-29 15:47:16 +010041/* Standard ARM platforms are expected to export plat_arm_psci_pm_ops */
42extern const plat_psci_ops_t plat_arm_psci_pm_ops;
43
Soby Mathew7799cf72015-04-16 14:49:09 +010044#if ARM_RECOM_STATE_ID_ENC
45extern unsigned int arm_pm_idle_states[];
46#endif /* __ARM_RECOM_STATE_ID_ENC__ */
47
Soby Mathew7799cf72015-04-16 14:49:09 +010048#if !ARM_RECOM_STATE_ID_ENC
Dan Handley9df48042015-03-19 18:58:55 +000049/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +010050 * ARM standard platform handler called to check the validity of the power state
51 * parameter.
Dan Handley9df48042015-03-19 18:58:55 +000052 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +010053int arm_validate_power_state(unsigned int power_state,
54 psci_power_state_t *req_state)
Dan Handley9df48042015-03-19 18:58:55 +000055{
Soby Mathewfec4eb72015-07-01 16:16:20 +010056 int pstate = psci_get_pstate_type(power_state);
57 int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
58 int i;
Dan Handley9df48042015-03-19 18:58:55 +000059
Soby Mathewfec4eb72015-07-01 16:16:20 +010060 assert(req_state);
Dan Handley9df48042015-03-19 18:58:55 +000061
Soby Mathewfec4eb72015-07-01 16:16:20 +010062 if (pwr_lvl > PLAT_MAX_PWR_LVL)
63 return PSCI_E_INVALID_PARAMS;
Dan Handley9df48042015-03-19 18:58:55 +000064
Dan Handley9df48042015-03-19 18:58:55 +000065 /* Sanity check the requested state */
Soby Mathewfec4eb72015-07-01 16:16:20 +010066 if (pstate == PSTATE_TYPE_STANDBY) {
Dan Handley9df48042015-03-19 18:58:55 +000067 /*
Soby Mathewfec4eb72015-07-01 16:16:20 +010068 * It's possible to enter standby only on power level 0
69 * Ignore any other power level.
Dan Handley9df48042015-03-19 18:58:55 +000070 */
Soby Mathewfec4eb72015-07-01 16:16:20 +010071 if (pwr_lvl != ARM_PWR_LVL0)
Dan Handley9df48042015-03-19 18:58:55 +000072 return PSCI_E_INVALID_PARAMS;
Soby Mathewfec4eb72015-07-01 16:16:20 +010073
74 req_state->pwr_domain_state[ARM_PWR_LVL0] =
75 ARM_LOCAL_STATE_RET;
76 } else {
77 for (i = ARM_PWR_LVL0; i <= pwr_lvl; i++)
78 req_state->pwr_domain_state[i] =
79 ARM_LOCAL_STATE_OFF;
Dan Handley9df48042015-03-19 18:58:55 +000080 }
81
82 /*
83 * We expect the 'state id' to be zero.
84 */
85 if (psci_get_pstate_id(power_state))
86 return PSCI_E_INVALID_PARAMS;
87
Soby Mathew7799cf72015-04-16 14:49:09 +010088 return PSCI_E_SUCCESS;
89}
90
91#else
92/*******************************************************************************
93 * ARM standard platform handler called to check the validity of the power
94 * state parameter. The power state parameter has to be a composite power
95 * state.
96 ******************************************************************************/
97int arm_validate_power_state(unsigned int power_state,
98 psci_power_state_t *req_state)
99{
100 unsigned int state_id;
101 int i;
102
103 assert(req_state);
104
105 /*
106 * Currently we are using a linear search for finding the matching
107 * entry in the idle power state array. This can be made a binary
108 * search if the number of entries justify the additional complexity.
109 */
110 for (i = 0; !!arm_pm_idle_states[i]; i++) {
111 if (power_state == arm_pm_idle_states[i])
112 break;
113 }
114
115 /* Return error if entry not found in the idle state array */
116 if (!arm_pm_idle_states[i])
117 return PSCI_E_INVALID_PARAMS;
118
119 i = 0;
120 state_id = psci_get_pstate_id(power_state);
121
122 /* Parse the State ID and populate the state info parameter */
123 while (state_id) {
124 req_state->pwr_domain_state[i++] = state_id &
125 ARM_LOCAL_PSTATE_MASK;
126 state_id >>= ARM_LOCAL_PSTATE_WIDTH;
127 }
128
Dan Handley9df48042015-03-19 18:58:55 +0000129 return PSCI_E_SUCCESS;
130}
Soby Mathew7799cf72015-04-16 14:49:09 +0100131#endif /* __ARM_RECOM_STATE_ID_ENC__ */
Soby Mathew0d9e8522015-07-15 13:36:24 +0100132
133/*******************************************************************************
134 * ARM standard platform handler called to check the validity of the non secure
135 * entrypoint.
136 ******************************************************************************/
137int arm_validate_ns_entrypoint(uintptr_t entrypoint)
138{
139 /*
140 * Check if the non secure entrypoint lies within the non
141 * secure DRAM.
142 */
143 if ((entrypoint >= ARM_NS_DRAM1_BASE) && (entrypoint <
144 (ARM_NS_DRAM1_BASE + ARM_NS_DRAM1_SIZE)))
145 return PSCI_E_SUCCESS;
146 if ((entrypoint >= ARM_DRAM2_BASE) && (entrypoint <
147 (ARM_DRAM2_BASE + ARM_DRAM2_SIZE)))
148 return PSCI_E_SUCCESS;
149
150 return PSCI_E_INVALID_ADDRESS;
151}
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100152
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100153/******************************************************************************
154 * Helper function to resume the platform from system suspend. Reinitialize
155 * the system components which are not in the Always ON power domain.
156 * TODO: Unify the platform setup when waking up from cold boot and system
157 * resume in arm_bl31_platform_setup().
158 *****************************************************************************/
159void arm_system_pwr_domain_resume(void)
160{
161 console_init(PLAT_ARM_BOOT_UART_BASE, PLAT_ARM_BOOT_UART_CLK_IN_HZ,
162 ARM_CONSOLE_BAUDRATE);
163
164 /* Assert system power domain is available on the platform */
165 assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
166
167 arm_gic_setup();
168 plat_arm_security_setup();
169
170 arm_configure_sys_timer();
171}
172
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100173/*******************************************************************************
174 * Private function to program the mailbox for a cpu before it is released
175 * from reset. This function assumes that the Trusted mail box base is within
176 * the ARM_SHARED_RAM region
177 ******************************************************************************/
178static void arm_program_trusted_mailbox(uintptr_t address)
179{
180 uintptr_t *mailbox = (void *) PLAT_ARM_TRUSTED_MAILBOX_BASE;
181
182 *mailbox = address;
183
184 /*
185 * Ensure that the PLAT_ARM_TRUSTED_MAILBOX_BASE is within
186 * ARM_SHARED_RAM region.
187 */
188 assert((PLAT_ARM_TRUSTED_MAILBOX_BASE >= ARM_SHARED_RAM_BASE) &&
189 ((PLAT_ARM_TRUSTED_MAILBOX_BASE + sizeof(*mailbox)) <= \
190 (ARM_SHARED_RAM_BASE + ARM_SHARED_RAM_SIZE)));
191
192 /* Flush data cache if the mail box shared RAM is cached */
193#if PLAT_ARM_SHARED_RAM_CACHED
194 flush_dcache_range((uintptr_t) mailbox, sizeof(*mailbox));
195#endif
196}
197
198/*******************************************************************************
199 * The ARM Standard platform definition of platform porting API
200 * `plat_setup_psci_ops`.
201 ******************************************************************************/
202int plat_setup_psci_ops(uintptr_t sec_entrypoint,
203 const plat_psci_ops_t **psci_ops)
204{
205 *psci_ops = &plat_arm_psci_pm_ops;
206
207 /* Setup mailbox with entry point. */
208 arm_program_trusted_mailbox(sec_entrypoint);
209 return 0;
210}