Skip to content

Commit ab09dd6

Browse files
committed
tee: add tee_shm_alloc_dma_mem()
Add tee_shm_alloc_dma_mem() to allocate DMA memory. The memory is represented by a tee_shm object using the new flag TEE_SHM_DMA_MEM to identify it as DMA memory. The allocated memory will later be lent to the TEE to be used as protected memory. Reviewed-by: Sumit Garg <sumit.garg@oss.qualcomm.com> Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
1 parent 146bf4e commit ab09dd6

2 files changed

Lines changed: 88 additions & 2 deletions

File tree

drivers/tee/tee_shm.c

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <linux/anon_inodes.h>
66
#include <linux/device.h>
77
#include <linux/dma-buf.h>
8+
#include <linux/dma-mapping.h>
9+
#include <linux/highmem.h>
810
#include <linux/idr.h>
911
#include <linux/io.h>
1012
#include <linux/mm.h>
@@ -13,9 +15,14 @@
1315
#include <linux/tee_core.h>
1416
#include <linux/uaccess.h>
1517
#include <linux/uio.h>
16-
#include <linux/highmem.h>
1718
#include "tee_private.h"
1819

20+
struct tee_shm_dma_mem {
21+
struct tee_shm shm;
22+
dma_addr_t dma_addr;
23+
struct page *page;
24+
};
25+
1926
static void shm_put_kernel_pages(struct page **pages, size_t page_count)
2027
{
2128
size_t n;
@@ -48,7 +55,16 @@ static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm)
4855
{
4956
void *p = shm;
5057

51-
if (shm->flags & TEE_SHM_DMA_BUF) {
58+
if (shm->flags & TEE_SHM_DMA_MEM) {
59+
#if IS_ENABLED(CONFIG_TEE_DMABUF_HEAPS)
60+
struct tee_shm_dma_mem *dma_mem;
61+
62+
dma_mem = container_of(shm, struct tee_shm_dma_mem, shm);
63+
p = dma_mem;
64+
dma_free_pages(&teedev->dev, shm->size, dma_mem->page,
65+
dma_mem->dma_addr, DMA_BIDIRECTIONAL);
66+
#endif
67+
} else if (shm->flags & TEE_SHM_DMA_BUF) {
5268
struct tee_shm_dmabuf_ref *ref;
5369

5470
ref = container_of(shm, struct tee_shm_dmabuf_ref, shm);
@@ -268,6 +284,71 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size)
268284
}
269285
EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf);
270286

287+
#if IS_ENABLED(CONFIG_TEE_DMABUF_HEAPS)
288+
/**
289+
* tee_shm_alloc_dma_mem() - Allocate DMA memory as shared memory object
290+
* @ctx: Context that allocates the shared memory
291+
* @page_count: Number of pages
292+
*
293+
* The allocated memory is expected to be lent (made inaccessible to the
294+
* kernel) to the TEE while it's used and returned (accessible to the
295+
* kernel again) before it's freed.
296+
*
297+
* This function should normally only be used internally in the TEE
298+
* drivers.
299+
*
300+
* @returns a pointer to 'struct tee_shm'
301+
*/
302+
struct tee_shm *tee_shm_alloc_dma_mem(struct tee_context *ctx,
303+
size_t page_count)
304+
{
305+
struct tee_device *teedev = ctx->teedev;
306+
struct tee_shm_dma_mem *dma_mem;
307+
dma_addr_t dma_addr;
308+
struct page *page;
309+
310+
if (!tee_device_get(teedev))
311+
return ERR_PTR(-EINVAL);
312+
313+
page = dma_alloc_pages(&teedev->dev, page_count * PAGE_SIZE,
314+
&dma_addr, DMA_BIDIRECTIONAL, GFP_KERNEL);
315+
if (!page)
316+
goto err_put_teedev;
317+
318+
dma_mem = kzalloc(sizeof(*dma_mem), GFP_KERNEL);
319+
if (!dma_mem)
320+
goto err_free_pages;
321+
322+
refcount_set(&dma_mem->shm.refcount, 1);
323+
dma_mem->shm.ctx = ctx;
324+
dma_mem->shm.paddr = page_to_phys(page);
325+
dma_mem->dma_addr = dma_addr;
326+
dma_mem->page = page;
327+
dma_mem->shm.size = page_count * PAGE_SIZE;
328+
dma_mem->shm.flags = TEE_SHM_DMA_MEM;
329+
330+
teedev_ctx_get(ctx);
331+
332+
return &dma_mem->shm;
333+
334+
err_free_pages:
335+
dma_free_pages(&teedev->dev, page_count * PAGE_SIZE, page, dma_addr,
336+
DMA_BIDIRECTIONAL);
337+
err_put_teedev:
338+
tee_device_put(teedev);
339+
340+
return ERR_PTR(-ENOMEM);
341+
}
342+
EXPORT_SYMBOL_GPL(tee_shm_alloc_dma_mem);
343+
#else
344+
struct tee_shm *tee_shm_alloc_dma_mem(struct tee_context *ctx,
345+
size_t page_count)
346+
{
347+
return ERR_PTR(-EINVAL);
348+
}
349+
EXPORT_SYMBOL_GPL(tee_shm_alloc_dma_mem);
350+
#endif
351+
271352
int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
272353
int (*shm_register)(struct tee_context *ctx,
273354
struct tee_shm *shm,

include/linux/tee_core.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#define TEE_SHM_POOL BIT(2) /* Memory allocated from pool */
3030
#define TEE_SHM_PRIV BIT(3) /* Memory private to TEE driver */
3131
#define TEE_SHM_DMA_BUF BIT(4) /* Memory with dma-buf handle */
32+
#define TEE_SHM_DMA_MEM BIT(5) /* Memory allocated with */
33+
/* dma_alloc_pages() */
3234

3335
#define TEE_DEVICE_FLAG_REGISTERED 0x1
3436
#define TEE_MAX_DEV_NAME_LEN 32
@@ -298,6 +300,9 @@ void *tee_get_drvdata(struct tee_device *teedev);
298300
*/
299301
struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size);
300302

303+
struct tee_shm *tee_shm_alloc_dma_mem(struct tee_context *ctx,
304+
size_t page_count);
305+
301306
int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
302307
int (*shm_register)(struct tee_context *ctx,
303308
struct tee_shm *shm,

0 commit comments

Comments
 (0)