blob: 9b356a76f0bc0f210d041271a56e50e63e13ac71 [file] [log] [blame]
Soren Brinkmann76fcae32016-03-06 20:16:27 -08001/*
Rajan Vaja83687612018-01-17 02:39:20 -08002 * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
Soren Brinkmann76fcae32016-03-06 20:16:27 -08003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soren Brinkmann76fcae32016-03-06 20:16:27 -08005 */
6
7/*
8 * Top-level SMC handler for ZynqMP power management calls and
9 * IPI setup functions for communication with PMU.
10 */
11
12#include <errno.h>
Soren Brinkmann76fcae32016-03-06 20:16:27 -080013#include <runtime_svc.h>
Isla Mitchelle3631462017-07-14 10:46:32 +010014#include "../zynqmp_private.h"
Soren Brinkmann76fcae32016-03-06 20:16:27 -080015#include "pm_api_sys.h"
16#include "pm_client.h"
17#include "pm_ipi.h"
Soren Brinkmann76fcae32016-03-06 20:16:27 -080018
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +053019#define PM_SET_SUSPEND_MODE 0xa02
Rajan Vajac7ee23d2018-02-14 23:10:54 -080020#define PM_GET_TRUSTZONE_VERSION 0xa03
Soren Brinkmann84f0af42016-09-30 14:24:25 -070021
Siva Durga Prasad Paladugu79f75952018-04-30 19:39:49 +053022/* !0 - UP, 0 - DOWN */
23static int32_t pm_up = 0;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080024
25/**
26 * pm_context - Structure which contains data for power management
27 * @api_version version of PM API, must match with one on PMU side
28 * @payload payload array used to store received
29 * data from ipi buffer registers
30 */
31static struct {
32 uint32_t api_version;
33 uint32_t payload[PAYLOAD_ARG_CNT];
34} pm_ctx;
35
36/**
37 * pm_setup() - PM service setup
38 *
39 * @return On success, the initialization function must return 0.
40 * Any other return value will cause the framework to ignore
41 * the service
42 *
43 * Initialization functions for ZynqMP power management for
44 * communicaton with PMU.
45 *
46 * Called from sip_svc_setup initialization function with the
47 * rt_svc_init signature.
Soren Brinkmann76fcae32016-03-06 20:16:27 -080048 */
49int pm_setup(void)
50{
Wendy Liang328105c2017-10-03 23:21:11 -070051 int status, ret;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080052
Wendy Liang328105c2017-10-03 23:21:11 -070053 status = pm_ipi_init(primary_proc);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080054
Wendy Liang328105c2017-10-03 23:21:11 -070055 if (status >= 0) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -080056 INFO("BL31: PM Service Init Complete: API v%d.%d\n",
57 PM_VERSION_MAJOR, PM_VERSION_MINOR);
Wendy Liang328105c2017-10-03 23:21:11 -070058 ret = 0;
59 } else {
Soren Brinkmann76fcae32016-03-06 20:16:27 -080060 INFO("BL31: PM Service Init Failed, Error Code %d!\n", status);
Wendy Liang328105c2017-10-03 23:21:11 -070061 ret = status;
62 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -080063
Siva Durga Prasad Paladugu79f75952018-04-30 19:39:49 +053064 pm_up = !status;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080065
Wendy Liang328105c2017-10-03 23:21:11 -070066 return ret;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080067}
68
69/**
70 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
71 * @smc_fid - Function Identifier
72 * @x1 - x4 - Arguments
73 * @cookie - Unused
74 * @handler - Pointer to caller's context structure
75 *
76 * @return - Unused
77 *
78 * Determines that smc_fid is valid and supported PM SMC Function ID from the
79 * list of pm_api_ids, otherwise completes the request with
80 * the unknown SMC Function ID
81 *
82 * The SMC calls for PM service are forwarded from SIP Service SMC handler
83 * function with rt_svc_handle signature
84 */
85uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3,
86 uint64_t x4, void *cookie, void *handle, uint64_t flags)
87{
88 enum pm_ret_status ret;
89
90 uint32_t pm_arg[4];
91
92 /* Handle case where PM wasn't initialized properly */
Siva Durga Prasad Paladugu79f75952018-04-30 19:39:49 +053093 if (!pm_up)
Soren Brinkmann76fcae32016-03-06 20:16:27 -080094 SMC_RET1(handle, SMC_UNK);
95
96 pm_arg[0] = (uint32_t)x1;
97 pm_arg[1] = (uint32_t)(x1 >> 32);
98 pm_arg[2] = (uint32_t)x2;
99 pm_arg[3] = (uint32_t)(x2 >> 32);
100
101 switch (smc_fid & FUNCID_NUM_MASK) {
102 /* PM API Functions */
103 case PM_SELF_SUSPEND:
104 ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
105 pm_arg[3]);
106 SMC_RET1(handle, (uint64_t)ret);
107
108 case PM_REQ_SUSPEND:
109 ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
110 pm_arg[3]);
111 SMC_RET1(handle, (uint64_t)ret);
112
113 case PM_REQ_WAKEUP:
Filip Drazic78ba1452017-02-07 12:03:57 +0100114 {
115 /* Use address flag is encoded in the 1st bit of the low-word */
116 unsigned int set_addr = pm_arg[1] & 0x1;
117 uint64_t address = (uint64_t)pm_arg[2] << 32;
118
119 address |= pm_arg[1] & (~0x1);
120 ret = pm_req_wakeup(pm_arg[0], set_addr, address,
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800121 pm_arg[3]);
122 SMC_RET1(handle, (uint64_t)ret);
Filip Drazic78ba1452017-02-07 12:03:57 +0100123 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800124
125 case PM_FORCE_POWERDOWN:
126 ret = pm_force_powerdown(pm_arg[0], pm_arg[1]);
127 SMC_RET1(handle, (uint64_t)ret);
128
129 case PM_ABORT_SUSPEND:
130 ret = pm_abort_suspend(pm_arg[0]);
131 SMC_RET1(handle, (uint64_t)ret);
132
133 case PM_SET_WAKEUP_SOURCE:
134 ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2]);
135 SMC_RET1(handle, (uint64_t)ret);
136
137 case PM_SYSTEM_SHUTDOWN:
Soren Brinkmann58fbb9b2016-09-02 09:50:54 -0700138 ret = pm_system_shutdown(pm_arg[0], pm_arg[1]);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800139 SMC_RET1(handle, (uint64_t)ret);
140
141 case PM_REQ_NODE:
142 ret = pm_req_node(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
143 SMC_RET1(handle, (uint64_t)ret);
144
145 case PM_RELEASE_NODE:
146 ret = pm_release_node(pm_arg[0]);
147 SMC_RET1(handle, (uint64_t)ret);
148
149 case PM_SET_REQUIREMENT:
150 ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2],
151 pm_arg[3]);
152 SMC_RET1(handle, (uint64_t)ret);
153
154 case PM_SET_MAX_LATENCY:
155 ret = pm_set_max_latency(pm_arg[0], pm_arg[1]);
156 SMC_RET1(handle, (uint64_t)ret);
157
158 case PM_GET_API_VERSION:
159 /* Check is PM API version already verified */
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700160 if (pm_ctx.api_version == PM_VERSION) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800161 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
162 ((uint64_t)PM_VERSION << 32));
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700163 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800164
165 ret = pm_get_api_version(&pm_ctx.api_version);
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700166 /*
167 * Enable IPI IRQ
168 * assume the rich OS is OK to handle callback IRQs now.
169 * Even if we were wrong, it would not enable the IRQ in
170 * the GIC.
171 */
Wendy Liang328105c2017-10-03 23:21:11 -0700172 pm_ipi_irq_enable(primary_proc);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800173 SMC_RET1(handle, (uint64_t)ret |
174 ((uint64_t)pm_ctx.api_version << 32));
175
176 case PM_SET_CONFIGURATION:
177 ret = pm_set_configuration(pm_arg[0]);
178 SMC_RET1(handle, (uint64_t)ret);
179
Filip Drazicf2ddd912017-03-15 11:50:47 +0100180 case PM_INIT_FINALIZE:
181 ret = pm_init_finalize();
182 SMC_RET1(handle, (uint64_t)ret);
183
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800184 case PM_GET_NODE_STATUS:
Anes Hadziahmetagic1caf88e2017-01-27 18:42:44 +0100185 {
186 uint32_t buff[3];
187
188 ret = pm_get_node_status(pm_arg[0], buff);
189 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buff[0] << 32),
190 (uint64_t)buff[1] | ((uint64_t)buff[2] << 32));
191 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800192
193 case PM_GET_OP_CHARACTERISTIC:
Anes Hadziahmetagic92aee012016-05-12 16:17:30 +0200194 {
195 uint32_t result;
196
197 ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result);
198 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32));
199 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800200
201 case PM_REGISTER_NOTIFIER:
202 ret = pm_register_notifier(pm_arg[0], pm_arg[1], pm_arg[2],
203 pm_arg[3]);
204 SMC_RET1(handle, (uint64_t)ret);
205
206 case PM_RESET_ASSERT:
207 ret = pm_reset_assert(pm_arg[0], pm_arg[1]);
208 SMC_RET1(handle, (uint64_t)ret);
209
210 case PM_RESET_GET_STATUS:
211 {
212 uint32_t reset_status;
213
214 ret = pm_reset_get_status(pm_arg[0], &reset_status);
215 SMC_RET1(handle, (uint64_t)ret |
216 ((uint64_t)reset_status << 32));
217 }
218
219 /* PM memory access functions */
220 case PM_MMIO_WRITE:
221 ret = pm_mmio_write(pm_arg[0], pm_arg[1], pm_arg[2]);
222 SMC_RET1(handle, (uint64_t)ret);
223
224 case PM_MMIO_READ:
225 {
226 uint32_t value;
227
228 ret = pm_mmio_read(pm_arg[0], &value);
229 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
230 }
Nava kishore Manne68d460c2016-08-20 23:18:09 +0530231
232 case PM_FPGA_LOAD:
233 ret = pm_fpga_load(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
234 SMC_RET1(handle, (uint64_t)ret);
235
236 case PM_FPGA_GET_STATUS:
237 {
238 uint32_t value;
239
240 ret = pm_fpga_get_status(&value);
241 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
242 }
243
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530244 case PM_GET_CHIPID:
Soren Brinkmanncb366812016-09-22 12:21:11 -0700245 {
246 uint32_t result[2];
247
248 ret = pm_get_chipid(result);
249 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32),
250 result[1]);
251 }
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530252
Siva Durga Prasad Paladugude93d982018-04-30 15:49:27 +0530253 case PM_SECURE_RSA_AES:
254 ret = pm_secure_rsaaes(pm_arg[0], pm_arg[1], pm_arg[2],
255 pm_arg[3]);
256 SMC_RET1(handle, (uint64_t)ret);
257
Rajan Vaja83687612018-01-17 02:39:20 -0800258 case PM_PINCTRL_REQUEST:
259 ret = pm_pinctrl_request(pm_arg[0]);
260 SMC_RET1(handle, (uint64_t)ret);
261
262 case PM_PINCTRL_RELEASE:
263 ret = pm_pinctrl_release(pm_arg[0]);
264 SMC_RET1(handle, (uint64_t)ret);
265
266 case PM_PINCTRL_GET_FUNCTION:
267 {
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800268 uint32_t value = 0;
Rajan Vaja83687612018-01-17 02:39:20 -0800269
270 ret = pm_pinctrl_get_function(pm_arg[0], &value);
271 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
272 }
273
274 case PM_PINCTRL_SET_FUNCTION:
275 ret = pm_pinctrl_set_function(pm_arg[0], pm_arg[1]);
276 SMC_RET1(handle, (uint64_t)ret);
277
278 case PM_PINCTRL_CONFIG_PARAM_GET:
279 {
280 uint32_t value;
281
282 ret = pm_pinctrl_get_config(pm_arg[0], pm_arg[1], &value);
283 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
284 }
285
286 case PM_PINCTRL_CONFIG_PARAM_SET:
287 ret = pm_pinctrl_set_config(pm_arg[0], pm_arg[1], pm_arg[2]);
288 SMC_RET1(handle, (uint64_t)ret);
289
Rajan Vaja5529a012018-01-17 02:39:23 -0800290 case PM_IOCTL:
291 {
292 uint32_t value;
293
294 ret = pm_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
295 pm_arg[3], &value);
296 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
297 }
298
Rajan Vaja35116132018-01-17 02:39:25 -0800299 case PM_QUERY_DATA:
300 {
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800301 uint32_t data[4] = { 0 };
Rajan Vaja35116132018-01-17 02:39:25 -0800302
303 ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
304 pm_arg[3], data);
305 SMC_RET2(handle, (uint64_t)data[0] | ((uint64_t)data[1] << 32),
306 (uint64_t)data[2] | ((uint64_t)data[3] << 32));
307 }
308
309 case PM_CLOCK_ENABLE:
310 ret = pm_clock_enable(pm_arg[0]);
311 SMC_RET1(handle, (uint64_t)ret);
312
313 case PM_CLOCK_DISABLE:
314 ret = pm_clock_disable(pm_arg[0]);
315 SMC_RET1(handle, (uint64_t)ret);
316
317 case PM_CLOCK_GETSTATE:
318 {
319 uint32_t value;
320
321 ret = pm_clock_getstate(pm_arg[0], &value);
322 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
323 }
324
325 case PM_CLOCK_SETDIVIDER:
326 ret = pm_clock_setdivider(pm_arg[0], pm_arg[1]);
327 SMC_RET1(handle, (uint64_t)ret);
328
329 case PM_CLOCK_GETDIVIDER:
330 {
331 uint32_t value;
332
333 ret = pm_clock_getdivider(pm_arg[0], &value);
334 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
335 }
336
337 case PM_CLOCK_SETRATE:
338 ret = pm_clock_setrate(pm_arg[0],
339 ((uint64_t)pm_arg[2]) << 32 | pm_arg[1]);
340
341 SMC_RET1(handle, (uint64_t)ret);
342
343 case PM_CLOCK_GETRATE:
344 {
345 uint64_t value;
346
347 ret = pm_clock_getrate(pm_arg[0], &value);
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800348 SMC_RET2(handle, (uint64_t)ret |
349 (((uint64_t)value & 0xFFFFFFFFU) << 32U),
350 (value >> 32U) & 0xFFFFFFFFU);
Rajan Vaja35116132018-01-17 02:39:25 -0800351
352 }
353
354 case PM_CLOCK_SETPARENT:
355 ret = pm_clock_setparent(pm_arg[0], pm_arg[1]);
356 SMC_RET1(handle, (uint64_t)ret);
357
358 case PM_CLOCK_GETPARENT:
359 {
360 uint32_t value;
361
362 ret = pm_clock_getparent(pm_arg[0], &value);
363 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
364 }
365
Rajan Vajac7ee23d2018-02-14 23:10:54 -0800366 case PM_GET_TRUSTZONE_VERSION:
367 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
368 ((uint64_t)ZYNQMP_TZ_VERSION << 32));
369
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +0530370 case PM_SET_SUSPEND_MODE:
371 ret = pm_set_suspend_mode(pm_arg[0]);
372 SMC_RET1(handle, (uint64_t)ret);
373
Siva Durga Prasad Paladuguf3994cc2018-05-01 11:12:55 +0530374 case PM_SECURE_SHA:
375 ret = pm_sha_hash(pm_arg[0], pm_arg[1], pm_arg[2],
376 pm_arg[3]);
377 SMC_RET1(handle, (uint64_t)ret);
378
379 case PM_SECURE_RSA:
380 ret = pm_rsa_core(pm_arg[0], pm_arg[1], pm_arg[2],
381 pm_arg[3]);
382 SMC_RET1(handle, (uint64_t)ret);
383
Siva Durga Prasad Paladugua4ed4b22018-04-30 20:06:58 +0530384 case PM_SECURE_IMAGE:
385 {
386 uint32_t result[2];
387
388 ret = pm_secure_image(pm_arg[0], pm_arg[1], pm_arg[2],
389 pm_arg[3], &result[0]);
390 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32),
391 result[1]);
392 }
393
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800394 default:
395 WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid);
396 SMC_RET1(handle, SMC_UNK);
397 }
398}