1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2015, Linaro Limited
4 */
5 #include <linux/arm-smccc.h>
6 #include <linux/device.h>
7 #include <linux/err.h>
8 #include <linux/errno.h>
9 #include <linux/mm.h>
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/tee_drv.h>
13 #include <linux/types.h>
14 #include <linux/uaccess.h>
15 #include "optee_private.h"
16 #include "optee_smc.h"
17
18 struct optee_call_waiter {
19 struct list_head list_node;
20 struct completion c;
21 };
22
optee_cq_wait_init(struct optee_call_queue * cq,struct optee_call_waiter * w)23 static void optee_cq_wait_init(struct optee_call_queue *cq,
24 struct optee_call_waiter *w)
25 {
26 /*
27 * We're preparing to make a call to secure world. In case we can't
28 * allocate a thread in secure world we'll end up waiting in
29 * optee_cq_wait_for_completion().
30 *
31 * Normally if there's no contention in secure world the call will
32 * complete and we can cleanup directly with optee_cq_wait_final().
33 */
34 mutex_lock(&cq->mutex);
35
36 /*
37 * We add ourselves to the queue, but we don't wait. This
38 * guarantees that we don't lose a completion if secure world
39 * returns busy and another thread just exited and try to complete
40 * someone.
41 */
42 init_completion(&w->c);
43 list_add_tail(&w->list_node, &cq->waiters);
44
45 mutex_unlock(&cq->mutex);
46 }
47
optee_cq_wait_for_completion(struct optee_call_queue * cq,struct optee_call_waiter * w)48 static void optee_cq_wait_for_completion(struct optee_call_queue *cq,
49 struct optee_call_waiter *w)
50 {
51 wait_for_completion(&w->c);
52
53 mutex_lock(&cq->mutex);
54
55 /* Move to end of list to get out of the way for other waiters */
56 list_del(&w->list_node);
57 reinit_completion(&w->c);
58 list_add_tail(&w->list_node, &cq->waiters);
59
60 mutex_unlock(&cq->mutex);
61 }
62
optee_cq_complete_one(struct optee_call_queue * cq)63 static void optee_cq_complete_one(struct optee_call_queue *cq)
64 {
65 struct optee_call_waiter *w;
66
67 list_for_each_entry(w, &cq->waiters, list_node) {
68 if (!completion_done(&w->c)) {
69 complete(&w->c);
70 break;
71 }
72 }
73 }
74
optee_cq_wait_final(struct optee_call_queue * cq,struct optee_call_waiter * w)75 static void optee_cq_wait_final(struct optee_call_queue *cq,
76 struct optee_call_waiter *w)
77 {
78 /*
79 * We're done with the call to secure world. The thread in secure
80 * world that was used for this call is now available for some
81 * other task to use.
82 */
83 mutex_lock(&cq->mutex);
84
85 /* Get out of the list */
86 list_del(&w->list_node);
87
88 /* Wake up one eventual waiting task */
89 optee_cq_complete_one(cq);
90
91 /*
92 * If we're completed we've got a completion from another task that
93 * was just done with its call to secure world. Since yet another
94 * thread now is available in secure world wake up another eventual
95 * waiting task.
96 */
97 if (completion_done(&w->c))
98 optee_cq_complete_one(cq);
99
100 mutex_unlock(&cq->mutex);
101 }
102
103 /* Requires the filpstate mutex to be held */
find_session(struct optee_context_data * ctxdata,u32 session_id)104 static struct optee_session *find_session(struct optee_context_data *ctxdata,
105 u32 session_id)
106 {
107 struct optee_session *sess;
108
109 list_for_each_entry(sess, &ctxdata->sess_list, list_node)
110 if (sess->session_id == session_id)
111 return sess;
112
113 return NULL;
114 }
115
116 /**
117 * optee_do_call_with_arg() - Do an SMC to OP-TEE in secure world
118 * @ctx: calling context
119 * @parg: physical address of message to pass to secure world
120 *
121 * Does and SMC to OP-TEE in secure world and handles eventual resulting
122 * Remote Procedure Calls (RPC) from OP-TEE.
123 *
124 * Returns return code from secure world, 0 is OK
125 */
optee_do_call_with_arg(struct tee_context * ctx,phys_addr_t parg)126 u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg)
127 {
128 struct optee *optee = tee_get_drvdata(ctx->teedev);
129 struct optee_call_waiter w;
130 struct optee_rpc_param param = { };
131 struct optee_call_ctx call_ctx = { };
132 u32 ret;
133
134 param.a0 = OPTEE_SMC_CALL_WITH_ARG;
135 reg_pair_from_64(¶m.a1, ¶m.a2, parg);
136 /* Initialize waiter */
137 optee_cq_wait_init(&optee->call_queue, &w);
138 while (true) {
139 struct arm_smccc_res res;
140
141 optee->invoke_fn(param.a0, param.a1, param.a2, param.a3,
142 param.a4, param.a5, param.a6, param.a7,
143 &res);
144
145 if (res.a0 == OPTEE_SMC_RETURN_ETHREAD_LIMIT) {
146 /*
147 * Out of threads in secure world, wait for a thread
148 * become available.
149 */
150 optee_cq_wait_for_completion(&optee->call_queue, &w);
151 } else if (OPTEE_SMC_RETURN_IS_RPC(res.a0)) {
152 if (need_resched())
153 cond_resched();
154 param.a0 = res.a0;
155 param.a1 = res.a1;
156 param.a2 = res.a2;
157 param.a3 = res.a3;
158 optee_handle_rpc(ctx, ¶m, &call_ctx);
159 } else {
160 ret = res.a0;
161 break;
162 }
163 }
164
165 optee_rpc_finalize_call(&call_ctx);
166 /*
167 * We're done with our thread in secure world, if there's any
168 * thread waiters wake up one.
169 */
170 optee_cq_wait_final(&optee->call_queue, &w);
171
172 return ret;
173 }
174
get_msg_arg(struct tee_context * ctx,size_t num_params,struct optee_msg_arg ** msg_arg,phys_addr_t * msg_parg)175 static struct tee_shm *get_msg_arg(struct tee_context *ctx, size_t num_params,
176 struct optee_msg_arg **msg_arg,
177 phys_addr_t *msg_parg)
178 {
179 int rc;
180 struct tee_shm *shm;
181 struct optee_msg_arg *ma;
182
183 shm = tee_shm_alloc(ctx, OPTEE_MSG_GET_ARG_SIZE(num_params),
184 TEE_SHM_MAPPED | TEE_SHM_PRIV);
185 if (IS_ERR(shm))
186 return shm;
187
188 ma = tee_shm_get_va(shm, 0);
189 if (IS_ERR(ma)) {
190 rc = PTR_ERR(ma);
191 goto out;
192 }
193
194 rc = tee_shm_get_pa(shm, 0, msg_parg);
195 if (rc)
196 goto out;
197
198 memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));
199 ma->num_params = num_params;
200 *msg_arg = ma;
201 out:
202 if (rc) {
203 tee_shm_free(shm);
204 return ERR_PTR(rc);
205 }
206
207 return shm;
208 }
209
optee_open_session(struct tee_context * ctx,struct tee_ioctl_open_session_arg * arg,struct tee_param * param)210 int optee_open_session(struct tee_context *ctx,
211 struct tee_ioctl_open_session_arg *arg,
212 struct tee_param *param)
213 {
214 struct optee_context_data *ctxdata = ctx->data;
215 int rc;
216 struct tee_shm *shm;
217 struct optee_msg_arg *msg_arg;
218 phys_addr_t msg_parg;
219 struct optee_session *sess = NULL;
220 uuid_t client_uuid;
221
222 /* +2 for the meta parameters added below */
223 shm = get_msg_arg(ctx, arg->num_params + 2, &msg_arg, &msg_parg);
224 if (IS_ERR(shm))
225 return PTR_ERR(shm);
226
227 msg_arg->cmd = OPTEE_MSG_CMD_OPEN_SESSION;
228 msg_arg->cancel_id = arg->cancel_id;
229
230 /*
231 * Initialize and add the meta parameters needed when opening a
232 * session.
233 */
234 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
235 OPTEE_MSG_ATTR_META;
236 msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
237 OPTEE_MSG_ATTR_META;
238 memcpy(&msg_arg->params[0].u.value, arg->uuid, sizeof(arg->uuid));
239 msg_arg->params[1].u.value.c = arg->clnt_login;
240
241 rc = tee_session_calc_client_uuid(&client_uuid, arg->clnt_login,
242 arg->clnt_uuid);
243 if (rc)
244 goto out;
245 export_uuid(msg_arg->params[1].u.octets, &client_uuid);
246
247 rc = optee_to_msg_param(msg_arg->params + 2, arg->num_params, param);
248 if (rc)
249 goto out;
250
251 sess = kzalloc(sizeof(*sess), GFP_KERNEL);
252 if (!sess) {
253 rc = -ENOMEM;
254 goto out;
255 }
256
257 if (optee_do_call_with_arg(ctx, msg_parg)) {
258 msg_arg->ret = TEEC_ERROR_COMMUNICATION;
259 msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
260 }
261
262 if (msg_arg->ret == TEEC_SUCCESS) {
263 /* A new session has been created, add it to the list. */
264 sess->session_id = msg_arg->session;
265 mutex_lock(&ctxdata->mutex);
266 list_add(&sess->list_node, &ctxdata->sess_list);
267 mutex_unlock(&ctxdata->mutex);
268 } else {
269 kfree(sess);
270 }
271
272 if (optee_from_msg_param(param, arg->num_params, msg_arg->params + 2)) {
273 arg->ret = TEEC_ERROR_COMMUNICATION;
274 arg->ret_origin = TEEC_ORIGIN_COMMS;
275 /* Close session again to avoid leakage */
276 optee_close_session(ctx, msg_arg->session);
277 } else {
278 arg->session = msg_arg->session;
279 arg->ret = msg_arg->ret;
280 arg->ret_origin = msg_arg->ret_origin;
281 }
282 out:
283 tee_shm_free(shm);
284
285 return rc;
286 }
287
optee_close_session(struct tee_context * ctx,u32 session)288 int optee_close_session(struct tee_context *ctx, u32 session)
289 {
290 struct optee_context_data *ctxdata = ctx->data;
291 struct tee_shm *shm;
292 struct optee_msg_arg *msg_arg;
293 phys_addr_t msg_parg;
294 struct optee_session *sess;
295
296 /* Check that the session is valid and remove it from the list */
297 mutex_lock(&ctxdata->mutex);
298 sess = find_session(ctxdata, session);
299 if (sess)
300 list_del(&sess->list_node);
301 mutex_unlock(&ctxdata->mutex);
302 if (!sess)
303 return -EINVAL;
304 kfree(sess);
305
306 shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg);
307 if (IS_ERR(shm))
308 return PTR_ERR(shm);
309
310 msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
311 msg_arg->session = session;
312 optee_do_call_with_arg(ctx, msg_parg);
313
314 tee_shm_free(shm);
315 return 0;
316 }
317
optee_invoke_func(struct tee_context * ctx,struct tee_ioctl_invoke_arg * arg,struct tee_param * param)318 int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
319 struct tee_param *param)
320 {
321 struct optee_context_data *ctxdata = ctx->data;
322 struct tee_shm *shm;
323 struct optee_msg_arg *msg_arg;
324 phys_addr_t msg_parg;
325 struct optee_session *sess;
326 int rc;
327
328 /* Check that the session is valid */
329 mutex_lock(&ctxdata->mutex);
330 sess = find_session(ctxdata, arg->session);
331 mutex_unlock(&ctxdata->mutex);
332 if (!sess)
333 return -EINVAL;
334
335 shm = get_msg_arg(ctx, arg->num_params, &msg_arg, &msg_parg);
336 if (IS_ERR(shm))
337 return PTR_ERR(shm);
338 msg_arg->cmd = OPTEE_MSG_CMD_INVOKE_COMMAND;
339 msg_arg->func = arg->func;
340 msg_arg->session = arg->session;
341 msg_arg->cancel_id = arg->cancel_id;
342
343 rc = optee_to_msg_param(msg_arg->params, arg->num_params, param);
344 if (rc)
345 goto out;
346
347 if (optee_do_call_with_arg(ctx, msg_parg)) {
348 msg_arg->ret = TEEC_ERROR_COMMUNICATION;
349 msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
350 }
351
352 if (optee_from_msg_param(param, arg->num_params, msg_arg->params)) {
353 msg_arg->ret = TEEC_ERROR_COMMUNICATION;
354 msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
355 }
356
357 arg->ret = msg_arg->ret;
358 arg->ret_origin = msg_arg->ret_origin;
359 out:
360 tee_shm_free(shm);
361 return rc;
362 }
363
optee_cancel_req(struct tee_context * ctx,u32 cancel_id,u32 session)364 int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session)
365 {
366 struct optee_context_data *ctxdata = ctx->data;
367 struct tee_shm *shm;
368 struct optee_msg_arg *msg_arg;
369 phys_addr_t msg_parg;
370 struct optee_session *sess;
371
372 /* Check that the session is valid */
373 mutex_lock(&ctxdata->mutex);
374 sess = find_session(ctxdata, session);
375 mutex_unlock(&ctxdata->mutex);
376 if (!sess)
377 return -EINVAL;
378
379 shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg);
380 if (IS_ERR(shm))
381 return PTR_ERR(shm);
382
383 msg_arg->cmd = OPTEE_MSG_CMD_CANCEL;
384 msg_arg->session = session;
385 msg_arg->cancel_id = cancel_id;
386 optee_do_call_with_arg(ctx, msg_parg);
387
388 tee_shm_free(shm);
389 return 0;
390 }
391
392 /**
393 * optee_enable_shm_cache() - Enables caching of some shared memory allocation
394 * in OP-TEE
395 * @optee: main service struct
396 */
optee_enable_shm_cache(struct optee * optee)397 void optee_enable_shm_cache(struct optee *optee)
398 {
399 struct optee_call_waiter w;
400
401 /* We need to retry until secure world isn't busy. */
402 optee_cq_wait_init(&optee->call_queue, &w);
403 while (true) {
404 struct arm_smccc_res res;
405
406 optee->invoke_fn(OPTEE_SMC_ENABLE_SHM_CACHE, 0, 0, 0, 0, 0, 0,
407 0, &res);
408 if (res.a0 == OPTEE_SMC_RETURN_OK)
409 break;
410 optee_cq_wait_for_completion(&optee->call_queue, &w);
411 }
412 optee_cq_wait_final(&optee->call_queue, &w);
413 }
414
415 /**
416 * __optee_disable_shm_cache() - Disables caching of some shared memory
417 * allocation in OP-TEE
418 * @optee: main service struct
419 * @is_mapped: true if the cached shared memory addresses were mapped by this
420 * kernel, are safe to dereference, and should be freed
421 */
__optee_disable_shm_cache(struct optee * optee,bool is_mapped)422 static void __optee_disable_shm_cache(struct optee *optee, bool is_mapped)
423 {
424 struct optee_call_waiter w;
425
426 /* We need to retry until secure world isn't busy. */
427 optee_cq_wait_init(&optee->call_queue, &w);
428 while (true) {
429 union {
430 struct arm_smccc_res smccc;
431 struct optee_smc_disable_shm_cache_result result;
432 } res;
433
434 optee->invoke_fn(OPTEE_SMC_DISABLE_SHM_CACHE, 0, 0, 0, 0, 0, 0,
435 0, &res.smccc);
436 if (res.result.status == OPTEE_SMC_RETURN_ENOTAVAIL)
437 break; /* All shm's freed */
438 if (res.result.status == OPTEE_SMC_RETURN_OK) {
439 struct tee_shm *shm;
440
441 /*
442 * Shared memory references that were not mapped by
443 * this kernel must be ignored to prevent a crash.
444 */
445 if (!is_mapped)
446 continue;
447
448 shm = reg_pair_to_ptr(res.result.shm_upper32,
449 res.result.shm_lower32);
450 tee_shm_free(shm);
451 } else {
452 optee_cq_wait_for_completion(&optee->call_queue, &w);
453 }
454 }
455 optee_cq_wait_final(&optee->call_queue, &w);
456 }
457
458 /**
459 * optee_disable_shm_cache() - Disables caching of mapped shared memory
460 * allocations in OP-TEE
461 * @optee: main service struct
462 */
optee_disable_shm_cache(struct optee * optee)463 void optee_disable_shm_cache(struct optee *optee)
464 {
465 return __optee_disable_shm_cache(optee, true);
466 }
467
468 /**
469 * optee_disable_unmapped_shm_cache() - Disables caching of shared memory
470 * allocations in OP-TEE which are not
471 * currently mapped
472 * @optee: main service struct
473 */
optee_disable_unmapped_shm_cache(struct optee * optee)474 void optee_disable_unmapped_shm_cache(struct optee *optee)
475 {
476 return __optee_disable_shm_cache(optee, false);
477 }
478
479 #define PAGELIST_ENTRIES_PER_PAGE \
480 ((OPTEE_MSG_NONCONTIG_PAGE_SIZE / sizeof(u64)) - 1)
481
482 /**
483 * optee_fill_pages_list() - write list of user pages to given shared
484 * buffer.
485 *
486 * @dst: page-aligned buffer where list of pages will be stored
487 * @pages: array of pages that represents shared buffer
488 * @num_pages: number of entries in @pages
489 * @page_offset: offset of user buffer from page start
490 *
491 * @dst should be big enough to hold list of user page addresses and
492 * links to the next pages of buffer
493 */
optee_fill_pages_list(u64 * dst,struct page ** pages,int num_pages,size_t page_offset)494 void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
495 size_t page_offset)
496 {
497 int n = 0;
498 phys_addr_t optee_page;
499 /*
500 * Refer to OPTEE_MSG_ATTR_NONCONTIG description in optee_msg.h
501 * for details.
502 */
503 struct {
504 u64 pages_list[PAGELIST_ENTRIES_PER_PAGE];
505 u64 next_page_data;
506 } *pages_data;
507
508 /*
509 * Currently OP-TEE uses 4k page size and it does not looks
510 * like this will change in the future. On other hand, there are
511 * no know ARM architectures with page size < 4k.
512 * Thus the next built assert looks redundant. But the following
513 * code heavily relies on this assumption, so it is better be
514 * safe than sorry.
515 */
516 BUILD_BUG_ON(PAGE_SIZE < OPTEE_MSG_NONCONTIG_PAGE_SIZE);
517
518 pages_data = (void *)dst;
519 /*
520 * If linux page is bigger than 4k, and user buffer offset is
521 * larger than 4k/8k/12k/etc this will skip first 4k pages,
522 * because they bear no value data for OP-TEE.
523 */
524 optee_page = page_to_phys(*pages) +
525 round_down(page_offset, OPTEE_MSG_NONCONTIG_PAGE_SIZE);
526
527 while (true) {
528 pages_data->pages_list[n++] = optee_page;
529
530 if (n == PAGELIST_ENTRIES_PER_PAGE) {
531 pages_data->next_page_data =
532 virt_to_phys(pages_data + 1);
533 pages_data++;
534 n = 0;
535 }
536
537 optee_page += OPTEE_MSG_NONCONTIG_PAGE_SIZE;
538 if (!(optee_page & ~PAGE_MASK)) {
539 if (!--num_pages)
540 break;
541 pages++;
542 optee_page = page_to_phys(*pages);
543 }
544 }
545 }
546
547 /*
548 * The final entry in each pagelist page is a pointer to the next
549 * pagelist page.
550 */
get_pages_list_size(size_t num_entries)551 static size_t get_pages_list_size(size_t num_entries)
552 {
553 int pages = DIV_ROUND_UP(num_entries, PAGELIST_ENTRIES_PER_PAGE);
554
555 return pages * OPTEE_MSG_NONCONTIG_PAGE_SIZE;
556 }
557
optee_allocate_pages_list(size_t num_entries)558 u64 *optee_allocate_pages_list(size_t num_entries)
559 {
560 return alloc_pages_exact(get_pages_list_size(num_entries), GFP_KERNEL);
561 }
562
optee_free_pages_list(void * list,size_t num_entries)563 void optee_free_pages_list(void *list, size_t num_entries)
564 {
565 free_pages_exact(list, get_pages_list_size(num_entries));
566 }
567
is_normal_memory(pgprot_t p)568 static bool is_normal_memory(pgprot_t p)
569 {
570 #if defined(CONFIG_ARM)
571 return (((pgprot_val(p) & L_PTE_MT_MASK) == L_PTE_MT_WRITEALLOC) ||
572 ((pgprot_val(p) & L_PTE_MT_MASK) == L_PTE_MT_WRITEBACK));
573 #elif defined(CONFIG_ARM64)
574 return (pgprot_val(p) & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL);
575 #else
576 #error "Unuspported architecture"
577 #endif
578 }
579
__check_mem_type(struct vm_area_struct * vma,unsigned long end)580 static int __check_mem_type(struct vm_area_struct *vma, unsigned long end)
581 {
582 while (vma && is_normal_memory(vma->vm_page_prot)) {
583 if (vma->vm_end >= end)
584 return 0;
585 vma = vma->vm_next;
586 }
587
588 return -EINVAL;
589 }
590
check_mem_type(unsigned long start,size_t num_pages)591 static int check_mem_type(unsigned long start, size_t num_pages)
592 {
593 struct mm_struct *mm = current->mm;
594 int rc;
595
596 /*
597 * Allow kernel address to register with OP-TEE as kernel
598 * pages are configured as normal memory only.
599 */
600 if (virt_addr_valid(start))
601 return 0;
602
603 mmap_read_lock(mm);
604 rc = __check_mem_type(find_vma(mm, start),
605 start + num_pages * PAGE_SIZE);
606 mmap_read_unlock(mm);
607
608 return rc;
609 }
610
optee_shm_register(struct tee_context * ctx,struct tee_shm * shm,struct page ** pages,size_t num_pages,unsigned long start)611 int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
612 struct page **pages, size_t num_pages,
613 unsigned long start)
614 {
615 struct tee_shm *shm_arg = NULL;
616 struct optee_msg_arg *msg_arg;
617 u64 *pages_list;
618 phys_addr_t msg_parg;
619 int rc;
620
621 if (!num_pages)
622 return -EINVAL;
623
624 rc = check_mem_type(start, num_pages);
625 if (rc)
626 return rc;
627
628 pages_list = optee_allocate_pages_list(num_pages);
629 if (!pages_list)
630 return -ENOMEM;
631
632 shm_arg = get_msg_arg(ctx, 1, &msg_arg, &msg_parg);
633 if (IS_ERR(shm_arg)) {
634 rc = PTR_ERR(shm_arg);
635 goto out;
636 }
637
638 optee_fill_pages_list(pages_list, pages, num_pages,
639 tee_shm_get_page_offset(shm));
640
641 msg_arg->cmd = OPTEE_MSG_CMD_REGISTER_SHM;
642 msg_arg->params->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
643 OPTEE_MSG_ATTR_NONCONTIG;
644 msg_arg->params->u.tmem.shm_ref = (unsigned long)shm;
645 msg_arg->params->u.tmem.size = tee_shm_get_size(shm);
646 /*
647 * In the least bits of msg_arg->params->u.tmem.buf_ptr we
648 * store buffer offset from 4k page, as described in OP-TEE ABI.
649 */
650 msg_arg->params->u.tmem.buf_ptr = virt_to_phys(pages_list) |
651 (tee_shm_get_page_offset(shm) & (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
652
653 if (optee_do_call_with_arg(ctx, msg_parg) ||
654 msg_arg->ret != TEEC_SUCCESS)
655 rc = -EINVAL;
656
657 tee_shm_free(shm_arg);
658 out:
659 optee_free_pages_list(pages_list, num_pages);
660 return rc;
661 }
662
optee_shm_unregister(struct tee_context * ctx,struct tee_shm * shm)663 int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm)
664 {
665 struct tee_shm *shm_arg;
666 struct optee_msg_arg *msg_arg;
667 phys_addr_t msg_parg;
668 int rc = 0;
669
670 shm_arg = get_msg_arg(ctx, 1, &msg_arg, &msg_parg);
671 if (IS_ERR(shm_arg))
672 return PTR_ERR(shm_arg);
673
674 msg_arg->cmd = OPTEE_MSG_CMD_UNREGISTER_SHM;
675
676 msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
677 msg_arg->params[0].u.rmem.shm_ref = (unsigned long)shm;
678
679 if (optee_do_call_with_arg(ctx, msg_parg) ||
680 msg_arg->ret != TEEC_SUCCESS)
681 rc = -EINVAL;
682 tee_shm_free(shm_arg);
683 return rc;
684 }
685
optee_shm_register_supp(struct tee_context * ctx,struct tee_shm * shm,struct page ** pages,size_t num_pages,unsigned long start)686 int optee_shm_register_supp(struct tee_context *ctx, struct tee_shm *shm,
687 struct page **pages, size_t num_pages,
688 unsigned long start)
689 {
690 /*
691 * We don't want to register supplicant memory in OP-TEE.
692 * Instead information about it will be passed in RPC code.
693 */
694 return check_mem_type(start, num_pages);
695 }
696
optee_shm_unregister_supp(struct tee_context * ctx,struct tee_shm * shm)697 int optee_shm_unregister_supp(struct tee_context *ctx, struct tee_shm *shm)
698 {
699 return 0;
700 }
701