1 /*
2  * Copyright (c) 2015, Linaro Limited
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14 
15 #ifndef OPTEE_PRIVATE_H
16 #define OPTEE_PRIVATE_H
17 
18 #include <linux/arm-smccc.h>
19 #include <linux/semaphore.h>
20 #include <linux/tee_drv.h>
21 #include <linux/types.h>
22 #include "optee_msg.h"
23 
24 #define OPTEE_MAX_ARG_SIZE	1024
25 
26 /* Some Global Platform error codes used in this driver */
27 #define TEEC_SUCCESS			0x00000000
28 #define TEEC_ERROR_BAD_PARAMETERS	0xFFFF0006
29 #define TEEC_ERROR_COMMUNICATION	0xFFFF000E
30 #define TEEC_ERROR_OUT_OF_MEMORY	0xFFFF000C
31 
32 #define TEEC_ORIGIN_COMMS		0x00000002
33 
34 typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,
35 				unsigned long, unsigned long, unsigned long,
36 				unsigned long, unsigned long,
37 				struct arm_smccc_res *);
38 
39 struct optee_call_queue {
40 	/* Serializes access to this struct */
41 	struct mutex mutex;
42 	struct list_head waiters;
43 };
44 
45 struct optee_wait_queue {
46 	/* Serializes access to this struct */
47 	struct mutex mu;
48 	struct list_head db;
49 };
50 
51 /**
52  * struct optee_supp - supplicant synchronization struct
53  * @ctx			the context of current connected supplicant.
54  *			if !NULL the supplicant device is available for use,
55  *			else busy
56  * @mutex:		held while accessing content of this struct
57  * @req_id:		current request id if supplicant is doing synchronous
58  *			communication, else -1
59  * @reqs:		queued request not yet retrieved by supplicant
60  * @idr:		IDR holding all requests currently being processed
61  *			by supplicant
62  * @reqs_c:		completion used by supplicant when waiting for a
63  *			request to be queued.
64  */
65 struct optee_supp {
66 	/* Serializes access to this struct */
67 	struct mutex mutex;
68 	struct tee_context *ctx;
69 
70 	int req_id;
71 	struct list_head reqs;
72 	struct idr idr;
73 	struct completion reqs_c;
74 };
75 
76 /**
77  * struct optee - main service struct
78  * @supp_teedev:	supplicant device
79  * @teedev:		client device
80  * @invoke_fn:		function to issue smc or hvc
81  * @call_queue:		queue of threads waiting to call @invoke_fn
82  * @wait_queue:		queue of threads from secure world waiting for a
83  *			secure world sync object
84  * @supp:		supplicant synchronization struct for RPC to supplicant
85  * @pool:		shared memory pool
86  * @memremaped_shm	virtual address of memory in shared memory pool
87  * @sec_caps:		secure world capabilities defined by
88  *			OPTEE_SMC_SEC_CAP_* in optee_smc.h
89  */
90 struct optee {
91 	struct tee_device *supp_teedev;
92 	struct tee_device *teedev;
93 	optee_invoke_fn *invoke_fn;
94 	struct optee_call_queue call_queue;
95 	struct optee_wait_queue wait_queue;
96 	struct optee_supp supp;
97 	struct tee_shm_pool *pool;
98 	void *memremaped_shm;
99 	u32 sec_caps;
100 };
101 
102 struct optee_session {
103 	struct list_head list_node;
104 	u32 session_id;
105 };
106 
107 struct optee_context_data {
108 	/* Serializes access to this struct */
109 	struct mutex mutex;
110 	struct list_head sess_list;
111 };
112 
113 struct optee_rpc_param {
114 	u32	a0;
115 	u32	a1;
116 	u32	a2;
117 	u32	a3;
118 	u32	a4;
119 	u32	a5;
120 	u32	a6;
121 	u32	a7;
122 };
123 
124 /* Holds context that is preserved during one STD call */
125 struct optee_call_ctx {
126 	/* information about pages list used in last allocation */
127 	void *pages_list;
128 	size_t num_entries;
129 };
130 
131 void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param,
132 		      struct optee_call_ctx *call_ctx);
133 void optee_rpc_finalize_call(struct optee_call_ctx *call_ctx);
134 
135 void optee_wait_queue_init(struct optee_wait_queue *wq);
136 void optee_wait_queue_exit(struct optee_wait_queue *wq);
137 
138 u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
139 			struct tee_param *param);
140 
141 int optee_supp_read(struct tee_context *ctx, void __user *buf, size_t len);
142 int optee_supp_write(struct tee_context *ctx, void __user *buf, size_t len);
143 void optee_supp_init(struct optee_supp *supp);
144 void optee_supp_uninit(struct optee_supp *supp);
145 void optee_supp_release(struct optee_supp *supp);
146 
147 int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
148 		    struct tee_param *param);
149 int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
150 		    struct tee_param *param);
151 
152 u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg);
153 int optee_open_session(struct tee_context *ctx,
154 		       struct tee_ioctl_open_session_arg *arg,
155 		       struct tee_param *param);
156 int optee_close_session(struct tee_context *ctx, u32 session);
157 int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
158 		      struct tee_param *param);
159 int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
160 
161 void optee_enable_shm_cache(struct optee *optee);
162 void optee_disable_shm_cache(struct optee *optee);
163 
164 int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
165 		       struct page **pages, size_t num_pages,
166 		       unsigned long start);
167 int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm);
168 
169 int optee_shm_register_supp(struct tee_context *ctx, struct tee_shm *shm,
170 			    struct page **pages, size_t num_pages,
171 			    unsigned long start);
172 int optee_shm_unregister_supp(struct tee_context *ctx, struct tee_shm *shm);
173 
174 int optee_from_msg_param(struct tee_param *params, size_t num_params,
175 			 const struct optee_msg_param *msg_params);
176 int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
177 		       const struct tee_param *params);
178 
179 u64 *optee_allocate_pages_list(size_t num_entries);
180 void optee_free_pages_list(void *array, size_t num_entries);
181 void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
182 			   size_t page_offset);
183 
184 /*
185  * Small helpers
186  */
187 
188 static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)
189 {
190 	return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);
191 }
192 
193 static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
194 {
195 	*reg0 = val >> 32;
196 	*reg1 = val;
197 }
198 
199 #endif /*OPTEE_PRIVATE_H*/
200