xref: /rk3399_ARM-atf/plat/xilinx/common/pm_service/pm_ipi.c (revision 6173d914d673249ec47c080909c31a1654545913)
165c80d60SJolly Shah /*
2fe550edeSVenkatesh Yadav Abbarapu  * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved.
328ba1400SRajan Vaja  * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved.
428ba1400SRajan Vaja  * Copyright (c) 2022, Advanced Micro Devices, Inc. All rights reserved.
565c80d60SJolly Shah  *
665c80d60SJolly Shah  * SPDX-License-Identifier: BSD-3-Clause
765c80d60SJolly Shah  */
865c80d60SJolly Shah 
965c80d60SJolly Shah 
1065c80d60SJolly Shah #include <arch_helpers.h>
1165c80d60SJolly Shah #include <lib/bakery_lock.h>
1265c80d60SJolly Shah #include <lib/mmio.h>
1365c80d60SJolly Shah #include <ipi.h>
1465c80d60SJolly Shah #include <plat_ipi.h>
1565c80d60SJolly Shah #include <plat_private.h>
1665c80d60SJolly Shah #include <plat/common/platform.h>
1765c80d60SJolly Shah 
1828ba1400SRajan Vaja #include "pm_defs.h"
1965c80d60SJolly Shah #include "pm_ipi.h"
2065c80d60SJolly Shah 
2115dc3e4fSHariBabu Gattem #define ERROR_CODE_MASK		(0xFFFFU)
22590519a8SHariBabu Gattem #define PM_OFFSET		(0U)
23addc4e96SRavi Patel 
2465c80d60SJolly Shah DEFINE_BAKERY_LOCK(pm_secure_lock);
2565c80d60SJolly Shah 
2665c80d60SJolly Shah /**
2765c80d60SJolly Shah  * pm_ipi_init() - Initialize IPI peripheral for communication with
2865c80d60SJolly Shah  *		   remote processor
2965c80d60SJolly Shah  *
3065c80d60SJolly Shah  * @proc	Pointer to the processor who is initiating request
3165c80d60SJolly Shah  * @return	On success, the initialization function must return 0.
3265c80d60SJolly Shah  *		Any other return value will cause the framework to ignore
3365c80d60SJolly Shah  *		the service
3465c80d60SJolly Shah  *
3565c80d60SJolly Shah  * Called from pm_setup initialization function
3665c80d60SJolly Shah  */
37590519a8SHariBabu Gattem void pm_ipi_init(const struct pm_proc *proc)
3865c80d60SJolly Shah {
3965c80d60SJolly Shah 	bakery_lock_init(&pm_secure_lock);
4065c80d60SJolly Shah 	ipi_mb_open(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
4165c80d60SJolly Shah }
4265c80d60SJolly Shah 
4365c80d60SJolly Shah /**
4465c80d60SJolly Shah  * pm_ipi_send_common() - Sends IPI request to the remote processor
4565c80d60SJolly Shah  * @proc	Pointer to the processor who is initiating request
4665c80d60SJolly Shah  * @payload	API id and call arguments to be written in IPI buffer
4765c80d60SJolly Shah  *
4865c80d60SJolly Shah  * Send an IPI request to the power controller. Caller needs to hold
4965c80d60SJolly Shah  * the 'pm_secure_lock' lock.
5065c80d60SJolly Shah  *
5165c80d60SJolly Shah  * @return	Returns status, either success or error+reason
5265c80d60SJolly Shah  */
5365c80d60SJolly Shah static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc,
5465c80d60SJolly Shah 					     uint32_t payload[PAYLOAD_ARG_CNT],
5565c80d60SJolly Shah 					     uint32_t is_blocking)
5665c80d60SJolly Shah {
57590519a8SHariBabu Gattem 	uint32_t offset = PM_OFFSET;
5865c80d60SJolly Shah 	uintptr_t buffer_base = proc->ipi->buffer_base +
5965c80d60SJolly Shah 					IPI_BUFFER_TARGET_REMOTE_OFFSET +
6065c80d60SJolly Shah 					IPI_BUFFER_REQ_OFFSET;
61d7758354SVenkatesh Yadav Abbarapu #if IPI_CRC_CHECK
62fe550edeSVenkatesh Yadav Abbarapu 	payload[PAYLOAD_CRC_POS] = calculate_crc(payload, IPI_W0_TO_W6_SIZE);
63fe550edeSVenkatesh Yadav Abbarapu #endif
6465c80d60SJolly Shah 
6565c80d60SJolly Shah 	/* Write payload into IPI buffer */
6665c80d60SJolly Shah 	for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) {
6765c80d60SJolly Shah 		mmio_write_32(buffer_base + offset, payload[i]);
6865c80d60SJolly Shah 		offset += PAYLOAD_ARG_SIZE;
6965c80d60SJolly Shah 	}
7065c80d60SJolly Shah 
7165c80d60SJolly Shah 	/* Generate IPI to remote processor */
7262f9134dSVenkatesh Yadav Abbarapu 	ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id,
7365c80d60SJolly Shah 		      is_blocking);
7465c80d60SJolly Shah 
7562f9134dSVenkatesh Yadav Abbarapu 	return PM_RET_SUCCESS;
764d9b9b23STejas Patel }
774d9b9b23STejas Patel 
7865c80d60SJolly Shah /**
7965c80d60SJolly Shah  * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor
8065c80d60SJolly Shah  *			        without blocking notification
8165c80d60SJolly Shah  * @proc	Pointer to the processor who is initiating request
8265c80d60SJolly Shah  * @payload	API id and call arguments to be written in IPI buffer
8365c80d60SJolly Shah  *
8465c80d60SJolly Shah  * Send an IPI request to the power controller.
8565c80d60SJolly Shah  *
8665c80d60SJolly Shah  * @return	Returns status, either success or error+reason
8765c80d60SJolly Shah  */
8865c80d60SJolly Shah enum pm_ret_status pm_ipi_send_non_blocking(const struct pm_proc *proc,
8965c80d60SJolly Shah 					    uint32_t payload[PAYLOAD_ARG_CNT])
9065c80d60SJolly Shah {
9165c80d60SJolly Shah 	enum pm_ret_status ret;
9265c80d60SJolly Shah 
9365c80d60SJolly Shah 	bakery_lock_get(&pm_secure_lock);
9465c80d60SJolly Shah 
9565c80d60SJolly Shah 	ret = pm_ipi_send_common(proc, payload, IPI_NON_BLOCKING);
9665c80d60SJolly Shah 
9765c80d60SJolly Shah 	bakery_lock_release(&pm_secure_lock);
9865c80d60SJolly Shah 
9965c80d60SJolly Shah 	return ret;
10065c80d60SJolly Shah }
10165c80d60SJolly Shah 
10265c80d60SJolly Shah /**
10365c80d60SJolly Shah  * pm_ipi_send() - Sends IPI request to the remote processor
10465c80d60SJolly Shah  * @proc	Pointer to the processor who is initiating request
10565c80d60SJolly Shah  * @payload	API id and call arguments to be written in IPI buffer
10665c80d60SJolly Shah  *
10765c80d60SJolly Shah  * Send an IPI request to the power controller.
10865c80d60SJolly Shah  *
10965c80d60SJolly Shah  * @return	Returns status, either success or error+reason
11065c80d60SJolly Shah  */
11165c80d60SJolly Shah enum pm_ret_status pm_ipi_send(const struct pm_proc *proc,
11265c80d60SJolly Shah 			       uint32_t payload[PAYLOAD_ARG_CNT])
11365c80d60SJolly Shah {
11465c80d60SJolly Shah 	enum pm_ret_status ret;
11565c80d60SJolly Shah 
11665c80d60SJolly Shah 	bakery_lock_get(&pm_secure_lock);
11765c80d60SJolly Shah 
11865c80d60SJolly Shah 	ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
11965c80d60SJolly Shah 
12065c80d60SJolly Shah 	bakery_lock_release(&pm_secure_lock);
12165c80d60SJolly Shah 
12265c80d60SJolly Shah 	return ret;
12365c80d60SJolly Shah }
12465c80d60SJolly Shah 
12565c80d60SJolly Shah 
12665c80d60SJolly Shah /**
12765c80d60SJolly Shah  * pm_ipi_buff_read() - Reads IPI response after remote processor has handled
12865c80d60SJolly Shah  *			interrupt
12965c80d60SJolly Shah  * @proc	Pointer to the processor who is waiting and reading response
13065c80d60SJolly Shah  * @value	Used to return value from IPI buffer element (optional)
13165c80d60SJolly Shah  * @count	Number of values to return in @value
13265c80d60SJolly Shah  *
13365c80d60SJolly Shah  * @return	Returns status, either success or error+reason
13465c80d60SJolly Shah  */
13565c80d60SJolly Shah static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc,
136ffa91031SVenkatesh Yadav Abbarapu 					   uint32_t *value, size_t count)
13765c80d60SJolly Shah {
13865c80d60SJolly Shah 	size_t i;
1395e92be51SNaman Trivedi Manojbhai 	enum pm_ret_status ret;
140d7758354SVenkatesh Yadav Abbarapu #if IPI_CRC_CHECK
1415e92be51SNaman Trivedi Manojbhai 	uint32_t *payload_ptr = value;
142fe550edeSVenkatesh Yadav Abbarapu 	size_t j;
143ffa91031SVenkatesh Yadav Abbarapu 	uint32_t response_payload[PAYLOAD_ARG_CNT];
144fe550edeSVenkatesh Yadav Abbarapu #endif
14565c80d60SJolly Shah 	uintptr_t buffer_base = proc->ipi->buffer_base +
14665c80d60SJolly Shah 				IPI_BUFFER_TARGET_REMOTE_OFFSET +
14765c80d60SJolly Shah 				IPI_BUFFER_RESP_OFFSET;
14865c80d60SJolly Shah 
14965c80d60SJolly Shah 	/*
15065c80d60SJolly Shah 	 * Read response from IPI buffer
15165c80d60SJolly Shah 	 * buf-0: success or error+reason
15265c80d60SJolly Shah 	 * buf-1: value
15365c80d60SJolly Shah 	 * buf-2: unused
15465c80d60SJolly Shah 	 * buf-3: unused
15565c80d60SJolly Shah 	 */
15665c80d60SJolly Shah 	for (i = 1; i <= count; i++) {
15765c80d60SJolly Shah 		*value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
15865c80d60SJolly Shah 		value++;
15965c80d60SJolly Shah 	}
1605e92be51SNaman Trivedi Manojbhai 
1615e92be51SNaman Trivedi Manojbhai 	ret = mmio_read_32(buffer_base);
162d7758354SVenkatesh Yadav Abbarapu #if IPI_CRC_CHECK
163eb0d2b17SVenkatesh Yadav Abbarapu 	for (j = 0; j < PAYLOAD_ARG_CNT; j++) {
164fe550edeSVenkatesh Yadav Abbarapu 		response_payload[j] = mmio_read_32(buffer_base +
165fe550edeSVenkatesh Yadav Abbarapu 						(j * PAYLOAD_ARG_SIZE));
166eb0d2b17SVenkatesh Yadav Abbarapu 	}
167fe550edeSVenkatesh Yadav Abbarapu 
168fe550edeSVenkatesh Yadav Abbarapu 	if (response_payload[PAYLOAD_CRC_POS] !=
169eb0d2b17SVenkatesh Yadav Abbarapu 			calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) {
170fe550edeSVenkatesh Yadav Abbarapu 		NOTICE("ERROR in CRC response payload value:0x%x\n",
171fe550edeSVenkatesh Yadav Abbarapu 					response_payload[PAYLOAD_CRC_POS]);
1725e92be51SNaman Trivedi Manojbhai 		ret = PM_RET_ERROR_INVALID_CRC;
1735e92be51SNaman Trivedi Manojbhai 		/* Payload data is invalid as CRC validation failed
1745e92be51SNaman Trivedi Manojbhai 		 * Clear the payload to avoid leakage of data to upper layers
1755e92be51SNaman Trivedi Manojbhai 		 */
1765e92be51SNaman Trivedi Manojbhai 		memset(payload_ptr, 0, count);
177eb0d2b17SVenkatesh Yadav Abbarapu 	}
178fe550edeSVenkatesh Yadav Abbarapu #endif
17965c80d60SJolly Shah 
1805e92be51SNaman Trivedi Manojbhai 	return ret;
18165c80d60SJolly Shah }
18265c80d60SJolly Shah 
18365c80d60SJolly Shah /**
184b96065a0SNaman Patel  * pm_ipi_buff_read_callb() - Callback function that reads value from
185b96065a0SNaman Patel  *			      ipi response buffer
186b96065a0SNaman Patel  * @value	Used to return value from IPI buffer element
18765c80d60SJolly Shah  * @count	Number of values to return in @value
18865c80d60SJolly Shah  *
189b96065a0SNaman Patel  * This callback function fills requested data in @value from ipi response
190b96065a0SNaman Patel  * buffer.
191*6173d914SNaman Trivedi Manojbhai  * @return 	Returns status, either success or error
19265c80d60SJolly Shah  */
193*6173d914SNaman Trivedi Manojbhai enum pm_ret_status pm_ipi_buff_read_callb(uint32_t *value, size_t count)
19465c80d60SJolly Shah {
19565c80d60SJolly Shah 	size_t i;
196d7758354SVenkatesh Yadav Abbarapu #if IPI_CRC_CHECK
197*6173d914SNaman Trivedi Manojbhai 	uint32_t *payload_ptr = value;
198fe550edeSVenkatesh Yadav Abbarapu 	size_t j;
199590519a8SHariBabu Gattem 	unsigned int response_payload[PAYLOAD_ARG_CNT] = {0};
200fe550edeSVenkatesh Yadav Abbarapu #endif
20165c80d60SJolly Shah 	uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE +
20265c80d60SJolly Shah 				IPI_BUFFER_TARGET_LOCAL_OFFSET +
20365c80d60SJolly Shah 				IPI_BUFFER_REQ_OFFSET;
204*6173d914SNaman Trivedi Manojbhai 	enum pm_ret_status ret = PM_RET_SUCCESS;
20565c80d60SJolly Shah 
206eb0d2b17SVenkatesh Yadav Abbarapu 	if (count > IPI_BUFFER_MAX_WORDS) {
20765c80d60SJolly Shah 		count = IPI_BUFFER_MAX_WORDS;
208eb0d2b17SVenkatesh Yadav Abbarapu 	}
20965c80d60SJolly Shah 
21065c80d60SJolly Shah 	for (i = 0; i <= count; i++) {
21165c80d60SJolly Shah 		*value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
21265c80d60SJolly Shah 		value++;
21365c80d60SJolly Shah 	}
214d7758354SVenkatesh Yadav Abbarapu #if IPI_CRC_CHECK
215eb0d2b17SVenkatesh Yadav Abbarapu 	for (j = 0; j < PAYLOAD_ARG_CNT; j++) {
216fe550edeSVenkatesh Yadav Abbarapu 		response_payload[j] = mmio_read_32(buffer_base +
217fe550edeSVenkatesh Yadav Abbarapu 						(j * PAYLOAD_ARG_SIZE));
218eb0d2b17SVenkatesh Yadav Abbarapu 	}
219fe550edeSVenkatesh Yadav Abbarapu 
220fe550edeSVenkatesh Yadav Abbarapu 	if (response_payload[PAYLOAD_CRC_POS] !=
221eb0d2b17SVenkatesh Yadav Abbarapu 			calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) {
222fe550edeSVenkatesh Yadav Abbarapu 		NOTICE("ERROR in CRC response payload value:0x%x\n",
223fe550edeSVenkatesh Yadav Abbarapu 					response_payload[PAYLOAD_CRC_POS]);
224*6173d914SNaman Trivedi Manojbhai 		ret = PM_RET_ERROR_INVALID_CRC;
225*6173d914SNaman Trivedi Manojbhai 		/* Payload data is invalid as CRC validation failed
226*6173d914SNaman Trivedi Manojbhai 		 * Clear the payload to avoid leakage of data to upper layers
227*6173d914SNaman Trivedi Manojbhai 		 */
228*6173d914SNaman Trivedi Manojbhai 		memset(payload_ptr, 0, count);
229eb0d2b17SVenkatesh Yadav Abbarapu 	}
230fe550edeSVenkatesh Yadav Abbarapu #endif
231*6173d914SNaman Trivedi Manojbhai 	return ret;
23265c80d60SJolly Shah }
23365c80d60SJolly Shah 
23465c80d60SJolly Shah /**
23565c80d60SJolly Shah  * pm_ipi_send_sync() - Sends IPI request to the remote processor
23665c80d60SJolly Shah  * @proc	Pointer to the processor who is initiating request
23765c80d60SJolly Shah  * @payload	API id and call arguments to be written in IPI buffer
23865c80d60SJolly Shah  * @value	Used to return value from IPI buffer element (optional)
23965c80d60SJolly Shah  * @count	Number of values to return in @value
24065c80d60SJolly Shah  *
24165c80d60SJolly Shah  * Send an IPI request to the power controller and wait for it to be handled.
24265c80d60SJolly Shah  *
24365c80d60SJolly Shah  * @return	Returns status, either success or error+reason and, optionally,
24465c80d60SJolly Shah  *		@value
24565c80d60SJolly Shah  */
24665c80d60SJolly Shah enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc,
24765c80d60SJolly Shah 				    uint32_t payload[PAYLOAD_ARG_CNT],
248ffa91031SVenkatesh Yadav Abbarapu 				    uint32_t *value, size_t count)
24965c80d60SJolly Shah {
25065c80d60SJolly Shah 	enum pm_ret_status ret;
25165c80d60SJolly Shah 
25265c80d60SJolly Shah 	bakery_lock_get(&pm_secure_lock);
25365c80d60SJolly Shah 
25465c80d60SJolly Shah 	ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
255eb0d2b17SVenkatesh Yadav Abbarapu 	if (ret != PM_RET_SUCCESS) {
25665c80d60SJolly Shah 		goto unlock;
257eb0d2b17SVenkatesh Yadav Abbarapu 	}
25865c80d60SJolly Shah 
259addc4e96SRavi Patel 	ret = ERROR_CODE_MASK & (pm_ipi_buff_read(proc, value, count));
26065c80d60SJolly Shah 
26165c80d60SJolly Shah unlock:
26265c80d60SJolly Shah 	bakery_lock_release(&pm_secure_lock);
26365c80d60SJolly Shah 
26465c80d60SJolly Shah 	return ret;
26565c80d60SJolly Shah }
26665c80d60SJolly Shah 
26765c80d60SJolly Shah void pm_ipi_irq_enable(const struct pm_proc *proc)
26865c80d60SJolly Shah {
26965c80d60SJolly Shah 	ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
27065c80d60SJolly Shah }
27165c80d60SJolly Shah 
27265c80d60SJolly Shah void pm_ipi_irq_clear(const struct pm_proc *proc)
27365c80d60SJolly Shah {
27465c80d60SJolly Shah 	ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
27565c80d60SJolly Shah }
27665c80d60SJolly Shah 
27765c80d60SJolly Shah uint32_t pm_ipi_irq_status(const struct pm_proc *proc)
27865c80d60SJolly Shah {
279ffa91031SVenkatesh Yadav Abbarapu 	int32_t ret;
28065c80d60SJolly Shah 
28165c80d60SJolly Shah 	ret = ipi_mb_enquire_status(proc->ipi->local_ipi_id,
28265c80d60SJolly Shah 				    proc->ipi->remote_ipi_id);
283eb0d2b17SVenkatesh Yadav Abbarapu 	if (ret & IPI_MB_STATUS_RECV_PENDING) {
28465c80d60SJolly Shah 		return 1;
285eb0d2b17SVenkatesh Yadav Abbarapu 	} else {
28665c80d60SJolly Shah 		return 0;
28765c80d60SJolly Shah 	}
288eb0d2b17SVenkatesh Yadav Abbarapu }
289fe550edeSVenkatesh Yadav Abbarapu 
290d7758354SVenkatesh Yadav Abbarapu #if IPI_CRC_CHECK
29181333eacSVenkatesh Yadav Abbarapu uint32_t calculate_crc(uint32_t payload[PAYLOAD_ARG_CNT], uint32_t bufsize)
292fe550edeSVenkatesh Yadav Abbarapu {
293fe550edeSVenkatesh Yadav Abbarapu 	uint32_t crcinit = CRC_INIT_VALUE;
294fe550edeSVenkatesh Yadav Abbarapu 	uint32_t order   = CRC_ORDER;
295fe550edeSVenkatesh Yadav Abbarapu 	uint32_t polynom = CRC_POLYNOM;
296fe550edeSVenkatesh Yadav Abbarapu 	uint32_t i, j, c, bit, datain, crcmask, crchighbit;
297fe550edeSVenkatesh Yadav Abbarapu 	uint32_t crc = crcinit;
298fe550edeSVenkatesh Yadav Abbarapu 
299fe550edeSVenkatesh Yadav Abbarapu 	crcmask = ((uint32_t)((1U << (order - 1U)) - 1U) << 1U) | 1U;
300fe550edeSVenkatesh Yadav Abbarapu 	crchighbit = (uint32_t)(1U << (order - 1U));
301fe550edeSVenkatesh Yadav Abbarapu 
302fe550edeSVenkatesh Yadav Abbarapu 	for (i = 0U; i < bufsize; i++) {
303fe550edeSVenkatesh Yadav Abbarapu 		datain = mmio_read_8((unsigned long)payload + i);
304fe550edeSVenkatesh Yadav Abbarapu 		c = datain;
305fe550edeSVenkatesh Yadav Abbarapu 		j = 0x80U;
306fe550edeSVenkatesh Yadav Abbarapu 		while (j != 0U) {
307fe550edeSVenkatesh Yadav Abbarapu 			bit = crc & crchighbit;
308fe550edeSVenkatesh Yadav Abbarapu 			crc <<= 1U;
309fe550edeSVenkatesh Yadav Abbarapu 			if (0U != (c & j))
310fe550edeSVenkatesh Yadav Abbarapu 				bit ^= crchighbit;
311fe550edeSVenkatesh Yadav Abbarapu 			if (bit != 0U)
312fe550edeSVenkatesh Yadav Abbarapu 				crc ^= polynom;
313fe550edeSVenkatesh Yadav Abbarapu 			j >>= 1U;
314fe550edeSVenkatesh Yadav Abbarapu 		}
315fe550edeSVenkatesh Yadav Abbarapu 		crc &= crcmask;
316fe550edeSVenkatesh Yadav Abbarapu 	}
317fe550edeSVenkatesh Yadav Abbarapu 	return crc;
318fe550edeSVenkatesh Yadav Abbarapu }
319fe550edeSVenkatesh Yadav Abbarapu #endif
320