Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Texas Instruments K3 Secure Proxy Driver |
| 3 | * Based on Linux and U-Boot implementation |
| 4 | * |
| 5 | * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/ |
| 6 | * |
| 7 | * SPDX-License-Identifier: BSD-3-Clause |
| 8 | */ |
| 9 | |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 10 | #include <errno.h> |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 11 | #include <stdlib.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | |
| 13 | #include <platform_def.h> |
| 14 | |
| 15 | #include <arch_helpers.h> |
| 16 | #include <common/debug.h> |
| 17 | #include <lib/mmio.h> |
| 18 | #include <lib/utils.h> |
| 19 | #include <lib/utils_def.h> |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 20 | |
| 21 | #include "sec_proxy.h" |
| 22 | |
| 23 | /* SEC PROXY RT THREAD STATUS */ |
| 24 | #define RT_THREAD_STATUS (0x0) |
| 25 | #define RT_THREAD_STATUS_ERROR_SHIFT (31) |
| 26 | #define RT_THREAD_STATUS_ERROR_MASK BIT(31) |
| 27 | #define RT_THREAD_STATUS_CUR_CNT_SHIFT (0) |
| 28 | #define RT_THREAD_STATUS_CUR_CNT_MASK GENMASK(7, 0) |
| 29 | |
| 30 | /* SEC PROXY SCFG THREAD CTRL */ |
| 31 | #define SCFG_THREAD_CTRL (0x1000) |
| 32 | #define SCFG_THREAD_CTRL_DIR_SHIFT (31) |
| 33 | #define SCFG_THREAD_CTRL_DIR_MASK BIT(31) |
| 34 | |
| 35 | #define SEC_PROXY_THREAD(base, x) ((base) + (0x1000 * (x))) |
| 36 | #define THREAD_IS_RX (1) |
| 37 | #define THREAD_IS_TX (0) |
| 38 | |
| 39 | /** |
| 40 | * struct k3_sec_proxy_desc - Description of secure proxy integration |
| 41 | * @timeout_us: Timeout for communication (in Microseconds) |
| 42 | * @max_msg_size: Message size in bytes |
| 43 | * @data_start_offset: Offset of the First data register of the thread |
| 44 | * @data_end_offset: Offset of the Last data register of the thread |
| 45 | */ |
| 46 | struct k3_sec_proxy_desc { |
| 47 | uint32_t timeout_us; |
| 48 | uint16_t max_msg_size; |
| 49 | uint16_t data_start_offset; |
| 50 | uint16_t data_end_offset; |
| 51 | }; |
| 52 | |
| 53 | /** |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 54 | * struct k3_sec_proxy_thread - Description of a Secure Proxy Thread |
| 55 | * @name: Thread Name |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 56 | * @data: Thread Data path region for target |
| 57 | * @scfg: Secure Config Region for Thread |
| 58 | * @rt: RealTime Region for Thread |
| 59 | */ |
| 60 | struct k3_sec_proxy_thread { |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 61 | const char *name; |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 62 | uintptr_t data; |
| 63 | uintptr_t scfg; |
| 64 | uintptr_t rt; |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * struct k3_sec_proxy_mbox - Description of a Secure Proxy Instance |
| 69 | * @desc: Description of the SoC integration |
| 70 | * @chans: Array for valid thread instances |
| 71 | */ |
| 72 | struct k3_sec_proxy_mbox { |
| 73 | const struct k3_sec_proxy_desc desc; |
| 74 | struct k3_sec_proxy_thread threads[]; |
| 75 | }; |
| 76 | |
| 77 | /* |
| 78 | * Thread ID #0: DMSC notify |
| 79 | * Thread ID #1: DMSC request response |
| 80 | * Thread ID #2: DMSC request high priority |
| 81 | * Thread ID #3: DMSC request low priority |
| 82 | * Thread ID #4: DMSC notify response |
| 83 | */ |
| 84 | #define SP_THREAD(_x) \ |
| 85 | [_x] = { \ |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 86 | .name = #_x, \ |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 87 | .data = SEC_PROXY_THREAD(SEC_PROXY_DATA_BASE, _x), \ |
| 88 | .scfg = SEC_PROXY_THREAD(SEC_PROXY_SCFG_BASE, _x), \ |
| 89 | .rt = SEC_PROXY_THREAD(SEC_PROXY_RT_BASE, _x), \ |
| 90 | } |
| 91 | |
| 92 | static struct k3_sec_proxy_mbox spm = { |
| 93 | .desc = { |
| 94 | .timeout_us = SEC_PROXY_TIMEOUT_US, |
| 95 | .max_msg_size = SEC_PROXY_MAX_MESSAGE_SIZE, |
| 96 | .data_start_offset = 0x4, |
| 97 | .data_end_offset = 0x3C, |
| 98 | }, |
| 99 | .threads = { |
| 100 | SP_THREAD(SP_NOTIFY), |
| 101 | SP_THREAD(SP_RESPONSE), |
| 102 | SP_THREAD(SP_HIGH_PRIORITY), |
| 103 | SP_THREAD(SP_LOW_PRIORITY), |
| 104 | SP_THREAD(SP_NOTIFY_RESP), |
| 105 | }, |
| 106 | }; |
| 107 | |
| 108 | /** |
| 109 | * struct sec_msg_hdr - Message header for secure messages and responses |
| 110 | * @checksum: CRC of message for integrity checking |
| 111 | */ |
| 112 | union sec_msg_hdr { |
| 113 | struct { |
| 114 | uint16_t checksum; |
| 115 | uint16_t reserved; |
| 116 | } __packed; |
| 117 | uint32_t data; |
| 118 | }; |
| 119 | |
| 120 | /** |
| 121 | * k3_sec_proxy_verify_thread() - Verify thread status before |
| 122 | * sending/receiving data |
| 123 | * @spt: Pointer to Secure Proxy thread description |
| 124 | * @dir: Direction of the thread |
| 125 | * |
| 126 | * Return: 0 if all goes well, else appropriate error message |
| 127 | */ |
| 128 | static inline int k3_sec_proxy_verify_thread(struct k3_sec_proxy_thread *spt, |
| 129 | uint32_t dir) |
| 130 | { |
| 131 | /* Check for any errors already available */ |
| 132 | if (mmio_read_32(spt->rt + RT_THREAD_STATUS) & |
| 133 | RT_THREAD_STATUS_ERROR_MASK) { |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 134 | ERROR("Thread %s is corrupted, cannot send data\n", spt->name); |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 135 | return -EINVAL; |
| 136 | } |
| 137 | |
| 138 | /* Make sure thread is configured for right direction */ |
| 139 | if ((mmio_read_32(spt->scfg + SCFG_THREAD_CTRL) & SCFG_THREAD_CTRL_DIR_MASK) |
| 140 | != (dir << SCFG_THREAD_CTRL_DIR_SHIFT)) { |
Andrew F. Davis | 40d1ded | 2019-04-10 11:45:19 -0400 | [diff] [blame^] | 141 | if (dir == THREAD_IS_TX) |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 142 | ERROR("Trying to send data on RX Thread %s\n", |
| 143 | spt->name); |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 144 | else |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 145 | ERROR("Trying to receive data on TX Thread %s\n", |
| 146 | spt->name); |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 147 | return -EINVAL; |
| 148 | } |
| 149 | |
| 150 | /* Check the message queue before sending/receiving data */ |
| 151 | uint32_t tick_start = (uint32_t)read_cntpct_el0(); |
| 152 | uint32_t ticks_per_us = SYS_COUNTER_FREQ_IN_TICKS / 1000000; |
| 153 | while (!(mmio_read_32(spt->rt + RT_THREAD_STATUS) & RT_THREAD_STATUS_CUR_CNT_MASK)) { |
Andrew F. Davis | 40d1ded | 2019-04-10 11:45:19 -0400 | [diff] [blame^] | 154 | VERBOSE("Waiting for thread %s to %s\n", |
| 155 | spt->name, (dir == THREAD_IS_TX) ? "empty" : "fill"); |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 156 | if (((uint32_t)read_cntpct_el0() - tick_start) > |
| 157 | (spm.desc.timeout_us * ticks_per_us)) { |
Andrew F. Davis | 40d1ded | 2019-04-10 11:45:19 -0400 | [diff] [blame^] | 158 | ERROR("Timeout waiting for thread %s to %s\n", |
| 159 | spt->name, (dir == THREAD_IS_TX) ? "empty" : "fill"); |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 160 | return -ETIMEDOUT; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | /** |
Andrew F. Davis | 82a3c1d | 2019-01-04 12:44:00 -0600 | [diff] [blame] | 168 | * k3_sec_proxy_clear_rx_thread() - Clear Secure Proxy thread |
| 169 | * |
| 170 | * @id: Channel Identifier |
| 171 | * |
| 172 | * Return: 0 if all goes well, else appropriate error message |
| 173 | */ |
| 174 | int k3_sec_proxy_clear_rx_thread(enum k3_sec_proxy_chan_id id) |
| 175 | { |
| 176 | struct k3_sec_proxy_thread *spt = &spm.threads[id]; |
| 177 | |
| 178 | /* Check for any errors already available */ |
| 179 | if (mmio_read_32(spt->rt + RT_THREAD_STATUS) & |
| 180 | RT_THREAD_STATUS_ERROR_MASK) { |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 181 | ERROR("Thread %s is corrupted, cannot send data\n", spt->name); |
Andrew F. Davis | 82a3c1d | 2019-01-04 12:44:00 -0600 | [diff] [blame] | 182 | return -EINVAL; |
| 183 | } |
| 184 | |
| 185 | /* Make sure thread is configured for right direction */ |
| 186 | if (!(mmio_read_32(spt->scfg + SCFG_THREAD_CTRL) & SCFG_THREAD_CTRL_DIR_MASK)) { |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 187 | ERROR("Cannot clear a transmit thread %s\n", spt->name); |
Andrew F. Davis | 82a3c1d | 2019-01-04 12:44:00 -0600 | [diff] [blame] | 188 | return -EINVAL; |
| 189 | } |
| 190 | |
| 191 | /* Read off messages from thread until empty */ |
| 192 | uint32_t try_count = 10; |
| 193 | while (mmio_read_32(spt->rt + RT_THREAD_STATUS) & RT_THREAD_STATUS_CUR_CNT_MASK) { |
| 194 | if (!(try_count--)) { |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 195 | ERROR("Could not clear all messages from thread %s\n", spt->name); |
Andrew F. Davis | 82a3c1d | 2019-01-04 12:44:00 -0600 | [diff] [blame] | 196 | return -ETIMEDOUT; |
| 197 | } |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 198 | WARN("Clearing message from thread %s\n", spt->name); |
Andrew F. Davis | 82a3c1d | 2019-01-04 12:44:00 -0600 | [diff] [blame] | 199 | mmio_read_32(spt->data + spm.desc.data_end_offset); |
| 200 | } |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /** |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 206 | * k3_sec_proxy_send() - Send data over a Secure Proxy thread |
| 207 | * @id: Channel Identifier |
| 208 | * @msg: Pointer to k3_sec_proxy_msg |
| 209 | * |
| 210 | * Return: 0 if all goes well, else appropriate error message |
| 211 | */ |
| 212 | int k3_sec_proxy_send(enum k3_sec_proxy_chan_id id, const struct k3_sec_proxy_msg *msg) |
| 213 | { |
| 214 | struct k3_sec_proxy_thread *spt = &spm.threads[id]; |
| 215 | union sec_msg_hdr secure_header; |
| 216 | int num_words, trail_bytes, i, ret; |
| 217 | uintptr_t data_reg; |
| 218 | |
| 219 | ret = k3_sec_proxy_verify_thread(spt, THREAD_IS_TX); |
| 220 | if (ret) { |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 221 | ERROR("Thread %s verification failed (%d)\n", spt->name, ret); |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | /* Check the message size */ |
| 226 | if (msg->len + sizeof(secure_header) > spm.desc.max_msg_size) { |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 227 | ERROR("Thread %s message length %lu > max msg size\n", |
| 228 | spt->name, msg->len); |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 229 | return -EINVAL; |
| 230 | } |
| 231 | |
| 232 | /* TODO: Calculate checksum */ |
| 233 | secure_header.checksum = 0; |
| 234 | |
| 235 | /* Send the secure header */ |
| 236 | data_reg = spm.desc.data_start_offset; |
| 237 | mmio_write_32(spt->data + data_reg, secure_header.data); |
| 238 | data_reg += sizeof(uint32_t); |
| 239 | |
| 240 | /* Send whole words */ |
| 241 | num_words = msg->len / sizeof(uint32_t); |
| 242 | for (i = 0; i < num_words; i++) { |
| 243 | mmio_write_32(spt->data + data_reg, ((uint32_t *)msg->buf)[i]); |
| 244 | data_reg += sizeof(uint32_t); |
| 245 | } |
| 246 | |
| 247 | /* Send remaining bytes */ |
| 248 | trail_bytes = msg->len % sizeof(uint32_t); |
| 249 | if (trail_bytes) { |
| 250 | uint32_t data_trail = 0; |
| 251 | |
| 252 | i = msg->len - trail_bytes; |
| 253 | while (trail_bytes--) { |
| 254 | data_trail <<= 8; |
| 255 | data_trail |= msg->buf[i++]; |
| 256 | } |
| 257 | |
| 258 | mmio_write_32(spt->data + data_reg, data_trail); |
| 259 | data_reg += sizeof(uint32_t); |
| 260 | } |
| 261 | /* |
| 262 | * 'data_reg' indicates next register to write. If we did not already |
| 263 | * write on tx complete reg(last reg), we must do so for transmit |
| 264 | */ |
| 265 | if (data_reg <= spm.desc.data_end_offset) |
| 266 | mmio_write_32(spt->data + spm.desc.data_end_offset, 0); |
| 267 | |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 268 | VERBOSE("Message successfully sent on thread %s\n", spt->name); |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * k3_sec_proxy_recv() - Receive data from a Secure Proxy thread |
| 275 | * @id: Channel Identifier |
| 276 | * @msg: Pointer to k3_sec_proxy_msg |
| 277 | * |
| 278 | * Return: 0 if all goes well, else appropriate error message |
| 279 | */ |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 280 | int k3_sec_proxy_recv(enum k3_sec_proxy_chan_id id, struct k3_sec_proxy_msg *msg) |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 281 | { |
| 282 | struct k3_sec_proxy_thread *spt = &spm.threads[id]; |
| 283 | union sec_msg_hdr secure_header; |
| 284 | uintptr_t data_reg; |
| 285 | int num_words, trail_bytes, i, ret; |
| 286 | |
| 287 | ret = k3_sec_proxy_verify_thread(spt, THREAD_IS_RX); |
| 288 | if (ret) { |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 289 | ERROR("Thread %s verification failed (%d)\n", spt->name, ret); |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | /* Read secure header */ |
| 294 | data_reg = spm.desc.data_start_offset; |
| 295 | secure_header.data = mmio_read_32(spt->data + data_reg); |
| 296 | data_reg += sizeof(uint32_t); |
| 297 | |
| 298 | /* Read whole words */ |
| 299 | num_words = msg->len / sizeof(uint32_t); |
| 300 | for (i = 0; i < num_words; i++) { |
| 301 | ((uint32_t *)msg->buf)[i] = mmio_read_32(spt->data + data_reg); |
| 302 | data_reg += sizeof(uint32_t); |
| 303 | } |
| 304 | |
| 305 | /* Read remaining bytes */ |
| 306 | trail_bytes = msg->len % sizeof(uint32_t); |
| 307 | if (trail_bytes) { |
| 308 | uint32_t data_trail = mmio_read_32(spt->data + data_reg); |
| 309 | data_reg += sizeof(uint32_t); |
| 310 | |
| 311 | i = msg->len - trail_bytes; |
| 312 | while (trail_bytes--) { |
| 313 | msg->buf[i] = data_trail & 0xff; |
| 314 | data_trail >>= 8; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * 'data_reg' indicates next register to read. If we did not already |
| 320 | * read on rx complete reg(last reg), we must do so for receive |
| 321 | */ |
| 322 | if (data_reg <= spm.desc.data_end_offset) |
| 323 | mmio_read_32(spt->data + spm.desc.data_end_offset); |
| 324 | |
| 325 | /* TODO: Verify checksum */ |
| 326 | (void)secure_header.checksum; |
| 327 | |
Andrew F. Davis | 8e725cb | 2019-04-10 11:38:56 -0400 | [diff] [blame] | 328 | VERBOSE("Message successfully received from thread %s\n", spt->name); |
Andrew F. Davis | 537d3ff | 2018-05-04 19:06:08 +0000 | [diff] [blame] | 329 | |
| 330 | return 0; |
| 331 | } |