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