Merge tag 'v4.4-rc5'
[firefly-linux-kernel-4.4.55.git] / security / tlk_driver / ote_protocol.h
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 #ifndef __OTE_PROTOCOL_H__
20 #define __OTE_PROTOCOL_H__
21
22 #include "ote_types.h"
23
24 #define TE_IOCTL_MAGIC_NUMBER ('t')
25 #define TE_IOCTL_OPEN_CLIENT_SESSION \
26         _IOWR(TE_IOCTL_MAGIC_NUMBER, 0x10, union te_cmd)
27 #define TE_IOCTL_CLOSE_CLIENT_SESSION \
28         _IOWR(TE_IOCTL_MAGIC_NUMBER, 0x11, union te_cmd)
29 #define TE_IOCTL_LAUNCH_OPERATION \
30         _IOWR(TE_IOCTL_MAGIC_NUMBER, 0x14, union te_cmd)
31
32 /* ioctls using new structs (eventually to replace current ioctls) */
33 #define TE_IOCTL_OPEN_CLIENT_SESSION_COMPAT \
34         _IOWR(TE_IOCTL_MAGIC_NUMBER, 0x10, union te_cmd_compat)
35 #define TE_IOCTL_CLOSE_CLIENT_SESSION_COMPAT \
36         _IOWR(TE_IOCTL_MAGIC_NUMBER, 0x11, union te_cmd_compat)
37 #define TE_IOCTL_LAUNCH_OPERATION_COMPAT \
38         _IOWR(TE_IOCTL_MAGIC_NUMBER, 0x14, union te_cmd_compat)
39
40 #define TE_IOCTL_SS_NEW_REQ_LEGACY \
41         _IOR(TE_IOCTL_MAGIC_NUMBER,  0x20, struct te_ss_op_legacy)
42 #define TE_IOCTL_SS_REQ_COMPLETE_LEGACY \
43         _IOWR(TE_IOCTL_MAGIC_NUMBER, 0x21, struct te_ss_op_legacy)
44
45 /* ioctls using new SS structs (eventually to replace current SS ioctls) */
46 #define TE_IOCTL_SS_NEW_REQ \
47         _IOR(TE_IOCTL_MAGIC_NUMBER,  0x20, struct te_ss_op)
48 #define TE_IOCTL_SS_REQ_COMPLETE \
49         _IOWR(TE_IOCTL_MAGIC_NUMBER, 0x21, struct te_ss_op)
50
51 #define TE_IOCTL_MIN_NR _IOC_NR(TE_IOCTL_OPEN_CLIENT_SESSION)
52 #define TE_IOCTL_MAX_NR _IOC_NR(TE_IOCTL_SS_REQ_COMPLETE)
53
54 /* shared buffer is 2 pages: 1st are requests, 2nd are params */
55 #define TE_CMD_DESC_MAX (PAGE_SIZE / sizeof(struct te_request))
56 #define TE_PARAM_MAX    (PAGE_SIZE / sizeof(struct te_oper_param))
57
58 #define TE_CMD_DESC_MAX_COMPAT \
59         (PAGE_SIZE / sizeof(struct te_request_compat))
60 #define TE_PARAM_MAX_COMPAT \
61         (PAGE_SIZE / sizeof(struct te_oper_param_compat))
62
63 #define MAX_EXT_SMC_ARGS        12
64
65 extern struct mutex smc_lock;
66 extern struct tlk_device tlk_dev;
67
68 uint32_t _tlk_generic_smc(uint32_t arg0, uintptr_t arg1, uintptr_t arg2);
69 uint32_t tlk_generic_smc(uint32_t arg0, uintptr_t arg1, uintptr_t arg2);
70 uint32_t _tlk_extended_smc(uintptr_t *args);
71 uint32_t tlk_extended_smc(uintptr_t *args);
72 void tlk_irq_handler(void);
73
74 /* errors returned by secure world in reponse to SMC calls */
75 enum {
76         TE_ERROR_PREEMPT_BY_IRQ = 0xFFFFFFFD,
77         TE_ERROR_PREEMPT_BY_FS = 0xFFFFFFFE,
78 };
79
80 struct tlk_device {
81         struct te_request *req_addr;
82         dma_addr_t req_addr_phys;
83         struct te_oper_param *param_addr;
84         dma_addr_t param_addr_phys;
85
86         struct te_request_compat *req_addr_compat;
87         struct te_oper_param_compat *param_addr_compat;
88
89         char *req_param_buf;
90
91         unsigned long *param_bitmap;
92
93         struct list_head used_cmd_list;
94         struct list_head free_cmd_list;
95 };
96
97 struct te_cmd_req_desc {
98         struct te_request *req_addr;
99         struct list_head list;
100 };
101
102 struct te_cmd_req_desc_compat {
103         struct te_request_compat *req_addr;
104         struct list_head list;
105 };
106
107 struct te_shmem_desc {
108         struct list_head list;
109         void *buffer;
110         size_t size;
111         unsigned int mem_type;
112 };
113
114 struct tlk_context {
115         struct tlk_device *dev;
116         struct list_head shmem_alloc_list;
117 };
118
119 enum {
120         /* Trusted Application Calls */
121         TE_SMC_OPEN_SESSION             = 0x30000001,
122         TE_SMC_CLOSE_SESSION            = 0x30000002,
123         TE_SMC_LAUNCH_OPERATION         = 0x30000003,
124
125         /* Trusted OS calls */
126         TE_SMC_REGISTER_REQ_BUF         = 0x32000002,
127         TE_SMC_REGISTER_IRQ_HANDLER     = 0x32000004,
128         TE_SMC_NS_IRQ_DONE              = 0x32000005,
129         TE_SMC_INIT_LOGGER              = 0x32000007,
130         TE_SMC_SS_REGISTER_HANDLER_LEGACY       = 0x32000008,
131         TE_SMC_SS_REQ_COMPLETE          = 0x32000009,
132         TE_SMC_SS_REGISTER_HANDLER      = 0x32000010,
133
134         /* SIP (SOC specific) calls.  */
135         TE_SMC_PROGRAM_VPR              = 0x82000003,
136 };
137
138 enum {
139         TE_PARAM_TYPE_NONE      = 0,
140         TE_PARAM_TYPE_INT_RO    = 1,
141         TE_PARAM_TYPE_INT_RW    = 2,
142         TE_PARAM_TYPE_MEM_RO    = 3,
143         TE_PARAM_TYPE_MEM_RW    = 4,
144 };
145
146 struct te_oper_param {
147         uint32_t index;
148         uint32_t type;
149         union {
150                 struct {
151                         uint32_t val;
152                 } Int;
153                 struct {
154                         void  *base;
155                         uint32_t len;
156                 } Mem;
157         } u;
158         void *next_ptr_user;
159 };
160
161 struct te_oper_param_compat {
162         uint32_t index;
163         uint32_t type;
164         union {
165                 struct {
166                         uint32_t val;
167                 } Int;
168                 struct {
169                         uint64_t base;
170                         uint32_t len;
171                 } Mem;
172         } u;
173         uint64_t next_ptr_user;
174 };
175
176 struct te_operation {
177         uint32_t command;
178         struct te_oper_param *list_head;
179         /* Maintain a pointer to tail of list to easily add new param node */
180         struct te_oper_param *list_tail;
181         uint32_t list_count;
182         uint32_t status;
183         uint32_t iterface_side;
184 };
185
186 struct te_service_id {
187         uint32_t time_low;
188         uint16_t time_mid;
189         uint16_t time_hi_and_version;
190         uint8_t clock_seq_and_node[8];
191 };
192
193 /*
194  * OpenSession
195  */
196 struct te_opensession {
197         struct te_service_id dest_uuid;
198         struct te_operation operation;
199         uint32_t answer;
200 };
201
202 /*
203  * CloseSession
204  */
205 struct te_closesession {
206         uint32_t        session_id;
207         uint32_t        answer;
208 };
209
210 /*
211  * LaunchOperation
212  */
213 struct te_launchop {
214         uint32_t                session_id;
215         struct te_operation     operation;
216         uint32_t                answer;
217 };
218
219 union te_cmd {
220         struct te_opensession   opensession;
221         struct te_closesession  closesession;
222         struct te_launchop      launchop;
223 };
224
225 /*
226  * Compat versions of the original structs (eventually to replace
227  * the old structs, once the lib/TLK kernel changes are in).
228  */
229 struct te_operation_compat {
230         uint32_t        command;
231         uint32_t        status;
232         uint64_t        list_head;
233         uint64_t        list_tail;
234         uint32_t        list_count;
235         uint32_t        interface_side;
236 };
237
238 /*
239  * OpenSession
240  */
241 struct te_opensession_compat {
242         struct te_service_id            dest_uuid;
243         struct te_operation_compat      operation;
244         uint64_t                        answer;
245 };
246
247 /*
248  * CloseSession
249  */
250 struct te_closesession_compat {
251         uint32_t        session_id;
252         uint64_t        answer;
253 };
254
255 /*
256  * LaunchOperation
257  */
258 struct te_launchop_compat {
259         uint32_t                        session_id;
260         struct te_operation_compat      operation;
261         uint64_t                        answer;
262 };
263
264 union te_cmd_compat {
265         struct te_opensession_compat    opensession;
266         struct te_closesession_compat   closesession;
267         struct te_launchop_compat       launchop;
268 };
269
270 struct te_request {
271         uint32_t                type;
272         uint32_t                session_id;
273         uint32_t                command_id;
274         struct te_oper_param    *params;
275         uint32_t                params_size;
276         uint32_t                dest_uuid[4];
277         uint32_t                result;
278         uint32_t                result_origin;
279 };
280
281 struct te_request_compat {
282         uint32_t                type;
283         uint32_t                session_id;
284         uint32_t                command_id;
285         uint64_t                params;
286         uint32_t                params_size;
287         uint32_t                dest_uuid[4];
288         uint32_t                result;
289         uint32_t                result_origin;
290 };
291
292 struct te_answer {
293         uint32_t        result;
294         uint32_t        session_id;
295         uint32_t        result_origin;
296 };
297
298 void te_open_session(struct te_opensession *cmd,
299         struct te_request *request,
300         struct tlk_context *context);
301
302 void te_close_session(struct te_closesession *cmd,
303         struct te_request *request,
304         struct tlk_context *context);
305
306 void te_launch_operation(struct te_launchop *cmd,
307         struct te_request *request,
308         struct tlk_context *context);
309
310 void te_open_session_compat(struct te_opensession_compat *cmd,
311         struct te_request_compat *request,
312         struct tlk_context *context);
313
314 void te_close_session_compat(struct te_closesession_compat *cmd,
315         struct te_request_compat *request,
316         struct tlk_context *context);
317
318 void te_launch_operation_compat(struct te_launchop_compat *cmd,
319         struct te_request_compat *request,
320         struct tlk_context *context);
321
322 #define SS_OP_MAX_DATA_SIZE     0x1000
323 struct te_ss_op {
324         uint32_t        req_size;
325         uint8_t         data[SS_OP_MAX_DATA_SIZE];
326 };
327
328 struct te_ss_op_legacy {
329         uint8_t         data[SS_OP_MAX_DATA_SIZE];
330 };
331
332 int te_handle_ss_ioctl_legacy(struct file *file, unsigned int ioctl_num,
333                 unsigned long ioctl_param);
334 int te_handle_ss_ioctl(struct file *file, unsigned int ioctl_num,
335                 unsigned long ioctl_param);
336 int te_handle_fs_ioctl(struct file *file, unsigned int ioctl_num,
337                 unsigned long ioctl_param);
338 void ote_print_logs(void);
339 void tlk_ss_op(void);
340
341 #endif