[FOSS_TLK]security: tlk_driver: new ss protocol support
[tegra/ote_partner/tlk_driver.git] / security / tlk_driver / 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         int ss_cmd;
113
114         switch (ioctl_num) {
115         case TE_IOCTL_SS_NEW_REQ:
116                 /* wait for a new request */
117                 if (wait_for_completion_interruptible(&req_ready))
118                         return -ENODATA;
119
120                 /* transfer pending request to daemon's buffer */
121                 if (copy_to_user((void __user *)ioctl_param, ss_op_shmem->data,
122                                         ss_op_shmem->req_size)) {
123                         pr_err("copy_to_user failed for new request\n");
124                         return -EFAULT;
125                 }
126                 break;
127
128         case TE_IOCTL_SS_REQ_COMPLETE: /* request complete */
129                 if (copy_from_user(ss_op_shmem->data,
130                         (void __user *)ioctl_param, ss_op_shmem->req_size)) {
131                         pr_err("copy_from_user failed for request\n");
132                         return -EFAULT;
133                 }
134
135                 /* signal the producer */
136                 complete(&req_complete);
137                 break;
138         case TE_IOCTL_SS_CMD:
139                 if (copy_from_user(&ss_cmd, (void __user *)ioctl_param,
140                                         sizeof(ss_cmd))) {
141                         pr_err("%s: copy from user space failed\n", __func__);
142                         return -EFAULT;
143                 }
144
145                 if (ss_cmd == TE_IOCTL_SS_CMD_GET_NEW_REQ) {
146                         /* wait for a new request */
147                         if (wait_for_completion_interruptible(&req_ready))
148                                 return -ENODATA;
149                 } else if (ss_cmd == TE_IOCTL_SS_CMD_REQ_COMPLETE) {
150                         /* signal the producer */
151                         complete(&req_complete);
152                 } else {
153                         pr_err("%s: unknown ss_cmd 0x%x\n", __func__, ss_cmd);
154                         return -EINVAL;
155                 }
156         }
157
158         return 0;
159 }
160
161 void tlk_ss_op(void)
162 {
163         /* signal consumer */
164         complete(&req_ready);
165
166         /* wait for the consumer's signal */
167         wait_for_completion(&req_complete);
168 }
169
170 static int __init tlk_ss_init(void)
171 {
172         dma_addr_t ss_op_shmem_dma;
173         int32_t ret;
174
175         /* allocate shared memory buffer */
176         ss_op_shmem = dma_alloc_coherent(NULL, sizeof(struct te_ss_op),
177                         &ss_op_shmem_dma, GFP_KERNEL);
178         if (!ss_op_shmem) {
179                 pr_err("%s: no memory available for fs operations\n", __func__);
180                 return -ENOMEM;
181         }
182
183         ret = tlk_generic_smc(TE_SMC_SS_REGISTER_HANDLER,
184                         (uintptr_t)ss_op_shmem, 0);
185         if (ret != 0) {
186                 dma_free_coherent(NULL, sizeof(struct te_ss_op),
187                         (void *)ss_op_shmem, ss_op_shmem_dma);
188                 ss_op_shmem = NULL;
189                 return -ENOTSUPP;
190         }
191
192         return 0;
193 }
194
195 arch_initcall(tlk_ss_init);