blob: 5a4da90ebce294a328f143f7100ed686efb14150 [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>
13#include <gic_common.h>
14#include <runtime_svc.h>
15#include <string.h>
Isla Mitchelle3631462017-07-14 10:46:32 +010016#include "../zynqmp_private.h"
Soren Brinkmann76fcae32016-03-06 20:16:27 -080017#include "pm_api_sys.h"
18#include "pm_client.h"
19#include "pm_ipi.h"
Soren Brinkmann76fcae32016-03-06 20:16:27 -080020
Soren Brinkmann84f0af42016-09-30 14:24:25 -070021#define PM_GET_CALLBACK_DATA 0xa01
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +053022#define PM_SET_SUSPEND_MODE 0xa02
Rajan Vajac7ee23d2018-02-14 23:10:54 -080023#define PM_GET_TRUSTZONE_VERSION 0xa03
Soren Brinkmann84f0af42016-09-30 14:24:25 -070024
Soren Brinkmann76fcae32016-03-06 20:16:27 -080025/* 0 - UP, !0 - DOWN */
26static int32_t pm_down = !0;
27
28/**
29 * pm_context - Structure which contains data for power management
30 * @api_version version of PM API, must match with one on PMU side
31 * @payload payload array used to store received
32 * data from ipi buffer registers
33 */
34static struct {
35 uint32_t api_version;
36 uint32_t payload[PAYLOAD_ARG_CNT];
37} pm_ctx;
38
39/**
40 * pm_setup() - PM service setup
41 *
42 * @return On success, the initialization function must return 0.
43 * Any other return value will cause the framework to ignore
44 * the service
45 *
46 * Initialization functions for ZynqMP power management for
47 * communicaton with PMU.
48 *
49 * Called from sip_svc_setup initialization function with the
50 * rt_svc_init signature.
Soren Brinkmann76fcae32016-03-06 20:16:27 -080051 */
52int pm_setup(void)
53{
Wendy Liang328105c2017-10-03 23:21:11 -070054 int status, ret;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080055
56 if (!zynqmp_is_pmu_up())
57 return -ENODEV;
58
Wendy Liang328105c2017-10-03 23:21:11 -070059 status = pm_ipi_init(primary_proc);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080060
Wendy Liang328105c2017-10-03 23:21:11 -070061 if (status >= 0) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -080062 INFO("BL31: PM Service Init Complete: API v%d.%d\n",
63 PM_VERSION_MAJOR, PM_VERSION_MINOR);
Wendy Liang328105c2017-10-03 23:21:11 -070064 ret = 0;
65 } else {
Soren Brinkmann76fcae32016-03-06 20:16:27 -080066 INFO("BL31: PM Service Init Failed, Error Code %d!\n", status);
Wendy Liang328105c2017-10-03 23:21:11 -070067 ret = status;
68 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -080069
70 pm_down = status;
71
Wendy Liang328105c2017-10-03 23:21:11 -070072 return ret;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080073}
74
75/**
76 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
77 * @smc_fid - Function Identifier
78 * @x1 - x4 - Arguments
79 * @cookie - Unused
80 * @handler - Pointer to caller's context structure
81 *
82 * @return - Unused
83 *
84 * Determines that smc_fid is valid and supported PM SMC Function ID from the
85 * list of pm_api_ids, otherwise completes the request with
86 * the unknown SMC Function ID
87 *
88 * The SMC calls for PM service are forwarded from SIP Service SMC handler
89 * function with rt_svc_handle signature
90 */
91uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3,
92 uint64_t x4, void *cookie, void *handle, uint64_t flags)
93{
94 enum pm_ret_status ret;
95
96 uint32_t pm_arg[4];
97
98 /* Handle case where PM wasn't initialized properly */
99 if (pm_down)
100 SMC_RET1(handle, SMC_UNK);
101
102 pm_arg[0] = (uint32_t)x1;
103 pm_arg[1] = (uint32_t)(x1 >> 32);
104 pm_arg[2] = (uint32_t)x2;
105 pm_arg[3] = (uint32_t)(x2 >> 32);
106
107 switch (smc_fid & FUNCID_NUM_MASK) {
108 /* PM API Functions */
109 case PM_SELF_SUSPEND:
110 ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
111 pm_arg[3]);
112 SMC_RET1(handle, (uint64_t)ret);
113
114 case PM_REQ_SUSPEND:
115 ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
116 pm_arg[3]);
117 SMC_RET1(handle, (uint64_t)ret);
118
119 case PM_REQ_WAKEUP:
Filip Drazic78ba1452017-02-07 12:03:57 +0100120 {
121 /* Use address flag is encoded in the 1st bit of the low-word */
122 unsigned int set_addr = pm_arg[1] & 0x1;
123 uint64_t address = (uint64_t)pm_arg[2] << 32;
124
125 address |= pm_arg[1] & (~0x1);
126 ret = pm_req_wakeup(pm_arg[0], set_addr, address,
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800127 pm_arg[3]);
128 SMC_RET1(handle, (uint64_t)ret);
Filip Drazic78ba1452017-02-07 12:03:57 +0100129 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800130
131 case PM_FORCE_POWERDOWN:
132 ret = pm_force_powerdown(pm_arg[0], pm_arg[1]);
133 SMC_RET1(handle, (uint64_t)ret);
134
135 case PM_ABORT_SUSPEND:
136 ret = pm_abort_suspend(pm_arg[0]);
137 SMC_RET1(handle, (uint64_t)ret);
138
139 case PM_SET_WAKEUP_SOURCE:
140 ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2]);
141 SMC_RET1(handle, (uint64_t)ret);
142
143 case PM_SYSTEM_SHUTDOWN:
Soren Brinkmann58fbb9b2016-09-02 09:50:54 -0700144 ret = pm_system_shutdown(pm_arg[0], pm_arg[1]);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800145 SMC_RET1(handle, (uint64_t)ret);
146
147 case PM_REQ_NODE:
148 ret = pm_req_node(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
149 SMC_RET1(handle, (uint64_t)ret);
150
151 case PM_RELEASE_NODE:
152 ret = pm_release_node(pm_arg[0]);
153 SMC_RET1(handle, (uint64_t)ret);
154
155 case PM_SET_REQUIREMENT:
156 ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2],
157 pm_arg[3]);
158 SMC_RET1(handle, (uint64_t)ret);
159
160 case PM_SET_MAX_LATENCY:
161 ret = pm_set_max_latency(pm_arg[0], pm_arg[1]);
162 SMC_RET1(handle, (uint64_t)ret);
163
164 case PM_GET_API_VERSION:
165 /* Check is PM API version already verified */
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700166 if (pm_ctx.api_version == PM_VERSION) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800167 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
168 ((uint64_t)PM_VERSION << 32));
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700169 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800170
171 ret = pm_get_api_version(&pm_ctx.api_version);
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700172 /*
173 * Enable IPI IRQ
174 * assume the rich OS is OK to handle callback IRQs now.
175 * Even if we were wrong, it would not enable the IRQ in
176 * the GIC.
177 */
Wendy Liang328105c2017-10-03 23:21:11 -0700178 pm_ipi_irq_enable(primary_proc);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800179 SMC_RET1(handle, (uint64_t)ret |
180 ((uint64_t)pm_ctx.api_version << 32));
181
182 case PM_SET_CONFIGURATION:
183 ret = pm_set_configuration(pm_arg[0]);
184 SMC_RET1(handle, (uint64_t)ret);
185
Filip Drazicf2ddd912017-03-15 11:50:47 +0100186 case PM_INIT_FINALIZE:
187 ret = pm_init_finalize();
188 SMC_RET1(handle, (uint64_t)ret);
189
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800190 case PM_GET_NODE_STATUS:
Anes Hadziahmetagic1caf88e2017-01-27 18:42:44 +0100191 {
192 uint32_t buff[3];
193
194 ret = pm_get_node_status(pm_arg[0], buff);
195 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buff[0] << 32),
196 (uint64_t)buff[1] | ((uint64_t)buff[2] << 32));
197 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800198
199 case PM_GET_OP_CHARACTERISTIC:
Anes Hadziahmetagic92aee012016-05-12 16:17:30 +0200200 {
201 uint32_t result;
202
203 ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result);
204 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32));
205 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800206
207 case PM_REGISTER_NOTIFIER:
208 ret = pm_register_notifier(pm_arg[0], pm_arg[1], pm_arg[2],
209 pm_arg[3]);
210 SMC_RET1(handle, (uint64_t)ret);
211
212 case PM_RESET_ASSERT:
213 ret = pm_reset_assert(pm_arg[0], pm_arg[1]);
214 SMC_RET1(handle, (uint64_t)ret);
215
216 case PM_RESET_GET_STATUS:
217 {
218 uint32_t reset_status;
219
220 ret = pm_reset_get_status(pm_arg[0], &reset_status);
221 SMC_RET1(handle, (uint64_t)ret |
222 ((uint64_t)reset_status << 32));
223 }
224
225 /* PM memory access functions */
226 case PM_MMIO_WRITE:
227 ret = pm_mmio_write(pm_arg[0], pm_arg[1], pm_arg[2]);
228 SMC_RET1(handle, (uint64_t)ret);
229
230 case PM_MMIO_READ:
231 {
232 uint32_t value;
233
234 ret = pm_mmio_read(pm_arg[0], &value);
235 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
236 }
Nava kishore Manne68d460c2016-08-20 23:18:09 +0530237
238 case PM_FPGA_LOAD:
239 ret = pm_fpga_load(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
240 SMC_RET1(handle, (uint64_t)ret);
241
242 case PM_FPGA_GET_STATUS:
243 {
244 uint32_t value;
245
246 ret = pm_fpga_get_status(&value);
247 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
248 }
249
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530250 case PM_GET_CHIPID:
Soren Brinkmanncb366812016-09-22 12:21:11 -0700251 {
252 uint32_t result[2];
253
254 ret = pm_get_chipid(result);
255 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32),
256 result[1]);
257 }
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530258
Siva Durga Prasad Paladugude93d982018-04-30 15:49:27 +0530259 case PM_SECURE_RSA_AES:
260 ret = pm_secure_rsaaes(pm_arg[0], pm_arg[1], pm_arg[2],
261 pm_arg[3]);
262 SMC_RET1(handle, (uint64_t)ret);
263
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700264 case PM_GET_CALLBACK_DATA:
265 {
266 uint32_t result[4];
267
268 pm_get_callbackdata(result, sizeof(result));
269 SMC_RET2(handle,
270 (uint64_t)result[0] | ((uint64_t)result[1] << 32),
271 (uint64_t)result[2] | ((uint64_t)result[3] << 32));
272 }
273
Rajan Vaja83687612018-01-17 02:39:20 -0800274 case PM_PINCTRL_REQUEST:
275 ret = pm_pinctrl_request(pm_arg[0]);
276 SMC_RET1(handle, (uint64_t)ret);
277
278 case PM_PINCTRL_RELEASE:
279 ret = pm_pinctrl_release(pm_arg[0]);
280 SMC_RET1(handle, (uint64_t)ret);
281
282 case PM_PINCTRL_GET_FUNCTION:
283 {
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800284 uint32_t value = 0;
Rajan Vaja83687612018-01-17 02:39:20 -0800285
286 ret = pm_pinctrl_get_function(pm_arg[0], &value);
287 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
288 }
289
290 case PM_PINCTRL_SET_FUNCTION:
291 ret = pm_pinctrl_set_function(pm_arg[0], pm_arg[1]);
292 SMC_RET1(handle, (uint64_t)ret);
293
294 case PM_PINCTRL_CONFIG_PARAM_GET:
295 {
296 uint32_t value;
297
298 ret = pm_pinctrl_get_config(pm_arg[0], pm_arg[1], &value);
299 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
300 }
301
302 case PM_PINCTRL_CONFIG_PARAM_SET:
303 ret = pm_pinctrl_set_config(pm_arg[0], pm_arg[1], pm_arg[2]);
304 SMC_RET1(handle, (uint64_t)ret);
305
Rajan Vaja5529a012018-01-17 02:39:23 -0800306 case PM_IOCTL:
307 {
308 uint32_t value;
309
310 ret = pm_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
311 pm_arg[3], &value);
312 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
313 }
314
Rajan Vaja35116132018-01-17 02:39:25 -0800315 case PM_QUERY_DATA:
316 {
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800317 uint32_t data[4] = { 0 };
Rajan Vaja35116132018-01-17 02:39:25 -0800318
319 ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
320 pm_arg[3], data);
321 SMC_RET2(handle, (uint64_t)data[0] | ((uint64_t)data[1] << 32),
322 (uint64_t)data[2] | ((uint64_t)data[3] << 32));
323 }
324
325 case PM_CLOCK_ENABLE:
326 ret = pm_clock_enable(pm_arg[0]);
327 SMC_RET1(handle, (uint64_t)ret);
328
329 case PM_CLOCK_DISABLE:
330 ret = pm_clock_disable(pm_arg[0]);
331 SMC_RET1(handle, (uint64_t)ret);
332
333 case PM_CLOCK_GETSTATE:
334 {
335 uint32_t value;
336
337 ret = pm_clock_getstate(pm_arg[0], &value);
338 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
339 }
340
341 case PM_CLOCK_SETDIVIDER:
342 ret = pm_clock_setdivider(pm_arg[0], pm_arg[1]);
343 SMC_RET1(handle, (uint64_t)ret);
344
345 case PM_CLOCK_GETDIVIDER:
346 {
347 uint32_t value;
348
349 ret = pm_clock_getdivider(pm_arg[0], &value);
350 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
351 }
352
353 case PM_CLOCK_SETRATE:
354 ret = pm_clock_setrate(pm_arg[0],
355 ((uint64_t)pm_arg[2]) << 32 | pm_arg[1]);
356
357 SMC_RET1(handle, (uint64_t)ret);
358
359 case PM_CLOCK_GETRATE:
360 {
361 uint64_t value;
362
363 ret = pm_clock_getrate(pm_arg[0], &value);
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800364 SMC_RET2(handle, (uint64_t)ret |
365 (((uint64_t)value & 0xFFFFFFFFU) << 32U),
366 (value >> 32U) & 0xFFFFFFFFU);
Rajan Vaja35116132018-01-17 02:39:25 -0800367
368 }
369
370 case PM_CLOCK_SETPARENT:
371 ret = pm_clock_setparent(pm_arg[0], pm_arg[1]);
372 SMC_RET1(handle, (uint64_t)ret);
373
374 case PM_CLOCK_GETPARENT:
375 {
376 uint32_t value;
377
378 ret = pm_clock_getparent(pm_arg[0], &value);
379 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
380 }
381
Rajan Vajac7ee23d2018-02-14 23:10:54 -0800382 case PM_GET_TRUSTZONE_VERSION:
383 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
384 ((uint64_t)ZYNQMP_TZ_VERSION << 32));
385
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +0530386 case PM_SET_SUSPEND_MODE:
387 ret = pm_set_suspend_mode(pm_arg[0]);
388 SMC_RET1(handle, (uint64_t)ret);
389
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800390 default:
391 WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid);
392 SMC_RET1(handle, SMC_UNK);
393 }
394}