1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2021, 2023 Linaro Limited
4 */
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include <linux/arm_ffa.h>
9 #include <linux/errno.h>
10 #include <linux/rpmb.h>
11 #include <linux/scatterlist.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/tee_core.h>
16 #include <linux/types.h>
17 #include "optee_private.h"
18 #include "optee_ffa.h"
19 #include "optee_rpc_cmd.h"
20
21 /*
22 * This file implement the FF-A ABI used when communicating with secure world
23 * OP-TEE OS via FF-A.
24 * This file is divided into the following sections:
25 * 1. Maintain a hash table for lookup of a global FF-A memory handle
26 * 2. Convert between struct tee_param and struct optee_msg_param
27 * 3. Low level support functions to register shared memory in secure world
28 * 4. Dynamic shared memory pool based on alloc_pages()
29 * 5. Do a normal scheduled call into secure world
30 * 6. Driver initialization.
31 */
32
33 /*
34 * 1. Maintain a hash table for lookup of a global FF-A memory handle
35 *
36 * FF-A assigns a global memory handle for each piece shared memory.
37 * This handle is then used when communicating with secure world.
38 *
39 * Main functions are optee_shm_add_ffa_handle() and optee_shm_rem_ffa_handle()
40 */
41 struct shm_rhash {
42 struct tee_shm *shm;
43 u64 global_id;
44 struct rhash_head linkage;
45 };
46
rh_free_fn(void * ptr,void * arg)47 static void rh_free_fn(void *ptr, void *arg)
48 {
49 kfree(ptr);
50 }
51
52 static const struct rhashtable_params shm_rhash_params = {
53 .head_offset = offsetof(struct shm_rhash, linkage),
54 .key_len = sizeof(u64),
55 .key_offset = offsetof(struct shm_rhash, global_id),
56 .automatic_shrinking = true,
57 };
58
optee_shm_from_ffa_handle(struct optee * optee,u64 global_id)59 static struct tee_shm *optee_shm_from_ffa_handle(struct optee *optee,
60 u64 global_id)
61 {
62 struct tee_shm *shm = NULL;
63 struct shm_rhash *r;
64
65 mutex_lock(&optee->ffa.mutex);
66 r = rhashtable_lookup_fast(&optee->ffa.global_ids, &global_id,
67 shm_rhash_params);
68 if (r)
69 shm = r->shm;
70 mutex_unlock(&optee->ffa.mutex);
71
72 return shm;
73 }
74
optee_shm_add_ffa_handle(struct optee * optee,struct tee_shm * shm,u64 global_id)75 static int optee_shm_add_ffa_handle(struct optee *optee, struct tee_shm *shm,
76 u64 global_id)
77 {
78 struct shm_rhash *r;
79 int rc;
80
81 r = kmalloc_obj(*r);
82 if (!r)
83 return -ENOMEM;
84 r->shm = shm;
85 r->global_id = global_id;
86
87 mutex_lock(&optee->ffa.mutex);
88 rc = rhashtable_lookup_insert_fast(&optee->ffa.global_ids, &r->linkage,
89 shm_rhash_params);
90 mutex_unlock(&optee->ffa.mutex);
91
92 if (rc)
93 kfree(r);
94
95 return rc;
96 }
97
optee_shm_rem_ffa_handle(struct optee * optee,u64 global_id)98 static int optee_shm_rem_ffa_handle(struct optee *optee, u64 global_id)
99 {
100 struct shm_rhash *r;
101 int rc = -ENOENT;
102
103 mutex_lock(&optee->ffa.mutex);
104 r = rhashtable_lookup_fast(&optee->ffa.global_ids, &global_id,
105 shm_rhash_params);
106 if (r)
107 rc = rhashtable_remove_fast(&optee->ffa.global_ids,
108 &r->linkage, shm_rhash_params);
109 mutex_unlock(&optee->ffa.mutex);
110
111 if (!rc)
112 kfree(r);
113
114 return rc;
115 }
116
117 /*
118 * 2. Convert between struct tee_param and struct optee_msg_param
119 *
120 * optee_ffa_from_msg_param() and optee_ffa_to_msg_param() are the main
121 * functions.
122 */
123
from_msg_param_ffa_mem(struct optee * optee,struct tee_param * p,u32 attr,const struct optee_msg_param * mp)124 static void from_msg_param_ffa_mem(struct optee *optee, struct tee_param *p,
125 u32 attr, const struct optee_msg_param *mp)
126 {
127 struct tee_shm *shm = NULL;
128 u64 offs_high = 0;
129 u64 offs_low = 0;
130
131 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
132 attr - OPTEE_MSG_ATTR_TYPE_FMEM_INPUT;
133 p->u.memref.size = mp->u.fmem.size;
134
135 if (mp->u.fmem.global_id != OPTEE_MSG_FMEM_INVALID_GLOBAL_ID)
136 shm = optee_shm_from_ffa_handle(optee, mp->u.fmem.global_id);
137 p->u.memref.shm = shm;
138
139 if (shm) {
140 offs_low = mp->u.fmem.offs_low;
141 offs_high = mp->u.fmem.offs_high;
142 }
143 p->u.memref.shm_offs = offs_low | offs_high << 32;
144 }
145
146 /**
147 * optee_ffa_from_msg_param() - convert from OPTEE_MSG parameters to
148 * struct tee_param
149 * @optee: main service struct
150 * @params: subsystem internal parameter representation
151 * @num_params: number of elements in the parameter arrays
152 * @msg_params: OPTEE_MSG parameters
153 *
154 * Returns 0 on success or <0 on failure
155 */
optee_ffa_from_msg_param(struct optee * optee,struct tee_param * params,size_t num_params,const struct optee_msg_param * msg_params)156 static int optee_ffa_from_msg_param(struct optee *optee,
157 struct tee_param *params, size_t num_params,
158 const struct optee_msg_param *msg_params)
159 {
160 size_t n;
161
162 for (n = 0; n < num_params; n++) {
163 struct tee_param *p = params + n;
164 const struct optee_msg_param *mp = msg_params + n;
165 u32 attr = mp->attr & OPTEE_MSG_ATTR_TYPE_MASK;
166
167 switch (attr) {
168 case OPTEE_MSG_ATTR_TYPE_NONE:
169 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
170 memset(&p->u, 0, sizeof(p->u));
171 break;
172 case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT:
173 case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT:
174 case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT:
175 optee_from_msg_param_value(p, attr, mp);
176 break;
177 case OPTEE_MSG_ATTR_TYPE_FMEM_INPUT:
178 case OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT:
179 case OPTEE_MSG_ATTR_TYPE_FMEM_INOUT:
180 from_msg_param_ffa_mem(optee, p, attr, mp);
181 break;
182 default:
183 return -EINVAL;
184 }
185 }
186
187 return 0;
188 }
189
to_msg_param_ffa_mem(struct optee_msg_param * mp,const struct tee_param * p)190 static int to_msg_param_ffa_mem(struct optee_msg_param *mp,
191 const struct tee_param *p)
192 {
193 struct tee_shm *shm = p->u.memref.shm;
194
195 mp->attr = OPTEE_MSG_ATTR_TYPE_FMEM_INPUT + p->attr -
196 TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
197
198 if (shm) {
199 u64 shm_offs = p->u.memref.shm_offs;
200
201 mp->u.fmem.internal_offs = shm->offset;
202
203 mp->u.fmem.offs_low = shm_offs;
204 mp->u.fmem.offs_high = shm_offs >> 32;
205 /* Check that the entire offset could be stored. */
206 if (mp->u.fmem.offs_high != shm_offs >> 32)
207 return -EINVAL;
208
209 mp->u.fmem.global_id = shm->sec_world_id;
210 } else {
211 memset(&mp->u, 0, sizeof(mp->u));
212 mp->u.fmem.global_id = OPTEE_MSG_FMEM_INVALID_GLOBAL_ID;
213 }
214 mp->u.fmem.size = p->u.memref.size;
215
216 return 0;
217 }
218
219 /**
220 * optee_ffa_to_msg_param() - convert from struct tee_params to OPTEE_MSG
221 * parameters
222 * @optee: main service struct
223 * @msg_params: OPTEE_MSG parameters
224 * @num_params: number of elements in the parameter arrays
225 * @params: subsystem itnernal parameter representation
226 * Returns 0 on success or <0 on failure
227 */
optee_ffa_to_msg_param(struct optee * optee,struct optee_msg_param * msg_params,size_t num_params,const struct tee_param * params)228 static int optee_ffa_to_msg_param(struct optee *optee,
229 struct optee_msg_param *msg_params,
230 size_t num_params,
231 const struct tee_param *params)
232 {
233 size_t n;
234
235 for (n = 0; n < num_params; n++) {
236 const struct tee_param *p = params + n;
237 struct optee_msg_param *mp = msg_params + n;
238
239 switch (p->attr) {
240 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
241 mp->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
242 memset(&mp->u, 0, sizeof(mp->u));
243 break;
244 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
245 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
246 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
247 optee_to_msg_param_value(mp, p);
248 break;
249 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
250 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
251 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
252 if (to_msg_param_ffa_mem(mp, p))
253 return -EINVAL;
254 break;
255 default:
256 return -EINVAL;
257 }
258 }
259
260 return 0;
261 }
262
263 /*
264 * 3. Low level support functions to register shared memory in secure world
265 *
266 * Functions to register and unregister shared memory both for normal
267 * clients and for tee-supplicant.
268 */
269
optee_ffa_shm_register(struct tee_context * ctx,struct tee_shm * shm,struct page ** pages,size_t num_pages,unsigned long start)270 static int optee_ffa_shm_register(struct tee_context *ctx, struct tee_shm *shm,
271 struct page **pages, size_t num_pages,
272 unsigned long start)
273 {
274 struct optee *optee = tee_get_drvdata(ctx->teedev);
275 struct ffa_device *ffa_dev = optee->ffa.ffa_dev;
276 const struct ffa_mem_ops *mem_ops = ffa_dev->ops->mem_ops;
277 struct ffa_mem_region_attributes mem_attr = {
278 .receiver = ffa_dev->vm_id,
279 .attrs = FFA_MEM_RW,
280 };
281 struct ffa_mem_ops_args args = {
282 .use_txbuf = true,
283 .attrs = &mem_attr,
284 .nattrs = 1,
285 };
286 struct sg_table sgt;
287 int rc;
288
289 rc = optee_check_mem_type(start, num_pages);
290 if (rc)
291 return rc;
292
293 rc = sg_alloc_table_from_pages(&sgt, pages, num_pages, 0,
294 num_pages * PAGE_SIZE, GFP_KERNEL);
295 if (rc)
296 return rc;
297 args.sg = sgt.sgl;
298 rc = mem_ops->memory_share(&args);
299 sg_free_table(&sgt);
300 if (rc)
301 return rc;
302
303 rc = optee_shm_add_ffa_handle(optee, shm, args.g_handle);
304 if (rc) {
305 mem_ops->memory_reclaim(args.g_handle, 0);
306 return rc;
307 }
308
309 shm->sec_world_id = args.g_handle;
310
311 return 0;
312 }
313
optee_ffa_shm_unregister(struct tee_context * ctx,struct tee_shm * shm)314 static int optee_ffa_shm_unregister(struct tee_context *ctx,
315 struct tee_shm *shm)
316 {
317 struct optee *optee = tee_get_drvdata(ctx->teedev);
318 struct ffa_device *ffa_dev = optee->ffa.ffa_dev;
319 const struct ffa_msg_ops *msg_ops = ffa_dev->ops->msg_ops;
320 const struct ffa_mem_ops *mem_ops = ffa_dev->ops->mem_ops;
321 u64 global_handle = shm->sec_world_id;
322 struct ffa_send_direct_data data = {
323 .data0 = OPTEE_FFA_UNREGISTER_SHM,
324 .data1 = (u32)global_handle,
325 .data2 = (u32)(global_handle >> 32)
326 };
327 int rc;
328
329 optee_shm_rem_ffa_handle(optee, global_handle);
330 shm->sec_world_id = 0;
331
332 rc = msg_ops->sync_send_receive(ffa_dev, &data);
333 if (rc)
334 pr_err("Unregister SHM id 0x%llx rc %d\n", global_handle, rc);
335
336 rc = mem_ops->memory_reclaim(global_handle, 0);
337 if (rc)
338 pr_err("mem_reclaim: 0x%llx %d", global_handle, rc);
339
340 return rc;
341 }
342
optee_ffa_shm_unregister_supp(struct tee_context * ctx,struct tee_shm * shm)343 static int optee_ffa_shm_unregister_supp(struct tee_context *ctx,
344 struct tee_shm *shm)
345 {
346 struct optee *optee = tee_get_drvdata(ctx->teedev);
347 const struct ffa_mem_ops *mem_ops;
348 u64 global_handle = shm->sec_world_id;
349 int rc;
350
351 /*
352 * We're skipping the OPTEE_FFA_YIELDING_CALL_UNREGISTER_SHM call
353 * since this is OP-TEE freeing via RPC so it has already retired
354 * this ID.
355 */
356
357 optee_shm_rem_ffa_handle(optee, global_handle);
358 mem_ops = optee->ffa.ffa_dev->ops->mem_ops;
359 rc = mem_ops->memory_reclaim(global_handle, 0);
360 if (rc)
361 pr_err("mem_reclaim: 0x%llx %d", global_handle, rc);
362
363 shm->sec_world_id = 0;
364
365 return rc;
366 }
367
368 /*
369 * 4. Dynamic shared memory pool based on alloc_pages()
370 *
371 * Implements an OP-TEE specific shared memory pool.
372 * The main function is optee_ffa_shm_pool_alloc_pages().
373 */
374
pool_ffa_op_alloc(struct tee_shm_pool * pool,struct tee_shm * shm,size_t size,size_t align)375 static int pool_ffa_op_alloc(struct tee_shm_pool *pool,
376 struct tee_shm *shm, size_t size, size_t align)
377 {
378 return tee_dyn_shm_alloc_helper(shm, size, align,
379 optee_ffa_shm_register);
380 }
381
pool_ffa_op_free(struct tee_shm_pool * pool,struct tee_shm * shm)382 static void pool_ffa_op_free(struct tee_shm_pool *pool,
383 struct tee_shm *shm)
384 {
385 tee_dyn_shm_free_helper(shm, optee_ffa_shm_unregister);
386 }
387
pool_ffa_op_destroy_pool(struct tee_shm_pool * pool)388 static void pool_ffa_op_destroy_pool(struct tee_shm_pool *pool)
389 {
390 kfree(pool);
391 }
392
393 static const struct tee_shm_pool_ops pool_ffa_ops = {
394 .alloc = pool_ffa_op_alloc,
395 .free = pool_ffa_op_free,
396 .destroy_pool = pool_ffa_op_destroy_pool,
397 };
398
399 /**
400 * optee_ffa_shm_pool_alloc_pages() - create page-based allocator pool
401 *
402 * This pool is used with OP-TEE over FF-A. In this case command buffers
403 * and such are allocated from kernel's own memory.
404 */
optee_ffa_shm_pool_alloc_pages(void)405 static struct tee_shm_pool *optee_ffa_shm_pool_alloc_pages(void)
406 {
407 struct tee_shm_pool *pool = kzalloc_obj(*pool);
408
409 if (!pool)
410 return ERR_PTR(-ENOMEM);
411
412 pool->ops = &pool_ffa_ops;
413
414 return pool;
415 }
416
417 /*
418 * 5. Do a normal scheduled call into secure world
419 *
420 * The function optee_ffa_do_call_with_arg() performs a normal scheduled
421 * call into secure world. During this call may normal world request help
422 * from normal world using RPCs, Remote Procedure Calls. This includes
423 * delivery of non-secure interrupts to for instance allow rescheduling of
424 * the current task.
425 */
426
handle_ffa_rpc_func_cmd_shm_alloc(struct tee_context * ctx,struct optee * optee,struct optee_msg_arg * arg)427 static void handle_ffa_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
428 struct optee *optee,
429 struct optee_msg_arg *arg)
430 {
431 struct tee_shm *shm;
432
433 if (arg->num_params != 1 ||
434 arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
435 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
436 return;
437 }
438
439 switch (arg->params[0].u.value.a) {
440 case OPTEE_RPC_SHM_TYPE_APPL:
441 shm = optee_rpc_cmd_alloc_suppl(ctx, arg->params[0].u.value.b);
442 break;
443 case OPTEE_RPC_SHM_TYPE_KERNEL:
444 shm = tee_shm_alloc_priv_buf(optee->ctx,
445 arg->params[0].u.value.b);
446 break;
447 default:
448 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
449 return;
450 }
451
452 if (IS_ERR(shm)) {
453 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
454 return;
455 }
456
457 arg->params[0] = (struct optee_msg_param){
458 .attr = OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT,
459 .u.fmem.size = tee_shm_get_size(shm),
460 .u.fmem.global_id = shm->sec_world_id,
461 .u.fmem.internal_offs = shm->offset,
462 };
463
464 arg->ret = TEEC_SUCCESS;
465 }
466
handle_ffa_rpc_func_cmd_shm_free(struct tee_context * ctx,struct optee * optee,struct optee_msg_arg * arg)467 static void handle_ffa_rpc_func_cmd_shm_free(struct tee_context *ctx,
468 struct optee *optee,
469 struct optee_msg_arg *arg)
470 {
471 struct tee_shm *shm;
472
473 if (arg->num_params != 1 ||
474 arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
475 goto err_bad_param;
476
477 shm = optee_shm_from_ffa_handle(optee, arg->params[0].u.value.b);
478 if (!shm)
479 goto err_bad_param;
480 switch (arg->params[0].u.value.a) {
481 case OPTEE_RPC_SHM_TYPE_APPL:
482 optee_rpc_cmd_free_suppl(ctx, shm);
483 break;
484 case OPTEE_RPC_SHM_TYPE_KERNEL:
485 tee_shm_free(shm);
486 break;
487 default:
488 goto err_bad_param;
489 }
490 arg->ret = TEEC_SUCCESS;
491 return;
492
493 err_bad_param:
494 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
495 }
496
handle_ffa_rpc_func_cmd(struct tee_context * ctx,struct optee * optee,struct optee_msg_arg * arg)497 static void handle_ffa_rpc_func_cmd(struct tee_context *ctx,
498 struct optee *optee,
499 struct optee_msg_arg *arg)
500 {
501 arg->ret_origin = TEEC_ORIGIN_COMMS;
502 switch (arg->cmd) {
503 case OPTEE_RPC_CMD_SHM_ALLOC:
504 handle_ffa_rpc_func_cmd_shm_alloc(ctx, optee, arg);
505 break;
506 case OPTEE_RPC_CMD_SHM_FREE:
507 handle_ffa_rpc_func_cmd_shm_free(ctx, optee, arg);
508 break;
509 default:
510 optee_rpc_cmd(ctx, optee, arg);
511 }
512 }
513
optee_handle_ffa_rpc(struct tee_context * ctx,struct optee * optee,u32 cmd,struct optee_msg_arg * arg)514 static void optee_handle_ffa_rpc(struct tee_context *ctx, struct optee *optee,
515 u32 cmd, struct optee_msg_arg *arg)
516 {
517 switch (cmd) {
518 case OPTEE_FFA_YIELDING_CALL_RETURN_RPC_CMD:
519 handle_ffa_rpc_func_cmd(ctx, optee, arg);
520 break;
521 case OPTEE_FFA_YIELDING_CALL_RETURN_INTERRUPT:
522 /* Interrupt delivered by now */
523 break;
524 default:
525 pr_warn("Unknown RPC func 0x%x\n", cmd);
526 break;
527 }
528 }
529
optee_ffa_yielding_call(struct tee_context * ctx,struct ffa_send_direct_data * data,struct optee_msg_arg * rpc_arg,bool system_thread)530 static int optee_ffa_yielding_call(struct tee_context *ctx,
531 struct ffa_send_direct_data *data,
532 struct optee_msg_arg *rpc_arg,
533 bool system_thread)
534 {
535 struct optee *optee = tee_get_drvdata(ctx->teedev);
536 struct ffa_device *ffa_dev = optee->ffa.ffa_dev;
537 const struct ffa_msg_ops *msg_ops = ffa_dev->ops->msg_ops;
538 struct optee_call_waiter w;
539 u32 cmd = data->data0;
540 u32 w4 = data->data1;
541 u32 w5 = data->data2;
542 u32 w6 = data->data3;
543 int rc;
544
545 /* Initialize waiter */
546 optee_cq_wait_init(&optee->call_queue, &w, system_thread);
547 while (true) {
548 rc = msg_ops->sync_send_receive(ffa_dev, data);
549 if (rc)
550 goto done;
551
552 switch ((int)data->data0) {
553 case TEEC_SUCCESS:
554 break;
555 case TEEC_ERROR_BUSY:
556 if (cmd == OPTEE_FFA_YIELDING_CALL_RESUME) {
557 rc = -EIO;
558 goto done;
559 }
560
561 /*
562 * Out of threads in secure world, wait for a thread
563 * become available.
564 */
565 optee_cq_wait_for_completion(&optee->call_queue, &w);
566 data->data0 = cmd;
567 data->data1 = w4;
568 data->data2 = w5;
569 data->data3 = w6;
570 continue;
571 default:
572 rc = -EIO;
573 goto done;
574 }
575
576 if (data->data1 == OPTEE_FFA_YIELDING_CALL_RETURN_DONE)
577 goto done;
578
579 /*
580 * OP-TEE has returned with a RPC request.
581 *
582 * Note that data->data4 (passed in register w7) is already
583 * filled in by ffa_mem_ops->sync_send_receive() returning
584 * above.
585 */
586 cond_resched();
587 optee_handle_ffa_rpc(ctx, optee, data->data1, rpc_arg);
588 cmd = OPTEE_FFA_YIELDING_CALL_RESUME;
589 data->data0 = cmd;
590 data->data1 = 0;
591 data->data2 = 0;
592 data->data3 = 0;
593 }
594 done:
595 /*
596 * We're done with our thread in secure world, if there's any
597 * thread waiters wake up one.
598 */
599 optee_cq_wait_final(&optee->call_queue, &w);
600
601 return rc;
602 }
603
604 /**
605 * optee_ffa_do_call_with_arg() - Do a FF-A call to enter OP-TEE in secure world
606 * @ctx: calling context
607 * @shm: shared memory holding the message to pass to secure world
608 * @offs: offset of the message in @shm
609 * @system_thread: true if caller requests TEE system thread support
610 *
611 * Does a FF-A call to OP-TEE in secure world and handles eventual resulting
612 * Remote Procedure Calls (RPC) from OP-TEE.
613 *
614 * Returns return code from FF-A, 0 is OK
615 */
616
optee_ffa_do_call_with_arg(struct tee_context * ctx,struct tee_shm * shm,u_int offs,bool system_thread)617 static int optee_ffa_do_call_with_arg(struct tee_context *ctx,
618 struct tee_shm *shm, u_int offs,
619 bool system_thread)
620 {
621 struct ffa_send_direct_data data = {
622 .data0 = OPTEE_FFA_YIELDING_CALL_WITH_ARG,
623 .data1 = (u32)shm->sec_world_id,
624 .data2 = (u32)(shm->sec_world_id >> 32),
625 .data3 = offs,
626 };
627 struct optee_msg_arg *arg;
628 unsigned int rpc_arg_offs;
629 struct optee_msg_arg *rpc_arg;
630
631 /*
632 * The shared memory object has to start on a page when passed as
633 * an argument struct. This is also what the shm pool allocator
634 * returns, but check this before calling secure world to catch
635 * eventual errors early in case something changes.
636 */
637 if (shm->offset)
638 return -EINVAL;
639
640 arg = tee_shm_get_va(shm, offs);
641 if (IS_ERR(arg))
642 return PTR_ERR(arg);
643
644 rpc_arg_offs = OPTEE_MSG_GET_ARG_SIZE(arg->num_params);
645 rpc_arg = tee_shm_get_va(shm, offs + rpc_arg_offs);
646 if (IS_ERR(rpc_arg))
647 return PTR_ERR(rpc_arg);
648
649 return optee_ffa_yielding_call(ctx, &data, rpc_arg, system_thread);
650 }
651
do_call_lend_protmem(struct optee * optee,u64 cookie,u32 use_case)652 static int do_call_lend_protmem(struct optee *optee, u64 cookie, u32 use_case)
653 {
654 struct optee_shm_arg_entry *entry;
655 struct optee_msg_arg *msg_arg;
656 struct tee_shm *shm;
657 u_int offs;
658 int rc;
659
660 msg_arg = optee_get_msg_arg(optee->ctx, 1, &entry, &shm, &offs);
661 if (IS_ERR(msg_arg))
662 return PTR_ERR(msg_arg);
663
664 msg_arg->cmd = OPTEE_MSG_CMD_ASSIGN_PROTMEM;
665 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
666 msg_arg->params[0].u.value.a = cookie;
667 msg_arg->params[0].u.value.b = use_case;
668
669 rc = optee->ops->do_call_with_arg(optee->ctx, shm, offs, false);
670 if (rc)
671 goto out;
672 if (msg_arg->ret != TEEC_SUCCESS) {
673 rc = -EINVAL;
674 goto out;
675 }
676
677 out:
678 optee_free_msg_arg(optee->ctx, entry, offs);
679 return rc;
680 }
681
optee_ffa_lend_protmem(struct optee * optee,struct tee_shm * protmem,u32 * mem_attrs,unsigned int ma_count,u32 use_case)682 static int optee_ffa_lend_protmem(struct optee *optee, struct tee_shm *protmem,
683 u32 *mem_attrs, unsigned int ma_count,
684 u32 use_case)
685 {
686 struct ffa_device *ffa_dev = optee->ffa.ffa_dev;
687 const struct ffa_mem_ops *mem_ops = ffa_dev->ops->mem_ops;
688 const struct ffa_msg_ops *msg_ops = ffa_dev->ops->msg_ops;
689 struct ffa_send_direct_data data;
690 struct ffa_mem_region_attributes *mem_attr;
691 struct ffa_mem_ops_args args = {
692 .use_txbuf = true,
693 .tag = use_case,
694 };
695 struct page *page;
696 struct scatterlist sgl;
697 unsigned int n;
698 int rc;
699
700 mem_attr = kzalloc_objs(*mem_attr, ma_count);
701 for (n = 0; n < ma_count; n++) {
702 mem_attr[n].receiver = mem_attrs[n] & U16_MAX;
703 mem_attr[n].attrs = mem_attrs[n] >> 16;
704 }
705 args.attrs = mem_attr;
706 args.nattrs = ma_count;
707
708 page = phys_to_page(protmem->paddr);
709 sg_init_table(&sgl, 1);
710 sg_set_page(&sgl, page, protmem->size, 0);
711
712 args.sg = &sgl;
713 rc = mem_ops->memory_lend(&args);
714 kfree(mem_attr);
715 if (rc)
716 return rc;
717
718 rc = do_call_lend_protmem(optee, args.g_handle, use_case);
719 if (rc)
720 goto err_reclaim;
721
722 rc = optee_shm_add_ffa_handle(optee, protmem, args.g_handle);
723 if (rc)
724 goto err_unreg;
725
726 protmem->sec_world_id = args.g_handle;
727
728 return 0;
729
730 err_unreg:
731 data = (struct ffa_send_direct_data){
732 .data0 = OPTEE_FFA_RELEASE_PROTMEM,
733 .data1 = (u32)args.g_handle,
734 .data2 = (u32)(args.g_handle >> 32),
735 };
736 msg_ops->sync_send_receive(ffa_dev, &data);
737 err_reclaim:
738 mem_ops->memory_reclaim(args.g_handle, 0);
739 return rc;
740 }
741
optee_ffa_reclaim_protmem(struct optee * optee,struct tee_shm * protmem)742 static int optee_ffa_reclaim_protmem(struct optee *optee,
743 struct tee_shm *protmem)
744 {
745 struct ffa_device *ffa_dev = optee->ffa.ffa_dev;
746 const struct ffa_msg_ops *msg_ops = ffa_dev->ops->msg_ops;
747 const struct ffa_mem_ops *mem_ops = ffa_dev->ops->mem_ops;
748 u64 global_handle = protmem->sec_world_id;
749 struct ffa_send_direct_data data = {
750 .data0 = OPTEE_FFA_RELEASE_PROTMEM,
751 .data1 = (u32)global_handle,
752 .data2 = (u32)(global_handle >> 32)
753 };
754 int rc;
755
756 optee_shm_rem_ffa_handle(optee, global_handle);
757 protmem->sec_world_id = 0;
758
759 rc = msg_ops->sync_send_receive(ffa_dev, &data);
760 if (rc)
761 pr_err("Release SHM id 0x%llx rc %d\n", global_handle, rc);
762
763 rc = mem_ops->memory_reclaim(global_handle, 0);
764 if (rc)
765 pr_err("mem_reclaim: 0x%llx %d", global_handle, rc);
766
767 return rc;
768 }
769
770 /*
771 * 6. Driver initialization
772 *
773 * During driver inititialization is the OP-TEE Secure Partition is probed
774 * to find out which features it supports so the driver can be initialized
775 * with a matching configuration.
776 */
777
optee_ffa_get_os_revision(struct ffa_device * ffa_dev,const struct ffa_ops * ops,struct optee_revision * revision)778 static bool optee_ffa_get_os_revision(struct ffa_device *ffa_dev,
779 const struct ffa_ops *ops,
780 struct optee_revision *revision)
781 {
782 const struct ffa_msg_ops *msg_ops = ops->msg_ops;
783 struct ffa_send_direct_data data = {
784 .data0 = OPTEE_FFA_GET_OS_VERSION,
785 };
786 int rc;
787
788 msg_ops->mode_32bit_set(ffa_dev);
789
790 rc = msg_ops->sync_send_receive(ffa_dev, &data);
791 if (rc) {
792 pr_err("Unexpected error %d\n", rc);
793 return false;
794 }
795
796 if (revision) {
797 revision->os_major = data.data0;
798 revision->os_minor = data.data1;
799 revision->os_build_id = data.data2;
800 }
801
802 if (data.data2)
803 pr_info("revision %lu.%lu (%08lx)",
804 data.data0, data.data1, data.data2);
805 else
806 pr_info("revision %lu.%lu", data.data0, data.data1);
807
808 return true;
809 }
810
optee_ffa_api_is_compatible(struct ffa_device * ffa_dev,const struct ffa_ops * ops)811 static bool optee_ffa_api_is_compatible(struct ffa_device *ffa_dev,
812 const struct ffa_ops *ops)
813 {
814 const struct ffa_msg_ops *msg_ops = ops->msg_ops;
815 struct ffa_send_direct_data data = {
816 .data0 = OPTEE_FFA_GET_API_VERSION,
817 };
818 int rc;
819
820 msg_ops->mode_32bit_set(ffa_dev);
821
822 rc = msg_ops->sync_send_receive(ffa_dev, &data);
823 if (rc) {
824 pr_err("Unexpected error %d\n", rc);
825 return false;
826 }
827 if (data.data0 != OPTEE_FFA_VERSION_MAJOR ||
828 data.data1 < OPTEE_FFA_VERSION_MINOR) {
829 pr_err("Incompatible OP-TEE API version %lu.%lu",
830 data.data0, data.data1);
831 return false;
832 }
833
834 return true;
835 }
836
optee_ffa_exchange_caps(struct ffa_device * ffa_dev,const struct ffa_ops * ops,u32 * sec_caps,unsigned int * rpc_param_count,unsigned int * max_notif_value)837 static bool optee_ffa_exchange_caps(struct ffa_device *ffa_dev,
838 const struct ffa_ops *ops,
839 u32 *sec_caps,
840 unsigned int *rpc_param_count,
841 unsigned int *max_notif_value)
842 {
843 struct ffa_send_direct_data data = {
844 .data0 = OPTEE_FFA_EXCHANGE_CAPABILITIES,
845 };
846 int rc;
847
848 rc = ops->msg_ops->sync_send_receive(ffa_dev, &data);
849 if (rc) {
850 pr_err("Unexpected error %d", rc);
851 return false;
852 }
853 if (data.data0) {
854 pr_err("Unexpected exchange error %lu", data.data0);
855 return false;
856 }
857
858 *rpc_param_count = (u8)data.data1;
859 *sec_caps = data.data2;
860 if (data.data3)
861 *max_notif_value = data.data3;
862 else
863 *max_notif_value = OPTEE_DEFAULT_MAX_NOTIF_VALUE;
864
865 return true;
866 }
867
notif_work_fn(struct work_struct * work)868 static void notif_work_fn(struct work_struct *work)
869 {
870 struct optee_ffa *optee_ffa = container_of(work, struct optee_ffa,
871 notif_work);
872 struct optee *optee = container_of(optee_ffa, struct optee, ffa);
873
874 optee_do_bottom_half(optee->ctx);
875 }
876
notif_callback(int notify_id,void * cb_data)877 static void notif_callback(int notify_id, void *cb_data)
878 {
879 struct optee *optee = cb_data;
880
881 if (notify_id == optee->ffa.bottom_half_value)
882 queue_work(optee->ffa.notif_wq, &optee->ffa.notif_work);
883 else
884 optee_notif_send(optee, notify_id);
885 }
886
enable_async_notif(struct optee * optee)887 static int enable_async_notif(struct optee *optee)
888 {
889 struct ffa_device *ffa_dev = optee->ffa.ffa_dev;
890 struct ffa_send_direct_data data = {
891 .data0 = OPTEE_FFA_ENABLE_ASYNC_NOTIF,
892 .data1 = optee->ffa.bottom_half_value,
893 };
894 int rc;
895
896 rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &data);
897 if (rc)
898 return rc;
899 return data.data0;
900 }
901
optee_ffa_get_version(struct tee_device * teedev,struct tee_ioctl_version_data * vers)902 static void optee_ffa_get_version(struct tee_device *teedev,
903 struct tee_ioctl_version_data *vers)
904 {
905 struct tee_ioctl_version_data v = {
906 .impl_id = TEE_IMPL_ID_OPTEE,
907 .impl_caps = TEE_OPTEE_CAP_TZ,
908 .gen_caps = TEE_GEN_CAP_GP | TEE_GEN_CAP_REG_MEM |
909 TEE_GEN_CAP_MEMREF_NULL,
910 };
911
912 *vers = v;
913 }
914
optee_ffa_open(struct tee_context * ctx)915 static int optee_ffa_open(struct tee_context *ctx)
916 {
917 return optee_open(ctx, true);
918 }
919
920 static const struct tee_driver_ops optee_ffa_clnt_ops = {
921 .get_version = optee_ffa_get_version,
922 .get_tee_revision = optee_get_revision,
923 .open = optee_ffa_open,
924 .release = optee_release,
925 .open_session = optee_open_session,
926 .close_session = optee_close_session,
927 .invoke_func = optee_invoke_func,
928 .cancel_req = optee_cancel_req,
929 .shm_register = optee_ffa_shm_register,
930 .shm_unregister = optee_ffa_shm_unregister,
931 };
932
933 static const struct tee_desc optee_ffa_clnt_desc = {
934 .name = DRIVER_NAME "-ffa-clnt",
935 .ops = &optee_ffa_clnt_ops,
936 .owner = THIS_MODULE,
937 };
938
939 static const struct tee_driver_ops optee_ffa_supp_ops = {
940 .get_version = optee_ffa_get_version,
941 .get_tee_revision = optee_get_revision,
942 .open = optee_ffa_open,
943 .release = optee_release_supp,
944 .supp_recv = optee_supp_recv,
945 .supp_send = optee_supp_send,
946 .shm_register = optee_ffa_shm_register, /* same as for clnt ops */
947 .shm_unregister = optee_ffa_shm_unregister_supp,
948 };
949
950 static const struct tee_desc optee_ffa_supp_desc = {
951 .name = DRIVER_NAME "-ffa-supp",
952 .ops = &optee_ffa_supp_ops,
953 .owner = THIS_MODULE,
954 .flags = TEE_DESC_PRIVILEGED,
955 };
956
957 static const struct optee_ops optee_ffa_ops = {
958 .do_call_with_arg = optee_ffa_do_call_with_arg,
959 .to_msg_param = optee_ffa_to_msg_param,
960 .from_msg_param = optee_ffa_from_msg_param,
961 .lend_protmem = optee_ffa_lend_protmem,
962 .reclaim_protmem = optee_ffa_reclaim_protmem,
963 };
964
optee_ffa_remove(struct ffa_device * ffa_dev)965 static void optee_ffa_remove(struct ffa_device *ffa_dev)
966 {
967 struct optee *optee = ffa_dev_get_drvdata(ffa_dev);
968 u32 bottom_half_id = optee->ffa.bottom_half_value;
969
970 if (bottom_half_id != U32_MAX) {
971 ffa_dev->ops->notifier_ops->notify_relinquish(ffa_dev,
972 bottom_half_id);
973 destroy_workqueue(optee->ffa.notif_wq);
974 }
975 optee_remove_common(optee);
976
977 mutex_destroy(&optee->ffa.mutex);
978 rhashtable_free_and_destroy(&optee->ffa.global_ids, rh_free_fn, NULL);
979
980 kfree(optee);
981 }
982
optee_ffa_async_notif_init(struct ffa_device * ffa_dev,struct optee * optee)983 static int optee_ffa_async_notif_init(struct ffa_device *ffa_dev,
984 struct optee *optee)
985 {
986 bool is_per_vcpu = false;
987 u32 notif_id = 0;
988 int rc;
989
990 INIT_WORK(&optee->ffa.notif_work, notif_work_fn);
991 optee->ffa.notif_wq = create_workqueue("optee_notification");
992 if (!optee->ffa.notif_wq) {
993 rc = -EINVAL;
994 goto err;
995 }
996
997 while (true) {
998 rc = ffa_dev->ops->notifier_ops->notify_request(ffa_dev,
999 is_per_vcpu,
1000 notif_callback,
1001 optee,
1002 notif_id);
1003 if (!rc)
1004 break;
1005 /*
1006 * -EACCES means that the notification ID was
1007 * already bound, try the next one as long as we
1008 * haven't reached the max. Any other error is a
1009 * permanent error, so skip asynchronous
1010 * notifications in that case.
1011 */
1012 if (rc != -EACCES)
1013 goto err_wq;
1014 notif_id++;
1015 if (notif_id >= OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE)
1016 goto err_wq;
1017 }
1018 optee->ffa.bottom_half_value = notif_id;
1019
1020 rc = enable_async_notif(optee);
1021 if (rc < 0)
1022 goto err_rel;
1023
1024 return 0;
1025 err_rel:
1026 ffa_dev->ops->notifier_ops->notify_relinquish(ffa_dev, notif_id);
1027 err_wq:
1028 destroy_workqueue(optee->ffa.notif_wq);
1029 err:
1030 optee->ffa.bottom_half_value = U32_MAX;
1031
1032 return rc;
1033 }
1034
optee_ffa_protmem_pool_init(struct optee * optee,u32 sec_caps)1035 static int optee_ffa_protmem_pool_init(struct optee *optee, u32 sec_caps)
1036 {
1037 enum tee_dma_heap_id id = TEE_DMA_HEAP_SECURE_VIDEO_PLAY;
1038 struct tee_protmem_pool *pool;
1039 int rc = 0;
1040
1041 if (sec_caps & OPTEE_FFA_SEC_CAP_PROTMEM) {
1042 pool = optee_protmem_alloc_dyn_pool(optee, id);
1043 if (IS_ERR(pool))
1044 return PTR_ERR(pool);
1045
1046 rc = tee_device_register_dma_heap(optee->teedev, id, pool);
1047 if (rc)
1048 pool->ops->destroy_pool(pool);
1049 }
1050
1051 return rc;
1052 }
1053
optee_ffa_probe(struct ffa_device * ffa_dev)1054 static int optee_ffa_probe(struct ffa_device *ffa_dev)
1055 {
1056 const struct ffa_notifier_ops *notif_ops;
1057 const struct ffa_ops *ffa_ops;
1058 unsigned int max_notif_value;
1059 unsigned int rpc_param_count;
1060 struct tee_shm_pool *pool;
1061 struct tee_device *teedev;
1062 struct tee_context *ctx;
1063 u32 arg_cache_flags = 0;
1064 struct optee *optee;
1065 u32 sec_caps;
1066 int rc;
1067
1068 ffa_ops = ffa_dev->ops;
1069 notif_ops = ffa_ops->notifier_ops;
1070
1071 if (!optee_ffa_api_is_compatible(ffa_dev, ffa_ops))
1072 return -EINVAL;
1073
1074 if (!optee_ffa_exchange_caps(ffa_dev, ffa_ops, &sec_caps,
1075 &rpc_param_count, &max_notif_value))
1076 return -EINVAL;
1077 if (sec_caps & OPTEE_FFA_SEC_CAP_ARG_OFFSET)
1078 arg_cache_flags |= OPTEE_SHM_ARG_SHARED;
1079
1080 optee = kzalloc_obj(*optee);
1081 if (!optee)
1082 return -ENOMEM;
1083
1084 if (!optee_ffa_get_os_revision(ffa_dev, ffa_ops, &optee->revision)) {
1085 rc = -EINVAL;
1086 goto err_free_optee;
1087 }
1088
1089 pool = optee_ffa_shm_pool_alloc_pages();
1090 if (IS_ERR(pool)) {
1091 rc = PTR_ERR(pool);
1092 goto err_free_optee;
1093 }
1094 optee->pool = pool;
1095
1096 optee->ops = &optee_ffa_ops;
1097 optee->ffa.ffa_dev = ffa_dev;
1098 optee->ffa.bottom_half_value = U32_MAX;
1099 optee->rpc_param_count = rpc_param_count;
1100
1101 if (IS_REACHABLE(CONFIG_RPMB) &&
1102 (sec_caps & OPTEE_FFA_SEC_CAP_RPMB_PROBE))
1103 optee->in_kernel_rpmb_routing = true;
1104
1105 teedev = tee_device_alloc(&optee_ffa_clnt_desc, NULL, optee->pool,
1106 optee);
1107 if (IS_ERR(teedev)) {
1108 rc = PTR_ERR(teedev);
1109 goto err_free_shm_pool;
1110 }
1111 optee->teedev = teedev;
1112
1113 teedev = tee_device_alloc(&optee_ffa_supp_desc, NULL, optee->pool,
1114 optee);
1115 if (IS_ERR(teedev)) {
1116 rc = PTR_ERR(teedev);
1117 goto err_unreg_teedev;
1118 }
1119 optee->supp_teedev = teedev;
1120
1121 optee_set_dev_group(optee);
1122
1123 rc = tee_device_register(optee->teedev);
1124 if (rc)
1125 goto err_unreg_supp_teedev;
1126
1127 rc = tee_device_register(optee->supp_teedev);
1128 if (rc)
1129 goto err_unreg_supp_teedev;
1130
1131 rc = rhashtable_init(&optee->ffa.global_ids, &shm_rhash_params);
1132 if (rc)
1133 goto err_unreg_supp_teedev;
1134 mutex_init(&optee->ffa.mutex);
1135 optee_cq_init(&optee->call_queue, 0);
1136 optee_supp_init(&optee->supp);
1137 optee_shm_arg_cache_init(optee, arg_cache_flags);
1138 mutex_init(&optee->rpmb_dev_mutex);
1139 ffa_dev_set_drvdata(ffa_dev, optee);
1140 ctx = teedev_open(optee->teedev);
1141 if (IS_ERR(ctx)) {
1142 rc = PTR_ERR(ctx);
1143 goto err_rhashtable_free;
1144 }
1145 optee->ctx = ctx;
1146 rc = optee_notif_init(optee, OPTEE_DEFAULT_MAX_NOTIF_VALUE);
1147 if (rc)
1148 goto err_close_ctx;
1149 if (sec_caps & OPTEE_FFA_SEC_CAP_ASYNC_NOTIF) {
1150 rc = optee_ffa_async_notif_init(ffa_dev, optee);
1151 if (rc < 0)
1152 pr_err("Failed to initialize async notifications: %d",
1153 rc);
1154 }
1155
1156 if (optee_ffa_protmem_pool_init(optee, sec_caps))
1157 pr_info("Protected memory service not available\n");
1158
1159 rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);
1160 if (rc)
1161 goto err_unregister_devices;
1162
1163 INIT_WORK(&optee->rpmb_scan_bus_work, optee_bus_scan_rpmb);
1164 optee->rpmb_intf.notifier_call = optee_rpmb_intf_rdev;
1165 blocking_notifier_chain_register(&optee_rpmb_intf_added,
1166 &optee->rpmb_intf);
1167 pr_info("initialized driver\n");
1168 return 0;
1169
1170 err_unregister_devices:
1171 optee_unregister_devices();
1172 if (optee->ffa.bottom_half_value != U32_MAX)
1173 notif_ops->notify_relinquish(ffa_dev,
1174 optee->ffa.bottom_half_value);
1175 optee_notif_uninit(optee);
1176 err_close_ctx:
1177 teedev_close_context(ctx);
1178 err_rhashtable_free:
1179 rhashtable_free_and_destroy(&optee->ffa.global_ids, rh_free_fn, NULL);
1180 rpmb_dev_put(optee->rpmb_dev);
1181 mutex_destroy(&optee->rpmb_dev_mutex);
1182 optee_supp_uninit(&optee->supp);
1183 mutex_destroy(&optee->call_queue.mutex);
1184 mutex_destroy(&optee->ffa.mutex);
1185 err_unreg_supp_teedev:
1186 tee_device_unregister(optee->supp_teedev);
1187 err_unreg_teedev:
1188 tee_device_unregister(optee->teedev);
1189 err_free_shm_pool:
1190 tee_shm_pool_free(pool);
1191 err_free_optee:
1192 kfree(optee);
1193 return rc;
1194 }
1195
1196 static const struct ffa_device_id optee_ffa_device_id[] = {
1197 /* 486178e0-e7f8-11e3-bc5e0002a5d5c51b */
1198 { UUID_INIT(0x486178e0, 0xe7f8, 0x11e3,
1199 0xbc, 0x5e, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b) },
1200 {}
1201 };
1202
1203 static struct ffa_driver optee_ffa_driver = {
1204 .name = "optee",
1205 .probe = optee_ffa_probe,
1206 .remove = optee_ffa_remove,
1207 .id_table = optee_ffa_device_id,
1208 };
1209
optee_ffa_abi_register(void)1210 int optee_ffa_abi_register(void)
1211 {
1212 if (IS_REACHABLE(CONFIG_ARM_FFA_TRANSPORT))
1213 return ffa_register(&optee_ffa_driver);
1214 else
1215 return -EOPNOTSUPP;
1216 }
1217
optee_ffa_abi_unregister(void)1218 void optee_ffa_abi_unregister(void)
1219 {
1220 if (IS_REACHABLE(CONFIG_ARM_FFA_TRANSPORT))
1221 ffa_unregister(&optee_ffa_driver);
1222 }
1223