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