blob: 4257d3cd3c9d7d472018e9e8f189c29a0ca1df5d [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
Dimitris Papastamosd7a36512018-06-18 13:01:06 +010017/* Allow ARM Standard platforms to override these functions */
Soby Mathew0b4c5a32016-10-21 17:51:22 +010018#pragma weak plat_arm_psci_override_pm_ops
Dimitris Papastamosd7a36512018-06-18 13:01:06 +010019#pragma weak plat_arm_program_trusted_mailbox
Soby Mathew0b4c5a32016-10-21 17:51:22 +010020
Soby Mathew7799cf72015-04-16 14:49:09 +010021#if !ARM_RECOM_STATE_ID_ENC
Dan Handley9df48042015-03-19 18:58:55 +000022/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +010023 * ARM standard platform handler called to check the validity of the power state
24 * parameter.
Dan Handley9df48042015-03-19 18:58:55 +000025 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +010026int arm_validate_power_state(unsigned int power_state,
27 psci_power_state_t *req_state)
Dan Handley9df48042015-03-19 18:58:55 +000028{
Antonio Nino Diazfec756f2018-07-18 16:24:16 +010029 unsigned int pstate = psci_get_pstate_type(power_state);
30 unsigned int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
31 unsigned int i;
Dan Handley9df48042015-03-19 18:58:55 +000032
Antonio Nino Diazfec756f2018-07-18 16:24:16 +010033 assert(req_state > 0U);
Dan Handley9df48042015-03-19 18:58:55 +000034
Soby Mathewfec4eb72015-07-01 16:16:20 +010035 if (pwr_lvl > PLAT_MAX_PWR_LVL)
36 return PSCI_E_INVALID_PARAMS;
Dan Handley9df48042015-03-19 18:58:55 +000037
Dan Handley9df48042015-03-19 18:58:55 +000038 /* Sanity check the requested state */
Soby Mathewfec4eb72015-07-01 16:16:20 +010039 if (pstate == PSTATE_TYPE_STANDBY) {
Dan Handley9df48042015-03-19 18:58:55 +000040 /*
Soby Mathewfec4eb72015-07-01 16:16:20 +010041 * It's possible to enter standby only on power level 0
42 * Ignore any other power level.
Dan Handley9df48042015-03-19 18:58:55 +000043 */
Soby Mathewfec4eb72015-07-01 16:16:20 +010044 if (pwr_lvl != ARM_PWR_LVL0)
Dan Handley9df48042015-03-19 18:58:55 +000045 return PSCI_E_INVALID_PARAMS;
Soby Mathewfec4eb72015-07-01 16:16:20 +010046
47 req_state->pwr_domain_state[ARM_PWR_LVL0] =
48 ARM_LOCAL_STATE_RET;
49 } else {
50 for (i = ARM_PWR_LVL0; i <= pwr_lvl; i++)
51 req_state->pwr_domain_state[i] =
52 ARM_LOCAL_STATE_OFF;
Dan Handley9df48042015-03-19 18:58:55 +000053 }
54
55 /*
56 * We expect the 'state id' to be zero.
57 */
Antonio Nino Diazfec756f2018-07-18 16:24:16 +010058 if (psci_get_pstate_id(power_state) != 0U)
Dan Handley9df48042015-03-19 18:58:55 +000059 return PSCI_E_INVALID_PARAMS;
60
Soby Mathew7799cf72015-04-16 14:49:09 +010061 return PSCI_E_SUCCESS;
62}
63
64#else
65/*******************************************************************************
66 * ARM standard platform handler called to check the validity of the power
67 * state parameter. The power state parameter has to be a composite power
68 * state.
69 ******************************************************************************/
70int arm_validate_power_state(unsigned int power_state,
71 psci_power_state_t *req_state)
72{
73 unsigned int state_id;
74 int i;
75
76 assert(req_state);
77
78 /*
79 * Currently we are using a linear search for finding the matching
80 * entry in the idle power state array. This can be made a binary
81 * search if the number of entries justify the additional complexity.
82 */
83 for (i = 0; !!arm_pm_idle_states[i]; i++) {
84 if (power_state == arm_pm_idle_states[i])
85 break;
86 }
87
88 /* Return error if entry not found in the idle state array */
89 if (!arm_pm_idle_states[i])
90 return PSCI_E_INVALID_PARAMS;
91
92 i = 0;
93 state_id = psci_get_pstate_id(power_state);
94
95 /* Parse the State ID and populate the state info parameter */
96 while (state_id) {
97 req_state->pwr_domain_state[i++] = state_id &
98 ARM_LOCAL_PSTATE_MASK;
99 state_id >>= ARM_LOCAL_PSTATE_WIDTH;
100 }
101
Dan Handley9df48042015-03-19 18:58:55 +0000102 return PSCI_E_SUCCESS;
103}
Soby Mathew7799cf72015-04-16 14:49:09 +0100104#endif /* __ARM_RECOM_STATE_ID_ENC__ */
Soby Mathew0d9e8522015-07-15 13:36:24 +0100105
106/*******************************************************************************
107 * ARM standard platform handler called to check the validity of the non secure
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100108 * entrypoint. Returns 0 if the entrypoint is valid, or -1 otherwise.
Soby Mathew0d9e8522015-07-15 13:36:24 +0100109 ******************************************************************************/
110int arm_validate_ns_entrypoint(uintptr_t entrypoint)
111{
112 /*
113 * Check if the non secure entrypoint lies within the non
114 * secure DRAM.
115 */
116 if ((entrypoint >= ARM_NS_DRAM1_BASE) && (entrypoint <
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100117 (ARM_NS_DRAM1_BASE + ARM_NS_DRAM1_SIZE))) {
118 return 0;
119 }
dp-arm84fc2952017-05-03 12:14:10 +0100120#ifndef AARCH32
Soby Mathew0d9e8522015-07-15 13:36:24 +0100121 if ((entrypoint >= ARM_DRAM2_BASE) && (entrypoint <
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100122 (ARM_DRAM2_BASE + ARM_DRAM2_SIZE))) {
123 return 0;
124 }
dp-arm84fc2952017-05-03 12:14:10 +0100125#endif
Soby Mathew0d9e8522015-07-15 13:36:24 +0100126
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100127 return -1;
128}
129
130int arm_validate_psci_entrypoint(uintptr_t entrypoint)
131{
132 return arm_validate_ns_entrypoint(entrypoint) == 0 ? PSCI_E_SUCCESS :
133 PSCI_E_INVALID_ADDRESS;
Soby Mathew0d9e8522015-07-15 13:36:24 +0100134}
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100135
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100136/******************************************************************************
Soby Mathew0b4c5a32016-10-21 17:51:22 +0100137 * Default definition on ARM standard platforms to override the plat_psci_ops.
138 *****************************************************************************/
139const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
140{
141 return ops;
142}
143
144/******************************************************************************
Soby Mathew9ca28062017-10-11 16:08:58 +0100145 * Helper function to save the platform state before a system suspend. Save the
146 * state of the system components which are not in the Always ON power domain.
147 *****************************************************************************/
148void arm_system_pwr_domain_save(void)
149{
150 /* Assert system power domain is available on the platform */
151 assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
152
153 plat_arm_gic_save();
154
155 /*
Antonio Nino Diaz23ede6a2018-06-19 09:29:36 +0100156 * Unregister console now so that it is not registered for a second
157 * time during resume.
158 */
159 arm_console_runtime_end();
160
161 /*
Soby Mathew9ca28062017-10-11 16:08:58 +0100162 * All the other peripheral which are configured by ARM TF are
163 * re-initialized on resume from system suspend. Hence we
164 * don't save their state here.
165 */
166}
167
168/******************************************************************************
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100169 * Helper function to resume the platform from system suspend. Reinitialize
170 * the system components which are not in the Always ON power domain.
171 * TODO: Unify the platform setup when waking up from cold boot and system
172 * resume in arm_bl31_platform_setup().
173 *****************************************************************************/
174void arm_system_pwr_domain_resume(void)
175{
Antonio Nino Diaz23ede6a2018-06-19 09:29:36 +0100176 /* Initialize the console */
177 arm_console_runtime_init();
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100178
179 /* Assert system power domain is available on the platform */
180 assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
181
Soby Mathew9ca28062017-10-11 16:08:58 +0100182 plat_arm_gic_resume();
183
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100184 plat_arm_security_setup();
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100185 arm_configure_sys_timer();
186}
187
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100188/*******************************************************************************
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100189 * ARM platform function to program the mailbox for a cpu before it is released
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100190 * from reset. This function assumes that the Trusted mail box base is within
191 * the ARM_SHARED_RAM region
192 ******************************************************************************/
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100193void plat_arm_program_trusted_mailbox(uintptr_t address)
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100194{
195 uintptr_t *mailbox = (void *) PLAT_ARM_TRUSTED_MAILBOX_BASE;
196
197 *mailbox = address;
198
199 /*
200 * Ensure that the PLAT_ARM_TRUSTED_MAILBOX_BASE is within
201 * ARM_SHARED_RAM region.
202 */
203 assert((PLAT_ARM_TRUSTED_MAILBOX_BASE >= ARM_SHARED_RAM_BASE) &&
204 ((PLAT_ARM_TRUSTED_MAILBOX_BASE + sizeof(*mailbox)) <= \
205 (ARM_SHARED_RAM_BASE + ARM_SHARED_RAM_SIZE)));
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100206}
207
208/*******************************************************************************
209 * The ARM Standard platform definition of platform porting API
210 * `plat_setup_psci_ops`.
211 ******************************************************************************/
212int plat_setup_psci_ops(uintptr_t sec_entrypoint,
213 const plat_psci_ops_t **psci_ops)
214{
Soby Mathew0b4c5a32016-10-21 17:51:22 +0100215 *psci_ops = plat_arm_psci_override_pm_ops(&plat_arm_psci_pm_ops);
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100216
217 /* Setup mailbox with entry point. */
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100218 plat_arm_program_trusted_mailbox(sec_entrypoint);
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100219 return 0;
220}