blob: 18ae0969f3e5d18569ad68c802d6393ea8d630c4 [file] [log] [blame]
Jolly Shahc2583ab2019-01-08 11:31:49 -08001/*
Michal Simek2a47faa2023-04-14 08:43:51 +02002 * Copyright (c) 2017-2020, Arm Limited and Contributors. All rights reserved.
Rajan Vaja34cce8e2022-08-31 12:54:40 +02003 * Copyright (c) 2020-2022, Xilinx, Inc. All rights reserved.
Devanshi Chauhan Alpeshbhaiee5a5d62025-03-26 01:50:27 -07004 * Copyright (c) 2022-2025, Advanced Micro Devices, Inc. All rights reserved.
Jolly Shahc2583ab2019-01-08 11:31:49 -08005 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9/*
10 * Xilinx IPI agent registers access management
11 */
12
13#include <errno.h>
14#include <string.h>
15
16#include <common/debug.h>
17#include <common/runtime_svc.h>
18#include <lib/bakery_lock.h>
19#include <lib/mmio.h>
20
21#include <ipi.h>
Jolly Shahc2583ab2019-01-08 11:31:49 -080022#include <plat_private.h>
Devanshi Chauhan Alpeshbhaiee5a5d62025-03-26 01:50:27 -070023#include "pm_defs.h"
Jolly Shahc2583ab2019-01-08 11:31:49 -080024
25/*********************************************************************
26 * Macros definitions
27 ********************************************************************/
28
29/* IPI registers offsets macros */
30#define IPI_TRIG_OFFSET 0x00U
31#define IPI_OBR_OFFSET 0x04U
32#define IPI_ISR_OFFSET 0x10U
33#define IPI_IMR_OFFSET 0x14U
34#define IPI_IER_OFFSET 0x18U
35#define IPI_IDR_OFFSET 0x1CU
36
37/* IPI register start offset */
38#define IPI_REG_BASE(I) (ipi_table[(I)].ipi_reg_base)
39
40/* IPI register bit mask */
41#define IPI_BIT_MASK(I) (ipi_table[(I)].ipi_bit_mask)
42
43/* IPI configuration table */
Boyan Karatotev05e9d4d2022-11-22 14:31:41 +000044static const struct ipi_config *ipi_table;
Jolly Shahc2583ab2019-01-08 11:31:49 -080045
46/* Total number of IPI */
47static uint32_t ipi_total;
48
49/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +053050 * ipi_config_table_init() - Initialize IPI configuration data.
51 * @ipi_config_table: IPI configuration table.
52 * @total_ipi: Total number of IPI available.
Jolly Shahc2583ab2019-01-08 11:31:49 -080053 *
54 */
55void ipi_config_table_init(const struct ipi_config *ipi_config_table,
56 uint32_t total_ipi)
57{
58 ipi_table = ipi_config_table;
59 ipi_total = total_ipi;
60}
61
Prasad Kummari7d0623a2023-06-09 14:32:00 +053062/**
63 * is_ipi_mb_within_range() - verify if IPI mailbox is within range.
64 * @local: local IPI ID.
65 * @remote: remote IPI ID.
Jolly Shahc2583ab2019-01-08 11:31:49 -080066 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +053067 * Return: - 1 if within range, 0 if not.
Jolly Shahc2583ab2019-01-08 11:31:49 -080068 *
Jolly Shahc2583ab2019-01-08 11:31:49 -080069 */
70static inline int is_ipi_mb_within_range(uint32_t local, uint32_t remote)
71{
72 int ret = 1;
73
Nithin Ge6c28532024-04-22 13:06:06 +053074 if ((remote >= ipi_total) || (local >= ipi_total)) {
Jolly Shahc2583ab2019-01-08 11:31:49 -080075 ret = 0;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +053076 }
Jolly Shahc2583ab2019-01-08 11:31:49 -080077
78 return ret;
79}
80
81/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +053082 * ipi_mb_validate() - validate IPI mailbox access.
83 * @local: local IPI ID.
84 * @remote: remote IPI ID.
85 * @is_secure: indicate if the requester is from secure software.
Jolly Shahc2583ab2019-01-08 11:31:49 -080086 *
Prasad Kummari7d0623a2023-06-09 14:32:00 +053087 * Return: 0 success, negative value for errors.
Jolly Shahc2583ab2019-01-08 11:31:49 -080088 *
Jolly Shahc2583ab2019-01-08 11:31:49 -080089 */
90int ipi_mb_validate(uint32_t local, uint32_t remote, unsigned int is_secure)
91{
92 int ret = 0;
93
Maheedhar Bollapalli9c3fc0b2024-04-24 12:53:28 +053094 if (is_ipi_mb_within_range(local, remote) == 0) {
Jolly Shahc2583ab2019-01-08 11:31:49 -080095 ret = -EINVAL;
Maheedhar Bollapalli9c3fc0b2024-04-24 12:53:28 +053096 } else if (IPI_IS_SECURE(local) && (is_secure == 0U)) {
Jolly Shahc2583ab2019-01-08 11:31:49 -080097 ret = -EPERM;
Maheedhar Bollapalli9c3fc0b2024-04-24 12:53:28 +053098 } else if (IPI_IS_SECURE(remote) && (is_secure == 0U)) {
Jolly Shahc2583ab2019-01-08 11:31:49 -080099 ret = -EPERM;
Venkatesh Yadav Abbarapuccf6da72022-05-04 14:23:32 +0530100 } else {
101 /* To fix the misra 15.7 warning */
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530102 }
Jolly Shahc2583ab2019-01-08 11:31:49 -0800103
104 return ret;
105}
106
107/**
108 * ipi_mb_open() - Open IPI mailbox.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530109 * @local: local IPI ID.
110 * @remote: remote IPI ID.
Jolly Shahc2583ab2019-01-08 11:31:49 -0800111 *
112 */
113void ipi_mb_open(uint32_t local, uint32_t remote)
114{
Maheedhar Bollapalli0ec699c2024-04-23 12:17:22 +0530115 uint64_t idr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_IDR_OFFSET);
116 uint64_t isr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_ISR_OFFSET);
117
118 mmio_write_32(idr_offset,
Jolly Shahc2583ab2019-01-08 11:31:49 -0800119 IPI_BIT_MASK(remote));
Maheedhar Bollapalli0ec699c2024-04-23 12:17:22 +0530120 mmio_write_32(isr_offset,
Jolly Shahc2583ab2019-01-08 11:31:49 -0800121 IPI_BIT_MASK(remote));
122}
123
124/**
125 * ipi_mb_release() - Open IPI mailbox.
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530126 * @local: local IPI ID.
127 * @remote: remote IPI ID.
Jolly Shahc2583ab2019-01-08 11:31:49 -0800128 *
129 */
130void ipi_mb_release(uint32_t local, uint32_t remote)
131{
Maheedhar Bollapalli0ec699c2024-04-23 12:17:22 +0530132 uint64_t idr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_IDR_OFFSET);
133
134 mmio_write_32(idr_offset,
Jolly Shahc2583ab2019-01-08 11:31:49 -0800135 IPI_BIT_MASK(remote));
136}
137
138/**
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530139 * ipi_mb_enquire_status() - Enquire IPI mailbox status.
140 * @local: local IPI ID.
141 * @remote: remote IPI ID.
Jolly Shahc2583ab2019-01-08 11:31:49 -0800142 *
Devanshi Chauhan Alpeshbhaib3d4c9f2025-03-26 02:08:58 -0700143 * Return: 0 idle and positive value for pending sending or receiving.
Jolly Shahc2583ab2019-01-08 11:31:49 -0800144 *
Jolly Shahc2583ab2019-01-08 11:31:49 -0800145 */
Devanshi Chauhan Alpeshbhaib3d4c9f2025-03-26 02:08:58 -0700146uint32_t ipi_mb_enquire_status(uint32_t local, uint32_t remote)
Jolly Shahc2583ab2019-01-08 11:31:49 -0800147{
Devanshi Chauhan Alpeshbhaib3d4c9f2025-03-26 02:08:58 -0700148 uint32_t ret = (uint32_t)PM_RET_SUCCESS;
Jolly Shahc2583ab2019-01-08 11:31:49 -0800149 uint32_t status;
Maheedhar Bollapalli0ec699c2024-04-23 12:17:22 +0530150 uint64_t obr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_OBR_OFFSET);
151 uint64_t isr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_ISR_OFFSET);
Jolly Shahc2583ab2019-01-08 11:31:49 -0800152
Maheedhar Bollapalli0ec699c2024-04-23 12:17:22 +0530153 status = mmio_read_32(obr_offset);
Maheedhar Bollapallia9cc0bd2024-04-22 15:27:02 +0530154 if ((status & IPI_BIT_MASK(remote)) != 0U) {
Jolly Shahc2583ab2019-01-08 11:31:49 -0800155 ret |= IPI_MB_STATUS_SEND_PENDING;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530156 }
Maheedhar Bollapalli0ec699c2024-04-23 12:17:22 +0530157 status = mmio_read_32(isr_offset);
Maheedhar Bollapallia9cc0bd2024-04-22 15:27:02 +0530158 if ((status & IPI_BIT_MASK(remote)) != 0U) {
Jolly Shahc2583ab2019-01-08 11:31:49 -0800159 ret |= IPI_MB_STATUS_RECV_PENDING;
Venkatesh Yadav Abbarapu987fad32022-04-29 13:52:00 +0530160 }
Jolly Shahc2583ab2019-01-08 11:31:49 -0800161
162 return ret;
163}
164
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530165/**
166 * ipi_mb_notify() - Trigger IPI mailbox notification.
167 * @local: local IPI ID.
168 * @remote: remote IPI ID.
169 * @is_blocking: if to trigger the notification in blocking mode or not.
Jolly Shahc2583ab2019-01-08 11:31:49 -0800170 *
171 * It sets the remote bit in the IPI agent trigger register.
172 *
173 */
Venkatesh Yadav Abbarapuefa1d322021-08-04 21:33:15 -0600174void ipi_mb_notify(uint32_t local, uint32_t remote, uint32_t is_blocking)
Jolly Shahc2583ab2019-01-08 11:31:49 -0800175{
176 uint32_t status;
Maheedhar Bollapalli0ec699c2024-04-23 12:17:22 +0530177 uint64_t trig_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_TRIG_OFFSET);
178 uint64_t obr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_OBR_OFFSET);
Jolly Shahc2583ab2019-01-08 11:31:49 -0800179
Maheedhar Bollapalli0ec699c2024-04-23 12:17:22 +0530180 mmio_write_32(trig_offset,
Jolly Shahc2583ab2019-01-08 11:31:49 -0800181 IPI_BIT_MASK(remote));
Maheedhar Bollapallia9cc0bd2024-04-22 15:27:02 +0530182 if (is_blocking != 0U) {
Jolly Shahc2583ab2019-01-08 11:31:49 -0800183 do {
Maheedhar Bollapalli0ec699c2024-04-23 12:17:22 +0530184 status = mmio_read_32(obr_offset);
Maheedhar Bollapallia9cc0bd2024-04-22 15:27:02 +0530185 } while ((status & IPI_BIT_MASK(remote)) != 0U);
Jolly Shahc2583ab2019-01-08 11:31:49 -0800186 }
Jolly Shahc2583ab2019-01-08 11:31:49 -0800187}
188
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530189/**
190 * ipi_mb_ack() - Ack IPI mailbox notification from the other end.
191 * @local: local IPI ID.
192 * @remote: remote IPI ID.
Jolly Shahc2583ab2019-01-08 11:31:49 -0800193 *
194 * It will clear the remote bit in the isr register.
195 *
196 */
197void ipi_mb_ack(uint32_t local, uint32_t remote)
198{
Maheedhar Bollapalli0ec699c2024-04-23 12:17:22 +0530199 uint64_t isr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_ISR_OFFSET);
200
201 mmio_write_32(isr_offset,
Jolly Shahc2583ab2019-01-08 11:31:49 -0800202 IPI_BIT_MASK(remote));
203}
204
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530205/**
206 * ipi_mb_disable_irq() - Disable IPI mailbox notification interrupt.
207 * @local: local IPI ID.
208 * @remote: remote IPI ID.
Jolly Shahc2583ab2019-01-08 11:31:49 -0800209 *
210 * It will mask the remote bit in the idr register.
211 *
212 */
213void ipi_mb_disable_irq(uint32_t local, uint32_t remote)
214{
Maheedhar Bollapalli0ec699c2024-04-23 12:17:22 +0530215 uint64_t idr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_IDR_OFFSET);
216
217 mmio_write_32(idr_offset,
Jolly Shahc2583ab2019-01-08 11:31:49 -0800218 IPI_BIT_MASK(remote));
219}
220
Prasad Kummari7d0623a2023-06-09 14:32:00 +0530221/**
222 * ipi_mb_enable_irq() - Enable IPI mailbox notification interrupt.
223 * @local: local IPI ID.
224 * @remote: remote IPI ID.
Jolly Shahc2583ab2019-01-08 11:31:49 -0800225 *
226 * It will mask the remote bit in the idr register.
227 *
228 */
229void ipi_mb_enable_irq(uint32_t local, uint32_t remote)
230{
Maheedhar Bollapalli0ec699c2024-04-23 12:17:22 +0530231 uint64_t ier_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_IER_OFFSET);
232
233 mmio_write_32(ier_offset,
Jolly Shahc2583ab2019-01-08 11:31:49 -0800234 IPI_BIT_MASK(remote));
235}