blob: 055ab361a049e92347b742a5082542aa42afdb3d [file] [log] [blame]
Dan Handley9df48042015-03-19 18:58:55 +00001/*
Zelalem87675d42020-02-03 14:56:42 -06002 * Copyright (c) 2015-2020, 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
Dan Handley9df48042015-03-19 18:58:55 +00007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
Soby Mathewfeac8fc2015-09-29 15:47:16 +01009#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
11#include <arch_helpers.h>
12#include <lib/psci/psci.h>
Antonio Nino Diazbd7b7402019-01-25 14:30:04 +000013#include <plat/arm/common/plat_arm.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <plat/common/platform.h>
15
Dimitris Papastamosd7a36512018-06-18 13:01:06 +010016/* Allow ARM Standard platforms to override these functions */
Dimitris Papastamosd7a36512018-06-18 13:01:06 +010017#pragma weak plat_arm_program_trusted_mailbox
Soby Mathew0b4c5a32016-10-21 17:51:22 +010018
Soby Mathew7799cf72015-04-16 14:49:09 +010019#if !ARM_RECOM_STATE_ID_ENC
Dan Handley9df48042015-03-19 18:58:55 +000020/*******************************************************************************
Soby Mathewfec4eb72015-07-01 16:16:20 +010021 * ARM standard platform handler called to check the validity of the power state
22 * parameter.
Dan Handley9df48042015-03-19 18:58:55 +000023 ******************************************************************************/
Soby Mathewfec4eb72015-07-01 16:16:20 +010024int arm_validate_power_state(unsigned int power_state,
25 psci_power_state_t *req_state)
Dan Handley9df48042015-03-19 18:58:55 +000026{
Antonio Nino Diazfec756f2018-07-18 16:24:16 +010027 unsigned int pstate = psci_get_pstate_type(power_state);
28 unsigned int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
29 unsigned int i;
Dan Handley9df48042015-03-19 18:58:55 +000030
Sathees Balya50905c72018-10-05 13:30:59 +010031 assert(req_state != NULL);
Dan Handley9df48042015-03-19 18:58:55 +000032
Soby Mathewfec4eb72015-07-01 16:16:20 +010033 if (pwr_lvl > PLAT_MAX_PWR_LVL)
34 return PSCI_E_INVALID_PARAMS;
Dan Handley9df48042015-03-19 18:58:55 +000035
Dan Handley9df48042015-03-19 18:58:55 +000036 /* Sanity check the requested state */
Soby Mathewfec4eb72015-07-01 16:16:20 +010037 if (pstate == PSTATE_TYPE_STANDBY) {
Dan Handley9df48042015-03-19 18:58:55 +000038 /*
Soby Mathewfec4eb72015-07-01 16:16:20 +010039 * It's possible to enter standby only on power level 0
40 * Ignore any other power level.
Dan Handley9df48042015-03-19 18:58:55 +000041 */
Soby Mathewfec4eb72015-07-01 16:16:20 +010042 if (pwr_lvl != ARM_PWR_LVL0)
Dan Handley9df48042015-03-19 18:58:55 +000043 return PSCI_E_INVALID_PARAMS;
Soby Mathewfec4eb72015-07-01 16:16:20 +010044
45 req_state->pwr_domain_state[ARM_PWR_LVL0] =
46 ARM_LOCAL_STATE_RET;
47 } else {
48 for (i = ARM_PWR_LVL0; i <= pwr_lvl; i++)
49 req_state->pwr_domain_state[i] =
50 ARM_LOCAL_STATE_OFF;
Dan Handley9df48042015-03-19 18:58:55 +000051 }
52
53 /*
54 * We expect the 'state id' to be zero.
55 */
Antonio Nino Diazfec756f2018-07-18 16:24:16 +010056 if (psci_get_pstate_id(power_state) != 0U)
Dan Handley9df48042015-03-19 18:58:55 +000057 return PSCI_E_INVALID_PARAMS;
58
Soby Mathew7799cf72015-04-16 14:49:09 +010059 return PSCI_E_SUCCESS;
60}
61
62#else
63/*******************************************************************************
64 * ARM standard platform handler called to check the validity of the power
65 * state parameter. The power state parameter has to be a composite power
66 * state.
67 ******************************************************************************/
68int arm_validate_power_state(unsigned int power_state,
69 psci_power_state_t *req_state)
70{
71 unsigned int state_id;
72 int i;
73
Sathees Balya50905c72018-10-05 13:30:59 +010074 assert(req_state != NULL);
Soby Mathew7799cf72015-04-16 14:49:09 +010075
76 /*
77 * Currently we are using a linear search for finding the matching
78 * entry in the idle power state array. This can be made a binary
79 * search if the number of entries justify the additional complexity.
80 */
81 for (i = 0; !!arm_pm_idle_states[i]; i++) {
Wing Li05364b92023-01-26 18:33:43 -080082#if PSCI_OS_INIT_MODE
83 if ((power_state & ~ARM_LAST_AT_PLVL_MASK) ==
84 arm_pm_idle_states[i])
85#else
Soby Mathew7799cf72015-04-16 14:49:09 +010086 if (power_state == arm_pm_idle_states[i])
Wing Li05364b92023-01-26 18:33:43 -080087#endif /* __PSCI_OS_INIT_MODE__ */
Soby Mathew7799cf72015-04-16 14:49:09 +010088 break;
89 }
90
91 /* Return error if entry not found in the idle state array */
92 if (!arm_pm_idle_states[i])
93 return PSCI_E_INVALID_PARAMS;
94
95 i = 0;
96 state_id = psci_get_pstate_id(power_state);
97
98 /* Parse the State ID and populate the state info parameter */
Wing Li05364b92023-01-26 18:33:43 -080099 for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++) {
100 req_state->pwr_domain_state[i] = state_id &
Soby Mathew7799cf72015-04-16 14:49:09 +0100101 ARM_LOCAL_PSTATE_MASK;
102 state_id >>= ARM_LOCAL_PSTATE_WIDTH;
103 }
Wing Li05364b92023-01-26 18:33:43 -0800104#if PSCI_OS_INIT_MODE
105 req_state->last_at_pwrlvl = state_id & ARM_LOCAL_PSTATE_MASK;
106#endif /* __PSCI_OS_INIT_MODE__ */
Soby Mathew7799cf72015-04-16 14:49:09 +0100107
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 }
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700126#ifdef __aarch64__
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{
Sathees Balya50905c72018-10-05 13:30:59 +0100138 return (arm_validate_ns_entrypoint(entrypoint) == 0) ? PSCI_E_SUCCESS :
Jeenu Viswambharan59424d82017-09-19 09:27:18 +0100139 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 Mathew9ca28062017-10-11 16:08:58 +0100143 * Helper function to save the platform state before a system suspend. Save the
144 * state of the system components which are not in the Always ON power domain.
145 *****************************************************************************/
146void arm_system_pwr_domain_save(void)
147{
148 /* Assert system power domain is available on the platform */
149 assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
150
151 plat_arm_gic_save();
152
153 /*
Antonio Nino Diaz23ede6a2018-06-19 09:29:36 +0100154 * Unregister console now so that it is not registered for a second
155 * time during resume.
156 */
157 arm_console_runtime_end();
158
159 /*
Soby Mathew9ca28062017-10-11 16:08:58 +0100160 * All the other peripheral which are configured by ARM TF are
161 * re-initialized on resume from system suspend. Hence we
162 * don't save their state here.
163 */
164}
165
166/******************************************************************************
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100167 * Helper function to resume the platform from system suspend. Reinitialize
168 * the system components which are not in the Always ON power domain.
169 * TODO: Unify the platform setup when waking up from cold boot and system
170 * resume in arm_bl31_platform_setup().
171 *****************************************************************************/
172void arm_system_pwr_domain_resume(void)
173{
Antonio Nino Diaz23ede6a2018-06-19 09:29:36 +0100174 /* Initialize the console */
175 arm_console_runtime_init();
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100176
177 /* Assert system power domain is available on the platform */
178 assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
179
Soby Mathew9ca28062017-10-11 16:08:58 +0100180 plat_arm_gic_resume();
181
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100182 plat_arm_security_setup();
Soby Mathew61e8d0b2015-10-12 17:32:29 +0100183 arm_configure_sys_timer();
184}
185
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100186/*******************************************************************************
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100187 * ARM platform function to program the mailbox for a cpu before it is released
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100188 * from reset. This function assumes that the Trusted mail box base is within
189 * the ARM_SHARED_RAM region
190 ******************************************************************************/
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100191void plat_arm_program_trusted_mailbox(uintptr_t address)
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100192{
193 uintptr_t *mailbox = (void *) PLAT_ARM_TRUSTED_MAILBOX_BASE;
194
195 *mailbox = address;
196
197 /*
198 * Ensure that the PLAT_ARM_TRUSTED_MAILBOX_BASE is within
199 * ARM_SHARED_RAM region.
200 */
201 assert((PLAT_ARM_TRUSTED_MAILBOX_BASE >= ARM_SHARED_RAM_BASE) &&
Elyes Haouas183638f2023-02-13 10:05:41 +0100202 ((PLAT_ARM_TRUSTED_MAILBOX_BASE + sizeof(*mailbox)) <=
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100203 (ARM_SHARED_RAM_BASE + ARM_SHARED_RAM_SIZE)));
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100204}
205
206/*******************************************************************************
207 * The ARM Standard platform definition of platform porting API
208 * `plat_setup_psci_ops`.
209 ******************************************************************************/
Daniel Boulbyf45a4bb2018-09-18 13:26:03 +0100210int __init plat_setup_psci_ops(uintptr_t sec_entrypoint,
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100211 const plat_psci_ops_t **psci_ops)
212{
Soby Mathew0b4c5a32016-10-21 17:51:22 +0100213 *psci_ops = plat_arm_psci_override_pm_ops(&plat_arm_psci_pm_ops);
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100214
215 /* Setup mailbox with entry point. */
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100216 plat_arm_program_trusted_mailbox(sec_entrypoint);
Soby Mathewfeac8fc2015-09-29 15:47:16 +0100217 return 0;
218}