blob: c83d25b0d959fb56f6b26aea559f03c4c58e7e02 [file] [log] [blame]
Soren Brinkmann76fcae32016-03-06 20:16:27 -08001/*
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +05302 * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved.
Soren Brinkmann76fcae32016-03-06 20:16:27 -08003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soren Brinkmann76fcae32016-03-06 20:16:27 -08005 */
6
Jolly Shah259afe52019-01-09 12:37:57 -08007
Isla Mitchelle3631462017-07-14 10:46:32 +01008#include <arch_helpers.h>
Jolly Shah259afe52019-01-09 12:37:57 -08009
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <lib/bakery_lock.h>
11#include <lib/mmio.h>
Jolly Shahc2583ab2019-01-08 11:31:49 -080012
13#include <ipi.h>
Jolly Shah4c172372019-01-08 11:21:29 -080014#include <plat_ipi.h>
Jolly Shah0bfd7002019-01-08 11:10:47 -080015#include <plat_private.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <plat/common/platform.h>
17
Isla Mitchelle3631462017-07-14 10:46:32 +010018#include "pm_ipi.h"
Soren Brinkmann76fcae32016-03-06 20:16:27 -080019
Tejas Patelaf4b10e2018-02-09 02:42:59 -080020
Stefan Krsmanovicc5e9f662016-05-20 15:51:08 +020021DEFINE_BAKERY_LOCK(pm_secure_lock);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080022
Soren Brinkmann76fcae32016-03-06 20:16:27 -080023/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080024 * pm_ipi_init() - Initialize IPI peripheral for communication with
25 * remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -080026 *
Wendy Liang328105c2017-10-03 23:21:11 -070027 * @proc Pointer to the processor who is initiating request
Soren Brinkmann76fcae32016-03-06 20:16:27 -080028 * @return On success, the initialization function must return 0.
29 * Any other return value will cause the framework to ignore
30 * the service
31 *
Soren Brinkmann76fcae32016-03-06 20:16:27 -080032 * Called from pm_setup initialization function
33 */
Wendy Liang328105c2017-10-03 23:21:11 -070034int pm_ipi_init(const struct pm_proc *proc)
Soren Brinkmann76fcae32016-03-06 20:16:27 -080035{
36 bakery_lock_init(&pm_secure_lock);
Jolly Shah36c930b2019-01-07 12:51:40 -080037 ipi_mb_open(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080038
39 return 0;
40}
41
42/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080043 * pm_ipi_send_common() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -080044 * @proc Pointer to the processor who is initiating request
45 * @payload API id and call arguments to be written in IPI buffer
46 *
47 * Send an IPI request to the power controller. Caller needs to hold
48 * the 'pm_secure_lock' lock.
49 *
50 * @return Returns status, either success or error+reason
51 */
52static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc,
Tejas Patelaf4b10e2018-02-09 02:42:59 -080053 uint32_t payload[PAYLOAD_ARG_CNT],
54 uint32_t is_blocking)
Soren Brinkmann76fcae32016-03-06 20:16:27 -080055{
56 unsigned int offset = 0;
57 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -080058 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -080059 IPI_BUFFER_REQ_OFFSET;
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +053060#if ZYNQMP_IPI_CRC_CHECK
61 payload[PAYLOAD_CRC_POS] = calculate_crc(payload, IPI_W0_TO_W6_SIZE);
62#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -080063
Soren Brinkmann76fcae32016-03-06 20:16:27 -080064 /* Write payload into IPI buffer */
65 for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) {
66 mmio_write_32(buffer_base + offset, payload[i]);
67 offset += PAYLOAD_ARG_SIZE;
68 }
Tejas Patelaf4b10e2018-02-09 02:42:59 -080069
Jolly Shah9bb628d2019-01-07 12:53:32 -080070 /* Generate IPI to remote processor */
Jolly Shah36c930b2019-01-07 12:51:40 -080071 ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id,
Tejas Patelaf4b10e2018-02-09 02:42:59 -080072 is_blocking);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080073
74 return PM_RET_SUCCESS;
75}
76
77/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080078 * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor
79 * without blocking notification
Tejas Patelaf4b10e2018-02-09 02:42:59 -080080 * @proc Pointer to the processor who is initiating request
81 * @payload API id and call arguments to be written in IPI buffer
82 *
83 * Send an IPI request to the power controller.
84 *
85 * @return Returns status, either success or error+reason
86 */
87enum pm_ret_status pm_ipi_send_non_blocking(const struct pm_proc *proc,
88 uint32_t payload[PAYLOAD_ARG_CNT])
89{
90 enum pm_ret_status ret;
91
92 bakery_lock_get(&pm_secure_lock);
93
94 ret = pm_ipi_send_common(proc, payload, IPI_NON_BLOCKING);
95
96 bakery_lock_release(&pm_secure_lock);
97
98 return ret;
99}
100
101/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800102 * pm_ipi_send() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800103 * @proc Pointer to the processor who is initiating request
104 * @payload API id and call arguments to be written in IPI buffer
105 *
106 * Send an IPI request to the power controller.
107 *
108 * @return Returns status, either success or error+reason
109 */
110enum pm_ret_status pm_ipi_send(const struct pm_proc *proc,
111 uint32_t payload[PAYLOAD_ARG_CNT])
112{
113 enum pm_ret_status ret;
114
115 bakery_lock_get(&pm_secure_lock);
116
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800117 ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800118
119 bakery_lock_release(&pm_secure_lock);
120
121 return ret;
122}
123
124
125/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800126 * pm_ipi_buff_read() - Reads IPI response after remote processor has handled
127 * interrupt
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800128 * @proc Pointer to the processor who is waiting and reading response
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700129 * @value Used to return value from IPI buffer element (optional)
130 * @count Number of values to return in @value
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800131 *
132 * @return Returns status, either success or error+reason
133 */
134static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc,
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700135 unsigned int *value, size_t count)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800136{
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700137 size_t i;
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530138#if ZYNQMP_IPI_CRC_CHECK
139 size_t j;
140 unsigned int response_payload[PAYLOAD_ARG_CNT];
141#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800142 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -0800143 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800144 IPI_BUFFER_RESP_OFFSET;
145
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800146 /*
147 * Read response from IPI buffer
148 * buf-0: success or error+reason
149 * buf-1: value
150 * buf-2: unused
151 * buf-3: unused
152 */
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700153 for (i = 1; i <= count; i++) {
154 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
155 value++;
156 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530157#if ZYNQMP_IPI_CRC_CHECK
158 for (j = 0; j < PAYLOAD_ARG_CNT; j++)
159 response_payload[j] = mmio_read_32(buffer_base +
160 (j * PAYLOAD_ARG_SIZE));
161
162 if (response_payload[PAYLOAD_CRC_POS] !=
163 calculate_crc(response_payload, IPI_W0_TO_W6_SIZE))
164 NOTICE("ERROR in CRC response payload value:0x%x\n",
165 response_payload[PAYLOAD_CRC_POS]);
166#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800167
168 return mmio_read_32(buffer_base);
169}
170
171/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800172 * pm_ipi_buff_read_callb() - Reads IPI response after remote processor has
173 * handled interrupt
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700174 * @value Used to return value from IPI buffer element (optional)
175 * @count Number of values to return in @value
176 *
177 * @return Returns status, either success or error+reason
178 */
179void pm_ipi_buff_read_callb(unsigned int *value, size_t count)
180{
181 size_t i;
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530182#if ZYNQMP_IPI_CRC_CHECK
183 size_t j;
184 unsigned int response_payload[PAYLOAD_ARG_CNT];
185#endif
Jolly Shah9bb628d2019-01-07 12:53:32 -0800186 uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE +
187 IPI_BUFFER_TARGET_LOCAL_OFFSET +
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700188 IPI_BUFFER_REQ_OFFSET;
189
190 if (count > IPI_BUFFER_MAX_WORDS)
191 count = IPI_BUFFER_MAX_WORDS;
192
193 for (i = 0; i <= count; i++) {
194 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
195 value++;
196 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530197#if ZYNQMP_IPI_CRC_CHECK
198 for (j = 0; j < PAYLOAD_ARG_CNT; j++)
199 response_payload[j] = mmio_read_32(buffer_base +
200 (j * PAYLOAD_ARG_SIZE));
201
202 if (response_payload[PAYLOAD_CRC_POS] !=
203 calculate_crc(response_payload, IPI_W0_TO_W6_SIZE))
204 NOTICE("ERROR in CRC response payload value:0x%x\n",
205 response_payload[PAYLOAD_CRC_POS]);
206#endif
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700207}
208
209/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800210 * pm_ipi_send_sync() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800211 * @proc Pointer to the processor who is initiating request
212 * @payload API id and call arguments to be written in IPI buffer
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700213 * @value Used to return value from IPI buffer element (optional)
214 * @count Number of values to return in @value
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800215 *
216 * Send an IPI request to the power controller and wait for it to be handled.
217 *
218 * @return Returns status, either success or error+reason and, optionally,
219 * @value
220 */
221enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc,
222 uint32_t payload[PAYLOAD_ARG_CNT],
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700223 unsigned int *value, size_t count)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800224{
225 enum pm_ret_status ret;
226
227 bakery_lock_get(&pm_secure_lock);
228
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800229 ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800230 if (ret != PM_RET_SUCCESS)
231 goto unlock;
232
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700233 ret = pm_ipi_buff_read(proc, value, count);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800234
235unlock:
236 bakery_lock_release(&pm_secure_lock);
237
238 return ret;
239}
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700240
Wendy Liang328105c2017-10-03 23:21:11 -0700241void pm_ipi_irq_enable(const struct pm_proc *proc)
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700242{
Jolly Shah36c930b2019-01-07 12:51:40 -0800243 ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700244}
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700245
Wendy Liang328105c2017-10-03 23:21:11 -0700246void pm_ipi_irq_clear(const struct pm_proc *proc)
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700247{
Jolly Shah36c930b2019-01-07 12:51:40 -0800248 ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700249}
Jolly Shah259afe52019-01-09 12:37:57 -0800250
251uint32_t pm_ipi_irq_status(const struct pm_proc *proc)
252{
253 int ret;
254
255 ret = ipi_mb_enquire_status(proc->ipi->local_ipi_id,
256 proc->ipi->remote_ipi_id);
257 if (ret & IPI_MB_STATUS_RECV_PENDING)
258 return 1;
259 else
260 return 0;
261}
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530262
263#if ZYNQMP_IPI_CRC_CHECK
264uint32_t calculate_crc(uint32_t *payload, uint32_t bufsize)
265{
266 uint32_t crcinit = CRC_INIT_VALUE;
267 uint32_t order = CRC_ORDER;
268 uint32_t polynom = CRC_POLYNOM;
269 uint32_t i, j, c, bit, datain, crcmask, crchighbit;
270 uint32_t crc = crcinit;
271
272 crcmask = ((uint32_t)((1U << (order - 1U)) - 1U) << 1U) | 1U;
273 crchighbit = (uint32_t)(1U << (order - 1U));
274
275 for (i = 0U; i < bufsize; i++) {
276 datain = mmio_read_8((unsigned long)payload + i);
277 c = datain;
278 j = 0x80U;
279 while (j != 0U) {
280 bit = crc & crchighbit;
281 crc <<= 1U;
282 if (0U != (c & j))
283 bit ^= crchighbit;
284 if (bit != 0U)
285 crc ^= polynom;
286 j >>= 1U;
287 }
288 crc &= crcmask;
289 }
290 return crc;
291}
292#endif