18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2015, Linaro Limited 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci#include <linux/device.h> 68c2ecf20Sopenharmony_ci#include <linux/dma-buf.h> 78c2ecf20Sopenharmony_ci#include <linux/genalloc.h> 88c2ecf20Sopenharmony_ci#include <linux/slab.h> 98c2ecf20Sopenharmony_ci#include <linux/tee_drv.h> 108c2ecf20Sopenharmony_ci#include "tee_private.h" 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_cistatic int pool_op_gen_alloc(struct tee_shm_pool_mgr *poolm, 138c2ecf20Sopenharmony_ci struct tee_shm *shm, size_t size) 148c2ecf20Sopenharmony_ci{ 158c2ecf20Sopenharmony_ci unsigned long va; 168c2ecf20Sopenharmony_ci struct gen_pool *genpool = poolm->private_data; 178c2ecf20Sopenharmony_ci size_t s = roundup(size, 1 << genpool->min_alloc_order); 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci va = gen_pool_alloc(genpool, s); 208c2ecf20Sopenharmony_ci if (!va) 218c2ecf20Sopenharmony_ci return -ENOMEM; 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci memset((void *)va, 0, s); 248c2ecf20Sopenharmony_ci shm->kaddr = (void *)va; 258c2ecf20Sopenharmony_ci shm->paddr = gen_pool_virt_to_phys(genpool, va); 268c2ecf20Sopenharmony_ci shm->size = s; 278c2ecf20Sopenharmony_ci return 0; 288c2ecf20Sopenharmony_ci} 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistatic void pool_op_gen_free(struct tee_shm_pool_mgr *poolm, 318c2ecf20Sopenharmony_ci struct tee_shm *shm) 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci gen_pool_free(poolm->private_data, (unsigned long)shm->kaddr, 348c2ecf20Sopenharmony_ci shm->size); 358c2ecf20Sopenharmony_ci shm->kaddr = NULL; 368c2ecf20Sopenharmony_ci} 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic void pool_op_gen_destroy_poolmgr(struct tee_shm_pool_mgr *poolm) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci gen_pool_destroy(poolm->private_data); 418c2ecf20Sopenharmony_ci kfree(poolm); 428c2ecf20Sopenharmony_ci} 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic const struct tee_shm_pool_mgr_ops pool_ops_generic = { 458c2ecf20Sopenharmony_ci .alloc = pool_op_gen_alloc, 468c2ecf20Sopenharmony_ci .free = pool_op_gen_free, 478c2ecf20Sopenharmony_ci .destroy_poolmgr = pool_op_gen_destroy_poolmgr, 488c2ecf20Sopenharmony_ci}; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/** 518c2ecf20Sopenharmony_ci * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved 528c2ecf20Sopenharmony_ci * memory range 538c2ecf20Sopenharmony_ci * @priv_info: Information for driver private shared memory pool 548c2ecf20Sopenharmony_ci * @dmabuf_info: Information for dma-buf shared memory pool 558c2ecf20Sopenharmony_ci * 568c2ecf20Sopenharmony_ci * Start and end of pools will must be page aligned. 578c2ecf20Sopenharmony_ci * 588c2ecf20Sopenharmony_ci * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied 598c2ecf20Sopenharmony_ci * in @dmabuf, others will use the range provided by @priv. 608c2ecf20Sopenharmony_ci * 618c2ecf20Sopenharmony_ci * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure. 628c2ecf20Sopenharmony_ci */ 638c2ecf20Sopenharmony_cistruct tee_shm_pool * 648c2ecf20Sopenharmony_citee_shm_pool_alloc_res_mem(struct tee_shm_pool_mem_info *priv_info, 658c2ecf20Sopenharmony_ci struct tee_shm_pool_mem_info *dmabuf_info) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci struct tee_shm_pool_mgr *priv_mgr; 688c2ecf20Sopenharmony_ci struct tee_shm_pool_mgr *dmabuf_mgr; 698c2ecf20Sopenharmony_ci void *rc; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci /* 728c2ecf20Sopenharmony_ci * Create the pool for driver private shared memory 738c2ecf20Sopenharmony_ci */ 748c2ecf20Sopenharmony_ci rc = tee_shm_pool_mgr_alloc_res_mem(priv_info->vaddr, priv_info->paddr, 758c2ecf20Sopenharmony_ci priv_info->size, 768c2ecf20Sopenharmony_ci 3 /* 8 byte aligned */); 778c2ecf20Sopenharmony_ci if (IS_ERR(rc)) 788c2ecf20Sopenharmony_ci return rc; 798c2ecf20Sopenharmony_ci priv_mgr = rc; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci /* 828c2ecf20Sopenharmony_ci * Create the pool for dma_buf shared memory 838c2ecf20Sopenharmony_ci */ 848c2ecf20Sopenharmony_ci rc = tee_shm_pool_mgr_alloc_res_mem(dmabuf_info->vaddr, 858c2ecf20Sopenharmony_ci dmabuf_info->paddr, 868c2ecf20Sopenharmony_ci dmabuf_info->size, PAGE_SHIFT); 878c2ecf20Sopenharmony_ci if (IS_ERR(rc)) 888c2ecf20Sopenharmony_ci goto err_free_priv_mgr; 898c2ecf20Sopenharmony_ci dmabuf_mgr = rc; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr); 928c2ecf20Sopenharmony_ci if (IS_ERR(rc)) 938c2ecf20Sopenharmony_ci goto err_free_dmabuf_mgr; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci return rc; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_cierr_free_dmabuf_mgr: 988c2ecf20Sopenharmony_ci tee_shm_pool_mgr_destroy(dmabuf_mgr); 998c2ecf20Sopenharmony_cierr_free_priv_mgr: 1008c2ecf20Sopenharmony_ci tee_shm_pool_mgr_destroy(priv_mgr); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci return rc; 1038c2ecf20Sopenharmony_ci} 1048c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(tee_shm_pool_alloc_res_mem); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cistruct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vaddr, 1078c2ecf20Sopenharmony_ci phys_addr_t paddr, 1088c2ecf20Sopenharmony_ci size_t size, 1098c2ecf20Sopenharmony_ci int min_alloc_order) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci const size_t page_mask = PAGE_SIZE - 1; 1128c2ecf20Sopenharmony_ci struct tee_shm_pool_mgr *mgr; 1138c2ecf20Sopenharmony_ci int rc; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci /* Start and end must be page aligned */ 1168c2ecf20Sopenharmony_ci if (vaddr & page_mask || paddr & page_mask || size & page_mask) 1178c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci mgr = kzalloc(sizeof(*mgr), GFP_KERNEL); 1208c2ecf20Sopenharmony_ci if (!mgr) 1218c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci mgr->private_data = gen_pool_create(min_alloc_order, -1); 1248c2ecf20Sopenharmony_ci if (!mgr->private_data) { 1258c2ecf20Sopenharmony_ci rc = -ENOMEM; 1268c2ecf20Sopenharmony_ci goto err; 1278c2ecf20Sopenharmony_ci } 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci gen_pool_set_algo(mgr->private_data, gen_pool_best_fit, NULL); 1308c2ecf20Sopenharmony_ci rc = gen_pool_add_virt(mgr->private_data, vaddr, paddr, size, -1); 1318c2ecf20Sopenharmony_ci if (rc) { 1328c2ecf20Sopenharmony_ci gen_pool_destroy(mgr->private_data); 1338c2ecf20Sopenharmony_ci goto err; 1348c2ecf20Sopenharmony_ci } 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci mgr->ops = &pool_ops_generic; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci return mgr; 1398c2ecf20Sopenharmony_cierr: 1408c2ecf20Sopenharmony_ci kfree(mgr); 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci return ERR_PTR(rc); 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(tee_shm_pool_mgr_alloc_res_mem); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_cistatic bool check_mgr_ops(struct tee_shm_pool_mgr *mgr) 1478c2ecf20Sopenharmony_ci{ 1488c2ecf20Sopenharmony_ci return mgr && mgr->ops && mgr->ops->alloc && mgr->ops->free && 1498c2ecf20Sopenharmony_ci mgr->ops->destroy_poolmgr; 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistruct tee_shm_pool *tee_shm_pool_alloc(struct tee_shm_pool_mgr *priv_mgr, 1538c2ecf20Sopenharmony_ci struct tee_shm_pool_mgr *dmabuf_mgr) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci struct tee_shm_pool *pool; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci if (!check_mgr_ops(priv_mgr) || !check_mgr_ops(dmabuf_mgr)) 1588c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci pool = kzalloc(sizeof(*pool), GFP_KERNEL); 1618c2ecf20Sopenharmony_ci if (!pool) 1628c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci pool->private_mgr = priv_mgr; 1658c2ecf20Sopenharmony_ci pool->dma_buf_mgr = dmabuf_mgr; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci return pool; 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(tee_shm_pool_alloc); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci/** 1728c2ecf20Sopenharmony_ci * tee_shm_pool_free() - Free a shared memory pool 1738c2ecf20Sopenharmony_ci * @pool: The shared memory pool to free 1748c2ecf20Sopenharmony_ci * 1758c2ecf20Sopenharmony_ci * There must be no remaining shared memory allocated from this pool when 1768c2ecf20Sopenharmony_ci * this function is called. 1778c2ecf20Sopenharmony_ci */ 1788c2ecf20Sopenharmony_civoid tee_shm_pool_free(struct tee_shm_pool *pool) 1798c2ecf20Sopenharmony_ci{ 1808c2ecf20Sopenharmony_ci if (pool->private_mgr) 1818c2ecf20Sopenharmony_ci tee_shm_pool_mgr_destroy(pool->private_mgr); 1828c2ecf20Sopenharmony_ci if (pool->dma_buf_mgr) 1838c2ecf20Sopenharmony_ci tee_shm_pool_mgr_destroy(pool->dma_buf_mgr); 1848c2ecf20Sopenharmony_ci kfree(pool); 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(tee_shm_pool_free); 187