xref: /linux/drivers/tee/optee/optee_private.h (revision b7019ac550eb3916f34d79db583e9b7ea2524afa)
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  */
82 struct optee {
83 	struct tee_device *supp_teedev;
84 	struct tee_device *teedev;
85 	optee_invoke_fn *invoke_fn;
86 	struct optee_call_queue call_queue;
87 	struct optee_wait_queue wait_queue;
88 	struct optee_supp supp;
89 	struct tee_shm_pool *pool;
90 	void *memremaped_shm;
91 	u32 sec_caps;
92 };
93 
94 struct optee_session {
95 	struct list_head list_node;
96 	u32 session_id;
97 };
98 
99 struct optee_context_data {
100 	/* Serializes access to this struct */
101 	struct mutex mutex;
102 	struct list_head sess_list;
103 };
104 
105 struct optee_rpc_param {
106 	u32	a0;
107 	u32	a1;
108 	u32	a2;
109 	u32	a3;
110 	u32	a4;
111 	u32	a5;
112 	u32	a6;
113 	u32	a7;
114 };
115 
116 /* Holds context that is preserved during one STD call */
117 struct optee_call_ctx {
118 	/* information about pages list used in last allocation */
119 	void *pages_list;
120 	size_t num_entries;
121 };
122 
123 void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param,
124 		      struct optee_call_ctx *call_ctx);
125 void optee_rpc_finalize_call(struct optee_call_ctx *call_ctx);
126 
127 void optee_wait_queue_init(struct optee_wait_queue *wq);
128 void optee_wait_queue_exit(struct optee_wait_queue *wq);
129 
130 u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
131 			struct tee_param *param);
132 
133 int optee_supp_read(struct tee_context *ctx, void __user *buf, size_t len);
134 int optee_supp_write(struct tee_context *ctx, void __user *buf, size_t len);
135 void optee_supp_init(struct optee_supp *supp);
136 void optee_supp_uninit(struct optee_supp *supp);
137 void optee_supp_release(struct optee_supp *supp);
138 
139 int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
140 		    struct tee_param *param);
141 int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
142 		    struct tee_param *param);
143 
144 u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg);
145 int optee_open_session(struct tee_context *ctx,
146 		       struct tee_ioctl_open_session_arg *arg,
147 		       struct tee_param *param);
148 int optee_close_session(struct tee_context *ctx, u32 session);
149 int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
150 		      struct tee_param *param);
151 int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
152 
153 void optee_enable_shm_cache(struct optee *optee);
154 void optee_disable_shm_cache(struct optee *optee);
155 
156 int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
157 		       struct page **pages, size_t num_pages,
158 		       unsigned long start);
159 int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm);
160 
161 int optee_shm_register_supp(struct tee_context *ctx, struct tee_shm *shm,
162 			    struct page **pages, size_t num_pages,
163 			    unsigned long start);
164 int optee_shm_unregister_supp(struct tee_context *ctx, struct tee_shm *shm);
165 
166 int optee_from_msg_param(struct tee_param *params, size_t num_params,
167 			 const struct optee_msg_param *msg_params);
168 int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
169 		       const struct tee_param *params);
170 
171 u64 *optee_allocate_pages_list(size_t num_entries);
172 void optee_free_pages_list(void *array, size_t num_entries);
173 void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
174 			   size_t page_offset);
175 
176 int optee_enumerate_devices(void);
177 
178 /*
179  * Small helpers
180  */
181 
182 static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)
183 {
184 	return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);
185 }
186 
187 static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
188 {
189 	*reg0 = val >> 32;
190 	*reg1 = val;
191 }
192 
193 #endif /*OPTEE_PRIVATE_H*/
194