blob: 34b3ad4527b6c03d690e349e8178db78f9152747 [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:
179 ret = pm_get_node_status(pm_arg[0]);
180 SMC_RET1(handle, (uint64_t)ret);
181
182 case PM_GET_OP_CHARACTERISTIC:
Anes Hadziahmetagic92aee012016-05-12 16:17:30 +0200183 {
184 uint32_t result;
185
186 ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result);
187 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32));
188 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800189
190 case PM_REGISTER_NOTIFIER:
191 ret = pm_register_notifier(pm_arg[0], pm_arg[1], pm_arg[2],
192 pm_arg[3]);
193 SMC_RET1(handle, (uint64_t)ret);
194
195 case PM_RESET_ASSERT:
196 ret = pm_reset_assert(pm_arg[0], pm_arg[1]);
197 SMC_RET1(handle, (uint64_t)ret);
198
199 case PM_RESET_GET_STATUS:
200 {
201 uint32_t reset_status;
202
203 ret = pm_reset_get_status(pm_arg[0], &reset_status);
204 SMC_RET1(handle, (uint64_t)ret |
205 ((uint64_t)reset_status << 32));
206 }
207
208 /* PM memory access functions */
209 case PM_MMIO_WRITE:
210 ret = pm_mmio_write(pm_arg[0], pm_arg[1], pm_arg[2]);
211 SMC_RET1(handle, (uint64_t)ret);
212
213 case PM_MMIO_READ:
214 {
215 uint32_t value;
216
217 ret = pm_mmio_read(pm_arg[0], &value);
218 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
219 }
Nava kishore Manne68d460c2016-08-20 23:18:09 +0530220
221 case PM_FPGA_LOAD:
222 ret = pm_fpga_load(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
223 SMC_RET1(handle, (uint64_t)ret);
224
225 case PM_FPGA_GET_STATUS:
226 {
227 uint32_t value;
228
229 ret = pm_fpga_get_status(&value);
230 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
231 }
232
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530233 case PM_GET_CHIPID:
Soren Brinkmanncb366812016-09-22 12:21:11 -0700234 {
235 uint32_t result[2];
236
237 ret = pm_get_chipid(result);
238 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32),
239 result[1]);
240 }
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530241
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700242 case PM_GET_CALLBACK_DATA:
243 {
244 uint32_t result[4];
245
246 pm_get_callbackdata(result, sizeof(result));
247 SMC_RET2(handle,
248 (uint64_t)result[0] | ((uint64_t)result[1] << 32),
249 (uint64_t)result[2] | ((uint64_t)result[3] << 32));
250 }
251
Rajan Vaja83687612018-01-17 02:39:20 -0800252 case PM_PINCTRL_REQUEST:
253 ret = pm_pinctrl_request(pm_arg[0]);
254 SMC_RET1(handle, (uint64_t)ret);
255
256 case PM_PINCTRL_RELEASE:
257 ret = pm_pinctrl_release(pm_arg[0]);
258 SMC_RET1(handle, (uint64_t)ret);
259
260 case PM_PINCTRL_GET_FUNCTION:
261 {
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800262 uint32_t value = 0;
Rajan Vaja83687612018-01-17 02:39:20 -0800263
264 ret = pm_pinctrl_get_function(pm_arg[0], &value);
265 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
266 }
267
268 case PM_PINCTRL_SET_FUNCTION:
269 ret = pm_pinctrl_set_function(pm_arg[0], pm_arg[1]);
270 SMC_RET1(handle, (uint64_t)ret);
271
272 case PM_PINCTRL_CONFIG_PARAM_GET:
273 {
274 uint32_t value;
275
276 ret = pm_pinctrl_get_config(pm_arg[0], pm_arg[1], &value);
277 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
278 }
279
280 case PM_PINCTRL_CONFIG_PARAM_SET:
281 ret = pm_pinctrl_set_config(pm_arg[0], pm_arg[1], pm_arg[2]);
282 SMC_RET1(handle, (uint64_t)ret);
283
Rajan Vaja5529a012018-01-17 02:39:23 -0800284 case PM_IOCTL:
285 {
286 uint32_t value;
287
288 ret = pm_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
289 pm_arg[3], &value);
290 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
291 }
292
Rajan Vaja35116132018-01-17 02:39:25 -0800293 case PM_QUERY_DATA:
294 {
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800295 uint32_t data[4] = { 0 };
Rajan Vaja35116132018-01-17 02:39:25 -0800296
297 ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
298 pm_arg[3], data);
299 SMC_RET2(handle, (uint64_t)data[0] | ((uint64_t)data[1] << 32),
300 (uint64_t)data[2] | ((uint64_t)data[3] << 32));
301 }
302
303 case PM_CLOCK_ENABLE:
304 ret = pm_clock_enable(pm_arg[0]);
305 SMC_RET1(handle, (uint64_t)ret);
306
307 case PM_CLOCK_DISABLE:
308 ret = pm_clock_disable(pm_arg[0]);
309 SMC_RET1(handle, (uint64_t)ret);
310
311 case PM_CLOCK_GETSTATE:
312 {
313 uint32_t value;
314
315 ret = pm_clock_getstate(pm_arg[0], &value);
316 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
317 }
318
319 case PM_CLOCK_SETDIVIDER:
320 ret = pm_clock_setdivider(pm_arg[0], pm_arg[1]);
321 SMC_RET1(handle, (uint64_t)ret);
322
323 case PM_CLOCK_GETDIVIDER:
324 {
325 uint32_t value;
326
327 ret = pm_clock_getdivider(pm_arg[0], &value);
328 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
329 }
330
331 case PM_CLOCK_SETRATE:
332 ret = pm_clock_setrate(pm_arg[0],
333 ((uint64_t)pm_arg[2]) << 32 | pm_arg[1]);
334
335 SMC_RET1(handle, (uint64_t)ret);
336
337 case PM_CLOCK_GETRATE:
338 {
339 uint64_t value;
340
341 ret = pm_clock_getrate(pm_arg[0], &value);
Jolly Shah69fb5bf2018-02-07 16:25:41 -0800342 SMC_RET2(handle, (uint64_t)ret |
343 (((uint64_t)value & 0xFFFFFFFFU) << 32U),
344 (value >> 32U) & 0xFFFFFFFFU);
Rajan Vaja35116132018-01-17 02:39:25 -0800345
346 }
347
348 case PM_CLOCK_SETPARENT:
349 ret = pm_clock_setparent(pm_arg[0], pm_arg[1]);
350 SMC_RET1(handle, (uint64_t)ret);
351
352 case PM_CLOCK_GETPARENT:
353 {
354 uint32_t value;
355
356 ret = pm_clock_getparent(pm_arg[0], &value);
357 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
358 }
359
Rajan Vajac7ee23d2018-02-14 23:10:54 -0800360 case PM_GET_TRUSTZONE_VERSION:
361 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
362 ((uint64_t)ZYNQMP_TZ_VERSION << 32));
363
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800364 default:
365 WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid);
366 SMC_RET1(handle, SMC_UNK);
367 }
368}