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