tee_shm.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2015-2016, 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/fdtable.h>
  17. #include <linux/idr.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/tee_drv.h>
  21. #include "tee_private.h"
  22. static void tee_shm_release(struct tee_shm *shm)
  23. {
  24. struct tee_device *teedev = shm->teedev;
  25. struct tee_shm_pool_mgr *poolm;
  26. mutex_lock(&teedev->mutex);
  27. idr_remove(&teedev->idr, shm->id);
  28. if (shm->ctx)
  29. list_del(&shm->link);
  30. mutex_unlock(&teedev->mutex);
  31. if (shm->flags & TEE_SHM_DMA_BUF)
  32. poolm = &teedev->pool->dma_buf_mgr;
  33. else
  34. poolm = &teedev->pool->private_mgr;
  35. poolm->ops->free(poolm, shm);
  36. kfree(shm);
  37. tee_device_put(teedev);
  38. }
  39. static struct sg_table *tee_shm_op_map_dma_buf(struct dma_buf_attachment
  40. *attach, enum dma_data_direction dir)
  41. {
  42. return NULL;
  43. }
  44. static void tee_shm_op_unmap_dma_buf(struct dma_buf_attachment *attach,
  45. struct sg_table *table,
  46. enum dma_data_direction dir)
  47. {
  48. }
  49. static void tee_shm_op_release(struct dma_buf *dmabuf)
  50. {
  51. struct tee_shm *shm = dmabuf->priv;
  52. tee_shm_release(shm);
  53. }
  54. static void *tee_shm_op_map_atomic(struct dma_buf *dmabuf, unsigned long pgnum)
  55. {
  56. return NULL;
  57. }
  58. static void *tee_shm_op_map(struct dma_buf *dmabuf, unsigned long pgnum)
  59. {
  60. return NULL;
  61. }
  62. static int tee_shm_op_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
  63. {
  64. struct tee_shm *shm = dmabuf->priv;
  65. size_t size = vma->vm_end - vma->vm_start;
  66. return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
  67. size, vma->vm_page_prot);
  68. }
  69. static const struct dma_buf_ops tee_shm_dma_buf_ops = {
  70. .map_dma_buf = tee_shm_op_map_dma_buf,
  71. .unmap_dma_buf = tee_shm_op_unmap_dma_buf,
  72. .release = tee_shm_op_release,
  73. .map_atomic = tee_shm_op_map_atomic,
  74. .map = tee_shm_op_map,
  75. .mmap = tee_shm_op_mmap,
  76. };
  77. /**
  78. * tee_shm_alloc() - Allocate shared memory
  79. * @ctx: Context that allocates the shared memory
  80. * @size: Requested size of shared memory
  81. * @flags: Flags setting properties for the requested shared memory.
  82. *
  83. * Memory allocated as global shared memory is automatically freed when the
  84. * TEE file pointer is closed. The @flags field uses the bits defined by
  85. * TEE_SHM_* in <linux/tee_drv.h>. TEE_SHM_MAPPED must currently always be
  86. * set. If TEE_SHM_DMA_BUF global shared memory will be allocated and
  87. * associated with a dma-buf handle, else driver private memory.
  88. */
  89. struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags)
  90. {
  91. struct tee_device *teedev = ctx->teedev;
  92. struct tee_shm_pool_mgr *poolm = NULL;
  93. struct tee_shm *shm;
  94. void *ret;
  95. int rc;
  96. if (!(flags & TEE_SHM_MAPPED)) {
  97. dev_err(teedev->dev.parent,
  98. "only mapped allocations supported\n");
  99. return ERR_PTR(-EINVAL);
  100. }
  101. if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF))) {
  102. dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags);
  103. return ERR_PTR(-EINVAL);
  104. }
  105. if (!tee_device_get(teedev))
  106. return ERR_PTR(-EINVAL);
  107. if (!teedev->pool) {
  108. /* teedev has been detached from driver */
  109. ret = ERR_PTR(-EINVAL);
  110. goto err_dev_put;
  111. }
  112. shm = kzalloc(sizeof(*shm), GFP_KERNEL);
  113. if (!shm) {
  114. ret = ERR_PTR(-ENOMEM);
  115. goto err_dev_put;
  116. }
  117. shm->flags = flags;
  118. shm->teedev = teedev;
  119. shm->ctx = ctx;
  120. if (flags & TEE_SHM_DMA_BUF)
  121. poolm = &teedev->pool->dma_buf_mgr;
  122. else
  123. poolm = &teedev->pool->private_mgr;
  124. rc = poolm->ops->alloc(poolm, shm, size);
  125. if (rc) {
  126. ret = ERR_PTR(rc);
  127. goto err_kfree;
  128. }
  129. mutex_lock(&teedev->mutex);
  130. shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
  131. mutex_unlock(&teedev->mutex);
  132. if (shm->id < 0) {
  133. ret = ERR_PTR(shm->id);
  134. goto err_pool_free;
  135. }
  136. if (flags & TEE_SHM_DMA_BUF) {
  137. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  138. exp_info.ops = &tee_shm_dma_buf_ops;
  139. exp_info.size = shm->size;
  140. exp_info.flags = O_RDWR;
  141. exp_info.priv = shm;
  142. shm->dmabuf = dma_buf_export(&exp_info);
  143. if (IS_ERR(shm->dmabuf)) {
  144. ret = ERR_CAST(shm->dmabuf);
  145. goto err_rem;
  146. }
  147. }
  148. mutex_lock(&teedev->mutex);
  149. list_add_tail(&shm->link, &ctx->list_shm);
  150. mutex_unlock(&teedev->mutex);
  151. return shm;
  152. err_rem:
  153. mutex_lock(&teedev->mutex);
  154. idr_remove(&teedev->idr, shm->id);
  155. mutex_unlock(&teedev->mutex);
  156. err_pool_free:
  157. poolm->ops->free(poolm, shm);
  158. err_kfree:
  159. kfree(shm);
  160. err_dev_put:
  161. tee_device_put(teedev);
  162. return ret;
  163. }
  164. EXPORT_SYMBOL_GPL(tee_shm_alloc);
  165. /**
  166. * tee_shm_get_fd() - Increase reference count and return file descriptor
  167. * @shm: Shared memory handle
  168. * @returns user space file descriptor to shared memory
  169. */
  170. int tee_shm_get_fd(struct tee_shm *shm)
  171. {
  172. u32 req_flags = TEE_SHM_MAPPED | TEE_SHM_DMA_BUF;
  173. int fd;
  174. if ((shm->flags & req_flags) != req_flags)
  175. return -EINVAL;
  176. get_dma_buf(shm->dmabuf);
  177. fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC);
  178. if (fd < 0)
  179. dma_buf_put(shm->dmabuf);
  180. return fd;
  181. }
  182. /**
  183. * tee_shm_free() - Free shared memory
  184. * @shm: Handle to shared memory to free
  185. */
  186. void tee_shm_free(struct tee_shm *shm)
  187. {
  188. /*
  189. * dma_buf_put() decreases the dmabuf reference counter and will
  190. * call tee_shm_release() when the last reference is gone.
  191. *
  192. * In the case of driver private memory we call tee_shm_release
  193. * directly instead as it doesn't have a reference counter.
  194. */
  195. if (shm->flags & TEE_SHM_DMA_BUF)
  196. dma_buf_put(shm->dmabuf);
  197. else
  198. tee_shm_release(shm);
  199. }
  200. EXPORT_SYMBOL_GPL(tee_shm_free);
  201. /**
  202. * tee_shm_va2pa() - Get physical address of a virtual address
  203. * @shm: Shared memory handle
  204. * @va: Virtual address to tranlsate
  205. * @pa: Returned physical address
  206. * @returns 0 on success and < 0 on failure
  207. */
  208. int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa)
  209. {
  210. /* Check that we're in the range of the shm */
  211. if ((char *)va < (char *)shm->kaddr)
  212. return -EINVAL;
  213. if ((char *)va >= ((char *)shm->kaddr + shm->size))
  214. return -EINVAL;
  215. return tee_shm_get_pa(
  216. shm, (unsigned long)va - (unsigned long)shm->kaddr, pa);
  217. }
  218. EXPORT_SYMBOL_GPL(tee_shm_va2pa);
  219. /**
  220. * tee_shm_pa2va() - Get virtual address of a physical address
  221. * @shm: Shared memory handle
  222. * @pa: Physical address to tranlsate
  223. * @va: Returned virtual address
  224. * @returns 0 on success and < 0 on failure
  225. */
  226. int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va)
  227. {
  228. /* Check that we're in the range of the shm */
  229. if (pa < shm->paddr)
  230. return -EINVAL;
  231. if (pa >= (shm->paddr + shm->size))
  232. return -EINVAL;
  233. if (va) {
  234. void *v = tee_shm_get_va(shm, pa - shm->paddr);
  235. if (IS_ERR(v))
  236. return PTR_ERR(v);
  237. *va = v;
  238. }
  239. return 0;
  240. }
  241. EXPORT_SYMBOL_GPL(tee_shm_pa2va);
  242. /**
  243. * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
  244. * @shm: Shared memory handle
  245. * @offs: Offset from start of this shared memory
  246. * @returns virtual address of the shared memory + offs if offs is within
  247. * the bounds of this shared memory, else an ERR_PTR
  248. */
  249. void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
  250. {
  251. if (offs >= shm->size)
  252. return ERR_PTR(-EINVAL);
  253. return (char *)shm->kaddr + offs;
  254. }
  255. EXPORT_SYMBOL_GPL(tee_shm_get_va);
  256. /**
  257. * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
  258. * @shm: Shared memory handle
  259. * @offs: Offset from start of this shared memory
  260. * @pa: Physical address to return
  261. * @returns 0 if offs is within the bounds of this shared memory, else an
  262. * error code.
  263. */
  264. int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
  265. {
  266. if (offs >= shm->size)
  267. return -EINVAL;
  268. if (pa)
  269. *pa = shm->paddr + offs;
  270. return 0;
  271. }
  272. EXPORT_SYMBOL_GPL(tee_shm_get_pa);
  273. /**
  274. * tee_shm_get_from_id() - Find shared memory object and increase reference
  275. * count
  276. * @ctx: Context owning the shared memory
  277. * @id: Id of shared memory object
  278. * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
  279. */
  280. struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
  281. {
  282. struct tee_device *teedev;
  283. struct tee_shm *shm;
  284. if (!ctx)
  285. return ERR_PTR(-EINVAL);
  286. teedev = ctx->teedev;
  287. mutex_lock(&teedev->mutex);
  288. shm = idr_find(&teedev->idr, id);
  289. if (!shm || shm->ctx != ctx)
  290. shm = ERR_PTR(-EINVAL);
  291. else if (shm->flags & TEE_SHM_DMA_BUF)
  292. get_dma_buf(shm->dmabuf);
  293. mutex_unlock(&teedev->mutex);
  294. return shm;
  295. }
  296. EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
  297. /**
  298. * tee_shm_get_id() - Get id of a shared memory object
  299. * @shm: Shared memory handle
  300. * @returns id
  301. */
  302. int tee_shm_get_id(struct tee_shm *shm)
  303. {
  304. return shm->id;
  305. }
  306. EXPORT_SYMBOL_GPL(tee_shm_get_id);
  307. /**
  308. * tee_shm_put() - Decrease reference count on a shared memory handle
  309. * @shm: Shared memory handle
  310. */
  311. void tee_shm_put(struct tee_shm *shm)
  312. {
  313. if (shm->flags & TEE_SHM_DMA_BUF)
  314. dma_buf_put(shm->dmabuf);
  315. }
  316. EXPORT_SYMBOL_GPL(tee_shm_put);