blob: 9a9b697e91f51d5145a08e2f5c1c80bc2fe8decc [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
142 ret = tee_shm_alloc(dev, shm_size, 0, shm);
143 if (ret) {
144 dev_err(dev, "Failed to allocated shared memory: %d\n", ret);
145 return ret;
146 }
147
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100148 ret = __enum_services(dev, *shm, &shm_size, tee_sess, pta_cmd);
Etienne Carriere523a9512022-07-26 16:21:42 +0200149 if (!ret)
150 *count = shm_size / sizeof(struct tee_optee_ta_uuid);
151
152 return ret;
153}
154
155static int open_enum_session(struct udevice *dev, u32 *tee_sess)
156{
157 const struct tee_optee_ta_uuid pta_uuid = PTA_DEVICE_ENUM;
158 struct tee_open_session_arg arg = { };
159 int ret;
160
161 tee_optee_ta_uuid_to_octets(arg.uuid, &pta_uuid);
162
163 ret = tee_open_session(dev, &arg, 0, NULL);
164 if (ret || arg.ret) {
165 if (!ret)
166 ret = -EIO;
167 return ret;
168 }
169
170 *tee_sess = arg.session;
171
172 return 0;
173}
174
175static int bind_service_drivers(struct udevice *dev)
176{
177 struct tee_shm *service_list = NULL;
178 size_t service_count;
179 u32 tee_sess;
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100180 int ret, ret2;
Etienne Carriere523a9512022-07-26 16:21:42 +0200181
182 ret = open_enum_session(dev, &tee_sess);
183 if (ret)
184 return ret;
185
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100186 ret = enum_services(dev, &service_list, &service_count, tee_sess,
187 PTA_CMD_GET_DEVICES);
Etienne Carriere523a9512022-07-26 16:21:42 +0200188 if (!ret)
189 ret = bind_service_list(dev, service_list, service_count);
190
191 tee_shm_free(service_list);
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100192
193 ret2 = enum_services(dev, &service_list, &service_count, tee_sess,
194 PTA_CMD_GET_DEVICES_SUPP);
195 if (!ret2)
196 ret2 = bind_service_list(dev, service_list, service_count);
197
198 tee_shm_free(service_list);
199
Etienne Carriere523a9512022-07-26 16:21:42 +0200200 tee_close_session(dev, tee_sess);
201
Etienne Carrierede1a4a32022-12-07 16:30:00 +0100202 if (ret)
203 return ret;
204
205 return ret2;
Etienne Carriere523a9512022-07-26 16:21:42 +0200206}
207
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200208/**
209 * reg_pair_to_ptr() - Make a pointer of 2 32-bit values
210 * @reg0: High bits of the pointer
211 * @reg1: Low bits of the pointer
212 *
213 * Returns the combined result, note that if a pointer is 32-bit wide @reg0
214 * will be discarded.
215 */
216static void *reg_pair_to_ptr(u32 reg0, u32 reg1)
217{
218 return (void *)(ulong)(((u64)reg0 << 32) | reg1);
219}
220
221/**
222 * reg_pair_from_64() - Split a 64-bit value into two 32-bit values
223 * @reg0: High bits of @val
224 * @reg1: Low bits of @val
225 * @val: The value to split
226 */
227static void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
228{
229 *reg0 = val >> 32;
230 *reg1 = val;
231}
232
233/**
234 * optee_alloc_and_init_page_list() - Provide page list of memory buffer
235 * @buf: Start of buffer
236 * @len: Length of buffer
237 * @phys_buf_ptr Physical pointer with coded offset to page list
238 *
239 * Secure world doesn't share mapping with Normal world (U-Boot in this case)
240 * so physical pointers are needed when sharing pointers.
241 *
242 * Returns a pointer page list on success or NULL on failure
243 */
244void *optee_alloc_and_init_page_list(void *buf, ulong len, u64 *phys_buf_ptr)
245{
246 const unsigned int page_size = OPTEE_MSG_NONCONTIG_PAGE_SIZE;
247 const phys_addr_t page_mask = page_size - 1;
248 u8 *buf_base;
249 unsigned int page_offset;
250 unsigned int num_pages;
251 unsigned int list_size;
252 unsigned int n;
253 void *page_list;
254 struct {
255 u64 pages_list[PAGELIST_ENTRIES_PER_PAGE];
256 u64 next_page_data;
257 } *pages_data;
258
259 /*
260 * A Memory buffer is described in chunks of 4k. The list of
261 * physical addresses has to be represented by a physical pointer
262 * too and a single list has to start at a 4k page and fit into
263 * that page. In order to be able to describe large memory buffers
264 * these 4k pages carrying physical addresses are linked together
265 * in a list. See OPTEE_MSG_ATTR_NONCONTIG in
266 * drivers/tee/optee/optee_msg.h for more information.
267 */
268
269 page_offset = (ulong)buf & page_mask;
270 num_pages = roundup(page_offset + len, page_size) / page_size;
271 list_size = DIV_ROUND_UP(num_pages, PAGELIST_ENTRIES_PER_PAGE) *
272 page_size;
273 page_list = memalign(page_size, list_size);
274 if (!page_list)
275 return NULL;
276
277 pages_data = page_list;
278 buf_base = (u8 *)rounddown((ulong)buf, page_size);
279 n = 0;
280 while (num_pages) {
281 pages_data->pages_list[n] = virt_to_phys(buf_base);
282 n++;
283 buf_base += page_size;
284 num_pages--;
285
286 if (n == PAGELIST_ENTRIES_PER_PAGE) {
287 pages_data->next_page_data =
288 virt_to_phys(pages_data + 1);
289 pages_data++;
290 n = 0;
291 }
292 }
293
294 *phys_buf_ptr = virt_to_phys(page_list) | page_offset;
295 return page_list;
296}
297
298static void optee_get_version(struct udevice *dev,
299 struct tee_version_data *vers)
300{
301 struct tee_version_data v = {
302 .gen_caps = TEE_GEN_CAP_GP | TEE_GEN_CAP_REG_MEM,
303 };
304
305 *vers = v;
306}
307
308static int get_msg_arg(struct udevice *dev, uint num_params,
309 struct tee_shm **shmp, struct optee_msg_arg **msg_arg)
310{
311 int rc;
312 struct optee_msg_arg *ma;
313
314 rc = __tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL,
315 OPTEE_MSG_GET_ARG_SIZE(num_params), TEE_SHM_ALLOC,
316 shmp);
317 if (rc)
318 return rc;
319
320 ma = (*shmp)->addr;
321 memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));
322 ma->num_params = num_params;
323 *msg_arg = ma;
324
325 return 0;
326}
327
328static int to_msg_param(struct optee_msg_param *msg_params, uint num_params,
329 const struct tee_param *params)
330{
331 uint n;
332
333 for (n = 0; n < num_params; n++) {
334 const struct tee_param *p = params + n;
335 struct optee_msg_param *mp = msg_params + n;
336
337 switch (p->attr) {
338 case TEE_PARAM_ATTR_TYPE_NONE:
339 mp->attr = OPTEE_MSG_ATTR_TYPE_NONE;
340 memset(&mp->u, 0, sizeof(mp->u));
341 break;
342 case TEE_PARAM_ATTR_TYPE_VALUE_INPUT:
343 case TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT:
344 case TEE_PARAM_ATTR_TYPE_VALUE_INOUT:
345 mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -
346 TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
347 mp->u.value.a = p->u.value.a;
348 mp->u.value.b = p->u.value.b;
349 mp->u.value.c = p->u.value.c;
350 break;
351 case TEE_PARAM_ATTR_TYPE_MEMREF_INPUT:
352 case TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
353 case TEE_PARAM_ATTR_TYPE_MEMREF_INOUT:
354 mp->attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr -
355 TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
356 mp->u.rmem.shm_ref = (ulong)p->u.memref.shm;
357 mp->u.rmem.size = p->u.memref.size;
358 mp->u.rmem.offs = p->u.memref.shm_offs;
359 break;
360 default:
361 return -EINVAL;
362 }
363 }
364 return 0;
365}
366
367static int from_msg_param(struct tee_param *params, uint num_params,
368 const struct optee_msg_param *msg_params)
369{
370 uint n;
371 struct tee_shm *shm;
372
373 for (n = 0; n < num_params; n++) {
374 struct tee_param *p = params + n;
375 const struct optee_msg_param *mp = msg_params + n;
376 u32 attr = mp->attr & OPTEE_MSG_ATTR_TYPE_MASK;
377
378 switch (attr) {
379 case OPTEE_MSG_ATTR_TYPE_NONE:
380 p->attr = TEE_PARAM_ATTR_TYPE_NONE;
381 memset(&p->u, 0, sizeof(p->u));
382 break;
383 case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT:
384 case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT:
385 case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT:
386 p->attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT + attr -
387 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
388 p->u.value.a = mp->u.value.a;
389 p->u.value.b = mp->u.value.b;
390 p->u.value.c = mp->u.value.c;
391 break;
392 case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT:
393 case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT:
394 case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT:
395 p->attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT + attr -
396 OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
397 p->u.memref.size = mp->u.rmem.size;
398 shm = (struct tee_shm *)(ulong)mp->u.rmem.shm_ref;
399
400 if (!shm) {
401 p->u.memref.shm_offs = 0;
402 p->u.memref.shm = NULL;
403 break;
404 }
405 p->u.memref.shm_offs = mp->u.rmem.offs;
406 p->u.memref.shm = shm;
407 break;
408 default:
409 return -EINVAL;
410 }
411 }
412 return 0;
413}
414
415static void handle_rpc(struct udevice *dev, struct rpc_param *param,
416 void *page_list)
417{
418 struct tee_shm *shm;
419
420 switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) {
421 case OPTEE_SMC_RPC_FUNC_ALLOC:
422 if (!__tee_shm_add(dev, OPTEE_MSG_NONCONTIG_PAGE_SIZE, NULL,
423 param->a1, TEE_SHM_ALLOC | TEE_SHM_REGISTER,
424 &shm)) {
425 reg_pair_from_64(&param->a1, &param->a2,
426 virt_to_phys(shm->addr));
427 /* "cookie" */
428 reg_pair_from_64(&param->a4, &param->a5, (ulong)shm);
429 } else {
430 param->a1 = 0;
431 param->a2 = 0;
432 param->a4 = 0;
433 param->a5 = 0;
434 }
435 break;
436 case OPTEE_SMC_RPC_FUNC_FREE:
437 shm = reg_pair_to_ptr(param->a1, param->a2);
438 tee_shm_free(shm);
439 break;
440 case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR:
441 break;
442 case OPTEE_SMC_RPC_FUNC_CMD:
443 shm = reg_pair_to_ptr(param->a1, param->a2);
444 optee_suppl_cmd(dev, shm, page_list);
445 break;
446 default:
447 break;
448 }
449
450 param->a0 = OPTEE_SMC_CALL_RETURN_FROM_RPC;
451}
452
453static u32 call_err_to_res(u32 call_err)
454{
455 switch (call_err) {
456 case OPTEE_SMC_RETURN_OK:
457 return TEE_SUCCESS;
458 default:
459 return TEE_ERROR_BAD_PARAMETERS;
460 }
461}
462
Etienne Carriere1ba96782021-05-19 16:27:41 +0200463static void flush_shm_dcache(struct udevice *dev, struct optee_msg_arg *arg)
464{
465 size_t sz = OPTEE_MSG_GET_ARG_SIZE(arg->num_params);
466
467 flush_dcache_range(rounddown((ulong)arg, CONFIG_SYS_CACHELINE_SIZE),
468 roundup((ulong)arg + sz, CONFIG_SYS_CACHELINE_SIZE));
469
470 tee_flush_all_shm_dcache(dev);
471}
472
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200473static u32 do_call_with_arg(struct udevice *dev, struct optee_msg_arg *arg)
474{
Simon Glassfa20e932020-12-03 16:55:20 -0700475 struct optee_pdata *pdata = dev_get_plat(dev);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200476 struct rpc_param param = { .a0 = OPTEE_SMC_CALL_WITH_ARG };
477 void *page_list = NULL;
478
479 reg_pair_from_64(&param.a1, &param.a2, virt_to_phys(arg));
480 while (true) {
481 struct arm_smccc_res res;
482
Etienne Carriere1ba96782021-05-19 16:27:41 +0200483 /* If cache are off from U-Boot, sync the cache shared with OP-TEE */
484 if (!dcache_status())
485 flush_shm_dcache(dev, arg);
486
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200487 pdata->invoke_fn(param.a0, param.a1, param.a2, param.a3,
488 param.a4, param.a5, param.a6, param.a7, &res);
489
Etienne Carriere1ba96782021-05-19 16:27:41 +0200490 /* If cache are off from U-Boot, sync the cache shared with OP-TEE */
491 if (!dcache_status())
492 flush_shm_dcache(dev, arg);
493
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200494 free(page_list);
495 page_list = NULL;
496
497 if (OPTEE_SMC_RETURN_IS_RPC(res.a0)) {
498 param.a0 = res.a0;
499 param.a1 = res.a1;
500 param.a2 = res.a2;
501 param.a3 = res.a3;
502 handle_rpc(dev, &param, &page_list);
503 } else {
Jens Wiklanderf1420dd2018-09-25 16:40:14 +0200504 /*
505 * In case we've accessed RPMB to serve an RPC
506 * request we need to restore the previously
507 * selected partition as the caller may expect it
508 * to remain unchanged.
509 */
510 optee_suppl_rpmb_release(dev);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200511 return call_err_to_res(res.a0);
512 }
513 }
514}
515
516static int optee_close_session(struct udevice *dev, u32 session)
517{
518 int rc;
519 struct tee_shm *shm;
520 struct optee_msg_arg *msg_arg;
521
522 rc = get_msg_arg(dev, 0, &shm, &msg_arg);
523 if (rc)
524 return rc;
525
526 msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
527 msg_arg->session = session;
528 do_call_with_arg(dev, msg_arg);
529
530 tee_shm_free(shm);
531
532 return 0;
533}
534
535static int optee_open_session(struct udevice *dev,
536 struct tee_open_session_arg *arg,
537 uint num_params, struct tee_param *params)
538{
539 int rc;
540 struct tee_shm *shm;
541 struct optee_msg_arg *msg_arg;
542
543 rc = get_msg_arg(dev, num_params + 2, &shm, &msg_arg);
544 if (rc)
545 return rc;
546
547 msg_arg->cmd = OPTEE_MSG_CMD_OPEN_SESSION;
548 /*
549 * Initialize and add the meta parameters needed when opening a
550 * session.
551 */
552 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
553 OPTEE_MSG_ATTR_META;
554 msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
555 OPTEE_MSG_ATTR_META;
556 memcpy(&msg_arg->params[0].u.value, arg->uuid, sizeof(arg->uuid));
557 memcpy(&msg_arg->params[1].u.value, arg->uuid, sizeof(arg->clnt_uuid));
558 msg_arg->params[1].u.value.c = arg->clnt_login;
559
560 rc = to_msg_param(msg_arg->params + 2, num_params, params);
561 if (rc)
562 goto out;
563
564 arg->ret = do_call_with_arg(dev, msg_arg);
565 if (arg->ret) {
566 arg->ret_origin = TEE_ORIGIN_COMMS;
567 goto out;
568 }
569
570 if (from_msg_param(params, num_params, msg_arg->params + 2)) {
571 arg->ret = TEE_ERROR_COMMUNICATION;
572 arg->ret_origin = TEE_ORIGIN_COMMS;
573 /* Close session again to avoid leakage */
574 optee_close_session(dev, msg_arg->session);
575 goto out;
576 }
577
578 arg->session = msg_arg->session;
579 arg->ret = msg_arg->ret;
580 arg->ret_origin = msg_arg->ret_origin;
581out:
582 tee_shm_free(shm);
583
584 return rc;
585}
586
587static int optee_invoke_func(struct udevice *dev, struct tee_invoke_arg *arg,
588 uint num_params, struct tee_param *params)
589{
590 struct tee_shm *shm;
591 struct optee_msg_arg *msg_arg;
592 int rc;
593
594 rc = get_msg_arg(dev, num_params, &shm, &msg_arg);
595 if (rc)
596 return rc;
597 msg_arg->cmd = OPTEE_MSG_CMD_INVOKE_COMMAND;
598 msg_arg->func = arg->func;
599 msg_arg->session = arg->session;
600
601 rc = to_msg_param(msg_arg->params, num_params, params);
602 if (rc)
603 goto out;
604
605 arg->ret = do_call_with_arg(dev, msg_arg);
606 if (arg->ret) {
607 arg->ret_origin = TEE_ORIGIN_COMMS;
608 goto out;
609 }
610
611 if (from_msg_param(params, num_params, msg_arg->params)) {
612 arg->ret = TEE_ERROR_COMMUNICATION;
613 arg->ret_origin = TEE_ORIGIN_COMMS;
614 goto out;
615 }
616
617 arg->ret = msg_arg->ret;
618 arg->ret_origin = msg_arg->ret_origin;
619out:
620 tee_shm_free(shm);
621 return rc;
622}
623
624static int optee_shm_register(struct udevice *dev, struct tee_shm *shm)
625{
626 struct tee_shm *shm_arg;
627 struct optee_msg_arg *msg_arg;
628 void *pl;
629 u64 ph_ptr;
630 int rc;
631
632 rc = get_msg_arg(dev, 1, &shm_arg, &msg_arg);
633 if (rc)
634 return rc;
635
636 pl = optee_alloc_and_init_page_list(shm->addr, shm->size, &ph_ptr);
637 if (!pl) {
638 rc = -ENOMEM;
639 goto out;
640 }
641
642 msg_arg->cmd = OPTEE_MSG_CMD_REGISTER_SHM;
643 msg_arg->params->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
644 OPTEE_MSG_ATTR_NONCONTIG;
645 msg_arg->params->u.tmem.buf_ptr = ph_ptr;
646 msg_arg->params->u.tmem.shm_ref = (ulong)shm;
647 msg_arg->params->u.tmem.size = shm->size;
648
649 if (do_call_with_arg(dev, msg_arg) || msg_arg->ret)
650 rc = -EINVAL;
651
652 free(pl);
653out:
654 tee_shm_free(shm_arg);
655
656 return rc;
657}
658
659static int optee_shm_unregister(struct udevice *dev, struct tee_shm *shm)
660{
661 struct tee_shm *shm_arg;
662 struct optee_msg_arg *msg_arg;
663 int rc;
664
665 rc = get_msg_arg(dev, 1, &shm_arg, &msg_arg);
666 if (rc)
667 return rc;
668
669 msg_arg->cmd = OPTEE_MSG_CMD_UNREGISTER_SHM;
670 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
671 msg_arg->params[0].u.rmem.shm_ref = (ulong)shm;
672
673 if (do_call_with_arg(dev, msg_arg) || msg_arg->ret)
674 rc = -EINVAL;
675 tee_shm_free(shm_arg);
676
677 return rc;
678}
679
680static const struct tee_driver_ops optee_ops = {
681 .get_version = optee_get_version,
682 .open_session = optee_open_session,
683 .close_session = optee_close_session,
684 .invoke_func = optee_invoke_func,
685 .shm_register = optee_shm_register,
686 .shm_unregister = optee_shm_unregister,
687};
688
689static bool is_optee_api(optee_invoke_fn *invoke_fn)
690{
691 struct arm_smccc_res res;
692
693 invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res);
694
695 return res.a0 == OPTEE_MSG_UID_0 && res.a1 == OPTEE_MSG_UID_1 &&
696 res.a2 == OPTEE_MSG_UID_2 && res.a3 == OPTEE_MSG_UID_3;
697}
698
Patrick Delaunaybb7e7932020-03-02 13:21:53 +0100699static void print_os_revision(struct udevice *dev, optee_invoke_fn *invoke_fn)
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200700{
701 union {
702 struct arm_smccc_res smccc;
703 struct optee_smc_call_get_os_revision_result result;
704 } res = {
705 .result = {
706 .build_id = 0
707 }
708 };
709
710 invoke_fn(OPTEE_SMC_CALL_GET_OS_REVISION, 0, 0, 0, 0, 0, 0, 0,
711 &res.smccc);
712
713 if (res.result.build_id)
Patrick Delaunaybb7e7932020-03-02 13:21:53 +0100714 dev_info(dev, "OP-TEE: revision %lu.%lu (%08lx)\n",
715 res.result.major, res.result.minor,
716 res.result.build_id);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200717 else
Patrick Delaunaybb7e7932020-03-02 13:21:53 +0100718 dev_info(dev, "OP-TEE: revision %lu.%lu\n",
719 res.result.major, res.result.minor);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200720}
721
722static bool api_revision_is_compatible(optee_invoke_fn *invoke_fn)
723{
724 union {
725 struct arm_smccc_res smccc;
726 struct optee_smc_calls_revision_result result;
727 } res;
728
729 invoke_fn(OPTEE_SMC_CALLS_REVISION, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
730
731 return res.result.major == OPTEE_MSG_REVISION_MAJOR &&
732 (int)res.result.minor >= OPTEE_MSG_REVISION_MINOR;
733}
734
735static bool exchange_capabilities(optee_invoke_fn *invoke_fn, u32 *sec_caps)
736{
737 union {
738 struct arm_smccc_res smccc;
739 struct optee_smc_exchange_capabilities_result result;
740 } res;
741
742 invoke_fn(OPTEE_SMC_EXCHANGE_CAPABILITIES,
743 OPTEE_SMC_NSEC_CAP_UNIPROCESSOR, 0, 0, 0, 0, 0, 0,
744 &res.smccc);
745
746 if (res.result.status != OPTEE_SMC_RETURN_OK)
747 return false;
748
749 *sec_caps = res.result.capabilities;
750
751 return true;
752}
753
754/* Simple wrapper functions to be able to use a function pointer */
755static void optee_smccc_smc(unsigned long a0, unsigned long a1,
756 unsigned long a2, unsigned long a3,
757 unsigned long a4, unsigned long a5,
758 unsigned long a6, unsigned long a7,
759 struct arm_smccc_res *res)
760{
761 arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
762}
763
764static void optee_smccc_hvc(unsigned long a0, unsigned long a1,
765 unsigned long a2, unsigned long a3,
766 unsigned long a4, unsigned long a5,
767 unsigned long a6, unsigned long a7,
768 struct arm_smccc_res *res)
769{
770 arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
771}
772
773static optee_invoke_fn *get_invoke_func(struct udevice *dev)
774{
775 const char *method;
776
777 debug("optee: looking for conduit method in DT.\n");
Simon Glassa7ece582020-12-19 10:40:14 -0700778 method = ofnode_get_property(dev_ofnode(dev), "method", NULL);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200779 if (!method) {
780 debug("optee: missing \"method\" property\n");
781 return ERR_PTR(-ENXIO);
782 }
783
784 if (!strcmp("hvc", method))
785 return optee_smccc_hvc;
786 else if (!strcmp("smc", method))
787 return optee_smccc_smc;
788
789 debug("optee: invalid \"method\" property: %s\n", method);
790 return ERR_PTR(-EINVAL);
791}
792
Simon Glassaad29ae2020-12-03 16:55:21 -0700793static int optee_of_to_plat(struct udevice *dev)
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200794{
Simon Glassfa20e932020-12-03 16:55:20 -0700795 struct optee_pdata *pdata = dev_get_plat(dev);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200796
797 pdata->invoke_fn = get_invoke_func(dev);
798 if (IS_ERR(pdata->invoke_fn))
799 return PTR_ERR(pdata->invoke_fn);
800
801 return 0;
802}
803
Etienne Carriere523a9512022-07-26 16:21:42 +0200804static int optee_bind(struct udevice *dev)
805{
806 if (IS_ENABLED(CONFIG_OPTEE_SERVICE_DISCOVERY))
807 dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
808
809 return 0;
810}
811
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200812static int optee_probe(struct udevice *dev)
813{
Simon Glassfa20e932020-12-03 16:55:20 -0700814 struct optee_pdata *pdata = dev_get_plat(dev);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200815 u32 sec_caps;
Patrick Delaunay86a586b2022-03-29 14:21:24 +0200816 int ret;
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200817
818 if (!is_optee_api(pdata->invoke_fn)) {
Ilias Apalodimasb91f1002021-03-10 15:35:11 +0200819 dev_err(dev, "OP-TEE api uid mismatch\n");
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200820 return -ENOENT;
821 }
822
Patrick Delaunaybb7e7932020-03-02 13:21:53 +0100823 print_os_revision(dev, pdata->invoke_fn);
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200824
825 if (!api_revision_is_compatible(pdata->invoke_fn)) {
Ilias Apalodimasb91f1002021-03-10 15:35:11 +0200826 dev_err(dev, "OP-TEE api revision mismatch\n");
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200827 return -ENOENT;
828 }
829
830 /*
831 * OP-TEE can use both shared memory via predefined pool or as
832 * dynamic shared memory provided by normal world. To keep things
833 * simple we're only using dynamic shared memory in this driver.
834 */
835 if (!exchange_capabilities(pdata->invoke_fn, &sec_caps) ||
836 !(sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)) {
Ilias Apalodimasb91f1002021-03-10 15:35:11 +0200837 dev_err(dev, "OP-TEE capabilities mismatch\n");
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200838 return -ENOENT;
839 }
840
Etienne Carriere523a9512022-07-26 16:21:42 +0200841 if (IS_ENABLED(CONFIG_OPTEE_SERVICE_DISCOVERY)) {
842 ret = bind_service_drivers(dev);
843 if (ret)
844 return ret;
845 } else if (IS_ENABLED(CONFIG_RNG_OPTEE)) {
846 /*
847 * Discovery of TAs on the TEE bus is not supported in U-Boot:
848 * only bind the drivers associated to the supported OP-TEE TA
849 */
Patrick Delaunay86363662022-12-15 09:54:52 +0100850 ret = device_bind_driver_to_node(dev, "optee-rng", "optee-rng",
851 dev_ofnode(dev), NULL);
Patrick Delaunay86a586b2022-03-29 14:21:24 +0200852 if (ret)
Ilias Apalodimasaca07f92023-01-18 17:38:21 +0200853 dev_warn(dev, "optee-rng failed to bind: %d\n", ret);
Patrick Delaunay86a586b2022-03-29 14:21:24 +0200854 }
855
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200856 return 0;
857}
858
859static const struct udevice_id optee_match[] = {
860 { .compatible = "linaro,optee-tz" },
861 {},
862};
863
864U_BOOT_DRIVER(optee) = {
865 .name = "optee",
866 .id = UCLASS_TEE,
867 .of_match = optee_match,
Simon Glassaad29ae2020-12-03 16:55:21 -0700868 .of_to_plat = optee_of_to_plat,
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200869 .probe = optee_probe,
Etienne Carriere523a9512022-07-26 16:21:42 +0200870 .bind = optee_bind,
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200871 .ops = &optee_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700872 .plat_auto = sizeof(struct optee_pdata),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700873 .priv_auto = sizeof(struct optee_private),
Jens Wiklander2b7216b2018-09-25 16:40:11 +0200874};