Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 1 | /* |
Jolly Shah | 0bfd700 | 2019-01-08 11:10:47 -0800 | [diff] [blame] | 2 | * Copyright (c) 2013-2018, 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; |
| 60 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 61 | /* Write payload into IPI buffer */ |
| 62 | for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) { |
| 63 | mmio_write_32(buffer_base + offset, payload[i]); |
| 64 | offset += PAYLOAD_ARG_SIZE; |
| 65 | } |
Tejas Patel | af4b10e | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 66 | |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 67 | /* Generate IPI to remote processor */ |
Jolly Shah | 36c930b | 2019-01-07 12:51:40 -0800 | [diff] [blame] | 68 | 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] | 69 | is_blocking); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 70 | |
| 71 | return PM_RET_SUCCESS; |
| 72 | } |
| 73 | |
| 74 | /** |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 75 | * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor |
| 76 | * without blocking notification |
Tejas Patel | af4b10e | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 77 | * @proc Pointer to the processor who is initiating request |
| 78 | * @payload API id and call arguments to be written in IPI buffer |
| 79 | * |
| 80 | * Send an IPI request to the power controller. |
| 81 | * |
| 82 | * @return Returns status, either success or error+reason |
| 83 | */ |
| 84 | enum pm_ret_status pm_ipi_send_non_blocking(const struct pm_proc *proc, |
| 85 | uint32_t payload[PAYLOAD_ARG_CNT]) |
| 86 | { |
| 87 | enum pm_ret_status ret; |
| 88 | |
| 89 | bakery_lock_get(&pm_secure_lock); |
| 90 | |
| 91 | ret = pm_ipi_send_common(proc, payload, IPI_NON_BLOCKING); |
| 92 | |
| 93 | bakery_lock_release(&pm_secure_lock); |
| 94 | |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | /** |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 99 | * pm_ipi_send() - Sends IPI request to the remote processor |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 100 | * @proc Pointer to the processor who is initiating request |
| 101 | * @payload API id and call arguments to be written in IPI buffer |
| 102 | * |
| 103 | * Send an IPI request to the power controller. |
| 104 | * |
| 105 | * @return Returns status, either success or error+reason |
| 106 | */ |
| 107 | enum pm_ret_status pm_ipi_send(const struct pm_proc *proc, |
| 108 | uint32_t payload[PAYLOAD_ARG_CNT]) |
| 109 | { |
| 110 | enum pm_ret_status ret; |
| 111 | |
| 112 | bakery_lock_get(&pm_secure_lock); |
| 113 | |
Tejas Patel | af4b10e | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 114 | ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 115 | |
| 116 | bakery_lock_release(&pm_secure_lock); |
| 117 | |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /** |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 123 | * pm_ipi_buff_read() - Reads IPI response after remote processor has handled |
| 124 | * interrupt |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 125 | * @proc Pointer to the processor who is waiting and reading response |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 126 | * @value Used to return value from IPI buffer element (optional) |
| 127 | * @count Number of values to return in @value |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 128 | * |
| 129 | * @return Returns status, either success or error+reason |
| 130 | */ |
| 131 | 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] | 132 | unsigned int *value, size_t count) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 133 | { |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 134 | size_t i; |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 135 | uintptr_t buffer_base = proc->ipi->buffer_base + |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 136 | IPI_BUFFER_TARGET_REMOTE_OFFSET + |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 137 | IPI_BUFFER_RESP_OFFSET; |
| 138 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 139 | /* |
| 140 | * Read response from IPI buffer |
| 141 | * buf-0: success or error+reason |
| 142 | * buf-1: value |
| 143 | * buf-2: unused |
| 144 | * buf-3: unused |
| 145 | */ |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 146 | for (i = 1; i <= count; i++) { |
| 147 | *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE)); |
| 148 | value++; |
| 149 | } |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 150 | |
| 151 | return mmio_read_32(buffer_base); |
| 152 | } |
| 153 | |
| 154 | /** |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 155 | * pm_ipi_buff_read_callb() - Reads IPI response after remote processor has |
| 156 | * handled interrupt |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 157 | * @value Used to return value from IPI buffer element (optional) |
| 158 | * @count Number of values to return in @value |
| 159 | * |
| 160 | * @return Returns status, either success or error+reason |
| 161 | */ |
| 162 | void pm_ipi_buff_read_callb(unsigned int *value, size_t count) |
| 163 | { |
| 164 | size_t i; |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 165 | uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE + |
| 166 | IPI_BUFFER_TARGET_LOCAL_OFFSET + |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 167 | IPI_BUFFER_REQ_OFFSET; |
| 168 | |
| 169 | if (count > IPI_BUFFER_MAX_WORDS) |
| 170 | count = IPI_BUFFER_MAX_WORDS; |
| 171 | |
| 172 | for (i = 0; i <= count; i++) { |
| 173 | *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE)); |
| 174 | value++; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
Jolly Shah | 9bb628d | 2019-01-07 12:53:32 -0800 | [diff] [blame] | 179 | * pm_ipi_send_sync() - Sends IPI request to the remote processor |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 180 | * @proc Pointer to the processor who is initiating request |
| 181 | * @payload API id and call arguments to be written in IPI buffer |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 182 | * @value Used to return value from IPI buffer element (optional) |
| 183 | * @count Number of values to return in @value |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 184 | * |
| 185 | * Send an IPI request to the power controller and wait for it to be handled. |
| 186 | * |
| 187 | * @return Returns status, either success or error+reason and, optionally, |
| 188 | * @value |
| 189 | */ |
| 190 | enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc, |
| 191 | uint32_t payload[PAYLOAD_ARG_CNT], |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 192 | unsigned int *value, size_t count) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 193 | { |
| 194 | enum pm_ret_status ret; |
| 195 | |
| 196 | bakery_lock_get(&pm_secure_lock); |
| 197 | |
Tejas Patel | af4b10e | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 198 | ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 199 | if (ret != PM_RET_SUCCESS) |
| 200 | goto unlock; |
| 201 | |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 202 | ret = pm_ipi_buff_read(proc, value, count); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 203 | |
| 204 | unlock: |
| 205 | bakery_lock_release(&pm_secure_lock); |
| 206 | |
| 207 | return ret; |
| 208 | } |
Soren Brinkmann | a1b0a90 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 209 | |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 210 | void pm_ipi_irq_enable(const struct pm_proc *proc) |
Soren Brinkmann | a1b0a90 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 211 | { |
Jolly Shah | 36c930b | 2019-01-07 12:51:40 -0800 | [diff] [blame] | 212 | 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] | 213 | } |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 214 | |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 215 | void pm_ipi_irq_clear(const struct pm_proc *proc) |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 216 | { |
Jolly Shah | 36c930b | 2019-01-07 12:51:40 -0800 | [diff] [blame] | 217 | 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] | 218 | } |
Jolly Shah | 259afe5 | 2019-01-09 12:37:57 -0800 | [diff] [blame] | 219 | |
| 220 | uint32_t pm_ipi_irq_status(const struct pm_proc *proc) |
| 221 | { |
| 222 | int ret; |
| 223 | |
| 224 | ret = ipi_mb_enquire_status(proc->ipi->local_ipi_id, |
| 225 | proc->ipi->remote_ipi_id); |
| 226 | if (ret & IPI_MB_STATUS_RECV_PENDING) |
| 227 | return 1; |
| 228 | else |
| 229 | return 0; |
| 230 | } |