xref: /rk3399_ARM-atf/plat/xilinx/common/ipi_mailbox_service/ipi_mailbox_svc.c (revision 1d2706dbaf98634aa1eecc65e52b54acf330df3d)
1 /*
2  * Copyright (c) 2017-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /*
8  * Top-level SMC handler for ZynqMP IPI Mailbox doorbell functions.
9  */
10 
11 #include <errno.h>
12 #include <string.h>
13 
14 #include <common/debug.h>
15 #include <common/runtime_svc.h>
16 #include <lib/bakery_lock.h>
17 #include <lib/mmio.h>
18 
19 #include <ipi.h>
20 #include <plat_ipi.h>
21 #include <plat_private.h>
22 
23 #include "ipi_mailbox_svc.h"
24 
25 /*********************************************************************
26  * Macros definitions
27  ********************************************************************/
28 
29 /* IPI SMC calls macros: */
30 #define IPI_SMC_OPEN_IRQ_MASK		0x00000001U /* IRQ enable bit in IPI
31 						     * open SMC call
32 						     */
33 #define IPI_SMC_NOTIFY_BLOCK_MASK	0x00000001U /* Flag to indicate if
34 						     * IPI notification needs
35 						     * to be blocking.
36 						     */
37 #define IPI_SMC_ENQUIRY_DIRQ_MASK	0x00000001U /* Flag to indicate if
38 						     * notification interrupt
39 						     * to be disabled.
40 						     */
41 #define IPI_SMC_ACK_EIRQ_MASK		0x00000001U /* Flag to indicate if
42 						     * notification interrupt
43 						     * to be enable.
44 						     */
45 
46 #define UNSIGNED32_MASK			0xFFFFFFFFU /* 32bit mask */
47 
48 /**
49  * ipi_smc_handler() - SMC handler for IPI SMC calls
50  *
51  * @smc_fid - Function identifier
52  * @x1 - x4 - Arguments
53  * @cookie  - Unused
54  * @handler - Pointer to caller's context structure
55  *
56  * @return  - Unused
57  *
58  * Determines that smc_fid is valid and supported PM SMC Function ID from the
59  * list of pm_api_ids, otherwise completes the request with
60  * the unknown SMC Function ID
61  *
62  * The SMC calls for PM service are forwarded from SIP Service SMC handler
63  * function with rt_svc_handle signature
64  */
65 uint64_t ipi_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2,
66 			 uint64_t x3, uint64_t x4, const void *cookie,
67 			 void *handle, uint64_t flags)
68 {
69 	int32_t ret;
70 	uint32_t ipi_local_id;
71 	uint32_t ipi_remote_id;
72 	uint32_t is_secure;
73 
74 	ipi_local_id = x1 & UNSIGNED32_MASK;
75 	ipi_remote_id = x2 & UNSIGNED32_MASK;
76 
77 	/* OEN Number 48 to 63 is for Trusted App and OS
78 	 * GET_SMC_OEN limits the return value of OEN number to 63 by bitwise
79 	 * AND operation with 0x3F.
80 	 * Upper limit check for OEN value is not required.
81 	 */
82 	if (GET_SMC_OEN(smc_fid) >= OEN_TAP_START) {
83 		is_secure = 1;
84 	} else {
85 		is_secure = 0;
86 	}
87 
88 	/* Validate IPI mailbox access */
89 	ret = ipi_mb_validate(ipi_local_id, ipi_remote_id, is_secure);
90 	if (ret)
91 		SMC_RET1(handle, ret);
92 
93 	switch (GET_SMC_NUM(smc_fid)) {
94 	case IPI_MAILBOX_OPEN:
95 		ipi_mb_open(ipi_local_id, ipi_remote_id);
96 		SMC_RET1(handle, 0);
97 	case IPI_MAILBOX_RELEASE:
98 		ipi_mb_release(ipi_local_id, ipi_remote_id);
99 		SMC_RET1(handle, 0);
100 	case IPI_MAILBOX_STATUS_ENQUIRY:
101 	{
102 		int32_t disable_irq;
103 
104 		disable_irq = (x3 & IPI_SMC_ENQUIRY_DIRQ_MASK) ? 1 : 0;
105 		ret = ipi_mb_enquire_status(ipi_local_id, ipi_remote_id);
106 		if ((ret & IPI_MB_STATUS_RECV_PENDING) && disable_irq)
107 			ipi_mb_disable_irq(ipi_local_id, ipi_remote_id);
108 		SMC_RET1(handle, ret);
109 	}
110 	case IPI_MAILBOX_NOTIFY:
111 	{
112 		uint32_t is_blocking;
113 
114 		is_blocking = (x3 & IPI_SMC_NOTIFY_BLOCK_MASK) ? 1 : 0;
115 		ipi_mb_notify(ipi_local_id, ipi_remote_id, is_blocking);
116 		SMC_RET1(handle, 0);
117 	}
118 	case IPI_MAILBOX_ACK:
119 	{
120 		int32_t enable_irq;
121 
122 		enable_irq = (x3 & IPI_SMC_ACK_EIRQ_MASK) ? 1 : 0;
123 		ipi_mb_ack(ipi_local_id, ipi_remote_id);
124 		if (enable_irq)
125 			ipi_mb_enable_irq(ipi_local_id, ipi_remote_id);
126 		SMC_RET1(handle, 0);
127 	}
128 	case IPI_MAILBOX_ENABLE_IRQ:
129 		ipi_mb_enable_irq(ipi_local_id, ipi_remote_id);
130 		SMC_RET1(handle, 0);
131 	case IPI_MAILBOX_DISABLE_IRQ:
132 		ipi_mb_disable_irq(ipi_local_id, ipi_remote_id);
133 		SMC_RET1(handle, 0);
134 	default:
135 		WARN("Unimplemented IPI service call: 0x%x\n", smc_fid);
136 		SMC_RET1(handle, SMC_UNK);
137 	}
138 }
139