1/*
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 start = rounddown(addr, PAGE_SIZE);
267 shm->offset = addr - start;
268 shm->size = length;
269 num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
270 shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
271 if (!shm->pages) {
272 ret = ERR_PTR(-ENOMEM);
273 goto err;
274 }
275
276 rc = get_user_pages_fast(start, num_pages, 1, shm->pages);
277 if (rc > 0)
278 shm->num_pages = rc;
279 if (rc != num_pages) {
280 if (rc >= 0)
281 rc = -ENOMEM;
282 ret = ERR_PTR(rc);
283 goto err;
284 }
285
286 mutex_lock(&teedev->mutex);
287 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
288 mutex_unlock(&teedev->mutex);
289
290 if (shm->id < 0) {
291 ret = ERR_PTR(shm->id);
292 goto err;
293 }
294
295 rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
296 shm->num_pages, start);
297 if (rc) {
298 ret = ERR_PTR(rc);
299 goto err;
300 }
301
302 if (flags & TEE_SHM_DMA_BUF) {
303 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
304
305 exp_info.ops = &tee_shm_dma_buf_ops;
306 exp_info.size = shm->size;
307 exp_info.flags = O_RDWR;
308 exp_info.priv = shm;
309
310 shm->dmabuf = dma_buf_export(&exp_info);
311 if (IS_ERR(shm->dmabuf)) {
312 ret = ERR_CAST(shm->dmabuf);
313 teedev->desc->ops->shm_unregister(ctx, shm);
314 goto err;
315 }
316 }
317
318 mutex_lock(&teedev->mutex);
319 list_add_tail(&shm->link, &ctx->list_shm);
320 mutex_unlock(&teedev->mutex);
321
322 return shm;
323err:
324 if (shm) {
325 size_t n;
326
327 if (shm->id >= 0) {
328 mutex_lock(&teedev->mutex);
329 idr_remove(&teedev->idr, shm->id);
330 mutex_unlock(&teedev->mutex);
331 }
332 if (shm->pages) {
333 for (n = 0; n < shm->num_pages; n++)
334 put_page(shm->pages[n]);
335 kfree(shm->pages);
336 }
337 }
338 kfree(shm);
339 teedev_ctx_put(ctx);
340 tee_device_put(teedev);
341 return ret;
342}
343EXPORT_SYMBOL_GPL(tee_shm_register);
344
345/**
346 * tee_shm_get_fd() - Increase reference count and return file descriptor
347 * @shm: Shared memory handle
348 * @returns user space file descriptor to shared memory
349 */
350int tee_shm_get_fd(struct tee_shm *shm)
351{
352 int fd;
353
354 if (!(shm->flags & TEE_SHM_DMA_BUF))
355 return -EINVAL;
356
357 get_dma_buf(shm->dmabuf);
358 fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC);
359 if (fd < 0)
360 dma_buf_put(shm->dmabuf);
361 return fd;
362}
363
364/**
365 * tee_shm_free() - Free shared memory
366 * @shm: Handle to shared memory to free
367 */
368void tee_shm_free(struct tee_shm *shm)
369{
370 /*
371 * dma_buf_put() decreases the dmabuf reference counter and will
372 * call tee_shm_release() when the last reference is gone.
373 *
374 * In the case of driver private memory we call tee_shm_release
375 * directly instead as it doesn't have a reference counter.
376 */
377 if (shm->flags & TEE_SHM_DMA_BUF)
378 dma_buf_put(shm->dmabuf);
379 else
380 tee_shm_release(shm);
381}
382EXPORT_SYMBOL_GPL(tee_shm_free);
383
384/**
385 * tee_shm_va2pa() - Get physical address of a virtual address
386 * @shm: Shared memory handle
387 * @va: Virtual address to tranlsate
388 * @pa: Returned physical address
389 * @returns 0 on success and < 0 on failure
390 */
391int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa)
392{
393 if (!(shm->flags & TEE_SHM_MAPPED))
394 return -EINVAL;
395 /* Check that we're in the range of the shm */
396 if ((char *)va < (char *)shm->kaddr)
397 return -EINVAL;
398 if ((char *)va >= ((char *)shm->kaddr + shm->size))
399 return -EINVAL;
400
401 return tee_shm_get_pa(
402 shm, (unsigned long)va - (unsigned long)shm->kaddr, pa);
403}
404EXPORT_SYMBOL_GPL(tee_shm_va2pa);
405
406/**
407 * tee_shm_pa2va() - Get virtual address of a physical address
408 * @shm: Shared memory handle
409 * @pa: Physical address to tranlsate
410 * @va: Returned virtual address
411 * @returns 0 on success and < 0 on failure
412 */
413int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va)
414{
415 if (!(shm->flags & TEE_SHM_MAPPED))
416 return -EINVAL;
417 /* Check that we're in the range of the shm */
418 if (pa < shm->paddr)
419 return -EINVAL;
420 if (pa >= (shm->paddr + shm->size))
421 return -EINVAL;
422
423 if (va) {
424 void *v = tee_shm_get_va(shm, pa - shm->paddr);
425
426 if (IS_ERR(v))
427 return PTR_ERR(v);
428 *va = v;
429 }
430 return 0;
431}
432EXPORT_SYMBOL_GPL(tee_shm_pa2va);
433
434/**
435 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
436 * @shm: Shared memory handle
437 * @offs: Offset from start of this shared memory
438 * @returns virtual address of the shared memory + offs if offs is within
439 * the bounds of this shared memory, else an ERR_PTR
440 */
441void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
442{
443 if (!(shm->flags & TEE_SHM_MAPPED))
444 return ERR_PTR(-EINVAL);
445 if (offs >= shm->size)
446 return ERR_PTR(-EINVAL);
447 return (char *)shm->kaddr + offs;
448}
449EXPORT_SYMBOL_GPL(tee_shm_get_va);
450
451/**
452 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
453 * @shm: Shared memory handle
454 * @offs: Offset from start of this shared memory
455 * @pa: Physical address to return
456 * @returns 0 if offs is within the bounds of this shared memory, else an
457 * error code.
458 */
459int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
460{
461 if (offs >= shm->size)
462 return -EINVAL;
463 if (pa)
464 *pa = shm->paddr + offs;
465 return 0;
466}
467EXPORT_SYMBOL_GPL(tee_shm_get_pa);
468
469/**
470 * tee_shm_get_from_id() - Find shared memory object and increase reference
471 * count
472 * @ctx: Context owning the shared memory
473 * @id: Id of shared memory object
474 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
475 */
476struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
477{
478 struct tee_device *teedev;
479 struct tee_shm *shm;
480
481 if (!ctx)
482 return ERR_PTR(-EINVAL);
483
484 teedev = ctx->teedev;
485 mutex_lock(&teedev->mutex);
486 shm = idr_find(&teedev->idr, id);
487 if (!shm || shm->ctx != ctx)
488 shm = ERR_PTR(-EINVAL);
489 else if (shm->flags & TEE_SHM_DMA_BUF)
490 get_dma_buf(shm->dmabuf);
491 mutex_unlock(&teedev->mutex);
492 return shm;
493}
494EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
495
496/**
497 * tee_shm_put() - Decrease reference count on a shared memory handle
498 * @shm: Shared memory handle
499 */
500void tee_shm_put(struct tee_shm *shm)
501{
502 if (shm->flags & TEE_SHM_DMA_BUF)
503 dma_buf_put(shm->dmabuf);
504}
505EXPORT_SYMBOL_GPL(tee_shm_put);
506