Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Etienne Carriere | 1ba9678 | 2021-05-19 16:27:41 +0200 | [diff] [blame] | 3 | * Copyright (c) 2018-2020 Linaro Limited |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Etienne Carriere | 1ba9678 | 2021-05-19 16:27:41 +0200 | [diff] [blame] | 7 | #include <cpu_func.h> |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 8 | #include <dm.h> |
Sean Anderson | 4736416 | 2020-10-04 21:39:51 -0400 | [diff] [blame] | 9 | #include <dm/device_compat.h> |
Patrick Delaunay | 86a586b | 2022-03-29 14:21:24 +0200 | [diff] [blame] | 10 | #include <dm/lists.h> |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 13 | #include <tee.h> |
| 14 | #include <linux/arm-smccc.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 15 | #include <linux/err.h> |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 16 | #include <linux/io.h> |
| 17 | |
| 18 | #include "optee_smc.h" |
| 19 | #include "optee_msg.h" |
| 20 | #include "optee_private.h" |
| 21 | |
| 22 | #define PAGELIST_ENTRIES_PER_PAGE \ |
| 23 | ((OPTEE_MSG_NONCONTIG_PAGE_SIZE / sizeof(u64)) - 1) |
| 24 | |
| 25 | typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long, |
| 26 | unsigned long, unsigned long, unsigned long, |
| 27 | unsigned long, unsigned long, |
| 28 | struct arm_smccc_res *); |
| 29 | |
| 30 | struct optee_pdata { |
| 31 | optee_invoke_fn *invoke_fn; |
| 32 | }; |
| 33 | |
| 34 | struct rpc_param { |
| 35 | u32 a0; |
| 36 | u32 a1; |
| 37 | u32 a2; |
| 38 | u32 a3; |
| 39 | u32 a4; |
| 40 | u32 a5; |
| 41 | u32 a6; |
| 42 | u32 a7; |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * reg_pair_to_ptr() - Make a pointer of 2 32-bit values |
| 47 | * @reg0: High bits of the pointer |
| 48 | * @reg1: Low bits of the pointer |
| 49 | * |
| 50 | * Returns the combined result, note that if a pointer is 32-bit wide @reg0 |
| 51 | * will be discarded. |
| 52 | */ |
| 53 | static void *reg_pair_to_ptr(u32 reg0, u32 reg1) |
| 54 | { |
| 55 | return (void *)(ulong)(((u64)reg0 << 32) | reg1); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * reg_pair_from_64() - Split a 64-bit value into two 32-bit values |
| 60 | * @reg0: High bits of @val |
| 61 | * @reg1: Low bits of @val |
| 62 | * @val: The value to split |
| 63 | */ |
| 64 | static void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val) |
| 65 | { |
| 66 | *reg0 = val >> 32; |
| 67 | *reg1 = val; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * optee_alloc_and_init_page_list() - Provide page list of memory buffer |
| 72 | * @buf: Start of buffer |
| 73 | * @len: Length of buffer |
| 74 | * @phys_buf_ptr Physical pointer with coded offset to page list |
| 75 | * |
| 76 | * Secure world doesn't share mapping with Normal world (U-Boot in this case) |
| 77 | * so physical pointers are needed when sharing pointers. |
| 78 | * |
| 79 | * Returns a pointer page list on success or NULL on failure |
| 80 | */ |
| 81 | void *optee_alloc_and_init_page_list(void *buf, ulong len, u64 *phys_buf_ptr) |
| 82 | { |
| 83 | const unsigned int page_size = OPTEE_MSG_NONCONTIG_PAGE_SIZE; |
| 84 | const phys_addr_t page_mask = page_size - 1; |
| 85 | u8 *buf_base; |
| 86 | unsigned int page_offset; |
| 87 | unsigned int num_pages; |
| 88 | unsigned int list_size; |
| 89 | unsigned int n; |
| 90 | void *page_list; |
| 91 | struct { |
| 92 | u64 pages_list[PAGELIST_ENTRIES_PER_PAGE]; |
| 93 | u64 next_page_data; |
| 94 | } *pages_data; |
| 95 | |
| 96 | /* |
| 97 | * A Memory buffer is described in chunks of 4k. The list of |
| 98 | * physical addresses has to be represented by a physical pointer |
| 99 | * too and a single list has to start at a 4k page and fit into |
| 100 | * that page. In order to be able to describe large memory buffers |
| 101 | * these 4k pages carrying physical addresses are linked together |
| 102 | * in a list. See OPTEE_MSG_ATTR_NONCONTIG in |
| 103 | * drivers/tee/optee/optee_msg.h for more information. |
| 104 | */ |
| 105 | |
| 106 | page_offset = (ulong)buf & page_mask; |
| 107 | num_pages = roundup(page_offset + len, page_size) / page_size; |
| 108 | list_size = DIV_ROUND_UP(num_pages, PAGELIST_ENTRIES_PER_PAGE) * |
| 109 | page_size; |
| 110 | page_list = memalign(page_size, list_size); |
| 111 | if (!page_list) |
| 112 | return NULL; |
| 113 | |
| 114 | pages_data = page_list; |
| 115 | buf_base = (u8 *)rounddown((ulong)buf, page_size); |
| 116 | n = 0; |
| 117 | while (num_pages) { |
| 118 | pages_data->pages_list[n] = virt_to_phys(buf_base); |
| 119 | n++; |
| 120 | buf_base += page_size; |
| 121 | num_pages--; |
| 122 | |
| 123 | if (n == PAGELIST_ENTRIES_PER_PAGE) { |
| 124 | pages_data->next_page_data = |
| 125 | virt_to_phys(pages_data + 1); |
| 126 | pages_data++; |
| 127 | n = 0; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | *phys_buf_ptr = virt_to_phys(page_list) | page_offset; |
| 132 | return page_list; |
| 133 | } |
| 134 | |
| 135 | static void optee_get_version(struct udevice *dev, |
| 136 | struct tee_version_data *vers) |
| 137 | { |
| 138 | struct tee_version_data v = { |
| 139 | .gen_caps = TEE_GEN_CAP_GP | TEE_GEN_CAP_REG_MEM, |
| 140 | }; |
| 141 | |
| 142 | *vers = v; |
| 143 | } |
| 144 | |
| 145 | static int get_msg_arg(struct udevice *dev, uint num_params, |
| 146 | struct tee_shm **shmp, struct optee_msg_arg **msg_arg) |
| 147 | { |
| 148 | int rc; |
| 149 | struct optee_msg_arg *ma; |
| 150 | |
| 151 | rc = __tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL, |
| 152 | OPTEE_MSG_GET_ARG_SIZE(num_params), TEE_SHM_ALLOC, |
| 153 | shmp); |
| 154 | if (rc) |
| 155 | return rc; |
| 156 | |
| 157 | ma = (*shmp)->addr; |
| 158 | memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params)); |
| 159 | ma->num_params = num_params; |
| 160 | *msg_arg = ma; |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int to_msg_param(struct optee_msg_param *msg_params, uint num_params, |
| 166 | const struct tee_param *params) |
| 167 | { |
| 168 | uint n; |
| 169 | |
| 170 | for (n = 0; n < num_params; n++) { |
| 171 | const struct tee_param *p = params + n; |
| 172 | struct optee_msg_param *mp = msg_params + n; |
| 173 | |
| 174 | switch (p->attr) { |
| 175 | case TEE_PARAM_ATTR_TYPE_NONE: |
| 176 | mp->attr = OPTEE_MSG_ATTR_TYPE_NONE; |
| 177 | memset(&mp->u, 0, sizeof(mp->u)); |
| 178 | break; |
| 179 | case TEE_PARAM_ATTR_TYPE_VALUE_INPUT: |
| 180 | case TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT: |
| 181 | case TEE_PARAM_ATTR_TYPE_VALUE_INOUT: |
| 182 | mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr - |
| 183 | TEE_PARAM_ATTR_TYPE_VALUE_INPUT; |
| 184 | mp->u.value.a = p->u.value.a; |
| 185 | mp->u.value.b = p->u.value.b; |
| 186 | mp->u.value.c = p->u.value.c; |
| 187 | break; |
| 188 | case TEE_PARAM_ATTR_TYPE_MEMREF_INPUT: |
| 189 | case TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT: |
| 190 | case TEE_PARAM_ATTR_TYPE_MEMREF_INOUT: |
| 191 | mp->attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr - |
| 192 | TEE_PARAM_ATTR_TYPE_MEMREF_INPUT; |
| 193 | mp->u.rmem.shm_ref = (ulong)p->u.memref.shm; |
| 194 | mp->u.rmem.size = p->u.memref.size; |
| 195 | mp->u.rmem.offs = p->u.memref.shm_offs; |
| 196 | break; |
| 197 | default: |
| 198 | return -EINVAL; |
| 199 | } |
| 200 | } |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int from_msg_param(struct tee_param *params, uint num_params, |
| 205 | const struct optee_msg_param *msg_params) |
| 206 | { |
| 207 | uint n; |
| 208 | struct tee_shm *shm; |
| 209 | |
| 210 | for (n = 0; n < num_params; n++) { |
| 211 | struct tee_param *p = params + n; |
| 212 | const struct optee_msg_param *mp = msg_params + n; |
| 213 | u32 attr = mp->attr & OPTEE_MSG_ATTR_TYPE_MASK; |
| 214 | |
| 215 | switch (attr) { |
| 216 | case OPTEE_MSG_ATTR_TYPE_NONE: |
| 217 | p->attr = TEE_PARAM_ATTR_TYPE_NONE; |
| 218 | memset(&p->u, 0, sizeof(p->u)); |
| 219 | break; |
| 220 | case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT: |
| 221 | case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT: |
| 222 | case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT: |
| 223 | p->attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT + attr - |
| 224 | OPTEE_MSG_ATTR_TYPE_VALUE_INPUT; |
| 225 | p->u.value.a = mp->u.value.a; |
| 226 | p->u.value.b = mp->u.value.b; |
| 227 | p->u.value.c = mp->u.value.c; |
| 228 | break; |
| 229 | case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT: |
| 230 | case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT: |
| 231 | case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT: |
| 232 | p->attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT + attr - |
| 233 | OPTEE_MSG_ATTR_TYPE_RMEM_INPUT; |
| 234 | p->u.memref.size = mp->u.rmem.size; |
| 235 | shm = (struct tee_shm *)(ulong)mp->u.rmem.shm_ref; |
| 236 | |
| 237 | if (!shm) { |
| 238 | p->u.memref.shm_offs = 0; |
| 239 | p->u.memref.shm = NULL; |
| 240 | break; |
| 241 | } |
| 242 | p->u.memref.shm_offs = mp->u.rmem.offs; |
| 243 | p->u.memref.shm = shm; |
| 244 | break; |
| 245 | default: |
| 246 | return -EINVAL; |
| 247 | } |
| 248 | } |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static void handle_rpc(struct udevice *dev, struct rpc_param *param, |
| 253 | void *page_list) |
| 254 | { |
| 255 | struct tee_shm *shm; |
| 256 | |
| 257 | switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) { |
| 258 | case OPTEE_SMC_RPC_FUNC_ALLOC: |
| 259 | if (!__tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL, |
| 260 | param->a1, TEE_SHM_ALLOC | TEE_SHM_REGISTER, |
| 261 | &shm)) { |
| 262 | reg_pair_from_64(¶m->a1, ¶m->a2, |
| 263 | virt_to_phys(shm->addr)); |
| 264 | /* "cookie" */ |
| 265 | reg_pair_from_64(¶m->a4, ¶m->a5, (ulong)shm); |
| 266 | } else { |
| 267 | param->a1 = 0; |
| 268 | param->a2 = 0; |
| 269 | param->a4 = 0; |
| 270 | param->a5 = 0; |
| 271 | } |
| 272 | break; |
| 273 | case OPTEE_SMC_RPC_FUNC_FREE: |
| 274 | shm = reg_pair_to_ptr(param->a1, param->a2); |
| 275 | tee_shm_free(shm); |
| 276 | break; |
| 277 | case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR: |
| 278 | break; |
| 279 | case OPTEE_SMC_RPC_FUNC_CMD: |
| 280 | shm = reg_pair_to_ptr(param->a1, param->a2); |
| 281 | optee_suppl_cmd(dev, shm, page_list); |
| 282 | break; |
| 283 | default: |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | param->a0 = OPTEE_SMC_CALL_RETURN_FROM_RPC; |
| 288 | } |
| 289 | |
| 290 | static u32 call_err_to_res(u32 call_err) |
| 291 | { |
| 292 | switch (call_err) { |
| 293 | case OPTEE_SMC_RETURN_OK: |
| 294 | return TEE_SUCCESS; |
| 295 | default: |
| 296 | return TEE_ERROR_BAD_PARAMETERS; |
| 297 | } |
| 298 | } |
| 299 | |
Etienne Carriere | 1ba9678 | 2021-05-19 16:27:41 +0200 | [diff] [blame] | 300 | static void flush_shm_dcache(struct udevice *dev, struct optee_msg_arg *arg) |
| 301 | { |
| 302 | size_t sz = OPTEE_MSG_GET_ARG_SIZE(arg->num_params); |
| 303 | |
| 304 | flush_dcache_range(rounddown((ulong)arg, CONFIG_SYS_CACHELINE_SIZE), |
| 305 | roundup((ulong)arg + sz, CONFIG_SYS_CACHELINE_SIZE)); |
| 306 | |
| 307 | tee_flush_all_shm_dcache(dev); |
| 308 | } |
| 309 | |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 310 | static u32 do_call_with_arg(struct udevice *dev, struct optee_msg_arg *arg) |
| 311 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 312 | struct optee_pdata *pdata = dev_get_plat(dev); |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 313 | struct rpc_param param = { .a0 = OPTEE_SMC_CALL_WITH_ARG }; |
| 314 | void *page_list = NULL; |
| 315 | |
| 316 | reg_pair_from_64(¶m.a1, ¶m.a2, virt_to_phys(arg)); |
| 317 | while (true) { |
| 318 | struct arm_smccc_res res; |
| 319 | |
Etienne Carriere | 1ba9678 | 2021-05-19 16:27:41 +0200 | [diff] [blame] | 320 | /* If cache are off from U-Boot, sync the cache shared with OP-TEE */ |
| 321 | if (!dcache_status()) |
| 322 | flush_shm_dcache(dev, arg); |
| 323 | |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 324 | pdata->invoke_fn(param.a0, param.a1, param.a2, param.a3, |
| 325 | param.a4, param.a5, param.a6, param.a7, &res); |
| 326 | |
Etienne Carriere | 1ba9678 | 2021-05-19 16:27:41 +0200 | [diff] [blame] | 327 | /* If cache are off from U-Boot, sync the cache shared with OP-TEE */ |
| 328 | if (!dcache_status()) |
| 329 | flush_shm_dcache(dev, arg); |
| 330 | |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 331 | free(page_list); |
| 332 | page_list = NULL; |
| 333 | |
| 334 | if (OPTEE_SMC_RETURN_IS_RPC(res.a0)) { |
| 335 | param.a0 = res.a0; |
| 336 | param.a1 = res.a1; |
| 337 | param.a2 = res.a2; |
| 338 | param.a3 = res.a3; |
| 339 | handle_rpc(dev, ¶m, &page_list); |
| 340 | } else { |
Jens Wiklander | f1420dd | 2018-09-25 16:40:14 +0200 | [diff] [blame] | 341 | /* |
| 342 | * In case we've accessed RPMB to serve an RPC |
| 343 | * request we need to restore the previously |
| 344 | * selected partition as the caller may expect it |
| 345 | * to remain unchanged. |
| 346 | */ |
| 347 | optee_suppl_rpmb_release(dev); |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 348 | return call_err_to_res(res.a0); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | static int optee_close_session(struct udevice *dev, u32 session) |
| 354 | { |
| 355 | int rc; |
| 356 | struct tee_shm *shm; |
| 357 | struct optee_msg_arg *msg_arg; |
| 358 | |
| 359 | rc = get_msg_arg(dev, 0, &shm, &msg_arg); |
| 360 | if (rc) |
| 361 | return rc; |
| 362 | |
| 363 | msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION; |
| 364 | msg_arg->session = session; |
| 365 | do_call_with_arg(dev, msg_arg); |
| 366 | |
| 367 | tee_shm_free(shm); |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static int optee_open_session(struct udevice *dev, |
| 373 | struct tee_open_session_arg *arg, |
| 374 | uint num_params, struct tee_param *params) |
| 375 | { |
| 376 | int rc; |
| 377 | struct tee_shm *shm; |
| 378 | struct optee_msg_arg *msg_arg; |
| 379 | |
| 380 | rc = get_msg_arg(dev, num_params + 2, &shm, &msg_arg); |
| 381 | if (rc) |
| 382 | return rc; |
| 383 | |
| 384 | msg_arg->cmd = OPTEE_MSG_CMD_OPEN_SESSION; |
| 385 | /* |
| 386 | * Initialize and add the meta parameters needed when opening a |
| 387 | * session. |
| 388 | */ |
| 389 | msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT | |
| 390 | OPTEE_MSG_ATTR_META; |
| 391 | msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT | |
| 392 | OPTEE_MSG_ATTR_META; |
| 393 | memcpy(&msg_arg->params[0].u.value, arg->uuid, sizeof(arg->uuid)); |
| 394 | memcpy(&msg_arg->params[1].u.value, arg->uuid, sizeof(arg->clnt_uuid)); |
| 395 | msg_arg->params[1].u.value.c = arg->clnt_login; |
| 396 | |
| 397 | rc = to_msg_param(msg_arg->params + 2, num_params, params); |
| 398 | if (rc) |
| 399 | goto out; |
| 400 | |
| 401 | arg->ret = do_call_with_arg(dev, msg_arg); |
| 402 | if (arg->ret) { |
| 403 | arg->ret_origin = TEE_ORIGIN_COMMS; |
| 404 | goto out; |
| 405 | } |
| 406 | |
| 407 | if (from_msg_param(params, num_params, msg_arg->params + 2)) { |
| 408 | arg->ret = TEE_ERROR_COMMUNICATION; |
| 409 | arg->ret_origin = TEE_ORIGIN_COMMS; |
| 410 | /* Close session again to avoid leakage */ |
| 411 | optee_close_session(dev, msg_arg->session); |
| 412 | goto out; |
| 413 | } |
| 414 | |
| 415 | arg->session = msg_arg->session; |
| 416 | arg->ret = msg_arg->ret; |
| 417 | arg->ret_origin = msg_arg->ret_origin; |
| 418 | out: |
| 419 | tee_shm_free(shm); |
| 420 | |
| 421 | return rc; |
| 422 | } |
| 423 | |
| 424 | static int optee_invoke_func(struct udevice *dev, struct tee_invoke_arg *arg, |
| 425 | uint num_params, struct tee_param *params) |
| 426 | { |
| 427 | struct tee_shm *shm; |
| 428 | struct optee_msg_arg *msg_arg; |
| 429 | int rc; |
| 430 | |
| 431 | rc = get_msg_arg(dev, num_params, &shm, &msg_arg); |
| 432 | if (rc) |
| 433 | return rc; |
| 434 | msg_arg->cmd = OPTEE_MSG_CMD_INVOKE_COMMAND; |
| 435 | msg_arg->func = arg->func; |
| 436 | msg_arg->session = arg->session; |
| 437 | |
| 438 | rc = to_msg_param(msg_arg->params, num_params, params); |
| 439 | if (rc) |
| 440 | goto out; |
| 441 | |
| 442 | arg->ret = do_call_with_arg(dev, msg_arg); |
| 443 | if (arg->ret) { |
| 444 | arg->ret_origin = TEE_ORIGIN_COMMS; |
| 445 | goto out; |
| 446 | } |
| 447 | |
| 448 | if (from_msg_param(params, num_params, msg_arg->params)) { |
| 449 | arg->ret = TEE_ERROR_COMMUNICATION; |
| 450 | arg->ret_origin = TEE_ORIGIN_COMMS; |
| 451 | goto out; |
| 452 | } |
| 453 | |
| 454 | arg->ret = msg_arg->ret; |
| 455 | arg->ret_origin = msg_arg->ret_origin; |
| 456 | out: |
| 457 | tee_shm_free(shm); |
| 458 | return rc; |
| 459 | } |
| 460 | |
| 461 | static int optee_shm_register(struct udevice *dev, struct tee_shm *shm) |
| 462 | { |
| 463 | struct tee_shm *shm_arg; |
| 464 | struct optee_msg_arg *msg_arg; |
| 465 | void *pl; |
| 466 | u64 ph_ptr; |
| 467 | int rc; |
| 468 | |
| 469 | rc = get_msg_arg(dev, 1, &shm_arg, &msg_arg); |
| 470 | if (rc) |
| 471 | return rc; |
| 472 | |
| 473 | pl = optee_alloc_and_init_page_list(shm->addr, shm->size, &ph_ptr); |
| 474 | if (!pl) { |
| 475 | rc = -ENOMEM; |
| 476 | goto out; |
| 477 | } |
| 478 | |
| 479 | msg_arg->cmd = OPTEE_MSG_CMD_REGISTER_SHM; |
| 480 | msg_arg->params->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT | |
| 481 | OPTEE_MSG_ATTR_NONCONTIG; |
| 482 | msg_arg->params->u.tmem.buf_ptr = ph_ptr; |
| 483 | msg_arg->params->u.tmem.shm_ref = (ulong)shm; |
| 484 | msg_arg->params->u.tmem.size = shm->size; |
| 485 | |
| 486 | if (do_call_with_arg(dev, msg_arg) || msg_arg->ret) |
| 487 | rc = -EINVAL; |
| 488 | |
| 489 | free(pl); |
| 490 | out: |
| 491 | tee_shm_free(shm_arg); |
| 492 | |
| 493 | return rc; |
| 494 | } |
| 495 | |
| 496 | static int optee_shm_unregister(struct udevice *dev, struct tee_shm *shm) |
| 497 | { |
| 498 | struct tee_shm *shm_arg; |
| 499 | struct optee_msg_arg *msg_arg; |
| 500 | int rc; |
| 501 | |
| 502 | rc = get_msg_arg(dev, 1, &shm_arg, &msg_arg); |
| 503 | if (rc) |
| 504 | return rc; |
| 505 | |
| 506 | msg_arg->cmd = OPTEE_MSG_CMD_UNREGISTER_SHM; |
| 507 | msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT; |
| 508 | msg_arg->params[0].u.rmem.shm_ref = (ulong)shm; |
| 509 | |
| 510 | if (do_call_with_arg(dev, msg_arg) || msg_arg->ret) |
| 511 | rc = -EINVAL; |
| 512 | tee_shm_free(shm_arg); |
| 513 | |
| 514 | return rc; |
| 515 | } |
| 516 | |
| 517 | static const struct tee_driver_ops optee_ops = { |
| 518 | .get_version = optee_get_version, |
| 519 | .open_session = optee_open_session, |
| 520 | .close_session = optee_close_session, |
| 521 | .invoke_func = optee_invoke_func, |
| 522 | .shm_register = optee_shm_register, |
| 523 | .shm_unregister = optee_shm_unregister, |
| 524 | }; |
| 525 | |
| 526 | static bool is_optee_api(optee_invoke_fn *invoke_fn) |
| 527 | { |
| 528 | struct arm_smccc_res res; |
| 529 | |
| 530 | invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res); |
| 531 | |
| 532 | return res.a0 == OPTEE_MSG_UID_0 && res.a1 == OPTEE_MSG_UID_1 && |
| 533 | res.a2 == OPTEE_MSG_UID_2 && res.a3 == OPTEE_MSG_UID_3; |
| 534 | } |
| 535 | |
Patrick Delaunay | bb7e793 | 2020-03-02 13:21:53 +0100 | [diff] [blame] | 536 | static void print_os_revision(struct udevice *dev, optee_invoke_fn *invoke_fn) |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 537 | { |
| 538 | union { |
| 539 | struct arm_smccc_res smccc; |
| 540 | struct optee_smc_call_get_os_revision_result result; |
| 541 | } res = { |
| 542 | .result = { |
| 543 | .build_id = 0 |
| 544 | } |
| 545 | }; |
| 546 | |
| 547 | invoke_fn(OPTEE_SMC_CALL_GET_OS_REVISION, 0, 0, 0, 0, 0, 0, 0, |
| 548 | &res.smccc); |
| 549 | |
| 550 | if (res.result.build_id) |
Patrick Delaunay | bb7e793 | 2020-03-02 13:21:53 +0100 | [diff] [blame] | 551 | dev_info(dev, "OP-TEE: revision %lu.%lu (%08lx)\n", |
| 552 | res.result.major, res.result.minor, |
| 553 | res.result.build_id); |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 554 | else |
Patrick Delaunay | bb7e793 | 2020-03-02 13:21:53 +0100 | [diff] [blame] | 555 | dev_info(dev, "OP-TEE: revision %lu.%lu\n", |
| 556 | res.result.major, res.result.minor); |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static bool api_revision_is_compatible(optee_invoke_fn *invoke_fn) |
| 560 | { |
| 561 | union { |
| 562 | struct arm_smccc_res smccc; |
| 563 | struct optee_smc_calls_revision_result result; |
| 564 | } res; |
| 565 | |
| 566 | invoke_fn(OPTEE_SMC_CALLS_REVISION, 0, 0, 0, 0, 0, 0, 0, &res.smccc); |
| 567 | |
| 568 | return res.result.major == OPTEE_MSG_REVISION_MAJOR && |
| 569 | (int)res.result.minor >= OPTEE_MSG_REVISION_MINOR; |
| 570 | } |
| 571 | |
| 572 | static bool exchange_capabilities(optee_invoke_fn *invoke_fn, u32 *sec_caps) |
| 573 | { |
| 574 | union { |
| 575 | struct arm_smccc_res smccc; |
| 576 | struct optee_smc_exchange_capabilities_result result; |
| 577 | } res; |
| 578 | |
| 579 | invoke_fn(OPTEE_SMC_EXCHANGE_CAPABILITIES, |
| 580 | OPTEE_SMC_NSEC_CAP_UNIPROCESSOR, 0, 0, 0, 0, 0, 0, |
| 581 | &res.smccc); |
| 582 | |
| 583 | if (res.result.status != OPTEE_SMC_RETURN_OK) |
| 584 | return false; |
| 585 | |
| 586 | *sec_caps = res.result.capabilities; |
| 587 | |
| 588 | return true; |
| 589 | } |
| 590 | |
| 591 | /* Simple wrapper functions to be able to use a function pointer */ |
| 592 | static void optee_smccc_smc(unsigned long a0, unsigned long a1, |
| 593 | unsigned long a2, unsigned long a3, |
| 594 | unsigned long a4, unsigned long a5, |
| 595 | unsigned long a6, unsigned long a7, |
| 596 | struct arm_smccc_res *res) |
| 597 | { |
| 598 | arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res); |
| 599 | } |
| 600 | |
| 601 | static void optee_smccc_hvc(unsigned long a0, unsigned long a1, |
| 602 | unsigned long a2, unsigned long a3, |
| 603 | unsigned long a4, unsigned long a5, |
| 604 | unsigned long a6, unsigned long a7, |
| 605 | struct arm_smccc_res *res) |
| 606 | { |
| 607 | arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res); |
| 608 | } |
| 609 | |
| 610 | static optee_invoke_fn *get_invoke_func(struct udevice *dev) |
| 611 | { |
| 612 | const char *method; |
| 613 | |
| 614 | debug("optee: looking for conduit method in DT.\n"); |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 615 | method = ofnode_get_property(dev_ofnode(dev), "method", NULL); |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 616 | if (!method) { |
| 617 | debug("optee: missing \"method\" property\n"); |
| 618 | return ERR_PTR(-ENXIO); |
| 619 | } |
| 620 | |
| 621 | if (!strcmp("hvc", method)) |
| 622 | return optee_smccc_hvc; |
| 623 | else if (!strcmp("smc", method)) |
| 624 | return optee_smccc_smc; |
| 625 | |
| 626 | debug("optee: invalid \"method\" property: %s\n", method); |
| 627 | return ERR_PTR(-EINVAL); |
| 628 | } |
| 629 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 630 | static int optee_of_to_plat(struct udevice *dev) |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 631 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 632 | struct optee_pdata *pdata = dev_get_plat(dev); |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 633 | |
| 634 | pdata->invoke_fn = get_invoke_func(dev); |
| 635 | if (IS_ERR(pdata->invoke_fn)) |
| 636 | return PTR_ERR(pdata->invoke_fn); |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static int optee_probe(struct udevice *dev) |
| 642 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 643 | struct optee_pdata *pdata = dev_get_plat(dev); |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 644 | u32 sec_caps; |
Patrick Delaunay | 86a586b | 2022-03-29 14:21:24 +0200 | [diff] [blame] | 645 | struct udevice *child; |
| 646 | int ret; |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 647 | |
| 648 | if (!is_optee_api(pdata->invoke_fn)) { |
Ilias Apalodimas | b91f100 | 2021-03-10 15:35:11 +0200 | [diff] [blame] | 649 | dev_err(dev, "OP-TEE api uid mismatch\n"); |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 650 | return -ENOENT; |
| 651 | } |
| 652 | |
Patrick Delaunay | bb7e793 | 2020-03-02 13:21:53 +0100 | [diff] [blame] | 653 | print_os_revision(dev, pdata->invoke_fn); |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 654 | |
| 655 | if (!api_revision_is_compatible(pdata->invoke_fn)) { |
Ilias Apalodimas | b91f100 | 2021-03-10 15:35:11 +0200 | [diff] [blame] | 656 | dev_err(dev, "OP-TEE api revision mismatch\n"); |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 657 | return -ENOENT; |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * OP-TEE can use both shared memory via predefined pool or as |
| 662 | * dynamic shared memory provided by normal world. To keep things |
| 663 | * simple we're only using dynamic shared memory in this driver. |
| 664 | */ |
| 665 | if (!exchange_capabilities(pdata->invoke_fn, &sec_caps) || |
| 666 | !(sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)) { |
Ilias Apalodimas | b91f100 | 2021-03-10 15:35:11 +0200 | [diff] [blame] | 667 | dev_err(dev, "OP-TEE capabilities mismatch\n"); |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 668 | return -ENOENT; |
| 669 | } |
| 670 | |
Patrick Delaunay | 86a586b | 2022-03-29 14:21:24 +0200 | [diff] [blame] | 671 | /* |
| 672 | * in U-Boot, the discovery of TA on the TEE bus is not supported: |
| 673 | * only bind the drivers associated to the supported OP-TEE TA |
| 674 | */ |
| 675 | if (IS_ENABLED(CONFIG_RNG_OPTEE)) { |
| 676 | ret = device_bind_driver(dev, "optee-rng", "optee-rng", &child); |
| 677 | if (ret) |
| 678 | return ret; |
| 679 | } |
| 680 | |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | static const struct udevice_id optee_match[] = { |
| 685 | { .compatible = "linaro,optee-tz" }, |
| 686 | {}, |
| 687 | }; |
| 688 | |
| 689 | U_BOOT_DRIVER(optee) = { |
| 690 | .name = "optee", |
| 691 | .id = UCLASS_TEE, |
| 692 | .of_match = optee_match, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 693 | .of_to_plat = optee_of_to_plat, |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 694 | .probe = optee_probe, |
| 695 | .ops = &optee_ops, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 696 | .plat_auto = sizeof(struct optee_pdata), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 697 | .priv_auto = sizeof(struct optee_private), |
Jens Wiklander | 2b7216b | 2018-09-25 16:40:11 +0200 | [diff] [blame] | 698 | }; |