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. |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 4 | * Copyright (c) 2022-2023, 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> |
| 23 | |
| 24 | /********************************************************************* |
| 25 | * Macros definitions |
| 26 | ********************************************************************/ |
| 27 | |
| 28 | /* IPI registers offsets macros */ |
| 29 | #define IPI_TRIG_OFFSET 0x00U |
| 30 | #define IPI_OBR_OFFSET 0x04U |
| 31 | #define IPI_ISR_OFFSET 0x10U |
| 32 | #define IPI_IMR_OFFSET 0x14U |
| 33 | #define IPI_IER_OFFSET 0x18U |
| 34 | #define IPI_IDR_OFFSET 0x1CU |
| 35 | |
| 36 | /* IPI register start offset */ |
| 37 | #define IPI_REG_BASE(I) (ipi_table[(I)].ipi_reg_base) |
| 38 | |
| 39 | /* IPI register bit mask */ |
| 40 | #define IPI_BIT_MASK(I) (ipi_table[(I)].ipi_bit_mask) |
| 41 | |
| 42 | /* IPI configuration table */ |
Boyan Karatotev | 05e9d4d | 2022-11-22 14:31:41 +0000 | [diff] [blame] | 43 | static const struct ipi_config *ipi_table; |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 44 | |
| 45 | /* Total number of IPI */ |
| 46 | static uint32_t ipi_total; |
| 47 | |
| 48 | /** |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 49 | * ipi_config_table_init() - Initialize IPI configuration data. |
| 50 | * @ipi_config_table: IPI configuration table. |
| 51 | * @total_ipi: Total number of IPI available. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 52 | * |
| 53 | */ |
| 54 | void 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 | |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 61 | /** |
| 62 | * is_ipi_mb_within_range() - verify if IPI mailbox is within range. |
| 63 | * @local: local IPI ID. |
| 64 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 65 | * |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 66 | * Return: - 1 if within range, 0 if not. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 67 | * |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 68 | */ |
| 69 | static inline int is_ipi_mb_within_range(uint32_t local, uint32_t remote) |
| 70 | { |
| 71 | int ret = 1; |
| 72 | |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 73 | if (remote >= ipi_total || local >= ipi_total) { |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 74 | ret = 0; |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 75 | } |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | /** |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 81 | * ipi_mb_validate() - validate IPI mailbox access. |
| 82 | * @local: local IPI ID. |
| 83 | * @remote: remote IPI ID. |
| 84 | * @is_secure: indicate if the requester is from secure software. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 85 | * |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 86 | * Return: 0 success, negative value for errors. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 87 | * |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 88 | */ |
| 89 | int ipi_mb_validate(uint32_t local, uint32_t remote, unsigned int is_secure) |
| 90 | { |
| 91 | int ret = 0; |
| 92 | |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 93 | if (!is_ipi_mb_within_range(local, remote)) { |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 94 | ret = -EINVAL; |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 95 | } else if (IPI_IS_SECURE(local) && !is_secure) { |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 96 | ret = -EPERM; |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 97 | } else if (IPI_IS_SECURE(remote) && !is_secure) { |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 98 | ret = -EPERM; |
Venkatesh Yadav Abbarapu | ccf6da7 | 2022-05-04 14:23:32 +0530 | [diff] [blame] | 99 | } else { |
| 100 | /* To fix the misra 15.7 warning */ |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 101 | } |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 102 | |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * ipi_mb_open() - Open IPI mailbox. |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 108 | * @local: local IPI ID. |
| 109 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 110 | * |
| 111 | */ |
| 112 | void 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. |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 122 | * @local: local IPI ID. |
| 123 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 124 | * |
| 125 | */ |
| 126 | void ipi_mb_release(uint32_t local, uint32_t remote) |
| 127 | { |
| 128 | mmio_write_32(IPI_REG_BASE(local) + IPI_IDR_OFFSET, |
| 129 | IPI_BIT_MASK(remote)); |
| 130 | } |
| 131 | |
| 132 | /** |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 133 | * ipi_mb_enquire_status() - Enquire IPI mailbox status. |
| 134 | * @local: local IPI ID. |
| 135 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 136 | * |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 137 | * Return: 0 idle, positive value for pending sending or receiving, |
| 138 | * negative value for errors. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 139 | * |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 140 | */ |
| 141 | int ipi_mb_enquire_status(uint32_t local, uint32_t remote) |
| 142 | { |
HariBabu Gattem | 9f9db61 | 2022-09-15 22:35:11 -0700 | [diff] [blame] | 143 | int ret = 0U; |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 144 | uint32_t status; |
| 145 | |
| 146 | status = mmio_read_32(IPI_REG_BASE(local) + IPI_OBR_OFFSET); |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 147 | if (status & IPI_BIT_MASK(remote)) { |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 148 | ret |= IPI_MB_STATUS_SEND_PENDING; |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 149 | } |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 150 | status = mmio_read_32(IPI_REG_BASE(local) + IPI_ISR_OFFSET); |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 151 | if (status & IPI_BIT_MASK(remote)) { |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 152 | ret |= IPI_MB_STATUS_RECV_PENDING; |
Venkatesh Yadav Abbarapu | 987fad3 | 2022-04-29 13:52:00 +0530 | [diff] [blame] | 153 | } |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 154 | |
| 155 | return ret; |
| 156 | } |
| 157 | |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 158 | /** |
| 159 | * ipi_mb_notify() - Trigger IPI mailbox notification. |
| 160 | * @local: local IPI ID. |
| 161 | * @remote: remote IPI ID. |
| 162 | * @is_blocking: if to trigger the notification in blocking mode or not. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 163 | * |
| 164 | * It sets the remote bit in the IPI agent trigger register. |
| 165 | * |
| 166 | */ |
Venkatesh Yadav Abbarapu | efa1d32 | 2021-08-04 21:33:15 -0600 | [diff] [blame] | 167 | 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] | 168 | { |
| 169 | uint32_t status; |
| 170 | |
| 171 | mmio_write_32(IPI_REG_BASE(local) + IPI_TRIG_OFFSET, |
| 172 | IPI_BIT_MASK(remote)); |
| 173 | if (is_blocking) { |
| 174 | do { |
| 175 | status = mmio_read_32(IPI_REG_BASE(local) + |
| 176 | IPI_OBR_OFFSET); |
| 177 | } while (status & IPI_BIT_MASK(remote)); |
| 178 | } |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 181 | /** |
| 182 | * ipi_mb_ack() - Ack IPI mailbox notification from the other end. |
| 183 | * @local: local IPI ID. |
| 184 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 185 | * |
| 186 | * It will clear the remote bit in the isr register. |
| 187 | * |
| 188 | */ |
| 189 | void ipi_mb_ack(uint32_t local, uint32_t remote) |
| 190 | { |
| 191 | mmio_write_32(IPI_REG_BASE(local) + IPI_ISR_OFFSET, |
| 192 | IPI_BIT_MASK(remote)); |
| 193 | } |
| 194 | |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 195 | /** |
| 196 | * ipi_mb_disable_irq() - Disable IPI mailbox notification interrupt. |
| 197 | * @local: local IPI ID. |
| 198 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 199 | * |
| 200 | * It will mask the remote bit in the idr register. |
| 201 | * |
| 202 | */ |
| 203 | void ipi_mb_disable_irq(uint32_t local, uint32_t remote) |
| 204 | { |
| 205 | mmio_write_32(IPI_REG_BASE(local) + IPI_IDR_OFFSET, |
| 206 | IPI_BIT_MASK(remote)); |
| 207 | } |
| 208 | |
Prasad Kummari | 7d0623a | 2023-06-09 14:32:00 +0530 | [diff] [blame] | 209 | /** |
| 210 | * ipi_mb_enable_irq() - Enable IPI mailbox notification interrupt. |
| 211 | * @local: local IPI ID. |
| 212 | * @remote: remote IPI ID. |
Jolly Shah | c2583ab | 2019-01-08 11:31:49 -0800 | [diff] [blame] | 213 | * |
| 214 | * It will mask the remote bit in the idr register. |
| 215 | * |
| 216 | */ |
| 217 | void ipi_mb_enable_irq(uint32_t local, uint32_t remote) |
| 218 | { |
| 219 | mmio_write_32(IPI_REG_BASE(local) + IPI_IER_OFFSET, |
| 220 | IPI_BIT_MASK(remote)); |
| 221 | } |