blob: 22b6bc39f85e846f07ef766c57fcefc39bbd5ee3 [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
22
Soren Brinkmann76fcae32016-03-06 20:16:27 -080023/* 0 - UP, !0 - DOWN */
24static int32_t pm_down = !0;
25
26/**
27 * pm_context - Structure which contains data for power management
28 * @api_version version of PM API, must match with one on PMU side
29 * @payload payload array used to store received
30 * data from ipi buffer registers
31 */
32static struct {
33 uint32_t api_version;
34 uint32_t payload[PAYLOAD_ARG_CNT];
35} pm_ctx;
36
37/**
38 * pm_setup() - PM service setup
39 *
40 * @return On success, the initialization function must return 0.
41 * Any other return value will cause the framework to ignore
42 * the service
43 *
44 * Initialization functions for ZynqMP power management for
45 * communicaton with PMU.
46 *
47 * Called from sip_svc_setup initialization function with the
48 * rt_svc_init signature.
Soren Brinkmann76fcae32016-03-06 20:16:27 -080049 */
50int pm_setup(void)
51{
Wendy Liang328105c2017-10-03 23:21:11 -070052 int status, ret;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080053
54 if (!zynqmp_is_pmu_up())
55 return -ENODEV;
56
Wendy Liang328105c2017-10-03 23:21:11 -070057 status = pm_ipi_init(primary_proc);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080058
Wendy Liang328105c2017-10-03 23:21:11 -070059 if (status >= 0) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -080060 INFO("BL31: PM Service Init Complete: API v%d.%d\n",
61 PM_VERSION_MAJOR, PM_VERSION_MINOR);
Wendy Liang328105c2017-10-03 23:21:11 -070062 ret = 0;
63 } else {
Soren Brinkmann76fcae32016-03-06 20:16:27 -080064 INFO("BL31: PM Service Init Failed, Error Code %d!\n", status);
Wendy Liang328105c2017-10-03 23:21:11 -070065 ret = status;
66 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -080067
68 pm_down = status;
69
Wendy Liang328105c2017-10-03 23:21:11 -070070 return ret;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080071}
72
73/**
74 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
75 * @smc_fid - Function Identifier
76 * @x1 - x4 - Arguments
77 * @cookie - Unused
78 * @handler - Pointer to caller's context structure
79 *
80 * @return - Unused
81 *
82 * Determines that smc_fid is valid and supported PM SMC Function ID from the
83 * list of pm_api_ids, otherwise completes the request with
84 * the unknown SMC Function ID
85 *
86 * The SMC calls for PM service are forwarded from SIP Service SMC handler
87 * function with rt_svc_handle signature
88 */
89uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3,
90 uint64_t x4, void *cookie, void *handle, uint64_t flags)
91{
92 enum pm_ret_status ret;
93
94 uint32_t pm_arg[4];
95
96 /* Handle case where PM wasn't initialized properly */
97 if (pm_down)
98 SMC_RET1(handle, SMC_UNK);
99
100 pm_arg[0] = (uint32_t)x1;
101 pm_arg[1] = (uint32_t)(x1 >> 32);
102 pm_arg[2] = (uint32_t)x2;
103 pm_arg[3] = (uint32_t)(x2 >> 32);
104
105 switch (smc_fid & FUNCID_NUM_MASK) {
106 /* PM API Functions */
107 case PM_SELF_SUSPEND:
108 ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
109 pm_arg[3]);
110 SMC_RET1(handle, (uint64_t)ret);
111
112 case PM_REQ_SUSPEND:
113 ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
114 pm_arg[3]);
115 SMC_RET1(handle, (uint64_t)ret);
116
117 case PM_REQ_WAKEUP:
118 ret = pm_req_wakeup(pm_arg[0], pm_arg[1], pm_arg[2],
119 pm_arg[3]);
120 SMC_RET1(handle, (uint64_t)ret);
121
122 case PM_FORCE_POWERDOWN:
123 ret = pm_force_powerdown(pm_arg[0], pm_arg[1]);
124 SMC_RET1(handle, (uint64_t)ret);
125
126 case PM_ABORT_SUSPEND:
127 ret = pm_abort_suspend(pm_arg[0]);
128 SMC_RET1(handle, (uint64_t)ret);
129
130 case PM_SET_WAKEUP_SOURCE:
131 ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2]);
132 SMC_RET1(handle, (uint64_t)ret);
133
134 case PM_SYSTEM_SHUTDOWN:
Soren Brinkmann58fbb9b2016-09-02 09:50:54 -0700135 ret = pm_system_shutdown(pm_arg[0], pm_arg[1]);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800136 SMC_RET1(handle, (uint64_t)ret);
137
138 case PM_REQ_NODE:
139 ret = pm_req_node(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
140 SMC_RET1(handle, (uint64_t)ret);
141
142 case PM_RELEASE_NODE:
143 ret = pm_release_node(pm_arg[0]);
144 SMC_RET1(handle, (uint64_t)ret);
145
146 case PM_SET_REQUIREMENT:
147 ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2],
148 pm_arg[3]);
149 SMC_RET1(handle, (uint64_t)ret);
150
151 case PM_SET_MAX_LATENCY:
152 ret = pm_set_max_latency(pm_arg[0], pm_arg[1]);
153 SMC_RET1(handle, (uint64_t)ret);
154
155 case PM_GET_API_VERSION:
156 /* Check is PM API version already verified */
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700157 if (pm_ctx.api_version == PM_VERSION) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800158 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
159 ((uint64_t)PM_VERSION << 32));
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700160 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800161
162 ret = pm_get_api_version(&pm_ctx.api_version);
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700163 /*
164 * Enable IPI IRQ
165 * assume the rich OS is OK to handle callback IRQs now.
166 * Even if we were wrong, it would not enable the IRQ in
167 * the GIC.
168 */
Wendy Liang328105c2017-10-03 23:21:11 -0700169 pm_ipi_irq_enable(primary_proc);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800170 SMC_RET1(handle, (uint64_t)ret |
171 ((uint64_t)pm_ctx.api_version << 32));
172
173 case PM_SET_CONFIGURATION:
174 ret = pm_set_configuration(pm_arg[0]);
175 SMC_RET1(handle, (uint64_t)ret);
176
177 case PM_GET_NODE_STATUS:
178 ret = pm_get_node_status(pm_arg[0]);
179 SMC_RET1(handle, (uint64_t)ret);
180
181 case PM_GET_OP_CHARACTERISTIC:
Anes Hadziahmetagic92aee012016-05-12 16:17:30 +0200182 {
183 uint32_t result;
184
185 ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result);
186 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32));
187 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800188
189 case PM_REGISTER_NOTIFIER:
190 ret = pm_register_notifier(pm_arg[0], pm_arg[1], pm_arg[2],
191 pm_arg[3]);
192 SMC_RET1(handle, (uint64_t)ret);
193
194 case PM_RESET_ASSERT:
195 ret = pm_reset_assert(pm_arg[0], pm_arg[1]);
196 SMC_RET1(handle, (uint64_t)ret);
197
198 case PM_RESET_GET_STATUS:
199 {
200 uint32_t reset_status;
201
202 ret = pm_reset_get_status(pm_arg[0], &reset_status);
203 SMC_RET1(handle, (uint64_t)ret |
204 ((uint64_t)reset_status << 32));
205 }
206
207 /* PM memory access functions */
208 case PM_MMIO_WRITE:
209 ret = pm_mmio_write(pm_arg[0], pm_arg[1], pm_arg[2]);
210 SMC_RET1(handle, (uint64_t)ret);
211
212 case PM_MMIO_READ:
213 {
214 uint32_t value;
215
216 ret = pm_mmio_read(pm_arg[0], &value);
217 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
218 }
Nava kishore Manne68d460c2016-08-20 23:18:09 +0530219
220 case PM_FPGA_LOAD:
221 ret = pm_fpga_load(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
222 SMC_RET1(handle, (uint64_t)ret);
223
224 case PM_FPGA_GET_STATUS:
225 {
226 uint32_t value;
227
228 ret = pm_fpga_get_status(&value);
229 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
230 }
231
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530232 case PM_GET_CHIPID:
Soren Brinkmanncb366812016-09-22 12:21:11 -0700233 {
234 uint32_t result[2];
235
236 ret = pm_get_chipid(result);
237 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32),
238 result[1]);
239 }
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530240
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700241 case PM_GET_CALLBACK_DATA:
242 {
243 uint32_t result[4];
244
245 pm_get_callbackdata(result, sizeof(result));
246 SMC_RET2(handle,
247 (uint64_t)result[0] | ((uint64_t)result[1] << 32),
248 (uint64_t)result[2] | ((uint64_t)result[3] << 32));
249 }
250
Rajan Vaja83687612018-01-17 02:39:20 -0800251 case PM_PINCTRL_REQUEST:
252 ret = pm_pinctrl_request(pm_arg[0]);
253 SMC_RET1(handle, (uint64_t)ret);
254
255 case PM_PINCTRL_RELEASE:
256 ret = pm_pinctrl_release(pm_arg[0]);
257 SMC_RET1(handle, (uint64_t)ret);
258
259 case PM_PINCTRL_GET_FUNCTION:
260 {
261 uint32_t value;
262
263 ret = pm_pinctrl_get_function(pm_arg[0], &value);
264 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
265 }
266
267 case PM_PINCTRL_SET_FUNCTION:
268 ret = pm_pinctrl_set_function(pm_arg[0], pm_arg[1]);
269 SMC_RET1(handle, (uint64_t)ret);
270
271 case PM_PINCTRL_CONFIG_PARAM_GET:
272 {
273 uint32_t value;
274
275 ret = pm_pinctrl_get_config(pm_arg[0], pm_arg[1], &value);
276 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
277 }
278
279 case PM_PINCTRL_CONFIG_PARAM_SET:
280 ret = pm_pinctrl_set_config(pm_arg[0], pm_arg[1], pm_arg[2]);
281 SMC_RET1(handle, (uint64_t)ret);
282
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800283 default:
284 WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid);
285 SMC_RET1(handle, SMC_UNK);
286 }
287}