blob: 8b71839a357ede5683651b1eee89eb4a69360b6a [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/*
3 * Copyright (c) 2015, 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
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
Sumit Gargc3fa24a2019-01-29 11:19:37 +053022#define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020023
24#define TEEC_ORIGIN_COMMS 0x00000002
25
26typedef 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
31struct optee_call_queue {
32 /* Serializes access to this struct */
33 struct mutex mutex;
34 struct list_head waiters;
35};
36
37struct 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
Jens Wiklander1647a5a2016-12-23 13:13:39 +010048 * @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.
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020056 */
57struct optee_supp {
Jens Wiklander1647a5a2016-12-23 13:13:39 +010058 /* Serializes access to this struct */
59 struct mutex mutex;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020060 struct tee_context *ctx;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020061
Jens Wiklander1647a5a2016-12-23 13:13:39 +010062 int req_id;
63 struct list_head reqs;
64 struct idr idr;
65 struct completion reqs_c;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020066};
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
Volodymyr Babchukd885cc52017-11-29 14:48:34 +020079 * @sec_caps: secure world capabilities defined by
80 * OPTEE_SMC_SEC_CAP_* in optee_smc.h
Maxim Uvarov5f178bb72020-06-18 16:52:50 +030081 * @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
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020084 */
85struct 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;
Volodymyr Babchukd885cc52017-11-29 14:48:34 +020094 u32 sec_caps;
Maxim Uvarov5f178bb72020-06-18 16:52:50 +030095 bool scan_bus_done;
96 struct workqueue_struct *scan_bus_wq;
97 struct work_struct scan_bus_work;
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +020098};
99
100struct optee_session {
101 struct list_head list_node;
102 u32 session_id;
103};
104
105struct optee_context_data {
106 /* Serializes access to this struct */
107 struct mutex mutex;
108 struct list_head sess_list;
109};
110
111struct 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
Volodymyr Babchuk53a107c2017-11-29 14:48:33 +0200122/* Holds context that is preserved during one STD call */
123struct optee_call_ctx {
124 /* information about pages list used in last allocation */
125 void *pages_list;
126 size_t num_entries;
127};
128
129void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param,
130 struct optee_call_ctx *call_ctx);
131void optee_rpc_finalize_call(struct optee_call_ctx *call_ctx);
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200132
133void optee_wait_queue_init(struct optee_wait_queue *wq);
134void optee_wait_queue_exit(struct optee_wait_queue *wq);
135
136u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
137 struct tee_param *param);
138
139int optee_supp_read(struct tee_context *ctx, void __user *buf, size_t len);
140int optee_supp_write(struct tee_context *ctx, void __user *buf, size_t len);
141void optee_supp_init(struct optee_supp *supp);
142void optee_supp_uninit(struct optee_supp *supp);
Jens Wiklander1647a5a2016-12-23 13:13:39 +0100143void optee_supp_release(struct optee_supp *supp);
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200144
145int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
146 struct tee_param *param);
147int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
148 struct tee_param *param);
149
150u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg);
151int optee_open_session(struct tee_context *ctx,
152 struct tee_ioctl_open_session_arg *arg,
153 struct tee_param *param);
154int optee_close_session(struct tee_context *ctx, u32 session);
155int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
156 struct tee_param *param);
157int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
158
159void optee_enable_shm_cache(struct optee *optee);
160void optee_disable_shm_cache(struct optee *optee);
161
Volodymyr Babchuk06ca7912017-11-29 14:48:31 +0200162int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
Jens Wiklander95ffe4c2017-12-28 10:08:00 +0100163 struct page **pages, size_t num_pages,
164 unsigned long start);
Volodymyr Babchuk06ca7912017-11-29 14:48:31 +0200165int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm);
166
Volodymyr Babchuk53a107c2017-11-29 14:48:33 +0200167int optee_shm_register_supp(struct tee_context *ctx, struct tee_shm *shm,
Jens Wiklander95ffe4c2017-12-28 10:08:00 +0100168 struct page **pages, size_t num_pages,
169 unsigned long start);
Volodymyr Babchuk53a107c2017-11-29 14:48:33 +0200170int optee_shm_unregister_supp(struct tee_context *ctx, struct tee_shm *shm);
171
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200172int optee_from_msg_param(struct tee_param *params, size_t num_params,
173 const struct optee_msg_param *msg_params);
174int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
175 const struct tee_param *params);
176
Volodymyr Babchuk3bb48ba2017-11-29 14:48:30 +0200177u64 *optee_allocate_pages_list(size_t num_entries);
178void optee_free_pages_list(void *array, size_t num_entries);
179void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
180 size_t page_offset);
181
Maxim Uvarov5f178bb72020-06-18 16:52:50 +0300182#define PTA_CMD_GET_DEVICES 0x0
183#define PTA_CMD_GET_DEVICES_SUPP 0x1
184int optee_enumerate_devices(u32 func);
Sumit Gargc3fa24a2019-01-29 11:19:37 +0530185
Jens Wiklander4fb0a5e2015-04-14 14:33:20 +0200186/*
187 * Small helpers
188 */
189
190static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)
191{
192 return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);
193}
194
195static 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*/