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