blob: 69e711aa24f1304bdbf0760d538c37dcb471011c [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
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700255 case PM_GET_CALLBACK_DATA:
256 {
257 uint32_t result[4];
258
259 pm_get_callbackdata(result, sizeof(result));
260 SMC_RET2(handle,
261 (uint64_t)result[0] | ((uint64_t)result[1] << 32),
262 (uint64_t)result[2] | ((uint64_t)result[3] << 32));
263 }
264
Rajan Vaja83687612018-01-17 02:39:20 -0800265 case PM_PINCTRL_REQUEST:
266 ret = pm_pinctrl_request(pm_arg[0]);
267 SMC_RET1(handle, (uint64_t)ret);
268
269 case PM_PINCTRL_RELEASE:
270 ret = pm_pinctrl_release(pm_arg[0]);
271 SMC_RET1(handle, (uint64_t)ret);
272
273 case PM_PINCTRL_GET_FUNCTION:
274 {
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800275 uint32_t value = 0;
Rajan Vaja83687612018-01-17 02:39:20 -0800276
277 ret = pm_pinctrl_get_function(pm_arg[0], &value);
278 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
279 }
280
281 case PM_PINCTRL_SET_FUNCTION:
282 ret = pm_pinctrl_set_function(pm_arg[0], pm_arg[1]);
283 SMC_RET1(handle, (uint64_t)ret);
284
285 case PM_PINCTRL_CONFIG_PARAM_GET:
286 {
287 uint32_t value;
288
289 ret = pm_pinctrl_get_config(pm_arg[0], pm_arg[1], &value);
290 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
291 }
292
293 case PM_PINCTRL_CONFIG_PARAM_SET:
294 ret = pm_pinctrl_set_config(pm_arg[0], pm_arg[1], pm_arg[2]);
295 SMC_RET1(handle, (uint64_t)ret);
296
Rajan Vaja5529a012018-01-17 02:39:23 -0800297 case PM_IOCTL:
298 {
299 uint32_t value;
300
301 ret = pm_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
302 pm_arg[3], &value);
303 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
304 }
305
Rajan Vaja35116132018-01-17 02:39:25 -0800306 case PM_QUERY_DATA:
307 {
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800308 uint32_t data[4] = { 0 };
Rajan Vaja35116132018-01-17 02:39:25 -0800309
310 ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
311 pm_arg[3], data);
312 SMC_RET2(handle, (uint64_t)data[0] | ((uint64_t)data[1] << 32),
313 (uint64_t)data[2] | ((uint64_t)data[3] << 32));
314 }
315
316 case PM_CLOCK_ENABLE:
317 ret = pm_clock_enable(pm_arg[0]);
318 SMC_RET1(handle, (uint64_t)ret);
319
320 case PM_CLOCK_DISABLE:
321 ret = pm_clock_disable(pm_arg[0]);
322 SMC_RET1(handle, (uint64_t)ret);
323
324 case PM_CLOCK_GETSTATE:
325 {
326 uint32_t value;
327
328 ret = pm_clock_getstate(pm_arg[0], &value);
329 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
330 }
331
332 case PM_CLOCK_SETDIVIDER:
333 ret = pm_clock_setdivider(pm_arg[0], pm_arg[1]);
334 SMC_RET1(handle, (uint64_t)ret);
335
336 case PM_CLOCK_GETDIVIDER:
337 {
338 uint32_t value;
339
340 ret = pm_clock_getdivider(pm_arg[0], &value);
341 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
342 }
343
344 case PM_CLOCK_SETRATE:
345 ret = pm_clock_setrate(pm_arg[0],
346 ((uint64_t)pm_arg[2]) << 32 | pm_arg[1]);
347
348 SMC_RET1(handle, (uint64_t)ret);
349
350 case PM_CLOCK_GETRATE:
351 {
352 uint64_t value;
353
354 ret = pm_clock_getrate(pm_arg[0], &value);
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800355 SMC_RET2(handle, (uint64_t)ret |
356 (((uint64_t)value & 0xFFFFFFFFU) << 32U),
357 (value >> 32U) & 0xFFFFFFFFU);
Rajan Vaja35116132018-01-17 02:39:25 -0800358
359 }
360
361 case PM_CLOCK_SETPARENT:
362 ret = pm_clock_setparent(pm_arg[0], pm_arg[1]);
363 SMC_RET1(handle, (uint64_t)ret);
364
365 case PM_CLOCK_GETPARENT:
366 {
367 uint32_t value;
368
369 ret = pm_clock_getparent(pm_arg[0], &value);
370 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
371 }
372
Rajan Vajac7ee23d2018-02-14 23:10:54 -0800373 case PM_GET_TRUSTZONE_VERSION:
374 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
375 ((uint64_t)ZYNQMP_TZ_VERSION << 32));
376
Siva Durga Prasad Paladugu43b23a32018-04-27 16:26:47 +0530377 case PM_SET_SUSPEND_MODE:
378 ret = pm_set_suspend_mode(pm_arg[0]);
379 SMC_RET1(handle, (uint64_t)ret);
380
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800381 default:
382 WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid);
383 SMC_RET1(handle, SMC_UNK);
384 }
385}