tee_shm_pool.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 void pool_op_gen_destroy_poolmgr(struct tee_shm_pool_mgr *poolm)
  43. {
  44. gen_pool_destroy(poolm->private_data);
  45. kfree(poolm);
  46. }
  47. static const struct tee_shm_pool_mgr_ops pool_ops_generic = {
  48. .alloc = pool_op_gen_alloc,
  49. .free = pool_op_gen_free,
  50. .destroy_poolmgr = pool_op_gen_destroy_poolmgr,
  51. };
  52. /**
  53. * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved
  54. * memory range
  55. * @priv_info: Information for driver private shared memory pool
  56. * @dmabuf_info: Information for dma-buf shared memory pool
  57. *
  58. * Start and end of pools will must be page aligned.
  59. *
  60. * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
  61. * in @dmabuf, others will use the range provided by @priv.
  62. *
  63. * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
  64. */
  65. struct tee_shm_pool *
  66. tee_shm_pool_alloc_res_mem(struct tee_shm_pool_mem_info *priv_info,
  67. struct tee_shm_pool_mem_info *dmabuf_info)
  68. {
  69. struct tee_shm_pool_mgr *priv_mgr;
  70. struct tee_shm_pool_mgr *dmabuf_mgr;
  71. void *rc;
  72. /*
  73. * Create the pool for driver private shared memory
  74. */
  75. rc = tee_shm_pool_mgr_alloc_res_mem(priv_info->vaddr, priv_info->paddr,
  76. priv_info->size,
  77. 3 /* 8 byte aligned */);
  78. if (IS_ERR(rc))
  79. return rc;
  80. priv_mgr = rc;
  81. /*
  82. * Create the pool for dma_buf shared memory
  83. */
  84. rc = tee_shm_pool_mgr_alloc_res_mem(dmabuf_info->vaddr,
  85. dmabuf_info->paddr,
  86. dmabuf_info->size, PAGE_SHIFT);
  87. if (IS_ERR(rc))
  88. goto err_free_priv_mgr;
  89. dmabuf_mgr = rc;
  90. rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr);
  91. if (IS_ERR(rc))
  92. goto err_free_dmabuf_mgr;
  93. return rc;
  94. err_free_dmabuf_mgr:
  95. tee_shm_pool_mgr_destroy(dmabuf_mgr);
  96. err_free_priv_mgr:
  97. tee_shm_pool_mgr_destroy(priv_mgr);
  98. return rc;
  99. }
  100. EXPORT_SYMBOL_GPL(tee_shm_pool_alloc_res_mem);
  101. struct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vaddr,
  102. phys_addr_t paddr,
  103. size_t size,
  104. int min_alloc_order)
  105. {
  106. const size_t page_mask = PAGE_SIZE - 1;
  107. struct tee_shm_pool_mgr *mgr;
  108. int rc;
  109. /* Start and end must be page aligned */
  110. if (vaddr & page_mask || paddr & page_mask || size & page_mask)
  111. return ERR_PTR(-EINVAL);
  112. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  113. if (!mgr)
  114. return ERR_PTR(-ENOMEM);
  115. mgr->private_data = gen_pool_create(min_alloc_order, -1);
  116. if (!mgr->private_data) {
  117. rc = -ENOMEM;
  118. goto err;
  119. }
  120. gen_pool_set_algo(mgr->private_data, gen_pool_best_fit, NULL);
  121. rc = gen_pool_add_virt(mgr->private_data, vaddr, paddr, size, -1);
  122. if (rc) {
  123. gen_pool_destroy(mgr->private_data);
  124. goto err;
  125. }
  126. mgr->ops = &pool_ops_generic;
  127. return mgr;
  128. err:
  129. kfree(mgr);
  130. return ERR_PTR(rc);
  131. }
  132. EXPORT_SYMBOL_GPL(tee_shm_pool_mgr_alloc_res_mem);
  133. static bool check_mgr_ops(struct tee_shm_pool_mgr *mgr)
  134. {
  135. return mgr && mgr->ops && mgr->ops->alloc && mgr->ops->free &&
  136. mgr->ops->destroy_poolmgr;
  137. }
  138. struct tee_shm_pool *tee_shm_pool_alloc(struct tee_shm_pool_mgr *priv_mgr,
  139. struct tee_shm_pool_mgr *dmabuf_mgr)
  140. {
  141. struct tee_shm_pool *pool;
  142. if (!check_mgr_ops(priv_mgr) || !check_mgr_ops(dmabuf_mgr))
  143. return ERR_PTR(-EINVAL);
  144. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  145. if (!pool)
  146. return ERR_PTR(-ENOMEM);
  147. pool->private_mgr = priv_mgr;
  148. pool->dma_buf_mgr = dmabuf_mgr;
  149. return pool;
  150. }
  151. EXPORT_SYMBOL_GPL(tee_shm_pool_alloc);
  152. /**
  153. * tee_shm_pool_free() - Free a shared memory pool
  154. * @pool: The shared memory pool to free
  155. *
  156. * There must be no remaining shared memory allocated from this pool when
  157. * this function is called.
  158. */
  159. void tee_shm_pool_free(struct tee_shm_pool *pool)
  160. {
  161. if (pool->private_mgr)
  162. tee_shm_pool_mgr_destroy(pool->private_mgr);
  163. if (pool->dma_buf_mgr)
  164. tee_shm_pool_mgr_destroy(pool->dma_buf_mgr);
  165. kfree(pool);
  166. }
  167. EXPORT_SYMBOL_GPL(tee_shm_pool_free);