blob: 02d2f2d2f03f14350627bfac4d4a07ac2d17c187 [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
186 case PM_GET_NODE_STATUS:
Anes Hadziahmetagic1caf88e2017-01-27 18:42:44 +0100187 {
188 uint32_t buff[3];
189
190 ret = pm_get_node_status(pm_arg[0], buff);
191 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buff[0] << 32),
192 (uint64_t)buff[1] | ((uint64_t)buff[2] << 32));
193 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800194
195 case PM_GET_OP_CHARACTERISTIC:
Anes Hadziahmetagic92aee012016-05-12 16:17:30 +0200196 {
197 uint32_t result;
198
199 ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result);
200 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32));
201 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800202
203 case PM_REGISTER_NOTIFIER:
204 ret = pm_register_notifier(pm_arg[0], pm_arg[1], pm_arg[2],
205 pm_arg[3]);
206 SMC_RET1(handle, (uint64_t)ret);
207
208 case PM_RESET_ASSERT:
209 ret = pm_reset_assert(pm_arg[0], pm_arg[1]);
210 SMC_RET1(handle, (uint64_t)ret);
211
212 case PM_RESET_GET_STATUS:
213 {
214 uint32_t reset_status;
215
216 ret = pm_reset_get_status(pm_arg[0], &reset_status);
217 SMC_RET1(handle, (uint64_t)ret |
218 ((uint64_t)reset_status << 32));
219 }
220
221 /* PM memory access functions */
222 case PM_MMIO_WRITE:
223 ret = pm_mmio_write(pm_arg[0], pm_arg[1], pm_arg[2]);
224 SMC_RET1(handle, (uint64_t)ret);
225
226 case PM_MMIO_READ:
227 {
228 uint32_t value;
229
230 ret = pm_mmio_read(pm_arg[0], &value);
231 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
232 }
Nava kishore Manne68d460c2016-08-20 23:18:09 +0530233
234 case PM_FPGA_LOAD:
235 ret = pm_fpga_load(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
236 SMC_RET1(handle, (uint64_t)ret);
237
238 case PM_FPGA_GET_STATUS:
239 {
240 uint32_t value;
241
242 ret = pm_fpga_get_status(&value);
243 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
244 }
245
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530246 case PM_GET_CHIPID:
Soren Brinkmanncb366812016-09-22 12:21:11 -0700247 {
248 uint32_t result[2];
249
250 ret = pm_get_chipid(result);
251 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32),
252 result[1]);
253 }
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530254
Siva Durga Prasad Paladugude93d982018-04-30 15:49:27 +0530255 case PM_SECURE_RSA_AES:
256 ret = pm_secure_rsaaes(pm_arg[0], pm_arg[1], pm_arg[2],
257 pm_arg[3]);
258 SMC_RET1(handle, (uint64_t)ret);
259
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700260 case PM_GET_CALLBACK_DATA:
261 {
262 uint32_t result[4];
263
264 pm_get_callbackdata(result, sizeof(result));
265 SMC_RET2(handle,
266 (uint64_t)result[0] | ((uint64_t)result[1] << 32),
267 (uint64_t)result[2] | ((uint64_t)result[3] << 32));
268 }
269
Rajan Vaja83687612018-01-17 02:39:20 -0800270 case PM_PINCTRL_REQUEST:
271 ret = pm_pinctrl_request(pm_arg[0]);
272 SMC_RET1(handle, (uint64_t)ret);
273
274 case PM_PINCTRL_RELEASE:
275 ret = pm_pinctrl_release(pm_arg[0]);
276 SMC_RET1(handle, (uint64_t)ret);
277
278 case PM_PINCTRL_GET_FUNCTION:
279 {
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800280 uint32_t value = 0;
Rajan Vaja83687612018-01-17 02:39:20 -0800281
282 ret = pm_pinctrl_get_function(pm_arg[0], &value);
283 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
284 }
285
286 case PM_PINCTRL_SET_FUNCTION:
287 ret = pm_pinctrl_set_function(pm_arg[0], pm_arg[1]);
288 SMC_RET1(handle, (uint64_t)ret);
289
290 case PM_PINCTRL_CONFIG_PARAM_GET:
291 {
292 uint32_t value;
293
294 ret = pm_pinctrl_get_config(pm_arg[0], pm_arg[1], &value);
295 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
296 }
297
298 case PM_PINCTRL_CONFIG_PARAM_SET:
299 ret = pm_pinctrl_set_config(pm_arg[0], pm_arg[1], pm_arg[2]);
300 SMC_RET1(handle, (uint64_t)ret);
301
Rajan Vaja5529a012018-01-17 02:39:23 -0800302 case PM_IOCTL:
303 {
304 uint32_t value;
305
306 ret = pm_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
307 pm_arg[3], &value);
308 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
309 }
310
Rajan Vaja35116132018-01-17 02:39:25 -0800311 case PM_QUERY_DATA:
312 {
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800313 uint32_t data[4] = { 0 };
Rajan Vaja35116132018-01-17 02:39:25 -0800314
315 ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
316 pm_arg[3], data);
317 SMC_RET2(handle, (uint64_t)data[0] | ((uint64_t)data[1] << 32),
318 (uint64_t)data[2] | ((uint64_t)data[3] << 32));
319 }
320
321 case PM_CLOCK_ENABLE:
322 ret = pm_clock_enable(pm_arg[0]);
323 SMC_RET1(handle, (uint64_t)ret);
324
325 case PM_CLOCK_DISABLE:
326 ret = pm_clock_disable(pm_arg[0]);
327 SMC_RET1(handle, (uint64_t)ret);
328
329 case PM_CLOCK_GETSTATE:
330 {
331 uint32_t value;
332
333 ret = pm_clock_getstate(pm_arg[0], &value);
334 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
335 }
336
337 case PM_CLOCK_SETDIVIDER:
338 ret = pm_clock_setdivider(pm_arg[0], pm_arg[1]);
339 SMC_RET1(handle, (uint64_t)ret);
340
341 case PM_CLOCK_GETDIVIDER:
342 {
343 uint32_t value;
344
345 ret = pm_clock_getdivider(pm_arg[0], &value);
346 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
347 }
348
349 case PM_CLOCK_SETRATE:
350 ret = pm_clock_setrate(pm_arg[0],
351 ((uint64_t)pm_arg[2]) << 32 | pm_arg[1]);
352
353 SMC_RET1(handle, (uint64_t)ret);
354
355 case PM_CLOCK_GETRATE:
356 {
357 uint64_t value;
358
359 ret = pm_clock_getrate(pm_arg[0], &value);
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800360 SMC_RET2(handle, (uint64_t)ret |
361 (((uint64_t)value & 0xFFFFFFFFU) << 32U),
362 (value >> 32U) & 0xFFFFFFFFU);
Rajan Vaja35116132018-01-17 02:39:25 -0800363
364 }
365
366 case PM_CLOCK_SETPARENT:
367 ret = pm_clock_setparent(pm_arg[0], pm_arg[1]);
368 SMC_RET1(handle, (uint64_t)ret);
369
370 case PM_CLOCK_GETPARENT:
371 {
372 uint32_t value;
373
374 ret = pm_clock_getparent(pm_arg[0], &value);
375 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
376 }
377
Rajan Vajac7ee23d2018-02-14 23:10:54 -0800378 case PM_GET_TRUSTZONE_VERSION:
379 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
380 ((uint64_t)ZYNQMP_TZ_VERSION << 32));
381
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +0530382 case PM_SET_SUSPEND_MODE:
383 ret = pm_set_suspend_mode(pm_arg[0]);
384 SMC_RET1(handle, (uint64_t)ret);
385
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800386 default:
387 WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid);
388 SMC_RET1(handle, SMC_UNK);
389 }
390}