blob: 7b5bd0280530b521d3c4e9c84d56c3146c66d6ee [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
Ravi Patel65123712020-11-23 04:19:08 -080021#define ERROR_CODE_MASK 0xFFFFU
22
Stefan Krsmanovicc5e9f662016-05-20 15:51:08 +020023DEFINE_BAKERY_LOCK(pm_secure_lock);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080024
Soren Brinkmann76fcae32016-03-06 20:16:27 -080025/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080026 * pm_ipi_init() - Initialize IPI peripheral for communication with
27 * remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -080028 *
Wendy Liang328105c2017-10-03 23:21:11 -070029 * @proc Pointer to the processor who is initiating request
Soren Brinkmann76fcae32016-03-06 20:16:27 -080030 * @return On success, the initialization function must return 0.
31 * Any other return value will cause the framework to ignore
32 * the service
33 *
Soren Brinkmann76fcae32016-03-06 20:16:27 -080034 * Called from pm_setup initialization function
35 */
Wendy Liang328105c2017-10-03 23:21:11 -070036int pm_ipi_init(const struct pm_proc *proc)
Soren Brinkmann76fcae32016-03-06 20:16:27 -080037{
38 bakery_lock_init(&pm_secure_lock);
Jolly Shah36c930b2019-01-07 12:51:40 -080039 ipi_mb_open(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080040
41 return 0;
42}
43
44/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080045 * pm_ipi_send_common() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -080046 * @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 */
54static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc,
Tejas Patelaf4b10e2018-02-09 02:42:59 -080055 uint32_t payload[PAYLOAD_ARG_CNT],
56 uint32_t is_blocking)
Soren Brinkmann76fcae32016-03-06 20:16:27 -080057{
Tejas Patelc1cda9c2021-02-25 02:37:09 -080058 int status;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080059 unsigned int offset = 0;
60 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -080061 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -080062 IPI_BUFFER_REQ_OFFSET;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -070063#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +053064 payload[PAYLOAD_CRC_POS] = calculate_crc(payload, IPI_W0_TO_W6_SIZE);
65#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -080066
Soren Brinkmann76fcae32016-03-06 20:16:27 -080067 /* Write payload into IPI buffer */
68 for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) {
69 mmio_write_32(buffer_base + offset, payload[i]);
70 offset += PAYLOAD_ARG_SIZE;
71 }
Tejas Patelaf4b10e2018-02-09 02:42:59 -080072
Jolly Shah9bb628d2019-01-07 12:53:32 -080073 /* Generate IPI to remote processor */
Tejas Patelc1cda9c2021-02-25 02:37:09 -080074 status = ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id,
Tejas Patelaf4b10e2018-02-09 02:42:59 -080075 is_blocking);
Tejas Patelc1cda9c2021-02-25 02:37:09 -080076 if (status == 0) {
77 return PM_RET_SUCCESS;
78 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -080079
Tejas Patelc1cda9c2021-02-25 02:37:09 -080080 return PM_RET_ERROR_TIMEOUT;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080081}
82
83/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080084 * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor
85 * without blocking notification
Tejas Patelaf4b10e2018-02-09 02:42:59 -080086 * @proc Pointer to the processor who is initiating request
87 * @payload API id and call arguments to be written in IPI buffer
88 *
89 * Send an IPI request to the power controller.
90 *
91 * @return Returns status, either success or error+reason
92 */
93enum pm_ret_status pm_ipi_send_non_blocking(const struct pm_proc *proc,
94 uint32_t payload[PAYLOAD_ARG_CNT])
95{
96 enum pm_ret_status ret;
97
98 bakery_lock_get(&pm_secure_lock);
99
100 ret = pm_ipi_send_common(proc, payload, IPI_NON_BLOCKING);
101
102 bakery_lock_release(&pm_secure_lock);
103
104 return ret;
105}
106
107/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800108 * pm_ipi_send() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800109 * @proc Pointer to the processor who is initiating request
110 * @payload API id and call arguments to be written in IPI buffer
111 *
112 * Send an IPI request to the power controller.
113 *
114 * @return Returns status, either success or error+reason
115 */
116enum pm_ret_status pm_ipi_send(const struct pm_proc *proc,
117 uint32_t payload[PAYLOAD_ARG_CNT])
118{
119 enum pm_ret_status ret;
120
121 bakery_lock_get(&pm_secure_lock);
122
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800123 ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800124
125 bakery_lock_release(&pm_secure_lock);
126
127 return ret;
128}
129
130
131/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800132 * pm_ipi_buff_read() - Reads IPI response after remote processor has handled
133 * interrupt
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800134 * @proc Pointer to the processor who is waiting and reading response
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700135 * @value Used to return value from IPI buffer element (optional)
136 * @count Number of values to return in @value
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800137 *
138 * @return Returns status, either success or error+reason
139 */
140static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc,
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700141 unsigned int *value, size_t count)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800142{
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700143 size_t i;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700144#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530145 size_t j;
146 unsigned int response_payload[PAYLOAD_ARG_CNT];
147#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800148 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -0800149 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800150 IPI_BUFFER_RESP_OFFSET;
151
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800152 /*
153 * Read response from IPI buffer
154 * buf-0: success or error+reason
155 * buf-1: value
156 * buf-2: unused
157 * buf-3: unused
158 */
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700159 for (i = 1; i <= count; i++) {
160 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
161 value++;
162 }
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700163#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530164 for (j = 0; j < PAYLOAD_ARG_CNT; j++)
165 response_payload[j] = mmio_read_32(buffer_base +
166 (j * PAYLOAD_ARG_SIZE));
167
168 if (response_payload[PAYLOAD_CRC_POS] !=
169 calculate_crc(response_payload, IPI_W0_TO_W6_SIZE))
170 NOTICE("ERROR in CRC response payload value:0x%x\n",
171 response_payload[PAYLOAD_CRC_POS]);
172#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800173
174 return mmio_read_32(buffer_base);
175}
176
177/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800178 * pm_ipi_buff_read_callb() - Reads IPI response after remote processor has
179 * handled interrupt
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700180 * @value Used to return value from IPI buffer element (optional)
181 * @count Number of values to return in @value
182 *
183 * @return Returns status, either success or error+reason
184 */
185void pm_ipi_buff_read_callb(unsigned int *value, size_t count)
186{
187 size_t i;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700188#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530189 size_t j;
190 unsigned int response_payload[PAYLOAD_ARG_CNT];
191#endif
Jolly Shah9bb628d2019-01-07 12:53:32 -0800192 uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE +
193 IPI_BUFFER_TARGET_LOCAL_OFFSET +
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700194 IPI_BUFFER_REQ_OFFSET;
195
196 if (count > IPI_BUFFER_MAX_WORDS)
197 count = IPI_BUFFER_MAX_WORDS;
198
199 for (i = 0; i <= count; i++) {
200 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
201 value++;
202 }
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700203#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530204 for (j = 0; j < PAYLOAD_ARG_CNT; j++)
205 response_payload[j] = mmio_read_32(buffer_base +
206 (j * PAYLOAD_ARG_SIZE));
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#endif
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700213}
214
215/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800216 * pm_ipi_send_sync() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800217 * @proc Pointer to the processor who is initiating request
218 * @payload API id and call arguments to be written in IPI buffer
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700219 * @value Used to return value from IPI buffer element (optional)
220 * @count Number of values to return in @value
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800221 *
222 * Send an IPI request to the power controller and wait for it to be handled.
223 *
224 * @return Returns status, either success or error+reason and, optionally,
225 * @value
226 */
227enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc,
228 uint32_t payload[PAYLOAD_ARG_CNT],
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700229 unsigned int *value, size_t count)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800230{
231 enum pm_ret_status ret;
232
233 bakery_lock_get(&pm_secure_lock);
234
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800235 ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800236 if (ret != PM_RET_SUCCESS)
237 goto unlock;
238
Ravi Patel65123712020-11-23 04:19:08 -0800239 ret = ERROR_CODE_MASK & (pm_ipi_buff_read(proc, value, count));
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800240
241unlock:
242 bakery_lock_release(&pm_secure_lock);
243
244 return ret;
245}
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700246
Wendy Liang328105c2017-10-03 23:21:11 -0700247void pm_ipi_irq_enable(const struct pm_proc *proc)
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700248{
Jolly Shah36c930b2019-01-07 12:51:40 -0800249 ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700250}
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700251
Wendy Liang328105c2017-10-03 23:21:11 -0700252void pm_ipi_irq_clear(const struct pm_proc *proc)
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700253{
Jolly Shah36c930b2019-01-07 12:51:40 -0800254 ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700255}
Jolly Shah259afe52019-01-09 12:37:57 -0800256
257uint32_t pm_ipi_irq_status(const struct pm_proc *proc)
258{
259 int ret;
260
261 ret = ipi_mb_enquire_status(proc->ipi->local_ipi_id,
262 proc->ipi->remote_ipi_id);
263 if (ret & IPI_MB_STATUS_RECV_PENDING)
264 return 1;
265 else
266 return 0;
267}
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530268
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700269#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530270uint32_t calculate_crc(uint32_t *payload, uint32_t bufsize)
271{
272 uint32_t crcinit = CRC_INIT_VALUE;
273 uint32_t order = CRC_ORDER;
274 uint32_t polynom = CRC_POLYNOM;
275 uint32_t i, j, c, bit, datain, crcmask, crchighbit;
276 uint32_t crc = crcinit;
277
278 crcmask = ((uint32_t)((1U << (order - 1U)) - 1U) << 1U) | 1U;
279 crchighbit = (uint32_t)(1U << (order - 1U));
280
281 for (i = 0U; i < bufsize; i++) {
282 datain = mmio_read_8((unsigned long)payload + i);
283 c = datain;
284 j = 0x80U;
285 while (j != 0U) {
286 bit = crc & crchighbit;
287 crc <<= 1U;
288 if (0U != (c & j))
289 bit ^= crchighbit;
290 if (bit != 0U)
291 crc ^= polynom;
292 j >>= 1U;
293 }
294 crc &= crcmask;
295 }
296 return crc;
297}
298#endif