blob: 498dedf32ce7a4f28a19ab507f2c9a4ff0f85efe [file] [log] [blame]
Dan Handleyb4315302015-03-19 18:58:55 +00001/*
Jayanth Dodderi Chidanand0a9c2442024-01-29 15:23:48 +00002 * Copyright (c) 2015-2024, 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
Dan Handleyb4315302015-03-19 18:58:55 +00007#include <assert.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +00008
Soby Mathew785fb922015-09-29 15:47:16 +01009#include <platform_def.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000010
11#include <arch_helpers.h>
12#include <lib/psci/psci.h>
Antonio Nino Diazbd9344f2019-01-25 14:30:04 +000013#include <plat/arm/common/plat_arm.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000014#include <plat/common/platform.h>
15
Dimitris Papastamos2a246d2e2018-06-18 13:01:06 +010016/* Allow ARM Standard platforms to override these functions */
Dimitris Papastamos2a246d2e2018-06-18 13:01:06 +010017#pragma weak plat_arm_program_trusted_mailbox
Soby Mathew5486a962016-10-21 17:51:22 +010018
Soby Mathew2204afd2015-04-16 14:49:09 +010019#if !ARM_RECOM_STATE_ID_ENC
Dan Handleyb4315302015-03-19 18:58:55 +000020/*******************************************************************************
21 * ARM standard platform handler called to check the validity of the power state
22 * parameter.
23 ******************************************************************************/
Soby Mathew38dce702015-07-01 16:16:20 +010024int arm_validate_power_state(unsigned int power_state,
25 psci_power_state_t *req_state)
Dan Handleyb4315302015-03-19 18:58:55 +000026{
Antonio Nino Diaz2bc3dba2018-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;
Soby Mathew38dce702015-07-01 16:16:20 +010030
Sathees Balyae02f4692018-10-05 13:30:59 +010031 assert(req_state != NULL);
Soby Mathew38dce702015-07-01 16:16:20 +010032
33 if (pwr_lvl > PLAT_MAX_PWR_LVL)
34 return PSCI_E_INVALID_PARAMS;
35
Dan Handleyb4315302015-03-19 18:58:55 +000036 /* Sanity check the requested state */
Soby Mathew38dce702015-07-01 16:16:20 +010037 if (pstate == PSTATE_TYPE_STANDBY) {
Dan Handleyb4315302015-03-19 18:58:55 +000038 /*
Soby Mathew38dce702015-07-01 16:16:20 +010039 * It's possible to enter standby only on power level 0
40 * Ignore any other power level.
Dan Handleyb4315302015-03-19 18:58:55 +000041 */
Soby Mathew38dce702015-07-01 16:16:20 +010042 if (pwr_lvl != ARM_PWR_LVL0)
Dan Handleyb4315302015-03-19 18:58:55 +000043 return PSCI_E_INVALID_PARAMS;
Soby Mathew38dce702015-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 Handleyb4315302015-03-19 18:58:55 +000051 }
52
53 /*
54 * We expect the 'state id' to be zero.
55 */
Antonio Nino Diaz2bc3dba2018-07-18 16:24:16 +010056 if (psci_get_pstate_id(power_state) != 0U)
Dan Handleyb4315302015-03-19 18:58:55 +000057 return PSCI_E_INVALID_PARAMS;
58
59 return PSCI_E_SUCCESS;
60}
Soby Mathew2204afd2015-04-16 14:49:09 +010061
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 Balyae02f4692018-10-05 13:30:59 +010074 assert(req_state != NULL);
Soby Mathew2204afd2015-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 Lie75cc242023-01-26 18:33:43 -080082 if ((power_state & ~ARM_LAST_AT_PLVL_MASK) ==
83 arm_pm_idle_states[i])
Soby Mathew2204afd2015-04-16 14:49:09 +010084 break;
85 }
86
87 /* Return error if entry not found in the idle state array */
88 if (!arm_pm_idle_states[i])
89 return PSCI_E_INVALID_PARAMS;
90
91 i = 0;
92 state_id = psci_get_pstate_id(power_state);
93
94 /* Parse the State ID and populate the state info parameter */
Wing Lie75cc242023-01-26 18:33:43 -080095 for (i = ARM_PWR_LVL0; i <= PLAT_MAX_PWR_LVL; i++) {
96 req_state->pwr_domain_state[i] = state_id &
Soby Mathew2204afd2015-04-16 14:49:09 +010097 ARM_LOCAL_PSTATE_MASK;
98 state_id >>= ARM_LOCAL_PSTATE_WIDTH;
99 }
Wing Lie75cc242023-01-26 18:33:43 -0800100#if PSCI_OS_INIT_MODE
101 req_state->last_at_pwrlvl = state_id & ARM_LOCAL_PSTATE_MASK;
102#endif /* __PSCI_OS_INIT_MODE__ */
Soby Mathew2204afd2015-04-16 14:49:09 +0100103
104 return PSCI_E_SUCCESS;
105}
106#endif /* __ARM_RECOM_STATE_ID_ENC__ */
Soby Mathewf9e858b2015-07-15 13:36:24 +0100107
108/*******************************************************************************
109 * ARM standard platform handler called to check the validity of the non secure
Jeenu Viswambharan71e7a4e2017-09-19 09:27:18 +0100110 * entrypoint. Returns 0 if the entrypoint is valid, or -1 otherwise.
Soby Mathewf9e858b2015-07-15 13:36:24 +0100111 ******************************************************************************/
112int arm_validate_ns_entrypoint(uintptr_t entrypoint)
113{
114 /*
115 * Check if the non secure entrypoint lies within the non
116 * secure DRAM.
117 */
118 if ((entrypoint >= ARM_NS_DRAM1_BASE) && (entrypoint <
Jeenu Viswambharan71e7a4e2017-09-19 09:27:18 +0100119 (ARM_NS_DRAM1_BASE + ARM_NS_DRAM1_SIZE))) {
120 return 0;
121 }
Julius Werner402b3cf2019-07-09 14:02:43 -0700122#ifdef __aarch64__
Soby Mathewf9e858b2015-07-15 13:36:24 +0100123 if ((entrypoint >= ARM_DRAM2_BASE) && (entrypoint <
Jeenu Viswambharan71e7a4e2017-09-19 09:27:18 +0100124 (ARM_DRAM2_BASE + ARM_DRAM2_SIZE))) {
125 return 0;
126 }
dp-arm7c7dffd2017-05-03 12:14:10 +0100127#endif
Soby Mathewf9e858b2015-07-15 13:36:24 +0100128
Jeenu Viswambharan71e7a4e2017-09-19 09:27:18 +0100129 return -1;
130}
131
132int arm_validate_psci_entrypoint(uintptr_t entrypoint)
133{
Sathees Balyae02f4692018-10-05 13:30:59 +0100134 return (arm_validate_ns_entrypoint(entrypoint) == 0) ? PSCI_E_SUCCESS :
Jeenu Viswambharan71e7a4e2017-09-19 09:27:18 +0100135 PSCI_E_INVALID_ADDRESS;
Soby Mathewf9e858b2015-07-15 13:36:24 +0100136}
Soby Mathew785fb922015-09-29 15:47:16 +0100137
Soby Mathewc1bb8a02015-10-12 17:32:29 +0100138/******************************************************************************
Soby Mathewe35a3fb2017-10-11 16:08:58 +0100139 * Helper function to save the platform state before a system suspend. Save the
140 * state of the system components which are not in the Always ON power domain.
141 *****************************************************************************/
142void arm_system_pwr_domain_save(void)
143{
144 /* Assert system power domain is available on the platform */
145 assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
146
147 plat_arm_gic_save();
148
149 /*
Antonio Nino Diaz88a05232018-06-19 09:29:36 +0100150 * Unregister console now so that it is not registered for a second
151 * time during resume.
152 */
153 arm_console_runtime_end();
154
155 /*
Soby Mathewe35a3fb2017-10-11 16:08:58 +0100156 * All the other peripheral which are configured by ARM TF are
157 * re-initialized on resume from system suspend. Hence we
158 * don't save their state here.
159 */
160}
161
162/******************************************************************************
Soby Mathewc1bb8a02015-10-12 17:32:29 +0100163 * Helper function to resume the platform from system suspend. Reinitialize
164 * the system components which are not in the Always ON power domain.
165 * TODO: Unify the platform setup when waking up from cold boot and system
166 * resume in arm_bl31_platform_setup().
167 *****************************************************************************/
168void arm_system_pwr_domain_resume(void)
169{
Antonio Nino Diaz88a05232018-06-19 09:29:36 +0100170 /* Initialize the console */
171 arm_console_runtime_init();
Soby Mathewc1bb8a02015-10-12 17:32:29 +0100172
173 /* Assert system power domain is available on the platform */
174 assert(PLAT_MAX_PWR_LVL >= ARM_PWR_LVL2);
175
Soby Mathewe35a3fb2017-10-11 16:08:58 +0100176 plat_arm_gic_resume();
177
Soby Mathewc1bb8a02015-10-12 17:32:29 +0100178 plat_arm_security_setup();
Soby Mathewc1bb8a02015-10-12 17:32:29 +0100179 arm_configure_sys_timer();
180}
181
Soby Mathew785fb922015-09-29 15:47:16 +0100182/*******************************************************************************
Dimitris Papastamos2a246d2e2018-06-18 13:01:06 +0100183 * ARM platform function to program the mailbox for a cpu before it is released
Soby Mathew785fb922015-09-29 15:47:16 +0100184 * from reset. This function assumes that the Trusted mail box base is within
185 * the ARM_SHARED_RAM region
186 ******************************************************************************/
Dimitris Papastamos2a246d2e2018-06-18 13:01:06 +0100187void plat_arm_program_trusted_mailbox(uintptr_t address)
Soby Mathew785fb922015-09-29 15:47:16 +0100188{
189 uintptr_t *mailbox = (void *) PLAT_ARM_TRUSTED_MAILBOX_BASE;
190
191 *mailbox = address;
192
193 /*
194 * Ensure that the PLAT_ARM_TRUSTED_MAILBOX_BASE is within
195 * ARM_SHARED_RAM region.
196 */
197 assert((PLAT_ARM_TRUSTED_MAILBOX_BASE >= ARM_SHARED_RAM_BASE) &&
Elyes Haouas9a90d722023-02-13 10:05:41 +0100198 ((PLAT_ARM_TRUSTED_MAILBOX_BASE + sizeof(*mailbox)) <=
Soby Mathew785fb922015-09-29 15:47:16 +0100199 (ARM_SHARED_RAM_BASE + ARM_SHARED_RAM_SIZE)));
Soby Mathew785fb922015-09-29 15:47:16 +0100200}
201
202/*******************************************************************************
203 * The ARM Standard platform definition of platform porting API
204 * `plat_setup_psci_ops`.
205 ******************************************************************************/
Daniel Boulby4d010d02018-09-18 13:26:03 +0100206int __init plat_setup_psci_ops(uintptr_t sec_entrypoint,
Soby Mathew785fb922015-09-29 15:47:16 +0100207 const plat_psci_ops_t **psci_ops)
208{
Soby Mathew5486a962016-10-21 17:51:22 +0100209 *psci_ops = plat_arm_psci_override_pm_ops(&plat_arm_psci_pm_ops);
Soby Mathew785fb922015-09-29 15:47:16 +0100210
211 /* Setup mailbox with entry point. */
Dimitris Papastamos2a246d2e2018-06-18 13:01:06 +0100212 plat_arm_program_trusted_mailbox(sec_entrypoint);
Soby Mathew785fb922015-09-29 15:47:16 +0100213 return 0;
214}