Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 1 | /* |
Venkatesh Yadav Abbarapu | 35a8be9 | 2019-04-04 14:23:32 +0530 | [diff] [blame] | 2 | * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved. |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 5 | */ |
| 6 | |
Jolly Shah | 259afe5 | 2019-01-09 12:37:57 -0800 | [diff] [blame] | 7 | |
Isla Mitchell | e363146 | 2017-07-14 10:46:32 +0100 | [diff] [blame] | 8 | #include <arch_helpers.h> |
Jolly Shah | 259afe5 | 2019-01-09 12:37:57 -0800 | [diff] [blame] | 9 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <lib/bakery_lock.h> |
| 11 | #include <lib/mmio.h> |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 12 | |
| 13 | #include <ipi.h> |
Jolly Shah | 4c17237 | 2019-01-08 11:21:29 -0800 | [diff] [blame] | 14 | #include <plat_ipi.h> |
Jolly Shah | 0bfd700 | 2019-01-08 11:10:47 -0800 | [diff] [blame] | 15 | #include <plat_private.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 16 | #include <plat/common/platform.h> |
| 17 | |
Isla Mitchell | e363146 | 2017-07-14 10:46:32 +0100 | [diff] [blame] | 18 | #include "pm_ipi.h" |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 19 | |
Tejas Patel | af4b10e | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 20 | |
Stefan Krsmanovic | c5e9f66 | 2016-05-20 15:51:08 +0200 | [diff] [blame] | 21 | DEFINE_BAKERY_LOCK(pm_secure_lock); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 22 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 23 | /** |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 24 | * pm_ipi_init() - Initialize IPI peripheral for communication with |
| 25 | * remote processor |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 26 | * |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 27 | * @proc Pointer to the processor who is initiating request |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 28 | * @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 Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 32 | * Called from pm_setup initialization function |
| 33 | */ |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 34 | int pm_ipi_init(const struct pm_proc *proc) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 35 | { |
| 36 | bakery_lock_init(&pm_secure_lock); |
Jolly Shah | 36c930b | 2019-01-07 12:51:40 -0800 | [diff] [blame] | 37 | ipi_mb_open(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | /** |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 43 | * pm_ipi_send_common() - Sends IPI request to the remote processor |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 44 | * @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 | */ |
| 52 | static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc, |
Tejas Patel | af4b10e | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 53 | uint32_t payload[PAYLOAD_ARG_CNT], |
| 54 | uint32_t is_blocking) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 55 | { |
| 56 | unsigned int offset = 0; |
| 57 | uintptr_t buffer_base = proc->ipi->buffer_base + |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 58 | IPI_BUFFER_TARGET_REMOTE_OFFSET + |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 59 | IPI_BUFFER_REQ_OFFSET; |
Venkatesh Yadav Abbarapu | 35a8be9 | 2019-04-04 14:23:32 +0530 | [diff] [blame] | 60 | #if ZYNQMP_IPI_CRC_CHECK |
| 61 | payload[PAYLOAD_CRC_POS] = calculate_crc(payload, IPI_W0_TO_W6_SIZE); |
| 62 | #endif |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 63 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 64 | /* 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 Patel | af4b10e | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 69 | |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 70 | /* Generate IPI to remote processor */ |
Jolly Shah | 36c930b | 2019-01-07 12:51:40 -0800 | [diff] [blame] | 71 | ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id, |
Tejas Patel | af4b10e | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 72 | is_blocking); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 73 | |
| 74 | return PM_RET_SUCCESS; |
| 75 | } |
| 76 | |
| 77 | /** |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 78 | * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor |
| 79 | * without blocking notification |
Tejas Patel | af4b10e | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 80 | * @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 | */ |
| 87 | enum 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 Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 102 | * pm_ipi_send() - Sends IPI request to the remote processor |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 103 | * @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 | */ |
| 110 | enum 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 Patel | af4b10e | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 117 | ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 118 | |
| 119 | bakery_lock_release(&pm_secure_lock); |
| 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /** |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 126 | * pm_ipi_buff_read() - Reads IPI response after remote processor has handled |
| 127 | * interrupt |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 128 | * @proc Pointer to the processor who is waiting and reading response |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 129 | * @value Used to return value from IPI buffer element (optional) |
| 130 | * @count Number of values to return in @value |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 131 | * |
| 132 | * @return Returns status, either success or error+reason |
| 133 | */ |
| 134 | static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc, |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 135 | unsigned int *value, size_t count) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 136 | { |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 137 | size_t i; |
Venkatesh Yadav Abbarapu | 35a8be9 | 2019-04-04 14:23:32 +0530 | [diff] [blame] | 138 | #if ZYNQMP_IPI_CRC_CHECK |
| 139 | size_t j; |
| 140 | unsigned int response_payload[PAYLOAD_ARG_CNT]; |
| 141 | #endif |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 142 | uintptr_t buffer_base = proc->ipi->buffer_base + |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 143 | IPI_BUFFER_TARGET_REMOTE_OFFSET + |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 144 | IPI_BUFFER_RESP_OFFSET; |
| 145 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 146 | /* |
| 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 Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 153 | for (i = 1; i <= count; i++) { |
| 154 | *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE)); |
| 155 | value++; |
| 156 | } |
Venkatesh Yadav Abbarapu | 35a8be9 | 2019-04-04 14:23:32 +0530 | [diff] [blame] | 157 | #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 Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 167 | |
| 168 | return mmio_read_32(buffer_base); |
| 169 | } |
| 170 | |
| 171 | /** |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 172 | * pm_ipi_buff_read_callb() - Reads IPI response after remote processor has |
| 173 | * handled interrupt |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 174 | * @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 | */ |
| 179 | void pm_ipi_buff_read_callb(unsigned int *value, size_t count) |
| 180 | { |
| 181 | size_t i; |
Venkatesh Yadav Abbarapu | 35a8be9 | 2019-04-04 14:23:32 +0530 | [diff] [blame] | 182 | #if ZYNQMP_IPI_CRC_CHECK |
| 183 | size_t j; |
| 184 | unsigned int response_payload[PAYLOAD_ARG_CNT]; |
| 185 | #endif |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 186 | uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE + |
| 187 | IPI_BUFFER_TARGET_LOCAL_OFFSET + |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 188 | 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 Abbarapu | 35a8be9 | 2019-04-04 14:23:32 +0530 | [diff] [blame] | 197 | #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 Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 210 | * pm_ipi_send_sync() - Sends IPI request to the remote processor |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 211 | * @proc Pointer to the processor who is initiating request |
| 212 | * @payload API id and call arguments to be written in IPI buffer |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 213 | * @value Used to return value from IPI buffer element (optional) |
| 214 | * @count Number of values to return in @value |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 215 | * |
| 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 | */ |
| 221 | enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc, |
| 222 | uint32_t payload[PAYLOAD_ARG_CNT], |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 223 | unsigned int *value, size_t count) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 224 | { |
| 225 | enum pm_ret_status ret; |
| 226 | |
| 227 | bakery_lock_get(&pm_secure_lock); |
| 228 | |
Tejas Patel | af4b10e | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 229 | ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 230 | if (ret != PM_RET_SUCCESS) |
| 231 | goto unlock; |
| 232 | |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 233 | ret = pm_ipi_buff_read(proc, value, count); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 234 | |
| 235 | unlock: |
| 236 | bakery_lock_release(&pm_secure_lock); |
| 237 | |
| 238 | return ret; |
| 239 | } |
Soren Brinkmann | a1b0a90 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 240 | |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 241 | void pm_ipi_irq_enable(const struct pm_proc *proc) |
Soren Brinkmann | a1b0a90 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 242 | { |
Jolly Shah | 36c930b | 2019-01-07 12:51:40 -0800 | [diff] [blame] | 243 | ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); |
Soren Brinkmann | a1b0a90 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 244 | } |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 245 | |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 246 | void pm_ipi_irq_clear(const struct pm_proc *proc) |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 247 | { |
Jolly Shah | 36c930b | 2019-01-07 12:51:40 -0800 | [diff] [blame] | 248 | ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 249 | } |
Jolly Shah | 259afe5 | 2019-01-09 12:37:57 -0800 | [diff] [blame] | 250 | |
| 251 | uint32_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 Abbarapu | 35a8be9 | 2019-04-04 14:23:32 +0530 | [diff] [blame] | 262 | |
| 263 | #if ZYNQMP_IPI_CRC_CHECK |
| 264 | uint32_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 |