import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / misc / mediatek / trustzone / tz_mod.c
1 #include <linux/module.h>
2 #include <linux/init.h>
3 #include <linux/types.h>
4 #include <linux/kernel.h>
5 #include <linux/proc_fs.h>
6 #include <linux/cdev.h>
7 #include <linux/mm.h>
8 #include <linux/slab.h>
9 #include <linux/io.h>
10 #include <linux/uaccess.h>
11 #include <linux/ioctl.h>
12 #include <linux/xlog.h>
13 #include <linux/pagemap.h>
14 #include <linux/kthread.h>
15 #include <linux/freezer.h>
16 #include <linux/platform_device.h>
17
18 #include "trustzone/kree/tz_mod.h"
19 #include "trustzone/kree/mem.h"
20 #include "trustzone/kree/system.h"
21 #include "trustzone/kree/tz_pm.h"
22 #include "trustzone/kree/tz_irq.h"
23 #include "kree_int.h"
24 #include "tz_counter.h"
25 #include "trustzone/tz_cross/ta_pm.h"
26 #include "trustzone/tz_cross/ta_mem.h"
27 #include "tz_ndbg.h"
28
29 #include "tz_secure_clock.h"
30 #include <linux/earlysuspend.h>
31
32 #define MTEE_MOD_TAG "MTEE_MOD"
33
34 #define TZ_PAGESIZE 0x1000 // fix me!!!! need global define
35
36 #define PAGE_SHIFT 12 // fix me!!!! need global define
37
38 #define TZ_DEVNAME "mtk_tz"
39
40
41 /**************************************************************************
42 * TZ MODULE PARAMETER
43 **************************************************************************/
44 static uint memsz = 0;
45 module_param(memsz, uint, S_IRUSR|S_IRGRP|S_IROTH); /* r--r--r-- */
46 MODULE_PARM_DESC(memsz, "the memory size occupied by tee");
47
48 /**Used for mapping user space address to physical memory
49 */
50 typedef struct
51 {
52 uint32_t start;
53 uint32_t size;
54 uint32_t pageArray;
55 uint32_t nrPages;
56 uint32_t isPage;
57 } MTIOMMU_PIN_RANGE_T;
58
59 /*****************************************************************************
60 * FUNCTION DEFINITION
61 *****************************************************************************/
62 static struct cdev tz_client_cdev;
63 static dev_t tz_client_dev;
64 static int tz_client_open(struct inode *inode, struct file *filp);
65 static int tz_client_release(struct inode *inode, struct file *filp);
66 static long tz_client_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
67
68 static int tz_client_init_client_info(struct file *file);
69 static void tz_client_free_client_info(struct file *file);
70
71 #define TZ_CLIENT_INIT_HANDLE_SPACE 32
72 #define TZ_CLIENT_INIT_SHAREDMEM_SPACE 32
73
74
75 struct tz_sharedmem_info {
76 KREE_SHAREDMEM_HANDLE mem_handle;
77 KREE_SESSION_HANDLE session_handle;
78 uint32_t *resouce;
79 };
80
81 struct tz_client_info
82 {
83 int handle_num;
84 struct mutex mux;
85 KREE_SESSION_HANDLE *handles;
86 struct tz_sharedmem_info *shm_info;
87 int shm_info_num;
88 };
89
90
91 static const struct file_operations tz_client_fops = {
92 .open = tz_client_open,
93 .release = tz_client_release,
94 .unlocked_ioctl = tz_client_ioctl,
95 .owner = THIS_MODULE,
96 };
97
98 static int tz_client_open(struct inode *inode, struct file *filp)
99 {
100 return tz_client_init_client_info(filp);
101 }
102
103 static int tz_client_release(struct inode *inode, struct file *filp)
104 {
105 tz_client_free_client_info(filp);
106
107 return 0;
108 }
109
110 /* map user space pages */
111 /* control -> 0 = write, 1 = read only memory */
112 static long _map_user_pages (MTIOMMU_PIN_RANGE_T* pinRange, uint32_t uaddr, uint32_t size, uint32_t control)
113 {
114 int nr_pages;
115 unsigned int first,last;
116 struct page **pages;
117 struct vm_area_struct *vma;
118 int res, j;
119 uint32_t write;
120
121 if ((uaddr == 0) || (size == 0))
122 {
123 return -EFAULT;
124 }
125
126 pinRange->start = uaddr;
127 pinRange->size = size;
128
129 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
130 last = ((uaddr + size + PAGE_SIZE - 1) & PAGE_MASK) >> PAGE_SHIFT;
131 nr_pages = last-first;
132 if((pages = kzalloc(nr_pages * sizeof(struct page *), GFP_KERNEL)) == NULL)
133 {
134 return -ENOMEM;
135 }
136
137 pinRange->pageArray = (uint32_t)pages;
138 write = (control == 0) ? 1 : 0;
139
140 /* Try to fault in all of the necessary pages */
141 down_read(&current->mm->mmap_sem);
142 vma = find_vma_intersection(current->mm, uaddr, uaddr+size);
143 if (!vma)
144 {
145 res = -EFAULT;
146 goto out;
147 }
148 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
149 {
150 pinRange->isPage = 1;
151 res = get_user_pages(
152 current,
153 current->mm,
154 uaddr,
155 nr_pages,
156 write,
157 0, /* don't force */
158 pages,
159 NULL);
160 }
161 else
162 {
163 /* pfn mapped memory, don't touch page struct.
164 the buffer manager (possibly ion) should make sure it won't be used for anything else */
165 pinRange->isPage = 0;
166 res = 0;
167 do {
168 unsigned long *pfns = (void *)pages;
169 while( res < nr_pages && uaddr + PAGE_SIZE <= vma->vm_end) {
170 j = follow_pfn(vma, uaddr, &pfns[res]);
171 if(j) { /* error */
172 res = j;
173 goto out;
174 }
175 uaddr += PAGE_SIZE;
176 res++;
177 }
178 if(res >= nr_pages || uaddr < vma->vm_end)
179 break;
180 vma = find_vma_intersection(current->mm, uaddr, uaddr+1);
181 } while (vma && vma->vm_flags & (VM_IO | VM_PFNMAP));
182 }
183 out:
184 up_read(&current->mm->mmap_sem);
185 if (res < 0)
186 {
187 printk ("_map_user_pages error = 0x%x\n", res);
188 goto out_free;
189 }
190
191 pinRange->nrPages = res;
192 /* Errors and no page mapped should return here */
193 if (res < nr_pages)
194 goto out_unmap;
195
196 //printk("_map_user_pages success: (start: 0x%x, size: 0x%x, #pages: 0x%x)\n", pinRange->start, pinRange->size, pinRange->nrPages);
197
198 return 0;
199
200 out_unmap:
201 printk("_map_user_pages fail\n");
202 if (res > 0)
203 {
204 if(pinRange->isPage)
205 {
206 for (j=0; j < res; j++)
207 {
208 put_page(pages[j]);
209 }
210 }
211 }
212 res = -EFAULT;
213 out_free:
214 kfree(pages);
215 return res;
216 }
217
218 static void _unmap_user_pages (MTIOMMU_PIN_RANGE_T* pinRange)
219 {
220 int res;
221 int j;
222 struct page **pages;
223
224 pages = (struct page **) pinRange->pageArray;
225
226 if(pinRange->isPage)
227 {
228 res = pinRange->nrPages;
229
230 if (res > 0)
231 {
232 for (j=0; j < res; j++)
233 {
234 put_page(pages[j]);
235 }
236 res = 0;
237 }
238 }
239
240 kfree(pages);
241 }
242
243 static struct tz_sharedmem_info *tz_get_sharedmem (struct tz_client_info *info, KREE_SHAREDMEM_HANDLE handle)
244 {
245 struct tz_sharedmem_info *shm_info;
246 int i;
247
248 // search handle
249 shm_info = NULL;
250 for (i = 0; i < info->shm_info_num; i ++)
251 {
252 if (info->shm_info[i].mem_handle == handle)
253 {
254 shm_info = &info->shm_info[i];
255 break;
256 }
257 }
258
259 return shm_info;
260 }
261
262
263
264 /**************************************************************************
265 * DEV tz_client_info handling
266 **************************************************************************/
267 static int
268 tz_client_register_session(struct file *file, KREE_SESSION_HANDLE handle)
269 {
270 struct tz_client_info *info;
271 int i, num, nspace, ret = -1;
272 void *ptr;
273
274 info = (struct tz_client_info*)file->private_data;
275
276 // lock
277 mutex_lock(&info->mux);
278
279 // find empty space.
280 num = info->handle_num;
281 for (i=0; i<num; i++)
282 {
283 if ((int)info->handles[i] == 0)
284 {
285 ret = i;
286 break;
287 }
288 }
289
290 if (ret == -1)
291 {
292 // Try grow the space
293 nspace = num * 2;
294 ptr = krealloc(info->handles, nspace * sizeof(KREE_SESSION_HANDLE),
295 GFP_KERNEL);
296 if (ptr == 0)
297 {
298 mutex_unlock(&info->mux);
299 return -ENOMEM;
300 }
301
302 ret = num;
303 info->handle_num = nspace;
304 info->handles = (KREE_SESSION_HANDLE*)ptr;
305 memset(&info->handles[num], 0,
306 (nspace - num) * sizeof(KREE_SESSION_HANDLE));
307 }
308
309 if (ret >= 0)
310 info->handles[ret] = handle;
311
312 // unlock
313 mutex_unlock(&info->mux);
314 return ret+1;
315 }
316
317 static KREE_SESSION_HANDLE tz_client_get_session(struct file *file, int handle)
318 {
319 struct tz_client_info *info;
320 KREE_SESSION_HANDLE rhandle;
321
322 info = (struct tz_client_info*)file->private_data;
323
324 // lock
325 mutex_lock(&info->mux);
326
327 if (handle <= 0 || handle > info->handle_num)
328 rhandle = (KREE_SESSION_HANDLE)0;
329 else
330 rhandle = info->handles[handle-1];
331
332 // unlock
333 mutex_unlock(&info->mux);
334 return rhandle;
335 }
336
337 static int tz_client_unregister_session(struct file *file, int handle)
338 {
339 struct tz_client_info *info;
340 int ret = 0;
341
342 info = (struct tz_client_info*)file->private_data;
343
344 // lock
345 mutex_lock(&info->mux);
346
347 if (handle <= 0 || handle > info->handle_num || info->handles[handle-1] == 0)
348 ret = -EINVAL;
349 else
350 info->handles[handle-1] = (KREE_SESSION_HANDLE)0;
351
352 // unlock
353 mutex_unlock(&info->mux);
354 return ret;
355 }
356
357 static int
358 tz_client_register_sharedmem(struct file *file, KREE_SESSION_HANDLE handle, KREE_SHAREDMEM_HANDLE mem_handle, uint32_t *resource)
359 {
360 struct tz_client_info *info;
361 int i, num, nspace, ret = -1;
362 void *ptr;
363
364 info = (struct tz_client_info*)file->private_data;
365
366 // lock
367 mutex_lock(&info->mux);
368
369 // find empty space.
370 num = info->shm_info_num;
371 for (i=0; i<num; i++)
372 {
373 if ((int)info->shm_info[i].mem_handle == 0)
374 {
375 ret = i;
376 break;
377 }
378 }
379
380 if (ret == -1)
381 {
382 // Try grow the space
383 nspace = num * 2;
384 ptr = krealloc(info->shm_info, nspace * sizeof(struct tz_sharedmem_info),
385 GFP_KERNEL);
386 if (ptr == 0)
387 {
388 mutex_unlock(&info->mux);
389 return -ENOMEM;
390 }
391
392 ret = num;
393 info->shm_info_num = nspace;
394 info->shm_info = (struct tz_sharedmem_info *) ptr;
395 memset(&info->shm_info[num], 0,
396 (nspace - num) * sizeof(struct tz_sharedmem_info));
397 }
398
399 if (ret >= 0)
400 {
401 info->shm_info[ret].mem_handle = mem_handle;
402 info->shm_info[ret].session_handle = handle;
403 info->shm_info[ret].resouce = resource;
404 }
405
406 // unlock
407 mutex_unlock(&info->mux);
408
409 return ret;
410 }
411
412 static int tz_client_unregister_sharedmem(struct file *file, KREE_SHAREDMEM_HANDLE handle)
413 {
414 struct tz_client_info *info;
415 struct tz_sharedmem_info *shm_info;
416 MTIOMMU_PIN_RANGE_T *pin;
417 int ret = 0;
418
419 info = (struct tz_client_info*)file->private_data;
420
421 // lock
422 mutex_lock(&info->mux);
423
424 shm_info = tz_get_sharedmem(info, handle);
425
426 if ((shm_info == NULL) || (shm_info->mem_handle == 0))
427 ret = -EINVAL;
428 else
429 {
430 pin = (MTIOMMU_PIN_RANGE_T *) shm_info->resouce;
431 _unmap_user_pages(pin);
432 kfree(pin);
433 memset (shm_info, 0, sizeof (struct tz_sharedmem_info));
434 }
435
436 // unlock
437 mutex_unlock(&info->mux);
438
439 return ret;
440 }
441
442
443 static int tz_client_init_client_info(struct file *file)
444 {
445 struct tz_client_info *info;
446
447 info = (struct tz_client_info*)
448 kmalloc(sizeof(struct tz_client_info), GFP_KERNEL);
449 if (!info)
450 return -ENOMEM;
451
452 info->handles = (KREE_SESSION_HANDLE*)kzalloc(
453 TZ_CLIENT_INIT_HANDLE_SPACE * sizeof(KREE_SESSION_HANDLE),
454 GFP_KERNEL);
455 if (!info->handles)
456 {
457 kfree(info);
458 return -ENOMEM;
459 }
460 info->handle_num = TZ_CLIENT_INIT_HANDLE_SPACE;
461
462 // init shared memory
463 info->shm_info = (struct tz_sharedmem_info *)kzalloc(
464 TZ_CLIENT_INIT_SHAREDMEM_SPACE * sizeof (struct tz_sharedmem_info),
465 GFP_KERNEL);
466 if (!info->shm_info)
467 {
468 kfree(info->handles);
469 kfree(info);
470 return -ENOMEM;
471 }
472 info->shm_info_num = TZ_CLIENT_INIT_SHAREDMEM_SPACE;
473
474 mutex_init(&info->mux);
475 file->private_data = info;
476 return 0;
477 }
478
479 static void tz_client_free_client_info(struct file *file)
480 {
481 struct tz_client_info *info;
482 struct tz_sharedmem_info *shm_info;
483 MTIOMMU_PIN_RANGE_T *pin;
484 int i, num;
485
486 info = (struct tz_client_info*)file->private_data;
487
488 // lock
489 mutex_lock(&info->mux);
490
491 num = info->handle_num;
492 for (i=0; i<num; i++)
493 {
494 if (info->handles[i] == 0)
495 continue;
496
497 KREE_CloseSession(info->handles[i]);
498 info->handles[i] = (KREE_SESSION_HANDLE)0;
499 }
500
501 // unregister shared memory
502 num = info->shm_info_num;
503 for (i=0; i<num; i++)
504 {
505 if (info->shm_info[i].mem_handle == 0)
506 continue;
507
508 shm_info = tz_get_sharedmem (info, info->shm_info[i].mem_handle);
509 if (shm_info == NULL)
510 {
511 continue;
512 }
513
514 pin = (MTIOMMU_PIN_RANGE_T *) shm_info->resouce;
515
516 _unmap_user_pages (pin);
517 kfree (pin);
518 }
519
520 // unlock
521 file->private_data = 0;
522 mutex_unlock(&info->mux);
523 kfree(info->handles);
524 kfree(info->shm_info);
525 kfree(info);
526 }
527
528 /**************************************************************************
529 * DEV DRIVER IOCTL
530 **************************************************************************/
531 static long tz_client_open_session(struct file *file, unsigned long arg)
532 {
533 struct kree_session_cmd_param param;
534 unsigned long cret;
535 char uuid[40];
536 long len;
537 TZ_RESULT ret;
538 KREE_SESSION_HANDLE handle;
539
540 cret = copy_from_user(&param, (void*)arg, sizeof(param));
541 if (cret)
542 return -EFAULT;
543
544 // Check if can we access UUID string. 10 for min uuid len.
545 if (!access_ok(VERIFY_READ, param.data, 10))
546 return -EFAULT;
547
548 len = strncpy_from_user(uuid, param.data, sizeof(uuid));
549 if (len <= 0)
550 return -EFAULT;
551
552 uuid[sizeof(uuid)-1]=0;
553 ret = KREE_CreateSession(uuid, &handle);
554 param.ret = ret;
555
556 // Register session to fd
557 if (ret == TZ_RESULT_SUCCESS)
558 {
559 param.handle = tz_client_register_session(file, handle);
560 if (param.handle < 0)
561 goto error_register;
562 }
563
564 cret = copy_to_user((void*)arg, &param, sizeof(param));
565 if (cret)
566 goto error_copy;
567
568 return 0;
569
570 error_copy:
571 tz_client_unregister_session(file, param.handle);
572 error_register:
573 KREE_CloseSession(handle);
574 return -EFAULT;
575 }
576
577 static long tz_client_close_session(struct file *file, unsigned long arg)
578 {
579 struct kree_session_cmd_param param;
580 unsigned long cret;
581 TZ_RESULT ret;
582 KREE_SESSION_HANDLE handle;
583
584 cret = copy_from_user(&param, (void*)arg, sizeof(param));
585 if (cret)
586 return -EFAULT;
587
588 handle = tz_client_get_session(file, param.handle);
589 if (handle == 0)
590 return -EINVAL;
591
592 tz_client_unregister_session(file, param.handle);
593 ret = KREE_CloseSession(handle);
594 param.ret = ret;
595
596 cret = copy_to_user((void*)arg, &param, sizeof(param));
597 if (cret)
598 return -EFAULT;
599
600 return 0;
601 }
602
603 static long tz_client_tee_service(struct file *file, unsigned long arg)
604 {
605 struct kree_tee_service_cmd_param cparam;
606 unsigned long cret;
607 uint32_t tmpTypes;
608 MTEEC_PARAM param[4], oparam[4];
609 int i;
610 TZ_RESULT ret;
611 KREE_SESSION_HANDLE handle;
612
613 cret = copy_from_user(&cparam, (void*)arg, sizeof(cparam));
614 if (cret)
615 return -EFAULT;
616
617 if (cparam.paramTypes != TZPT_NONE || cparam.param)
618 {
619 // Check if can we access param
620 if (!access_ok(VERIFY_READ, cparam.param, sizeof(oparam)))
621 return -EFAULT;
622
623 cret = copy_from_user(oparam, (void*)cparam.param, sizeof(oparam));
624 if (cret)
625 return -EFAULT;
626 }
627
628 // Check handle
629 handle = tz_client_get_session(file, cparam.handle);
630 if (handle <= 0)
631 return -EINVAL;
632
633 // Parameter processing.
634 memset(param, 0, sizeof(param));
635 tmpTypes = cparam.paramTypes;
636 for (i=0; tmpTypes; i++)
637 {
638 TZ_PARAM_TYPES type = tmpTypes & 0xff;
639 tmpTypes >>= 8;
640 switch (type)
641 {
642 case TZPT_VALUE_INPUT:
643 case TZPT_VALUE_INOUT:
644 param[i] = oparam[i];
645 break;
646
647 case TZPT_VALUE_OUTPUT:
648 case TZPT_NONE:
649 // Nothing to do
650 break;
651
652 case TZPT_MEM_INPUT:
653 case TZPT_MEM_OUTPUT:
654 case TZPT_MEM_INOUT:
655 // Mem Access check
656 if (type != TZPT_MEM_OUTPUT)
657 {
658 if (!access_ok(VERIFY_READ, oparam[i].mem.buffer,
659 oparam[i].mem.size))
660 {
661 cret = -EFAULT;
662 goto error;
663 }
664 }
665 if (type != TZPT_MEM_INPUT)
666 {
667 if (!access_ok(VERIFY_WRITE, oparam[i].mem.buffer,
668 oparam[i].mem.size))
669 {
670 cret = -EFAULT;
671 goto error;
672 }
673 }
674
675 // Allocate kernel space memory. Fail if > 4kb
676 if (oparam[i].mem.size > TEE_PARAM_MEM_LIMIT)
677 {
678 cret = -ENOMEM;
679 goto error;
680 }
681
682 param[i].mem.size = oparam[i].mem.size;
683 param[i].mem.buffer = kmalloc(param[i].mem.size, GFP_KERNEL);
684 if (!param[i].mem.buffer)
685 {
686 cret = -ENOMEM;
687 goto error;
688 }
689
690 if (type != TZPT_MEM_OUTPUT)
691 {
692 cret = copy_from_user(param[i].mem.buffer,
693 (void*)oparam[i].mem.buffer,
694 param[i].mem.size);
695 if (cret)
696 {
697 cret = -EFAULT;
698 goto error;
699 }
700 }
701 break;
702
703 case TZPT_MEMREF_INPUT:
704 case TZPT_MEMREF_OUTPUT:
705 case TZPT_MEMREF_INOUT:
706 // Check if share memory is valid.
707 // Not done yet.
708 param[i] = oparam[i];
709 break;
710
711 default:
712 // Bad format, return.
713 ret = TZ_RESULT_ERROR_BAD_FORMAT;
714 goto error;
715 }
716 }
717
718 // Execute.
719 ret = KREE_TeeServiceCallNoCheck(handle, cparam.command, cparam.paramTypes, param);
720
721 // Copy memory back.
722 cparam.ret = ret;
723 tmpTypes = cparam.paramTypes;
724 for (i=0; tmpTypes; i++)
725 {
726 TZ_PARAM_TYPES type = tmpTypes & 0xff;
727 tmpTypes >>= 8;
728 switch (type)
729 {
730 case TZPT_VALUE_OUTPUT:
731 case TZPT_VALUE_INOUT:
732 oparam[i] = param[i];
733 break;
734
735 default:
736 // This should never happen
737 case TZPT_MEMREF_INPUT:
738 case TZPT_MEMREF_OUTPUT:
739 case TZPT_MEMREF_INOUT:
740 case TZPT_VALUE_INPUT:
741 case TZPT_NONE:
742 // Nothing to do
743 break;
744
745 case TZPT_MEM_INPUT:
746 case TZPT_MEM_OUTPUT:
747 case TZPT_MEM_INOUT:
748 if (type != TZPT_MEM_INPUT)
749 {
750 cret = copy_to_user((void*)oparam[i].mem.buffer,
751 param[i].mem.buffer,
752 param[i].mem.size);
753 if (cret)
754 {
755 cret = -EFAULT;
756 goto error;
757 }
758 }
759
760 if (param[i].mem.buffer)
761 {
762 kfree(param[i].mem.buffer);
763 param[i].mem.buffer = 0;
764 }
765 break;
766 }
767 }
768
769 // Copy data back.
770 if (cparam.paramTypes != TZPT_NONE)
771 {
772 cret = copy_to_user((void*)cparam.param, oparam, sizeof(oparam));
773 if (cret)
774 return -EFAULT;
775 }
776
777 cret = copy_to_user((void*)arg, &cparam, sizeof(cparam));
778 if (cret)
779 return -EFAULT;
780 return 0;
781
782 error:
783 tmpTypes = cparam.paramTypes;
784 for (i=0; tmpTypes; i++)
785 {
786 TZ_PARAM_TYPES type = tmpTypes & 0xff;
787 tmpTypes >>= 8;
788 switch (type)
789 {
790 case TZPT_MEM_INPUT:
791 case TZPT_MEM_OUTPUT:
792 case TZPT_MEM_INOUT:
793 if (param[i].mem.buffer)
794 kfree(param[i].mem.buffer);
795 break;
796
797 default:
798 // Don't care.
799 break;
800 }
801 }
802
803 return cret;
804 }
805
806 static long tz_client_reg_sharedmem (struct file *file, unsigned long arg)
807 {
808 unsigned long cret;
809 struct kree_sharedmemory_cmd_param cparam;
810 KREE_SESSION_HANDLE session;
811 uint32_t mem_handle;
812 MTIOMMU_PIN_RANGE_T *pin;
813 uint32_t *map_p;
814 TZ_RESULT ret;
815 struct page **page;
816 int i;
817 long errcode;
818 unsigned long *pfns;
819
820 cret = copy_from_user(&cparam, (void*)arg, sizeof(cparam));
821 if (cret)
822 {
823 return -EFAULT;
824 }
825
826 // session handle
827 session = tz_client_get_session(file, cparam.session);
828 if (session <= 0)
829 {
830 return -EINVAL;
831 }
832
833 /* map pages
834 */
835 // 1. get user pages
836 // note: 'pin' resource need to keep for unregister. It will be freed after unregisted.
837 if((pin = kzalloc (sizeof (MTIOMMU_PIN_RANGE_T), GFP_KERNEL)) == NULL)
838 {
839 errcode = -ENOMEM;
840 goto client_regshm_mapfail;
841 }
842 cret = _map_user_pages (pin, (uint32_t) cparam.buffer, cparam.size, cparam.control);
843 if (cret != 0)
844 {
845 printk ("tz_client_reg_sharedmem fail: map user pages = 0x%x\n", (uint32_t) cret);
846 errcode = -EFAULT;
847 goto client_regshm_mapfail_1;
848 }
849
850 // 2. build PA table
851 if((map_p = kzalloc(sizeof (uint32_t) * (pin->nrPages + 1), GFP_KERNEL)) == NULL)
852 {
853 errcode = -ENOMEM;
854 goto client_regshm_mapfail_2;
855 }
856 map_p[0] = pin->nrPages;
857 if(pin->isPage)
858 {
859 page = (struct page **) pin->pageArray;
860 for (i = 0; i < pin->nrPages; i ++)
861 {
862 map_p[1 + i] = PFN_PHYS(page_to_pfn(page[i])); // get PA
863 //printk ("tz_client_reg_sharedmem ---> 0x%x\n", map_p[1+i]);
864 }
865 }
866 else /* pfn */
867 {
868 pfns = (unsigned long*)pin->pageArray;
869 for (i = 0; i < pin->nrPages; i++) {
870 map_p[1 + i] = PFN_PHYS(pfns[i]); /* get PA */
871 /* pr_info("tz_client_reg_sharedmem ---> 0x%x\n", map_p[1+i]); */
872 }
873 }
874
875 /* register it ...
876 */
877 ret = kree_register_sharedmem (session, &mem_handle, (uint32_t) pin->start, pin->size, (uint32_t) map_p);
878 if (ret != TZ_RESULT_SUCCESS)
879 {
880 printk ("tz_client_reg_sharedmem fail: register shm 0x%x\n", ret);
881 kfree (map_p);
882 errcode = -EFAULT;
883 goto client_regshm_free;
884 }
885 kfree (map_p);
886
887 /* register to fd
888 */
889 cret = tz_client_register_sharedmem (file, session, (KREE_SHAREDMEM_HANDLE) mem_handle, (uint32_t *)pin);
890 if (cret < 0)
891 {
892 printk ("tz_client_reg_sharedmem fail: register fd 0x%x\n", (uint32_t) cret);
893 errcode = -EFAULT;
894 goto client_regshm_free_1;
895 }
896
897 cparam.mem_handle = mem_handle;
898 cparam.ret = ret; // TEE service call return
899 cret = copy_to_user((void*)arg, &cparam, sizeof(cparam));
900 if (cret)
901 {
902 return -EFAULT;
903 }
904
905 return TZ_RESULT_SUCCESS;
906
907 client_regshm_free_1:
908 kree_unregister_sharedmem (session, mem_handle);
909 client_regshm_free:
910 _unmap_user_pages (pin);
911 kfree (pin);
912 cparam.ret = ret; // TEE service call return
913 cret = copy_to_user((void*)arg, &cparam, sizeof(cparam));
914 printk ("tz_client_reg_sharedmem fail: shm reg\n");
915 return errcode;
916
917 client_regshm_mapfail_2:
918 _unmap_user_pages (pin);
919 client_regshm_mapfail_1:
920 kfree (pin);
921 client_regshm_mapfail:
922 printk ("tz_client_reg_sharedmem fail: map memory\n");
923 return errcode;
924 }
925
926 static long tz_client_unreg_sharedmem (struct file *file, unsigned long arg)
927 {
928 unsigned long cret;
929 struct kree_sharedmemory_cmd_param cparam;
930 KREE_SESSION_HANDLE session;
931 TZ_RESULT ret;
932
933 cret = copy_from_user(&cparam, (void*)arg, sizeof(cparam));
934 if (cret)
935 {
936 return -EFAULT;
937 }
938
939 // session handle
940 session = tz_client_get_session(file, cparam.session);
941 if (session <= 0)
942 {
943 return -EINVAL;
944 }
945
946 /* Unregister
947 */
948 ret = kree_unregister_sharedmem (session, (uint32_t) cparam.mem_handle);
949 if (ret != TZ_RESULT_SUCCESS)
950 {
951 printk ("tz_client_unreg_sharedmem: 0x%x\n", ret);
952 cparam.ret = ret;
953 cret = copy_to_user((void*)arg, &cparam, sizeof(cparam));
954 return -EFAULT;
955 }
956
957 /* unmap user pages and unregister to fd
958 */
959 ret = tz_client_unregister_sharedmem (file, cparam.mem_handle);
960 if (ret != TZ_RESULT_SUCCESS)
961 {
962 printk ("tz_client_unreg_sharedmem: unregister shm = 0x%x\n", ret);
963 return -EFAULT;
964 }
965
966 cparam.ret = ret;
967 cret = copy_to_user((void*)arg, &cparam, sizeof(cparam));
968 if (cret)
969 {
970 return -EFAULT;
971 }
972
973 return TZ_RESULT_SUCCESS;
974 }
975
976
977
978 static long tz_client_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
979 {
980 int err = 0;
981
982 /* ---------------------------------- */
983 /* IOCTL */
984 /* ---------------------------------- */
985 if (_IOC_TYPE(cmd) != MTEE_IOC_MAGIC)
986 return -ENOTTY;
987 if (_IOC_NR(cmd) > DEV_IOC_MAXNR)
988 return -ENOTTY;
989
990 if (_IOC_DIR(cmd) & _IOC_READ)
991 err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
992 if (_IOC_DIR(cmd) & _IOC_WRITE)
993 err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
994
995 if (err) return -EFAULT;
996
997 switch (cmd)
998 {
999 case MTEE_CMD_OPEN_SESSION:
1000 return tz_client_open_session(file, arg);
1001
1002 case MTEE_CMD_CLOSE_SESSION:
1003 return tz_client_close_session(file, arg);
1004
1005 case MTEE_CMD_TEE_SERVICE:
1006 return tz_client_tee_service(file, arg);
1007
1008 case MTEE_CMD_SHM_REG:
1009 return tz_client_reg_sharedmem (file, arg);
1010
1011 case MTEE_CMD_SHM_UNREG:
1012 return tz_client_unreg_sharedmem (file, arg);
1013
1014 default:
1015 return -ENOTTY;
1016 }
1017
1018 return 0;
1019 }
1020
1021 /* pm op funcstions */
1022 static int tz_suspend(struct device *pdev)
1023 {
1024 TZ_RESULT tzret;
1025 tzret = kree_pm_device_ops(MTEE_SUSPEND);
1026 return (tzret != TZ_RESULT_SUCCESS)?(-EBUSY):(0);
1027 }
1028
1029 static int tz_suspend_late(struct device *pdev)
1030 {
1031 TZ_RESULT tzret;
1032 tzret = kree_pm_device_ops(MTEE_SUSPEND_LATE);
1033 return (tzret != TZ_RESULT_SUCCESS)?(-EBUSY):(0);
1034 }
1035
1036 static int tz_resume(struct device *pdev)
1037 {
1038 TZ_RESULT tzret;
1039 tzret = kree_pm_device_ops(MTEE_RESUME);
1040 return (tzret != TZ_RESULT_SUCCESS)?(-EBUSY):(0);
1041 }
1042
1043 static int tz_resume_early(struct device *pdev)
1044 {
1045 TZ_RESULT tzret;
1046 tzret = kree_pm_device_ops(MTEE_RESUME_EARLY);
1047 return (tzret != TZ_RESULT_SUCCESS)?(-EBUSY):(0);
1048 }
1049
1050 static const struct dev_pm_ops tz_pm_ops = {
1051 .suspend_late = tz_suspend_late,
1052 .freeze_late = tz_suspend_late,
1053 .resume_early = tz_resume_early,
1054 .thaw_early = tz_resume_early,
1055 SET_SYSTEM_SLEEP_PM_OPS(tz_suspend, tz_resume)
1056 };
1057
1058 /* add tz virtual driver for suspend/resume support */
1059 static struct platform_driver tz_driver = {
1060 .driver = {
1061 .name = TZ_DEVNAME,
1062 .pm = &tz_pm_ops,
1063 .owner = THIS_MODULE,
1064 },
1065 };
1066
1067
1068 /* add tz virtual device for suspend/resume support */
1069 static struct platform_device tz_device = {
1070 .name = TZ_DEVNAME,
1071 .id = -1,
1072 };
1073
1074 /******************************************************************************
1075 * register_tz_driver
1076 *
1077 * DESCRIPTION:
1078 * register the device driver !
1079 *
1080 * PARAMETERS:
1081 * None
1082 *
1083 * RETURNS:
1084 * 0 for success
1085 *
1086 * NOTES:
1087 * None
1088 *
1089 ******************************************************************************/
1090 static int __init register_tz_driver(void)
1091 {
1092 int ret = 0;
1093 if(platform_device_register(&tz_device))
1094 {
1095 ret = -ENODEV;
1096 xlog_printk(ANDROID_LOG_ERROR, MTEE_MOD_TAG ,"[%s] could not register device for the device, ret:%d\n", MODULE_NAME, ret);
1097 return ret;
1098 }
1099
1100 if(platform_driver_register(&tz_driver))
1101 {
1102 ret = -ENODEV;
1103 xlog_printk(ANDROID_LOG_ERROR, MTEE_MOD_TAG ,"[%s] could not register device for the device, ret:%d\n", MODULE_NAME, ret);
1104 platform_device_unregister(&tz_device);
1105 return ret;
1106 }
1107
1108 return ret;
1109 }
1110
1111 /******************************************************************************
1112 * tz_client_init
1113 *
1114 * DESCRIPTION:
1115 * Init the device driver !
1116 *
1117 * PARAMETERS:
1118 * None
1119 *
1120 * RETURNS:
1121 * 0 for success
1122 *
1123 * NOTES:
1124 * None
1125 *
1126 ******************************************************************************/
1127 static struct class* pTzClass = NULL;
1128 static struct device* pTzDevice = NULL;
1129
1130 static int __init tz_client_init(void)
1131 {
1132 int ret = 0;
1133 TZ_RESULT tzret;
1134 KREE_SESSION_HANDLE session;
1135 #ifdef ENABLE_INC_ONLY_COUNTER
1136 struct task_struct *thread;
1137 #endif
1138 #ifdef TZ_SECURETIME_SUPPORT
1139 struct task_struct *thread_securetime_gb;
1140 #endif
1141 ret = register_tz_driver();
1142 if (ret)
1143 {
1144 xlog_printk(ANDROID_LOG_ERROR, MTEE_MOD_TAG ,"[%s] register device/driver failed, ret:%d\n", MODULE_NAME, ret);
1145 return ret;
1146 }
1147
1148 tz_client_dev = MKDEV(MAJOR_DEV_NUM, 0);
1149
1150 xlog_printk(ANDROID_LOG_INFO, MTEE_MOD_TAG ," init\n");
1151
1152 ret = register_chrdev_region(tz_client_dev, 1, TZ_DEV_NAME );
1153 if (ret)
1154 {
1155 xlog_printk(ANDROID_LOG_ERROR, MTEE_MOD_TAG ,"[%s] register device failed, ret:%d\n", MODULE_NAME, ret);
1156 return ret;
1157 }
1158
1159 /* initialize the device structure and register the device */
1160 cdev_init(&tz_client_cdev, &tz_client_fops);
1161 tz_client_cdev.owner = THIS_MODULE;
1162
1163 if ((ret = cdev_add(&tz_client_cdev, tz_client_dev, 1)) < 0)
1164 {
1165 xlog_printk(ANDROID_LOG_ERROR, MTEE_MOD_TAG ,"[%s] could not allocate chrdev for the device, ret:%d\n", MODULE_NAME, ret);
1166 return ret;
1167 }
1168
1169 tzret = KREE_InitTZ();
1170 if (tzret != TZ_RESULT_SUCCESS)
1171 {
1172 printk("tz_client_init: TZ Faild %d\n", (int)tzret);
1173 BUG();
1174 }
1175
1176 kree_irq_init();
1177 kree_pm_init();
1178
1179 #ifdef ENABLE_INC_ONLY_COUNTER
1180 thread = kthread_run(update_counter_thread, NULL, "update_tz_counter");
1181 #endif
1182
1183 printk("tz_client_init: successfully\n");
1184
1185 //tz_test();
1186
1187 #ifdef CC_ENABLE_NDBG
1188 tz_ndbg_init();
1189 #endif
1190
1191 /* create /dev/trustzone automaticly */
1192 pTzClass = class_create(THIS_MODULE, TZ_DEV_NAME);
1193 if (IS_ERR(pTzClass)) {
1194 int ret = PTR_ERR(pTzClass);
1195 xlog_printk(ANDROID_LOG_ERROR, MTEE_MOD_TAG ,"[%s] could not create class for the device, ret:%d\n", MODULE_NAME, ret);
1196 return ret;
1197 }
1198 pTzDevice = device_create(pTzClass, NULL, tz_client_dev, NULL, TZ_DEV_NAME);
1199
1200 tzret = KREE_CreateSession(TZ_TA_MEM_UUID, &session);
1201 if(tzret!=TZ_RESULT_SUCCESS)
1202 {
1203 printk("KREE_CreateSession (TA_MEM) Fail, ret=%d\n", (int)tzret);
1204 return 0;
1205 }
1206 tzret = KREE_GetTEETotalSize(session, &memsz);
1207 if(tzret!=TZ_RESULT_SUCCESS)
1208 {
1209 printk("KREE_GetTEETotalSize Fail, ret=%d\n", (int)tzret);
1210 }
1211 tzret = KREE_CloseSession(session);
1212 #ifdef TZ_SECURETIME_SUPPORT
1213 thread_securetime_gb = kthread_run(update_securetime_thread_gb, NULL, "update_securetime_gb");
1214 #endif
1215 return 0;
1216 }
1217
1218 arch_initcall(tz_client_init);