blob: 1d1ba852f71c7db56d8e4e9fc31851d9534ea0eb [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>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009#include <lib/bakery_lock.h>
10#include <lib/mmio.h>
Jolly Shahc2583ab2019-01-08 11:31:49 -080011#include <ipi.h>
Jolly Shah4c172372019-01-08 11:21:29 -080012#include <plat_ipi.h>
Jolly Shah0bfd7002019-01-08 11:10:47 -080013#include <plat_private.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <plat/common/platform.h>
15
Isla Mitchelle3631462017-07-14 10:46:32 +010016#include "pm_ipi.h"
Soren Brinkmann76fcae32016-03-06 20:16:27 -080017
Ravi Patel65123712020-11-23 04:19:08 -080018#define ERROR_CODE_MASK 0xFFFFU
19
Stefan Krsmanovicc5e9f662016-05-20 15:51:08 +020020DEFINE_BAKERY_LOCK(pm_secure_lock);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080021
Soren Brinkmann76fcae32016-03-06 20:16:27 -080022/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080023 * pm_ipi_init() - Initialize IPI peripheral for communication with
24 * remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -080025 *
Wendy Liang328105c2017-10-03 23:21:11 -070026 * @proc Pointer to the processor who is initiating request
Soren Brinkmann76fcae32016-03-06 20:16:27 -080027 * @return On success, the initialization function must return 0.
28 * Any other return value will cause the framework to ignore
29 * the service
30 *
Soren Brinkmann76fcae32016-03-06 20:16:27 -080031 * Called from pm_setup initialization function
32 */
Wendy Liang328105c2017-10-03 23:21:11 -070033int pm_ipi_init(const struct pm_proc *proc)
Soren Brinkmann76fcae32016-03-06 20:16:27 -080034{
35 bakery_lock_init(&pm_secure_lock);
Jolly Shah36c930b2019-01-07 12:51:40 -080036 ipi_mb_open(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080037
38 return 0;
39}
40
41/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080042 * pm_ipi_send_common() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -080043 * @proc Pointer to the processor who is initiating request
44 * @payload API id and call arguments to be written in IPI buffer
45 *
46 * Send an IPI request to the power controller. Caller needs to hold
47 * the 'pm_secure_lock' lock.
48 *
49 * @return Returns status, either success or error+reason
50 */
51static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc,
Tejas Patelaf4b10e2018-02-09 02:42:59 -080052 uint32_t payload[PAYLOAD_ARG_CNT],
53 uint32_t is_blocking)
Soren Brinkmann76fcae32016-03-06 20:16:27 -080054{
55 unsigned int offset = 0;
56 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -080057 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -080058 IPI_BUFFER_REQ_OFFSET;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -070059#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +053060 payload[PAYLOAD_CRC_POS] = calculate_crc(payload, IPI_W0_TO_W6_SIZE);
61#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -080062
Soren Brinkmann76fcae32016-03-06 20:16:27 -080063 /* Write payload into IPI buffer */
64 for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) {
65 mmio_write_32(buffer_base + offset, payload[i]);
66 offset += PAYLOAD_ARG_SIZE;
67 }
Tejas Patelaf4b10e2018-02-09 02:42:59 -080068
Jolly Shah9bb628d2019-01-07 12:53:32 -080069 /* Generate IPI to remote processor */
Venkatesh Yadav Abbarapuefa1d322021-08-04 21:33:15 -060070 ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id,
Tejas Patelaf4b10e2018-02-09 02:42:59 -080071 is_blocking);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080072
Venkatesh Yadav Abbarapuefa1d322021-08-04 21:33:15 -060073 return PM_RET_SUCCESS;
Soren Brinkmann76fcae32016-03-06 20:16:27 -080074}
75
76/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080077 * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor
78 * without blocking notification
Tejas Patelaf4b10e2018-02-09 02:42:59 -080079 * @proc Pointer to the processor who is initiating request
80 * @payload API id and call arguments to be written in IPI buffer
81 *
82 * Send an IPI request to the power controller.
83 *
84 * @return Returns status, either success or error+reason
85 */
86enum pm_ret_status pm_ipi_send_non_blocking(const struct pm_proc *proc,
87 uint32_t payload[PAYLOAD_ARG_CNT])
88{
89 enum pm_ret_status ret;
90
91 bakery_lock_get(&pm_secure_lock);
92
93 ret = pm_ipi_send_common(proc, payload, IPI_NON_BLOCKING);
94
95 bakery_lock_release(&pm_secure_lock);
96
97 return ret;
98}
99
100/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800101 * pm_ipi_send() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800102 * @proc Pointer to the processor who is initiating request
103 * @payload API id and call arguments to be written in IPI buffer
104 *
105 * Send an IPI request to the power controller.
106 *
107 * @return Returns status, either success or error+reason
108 */
109enum pm_ret_status pm_ipi_send(const struct pm_proc *proc,
110 uint32_t payload[PAYLOAD_ARG_CNT])
111{
112 enum pm_ret_status ret;
113
114 bakery_lock_get(&pm_secure_lock);
115
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800116 ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800117
118 bakery_lock_release(&pm_secure_lock);
119
120 return ret;
121}
122
123
124/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800125 * pm_ipi_buff_read() - Reads IPI response after remote processor has handled
126 * interrupt
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800127 * @proc Pointer to the processor who is waiting and reading response
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700128 * @value Used to return value from IPI buffer element (optional)
129 * @count Number of values to return in @value
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800130 *
131 * @return Returns status, either success or error+reason
132 */
133static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc,
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700134 unsigned int *value, size_t count)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800135{
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700136 size_t i;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700137#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530138 size_t j;
139 unsigned int response_payload[PAYLOAD_ARG_CNT];
140#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800141 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -0800142 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800143 IPI_BUFFER_RESP_OFFSET;
144
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800145 /*
146 * Read response from IPI buffer
147 * buf-0: success or error+reason
148 * buf-1: value
149 * buf-2: unused
150 * buf-3: unused
151 */
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700152 for (i = 1; i <= count; i++) {
153 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
154 value++;
155 }
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700156#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530157 for (j = 0; j < PAYLOAD_ARG_CNT; j++) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530158 response_payload[j] = mmio_read_32(buffer_base +
159 (j * PAYLOAD_ARG_SIZE));
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530160 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530161
162 if (response_payload[PAYLOAD_CRC_POS] !=
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530163 calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530164 NOTICE("ERROR in CRC response payload value:0x%x\n",
165 response_payload[PAYLOAD_CRC_POS]);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530166 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530167#endif
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800168
169 return mmio_read_32(buffer_base);
170}
171
172/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800173 * pm_ipi_buff_read_callb() - Reads IPI response after remote processor has
174 * handled interrupt
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700175 * @value Used to return value from IPI buffer element (optional)
176 * @count Number of values to return in @value
177 *
178 * @return Returns status, either success or error+reason
179 */
180void pm_ipi_buff_read_callb(unsigned int *value, size_t count)
181{
182 size_t i;
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700183#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530184 size_t j;
185 unsigned int response_payload[PAYLOAD_ARG_CNT];
186#endif
Jolly Shah9bb628d2019-01-07 12:53:32 -0800187 uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE +
188 IPI_BUFFER_TARGET_LOCAL_OFFSET +
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700189 IPI_BUFFER_REQ_OFFSET;
190
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530191 if (count > IPI_BUFFER_MAX_WORDS) {
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700192 count = IPI_BUFFER_MAX_WORDS;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530193 }
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700194
195 for (i = 0; i <= count; i++) {
196 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
197 value++;
198 }
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700199#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530200 for (j = 0; j < PAYLOAD_ARG_CNT; j++) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530201 response_payload[j] = mmio_read_32(buffer_base +
202 (j * PAYLOAD_ARG_SIZE));
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530203 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530204
205 if (response_payload[PAYLOAD_CRC_POS] !=
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530206 calculate_crc(response_payload, IPI_W0_TO_W6_SIZE)) {
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530207 NOTICE("ERROR in CRC response payload value:0x%x\n",
208 response_payload[PAYLOAD_CRC_POS]);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530209 }
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530210#endif
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700211}
212
213/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800214 * pm_ipi_send_sync() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800215 * @proc Pointer to the processor who is initiating request
216 * @payload API id and call arguments to be written in IPI buffer
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700217 * @value Used to return value from IPI buffer element (optional)
218 * @count Number of values to return in @value
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800219 *
220 * Send an IPI request to the power controller and wait for it to be handled.
221 *
222 * @return Returns status, either success or error+reason and, optionally,
223 * @value
224 */
225enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc,
226 uint32_t payload[PAYLOAD_ARG_CNT],
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700227 unsigned int *value, size_t count)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800228{
229 enum pm_ret_status ret;
230
231 bakery_lock_get(&pm_secure_lock);
232
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800233 ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530234 if (ret != PM_RET_SUCCESS) {
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800235 goto unlock;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530236 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800237
Ravi Patel65123712020-11-23 04:19:08 -0800238 ret = ERROR_CODE_MASK & (pm_ipi_buff_read(proc, value, count));
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800239
240unlock:
241 bakery_lock_release(&pm_secure_lock);
242
243 return ret;
244}
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700245
Wendy Liang328105c2017-10-03 23:21:11 -0700246void pm_ipi_irq_enable(const struct pm_proc *proc)
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700247{
Jolly Shah36c930b2019-01-07 12:51:40 -0800248 ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700249}
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700250
Wendy Liang328105c2017-10-03 23:21:11 -0700251void pm_ipi_irq_clear(const struct pm_proc *proc)
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700252{
Jolly Shah36c930b2019-01-07 12:51:40 -0800253 ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700254}
Jolly Shah259afe52019-01-09 12:37:57 -0800255
256uint32_t pm_ipi_irq_status(const struct pm_proc *proc)
257{
258 int ret;
259
260 ret = ipi_mb_enquire_status(proc->ipi->local_ipi_id,
261 proc->ipi->remote_ipi_id);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530262 if (ret & IPI_MB_STATUS_RECV_PENDING) {
Jolly Shah259afe52019-01-09 12:37:57 -0800263 return 1;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530264 } else {
Jolly Shah259afe52019-01-09 12:37:57 -0800265 return 0;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530266 }
Jolly Shah259afe52019-01-09 12:37:57 -0800267}
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530268
Venkatesh Yadav Abbarapubfa2a2a2021-02-19 01:40:14 -0700269#if IPI_CRC_CHECK
Venkatesh Yadav Abbarapu28b77592022-04-11 09:25:44 +0530270uint32_t calculate_crc(uint32_t payload[PAYLOAD_ARG_CNT], uint32_t bufsize)
Venkatesh Yadav Abbarapu35a8be92019-04-04 14:23:32 +0530271{
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