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