1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2015-2016, Linaro Limited
4 */
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/i2c.h>
11 #include <linux/slab.h>
12 #include <linux/tee_drv.h>
13 #include "optee_private.h"
14 #include "optee_smc.h"
15 #include "optee_rpc_cmd.h"
16
17 struct wq_entry {
18 struct list_head link;
19 struct completion c;
20 u32 key;
21 };
22
optee_wait_queue_init(struct optee_wait_queue * priv)23 void optee_wait_queue_init(struct optee_wait_queue *priv)
24 {
25 mutex_init(&priv->mu);
26 INIT_LIST_HEAD(&priv->db);
27 }
28
optee_wait_queue_exit(struct optee_wait_queue * priv)29 void optee_wait_queue_exit(struct optee_wait_queue *priv)
30 {
31 mutex_destroy(&priv->mu);
32 }
33
handle_rpc_func_cmd_get_time(struct optee_msg_arg * arg)34 static void handle_rpc_func_cmd_get_time(struct optee_msg_arg *arg)
35 {
36 struct timespec64 ts;
37
38 if (arg->num_params != 1)
39 goto bad;
40 if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
41 OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT)
42 goto bad;
43
44 ktime_get_real_ts64(&ts);
45 arg->params[0].u.value.a = ts.tv_sec;
46 arg->params[0].u.value.b = ts.tv_nsec;
47
48 arg->ret = TEEC_SUCCESS;
49 return;
50 bad:
51 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
52 }
53
54 #if IS_REACHABLE(CONFIG_I2C)
handle_rpc_func_cmd_i2c_transfer(struct tee_context * ctx,struct optee_msg_arg * arg)55 static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx,
56 struct optee_msg_arg *arg)
57 {
58 struct tee_param *params;
59 struct i2c_adapter *adapter;
60 struct i2c_msg msg = { };
61 size_t i;
62 int ret = -EOPNOTSUPP;
63 u8 attr[] = {
64 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT,
65 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT,
66 TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT,
67 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT,
68 };
69
70 if (arg->num_params != ARRAY_SIZE(attr)) {
71 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
72 return;
73 }
74
75 params = kmalloc_array(arg->num_params, sizeof(struct tee_param),
76 GFP_KERNEL);
77 if (!params) {
78 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
79 return;
80 }
81
82 if (optee_from_msg_param(params, arg->num_params, arg->params))
83 goto bad;
84
85 for (i = 0; i < arg->num_params; i++) {
86 if (params[i].attr != attr[i])
87 goto bad;
88 }
89
90 adapter = i2c_get_adapter(params[0].u.value.b);
91 if (!adapter)
92 goto bad;
93
94 if (params[1].u.value.a & OPTEE_RPC_I2C_FLAGS_TEN_BIT) {
95 if (!i2c_check_functionality(adapter,
96 I2C_FUNC_10BIT_ADDR)) {
97 i2c_put_adapter(adapter);
98 goto bad;
99 }
100
101 msg.flags = I2C_M_TEN;
102 }
103
104 msg.addr = params[0].u.value.c;
105 msg.buf = params[2].u.memref.shm->kaddr;
106 msg.len = params[2].u.memref.size;
107
108 switch (params[0].u.value.a) {
109 case OPTEE_RPC_I2C_TRANSFER_RD:
110 msg.flags |= I2C_M_RD;
111 break;
112 case OPTEE_RPC_I2C_TRANSFER_WR:
113 break;
114 default:
115 i2c_put_adapter(adapter);
116 goto bad;
117 }
118
119 ret = i2c_transfer(adapter, &msg, 1);
120
121 if (ret < 0) {
122 arg->ret = TEEC_ERROR_COMMUNICATION;
123 } else {
124 params[3].u.value.a = msg.len;
125 if (optee_to_msg_param(arg->params, arg->num_params, params))
126 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
127 else
128 arg->ret = TEEC_SUCCESS;
129 }
130
131 i2c_put_adapter(adapter);
132 kfree(params);
133 return;
134 bad:
135 kfree(params);
136 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
137 }
138 #else
handle_rpc_func_cmd_i2c_transfer(struct tee_context * ctx,struct optee_msg_arg * arg)139 static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx,
140 struct optee_msg_arg *arg)
141 {
142 arg->ret = TEEC_ERROR_NOT_SUPPORTED;
143 }
144 #endif
145
wq_entry_get(struct optee_wait_queue * wq,u32 key)146 static struct wq_entry *wq_entry_get(struct optee_wait_queue *wq, u32 key)
147 {
148 struct wq_entry *w;
149
150 mutex_lock(&wq->mu);
151
152 list_for_each_entry(w, &wq->db, link)
153 if (w->key == key)
154 goto out;
155
156 w = kmalloc(sizeof(*w), GFP_KERNEL);
157 if (w) {
158 init_completion(&w->c);
159 w->key = key;
160 list_add_tail(&w->link, &wq->db);
161 }
162 out:
163 mutex_unlock(&wq->mu);
164 return w;
165 }
166
wq_sleep(struct optee_wait_queue * wq,u32 key)167 static void wq_sleep(struct optee_wait_queue *wq, u32 key)
168 {
169 struct wq_entry *w = wq_entry_get(wq, key);
170
171 if (w) {
172 wait_for_completion(&w->c);
173 mutex_lock(&wq->mu);
174 list_del(&w->link);
175 mutex_unlock(&wq->mu);
176 kfree(w);
177 }
178 }
179
wq_wakeup(struct optee_wait_queue * wq,u32 key)180 static void wq_wakeup(struct optee_wait_queue *wq, u32 key)
181 {
182 struct wq_entry *w = wq_entry_get(wq, key);
183
184 if (w)
185 complete(&w->c);
186 }
187
handle_rpc_func_cmd_wq(struct optee * optee,struct optee_msg_arg * arg)188 static void handle_rpc_func_cmd_wq(struct optee *optee,
189 struct optee_msg_arg *arg)
190 {
191 if (arg->num_params != 1)
192 goto bad;
193
194 if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
195 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
196 goto bad;
197
198 switch (arg->params[0].u.value.a) {
199 case OPTEE_RPC_WAIT_QUEUE_SLEEP:
200 wq_sleep(&optee->wait_queue, arg->params[0].u.value.b);
201 break;
202 case OPTEE_RPC_WAIT_QUEUE_WAKEUP:
203 wq_wakeup(&optee->wait_queue, arg->params[0].u.value.b);
204 break;
205 default:
206 goto bad;
207 }
208
209 arg->ret = TEEC_SUCCESS;
210 return;
211 bad:
212 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
213 }
214
handle_rpc_func_cmd_wait(struct optee_msg_arg * arg)215 static void handle_rpc_func_cmd_wait(struct optee_msg_arg *arg)
216 {
217 u32 msec_to_wait;
218
219 if (arg->num_params != 1)
220 goto bad;
221
222 if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
223 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
224 goto bad;
225
226 msec_to_wait = arg->params[0].u.value.a;
227
228 /* Go to interruptible sleep */
229 msleep_interruptible(msec_to_wait);
230
231 arg->ret = TEEC_SUCCESS;
232 return;
233 bad:
234 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
235 }
236
handle_rpc_supp_cmd(struct tee_context * ctx,struct optee_msg_arg * arg)237 static void handle_rpc_supp_cmd(struct tee_context *ctx,
238 struct optee_msg_arg *arg)
239 {
240 struct tee_param *params;
241
242 arg->ret_origin = TEEC_ORIGIN_COMMS;
243
244 params = kmalloc_array(arg->num_params, sizeof(struct tee_param),
245 GFP_KERNEL);
246 if (!params) {
247 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
248 return;
249 }
250
251 if (optee_from_msg_param(params, arg->num_params, arg->params)) {
252 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
253 goto out;
254 }
255
256 arg->ret = optee_supp_thrd_req(ctx, arg->cmd, arg->num_params, params);
257
258 if (optee_to_msg_param(arg->params, arg->num_params, params))
259 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
260 out:
261 kfree(params);
262 }
263
cmd_alloc_suppl(struct tee_context * ctx,size_t sz)264 static struct tee_shm *cmd_alloc_suppl(struct tee_context *ctx, size_t sz)
265 {
266 u32 ret;
267 struct tee_param param;
268 struct optee *optee = tee_get_drvdata(ctx->teedev);
269 struct tee_shm *shm;
270
271 param.attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
272 param.u.value.a = OPTEE_RPC_SHM_TYPE_APPL;
273 param.u.value.b = sz;
274 param.u.value.c = 0;
275
276 ret = optee_supp_thrd_req(ctx, OPTEE_RPC_CMD_SHM_ALLOC, 1, ¶m);
277 if (ret)
278 return ERR_PTR(-ENOMEM);
279
280 mutex_lock(&optee->supp.mutex);
281 /* Increases count as secure world doesn't have a reference */
282 shm = tee_shm_get_from_id(optee->supp.ctx, param.u.value.c);
283 mutex_unlock(&optee->supp.mutex);
284 return shm;
285 }
286
handle_rpc_func_cmd_shm_alloc(struct tee_context * ctx,struct optee * optee,struct optee_msg_arg * arg,struct optee_call_ctx * call_ctx)287 static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
288 struct optee *optee,
289 struct optee_msg_arg *arg,
290 struct optee_call_ctx *call_ctx)
291 {
292 phys_addr_t pa;
293 struct tee_shm *shm;
294 size_t sz;
295 size_t n;
296
297 arg->ret_origin = TEEC_ORIGIN_COMMS;
298
299 if (!arg->num_params ||
300 arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
301 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
302 return;
303 }
304
305 for (n = 1; n < arg->num_params; n++) {
306 if (arg->params[n].attr != OPTEE_MSG_ATTR_TYPE_NONE) {
307 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
308 return;
309 }
310 }
311
312 sz = arg->params[0].u.value.b;
313 switch (arg->params[0].u.value.a) {
314 case OPTEE_RPC_SHM_TYPE_APPL:
315 shm = cmd_alloc_suppl(ctx, sz);
316 break;
317 case OPTEE_RPC_SHM_TYPE_KERNEL:
318 shm = tee_shm_alloc(optee->ctx, sz,
319 TEE_SHM_MAPPED | TEE_SHM_PRIV);
320 break;
321 default:
322 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
323 return;
324 }
325
326 if (IS_ERR(shm)) {
327 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
328 return;
329 }
330
331 if (tee_shm_get_pa(shm, 0, &pa)) {
332 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
333 goto bad;
334 }
335
336 sz = tee_shm_get_size(shm);
337
338 if (tee_shm_is_registered(shm)) {
339 struct page **pages;
340 u64 *pages_list;
341 size_t page_num;
342
343 pages = tee_shm_get_pages(shm, &page_num);
344 if (!pages || !page_num) {
345 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
346 goto bad;
347 }
348
349 pages_list = optee_allocate_pages_list(page_num);
350 if (!pages_list) {
351 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
352 goto bad;
353 }
354
355 call_ctx->pages_list = pages_list;
356 call_ctx->num_entries = page_num;
357
358 arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
359 OPTEE_MSG_ATTR_NONCONTIG;
360 /*
361 * In the least bits of u.tmem.buf_ptr we store buffer offset
362 * from 4k page, as described in OP-TEE ABI.
363 */
364 arg->params[0].u.tmem.buf_ptr = virt_to_phys(pages_list) |
365 (tee_shm_get_page_offset(shm) &
366 (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
367 arg->params[0].u.tmem.size = tee_shm_get_size(shm);
368 arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
369
370 optee_fill_pages_list(pages_list, pages, page_num,
371 tee_shm_get_page_offset(shm));
372 } else {
373 arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT;
374 arg->params[0].u.tmem.buf_ptr = pa;
375 arg->params[0].u.tmem.size = sz;
376 arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
377 }
378
379 arg->ret = TEEC_SUCCESS;
380 return;
381 bad:
382 tee_shm_free(shm);
383 }
384
cmd_free_suppl(struct tee_context * ctx,struct tee_shm * shm)385 static void cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm)
386 {
387 struct tee_param param;
388
389 param.attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
390 param.u.value.a = OPTEE_RPC_SHM_TYPE_APPL;
391 param.u.value.b = tee_shm_get_id(shm);
392 param.u.value.c = 0;
393
394 /*
395 * Match the tee_shm_get_from_id() in cmd_alloc_suppl() as secure
396 * world has released its reference.
397 *
398 * It's better to do this before sending the request to supplicant
399 * as we'd like to let the process doing the initial allocation to
400 * do release the last reference too in order to avoid stacking
401 * many pending fput() on the client process. This could otherwise
402 * happen if secure world does many allocate and free in a single
403 * invoke.
404 */
405 tee_shm_put(shm);
406
407 optee_supp_thrd_req(ctx, OPTEE_RPC_CMD_SHM_FREE, 1, ¶m);
408 }
409
handle_rpc_func_cmd_shm_free(struct tee_context * ctx,struct optee_msg_arg * arg)410 static void handle_rpc_func_cmd_shm_free(struct tee_context *ctx,
411 struct optee_msg_arg *arg)
412 {
413 struct tee_shm *shm;
414
415 arg->ret_origin = TEEC_ORIGIN_COMMS;
416
417 if (arg->num_params != 1 ||
418 arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
419 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
420 return;
421 }
422
423 shm = (struct tee_shm *)(unsigned long)arg->params[0].u.value.b;
424 switch (arg->params[0].u.value.a) {
425 case OPTEE_RPC_SHM_TYPE_APPL:
426 cmd_free_suppl(ctx, shm);
427 break;
428 case OPTEE_RPC_SHM_TYPE_KERNEL:
429 tee_shm_free(shm);
430 break;
431 default:
432 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
433 }
434 arg->ret = TEEC_SUCCESS;
435 }
436
free_pages_list(struct optee_call_ctx * call_ctx)437 static void free_pages_list(struct optee_call_ctx *call_ctx)
438 {
439 if (call_ctx->pages_list) {
440 optee_free_pages_list(call_ctx->pages_list,
441 call_ctx->num_entries);
442 call_ctx->pages_list = NULL;
443 call_ctx->num_entries = 0;
444 }
445 }
446
optee_rpc_finalize_call(struct optee_call_ctx * call_ctx)447 void optee_rpc_finalize_call(struct optee_call_ctx *call_ctx)
448 {
449 free_pages_list(call_ctx);
450 }
451
handle_rpc_func_cmd(struct tee_context * ctx,struct optee * optee,struct tee_shm * shm,struct optee_call_ctx * call_ctx)452 static void handle_rpc_func_cmd(struct tee_context *ctx, struct optee *optee,
453 struct tee_shm *shm,
454 struct optee_call_ctx *call_ctx)
455 {
456 struct optee_msg_arg *arg;
457
458 arg = tee_shm_get_va(shm, 0);
459 if (IS_ERR(arg)) {
460 pr_err("%s: tee_shm_get_va %p failed\n", __func__, shm);
461 return;
462 }
463
464 switch (arg->cmd) {
465 case OPTEE_RPC_CMD_GET_TIME:
466 handle_rpc_func_cmd_get_time(arg);
467 break;
468 case OPTEE_RPC_CMD_WAIT_QUEUE:
469 handle_rpc_func_cmd_wq(optee, arg);
470 break;
471 case OPTEE_RPC_CMD_SUSPEND:
472 handle_rpc_func_cmd_wait(arg);
473 break;
474 case OPTEE_RPC_CMD_SHM_ALLOC:
475 free_pages_list(call_ctx);
476 handle_rpc_func_cmd_shm_alloc(ctx, optee, arg, call_ctx);
477 break;
478 case OPTEE_RPC_CMD_SHM_FREE:
479 handle_rpc_func_cmd_shm_free(ctx, arg);
480 break;
481 case OPTEE_RPC_CMD_I2C_TRANSFER:
482 handle_rpc_func_cmd_i2c_transfer(ctx, arg);
483 break;
484 default:
485 handle_rpc_supp_cmd(ctx, arg);
486 }
487 }
488
489 /**
490 * optee_handle_rpc() - handle RPC from secure world
491 * @ctx: context doing the RPC
492 * @param: value of registers for the RPC
493 * @call_ctx: call context. Preserved during one OP-TEE invocation
494 *
495 * Result of RPC is written back into @param.
496 */
optee_handle_rpc(struct tee_context * ctx,struct optee_rpc_param * param,struct optee_call_ctx * call_ctx)497 void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param,
498 struct optee_call_ctx *call_ctx)
499 {
500 struct tee_device *teedev = ctx->teedev;
501 struct optee *optee = tee_get_drvdata(teedev);
502 struct tee_shm *shm;
503 phys_addr_t pa;
504
505 switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) {
506 case OPTEE_SMC_RPC_FUNC_ALLOC:
507 shm = tee_shm_alloc(optee->ctx, param->a1,
508 TEE_SHM_MAPPED | TEE_SHM_PRIV);
509 if (!IS_ERR(shm) && !tee_shm_get_pa(shm, 0, &pa)) {
510 reg_pair_from_64(¶m->a1, ¶m->a2, pa);
511 reg_pair_from_64(¶m->a4, ¶m->a5,
512 (unsigned long)shm);
513 } else {
514 param->a1 = 0;
515 param->a2 = 0;
516 param->a4 = 0;
517 param->a5 = 0;
518 }
519 break;
520 case OPTEE_SMC_RPC_FUNC_FREE:
521 shm = reg_pair_to_ptr(param->a1, param->a2);
522 tee_shm_free(shm);
523 break;
524 case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR:
525 /*
526 * A foreign interrupt was raised while secure world was
527 * executing, since they are handled in Linux a dummy RPC is
528 * performed to let Linux take the interrupt through the normal
529 * vector.
530 */
531 break;
532 case OPTEE_SMC_RPC_FUNC_CMD:
533 shm = reg_pair_to_ptr(param->a1, param->a2);
534 handle_rpc_func_cmd(ctx, optee, shm, call_ctx);
535 break;
536 default:
537 pr_warn("Unknown RPC func 0x%x\n",
538 (u32)OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0));
539 break;
540 }
541
542 param->a0 = OPTEE_SMC_CALL_RETURN_FROM_RPC;
543 }
544