blob: a0403cf113fad0a181365166a8e4f0b47e15d9ce [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.
Rajan Vaja566b2f92022-08-31 12:54:38 +02003 * Copyright (c) 2019-2022, Xilinx, Inc. All rights reserved.
4 * Copyright (c) 2022, Advanced Micro Devices, Inc. All rights reserved.
Soren Brinkmann76fcae32016-03-06 20:16:27 -08005 *
dp-armfa3cf0b2017-05-03 09:38:09 +01006 * SPDX-License-Identifier: BSD-3-Clause
Soren Brinkmann76fcae32016-03-06 20:16:27 -08007 */
8
Jolly Shah259afe52019-01-09 12:37:57 -08009
Isla Mitchelle3631462017-07-14 10:46:32 +010010#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <lib/bakery_lock.h>
12#include <lib/mmio.h>
Jolly Shahc2583ab2019-01-08 11:31:49 -080013#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
Rajan Vaja566b2f92022-08-31 12:54:38 +020018#include "pm_defs.h"
Isla Mitchelle3631462017-07-14 10:46:32 +010019#include "pm_ipi.h"
Soren Brinkmann76fcae32016-03-06 20:16:27 -080020
HariBabu Gattem9f9db612022-09-15 22:35:11 -070021#define ERROR_CODE_MASK (0xFFFFU)
Ravi Patel65123712020-11-23 04:19:08 -080022
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 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +053036int32_t 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{
58 unsigned int offset = 0;
59 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -080060 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -080061 IPI_BUFFER_REQ_OFFSET;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -070062#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +053063 payload[PAYLOAD_CRC_POS] = calculate_crc(payload, IPI_W0_TO_W6_SIZE);
64#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -080065
Soren Brinkmann76fcae32016-03-06 20:16:27 -080066 /* 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 }
Tejas Patelaf4b10e2018-02-09 02:42:59 -080071
Jolly Shah9bb628d2019-01-07 12:53:32 -080072 /* Generate IPI to remote processor */
Venkatesh Yadav Abbarapuefa1d322021-08-04 21:33:15 -060073 ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id,
Tejas Patelaf4b10e2018-02-09 02:42:59 -080074 is_blocking);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080075
Venkatesh Yadav Abbarapuefa1d322021-08-04 21:33:15 -060076 return PM_RET_SUCCESS;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080077}
78
79/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080080 * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor
81 * without blocking notification
Tejas Patelaf4b10e2018-02-09 02:42:59 -080082 * @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 */
89enum 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/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800104 * pm_ipi_send() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800105 * @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 */
112enum 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
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800119 ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800120
121 bakery_lock_release(&pm_secure_lock);
122
123 return ret;
124}
125
126
127/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800128 * pm_ipi_buff_read() - Reads IPI response after remote processor has handled
129 * interrupt
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800130 * @proc Pointer to the processor who is waiting and reading response
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700131 * @value Used to return value from IPI buffer element (optional)
132 * @count Number of values to return in @value
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800133 *
134 * @return Returns status, either success or error+reason
135 */
136static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc,
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530137 uint32_t *value, size_t count)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800138{
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700139 size_t i;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700140#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530141 size_t j;
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530142 uint32_t response_payload[PAYLOAD_ARG_CNT];
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530143#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800144 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -0800145 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800146 IPI_BUFFER_RESP_OFFSET;
147
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800148 /*
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 */
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700155 for (i = 1; i <= count; i++) {
156 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
157 value++;
158 }
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700159#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530160 for (j = 0; j < PAYLOAD_ARG_CNT; j++) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530161 response_payload[j] = mmio_read_32(buffer_base +
162 (j * PAYLOAD_ARG_SIZE));
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530163 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530164
165 if (response_payload[PAYLOAD_CRC_POS] !=
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530166 calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530167 NOTICE("ERROR in CRC response payload value:0x%x\n",
168 response_payload[PAYLOAD_CRC_POS]);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530169 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530170#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800171
172 return mmio_read_32(buffer_base);
173}
174
175/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800176 * pm_ipi_buff_read_callb() - Reads IPI response after remote processor has
177 * handled interrupt
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700178 * @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 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530183void pm_ipi_buff_read_callb(uint32_t *value, size_t count)
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700184{
185 size_t i;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700186#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530187 size_t j;
188 unsigned int response_payload[PAYLOAD_ARG_CNT];
189#endif
Jolly Shah9bb628d2019-01-07 12:53:32 -0800190 uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE +
191 IPI_BUFFER_TARGET_LOCAL_OFFSET +
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700192 IPI_BUFFER_REQ_OFFSET;
193
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530194 if (count > IPI_BUFFER_MAX_WORDS) {
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700195 count = IPI_BUFFER_MAX_WORDS;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530196 }
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700197
198 for (i = 0; i <= count; i++) {
199 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
200 value++;
201 }
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700202#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530203 for (j = 0; j < PAYLOAD_ARG_CNT; j++) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530204 response_payload[j] = mmio_read_32(buffer_base +
205 (j * PAYLOAD_ARG_SIZE));
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530206 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530207
208 if (response_payload[PAYLOAD_CRC_POS] !=
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530209 calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530210 NOTICE("ERROR in CRC response payload value:0x%x\n",
211 response_payload[PAYLOAD_CRC_POS]);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530212 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530213#endif
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700214}
215
216/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800217 * pm_ipi_send_sync() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800218 * @proc Pointer to the processor who is initiating request
219 * @payload API id and call arguments to be written in IPI buffer
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700220 * @value Used to return value from IPI buffer element (optional)
221 * @count Number of values to return in @value
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800222 *
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 */
228enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc,
229 uint32_t payload[PAYLOAD_ARG_CNT],
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530230 uint32_t *value, size_t count)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800231{
232 enum pm_ret_status ret;
233
234 bakery_lock_get(&pm_secure_lock);
235
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800236 ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530237 if (ret != PM_RET_SUCCESS) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800238 goto unlock;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530239 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800240
Ravi Patel65123712020-11-23 04:19:08 -0800241 ret = ERROR_CODE_MASK & (pm_ipi_buff_read(proc, value, count));
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800242
243unlock:
244 bakery_lock_release(&pm_secure_lock);
245
246 return ret;
247}
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700248
Wendy Liang328105c2017-10-03 23:21:11 -0700249void pm_ipi_irq_enable(const struct pm_proc *proc)
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700250{
Jolly Shah36c930b2019-01-07 12:51:40 -0800251 ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700252}
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700253
Wendy Liang328105c2017-10-03 23:21:11 -0700254void pm_ipi_irq_clear(const struct pm_proc *proc)
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700255{
Jolly Shah36c930b2019-01-07 12:51:40 -0800256 ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700257}
Jolly Shah259afe52019-01-09 12:37:57 -0800258
259uint32_t pm_ipi_irq_status(const struct pm_proc *proc)
260{
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530261 int32_t ret;
Jolly Shah259afe52019-01-09 12:37:57 -0800262
263 ret = ipi_mb_enquire_status(proc->ipi->local_ipi_id,
264 proc->ipi->remote_ipi_id);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530265 if (ret & IPI_MB_STATUS_RECV_PENDING) {
Jolly Shah259afe52019-01-09 12:37:57 -0800266 return 1;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530267 } else {
Jolly Shah259afe52019-01-09 12:37:57 -0800268 return 0;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530269 }
Jolly Shah259afe52019-01-09 12:37:57 -0800270}
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530271
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700272#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu28b77592022-04-11 09:25:44 +0530273uint32_t calculate_crc(uint32_t payload[PAYLOAD_ARG_CNT], uint32_t bufsize)
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530274{
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