blob: 47f845cffe378d78cfdb176dab17b3d0c7ca81ad [file] [log] [blame]
Jens Wiklander2b7216b2018-09-25 16:40:11 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
Etienne Carriere1ba96782021-05-19 16:27:41 +02003 * Copyright (c) 2018-2020 Linaro Limited
Jens Wiklander2b7216b2018-09-25 16:40:11 +02004 */
5
6#include <common.h>
Etienne Carriere1ba96782021-05-19 16:27:41 +02007#include <cpu_func.h>
Jens Wiklander2b7216b2018-09-25 16:40:11 +02008#include <dm.h>
Sean Anderson47364162020-10-04 21:39:51 -04009#include <dm/device_compat.h>
Patrick Delaunay86a586b2022-03-29 14:21:24 +020010#include <dm/lists.h>
Jens Wiklander2b7216b2018-09-25 16:40:11 +020011#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Jens Wiklander2b7216b2018-09-25 16:40:11 +020013#include <tee.h>
14#include <linux/arm-smccc.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070015#include <linux/err.h>
Jens Wiklander2b7216b2018-09-25 16:40:11 +020016#include <linux/io.h>
Etienne Carriere523a9512022-07-26 16:21:42 +020017#include <tee/optee_service.h>
Jens Wiklander2b7216b2018-09-25 16:40:11 +020018
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 Carriere523a9512022-07-26 16:21:42 +020026/*
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 Wiklander2b7216b2018-09-25 16:40:11 +020045typedef 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
50struct optee_pdata {
51 optee_invoke_fn *invoke_fn;
52};
53
54struct 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 Carriere523a9512022-07-26 16:21:42 +020065static 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);
Ilias Apalodimasfe7ead72023-01-19 11:21:37 +020076 if (!memcmp(uuid, loc_uuid, sizeof(*uuid)))
Etienne Carriere523a9512022-07-26 16:21:42 +020077 return service;
78 }
79
80 return NULL;
81}
82
83static 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
Patrick Delaunay86363662022-12-15 09:54:52 +010095 ret = device_bind_driver_to_node(dev, service->driver_name, service->driver_name,
96 dev_ofnode(dev), NULL);
Etienne Carriere523a9512022-07-26 16:21:42 +020097 if (ret) {
98 dev_warn(dev, "%s was not bound: %d, ignored\n", service->driver_name, ret);
99 continue;
100 }
101 }
102
103 return 0;
104}
105
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100106static int __enum_services(struct udevice *dev, struct tee_shm *shm, size_t *shm_size, u32 tee_sess,
107 unsigned int pta_cmd)
Etienne Carriere523a9512022-07-26 16:21:42 +0200108{
109 struct tee_invoke_arg arg = { };
110 struct tee_param param = { };
111 int ret = 0;
112
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100113 arg.func = pta_cmd;
Etienne Carriere523a9512022-07-26 16:21:42 +0200114 arg.session = tee_sess;
115
116 /* Fill invoke cmd params */
117 param.attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
118 param.u.memref.shm = shm;
119 param.u.memref.size = *shm_size;
120
121 ret = tee_invoke_func(dev, &arg, 1, &param);
122 if (ret || (arg.ret && arg.ret != TEE_ERROR_SHORT_BUFFER)) {
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100123 dev_err(dev, "Enumeration command 0x%x failed: 0x%x\n", pta_cmd, arg.ret);
Etienne Carriere523a9512022-07-26 16:21:42 +0200124 return -EINVAL;
125 }
126
127 *shm_size = param.u.memref.size;
128
129 return 0;
130}
131
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100132static int enum_services(struct udevice *dev, struct tee_shm **shm, size_t *count, u32 tee_sess,
133 unsigned int pta_cmd)
Etienne Carriere523a9512022-07-26 16:21:42 +0200134{
135 size_t shm_size = 0;
136 int ret;
137
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100138 ret = __enum_services(dev, NULL, &shm_size, tee_sess, pta_cmd);
Etienne Carriere523a9512022-07-26 16:21:42 +0200139 if (ret)
140 return ret;
141
Etienne Carriere6df6e362023-11-29 13:37:53 +0100142 if (!shm_size) {
143 *count = 0;
144 return 0;
145 }
146
Etienne Carriere523a9512022-07-26 16:21:42 +0200147 ret = tee_shm_alloc(dev, shm_size, 0, shm);
148 if (ret) {
149 dev_err(dev, "Failed to allocated shared memory: %d\n", ret);
150 return ret;
151 }
152
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100153 ret = __enum_services(dev, *shm, &shm_size, tee_sess, pta_cmd);
Etienne Carriere523a9512022-07-26 16:21:42 +0200154 if (!ret)
155 *count = shm_size / sizeof(struct tee_optee_ta_uuid);
156
157 return ret;
158}
159
160static int open_enum_session(struct udevice *dev, u32 *tee_sess)
161{
162 const struct tee_optee_ta_uuid pta_uuid = PTA_DEVICE_ENUM;
163 struct tee_open_session_arg arg = { };
164 int ret;
165
166 tee_optee_ta_uuid_to_octets(arg.uuid, &pta_uuid);
167
168 ret = tee_open_session(dev, &arg, 0, NULL);
169 if (ret || arg.ret) {
170 if (!ret)
171 ret = -EIO;
172 return ret;
173 }
174
175 *tee_sess = arg.session;
176
177 return 0;
178}
179
180static int bind_service_drivers(struct udevice *dev)
181{
182 struct tee_shm *service_list = NULL;
183 size_t service_count;
184 u32 tee_sess;
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100185 int ret, ret2;
Etienne Carriere523a9512022-07-26 16:21:42 +0200186
187 ret = open_enum_session(dev, &tee_sess);
188 if (ret)
189 return ret;
190
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100191 ret = enum_services(dev, &service_list, &service_count, tee_sess,
192 PTA_CMD_GET_DEVICES);
Etienne Carriere6df6e362023-11-29 13:37:53 +0100193 if (!ret && service_count)
Etienne Carriere523a9512022-07-26 16:21:42 +0200194 ret = bind_service_list(dev, service_list, service_count);
195
196 tee_shm_free(service_list);
Etienne Carriere6df6e362023-11-29 13:37:53 +0100197 service_list = NULL;
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100198
199 ret2 = enum_services(dev, &service_list, &service_count, tee_sess,
200 PTA_CMD_GET_DEVICES_SUPP);
Etienne Carriere6df6e362023-11-29 13:37:53 +0100201 if (!ret2 && service_count)
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100202 ret2 = bind_service_list(dev, service_list, service_count);
203
204 tee_shm_free(service_list);
205
Etienne Carriere523a9512022-07-26 16:21:42 +0200206 tee_close_session(dev, tee_sess);
207
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100208 if (ret)
209 return ret;
210
211 return ret2;
Etienne Carriere523a9512022-07-26 16:21:42 +0200212}
213
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200214/**
215 * reg_pair_to_ptr() - Make a pointer of 2 32-bit values
216 * @reg0: High bits of the pointer
217 * @reg1: Low bits of the pointer
218 *
219 * Returns the combined result, note that if a pointer is 32-bit wide @reg0
220 * will be discarded.
221 */
222static void *reg_pair_to_ptr(u32 reg0, u32 reg1)
223{
224 return (void *)(ulong)(((u64)reg0 << 32) | reg1);
225}
226
227/**
228 * reg_pair_from_64() - Split a 64-bit value into two 32-bit values
229 * @reg0: High bits of @val
230 * @reg1: Low bits of @val
231 * @val: The value to split
232 */
233static void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
234{
235 *reg0 = val >> 32;
236 *reg1 = val;
237}
238
239/**
240 * optee_alloc_and_init_page_list() - Provide page list of memory buffer
241 * @buf: Start of buffer
242 * @len: Length of buffer
243 * @phys_buf_ptr Physical pointer with coded offset to page list
244 *
245 * Secure world doesn't share mapping with Normal world (U-Boot in this case)
246 * so physical pointers are needed when sharing pointers.
247 *
248 * Returns a pointer page list on success or NULL on failure
249 */
250void *optee_alloc_and_init_page_list(void *buf, ulong len, u64 *phys_buf_ptr)
251{
252 const unsigned int page_size = OPTEE_MSG_NONCONTIG_PAGE_SIZE;
253 const phys_addr_t page_mask = page_size - 1;
254 u8 *buf_base;
255 unsigned int page_offset;
256 unsigned int num_pages;
257 unsigned int list_size;
258 unsigned int n;
259 void *page_list;
260 struct {
261 u64 pages_list[PAGELIST_ENTRIES_PER_PAGE];
262 u64 next_page_data;
263 } *pages_data;
264
265 /*
266 * A Memory buffer is described in chunks of 4k. The list of
267 * physical addresses has to be represented by a physical pointer
268 * too and a single list has to start at a 4k page and fit into
269 * that page. In order to be able to describe large memory buffers
270 * these 4k pages carrying physical addresses are linked together
271 * in a list. See OPTEE_MSG_ATTR_NONCONTIG in
272 * drivers/tee/optee/optee_msg.h for more information.
273 */
274
275 page_offset = (ulong)buf & page_mask;
276 num_pages = roundup(page_offset + len, page_size) / page_size;
277 list_size = DIV_ROUND_UP(num_pages, PAGELIST_ENTRIES_PER_PAGE) *
278 page_size;
279 page_list = memalign(page_size, list_size);
280 if (!page_list)
281 return NULL;
282
283 pages_data = page_list;
284 buf_base = (u8 *)rounddown((ulong)buf, page_size);
285 n = 0;
286 while (num_pages) {
287 pages_data->pages_list[n] = virt_to_phys(buf_base);
288 n++;
289 buf_base += page_size;
290 num_pages--;
291
292 if (n == PAGELIST_ENTRIES_PER_PAGE) {
293 pages_data->next_page_data =
294 virt_to_phys(pages_data + 1);
295 pages_data++;
296 n = 0;
297 }
298 }
299
300 *phys_buf_ptr = virt_to_phys(page_list) | page_offset;
301 return page_list;
302}
303
304static void optee_get_version(struct udevice *dev,
305 struct tee_version_data *vers)
306{
307 struct tee_version_data v = {
308 .gen_caps = TEE_GEN_CAP_GP | TEE_GEN_CAP_REG_MEM,
309 };
310
311 *vers = v;
312}
313
314static int get_msg_arg(struct udevice *dev, uint num_params,
315 struct tee_shm **shmp, struct optee_msg_arg **msg_arg)
316{
317 int rc;
318 struct optee_msg_arg *ma;
319
320 rc = __tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL,
321 OPTEE_MSG_GET_ARG_SIZE(num_params), TEE_SHM_ALLOC,
322 shmp);
323 if (rc)
324 return rc;
325
326 ma = (*shmp)->addr;
327 memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));
328 ma->num_params = num_params;
329 *msg_arg = ma;
330
331 return 0;
332}
333
334static int to_msg_param(struct optee_msg_param *msg_params, uint num_params,
335 const struct tee_param *params)
336{
337 uint n;
338
339 for (n = 0; n < num_params; n++) {
340 const struct tee_param *p = params + n;
341 struct optee_msg_param *mp = msg_params + n;
342
343 switch (p->attr) {
344 case TEE_PARAM_ATTR_TYPE_NONE:
345 mp->attr = OPTEE_MSG_ATTR_TYPE_NONE;
346 memset(&mp->u, 0, sizeof(mp->u));
347 break;
348 case TEE_PARAM_ATTR_TYPE_VALUE_INPUT:
349 case TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT:
350 case TEE_PARAM_ATTR_TYPE_VALUE_INOUT:
351 mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -
352 TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
353 mp->u.value.a = p->u.value.a;
354 mp->u.value.b = p->u.value.b;
355 mp->u.value.c = p->u.value.c;
356 break;
357 case TEE_PARAM_ATTR_TYPE_MEMREF_INPUT:
358 case TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
359 case TEE_PARAM_ATTR_TYPE_MEMREF_INOUT:
360 mp->attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr -
361 TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
362 mp->u.rmem.shm_ref = (ulong)p->u.memref.shm;
363 mp->u.rmem.size = p->u.memref.size;
364 mp->u.rmem.offs = p->u.memref.shm_offs;
365 break;
366 default:
367 return -EINVAL;
368 }
369 }
370 return 0;
371}
372
373static int from_msg_param(struct tee_param *params, uint num_params,
374 const struct optee_msg_param *msg_params)
375{
376 uint n;
377 struct tee_shm *shm;
378
379 for (n = 0; n < num_params; n++) {
380 struct tee_param *p = params + n;
381 const struct optee_msg_param *mp = msg_params + n;
382 u32 attr = mp->attr & OPTEE_MSG_ATTR_TYPE_MASK;
383
384 switch (attr) {
385 case OPTEE_MSG_ATTR_TYPE_NONE:
386 p->attr = TEE_PARAM_ATTR_TYPE_NONE;
387 memset(&p->u, 0, sizeof(p->u));
388 break;
389 case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT:
390 case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT:
391 case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT:
392 p->attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT + attr -
393 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
394 p->u.value.a = mp->u.value.a;
395 p->u.value.b = mp->u.value.b;
396 p->u.value.c = mp->u.value.c;
397 break;
398 case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT:
399 case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT:
400 case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT:
401 p->attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT + attr -
402 OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
403 p->u.memref.size = mp->u.rmem.size;
404 shm = (struct tee_shm *)(ulong)mp->u.rmem.shm_ref;
405
406 if (!shm) {
407 p->u.memref.shm_offs = 0;
408 p->u.memref.shm = NULL;
409 break;
410 }
411 p->u.memref.shm_offs = mp->u.rmem.offs;
412 p->u.memref.shm = shm;
413 break;
414 default:
415 return -EINVAL;
416 }
417 }
418 return 0;
419}
420
421static void handle_rpc(struct udevice *dev, struct rpc_param *param,
422 void *page_list)
423{
424 struct tee_shm *shm;
425
426 switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) {
427 case OPTEE_SMC_RPC_FUNC_ALLOC:
428 if (!__tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL,
429 param->a1, TEE_SHM_ALLOC | TEE_SHM_REGISTER,
430 &shm)) {
431 reg_pair_from_64(&param->a1, &param->a2,
432 virt_to_phys(shm->addr));
433 /* "cookie" */
434 reg_pair_from_64(&param->a4, &param->a5, (ulong)shm);
435 } else {
436 param->a1 = 0;
437 param->a2 = 0;
438 param->a4 = 0;
439 param->a5 = 0;
440 }
441 break;
442 case OPTEE_SMC_RPC_FUNC_FREE:
443 shm = reg_pair_to_ptr(param->a1, param->a2);
444 tee_shm_free(shm);
445 break;
446 case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR:
447 break;
448 case OPTEE_SMC_RPC_FUNC_CMD:
449 shm = reg_pair_to_ptr(param->a1, param->a2);
450 optee_suppl_cmd(dev, shm, page_list);
451 break;
452 default:
453 break;
454 }
455
456 param->a0 = OPTEE_SMC_CALL_RETURN_FROM_RPC;
457}
458
459static u32 call_err_to_res(u32 call_err)
460{
461 switch (call_err) {
462 case OPTEE_SMC_RETURN_OK:
463 return TEE_SUCCESS;
464 default:
465 return TEE_ERROR_BAD_PARAMETERS;
466 }
467}
468
Etienne Carriere1ba96782021-05-19 16:27:41 +0200469static void flush_shm_dcache(struct udevice *dev, struct optee_msg_arg *arg)
470{
471 size_t sz = OPTEE_MSG_GET_ARG_SIZE(arg->num_params);
472
473 flush_dcache_range(rounddown((ulong)arg, CONFIG_SYS_CACHELINE_SIZE),
474 roundup((ulong)arg + sz, CONFIG_SYS_CACHELINE_SIZE));
475
476 tee_flush_all_shm_dcache(dev);
477}
478
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200479static u32 do_call_with_arg(struct udevice *dev, struct optee_msg_arg *arg)
480{
Simon Glassfa20e932020-12-03 16:55:20 -0700481 struct optee_pdata *pdata = dev_get_plat(dev);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200482 struct rpc_param param = { .a0 = OPTEE_SMC_CALL_WITH_ARG };
483 void *page_list = NULL;
484
485 reg_pair_from_64(&param.a1, &param.a2, virt_to_phys(arg));
486 while (true) {
487 struct arm_smccc_res res;
488
Etienne Carriere1ba96782021-05-19 16:27:41 +0200489 /* If cache are off from U-Boot, sync the cache shared with OP-TEE */
490 if (!dcache_status())
491 flush_shm_dcache(dev, arg);
492
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200493 pdata->invoke_fn(param.a0, param.a1, param.a2, param.a3,
494 param.a4, param.a5, param.a6, param.a7, &res);
495
Etienne Carriere1ba96782021-05-19 16:27:41 +0200496 /* If cache are off from U-Boot, sync the cache shared with OP-TEE */
497 if (!dcache_status())
498 flush_shm_dcache(dev, arg);
499
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200500 free(page_list);
501 page_list = NULL;
502
503 if (OPTEE_SMC_RETURN_IS_RPC(res.a0)) {
504 param.a0 = res.a0;
505 param.a1 = res.a1;
506 param.a2 = res.a2;
507 param.a3 = res.a3;
508 handle_rpc(dev, &param, &page_list);
509 } else {
Jens Wiklanderf1420dd2018-09-25 16:40:14 +0200510 /*
511 * In case we've accessed RPMB to serve an RPC
512 * request we need to restore the previously
513 * selected partition as the caller may expect it
514 * to remain unchanged.
515 */
516 optee_suppl_rpmb_release(dev);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200517 return call_err_to_res(res.a0);
518 }
519 }
520}
521
522static int optee_close_session(struct udevice *dev, u32 session)
523{
524 int rc;
525 struct tee_shm *shm;
526 struct optee_msg_arg *msg_arg;
527
528 rc = get_msg_arg(dev, 0, &shm, &msg_arg);
529 if (rc)
530 return rc;
531
532 msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
533 msg_arg->session = session;
534 do_call_with_arg(dev, msg_arg);
535
536 tee_shm_free(shm);
537
538 return 0;
539}
540
541static int optee_open_session(struct udevice *dev,
542 struct tee_open_session_arg *arg,
543 uint num_params, struct tee_param *params)
544{
545 int rc;
546 struct tee_shm *shm;
547 struct optee_msg_arg *msg_arg;
548
549 rc = get_msg_arg(dev, num_params + 2, &shm, &msg_arg);
550 if (rc)
551 return rc;
552
553 msg_arg->cmd = OPTEE_MSG_CMD_OPEN_SESSION;
554 /*
555 * Initialize and add the meta parameters needed when opening a
556 * session.
557 */
558 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
559 OPTEE_MSG_ATTR_META;
560 msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
561 OPTEE_MSG_ATTR_META;
562 memcpy(&msg_arg->params[0].u.value, arg->uuid, sizeof(arg->uuid));
563 memcpy(&msg_arg->params[1].u.value, arg->uuid, sizeof(arg->clnt_uuid));
564 msg_arg->params[1].u.value.c = arg->clnt_login;
565
566 rc = to_msg_param(msg_arg->params + 2, num_params, params);
567 if (rc)
568 goto out;
569
570 arg->ret = do_call_with_arg(dev, msg_arg);
571 if (arg->ret) {
572 arg->ret_origin = TEE_ORIGIN_COMMS;
573 goto out;
574 }
575
576 if (from_msg_param(params, num_params, msg_arg->params + 2)) {
577 arg->ret = TEE_ERROR_COMMUNICATION;
578 arg->ret_origin = TEE_ORIGIN_COMMS;
579 /* Close session again to avoid leakage */
580 optee_close_session(dev, msg_arg->session);
581 goto out;
582 }
583
584 arg->session = msg_arg->session;
585 arg->ret = msg_arg->ret;
586 arg->ret_origin = msg_arg->ret_origin;
587out:
588 tee_shm_free(shm);
589
590 return rc;
591}
592
593static int optee_invoke_func(struct udevice *dev, struct tee_invoke_arg *arg,
594 uint num_params, struct tee_param *params)
595{
596 struct tee_shm *shm;
597 struct optee_msg_arg *msg_arg;
598 int rc;
599
600 rc = get_msg_arg(dev, num_params, &shm, &msg_arg);
601 if (rc)
602 return rc;
603 msg_arg->cmd = OPTEE_MSG_CMD_INVOKE_COMMAND;
604 msg_arg->func = arg->func;
605 msg_arg->session = arg->session;
606
607 rc = to_msg_param(msg_arg->params, num_params, params);
608 if (rc)
609 goto out;
610
611 arg->ret = do_call_with_arg(dev, msg_arg);
612 if (arg->ret) {
613 arg->ret_origin = TEE_ORIGIN_COMMS;
614 goto out;
615 }
616
617 if (from_msg_param(params, num_params, msg_arg->params)) {
618 arg->ret = TEE_ERROR_COMMUNICATION;
619 arg->ret_origin = TEE_ORIGIN_COMMS;
620 goto out;
621 }
622
623 arg->ret = msg_arg->ret;
624 arg->ret_origin = msg_arg->ret_origin;
625out:
626 tee_shm_free(shm);
627 return rc;
628}
629
630static int optee_shm_register(struct udevice *dev, struct tee_shm *shm)
631{
632 struct tee_shm *shm_arg;
633 struct optee_msg_arg *msg_arg;
634 void *pl;
635 u64 ph_ptr;
636 int rc;
637
638 rc = get_msg_arg(dev, 1, &shm_arg, &msg_arg);
639 if (rc)
640 return rc;
641
642 pl = optee_alloc_and_init_page_list(shm->addr, shm->size, &ph_ptr);
643 if (!pl) {
644 rc = -ENOMEM;
645 goto out;
646 }
647
648 msg_arg->cmd = OPTEE_MSG_CMD_REGISTER_SHM;
649 msg_arg->params->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
650 OPTEE_MSG_ATTR_NONCONTIG;
651 msg_arg->params->u.tmem.buf_ptr = ph_ptr;
652 msg_arg->params->u.tmem.shm_ref = (ulong)shm;
653 msg_arg->params->u.tmem.size = shm->size;
654
655 if (do_call_with_arg(dev, msg_arg) || msg_arg->ret)
656 rc = -EINVAL;
657
658 free(pl);
659out:
660 tee_shm_free(shm_arg);
661
662 return rc;
663}
664
665static int optee_shm_unregister(struct udevice *dev, struct tee_shm *shm)
666{
667 struct tee_shm *shm_arg;
668 struct optee_msg_arg *msg_arg;
669 int rc;
670
671 rc = get_msg_arg(dev, 1, &shm_arg, &msg_arg);
672 if (rc)
673 return rc;
674
675 msg_arg->cmd = OPTEE_MSG_CMD_UNREGISTER_SHM;
676 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
677 msg_arg->params[0].u.rmem.shm_ref = (ulong)shm;
678
679 if (do_call_with_arg(dev, msg_arg) || msg_arg->ret)
680 rc = -EINVAL;
681 tee_shm_free(shm_arg);
682
683 return rc;
684}
685
686static const struct tee_driver_ops optee_ops = {
687 .get_version = optee_get_version,
688 .open_session = optee_open_session,
689 .close_session = optee_close_session,
690 .invoke_func = optee_invoke_func,
691 .shm_register = optee_shm_register,
692 .shm_unregister = optee_shm_unregister,
693};
694
695static bool is_optee_api(optee_invoke_fn *invoke_fn)
696{
697 struct arm_smccc_res res;
698
699 invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res);
700
701 return res.a0 == OPTEE_MSG_UID_0 && res.a1 == OPTEE_MSG_UID_1 &&
702 res.a2 == OPTEE_MSG_UID_2 && res.a3 == OPTEE_MSG_UID_3;
703}
704
Patrick Delaunaybb7e7932020-03-02 13:21:53 +0100705static void print_os_revision(struct udevice *dev, optee_invoke_fn *invoke_fn)
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200706{
707 union {
708 struct arm_smccc_res smccc;
709 struct optee_smc_call_get_os_revision_result result;
710 } res = {
711 .result = {
712 .build_id = 0
713 }
714 };
715
716 invoke_fn(OPTEE_SMC_CALL_GET_OS_REVISION, 0, 0, 0, 0, 0, 0, 0,
717 &res.smccc);
718
719 if (res.result.build_id)
Patrick Delaunaybb7e7932020-03-02 13:21:53 +0100720 dev_info(dev, "OP-TEE: revision %lu.%lu (%08lx)\n",
721 res.result.major, res.result.minor,
722 res.result.build_id);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200723 else
Patrick Delaunaybb7e7932020-03-02 13:21:53 +0100724 dev_info(dev, "OP-TEE: revision %lu.%lu\n",
725 res.result.major, res.result.minor);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200726}
727
728static bool api_revision_is_compatible(optee_invoke_fn *invoke_fn)
729{
730 union {
731 struct arm_smccc_res smccc;
732 struct optee_smc_calls_revision_result result;
733 } res;
734
735 invoke_fn(OPTEE_SMC_CALLS_REVISION, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
736
737 return res.result.major == OPTEE_MSG_REVISION_MAJOR &&
738 (int)res.result.minor >= OPTEE_MSG_REVISION_MINOR;
739}
740
741static bool exchange_capabilities(optee_invoke_fn *invoke_fn, u32 *sec_caps)
742{
743 union {
744 struct arm_smccc_res smccc;
745 struct optee_smc_exchange_capabilities_result result;
746 } res;
747
748 invoke_fn(OPTEE_SMC_EXCHANGE_CAPABILITIES,
749 OPTEE_SMC_NSEC_CAP_UNIPROCESSOR, 0, 0, 0, 0, 0, 0,
750 &res.smccc);
751
752 if (res.result.status != OPTEE_SMC_RETURN_OK)
753 return false;
754
755 *sec_caps = res.result.capabilities;
756
757 return true;
758}
759
760/* Simple wrapper functions to be able to use a function pointer */
761static void optee_smccc_smc(unsigned long a0, unsigned long a1,
762 unsigned long a2, unsigned long a3,
763 unsigned long a4, unsigned long a5,
764 unsigned long a6, unsigned long a7,
765 struct arm_smccc_res *res)
766{
767 arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
768}
769
770static void optee_smccc_hvc(unsigned long a0, unsigned long a1,
771 unsigned long a2, unsigned long a3,
772 unsigned long a4, unsigned long a5,
773 unsigned long a6, unsigned long a7,
774 struct arm_smccc_res *res)
775{
776 arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
777}
778
779static optee_invoke_fn *get_invoke_func(struct udevice *dev)
780{
781 const char *method;
782
783 debug("optee: looking for conduit method in DT.\n");
Simon Glassa7ece582020-12-19 10:40:14 -0700784 method = ofnode_get_property(dev_ofnode(dev), "method", NULL);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200785 if (!method) {
786 debug("optee: missing \"method\" property\n");
787 return ERR_PTR(-ENXIO);
788 }
789
790 if (!strcmp("hvc", method))
791 return optee_smccc_hvc;
792 else if (!strcmp("smc", method))
793 return optee_smccc_smc;
794
795 debug("optee: invalid \"method\" property: %s\n", method);
796 return ERR_PTR(-EINVAL);
797}
798
Simon Glassaad29ae2020-12-03 16:55:21 -0700799static int optee_of_to_plat(struct udevice *dev)
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200800{
Simon Glassfa20e932020-12-03 16:55:20 -0700801 struct optee_pdata *pdata = dev_get_plat(dev);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200802
803 pdata->invoke_fn = get_invoke_func(dev);
804 if (IS_ERR(pdata->invoke_fn))
805 return PTR_ERR(pdata->invoke_fn);
806
807 return 0;
808}
809
Etienne Carriere523a9512022-07-26 16:21:42 +0200810static int optee_bind(struct udevice *dev)
811{
812 if (IS_ENABLED(CONFIG_OPTEE_SERVICE_DISCOVERY))
813 dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
814
815 return 0;
816}
817
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200818static int optee_probe(struct udevice *dev)
819{
Simon Glassfa20e932020-12-03 16:55:20 -0700820 struct optee_pdata *pdata = dev_get_plat(dev);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200821 u32 sec_caps;
Patrick Delaunay86a586b2022-03-29 14:21:24 +0200822 int ret;
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200823
824 if (!is_optee_api(pdata->invoke_fn)) {
Ilias Apalodimasb91f1002021-03-10 15:35:11 +0200825 dev_err(dev, "OP-TEE api uid mismatch\n");
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200826 return -ENOENT;
827 }
828
Patrick Delaunaybb7e7932020-03-02 13:21:53 +0100829 print_os_revision(dev, pdata->invoke_fn);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200830
831 if (!api_revision_is_compatible(pdata->invoke_fn)) {
Ilias Apalodimasb91f1002021-03-10 15:35:11 +0200832 dev_err(dev, "OP-TEE api revision mismatch\n");
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200833 return -ENOENT;
834 }
835
836 /*
837 * OP-TEE can use both shared memory via predefined pool or as
838 * dynamic shared memory provided by normal world. To keep things
839 * simple we're only using dynamic shared memory in this driver.
840 */
841 if (!exchange_capabilities(pdata->invoke_fn, &sec_caps) ||
842 !(sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)) {
Ilias Apalodimasb91f1002021-03-10 15:35:11 +0200843 dev_err(dev, "OP-TEE capabilities mismatch\n");
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200844 return -ENOENT;
845 }
846
Etienne Carriere523a9512022-07-26 16:21:42 +0200847 if (IS_ENABLED(CONFIG_OPTEE_SERVICE_DISCOVERY)) {
848 ret = bind_service_drivers(dev);
849 if (ret)
Etienne Carriere3b052ce2023-11-29 13:37:52 +0100850 dev_warn(dev, "optee service enumeration failed: %d\n", ret);
Etienne Carriere523a9512022-07-26 16:21:42 +0200851 } else if (IS_ENABLED(CONFIG_RNG_OPTEE)) {
852 /*
853 * Discovery of TAs on the TEE bus is not supported in U-Boot:
854 * only bind the drivers associated to the supported OP-TEE TA
855 */
Patrick Delaunay86363662022-12-15 09:54:52 +0100856 ret = device_bind_driver_to_node(dev, "optee-rng", "optee-rng",
857 dev_ofnode(dev), NULL);
Patrick Delaunay86a586b2022-03-29 14:21:24 +0200858 if (ret)
Ilias Apalodimasaca07f92023-01-18 17:38:21 +0200859 dev_warn(dev, "optee-rng failed to bind: %d\n", ret);
Patrick Delaunay86a586b2022-03-29 14:21:24 +0200860 }
861
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200862 return 0;
863}
864
865static const struct udevice_id optee_match[] = {
866 { .compatible = "linaro,optee-tz" },
867 {},
868};
869
870U_BOOT_DRIVER(optee) = {
871 .name = "optee",
872 .id = UCLASS_TEE,
873 .of_match = optee_match,
Simon Glassaad29ae2020-12-03 16:55:21 -0700874 .of_to_plat = optee_of_to_plat,
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200875 .probe = optee_probe,
Etienne Carriere523a9512022-07-26 16:21:42 +0200876 .bind = optee_bind,
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200877 .ops = &optee_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700878 .plat_auto = sizeof(struct optee_pdata),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700879 .priv_auto = sizeof(struct optee_private),
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200880};