Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 1 | /* |
Jolly Shah | 31c3842 | 2019-01-08 11:10:47 -0800 | [diff] [blame] | 2 | * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved. |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 3 | * |
dp-arm | 82cb2c1 | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 5 | */ |
| 6 | |
Isla Mitchell | ee1ebbd | 2017-07-14 10:46:32 +0100 | [diff] [blame] | 7 | #include <arch_helpers.h> |
Antonio Nino Diaz | 09d40e0 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | #include <lib/bakery_lock.h> |
| 9 | #include <lib/mmio.h> |
Jolly Shah | 63436bd | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 10 | |
| 11 | #include <ipi.h> |
Jolly Shah | 1611ef2 | 2019-01-08 11:21:29 -0800 | [diff] [blame] | 12 | #include <plat_ipi.h> |
Jolly Shah | 31c3842 | 2019-01-08 11:10:47 -0800 | [diff] [blame] | 13 | #include <plat_private.h> |
Antonio Nino Diaz | 09d40e0 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | #include <plat/common/platform.h> |
| 15 | |
Isla Mitchell | ee1ebbd | 2017-07-14 10:46:32 +0100 | [diff] [blame] | 16 | #include "pm_ipi.h" |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 17 | |
| 18 | /* IPI message buffers */ |
| 19 | #define IPI_BUFFER_BASEADDR 0xFF990000U |
| 20 | |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 21 | #define IPI_BUFFER_APU_BASE (IPI_BUFFER_BASEADDR + 0x400U) |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 22 | #define IPI_BUFFER_PMU_BASE (IPI_BUFFER_BASEADDR + 0xE00U) |
| 23 | |
Jolly Shah | 5bd029b | 2019-01-07 12:53:32 -0800 | [diff] [blame^] | 24 | #define IPI_BUFFER_LOCAL_BASE IPI_BUFFER_APU_BASE |
| 25 | #define IPI_BUFFER_REMOTE_BASE IPI_BUFFER_PMU_BASE |
| 26 | |
| 27 | #define IPI_BUFFER_TARGET_LOCAL_OFFSET 0x80U |
| 28 | #define IPI_BUFFER_TARGET_REMOTE_OFFSET 0x1C0U |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 29 | |
Soren Brinkmann | 300cbb0 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 30 | #define IPI_BUFFER_MAX_WORDS 8 |
| 31 | |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 32 | #define IPI_BUFFER_REQ_OFFSET 0x0U |
| 33 | #define IPI_BUFFER_RESP_OFFSET 0x20U |
| 34 | |
Tejas Patel | 7dae613 | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 35 | #define IPI_BLOCKING 1 |
| 36 | #define IPI_NON_BLOCKING 0 |
| 37 | |
Stefan Krsmanovic | 8212f1f | 2016-05-20 15:51:08 +0200 | [diff] [blame] | 38 | DEFINE_BAKERY_LOCK(pm_secure_lock); |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 39 | |
| 40 | const struct pm_ipi apu_ipi = { |
Jolly Shah | 28e4d37 | 2019-01-07 12:51:40 -0800 | [diff] [blame] | 41 | .local_ipi_id = IPI_ID_APU, |
| 42 | .remote_ipi_id = IPI_ID_PMU0, |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 43 | .buffer_base = IPI_BUFFER_APU_BASE, |
| 44 | }; |
| 45 | |
| 46 | /** |
Jolly Shah | 5bd029b | 2019-01-07 12:53:32 -0800 | [diff] [blame^] | 47 | * pm_ipi_init() - Initialize IPI peripheral for communication with |
| 48 | * remote processor |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 49 | * |
Wendy Liang | ebc0516 | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 50 | * @proc Pointer to the processor who is initiating request |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 51 | * @return On success, the initialization function must return 0. |
| 52 | * Any other return value will cause the framework to ignore |
| 53 | * the service |
| 54 | * |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 55 | * Called from pm_setup initialization function |
| 56 | */ |
Wendy Liang | ebc0516 | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 57 | int pm_ipi_init(const struct pm_proc *proc) |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 58 | { |
| 59 | bakery_lock_init(&pm_secure_lock); |
Jolly Shah | 28e4d37 | 2019-01-07 12:51:40 -0800 | [diff] [blame] | 60 | ipi_mb_open(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /** |
Jolly Shah | 5bd029b | 2019-01-07 12:53:32 -0800 | [diff] [blame^] | 66 | * pm_ipi_send_common() - Sends IPI request to the remote processor |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 67 | * @proc Pointer to the processor who is initiating request |
| 68 | * @payload API id and call arguments to be written in IPI buffer |
| 69 | * |
| 70 | * Send an IPI request to the power controller. Caller needs to hold |
| 71 | * the 'pm_secure_lock' lock. |
| 72 | * |
| 73 | * @return Returns status, either success or error+reason |
| 74 | */ |
| 75 | static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc, |
Tejas Patel | 7dae613 | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 76 | uint32_t payload[PAYLOAD_ARG_CNT], |
| 77 | uint32_t is_blocking) |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 78 | { |
| 79 | unsigned int offset = 0; |
| 80 | uintptr_t buffer_base = proc->ipi->buffer_base + |
Jolly Shah | 5bd029b | 2019-01-07 12:53:32 -0800 | [diff] [blame^] | 81 | IPI_BUFFER_TARGET_REMOTE_OFFSET + |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 82 | IPI_BUFFER_REQ_OFFSET; |
| 83 | |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 84 | /* Write payload into IPI buffer */ |
| 85 | for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) { |
| 86 | mmio_write_32(buffer_base + offset, payload[i]); |
| 87 | offset += PAYLOAD_ARG_SIZE; |
| 88 | } |
Tejas Patel | 7dae613 | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 89 | |
Jolly Shah | 5bd029b | 2019-01-07 12:53:32 -0800 | [diff] [blame^] | 90 | /* Generate IPI to remote processor */ |
Jolly Shah | 28e4d37 | 2019-01-07 12:51:40 -0800 | [diff] [blame] | 91 | ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id, |
Tejas Patel | 7dae613 | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 92 | is_blocking); |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 93 | |
| 94 | return PM_RET_SUCCESS; |
| 95 | } |
| 96 | |
| 97 | /** |
Jolly Shah | 5bd029b | 2019-01-07 12:53:32 -0800 | [diff] [blame^] | 98 | * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor |
| 99 | * without blocking notification |
Tejas Patel | 7dae613 | 2018-02-09 02:42:59 -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_non_blocking(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 | |
| 114 | ret = pm_ipi_send_common(proc, payload, IPI_NON_BLOCKING); |
| 115 | |
| 116 | bakery_lock_release(&pm_secure_lock); |
| 117 | |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | /** |
Jolly Shah | 5bd029b | 2019-01-07 12:53:32 -0800 | [diff] [blame^] | 122 | * pm_ipi_send() - Sends IPI request to the remote processor |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 123 | * @proc Pointer to the processor who is initiating request |
| 124 | * @payload API id and call arguments to be written in IPI buffer |
| 125 | * |
| 126 | * Send an IPI request to the power controller. |
| 127 | * |
| 128 | * @return Returns status, either success or error+reason |
| 129 | */ |
| 130 | enum pm_ret_status pm_ipi_send(const struct pm_proc *proc, |
| 131 | uint32_t payload[PAYLOAD_ARG_CNT]) |
| 132 | { |
| 133 | enum pm_ret_status ret; |
| 134 | |
| 135 | bakery_lock_get(&pm_secure_lock); |
| 136 | |
Tejas Patel | 7dae613 | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 137 | ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING); |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 138 | |
| 139 | bakery_lock_release(&pm_secure_lock); |
| 140 | |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /** |
Jolly Shah | 5bd029b | 2019-01-07 12:53:32 -0800 | [diff] [blame^] | 146 | * pm_ipi_buff_read() - Reads IPI response after remote processor has handled |
| 147 | * interrupt |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 148 | * @proc Pointer to the processor who is waiting and reading response |
Soren Brinkmann | dc0c5a4 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 149 | * @value Used to return value from IPI buffer element (optional) |
| 150 | * @count Number of values to return in @value |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 151 | * |
| 152 | * @return Returns status, either success or error+reason |
| 153 | */ |
| 154 | static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc, |
Soren Brinkmann | dc0c5a4 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 155 | unsigned int *value, size_t count) |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 156 | { |
Soren Brinkmann | dc0c5a4 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 157 | size_t i; |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 158 | uintptr_t buffer_base = proc->ipi->buffer_base + |
Jolly Shah | 5bd029b | 2019-01-07 12:53:32 -0800 | [diff] [blame^] | 159 | IPI_BUFFER_TARGET_REMOTE_OFFSET + |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 160 | IPI_BUFFER_RESP_OFFSET; |
| 161 | |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 162 | /* |
| 163 | * Read response from IPI buffer |
| 164 | * buf-0: success or error+reason |
| 165 | * buf-1: value |
| 166 | * buf-2: unused |
| 167 | * buf-3: unused |
| 168 | */ |
Soren Brinkmann | dc0c5a4 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 169 | for (i = 1; i <= count; i++) { |
| 170 | *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE)); |
| 171 | value++; |
| 172 | } |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 173 | |
| 174 | return mmio_read_32(buffer_base); |
| 175 | } |
| 176 | |
| 177 | /** |
Jolly Shah | 5bd029b | 2019-01-07 12:53:32 -0800 | [diff] [blame^] | 178 | * pm_ipi_buff_read_callb() - Reads IPI response after remote processor has |
| 179 | * handled interrupt |
Soren Brinkmann | 300cbb0 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 180 | * @value Used to return value from IPI buffer element (optional) |
| 181 | * @count Number of values to return in @value |
| 182 | * |
| 183 | * @return Returns status, either success or error+reason |
| 184 | */ |
| 185 | void pm_ipi_buff_read_callb(unsigned int *value, size_t count) |
| 186 | { |
| 187 | size_t i; |
Jolly Shah | 5bd029b | 2019-01-07 12:53:32 -0800 | [diff] [blame^] | 188 | uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE + |
| 189 | IPI_BUFFER_TARGET_LOCAL_OFFSET + |
Soren Brinkmann | 300cbb0 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 190 | IPI_BUFFER_REQ_OFFSET; |
| 191 | |
| 192 | if (count > IPI_BUFFER_MAX_WORDS) |
| 193 | count = IPI_BUFFER_MAX_WORDS; |
| 194 | |
| 195 | for (i = 0; i <= count; i++) { |
| 196 | *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE)); |
| 197 | value++; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
Jolly Shah | 5bd029b | 2019-01-07 12:53:32 -0800 | [diff] [blame^] | 202 | * pm_ipi_send_sync() - Sends IPI request to the remote processor |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 203 | * @proc Pointer to the processor who is initiating request |
| 204 | * @payload API id and call arguments to be written in IPI buffer |
Soren Brinkmann | dc0c5a4 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 205 | * @value Used to return value from IPI buffer element (optional) |
| 206 | * @count Number of values to return in @value |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 207 | * |
| 208 | * Send an IPI request to the power controller and wait for it to be handled. |
| 209 | * |
| 210 | * @return Returns status, either success or error+reason and, optionally, |
| 211 | * @value |
| 212 | */ |
| 213 | enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc, |
| 214 | uint32_t payload[PAYLOAD_ARG_CNT], |
Soren Brinkmann | dc0c5a4 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 215 | unsigned int *value, size_t count) |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 216 | { |
| 217 | enum pm_ret_status ret; |
| 218 | |
| 219 | bakery_lock_get(&pm_secure_lock); |
| 220 | |
Tejas Patel | 7dae613 | 2018-02-09 02:42:59 -0800 | [diff] [blame] | 221 | ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING); |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 222 | if (ret != PM_RET_SUCCESS) |
| 223 | goto unlock; |
| 224 | |
Soren Brinkmann | dc0c5a4 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 225 | ret = pm_ipi_buff_read(proc, value, count); |
Soren Brinkmann | c828440 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 226 | |
| 227 | unlock: |
| 228 | bakery_lock_release(&pm_secure_lock); |
| 229 | |
| 230 | return ret; |
| 231 | } |
Soren Brinkmann | a76c369 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 232 | |
Wendy Liang | ebc0516 | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 233 | void pm_ipi_irq_enable(const struct pm_proc *proc) |
Soren Brinkmann | a76c369 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 234 | { |
Jolly Shah | 28e4d37 | 2019-01-07 12:51:40 -0800 | [diff] [blame] | 235 | ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); |
Soren Brinkmann | a76c369 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Wendy Liang | ebc0516 | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 238 | void pm_ipi_irq_clear(const struct pm_proc *proc) |
Soren Brinkmann | a76c369 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 239 | { |
Jolly Shah | 28e4d37 | 2019-01-07 12:51:40 -0800 | [diff] [blame] | 240 | ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id); |
Soren Brinkmann | 300cbb0 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 241 | } |