blob: 6648fec732b6bd05ae7628a4d31c94fb458e6b9e [file] [log] [blame]
Soren Brinkmannc8284402016-03-06 20:16:27 -08001/*
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 Brinkmann0a48e2b2016-04-11 15:30:56 -070069#define IPI_APU_IXR_PMU_0_MASK (1 << 16)
Soren Brinkmannc8284402016-03-06 20:16:27 -080070
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 Brinkmann0a48e2b2016-04-11 15:30:56 -070077#define IPI_PMU_PM_INT_MASK (IPI_APU_IXR_PMU_0_MASK << PM_INT_NUM)
Soren Brinkmannc8284402016-03-06 20:16:27 -080078#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 Krsmanovic8212f1f2016-05-20 15:51:08 +020084DEFINE_BAKERY_LOCK(pm_secure_lock);
Soren Brinkmannc8284402016-03-06 20:16:27 -080085
86const 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 */
102int 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 */
117static 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 */
142static 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 */
173enum 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
Soren Brinkmanndc0c5a42016-09-22 11:35:47 -0700191 * @value Used to return value from IPI buffer element (optional)
192 * @count Number of values to return in @value
Soren Brinkmannc8284402016-03-06 20:16:27 -0800193 *
194 * @return Returns status, either success or error+reason
195 */
196static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc,
Soren Brinkmanndc0c5a42016-09-22 11:35:47 -0700197 unsigned int *value, size_t count)
Soren Brinkmannc8284402016-03-06 20:16:27 -0800198{
Soren Brinkmanndc0c5a42016-09-22 11:35:47 -0700199 size_t i;
Soren Brinkmannc8284402016-03-06 20:16:27 -0800200 uintptr_t buffer_base = proc->ipi->buffer_base +
201 IPI_BUFFER_TARGET_PMU_OFFSET +
202 IPI_BUFFER_RESP_OFFSET;
203
204 pm_ipi_wait(proc);
205
206 /*
207 * Read response from IPI buffer
208 * buf-0: success or error+reason
209 * buf-1: value
210 * buf-2: unused
211 * buf-3: unused
212 */
Soren Brinkmanndc0c5a42016-09-22 11:35:47 -0700213 for (i = 1; i <= count; i++) {
214 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
215 value++;
216 }
Soren Brinkmannc8284402016-03-06 20:16:27 -0800217
218 return mmio_read_32(buffer_base);
219}
220
221/**
222 * pm_ipi_send_sync() - Sends IPI request to the PMU
223 * @proc Pointer to the processor who is initiating request
224 * @payload API id and call arguments to be written in IPI buffer
Soren Brinkmanndc0c5a42016-09-22 11:35:47 -0700225 * @value Used to return value from IPI buffer element (optional)
226 * @count Number of values to return in @value
Soren Brinkmannc8284402016-03-06 20:16:27 -0800227 *
228 * Send an IPI request to the power controller and wait for it to be handled.
229 *
230 * @return Returns status, either success or error+reason and, optionally,
231 * @value
232 */
233enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc,
234 uint32_t payload[PAYLOAD_ARG_CNT],
Soren Brinkmanndc0c5a42016-09-22 11:35:47 -0700235 unsigned int *value, size_t count)
Soren Brinkmannc8284402016-03-06 20:16:27 -0800236{
237 enum pm_ret_status ret;
238
239 bakery_lock_get(&pm_secure_lock);
240
241 ret = pm_ipi_send_common(proc, payload);
242 if (ret != PM_RET_SUCCESS)
243 goto unlock;
244
Soren Brinkmanndc0c5a42016-09-22 11:35:47 -0700245 ret = pm_ipi_buff_read(proc, value, count);
Soren Brinkmannc8284402016-03-06 20:16:27 -0800246
247unlock:
248 bakery_lock_release(&pm_secure_lock);
249
250 return ret;
251}