blob: 55a095662fc41a1ef67cbc6d395fb31c817dae4b [file] [log] [blame]
Tejas Patel354fe572018-12-14 00:55:37 -08001/*
Tejas Patel5b433b62020-01-29 22:09:55 -08002 * Copyright (c) 2019-2020, Xilinx, Inc. All rights reserved.
Tejas Patel354fe572018-12-14 00:55:37 -08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/*
8 * Top-level SMC handler for Versal power management calls and
9 * IPI setup functions for communication with PMC.
10 */
11
12#include <errno.h>
13#include <plat_private.h>
Tejas Patel59c608a2019-01-09 04:10:29 -080014#include <stdbool.h>
15#include <common/runtime_svc.h>
16#include "pm_api_sys.h"
Tejas Patel354fe572018-12-14 00:55:37 -080017#include "pm_client.h"
18#include "pm_ipi.h"
19
Tejas Patel59c608a2019-01-09 04:10:29 -080020/* pm_up = true - UP, pm_up = false - DOWN */
21static bool pm_up;
22
Tejas Patel354fe572018-12-14 00:55:37 -080023/**
24 * pm_setup() - PM service setup
25 *
26 * @return On success, the initialization function must return 0.
27 * Any other return value will cause the framework to ignore
28 * the service
29 *
30 * Initialization functions for Versal power management for
31 * communicaton with PMC.
32 *
33 * Called from sip_svc_setup initialization function with the
34 * rt_svc_init signature.
35 */
36int pm_setup(void)
37{
38 int status, ret = 0;
39
40 status = pm_ipi_init(primary_proc);
41
42 if (status < 0) {
43 INFO("BL31: PM Service Init Failed, Error Code %d!\n", status);
44 ret = status;
Tejas Patel59c608a2019-01-09 04:10:29 -080045 } else {
46 pm_up = true;
Tejas Patel354fe572018-12-14 00:55:37 -080047 }
48
49 return ret;
50}
Tejas Patel59c608a2019-01-09 04:10:29 -080051
52/**
53 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
54 * @smc_fid - Function Identifier
55 * @x1 - x4 - Arguments
56 * @cookie - Unused
57 * @handler - Pointer to caller's context structure
58 *
59 * @return - Unused
60 *
61 * Determines that smc_fid is valid and supported PM SMC Function ID from the
62 * list of pm_api_ids, otherwise completes the request with
63 * the unknown SMC Function ID
64 *
65 * The SMC calls for PM service are forwarded from SIP Service SMC handler
66 * function with rt_svc_handle signature
67 */
68uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3,
69 uint64_t x4, void *cookie, void *handle, uint64_t flags)
70{
71 enum pm_ret_status ret;
72
73 uint32_t pm_arg[4];
Tejas Patel18072da2021-02-25 20:16:56 -080074 uint32_t security_flag = SECURE_FLAG;
Tejas Patel59c608a2019-01-09 04:10:29 -080075
76 /* Handle case where PM wasn't initialized properly */
77 if (!pm_up)
78 SMC_RET1(handle, SMC_UNK);
79
80 pm_arg[0] = (uint32_t)x1;
81 pm_arg[1] = (uint32_t)(x1 >> 32);
82 pm_arg[2] = (uint32_t)x2;
83 pm_arg[3] = (uint32_t)(x2 >> 32);
84
Tejas Patel18072da2021-02-25 20:16:56 -080085 /*
86 * Mark BIT24 payload (i.e 1st bit of pm_arg[3] ) as non-secure (1)
87 * if smc called is non secure
88 */
89 if (is_caller_non_secure(flags)) {
90 security_flag = NON_SECURE_FLAG;
91 }
92
Tejas Patel59c608a2019-01-09 04:10:29 -080093 switch (smc_fid & FUNCID_NUM_MASK) {
94 /* PM API Functions */
95 case PM_SELF_SUSPEND:
96 ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
Tejas Patel18072da2021-02-25 20:16:56 -080097 pm_arg[3], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -080098 SMC_RET1(handle, (uint64_t)ret);
99
Tejas Patel6b282252019-01-10 03:03:47 -0800100 case PM_FORCE_POWERDOWN:
Tejas Patel18072da2021-02-25 20:16:56 -0800101 ret = pm_force_powerdown(pm_arg[0], pm_arg[1], security_flag);
Tejas Patel6b282252019-01-10 03:03:47 -0800102 SMC_RET1(handle, (uint64_t)ret);
103
Tejas Patel59c608a2019-01-09 04:10:29 -0800104 case PM_REQ_SUSPEND:
105 ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
Tejas Patel18072da2021-02-25 20:16:56 -0800106 pm_arg[3], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800107 SMC_RET1(handle, (uint64_t)ret);
108
109 case PM_ABORT_SUSPEND:
Tejas Patel18072da2021-02-25 20:16:56 -0800110 ret = pm_abort_suspend(pm_arg[0], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800111 SMC_RET1(handle, (uint64_t)ret);
112
Tejas Patel6b282252019-01-10 03:03:47 -0800113 case PM_SYSTEM_SHUTDOWN:
Tejas Patel18072da2021-02-25 20:16:56 -0800114 ret = pm_system_shutdown(pm_arg[0], pm_arg[1], security_flag);
Tejas Patel6b282252019-01-10 03:03:47 -0800115 SMC_RET1(handle, (uint64_t)ret);
116
Tejas Patel49cd8712019-01-23 14:18:51 +0530117 case PM_REQ_WAKEUP:
Tejas Patel18072da2021-02-25 20:16:56 -0800118 ret = pm_req_wakeup(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3],
119 security_flag);
Tejas Patel49cd8712019-01-23 14:18:51 +0530120 SMC_RET1(handle, (uint64_t)ret);
121
Tejas Pateldb812052019-01-23 14:18:53 +0530122 case PM_SET_WAKEUP_SOURCE:
Tejas Patel18072da2021-02-25 20:16:56 -0800123 ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2],
124 security_flag);
Tejas Pateldb812052019-01-23 14:18:53 +0530125 SMC_RET1(handle, (uint64_t)ret);
126
Tejas Patel59c608a2019-01-09 04:10:29 -0800127 case PM_REQUEST_DEVICE:
128 ret = pm_request_device(pm_arg[0], pm_arg[1], pm_arg[2],
Tejas Patel18072da2021-02-25 20:16:56 -0800129 pm_arg[3], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800130 SMC_RET1(handle, (uint64_t)ret);
131
132 case PM_RELEASE_DEVICE:
Tejas Patel18072da2021-02-25 20:16:56 -0800133 ret = pm_release_device(pm_arg[0], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800134 SMC_RET1(handle, (uint64_t)ret);
135
136 case PM_SET_REQUIREMENT:
137 ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2],
Tejas Patel18072da2021-02-25 20:16:56 -0800138 pm_arg[3], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800139 SMC_RET1(handle, (uint64_t)ret);
140
141 case PM_GET_API_VERSION:
142 {
143 uint32_t api_version;
144
Tejas Patel18072da2021-02-25 20:16:56 -0800145 ret = pm_get_api_version(&api_version, security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800146 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
147 ((uint64_t)api_version << 32));
148 }
149
150 case PM_GET_DEVICE_STATUS:
151 {
152 uint32_t buff[3];
153
Tejas Patel18072da2021-02-25 20:16:56 -0800154 ret = pm_get_device_status(pm_arg[0], buff, security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800155 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buff[0] << 32),
156 (uint64_t)buff[1] | ((uint64_t)buff[2] << 32));
157 }
158
159 case PM_RESET_ASSERT:
Tejas Patel18072da2021-02-25 20:16:56 -0800160 ret = pm_reset_assert(pm_arg[0], pm_arg[1], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800161 SMC_RET1(handle, (uint64_t)ret);
162
163 case PM_RESET_GET_STATUS:
164 {
165 uint32_t reset_status;
166
Tejas Patel18072da2021-02-25 20:16:56 -0800167 ret = pm_reset_get_status(pm_arg[0], &reset_status,
168 security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800169 SMC_RET1(handle, (uint64_t)ret |
170 ((uint64_t)reset_status << 32));
171 }
172
Tejas Patel02662022019-01-21 17:56:49 +0530173 case PM_INIT_FINALIZE:
Tejas Patel18072da2021-02-25 20:16:56 -0800174 ret = pm_init_finalize(security_flag);
Ravi Patel476b5b12019-08-12 03:10:10 -0700175 SMC_RET1(handle, (uint64_t)ret);
Tejas Patel02662022019-01-21 17:56:49 +0530176
Rajan Vaja649e9242019-03-04 11:09:40 +0530177 case PM_GET_CALLBACK_DATA:
178 {
179 uint32_t result[4] = {0};
180
Tejas Patel18072da2021-02-25 20:16:56 -0800181 pm_get_callbackdata(result, ARRAY_SIZE(result), security_flag);
Rajan Vaja649e9242019-03-04 11:09:40 +0530182 SMC_RET2(handle,
183 (uint64_t)result[0] | ((uint64_t)result[1] << 32),
184 (uint64_t)result[2] | ((uint64_t)result[3] << 32));
185 }
186
Tejas Patel59c608a2019-01-09 04:10:29 -0800187 case PM_PINCTRL_REQUEST:
Tejas Patel18072da2021-02-25 20:16:56 -0800188 ret = pm_pinctrl_request(pm_arg[0], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800189 SMC_RET1(handle, (uint64_t)ret);
190
191 case PM_PINCTRL_RELEASE:
Tejas Patel18072da2021-02-25 20:16:56 -0800192 ret = pm_pinctrl_release(pm_arg[0], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800193 SMC_RET1(handle, (uint64_t)ret);
194
195 case PM_PINCTRL_GET_FUNCTION:
196 {
197 uint32_t value = 0;
198
Tejas Patel18072da2021-02-25 20:16:56 -0800199 ret = pm_pinctrl_get_function(pm_arg[0], &value, security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800200 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
201 }
202
203 case PM_PINCTRL_SET_FUNCTION:
Tejas Patel18072da2021-02-25 20:16:56 -0800204 ret = pm_pinctrl_set_function(pm_arg[0], pm_arg[1],
205 security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800206 SMC_RET1(handle, (uint64_t)ret);
207
208 case PM_PINCTRL_CONFIG_PARAM_GET:
209 {
210 uint32_t value;
211
Tejas Patel18072da2021-02-25 20:16:56 -0800212 ret = pm_pinctrl_get_pin_param(pm_arg[0], pm_arg[1], &value,
213 security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800214 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
215 }
216
217 case PM_PINCTRL_CONFIG_PARAM_SET:
Tejas Patel18072da2021-02-25 20:16:56 -0800218 ret = pm_pinctrl_set_pin_param(pm_arg[0], pm_arg[1], pm_arg[2],
219 security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800220 SMC_RET1(handle, (uint64_t)ret);
221
Tejas Patel9141f442019-01-10 03:03:48 -0800222 case PM_IOCTL:
223 {
224 uint32_t value;
225
226 ret = pm_api_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
Tejas Patel18072da2021-02-25 20:16:56 -0800227 pm_arg[3], &value, security_flag);
Tejas Patel9141f442019-01-10 03:03:48 -0800228 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
229 }
230
Tejas Patela3e34ad2019-02-01 17:25:19 +0530231 case PM_QUERY_DATA:
232 {
Rajan Vaja030620d2020-11-23 04:13:54 -0800233 uint32_t data[8] = { 0 };
Tejas Patela3e34ad2019-02-01 17:25:19 +0530234
235 ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
Tejas Patel18072da2021-02-25 20:16:56 -0800236 pm_arg[3], data, security_flag);
Rajan Vaja030620d2020-11-23 04:13:54 -0800237
Tejas Patela3e34ad2019-02-01 17:25:19 +0530238 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)data[0] << 32),
Rajan Vaja030620d2020-11-23 04:13:54 -0800239 (uint64_t)data[1] | ((uint64_t)data[2] << 32));
Tejas Patela3e34ad2019-02-01 17:25:19 +0530240
Rajan Vaja030620d2020-11-23 04:13:54 -0800241 }
Tejas Patel59c608a2019-01-09 04:10:29 -0800242 case PM_CLOCK_ENABLE:
Tejas Patel18072da2021-02-25 20:16:56 -0800243 ret = pm_clock_enable(pm_arg[0], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800244 SMC_RET1(handle, (uint64_t)ret);
245
246 case PM_CLOCK_DISABLE:
Tejas Patel18072da2021-02-25 20:16:56 -0800247 ret = pm_clock_disable(pm_arg[0], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800248 SMC_RET1(handle, (uint64_t)ret);
249
250 case PM_CLOCK_GETSTATE:
251 {
252 uint32_t value;
253
Tejas Patel18072da2021-02-25 20:16:56 -0800254 ret = pm_clock_get_state(pm_arg[0], &value, security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800255 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
256 }
257
258 case PM_CLOCK_SETDIVIDER:
Tejas Patel18072da2021-02-25 20:16:56 -0800259 ret = pm_clock_set_divider(pm_arg[0], pm_arg[1], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800260 SMC_RET1(handle, (uint64_t)ret);
261
262 case PM_CLOCK_GETDIVIDER:
263 {
264 uint32_t value;
265
Tejas Patel18072da2021-02-25 20:16:56 -0800266 ret = pm_clock_get_divider(pm_arg[0], &value, security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800267 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
268 }
269
270 case PM_CLOCK_SETPARENT:
Tejas Patel18072da2021-02-25 20:16:56 -0800271 ret = pm_clock_set_parent(pm_arg[0], pm_arg[1], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800272 SMC_RET1(handle, (uint64_t)ret);
273
274 case PM_CLOCK_GETPARENT:
275 {
276 uint32_t value;
277
Tejas Patel18072da2021-02-25 20:16:56 -0800278 ret = pm_clock_get_parent(pm_arg[0], &value, security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800279 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
280 }
281
Tejas Patel84275bf2020-09-01 04:43:53 -0700282 case PM_CLOCK_GETRATE:
283 {
284 uint32_t rate[2] = { 0 };
285
Tejas Patel18072da2021-02-25 20:16:56 -0800286 ret = pm_clock_get_rate(pm_arg[0], rate, security_flag);
Tejas Patel84275bf2020-09-01 04:43:53 -0700287 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)rate[0] << 32),
288 rate[1]);
289 }
290
Tejas Patel59c608a2019-01-09 04:10:29 -0800291 case PM_PLL_SET_PARAMETER:
Tejas Patel18072da2021-02-25 20:16:56 -0800292 ret = pm_pll_set_param(pm_arg[0], pm_arg[1], pm_arg[2],
293 security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800294 SMC_RET1(handle, (uint64_t)ret);
295
296 case PM_PLL_GET_PARAMETER:
297 {
298 uint32_t value;
299
Tejas Patel18072da2021-02-25 20:16:56 -0800300 ret = pm_pll_get_param(pm_arg[0], pm_arg[1], &value,
301 security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800302 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value << 32));
303 }
304
305 case PM_PLL_SET_MODE:
Tejas Patel18072da2021-02-25 20:16:56 -0800306 ret = pm_pll_set_mode(pm_arg[0], pm_arg[1], security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800307 SMC_RET1(handle, (uint64_t)ret);
308
309 case PM_PLL_GET_MODE:
310 {
311 uint32_t mode;
312
Tejas Patel18072da2021-02-25 20:16:56 -0800313 ret = pm_pll_get_mode(pm_arg[0], &mode, security_flag);
Tejas Patel59c608a2019-01-09 04:10:29 -0800314 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)mode << 32));
315 }
316
Tejas Patel044001d2019-01-21 17:56:48 +0530317 case PM_GET_TRUSTZONE_VERSION:
318 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
319 ((uint64_t)VERSAL_TZ_VERSION << 32));
320
Ravi Patelbd4aa5a2019-08-12 03:17:54 -0700321 case PM_GET_CHIPID:
322 {
323 uint32_t result[2];
324
Tejas Patel18072da2021-02-25 20:16:56 -0800325 ret = pm_get_chipid(result, security_flag);
Ravi Patelbd4aa5a2019-08-12 03:17:54 -0700326 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32),
327 result[1]);
328 }
329
Ravi Patel22b0b492019-03-06 12:34:46 +0530330 case PM_FEATURE_CHECK:
331 {
332 uint32_t version;
333
Tejas Patel18072da2021-02-25 20:16:56 -0800334 ret = pm_feature_check(pm_arg[0], &version, security_flag);
Ravi Patel22b0b492019-03-06 12:34:46 +0530335 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)version << 32));
336 }
337
Jolly Shahed05a712019-03-22 05:33:39 +0530338 case PM_LOAD_PDI:
339 {
Tejas Patel18072da2021-02-25 20:16:56 -0800340 ret = pm_load_pdi(pm_arg[0], pm_arg[1], pm_arg[2],
341 security_flag);
Jolly Shahed05a712019-03-22 05:33:39 +0530342 SMC_RET1(handle, (uint64_t)ret);
343 }
344
Saeed Nowshadi2294b422019-06-03 10:22:35 -0700345 case PM_GET_OP_CHARACTERISTIC:
346 {
347 uint32_t result;
348
Tejas Patel18072da2021-02-25 20:16:56 -0800349 ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result,
350 security_flag);
Saeed Nowshadi2294b422019-06-03 10:22:35 -0700351 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32));
352 }
353
Tejas Patel5c154e12020-11-25 01:53:12 -0800354 case PM_SET_MAX_LATENCY:
355 {
Tejas Patel18072da2021-02-25 20:16:56 -0800356 ret = pm_set_max_latency(pm_arg[0], pm_arg[1], security_flag);
Tejas Patel5c154e12020-11-25 01:53:12 -0800357 SMC_RET1(handle, (uint64_t)ret);
358 }
359
Tejas Patel42015552020-11-25 01:56:57 -0800360 case PM_REGISTER_NOTIFIER:
361 {
Tejas Patel18072da2021-02-25 20:16:56 -0800362 ret = pm_register_notifier(pm_arg[0], pm_arg[1], pm_arg[2],
363 pm_arg[3], security_flag);
Tejas Patel42015552020-11-25 01:56:57 -0800364 SMC_RET1(handle, (uint64_t)ret);
365 }
366
Tejas Patel59c608a2019-01-09 04:10:29 -0800367 default:
368 WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid);
369 SMC_RET1(handle, SMC_UNK);
370 }
371}