blob: ec9a93ecfd3a0110a68ebbf7e7e102494cff16d6 [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
Rajan Vajac7ee23d2018-02-14 23:10:54 -080022#define PM_GET_TRUSTZONE_VERSION 0xa03
Soren Brinkmann84f0af42016-09-30 14:24:25 -070023
Soren Brinkmann76fcae32016-03-06 20:16:27 -080024/* 0 - UP, !0 - DOWN */
25static int32_t pm_down = !0;
26
27/**
28 * pm_context - Structure which contains data for power management
29 * @api_version version of PM API, must match with one on PMU side
30 * @payload payload array used to store received
31 * data from ipi buffer registers
32 */
33static struct {
34 uint32_t api_version;
35 uint32_t payload[PAYLOAD_ARG_CNT];
36} pm_ctx;
37
38/**
39 * pm_setup() - PM service setup
40 *
41 * @return On success, the initialization function must return 0.
42 * Any other return value will cause the framework to ignore
43 * the service
44 *
45 * Initialization functions for ZynqMP power management for
46 * communicaton with PMU.
47 *
48 * Called from sip_svc_setup initialization function with the
49 * rt_svc_init signature.
Soren Brinkmann76fcae32016-03-06 20:16:27 -080050 */
51int pm_setup(void)
52{
Wendy Liang328105c2017-10-03 23:21:11 -070053 int status, ret;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080054
55 if (!zynqmp_is_pmu_up())
56 return -ENODEV;
57
Wendy Liang328105c2017-10-03 23:21:11 -070058 status = pm_ipi_init(primary_proc);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080059
Wendy Liang328105c2017-10-03 23:21:11 -070060 if (status >= 0) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -080061 INFO("BL31: PM Service Init Complete: API v%d.%d\n",
62 PM_VERSION_MAJOR, PM_VERSION_MINOR);
Wendy Liang328105c2017-10-03 23:21:11 -070063 ret = 0;
64 } else {
Soren Brinkmann76fcae32016-03-06 20:16:27 -080065 INFO("BL31: PM Service Init Failed, Error Code %d!\n", status);
Wendy Liang328105c2017-10-03 23:21:11 -070066 ret = status;
67 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -080068
69 pm_down = status;
70
Wendy Liang328105c2017-10-03 23:21:11 -070071 return ret;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080072}
73
74/**
75 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
76 * @smc_fid - Function Identifier
77 * @x1 - x4 - Arguments
78 * @cookie - Unused
79 * @handler - Pointer to caller's context structure
80 *
81 * @return - Unused
82 *
83 * Determines that smc_fid is valid and supported PM SMC Function ID from the
84 * list of pm_api_ids, otherwise completes the request with
85 * the unknown SMC Function ID
86 *
87 * The SMC calls for PM service are forwarded from SIP Service SMC handler
88 * function with rt_svc_handle signature
89 */
90uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3,
91 uint64_t x4, void *cookie, void *handle, uint64_t flags)
92{
93 enum pm_ret_status ret;
94
95 uint32_t pm_arg[4];
96
97 /* Handle case where PM wasn't initialized properly */
98 if (pm_down)
99 SMC_RET1(handle, SMC_UNK);
100
101 pm_arg[0] = (uint32_t)x1;
102 pm_arg[1] = (uint32_t)(x1 >> 32);
103 pm_arg[2] = (uint32_t)x2;
104 pm_arg[3] = (uint32_t)(x2 >> 32);
105
106 switch (smc_fid & FUNCID_NUM_MASK) {
107 /* PM API Functions */
108 case PM_SELF_SUSPEND:
109 ret = pm_self_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_SUSPEND:
114 ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
115 pm_arg[3]);
116 SMC_RET1(handle, (uint64_t)ret);
117
118 case PM_REQ_WAKEUP:
119 ret = pm_req_wakeup(pm_arg[0], pm_arg[1], pm_arg[2],
120 pm_arg[3]);
121 SMC_RET1(handle, (uint64_t)ret);
122
123 case PM_FORCE_POWERDOWN:
124 ret = pm_force_powerdown(pm_arg[0], pm_arg[1]);
125 SMC_RET1(handle, (uint64_t)ret);
126
127 case PM_ABORT_SUSPEND:
128 ret = pm_abort_suspend(pm_arg[0]);
129 SMC_RET1(handle, (uint64_t)ret);
130
131 case PM_SET_WAKEUP_SOURCE:
132 ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2]);
133 SMC_RET1(handle, (uint64_t)ret);
134
135 case PM_SYSTEM_SHUTDOWN:
Soren Brinkmann58fbb9b2016-09-02 09:50:54 -0700136 ret = pm_system_shutdown(pm_arg[0], pm_arg[1]);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800137 SMC_RET1(handle, (uint64_t)ret);
138
139 case PM_REQ_NODE:
140 ret = pm_req_node(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
141 SMC_RET1(handle, (uint64_t)ret);
142
143 case PM_RELEASE_NODE:
144 ret = pm_release_node(pm_arg[0]);
145 SMC_RET1(handle, (uint64_t)ret);
146
147 case PM_SET_REQUIREMENT:
148 ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2],
149 pm_arg[3]);
150 SMC_RET1(handle, (uint64_t)ret);
151
152 case PM_SET_MAX_LATENCY:
153 ret = pm_set_max_latency(pm_arg[0], pm_arg[1]);
154 SMC_RET1(handle, (uint64_t)ret);
155
156 case PM_GET_API_VERSION:
157 /* Check is PM API version already verified */
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700158 if (pm_ctx.api_version == PM_VERSION) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800159 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
160 ((uint64_t)PM_VERSION << 32));
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700161 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800162
163 ret = pm_get_api_version(&pm_ctx.api_version);
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700164 /*
165 * Enable IPI IRQ
166 * assume the rich OS is OK to handle callback IRQs now.
167 * Even if we were wrong, it would not enable the IRQ in
168 * the GIC.
169 */
Wendy Liang328105c2017-10-03 23:21:11 -0700170 pm_ipi_irq_enable(primary_proc);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800171 SMC_RET1(handle, (uint64_t)ret |
172 ((uint64_t)pm_ctx.api_version << 32));
173
174 case PM_SET_CONFIGURATION:
175 ret = pm_set_configuration(pm_arg[0]);
176 SMC_RET1(handle, (uint64_t)ret);
177
178 case PM_GET_NODE_STATUS:
Anes Hadziahmetagic1caf88e2017-01-27 18:42:44 +0100179 {
180 uint32_t buff[3];
181
182 ret = pm_get_node_status(pm_arg[0], buff);
183 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buff[0] << 32),
184 (uint64_t)buff[1] | ((uint64_t)buff[2] << 32));
185 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800186
187 case PM_GET_OP_CHARACTERISTIC:
Anes Hadziahmetagic92aee012016-05-12 16:17:30 +0200188 {
189 uint32_t result;
190
191 ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result);
192 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32));
193 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800194
195 case PM_REGISTER_NOTIFIER:
196 ret = pm_register_notifier(pm_arg[0], pm_arg[1], pm_arg[2],
197 pm_arg[3]);
198 SMC_RET1(handle, (uint64_t)ret);
199
200 case PM_RESET_ASSERT:
201 ret = pm_reset_assert(pm_arg[0], pm_arg[1]);
202 SMC_RET1(handle, (uint64_t)ret);
203
204 case PM_RESET_GET_STATUS:
205 {
206 uint32_t reset_status;
207
208 ret = pm_reset_get_status(pm_arg[0], &reset_status);
209 SMC_RET1(handle, (uint64_t)ret |
210 ((uint64_t)reset_status << 32));
211 }
212
213 /* PM memory access functions */
214 case PM_MMIO_WRITE:
215 ret = pm_mmio_write(pm_arg[0], pm_arg[1], pm_arg[2]);
216 SMC_RET1(handle, (uint64_t)ret);
217
218 case PM_MMIO_READ:
219 {
220 uint32_t value;
221
222 ret = pm_mmio_read(pm_arg[0], &value);
223 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
224 }
Nava kishore Manne68d460c2016-08-20 23:18:09 +0530225
226 case PM_FPGA_LOAD:
227 ret = pm_fpga_load(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
228 SMC_RET1(handle, (uint64_t)ret);
229
230 case PM_FPGA_GET_STATUS:
231 {
232 uint32_t value;
233
234 ret = pm_fpga_get_status(&value);
235 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
236 }
237
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530238 case PM_GET_CHIPID:
Soren Brinkmanncb366812016-09-22 12:21:11 -0700239 {
240 uint32_t result[2];
241
242 ret = pm_get_chipid(result);
243 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32),
244 result[1]);
245 }
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530246
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700247 case PM_GET_CALLBACK_DATA:
248 {
249 uint32_t result[4];
250
251 pm_get_callbackdata(result, sizeof(result));
252 SMC_RET2(handle,
253 (uint64_t)result[0] | ((uint64_t)result[1] << 32),
254 (uint64_t)result[2] | ((uint64_t)result[3] << 32));
255 }
256
Rajan Vaja83687612018-01-17 02:39:20 -0800257 case PM_PINCTRL_REQUEST:
258 ret = pm_pinctrl_request(pm_arg[0]);
259 SMC_RET1(handle, (uint64_t)ret);
260
261 case PM_PINCTRL_RELEASE:
262 ret = pm_pinctrl_release(pm_arg[0]);
263 SMC_RET1(handle, (uint64_t)ret);
264
265 case PM_PINCTRL_GET_FUNCTION:
266 {
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800267 uint32_t value = 0;
Rajan Vaja83687612018-01-17 02:39:20 -0800268
269 ret = pm_pinctrl_get_function(pm_arg[0], &value);
270 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
271 }
272
273 case PM_PINCTRL_SET_FUNCTION:
274 ret = pm_pinctrl_set_function(pm_arg[0], pm_arg[1]);
275 SMC_RET1(handle, (uint64_t)ret);
276
277 case PM_PINCTRL_CONFIG_PARAM_GET:
278 {
279 uint32_t value;
280
281 ret = pm_pinctrl_get_config(pm_arg[0], pm_arg[1], &value);
282 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
283 }
284
285 case PM_PINCTRL_CONFIG_PARAM_SET:
286 ret = pm_pinctrl_set_config(pm_arg[0], pm_arg[1], pm_arg[2]);
287 SMC_RET1(handle, (uint64_t)ret);
288
Rajan Vaja5529a012018-01-17 02:39:23 -0800289 case PM_IOCTL:
290 {
291 uint32_t value;
292
293 ret = pm_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
294 pm_arg[3], &value);
295 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
296 }
297
Rajan Vaja35116132018-01-17 02:39:25 -0800298 case PM_QUERY_DATA:
299 {
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800300 uint32_t data[4] = { 0 };
Rajan Vaja35116132018-01-17 02:39:25 -0800301
302 ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
303 pm_arg[3], data);
304 SMC_RET2(handle, (uint64_t)data[0] | ((uint64_t)data[1] << 32),
305 (uint64_t)data[2] | ((uint64_t)data[3] << 32));
306 }
307
308 case PM_CLOCK_ENABLE:
309 ret = pm_clock_enable(pm_arg[0]);
310 SMC_RET1(handle, (uint64_t)ret);
311
312 case PM_CLOCK_DISABLE:
313 ret = pm_clock_disable(pm_arg[0]);
314 SMC_RET1(handle, (uint64_t)ret);
315
316 case PM_CLOCK_GETSTATE:
317 {
318 uint32_t value;
319
320 ret = pm_clock_getstate(pm_arg[0], &value);
321 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
322 }
323
324 case PM_CLOCK_SETDIVIDER:
325 ret = pm_clock_setdivider(pm_arg[0], pm_arg[1]);
326 SMC_RET1(handle, (uint64_t)ret);
327
328 case PM_CLOCK_GETDIVIDER:
329 {
330 uint32_t value;
331
332 ret = pm_clock_getdivider(pm_arg[0], &value);
333 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
334 }
335
336 case PM_CLOCK_SETRATE:
337 ret = pm_clock_setrate(pm_arg[0],
338 ((uint64_t)pm_arg[2]) << 32 | pm_arg[1]);
339
340 SMC_RET1(handle, (uint64_t)ret);
341
342 case PM_CLOCK_GETRATE:
343 {
344 uint64_t value;
345
346 ret = pm_clock_getrate(pm_arg[0], &value);
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800347 SMC_RET2(handle, (uint64_t)ret |
348 (((uint64_t)value & 0xFFFFFFFFU) << 32U),
349 (value >> 32U) & 0xFFFFFFFFU);
Rajan Vaja35116132018-01-17 02:39:25 -0800350
351 }
352
353 case PM_CLOCK_SETPARENT:
354 ret = pm_clock_setparent(pm_arg[0], pm_arg[1]);
355 SMC_RET1(handle, (uint64_t)ret);
356
357 case PM_CLOCK_GETPARENT:
358 {
359 uint32_t value;
360
361 ret = pm_clock_getparent(pm_arg[0], &value);
362 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
363 }
364
Rajan Vajac7ee23d2018-02-14 23:10:54 -0800365 case PM_GET_TRUSTZONE_VERSION:
366 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
367 ((uint64_t)ZYNQMP_TZ_VERSION << 32));
368
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800369 default:
370 WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid);
371 SMC_RET1(handle, SMC_UNK);
372 }
373}