blob: 73d7106bdd0d8763fa47a462736adfc3b9fe7afc [file] [log] [blame]
Dan Handleyb4315302015-03-19 18:58:55 +00001/*
Antonio Nino Diaz88a05232018-06-19 09:29:36 +01002 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
Dan Handleyb4315302015-03-19 18:58:55 +00003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Dan Handleyb4315302015-03-19 18:58:55 +00005 */
6
7#include <arch_helpers.h>
Soby Mathewf9e858b2015-07-15 13:36:24 +01008#include <arm_def.h>
Soby Mathewc1bb8a02015-10-12 17:32:29 +01009#include <arm_gic.h>
Dan Handleyb4315302015-03-19 18:58:55 +000010#include <assert.h>
11#include <errno.h>
Soby Mathew2204afd2015-04-16 14:49:09 +010012#include <plat_arm.h>
Soby Mathewe35a3fb2017-10-11 16:08:58 +010013#include <platform.h>
Soby Mathew785fb922015-09-29 15:47:16 +010014#include <platform_def.h>
Dan Handleyb4315302015-03-19 18:58:55 +000015#include <psci.h>
16
Dimitris Papastamos2a246d2e2018-06-18 13:01:06 +010017/* Allow ARM Standard platforms to override these functions */
Soby Mathew5486a962016-10-21 17:51:22 +010018#pragma weak plat_arm_psci_override_pm_ops
Dimitris Papastamos2a246d2e2018-06-18 13:01:06 +010019#pragma weak plat_arm_program_trusted_mailbox
Soby Mathew5486a962016-10-21 17:51:22 +010020
Soby Mathew785fb922015-09-29 15:47:16 +010021/* Standard ARM platforms are expected to export plat_arm_psci_pm_ops */
Soby Mathew5486a962016-10-21 17:51:22 +010022extern plat_psci_ops_t plat_arm_psci_pm_ops;
Soby Mathew785fb922015-09-29 15:47:16 +010023
Soby Mathew2204afd2015-04-16 14:49:09 +010024#if ARM_RECOM_STATE_ID_ENC
25extern unsigned int arm_pm_idle_states[];
26#endif /* __ARM_RECOM_STATE_ID_ENC__ */
27
Soby Mathew2204afd2015-04-16 14:49:09 +010028#if !ARM_RECOM_STATE_ID_ENC
Dan Handleyb4315302015-03-19 18:58:55 +000029/*******************************************************************************
30 * ARM standard platform handler called to check the validity of the power state
31 * parameter.
32 ******************************************************************************/
Soby Mathew38dce702015-07-01 16:16:20 +010033int arm_validate_power_state(unsigned int power_state,
34 psci_power_state_t *req_state)
Dan Handleyb4315302015-03-19 18:58:55 +000035{
Soby Mathew38dce702015-07-01 16:16:20 +010036 int pstate = psci_get_pstate_type(power_state);
37 int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
38 int i;
39
40 assert(req_state);
41
42 if (pwr_lvl > PLAT_MAX_PWR_LVL)
43 return PSCI_E_INVALID_PARAMS;
44
Dan Handleyb4315302015-03-19 18:58:55 +000045 /* Sanity check the requested state */
Soby Mathew38dce702015-07-01 16:16:20 +010046 if (pstate == PSTATE_TYPE_STANDBY) {
Dan Handleyb4315302015-03-19 18:58:55 +000047 /*
Soby Mathew38dce702015-07-01 16:16:20 +010048 * It's possible to enter standby only on power level 0
49 * Ignore any other power level.
Dan Handleyb4315302015-03-19 18:58:55 +000050 */
Soby Mathew38dce702015-07-01 16:16:20 +010051 if (pwr_lvl != ARM_PWR_LVL0)
Dan Handleyb4315302015-03-19 18:58:55 +000052 return PSCI_E_INVALID_PARAMS;
Soby Mathew38dce702015-07-01 16:16:20 +010053
54 req_state->pwr_domain_state[ARM_PWR_LVL0] =
55 ARM_LOCAL_STATE_RET;
56 } else {
57 for (i = ARM_PWR_LVL0; i <= pwr_lvl; i++)
58 req_state->pwr_domain_state[i] =
59 ARM_LOCAL_STATE_OFF;
Dan Handleyb4315302015-03-19 18:58:55 +000060 }
61
62 /*
63 * We expect the 'state id' to be zero.
64 */
65 if (psci_get_pstate_id(power_state))
66 return PSCI_E_INVALID_PARAMS;
67
68 return PSCI_E_SUCCESS;
69}
Soby Mathew2204afd2015-04-16 14:49:09 +010070
71#else
72/*******************************************************************************
73 * ARM standard platform handler called to check the validity of the power
74 * state parameter. The power state parameter has to be a composite power
75 * state.
76 ******************************************************************************/
77int arm_validate_power_state(unsigned int power_state,
78 psci_power_state_t *req_state)
79{
80 unsigned int state_id;
81 int i;
82
83 assert(req_state);
84
85 /*
86 * Currently we are using a linear search for finding the matching
87 * entry in the idle power state array. This can be made a binary
88 * search if the number of entries justify the additional complexity.
89 */
90 for (i = 0; !!arm_pm_idle_states[i]; i++) {
91 if (power_state == arm_pm_idle_states[i])
92 break;
93 }
94
95 /* Return error if entry not found in the idle state array */
96 if (!arm_pm_idle_states[i])
97 return PSCI_E_INVALID_PARAMS;
98
99 i = 0;
100 state_id = psci_get_pstate_id(power_state);
101
102 /* Parse the State ID and populate the state info parameter */
103 while (state_id) {
104 req_state->pwr_domain_state[i++] = state_id &
105 ARM_LOCAL_PSTATE_MASK;
106 state_id >>= ARM_LOCAL_PSTATE_WIDTH;
107 }
108
109 return PSCI_E_SUCCESS;
110}
111#endif /* __ARM_RECOM_STATE_ID_ENC__ */
Soby Mathewf9e858b2015-07-15 13:36:24 +0100112
113/*******************************************************************************
114 * ARM standard platform handler called to check the validity of the non secure
Jeenu Viswambharan71e7a4e2017-09-19 09:27:18 +0100115 * entrypoint. Returns 0 if the entrypoint is valid, or -1 otherwise.
Soby Mathewf9e858b2015-07-15 13:36:24 +0100116 ******************************************************************************/
117int arm_validate_ns_entrypoint(uintptr_t entrypoint)
118{
119 /*
120 * Check if the non secure entrypoint lies within the non
121 * secure DRAM.
122 */
123 if ((entrypoint >= ARM_NS_DRAM1_BASE) && (entrypoint <
Jeenu Viswambharan71e7a4e2017-09-19 09:27:18 +0100124 (ARM_NS_DRAM1_BASE + ARM_NS_DRAM1_SIZE))) {
125 return 0;
126 }
dp-arm7c7dffd2017-05-03 12:14:10 +0100127#ifndef AARCH32
Soby Mathewf9e858b2015-07-15 13:36:24 +0100128 if ((entrypoint >= ARM_DRAM2_BASE) && (entrypoint <
Jeenu Viswambharan71e7a4e2017-09-19 09:27:18 +0100129 (ARM_DRAM2_BASE + ARM_DRAM2_SIZE))) {
130 return 0;
131 }
dp-arm7c7dffd2017-05-03 12:14:10 +0100132#endif
Soby Mathewf9e858b2015-07-15 13:36:24 +0100133
Jeenu Viswambharan71e7a4e2017-09-19 09:27:18 +0100134 return -1;
135}
136
137int arm_validate_psci_entrypoint(uintptr_t entrypoint)
138{
139 return arm_validate_ns_entrypoint(entrypoint) == 0 ? PSCI_E_SUCCESS :
140 PSCI_E_INVALID_ADDRESS;
Soby Mathewf9e858b2015-07-15 13:36:24 +0100141}
Soby Mathew785fb922015-09-29 15:47:16 +0100142
Soby Mathewc1bb8a02015-10-12 17:32:29 +0100143/******************************************************************************
Soby Mathew5486a962016-10-21 17:51:22 +0100144 * Default definition on ARM standard platforms to override the plat_psci_ops.
145 *****************************************************************************/
146const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
147{
148 return ops;
149}
150
151/******************************************************************************
Soby Mathewe35a3fb2017-10-11 16:08:58 +0100152 * Helper function to save the platform state before a system suspend. Save the
153 * state of the system components which are not in the Always ON power domain.
154 *****************************************************************************/
155void arm_system_pwr_domain_save(void)
156{
157 /* Assert system power domain is available on the platform */
158 assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
159
160 plat_arm_gic_save();
161
162 /*
Antonio Nino Diaz88a05232018-06-19 09:29:36 +0100163 * Unregister console now so that it is not registered for a second
164 * time during resume.
165 */
166 arm_console_runtime_end();
167
168 /*
Soby Mathewe35a3fb2017-10-11 16:08:58 +0100169 * All the other peripheral which are configured by ARM TF are
170 * re-initialized on resume from system suspend. Hence we
171 * don't save their state here.
172 */
173}
174
175/******************************************************************************
Soby Mathewc1bb8a02015-10-12 17:32:29 +0100176 * Helper function to resume the platform from system suspend. Reinitialize
177 * the system components which are not in the Always ON power domain.
178 * TODO: Unify the platform setup when waking up from cold boot and system
179 * resume in arm_bl31_platform_setup().
180 *****************************************************************************/
181void arm_system_pwr_domain_resume(void)
182{
Antonio Nino Diaz88a05232018-06-19 09:29:36 +0100183 /* Initialize the console */
184 arm_console_runtime_init();
Soby Mathewc1bb8a02015-10-12 17:32:29 +0100185
186 /* Assert system power domain is available on the platform */
187 assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
188
Soby Mathewe35a3fb2017-10-11 16:08:58 +0100189 plat_arm_gic_resume();
190
Soby Mathewc1bb8a02015-10-12 17:32:29 +0100191 plat_arm_security_setup();
Soby Mathewc1bb8a02015-10-12 17:32:29 +0100192 arm_configure_sys_timer();
193}
194
Soby Mathew785fb922015-09-29 15:47:16 +0100195/*******************************************************************************
Dimitris Papastamos2a246d2e2018-06-18 13:01:06 +0100196 * ARM platform function to program the mailbox for a cpu before it is released
Soby Mathew785fb922015-09-29 15:47:16 +0100197 * from reset. This function assumes that the Trusted mail box base is within
198 * the ARM_SHARED_RAM region
199 ******************************************************************************/
Dimitris Papastamos2a246d2e2018-06-18 13:01:06 +0100200void plat_arm_program_trusted_mailbox(uintptr_t address)
Soby Mathew785fb922015-09-29 15:47:16 +0100201{
202 uintptr_t *mailbox = (void *) PLAT_ARM_TRUSTED_MAILBOX_BASE;
203
204 *mailbox = address;
205
206 /*
207 * Ensure that the PLAT_ARM_TRUSTED_MAILBOX_BASE is within
208 * ARM_SHARED_RAM region.
209 */
210 assert((PLAT_ARM_TRUSTED_MAILBOX_BASE >= ARM_SHARED_RAM_BASE) &&
211 ((PLAT_ARM_TRUSTED_MAILBOX_BASE + sizeof(*mailbox)) <= \
212 (ARM_SHARED_RAM_BASE + ARM_SHARED_RAM_SIZE)));
Soby Mathew785fb922015-09-29 15:47:16 +0100213}
214
215/*******************************************************************************
216 * The ARM Standard platform definition of platform porting API
217 * `plat_setup_psci_ops`.
218 ******************************************************************************/
219int plat_setup_psci_ops(uintptr_t sec_entrypoint,
220 const plat_psci_ops_t **psci_ops)
221{
Soby Mathew5486a962016-10-21 17:51:22 +0100222 *psci_ops = plat_arm_psci_override_pm_ops(&plat_arm_psci_pm_ops);
Soby Mathew785fb922015-09-29 15:47:16 +0100223
224 /* Setup mailbox with entry point. */
Dimitris Papastamos2a246d2e2018-06-18 13:01:06 +0100225 plat_arm_program_trusted_mailbox(sec_entrypoint);
Soby Mathew785fb922015-09-29 15:47:16 +0100226 return 0;
227}