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