blob: 513d6be0a67eca350737ce36aa6e1cd62690a46d [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)
HariBabu Gattemaa811712022-10-07 00:07:49 -070022#define PM_OFFSET (0U)
Ravi Patel65123712020-11-23 04:19:08 -080023
Stefan Krsmanovicc5e9f662016-05-20 15:51:08 +020024DEFINE_BAKERY_LOCK(pm_secure_lock);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080025
Soren Brinkmann76fcae32016-03-06 20:16:27 -080026/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080027 * pm_ipi_init() - Initialize IPI peripheral for communication with
28 * remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -080029 *
Wendy Liang328105c2017-10-03 23:21:11 -070030 * @proc Pointer to the processor who is initiating request
Soren Brinkmann76fcae32016-03-06 20:16:27 -080031 * @return On success, the initialization function must return 0.
32 * Any other return value will cause the framework to ignore
33 * the service
34 *
Soren Brinkmann76fcae32016-03-06 20:16:27 -080035 * Called from pm_setup initialization function
36 */
HariBabu Gattemaa811712022-10-07 00:07:49 -070037void pm_ipi_init(const struct pm_proc *proc)
Soren Brinkmann76fcae32016-03-06 20:16:27 -080038{
39 bakery_lock_init(&pm_secure_lock);
Jolly Shah36c930b2019-01-07 12:51:40 -080040 ipi_mb_open(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080041}
42
43/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080044 * pm_ipi_send_common() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -080045 * @proc Pointer to the processor who is initiating request
46 * @payload API id and call arguments to be written in IPI buffer
47 *
48 * Send an IPI request to the power controller. Caller needs to hold
49 * the 'pm_secure_lock' lock.
50 *
51 * @return Returns status, either success or error+reason
52 */
53static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc,
Tejas Patelaf4b10e2018-02-09 02:42:59 -080054 uint32_t payload[PAYLOAD_ARG_CNT],
55 uint32_t is_blocking)
Soren Brinkmann76fcae32016-03-06 20:16:27 -080056{
HariBabu Gattemaa811712022-10-07 00:07:49 -070057 uint32_t offset = PM_OFFSET;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080058 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -080059 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -080060 IPI_BUFFER_REQ_OFFSET;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -070061#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +053062 payload[PAYLOAD_CRC_POS] = calculate_crc(payload, IPI_W0_TO_W6_SIZE);
63#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -080064
Soren Brinkmann76fcae32016-03-06 20:16:27 -080065 /* Write payload into IPI buffer */
66 for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) {
67 mmio_write_32(buffer_base + offset, payload[i]);
68 offset += PAYLOAD_ARG_SIZE;
69 }
Tejas Patelaf4b10e2018-02-09 02:42:59 -080070
Jolly Shah9bb628d2019-01-07 12:53:32 -080071 /* Generate IPI to remote processor */
Venkatesh Yadav Abbarapuefa1d322021-08-04 21:33:15 -060072 ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id,
Tejas Patelaf4b10e2018-02-09 02:42:59 -080073 is_blocking);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080074
Venkatesh Yadav Abbarapuefa1d322021-08-04 21:33:15 -060075 return PM_RET_SUCCESS;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080076}
77
78/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080079 * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor
80 * without blocking notification
Tejas Patelaf4b10e2018-02-09 02:42:59 -080081 * @proc Pointer to the processor who is initiating request
82 * @payload API id and call arguments to be written in IPI buffer
83 *
84 * Send an IPI request to the power controller.
85 *
86 * @return Returns status, either success or error+reason
87 */
88enum pm_ret_status pm_ipi_send_non_blocking(const struct pm_proc *proc,
89 uint32_t payload[PAYLOAD_ARG_CNT])
90{
91 enum pm_ret_status ret;
92
93 bakery_lock_get(&pm_secure_lock);
94
95 ret = pm_ipi_send_common(proc, payload, IPI_NON_BLOCKING);
96
97 bakery_lock_release(&pm_secure_lock);
98
99 return ret;
100}
101
102/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800103 * pm_ipi_send() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800104 * @proc Pointer to the processor who is initiating request
105 * @payload API id and call arguments to be written in IPI buffer
106 *
107 * Send an IPI request to the power controller.
108 *
109 * @return Returns status, either success or error+reason
110 */
111enum pm_ret_status pm_ipi_send(const struct pm_proc *proc,
112 uint32_t payload[PAYLOAD_ARG_CNT])
113{
114 enum pm_ret_status ret;
115
116 bakery_lock_get(&pm_secure_lock);
117
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800118 ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800119
120 bakery_lock_release(&pm_secure_lock);
121
122 return ret;
123}
124
125
126/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800127 * pm_ipi_buff_read() - Reads IPI response after remote processor has handled
128 * interrupt
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800129 * @proc Pointer to the processor who is waiting and reading response
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700130 * @value Used to return value from IPI buffer element (optional)
131 * @count Number of values to return in @value
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800132 *
133 * @return Returns status, either success or error+reason
134 */
135static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc,
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530136 uint32_t *value, size_t count)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800137{
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700138 size_t i;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700139#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530140 size_t j;
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530141 uint32_t response_payload[PAYLOAD_ARG_CNT];
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530142#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800143 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -0800144 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800145 IPI_BUFFER_RESP_OFFSET;
146
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800147 /*
148 * Read response from IPI buffer
149 * buf-0: success or error+reason
150 * buf-1: value
151 * buf-2: unused
152 * buf-3: unused
153 */
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700154 for (i = 1; i <= count; i++) {
155 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
156 value++;
157 }
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700158#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530159 for (j = 0; j < PAYLOAD_ARG_CNT; j++) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530160 response_payload[j] = mmio_read_32(buffer_base +
161 (j * PAYLOAD_ARG_SIZE));
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530162 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530163
164 if (response_payload[PAYLOAD_CRC_POS] !=
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530165 calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530166 NOTICE("ERROR in CRC response payload value:0x%x\n",
167 response_payload[PAYLOAD_CRC_POS]);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530168 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530169#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800170
171 return mmio_read_32(buffer_base);
172}
173
174/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800175 * pm_ipi_buff_read_callb() - Reads IPI response after remote processor has
176 * handled interrupt
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700177 * @value Used to return value from IPI buffer element (optional)
178 * @count Number of values to return in @value
179 *
180 * @return Returns status, either success or error+reason
181 */
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530182void pm_ipi_buff_read_callb(uint32_t *value, size_t count)
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700183{
184 size_t i;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700185#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530186 size_t j;
HariBabu Gattemaa811712022-10-07 00:07:49 -0700187 unsigned int response_payload[PAYLOAD_ARG_CNT] = {0};
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530188#endif
Jolly Shah9bb628d2019-01-07 12:53:32 -0800189 uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE +
190 IPI_BUFFER_TARGET_LOCAL_OFFSET +
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700191 IPI_BUFFER_REQ_OFFSET;
192
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530193 if (count > IPI_BUFFER_MAX_WORDS) {
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700194 count = IPI_BUFFER_MAX_WORDS;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530195 }
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700196
197 for (i = 0; i <= count; i++) {
198 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
199 value++;
200 }
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700201#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530202 for (j = 0; j < PAYLOAD_ARG_CNT; j++) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530203 response_payload[j] = mmio_read_32(buffer_base +
204 (j * PAYLOAD_ARG_SIZE));
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530205 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530206
207 if (response_payload[PAYLOAD_CRC_POS] !=
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530208 calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530209 NOTICE("ERROR in CRC response payload value:0x%x\n",
210 response_payload[PAYLOAD_CRC_POS]);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530211 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530212#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],
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530229 uint32_t *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);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530236 if (ret != PM_RET_SUCCESS) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800237 goto unlock;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530238 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800239
Ravi Patel65123712020-11-23 04:19:08 -0800240 ret = ERROR_CODE_MASK & (pm_ipi_buff_read(proc, value, count));
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800241
242unlock:
243 bakery_lock_release(&pm_secure_lock);
244
245 return ret;
246}
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700247
Wendy Liang328105c2017-10-03 23:21:11 -0700248void pm_ipi_irq_enable(const struct pm_proc *proc)
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700249{
Jolly Shah36c930b2019-01-07 12:51:40 -0800250 ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700251}
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700252
Wendy Liang328105c2017-10-03 23:21:11 -0700253void pm_ipi_irq_clear(const struct pm_proc *proc)
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700254{
Jolly Shah36c930b2019-01-07 12:51:40 -0800255 ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700256}
Jolly Shah259afe52019-01-09 12:37:57 -0800257
258uint32_t pm_ipi_irq_status(const struct pm_proc *proc)
259{
Venkatesh Yadav Abbarapue7c45382022-05-19 14:49:49 +0530260 int32_t ret;
Jolly Shah259afe52019-01-09 12:37:57 -0800261
262 ret = ipi_mb_enquire_status(proc->ipi->local_ipi_id,
263 proc->ipi->remote_ipi_id);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530264 if (ret & IPI_MB_STATUS_RECV_PENDING) {
Jolly Shah259afe52019-01-09 12:37:57 -0800265 return 1;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530266 } else {
Jolly Shah259afe52019-01-09 12:37:57 -0800267 return 0;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530268 }
Jolly Shah259afe52019-01-09 12:37:57 -0800269}
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530270
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700271#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu28b77592022-04-11 09:25:44 +0530272uint32_t calculate_crc(uint32_t payload[PAYLOAD_ARG_CNT], uint32_t bufsize)
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530273{
274 uint32_t crcinit = CRC_INIT_VALUE;
275 uint32_t order = CRC_ORDER;
276 uint32_t polynom = CRC_POLYNOM;
277 uint32_t i, j, c, bit, datain, crcmask, crchighbit;
278 uint32_t crc = crcinit;
279
280 crcmask = ((uint32_t)((1U << (order - 1U)) - 1U) << 1U) | 1U;
281 crchighbit = (uint32_t)(1U << (order - 1U));
282
283 for (i = 0U; i < bufsize; i++) {
284 datain = mmio_read_8((unsigned long)payload + i);
285 c = datain;
286 j = 0x80U;
287 while (j != 0U) {
288 bit = crc & crchighbit;
289 crc <<= 1U;
290 if (0U != (c & j))
291 bit ^= crchighbit;
292 if (bit != 0U)
293 crc ^= polynom;
294 j >>= 1U;
295 }
296 crc &= crcmask;
297 }
298 return crc;
299}
300#endif