Skip to content

Commit eb40f63

Browse files
author
Pascal Brand
committed
dma_buf support
Reviewed-by: Joakim Bech <[email protected]> Reviewed-by: Jerome Forissier <[email protected]> Tested-by: Pascal Brand <[email protected]> (STM platform) Tested-by: Pascal Brand <[email protected]> (QEMU platform) Tested-by: Jerome Forissier <[email protected]> (FVP) Signed-off-by: Pascal Brand <[email protected]>
1 parent 8cd63cb commit eb40f63

File tree

13 files changed

+920
-742
lines changed

13 files changed

+920
-742
lines changed

armtz/tee_mem.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ void *tee_shm_pool_p2v(struct device *dev, struct shm_pool *pool,
318318
} else {
319319
unsigned long offset = paddr - pool->paddr;
320320
void *p = (void *)((unsigned long)pool->vaddr + offset);
321+
321322
mutex_unlock(&pool->lock);
322323
return p;
323324
}
@@ -351,6 +352,7 @@ unsigned long tee_shm_pool_v2p(struct device *dev, struct shm_pool *pool,
351352
} else {
352353
unsigned long offset = vaddr - pool->vaddr;
353354
unsigned long p = pool->paddr + offset;
355+
354356
mutex_unlock(&pool->lock);
355357
return p;
356358
}

armtz/tee_smc-arm.S

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,3 @@ ENTRY(tee_smc_call)
2727
stm r8, {r0-r7}
2828
pop {r4-r8, pc}
2929
ENDPROC(tee_smc_call)
30-
31-

armtz/tee_tz_drv.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,26 @@ static void handle_rpc_func_cmd(struct tee_tz *ptee, u32 parg32)
257257
}
258258
}
259259

