xref: /rk3399_ARM-atf/plat/xilinx/common/pm_service/pm_ipi.c (revision 6dc5979a6cb2121e4c16e7bd62e24030e0f42755)
1 /*
2  * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved.
3  * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved.
4  * Copyright (c) 2022, Advanced Micro Devices, Inc. All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 
10 #include <arch_helpers.h>
11 #include <lib/bakery_lock.h>
12 #include <lib/mmio.h>
13 #include <ipi.h>
14 #include <plat_ipi.h>
15 #include <plat_private.h>
16 #include <plat/common/platform.h>
17 
18 #include "pm_defs.h"
19 #include "pm_ipi.h"
20 
21 #define ERROR_CODE_MASK		(0xFFFFU)
22 
23 DEFINE_BAKERY_LOCK(pm_secure_lock);
24 
25 /**
26  * pm_ipi_init() - Initialize IPI peripheral for communication with
27  *		   remote processor
28  *
29  * @proc	Pointer to the processor who is initiating request
30  * @return	On success, the initialization function must return 0.
31  *		Any other return value will cause the framework to ignore
32  *		the service
33  *
34  * Called from pm_setup initialization function
35  */
36 int32_t pm_ipi_init(const struct pm_proc *proc)
37 {
38 	bakery_lock_init(&pm_secure_lock);
39 	ipi_mb_open(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
40 
41 	return 0;
42 }
43 
44 /**
45  * pm_ipi_send_common() - Sends IPI request to the remote processor
46  * @proc	Pointer to the processor who is initiating request
47  * @payload	API id and call arguments to be written in IPI buffer
48  *
49  * Send an IPI request to the power controller. Caller needs to hold
50  * the 'pm_secure_lock' lock.
51  *
52  * @return	Returns status, either success or error+reason
53  */
54 static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc,
55 					     uint32_t payload[PAYLOAD_ARG_CNT],
56 					     uint32_t is_blocking)
57 {
58 	unsigned int offset = 0;
59 	uintptr_t buffer_base = proc->ipi->buffer_base +
60 					IPI_BUFFER_TARGET_REMOTE_OFFSET +
61 					IPI_BUFFER_REQ_OFFSET;
62 #if IPI_CRC_CHECK
63 	payload[PAYLOAD_CRC_POS] = calculate_crc(payload, IPI_W0_TO_W6_SIZE);
64 #endif
65 
66 	/* Write payload into IPI buffer */
67 	for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) {
68 		mmio_write_32(buffer_base + offset, payload[i]);
69 		offset += PAYLOAD_ARG_SIZE;
70 	}
71 
72 	/* Generate IPI to remote processor */
73 	ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id,
74 		      is_blocking);
75 
76 	return PM_RET_SUCCESS;
77 }
78 
79 /**
80  * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor
81  *			        without blocking notification
82  * @proc	Pointer to the processor who is initiating request
83  * @payload	API id and call arguments to be written in IPI buffer
84  *
85  * Send an IPI request to the power controller.
86  *
87  * @return	Returns status, either success or error+reason
88  */
89 enum pm_ret_status pm_ipi_send_non_blocking(const struct pm_proc *proc,
90 					    uint32_t payload[PAYLOAD_ARG_CNT])
91 {
92 	enum pm_ret_status ret;
93 
94 	bakery_lock_get(&pm_secure_lock);
95 
96 	ret = pm_ipi_send_common(proc, payload, IPI_NON_BLOCKING);
97 
98 	bakery_lock_release(&pm_secure_lock);
99 
100 	return ret;
101 }
102 
103 /**
104  * pm_ipi_send() - Sends IPI request to the remote processor
105  * @proc	Pointer to the processor who is initiating request
106  * @payload	API id and call arguments to be written in IPI buffer
107  *
108  * Send an IPI request to the power controller.
109  *
110  * @return	Returns status, either success or error+reason
111  */
112 enum pm_ret_status pm_ipi_send(const struct pm_proc *proc,
113 			       uint32_t payload[PAYLOAD_ARG_CNT])
114 {
115 	enum pm_ret_status ret;
116 
117 	bakery_lock_get(&pm_secure_lock);
118 
119 	ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
120 
121 	bakery_lock_release(&pm_secure_lock);
122 
123 	return ret;
124 }
125 
126 
127 /**
128  * pm_ipi_buff_read() - Reads IPI response after remote processor has handled
129  *			interrupt
130  * @proc	Pointer to the processor who is waiting and reading response
131  * @value	Used to return value from IPI buffer element (optional)
132  * @count	Number of values to return in @value
133  *
134  * @return	Returns status, either success or error+reason
135  */
136 static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc,
137 					   uint32_t *value, size_t count)
138 {
139 	size_t i;
140 #if IPI_CRC_CHECK
141 	size_t j;
142 	uint32_t response_payload[PAYLOAD_ARG_CNT];
143 #endif
144 	uintptr_t buffer_base = proc->ipi->buffer_base +
145 				IPI_BUFFER_TARGET_REMOTE_OFFSET +
146 				IPI_BUFFER_RESP_OFFSET;
147 
148 	/*
149 	 * Read response from IPI buffer
150 	 * buf-0: success or error+reason
151 	 * buf-1: value
152 	 * buf-2: unused
153 	 * buf-3: unused
154 	 */
155 	for (i = 1; i <= count; i++) {
156 		*value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
157 		value++;
158 	}
159 #if IPI_CRC_CHECK
160 	for (j = 0; j < PAYLOAD_ARG_CNT; j++) {
161 		response_payload[j] = mmio_read_32(buffer_base +
162 						(j * PAYLOAD_ARG_SIZE));
163 	}
164 
165 	if (response_payload[PAYLOAD_CRC_POS] !=
166 			calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) {
167 		NOTICE("ERROR in CRC response payload value:0x%x\n",
168 					response_payload[PAYLOAD_CRC_POS]);
169 	}
170 #endif
171 
172 	return mmio_read_32(buffer_base);
173 }
174 
175 /**
176  * pm_ipi_buff_read_callb() - Reads IPI response after remote processor has
177  *			      handled interrupt
178  * @value	Used to return value from IPI buffer element (optional)
179  * @count	Number of values to return in @value
180  *
181  * @return	Returns status, either success or error+reason
182  */
183 void pm_ipi_buff_read_callb(uint32_t *value, size_t count)
184 {
185 	size_t i;
186 #if IPI_CRC_CHECK
187 	size_t j;
188 	unsigned int response_payload[PAYLOAD_ARG_CNT];
189 #endif
190 	uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE +
191 				IPI_BUFFER_TARGET_LOCAL_OFFSET +
192 				IPI_BUFFER_REQ_OFFSET;
193 
194 	if (count > IPI_BUFFER_MAX_WORDS) {
195 		count = IPI_BUFFER_MAX_WORDS;
196 	}
197 
198 	for (i = 0; i <= count; i++) {
199 		*value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
200 		value++;
201 	}
202 #if IPI_CRC_CHECK
203 	for (j = 0; j < PAYLOAD_ARG_CNT; j++) {
204 		response_payload[j] = mmio_read_32(buffer_base +
205 						(j * PAYLOAD_ARG_SIZE));
206 	}
207 
208 	if (response_payload[PAYLOAD_CRC_POS] !=
209 			calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) {
210 		NOTICE("ERROR in CRC response payload value:0x%x\n",
211 					response_payload[PAYLOAD_CRC_POS]);
212 	}
213 #endif
214 }
215 
216 /**
217  * pm_ipi_send_sync() - Sends IPI request to the remote processor
218  * @proc	Pointer to the processor who is initiating request
219  * @payload	API id and call arguments to be written in IPI buffer
220  * @value	Used to return value from IPI buffer element (optional)
221  * @count	Number of values to return in @value
222  *
223  * Send an IPI request to the power controller and wait for it to be handled.
224  *
225  * @return	Returns status, either success or error+reason and, optionally,
226  *		@value
227  */
228 enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc,
229 				    uint32_t payload[PAYLOAD_ARG_CNT],
230 				    uint32_t *value, size_t count)
231 {
232 	enum pm_ret_status ret;
233 
234 	bakery_lock_get(&pm_secure_lock);
235 
236 	ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
237 	if (ret != PM_RET_SUCCESS) {
238 		goto unlock;
239 	}
240 
241 	ret = ERROR_CODE_MASK & (pm_ipi_buff_read(proc, value, count));
242 
243 unlock:
244 	bakery_lock_release(&pm_secure_lock);
245 
246 	return ret;
247 }
248 
249 void pm_ipi_irq_enable(const struct pm_proc *proc)
250 {
251 	ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
252 }
253 
254 void pm_ipi_irq_clear(const struct pm_proc *proc)
255 {
256 	ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
257 }
258 
259 uint32_t pm_ipi_irq_status(const struct pm_proc *proc)
260 {
261 	int32_t ret;
262 
263 	ret = ipi_mb_enquire_status(proc->ipi->local_ipi_id,
264 				    proc->ipi->remote_ipi_id);
265 	if (ret & IPI_MB_STATUS_RECV_PENDING) {
266 		return 1;
267 	} else {
268 		return 0;
269 	}
270 }
271 
272 #if IPI_CRC_CHECK
273 uint32_t calculate_crc(uint32_t payload[PAYLOAD_ARG_CNT], uint32_t bufsize)
274 {
275 	uint32_t crcinit = CRC_INIT_VALUE;
276 	uint32_t order   = CRC_ORDER;
277 	uint32_t polynom = CRC_POLYNOM;
278 	uint32_t i, j, c, bit, datain, crcmask, crchighbit;
279 	uint32_t crc = crcinit;
280 
281 	crcmask = ((uint32_t)((1U << (order - 1U)) - 1U) << 1U) | 1U;
282 	crchighbit = (uint32_t)(1U << (order - 1U));
283 
284 	for (i = 0U; i < bufsize; i++) {
285 		datain = mmio_read_8((unsigned long)payload + i);
286 		c = datain;
287 		j = 0x80U;
288 		while (j != 0U) {
289 			bit = crc & crchighbit;
290 			crc <<= 1U;
291 			if (0U != (c & j))
292 				bit ^= crchighbit;
293 			if (bit != 0U)
294 				crc ^= polynom;
295 			j >>= 1U;
296 		}
297 		crc &= crcmask;
298 	}
299 	return crc;
300 }
301 #endif
302