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>
8 #include <linux/slab.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>
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"
24 #include "tz_counter.h"
25 #include "trustzone/tz_cross/ta_pm.h"
26 #include "trustzone/tz_cross/ta_mem.h"
29 #include "tz_secure_clock.h"
30 #include <linux/earlysuspend.h>
32 #define MTEE_MOD_TAG "MTEE_MOD"
34 #define TZ_PAGESIZE 0x1000 // fix me!!!! need global define
36 #define PAGE_SHIFT 12 // fix me!!!! need global define
38 #define TZ_DEVNAME "mtk_tz"
41 /**************************************************************************
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");
48 /**Used for mapping user space address to physical memory
57 } MTIOMMU_PIN_RANGE_T
;
59 /*****************************************************************************
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
);
68 static int tz_client_init_client_info(struct file
*file
);
69 static void tz_client_free_client_info(struct file
*file
);
71 #define TZ_CLIENT_INIT_HANDLE_SPACE 32
72 #define TZ_CLIENT_INIT_SHAREDMEM_SPACE 32
75 struct tz_sharedmem_info
{
76 KREE_SHAREDMEM_HANDLE mem_handle
;
77 KREE_SESSION_HANDLE session_handle
;
85 KREE_SESSION_HANDLE
*handles
;
86 struct tz_sharedmem_info
*shm_info
;
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
,
98 static int tz_client_open(struct inode
*inode
, struct file
*filp
)
100 return tz_client_init_client_info(filp
);
103 static int tz_client_release(struct inode
*inode
, struct file
*filp
)
105 tz_client_free_client_info(filp
);
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
)
115 unsigned int first
,last
;
117 struct vm_area_struct
*vma
;
121 if ((uaddr
== 0) || (size
== 0))
126 pinRange
->start
= uaddr
;
127 pinRange
->size
= size
;
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
)
137 pinRange
->pageArray
= (uint32_t)pages
;
138 write
= (control
== 0) ? 1 : 0;
140 /* Try to fault in all of the necessary pages */
141 down_read(¤t
->mm
->mmap_sem
);
142 vma
= find_vma_intersection(current
->mm
, uaddr
, uaddr
+size
);
148 if (!(vma
->vm_flags
& (VM_IO
| VM_PFNMAP
)))
150 pinRange
->isPage
= 1;
151 res
= get_user_pages(
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;
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
]);
178 if(res
>= nr_pages
|| uaddr
< vma
->vm_end
)
180 vma
= find_vma_intersection(current
->mm
, uaddr
, uaddr
+1);
181 } while (vma
&& vma
->vm_flags
& (VM_IO
| VM_PFNMAP
));
184 up_read(¤t
->mm
->mmap_sem
);
187 printk ("_map_user_pages error = 0x%x\n", res
);
191 pinRange
->nrPages
= res
;
192 /* Errors and no page mapped should return here */
196 //printk("_map_user_pages success: (start: 0x%x, size: 0x%x, #pages: 0x%x)\n", pinRange->start, pinRange->size, pinRange->nrPages);
201 printk("_map_user_pages fail\n");
206 for (j
=0; j
< res
; j
++)
218 static void _unmap_user_pages (MTIOMMU_PIN_RANGE_T
* pinRange
)
224 pages
= (struct page
**) pinRange
->pageArray
;
228 res
= pinRange
->nrPages
;
232 for (j
=0; j
< res
; j
++)
243 static struct tz_sharedmem_info
*tz_get_sharedmem (struct tz_client_info
*info
, KREE_SHAREDMEM_HANDLE handle
)
245 struct tz_sharedmem_info
*shm_info
;
250 for (i
= 0; i
< info
->shm_info_num
; i
++)
252 if (info
->shm_info
[i
].mem_handle
== handle
)
254 shm_info
= &info
->shm_info
[i
];
264 /**************************************************************************
265 * DEV tz_client_info handling
266 **************************************************************************/
268 tz_client_register_session(struct file
*file
, KREE_SESSION_HANDLE handle
)
270 struct tz_client_info
*info
;
271 int i
, num
, nspace
, ret
= -1;
274 info
= (struct tz_client_info
*)file
->private_data
;
277 mutex_lock(&info
->mux
);
280 num
= info
->handle_num
;
281 for (i
=0; i
<num
; i
++)
283 if ((int)info
->handles
[i
] == 0)
292 // Try grow the space
294 ptr
= krealloc(info
->handles
, nspace
* sizeof(KREE_SESSION_HANDLE
),
298 mutex_unlock(&info
->mux
);
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
));
310 info
->handles
[ret
] = handle
;
313 mutex_unlock(&info
->mux
);
317 static KREE_SESSION_HANDLE
tz_client_get_session(struct file
*file
, int handle
)
319 struct tz_client_info
*info
;
320 KREE_SESSION_HANDLE rhandle
;
322 info
= (struct tz_client_info
*)file
->private_data
;
325 mutex_lock(&info
->mux
);
327 if (handle
<= 0 || handle
> info
->handle_num
)
328 rhandle
= (KREE_SESSION_HANDLE
)0;
330 rhandle
= info
->handles
[handle
-1];
333 mutex_unlock(&info
->mux
);
337 static int tz_client_unregister_session(struct file
*file
, int handle
)
339 struct tz_client_info
*info
;
342 info
= (struct tz_client_info
*)file
->private_data
;
345 mutex_lock(&info
->mux
);
347 if (handle
<= 0 || handle
> info
->handle_num
|| info
->handles
[handle
-1] == 0)
350 info
->handles
[handle
-1] = (KREE_SESSION_HANDLE
)0;
353 mutex_unlock(&info
->mux
);
358 tz_client_register_sharedmem(struct file
*file
, KREE_SESSION_HANDLE handle
, KREE_SHAREDMEM_HANDLE mem_handle
, uint32_t *resource
)
360 struct tz_client_info
*info
;
361 int i
, num
, nspace
, ret
= -1;
364 info
= (struct tz_client_info
*)file
->private_data
;
367 mutex_lock(&info
->mux
);
370 num
= info
->shm_info_num
;
371 for (i
=0; i
<num
; i
++)
373 if ((int)info
->shm_info
[i
].mem_handle
== 0)
382 // Try grow the space
384 ptr
= krealloc(info
->shm_info
, nspace
* sizeof(struct tz_sharedmem_info
),
388 mutex_unlock(&info
->mux
);
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
));
401 info
->shm_info
[ret
].mem_handle
= mem_handle
;
402 info
->shm_info
[ret
].session_handle
= handle
;
403 info
->shm_info
[ret
].resouce
= resource
;
407 mutex_unlock(&info
->mux
);
412 static int tz_client_unregister_sharedmem(struct file
*file
, KREE_SHAREDMEM_HANDLE handle
)
414 struct tz_client_info
*info
;
415 struct tz_sharedmem_info
*shm_info
;
416 MTIOMMU_PIN_RANGE_T
*pin
;
419 info
= (struct tz_client_info
*)file
->private_data
;
422 mutex_lock(&info
->mux
);
424 shm_info
= tz_get_sharedmem(info
, handle
);
426 if ((shm_info
== NULL
) || (shm_info
->mem_handle
== 0))
430 pin
= (MTIOMMU_PIN_RANGE_T
*) shm_info
->resouce
;
431 _unmap_user_pages(pin
);
433 memset (shm_info
, 0, sizeof (struct tz_sharedmem_info
));
437 mutex_unlock(&info
->mux
);
443 static int tz_client_init_client_info(struct file
*file
)
445 struct tz_client_info
*info
;
447 info
= (struct tz_client_info
*)
448 kmalloc(sizeof(struct tz_client_info
), GFP_KERNEL
);
452 info
->handles
= (KREE_SESSION_HANDLE
*)kzalloc(
453 TZ_CLIENT_INIT_HANDLE_SPACE
* sizeof(KREE_SESSION_HANDLE
),
460 info
->handle_num
= TZ_CLIENT_INIT_HANDLE_SPACE
;
462 // init shared memory
463 info
->shm_info
= (struct tz_sharedmem_info
*)kzalloc(
464 TZ_CLIENT_INIT_SHAREDMEM_SPACE
* sizeof (struct tz_sharedmem_info
),
468 kfree(info
->handles
);
472 info
->shm_info_num
= TZ_CLIENT_INIT_SHAREDMEM_SPACE
;
474 mutex_init(&info
->mux
);
475 file
->private_data
= info
;
479 static void tz_client_free_client_info(struct file
*file
)
481 struct tz_client_info
*info
;
482 struct tz_sharedmem_info
*shm_info
;
483 MTIOMMU_PIN_RANGE_T
*pin
;
486 info
= (struct tz_client_info
*)file
->private_data
;
489 mutex_lock(&info
->mux
);
491 num
= info
->handle_num
;
492 for (i
=0; i
<num
; i
++)
494 if (info
->handles
[i
] == 0)
497 KREE_CloseSession(info
->handles
[i
]);
498 info
->handles
[i
] = (KREE_SESSION_HANDLE
)0;
501 // unregister shared memory
502 num
= info
->shm_info_num
;
503 for (i
=0; i
<num
; i
++)
505 if (info
->shm_info
[i
].mem_handle
== 0)
508 shm_info
= tz_get_sharedmem (info
, info
->shm_info
[i
].mem_handle
);
509 if (shm_info
== NULL
)
514 pin
= (MTIOMMU_PIN_RANGE_T
*) shm_info
->resouce
;
516 _unmap_user_pages (pin
);
521 file
->private_data
= 0;
522 mutex_unlock(&info
->mux
);
523 kfree(info
->handles
);
524 kfree(info
->shm_info
);
528 /**************************************************************************
530 **************************************************************************/
531 static long tz_client_open_session(struct file
*file
, unsigned long arg
)
533 struct kree_session_cmd_param param
;
538 KREE_SESSION_HANDLE handle
;
540 cret
= copy_from_user(¶m
, (void*)arg
, sizeof(param
));
544 // Check if can we access UUID string. 10 for min uuid len.
545 if (!access_ok(VERIFY_READ
, param
.data
, 10))
548 len
= strncpy_from_user(uuid
, param
.data
, sizeof(uuid
));
552 uuid
[sizeof(uuid
)-1]=0;
553 ret
= KREE_CreateSession(uuid
, &handle
);
556 // Register session to fd
557 if (ret
== TZ_RESULT_SUCCESS
)
559 param
.handle
= tz_client_register_session(file
, handle
);
560 if (param
.handle
< 0)
564 cret
= copy_to_user((void*)arg
, ¶m
, sizeof(param
));
571 tz_client_unregister_session(file
, param
.handle
);
573 KREE_CloseSession(handle
);
577 static long tz_client_close_session(struct file
*file
, unsigned long arg
)
579 struct kree_session_cmd_param param
;
582 KREE_SESSION_HANDLE handle
;
584 cret
= copy_from_user(¶m
, (void*)arg
, sizeof(param
));
588 handle
= tz_client_get_session(file
, param
.handle
);
592 tz_client_unregister_session(file
, param
.handle
);
593 ret
= KREE_CloseSession(handle
);
596 cret
= copy_to_user((void*)arg
, ¶m
, sizeof(param
));
603 static long tz_client_tee_service(struct file
*file
, unsigned long arg
)
605 struct kree_tee_service_cmd_param cparam
;
608 MTEEC_PARAM param
[4], oparam
[4];
611 KREE_SESSION_HANDLE handle
;
613 cret
= copy_from_user(&cparam
, (void*)arg
, sizeof(cparam
));
617 if (cparam
.paramTypes
!= TZPT_NONE
|| cparam
.param
)
619 // Check if can we access param
620 if (!access_ok(VERIFY_READ
, cparam
.param
, sizeof(oparam
)))
623 cret
= copy_from_user(oparam
, (void*)cparam
.param
, sizeof(oparam
));
629 handle
= tz_client_get_session(file
, cparam
.handle
);
633 // Parameter processing.
634 memset(param
, 0, sizeof(param
));
635 tmpTypes
= cparam
.paramTypes
;
636 for (i
=0; tmpTypes
; i
++)
638 TZ_PARAM_TYPES type
= tmpTypes
& 0xff;
642 case TZPT_VALUE_INPUT
:
643 case TZPT_VALUE_INOUT
:
644 param
[i
] = oparam
[i
];
647 case TZPT_VALUE_OUTPUT
:
653 case TZPT_MEM_OUTPUT
:
656 if (type
!= TZPT_MEM_OUTPUT
)
658 if (!access_ok(VERIFY_READ
, oparam
[i
].mem
.buffer
,
665 if (type
!= TZPT_MEM_INPUT
)
667 if (!access_ok(VERIFY_WRITE
, oparam
[i
].mem
.buffer
,
675 // Allocate kernel space memory. Fail if > 4kb
676 if (oparam
[i
].mem
.size
> TEE_PARAM_MEM_LIMIT
)
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
)
690 if (type
!= TZPT_MEM_OUTPUT
)
692 cret
= copy_from_user(param
[i
].mem
.buffer
,
693 (void*)oparam
[i
].mem
.buffer
,
703 case TZPT_MEMREF_INPUT
:
704 case TZPT_MEMREF_OUTPUT
:
705 case TZPT_MEMREF_INOUT
:
706 // Check if share memory is valid.
708 param
[i
] = oparam
[i
];
712 // Bad format, return.
713 ret
= TZ_RESULT_ERROR_BAD_FORMAT
;
719 ret
= KREE_TeeServiceCallNoCheck(handle
, cparam
.command
, cparam
.paramTypes
, param
);
723 tmpTypes
= cparam
.paramTypes
;
724 for (i
=0; tmpTypes
; i
++)
726 TZ_PARAM_TYPES type
= tmpTypes
& 0xff;
730 case TZPT_VALUE_OUTPUT
:
731 case TZPT_VALUE_INOUT
:
732 oparam
[i
] = param
[i
];
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
:
746 case TZPT_MEM_OUTPUT
:
748 if (type
!= TZPT_MEM_INPUT
)
750 cret
= copy_to_user((void*)oparam
[i
].mem
.buffer
,
760 if (param
[i
].mem
.buffer
)
762 kfree(param
[i
].mem
.buffer
);
763 param
[i
].mem
.buffer
= 0;
770 if (cparam
.paramTypes
!= TZPT_NONE
)
772 cret
= copy_to_user((void*)cparam
.param
, oparam
, sizeof(oparam
));
777 cret
= copy_to_user((void*)arg
, &cparam
, sizeof(cparam
));
783 tmpTypes
= cparam
.paramTypes
;
784 for (i
=0; tmpTypes
; i
++)
786 TZ_PARAM_TYPES type
= tmpTypes
& 0xff;
791 case TZPT_MEM_OUTPUT
:
793 if (param
[i
].mem
.buffer
)
794 kfree(param
[i
].mem
.buffer
);
806 static long tz_client_reg_sharedmem (struct file
*file
, unsigned long arg
)
809 struct kree_sharedmemory_cmd_param cparam
;
810 KREE_SESSION_HANDLE session
;
812 MTIOMMU_PIN_RANGE_T
*pin
;
820 cret
= copy_from_user(&cparam
, (void*)arg
, sizeof(cparam
));
827 session
= tz_client_get_session(file
, cparam
.session
);
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
)
840 goto client_regshm_mapfail
;
842 cret
= _map_user_pages (pin
, (uint32_t) cparam
.buffer
, cparam
.size
, cparam
.control
);
845 printk ("tz_client_reg_sharedmem fail: map user pages = 0x%x\n", (uint32_t) cret
);
847 goto client_regshm_mapfail_1
;
851 if((map_p
= kzalloc(sizeof (uint32_t) * (pin
->nrPages
+ 1), GFP_KERNEL
)) == NULL
)
854 goto client_regshm_mapfail_2
;
856 map_p
[0] = pin
->nrPages
;
859 page
= (struct page
**) pin
->pageArray
;
860 for (i
= 0; i
< pin
->nrPages
; i
++)
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]);
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]); */
877 ret
= kree_register_sharedmem (session
, &mem_handle
, (uint32_t) pin
->start
, pin
->size
, (uint32_t) map_p
);
878 if (ret
!= TZ_RESULT_SUCCESS
)
880 printk ("tz_client_reg_sharedmem fail: register shm 0x%x\n", ret
);
883 goto client_regshm_free
;
889 cret
= tz_client_register_sharedmem (file
, session
, (KREE_SHAREDMEM_HANDLE
) mem_handle
, (uint32_t *)pin
);
892 printk ("tz_client_reg_sharedmem fail: register fd 0x%x\n", (uint32_t) cret
);
894 goto client_regshm_free_1
;
897 cparam
.mem_handle
= mem_handle
;
898 cparam
.ret
= ret
; // TEE service call return
899 cret
= copy_to_user((void*)arg
, &cparam
, sizeof(cparam
));
905 return TZ_RESULT_SUCCESS
;
907 client_regshm_free_1
:
908 kree_unregister_sharedmem (session
, mem_handle
);
910 _unmap_user_pages (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");
917 client_regshm_mapfail_2
:
918 _unmap_user_pages (pin
);
919 client_regshm_mapfail_1
:
921 client_regshm_mapfail
:
922 printk ("tz_client_reg_sharedmem fail: map memory\n");
926 static long tz_client_unreg_sharedmem (struct file
*file
, unsigned long arg
)
929 struct kree_sharedmemory_cmd_param cparam
;
930 KREE_SESSION_HANDLE session
;
933 cret
= copy_from_user(&cparam
, (void*)arg
, sizeof(cparam
));
940 session
= tz_client_get_session(file
, cparam
.session
);
948 ret
= kree_unregister_sharedmem (session
, (uint32_t) cparam
.mem_handle
);
949 if (ret
!= TZ_RESULT_SUCCESS
)
951 printk ("tz_client_unreg_sharedmem: 0x%x\n", ret
);
953 cret
= copy_to_user((void*)arg
, &cparam
, sizeof(cparam
));
957 /* unmap user pages and unregister to fd
959 ret
= tz_client_unregister_sharedmem (file
, cparam
.mem_handle
);
960 if (ret
!= TZ_RESULT_SUCCESS
)
962 printk ("tz_client_unreg_sharedmem: unregister shm = 0x%x\n", ret
);
967 cret
= copy_to_user((void*)arg
, &cparam
, sizeof(cparam
));
973 return TZ_RESULT_SUCCESS
;
978 static long tz_client_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
982 /* ---------------------------------- */
984 /* ---------------------------------- */
985 if (_IOC_TYPE(cmd
) != MTEE_IOC_MAGIC
)
987 if (_IOC_NR(cmd
) > DEV_IOC_MAXNR
)
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
));
995 if (err
) return -EFAULT
;
999 case MTEE_CMD_OPEN_SESSION
:
1000 return tz_client_open_session(file
, arg
);
1002 case MTEE_CMD_CLOSE_SESSION
:
1003 return tz_client_close_session(file
, arg
);
1005 case MTEE_CMD_TEE_SERVICE
:
1006 return tz_client_tee_service(file
, arg
);
1008 case MTEE_CMD_SHM_REG
:
1009 return tz_client_reg_sharedmem (file
, arg
);
1011 case MTEE_CMD_SHM_UNREG
:
1012 return tz_client_unreg_sharedmem (file
, arg
);
1021 /* pm op funcstions */
1022 static int tz_suspend(struct device
*pdev
)
1025 tzret
= kree_pm_device_ops(MTEE_SUSPEND
);
1026 return (tzret
!= TZ_RESULT_SUCCESS
)?(-EBUSY
):(0);
1029 static int tz_suspend_late(struct device
*pdev
)
1032 tzret
= kree_pm_device_ops(MTEE_SUSPEND_LATE
);
1033 return (tzret
!= TZ_RESULT_SUCCESS
)?(-EBUSY
):(0);
1036 static int tz_resume(struct device
*pdev
)
1039 tzret
= kree_pm_device_ops(MTEE_RESUME
);
1040 return (tzret
!= TZ_RESULT_SUCCESS
)?(-EBUSY
):(0);
1043 static int tz_resume_early(struct device
*pdev
)
1046 tzret
= kree_pm_device_ops(MTEE_RESUME_EARLY
);
1047 return (tzret
!= TZ_RESULT_SUCCESS
)?(-EBUSY
):(0);
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
)
1058 /* add tz virtual driver for suspend/resume support */
1059 static struct platform_driver tz_driver
= {
1063 .owner
= THIS_MODULE
,
1068 /* add tz virtual device for suspend/resume support */
1069 static struct platform_device tz_device
= {
1074 /******************************************************************************
1075 * register_tz_driver
1078 * register the device driver !
1089 ******************************************************************************/
1090 static int __init
register_tz_driver(void)
1093 if(platform_device_register(&tz_device
))
1096 xlog_printk(ANDROID_LOG_ERROR
, MTEE_MOD_TAG
,"[%s] could not register device for the device, ret:%d\n", MODULE_NAME
, ret
);
1100 if(platform_driver_register(&tz_driver
))
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
);
1111 /******************************************************************************
1115 * Init the device driver !
1126 ******************************************************************************/
1127 static struct class* pTzClass
= NULL
;
1128 static struct device
* pTzDevice
= NULL
;
1130 static int __init
tz_client_init(void)
1134 KREE_SESSION_HANDLE session
;
1135 #ifdef ENABLE_INC_ONLY_COUNTER
1136 struct task_struct
*thread
;
1138 #ifdef TZ_SECURETIME_SUPPORT
1139 struct task_struct
*thread_securetime_gb
;
1141 ret
= register_tz_driver();
1144 xlog_printk(ANDROID_LOG_ERROR
, MTEE_MOD_TAG
,"[%s] register device/driver failed, ret:%d\n", MODULE_NAME
, ret
);
1148 tz_client_dev
= MKDEV(MAJOR_DEV_NUM
, 0);
1150 xlog_printk(ANDROID_LOG_INFO
, MTEE_MOD_TAG
," init\n");
1152 ret
= register_chrdev_region(tz_client_dev
, 1, TZ_DEV_NAME
);
1155 xlog_printk(ANDROID_LOG_ERROR
, MTEE_MOD_TAG
,"[%s] register device failed, ret:%d\n", MODULE_NAME
, ret
);
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
;
1163 if ((ret
= cdev_add(&tz_client_cdev
, tz_client_dev
, 1)) < 0)
1165 xlog_printk(ANDROID_LOG_ERROR
, MTEE_MOD_TAG
,"[%s] could not allocate chrdev for the device, ret:%d\n", MODULE_NAME
, ret
);
1169 tzret
= KREE_InitTZ();
1170 if (tzret
!= TZ_RESULT_SUCCESS
)
1172 printk("tz_client_init: TZ Faild %d\n", (int)tzret
);
1179 #ifdef ENABLE_INC_ONLY_COUNTER
1180 thread
= kthread_run(update_counter_thread
, NULL
, "update_tz_counter");
1183 printk("tz_client_init: successfully\n");
1187 #ifdef CC_ENABLE_NDBG
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
);
1198 pTzDevice
= device_create(pTzClass
, NULL
, tz_client_dev
, NULL
, TZ_DEV_NAME
);
1200 tzret
= KREE_CreateSession(TZ_TA_MEM_UUID
, &session
);
1201 if(tzret
!=TZ_RESULT_SUCCESS
)
1203 printk("KREE_CreateSession (TA_MEM) Fail, ret=%d\n", (int)tzret
);
1206 tzret
= KREE_GetTEETotalSize(session
, &memsz
);
1207 if(tzret
!=TZ_RESULT_SUCCESS
)
1209 printk("KREE_GetTEETotalSize Fail, ret=%d\n", (int)tzret
);
1211 tzret
= KREE_CloseSession(session
);
1212 #ifdef TZ_SECURETIME_SUPPORT
1213 thread_securetime_gb
= kthread_run(update_securetime_thread_gb
, NULL
, "update_securetime_gb");
1218 arch_initcall(tz_client_init
);