blob: 4d0ce606f0fcb368a473103f01d541b44d1fe772 [file] [log] [blame]
Jens Wiklanderfb938962015-03-11 14:39:39 +01001/*
2 * Copyright (c) 2015-2016, Linaro Limited
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
15#define pr_fmt(fmt) "%s: " fmt, __func__
16
17#include <linux/cdev.h>
18#include <linux/device.h>
19#include <linux/fs.h>
20#include <linux/idr.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/tee_drv.h>
24#include <linux/uaccess.h>
25#include "tee_private.h"
26
27#define TEE_NUM_DEVICES 32
28
29#define TEE_IOCTL_PARAM_SIZE(x) (sizeof(struct tee_param) * (x))
30
31/*
32 * Unprivileged devices in the lower half range and privileged devices in
33 * the upper half range.
34 */
35static DECLARE_BITMAP(dev_mask, TEE_NUM_DEVICES);
36static DEFINE_SPINLOCK(driver_lock);
37
38static struct class *tee_class;
39static dev_t tee_devt;
40
41static int tee_open(struct inode *inode, struct file *filp)
42{
43 int rc;
44 struct tee_device *teedev;
45 struct tee_context *ctx;
46
47 teedev = container_of(inode->i_cdev, struct tee_device, cdev);
48 if (!tee_device_get(teedev))
49 return -EINVAL;
50
51 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
52 if (!ctx) {
53 rc = -ENOMEM;
54 goto err;
55 }
56
57 ctx->teedev = teedev;
58 INIT_LIST_HEAD(&ctx->list_shm);
59 filp->private_data = ctx;
60 rc = teedev->desc->ops->open(ctx);
61 if (rc)
62 goto err;
63
64 return 0;
65err:
66 kfree(ctx);
67 tee_device_put(teedev);
68 return rc;
69}
70
71static int tee_release(struct inode *inode, struct file *filp)
72{
73 struct tee_context *ctx = filp->private_data;
74 struct tee_device *teedev = ctx->teedev;
75 struct tee_shm *shm;
76
77 ctx->teedev->desc->ops->release(ctx);
78 mutex_lock(&ctx->teedev->mutex);
79 list_for_each_entry(shm, &ctx->list_shm, link)
80 shm->ctx = NULL;
81 mutex_unlock(&ctx->teedev->mutex);
82 kfree(ctx);
83 tee_device_put(teedev);
84 return 0;
85}
86
87static int tee_ioctl_version(struct tee_context *ctx,
88 struct tee_ioctl_version_data __user *uvers)
89{
90 struct tee_ioctl_version_data vers;
91
92 ctx->teedev->desc->ops->get_version(ctx->teedev, &vers);
Jens Wiklandere42b1e32017-02-16 09:07:02 +010093
94 if (ctx->teedev->desc->flags & TEE_DESC_PRIVILEGED)
95 vers.gen_caps |= TEE_GEN_CAP_PRIVILEGED;
96
Jens Wiklanderfb938962015-03-11 14:39:39 +010097 if (copy_to_user(uvers, &vers, sizeof(vers)))
98 return -EFAULT;
Jens Wiklandere42b1e32017-02-16 09:07:02 +010099
Jens Wiklanderfb938962015-03-11 14:39:39 +0100100 return 0;
101}
102
103static int tee_ioctl_shm_alloc(struct tee_context *ctx,
104 struct tee_ioctl_shm_alloc_data __user *udata)
105{
106 long ret;
107 struct tee_ioctl_shm_alloc_data data;
108 struct tee_shm *shm;
109
110 if (copy_from_user(&data, udata, sizeof(data)))
111 return -EFAULT;
112
113 /* Currently no input flags are supported */
114 if (data.flags)
115 return -EINVAL;
116
117 data.id = -1;
118
119 shm = tee_shm_alloc(ctx, data.size, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
120 if (IS_ERR(shm))
121 return PTR_ERR(shm);
122
123 data.id = shm->id;
124 data.flags = shm->flags;
125 data.size = shm->size;
126
127 if (copy_to_user(udata, &data, sizeof(data)))
128 ret = -EFAULT;
129 else
130 ret = tee_shm_get_fd(shm);
131
132 /*
133 * When user space closes the file descriptor the shared memory
134 * should be freed or if tee_shm_get_fd() failed then it will
135 * be freed immediately.
136 */
137 tee_shm_put(shm);
138 return ret;
139}
140
141static int params_from_user(struct tee_context *ctx, struct tee_param *params,
142 size_t num_params,
143 struct tee_ioctl_param __user *uparams)
144{
145 size_t n;
146
147 for (n = 0; n < num_params; n++) {
148 struct tee_shm *shm;
149 struct tee_ioctl_param ip;
150
151 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
152 return -EFAULT;
153
154 /* All unused attribute bits has to be zero */
Jens Wiklander67f68792016-12-23 13:13:34 +0100155 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
Jens Wiklanderfb938962015-03-11 14:39:39 +0100156 return -EINVAL;
157
158 params[n].attr = ip.attr;
Jens Wiklander67f68792016-12-23 13:13:34 +0100159 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
Jens Wiklanderfb938962015-03-11 14:39:39 +0100160 case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
161 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
162 break;
163 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
164 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
165 params[n].u.value.a = ip.a;
166 params[n].u.value.b = ip.b;
167 params[n].u.value.c = ip.c;
168 break;
169 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
170 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
171 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
172 /*
173 * If we fail to get a pointer to a shared memory
174 * object (and increase the ref count) from an
175 * identifier we return an error. All pointers that
176 * has been added in params have an increased ref
177 * count. It's the callers responibility to do
178 * tee_shm_put() on all resolved pointers.
179 */
180 shm = tee_shm_get_from_id(ctx, ip.c);
181 if (IS_ERR(shm))
182 return PTR_ERR(shm);
183
184 params[n].u.memref.shm_offs = ip.a;
185 params[n].u.memref.size = ip.b;
186 params[n].u.memref.shm = shm;
187 break;
188 default:
189 /* Unknown attribute */
190 return -EINVAL;
191 }
192 }
193 return 0;
194}
195
196static int params_to_user(struct tee_ioctl_param __user *uparams,
197 size_t num_params, struct tee_param *params)
198{
199 size_t n;
200
201 for (n = 0; n < num_params; n++) {
202 struct tee_ioctl_param __user *up = uparams + n;
203 struct tee_param *p = params + n;
204
205 switch (p->attr) {
206 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
207 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
208 if (put_user(p->u.value.a, &up->a) ||
209 put_user(p->u.value.b, &up->b) ||
210 put_user(p->u.value.c, &up->c))
211 return -EFAULT;
212 break;
213 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
214 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
215 if (put_user((u64)p->u.memref.size, &up->b))
216 return -EFAULT;
217 default:
218 break;
219 }
220 }
221 return 0;
222}
223
Jens Wiklanderfb938962015-03-11 14:39:39 +0100224static int tee_ioctl_open_session(struct tee_context *ctx,
225 struct tee_ioctl_buf_data __user *ubuf)
226{
227 int rc;
228 size_t n;
229 struct tee_ioctl_buf_data buf;
230 struct tee_ioctl_open_session_arg __user *uarg;
231 struct tee_ioctl_open_session_arg arg;
232 struct tee_ioctl_param __user *uparams = NULL;
233 struct tee_param *params = NULL;
234 bool have_session = false;
235
236 if (!ctx->teedev->desc->ops->open_session)
237 return -EINVAL;
238
239 if (copy_from_user(&buf, ubuf, sizeof(buf)))
240 return -EFAULT;
241
242 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
243 buf.buf_len < sizeof(struct tee_ioctl_open_session_arg))
244 return -EINVAL;
245
246 uarg = u64_to_user_ptr(buf.buf_ptr);
247 if (copy_from_user(&arg, uarg, sizeof(arg)))
248 return -EFAULT;
249
250 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
251 return -EINVAL;
252
253 if (arg.num_params) {
254 params = kcalloc(arg.num_params, sizeof(struct tee_param),
255 GFP_KERNEL);
256 if (!params)
257 return -ENOMEM;
258 uparams = uarg->params;
259 rc = params_from_user(ctx, params, arg.num_params, uparams);
260 if (rc)
261 goto out;
262 }
263
264 rc = ctx->teedev->desc->ops->open_session(ctx, &arg, params);
265 if (rc)
266 goto out;
267 have_session = true;
268
269 if (put_user(arg.session, &uarg->session) ||
270 put_user(arg.ret, &uarg->ret) ||
271 put_user(arg.ret_origin, &uarg->ret_origin)) {
272 rc = -EFAULT;
273 goto out;
274 }
275 rc = params_to_user(uparams, arg.num_params, params);
276out:
277 /*
278 * If we've succeeded to open the session but failed to communicate
279 * it back to user space, close the session again to avoid leakage.
280 */
281 if (rc && have_session && ctx->teedev->desc->ops->close_session)
282 ctx->teedev->desc->ops->close_session(ctx, arg.session);
283
284 if (params) {
285 /* Decrease ref count for all valid shared memory pointers */
286 for (n = 0; n < arg.num_params; n++)
Jens Wiklander525fb3c2016-12-23 13:13:27 +0100287 if (tee_param_is_memref(params + n) &&
Jens Wiklanderfb938962015-03-11 14:39:39 +0100288 params[n].u.memref.shm)
289 tee_shm_put(params[n].u.memref.shm);
290 kfree(params);
291 }
292
293 return rc;
294}
295
296static int tee_ioctl_invoke(struct tee_context *ctx,
297 struct tee_ioctl_buf_data __user *ubuf)
298{
299 int rc;
300 size_t n;
301 struct tee_ioctl_buf_data buf;
302 struct tee_ioctl_invoke_arg __user *uarg;
303 struct tee_ioctl_invoke_arg arg;
304 struct tee_ioctl_param __user *uparams = NULL;
305 struct tee_param *params = NULL;
306
307 if (!ctx->teedev->desc->ops->invoke_func)
308 return -EINVAL;
309
310 if (copy_from_user(&buf, ubuf, sizeof(buf)))
311 return -EFAULT;
312
313 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
314 buf.buf_len < sizeof(struct tee_ioctl_invoke_arg))
315 return -EINVAL;
316
317 uarg = u64_to_user_ptr(buf.buf_ptr);
318 if (copy_from_user(&arg, uarg, sizeof(arg)))
319 return -EFAULT;
320
321 if (sizeof(arg) + TEE_IOCTL_PARAM_SIZE(arg.num_params) != buf.buf_len)
322 return -EINVAL;
323
324 if (arg.num_params) {
325 params = kcalloc(arg.num_params, sizeof(struct tee_param),
326 GFP_KERNEL);
327 if (!params)
328 return -ENOMEM;
329 uparams = uarg->params;
330 rc = params_from_user(ctx, params, arg.num_params, uparams);
331 if (rc)
332 goto out;
333 }
334
335 rc = ctx->teedev->desc->ops->invoke_func(ctx, &arg, params);
336 if (rc)
337 goto out;
338
339 if (put_user(arg.ret, &uarg->ret) ||
340 put_user(arg.ret_origin, &uarg->ret_origin)) {
341 rc = -EFAULT;
342 goto out;
343 }
344 rc = params_to_user(uparams, arg.num_params, params);
345out:
346 if (params) {
347 /* Decrease ref count for all valid shared memory pointers */
348 for (n = 0; n < arg.num_params; n++)
Jens Wiklander525fb3c2016-12-23 13:13:27 +0100349 if (tee_param_is_memref(params + n) &&
Jens Wiklanderfb938962015-03-11 14:39:39 +0100350 params[n].u.memref.shm)
351 tee_shm_put(params[n].u.memref.shm);
352 kfree(params);
353 }
354 return rc;
355}
356
357static int tee_ioctl_cancel(struct tee_context *ctx,
358 struct tee_ioctl_cancel_arg __user *uarg)
359{
360 struct tee_ioctl_cancel_arg arg;
361
362 if (!ctx->teedev->desc->ops->cancel_req)
363 return -EINVAL;
364
365 if (copy_from_user(&arg, uarg, sizeof(arg)))
366 return -EFAULT;
367
368 return ctx->teedev->desc->ops->cancel_req(ctx, arg.cancel_id,
369 arg.session);
370}
371
372static int
373tee_ioctl_close_session(struct tee_context *ctx,
374 struct tee_ioctl_close_session_arg __user *uarg)
375{
376 struct tee_ioctl_close_session_arg arg;
377
378 if (!ctx->teedev->desc->ops->close_session)
379 return -EINVAL;
380
381 if (copy_from_user(&arg, uarg, sizeof(arg)))
382 return -EFAULT;
383
384 return ctx->teedev->desc->ops->close_session(ctx, arg.session);
385}
386
387static int params_to_supp(struct tee_context *ctx,
388 struct tee_ioctl_param __user *uparams,
389 size_t num_params, struct tee_param *params)
390{
391 size_t n;
392
393 for (n = 0; n < num_params; n++) {
394 struct tee_ioctl_param ip;
395 struct tee_param *p = params + n;
396
Jens Wiklander67f68792016-12-23 13:13:34 +0100397 ip.attr = p->attr;
398 switch (p->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
Jens Wiklanderfb938962015-03-11 14:39:39 +0100399 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
400 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
401 ip.a = p->u.value.a;
402 ip.b = p->u.value.b;
403 ip.c = p->u.value.c;
404 break;
405 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
406 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
407 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
408 ip.b = p->u.memref.size;
409 if (!p->u.memref.shm) {
410 ip.a = 0;
411 ip.c = (u64)-1; /* invalid shm id */
412 break;
413 }
414 ip.a = p->u.memref.shm_offs;
415 ip.c = p->u.memref.shm->id;
416 break;
417 default:
418 ip.a = 0;
419 ip.b = 0;
420 ip.c = 0;
421 break;
422 }
423
424 if (copy_to_user(uparams + n, &ip, sizeof(ip)))
425 return -EFAULT;
426 }
427
428 return 0;
429}
430
431static int tee_ioctl_supp_recv(struct tee_context *ctx,
432 struct tee_ioctl_buf_data __user *ubuf)
433{
434 int rc;
435 struct tee_ioctl_buf_data buf;
436 struct tee_iocl_supp_recv_arg __user *uarg;
437 struct tee_param *params;
438 u32 num_params;
439 u32 func;
440
441 if (!ctx->teedev->desc->ops->supp_recv)
442 return -EINVAL;
443
444 if (copy_from_user(&buf, ubuf, sizeof(buf)))
445 return -EFAULT;
446
447 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
448 buf.buf_len < sizeof(struct tee_iocl_supp_recv_arg))
449 return -EINVAL;
450
451 uarg = u64_to_user_ptr(buf.buf_ptr);
452 if (get_user(num_params, &uarg->num_params))
453 return -EFAULT;
454
455 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) != buf.buf_len)
456 return -EINVAL;
457
458 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
459 if (!params)
460 return -ENOMEM;
461
Jens Wiklander67f68792016-12-23 13:13:34 +0100462 rc = params_from_user(ctx, params, num_params, uarg->params);
463 if (rc)
464 goto out;
465
Jens Wiklanderfb938962015-03-11 14:39:39 +0100466 rc = ctx->teedev->desc->ops->supp_recv(ctx, &func, &num_params, params);
467 if (rc)
468 goto out;
469
470 if (put_user(func, &uarg->func) ||
471 put_user(num_params, &uarg->num_params)) {
472 rc = -EFAULT;
473 goto out;
474 }
475
476 rc = params_to_supp(ctx, uarg->params, num_params, params);
477out:
478 kfree(params);
479 return rc;
480}
481
482static int params_from_supp(struct tee_param *params, size_t num_params,
483 struct tee_ioctl_param __user *uparams)
484{
485 size_t n;
486
487 for (n = 0; n < num_params; n++) {
488 struct tee_param *p = params + n;
489 struct tee_ioctl_param ip;
490
491 if (copy_from_user(&ip, uparams + n, sizeof(ip)))
492 return -EFAULT;
493
494 /* All unused attribute bits has to be zero */
Jens Wiklander67f68792016-12-23 13:13:34 +0100495 if (ip.attr & ~TEE_IOCTL_PARAM_ATTR_MASK)
Jens Wiklanderfb938962015-03-11 14:39:39 +0100496 return -EINVAL;
497
498 p->attr = ip.attr;
Jens Wiklander67f68792016-12-23 13:13:34 +0100499 switch (ip.attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
Jens Wiklanderfb938962015-03-11 14:39:39 +0100500 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
501 case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
502 /* Only out and in/out values can be updated */
503 p->u.value.a = ip.a;
504 p->u.value.b = ip.b;
505 p->u.value.c = ip.c;
506 break;
507 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
508 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
509 /*
510 * Only the size of the memref can be updated.
511 * Since we don't have access to the original
512 * parameters here, only store the supplied size.
513 * The driver will copy the updated size into the
514 * original parameters.
515 */
516 p->u.memref.shm = NULL;
517 p->u.memref.shm_offs = 0;
518 p->u.memref.size = ip.b;
519 break;
520 default:
521 memset(&p->u, 0, sizeof(p->u));
522 break;
523 }
524 }
525 return 0;
526}
527
528static int tee_ioctl_supp_send(struct tee_context *ctx,
529 struct tee_ioctl_buf_data __user *ubuf)
530{
531 long rc;
532 struct tee_ioctl_buf_data buf;
533 struct tee_iocl_supp_send_arg __user *uarg;
534 struct tee_param *params;
535 u32 num_params;
536 u32 ret;
537
538 /* Not valid for this driver */
539 if (!ctx->teedev->desc->ops->supp_send)
540 return -EINVAL;
541
542 if (copy_from_user(&buf, ubuf, sizeof(buf)))
543 return -EFAULT;
544
545 if (buf.buf_len > TEE_MAX_ARG_SIZE ||
546 buf.buf_len < sizeof(struct tee_iocl_supp_send_arg))
547 return -EINVAL;
548
549 uarg = u64_to_user_ptr(buf.buf_ptr);
550 if (get_user(ret, &uarg->ret) ||
551 get_user(num_params, &uarg->num_params))
552 return -EFAULT;
553
554 if (sizeof(*uarg) + TEE_IOCTL_PARAM_SIZE(num_params) > buf.buf_len)
555 return -EINVAL;
556
557 params = kcalloc(num_params, sizeof(struct tee_param), GFP_KERNEL);
558 if (!params)
559 return -ENOMEM;
560
561 rc = params_from_supp(params, num_params, uarg->params);
562 if (rc)
563 goto out;
564
565 rc = ctx->teedev->desc->ops->supp_send(ctx, ret, num_params, params);
566out:
567 kfree(params);
568 return rc;
569}
570
571static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
572{
573 struct tee_context *ctx = filp->private_data;
574 void __user *uarg = (void __user *)arg;
575
576 switch (cmd) {
577 case TEE_IOC_VERSION:
578 return tee_ioctl_version(ctx, uarg);
579 case TEE_IOC_SHM_ALLOC:
580 return tee_ioctl_shm_alloc(ctx, uarg);
581 case TEE_IOC_OPEN_SESSION:
582 return tee_ioctl_open_session(ctx, uarg);
583 case TEE_IOC_INVOKE:
584 return tee_ioctl_invoke(ctx, uarg);
585 case TEE_IOC_CANCEL:
586 return tee_ioctl_cancel(ctx, uarg);
587 case TEE_IOC_CLOSE_SESSION:
588 return tee_ioctl_close_session(ctx, uarg);
589 case TEE_IOC_SUPPL_RECV:
590 return tee_ioctl_supp_recv(ctx, uarg);
591 case TEE_IOC_SUPPL_SEND:
592 return tee_ioctl_supp_send(ctx, uarg);
593 default:
594 return -EINVAL;
595 }
596}
597
598static const struct file_operations tee_fops = {
599 .owner = THIS_MODULE,
600 .open = tee_open,
601 .release = tee_release,
602 .unlocked_ioctl = tee_ioctl,
603 .compat_ioctl = tee_ioctl,
604};
605
606static void tee_release_device(struct device *dev)
607{
608 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
609
610 spin_lock(&driver_lock);
611 clear_bit(teedev->id, dev_mask);
612 spin_unlock(&driver_lock);
613 mutex_destroy(&teedev->mutex);
614 idr_destroy(&teedev->idr);
615 kfree(teedev);
616}
617
618/**
619 * tee_device_alloc() - Allocate a new struct tee_device instance
620 * @teedesc: Descriptor for this driver
621 * @dev: Parent device for this device
622 * @pool: Shared memory pool, NULL if not used
623 * @driver_data: Private driver data for this device
624 *
625 * Allocates a new struct tee_device instance. The device is
626 * removed by tee_device_unregister().
627 *
628 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
629 */
630struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
631 struct device *dev,
632 struct tee_shm_pool *pool,
633 void *driver_data)
634{
635 struct tee_device *teedev;
636 void *ret;
637 int rc;
638 int offs = 0;
639
640 if (!teedesc || !teedesc->name || !teedesc->ops ||
641 !teedesc->ops->get_version || !teedesc->ops->open ||
642 !teedesc->ops->release || !pool)
643 return ERR_PTR(-EINVAL);
644
645 teedev = kzalloc(sizeof(*teedev), GFP_KERNEL);
646 if (!teedev) {
647 ret = ERR_PTR(-ENOMEM);
648 goto err;
649 }
650
651 if (teedesc->flags & TEE_DESC_PRIVILEGED)
652 offs = TEE_NUM_DEVICES / 2;
653
654 spin_lock(&driver_lock);
655 teedev->id = find_next_zero_bit(dev_mask, TEE_NUM_DEVICES, offs);
656 if (teedev->id < TEE_NUM_DEVICES)
657 set_bit(teedev->id, dev_mask);
658 spin_unlock(&driver_lock);
659
660 if (teedev->id >= TEE_NUM_DEVICES) {
661 ret = ERR_PTR(-ENOMEM);
662 goto err;
663 }
664
665 snprintf(teedev->name, sizeof(teedev->name), "tee%s%d",
666 teedesc->flags & TEE_DESC_PRIVILEGED ? "priv" : "",
667 teedev->id - offs);
668
669 teedev->dev.class = tee_class;
670 teedev->dev.release = tee_release_device;
671 teedev->dev.parent = dev;
672
673 teedev->dev.devt = MKDEV(MAJOR(tee_devt), teedev->id);
674
675 rc = dev_set_name(&teedev->dev, "%s", teedev->name);
676 if (rc) {
677 ret = ERR_PTR(rc);
678 goto err_devt;
679 }
680
681 cdev_init(&teedev->cdev, &tee_fops);
682 teedev->cdev.owner = teedesc->owner;
683 teedev->cdev.kobj.parent = &teedev->dev.kobj;
684
685 dev_set_drvdata(&teedev->dev, driver_data);
686 device_initialize(&teedev->dev);
687
688 /* 1 as tee_device_unregister() does one final tee_device_put() */
689 teedev->num_users = 1;
690 init_completion(&teedev->c_no_users);
691 mutex_init(&teedev->mutex);
692 idr_init(&teedev->idr);
693
694 teedev->desc = teedesc;
695 teedev->pool = pool;
696
697 return teedev;
698err_devt:
699 unregister_chrdev_region(teedev->dev.devt, 1);
700err:
701 pr_err("could not register %s driver\n",
702 teedesc->flags & TEE_DESC_PRIVILEGED ? "privileged" : "client");
703 if (teedev && teedev->id < TEE_NUM_DEVICES) {
704 spin_lock(&driver_lock);
705 clear_bit(teedev->id, dev_mask);
706 spin_unlock(&driver_lock);
707 }
708 kfree(teedev);
709 return ret;
710}
711EXPORT_SYMBOL_GPL(tee_device_alloc);
712
713static ssize_t implementation_id_show(struct device *dev,
714 struct device_attribute *attr, char *buf)
715{
716 struct tee_device *teedev = container_of(dev, struct tee_device, dev);
717 struct tee_ioctl_version_data vers;
718
719 teedev->desc->ops->get_version(teedev, &vers);
720 return scnprintf(buf, PAGE_SIZE, "%d\n", vers.impl_id);
721}
722static DEVICE_ATTR_RO(implementation_id);
723
724static struct attribute *tee_dev_attrs[] = {
725 &dev_attr_implementation_id.attr,
726 NULL
727};
728
729static const struct attribute_group tee_dev_group = {
730 .attrs = tee_dev_attrs,
731};
732
733/**
734 * tee_device_register() - Registers a TEE device
735 * @teedev: Device to register
736 *
737 * tee_device_unregister() need to be called to remove the @teedev if
738 * this function fails.
739 *
740 * @returns < 0 on failure
741 */
742int tee_device_register(struct tee_device *teedev)
743{
744 int rc;
745
746 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
747 dev_err(&teedev->dev, "attempt to register twice\n");
748 return -EINVAL;
749 }
750
751 rc = cdev_add(&teedev->cdev, teedev->dev.devt, 1);
752 if (rc) {
753 dev_err(&teedev->dev,
754 "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
755 teedev->name, MAJOR(teedev->dev.devt),
756 MINOR(teedev->dev.devt), rc);
757 return rc;
758 }
759
760 rc = device_add(&teedev->dev);
761 if (rc) {
762 dev_err(&teedev->dev,
763 "unable to device_add() %s, major %d, minor %d, err=%d\n",
764 teedev->name, MAJOR(teedev->dev.devt),
765 MINOR(teedev->dev.devt), rc);
766 goto err_device_add;
767 }
768
769 rc = sysfs_create_group(&teedev->dev.kobj, &tee_dev_group);
770 if (rc) {
771 dev_err(&teedev->dev,
772 "failed to create sysfs attributes, err=%d\n", rc);
773 goto err_sysfs_create_group;
774 }
775
776 teedev->flags |= TEE_DEVICE_FLAG_REGISTERED;
777 return 0;
778
779err_sysfs_create_group:
780 device_del(&teedev->dev);
781err_device_add:
782 cdev_del(&teedev->cdev);
783 return rc;
784}
785EXPORT_SYMBOL_GPL(tee_device_register);
786
787void tee_device_put(struct tee_device *teedev)
788{
789 mutex_lock(&teedev->mutex);
790 /* Shouldn't put in this state */
791 if (!WARN_ON(!teedev->desc)) {
792 teedev->num_users--;
793 if (!teedev->num_users) {
794 teedev->desc = NULL;
795 complete(&teedev->c_no_users);
796 }
797 }
798 mutex_unlock(&teedev->mutex);
799}
800
801bool tee_device_get(struct tee_device *teedev)
802{
803 mutex_lock(&teedev->mutex);
804 if (!teedev->desc) {
805 mutex_unlock(&teedev->mutex);
806 return false;
807 }
808 teedev->num_users++;
809 mutex_unlock(&teedev->mutex);
810 return true;
811}
812
813/**
814 * tee_device_unregister() - Removes a TEE device
815 * @teedev: Device to unregister
816 *
817 * This function should be called to remove the @teedev even if
818 * tee_device_register() hasn't been called yet. Does nothing if
819 * @teedev is NULL.
820 */
821void tee_device_unregister(struct tee_device *teedev)
822{
823 if (!teedev)
824 return;
825
826 if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
827 sysfs_remove_group(&teedev->dev.kobj, &tee_dev_group);
828 cdev_del(&teedev->cdev);
829 device_del(&teedev->dev);
830 }
831
832 tee_device_put(teedev);
833 wait_for_completion(&teedev->c_no_users);
834
835 /*
836 * No need to take a mutex any longer now since teedev->desc was
837 * set to NULL before teedev->c_no_users was completed.
838 */
839
840 teedev->pool = NULL;
841
842 put_device(&teedev->dev);
843}
844EXPORT_SYMBOL_GPL(tee_device_unregister);
845
846/**
847 * tee_get_drvdata() - Return driver_data pointer
848 * @teedev: Device containing the driver_data pointer
849 * @returns the driver_data pointer supplied to tee_register().
850 */
851void *tee_get_drvdata(struct tee_device *teedev)
852{
853 return dev_get_drvdata(&teedev->dev);
854}
855EXPORT_SYMBOL_GPL(tee_get_drvdata);
856
857static int __init tee_init(void)
858{
859 int rc;
860
861 tee_class = class_create(THIS_MODULE, "tee");
862 if (IS_ERR(tee_class)) {
863 pr_err("couldn't create class\n");
864 return PTR_ERR(tee_class);
865 }
866
867 rc = alloc_chrdev_region(&tee_devt, 0, TEE_NUM_DEVICES, "tee");
868 if (rc) {
869 pr_err("failed to allocate char dev region\n");
870 class_destroy(tee_class);
871 tee_class = NULL;
872 }
873
874 return rc;
875}
876
877static void __exit tee_exit(void)
878{
879 class_destroy(tee_class);
880 tee_class = NULL;
881 unregister_chrdev_region(tee_devt, TEE_NUM_DEVICES);
882}
883
884subsys_initcall(tee_init);
885module_exit(tee_exit);
886
887MODULE_AUTHOR("Linaro");
888MODULE_DESCRIPTION("TEE Driver");
889MODULE_VERSION("1.0");
890MODULE_LICENSE("GPL v2");