blob: 034cd5bc8ce46a0e98ced735fb140d6a188177cd [file] [log] [blame]
Soren Brinkmann76fcae32016-03-06 20:16:27 -08001/*
Jolly Shah0bfd7002019-01-08 11:10:47 -08002 * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
Soren Brinkmann76fcae32016-03-06 20:16:27 -08003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soren Brinkmann76fcae32016-03-06 20:16:27 -08005 */
6
Jolly Shah259afe52019-01-09 12:37:57 -08007
Isla Mitchelle3631462017-07-14 10:46:32 +01008#include <arch_helpers.h>
Jolly Shah259afe52019-01-09 12:37:57 -08009
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <lib/bakery_lock.h>
11#include <lib/mmio.h>
Jolly Shahc2583ab2019-01-08 11:31:49 -080012
13#include <ipi.h>
Jolly Shah4c172372019-01-08 11:21:29 -080014#include <plat_ipi.h>
Jolly Shah0bfd7002019-01-08 11:10:47 -080015#include <plat_private.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <plat/common/platform.h>
17
Isla Mitchelle3631462017-07-14 10:46:32 +010018#include "pm_ipi.h"
Soren Brinkmann76fcae32016-03-06 20:16:27 -080019
Tejas Patelaf4b10e2018-02-09 02:42:59 -080020
Stefan Krsmanovicc5e9f662016-05-20 15:51:08 +020021DEFINE_BAKERY_LOCK(pm_secure_lock);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080022
Soren Brinkmann76fcae32016-03-06 20:16:27 -080023/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080024 * pm_ipi_init() - Initialize IPI peripheral for communication with
25 * remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -080026 *
Wendy Liang328105c2017-10-03 23:21:11 -070027 * @proc Pointer to the processor who is initiating request
Soren Brinkmann76fcae32016-03-06 20:16:27 -080028 * @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 Brinkmann76fcae32016-03-06 20:16:27 -080032 * Called from pm_setup initialization function
33 */
Wendy Liang328105c2017-10-03 23:21:11 -070034int pm_ipi_init(const struct pm_proc *proc)
Soren Brinkmann76fcae32016-03-06 20:16:27 -080035{
36 bakery_lock_init(&pm_secure_lock);
Jolly Shah36c930b2019-01-07 12:51:40 -080037 ipi_mb_open(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080038
39 return 0;
40}
41
42/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080043 * pm_ipi_send_common() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -080044 * @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 */
52static enum pm_ret_status pm_ipi_send_common(const struct pm_proc *proc,
Tejas Patelaf4b10e2018-02-09 02:42:59 -080053 uint32_t payload[PAYLOAD_ARG_CNT],
54 uint32_t is_blocking)
Soren Brinkmann76fcae32016-03-06 20:16:27 -080055{
56 unsigned int offset = 0;
57 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -080058 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -080059 IPI_BUFFER_REQ_OFFSET;
60
Soren Brinkmann76fcae32016-03-06 20:16:27 -080061 /* 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 Patelaf4b10e2018-02-09 02:42:59 -080066
Jolly Shah9bb628d2019-01-07 12:53:32 -080067 /* Generate IPI to remote processor */
Jolly Shah36c930b2019-01-07 12:51:40 -080068 ipi_mb_notify(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id,
Tejas Patelaf4b10e2018-02-09 02:42:59 -080069 is_blocking);
Soren Brinkmann76fcae32016-03-06 20:16:27 -080070
71 return PM_RET_SUCCESS;
72}
73
74/**
Jolly Shah9bb628d2019-01-07 12:53:32 -080075 * pm_ipi_send_non_blocking() - Sends IPI request to the remote processor
76 * without blocking notification
Tejas Patelaf4b10e2018-02-09 02:42:59 -080077 * @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 */
84enum 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 Shah9bb628d2019-01-07 12:53:32 -080099 * pm_ipi_send() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800100 * @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 */
107enum 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 Patelaf4b10e2018-02-09 02:42:59 -0800114 ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800115
116 bakery_lock_release(&pm_secure_lock);
117
118 return ret;
119}
120
121
122/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800123 * pm_ipi_buff_read() - Reads IPI response after remote processor has handled
124 * interrupt
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800125 * @proc Pointer to the processor who is waiting and reading response
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700126 * @value Used to return value from IPI buffer element (optional)
127 * @count Number of values to return in @value
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800128 *
129 * @return Returns status, either success or error+reason
130 */
131static enum pm_ret_status pm_ipi_buff_read(const struct pm_proc *proc,
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700132 unsigned int *value, size_t count)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800133{
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700134 size_t i;
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800135 uintptr_t buffer_base = proc->ipi->buffer_base +
Jolly Shah9bb628d2019-01-07 12:53:32 -0800136 IPI_BUFFER_TARGET_REMOTE_OFFSET +
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800137 IPI_BUFFER_RESP_OFFSET;
138
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800139 /*
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 Brinkmannd6c9e032016-09-22 11:35:47 -0700146 for (i = 1; i <= count; i++) {
147 *value = mmio_read_32(buffer_base + (i * PAYLOAD_ARG_SIZE));
148 value++;
149 }
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800150
151 return mmio_read_32(buffer_base);
152}
153
154/**
Jolly Shah9bb628d2019-01-07 12:53:32 -0800155 * pm_ipi_buff_read_callb() - Reads IPI response after remote processor has
156 * handled interrupt
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700157 * @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 */
162void pm_ipi_buff_read_callb(unsigned int *value, size_t count)
163{
164 size_t i;
Jolly Shah9bb628d2019-01-07 12:53:32 -0800165 uintptr_t buffer_base = IPI_BUFFER_REMOTE_BASE +
166 IPI_BUFFER_TARGET_LOCAL_OFFSET +
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700167 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 Shah9bb628d2019-01-07 12:53:32 -0800179 * pm_ipi_send_sync() - Sends IPI request to the remote processor
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800180 * @proc Pointer to the processor who is initiating request
181 * @payload API id and call arguments to be written in IPI buffer
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700182 * @value Used to return value from IPI buffer element (optional)
183 * @count Number of values to return in @value
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800184 *
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 */
190enum pm_ret_status pm_ipi_send_sync(const struct pm_proc *proc,
191 uint32_t payload[PAYLOAD_ARG_CNT],
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700192 unsigned int *value, size_t count)
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800193{
194 enum pm_ret_status ret;
195
196 bakery_lock_get(&pm_secure_lock);
197
Tejas Patelaf4b10e2018-02-09 02:42:59 -0800198 ret = pm_ipi_send_common(proc, payload, IPI_BLOCKING);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800199 if (ret != PM_RET_SUCCESS)
200 goto unlock;
201
Soren Brinkmannd6c9e032016-09-22 11:35:47 -0700202 ret = pm_ipi_buff_read(proc, value, count);
Soren Brinkmann76fcae32016-03-06 20:16:27 -0800203
204unlock:
205 bakery_lock_release(&pm_secure_lock);
206
207 return ret;
208}
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700209
Wendy Liang328105c2017-10-03 23:21:11 -0700210void pm_ipi_irq_enable(const struct pm_proc *proc)
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700211{
Jolly Shah36c930b2019-01-07 12:51:40 -0800212 ipi_mb_enable_irq(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmanna1b0a902016-09-30 11:30:21 -0700213}
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700214
Wendy Liang328105c2017-10-03 23:21:11 -0700215void pm_ipi_irq_clear(const struct pm_proc *proc)
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700216{
Jolly Shah36c930b2019-01-07 12:51:40 -0800217 ipi_mb_ack(proc->ipi->local_ipi_id, proc->ipi->remote_ipi_id);
Soren Brinkmann84f0af42016-09-30 14:24:25 -0700218}
Jolly Shah259afe52019-01-09 12:37:57 -0800219
220uint32_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}