blob: 956f583c0fb5ce10c07674315f9e4eba39d1466e [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
7#include <arch_helpers.h>
8#include <assert.h>
9#include <css_def.h>
10#include <css_pm.h>
11#include <debug.h>
12#include <plat_arm.h>
13#include <platform.h>
14#include <string.h>
15#include "../scmi/scmi.h"
16#include "css_scp.h"
17
18/*
19 * This file implements the SCP helper functions using SCMI protocol.
20 */
21
22/*
23 * SCMI power state parameter bit field encoding for ARM CSS platforms.
24 *
25 * 31 20 19 16 15 12 11 8 7 4 3 0
26 * +-------------------------------------------------------------+
27 * | SBZ | Max level | Level 3 | Level 2 | Level 1 | Level 0 |
28 * | | | state | state | state | state |
29 * +-------------------------------------------------------------+
30 *
31 * `Max level` encodes the highest level that has a valid power state
32 * encoded in the power state.
33 */
34#define SCMI_PWR_STATE_MAX_PWR_LVL_SHIFT 16
35#define SCMI_PWR_STATE_MAX_PWR_LVL_WIDTH 4
36#define SCMI_PWR_STATE_MAX_PWR_LVL_MASK \
37 ((1 << SCMI_PWR_STATE_MAX_PWR_LVL_WIDTH) - 1)
Daniel Boulbyddf6d402018-05-09 12:21:46 +010038#define SCMI_SET_PWR_STATE_MAX_PWR_LVL(_power_state, _max_level) \
39 (_power_state) |= ((_max_level) & SCMI_PWR_STATE_MAX_PWR_LVL_MASK)\
Soby Mathewea26bad2016-11-14 12:25:45 +000040 << SCMI_PWR_STATE_MAX_PWR_LVL_SHIFT
Daniel Boulbyddf6d402018-05-09 12:21:46 +010041#define SCMI_GET_PWR_STATE_MAX_PWR_LVL(_power_state) \
42 (((_power_state) >> SCMI_PWR_STATE_MAX_PWR_LVL_SHIFT) \
Soby Mathewea26bad2016-11-14 12:25:45 +000043 & SCMI_PWR_STATE_MAX_PWR_LVL_MASK)
44
45#define SCMI_PWR_STATE_LVL_WIDTH 4
46#define SCMI_PWR_STATE_LVL_MASK \
47 ((1 << SCMI_PWR_STATE_LVL_WIDTH) - 1)
Daniel Boulbyddf6d402018-05-09 12:21:46 +010048#define SCMI_SET_PWR_STATE_LVL(_power_state, _level, _level_state) \
49 (_power_state) |= ((_level_state) & SCMI_PWR_STATE_LVL_MASK) \
50 << (SCMI_PWR_STATE_LVL_WIDTH * (_level))
51#define SCMI_GET_PWR_STATE_LVL(_power_state, _level) \
52 (((_power_state) >> (SCMI_PWR_STATE_LVL_WIDTH * (_level))) & \
Soby Mathewea26bad2016-11-14 12:25:45 +000053 SCMI_PWR_STATE_LVL_MASK)
54
55/*
56 * The SCMI power state enumeration for a power domain level
57 */
58typedef enum {
59 scmi_power_state_off = 0,
60 scmi_power_state_on = 1,
61 scmi_power_state_sleep = 2,
62} scmi_power_state_t;
63
64/*
Soby Mathewea26bad2016-11-14 12:25:45 +000065 * The global handle for invoking the SCMI driver APIs after the driver
66 * has been initialized.
67 */
Roberto Vargas2b36b152018-02-12 12:36:17 +000068static void *scmi_handle;
Soby Mathewea26bad2016-11-14 12:25:45 +000069
70/* The SCMI channel global object */
Daniel Boulbyebdb6342018-05-14 17:18:58 +010071static scmi_channel_t channel;
Soby Mathewea26bad2016-11-14 12:25:45 +000072
Roberto Vargas00996942017-11-13 13:41:58 +000073ARM_SCMI_INSTANTIATE_LOCK;
Soby Mathewea26bad2016-11-14 12:25:45 +000074
75/*
76 * Helper function to suspend a CPU power domain and its parent power domains
77 * if applicable.
78 */
Roberto Vargas5f5a5e62018-02-12 12:36:17 +000079void css_scp_suspend(const struct psci_power_state *target_state)
Soby Mathewea26bad2016-11-14 12:25:45 +000080{
Deepak Pandey207c5222017-10-10 21:34:32 +053081 int ret;
Soby Mathewea26bad2016-11-14 12:25:45 +000082
83 /* At least power domain level 0 should be specified to be suspended */
84 assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
85 ARM_LOCAL_STATE_OFF);
86
87 /* Check if power down at system power domain level is requested */
Nariman Poushincd956262018-05-01 09:28:40 +010088 if (css_system_pwr_state(target_state) == ARM_LOCAL_STATE_OFF) {
Soby Mathewea26bad2016-11-14 12:25:45 +000089 /* Issue SCMI command for SYSTEM_SUSPEND */
90 ret = scmi_sys_pwr_state_set(scmi_handle,
91 SCMI_SYS_PWR_FORCEFUL_REQ,
92 SCMI_SYS_PWR_SUSPEND);
93 if (ret != SCMI_E_SUCCESS) {
94 ERROR("SCMI system power domain suspend return 0x%x unexpected\n",
95 ret);
96 panic();
97 }
98 return;
99 }
Deepak Pandey207c5222017-10-10 21:34:32 +0530100#if !HW_ASSISTED_COHERENCY
101 int lvl;
102 uint32_t scmi_pwr_state = 0;
Soby Mathewea26bad2016-11-14 12:25:45 +0000103 /*
104 * If we reach here, then assert that power down at system power domain
105 * level is running.
106 */
Soby Mathewfd2e5e42018-09-10 11:32:49 +0100107 assert(css_system_pwr_state(target_state) == ARM_LOCAL_STATE_RUN);
Soby Mathewea26bad2016-11-14 12:25:45 +0000108
109 /* For level 0, specify `scmi_power_state_sleep` as the power state */
110 SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, ARM_PWR_LVL0,
111 scmi_power_state_sleep);
112
113 for (lvl = ARM_PWR_LVL1; lvl <= PLAT_MAX_PWR_LVL; lvl++) {
114 if (target_state->pwr_domain_state[lvl] == ARM_LOCAL_STATE_RUN)
115 break;
116
117 assert(target_state->pwr_domain_state[lvl] ==
118 ARM_LOCAL_STATE_OFF);
119 /*
120 * Specify `scmi_power_state_off` as power state for higher
121 * levels.
122 */
123 SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, lvl,
124 scmi_power_state_off);
125 }
126
127 SCMI_SET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state, lvl - 1);
128
129 ret = scmi_pwr_state_set(scmi_handle,
130 plat_css_core_pos_to_scmi_dmn_id_map[plat_my_core_pos()],
131 scmi_pwr_state);
132
133 if (ret != SCMI_E_SUCCESS) {
134 ERROR("SCMI set power state command return 0x%x unexpected\n",
135 ret);
136 panic();
137 }
Deepak Pandey207c5222017-10-10 21:34:32 +0530138#endif
Soby Mathewea26bad2016-11-14 12:25:45 +0000139}
140
141/*
142 * Helper function to turn off a CPU power domain and its parent power domains
143 * if applicable.
144 */
Roberto Vargas85664f52018-02-12 12:36:17 +0000145void css_scp_off(const struct psci_power_state *target_state)
Soby Mathewea26bad2016-11-14 12:25:45 +0000146{
147 int lvl = 0, ret;
148 uint32_t scmi_pwr_state = 0;
149
150 /* At-least the CPU level should be specified to be OFF */
151 assert(target_state->pwr_domain_state[ARM_PWR_LVL0] ==
152 ARM_LOCAL_STATE_OFF);
153
154 /* PSCI CPU OFF cannot be used to turn OFF system power domain */
Soby Mathewfd2e5e42018-09-10 11:32:49 +0100155 assert(css_system_pwr_state(target_state) == ARM_LOCAL_STATE_RUN);
Soby Mathewea26bad2016-11-14 12:25:45 +0000156
157 for (; lvl <= PLAT_MAX_PWR_LVL; lvl++) {
158 if (target_state->pwr_domain_state[lvl] == ARM_LOCAL_STATE_RUN)
159 break;
160
161 assert(target_state->pwr_domain_state[lvl] ==
162 ARM_LOCAL_STATE_OFF);
163 SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, lvl,
164 scmi_power_state_off);
165 }
166
167 SCMI_SET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state, lvl - 1);
168
169 ret = scmi_pwr_state_set(scmi_handle,
170 plat_css_core_pos_to_scmi_dmn_id_map[plat_my_core_pos()],
171 scmi_pwr_state);
172
173 if (ret != SCMI_E_QUEUED && ret != SCMI_E_SUCCESS) {
174 ERROR("SCMI set power state command return 0x%x unexpected\n",
175 ret);
176 panic();
177 }
178}
179
180/*
181 * Helper function to turn ON a CPU power domain and its parent power domains
182 * if applicable.
183 */
184void css_scp_on(u_register_t mpidr)
185{
Soby Mathewe089e3f2017-06-09 15:04:43 +0100186 int lvl = 0, ret, core_pos;
Soby Mathewea26bad2016-11-14 12:25:45 +0000187 uint32_t scmi_pwr_state = 0;
188
189 for (; lvl <= PLAT_MAX_PWR_LVL; lvl++)
190 SCMI_SET_PWR_STATE_LVL(scmi_pwr_state, lvl,
191 scmi_power_state_on);
192
193 SCMI_SET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state, lvl - 1);
194
Soby Mathewe089e3f2017-06-09 15:04:43 +0100195 core_pos = plat_core_pos_by_mpidr(mpidr);
196 assert(core_pos >= 0 && core_pos < PLATFORM_CORE_COUNT);
197
Soby Mathewea26bad2016-11-14 12:25:45 +0000198 ret = scmi_pwr_state_set(scmi_handle,
Soby Mathewe089e3f2017-06-09 15:04:43 +0100199 plat_css_core_pos_to_scmi_dmn_id_map[core_pos],
Soby Mathewea26bad2016-11-14 12:25:45 +0000200 scmi_pwr_state);
201
202 if (ret != SCMI_E_QUEUED && ret != SCMI_E_SUCCESS) {
203 ERROR("SCMI set power state command return 0x%x unexpected\n",
204 ret);
205 panic();
206 }
207}
208
209/*
210 * Helper function to get the power state of a power domain node as reported
211 * by the SCP.
212 */
213int css_scp_get_power_state(u_register_t mpidr, unsigned int power_level)
214{
215 int ret, cpu_idx;
216 uint32_t scmi_pwr_state = 0, lvl_state;
217
218 /* We don't support get power state at the system power domain level */
219 if ((power_level > PLAT_MAX_PWR_LVL) ||
220 (power_level == CSS_SYSTEM_PWR_DMN_LVL)) {
221 WARN("Invalid power level %u specified for SCMI get power state\n",
222 power_level);
223 return PSCI_E_INVALID_PARAMS;
224 }
225
226 cpu_idx = plat_core_pos_by_mpidr(mpidr);
227 assert(cpu_idx > -1);
228
229 ret = scmi_pwr_state_get(scmi_handle,
230 plat_css_core_pos_to_scmi_dmn_id_map[cpu_idx],
231 &scmi_pwr_state);
232
233 if (ret != SCMI_E_SUCCESS) {
234 WARN("SCMI get power state command return 0x%x unexpected\n",
235 ret);
236 return PSCI_E_INVALID_PARAMS;
237 }
238
239 /*
240 * Find the maximum power level described in the get power state
241 * command. If it is less than the requested power level, then assume
242 * the requested power level is ON.
243 */
244 if (SCMI_GET_PWR_STATE_MAX_PWR_LVL(scmi_pwr_state) < power_level)
245 return HW_ON;
246
247 lvl_state = SCMI_GET_PWR_STATE_LVL(scmi_pwr_state, power_level);
248 if (lvl_state == scmi_power_state_on)
249 return HW_ON;
250
251 assert((lvl_state == scmi_power_state_off) ||
252 (lvl_state == scmi_power_state_sleep));
253 return HW_OFF;
254}
255
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100256void __dead2 css_scp_system_off(int state)
Soby Mathewea26bad2016-11-14 12:25:45 +0000257{
258 int ret;
259
260 /*
261 * Disable GIC CPU interface to prevent pending interrupt from waking
262 * up the AP from WFI.
263 */
264 plat_arm_gic_cpuif_disable();
265
266 /*
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100267 * Issue SCMI command. First issue a graceful
Soby Mathewea26bad2016-11-14 12:25:45 +0000268 * request and if that fails force the request.
269 */
270 ret = scmi_sys_pwr_state_set(scmi_handle,
271 SCMI_SYS_PWR_FORCEFUL_REQ,
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100272 state);
273
Soby Mathewea26bad2016-11-14 12:25:45 +0000274 if (ret != SCMI_E_SUCCESS) {
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100275 ERROR("SCMI system power state set 0x%x returns unexpected 0x%x\n",
276 state, ret);
Soby Mathewea26bad2016-11-14 12:25:45 +0000277 panic();
278 }
Soby Mathewea26bad2016-11-14 12:25:45 +0000279 wfi();
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100280 ERROR("CSS set power state: operation not handled.\n");
Soby Mathewea26bad2016-11-14 12:25:45 +0000281 panic();
282}
283
284/*
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100285 * Helper function to shutdown the system via SCMI.
286 */
287void __dead2 css_scp_sys_shutdown(void)
288{
289 css_scp_system_off(SCMI_SYS_PWR_SHUTDOWN);
290}
291
292/*
Soby Mathewea26bad2016-11-14 12:25:45 +0000293 * Helper function to reset the system via SCMI.
294 */
295void __dead2 css_scp_sys_reboot(void)
296{
Roberto Vargasfc2b4eb2017-07-31 09:45:10 +0100297 css_scp_system_off(SCMI_SYS_PWR_COLD_RESET);
Soby Mathewea26bad2016-11-14 12:25:45 +0000298}
299
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100300static int scmi_ap_core_init(scmi_channel_t *ch)
301{
302#if PROGRAMMABLE_RESET_ADDRESS
303 uint32_t version;
304 int ret;
305
306 ret = scmi_proto_version(ch, SCMI_AP_CORE_PROTO_ID, &version);
307 if (ret != SCMI_E_SUCCESS) {
308 WARN("SCMI AP core protocol version message failed\n");
309 return -1;
310 }
311
312 if (!is_scmi_version_compatible(SCMI_AP_CORE_PROTO_VER, version)) {
313 WARN("SCMI AP core protocol version 0x%x incompatible with driver version 0x%x\n",
314 version, SCMI_AP_CORE_PROTO_VER);
315 return -1;
316 }
317 INFO("SCMI AP core protocol version 0x%x detected\n", version);
318#endif
319 return 0;
320}
321
Daniel Boulbyf45a4bb2018-09-18 13:26:03 +0100322void __init plat_arm_pwrc_setup(void)
Soby Mathewea26bad2016-11-14 12:25:45 +0000323{
Chandni Cherukuri61f3a7c2018-10-11 14:08:08 +0530324 channel.info = plat_css_get_scmi_info();
Roberto Vargas00996942017-11-13 13:41:58 +0000325 channel.lock = ARM_SCMI_LOCK_GET_INSTANCE;
Daniel Boulbyebdb6342018-05-14 17:18:58 +0100326 scmi_handle = scmi_init(&channel);
Soby Mathewea26bad2016-11-14 12:25:45 +0000327 if (scmi_handle == NULL) {
328 ERROR("SCMI Initialization failed\n");
329 panic();
330 }
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100331 if (scmi_ap_core_init(&channel) < 0) {
332 ERROR("SCMI AP core protocol initialization failed\n");
333 panic();
334 }
Soby Mathewea26bad2016-11-14 12:25:45 +0000335}
336
337/******************************************************************************
338 * This function overrides the default definition for ARM platforms. Initialize
339 * the SCMI driver, query capability via SCMI and modify the PSCI capability
340 * based on that.
341 *****************************************************************************/
342const plat_psci_ops_t *plat_arm_psci_override_pm_ops(plat_psci_ops_t *ops)
343{
344 uint32_t msg_attr;
345 int ret;
346
347 assert(scmi_handle);
348
349 /* Check that power domain POWER_STATE_SET message is supported */
350 ret = scmi_proto_msg_attr(scmi_handle, SCMI_PWR_DMN_PROTO_ID,
351 SCMI_PWR_STATE_SET_MSG, &msg_attr);
352 if (ret != SCMI_E_SUCCESS) {
353 ERROR("Set power state command is not supported by SCMI\n");
354 panic();
355 }
356
357 /*
358 * Don't support PSCI NODE_HW_STATE call if SCMI doesn't support
359 * POWER_STATE_GET message.
360 */
361 ret = scmi_proto_msg_attr(scmi_handle, SCMI_PWR_DMN_PROTO_ID,
362 SCMI_PWR_STATE_GET_MSG, &msg_attr);
363 if (ret != SCMI_E_SUCCESS)
364 ops->get_node_hw_state = NULL;
365
366 /* Check if the SCMI SYSTEM_POWER_STATE_SET message is supported */
367 ret = scmi_proto_msg_attr(scmi_handle, SCMI_SYS_PWR_PROTO_ID,
368 SCMI_SYS_PWR_STATE_SET_MSG, &msg_attr);
369 if (ret != SCMI_E_SUCCESS) {
370 /* System power management operations are not supported */
371 ops->system_off = NULL;
372 ops->system_reset = NULL;
373 ops->get_sys_suspend_power_state = NULL;
Roberto Vargas3caafd72017-08-16 08:57:45 +0100374 } else {
375 if (!(msg_attr & SCMI_SYS_PWR_SUSPEND_SUPPORTED)) {
376 /*
377 * System power management protocol is available, but
378 * it does not support SYSTEM SUSPEND.
379 */
380 ops->get_sys_suspend_power_state = NULL;
381 }
382 if (!(msg_attr & SCMI_SYS_PWR_WARM_RESET_SUPPORTED)) {
383 /*
384 * WARM reset is not available.
385 */
386 ops->system_reset2 = NULL;
387 }
Soby Mathewea26bad2016-11-14 12:25:45 +0000388 }
389
390 return ops;
391}
Roberto Vargas3caafd72017-08-16 08:57:45 +0100392
393int css_system_reset2(int is_vendor, int reset_type, u_register_t cookie)
394{
395 if (is_vendor || (reset_type != PSCI_RESET2_SYSTEM_WARM_RESET))
396 return PSCI_E_INVALID_PARAMS;
397
398 css_scp_system_off(SCMI_SYS_PWR_WARM_RESET);
399 /*
400 * css_scp_system_off cannot return (it is a __dead function),
401 * but css_system_reset2 has to return some value, even in
402 * this case.
403 */
404 return 0;
405}
Dimitris Papastamosd7a36512018-06-18 13:01:06 +0100406
407#if PROGRAMMABLE_RESET_ADDRESS
408void plat_arm_program_trusted_mailbox(uintptr_t address)
409{
410 int ret;
411
412 assert(scmi_handle);
413 ret = scmi_ap_core_set_reset_addr(scmi_handle, address,
414 SCMI_AP_CORE_LOCK_ATTR);
415 if (ret != SCMI_E_SUCCESS) {
416 ERROR("CSS: Failed to program reset address: %d\n", ret);
417 panic();
418 }
419}
420#endif