Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 1 | /* |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 2 | * Copyright (c) 2013-2017, 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 | |
Isla Mitchell | e363146 | 2017-07-14 10:46:32 +0100 | [diff] [blame] | 7 | #include <arch_helpers.h> |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 8 | #include <bakery_lock.h> |
| 9 | #include <mmio.h> |
| 10 | #include <platform.h> |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 11 | #include "../zynqmp_ipi.h" |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 12 | #include "../zynqmp_private.h" |
Isla Mitchell | e363146 | 2017-07-14 10:46:32 +0100 | [diff] [blame] | 13 | #include "pm_ipi.h" |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 14 | |
| 15 | /* IPI message buffers */ |
| 16 | #define IPI_BUFFER_BASEADDR 0xFF990000U |
| 17 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 18 | #define IPI_BUFFER_APU_BASE (IPI_BUFFER_BASEADDR + 0x400U) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 19 | #define IPI_BUFFER_PMU_BASE (IPI_BUFFER_BASEADDR + 0xE00U) |
| 20 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 21 | #define IPI_BUFFER_TARGET_APU_OFFSET 0x80U |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 22 | #define IPI_BUFFER_TARGET_PMU_OFFSET 0x1C0U |
| 23 | |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 24 | #define IPI_BUFFER_MAX_WORDS 8 |
| 25 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 26 | #define IPI_BUFFER_REQ_OFFSET 0x0U |
| 27 | #define IPI_BUFFER_RESP_OFFSET 0x20U |
| 28 | |
Stefan Krsmanovic | c5e9f66 | 2016-05-20 15:51:08 +0200 | [diff] [blame] | 29 | DEFINE_BAKERY_LOCK(pm_secure_lock); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 30 | |
| 31 | const struct pm_ipi apu_ipi = { |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 32 | .apu_ipi_id = IPI_ID_APU, |
| 33 | .pmu_ipi_id = IPI_ID_PMU0, |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 34 | .buffer_base = IPI_BUFFER_APU_BASE, |
| 35 | }; |
| 36 | |
| 37 | /** |
| 38 | * pm_ipi_init() - Initialize IPI peripheral for communication with PMU |
| 39 | * |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 40 | * @proc Pointer to the processor who is initiating request |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 41 | * @return On success, the initialization function must return 0. |
| 42 | * Any other return value will cause the framework to ignore |
| 43 | * the service |
| 44 | * |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 45 | * Called from pm_setup initialization function |
| 46 | */ |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 47 | int pm_ipi_init(const struct pm_proc *proc) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 48 | { |
| 49 | bakery_lock_init(&pm_secure_lock); |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 50 | ipi_mb_open(proc->ipi->apu_ipi_id, proc->ipi->pmu_ipi_id); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | /** |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 56 | * pm_ipi_send_common() - Sends IPI request to the PMU |
| 57 | * @proc Pointer to the processor who is initiating request |
| 58 | * @payload API id and call arguments to be written in IPI buffer |
| 59 | * |
| 60 | * Send an IPI request to the power controller. Caller needs to hold |
| 61 | * the 'pm_secure_lock' lock. |
| 62 | * |
| 63 | * @return Returns status, either success or error+reason |
| 64 | */ |
| 65 | static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc, |
| 66 | uint32_t payload[PAYLOAD_ARG_CNT]) |
| 67 | { |
| 68 | unsigned int offset = 0; |
| 69 | uintptr_t buffer_base = proc->ipi->buffer_base + |
| 70 | IPI_BUFFER_TARGET_PMU_OFFSET + |
| 71 | IPI_BUFFER_REQ_OFFSET; |
| 72 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 73 | /* Write payload into IPI buffer */ |
| 74 | for (size_t i = 0; i < PAYLOAD_ARG_CNT; i++) { |
| 75 | mmio_write_32(buffer_base + offset, payload[i]); |
| 76 | offset += PAYLOAD_ARG_SIZE; |
| 77 | } |
| 78 | /* Generate IPI to PMU */ |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 79 | ipi_mb_notify(proc->ipi->apu_ipi_id, proc->ipi->pmu_ipi_id, 1); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 80 | |
| 81 | return PM_RET_SUCCESS; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * pm_ipi_send() - Sends IPI request to the PMU |
| 86 | * @proc Pointer to the processor who is initiating request |
| 87 | * @payload API id and call arguments to be written in IPI buffer |
| 88 | * |
| 89 | * Send an IPI request to the power controller. |
| 90 | * |
| 91 | * @return Returns status, either success or error+reason |
| 92 | */ |
| 93 | enum pm_ret_status pm_ipi_send(const struct pm_proc *proc, |
| 94 | uint32_t payload[PAYLOAD_ARG_CNT]) |
| 95 | { |
| 96 | enum pm_ret_status ret; |
| 97 | |
| 98 | bakery_lock_get(&pm_secure_lock); |
| 99 | |
| 100 | ret = pm_ipi_send_common(proc, payload); |
| 101 | |
| 102 | bakery_lock_release(&pm_secure_lock); |
| 103 | |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * pm_ipi_buff_read() - Reads IPI response after PMU has handled interrupt |
| 110 | * @proc Pointer to the processor who is waiting and reading response |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 111 | * @value Used to return value from IPI buffer element (optional) |
| 112 | * @count Number of values to return in @value |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 113 | * |
| 114 | * @return Returns status, either success or error+reason |
| 115 | */ |
| 116 | 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] | 117 | unsigned int *value, size_t count) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 118 | { |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 119 | size_t i; |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 120 | uintptr_t buffer_base = proc->ipi->buffer_base + |
| 121 | IPI_BUFFER_TARGET_PMU_OFFSET + |
| 122 | IPI_BUFFER_RESP_OFFSET; |
| 123 | |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 124 | /* |
| 125 | * Read response from IPI buffer |
| 126 | * buf-0: success or error+reason |
| 127 | * buf-1: value |
| 128 | * buf-2: unused |
| 129 | * buf-3: unused |
| 130 | */ |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 131 | for (i = 1; i <= count; i++) { |
| 132 | *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE)); |
| 133 | value++; |
| 134 | } |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 135 | |
| 136 | return mmio_read_32(buffer_base); |
| 137 | } |
| 138 | |
| 139 | /** |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 140 | * pm_ipi_buff_read_callb() - Reads IPI response after PMU has handled interrupt |
| 141 | * @value Used to return value from IPI buffer element (optional) |
| 142 | * @count Number of values to return in @value |
| 143 | * |
| 144 | * @return Returns status, either success or error+reason |
| 145 | */ |
| 146 | void pm_ipi_buff_read_callb(unsigned int *value, size_t count) |
| 147 | { |
| 148 | size_t i; |
| 149 | uintptr_t buffer_base = IPI_BUFFER_PMU_BASE + |
| 150 | IPI_BUFFER_TARGET_APU_OFFSET + |
| 151 | IPI_BUFFER_REQ_OFFSET; |
| 152 | |
| 153 | if (count > IPI_BUFFER_MAX_WORDS) |
| 154 | count = IPI_BUFFER_MAX_WORDS; |
| 155 | |
| 156 | for (i = 0; i <= count; i++) { |
| 157 | *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE)); |
| 158 | value++; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 163 | * pm_ipi_send_sync() - Sends IPI request to the PMU |
| 164 | * @proc Pointer to the processor who is initiating request |
| 165 | * @payload API id and call arguments to be written in IPI buffer |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 166 | * @value Used to return value from IPI buffer element (optional) |
| 167 | * @count Number of values to return in @value |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 168 | * |
| 169 | * Send an IPI request to the power controller and wait for it to be handled. |
| 170 | * |
| 171 | * @return Returns status, either success or error+reason and, optionally, |
| 172 | * @value |
| 173 | */ |
| 174 | enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc, |
| 175 | uint32_t payload[PAYLOAD_ARG_CNT], |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 176 | unsigned int *value, size_t count) |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 177 | { |
| 178 | enum pm_ret_status ret; |
| 179 | |
| 180 | bakery_lock_get(&pm_secure_lock); |
| 181 | |
| 182 | ret = pm_ipi_send_common(proc, payload); |
| 183 | if (ret != PM_RET_SUCCESS) |
| 184 | goto unlock; |
| 185 | |
Soren Brinkmann | d6c9e03 | 2016-09-22 11:35:47 -0700 | [diff] [blame] | 186 | ret = pm_ipi_buff_read(proc, value, count); |
Soren Brinkmann | 76fcae3 | 2016-03-06 20:16:27 -0800 | [diff] [blame] | 187 | |
| 188 | unlock: |
| 189 | bakery_lock_release(&pm_secure_lock); |
| 190 | |
| 191 | return ret; |
| 192 | } |
Soren Brinkmann | a1b0a90 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 193 | |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 194 | void pm_ipi_irq_enable(const struct pm_proc *proc) |
Soren Brinkmann | a1b0a90 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 195 | { |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 196 | ipi_mb_enable_irq(proc->ipi->apu_ipi_id, proc->ipi->pmu_ipi_id); |
Soren Brinkmann | a1b0a90 | 2016-09-30 11:30:21 -0700 | [diff] [blame] | 197 | } |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 198 | |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 199 | void pm_ipi_irq_clear(const struct pm_proc *proc) |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 200 | { |
Wendy Liang | 328105c | 2017-10-03 23:21:11 -0700 | [diff] [blame] | 201 | ipi_mb_ack(proc->ipi->apu_ipi_id, proc->ipi->pmu_ipi_id); |
Soren Brinkmann | 84f0af4 | 2016-09-30 14:24:25 -0700 | [diff] [blame] | 202 | } |