tlk_driver: 5/22 update
[tegra/ote_partner/tlk_driver.git] / ote_fs.c
1 /*
2  * Copyright (c) 2013-2014 NVIDIA Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #include <linux/slab.h>
20 #include <linux/syscalls.h>
21 #include <linux/list.h>
22 #include <linux/completion.h>
23 #include <linux/workqueue.h>
24 #include <linux/bitops.h>
25 #include <linux/uaccess.h>
26 #include <linux/dma-mapping.h>
27
28 #include "ote_protocol.h"
29
30 static DECLARE_COMPLETION(req_ready);
31 static DECLARE_COMPLETION(req_complete);
32
33 static struct te_ss_op_legacy *ss_op_shmem_legacy;
34 static struct te_ss_op *ss_op_shmem;
35 static uint32_t ss_op_size;
36
37 static void indicate_ss_op_complete(void)
38 {
39         tlk_generic_smc(TE_SMC_SS_REQ_COMPLETE, 0, 0);
40 }
41
42 int te_handle_ss_ioctl_legacy(struct file *file, unsigned int ioctl_num,
43         unsigned long ioctl_param)
44 {
45         switch (ioctl_num) {
46         case TE_IOCTL_SS_NEW_REQ_LEGACY:
47                 /* wait for a new request */
48                 if (wait_for_completion_interruptible(&req_ready))
49                         return -ENODATA;
50
51                 /* transfer pending request to daemon's buffer */
52                 if (copy_to_user((void __user *)ioctl_param, ss_op_shmem_legacy,
53                                         ss_op_size)) {
54                         pr_err("copy_to_user failed for new request\n");
55                         return -EFAULT;
56                 }
57                 break;
58
59         case TE_IOCTL_SS_REQ_COMPLETE_LEGACY: /* request complete */
60                 if (copy_from_user(ss_op_shmem_legacy,
61                         (void __user *)ioctl_param, ss_op_size)) {
62                         pr_err("copy_from_user failed for request\n");
63                         return -EFAULT;
64                 }
65
66                 /* signal the producer */
67                 complete(&req_complete);
68                 break;
69         }
70
71         return 0;
72 }
73
74 void tlk_ss_op_legacy(uint32_t size)
75 {
76         /* store size of request */
77         ss_op_size = size;
78
79         /* signal consumer */
80         complete(&req_ready);
81
82         /* wait for the consumer's signal */
83         wait_for_completion(&req_complete);
84
85         /* signal completion to the secure world */
86         indicate_ss_op_complete();
87 }
88
89 static int __init tlk_ss_init_legacy(void)
90 {
91         dma_addr_t ss_op_shmem_dma;
92
93         /* allocate shared memory buffer */
94         ss_op_shmem_legacy = dma_alloc_coherent(NULL,
95                 sizeof(struct te_ss_op_legacy), &ss_op_shmem_dma, GFP_KERNEL);
96         if (!ss_op_shmem_legacy) {
97                 pr_err("%s: no memory available for fs operations\n", __func__);
98                 return -ENOMEM;
99         }
100
101         tlk_generic_smc(TE_SMC_SS_REGISTER_HANDLER_LEGACY,
102                 (uintptr_t)tlk_ss_op_legacy, (uintptr_t)ss_op_shmem_legacy);
103
104         return 0;
105 }
106
107 arch_initcall(tlk_ss_init_legacy);
108
109 int te_handle_ss_ioctl(struct file *file, unsigned int ioctl_num,
110         unsigned long ioctl_param)
111 {
112         switch (ioctl_num) {
113         case TE_IOCTL_SS_NEW_REQ:
114                 /* wait for a new request */
115                 if (wait_for_completion_interruptible(&req_ready))
116                         return -ENODATA;
117
118                 /* transfer pending request to daemon's buffer */
119                 if (copy_to_user((void __user *)ioctl_param, ss_op_shmem->data,
120                                         ss_op_shmem->req_size)) {
121                         pr_err("copy_to_user failed for new request\n");
122                         return -EFAULT;
123                 }
124                 break;
125
126         case TE_IOCTL_SS_REQ_COMPLETE: /* request complete */
127                 if (copy_from_user(ss_op_shmem->data,
128                         (void __user *)ioctl_param, ss_op_shmem->req_size)) {
129                         pr_err("copy_from_user failed for request\n");
130                         return -EFAULT;
131                 }
132
133                 /* signal the producer */
134                 complete(&req_complete);
135                 break;
136         }
137
138         return 0;
139 }
140
141 void tlk_ss_op(void)
142 {
143         /* signal consumer */
144         complete(&req_ready);
145
146         /* wait for the consumer's signal */
147         wait_for_completion(&req_complete);
148 }
149
150 static int __init tlk_ss_init(void)
151 {
152         dma_addr_t ss_op_shmem_dma;
153         int32_t ret;
154
155         /* allocate shared memory buffer */
156         ss_op_shmem = dma_alloc_coherent(NULL, sizeof(struct te_ss_op),
157                         &ss_op_shmem_dma, GFP_KERNEL);
158         if (!ss_op_shmem) {
159                 pr_err("%s: no memory available for fs operations\n", __func__);
160                 return -ENOMEM;
161         }
162
163         ret = tlk_generic_smc(TE_SMC_SS_REGISTER_HANDLER,
164                         (uintptr_t)ss_op_shmem, 0);
165         if (ret != 0) {
166                 dma_free_coherent(NULL, sizeof(struct te_ss_op),
167                         (void *)ss_op_shmem, ss_op_shmem_dma);
168                 ss_op_shmem = NULL;
169                 return -ENOTSUPP;
170         }
171
172         return 0;
173 }
174
175 arch_initcall(tlk_ss_init);