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