blob: 40af6b059b20c6c58bff36631c8c610025b1e458 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +02002/*
Jens Wiklander4602c582021-03-25 15:08:50 +01003 * Copyright (c) 2015-2021, Linaro Limited
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +02004 */
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
Jens Wiklanderc51a5642021-07-21 16:30:28 +020015#define DRIVER_NAME "optee"
16
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020017#define OPTEE_MAX_ARG_SIZE 1024
18
19/* Some Global Platform error codes used in this driver */
20#define TEEC_SUCCESS 0x00000000
21#define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006
Jorge Ramirez-Ortizc05210a2020-08-14 13:12:21 +020022#define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020023#define TEEC_ERROR_COMMUNICATION 0xFFFF000E
24#define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C
Sumit Gargc3fa24a2019-01-29 11:19:37 +053025#define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020026
27#define TEEC_ORIGIN_COMMS 0x00000002
28
29typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,
30 unsigned long, unsigned long, unsigned long,
31 unsigned long, unsigned long,
32 struct arm_smccc_res *);
33
Jens Wiklanderc51a5642021-07-21 16:30:28 +020034struct optee_call_waiter {
35 struct list_head list_node;
36 struct completion c;
37};
38
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020039struct optee_call_queue {
40 /* Serializes access to this struct */
41 struct mutex mutex;
42 struct list_head waiters;
43};
44
45struct 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
Jens Wiklander1647a5a2016-12-23 13:13:39 +010056 * @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.
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020064 */
65struct optee_supp {
Jens Wiklander1647a5a2016-12-23 13:13:39 +010066 /* Serializes access to this struct */
67 struct mutex mutex;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020068 struct tee_context *ctx;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020069
Jens Wiklander1647a5a2016-12-23 13:13:39 +010070 int req_id;
71 struct list_head reqs;
72 struct idr idr;
73 struct completion reqs_c;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020074};
75
Jens Wiklanderc51a5642021-07-21 16:30:28 +020076/**
77 * struct optee_smc - SMC ABI specifics
78 * @invoke_fn: function to issue smc or hvc
79 * @memremaped_shm virtual address of memory in shared memory pool
80 * @sec_caps: secure world capabilities defined by
81 * OPTEE_SMC_SEC_CAP_* in optee_smc.h
82 */
83struct optee_smc {
84 optee_invoke_fn *invoke_fn;
85 void *memremaped_shm;
86 u32 sec_caps;
87};
88
Jens Wiklander4602c582021-03-25 15:08:50 +010089struct optee;
90
91/**
92 * struct optee_ops - OP-TEE driver internal operations
93 * @do_call_with_arg: enters OP-TEE in secure world
94 * @to_msg_param: converts from struct tee_param to OPTEE_MSG parameters
95 * @from_msg_param: converts from OPTEE_MSG parameters to struct tee_param
96 *
97 * These OPs are only supposed to be used internally in the OP-TEE driver
98 * as a way of abstracting the different methogs of entering OP-TEE in
99 * secure world.
100 */
101struct optee_ops {
102 int (*do_call_with_arg)(struct tee_context *ctx,
103 struct tee_shm *shm_arg);
104 int (*to_msg_param)(struct optee *optee,
105 struct optee_msg_param *msg_params,
106 size_t num_params, const struct tee_param *params);
107 int (*from_msg_param)(struct optee *optee, struct tee_param *params,
108 size_t num_params,
109 const struct optee_msg_param *msg_params);
110};
111
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200112/**
113 * struct optee - main service struct
114 * @supp_teedev: supplicant device
Jens Wiklander4602c582021-03-25 15:08:50 +0100115 * @ops: internal callbacks for different ways to reach secure
116 * world
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200117 * @teedev: client device
Jens Wiklanderc51a5642021-07-21 16:30:28 +0200118 * @smc: specific to SMC ABI
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200119 * @call_queue: queue of threads waiting to call @invoke_fn
120 * @wait_queue: queue of threads from secure world waiting for a
121 * secure world sync object
122 * @supp: supplicant synchronization struct for RPC to supplicant
123 * @pool: shared memory pool
Maxim Uvarov5f178bb72020-06-18 16:52:50 +0300124 * @scan_bus_done flag if device registation was already done.
125 * @scan_bus_wq workqueue to scan optee bus and register optee drivers
126 * @scan_bus_work workq to scan optee bus and register optee drivers
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200127 */
128struct optee {
129 struct tee_device *supp_teedev;
130 struct tee_device *teedev;
Jens Wiklander4602c582021-03-25 15:08:50 +0100131 const struct optee_ops *ops;
Jens Wiklanderc51a5642021-07-21 16:30:28 +0200132 struct optee_smc smc;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200133 struct optee_call_queue call_queue;
134 struct optee_wait_queue wait_queue;
135 struct optee_supp supp;
136 struct tee_shm_pool *pool;
Maxim Uvarov5f178bb72020-06-18 16:52:50 +0300137 bool scan_bus_done;
138 struct workqueue_struct *scan_bus_wq;
139 struct work_struct scan_bus_work;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200140};
141
142struct optee_session {
143 struct list_head list_node;
144 u32 session_id;
145};
146
147struct optee_context_data {
148 /* Serializes access to this struct */
149 struct mutex mutex;
150 struct list_head sess_list;
151};
152
153struct optee_rpc_param {
154 u32 a0;
155 u32 a1;
156 u32 a2;
157 u32 a3;
158 u32 a4;
159 u32 a5;
160 u32 a6;
161 u32 a7;
162};
163
Volodymyr Babchuk53a107c2017-11-29 14:48:33 +0200164/* Holds context that is preserved during one STD call */
165struct optee_call_ctx {
166 /* information about pages list used in last allocation */
167 void *pages_list;
168 size_t num_entries;
169};
170
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200171void optee_wait_queue_init(struct optee_wait_queue *wq);
172void optee_wait_queue_exit(struct optee_wait_queue *wq);
173
174u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
175 struct tee_param *param);
176
177int optee_supp_read(struct tee_context *ctx, void __user *buf, size_t len);
178int optee_supp_write(struct tee_context *ctx, void __user *buf, size_t len);
179void optee_supp_init(struct optee_supp *supp);
180void optee_supp_uninit(struct optee_supp *supp);
Jens Wiklander1647a5a2016-12-23 13:13:39 +0100181void optee_supp_release(struct optee_supp *supp);
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200182
183int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
184 struct tee_param *param);
185int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
186 struct tee_param *param);
187
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200188int optee_open_session(struct tee_context *ctx,
189 struct tee_ioctl_open_session_arg *arg,
190 struct tee_param *param);
Jens Wiklanderc0ab6db2021-03-25 15:08:46 +0100191int optee_close_session_helper(struct tee_context *ctx, u32 session);
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200192int optee_close_session(struct tee_context *ctx, u32 session);
193int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
194 struct tee_param *param);
195int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
196
Maxim Uvarov5f178bb72020-06-18 16:52:50 +0300197#define PTA_CMD_GET_DEVICES 0x0
198#define PTA_CMD_GET_DEVICES_SUPP 0x1
199int optee_enumerate_devices(u32 func);
Sumit Garg7f565d02021-10-12 13:01:16 +0530200void optee_unregister_devices(void);
Sumit Gargc3fa24a2019-01-29 11:19:37 +0530201
Jens Wiklanderc51a5642021-07-21 16:30:28 +0200202int optee_pool_op_alloc_helper(struct tee_shm_pool_mgr *poolm,
203 struct tee_shm *shm, size_t size,
204 int (*shm_register)(struct tee_context *ctx,
205 struct tee_shm *shm,
206 struct page **pages,
207 size_t num_pages,
208 unsigned long start));
209
210
211void optee_remove_common(struct optee *optee);
212int optee_open(struct tee_context *ctx, bool cap_memref_null);
213void optee_release(struct tee_context *ctx);
214void optee_release_supp(struct tee_context *ctx);
215
216static inline void optee_from_msg_param_value(struct tee_param *p, u32 attr,
217 const struct optee_msg_param *mp)
218{
219 p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT +
220 attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
221 p->u.value.a = mp->u.value.a;
222 p->u.value.b = mp->u.value.b;
223 p->u.value.c = mp->u.value.c;
224}
225
226static inline void optee_to_msg_param_value(struct optee_msg_param *mp,
227 const struct tee_param *p)
228{
229 mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -
230 TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
231 mp->u.value.a = p->u.value.a;
232 mp->u.value.b = p->u.value.b;
233 mp->u.value.c = p->u.value.c;
234}
235
236void optee_cq_wait_init(struct optee_call_queue *cq,
237 struct optee_call_waiter *w);
238void optee_cq_wait_for_completion(struct optee_call_queue *cq,
239 struct optee_call_waiter *w);
240void optee_cq_wait_final(struct optee_call_queue *cq,
241 struct optee_call_waiter *w);
242int optee_check_mem_type(unsigned long start, size_t num_pages);
243struct tee_shm *optee_get_msg_arg(struct tee_context *ctx, size_t num_params,
244 struct optee_msg_arg **msg_arg);
245
246struct tee_shm *optee_rpc_cmd_alloc_suppl(struct tee_context *ctx, size_t sz);
247void optee_rpc_cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm);
248void optee_rpc_cmd(struct tee_context *ctx, struct optee *optee,
249 struct optee_msg_arg *arg);
250
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200251/*
252 * Small helpers
253 */
254
255static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)
256{
257 return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);
258}
259
260static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
261{
262 *reg0 = val >> 32;
263 *reg1 = val;
264}
265
Jens Wiklanderc51a5642021-07-21 16:30:28 +0200266/* Registration of the ABIs */
267int optee_smc_abi_register(void);
268void optee_smc_abi_unregister(void);
269
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200270#endif /*OPTEE_PRIVATE_H*/