blob: be45ee620299168fc6034a9b5602f6cce853f315 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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/slab.h>
11#include <linux/tee_drv.h>
12#include "optee_private.h"
13#include "optee_smc.h"
14
15struct wq_entry {
16 struct list_head link;
17 struct completion c;
18 u32 key;
19};
20
21void optee_wait_queue_init(struct optee_wait_queue *priv)
22{
23 mutex_init(&priv->mu);
24 INIT_LIST_HEAD(&priv->db);
25}
26
27void optee_wait_queue_exit(struct optee_wait_queue *priv)
28{
29 mutex_destroy(&priv->mu);
30}
31
32static void handle_rpc_func_cmd_get_time(struct optee_msg_arg *arg)
33{
34 struct timespec64 ts;
35
36 if (arg->num_params != 1)
37 goto bad;
38 if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
39 OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT)
40 goto bad;
41
42 ktime_get_real_ts64(&ts);
43 arg->params[0].u.value.a = ts.tv_sec;
44 arg->params[0].u.value.b = ts.tv_nsec;
45
46 arg->ret = TEEC_SUCCESS;
47 return;
48bad:
49 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
50}
51
52static struct wq_entry *wq_entry_get(struct optee_wait_queue *wq, u32 key)
53{
54 struct wq_entry *w;
55
56 mutex_lock(&wq->mu);
57
58 list_for_each_entry(w, &wq->db, link)
59 if (w->key == key)
60 goto out;
61
62 w = kmalloc(sizeof(*w), GFP_KERNEL);
63 if (w) {
64 init_completion(&w->c);
65 w->key = key;
66 list_add_tail(&w->link, &wq->db);
67 }
68out:
69 mutex_unlock(&wq->mu);
70 return w;
71}
72
73static void wq_sleep(struct optee_wait_queue *wq, u32 key)
74{
75 struct wq_entry *w = wq_entry_get(wq, key);
76
77 if (w) {
78 wait_for_completion(&w->c);
79 mutex_lock(&wq->mu);
80 list_del(&w->link);
81 mutex_unlock(&wq->mu);
82 kfree(w);
83 }
84}
85
86static void wq_wakeup(struct optee_wait_queue *wq, u32 key)
87{
88 struct wq_entry *w = wq_entry_get(wq, key);
89
90 if (w)
91 complete(&w->c);
92}
93
94static void handle_rpc_func_cmd_wq(struct optee *optee,
95 struct optee_msg_arg *arg)
96{
97 if (arg->num_params != 1)
98 goto bad;
99
100 if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
101 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
102 goto bad;
103
104 switch (arg->params[0].u.value.a) {
105 case OPTEE_MSG_RPC_WAIT_QUEUE_SLEEP:
106 wq_sleep(&optee->wait_queue, arg->params[0].u.value.b);
107 break;
108 case OPTEE_MSG_RPC_WAIT_QUEUE_WAKEUP:
109 wq_wakeup(&optee->wait_queue, arg->params[0].u.value.b);
110 break;
111 default:
112 goto bad;
113 }
114
115 arg->ret = TEEC_SUCCESS;
116 return;
117bad:
118 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
119}
120
121static void handle_rpc_func_cmd_wait(struct optee_msg_arg *arg)
122{
123 u32 msec_to_wait;
124
125 if (arg->num_params != 1)
126 goto bad;
127
128 if ((arg->params[0].attr & OPTEE_MSG_ATTR_TYPE_MASK) !=
129 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
130 goto bad;
131
132 msec_to_wait = arg->params[0].u.value.a;
133
134 /* Go to interruptible sleep */
135 msleep_interruptible(msec_to_wait);
136
137 arg->ret = TEEC_SUCCESS;
138 return;
139bad:
140 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
141}
142
143static void handle_rpc_supp_cmd(struct tee_context *ctx,
144 struct optee_msg_arg *arg)
145{
146 struct tee_param *params;
147
148 arg->ret_origin = TEEC_ORIGIN_COMMS;
149
150 params = kmalloc_array(arg->num_params, sizeof(struct tee_param),
151 GFP_KERNEL);
152 if (!params) {
153 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
154 return;
155 }
156
157 if (optee_from_msg_param(params, arg->num_params, arg->params)) {
158 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
159 goto out;
160 }
161
162 arg->ret = optee_supp_thrd_req(ctx, arg->cmd, arg->num_params, params);
163
164 if (optee_to_msg_param(arg->params, arg->num_params, params))
165 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
166out:
167 kfree(params);
168}
169
170static struct tee_shm *cmd_alloc_suppl(struct tee_context *ctx, size_t sz)
171{
172 u32 ret;
173 struct tee_param param;
174 struct optee *optee = tee_get_drvdata(ctx->teedev);
175 struct tee_shm *shm;
176
177 param.attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
178 param.u.value.a = OPTEE_MSG_RPC_SHM_TYPE_APPL;
179 param.u.value.b = sz;
180 param.u.value.c = 0;
181
182 ret = optee_supp_thrd_req(ctx, OPTEE_MSG_RPC_CMD_SHM_ALLOC, 1, &param);
183 if (ret)
184 return ERR_PTR(-ENOMEM);
185
186 mutex_lock(&optee->supp.mutex);
187 /* Increases count as secure world doesn't have a reference */
188 shm = tee_shm_get_from_id(optee->supp.ctx, param.u.value.c);
189 mutex_unlock(&optee->supp.mutex);
190 return shm;
191}
192
193static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
194 struct optee *optee,
195 struct optee_msg_arg *arg,
196 struct optee_call_ctx *call_ctx)
197{
198 phys_addr_t pa;
199 struct tee_shm *shm;
200 size_t sz;
201 size_t n;
202
203 arg->ret_origin = TEEC_ORIGIN_COMMS;
204
205 if (!arg->num_params ||
206 arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
207 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
208 return;
209 }
210
211 for (n = 1; n < arg->num_params; n++) {
212 if (arg->params[n].attr != OPTEE_MSG_ATTR_TYPE_NONE) {
213 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
214 return;
215 }
216 }
217
218 sz = arg->params[0].u.value.b;
219 switch (arg->params[0].u.value.a) {
220 case OPTEE_MSG_RPC_SHM_TYPE_APPL:
221 shm = cmd_alloc_suppl(ctx, sz);
222 break;
223 case OPTEE_MSG_RPC_SHM_TYPE_KERNEL:
224 shm = tee_shm_alloc(optee->ctx, sz,
225 TEE_SHM_MAPPED | TEE_SHM_PRIV);
226 break;
227 default:
228 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
229 return;
230 }
231
232 if (IS_ERR(shm)) {
233 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
234 return;
235 }
236
237 if (tee_shm_get_pa(shm, 0, &pa)) {
238 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
239 goto bad;
240 }
241
242 sz = tee_shm_get_size(shm);
243
244 if (tee_shm_is_registered(shm)) {
245 struct page **pages;
246 u64 *pages_list;
247 size_t page_num;
248
249 pages = tee_shm_get_pages(shm, &page_num);
250 if (!pages || !page_num) {
251 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
252 goto bad;
253 }
254
255 pages_list = optee_allocate_pages_list(page_num);
256 if (!pages_list) {
257 arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
258 goto bad;
259 }
260
261 call_ctx->pages_list = pages_list;
262 call_ctx->num_entries = page_num;
263
264 arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
265 OPTEE_MSG_ATTR_NONCONTIG;
266 /*
267 * In the least bits of u.tmem.buf_ptr we store buffer offset
268 * from 4k page, as described in OP-TEE ABI.
269 */
270 arg->params[0].u.tmem.buf_ptr = virt_to_phys(pages_list) |
271 (tee_shm_get_page_offset(shm) &
272 (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
273 arg->params[0].u.tmem.size = tee_shm_get_size(shm);
274 arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
275
276 optee_fill_pages_list(pages_list, pages, page_num,
277 tee_shm_get_page_offset(shm));
278 } else {
279 arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT;
280 arg->params[0].u.tmem.buf_ptr = pa;
281 arg->params[0].u.tmem.size = sz;
282 arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
283 }
284
285 arg->ret = TEEC_SUCCESS;
286 return;
287bad:
288 tee_shm_free(shm);
289}
290
291static void cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm)
292{
293 struct tee_param param;
294
295 param.attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
296 param.u.value.a = OPTEE_MSG_RPC_SHM_TYPE_APPL;
297 param.u.value.b = tee_shm_get_id(shm);
298 param.u.value.c = 0;
299
300 /*
301 * Match the tee_shm_get_from_id() in cmd_alloc_suppl() as secure
302 * world has released its reference.
303 *
304 * It's better to do this before sending the request to supplicant
305 * as we'd like to let the process doing the initial allocation to
306 * do release the last reference too in order to avoid stacking
307 * many pending fput() on the client process. This could otherwise
308 * happen if secure world does many allocate and free in a single
309 * invoke.
310 */
311 tee_shm_put(shm);
312
313 optee_supp_thrd_req(ctx, OPTEE_MSG_RPC_CMD_SHM_FREE, 1, &param);
314}
315
316static void handle_rpc_func_cmd_shm_free(struct tee_context *ctx,
317 struct optee_msg_arg *arg)
318{
319 struct tee_shm *shm;
320
321 arg->ret_origin = TEEC_ORIGIN_COMMS;
322
323 if (arg->num_params != 1 ||
324 arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
325 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
326 return;
327 }
328
329 shm = (struct tee_shm *)(unsigned long)arg->params[0].u.value.b;
330 switch (arg->params[0].u.value.a) {
331 case OPTEE_MSG_RPC_SHM_TYPE_APPL:
332 cmd_free_suppl(ctx, shm);
333 break;
334 case OPTEE_MSG_RPC_SHM_TYPE_KERNEL:
335 tee_shm_free(shm);
336 break;
337 default:
338 arg->ret = TEEC_ERROR_BAD_PARAMETERS;
339 }
340 arg->ret = TEEC_SUCCESS;
341}
342
343static void free_pages_list(struct optee_call_ctx *call_ctx)
344{
345 if (call_ctx->pages_list) {
346 optee_free_pages_list(call_ctx->pages_list,
347 call_ctx->num_entries);
348 call_ctx->pages_list = NULL;
349 call_ctx->num_entries = 0;
350 }
351}
352
353void optee_rpc_finalize_call(struct optee_call_ctx *call_ctx)
354{
355 free_pages_list(call_ctx);
356}
357
358static void handle_rpc_func_cmd(struct tee_context *ctx, struct optee *optee,
359 struct tee_shm *shm,
360 struct optee_call_ctx *call_ctx)
361{
362 struct optee_msg_arg *arg;
363
364 arg = tee_shm_get_va(shm, 0);
365 if (IS_ERR(arg)) {
366 pr_err("%s: tee_shm_get_va %p failed\n", __func__, shm);
367 return;
368 }
369
370 switch (arg->cmd) {
371 case OPTEE_MSG_RPC_CMD_GET_TIME:
372 handle_rpc_func_cmd_get_time(arg);
373 break;
374 case OPTEE_MSG_RPC_CMD_WAIT_QUEUE:
375 handle_rpc_func_cmd_wq(optee, arg);
376 break;
377 case OPTEE_MSG_RPC_CMD_SUSPEND:
378 handle_rpc_func_cmd_wait(arg);
379 break;
380 case OPTEE_MSG_RPC_CMD_SHM_ALLOC:
381 free_pages_list(call_ctx);
382 handle_rpc_func_cmd_shm_alloc(ctx, optee, arg, call_ctx);
383 break;
384 case OPTEE_MSG_RPC_CMD_SHM_FREE:
385 handle_rpc_func_cmd_shm_free(ctx, arg);
386 break;
387 default:
388 handle_rpc_supp_cmd(ctx, arg);
389 }
390}
391
392/**
393 * optee_handle_rpc() - handle RPC from secure world
394 * @ctx: context doing the RPC
395 * @param: value of registers for the RPC
396 * @call_ctx: call context. Preserved during one OP-TEE invocation
397 *
398 * Result of RPC is written back into @param.
399 */
400void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param,
401 struct optee_call_ctx *call_ctx)
402{
403 struct tee_device *teedev = ctx->teedev;
404 struct optee *optee = tee_get_drvdata(teedev);
405 struct tee_shm *shm;
406 phys_addr_t pa;
407
408 switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) {
409 case OPTEE_SMC_RPC_FUNC_ALLOC:
410 shm = tee_shm_alloc(optee->ctx, param->a1,
411 TEE_SHM_MAPPED | TEE_SHM_PRIV);
412 if (!IS_ERR(shm) && !tee_shm_get_pa(shm, 0, &pa)) {
413 reg_pair_from_64(&param->a1, &param->a2, pa);
414 reg_pair_from_64(&param->a4, &param->a5,
415 (unsigned long)shm);
416 } else {
417 param->a1 = 0;
418 param->a2 = 0;
419 param->a4 = 0;
420 param->a5 = 0;
421 }
422 break;
423 case OPTEE_SMC_RPC_FUNC_FREE:
424 shm = reg_pair_to_ptr(param->a1, param->a2);
425 tee_shm_free(shm);
426 break;
427 case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR:
428 /*
429 * A foreign interrupt was raised while secure world was
430 * executing, since they are handled in Linux a dummy RPC is
431 * performed to let Linux take the interrupt through the normal
432 * vector.
433 */
434 break;
435 case OPTEE_SMC_RPC_FUNC_CMD:
436 shm = reg_pair_to_ptr(param->a1, param->a2);
437 handle_rpc_func_cmd(ctx, optee, shm, call_ctx);
438 break;
439 default:
440 pr_warn("Unknown RPC func 0x%x\n",
441 (u32)OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0));
442 break;
443 }
444
445 param->a0 = OPTEE_SMC_CALL_RETURN_FROM_RPC;
446}