Skip to content

Commit 0e32abe

Browse files
committed
optee: support protected memory allocation
Add support in the OP-TEE backend driver for protected memory allocation. The support is limited to only the SMC ABI and for secure video buffers. OP-TEE is probed for the range of protected physical memory and a memory pool allocator is initialized if OP-TEE have support for such memory. Reviewed-by: Sumit Garg <sumit.garg@oss.qualcomm.com> Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
1 parent ab09dd6 commit 0e32abe

4 files changed

Lines changed: 81 additions & 2 deletions

File tree

drivers/tee/optee/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ config OPTEE_INSECURE_LOAD_IMAGE
2525

2626
Additional documentation on kernel security risks are at
2727
Documentation/tee/op-tee.rst.
28+
29+
config OPTEE_STATIC_PROTMEM_POOL
30+
bool
31+
depends on HAS_IOMEM && TEE_DMABUF_HEAPS
32+
default y

drivers/tee/optee/core.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ int optee_rpmb_intf_rdev(struct notifier_block *intf, unsigned long action,
5656
return 0;
5757
}
5858

59+
int optee_set_dma_mask(struct optee *optee, u_int pa_width)
60+
{
61+
u64 mask = DMA_BIT_MASK(min(64, pa_width));
62+
63+
return dma_coerce_mask_and_coherent(&optee->teedev->dev, mask);
64+
}
65+
5966
static void optee_bus_scan(struct work_struct *work)
6067
{
6168
WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));

drivers/tee/optee/optee_private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ struct optee_call_ctx {
274274

275275
extern struct blocking_notifier_head optee_rpmb_intf_added;
276276

277+
int optee_set_dma_mask(struct optee *optee, u_int pa_width);
278+
277279
int optee_notif_init(struct optee *optee, u_int max_key);
278280
void optee_notif_uninit(struct optee *optee);
279281
int optee_notif_wait(struct optee *optee, u_int key, u32 timeout);

drivers/tee/optee/smc_abi.c

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,68 @@ static inline int optee_load_fw(struct platform_device *pdev,
15831583
}
15841584
#endif
15851585

1586+
static struct tee_protmem_pool *static_protmem_pool_init(struct optee *optee)
1587+
{
1588+
#if IS_ENABLED(CONFIG_OPTEE_STATIC_PROTMEM_POOL)
1589+
union {
1590+
struct arm_smccc_res smccc;
1591+
struct optee_smc_get_protmem_config_result result;
1592+
} res;
1593+
struct tee_protmem_pool *pool;
1594+
void *p;
1595+
int rc;
1596+
1597+
optee->smc.invoke_fn(OPTEE_SMC_GET_PROTMEM_CONFIG, 0, 0, 0, 0,
1598+
0, 0, 0, &res.smccc);
1599+
if (res.result.status != OPTEE_SMC_RETURN_OK)
1600+
return ERR_PTR(-EINVAL);
1601+
1602+
rc = optee_set_dma_mask(optee, res.result.pa_width);
1603+
if (rc)
1604+
return ERR_PTR(rc);
1605+
1606+
/*
1607+
* Map the memory as uncached to make sure the kernel can work with
1608+
* __pfn_to_page() and friends since that's needed when passing the
1609+
* protected DMA-buf to a device. The memory should otherwise not
1610+
* be touched by the kernel since it's likely to cause an external
1611+
* abort due to the protection status.
1612+
*/
1613+
p = devm_memremap(&optee->teedev->dev, res.result.start,
1614+
res.result.size, MEMREMAP_WC);
1615+
if (IS_ERR(p))
1616+
return p;
1617+
1618+
pool = tee_protmem_static_pool_alloc(res.result.start, res.result.size);
1619+
if (IS_ERR(pool))
1620+
devm_memunmap(&optee->teedev->dev, p);
1621+
1622+
return pool;
1623+
#else
1624+
return ERR_PTR(-EINVAL);
1625+
#endif
1626+
}
1627+
1628+
static int optee_protmem_pool_init(struct optee *optee)
1629+
{
1630+
enum tee_dma_heap_id heap_id = TEE_DMA_HEAP_SECURE_VIDEO_PLAY;
1631+
struct tee_protmem_pool *pool = ERR_PTR(-EINVAL);
1632+
int rc;
1633+
1634+
if (!(optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_PROTMEM))
1635+
return 0;
1636+
1637+
pool = static_protmem_pool_init(optee);
1638+
if (IS_ERR(pool))
1639+
return PTR_ERR(pool);
1640+
1641+
rc = tee_device_register_dma_heap(optee->teedev, heap_id, pool);
1642+
if (rc)
1643+
pool->ops->destroy_pool(pool);
1644+
1645+
return rc;
1646+
}
1647+
15861648
static int optee_probe(struct platform_device *pdev)
15871649
{
15881650
optee_invoke_fn *invoke_fn;
@@ -1678,7 +1740,7 @@ static int optee_probe(struct platform_device *pdev)
16781740
optee = kzalloc(sizeof(*optee), GFP_KERNEL);
16791741
if (!optee) {
16801742
rc = -ENOMEM;
1681-
goto err_free_pool;
1743+
goto err_free_shm_pool;
16821744
}
16831745

16841746
optee->ops = &optee_ops;
@@ -1751,6 +1813,9 @@ static int optee_probe(struct platform_device *pdev)
17511813
pr_info("Asynchronous notifications enabled\n");
17521814
}
17531815

1816+
if (optee_protmem_pool_init(optee))
1817+
pr_info("Protected memory service not available\n");
1818+
17541819
/*
17551820
* Ensure that there are no pre-existing shm objects before enabling
17561821
* the shm cache so that there's no chance of receiving an invalid
@@ -1802,7 +1867,7 @@ static int optee_probe(struct platform_device *pdev)
18021867
tee_device_unregister(optee->teedev);
18031868
err_free_optee:
18041869
kfree(optee);
1805-
err_free_pool:
1870+
err_free_shm_pool:
18061871
tee_shm_pool_free(pool);
18071872
if (memremaped_shm)
18081873
memunmap(memremaped_shm);

0 commit comments

Comments
 (0)