blob: d0350d6c5d5cb5b5c2bf80c8cbad9836a10b0307 [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
22extern unsigned int arm_pm_idle_states[];
23#endif /* __ARM_RECOM_STATE_ID_ENC__ */
24
Soby Mathew7799cf72015-04-16 14:49:09 +010025#if !ARM_RECOM_STATE_ID_ENC
Dan Handley9df48042015-03-19 18:58:55 +000026/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +010027 * ARM standard platform handler called to check the validity of the power state
28 * parameter.
Dan Handley9df48042015-03-19 18:58:55 +000029 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +010030int arm_validate_power_state(unsigned int power_state,
31 psci_power_state_t *req_state)
Dan Handley9df48042015-03-19 18:58:55 +000032{
Soby Mathewfec4eb72015-07-01 16:16:20 +010033 int pstate = psci_get_pstate_type(power_state);
34 int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
35 int i;
Dan Handley9df48042015-03-19 18:58:55 +000036
Soby Mathewfec4eb72015-07-01 16:16:20 +010037 assert(req_state);
Dan Handley9df48042015-03-19 18:58:55 +000038
Soby Mathewfec4eb72015-07-01 16:16:20 +010039 if (pwr_lvl > PLAT_MAX_PWR_LVL)
40 return PSCI_E_INVALID_PARAMS;
Dan Handley9df48042015-03-19 18:58:55 +000041
Dan Handley9df48042015-03-19 18:58:55 +000042 /* Sanity check the requested state */
Soby Mathewfec4eb72015-07-01 16:16:20 +010043 if (pstate == PSTATE_TYPE_STANDBY) {
Dan Handley9df48042015-03-19 18:58:55 +000044 /*
Soby Mathewfec4eb72015-07-01 16:16:20 +010045 * It's possible to enter standby only on power level 0
46 * Ignore any other power level.
Dan Handley9df48042015-03-19 18:58:55 +000047 */
Soby Mathewfec4eb72015-07-01 16:16:20 +010048 if (pwr_lvl != ARM_PWR_LVL0)
Dan Handley9df48042015-03-19 18:58:55 +000049 return PSCI_E_INVALID_PARAMS;
Soby Mathewfec4eb72015-07-01 16:16:20 +010050
51 req_state->pwr_domain_state[ARM_PWR_LVL0] =
52 ARM_LOCAL_STATE_RET;
53 } else {
54 for (i = ARM_PWR_LVL0; i <= pwr_lvl; i++)
55 req_state->pwr_domain_state[i] =
56 ARM_LOCAL_STATE_OFF;
Dan Handley9df48042015-03-19 18:58:55 +000057 }
58
59 /*
60 * We expect the 'state id' to be zero.
61 */
62 if (psci_get_pstate_id(power_state))
63 return PSCI_E_INVALID_PARAMS;
64
Soby Mathew7799cf72015-04-16 14:49:09 +010065 return PSCI_E_SUCCESS;
66}
67
68#else
69/*******************************************************************************
70 * ARM standard platform handler called to check the validity of the power
71 * state parameter. The power state parameter has to be a composite power
72 * state.
73 ******************************************************************************/
74int arm_validate_power_state(unsigned int power_state,
75 psci_power_state_t *req_state)
76{
77 unsigned int state_id;
78 int i;
79
80 assert(req_state);
81
82 /*
83 * Currently we are using a linear search for finding the matching
84 * entry in the idle power state array. This can be made a binary
85 * search if the number of entries justify the additional complexity.
86 */
87 for (i = 0; !!arm_pm_idle_states[i]; i++) {
88 if (power_state == arm_pm_idle_states[i])
89 break;
90 }
91
92 /* Return error if entry not found in the idle state array */
93 if (!arm_pm_idle_states[i])
94 return PSCI_E_INVALID_PARAMS;
95
96 i = 0;
97 state_id = psci_get_pstate_id(power_state);
98
99 /* Parse the State ID and populate the state info parameter */
100 while (state_id) {
101 req_state->pwr_domain_state[i++] = state_id &
102 ARM_LOCAL_PSTATE_MASK;
103 state_id >>= ARM_LOCAL_PSTATE_WIDTH;
104 }
105
Dan Handley9df48042015-03-19 18:58:55 +0000106 return PSCI_E_SUCCESS;
107}
Soby Mathew7799cf72015-04-16 14:49:09 +0100108#endif /* __ARM_RECOM_STATE_ID_ENC__ */
Soby Mathew0d9e8522015-07-15 13:36:24 +0100109
110/*******************************************************************************
111 * ARM standard platform handler called to check the validity of the non secure
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100112 * entrypoint. Returns 0 if the entrypoint is valid, or -1 otherwise.
Soby Mathew0d9e8522015-07-15 13:36:24 +0100113 ******************************************************************************/
114int arm_validate_ns_entrypoint(uintptr_t entrypoint)
115{
116 /*
117 * Check if the non secure entrypoint lies within the non
118 * secure DRAM.
119 */
120 if ((entrypoint >= ARM_NS_DRAM1_BASE) && (entrypoint <
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100121 (ARM_NS_DRAM1_BASE + ARM_NS_DRAM1_SIZE))) {
122 return 0;
123 }
dp-arm84fc2952017-05-03 12:14:10 +0100124#ifndef AARCH32
Soby Mathew0d9e8522015-07-15 13:36:24 +0100125 if ((entrypoint >= ARM_DRAM2_BASE) && (entrypoint <
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100126 (ARM_DRAM2_BASE + ARM_DRAM2_SIZE))) {
127 return 0;
128 }
dp-arm84fc2952017-05-03 12:14:10 +0100129#endif
Soby Mathew0d9e8522015-07-15 13:36:24 +0100130
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100131 return -1;
132}
133
134int arm_validate_psci_entrypoint(uintptr_t entrypoint)
135{
136 return arm_validate_ns_entrypoint(entrypoint) == 0 ? PSCI_E_SUCCESS :
137 PSCI_E_INVALID_ADDRESS;
Soby Mathew0d9e8522015-07-15 13:36:24 +0100138}
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100139
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100140/******************************************************************************
Soby Mathew0b4c5a32016-10-21 17:51:22 +0100141 * Default definition on ARM standard platforms to override the plat_psci_ops.
142 *****************************************************************************/
143const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
144{
145 return ops;
146}
147
148/******************************************************************************
Soby Mathew9ca28062017-10-11 16:08:58 +0100149 * Helper function to save the platform state before a system suspend. Save the
150 * state of the system components which are not in the Always ON power domain.
151 *****************************************************************************/
152void arm_system_pwr_domain_save(void)
153{
154 /* Assert system power domain is available on the platform */
155 assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
156
157 plat_arm_gic_save();
158
159 /*
Antonio Nino Diaz23ede6a2018-06-19 09:29:36 +0100160 * Unregister console now so that it is not registered for a second
161 * time during resume.
162 */
163 arm_console_runtime_end();
164
165 /*
Soby Mathew9ca28062017-10-11 16:08:58 +0100166 * All the other peripheral which are configured by ARM TF are
167 * re-initialized on resume from system suspend. Hence we
168 * don't save their state here.
169 */
170}
171
172/******************************************************************************
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100173 * Helper function to resume the platform from system suspend. Reinitialize
174 * the system components which are not in the Always ON power domain.
175 * TODO: Unify the platform setup when waking up from cold boot and system
176 * resume in arm_bl31_platform_setup().
177 *****************************************************************************/
178void arm_system_pwr_domain_resume(void)
179{
Antonio Nino Diaz23ede6a2018-06-19 09:29:36 +0100180 /* Initialize the console */
181 arm_console_runtime_init();
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100182
183 /* Assert system power domain is available on the platform */
184 assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
185
Soby Mathew9ca28062017-10-11 16:08:58 +0100186 plat_arm_gic_resume();
187
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100188 plat_arm_security_setup();
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100189 arm_configure_sys_timer();
190}
191
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100192/*******************************************************************************
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100193 * ARM platform function to program the mailbox for a cpu before it is released
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100194 * from reset. This function assumes that the Trusted mail box base is within
195 * the ARM_SHARED_RAM region
196 ******************************************************************************/
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100197void plat_arm_program_trusted_mailbox(uintptr_t address)
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100198{
199 uintptr_t *mailbox = (void *) PLAT_ARM_TRUSTED_MAILBOX_BASE;
200
201 *mailbox = address;
202
203 /*
204 * Ensure that the PLAT_ARM_TRUSTED_MAILBOX_BASE is within
205 * ARM_SHARED_RAM region.
206 */
207 assert((PLAT_ARM_TRUSTED_MAILBOX_BASE >= ARM_SHARED_RAM_BASE) &&
208 ((PLAT_ARM_TRUSTED_MAILBOX_BASE + sizeof(*mailbox)) <= \
209 (ARM_SHARED_RAM_BASE + ARM_SHARED_RAM_SIZE)));
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100210}
211
212/*******************************************************************************
213 * The ARM Standard platform definition of platform porting API
214 * `plat_setup_psci_ops`.
215 ******************************************************************************/
216int plat_setup_psci_ops(uintptr_t sec_entrypoint,
217 const plat_psci_ops_t **psci_ops)
218{
Soby Mathew0b4c5a32016-10-21 17:51:22 +0100219 *psci_ops = plat_arm_psci_override_pm_ops(&plat_arm_psci_pm_ops);
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100220
221 /* Setup mailbox with entry point. */
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100222 plat_arm_program_trusted_mailbox(sec_entrypoint);
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100223 return 0;
224}