Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <bakery_lock.h> |
| 32 | #include <mmio.h> |
| 33 | #include <platform.h> |
| 34 | #include <arch_helpers.h> |
| 35 | #include "pm_ipi.h" |
| 36 | #include "../zynqmp_private.h" |
| 37 | |
| 38 | /* IPI message buffers */ |
| 39 | #define IPI_BUFFER_BASEADDR 0xFF990000U |
| 40 | |
| 41 | #define IPI_BUFFER_RPU_0_BASE (IPI_BUFFER_BASEADDR + 0x0U) |
| 42 | #define IPI_BUFFER_RPU_1_BASE (IPI_BUFFER_BASEADDR + 0x200U) |
| 43 | #define IPI_BUFFER_APU_BASE (IPI_BUFFER_BASEADDR + 0x400U) |
| 44 | #define IPI_BUFFER_PL_0_BASE (IPI_BUFFER_BASEADDR + 0x600U) |
| 45 | #define IPI_BUFFER_PL_1_BASE (IPI_BUFFER_BASEADDR + 0x800U) |
| 46 | #define IPI_BUFFER_PL_2_BASE (IPI_BUFFER_BASEADDR + 0xA00U) |
| 47 | #define IPI_BUFFER_PL_3_BASE (IPI_BUFFER_BASEADDR + 0xC00U) |
| 48 | #define IPI_BUFFER_PMU_BASE (IPI_BUFFER_BASEADDR + 0xE00U) |
| 49 | |
| 50 | #define IPI_BUFFER_TARGET_RPU_0_OFFSET 0x0U |
| 51 | #define IPI_BUFFER_TARGET_RPU_1_OFFSET 0x40U |
| 52 | #define IPI_BUFFER_TARGET_APU_OFFSET 0x80U |
| 53 | #define IPI_BUFFER_TARGET_PL_0_OFFSET 0xC0U |
| 54 | #define IPI_BUFFER_TARGET_PL_1_OFFSET 0x100U |
| 55 | #define IPI_BUFFER_TARGET_PL_2_OFFSET 0x140U |
| 56 | #define IPI_BUFFER_TARGET_PL_3_OFFSET 0x180U |
| 57 | #define IPI_BUFFER_TARGET_PMU_OFFSET 0x1C0U |
| 58 | |
| 59 | #define IPI_BUFFER_REQ_OFFSET 0x0U |
| 60 | #define IPI_BUFFER_RESP_OFFSET 0x20U |
| 61 | |
| 62 | /* IPI Base Address */ |
| 63 | #define IPI_BASEADDR 0XFF300000 |
| 64 | |
| 65 | /* APU's IPI registers */ |
| 66 | #define IPI_APU_ISR (IPI_BASEADDR + 0X00000010) |
| 67 | #define IPI_APU_IER (IPI_BASEADDR + 0X00000018) |
| 68 | #define IPI_APU_IDR (IPI_BASEADDR + 0X0000001C) |
Soren Brinkmann | acff8a4 | 2016-04-11 15:30:56 -0700 | [diff] [blame] | 69 | #define IPI_APU_IXR_PMU_0_MASK (1 << 16) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 70 | |
| 71 | #define IPI_TRIG_OFFSET 0 |
| 72 | #define IPI_OBS_OFFSET 4 |
| 73 | |
| 74 | /* Power Management IPI interrupt number */ |
| 75 | #define PM_INT_NUM 0 |
| 76 | #define IPI_PMU_PM_INT_BASE (IPI_PMU_0_TRIG + (PM_INT_NUM * 0x1000)) |
Soren Brinkmann | acff8a4 | 2016-04-11 15:30:56 -0700 | [diff] [blame] | 77 | #define IPI_PMU_PM_INT_MASK (IPI_APU_IXR_PMU_0_MASK << PM_INT_NUM) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 78 | #if (PM_INT_NUM < 0 || PM_INT_NUM > 3) |
| 79 | #error PM_INT_NUM value out of range |
| 80 | #endif |
| 81 | |
| 82 | #define IPI_APU_MASK 1U |
| 83 | |
Stefan Krsmanovic | c5e9f66 | 2016-05-20 15:51:08 +0200 | [diff] [blame] | 84 | DEFINE_BAKERY_LOCK(pm_secure_lock); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 85 | |
| 86 | const struct pm_ipi apu_ipi = { |
| 87 | .mask = IPI_APU_MASK, |
| 88 | .base = IPI_BASEADDR, |
| 89 | .buffer_base = IPI_BUFFER_APU_BASE, |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * pm_ipi_init() - Initialize IPI peripheral for communication with PMU |
| 94 | * |
| 95 | * @return On success, the initialization function must return 0. |
| 96 | * Any other return value will cause the framework to ignore |
| 97 | * the service |
| 98 | * |
| 99 | * Enable interrupts at registered entrance in IPI peripheral |
| 100 | * Called from pm_setup initialization function |
| 101 | */ |
| 102 | int pm_ipi_init(void) |
| 103 | { |
| 104 | bakery_lock_init(&pm_secure_lock); |
| 105 | |
| 106 | /* IPI Interrupts Clear & Disable */ |
| 107 | mmio_write_32(IPI_APU_ISR, 0xffffffff); |
| 108 | mmio_write_32(IPI_APU_IDR, 0xffffffff); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * pm_ipi_wait() - wait for pmu to handle request |
| 115 | * @proc proc which is waiting for PMU to handle request |
| 116 | */ |
| 117 | static enum pm_ret_status pm_ipi_wait(const struct pm_proc *proc) |
| 118 | { |
| 119 | int status; |
| 120 | |
| 121 | /* Wait until previous interrupt is handled by PMU */ |
| 122 | do { |
| 123 | status = mmio_read_32(proc->ipi->base + IPI_OBS_OFFSET) & |
| 124 | IPI_PMU_PM_INT_MASK; |
| 125 | /* TODO: 1) Use timer to add delay between read attempts */ |
| 126 | /* TODO: 2) Return PM_RET_ERR_TIMEOUT if this times out */ |
| 127 | } while (status); |
| 128 | |
| 129 | return PM_RET_SUCCESS; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * pm_ipi_send_common() - Sends IPI request to the PMU |
| 134 | * @proc Pointer to the processor who is initiating request |
| 135 | * @payload API id and call arguments to be written in IPI buffer |
| 136 | * |
| 137 | * Send an IPI request to the power controller. Caller needs to hold |
| 138 | * the 'pm_secure_lock' lock. |
| 139 | * |
| 140 | * @return Returns status, either success or error+reason |
| 141 | */ |
| 142 | static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc, |
| 143 | uint32_t payload[PAYLOAD_ARG_CNT]) |
| 144 | { |
| 145 | unsigned int offset = 0; |
| 146 | uintptr_t buffer_base = proc->ipi->buffer_base + |
| 147 | IPI_BUFFER_TARGET_PMU_OFFSET + |
| 148 | IPI_BUFFER_REQ_OFFSET; |
| 149 | |
| 150 | /* Wait until previous interrupt is handled by PMU */ |
| 151 | pm_ipi_wait(proc); |
| 152 | |
| 153 | /* Write payload into IPI buffer */ |
| 154 | for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) { |
| 155 | mmio_write_32(buffer_base + offset, payload[i]); |
| 156 | offset += PAYLOAD_ARG_SIZE; |
| 157 | } |
| 158 | /* Generate IPI to PMU */ |
| 159 | mmio_write_32(proc->ipi->base + IPI_TRIG_OFFSET, IPI_PMU_PM_INT_MASK); |
| 160 | |
| 161 | return PM_RET_SUCCESS; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * pm_ipi_send() - Sends IPI request to the PMU |
| 166 | * @proc Pointer to the processor who is initiating request |
| 167 | * @payload API id and call arguments to be written in IPI buffer |
| 168 | * |
| 169 | * Send an IPI request to the power controller. |
| 170 | * |
| 171 | * @return Returns status, either success or error+reason |
| 172 | */ |
| 173 | enum pm_ret_status pm_ipi_send(const struct pm_proc *proc, |
| 174 | uint32_t payload[PAYLOAD_ARG_CNT]) |
| 175 | { |
| 176 | enum pm_ret_status ret; |
| 177 | |
| 178 | bakery_lock_get(&pm_secure_lock); |
| 179 | |
| 180 | ret = pm_ipi_send_common(proc, payload); |
| 181 | |
| 182 | bakery_lock_release(&pm_secure_lock); |
| 183 | |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | /** |
| 189 | * pm_ipi_buff_read() - Reads IPI response after PMU has handled interrupt |
| 190 | * @proc Pointer to the processor who is waiting and reading response |
| 191 | * @value Used to return value from 2nd IPI buffer element (optional) |
| 192 | * |
| 193 | * @return Returns status, either success or error+reason |
| 194 | */ |
| 195 | static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc, |
| 196 | unsigned int *value) |
| 197 | { |
| 198 | uintptr_t buffer_base = proc->ipi->buffer_base + |
| 199 | IPI_BUFFER_TARGET_PMU_OFFSET + |
| 200 | IPI_BUFFER_RESP_OFFSET; |
| 201 | |
| 202 | pm_ipi_wait(proc); |
| 203 | |
| 204 | /* |
| 205 | * Read response from IPI buffer |
| 206 | * buf-0: success or error+reason |
| 207 | * buf-1: value |
| 208 | * buf-2: unused |
| 209 | * buf-3: unused |
| 210 | */ |
| 211 | if (value != NULL) |
| 212 | *value = mmio_read_32(buffer_base + PAYLOAD_ARG_SIZE); |
| 213 | |
| 214 | return mmio_read_32(buffer_base); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * pm_ipi_send_sync() - Sends IPI request to the PMU |
| 219 | * @proc Pointer to the processor who is initiating request |
| 220 | * @payload API id and call arguments to be written in IPI buffer |
| 221 | * @value Used to return value from 2nd IPI buffer element (optional) |
| 222 | * |
| 223 | * Send an IPI request to the power controller and wait for it to be handled. |
| 224 | * |
| 225 | * @return Returns status, either success or error+reason and, optionally, |
| 226 | * @value |
| 227 | */ |
| 228 | enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc, |
| 229 | uint32_t payload[PAYLOAD_ARG_CNT], |
| 230 | unsigned int *value) |
| 231 | { |
| 232 | enum pm_ret_status ret; |
| 233 | |
| 234 | bakery_lock_get(&pm_secure_lock); |
| 235 | |
| 236 | ret = pm_ipi_send_common(proc, payload); |
| 237 | if (ret != PM_RET_SUCCESS) |
| 238 | goto unlock; |
| 239 | |
| 240 | ret = pm_ipi_buff_read(proc, value); |
| 241 | |
| 242 | unlock: |
| 243 | bakery_lock_release(&pm_secure_lock); |
| 244 | |
| 245 | return ret; |
| 246 | } |