xref: /rk3399_ARM-atf/plat/xilinx/zynqmp/pm_service/zynqmp_pm_svc_main.c (revision 7dae0451dda5074191c3ecfdec5eece768c28212)
1 /*
2  * Copyright (c) 2013-2022, Arm Limited and Contributors. All rights reserved.
3  * Copyright (c) 2023-2025, Advanced Micro Devices, Inc. All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 /*
9  * Top-level SMC handler for ZynqMP power management calls and
10  * IPI setup functions for communication with PMU.
11  */
12 
13 #include <errno.h>
14 
15 #include <arch_helpers.h>
16 #include <common/runtime_svc.h>
17 #include <drivers/arm/gicv2.h>
18 #include <lib/mmio.h>
19 #include <lib/spinlock.h>
20 #include <plat/common/platform.h>
21 
22 #include <plat_private.h>
23 #include "pm_client.h"
24 #include "pm_ipi.h"
25 #include "pm_svc_main.h"
26 #include "zynqmp_pm_api_sys.h"
27 #include "zynqmp_pm_defs.h"
28 
29 /* pm_up = !0 - UP, pm_up = 0 - DOWN */
30 static int32_t pm_up, ipi_irq_flag;
31 
32 #if ZYNQMP_WDT_RESTART
33 static spinlock_t inc_lock;
34 static int active_cores = 0;
35 #endif
36 
37 /**
38  * typedef pm_ctx_t - Structure which contains data for power management.
39  * @api_version: version of PM API, must match with one on PMU side.
40  * @payload: payload array used to store received.
41  *           data from ipi buffer registers.
42  *
43  */
44 typedef struct {
45 	uint32_t api_version;
46 	uint32_t payload[PAYLOAD_ARG_CNT];
47 } pm_ctx_t;
48 
49 static pm_ctx_t pm_ctx;
50 
51 #if ZYNQMP_WDT_RESTART
52 /**
53  * trigger_wdt_restart() - Trigger warm restart event to APU cores.
54  *
55  * This function triggers SGI for all active APU CPUs. SGI handler then
56  * power down CPU and call system reset.
57  *
58  */
59 static void trigger_wdt_restart(void)
60 {
61 	uint32_t core_count = 0;
62 	uint32_t core_status[3];
63 	uint32_t target_cpu_list = 0;
64 	int i;
65 
66 	for (i = 0; i < 4; i++) {
67 		pm_get_node_status(NODE_APU_0 + i, core_status, SECURE);
68 		if (core_status[0] == 1) {
69 			core_count++;
70 			target_cpu_list |= (1 << i);
71 		}
72 	}
73 
74 	spin_lock(&inc_lock);
75 	active_cores = core_count;
76 	spin_unlock(&inc_lock);
77 
78 	INFO("Active Cores: %d\n", active_cores);
79 
80 	for (i = PLATFORM_CORE_COUNT - 1; i >= 0; i--) {
81 		if (target_cpu_list & (1 << i)) {
82 			/* trigger SGI to active cores */
83 			plat_ic_raise_el3_sgi(ARM_IRQ_SEC_SGI_7, i);
84 		}
85 	}
86 }
87 
88 /**
89  * ttc_fiq_handler() - TTC Handler for timer event.
90  * @id: number of the highest priority pending interrupt of the type
91  *      that this handler was registered for.
92  * @flags: security state, bit[0].
93  * @handle: pointer to 'cpu_context' structure of the current CPU for the
94  *           security state specified in the 'flags' parameter.
95  * @cookie: unused.
96  *
97  * Function registered as INTR_TYPE_EL3 interrupt handler.
98  *
99  * When WDT event is received in PMU, PMU needs to notify master to do cleanup
100  * if required. PMU sets up timer and starts timer to overflow in zero time upon
101  * WDT event. TF-A handles this timer event and takes necessary action required
102  * for warm restart.
103  *
104  * In presence of non-secure software layers (EL1/2) sets the interrupt
105  * at registered entrance in GIC and informs that PMU responded or demands
106  * action.
107  *
108  * Return: 0 on success.
109  *
110  */
111 static uint64_t ttc_fiq_handler(uint32_t id, uint32_t flags, void *handle,
112 				void *cookie)
113 {
114 	INFO("BL31: Got TTC FIQ\n");
115 
116 	plat_ic_end_of_interrupt(id);
117 
118 	/* Clear TTC interrupt by reading interrupt register */
119 	mmio_read_32(TTC3_INTR_REGISTER_1);
120 
121 	/* Disable the timer interrupts */
122 	mmio_write_32(TTC3_INTR_ENABLE_1, 0);
123 
124 	trigger_wdt_restart();
125 
126 	return 0;
127 }
128 
129 /**
130  * zynqmp_sgi7_irq() - Handler for SGI7 IRQ.
131  * @id: number of the highest priority pending interrupt of the type
132  *      that this handler was registered for.
133  * @flags: security state, bit[0].
134  * @handle: pointer to 'cpu_context' structure of the current CPU for the
135  *           security state specified in the 'flags' parameter.
136  * @cookie: unused.
137  *
138  * Function registered as INTR_TYPE_EL3 interrupt handler
139  *
140  * On receiving WDT event from PMU, TF-A generates SGI7 to all running CPUs.
141  * In response to SGI7 interrupt, each CPUs do clean up if required and last
142  * running CPU calls system restart.
143  *
144  * Return: This function does not return a value and it enters into wfi.
145  */
146 static uint64_t __unused __dead2 zynqmp_sgi7_irq(uint32_t id, uint32_t flags,
147 						 void *handle, void *cookie)
148 {
149 	int i;
150 	uint32_t value;
151 
152 	/* enter wfi and stay there */
153 	INFO("Entering wfi\n");
154 
155 	spin_lock(&inc_lock);
156 	active_cores--;
157 
158 	for (i = 0; i < 4; i++) {
159 		mmio_write_32(BASE_GICD_BASE + GICD_CPENDSGIR + 4 * i,
160 				0xffffffff);
161 	}
162 
163 	dsb();
164 
165 	spin_unlock(&inc_lock);
166 
167 	if (active_cores == 0) {
168 		pm_mmio_read(PMU_GLOBAL_GEN_STORAGE4, &value, SECURE);
169 		value = (value & RESTART_SCOPE_MASK) >> RESTART_SCOPE_SHIFT;
170 		pm_system_shutdown(PMF_SHUTDOWN_TYPE_RESET, value, SECURE);
171 	}
172 
173 	/* enter wfi and stay there */
174 	while (1)
175 		wfi();
176 }
177 
178 /**
179  * pm_wdt_restart_setup() - Setup warm restart interrupts.
180  *
181  * Return: Returns status, 0 on success or error+reason.
182  *
183  * This function sets up handler for SGI7 and TTC interrupts
184  * used for warm restart.
185  */
186 static int pm_wdt_restart_setup(void)
187 {
188 	int ret;
189 
190 	/* register IRQ handler for SGI7 */
191 	ret = request_intr_type_el3(ARM_IRQ_SEC_SGI_7, zynqmp_sgi7_irq);
192 	if (ret) {
193 		WARN("BL31: registering SGI7 interrupt failed\n");
194 		goto err;
195 	}
196 
197 	ret = request_intr_type_el3(IRQ_TTC3_1, ttc_fiq_handler);
198 	if (ret)
199 		WARN("BL31: registering TTC3 interrupt failed\n");
200 
201 err:
202 	return ret;
203 }
204 #endif
205 
206 /**
207  * pm_setup() - PM service setup.
208  *
209  * Return: On success, the initialization function must return 0.
210  *         Any other return value will cause the framework to ignore
211  *         the service.
212  *
213  * Initialization functions for ZynqMP power management for
214  * communicaton with PMU.
215  *
216  * Called from sip_svc_setup initialization function with the
217  * rt_svc_init signature.
218  *
219  */
220 int32_t pm_setup(void)
221 {
222 	enum pm_ret_status err;
223 	int32_t ret = -EINVAL;
224 
225 	pm_ipi_init(primary_proc);
226 
227 	err = pm_get_api_version(&pm_ctx.api_version, SECURE);
228 	if (err != PM_RET_SUCCESS) {
229 		ERROR("BL31: Failed to read Platform Management API version. "
230 		      "Return: %d\n", err);
231 		goto exit_label;
232 	}
233 	if (pm_ctx.api_version < PM_VERSION) {
234 		ERROR("BL31: Platform Management API version error. Expected: "
235 		      "v%d.%d - Found: v%d.%d\n", PM_VERSION_MAJOR,
236 		      PM_VERSION_MINOR, pm_ctx.api_version >> 16,
237 		      pm_ctx.api_version & 0xFFFFU);
238 		goto exit_label;
239 	}
240 
241 	int32_t status = 0;
242 #if ZYNQMP_WDT_RESTART
243 	status = pm_wdt_restart_setup();
244 	if (status)
245 		WARN("BL31: warm-restart setup failed\n");
246 #endif
247 
248 	if (status >= 0) {
249 		INFO("BL31: PM Service Init Complete: API v%d.%d\n",
250 		     PM_VERSION_MAJOR, PM_VERSION_MINOR);
251 		ret = 0;
252 	} else {
253 		INFO("BL31: PM Service Init Failed, Error Code %d!\n", status);
254 		ret = status;
255 	}
256 
257 	pm_up = (status == 0);
258 
259 exit_label:
260 	return ret;
261 }
262 
263 /**
264  * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
265  * @smc_fid: Function Identifier.
266  * @x1: Arguments.
267  * @x2: Arguments.
268  * @x3: Arguments.
269  * @x4: Arguments.
270  * @cookie: Unused.
271  * @handle: Pointer to caller's context structure.
272  * @flags: SECURE or NON_SECURE
273  *
274  * Determines that smc_fid is valid and supported PM SMC Function ID from the
275  * list of pm_api_ids, otherwise completes the request with
276  * the unknown SMC Function ID.
277  *
278  * The SMC calls for PM service are forwarded from SIP Service SMC handler
279  * function with rt_svc_handle signature.
280  *
281  * Return: Unused.
282  *
283  */
284 uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3,
285 			uint64_t x4, const void *cookie, void *handle, uint64_t flags)
286 {
287 	(void)x4;
288 	(void)cookie;
289 	enum pm_ret_status ret;
290 	uint32_t payload[PAYLOAD_ARG_CNT];
291 
292 	uint32_t pm_arg[5];
293 	uint32_t result[RET_PAYLOAD_ARG_CNT] = {0};
294 	uint32_t api_id;
295 	uint32_t security_flag = NON_SECURE;
296 	bool status = false, status_tmp = false;
297 
298 	/* Handle case where PM wasn't initialized properly */
299 	if (pm_up == 0)
300 		SMC_RET1(handle, SMC_UNK);
301 
302 	/*
303 	 * Mark BIT24 payload (i.e 1st bit of pm_arg[3] ) as secure (0)
304 	 * if smc called is secure
305 	 *
306 	 * Add redundant macro call to immune the code from glitches
307 	 */
308 	SECURE_REDUNDANT_CALL(status, status_tmp, is_caller_secure, flags);
309 	if ((status != false) && (status_tmp != false)) {
310 		security_flag = SECURE;
311 	}
312 
313 	pm_arg[0] = (uint32_t)x1;
314 	pm_arg[1] = (uint32_t)(x1 >> 32);
315 	pm_arg[2] = (uint32_t)x2;
316 	pm_arg[3] = (uint32_t)(x2 >> 32);
317 	pm_arg[4] = (uint32_t)x3;
318 
319 	api_id = smc_fid & FUNCID_NUM_MASK;
320 
321 	switch (api_id) {
322 	/* PM API Functions */
323 	case PM_SELF_SUSPEND:
324 		ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
325 				      pm_arg[3], security_flag);
326 		SMC_RET1(handle, (uint64_t)ret);
327 
328 	case PM_REQ_SUSPEND:
329 		ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
330 				     pm_arg[3], security_flag);
331 		SMC_RET1(handle, (uint64_t)ret);
332 
333 	case PM_REQ_WAKEUP:
334 	{
335 		/* Use address flag is encoded in the 1st bit of the low-word */
336 		uint32_t set_addr = pm_arg[1] & 0x1U;
337 		uint64_t address = (uint64_t)pm_arg[2] << 32U;
338 
339 		address |= (uint64_t)(pm_arg[1] & (~0x1U));
340 		ret = pm_req_wakeup(pm_arg[0], set_addr, address,
341 				    pm_arg[3], security_flag);
342 		SMC_RET1(handle, (uint64_t)ret);
343 	}
344 
345 	case PM_FORCE_POWERDOWN:
346 		ret = pm_force_powerdown(pm_arg[0], pm_arg[1], security_flag);
347 		SMC_RET1(handle, (uint64_t)ret);
348 
349 	case PM_ABORT_SUSPEND:
350 		ret = pm_abort_suspend(pm_arg[0], security_flag);
351 		SMC_RET1(handle, (uint64_t)ret);
352 
353 	case PM_SET_WAKEUP_SOURCE:
354 		ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2], security_flag);
355 		SMC_RET1(handle, (uint64_t)ret);
356 
357 	case PM_SYSTEM_SHUTDOWN:
358 		ret = pm_system_shutdown(pm_arg[0], pm_arg[1], security_flag);
359 		SMC_RET1(handle, (uint64_t)ret);
360 
361 	case PM_REQ_NODE:
362 		ret = pm_req_node(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3], security_flag);
363 		SMC_RET1(handle, (uint64_t)ret);
364 
365 	case PM_SET_REQUIREMENT:
366 		ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2],
367 					 pm_arg[3], security_flag);
368 		SMC_RET1(handle, (uint64_t)ret);
369 
370 	case PM_GET_API_VERSION:
371 		if ((uint32_t)ipi_irq_flag == 0U) {
372 			/*
373 			 * Enable IPI IRQ
374 			 * assume the rich OS is OK to handle callback IRQs now.
375 			 * Even if we were wrong, it would not enable the IRQ in
376 			 * the GIC.
377 			 */
378 			pm_ipi_irq_enable(primary_proc);
379 			ipi_irq_flag = 1U;
380 		}
381 		SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
382 			 ((uint64_t)pm_ctx.api_version << 32));
383 	case PM_FPGA_LOAD:
384 		ret = pm_fpga_load(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3], security_flag);
385 		SMC_RET1(handle, (uint64_t)ret);
386 
387 	case PM_FPGA_GET_STATUS:
388 	{
389 		uint32_t value = 0U;
390 
391 		ret = pm_fpga_get_status(&value, security_flag);
392 		SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32)));
393 	}
394 
395 	case PM_SECURE_RSA_AES:
396 		ret = pm_secure_rsaaes(pm_arg[0], pm_arg[1], pm_arg[2],
397 				       pm_arg[3], security_flag);
398 		SMC_RET1(handle, (uint64_t)ret);
399 
400 	case PM_GET_CALLBACK_DATA:
401 		ret = pm_get_callbackdata(result, ARRAY_SIZE(result));
402 		if (ret != PM_RET_SUCCESS) {
403 			result[0] = ret;
404 		}
405 
406 		SMC_RET2(handle,
407 			 ((uint64_t)result[0] | ((uint64_t)result[1] << 32)),
408 			 ((uint64_t)result[2] | ((uint64_t)result[3] << 32)));
409 	case PM_IOCTL:
410 	{
411 		uint32_t value = 0U;
412 
413 		ret = pm_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
414 			       pm_arg[3], &value, security_flag);
415 		SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32)));
416 	}
417 
418 	case PM_QUERY_DATA:
419 	{
420 		uint32_t data[4] = { 0 };
421 
422 		pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
423 			      pm_arg[3], data, security_flag);
424 		SMC_RET2(handle, ((uint64_t)data[0]  | ((uint64_t)data[1] << 32)),
425 			 ((uint64_t)data[2] | ((uint64_t)data[3] << 32)));
426 	}
427 
428 	case PM_CLOCK_ENABLE:
429 		ret = pm_clock_enable(pm_arg[0], security_flag);
430 		SMC_RET1(handle, (uint64_t)ret);
431 
432 	case PM_CLOCK_DISABLE:
433 		ret = pm_clock_disable(pm_arg[0], security_flag);
434 		SMC_RET1(handle, (uint64_t)ret);
435 
436 	case PM_CLOCK_GETSTATE:
437 	{
438 		uint32_t value = 0U;
439 
440 		ret = pm_clock_getstate(pm_arg[0], &value, security_flag);
441 		SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32)));
442 	}
443 
444 	case PM_CLOCK_SETDIVIDER:
445 		ret = pm_clock_setdivider(pm_arg[0], pm_arg[1], security_flag);
446 		SMC_RET1(handle, (uint64_t)ret);
447 
448 	case PM_CLOCK_GETDIVIDER:
449 	{
450 		uint32_t value = 0U;
451 
452 		ret = pm_clock_getdivider(pm_arg[0], &value, security_flag);
453 		SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32)));
454 	}
455 
456 	case PM_CLOCK_SETPARENT:
457 		ret = pm_clock_setparent(pm_arg[0], pm_arg[1], security_flag);
458 		SMC_RET1(handle, (uint64_t)ret);
459 
460 	case PM_CLOCK_GETPARENT:
461 	{
462 		uint32_t value = 0U;
463 
464 		ret = pm_clock_getparent(pm_arg[0], &value, security_flag);
465 		SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32U)));
466 	}
467 
468 	case PM_GET_TRUSTZONE_VERSION:
469 		SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
470 			 ((uint64_t)ZYNQMP_TZ_VERSION << 32U));
471 
472 	case PM_SET_SUSPEND_MODE:
473 		ret = pm_set_suspend_mode(pm_arg[0]);
474 		SMC_RET1(handle, (uint64_t)ret);
475 
476 	case PM_SECURE_SHA:
477 		ret = pm_sha_hash(pm_arg[0], pm_arg[1], pm_arg[2],
478 				  pm_arg[3], security_flag);
479 		SMC_RET1(handle, (uint64_t)ret);
480 
481 	case PM_SECURE_RSA:
482 		ret = pm_rsa_core(pm_arg[0], pm_arg[1], pm_arg[2],
483 				  pm_arg[3], security_flag);
484 		SMC_RET1(handle, (uint64_t)ret);
485 
486 	case PM_SECURE_IMAGE:
487 	{
488 		ret = pm_secure_image(pm_arg[0], pm_arg[1], pm_arg[2],
489 				      pm_arg[3], &result[0], security_flag);
490 		SMC_RET2(handle, ((uint64_t)ret | ((uint64_t)result[0] << 32U)),
491 			 result[1]);
492 	}
493 
494 	case PM_FPGA_READ:
495 	{
496 		uint32_t value = 0U;
497 
498 		ret = pm_fpga_read(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3],
499 				   &value, security_flag);
500 		SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32U)));
501 	}
502 
503 	case PM_SECURE_AES:
504 	{
505 		uint32_t value = 0U;
506 
507 		ret = pm_aes_engine(pm_arg[0], pm_arg[1], &value, security_flag);
508 		SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32U)));
509 	}
510 
511 	case PM_PLL_SET_PARAMETER:
512 		ret = pm_pll_set_parameter(pm_arg[0], pm_arg[1], pm_arg[2],
513 					   security_flag);
514 		SMC_RET1(handle, (uint64_t)ret);
515 
516 	case PM_PLL_GET_PARAMETER:
517 	{
518 		uint32_t value = 0U;
519 
520 		ret = pm_pll_get_parameter(pm_arg[0], pm_arg[1], &value,
521 					   security_flag);
522 		SMC_RET1(handle, ((uint64_t)ret | ((uint64_t)value << 32U)));
523 	}
524 
525 	case PM_PLL_SET_MODE:
526 		ret = pm_pll_set_mode(pm_arg[0], pm_arg[1], security_flag);
527 		SMC_RET1(handle, (uint64_t)ret);
528 
529 	case PM_PLL_GET_MODE:
530 	{
531 		uint32_t mode = 0U;
532 
533 		ret = pm_pll_get_mode(pm_arg[0], &mode, security_flag);
534 		SMC_RET1(handle, ((uint64_t)ret | ((uint64_t)mode << 32U)));
535 	}
536 
537 	case PM_REGISTER_ACCESS:
538 	{
539 		uint32_t value = 0U;
540 
541 		ret = pm_register_access(pm_arg[0], pm_arg[1], pm_arg[2],
542 					 pm_arg[3], &value, security_flag);
543 		SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32U)));
544 	}
545 
546 	case PM_EFUSE_ACCESS:
547 	{
548 		uint32_t value = 0U;
549 
550 #if defined(ZYNQMP_SECURE_EFUSES)
551 		if (is_caller_non_secure(flags)) {
552 			SMC_RET1(handle,
553 				 (((uint64_t)PM_RET_ERROR_NOT_ENABLED) << 32U) |
554 				 (uint64_t)PM_RET_ERROR_ACCESS);
555 		}
556 #endif
557 		ret = pm_efuse_access(pm_arg[0], pm_arg[1], &value, security_flag);
558 		SMC_RET1(handle, (uint64_t)ret | (((uint64_t)value) << 32U));
559 	}
560 
561 	case PM_FPGA_GET_VERSION:
562 	case PM_FPGA_GET_FEATURE_LIST:
563 	{
564 		uint32_t ret_payload[PAYLOAD_ARG_CNT];
565 
566 		PM_PACK_PAYLOAD5(payload, security_flag, smc_fid & FUNCID_NUM_MASK,
567 				 pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
568 		ret = pm_ipi_send_sync(primary_proc, payload, ret_payload, 3U);
569 		SMC_RET2(handle, ((uint64_t)ret | ((uint64_t)ret_payload[0] << 32U)),
570 			 ((uint64_t)ret_payload[1] | ((uint64_t)ret_payload[2] << 32U)));
571 	}
572 
573 	case PM_FEATURE_CHECK:
574 	{
575 		uint32_t version_type = 0;
576 		uint32_t bit_mask[2] = {0};
577 
578 		ret = pm_feature_check(pm_arg[0], &version_type, bit_mask,
579 				       (uint8_t)ARRAY_SIZE(bit_mask),
580 				       security_flag);
581 		SMC_RET2(handle, ((uint64_t)ret | ((uint64_t)version_type << 32U)),
582 			 ((uint64_t)bit_mask[0] | ((uint64_t)bit_mask[1] << 32U)));
583 	}
584 
585 	default:
586 		/* Send request to the PMU */
587 		PM_PACK_PAYLOAD6(payload, security_flag, api_id, pm_arg[0],
588 				 pm_arg[1], pm_arg[2], pm_arg[3], pm_arg[4]);
589 		ret = pm_ipi_send_sync(primary_proc, payload, result,
590 				       RET_PAYLOAD_ARG_CNT);
591 		SMC_RET2(handle, ((uint64_t)ret | ((uint64_t)result[0] << 32U)),
592 			 ((uint64_t)result[1] | ((uint64_t)result[2] << 32U)));
593 	}
594 }
595