tee_shm_pool.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2015, 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/genalloc.h>
  17. #include <linux/slab.h>
  18. #include <linux/tee_drv.h>
  19. #include "tee_private.h"
  20. static int pool_op_gen_alloc(struct tee_shm_pool_mgr *poolm,
  21. struct tee_shm *shm, size_t size)
  22. {
  23. unsigned long va;
  24. struct gen_pool *genpool = poolm->private_data;
  25. size_t s = roundup(size, 1 << genpool->min_alloc_order);
  26. va = gen_pool_alloc(genpool, s);
  27. if (!va)
  28. return -ENOMEM;
  29. memset((void *)va, 0, s);
  30. shm->kaddr = (void *)va;
  31. shm->paddr = gen_pool_virt_to_phys(genpool, va);
  32. shm->size = s;
  33. return 0;
  34. }
  35. static void pool_op_gen_free(struct tee_shm_pool_mgr *poolm,
  36. struct tee_shm *shm)
  37. {
  38. gen_pool_free(poolm->private_data, (unsigned long)shm->kaddr,
  39. shm->size);
  40. shm->kaddr = NULL;
  41. }
  42. static const struct tee_shm_pool_mgr_ops pool_ops_generic = {
  43. .alloc = pool_op_gen_alloc,
  44. .free = pool_op_gen_free,
  45. };
  46. static void pool_res_mem_destroy(struct tee_shm_pool *pool)
  47. {
  48. gen_pool_destroy(pool->private_mgr.private_data);
  49. gen_pool_destroy(pool->dma_buf_mgr.private_data);
  50. }
  51. static int pool_res_mem_mgr_init(struct tee_shm_pool_mgr *mgr,
  52. struct tee_shm_pool_mem_info *info,
  53. int min_alloc_order)
  54. {
  55. size_t page_mask = PAGE_SIZE - 1;
  56. struct gen_pool *genpool = NULL;
  57. int rc;
  58. /*
  59. * Start and end must be page aligned
  60. */
  61. if ((info->vaddr & page_mask) || (info->paddr & page_mask) ||
  62. (info->size & page_mask))
  63. return -EINVAL;
  64. genpool = gen_pool_create(min_alloc_order, -1);
  65. if (!genpool)
  66. return -ENOMEM;
  67. gen_pool_set_algo(genpool, gen_pool_best_fit, NULL);
  68. rc = gen_pool_add_virt(genpool, info->vaddr, info->paddr, info->size,
  69. -1);
  70. if (rc) {
  71. gen_pool_destroy(genpool);
  72. return rc;
  73. }
  74. mgr->private_data = genpool;
  75. mgr->ops = &pool_ops_generic;
  76. return 0;
  77. }
  78. /**
  79. * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved
  80. * memory range
  81. * @priv_info: Information for driver private shared memory pool
  82. * @dmabuf_info: Information for dma-buf shared memory pool
  83. *
  84. * Start and end of pools will must be page aligned.
  85. *
  86. * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
  87. * in @dmabuf, others will use the range provided by @priv.
  88. *
  89. * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
  90. */
  91. struct tee_shm_pool *
  92. tee_shm_pool_alloc_res_mem(struct tee_shm_pool_mem_info *priv_info,
  93. struct tee_shm_pool_mem_info *dmabuf_info)
  94. {
  95. struct tee_shm_pool *pool = NULL;
  96. int ret;
  97. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  98. if (!pool) {
  99. ret = -ENOMEM;
  100. goto err;
  101. }
  102. /*
  103. * Create the pool for driver private shared memory
  104. */
  105. ret = pool_res_mem_mgr_init(&pool->private_mgr, priv_info,
  106. 3 /* 8 byte aligned */);
  107. if (ret)
  108. goto err;
  109. /*
  110. * Create the pool for dma_buf shared memory
  111. */
  112. ret = pool_res_mem_mgr_init(&pool->dma_buf_mgr, dmabuf_info,
  113. PAGE_SHIFT);
  114. if (ret)
  115. goto err;
  116. pool->destroy = pool_res_mem_destroy;
  117. return pool;
  118. err:
  119. if (ret == -ENOMEM)
  120. pr_err("%s: can't allocate memory for res_mem shared memory pool\n", __func__);
  121. if (pool && pool->private_mgr.private_data)
  122. gen_pool_destroy(pool->private_mgr.private_data);
  123. kfree(pool);
  124. return ERR_PTR(ret);
  125. }
  126. EXPORT_SYMBOL_GPL(tee_shm_pool_alloc_res_mem);
  127. /**
  128. * tee_shm_pool_free() - Free a shared memory pool
  129. * @pool: The shared memory pool to free
  130. *
  131. * There must be no remaining shared memory allocated from this pool when
  132. * this function is called.
  133. */
  134. void tee_shm_pool_free(struct tee_shm_pool *pool)
  135. {
  136. pool->destroy(pool);
  137. kfree(pool);
  138. }
  139. EXPORT_SYMBOL_GPL(tee_shm_pool_free);