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 | /** |
| 54 | * struct k3_sec_proxy_thread - Description of a secure proxy Thread |
| 55 | * @id: Thread ID |
| 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 { |
| 61 | uint32_t id; |
| 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] = { \ |
| 86 | .id = _x, \ |
| 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) { |
| 134 | ERROR("Thread %d is corrupted, cannot send data\n", spt->id); |
| 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)) { |
| 141 | if (dir) |
| 142 | ERROR("Trying to receive data on tx Thread %d\n", |
| 143 | spt->id); |
| 144 | else |
| 145 | ERROR("Trying to send data on rx Thread %d\n", |
| 146 | spt->id); |
| 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)) { |
| 154 | VERBOSE("Waiting for thread %d to clear\n", spt->id); |
| 155 | if (((uint32_t)read_cntpct_el0() - tick_start) > |
| 156 | (spm.desc.timeout_us * ticks_per_us)) { |
| 157 | ERROR("Timeout waiting for thread %d to clear\n", spt->id); |
| 158 | return -ETIMEDOUT; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * k3_sec_proxy_send() - Send data over a Secure Proxy thread |
| 167 | * @id: Channel Identifier |
| 168 | * @msg: Pointer to k3_sec_proxy_msg |
| 169 | * |
| 170 | * Return: 0 if all goes well, else appropriate error message |
| 171 | */ |
| 172 | int k3_sec_proxy_send(enum k3_sec_proxy_chan_id id, const struct k3_sec_proxy_msg *msg) |
| 173 | { |
| 174 | struct k3_sec_proxy_thread *spt = &spm.threads[id]; |
| 175 | union sec_msg_hdr secure_header; |
| 176 | int num_words, trail_bytes, i, ret; |
| 177 | uintptr_t data_reg; |
| 178 | |
| 179 | ret = k3_sec_proxy_verify_thread(spt, THREAD_IS_TX); |
| 180 | if (ret) { |
| 181 | ERROR("Thread %d verification failed (%d)\n", spt->id, ret); |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | /* Check the message size */ |
| 186 | if (msg->len + sizeof(secure_header) > spm.desc.max_msg_size) { |
| 187 | ERROR("Thread %d message length %lu > max msg size\n", |
| 188 | spt->id, msg->len); |
| 189 | return -EINVAL; |
| 190 | } |
| 191 | |
| 192 | /* TODO: Calculate checksum */ |
| 193 | secure_header.checksum = 0; |
| 194 | |
| 195 | /* Send the secure header */ |
| 196 | data_reg = spm.desc.data_start_offset; |
| 197 | mmio_write_32(spt->data + data_reg, secure_header.data); |
| 198 | data_reg += sizeof(uint32_t); |
| 199 | |
| 200 | /* Send whole words */ |
| 201 | num_words = msg->len / sizeof(uint32_t); |
| 202 | for (i = 0; i < num_words; i++) { |
| 203 | mmio_write_32(spt->data + data_reg, ((uint32_t *)msg->buf)[i]); |
| 204 | data_reg += sizeof(uint32_t); |
| 205 | } |
| 206 | |
| 207 | /* Send remaining bytes */ |
| 208 | trail_bytes = msg->len % sizeof(uint32_t); |
| 209 | if (trail_bytes) { |
| 210 | uint32_t data_trail = 0; |
| 211 | |
| 212 | i = msg->len - trail_bytes; |
| 213 | while (trail_bytes--) { |
| 214 | data_trail <<= 8; |
| 215 | data_trail |= msg->buf[i++]; |
| 216 | } |
| 217 | |
| 218 | mmio_write_32(spt->data + data_reg, data_trail); |
| 219 | data_reg += sizeof(uint32_t); |
| 220 | } |
| 221 | /* |
| 222 | * 'data_reg' indicates next register to write. If we did not already |
| 223 | * write on tx complete reg(last reg), we must do so for transmit |
| 224 | */ |
| 225 | if (data_reg <= spm.desc.data_end_offset) |
| 226 | mmio_write_32(spt->data + spm.desc.data_end_offset, 0); |
| 227 | |
| 228 | VERBOSE("Message successfully sent on thread %ud\n", id); |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * k3_sec_proxy_recv() - Receive data from a Secure Proxy thread |
| 235 | * @id: Channel Identifier |
| 236 | * @msg: Pointer to k3_sec_proxy_msg |
| 237 | * |
| 238 | * Return: 0 if all goes well, else appropriate error message |
| 239 | */ |
| 240 | int k3_sec_proxy_recv(uint32_t id, struct k3_sec_proxy_msg *msg) |
| 241 | { |
| 242 | struct k3_sec_proxy_thread *spt = &spm.threads[id]; |
| 243 | union sec_msg_hdr secure_header; |
| 244 | uintptr_t data_reg; |
| 245 | int num_words, trail_bytes, i, ret; |
| 246 | |
| 247 | ret = k3_sec_proxy_verify_thread(spt, THREAD_IS_RX); |
| 248 | if (ret) { |
| 249 | ERROR("Thread %d verification failed (%d)\n", spt->id, ret); |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | /* Read secure header */ |
| 254 | data_reg = spm.desc.data_start_offset; |
| 255 | secure_header.data = mmio_read_32(spt->data + data_reg); |
| 256 | data_reg += sizeof(uint32_t); |
| 257 | |
| 258 | /* Read whole words */ |
| 259 | num_words = msg->len / sizeof(uint32_t); |
| 260 | for (i = 0; i < num_words; i++) { |
| 261 | ((uint32_t *)msg->buf)[i] = mmio_read_32(spt->data + data_reg); |
| 262 | data_reg += sizeof(uint32_t); |
| 263 | } |
| 264 | |
| 265 | /* Read remaining bytes */ |
| 266 | trail_bytes = msg->len % sizeof(uint32_t); |
| 267 | if (trail_bytes) { |
| 268 | uint32_t data_trail = mmio_read_32(spt->data + data_reg); |
| 269 | data_reg += sizeof(uint32_t); |
| 270 | |
| 271 | i = msg->len - trail_bytes; |
| 272 | while (trail_bytes--) { |
| 273 | msg->buf[i] = data_trail & 0xff; |
| 274 | data_trail >>= 8; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * 'data_reg' indicates next register to read. If we did not already |
| 280 | * read on rx complete reg(last reg), we must do so for receive |
| 281 | */ |
| 282 | if (data_reg <= spm.desc.data_end_offset) |
| 283 | mmio_read_32(spt->data + spm.desc.data_end_offset); |
| 284 | |
| 285 | /* TODO: Verify checksum */ |
| 286 | (void)secure_header.checksum; |
| 287 | |
| 288 | VERBOSE("Message successfully received from thread %ud\n", id); |
| 289 | |
| 290 | return 0; |
| 291 | } |