Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 1 | /* |
Michal Simek | 2a47faa | 2023-04-14 08:43:51 +0200 | [diff] [blame] | 2 | * Copyright (c) 2017-2020, Arm Limited and Contributors. All rights reserved. |
Rajan Vaja | 34cce8e | 2022-08-31 12:54:40 +0200 | [diff] [blame] | 3 | * Copyright (c) 2020-2022, Xilinx, Inc. All rights reserved. |
Devanshi Chauhan Alpeshbhai | ee5a5d6 | 2025-03-26 01:50:27 -0700 | [diff] [blame^] | 4 | * Copyright (c) 2022-2025, Advanced Micro Devices, Inc. All rights reserved. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 5 | * |
| 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 Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 22 | #include <plat_private.h> |
Devanshi Chauhan Alpeshbhai | ee5a5d6 | 2025-03-26 01:50:27 -0700 | [diff] [blame^] | 23 | #include "pm_defs.h" |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 24 | |
| 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 Karatotev | 05e9d4d | 2022-11-22 14:31:41 +0000 | [diff] [blame] | 44 | static const struct ipi_config *ipi_table; |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 45 | |
| 46 | /* Total number of IPI */ |
| 47 | static uint32_t ipi_total; |
| 48 | |
| 49 | /** |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 50 | * ipi_config_table_init() - Initialize IPI configuration data. |
| 51 | * @ipi_config_table: IPI configuration table. |
| 52 | * @total_ipi: Total number of IPI available. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 53 | * |
| 54 | */ |
| 55 | void 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 Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 62 | /** |
| 63 | * is_ipi_mb_within_range() - verify if IPI mailbox is within range. |
| 64 | * @local: local IPI ID. |
| 65 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 66 | * |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 67 | * Return: - 1 if within range, 0 if not. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 68 | * |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 69 | */ |
| 70 | static inline int is_ipi_mb_within_range(uint32_t local, uint32_t remote) |
| 71 | { |
| 72 | int ret = 1; |
| 73 | |
Nithin G | e6c2853 | 2024-04-22 13:06:06 +0530 | [diff] [blame] | 74 | if ((remote >= ipi_total) || (local >= ipi_total)) { |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 75 | ret = 0; |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 76 | } |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | /** |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 82 | * 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 Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 86 | * |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 87 | * Return: 0 success, negative value for errors. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 88 | * |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 89 | */ |
| 90 | int ipi_mb_validate(uint32_t local, uint32_t remote, unsigned int is_secure) |
| 91 | { |
| 92 | int ret = 0; |
| 93 | |
Maheedhar Bollapalli | 9c3fc0b | 2024-04-24 12:53:28 +0530 | [diff] [blame] | 94 | if (is_ipi_mb_within_range(local, remote) == 0) { |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 95 | ret = -EINVAL; |
Maheedhar Bollapalli | 9c3fc0b | 2024-04-24 12:53:28 +0530 | [diff] [blame] | 96 | } else if (IPI_IS_SECURE(local) && (is_secure == 0U)) { |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 97 | ret = -EPERM; |
Maheedhar Bollapalli | 9c3fc0b | 2024-04-24 12:53:28 +0530 | [diff] [blame] | 98 | } else if (IPI_IS_SECURE(remote) && (is_secure == 0U)) { |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 99 | ret = -EPERM; |
Venkatesh Yadav Abbarapu | ccf6da7 | 2022-05-04 14:23:32 +0530 | [diff] [blame] | 100 | } else { |
| 101 | /* To fix the misra 15.7 warning */ |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 102 | } |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 103 | |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * ipi_mb_open() - Open IPI mailbox. |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 109 | * @local: local IPI ID. |
| 110 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 111 | * |
| 112 | */ |
| 113 | void ipi_mb_open(uint32_t local, uint32_t remote) |
| 114 | { |
Maheedhar Bollapalli | 0ec699c | 2024-04-23 12:17:22 +0530 | [diff] [blame] | 115 | 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 Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 119 | IPI_BIT_MASK(remote)); |
Maheedhar Bollapalli | 0ec699c | 2024-04-23 12:17:22 +0530 | [diff] [blame] | 120 | mmio_write_32(isr_offset, |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 121 | IPI_BIT_MASK(remote)); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * ipi_mb_release() - Open IPI mailbox. |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 126 | * @local: local IPI ID. |
| 127 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 128 | * |
| 129 | */ |
| 130 | void ipi_mb_release(uint32_t local, uint32_t remote) |
| 131 | { |
Maheedhar Bollapalli | 0ec699c | 2024-04-23 12:17:22 +0530 | [diff] [blame] | 132 | uint64_t idr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_IDR_OFFSET); |
| 133 | |
| 134 | mmio_write_32(idr_offset, |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 135 | IPI_BIT_MASK(remote)); |
| 136 | } |
| 137 | |
| 138 | /** |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 139 | * ipi_mb_enquire_status() - Enquire IPI mailbox status. |
| 140 | * @local: local IPI ID. |
| 141 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 142 | * |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 143 | * Return: 0 idle, positive value for pending sending or receiving, |
| 144 | * negative value for errors. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 145 | * |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 146 | */ |
| 147 | int ipi_mb_enquire_status(uint32_t local, uint32_t remote) |
| 148 | { |
Devanshi Chauhan Alpeshbhai | ee5a5d6 | 2025-03-26 01:50:27 -0700 | [diff] [blame^] | 149 | int ret = (int)PM_RET_SUCCESS; |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 150 | uint32_t status; |
Maheedhar Bollapalli | 0ec699c | 2024-04-23 12:17:22 +0530 | [diff] [blame] | 151 | uint64_t obr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_OBR_OFFSET); |
| 152 | uint64_t isr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_ISR_OFFSET); |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 153 | |
Maheedhar Bollapalli | 0ec699c | 2024-04-23 12:17:22 +0530 | [diff] [blame] | 154 | status = mmio_read_32(obr_offset); |
Maheedhar Bollapalli | a9cc0bd | 2024-04-22 15:27:02 +0530 | [diff] [blame] | 155 | if ((status & IPI_BIT_MASK(remote)) != 0U) { |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 156 | ret |= IPI_MB_STATUS_SEND_PENDING; |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 157 | } |
Maheedhar Bollapalli | 0ec699c | 2024-04-23 12:17:22 +0530 | [diff] [blame] | 158 | status = mmio_read_32(isr_offset); |
Maheedhar Bollapalli | a9cc0bd | 2024-04-22 15:27:02 +0530 | [diff] [blame] | 159 | if ((status & IPI_BIT_MASK(remote)) != 0U) { |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 160 | ret |= IPI_MB_STATUS_RECV_PENDING; |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 161 | } |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 166 | /** |
| 167 | * ipi_mb_notify() - Trigger IPI mailbox notification. |
| 168 | * @local: local IPI ID. |
| 169 | * @remote: remote IPI ID. |
| 170 | * @is_blocking: if to trigger the notification in blocking mode or not. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 171 | * |
| 172 | * It sets the remote bit in the IPI agent trigger register. |
| 173 | * |
| 174 | */ |
Venkatesh Yadav Abbarapu | efa1d32 | 2021-08-04 21:33:15 -0600 | [diff] [blame] | 175 | void ipi_mb_notify(uint32_t local, uint32_t remote, uint32_t is_blocking) |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 176 | { |
| 177 | uint32_t status; |
Maheedhar Bollapalli | 0ec699c | 2024-04-23 12:17:22 +0530 | [diff] [blame] | 178 | uint64_t trig_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_TRIG_OFFSET); |
| 179 | uint64_t obr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_OBR_OFFSET); |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 180 | |
Maheedhar Bollapalli | 0ec699c | 2024-04-23 12:17:22 +0530 | [diff] [blame] | 181 | mmio_write_32(trig_offset, |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 182 | IPI_BIT_MASK(remote)); |
Maheedhar Bollapalli | a9cc0bd | 2024-04-22 15:27:02 +0530 | [diff] [blame] | 183 | if (is_blocking != 0U) { |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 184 | do { |
Maheedhar Bollapalli | 0ec699c | 2024-04-23 12:17:22 +0530 | [diff] [blame] | 185 | status = mmio_read_32(obr_offset); |
Maheedhar Bollapalli | a9cc0bd | 2024-04-22 15:27:02 +0530 | [diff] [blame] | 186 | } while ((status & IPI_BIT_MASK(remote)) != 0U); |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 187 | } |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 190 | /** |
| 191 | * ipi_mb_ack() - Ack IPI mailbox notification from the other end. |
| 192 | * @local: local IPI ID. |
| 193 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 194 | * |
| 195 | * It will clear the remote bit in the isr register. |
| 196 | * |
| 197 | */ |
| 198 | void ipi_mb_ack(uint32_t local, uint32_t remote) |
| 199 | { |
Maheedhar Bollapalli | 0ec699c | 2024-04-23 12:17:22 +0530 | [diff] [blame] | 200 | uint64_t isr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_ISR_OFFSET); |
| 201 | |
| 202 | mmio_write_32(isr_offset, |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 203 | IPI_BIT_MASK(remote)); |
| 204 | } |
| 205 | |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 206 | /** |
| 207 | * ipi_mb_disable_irq() - Disable IPI mailbox notification interrupt. |
| 208 | * @local: local IPI ID. |
| 209 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 210 | * |
| 211 | * It will mask the remote bit in the idr register. |
| 212 | * |
| 213 | */ |
| 214 | void ipi_mb_disable_irq(uint32_t local, uint32_t remote) |
| 215 | { |
Maheedhar Bollapalli | 0ec699c | 2024-04-23 12:17:22 +0530 | [diff] [blame] | 216 | uint64_t idr_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_IDR_OFFSET); |
| 217 | |
| 218 | mmio_write_32(idr_offset, |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 219 | IPI_BIT_MASK(remote)); |
| 220 | } |
| 221 | |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 222 | /** |
| 223 | * ipi_mb_enable_irq() - Enable IPI mailbox notification interrupt. |
| 224 | * @local: local IPI ID. |
| 225 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 226 | * |
| 227 | * It will mask the remote bit in the idr register. |
| 228 | * |
| 229 | */ |
| 230 | void ipi_mb_enable_irq(uint32_t local, uint32_t remote) |
| 231 | { |
Maheedhar Bollapalli | 0ec699c | 2024-04-23 12:17:22 +0530 | [diff] [blame] | 232 | uint64_t ier_offset = (uint64_t)(IPI_REG_BASE(local) + IPI_IER_OFFSET); |
| 233 | |
| 234 | mmio_write_32(ier_offset, |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 235 | IPI_BIT_MASK(remote)); |
| 236 | } |