blob: 1966c44caf53e647bc08e051438b7f99211abe7e [file] [log] [blame]
Soby Mathewea26bad2016-11-14 12:25:45 +00001/*
Roberto Vargas2b36b152018-02-12 12:36:17 +00002 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
Soby Mathewea26bad2016-11-14 12:25:45 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Soby Mathewea26bad2016-11-14 12:25:45 +00007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008#include <string.h>
9
10#include <arch_helpers.h>
11#include <common/debug.h>
Antonio Nino Diaz326f56b2019-01-23 18:55:03 +000012#include <drivers/arm/css/css_scp.h>
Antonio Nino Diazc30db5b2019-01-23 20:37:32 +000013#include <drivers/arm/css/scmi.h>
Antonio Nino Diazbd7b7402019-01-25 14:30:04 +000014#include <plat/arm/common/plat_arm.h>
15#include <plat/arm/css/common/css_pm.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <plat/common/platform.h>
Antonio Nino Diaza320ecd2019-01-15 14:19:50 +000017#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000018
Soby Mathewea26bad2016-11-14 12:25:45 +000019/*
20 * This file implements the SCP helper functions using SCMI protocol.
21 */
22
23/*
24 * SCMI power state parameter bit field encoding for ARM CSS platforms.
25 *
26 * 31 20 19 16 15 12 11 8 7 4 3 0
27 * +-------------------------------------------------------------+
28 * | SBZ | Max level | Level 3 | Level 2 | Level 1 | Level 0 |
29 * | | | state | state | state | state |
30 * +-------------------------------------------------------------+
31 *
32 * `Max level` encodes the highest level that has a valid power state
33 * encoded in the power state.
34 */
35#define SCMI_PWR_STATE_MAX_PWR_LVL_SHIFT 16
36#define SCMI_PWR_STATE_MAX_PWR_LVL_WIDTH 4
37#define SCMI_PWR_STATE_MAX_PWR_LVL_MASK \
38 ((1 << SCMI_PWR_STATE_MAX_PWR_LVL_WIDTH) - 1)
Daniel Boulbyddf6d402018-05-09 12:21:46 +010039#define SCMI_SET_PWR_STATE_MAX_PWR_LVL(_power_state, _max_level) \
40 (_power_state) |= ((_max_level) & SCMI_PWR_STATE_MAX_PWR_LVL_MASK)\
Soby Mathewea26bad2016-11-14 12:25:45 +000041 << SCMI_PWR_STATE_MAX_PWR_LVL_SHIFT
Daniel Boulbyddf6d402018-05-09 12:21:46 +010042#define SCMI_GET_PWR_STATE_MAX_PWR_LVL(_power_state) \
43 (((_power_state) >> SCMI_PWR_STATE_MAX_PWR_LVL_SHIFT) \
Soby Mathewea26bad2016-11-14 12:25:45 +000044 & SCMI_PWR_STATE_MAX_PWR_LVL_MASK)
45
46#define SCMI_PWR_STATE_LVL_WIDTH 4
47#define SCMI_PWR_STATE_LVL_MASK \
48 ((1 << SCMI_PWR_STATE_LVL_WIDTH) - 1)
Daniel Boulbyddf6d402018-05-09 12:21:46 +010049#define SCMI_SET_PWR_STATE_LVL(_power_state, _level, _level_state) \
50 (_power_state) |= ((_level_state) & SCMI_PWR_STATE_LVL_MASK) \
51 << (SCMI_PWR_STATE_LVL_WIDTH * (_level))
52#define SCMI_GET_PWR_STATE_LVL(_power_state, _level) \
53 (((_power_state) >> (SCMI_PWR_STATE_LVL_WIDTH * (_level))) & \
Soby Mathewea26bad2016-11-14 12:25:45 +000054 SCMI_PWR_STATE_LVL_MASK)
55
56/*
57 * The SCMI power state enumeration for a power domain level
58 */
59typedef enum {
60 scmi_power_state_off = 0,
61 scmi_power_state_on = 1,
62 scmi_power_state_sleep = 2,
63} scmi_power_state_t;
64
65/*
Soby Mathewea26bad2016-11-14 12:25:45 +000066 * The global handle for invoking the SCMI driver APIs after the driver
67 * has been initialized.
68 */
Roberto Vargas2b36b152018-02-12 12:36:17 +000069static void *scmi_handle;
Soby Mathewea26bad2016-11-14 12:25:45 +000070
71/* The SCMI channel global object */
Daniel Boulbyebdb6342018-05-14 17:18:58 +010072static scmi_channel_t channel;
Soby Mathewea26bad2016-11-14 12:25:45 +000073
Roberto Vargas00996942017-11-13 13:41:58 +000074ARM_SCMI_INSTANTIATE_LOCK;
Soby Mathewea26bad2016-11-14 12:25:45 +000075
76/*
77 * Helper function to suspend a CPU power domain and its parent power domains
78 * if applicable.
79 */
Roberto Vargas5f5a5e62018-02-12 12:36:17 +000080void css_scp_suspend(const struct psci_power_state *target_state)
Soby Mathewea26bad2016-11-14 12:25:45 +000081{
Deepak Pandey207c5222017-10-10 21:34:32 +053082 int ret;
Soby Mathewea26bad2016-11-14 12:25:45 +000083
84 /* At least power domain level 0 should be specified to be suspended */
85 assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
86 ARM_LOCAL_STATE_OFF);
87
88 /* Check if power down at system power domain level is requested */
Nariman Poushincd956262018-05-01 09:28:40 +010089 if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF) {
Soby Mathewea26bad2016-11-14 12:25:45 +000090 /* Issue SCMI command for SYSTEM_SUSPEND */
91 ret = scmi_sys_pwr_state_set(scmi_handle,
92 SCMI_SYS_PWR_FORCEFUL_REQ,
93 SCMI_SYS_PWR_SUSPEND);
94 if (ret != SCMI_E_SUCCESS) {
95 ERROR("SCMI system power domain suspend return 0x%x unexpected\n",
96 ret);
97 panic();
98 }
99 return;
100 }
Deepak Pandey207c5222017-10-10 21:34:32 +0530101#if !HW_ASSISTED_COHERENCY
102 int lvl;
103 uint32_t scmi_pwr_state = 0;
Soby Mathewea26bad2016-11-14 12:25:45 +0000104 /*
105 * If we reach here, then assert that power down at system power domain
106 * level is running.
107 */
Soby Mathewfd2e5e42018-09-10 11:32:49 +0100108 assert(css_system_pwr_state(target_state) == ARM_LOCAL_STATE_RUN);
Soby Mathewea26bad2016-11-14 12:25:45 +0000109
110 /* For level 0, specify `scmi_power_state_sleep` as the power state */
111 SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, ARM_PWR_LVL0,
112 scmi_power_state_sleep);
113
114 for (lvl = ARM_PWR_LVL1; lvl <= PLAT_MAX_PWR_LVL; lvl++) {
115 if (target_state->pwr_domain_state[lvl] == ARM_LOCAL_STATE_RUN)
116 break;
117
118 assert(target_state->pwr_domain_state[lvl] ==
119 ARM_LOCAL_STATE_OFF);
120 /*
121 * Specify `scmi_power_state_off` as power state for higher
122 * levels.
123 */
124 SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, lvl,
125 scmi_power_state_off);
126 }
127
128 SCMI_SET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state, lvl - 1);
129
130 ret = scmi_pwr_state_set(scmi_handle,
131 plat_css_core_pos_to_scmi_dmn_id_map[plat_my_core_pos()],
132 scmi_pwr_state);
133
134 if (ret != SCMI_E_SUCCESS) {
135 ERROR("SCMI set power state command return 0x%x unexpected\n",
136 ret);
137 panic();
138 }
Deepak Pandey207c5222017-10-10 21:34:32 +0530139#endif
Soby Mathewea26bad2016-11-14 12:25:45 +0000140}
141
142/*
143 * Helper function to turn off a CPU power domain and its parent power domains
144 * if applicable.
145 */
Roberto Vargas85664f52018-02-12 12:36:17 +0000146void css_scp_off(const struct psci_power_state *target_state)
Soby Mathewea26bad2016-11-14 12:25:45 +0000147{
148 int lvl = 0, ret;
149 uint32_t scmi_pwr_state = 0;
150
151 /* At-least the CPU level should be specified to be OFF */
152 assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
153 ARM_LOCAL_STATE_OFF);
154
155 /* PSCI CPU OFF cannot be used to turn OFF system power domain */
Soby Mathewfd2e5e42018-09-10 11:32:49 +0100156 assert(css_system_pwr_state(target_state) == ARM_LOCAL_STATE_RUN);
Soby Mathewea26bad2016-11-14 12:25:45 +0000157
158 for (; lvl <= PLAT_MAX_PWR_LVL; lvl++) {
159 if (target_state->pwr_domain_state[lvl] == ARM_LOCAL_STATE_RUN)
160 break;
161
162 assert(target_state->pwr_domain_state[lvl] ==
163 ARM_LOCAL_STATE_OFF);
164 SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, lvl,
165 scmi_power_state_off);
166 }
167
168 SCMI_SET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state, lvl - 1);
169
170 ret = scmi_pwr_state_set(scmi_handle,
171 plat_css_core_pos_to_scmi_dmn_id_map[plat_my_core_pos()],
172 scmi_pwr_state);
173
174 if (ret != SCMI_E_QUEUED && ret != SCMI_E_SUCCESS) {
175 ERROR("SCMI set power state command return 0x%x unexpected\n",
176 ret);
177 panic();
178 }
179}
180
181/*
182 * Helper function to turn ON a CPU power domain and its parent power domains
183 * if applicable.
184 */
185void css_scp_on(u_register_t mpidr)
186{
Soby Mathewe089e3f2017-06-09 15:04:43 +0100187 int lvl = 0, ret, core_pos;
Soby Mathewea26bad2016-11-14 12:25:45 +0000188 uint32_t scmi_pwr_state = 0;
189
190 for (; lvl <= PLAT_MAX_PWR_LVL; lvl++)
191 SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, lvl,
192 scmi_power_state_on);
193
194 SCMI_SET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state, lvl - 1);
195
Soby Mathewe089e3f2017-06-09 15:04:43 +0100196 core_pos = plat_core_pos_by_mpidr(mpidr);
197 assert(core_pos >= 0 && core_pos < PLATFORM_CORE_COUNT);
198
Soby Mathewea26bad2016-11-14 12:25:45 +0000199 ret = scmi_pwr_state_set(scmi_handle,
Soby Mathewe089e3f2017-06-09 15:04:43 +0100200 plat_css_core_pos_to_scmi_dmn_id_map[core_pos],
Soby Mathewea26bad2016-11-14 12:25:45 +0000201 scmi_pwr_state);
202
203 if (ret != SCMI_E_QUEUED && ret != SCMI_E_SUCCESS) {
204 ERROR("SCMI set power state command return 0x%x unexpected\n",
205 ret);
206 panic();
207 }
208}
209
210/*
211 * Helper function to get the power state of a power domain node as reported
212 * by the SCP.
213 */
214int css_scp_get_power_state(u_register_t mpidr, unsigned int power_level)
215{
216 int ret, cpu_idx;
217 uint32_t scmi_pwr_state = 0, lvl_state;
218
219 /* We don't support get power state at the system power domain level */
220 if ((power_level > PLAT_MAX_PWR_LVL) ||
221 (power_level == CSS_SYSTEM_PWR_DMN_LVL)) {
222 WARN("Invalid power level %u specified for SCMI get power state\n",
223 power_level);
224 return PSCI_E_INVALID_PARAMS;
225 }
226
227 cpu_idx = plat_core_pos_by_mpidr(mpidr);
228 assert(cpu_idx > -1);
229
230 ret = scmi_pwr_state_get(scmi_handle,
231 plat_css_core_pos_to_scmi_dmn_id_map[cpu_idx],
232 &scmi_pwr_state);
233
234 if (ret != SCMI_E_SUCCESS) {
235 WARN("SCMI get power state command return 0x%x unexpected\n",
236 ret);
237 return PSCI_E_INVALID_PARAMS;
238 }
239
240 /*
241 * Find the maximum power level described in the get power state
242 * command. If it is less than the requested power level, then assume
243 * the requested power level is ON.
244 */
245 if (SCMI_GET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state) < power_level)
246 return HW_ON;
247
248 lvl_state = SCMI_GET_PWR_STATE_LVL(scmi_pwr_state, power_level);
249 if (lvl_state == scmi_power_state_on)
250 return HW_ON;
251
252 assert((lvl_state == scmi_power_state_off) ||
253 (lvl_state == scmi_power_state_sleep));
254 return HW_OFF;
255}
256
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100257void __dead2 css_scp_system_off(int state)
Soby Mathewea26bad2016-11-14 12:25:45 +0000258{
259 int ret;
260
261 /*
262 * Disable GIC CPU interface to prevent pending interrupt from waking
263 * up the AP from WFI.
264 */
265 plat_arm_gic_cpuif_disable();
266
267 /*
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100268 * Issue SCMI command. First issue a graceful
Soby Mathewea26bad2016-11-14 12:25:45 +0000269 * request and if that fails force the request.
270 */
271 ret = scmi_sys_pwr_state_set(scmi_handle,
272 SCMI_SYS_PWR_FORCEFUL_REQ,
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100273 state);
274
Soby Mathewea26bad2016-11-14 12:25:45 +0000275 if (ret != SCMI_E_SUCCESS) {
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100276 ERROR("SCMI system power state set 0x%x returns unexpected 0x%x\n",
277 state, ret);
Soby Mathewea26bad2016-11-14 12:25:45 +0000278 panic();
279 }
Soby Mathewea26bad2016-11-14 12:25:45 +0000280 wfi();
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100281 ERROR("CSS set power state: operation not handled.\n");
Soby Mathewea26bad2016-11-14 12:25:45 +0000282 panic();
283}
284
285/*
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100286 * Helper function to shutdown the system via SCMI.
287 */
288void __dead2 css_scp_sys_shutdown(void)
289{
290 css_scp_system_off(SCMI_SYS_PWR_SHUTDOWN);
291}
292
293/*
Soby Mathewea26bad2016-11-14 12:25:45 +0000294 * Helper function to reset the system via SCMI.
295 */
296void __dead2 css_scp_sys_reboot(void)
297{
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100298 css_scp_system_off(SCMI_SYS_PWR_COLD_RESET);
Soby Mathewea26bad2016-11-14 12:25:45 +0000299}
300
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100301static int scmi_ap_core_init(scmi_channel_t *ch)
302{
303#if PROGRAMMABLE_RESET_ADDRESS
304 uint32_t version;
305 int ret;
306
307 ret = scmi_proto_version(ch, SCMI_AP_CORE_PROTO_ID, &version);
308 if (ret != SCMI_E_SUCCESS) {
309 WARN("SCMI AP core protocol version message failed\n");
310 return -1;
311 }
312
313 if (!is_scmi_version_compatible(SCMI_AP_CORE_PROTO_VER, version)) {
314 WARN("SCMI AP core protocol version 0x%x incompatible with driver version 0x%x\n",
315 version, SCMI_AP_CORE_PROTO_VER);
316 return -1;
317 }
318 INFO("SCMI AP core protocol version 0x%x detected\n", version);
319#endif
320 return 0;
321}
322
Daniel Boulbyf45a4bb2018-09-18 13:26:03 +0100323void __init plat_arm_pwrc_setup(void)
Soby Mathewea26bad2016-11-14 12:25:45 +0000324{
Chandni Cherukuri61f3a7c2018-10-11 14:08:08 +0530325 channel.info = plat_css_get_scmi_info();
Roberto Vargas00996942017-11-13 13:41:58 +0000326 channel.lock = ARM_SCMI_LOCK_GET_INSTANCE;
Daniel Boulbyebdb6342018-05-14 17:18:58 +0100327 scmi_handle = scmi_init(&channel);
Soby Mathewea26bad2016-11-14 12:25:45 +0000328 if (scmi_handle == NULL) {
329 ERROR("SCMI Initialization failed\n");
330 panic();
331 }
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100332 if (scmi_ap_core_init(&channel) < 0) {
333 ERROR("SCMI AP core protocol initialization failed\n");
334 panic();
335 }
Soby Mathewea26bad2016-11-14 12:25:45 +0000336}
337
338/******************************************************************************
339 * This function overrides the default definition for ARM platforms. Initialize
340 * the SCMI driver, query capability via SCMI and modify the PSCI capability
341 * based on that.
342 *****************************************************************************/
Chandni Cherukurie4bf6a02018-11-14 13:43:59 +0530343const plat_psci_ops_t *css_scmi_override_pm_ops(plat_psci_ops_t *ops)
Soby Mathewea26bad2016-11-14 12:25:45 +0000344{
345 uint32_t msg_attr;
346 int ret;
347
348 assert(scmi_handle);
349
350 /* Check that power domain POWER_STATE_SET message is supported */
351 ret = scmi_proto_msg_attr(scmi_handle, SCMI_PWR_DMN_PROTO_ID,
352 SCMI_PWR_STATE_SET_MSG, &msg_attr);
353 if (ret != SCMI_E_SUCCESS) {
354 ERROR("Set power state command is not supported by SCMI\n");
355 panic();
356 }
357
358 /*
359 * Don't support PSCI NODE_HW_STATE call if SCMI doesn't support
360 * POWER_STATE_GET message.
361 */
362 ret = scmi_proto_msg_attr(scmi_handle, SCMI_PWR_DMN_PROTO_ID,
363 SCMI_PWR_STATE_GET_MSG, &msg_attr);
364 if (ret != SCMI_E_SUCCESS)
365 ops->get_node_hw_state = NULL;
366
367 /* Check if the SCMI SYSTEM_POWER_STATE_SET message is supported */
368 ret = scmi_proto_msg_attr(scmi_handle, SCMI_SYS_PWR_PROTO_ID,
369 SCMI_SYS_PWR_STATE_SET_MSG, &msg_attr);
370 if (ret != SCMI_E_SUCCESS) {
371 /* System power management operations are not supported */
372 ops->system_off = NULL;
373 ops->system_reset = NULL;
374 ops->get_sys_suspend_power_state = NULL;
Roberto Vargas3caafd72017-08-16 08:57:45 +0100375 } else {
376 if (!(msg_attr & SCMI_SYS_PWR_SUSPEND_SUPPORTED)) {
377 /*
378 * System power management protocol is available, but
379 * it does not support SYSTEM SUSPEND.
380 */
381 ops->get_sys_suspend_power_state = NULL;
382 }
383 if (!(msg_attr & SCMI_SYS_PWR_WARM_RESET_SUPPORTED)) {
384 /*
385 * WARM reset is not available.
386 */
387 ops->system_reset2 = NULL;
388 }
Soby Mathewea26bad2016-11-14 12:25:45 +0000389 }
390
391 return ops;
392}
Roberto Vargas3caafd72017-08-16 08:57:45 +0100393
394int css_system_reset2(int is_vendor, int reset_type, u_register_t cookie)
395{
396 if (is_vendor || (reset_type != PSCI_RESET2_SYSTEM_WARM_RESET))
397 return PSCI_E_INVALID_PARAMS;
398
399 css_scp_system_off(SCMI_SYS_PWR_WARM_RESET);
400 /*
401 * css_scp_system_off cannot return (it is a __dead function),
402 * but css_system_reset2 has to return some value, even in
403 * this case.
404 */
405 return 0;
406}
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100407
408#if PROGRAMMABLE_RESET_ADDRESS
409void plat_arm_program_trusted_mailbox(uintptr_t address)
410{
411 int ret;
412
413 assert(scmi_handle);
414 ret = scmi_ap_core_set_reset_addr(scmi_handle, address,
415 SCMI_AP_CORE_LOCK_ATTR);
416 if (ret != SCMI_E_SUCCESS) {
417 ERROR("CSS: Failed to program reset address: %d\n", ret);
418 panic();
419 }
420}
421#endif