1 /*
2 * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved.
3 * Copyright (c) 2022, Advanced Micro Devices, Inc. All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8 /*
9 * Top-level SMC handler for Versal power management calls and
10 * IPI setup functions for communication with PMC.
11 */
12
13 #include <errno.h>
14 #include <plat_private.h>
15 #include <stdbool.h>
16 #include <common/runtime_svc.h>
17 #include <plat/common/platform.h>
18 #include "pm_api_sys.h"
19 #include "pm_client.h"
20 #include "pm_ipi.h"
21 #include <drivers/arm/gicv3.h>
22 #include "../drivers/arm/gic/v3/gicv3_private.h"
23
24 #define MODE 0x80000000U
25
26 #define XSCUGIC_SGIR_EL1_INITID_SHIFT 24U
27 #define INVALID_SGI 0xFFU
28 #define PM_INIT_SUSPEND_CB (30U)
29 #define PM_NOTIFY_CB (32U)
DEFINE_RENAME_SYSREG_RW_FUNCS(icc_asgi1r_el1,S3_0_C12_C11_6)30 DEFINE_RENAME_SYSREG_RW_FUNCS(icc_asgi1r_el1, S3_0_C12_C11_6)
31
32 /* pm_up = true - UP, pm_up = false - DOWN */
33 static bool pm_up;
34 static uint32_t sgi = (uint32_t)INVALID_SGI;
35
36 static void notify_os(void)
37 {
38 int32_t cpu;
39 uint32_t reg;
40
41 cpu = plat_my_core_pos() + 1U;
42
43 reg = (cpu | (sgi << XSCUGIC_SGIR_EL1_INITID_SHIFT));
44 write_icc_asgi1r_el1(reg);
45 }
46
ipi_fiq_handler(uint32_t id,uint32_t flags,void * handle,void * cookie)47 static uint64_t ipi_fiq_handler(uint32_t id, uint32_t flags, void *handle,
48 void *cookie)
49 {
50 uint32_t payload[4] = {0};
51
52 VERBOSE("Received IPI FIQ from firmware\n");
53
54 (void)plat_ic_acknowledge_interrupt();
55
56 pm_get_callbackdata(payload, ARRAY_SIZE(payload), 0, 0);
57 switch (payload[0]) {
58 case PM_INIT_SUSPEND_CB:
59 case PM_NOTIFY_CB:
60 if (sgi != INVALID_SGI) {
61 notify_os();
62 }
63 break;
64 default:
65 pm_ipi_irq_clear(primary_proc);
66 WARN("Invalid IPI payload\n");
67 break;
68 }
69
70 /* Clear FIQ */
71 plat_ic_end_of_interrupt(id);
72
73 return 0;
74 }
75
76 /**
77 * pm_register_sgi() - PM register the IPI interrupt
78 *
79 * @sgi - SGI number to be used for communication.
80 * @reset - Reset to invalid SGI when reset=1.
81 * @return On success, the initialization function must return 0.
82 * Any other return value will cause the framework to ignore
83 * the service
84 *
85 * Update the SGI number to be used.
86 *
87 */
pm_register_sgi(uint32_t sgi_num,uint32_t reset)88 int32_t pm_register_sgi(uint32_t sgi_num, uint32_t reset)
89 {
90 if (reset == 1U) {
91 sgi = INVALID_SGI;
92 return 0;
93 }
94
95 if (sgi != INVALID_SGI) {
96 return -EBUSY;
97 }
98
99 if (sgi_num >= GICV3_MAX_SGI_TARGETS) {
100 return -EINVAL;
101 }
102
103 sgi = (uint32_t)sgi_num;
104 return 0;
105 }
106
107 /**
108 * pm_setup() - PM service setup
109 *
110 * @return On success, the initialization function must return 0.
111 * Any other return value will cause the framework to ignore
112 * the service
113 *
114 * Initialization functions for Versal power management for
115 * communicaton with PMC.
116 *
117 * Called from sip_svc_setup initialization function with the
118 * rt_svc_init signature.
119 */
pm_setup(void)120 int32_t pm_setup(void)
121 {
122 int32_t ret = 0;
123
124 pm_ipi_init(primary_proc);
125 pm_up = true;
126
127 /*
128 * Enable IPI IRQ
129 * assume the rich OS is OK to handle callback IRQs now.
130 * Even if we were wrong, it would not enable the IRQ in
131 * the GIC.
132 */
133 pm_ipi_irq_enable(primary_proc);
134
135 ret = request_intr_type_el3(PLAT_VERSAL_IPI_IRQ, ipi_fiq_handler);
136 if (ret != 0) {
137 WARN("BL31: registering IPI interrupt failed\n");
138 }
139
140 gicd_write_irouter(gicv3_driver_data->gicd_base, PLAT_VERSAL_IPI_IRQ, MODE);
141 return ret;
142 }
143
144 /**
145 * eemi_for_compatibility() - EEMI calls handler for deprecated calls
146 *
147 * @return - If EEMI API found then, uintptr_t type address, else 0
148 *
149 * Some EEMI API's use case needs to be changed in Linux driver, so they
150 * can take advantage of common EEMI handler in TF-A. As of now the old
151 * implementation of these APIs are required to maintain backward compatibility
152 * until their use case in linux driver changes.
153 */
eemi_for_compatibility(uint32_t api_id,uint32_t * pm_arg,void * handle,uint32_t security_flag)154 static uintptr_t eemi_for_compatibility(uint32_t api_id, uint32_t *pm_arg,
155 void *handle, uint32_t security_flag)
156 {
157 enum pm_ret_status ret;
158
159 switch (api_id) {
160
161 case PM_IOCTL:
162 {
163 uint32_t value;
164
165 ret = pm_api_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
166 pm_arg[3], pm_arg[4],
167 &value, security_flag);
168 if (ret == PM_RET_ERROR_NOTSUPPORTED)
169 return (uintptr_t)0;
170
171 SMC_RET1(handle, (uint64_t)ret | ((uint64_t)value) << 32U);
172 }
173
174 case PM_QUERY_DATA:
175 {
176 uint32_t data[PAYLOAD_ARG_CNT] = { 0 };
177
178 ret = pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
179 pm_arg[3], data, security_flag);
180
181 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)data[0] << 32U),
182 (uint64_t)data[1] | ((uint64_t)data[2] << 32U));
183 }
184
185 case PM_FEATURE_CHECK:
186 {
187 uint32_t result[PAYLOAD_ARG_CNT] = {0U};
188
189 ret = pm_feature_check(pm_arg[0], result, security_flag);
190 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)result[0] << 32U),
191 (uint64_t)result[1] | ((uint64_t)result[2] << 32U));
192 }
193
194 case PM_LOAD_PDI:
195 {
196 ret = pm_load_pdi(pm_arg[0], pm_arg[1], pm_arg[2],
197 security_flag);
198 SMC_RET1(handle, (uint64_t)ret);
199 }
200
201 default:
202 return (uintptr_t)0;
203 }
204 }
205
206 /**
207 * eemi_psci_debugfs_handler() - EEMI API invoked from PSCI
208 *
209 * These EEMI APIs performs CPU specific power management tasks.
210 * These EEMI APIs are invoked either from PSCI or from debugfs in kernel.
211 * These calls require CPU specific processing before sending IPI request to
212 * Platform Management Controller. For example enable/disable CPU specific
213 * interrupts. This requires separate handler for these calls and may not be
214 * handled using common eemi handler
215 */
eemi_psci_debugfs_handler(uint32_t api_id,uint32_t * pm_arg,void * handle,uint32_t security_flag)216 static uintptr_t eemi_psci_debugfs_handler(uint32_t api_id, uint32_t *pm_arg,
217 void *handle, uint32_t security_flag)
218 {
219 enum pm_ret_status ret;
220
221 switch (api_id) {
222
223 case PM_SELF_SUSPEND:
224 ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
225 pm_arg[3], security_flag);
226 SMC_RET1(handle, (u_register_t)ret);
227
228 case PM_FORCE_POWERDOWN:
229 ret = pm_force_powerdown(pm_arg[0], pm_arg[1], security_flag);
230 SMC_RET1(handle, (u_register_t)ret);
231
232 case PM_REQ_SUSPEND:
233 ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
234 pm_arg[3], security_flag);
235 SMC_RET1(handle, (u_register_t)ret);
236
237 case PM_ABORT_SUSPEND:
238 ret = pm_abort_suspend(pm_arg[0], security_flag);
239 SMC_RET1(handle, (u_register_t)ret);
240
241 case PM_SYSTEM_SHUTDOWN:
242 ret = pm_system_shutdown(pm_arg[0], pm_arg[1], security_flag);
243 SMC_RET1(handle, (u_register_t)ret);
244
245 default:
246 return (uintptr_t)0;
247 }
248 }
249
250 /**
251 * TF_A_specific_handler() - SMC handler for TF-A specific functionality
252 *
253 * These EEMI calls performs functionality that does not require
254 * IPI transaction. The handler ends in TF-A and returns requested data to
255 * kernel from TF-A.
256 */
TF_A_specific_handler(uint32_t api_id,uint32_t * pm_arg,void * handle,uint32_t security_flag)257 static uintptr_t TF_A_specific_handler(uint32_t api_id, uint32_t *pm_arg,
258 void *handle, uint32_t security_flag)
259 {
260 switch (api_id) {
261
262 case TF_A_PM_REGISTER_SGI:
263 {
264 int32_t ret;
265
266 ret = pm_register_sgi(pm_arg[0], pm_arg[1]);
267 if (ret != 0) {
268 SMC_RET1(handle, (uint32_t)PM_RET_ERROR_ARGS);
269 }
270
271 SMC_RET1(handle, (uint32_t)PM_RET_SUCCESS);
272 }
273
274 case PM_GET_CALLBACK_DATA:
275 {
276 uint32_t result[4] = {0};
277
278 pm_get_callbackdata(result, ARRAY_SIZE(result), security_flag, 1U);
279 SMC_RET2(handle,
280 (uint64_t)result[0] | ((uint64_t)result[1] << 32U),
281 (uint64_t)result[2] | ((uint64_t)result[3] << 32U));
282 }
283
284 case PM_GET_TRUSTZONE_VERSION:
285 SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
286 ((uint64_t)TZ_VERSION << 32U));
287
288 default:
289 return (uintptr_t)0;
290 }
291 }
292
293 /**
294 * eemi_handler() - Prepare EEMI payload and perform IPI transaction
295 *
296 * EEMI - Embedded Energy Management Interface is Xilinx proprietary protocol
297 * to allow communication between power management controller and different
298 * processing clusters.
299 *
300 * This handler prepares EEMI protocol payload received from kernel and performs
301 * IPI transaction.
302 */
eemi_handler(uint32_t api_id,uint32_t * pm_arg,void * handle,uint32_t security_flag)303 static uintptr_t eemi_handler(uint32_t api_id, uint32_t *pm_arg,
304 void *handle, uint32_t security_flag)
305 {
306 enum pm_ret_status ret;
307 uint32_t buf[PAYLOAD_ARG_CNT] = {0};
308
309 ret = pm_handle_eemi_call(security_flag, api_id, pm_arg[0], pm_arg[1],
310 pm_arg[2], pm_arg[3], pm_arg[4],
311 (uint64_t *)buf);
312 /*
313 * Two IOCTLs, to get clock name and pinctrl name of pm_query_data API
314 * receives 5 words of respoonse from firmware. Currently linux driver can
315 * receive only 4 words from TF-A. So, this needs to be handled separately
316 * than other eemi calls.
317 */
318 if (api_id == PM_QUERY_DATA) {
319 if ((pm_arg[0] == XPM_QID_CLOCK_GET_NAME ||
320 pm_arg[0] == XPM_QID_PINCTRL_GET_FUNCTION_NAME) &&
321 ret == PM_RET_SUCCESS) {
322 SMC_RET2(handle, (uint64_t)buf[0] | ((uint64_t)buf[1] << 32U),
323 (uint64_t)buf[2] | ((uint64_t)buf[3] << 32U));
324 }
325 }
326
327 SMC_RET2(handle, (uint64_t)ret | ((uint64_t)buf[0] << 32U),
328 (uint64_t)buf[1] | ((uint64_t)buf[2] << 32U));
329 }
330
331 /**
332 * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
333 * @smc_fid - Function Identifier
334 * @x1 - x4 - SMC64 Arguments from kernel
335 * x3 (upper 32-bits) and x4 are Unused
336 * @cookie - Unused
337 * @handler - Pointer to caller's context structure
338 *
339 * @return - Unused
340 *
341 * Determines that smc_fid is valid and supported PM SMC Function ID from the
342 * list of pm_api_ids, otherwise completes the request with
343 * the unknown SMC Function ID
344 *
345 * The SMC calls for PM service are forwarded from SIP Service SMC handler
346 * function with rt_svc_handle signature
347 */
pm_smc_handler(uint32_t smc_fid,uint64_t x1,uint64_t x2,uint64_t x3,uint64_t x4,const void * cookie,void * handle,uint64_t flags)348 uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3,
349 uint64_t x4, const void *cookie, void *handle, uint64_t flags)
350 {
351 uintptr_t ret;
352 uint32_t pm_arg[PAYLOAD_ARG_CNT] = {0};
353 uint32_t security_flag = SECURE_FLAG;
354 uint32_t api_id;
355
356 /* Handle case where PM wasn't initialized properly */
357 if (pm_up == false) {
358 SMC_RET1(handle, SMC_UNK);
359 }
360
361 /*
362 * Mark BIT24 payload (i.e 1st bit of pm_arg[3] ) as non-secure (1)
363 * if smc called is non secure
364 */
365 if (is_caller_non_secure(flags) != 0) {
366 security_flag = NON_SECURE_FLAG;
367 }
368
369 pm_arg[0] = (uint32_t)x1;
370 pm_arg[1] = (uint32_t)(x1 >> 32U);
371 pm_arg[2] = (uint32_t)x2;
372 pm_arg[3] = (uint32_t)(x2 >> 32U);
373 pm_arg[4] = (uint32_t)x3;
374 (void)(x4);
375 api_id = smc_fid & FUNCID_NUM_MASK;
376
377 ret = eemi_for_compatibility(api_id, pm_arg, handle, security_flag);
378 if (ret != (uintptr_t)0) {
379 return ret;
380 }
381
382 ret = eemi_psci_debugfs_handler(api_id, pm_arg, handle, flags);
383 if (ret != (uintptr_t)0) {
384 return ret;
385 }
386
387 ret = TF_A_specific_handler(api_id, pm_arg, handle, security_flag);
388 if (ret != (uintptr_t)0) {
389 return ret;
390 }
391
392 ret = eemi_handler(api_id, pm_arg, handle, security_flag);
393
394 return ret;
395 }
396