blob: 35e79386c556bca1d2819c745d415cc50f63fa74 [file] [log] [blame]
Jens Wiklander18ebb2f2015-04-14 14:33:20 +02001/*
2 * Copyright (c) 2015, Linaro Limited
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
15#ifndef OPTEE_PRIVATE_H
16#define OPTEE_PRIVATE_H
17
18#include <linux/arm-smccc.h>
19#include <linux/semaphore.h>
20#include <linux/tee_drv.h>
21#include <linux/types.h>
22#include "optee_msg.h"
23
24#define OPTEE_MAX_ARG_SIZE 1024
25
26/* Some Global Platform error codes used in this driver */
27#define TEEC_SUCCESS 0x00000000
28#define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006
29#define TEEC_ERROR_COMMUNICATION 0xFFFF000E
30#define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C
31
32#define TEEC_ORIGIN_COMMS 0x00000002
33
34typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,
35 unsigned long, unsigned long, unsigned long,
36 unsigned long, unsigned long,
37 struct arm_smccc_res *);
38
39struct 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 Wiklanderad675fa2016-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 Wiklander18ebb2f2015-04-14 14:33:20 +020064 */
65struct optee_supp {
Jens Wiklanderad675fa2016-12-23 13:13:39 +010066 /* Serializes access to this struct */
67 struct mutex mutex;
Jens Wiklander18ebb2f2015-04-14 14:33:20 +020068 struct tee_context *ctx;
Jens Wiklander18ebb2f2015-04-14 14:33:20 +020069
Jens Wiklanderad675fa2016-12-23 13:13:39 +010070 int req_id;
71 struct list_head reqs;
72 struct idr idr;
73 struct completion reqs_c;
Jens Wiklander18ebb2f2015-04-14 14:33:20 +020074};
75
76/**
77 * struct optee - main service struct
78 * @supp_teedev: supplicant device
79 * @teedev: client device
80 * @invoke_fn: function to issue smc or hvc
81 * @call_queue: queue of threads waiting to call @invoke_fn
82 * @wait_queue: queue of threads from secure world waiting for a
83 * secure world sync object
84 * @supp: supplicant synchronization struct for RPC to supplicant
85 * @pool: shared memory pool
86 * @memremaped_shm virtual address of memory in shared memory pool
Volodymyr Babchuk8fecf082017-11-29 14:48:34 +020087 * @sec_caps: secure world capabilities defined by
88 * OPTEE_SMC_SEC_CAP_* in optee_smc.h
Jens Wiklander18ebb2f2015-04-14 14:33:20 +020089 */
90struct optee {
91 struct tee_device *supp_teedev;
92 struct tee_device *teedev;
93 optee_invoke_fn *invoke_fn;
94 struct optee_call_queue call_queue;
95 struct optee_wait_queue wait_queue;
96 struct optee_supp supp;
97 struct tee_shm_pool *pool;
98 void *memremaped_shm;
Volodymyr Babchuk8fecf082017-11-29 14:48:34 +020099 u32 sec_caps;
Jens Wiklander18ebb2f2015-04-14 14:33:20 +0200100};
101
102struct optee_session {
103 struct list_head list_node;
104 u32 session_id;
105};
106
107struct optee_context_data {
108 /* Serializes access to this struct */
109 struct mutex mutex;
110 struct list_head sess_list;
111};
112
113struct optee_rpc_param {
114 u32 a0;
115 u32 a1;
116 u32 a2;
117 u32 a3;
118 u32 a4;
119 u32 a5;
120 u32 a6;
121 u32 a7;
122};
123
Volodymyr Babchukca453512017-11-29 14:48:33 +0200124/* Holds context that is preserved during one STD call */
125struct optee_call_ctx {
126 /* information about pages list used in last allocation */
127 void *pages_list;
128 size_t num_entries;
129};
130
131void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param,
132 struct optee_call_ctx *call_ctx);
133void optee_rpc_finalize_call(struct optee_call_ctx *call_ctx);
Jens Wiklander18ebb2f2015-04-14 14:33:20 +0200134
135void optee_wait_queue_init(struct optee_wait_queue *wq);
136void optee_wait_queue_exit(struct optee_wait_queue *wq);
137
138u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
139 struct tee_param *param);
140
141int optee_supp_read(struct tee_context *ctx, void __user *buf, size_t len);
142int optee_supp_write(struct tee_context *ctx, void __user *buf, size_t len);
143void optee_supp_init(struct optee_supp *supp);
144void optee_supp_uninit(struct optee_supp *supp);
Jens Wiklanderad675fa2016-12-23 13:13:39 +0100145void optee_supp_release(struct optee_supp *supp);
Jens Wiklander18ebb2f2015-04-14 14:33:20 +0200146
147int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
148 struct tee_param *param);
149int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
150 struct tee_param *param);
151
152u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg);
153int optee_open_session(struct tee_context *ctx,
154 struct tee_ioctl_open_session_arg *arg,
155 struct tee_param *param);
156int optee_close_session(struct tee_context *ctx, u32 session);
157int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
158 struct tee_param *param);
159int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
160
161void optee_enable_shm_cache(struct optee *optee);
162void optee_disable_shm_cache(struct optee *optee);
163
Volodymyr Babchuk88be5b82017-11-29 14:48:31 +0200164int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
Jens Wiklanderce42b242017-12-28 10:08:00 +0100165 struct page **pages, size_t num_pages,
166 unsigned long start);
Volodymyr Babchuk88be5b82017-11-29 14:48:31 +0200167int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm);
168
Volodymyr Babchukca453512017-11-29 14:48:33 +0200169int optee_shm_register_supp(struct tee_context *ctx, struct tee_shm *shm,
Jens Wiklanderce42b242017-12-28 10:08:00 +0100170 struct page **pages, size_t num_pages,
171 unsigned long start);
Volodymyr Babchukca453512017-11-29 14:48:33 +0200172int optee_shm_unregister_supp(struct tee_context *ctx, struct tee_shm *shm);
173
Jens Wiklander18ebb2f2015-04-14 14:33:20 +0200174int optee_from_msg_param(struct tee_param *params, size_t num_params,
175 const struct optee_msg_param *msg_params);
176int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
177 const struct tee_param *params);
178
Volodymyr Babchuk9cba6e72017-11-29 14:48:30 +0200179u64 *optee_allocate_pages_list(size_t num_entries);
180void optee_free_pages_list(void *array, size_t num_entries);
181void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
182 size_t page_offset);
183
Jens Wiklander18ebb2f2015-04-14 14:33:20 +0200184/*
185 * Small helpers
186 */
187
188static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)
189{
190 return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);
191}
192
193static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
194{
195 *reg0 = val >> 32;
196 *reg1 = val;
197}
198
199#endif /*OPTEE_PRIVATE_H*/