260+
static struct tee_shm *handle_rpc_alloc(struct tee_tz *ptee, size_t size)
261+
{
262+
struct tee_rpc_alloc rpc_alloc;
263+
264+
rpc_alloc.size = size;
265+
tee_supp_cmd(ptee->tee, TEE_RPC_ICMD_ALLOCATE,
266+
&rpc_alloc, sizeof(rpc_alloc));
267+
return rpc_alloc.shm;
268+
}
269+
270+
static void handle_rpc_free(struct tee_tz *ptee, struct tee_shm *shm)
271+
{
272+
struct tee_rpc_free rpc_free;
273+
274+
if (!shm)
275+
return;
276+
rpc_free.shm = shm;
277+
tee_supp_cmd(ptee->tee, TEE_RPC_ICMD_FREE, &rpc_free, sizeof(rpc_free));
278+
}
279+
260280
static u32 handle_rpc(struct tee_tz *ptee, struct smc_param *param)
261281
{
262282
struct tee_shm *shm;
@@ -278,27 +298,23 @@ static u32 handle_rpc(struct tee_tz *ptee, struct smc_param *param)
278298
/* Can't support payload shared memory with this interface */
279299
break;
280300
case TEESMC_ST_RPC_FUNC_ALLOC_PAYLOAD:
281-
shm = tee_shm_alloc_from_rpc(ptee->tee, param->a1,
282-
TEE_SHM_TEMP | TEE_SHM_FROM_RPC);
283-
if (!shm) {
301+
shm = handle_rpc_alloc(ptee, param->a1);
302+
if (IS_ERR_OR_NULL(shm)) {
284303
param->a1 = 0;
285304
break;
286305
}
287306
cookie = handle_get(&shm_handle_db, shm);
288307
if (cookie < 0) {
289-
tee_shm_free_from_rpc(shm);
308+
handle_rpc_free(ptee, shm);
290309
param->a1 = 0;
291310
break;
292311
}
293312
param->a1 = shm->paddr;
294313
param->a2 = cookie;
295314
break;
296315
case TEESMC_ST_RPC_FUNC_FREE_PAYLOAD:
297-
if (true || param->a1) {
298-
shm = handle_put(&shm_handle_db, param->a1);
299-
if (shm)
300-
tee_shm_free_from_rpc(shm);
301-
}
316+
shm = handle_put(&shm_handle_db, param->a1);
317+
handle_rpc_free(ptee, shm);
302318
break;
303319
case TEESMC_RPC_FUNC_IRQ:
304320
break;
@@ -1164,8 +1180,6 @@ static void tz_tee_deinit(struct platform_device *pdev)
11641180

11651181
dev_dbg(tee->dev, "%s: dev=%s, Secure armv7 started=%d\n", __func__,
11661182
tee->name, ptee->started);
1167-
1168-
return;
11691183
}
11701184

11711185
static int tz_tee_probe(struct platform_device *pdev)

core/tee_context.c

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
int tee_context_dump(struct tee *tee, char *buff, size_t len)
3030
{
31-
struct list_head *ptrCtx, *ptrSess, *ptrShm;
31+
struct list_head *ptr_ctx, *ptr_sess, *ptr_shm;
3232
struct tee_context *ctx;
3333
struct tee_session *sess;
3434
struct tee_shm *shm;
@@ -39,74 +39,75 @@ int tee_context_dump(struct tee *tee, char *buff, size_t len)
3939

4040
BUG_ON(!tee);
4141

42-
if (len < 80)
42+
if (len < 80 || list_empty(&tee->list_ctx))
4343
return 0;
4444

4545
mutex_lock(&tee->lock);
46-
if (!list_empty(&tee->list_ctx)) {
47-
list_for_each(ptrCtx, &tee->list_ctx) {
48-
ctx = list_entry(ptrCtx, struct tee_context, entry);
46+
47+
list_for_each(ptr_ctx, &tee->list_ctx) {
48+
ctx = list_entry(ptr_ctx, struct tee_context, entry);
49+
50+
pos += sprintf(buff + pos,
51+
"[%02d] ctx=%p (refcount=%d) (usr=%d)",
52+
i, ctx,
53+
(int)atomic_read(&ctx->refcount.
54+
refcount),
55+
ctx->usr_client);
56+
pos += sprintf(buff + pos, "name=\"%s\" (tgid=%d)\n",
57+
ctx->name,
58+
ctx->tgid);
59+
if ((len - pos) < 80) {
60+
pos = 0;
61+
goto out;
62+
}
63+
64+
if (list_empty(&ctx->list_sess))
65+
goto out;
66+
67+
j = 0;
68+
list_for_each(ptr_sess, &ctx->list_sess) {
69+
sess = list_entry(ptr_sess,
70+
struct tee_session,
71+
entry);
4972

5073
pos += sprintf(buff + pos,
51-
"[%02d] ctx=%p (refcount=%d) (usr=%d) "
52-
"name=\"%s\" (tgid=%d)\n",
53-
i, ctx,
54-
(int)atomic_read(&ctx->refcount.
55-
refcount),
56-
ctx->usr_client, ctx->name, ctx->tgid);
74+
"[%02d.%d] sess=%p sessid=%08x\n",
75+
i, j, sess,
76+
sess->sessid);
5777

5878
if ((len - pos) < 80) {
5979
pos = 0;
6080
goto out;
6181
}
6282

63-
j = 0;
64-
if (!list_empty(&ctx->list_sess)) {
65-
list_for_each(ptrSess, &ctx->list_sess) {
66-
sess = list_entry(ptrSess,
67-
struct tee_session,
68-
entry);
69-
70-
pos += sprintf(buff + pos,
71-
" [%02d.%d] sess=%p sessid=%08x\n",
72-
i, j, sess,
73-
sess->sessid);
74-
75-
if ((len - pos) < 80) {
76-
pos = 0;
77-
goto out;
78-
}
79-
80-
j++;
81-
}
82-
}
83+
j++;
84+
}
85+
86+
if (list_empty(&ctx->list_shm))
87+
goto out;
88+
89+
j = 0;
90+
list_for_each(ptr_shm, &ctx->list_shm) {
91+
shm = list_entry(ptr_shm, struct tee_shm, entry);
8392

84-
j = 0;
85-
if (!list_empty(&ctx->list_shm)) {
86-
list_for_each(ptrShm, &ctx->list_shm) {
87-
shm =
88-
list_entry(ptrShm, struct tee_shm,
89-
entry);
90-
91-
pos += sprintf(buff + pos,
92-
" [%02d.%d] shm=%p paddr=%pad "
93-
"kaddr=%p s=%zu(%zu)\n",
94-
i, j, shm,
95-
&shm->paddr,
96-
shm->kaddr,
97-
shm->size_req,
98-
shm->size_alloc);
99-
100-
if ((len - pos) < 80) {
101-
pos = 0;
102-
goto out;
103-
}
104-
105-
j++;
106-
}
93+
pos += sprintf(buff + pos,
94+
"[%02d.%d] shm=%p paddr=%p kaddr=%p",
95+
i, j, shm,
96+
&shm->paddr,
97+
shm->kaddr);
98+
pos += sprintf(buff + pos,
99+
" s=%zu(%zu)\n",
100+
shm->size_req,
101+
shm->size_alloc);
102+
if ((len - pos) < 80) {
103+
pos = 0;
104+
goto out;
107105
}
108-
i++;
106+
107+
j++;
109108
}
109+
110+
i++;
110111
}
111112

112113
out:
@@ -200,6 +201,7 @@ void tee_context_get(struct tee_context *ctx)
200201
static int is_in_list(struct tee *tee, struct list_head *entry)
201202
{
202203
int present = 1;
204+
203205
mutex_lock(&tee->lock);
204206
if ((entry->next == LIST_POISON1) && (entry->prev == LIST_POISON2))
205207
present = 0;
@@ -216,6 +218,7 @@ void tee_context_put(struct tee_context *ctx)
216218
{
217219
struct tee_context *_ctx = ctx;
218220
struct tee *tee;
221+
219222
BUG_ON(!ctx || !ctx->tee);
220223
tee = ctx->tee;
221224

@@ -249,6 +252,7 @@ int tee_context_copy_from_client(const struct tee_context *ctx,
249252
void *dest, const void *src, size_t size)
250253
{
251254
int res = 0;
255+
252256
if (dest && src && (size > 0)) {
253257
if (ctx->usr_client)
254258
res = copy_from_user(dest, src, size);
@@ -259,22 +263,25 @@ int tee_context_copy_from_client(const struct tee_context *ctx,
259263
}
260264

261265
struct tee_shm *tee_context_alloc_shm_tmp(struct tee_context *ctx,
262-
size_t size, const void *data,
266+
size_t size, const void *src,
263267
int type)
264268
{
265269
struct tee_shm *shm;
266270

267271
type &= (TEEC_MEM_INPUT | TEEC_MEM_OUTPUT);
268272

269-
shm = tee_shm_alloc(ctx, size, TEE_SHM_MAPPED | TEE_SHM_TEMP | type);
273+
shm = tee_shm_alloc(ctx->tee, size,
274+
TEE_SHM_MAPPED | TEE_SHM_TEMP | type);
270275
if (IS_ERR_OR_NULL(shm)) {
271276
dev_err(_DEV(ctx->tee), "%s: buffer allocation failed (%ld)\n",
272277
__func__, PTR_ERR(shm));
273278
return shm;
274279
}
275280

276-
if (shm && (type & TEEC_MEM_INPUT)) {
277-
if (tee_context_copy_from_client(ctx, shm->kaddr, data, size)) {
281+
shm->ctx = ctx;
282+
283+
if (type & TEEC_MEM_INPUT) {
284+
if (tee_context_copy_from_client(ctx, shm->kaddr, src, size)) {
278285
dev_err(_DEV(ctx->tee),
279286
"%s: tee_context_copy_from_client failed\n",
280287
__func__);

core/tee_core.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static int device_match(struct device *device, const void *devname)
4848
{
4949
struct tee *tee = dev_get_drvdata(device);
5050
int ret = strncmp(devname, tee->name, sizeof(tee->name));
51+
5152
BUG_ON(!tee);
5253
if (ret == 0)
5354
return 1;
@@ -62,6 +63,7 @@ static int device_match(struct device *device, const void *devname)
6263
struct tee *tee_get_tee(const char *devname)
6364
{
6465
struct device *device;
66+
6567
if (!devname)
6668
return NULL;
6769
device = class_find_device(misc_class, NULL, devname, device_match);
@@ -115,6 +117,7 @@ int tee_get(struct tee *tee)
115117
atomic_dec(&tee->refcount);
116118
} else {
117119
int count = (int)atomic_read(&tee->refcount);
120+
118121
dev_dbg(_DEV(tee), "%s: refcount=%d\n", __func__, count);
119122
if (count > tee->max_refcount)
120123
tee->max_refcount = count;
@@ -157,6 +160,7 @@ int tee_put(struct tee *tee)
157160
static int tee_supp_open(struct tee *tee)
158161
{
159162
int ret = 0;
163+
160164
dev_dbg(_DEV(tee), "%s: appclient=\"%s\" pid=%d\n", __func__,
161165
current->comm, current->pid);
162166

@@ -290,6 +294,7 @@ static int tee_do_shm_alloc(struct tee_context *ctx,
290294
int ret = -EINVAL;
291295
struct tee_shm_io k_shm;
292296
struct tee *tee = ctx->tee;
297+
293298
BUG_ON(!ctx->usr_client);
294299

295300
dev_dbg(_DEV(tee), "%s: >\n", __func__);
@@ -310,11 +315,12 @@ static int tee_do_shm_alloc(struct tee_context *ctx,
310315
goto exit;
311316
}
312317

313-
ret = tee_shm_alloc_fd(ctx, &k_shm);
318+
ret = tee_shm_alloc_io(ctx, &k_shm);
314319
if (ret)
315320
goto exit;
316321

317322
put_user(k_shm.fd_shm, &u_shm->fd_shm);
323+
put_user(k_shm.flags, &u_shm->flags);
318324

319325
exit:
320326
dev_dbg(_DEV(tee), "%s: < ret=%d, shmfd=%d\n", __func__, ret,
@@ -346,7 +352,7 @@ static int tee_do_get_fd_for_rpc_shm(struct tee_context *ctx,
346352
goto exit;
347353
}
348354

349-
ret = tee_shm_get_fd(ctx, &k_shm);
355+
ret = tee_shm_fd_for_rpc(ctx, &k_shm);
350356
if (ret)
351357
goto exit;
352358

0 commit comments

Comments
 (0)