blob: 4632099e7f49611bff6dea3fbdedc72866678770 [file] [log] [blame]
Dan Handley9df48042015-03-19 18:58:55 +00001/*
Antonio Nino Diaz23ede6a2018-06-19 09:29:36 +01002 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
Dan Handley9df48042015-03-19 18:58:55 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Dan Handley9df48042015-03-19 18:58:55 +00005 */
6
7#include <arch_helpers.h>
Soby Mathew0d9e8522015-07-15 13:36:24 +01008#include <arm_def.h>
Soby Mathew61e8d0b2015-10-12 17:32:29 +01009#include <arm_gic.h>
Dan Handley9df48042015-03-19 18:58:55 +000010#include <assert.h>
11#include <errno.h>
Soby Mathew7799cf72015-04-16 14:49:09 +010012#include <plat_arm.h>
Soby Mathew9ca28062017-10-11 16:08:58 +010013#include <platform.h>
Soby Mathewfeac8fc2015-09-29 15:47:16 +010014#include <platform_def.h>
Dan Handley9df48042015-03-19 18:58:55 +000015#include <psci.h>
16
Soby Mathew0b4c5a32016-10-21 17:51:22 +010017/* Allow ARM Standard platforms to override this function */
18#pragma weak plat_arm_psci_override_pm_ops
19
Soby Mathewfeac8fc2015-09-29 15:47:16 +010020/* Standard ARM platforms are expected to export plat_arm_psci_pm_ops */
Soby Mathew0b4c5a32016-10-21 17:51:22 +010021extern plat_psci_ops_t plat_arm_psci_pm_ops;
Soby Mathewfeac8fc2015-09-29 15:47:16 +010022
Soby Mathew7799cf72015-04-16 14:49:09 +010023#if ARM_RECOM_STATE_ID_ENC
24extern unsigned int arm_pm_idle_states[];
25#endif /* __ARM_RECOM_STATE_ID_ENC__ */
26
Soby Mathew7799cf72015-04-16 14:49:09 +010027#if !ARM_RECOM_STATE_ID_ENC
Dan Handley9df48042015-03-19 18:58:55 +000028/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +010029 * ARM standard platform handler called to check the validity of the power state
30 * parameter.
Dan Handley9df48042015-03-19 18:58:55 +000031 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +010032int arm_validate_power_state(unsigned int power_state,
33 psci_power_state_t *req_state)
Dan Handley9df48042015-03-19 18:58:55 +000034{
Soby Mathewfec4eb72015-07-01 16:16:20 +010035 int pstate = psci_get_pstate_type(power_state);
36 int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
37 int i;
Dan Handley9df48042015-03-19 18:58:55 +000038
Soby Mathewfec4eb72015-07-01 16:16:20 +010039 assert(req_state);
Dan Handley9df48042015-03-19 18:58:55 +000040
Soby Mathewfec4eb72015-07-01 16:16:20 +010041 if (pwr_lvl > PLAT_MAX_PWR_LVL)
42 return PSCI_E_INVALID_PARAMS;
Dan Handley9df48042015-03-19 18:58:55 +000043
Dan Handley9df48042015-03-19 18:58:55 +000044 /* Sanity check the requested state */
Soby Mathewfec4eb72015-07-01 16:16:20 +010045 if (pstate == PSTATE_TYPE_STANDBY) {
Dan Handley9df48042015-03-19 18:58:55 +000046 /*
Soby Mathewfec4eb72015-07-01 16:16:20 +010047 * It's possible to enter standby only on power level 0
48 * Ignore any other power level.
Dan Handley9df48042015-03-19 18:58:55 +000049 */
Soby Mathewfec4eb72015-07-01 16:16:20 +010050 if (pwr_lvl != ARM_PWR_LVL0)
Dan Handley9df48042015-03-19 18:58:55 +000051 return PSCI_E_INVALID_PARAMS;
Soby Mathewfec4eb72015-07-01 16:16:20 +010052
53 req_state->pwr_domain_state[ARM_PWR_LVL0] =
54 ARM_LOCAL_STATE_RET;
55 } else {
56 for (i = ARM_PWR_LVL0; i <= pwr_lvl; i++)
57 req_state->pwr_domain_state[i] =
58 ARM_LOCAL_STATE_OFF;
Dan Handley9df48042015-03-19 18:58:55 +000059 }
60
61 /*
62 * We expect the 'state id' to be zero.
63 */
64 if (psci_get_pstate_id(power_state))
65 return PSCI_E_INVALID_PARAMS;
66
Soby Mathew7799cf72015-04-16 14:49:09 +010067 return PSCI_E_SUCCESS;
68}
69
70#else
71/*******************************************************************************
72 * ARM standard platform handler called to check the validity of the power
73 * state parameter. The power state parameter has to be a composite power
74 * state.
75 ******************************************************************************/
76int arm_validate_power_state(unsigned int power_state,
77 psci_power_state_t *req_state)
78{
79 unsigned int state_id;
80 int i;
81
82 assert(req_state);
83
84 /*
85 * Currently we are using a linear search for finding the matching
86 * entry in the idle power state array. This can be made a binary
87 * search if the number of entries justify the additional complexity.
88 */
89 for (i = 0; !!arm_pm_idle_states[i]; i++) {
90 if (power_state == arm_pm_idle_states[i])
91 break;
92 }
93
94 /* Return error if entry not found in the idle state array */
95 if (!arm_pm_idle_states[i])
96 return PSCI_E_INVALID_PARAMS;
97
98 i = 0;
99 state_id = psci_get_pstate_id(power_state);
100
101 /* Parse the State ID and populate the state info parameter */
102 while (state_id) {
103 req_state->pwr_domain_state[i++] = state_id &
104 ARM_LOCAL_PSTATE_MASK;
105 state_id >>= ARM_LOCAL_PSTATE_WIDTH;
106 }
107
Dan Handley9df48042015-03-19 18:58:55 +0000108 return PSCI_E_SUCCESS;
109}
Soby Mathew7799cf72015-04-16 14:49:09 +0100110#endif /* __ARM_RECOM_STATE_ID_ENC__ */
Soby Mathew0d9e8522015-07-15 13:36:24 +0100111
112/*******************************************************************************
113 * ARM standard platform handler called to check the validity of the non secure
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100114 * entrypoint. Returns 0 if the entrypoint is valid, or -1 otherwise.
Soby Mathew0d9e8522015-07-15 13:36:24 +0100115 ******************************************************************************/
116int arm_validate_ns_entrypoint(uintptr_t entrypoint)
117{
118 /*
119 * Check if the non secure entrypoint lies within the non
120 * secure DRAM.
121 */
122 if ((entrypoint >= ARM_NS_DRAM1_BASE) && (entrypoint <
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100123 (ARM_NS_DRAM1_BASE + ARM_NS_DRAM1_SIZE))) {
124 return 0;
125 }
dp-arm84fc2952017-05-03 12:14:10 +0100126#ifndef AARCH32
Soby Mathew0d9e8522015-07-15 13:36:24 +0100127 if ((entrypoint >= ARM_DRAM2_BASE) && (entrypoint <
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100128 (ARM_DRAM2_BASE + ARM_DRAM2_SIZE))) {
129 return 0;
130 }
dp-arm84fc2952017-05-03 12:14:10 +0100131#endif
Soby Mathew0d9e8522015-07-15 13:36:24 +0100132
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100133 return -1;
134}
135
136int arm_validate_psci_entrypoint(uintptr_t entrypoint)
137{
138 return arm_validate_ns_entrypoint(entrypoint) == 0 ? PSCI_E_SUCCESS :
139 PSCI_E_INVALID_ADDRESS;
Soby Mathew0d9e8522015-07-15 13:36:24 +0100140}
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100141
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100142/******************************************************************************
Soby Mathew0b4c5a32016-10-21 17:51:22 +0100143 * Default definition on ARM standard platforms to override the plat_psci_ops.
144 *****************************************************************************/
145const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
146{
147 return ops;
148}
149
150/******************************************************************************
Soby Mathew9ca28062017-10-11 16:08:58 +0100151 * Helper function to save the platform state before a system suspend. Save the
152 * state of the system components which are not in the Always ON power domain.
153 *****************************************************************************/
154void arm_system_pwr_domain_save(void)
155{
156 /* Assert system power domain is available on the platform */
157 assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
158
159 plat_arm_gic_save();
160
161 /*
Antonio Nino Diaz23ede6a2018-06-19 09:29:36 +0100162 * Unregister console now so that it is not registered for a second
163 * time during resume.
164 */
165 arm_console_runtime_end();
166
167 /*
Soby Mathew9ca28062017-10-11 16:08:58 +0100168 * All the other peripheral which are configured by ARM TF are
169 * re-initialized on resume from system suspend. Hence we
170 * don't save their state here.
171 */
172}
173
174/******************************************************************************
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100175 * Helper function to resume the platform from system suspend. Reinitialize
176 * the system components which are not in the Always ON power domain.
177 * TODO: Unify the platform setup when waking up from cold boot and system
178 * resume in arm_bl31_platform_setup().
179 *****************************************************************************/
180void arm_system_pwr_domain_resume(void)
181{
Antonio Nino Diaz23ede6a2018-06-19 09:29:36 +0100182 /* Initialize the console */
183 arm_console_runtime_init();
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100184
185 /* Assert system power domain is available on the platform */
186 assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
187
Soby Mathew9ca28062017-10-11 16:08:58 +0100188 plat_arm_gic_resume();
189
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100190 plat_arm_security_setup();
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100191 arm_configure_sys_timer();
192}
193
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100194/*******************************************************************************
195 * Private function to program the mailbox for a cpu before it is released
196 * from reset. This function assumes that the Trusted mail box base is within
197 * the ARM_SHARED_RAM region
198 ******************************************************************************/
Sandrine Bailleux03897bb2015-11-26 16:31:34 +0000199void arm_program_trusted_mailbox(uintptr_t address)
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100200{
201 uintptr_t *mailbox = (void *) PLAT_ARM_TRUSTED_MAILBOX_BASE;
202
203 *mailbox = address;
204
205 /*
206 * Ensure that the PLAT_ARM_TRUSTED_MAILBOX_BASE is within
207 * ARM_SHARED_RAM region.
208 */
209 assert((PLAT_ARM_TRUSTED_MAILBOX_BASE >= ARM_SHARED_RAM_BASE) &&
210 ((PLAT_ARM_TRUSTED_MAILBOX_BASE + sizeof(*mailbox)) <= \
211 (ARM_SHARED_RAM_BASE + ARM_SHARED_RAM_SIZE)));
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100212}
213
214/*******************************************************************************
215 * The ARM Standard platform definition of platform porting API
216 * `plat_setup_psci_ops`.
217 ******************************************************************************/
218int plat_setup_psci_ops(uintptr_t sec_entrypoint,
219 const plat_psci_ops_t **psci_ops)
220{
Soby Mathew0b4c5a32016-10-21 17:51:22 +0100221 *psci_ops = plat_arm_psci_override_pm_ops(&plat_arm_psci_pm_ops);
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100222
223 /* Setup mailbox with entry point. */
224 arm_program_trusted_mailbox(sec_entrypoint);
225 return 0;
226}