xref: /rk3399_ARM-atf/plat/xilinx/common/ipi_mailbox_service/ipi_mailbox_svc.c (revision 0c0b19f42de25bb75760d6cca02c325c08a33882)
126f1534eSWendy Liang /*
2619bc13eSMichal Simek  * Copyright (c) 2017-2019, Arm Limited and Contributors. All rights reserved.
3bdba3c84SDevanshi Chauhan Alpeshbhai  * Copyright (c) 2022-2025, Advanced Micro Devices, Inc. All rights reserved.
426f1534eSWendy Liang  *
526f1534eSWendy Liang  * SPDX-License-Identifier: BSD-3-Clause
626f1534eSWendy Liang  */
726f1534eSWendy Liang 
826f1534eSWendy Liang /*
926f1534eSWendy Liang  * Top-level SMC handler for ZynqMP IPI Mailbox doorbell functions.
1026f1534eSWendy Liang  */
1126f1534eSWendy Liang 
1226f1534eSWendy Liang #include <errno.h>
1326f1534eSWendy Liang #include <string.h>
1426f1534eSWendy Liang 
1526f1534eSWendy Liang #include <common/debug.h>
1626f1534eSWendy Liang #include <common/runtime_svc.h>
1726f1534eSWendy Liang #include <lib/bakery_lock.h>
1826f1534eSWendy Liang #include <lib/mmio.h>
1926f1534eSWendy Liang 
2026f1534eSWendy Liang #include <ipi.h>
2101a326abSPrasad Kummari #include "ipi_mailbox_svc.h"
2226f1534eSWendy Liang #include <plat_ipi.h>
2326f1534eSWendy Liang #include <plat_private.h>
2426f1534eSWendy Liang 
2526f1534eSWendy Liang /*********************************************************************
2626f1534eSWendy Liang  * Macros definitions
2726f1534eSWendy Liang  ********************************************************************/
2826f1534eSWendy Liang 
2926f1534eSWendy Liang /* IPI SMC calls macros: */
3026f1534eSWendy Liang #define IPI_SMC_OPEN_IRQ_MASK		0x00000001U /* IRQ enable bit in IPI
3126f1534eSWendy Liang 						     * open SMC call
3226f1534eSWendy Liang 						     */
3326f1534eSWendy Liang #define IPI_SMC_NOTIFY_BLOCK_MASK	0x00000001U /* Flag to indicate if
3426f1534eSWendy Liang 						     * IPI notification needs
3526f1534eSWendy Liang 						     * to be blocking.
3626f1534eSWendy Liang 						     */
3726f1534eSWendy Liang #define IPI_SMC_ENQUIRY_DIRQ_MASK	0x00000001U /* Flag to indicate if
3826f1534eSWendy Liang 						     * notification interrupt
3926f1534eSWendy Liang 						     * to be disabled.
4026f1534eSWendy Liang 						     */
4126f1534eSWendy Liang #define IPI_SMC_ACK_EIRQ_MASK		0x00000001U /* Flag to indicate if
4226f1534eSWendy Liang 						     * notification interrupt
4326f1534eSWendy Liang 						     * to be enable.
4426f1534eSWendy Liang 						     */
4526f1534eSWendy Liang 
4626f1534eSWendy Liang #define UNSIGNED32_MASK			0xFFFFFFFFU /* 32bit mask */
4726f1534eSWendy Liang 
4826f1534eSWendy Liang /**
49de7ed953SPrasad Kummari  * ipi_smc_handler() - SMC handler for IPI SMC calls.
50de7ed953SPrasad Kummari  * @smc_fid: Function identifier.
51de7ed953SPrasad Kummari  * @x1: Arguments.
52de7ed953SPrasad Kummari  * @x2: Arguments.
53de7ed953SPrasad Kummari  * @x3: Arguments.
54de7ed953SPrasad Kummari  * @x4: Arguments.
55de7ed953SPrasad Kummari  * @cookie: Unused.
56de7ed953SPrasad Kummari  * @handle: Pointer to caller's context structure.
57*4fd510e0SRonak Jain  * @flags: SECURE or NON_SECURE.
5826f1534eSWendy Liang  *
59de7ed953SPrasad Kummari  * Return: Unused.
6026f1534eSWendy Liang  *
6126f1534eSWendy Liang  * Determines that smc_fid is valid and supported PM SMC Function ID from the
6226f1534eSWendy Liang  * list of pm_api_ids, otherwise completes the request with
63de7ed953SPrasad Kummari  * the unknown SMC Function ID.
6426f1534eSWendy Liang  *
6526f1534eSWendy Liang  * The SMC calls for PM service are forwarded from SIP Service SMC handler
66de7ed953SPrasad Kummari  * function with rt_svc_handle signature.
67de7ed953SPrasad Kummari  *
6826f1534eSWendy Liang  */
ipi_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)6926f1534eSWendy Liang uint64_t ipi_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2,
703d2ebe75SVenkatesh Yadav Abbarapu 			 uint64_t x3, uint64_t x4, const void *cookie,
7126f1534eSWendy Liang 			 void *handle, uint64_t flags)
7226f1534eSWendy Liang {
73d3bb350cSMaheedhar Bollapalli 	(void) x4;
74d3bb350cSMaheedhar Bollapalli 	(void) flags;
75d3bb350cSMaheedhar Bollapalli 	(void) cookie;
76ffa91031SVenkatesh Yadav Abbarapu 	int32_t ret;
7726f1534eSWendy Liang 	uint32_t ipi_local_id;
7826f1534eSWendy Liang 	uint32_t ipi_remote_id;
79ffa91031SVenkatesh Yadav Abbarapu 	uint32_t is_secure;
8026f1534eSWendy Liang 
813a1a2daeSMaheedhar Bollapalli 	ipi_local_id = (uint32_t)(x1 & UNSIGNED32_MASK);
823a1a2daeSMaheedhar Bollapalli 	ipi_remote_id = (uint32_t)(x2 & UNSIGNED32_MASK);
8326f1534eSWendy Liang 
844e46db40SAkshay Belsare 	/* OEN Number 48 to 63 is for Trusted App and OS
854e46db40SAkshay Belsare 	 * GET_SMC_OEN limits the return value of OEN number to 63 by bitwise
864e46db40SAkshay Belsare 	 * AND operation with 0x3F.
874e46db40SAkshay Belsare 	 * Upper limit check for OEN value is not required.
884e46db40SAkshay Belsare 	 */
894e46db40SAkshay Belsare 	if (GET_SMC_OEN(smc_fid) >= OEN_TAP_START) {
9026f1534eSWendy Liang 		is_secure = 1;
910ee07d79SAkshay Belsare 	} else {
9226f1534eSWendy Liang 		is_secure = 0;
930ee07d79SAkshay Belsare 	}
9426f1534eSWendy Liang 
9526f1534eSWendy Liang 	/* Validate IPI mailbox access */
9626f1534eSWendy Liang 	ret = ipi_mb_validate(ipi_local_id, ipi_remote_id, is_secure);
97e2230375SMaheedhar Bollapalli 	if (ret != 0)
9826f1534eSWendy Liang 		SMC_RET1(handle, ret);
9926f1534eSWendy Liang 
1000ee07d79SAkshay Belsare 	switch (GET_SMC_NUM(smc_fid)) {
10187904ba8SSaivardhan Thatikonda 	case (uint32_t)IPI_MAILBOX_OPEN:
10226f1534eSWendy Liang 		ipi_mb_open(ipi_local_id, ipi_remote_id);
10326f1534eSWendy Liang 		SMC_RET1(handle, 0);
10487904ba8SSaivardhan Thatikonda 	case (uint32_t)IPI_MAILBOX_RELEASE:
10526f1534eSWendy Liang 		ipi_mb_release(ipi_local_id, ipi_remote_id);
10626f1534eSWendy Liang 		SMC_RET1(handle, 0);
10787904ba8SSaivardhan Thatikonda 	case (uint32_t)IPI_MAILBOX_STATUS_ENQUIRY:
10826f1534eSWendy Liang 	{
1097d15b94bSMaheedhar Bollapalli 		bool disable_interrupt;
11026f1534eSWendy Liang 
1117d15b94bSMaheedhar Bollapalli 		disable_interrupt = ((x3 & IPI_SMC_ENQUIRY_DIRQ_MASK) != 0U);
112bdba3c84SDevanshi Chauhan Alpeshbhai 		ret = (int32_t)ipi_mb_enquire_status(ipi_local_id, ipi_remote_id);
113e4a0c44fSNithin G 		if ((((uint32_t)ret & IPI_MB_STATUS_RECV_PENDING) > 0U) && disable_interrupt) {
11426f1534eSWendy Liang 			ipi_mb_disable_irq(ipi_local_id, ipi_remote_id);
115e4a0c44fSNithin G 		}
11626f1534eSWendy Liang 		SMC_RET1(handle, ret);
11726f1534eSWendy Liang 	}
11887904ba8SSaivardhan Thatikonda 	case (uint32_t)IPI_MAILBOX_NOTIFY:
11926f1534eSWendy Liang 	{
12026f1534eSWendy Liang 		uint32_t is_blocking;
12126f1534eSWendy Liang 
12287904ba8SSaivardhan Thatikonda 		is_blocking = (uint32_t)((x3 & IPI_SMC_NOTIFY_BLOCK_MASK) != 0U);
12362f9134dSVenkatesh Yadav Abbarapu 		ipi_mb_notify(ipi_local_id, ipi_remote_id, is_blocking);
12462f9134dSVenkatesh Yadav Abbarapu 		SMC_RET1(handle, 0);
12526f1534eSWendy Liang 	}
12687904ba8SSaivardhan Thatikonda 	case (uint32_t)IPI_MAILBOX_ACK:
12726f1534eSWendy Liang 	{
1287d15b94bSMaheedhar Bollapalli 		bool enable_interrupt;
12926f1534eSWendy Liang 
1307d15b94bSMaheedhar Bollapalli 		enable_interrupt = ((x3 & IPI_SMC_ACK_EIRQ_MASK) != 0U);
13126f1534eSWendy Liang 		ipi_mb_ack(ipi_local_id, ipi_remote_id);
132e4a0c44fSNithin G 		if (enable_interrupt != 0) {
13326f1534eSWendy Liang 			ipi_mb_enable_irq(ipi_local_id, ipi_remote_id);
134e4a0c44fSNithin G 		}
13526f1534eSWendy Liang 		SMC_RET1(handle, 0);
13626f1534eSWendy Liang 	}
13787904ba8SSaivardhan Thatikonda 	case (uint32_t)IPI_MAILBOX_ENABLE_IRQ:
13826f1534eSWendy Liang 		ipi_mb_enable_irq(ipi_local_id, ipi_remote_id);
13926f1534eSWendy Liang 		SMC_RET1(handle, 0);
14087904ba8SSaivardhan Thatikonda 	case (uint32_t)IPI_MAILBOX_DISABLE_IRQ:
14126f1534eSWendy Liang 		ipi_mb_disable_irq(ipi_local_id, ipi_remote_id);
14226f1534eSWendy Liang 		SMC_RET1(handle, 0);
14326f1534eSWendy Liang 	default:
14426f1534eSWendy Liang 		WARN("Unimplemented IPI service call: 0x%x\n", smc_fid);
14526f1534eSWendy Liang 		SMC_RET1(handle, SMC_UNK);
14626f1534eSWendy Liang 	}
14726f1534eSWendy Liang }
148