blob: 2f52f38f33895bf163e6eae1ab31b1ea0a73916f [file] [log] [blame]
Jolly Shahc2583ab2019-01-08 11:31:49 -08001/*
2 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/*
8 * Xilinx IPI agent registers access management
9 */
10
11#include <errno.h>
12#include <string.h>
13
14#include <common/debug.h>
15#include <common/runtime_svc.h>
16#include <lib/bakery_lock.h>
17#include <lib/mmio.h>
18
19#include <ipi.h>
20#include <plat_ipi.h>
21#include <plat_private.h>
22
23/*********************************************************************
24 * Macros definitions
25 ********************************************************************/
26
27/* IPI registers offsets macros */
28#define IPI_TRIG_OFFSET 0x00U
29#define IPI_OBR_OFFSET 0x04U
30#define IPI_ISR_OFFSET 0x10U
31#define IPI_IMR_OFFSET 0x14U
32#define IPI_IER_OFFSET 0x18U
33#define IPI_IDR_OFFSET 0x1CU
34
35/* IPI register start offset */
36#define IPI_REG_BASE(I) (ipi_table[(I)].ipi_reg_base)
37
38/* IPI register bit mask */
39#define IPI_BIT_MASK(I) (ipi_table[(I)].ipi_bit_mask)
40
41/* IPI configuration table */
42const static struct ipi_config *ipi_table;
43
44/* Total number of IPI */
45static uint32_t ipi_total;
46
47/**
48 * ipi_config_init() - Initialize IPI configuration data
49 *
50 * @ipi_config_table - IPI configuration table
51 * @ipi_total - Total number of IPI available
52 *
53 */
54void ipi_config_table_init(const struct ipi_config *ipi_config_table,
55 uint32_t total_ipi)
56{
57 ipi_table = ipi_config_table;
58 ipi_total = total_ipi;
59}
60
61/* is_ipi_mb_within_range() - verify if IPI mailbox is within range
62 *
63 * @local - local IPI ID
64 * @remote - remote IPI ID
65 *
66 * return - 1 if within range, 0 if not
67 */
68static inline int is_ipi_mb_within_range(uint32_t local, uint32_t remote)
69{
70 int ret = 1;
71
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +053072 if (remote >= ipi_total || local >= ipi_total) {
Jolly Shahc2583ab2019-01-08 11:31:49 -080073 ret = 0;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +053074 }
Jolly Shahc2583ab2019-01-08 11:31:49 -080075
76 return ret;
77}
78
79/**
80 * ipi_mb_validate() - validate IPI mailbox access
81 *
82 * @local - local IPI ID
83 * @remote - remote IPI ID
84 * @is_secure - indicate if the requester is from secure software
85 *
86 * return - 0 success, negative value for errors
87 */
88int ipi_mb_validate(uint32_t local, uint32_t remote, unsigned int is_secure)
89{
90 int ret = 0;
91
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +053092 if (!is_ipi_mb_within_range(local, remote)) {
Jolly Shahc2583ab2019-01-08 11:31:49 -080093 ret = -EINVAL;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +053094 } else if (IPI_IS_SECURE(local) && !is_secure) {
Jolly Shahc2583ab2019-01-08 11:31:49 -080095 ret = -EPERM;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +053096 } else if (IPI_IS_SECURE(remote) && !is_secure) {
Jolly Shahc2583ab2019-01-08 11:31:49 -080097 ret = -EPERM;
Venkatesh Yadav Abbarapuccf6da72022-05-04 14:23:32 +053098 } else {
99 /* To fix the misra 15.7 warning */
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530100 }
Jolly Shahc2583ab2019-01-08 11:31:49 -0800101
102 return ret;
103}
104
105/**
106 * ipi_mb_open() - Open IPI mailbox.
107 *
108 * @local - local IPI ID
109 * @remote - remote IPI ID
110 *
111 */
112void ipi_mb_open(uint32_t local, uint32_t remote)
113{
114 mmio_write_32(IPI_REG_BASE(local) + IPI_IDR_OFFSET,
115 IPI_BIT_MASK(remote));
116 mmio_write_32(IPI_REG_BASE(local) + IPI_ISR_OFFSET,
117 IPI_BIT_MASK(remote));
118}
119
120/**
121 * ipi_mb_release() - Open IPI mailbox.
122 *
123 * @local - local IPI ID
124 * @remote - remote IPI ID
125 *
126 */
127void ipi_mb_release(uint32_t local, uint32_t remote)
128{
129 mmio_write_32(IPI_REG_BASE(local) + IPI_IDR_OFFSET,
130 IPI_BIT_MASK(remote));
131}
132
133/**
134 * ipi_mb_enquire_status() - Enquire IPI mailbox status
135 *
136 * @local - local IPI ID
137 * @remote - remote IPI ID
138 *
139 * return - 0 idle, positive value for pending sending or receiving,
140 * negative value for errors
141 */
142int ipi_mb_enquire_status(uint32_t local, uint32_t remote)
143{
144 int ret = 0;
145 uint32_t status;
146
147 status = mmio_read_32(IPI_REG_BASE(local) + IPI_OBR_OFFSET);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530148 if (status & IPI_BIT_MASK(remote)) {
Jolly Shahc2583ab2019-01-08 11:31:49 -0800149 ret |= IPI_MB_STATUS_SEND_PENDING;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530150 }
Jolly Shahc2583ab2019-01-08 11:31:49 -0800151 status = mmio_read_32(IPI_REG_BASE(local) + IPI_ISR_OFFSET);
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530152 if (status & IPI_BIT_MASK(remote)) {
Jolly Shahc2583ab2019-01-08 11:31:49 -0800153 ret |= IPI_MB_STATUS_RECV_PENDING;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530154 }
Jolly Shahc2583ab2019-01-08 11:31:49 -0800155
156 return ret;
157}
158
159/* ipi_mb_notify() - Trigger IPI mailbox notification
160 *
161 * @local - local IPI ID
162 * @remote - remote IPI ID
163 * @is_blocking - if to trigger the notification in blocking mode or not.
164 *
165 * It sets the remote bit in the IPI agent trigger register.
166 *
167 */
Venkatesh Yadav Abbarapuefa1d322021-08-04 21:33:15 -0600168void ipi_mb_notify(uint32_t local, uint32_t remote, uint32_t is_blocking)
Jolly Shahc2583ab2019-01-08 11:31:49 -0800169{
170 uint32_t status;
171
172 mmio_write_32(IPI_REG_BASE(local) + IPI_TRIG_OFFSET,
173 IPI_BIT_MASK(remote));
174 if (is_blocking) {
175 do {
176 status = mmio_read_32(IPI_REG_BASE(local) +
177 IPI_OBR_OFFSET);
178 } while (status & IPI_BIT_MASK(remote));
179 }
Jolly Shahc2583ab2019-01-08 11:31:49 -0800180}
181
182/* ipi_mb_ack() - Ack IPI mailbox notification from the other end
183 *
184 * @local - local IPI ID
185 * @remote - remote IPI ID
186 *
187 * It will clear the remote bit in the isr register.
188 *
189 */
190void ipi_mb_ack(uint32_t local, uint32_t remote)
191{
192 mmio_write_32(IPI_REG_BASE(local) + IPI_ISR_OFFSET,
193 IPI_BIT_MASK(remote));
194}
195
196/* ipi_mb_disable_irq() - Disable IPI mailbox notification interrupt
197 *
198 * @local - local IPI ID
199 * @remote - remote IPI ID
200 *
201 * It will mask the remote bit in the idr register.
202 *
203 */
204void ipi_mb_disable_irq(uint32_t local, uint32_t remote)
205{
206 mmio_write_32(IPI_REG_BASE(local) + IPI_IDR_OFFSET,
207 IPI_BIT_MASK(remote));
208}
209
210/* ipi_mb_enable_irq() - Enable IPI mailbox notification interrupt
211 *
212 * @local - local IPI ID
213 * @remote - remote IPI ID
214 *
215 * It will mask the remote bit in the idr register.
216 *
217 */
218void ipi_mb_enable_irq(uint32_t local, uint32_t remote)
219{
220 mmio_write_32(IPI_REG_BASE(local) + IPI_IER_OFFSET,
221 IPI_BIT_MASK(remote));
222}