protmem.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2025, Linaro Limited
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/errno.h>
  7. #include <linux/genalloc.h>
  8. #include <linux/slab.h>
  9. #include <linux/string.h>
  10. #include <linux/tee_core.h>
  11. #include <linux/types.h>
  12. #include "optee_private.h"
  13. struct optee_protmem_dyn_pool {
  14. struct tee_protmem_pool pool;
  15. struct gen_pool *gen_pool;
  16. struct optee *optee;
  17. size_t page_count;
  18. u32 *mem_attrs;
  19. u_int mem_attr_count;
  20. refcount_t refcount;
  21. u32 use_case;
  22. struct tee_shm *protmem;
  23. /* Protects when initializing and tearing down this struct */
  24. struct mutex mutex;
  25. };
  26. static struct optee_protmem_dyn_pool *
  27. to_protmem_dyn_pool(struct tee_protmem_pool *pool)
  28. {
  29. return container_of(pool, struct optee_protmem_dyn_pool, pool);
  30. }
  31. static int init_dyn_protmem(struct optee_protmem_dyn_pool *rp)
  32. {
  33. int rc;
  34. rp->protmem = tee_shm_alloc_dma_mem(rp->optee->ctx, rp->page_count);
  35. if (IS_ERR(rp->protmem)) {
  36. rc = PTR_ERR(rp->protmem);
  37. goto err_null_protmem;
  38. }
  39. /*
  40. * TODO unmap the memory range since the physical memory will
  41. * become inaccesible after the lend_protmem() call.
  42. *
  43. * If the platform supports a hypervisor at EL2, it will unmap the
  44. * intermediate physical memory for us and stop cache pre-fetch of
  45. * the memory.
  46. */
  47. rc = rp->optee->ops->lend_protmem(rp->optee, rp->protmem,
  48. rp->mem_attrs,
  49. rp->mem_attr_count, rp->use_case);
  50. if (rc)
  51. goto err_put_shm;
  52. rp->protmem->flags |= TEE_SHM_DYNAMIC;
  53. rp->gen_pool = gen_pool_create(PAGE_SHIFT, -1);
  54. if (!rp->gen_pool) {
  55. rc = -ENOMEM;
  56. goto err_reclaim;
  57. }
  58. rc = gen_pool_add(rp->gen_pool, rp->protmem->paddr,
  59. rp->protmem->size, -1);
  60. if (rc)
  61. goto err_free_pool;
  62. refcount_set(&rp->refcount, 1);
  63. return 0;
  64. err_free_pool:
  65. gen_pool_destroy(rp->gen_pool);
  66. rp->gen_pool = NULL;
  67. err_reclaim:
  68. rp->optee->ops->reclaim_protmem(rp->optee, rp->protmem);
  69. err_put_shm:
  70. tee_shm_put(rp->protmem);
  71. err_null_protmem:
  72. rp->protmem = NULL;
  73. return rc;
  74. }
  75. static int get_dyn_protmem(struct optee_protmem_dyn_pool *rp)
  76. {
  77. int rc = 0;
  78. if (!refcount_inc_not_zero(&rp->refcount)) {
  79. mutex_lock(&rp->mutex);
  80. if (rp->gen_pool) {
  81. /*
  82. * Another thread has already initialized the pool
  83. * before us, or the pool was just about to be torn
  84. * down. Either way we only need to increase the
  85. * refcount and we're done.
  86. */
  87. refcount_inc(&rp->refcount);
  88. } else {
  89. rc = init_dyn_protmem(rp);
  90. }
  91. mutex_unlock(&rp->mutex);
  92. }
  93. return rc;
  94. }
  95. static void release_dyn_protmem(struct optee_protmem_dyn_pool *rp)
  96. {
  97. gen_pool_destroy(rp->gen_pool);
  98. rp->gen_pool = NULL;
  99. rp->optee->ops->reclaim_protmem(rp->optee, rp->protmem);
  100. rp->protmem->flags &= ~TEE_SHM_DYNAMIC;
  101. WARN(refcount_read(&rp->protmem->refcount) != 1, "Unexpected refcount");
  102. tee_shm_put(rp->protmem);
  103. rp->protmem = NULL;
  104. }
  105. static void put_dyn_protmem(struct optee_protmem_dyn_pool *rp)
  106. {
  107. if (refcount_dec_and_test(&rp->refcount)) {
  108. mutex_lock(&rp->mutex);
  109. if (rp->gen_pool)
  110. release_dyn_protmem(rp);
  111. mutex_unlock(&rp->mutex);
  112. }
  113. }
  114. static int protmem_pool_op_dyn_alloc(struct tee_protmem_pool *pool,
  115. struct sg_table *sgt, size_t size,
  116. size_t *offs)
  117. {
  118. struct optee_protmem_dyn_pool *rp = to_protmem_dyn_pool(pool);
  119. size_t sz = ALIGN(size, PAGE_SIZE);
  120. phys_addr_t pa;
  121. int rc;
  122. rc = get_dyn_protmem(rp);
  123. if (rc)
  124. return rc;
  125. pa = gen_pool_alloc(rp->gen_pool, sz);
  126. if (!pa) {
  127. rc = -ENOMEM;
  128. goto err_put;
  129. }
  130. rc = sg_alloc_table(sgt, 1, GFP_KERNEL);
  131. if (rc)
  132. goto err_free;
  133. sg_set_page(sgt->sgl, phys_to_page(pa), size, 0);
  134. *offs = pa - rp->protmem->paddr;
  135. return 0;
  136. err_free:
  137. gen_pool_free(rp->gen_pool, pa, size);
  138. err_put:
  139. put_dyn_protmem(rp);
  140. return rc;
  141. }
  142. static void protmem_pool_op_dyn_free(struct tee_protmem_pool *pool,
  143. struct sg_table *sgt)
  144. {
  145. struct optee_protmem_dyn_pool *rp = to_protmem_dyn_pool(pool);
  146. struct scatterlist *sg;
  147. int i;
  148. for_each_sgtable_sg(sgt, sg, i)
  149. gen_pool_free(rp->gen_pool, sg_phys(sg), sg->length);
  150. sg_free_table(sgt);
  151. put_dyn_protmem(rp);
  152. }
  153. static int protmem_pool_op_dyn_update_shm(struct tee_protmem_pool *pool,
  154. struct sg_table *sgt, size_t offs,
  155. struct tee_shm *shm,
  156. struct tee_shm **parent_shm)
  157. {
  158. struct optee_protmem_dyn_pool *rp = to_protmem_dyn_pool(pool);
  159. *parent_shm = rp->protmem;
  160. return 0;
  161. }
  162. static void pool_op_dyn_destroy_pool(struct tee_protmem_pool *pool)
  163. {
  164. struct optee_protmem_dyn_pool *rp = to_protmem_dyn_pool(pool);
  165. mutex_destroy(&rp->mutex);
  166. kfree(rp);
  167. }
  168. static struct tee_protmem_pool_ops protmem_pool_ops_dyn = {
  169. .alloc = protmem_pool_op_dyn_alloc,
  170. .free = protmem_pool_op_dyn_free,
  171. .update_shm = protmem_pool_op_dyn_update_shm,
  172. .destroy_pool = pool_op_dyn_destroy_pool,
  173. };
  174. static int get_protmem_config(struct optee *optee, u32 use_case,
  175. size_t *min_size, u_int *pa_width,
  176. u32 *mem_attrs, u_int *ma_count)
  177. {
  178. struct tee_param params[2] = {
  179. [0] = {
  180. .attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT,
  181. .u.value.a = use_case,
  182. },
  183. [1] = {
  184. .attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT,
  185. },
  186. };
  187. struct optee_shm_arg_entry *entry;
  188. struct tee_shm *shm_param = NULL;
  189. struct optee_msg_arg *msg_arg;
  190. struct tee_shm *shm;
  191. u_int offs;
  192. int rc;
  193. if (mem_attrs && *ma_count) {
  194. params[1].u.memref.size = *ma_count * sizeof(*mem_attrs);
  195. shm_param = tee_shm_alloc_priv_buf(optee->ctx,
  196. params[1].u.memref.size);
  197. if (IS_ERR(shm_param))
  198. return PTR_ERR(shm_param);
  199. params[1].u.memref.shm = shm_param;
  200. }
  201. msg_arg = optee_get_msg_arg(optee->ctx, ARRAY_SIZE(params), &entry,
  202. &shm, &offs);
  203. if (IS_ERR(msg_arg)) {
  204. rc = PTR_ERR(msg_arg);
  205. goto out_free_shm;
  206. }
  207. msg_arg->cmd = OPTEE_MSG_CMD_GET_PROTMEM_CONFIG;
  208. rc = optee->ops->to_msg_param(optee, msg_arg->params,
  209. ARRAY_SIZE(params), params);
  210. if (rc)
  211. goto out_free_msg;
  212. rc = optee->ops->do_call_with_arg(optee->ctx, shm, offs, false);
  213. if (rc)
  214. goto out_free_msg;
  215. if (msg_arg->ret && msg_arg->ret != TEEC_ERROR_SHORT_BUFFER) {
  216. rc = -EINVAL;
  217. goto out_free_msg;
  218. }
  219. rc = optee->ops->from_msg_param(optee, params, ARRAY_SIZE(params),
  220. msg_arg->params);
  221. if (rc)
  222. goto out_free_msg;
  223. if (!msg_arg->ret && mem_attrs &&
  224. *ma_count < params[1].u.memref.size / sizeof(*mem_attrs)) {
  225. rc = -EINVAL;
  226. goto out_free_msg;
  227. }
  228. *min_size = params[0].u.value.a;
  229. *pa_width = params[0].u.value.c;
  230. *ma_count = params[1].u.memref.size / sizeof(*mem_attrs);
  231. if (msg_arg->ret == TEEC_ERROR_SHORT_BUFFER) {
  232. rc = -ENOSPC;
  233. goto out_free_msg;
  234. }
  235. if (mem_attrs)
  236. memcpy(mem_attrs, tee_shm_get_va(shm_param, 0),
  237. params[1].u.memref.size);
  238. out_free_msg:
  239. optee_free_msg_arg(optee->ctx, entry, offs);
  240. out_free_shm:
  241. if (shm_param)
  242. tee_shm_free(shm_param);
  243. return rc;
  244. }
  245. struct tee_protmem_pool *optee_protmem_alloc_dyn_pool(struct optee *optee,
  246. enum tee_dma_heap_id id)
  247. {
  248. struct optee_protmem_dyn_pool *rp;
  249. size_t min_size;
  250. u_int pa_width;
  251. int rc;
  252. rp = kzalloc_obj(*rp);
  253. if (!rp)
  254. return ERR_PTR(-ENOMEM);
  255. rp->use_case = id;
  256. rc = get_protmem_config(optee, id, &min_size, &pa_width, NULL,
  257. &rp->mem_attr_count);
  258. if (rc) {
  259. if (rc != -ENOSPC)
  260. goto err;
  261. rp->mem_attrs = kcalloc(rp->mem_attr_count,
  262. sizeof(*rp->mem_attrs), GFP_KERNEL);
  263. if (!rp->mem_attrs) {
  264. rc = -ENOMEM;
  265. goto err;
  266. }
  267. rc = get_protmem_config(optee, id, &min_size, &pa_width,
  268. rp->mem_attrs, &rp->mem_attr_count);
  269. if (rc)
  270. goto err_kfree_eps;
  271. }
  272. rc = optee_set_dma_mask(optee, pa_width);
  273. if (rc)
  274. goto err_kfree_eps;
  275. rp->pool.ops = &protmem_pool_ops_dyn;
  276. rp->optee = optee;
  277. rp->page_count = min_size / PAGE_SIZE;
  278. mutex_init(&rp->mutex);
  279. return &rp->pool;
  280. err_kfree_eps:
  281. kfree(rp->mem_attrs);
  282. err:
  283. kfree(rp);
  284. return ERR_PTR(rc);
  285. }