blob: 8e7b52ab6c63e8532123065514474bed63582017 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * Copyright (c) 2015-2016, 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#include <linux/device.h>
15#include <linux/dma-buf.h>
16#include <linux/fdtable.h>
17#include <linux/idr.h>
18#include <linux/sched.h>
19#include <linux/slab.h>
20#include <linux/tee_drv.h>
21#include "tee_private.h"
22
23static void tee_shm_release(struct tee_shm *shm)
24{
25 struct tee_device *teedev = shm->teedev;
26
27 mutex_lock(&teedev->mutex);
28 idr_remove(&teedev->idr, shm->id);
29 if (shm->ctx)
30 list_del(&shm->link);
31 mutex_unlock(&teedev->mutex);
32
33 if (shm->flags & TEE_SHM_POOL) {
34 struct tee_shm_pool_mgr *poolm;
35
36 if (shm->flags & TEE_SHM_DMA_BUF)
37 poolm = teedev->pool->dma_buf_mgr;
38 else
39 poolm = teedev->pool->private_mgr;
40
41 poolm->ops->free(poolm, shm);
42 } else if (shm->flags & TEE_SHM_REGISTER) {
43 size_t n;
44 int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm);
45
46 if (rc)
47 dev_err(teedev->dev.parent,
48 "unregister shm %p failed: %d", shm, rc);
49
50 for (n = 0; n < shm->num_pages; n++)
51 put_page(shm->pages[n]);
52
53 kfree(shm->pages);
54 }
55
56 if (shm->ctx)
57 teedev_ctx_put(shm->ctx);
58
59 kfree(shm);
60
61 tee_device_put(teedev);
62}
63
64static struct sg_table *tee_shm_op_map_dma_buf(struct dma_buf_attachment
65 *attach, enum dma_data_direction dir)
66{
67 return NULL;
68}
69
70static void tee_shm_op_unmap_dma_buf(struct dma_buf_attachment *attach,
71 struct sg_table *table,
72 enum dma_data_direction dir)
73{
74}
75
76static void tee_shm_op_release(struct dma_buf *dmabuf)
77{
78 struct tee_shm *shm = dmabuf->priv;
79
80 tee_shm_release(shm);
81}
82
83static void *tee_shm_op_map(struct dma_buf *dmabuf, unsigned long pgnum)
84{
85 return NULL;
86}
87
88static int tee_shm_op_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
89{
90 struct tee_shm *shm = dmabuf->priv;
91 size_t size = vma->vm_end - vma->vm_start;
92
93 /* Refuse sharing shared memory provided by application */
94 if (shm->flags & TEE_SHM_REGISTER)
95 return -EINVAL;
96
97 return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
98 size, vma->vm_page_prot);
99}
100
101static const struct dma_buf_ops tee_shm_dma_buf_ops = {
102 .map_dma_buf = tee_shm_op_map_dma_buf,
103 .unmap_dma_buf = tee_shm_op_unmap_dma_buf,
104 .release = tee_shm_op_release,
105 .map = tee_shm_op_map,
106 .mmap = tee_shm_op_mmap,
107};
108
109static struct tee_shm *__tee_shm_alloc(struct tee_context *ctx,
110 struct tee_device *teedev,
111 size_t size, u32 flags)
112{
113 struct tee_shm_pool_mgr *poolm = NULL;
114 struct tee_shm *shm;
115 void *ret;
116 int rc;
117
118 if (ctx && ctx->teedev != teedev) {
119 dev_err(teedev->dev.parent, "ctx and teedev mismatch\n");
120 return ERR_PTR(-EINVAL);
121 }
122
123 if (!(flags & TEE_SHM_MAPPED)) {
124 dev_err(teedev->dev.parent,
125 "only mapped allocations supported\n");
126 return ERR_PTR(-EINVAL);
127 }
128
129 if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF))) {
130 dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags);
131 return ERR_PTR(-EINVAL);
132 }
133
134 if (!tee_device_get(teedev))
135 return ERR_PTR(-EINVAL);
136
137 if (!teedev->pool) {
138 /* teedev has been detached from driver */
139 ret = ERR_PTR(-EINVAL);
140 goto err_dev_put;
141 }
142
143 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
144 if (!shm) {
145 ret = ERR_PTR(-ENOMEM);
146 goto err_dev_put;
147 }
148
149 shm->flags = flags | TEE_SHM_POOL;
150 shm->teedev = teedev;
151 shm->ctx = ctx;
152 if (flags & TEE_SHM_DMA_BUF)
153 poolm = teedev->pool->dma_buf_mgr;
154 else
155 poolm = teedev->pool->private_mgr;
156
157 rc = poolm->ops->alloc(poolm, shm, size);
158 if (rc) {
159 ret = ERR_PTR(rc);
160 goto err_kfree;
161 }
162
163 mutex_lock(&teedev->mutex);
164 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
165 mutex_unlock(&teedev->mutex);
166 if (shm->id < 0) {
167 ret = ERR_PTR(shm->id);
168 goto err_pool_free;
169 }
170
171 if (flags & TEE_SHM_DMA_BUF) {
172 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
173
174 exp_info.ops = &tee_shm_dma_buf_ops;
175 exp_info.size = shm->size;
176 exp_info.flags = O_RDWR;
177 exp_info.priv = shm;
178
179 shm->dmabuf = dma_buf_export(&exp_info);
180 if (IS_ERR(shm->dmabuf)) {
181 ret = ERR_CAST(shm->dmabuf);
182 goto err_rem;
183 }
184 }
185
186 if (ctx) {
187 teedev_ctx_get(ctx);
188 mutex_lock(&teedev->mutex);
189 list_add_tail(&shm->link, &ctx->list_shm);
190 mutex_unlock(&teedev->mutex);
191 }
192
193 return shm;
194err_rem:
195 mutex_lock(&teedev->mutex);
196 idr_remove(&teedev->idr, shm->id);
197 mutex_unlock(&teedev->mutex);
198err_pool_free:
199 poolm->ops->free(poolm, shm);
200err_kfree:
201 kfree(shm);
202err_dev_put:
203 tee_device_put(teedev);
204 return ret;
205}
206
207/**
208 * tee_shm_alloc() - Allocate shared memory
209 * @ctx: Context that allocates the shared memory
210 * @size: Requested size of shared memory
211 * @flags: Flags setting properties for the requested shared memory.
212 *
213 * Memory allocated as global shared memory is automatically freed when the
214 * TEE file pointer is closed. The @flags field uses the bits defined by
215 * TEE_SHM_* in <linux/tee_drv.h>. TEE_SHM_MAPPED must currently always be
216 * set. If TEE_SHM_DMA_BUF global shared memory will be allocated and
217 * associated with a dma-buf handle, else driver private memory.
218 */
219struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags)
220{
221 return __tee_shm_alloc(ctx, ctx->teedev, size, flags);
222}
223EXPORT_SYMBOL_GPL(tee_shm_alloc);
224
225struct tee_shm *tee_shm_priv_alloc(struct tee_device *teedev, size_t size)
226{
227 return __tee_shm_alloc(NULL, teedev, size, TEE_SHM_MAPPED);
228}
229EXPORT_SYMBOL_GPL(tee_shm_priv_alloc);
230
231struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
232 size_t length, u32 flags)
233{
234 struct tee_device *teedev = ctx->teedev;
235 const u32 req_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED;
236 struct tee_shm *shm;
237 void *ret;
238 int rc;
239 int num_pages;
240 unsigned long start;
241
242 if (flags != req_flags)
243 return ERR_PTR(-ENOTSUPP);
244
245 if (!tee_device_get(teedev))
246 return ERR_PTR(-EINVAL);
247
248 if (!teedev->desc->ops->shm_register ||
249 !teedev->desc->ops->shm_unregister) {
250 tee_device_put(teedev);
251 return ERR_PTR(-ENOTSUPP);
252 }
253
254 teedev_ctx_get(ctx);
255
256 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
257 if (!shm) {
258 ret = ERR_PTR(-ENOMEM);
259 goto err;
260 }
261
262 shm->flags = flags | TEE_SHM_REGISTER;
263 shm->teedev = teedev;
264 shm->ctx = ctx;
265 shm->id = -1;
266 addr = untagged_addr(addr);
267 start = rounddown(addr, PAGE_SIZE);
268 shm->offset = addr - start;
269 shm->size = length;
270 num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
271 shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
272 if (!shm->pages) {
273 ret = ERR_PTR(-ENOMEM);
274 goto err;
275 }
276
277 rc = get_user_pages_fast(start, num_pages, 1, shm->pages);
278 if (rc > 0)
279 shm->num_pages = rc;
280 if (rc != num_pages) {
281 if (rc >= 0)
282 rc = -ENOMEM;
283 ret = ERR_PTR(rc);
284 goto err;
285 }
286
287 mutex_lock(&teedev->mutex);
288 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
289 mutex_unlock(&teedev->mutex);
290
291 if (shm->id < 0) {
292 ret = ERR_PTR(shm->id);
293 goto err;
294 }
295
296 rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
297 shm->num_pages, start);
298 if (rc) {
299 ret = ERR_PTR(rc);
300 goto err;
301 }
302
303 if (flags & TEE_SHM_DMA_BUF) {
304 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
305
306 exp_info.ops = &tee_shm_dma_buf_ops;
307 exp_info.size = shm->size;
308 exp_info.flags = O_RDWR;
309 exp_info.priv = shm;
310
311 shm->dmabuf = dma_buf_export(&exp_info);
312 if (IS_ERR(shm->dmabuf)) {
313 ret = ERR_CAST(shm->dmabuf);
314 teedev->desc->ops->shm_unregister(ctx, shm);
315 goto err;
316 }
317 }
318
319 mutex_lock(&teedev->mutex);
320 list_add_tail(&shm->link, &ctx->list_shm);
321 mutex_unlock(&teedev->mutex);
322
323 return shm;
324err:
325 if (shm) {
326 size_t n;
327
328 if (shm->id >= 0) {
329 mutex_lock(&teedev->mutex);
330 idr_remove(&teedev->idr, shm->id);
331 mutex_unlock(&teedev->mutex);
332 }
333 if (shm->pages) {
334 for (n = 0; n < shm->num_pages; n++)
335 put_page(shm->pages[n]);
336 kfree(shm->pages);
337 }
338 }
339 kfree(shm);
340 teedev_ctx_put(ctx);
341 tee_device_put(teedev);
342 return ret;
343}
344EXPORT_SYMBOL_GPL(tee_shm_register);
345
346/**
347 * tee_shm_get_fd() - Increase reference count and return file descriptor
348 * @shm: Shared memory handle
349 * @returns user space file descriptor to shared memory
350 */
351int tee_shm_get_fd(struct tee_shm *shm)
352{
353 int fd;
354
355 if (!(shm->flags & TEE_SHM_DMA_BUF))
356 return -EINVAL;
357
358 get_dma_buf(shm->dmabuf);
359 fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC);
360 if (fd < 0)
361 dma_buf_put(shm->dmabuf);
362 return fd;
363}
364
365/**
366 * tee_shm_free() - Free shared memory
367 * @shm: Handle to shared memory to free
368 */
369void tee_shm_free(struct tee_shm *shm)
370{
371 /*
372 * dma_buf_put() decreases the dmabuf reference counter and will
373 * call tee_shm_release() when the last reference is gone.
374 *
375 * In the case of driver private memory we call tee_shm_release
376 * directly instead as it doesn't have a reference counter.
377 */
378 if (shm->flags & TEE_SHM_DMA_BUF)
379 dma_buf_put(shm->dmabuf);
380 else
381 tee_shm_release(shm);
382}
383EXPORT_SYMBOL_GPL(tee_shm_free);
384
385/**
386 * tee_shm_va2pa() - Get physical address of a virtual address
387 * @shm: Shared memory handle
388 * @va: Virtual address to tranlsate
389 * @pa: Returned physical address
390 * @returns 0 on success and < 0 on failure
391 */
392int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa)
393{
394 if (!(shm->flags & TEE_SHM_MAPPED))
395 return -EINVAL;
396 /* Check that we're in the range of the shm */
397 if ((char *)va < (char *)shm->kaddr)
398 return -EINVAL;
399 if ((char *)va >= ((char *)shm->kaddr + shm->size))
400 return -EINVAL;
401
402 return tee_shm_get_pa(
403 shm, (unsigned long)va - (unsigned long)shm->kaddr, pa);
404}
405EXPORT_SYMBOL_GPL(tee_shm_va2pa);
406
407/**
408 * tee_shm_pa2va() - Get virtual address of a physical address
409 * @shm: Shared memory handle
410 * @pa: Physical address to tranlsate
411 * @va: Returned virtual address
412 * @returns 0 on success and < 0 on failure
413 */
414int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va)
415{
416 if (!(shm->flags & TEE_SHM_MAPPED))
417 return -EINVAL;
418 /* Check that we're in the range of the shm */
419 if (pa < shm->paddr)
420 return -EINVAL;
421 if (pa >= (shm->paddr + shm->size))
422 return -EINVAL;
423
424 if (va) {
425 void *v = tee_shm_get_va(shm, pa - shm->paddr);
426
427 if (IS_ERR(v))
428 return PTR_ERR(v);
429 *va = v;
430 }
431 return 0;
432}
433EXPORT_SYMBOL_GPL(tee_shm_pa2va);
434
435/**
436 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
437 * @shm: Shared memory handle
438 * @offs: Offset from start of this shared memory
439 * @returns virtual address of the shared memory + offs if offs is within
440 * the bounds of this shared memory, else an ERR_PTR
441 */
442void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
443{
444 if (!(shm->flags & TEE_SHM_MAPPED))
445 return ERR_PTR(-EINVAL);
446 if (offs >= shm->size)
447 return ERR_PTR(-EINVAL);
448 return (char *)shm->kaddr + offs;
449}
450EXPORT_SYMBOL_GPL(tee_shm_get_va);
451
452/**
453 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
454 * @shm: Shared memory handle
455 * @offs: Offset from start of this shared memory
456 * @pa: Physical address to return
457 * @returns 0 if offs is within the bounds of this shared memory, else an
458 * error code.
459 */
460int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
461{
462 if (offs >= shm->size)
463 return -EINVAL;
464 if (pa)
465 *pa = shm->paddr + offs;
466 return 0;
467}
468EXPORT_SYMBOL_GPL(tee_shm_get_pa);
469
470/**
471 * tee_shm_get_from_id() - Find shared memory object and increase reference
472 * count
473 * @ctx: Context owning the shared memory
474 * @id: Id of shared memory object
475 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
476 */
477struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
478{
479 struct tee_device *teedev;
480 struct tee_shm *shm;
481
482 if (!ctx)
483 return ERR_PTR(-EINVAL);
484
485 teedev = ctx->teedev;
486 mutex_lock(&teedev->mutex);
487 shm = idr_find(&teedev->idr, id);
488 if (!shm || shm->ctx != ctx)
489 shm = ERR_PTR(-EINVAL);
490 else if (shm->flags & TEE_SHM_DMA_BUF)
491 get_dma_buf(shm->dmabuf);
492 mutex_unlock(&teedev->mutex);
493 return shm;
494}
495EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
496
497/**
498 * tee_shm_put() - Decrease reference count on a shared memory handle
499 * @shm: Shared memory handle
500 */
501void tee_shm_put(struct tee_shm *shm)
502{
503 if (shm->flags & TEE_SHM_DMA_BUF)
504 dma_buf_put(shm->dmabuf);
505}
506EXPORT_SYMBOL_GPL(tee_shm_put);