1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2023, Arm Limited
*/
#include <linux/arm_ffa.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/limits.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/tee_core.h>
#include <linux/types.h>
#include <linux/uuid.h>
#include <linux/xarray.h>
#include "tstee_private.h"
#define FFA_DIRECT_REQ_ARG_NUM 5
#define FFA_INVALID_MEM_HANDLE U64_MAX
static void arg_list_to_ffa_data(const u32 *args,
struct ffa_send_direct_data *data)
{
data->data0 = args[0];
data->data1 = args[1];
data->data2 = args[2];
data->data3 = args[3];
data->data4 = args[4];
}
static void arg_list_from_ffa_data(const struct ffa_send_direct_data *data,
u32 *args)
{
args[0] = lower_32_bits(data->data0);
args[1] = lower_32_bits(data->data1);
args[2] = lower_32_bits(data->data2);
args[3] = lower_32_bits(data->data3);
args[4] = lower_32_bits(data->data4);
}
static void tstee_get_version(struct tee_device *teedev,
struct tee_ioctl_version_data *vers)
{
struct tstee *tstee = tee_get_drvdata(teedev);
struct tee_ioctl_version_data v = {
.impl_id = TEE_IMPL_ID_TSTEE,
/* FF-A endpoint ID only uses the lower 16 bits */
.impl_caps = lower_16_bits(tstee->ffa_dev->vm_id),
.gen_caps = 0,
};
*vers = v;
}
static int tstee_open(struct tee_context *ctx)
{
struct ts_context_data *ctxdata;
ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
if (!ctxdata)
return -ENOMEM;
xa_init_flags(&ctxdata->sess_list, XA_FLAGS_ALLOC);
ctx->data = ctxdata;
return 0;
}
static void tstee_release(struct tee_context *ctx)
{
struct ts_context_data *ctxdata = ctx->data;
struct ts_session *sess;
unsigned long idx;
if (!ctxdata)
return;
xa_for_each(&ctxdata->sess_list, idx, sess) {
xa_erase(&ctxdata->sess_list, idx);
kfree(sess);
}
xa_destroy(&ctxdata->sess_list);
kfree(ctxdata);
ctx->data = NULL;
}
static int tstee_open_session(struct tee_context *ctx,
struct tee_ioctl_open_session_arg *arg,
struct tee_param *param __always_unused)
{
struct tstee *tstee = tee_get_drvdata(ctx->teedev);
struct ffa_device *ffa_dev = tstee->ffa_dev;
struct ts_context_data *ctxdata = ctx->data;
struct ffa_send_direct_data ffa_data;
struct ts_session *sess = NULL;
u32 ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
u32 sess_id;
int rc;
ffa_args[TS_RPC_CTRL_REG] =
TS_RPC_CTRL_PACK_IFACE_OPCODE(TS_RPC_MGMT_IFACE_ID,
TS_RPC_OP_SERVICE_INFO);
memcpy(ffa_args + TS_RPC_SERVICE_INFO_UUID0, arg->uuid, UUID_SIZE);
arg_list_to_ffa_data(ffa_args, &ffa_data);
rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data);
if (rc)
return rc;
arg_list_from_ffa_data(&ffa_data, ffa_args);
if (ffa_args[TS_RPC_SERVICE_INFO_RPC_STATUS] != TS_RPC_OK)
return -ENODEV;
if (ffa_args[TS_RPC_SERVICE_INFO_IFACE] > U8_MAX)
return -EINVAL;
sess = kzalloc(sizeof(*sess), GFP_KERNEL);
if (!sess)
return -ENOMEM;
sess->iface_id = ffa_args[TS_RPC_SERVICE_INFO_IFACE];
rc = xa_alloc(&ctxdata->sess_list, &sess_id, sess, xa_limit_32b,
GFP_KERNEL);
if (rc) {
kfree(sess);
return rc;
}
arg->session = sess_id;
arg->ret = 0;
return 0;
}
static int tstee_close_session(struct tee_context *ctx, u32 session)
{
struct ts_context_data *ctxdata = ctx->data;
struct ts_session *sess;
/* Calls xa_lock() internally */
sess = xa_erase(&ctxdata->sess_list, session);
if (!sess)
return -EINVAL;
kfree(sess);
return 0;
}
static int tstee_invoke_func(struct tee_context *ctx,
struct tee_ioctl_invoke_arg *arg,
struct tee_param *param)
{
struct tstee *tstee = tee_get_drvdata(ctx->teedev);
struct ffa_device *ffa_dev = tstee->ffa_dev;
struct ts_context_data *ctxdata = ctx->data;
struct ffa_send_direct_data ffa_data;
struct tee_shm *shm = NULL;
struct ts_session *sess;
u32 req_len, ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
int shm_id, rc;
u8 iface_id;
u64 handle;
u16 opcode;
xa_lock(&ctxdata->sess_list);
sess = xa_load(&ctxdata->sess_list, arg->session);
/*
* Do this while holding the lock to make sure that the session wasn't
* closed meanwhile
*/
if (sess)
iface_id = sess->iface_id;
xa_unlock(&ctxdata->sess_list);
if (!sess)
return -EINVAL;
opcode = lower_16_bits(arg->func);
shm_id = lower_32_bits(param[0].u.value.a);
req_len = lower_32_bits(param[0].u.value.b);
if (shm_id != 0) {
shm = tee_shm_get_from_id(ctx, shm_id);
if (IS_ERR(shm))
return PTR_ERR(shm);
if (shm->size < req_len) {
dev_err(&ffa_dev->dev,
"request doesn't fit into shared memory buffer\n");
rc = -EINVAL;
goto out;
}
handle = shm->sec_world_id;
} else {
handle = FFA_INVALID_MEM_HANDLE;
}
ffa_args[TS_RPC_CTRL_REG] = TS_RPC_CTRL_PACK_IFACE_OPCODE(iface_id,
opcode);
ffa_args[TS_RPC_SERVICE_MEM_HANDLE_LSW] = lower_32_bits(handle);
ffa_args[TS_RPC_SERVICE_MEM_HANDLE_MSW] = upper_32_bits(handle);
ffa_args[TS_RPC_SERVICE_REQ_LEN] = req_len;
ffa_args[TS_RPC_SERVICE_CLIENT_ID] = 0;
arg_list_to_ffa_data(ffa_args, &ffa_data);
rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data);
if (rc)
goto out;
arg_list_from_ffa_data(&ffa_data, ffa_args);
if (ffa_args[TS_RPC_SERVICE_RPC_STATUS] != TS_RPC_OK) {
dev_err(&ffa_dev->dev, "invoke_func rpc status: %d\n",
ffa_args[TS_RPC_SERVICE_RPC_STATUS]);
rc = -EINVAL;
goto out;
}
arg->ret = ffa_args[TS_RPC_SERVICE_STATUS];
if (shm && shm->size >= ffa_args[TS_RPC_SERVICE_RESP_LEN])
param[0].u.value.a = ffa_args[TS_RPC_SERVICE_RESP_LEN];
out:
if (shm)
tee_shm_put(shm);
return rc;
}
static int tstee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
struct page **pages, size_t num_pages,
unsigned long start __always_unused)
{
struct tstee *tstee = tee_get_drvdata(ctx->teedev);
struct ffa_device *ffa_dev = tstee->ffa_dev;
struct ffa_mem_region_attributes mem_attr = {
.receiver = tstee->ffa_dev->vm_id,
.attrs = FFA_MEM_RW,
.flag = 0,
};
struct ffa_mem_ops_args mem_args = {
.attrs = &mem_attr,
.use_txbuf = true,
.nattrs = 1,
.flags = 0,
};
struct ffa_send_direct_data ffa_data;
struct sg_table sgt;
u32 ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
int rc;
rc = sg_alloc_table_from_pages(&sgt, pages, num_pages, 0,
num_pages * PAGE_SIZE, GFP_KERNEL);
if (rc)
return rc;
mem_args.sg = sgt.sgl;
rc = ffa_dev->ops->mem_ops->memory_share(&mem_args);
sg_free_table(&sgt);
if (rc)
return rc;
shm->sec_world_id = mem_args.g_handle;
ffa_args[TS_RPC_CTRL_REG] =
TS_RPC_CTRL_PACK_IFACE_OPCODE(TS_RPC_MGMT_IFACE_ID,
TS_RPC_OP_RETRIEVE_MEM);
ffa_args[TS_RPC_RETRIEVE_MEM_HANDLE_LSW] =
lower_32_bits(shm->sec_world_id);
ffa_args[TS_RPC_RETRIEVE_MEM_HANDLE_MSW] =
upper_32_bits(shm->sec_world_id);
ffa_args[TS_RPC_RETRIEVE_MEM_TAG_LSW] = 0;
ffa_args[TS_RPC_RETRIEVE_MEM_TAG_MSW] = 0;
arg_list_to_ffa_data(ffa_args, &ffa_data);
rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data);
if (rc) {
(void)ffa_dev->ops->mem_ops->memory_reclaim(shm->sec_world_id,
0);
return rc;
}
arg_list_from_ffa_data(&ffa_data, ffa_args);
if (ffa_args[TS_RPC_RETRIEVE_MEM_RPC_STATUS] != TS_RPC_OK) {
dev_err(&ffa_dev->dev, "shm_register rpc status: %d\n",
ffa_args[TS_RPC_RETRIEVE_MEM_RPC_STATUS]);
ffa_dev->ops->mem_ops->memory_reclaim(shm->sec_world_id, 0);
return -EINVAL;
}
return 0;
}
static int tstee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm)
{
struct tstee *tstee = tee_get_drvdata(ctx->teedev);
struct ffa_device *ffa_dev = tstee->ffa_dev;
struct ffa_send_direct_data ffa_data;
u32 ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
int rc;
ffa_args[TS_RPC_CTRL_REG] =
TS_RPC_CTRL_PACK_IFACE_OPCODE(TS_RPC_MGMT_IFACE_ID,
TS_RPC_OP_RELINQ_MEM);
ffa_args[TS_RPC_RELINQ_MEM_HANDLE_LSW] =
lower_32_bits(shm->sec_world_id);
ffa_args[TS_RPC_RELINQ_MEM_HANDLE_MSW] =
upper_32_bits(shm->sec_world_id);
arg_list_to_ffa_data(ffa_args, &ffa_data);
rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data);
if (rc)
return rc;
arg_list_from_ffa_data(&ffa_data, ffa_args);
if (ffa_args[TS_RPC_RELINQ_MEM_RPC_STATUS] != TS_RPC_OK) {
dev_err(&ffa_dev->dev, "shm_unregister rpc status: %d\n",
ffa_args[TS_RPC_RELINQ_MEM_RPC_STATUS]);
return -EINVAL;
}
rc = ffa_dev->ops->mem_ops->memory_reclaim(shm->sec_world_id, 0);
return rc;
}
static const struct tee_driver_ops tstee_ops = {
.get_version = tstee_get_version,
.open = tstee_open,
.release = tstee_release,
.open_session = tstee_open_session,
.close_session = tstee_close_session,
.invoke_func = tstee_invoke_func,
};
static const struct tee_desc tstee_desc = {
.name = "tstee-clnt",
.ops = &tstee_ops,
.owner = THIS_MODULE,
};
static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm,
size_t size, size_t align)
{
return tee_dyn_shm_alloc_helper(shm, size, align, tstee_shm_register);
}
static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm)
{
tee_dyn_shm_free_helper(shm, tstee_shm_unregister);
}
static void pool_op_destroy_pool(struct tee_shm_pool *pool)
{
kfree(pool);
}
static const struct tee_shm_pool_ops pool_ops = {
.alloc = pool_op_alloc,
.free = pool_op_free,
.destroy_pool = pool_op_destroy_pool,
};
static struct tee_shm_pool *tstee_create_shm_pool(void)
{
struct tee_shm_pool *pool = kzalloc(sizeof(*pool), GFP_KERNEL);
if (!pool)
return ERR_PTR(-ENOMEM);
pool->ops = &pool_ops;
return pool;
}
static bool tstee_check_rpc_compatible(struct ffa_device *ffa_dev)
{
struct ffa_send_direct_data ffa_data;
u32 ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
ffa_args[TS_RPC_CTRL_REG] =
TS_RPC_CTRL_PACK_IFACE_OPCODE(TS_RPC_MGMT_IFACE_ID,
TS_RPC_OP_GET_VERSION);
arg_list_to_ffa_data(ffa_args, &ffa_data);
if (ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data))
return false;
arg_list_from_ffa_data(&ffa_data, ffa_args);
return ffa_args[TS_RPC_GET_VERSION_RESP] == TS_RPC_PROTOCOL_VERSION;
}
static int tstee_probe(struct ffa_device *ffa_dev)
{
struct tstee *tstee;
int rc;
ffa_dev->ops->msg_ops->mode_32bit_set(ffa_dev);
if (!tstee_check_rpc_compatible(ffa_dev))
return -EINVAL;
tstee = kzalloc(sizeof(*tstee), GFP_KERNEL);
if (!tstee)
return -ENOMEM;
tstee->ffa_dev = ffa_dev;
tstee->pool = tstee_create_shm_pool();
if (IS_ERR(tstee->pool)) {
rc = PTR_ERR(tstee->pool);
tstee->pool = NULL;
goto err_free_tstee;
}
tstee->teedev = tee_device_alloc(&tstee_desc, NULL, tstee->pool, tstee);
if (IS_ERR(tstee->teedev)) {
rc = PTR_ERR(tstee->teedev);
tstee->teedev = NULL;
goto err_free_pool;
}
rc = tee_device_register(tstee->teedev);
if (rc)
goto err_unreg_teedev;
ffa_dev_set_drvdata(ffa_dev, tstee);
return 0;
err_unreg_teedev:
tee_device_unregister(tstee->teedev);
err_free_pool:
tee_shm_pool_free(tstee->pool);
err_free_tstee:
kfree(tstee);
return rc;
}
static void tstee_remove(struct ffa_device *ffa_dev)
{
struct tstee *tstee = ffa_dev->dev.driver_data;
tee_device_unregister(tstee->teedev);
tee_shm_pool_free(tstee->pool);
kfree(tstee);
}
static const struct ffa_device_id tstee_device_ids[] = {
/* TS RPC protocol UUID: bdcd76d7-825e-4751-963b-86d4f84943ac */
{ TS_RPC_UUID },
{}
};
static struct ffa_driver tstee_driver = {
.name = "arm_tstee",
.probe = tstee_probe,
.remove = tstee_remove,
.id_table = tstee_device_ids,
};
module_ffa_driver(tstee_driver);
MODULE_AUTHOR("Balint Dobszay <balint.dobszay@arm.com>");
MODULE_DESCRIPTION("Arm Trusted Services TEE driver");
MODULE_LICENSE("GPL");
|