blob: 036ed8a20299608a88a85db2e8d0e67f4f5d6a7a [file] [log] [blame]
Soren Brinkmann76fcae32016-03-06 20:16:27 -08001/*
2 * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/*
32 * Top-level SMC handler for ZynqMP power management calls and
33 * IPI setup functions for communication with PMU.
34 */
35
36#include <errno.h>
37#include <gic_common.h>
38#include <runtime_svc.h>
39#include <string.h>
40#include "pm_api_sys.h"
41#include "pm_client.h"
42#include "pm_ipi.h"
43#include "../zynqmp_private.h"
44
Soren Brinkmann84f0af42016-09-30 14:24:25 -070045#define PM_GET_CALLBACK_DATA 0xa01
46
Soren Brinkmann76fcae32016-03-06 20:16:27 -080047/* 0 - UP, !0 - DOWN */
48static int32_t pm_down = !0;
49
50/**
51 * pm_context - Structure which contains data for power management
52 * @api_version version of PM API, must match with one on PMU side
53 * @payload payload array used to store received
54 * data from ipi buffer registers
55 */
56static struct {
57 uint32_t api_version;
58 uint32_t payload[PAYLOAD_ARG_CNT];
59} pm_ctx;
60
61/**
62 * pm_setup() - PM service setup
63 *
64 * @return On success, the initialization function must return 0.
65 * Any other return value will cause the framework to ignore
66 * the service
67 *
68 * Initialization functions for ZynqMP power management for
69 * communicaton with PMU.
70 *
71 * Called from sip_svc_setup initialization function with the
72 * rt_svc_init signature.
Soren Brinkmann76fcae32016-03-06 20:16:27 -080073 */
74int pm_setup(void)
75{
76 int status;
77
78 if (!zynqmp_is_pmu_up())
79 return -ENODEV;
80
81 status = pm_ipi_init();
82
83 if (status == 0)
84 INFO("BL31: PM Service Init Complete: API v%d.%d\n",
85 PM_VERSION_MAJOR, PM_VERSION_MINOR);
86 else
87 INFO("BL31: PM Service Init Failed, Error Code %d!\n", status);
88
89 pm_down = status;
90
91 return status;
92}
93
94/**
95 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
96 * @smc_fid - Function Identifier
97 * @x1 - x4 - Arguments
98 * @cookie - Unused
99 * @handler - Pointer to caller's context structure
100 *
101 * @return - Unused
102 *
103 * Determines that smc_fid is valid and supported PM SMC Function ID from the
104 * list of pm_api_ids, otherwise completes the request with
105 * the unknown SMC Function ID
106 *
107 * The SMC calls for PM service are forwarded from SIP Service SMC handler
108 * function with rt_svc_handle signature
109 */
110uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3,
111 uint64_t x4, void *cookie, void *handle, uint64_t flags)
112{
113 enum pm_ret_status ret;
114
115 uint32_t pm_arg[4];
116
117 /* Handle case where PM wasn't initialized properly */
118 if (pm_down)
119 SMC_RET1(handle, SMC_UNK);
120
121 pm_arg[0] = (uint32_t)x1;
122 pm_arg[1] = (uint32_t)(x1 >> 32);
123 pm_arg[2] = (uint32_t)x2;
124 pm_arg[3] = (uint32_t)(x2 >> 32);
125
126 switch (smc_fid & FUNCID_NUM_MASK) {
127 /* PM API Functions */
128 case PM_SELF_SUSPEND:
129 ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
130 pm_arg[3]);
131 SMC_RET1(handle, (uint64_t)ret);
132
133 case PM_REQ_SUSPEND:
134 ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
135 pm_arg[3]);
136 SMC_RET1(handle, (uint64_t)ret);
137
138 case PM_REQ_WAKEUP:
139 ret = pm_req_wakeup(pm_arg[0], pm_arg[1], pm_arg[2],
140 pm_arg[3]);
141 SMC_RET1(handle, (uint64_t)ret);
142
143 case PM_FORCE_POWERDOWN:
144 ret = pm_force_powerdown(pm_arg[0], pm_arg[1]);
145 SMC_RET1(handle, (uint64_t)ret);
146
147 case PM_ABORT_SUSPEND:
148 ret = pm_abort_suspend(pm_arg[0]);
149 SMC_RET1(handle, (uint64_t)ret);
150
151 case PM_SET_WAKEUP_SOURCE:
152 ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2]);
153 SMC_RET1(handle, (uint64_t)ret);
154
155 case PM_SYSTEM_SHUTDOWN:
Soren Brinkmann58fbb9b2016-09-02 09:50:54 -0700156 ret = pm_system_shutdown(pm_arg[0], pm_arg[1]);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800157 SMC_RET1(handle, (uint64_t)ret);
158
159 case PM_REQ_NODE:
160 ret = pm_req_node(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
161 SMC_RET1(handle, (uint64_t)ret);
162
163 case PM_RELEASE_NODE:
164 ret = pm_release_node(pm_arg[0]);
165 SMC_RET1(handle, (uint64_t)ret);
166
167 case PM_SET_REQUIREMENT:
168 ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2],
169 pm_arg[3]);
170 SMC_RET1(handle, (uint64_t)ret);
171
172 case PM_SET_MAX_LATENCY:
173 ret = pm_set_max_latency(pm_arg[0], pm_arg[1]);
174 SMC_RET1(handle, (uint64_t)ret);
175
176 case PM_GET_API_VERSION:
177 /* Check is PM API version already verified */
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700178 if (pm_ctx.api_version == PM_VERSION) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800179 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
180 ((uint64_t)PM_VERSION << 32));
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700181 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800182
183 ret = pm_get_api_version(&pm_ctx.api_version);
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700184 /*
185 * Enable IPI IRQ
186 * assume the rich OS is OK to handle callback IRQs now.
187 * Even if we were wrong, it would not enable the IRQ in
188 * the GIC.
189 */
190 pm_ipi_irq_enable();
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800191 SMC_RET1(handle, (uint64_t)ret |
192 ((uint64_t)pm_ctx.api_version << 32));
193
194 case PM_SET_CONFIGURATION:
195 ret = pm_set_configuration(pm_arg[0]);
196 SMC_RET1(handle, (uint64_t)ret);
197
198 case PM_GET_NODE_STATUS:
199 ret = pm_get_node_status(pm_arg[0]);
200 SMC_RET1(handle, (uint64_t)ret);
201
202 case PM_GET_OP_CHARACTERISTIC:
Anes Hadziahmetagic92aee012016-05-12 16:17:30 +0200203 {
204 uint32_t result;
205
206 ret = pm_get_op_characteristic(pm_arg[0], pm_arg[1], &result);
207 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)result << 32));
208 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800209
210 case PM_REGISTER_NOTIFIER:
211 ret = pm_register_notifier(pm_arg[0], pm_arg[1], pm_arg[2],
212 pm_arg[3]);
213 SMC_RET1(handle, (uint64_t)ret);
214
215 case PM_RESET_ASSERT:
216 ret = pm_reset_assert(pm_arg[0], pm_arg[1]);
217 SMC_RET1(handle, (uint64_t)ret);
218
219 case PM_RESET_GET_STATUS:
220 {
221 uint32_t reset_status;
222
223 ret = pm_reset_get_status(pm_arg[0], &reset_status);
224 SMC_RET1(handle, (uint64_t)ret |
225 ((uint64_t)reset_status << 32));
226 }
227
228 /* PM memory access functions */
229 case PM_MMIO_WRITE:
230 ret = pm_mmio_write(pm_arg[0], pm_arg[1], pm_arg[2]);
231 SMC_RET1(handle, (uint64_t)ret);
232
233 case PM_MMIO_READ:
234 {
235 uint32_t value;
236
237 ret = pm_mmio_read(pm_arg[0], &value);
238 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
239 }
Nava kishore Manne68d460c2016-08-20 23:18:09 +0530240
241 case PM_FPGA_LOAD:
242 ret = pm_fpga_load(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
243 SMC_RET1(handle, (uint64_t)ret);
244
245 case PM_FPGA_GET_STATUS:
246 {
247 uint32_t value;
248
249 ret = pm_fpga_get_status(&value);
250 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32);
251 }
252
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530253 case PM_GET_CHIPID:
Soren Brinkmanncb366812016-09-22 12:21:11 -0700254 {
255 uint32_t result[2];
256
257 ret = pm_get_chipid(result);
258 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32),
259 result[1]);
260 }
Siva Durga Prasad Paladugu16427d12016-08-24 11:45:47 +0530261
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700262 case PM_GET_CALLBACK_DATA:
263 {
264 uint32_t result[4];
265
266 pm_get_callbackdata(result, sizeof(result));
267 SMC_RET2(handle,
268 (uint64_t)result[0] | ((uint64_t)result[1] << 32),
269 (uint64_t)result[2] | ((uint64_t)result[3] << 32));
270 }
271
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800272 default:
273 WARN("Unimplemented PM Service Call: 0x%x\n", smc_fid);
274 SMC_RET1(handle, SMC_UNK);
275 }
276}